rseq-ppc.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2. /*
  3. * rseq-ppc.h
  4. *
  5. * (C) Copyright 2016-2018 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  6. * (C) Copyright 2016-2018 - Boqun Feng <boqun.feng@gmail.com>
  7. */
  8. #define RSEQ_SIG 0x53053053
  9. #define rseq_smp_mb() __asm__ __volatile__ ("sync" ::: "memory", "cc")
  10. #define rseq_smp_lwsync() __asm__ __volatile__ ("lwsync" ::: "memory", "cc")
  11. #define rseq_smp_rmb() rseq_smp_lwsync()
  12. #define rseq_smp_wmb() rseq_smp_lwsync()
  13. #define rseq_smp_load_acquire(p) \
  14. __extension__ ({ \
  15. __typeof(*p) ____p1 = RSEQ_READ_ONCE(*p); \
  16. rseq_smp_lwsync(); \
  17. ____p1; \
  18. })
  19. #define rseq_smp_acquire__after_ctrl_dep() rseq_smp_lwsync()
  20. #define rseq_smp_store_release(p, v) \
  21. do { \
  22. rseq_smp_lwsync(); \
  23. RSEQ_WRITE_ONCE(*p, v); \
  24. } while (0)
  25. #ifdef RSEQ_SKIP_FASTPATH
  26. #include "rseq-skip.h"
  27. #else /* !RSEQ_SKIP_FASTPATH */
  28. /*
  29. * The __rseq_table section can be used by debuggers to better handle
  30. * single-stepping through the restartable critical sections.
  31. */
  32. #ifdef __PPC64__
  33. #define STORE_WORD "std "
  34. #define LOAD_WORD "ld "
  35. #define LOADX_WORD "ldx "
  36. #define CMP_WORD "cmpd "
  37. #define __RSEQ_ASM_DEFINE_TABLE(label, version, flags, \
  38. start_ip, post_commit_offset, abort_ip) \
  39. ".pushsection __rseq_table, \"aw\"\n\t" \
  40. ".balign 32\n\t" \
  41. __rseq_str(label) ":\n\t" \
  42. ".long " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \
  43. ".quad " __rseq_str(start_ip) ", " __rseq_str(post_commit_offset) ", " __rseq_str(abort_ip) "\n\t" \
  44. ".popsection\n\t"
  45. #define RSEQ_ASM_STORE_RSEQ_CS(label, cs_label, rseq_cs) \
  46. RSEQ_INJECT_ASM(1) \
  47. "lis %%r17, (" __rseq_str(cs_label) ")@highest\n\t" \
  48. "ori %%r17, %%r17, (" __rseq_str(cs_label) ")@higher\n\t" \
  49. "rldicr %%r17, %%r17, 32, 31\n\t" \
  50. "oris %%r17, %%r17, (" __rseq_str(cs_label) ")@high\n\t" \
  51. "ori %%r17, %%r17, (" __rseq_str(cs_label) ")@l\n\t" \
  52. "std %%r17, %[" __rseq_str(rseq_cs) "]\n\t" \
  53. __rseq_str(label) ":\n\t"
  54. #else /* #ifdef __PPC64__ */
  55. #define STORE_WORD "stw "
  56. #define LOAD_WORD "lwz "
  57. #define LOADX_WORD "lwzx "
  58. #define CMP_WORD "cmpw "
  59. #define __RSEQ_ASM_DEFINE_TABLE(label, version, flags, \
  60. start_ip, post_commit_offset, abort_ip) \
  61. ".pushsection __rseq_table, \"aw\"\n\t" \
  62. ".balign 32\n\t" \
  63. __rseq_str(label) ":\n\t" \
  64. ".long " __rseq_str(version) ", " __rseq_str(flags) "\n\t" \
  65. /* 32-bit only supported on BE */ \
  66. ".long 0x0, " __rseq_str(start_ip) ", 0x0, " __rseq_str(post_commit_offset) ", 0x0, " __rseq_str(abort_ip) "\n\t" \
  67. ".popsection\n\t"
  68. #define RSEQ_ASM_STORE_RSEQ_CS(label, cs_label, rseq_cs) \
  69. RSEQ_INJECT_ASM(1) \
  70. "lis %%r17, (" __rseq_str(cs_label) ")@ha\n\t" \
  71. "addi %%r17, %%r17, (" __rseq_str(cs_label) ")@l\n\t" \
  72. "stw %%r17, %[" __rseq_str(rseq_cs) "]\n\t" \
  73. __rseq_str(label) ":\n\t"
  74. #endif /* #ifdef __PPC64__ */
  75. #define RSEQ_ASM_DEFINE_TABLE(label, start_ip, post_commit_ip, abort_ip) \
  76. __RSEQ_ASM_DEFINE_TABLE(label, 0x0, 0x0, start_ip, \
  77. (post_commit_ip - start_ip), abort_ip)
  78. #define RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, label) \
  79. RSEQ_INJECT_ASM(2) \
  80. "lwz %%r17, %[" __rseq_str(current_cpu_id) "]\n\t" \
  81. "cmpw cr7, %[" __rseq_str(cpu_id) "], %%r17\n\t" \
  82. "bne- cr7, " __rseq_str(label) "\n\t"
  83. #define RSEQ_ASM_DEFINE_ABORT(label, abort_label) \
  84. ".pushsection __rseq_failure, \"ax\"\n\t" \
  85. ".long " __rseq_str(RSEQ_SIG) "\n\t" \
  86. __rseq_str(label) ":\n\t" \
  87. "b %l[" __rseq_str(abort_label) "]\n\t" \
  88. ".popsection\n\t"
  89. /*
  90. * RSEQ_ASM_OPs: asm operations for rseq
  91. * RSEQ_ASM_OP_R_*: has hard-code registers in it
  92. * RSEQ_ASM_OP_* (else): doesn't have hard-code registers(unless cr7)
  93. */
  94. #define RSEQ_ASM_OP_CMPEQ(var, expect, label) \
  95. LOAD_WORD "%%r17, %[" __rseq_str(var) "]\n\t" \
  96. CMP_WORD "cr7, %%r17, %[" __rseq_str(expect) "]\n\t" \
  97. "bne- cr7, " __rseq_str(label) "\n\t"
  98. #define RSEQ_ASM_OP_CMPNE(var, expectnot, label) \
  99. LOAD_WORD "%%r17, %[" __rseq_str(var) "]\n\t" \
  100. CMP_WORD "cr7, %%r17, %[" __rseq_str(expectnot) "]\n\t" \
  101. "beq- cr7, " __rseq_str(label) "\n\t"
  102. #define RSEQ_ASM_OP_STORE(value, var) \
  103. STORE_WORD "%[" __rseq_str(value) "], %[" __rseq_str(var) "]\n\t"
  104. /* Load @var to r17 */
  105. #define RSEQ_ASM_OP_R_LOAD(var) \
  106. LOAD_WORD "%%r17, %[" __rseq_str(var) "]\n\t"
  107. /* Store r17 to @var */
  108. #define RSEQ_ASM_OP_R_STORE(var) \
  109. STORE_WORD "%%r17, %[" __rseq_str(var) "]\n\t"
  110. /* Add @count to r17 */
  111. #define RSEQ_ASM_OP_R_ADD(count) \
  112. "add %%r17, %[" __rseq_str(count) "], %%r17\n\t"
  113. /* Load (r17 + voffp) to r17 */
  114. #define RSEQ_ASM_OP_R_LOADX(voffp) \
  115. LOADX_WORD "%%r17, %[" __rseq_str(voffp) "], %%r17\n\t"
  116. /* TODO: implement a faster memcpy. */
  117. #define RSEQ_ASM_OP_R_MEMCPY() \
  118. "cmpdi %%r19, 0\n\t" \
  119. "beq 333f\n\t" \
  120. "addi %%r20, %%r20, -1\n\t" \
  121. "addi %%r21, %%r21, -1\n\t" \
  122. "222:\n\t" \
  123. "lbzu %%r18, 1(%%r20)\n\t" \
  124. "stbu %%r18, 1(%%r21)\n\t" \
  125. "addi %%r19, %%r19, -1\n\t" \
  126. "cmpdi %%r19, 0\n\t" \
  127. "bne 222b\n\t" \
  128. "333:\n\t" \
  129. #define RSEQ_ASM_OP_R_FINAL_STORE(var, post_commit_label) \
  130. STORE_WORD "%%r17, %[" __rseq_str(var) "]\n\t" \
  131. __rseq_str(post_commit_label) ":\n\t"
  132. #define RSEQ_ASM_OP_FINAL_STORE(value, var, post_commit_label) \
  133. STORE_WORD "%[" __rseq_str(value) "], %[" __rseq_str(var) "]\n\t" \
  134. __rseq_str(post_commit_label) ":\n\t"
  135. static inline __attribute__((always_inline))
  136. int rseq_cmpeqv_storev(intptr_t *v, intptr_t expect, intptr_t newv, int cpu)
  137. {
  138. RSEQ_INJECT_C(9)
  139. __asm__ __volatile__ goto (
  140. RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
  141. /* Start rseq by storing table entry pointer into rseq_cs. */
  142. RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
  143. /* cmp cpuid */
  144. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
  145. RSEQ_INJECT_ASM(3)
  146. /* cmp @v equal to @expect */
  147. RSEQ_ASM_OP_CMPEQ(v, expect, %l[cmpfail])
  148. RSEQ_INJECT_ASM(4)
  149. #ifdef RSEQ_COMPARE_TWICE
  150. /* cmp cpuid */
  151. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, %l[error1])
  152. /* cmp @v equal to @expect */
  153. RSEQ_ASM_OP_CMPEQ(v, expect, %l[error2])
  154. #endif
  155. /* final store */
  156. RSEQ_ASM_OP_FINAL_STORE(newv, v, 2)
  157. RSEQ_INJECT_ASM(5)
  158. RSEQ_ASM_DEFINE_ABORT(4, abort)
  159. : /* gcc asm goto does not allow outputs */
  160. : [cpu_id] "r" (cpu),
  161. [current_cpu_id] "m" (__rseq_abi.cpu_id),
  162. [rseq_cs] "m" (__rseq_abi.rseq_cs),
  163. [v] "m" (*v),
  164. [expect] "r" (expect),
  165. [newv] "r" (newv)
  166. RSEQ_INJECT_INPUT
  167. : "memory", "cc", "r17"
  168. RSEQ_INJECT_CLOBBER
  169. : abort, cmpfail
  170. #ifdef RSEQ_COMPARE_TWICE
  171. , error1, error2
  172. #endif
  173. );
  174. return 0;
  175. abort:
  176. RSEQ_INJECT_FAILED
  177. return -1;
  178. cmpfail:
  179. return 1;
  180. #ifdef RSEQ_COMPARE_TWICE
  181. error1:
  182. rseq_bug("cpu_id comparison failed");
  183. error2:
  184. rseq_bug("expected value comparison failed");
  185. #endif
  186. }
  187. static inline __attribute__((always_inline))
  188. int rseq_cmpnev_storeoffp_load(intptr_t *v, intptr_t expectnot,
  189. off_t voffp, intptr_t *load, int cpu)
  190. {
  191. RSEQ_INJECT_C(9)
  192. __asm__ __volatile__ goto (
  193. RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
  194. /* Start rseq by storing table entry pointer into rseq_cs. */
  195. RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
  196. /* cmp cpuid */
  197. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
  198. RSEQ_INJECT_ASM(3)
  199. /* cmp @v not equal to @expectnot */
  200. RSEQ_ASM_OP_CMPNE(v, expectnot, %l[cmpfail])
  201. RSEQ_INJECT_ASM(4)
  202. #ifdef RSEQ_COMPARE_TWICE
  203. /* cmp cpuid */
  204. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, %l[error1])
  205. /* cmp @v not equal to @expectnot */
  206. RSEQ_ASM_OP_CMPNE(v, expectnot, %l[error2])
  207. #endif
  208. /* load the value of @v */
  209. RSEQ_ASM_OP_R_LOAD(v)
  210. /* store it in @load */
  211. RSEQ_ASM_OP_R_STORE(load)
  212. /* dereference voffp(v) */
  213. RSEQ_ASM_OP_R_LOADX(voffp)
  214. /* final store the value at voffp(v) */
  215. RSEQ_ASM_OP_R_FINAL_STORE(v, 2)
  216. RSEQ_INJECT_ASM(5)
  217. RSEQ_ASM_DEFINE_ABORT(4, abort)
  218. : /* gcc asm goto does not allow outputs */
  219. : [cpu_id] "r" (cpu),
  220. [current_cpu_id] "m" (__rseq_abi.cpu_id),
  221. [rseq_cs] "m" (__rseq_abi.rseq_cs),
  222. /* final store input */
  223. [v] "m" (*v),
  224. [expectnot] "r" (expectnot),
  225. [voffp] "b" (voffp),
  226. [load] "m" (*load)
  227. RSEQ_INJECT_INPUT
  228. : "memory", "cc", "r17"
  229. RSEQ_INJECT_CLOBBER
  230. : abort, cmpfail
  231. #ifdef RSEQ_COMPARE_TWICE
  232. , error1, error2
  233. #endif
  234. );
  235. return 0;
  236. abort:
  237. RSEQ_INJECT_FAILED
  238. return -1;
  239. cmpfail:
  240. return 1;
  241. #ifdef RSEQ_COMPARE_TWICE
  242. error1:
  243. rseq_bug("cpu_id comparison failed");
  244. error2:
  245. rseq_bug("expected value comparison failed");
  246. #endif
  247. }
  248. static inline __attribute__((always_inline))
  249. int rseq_addv(intptr_t *v, intptr_t count, int cpu)
  250. {
  251. RSEQ_INJECT_C(9)
  252. __asm__ __volatile__ goto (
  253. RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
  254. /* Start rseq by storing table entry pointer into rseq_cs. */
  255. RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
  256. /* cmp cpuid */
  257. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
  258. RSEQ_INJECT_ASM(3)
  259. #ifdef RSEQ_COMPARE_TWICE
  260. /* cmp cpuid */
  261. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, %l[error1])
  262. #endif
  263. /* load the value of @v */
  264. RSEQ_ASM_OP_R_LOAD(v)
  265. /* add @count to it */
  266. RSEQ_ASM_OP_R_ADD(count)
  267. /* final store */
  268. RSEQ_ASM_OP_R_FINAL_STORE(v, 2)
  269. RSEQ_INJECT_ASM(4)
  270. RSEQ_ASM_DEFINE_ABORT(4, abort)
  271. : /* gcc asm goto does not allow outputs */
  272. : [cpu_id] "r" (cpu),
  273. [current_cpu_id] "m" (__rseq_abi.cpu_id),
  274. [rseq_cs] "m" (__rseq_abi.rseq_cs),
  275. /* final store input */
  276. [v] "m" (*v),
  277. [count] "r" (count)
  278. RSEQ_INJECT_INPUT
  279. : "memory", "cc", "r17"
  280. RSEQ_INJECT_CLOBBER
  281. : abort
  282. #ifdef RSEQ_COMPARE_TWICE
  283. , error1
  284. #endif
  285. );
  286. return 0;
  287. abort:
  288. RSEQ_INJECT_FAILED
  289. return -1;
  290. #ifdef RSEQ_COMPARE_TWICE
  291. error1:
  292. rseq_bug("cpu_id comparison failed");
  293. #endif
  294. }
  295. static inline __attribute__((always_inline))
  296. int rseq_cmpeqv_trystorev_storev(intptr_t *v, intptr_t expect,
  297. intptr_t *v2, intptr_t newv2,
  298. intptr_t newv, int cpu)
  299. {
  300. RSEQ_INJECT_C(9)
  301. __asm__ __volatile__ goto (
  302. RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
  303. /* Start rseq by storing table entry pointer into rseq_cs. */
  304. RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
  305. /* cmp cpuid */
  306. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
  307. RSEQ_INJECT_ASM(3)
  308. /* cmp @v equal to @expect */
  309. RSEQ_ASM_OP_CMPEQ(v, expect, %l[cmpfail])
  310. RSEQ_INJECT_ASM(4)
  311. #ifdef RSEQ_COMPARE_TWICE
  312. /* cmp cpuid */
  313. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, %l[error1])
  314. /* cmp @v equal to @expect */
  315. RSEQ_ASM_OP_CMPEQ(v, expect, %l[error2])
  316. #endif
  317. /* try store */
  318. RSEQ_ASM_OP_STORE(newv2, v2)
  319. RSEQ_INJECT_ASM(5)
  320. /* final store */
  321. RSEQ_ASM_OP_FINAL_STORE(newv, v, 2)
  322. RSEQ_INJECT_ASM(6)
  323. RSEQ_ASM_DEFINE_ABORT(4, abort)
  324. : /* gcc asm goto does not allow outputs */
  325. : [cpu_id] "r" (cpu),
  326. [current_cpu_id] "m" (__rseq_abi.cpu_id),
  327. [rseq_cs] "m" (__rseq_abi.rseq_cs),
  328. /* try store input */
  329. [v2] "m" (*v2),
  330. [newv2] "r" (newv2),
  331. /* final store input */
  332. [v] "m" (*v),
  333. [expect] "r" (expect),
  334. [newv] "r" (newv)
  335. RSEQ_INJECT_INPUT
  336. : "memory", "cc", "r17"
  337. RSEQ_INJECT_CLOBBER
  338. : abort, cmpfail
  339. #ifdef RSEQ_COMPARE_TWICE
  340. , error1, error2
  341. #endif
  342. );
  343. return 0;
  344. abort:
  345. RSEQ_INJECT_FAILED
  346. return -1;
  347. cmpfail:
  348. return 1;
  349. #ifdef RSEQ_COMPARE_TWICE
  350. error1:
  351. rseq_bug("cpu_id comparison failed");
  352. error2:
  353. rseq_bug("expected value comparison failed");
  354. #endif
  355. }
  356. static inline __attribute__((always_inline))
  357. int rseq_cmpeqv_trystorev_storev_release(intptr_t *v, intptr_t expect,
  358. intptr_t *v2, intptr_t newv2,
  359. intptr_t newv, int cpu)
  360. {
  361. RSEQ_INJECT_C(9)
  362. __asm__ __volatile__ goto (
  363. RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
  364. /* Start rseq by storing table entry pointer into rseq_cs. */
  365. RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
  366. /* cmp cpuid */
  367. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
  368. RSEQ_INJECT_ASM(3)
  369. /* cmp @v equal to @expect */
  370. RSEQ_ASM_OP_CMPEQ(v, expect, %l[cmpfail])
  371. RSEQ_INJECT_ASM(4)
  372. #ifdef RSEQ_COMPARE_TWICE
  373. /* cmp cpuid */
  374. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, %l[error1])
  375. /* cmp @v equal to @expect */
  376. RSEQ_ASM_OP_CMPEQ(v, expect, %l[error2])
  377. #endif
  378. /* try store */
  379. RSEQ_ASM_OP_STORE(newv2, v2)
  380. RSEQ_INJECT_ASM(5)
  381. /* for 'release' */
  382. "lwsync\n\t"
  383. /* final store */
  384. RSEQ_ASM_OP_FINAL_STORE(newv, v, 2)
  385. RSEQ_INJECT_ASM(6)
  386. RSEQ_ASM_DEFINE_ABORT(4, abort)
  387. : /* gcc asm goto does not allow outputs */
  388. : [cpu_id] "r" (cpu),
  389. [current_cpu_id] "m" (__rseq_abi.cpu_id),
  390. [rseq_cs] "m" (__rseq_abi.rseq_cs),
  391. /* try store input */
  392. [v2] "m" (*v2),
  393. [newv2] "r" (newv2),
  394. /* final store input */
  395. [v] "m" (*v),
  396. [expect] "r" (expect),
  397. [newv] "r" (newv)
  398. RSEQ_INJECT_INPUT
  399. : "memory", "cc", "r17"
  400. RSEQ_INJECT_CLOBBER
  401. : abort, cmpfail
  402. #ifdef RSEQ_COMPARE_TWICE
  403. , error1, error2
  404. #endif
  405. );
  406. return 0;
  407. abort:
  408. RSEQ_INJECT_FAILED
  409. return -1;
  410. cmpfail:
  411. return 1;
  412. #ifdef RSEQ_COMPARE_TWICE
  413. error1:
  414. rseq_bug("cpu_id comparison failed");
  415. error2:
  416. rseq_bug("expected value comparison failed");
  417. #endif
  418. }
  419. static inline __attribute__((always_inline))
  420. int rseq_cmpeqv_cmpeqv_storev(intptr_t *v, intptr_t expect,
  421. intptr_t *v2, intptr_t expect2,
  422. intptr_t newv, int cpu)
  423. {
  424. RSEQ_INJECT_C(9)
  425. __asm__ __volatile__ goto (
  426. RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
  427. /* Start rseq by storing table entry pointer into rseq_cs. */
  428. RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
  429. /* cmp cpuid */
  430. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
  431. RSEQ_INJECT_ASM(3)
  432. /* cmp @v equal to @expect */
  433. RSEQ_ASM_OP_CMPEQ(v, expect, %l[cmpfail])
  434. RSEQ_INJECT_ASM(4)
  435. /* cmp @v2 equal to @expct2 */
  436. RSEQ_ASM_OP_CMPEQ(v2, expect2, %l[cmpfail])
  437. RSEQ_INJECT_ASM(5)
  438. #ifdef RSEQ_COMPARE_TWICE
  439. /* cmp cpuid */
  440. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, %l[error1])
  441. /* cmp @v equal to @expect */
  442. RSEQ_ASM_OP_CMPEQ(v, expect, %l[error2])
  443. /* cmp @v2 equal to @expct2 */
  444. RSEQ_ASM_OP_CMPEQ(v2, expect2, %l[error3])
  445. #endif
  446. /* final store */
  447. RSEQ_ASM_OP_FINAL_STORE(newv, v, 2)
  448. RSEQ_INJECT_ASM(6)
  449. RSEQ_ASM_DEFINE_ABORT(4, abort)
  450. : /* gcc asm goto does not allow outputs */
  451. : [cpu_id] "r" (cpu),
  452. [current_cpu_id] "m" (__rseq_abi.cpu_id),
  453. [rseq_cs] "m" (__rseq_abi.rseq_cs),
  454. /* cmp2 input */
  455. [v2] "m" (*v2),
  456. [expect2] "r" (expect2),
  457. /* final store input */
  458. [v] "m" (*v),
  459. [expect] "r" (expect),
  460. [newv] "r" (newv)
  461. RSEQ_INJECT_INPUT
  462. : "memory", "cc", "r17"
  463. RSEQ_INJECT_CLOBBER
  464. : abort, cmpfail
  465. #ifdef RSEQ_COMPARE_TWICE
  466. , error1, error2, error3
  467. #endif
  468. );
  469. return 0;
  470. abort:
  471. RSEQ_INJECT_FAILED
  472. return -1;
  473. cmpfail:
  474. return 1;
  475. #ifdef RSEQ_COMPARE_TWICE
  476. error1:
  477. rseq_bug("cpu_id comparison failed");
  478. error2:
  479. rseq_bug("1st expected value comparison failed");
  480. error3:
  481. rseq_bug("2nd expected value comparison failed");
  482. #endif
  483. }
  484. static inline __attribute__((always_inline))
  485. int rseq_cmpeqv_trymemcpy_storev(intptr_t *v, intptr_t expect,
  486. void *dst, void *src, size_t len,
  487. intptr_t newv, int cpu)
  488. {
  489. RSEQ_INJECT_C(9)
  490. __asm__ __volatile__ goto (
  491. RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
  492. /* setup for mempcy */
  493. "mr %%r19, %[len]\n\t"
  494. "mr %%r20, %[src]\n\t"
  495. "mr %%r21, %[dst]\n\t"
  496. /* Start rseq by storing table entry pointer into rseq_cs. */
  497. RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
  498. /* cmp cpuid */
  499. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
  500. RSEQ_INJECT_ASM(3)
  501. /* cmp @v equal to @expect */
  502. RSEQ_ASM_OP_CMPEQ(v, expect, %l[cmpfail])
  503. RSEQ_INJECT_ASM(4)
  504. #ifdef RSEQ_COMPARE_TWICE
  505. /* cmp cpuid */
  506. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, %l[error1])
  507. /* cmp @v equal to @expect */
  508. RSEQ_ASM_OP_CMPEQ(v, expect, %l[error2])
  509. #endif
  510. /* try memcpy */
  511. RSEQ_ASM_OP_R_MEMCPY()
  512. RSEQ_INJECT_ASM(5)
  513. /* final store */
  514. RSEQ_ASM_OP_FINAL_STORE(newv, v, 2)
  515. RSEQ_INJECT_ASM(6)
  516. /* teardown */
  517. RSEQ_ASM_DEFINE_ABORT(4, abort)
  518. : /* gcc asm goto does not allow outputs */
  519. : [cpu_id] "r" (cpu),
  520. [current_cpu_id] "m" (__rseq_abi.cpu_id),
  521. [rseq_cs] "m" (__rseq_abi.rseq_cs),
  522. /* final store input */
  523. [v] "m" (*v),
  524. [expect] "r" (expect),
  525. [newv] "r" (newv),
  526. /* try memcpy input */
  527. [dst] "r" (dst),
  528. [src] "r" (src),
  529. [len] "r" (len)
  530. RSEQ_INJECT_INPUT
  531. : "memory", "cc", "r17", "r18", "r19", "r20", "r21"
  532. RSEQ_INJECT_CLOBBER
  533. : abort, cmpfail
  534. #ifdef RSEQ_COMPARE_TWICE
  535. , error1, error2
  536. #endif
  537. );
  538. return 0;
  539. abort:
  540. RSEQ_INJECT_FAILED
  541. return -1;
  542. cmpfail:
  543. return 1;
  544. #ifdef RSEQ_COMPARE_TWICE
  545. error1:
  546. rseq_bug("cpu_id comparison failed");
  547. error2:
  548. rseq_bug("expected value comparison failed");
  549. #endif
  550. }
  551. static inline __attribute__((always_inline))
  552. int rseq_cmpeqv_trymemcpy_storev_release(intptr_t *v, intptr_t expect,
  553. void *dst, void *src, size_t len,
  554. intptr_t newv, int cpu)
  555. {
  556. RSEQ_INJECT_C(9)
  557. __asm__ __volatile__ goto (
  558. RSEQ_ASM_DEFINE_TABLE(3, 1f, 2f, 4f) /* start, commit, abort */
  559. /* setup for mempcy */
  560. "mr %%r19, %[len]\n\t"
  561. "mr %%r20, %[src]\n\t"
  562. "mr %%r21, %[dst]\n\t"
  563. /* Start rseq by storing table entry pointer into rseq_cs. */
  564. RSEQ_ASM_STORE_RSEQ_CS(1, 3b, rseq_cs)
  565. /* cmp cpuid */
  566. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, 4f)
  567. RSEQ_INJECT_ASM(3)
  568. /* cmp @v equal to @expect */
  569. RSEQ_ASM_OP_CMPEQ(v, expect, %l[cmpfail])
  570. RSEQ_INJECT_ASM(4)
  571. #ifdef RSEQ_COMPARE_TWICE
  572. /* cmp cpuid */
  573. RSEQ_ASM_CMP_CPU_ID(cpu_id, current_cpu_id, %l[error1])
  574. /* cmp @v equal to @expect */
  575. RSEQ_ASM_OP_CMPEQ(v, expect, %l[error2])
  576. #endif
  577. /* try memcpy */
  578. RSEQ_ASM_OP_R_MEMCPY()
  579. RSEQ_INJECT_ASM(5)
  580. /* for 'release' */
  581. "lwsync\n\t"
  582. /* final store */
  583. RSEQ_ASM_OP_FINAL_STORE(newv, v, 2)
  584. RSEQ_INJECT_ASM(6)
  585. /* teardown */
  586. RSEQ_ASM_DEFINE_ABORT(4, abort)
  587. : /* gcc asm goto does not allow outputs */
  588. : [cpu_id] "r" (cpu),
  589. [current_cpu_id] "m" (__rseq_abi.cpu_id),
  590. [rseq_cs] "m" (__rseq_abi.rseq_cs),
  591. /* final store input */
  592. [v] "m" (*v),
  593. [expect] "r" (expect),
  594. [newv] "r" (newv),
  595. /* try memcpy input */
  596. [dst] "r" (dst),
  597. [src] "r" (src),
  598. [len] "r" (len)
  599. RSEQ_INJECT_INPUT
  600. : "memory", "cc", "r17", "r18", "r19", "r20", "r21"
  601. RSEQ_INJECT_CLOBBER
  602. : abort, cmpfail
  603. #ifdef RSEQ_COMPARE_TWICE
  604. , error1, error2
  605. #endif
  606. );
  607. return 0;
  608. abort:
  609. RSEQ_INJECT_FAILED
  610. return -1;
  611. cmpfail:
  612. return 1;
  613. #ifdef RSEQ_COMPARE_TWICE
  614. error1:
  615. rseq_bug("cpu_id comparison failed");
  616. error2:
  617. rseq_bug("expected value comparison failed");
  618. #endif
  619. }
  620. #undef STORE_WORD
  621. #undef LOAD_WORD
  622. #undef LOADX_WORD
  623. #undef CMP_WORD
  624. #endif /* !RSEQ_SKIP_FASTPATH */