syscall.S 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * Linux/PA-RISC Project (http://www.parisc-linux.org/)
  3. *
  4. * System call entry code / Linux gateway page
  5. * Copyright (c) Matthew Wilcox 1999 <willy@bofh.ai>
  6. * Licensed under the GNU GPL.
  7. * thanks to Philipp Rumpf, Mike Shaver and various others
  8. * sorry about the wall, puffin..
  9. */
  10. /*
  11. How does the Linux gateway page on PA-RISC work?
  12. ------------------------------------------------
  13. The Linux gateway page on PA-RISC is "special".
  14. It actually has PAGE_GATEWAY bits set (this is linux terminology; in parisc
  15. terminology it's Execute, promote to PL0) in the page map. So anything
  16. executing on this page executes with kernel level privilege (there's more to it
  17. than that: to have this happen, you also have to use a branch with a ,gate
  18. completer to activate the privilege promotion). The upshot is that everything
  19. that runs on the gateway page runs at kernel privilege but with the current
  20. user process address space (although you have access to kernel space via %sr2).
  21. For the 0x100 syscall entry, we redo the space registers to point to the kernel
  22. address space (preserving the user address space in %sr3), move to wide mode if
  23. required, save the user registers and branch into the kernel syscall entry
  24. point. For all the other functions, we execute at kernel privilege but don't
  25. flip address spaces. The basic upshot of this is that these code snippets are
  26. executed atomically (because the kernel can't be pre-empted) and they may
  27. perform architecturally forbidden (to PL3) operations (like setting control
  28. registers).
  29. */
  30. #include <asm/asm-offsets.h>
  31. #include <asm/unistd.h>
  32. #include <asm/errno.h>
  33. #include <asm/page.h>
  34. #include <asm/psw.h>
  35. #include <asm/thread_info.h>
  36. #include <asm/assembly.h>
  37. #include <asm/processor.h>
  38. #include <asm/cache.h>
  39. #include <linux/linkage.h>
  40. /* We fill the empty parts of the gateway page with
  41. * something that will kill the kernel or a
  42. * userspace application.
  43. */
  44. #define KILL_INSN break 0,0
  45. .level LEVEL
  46. .text
  47. .import syscall_exit,code
  48. .import syscall_exit_rfi,code
  49. /* Linux gateway page is aliased to virtual page 0 in the kernel
  50. * address space. Since it is a gateway page it cannot be
  51. * dereferenced, so null pointers will still fault. We start
  52. * the actual entry point at 0x100. We put break instructions
  53. * at the beginning of the page to trap null indirect function
  54. * pointers.
  55. */
  56. .align PAGE_SIZE
  57. ENTRY(linux_gateway_page)
  58. /* ADDRESS 0x00 to 0xb0 = 176 bytes / 4 bytes per insn = 44 insns */
  59. .rept 44
  60. KILL_INSN
  61. .endr
  62. /* ADDRESS 0xb0 to 0xb8, lws uses two insns for entry */
  63. /* Light-weight-syscall entry must always be located at 0xb0 */
  64. /* WARNING: Keep this number updated with table size changes */
  65. #define __NR_lws_entries (3)
  66. lws_entry:
  67. gate lws_start, %r0 /* increase privilege */
  68. depi 3, 31, 2, %r31 /* Ensure we return into user mode. */
  69. /* Fill from 0xb8 to 0xe0 */
  70. .rept 10
  71. KILL_INSN
  72. .endr
  73. /* This function MUST be located at 0xe0 for glibc's threading
  74. mechanism to work. DO NOT MOVE THIS CODE EVER! */
  75. set_thread_pointer:
  76. gate .+8, %r0 /* increase privilege */
  77. depi 3, 31, 2, %r31 /* Ensure we return into user mode. */
  78. be 0(%sr7,%r31) /* return to user space */
  79. mtctl %r26, %cr27 /* move arg0 to the control register */
  80. /* Increase the chance of trapping if random jumps occur to this
  81. address, fill from 0xf0 to 0x100 */
  82. .rept 4
  83. KILL_INSN
  84. .endr
  85. /* This address must remain fixed at 0x100 for glibc's syscalls to work */
  86. .align LINUX_GATEWAY_ADDR
  87. linux_gateway_entry:
  88. gate .+8, %r0 /* become privileged */
  89. mtsp %r0,%sr4 /* get kernel space into sr4 */
  90. mtsp %r0,%sr5 /* get kernel space into sr5 */
  91. mtsp %r0,%sr6 /* get kernel space into sr6 */
  92. #ifdef CONFIG_64BIT
  93. /* for now we can *always* set the W bit on entry to the syscall
  94. * since we don't support wide userland processes. We could
  95. * also save the current SM other than in r0 and restore it on
  96. * exit from the syscall, and also use that value to know
  97. * whether to do narrow or wide syscalls. -PB
  98. */
  99. ssm PSW_SM_W, %r1
  100. extrd,u %r1,PSW_W_BIT,1,%r1
  101. /* sp must be aligned on 4, so deposit the W bit setting into
  102. * the bottom of sp temporarily */
  103. or,ev %r1,%r30,%r30
  104. b,n 1f
  105. /* The top halves of argument registers must be cleared on syscall
  106. * entry from narrow executable.
  107. */
  108. depdi 0, 31, 32, %r26
  109. depdi 0, 31, 32, %r25
  110. depdi 0, 31, 32, %r24
  111. depdi 0, 31, 32, %r23
  112. depdi 0, 31, 32, %r22
  113. depdi 0, 31, 32, %r21
  114. 1:
  115. #endif
  116. /* We use a rsm/ssm pair to prevent sr3 from being clobbered
  117. * by external interrupts.
  118. */
  119. mfsp %sr7,%r1 /* save user sr7 */
  120. rsm PSW_SM_I, %r0 /* disable interrupts */
  121. mtsp %r1,%sr3 /* and store it in sr3 */
  122. mfctl %cr30,%r1
  123. xor %r1,%r30,%r30 /* ye olde xor trick */
  124. xor %r1,%r30,%r1
  125. xor %r1,%r30,%r30
  126. ldo THREAD_SZ_ALGN+FRAME_SIZE(%r30),%r30 /* set up kernel stack */
  127. /* N.B.: It is critical that we don't set sr7 to 0 until r30
  128. * contains a valid kernel stack pointer. It is also
  129. * critical that we don't start using the kernel stack
  130. * until after sr7 has been set to 0.
  131. */
  132. mtsp %r0,%sr7 /* get kernel space into sr7 */
  133. ssm PSW_SM_I, %r0 /* enable interrupts */
  134. STREGM %r1,FRAME_SIZE(%r30) /* save r1 (usp) here for now */
  135. mfctl %cr30,%r1 /* get task ptr in %r1 */
  136. LDREG TI_TASK(%r1),%r1
  137. /* Save some registers for sigcontext and potential task
  138. switch (see entry.S for the details of which ones are
  139. saved/restored). TASK_PT_PSW is zeroed so we can see whether
  140. a process is on a syscall or not. For an interrupt the real
  141. PSW value is stored. This is needed for gdb and sys_ptrace. */
  142. STREG %r0, TASK_PT_PSW(%r1)
  143. STREG %r2, TASK_PT_GR2(%r1) /* preserve rp */
  144. STREG %r19, TASK_PT_GR19(%r1)
  145. LDREGM -FRAME_SIZE(%r30), %r2 /* get users sp back */
  146. #ifdef CONFIG_64BIT
  147. extrd,u %r2,63,1,%r19 /* W hidden in bottom bit */
  148. #if 0
  149. xor %r19,%r2,%r2 /* clear bottom bit */
  150. depd,z %r19,1,1,%r19
  151. std %r19,TASK_PT_PSW(%r1)
  152. #endif
  153. #endif
  154. STREG %r2, TASK_PT_GR30(%r1) /* ... and save it */
  155. STREG %r20, TASK_PT_GR20(%r1) /* Syscall number */
  156. STREG %r21, TASK_PT_GR21(%r1)
  157. STREG %r22, TASK_PT_GR22(%r1)
  158. STREG %r23, TASK_PT_GR23(%r1) /* 4th argument */
  159. STREG %r24, TASK_PT_GR24(%r1) /* 3rd argument */
  160. STREG %r25, TASK_PT_GR25(%r1) /* 2nd argument */
  161. STREG %r26, TASK_PT_GR26(%r1) /* 1st argument */
  162. STREG %r27, TASK_PT_GR27(%r1) /* user dp */
  163. STREG %r28, TASK_PT_GR28(%r1) /* return value 0 */
  164. STREG %r0, TASK_PT_ORIG_R28(%r1) /* don't prohibit restarts */
  165. STREG %r29, TASK_PT_GR29(%r1) /* return value 1 */
  166. STREG %r31, TASK_PT_GR31(%r1) /* preserve syscall return ptr */
  167. ldo TASK_PT_FR0(%r1), %r27 /* save fpregs from the kernel */
  168. save_fp %r27 /* or potential task switch */
  169. mfctl %cr11, %r27 /* i.e. SAR */
  170. STREG %r27, TASK_PT_SAR(%r1)
  171. loadgp
  172. #ifdef CONFIG_64BIT
  173. ldo -16(%r30),%r29 /* Reference param save area */
  174. copy %r19,%r2 /* W bit back to r2 */
  175. #else
  176. /* no need to save these on stack in wide mode because the first 8
  177. * args are passed in registers */
  178. stw %r22, -52(%r30) /* 5th argument */
  179. stw %r21, -56(%r30) /* 6th argument */
  180. #endif
  181. /* Are we being ptraced? */
  182. mfctl %cr30, %r1
  183. LDREG TI_FLAGS(%r1),%r1
  184. ldi _TIF_SYSCALL_TRACE_MASK, %r19
  185. and,COND(=) %r1, %r19, %r0
  186. b,n .Ltracesys
  187. /* Note! We cannot use the syscall table that is mapped
  188. nearby since the gateway page is mapped execute-only. */
  189. #ifdef CONFIG_64BIT
  190. ldil L%sys_call_table, %r1
  191. or,= %r2,%r2,%r2
  192. addil L%(sys_call_table64-sys_call_table), %r1
  193. ldo R%sys_call_table(%r1), %r19
  194. or,= %r2,%r2,%r2
  195. ldo R%sys_call_table64(%r1), %r19
  196. #else
  197. ldil L%sys_call_table, %r1
  198. ldo R%sys_call_table(%r1), %r19
  199. #endif
  200. comiclr,>> __NR_Linux_syscalls, %r20, %r0
  201. b,n .Lsyscall_nosys
  202. LDREGX %r20(%r19), %r19
  203. /* If this is a sys_rt_sigreturn call, and the signal was received
  204. * when not in_syscall, then we want to return via syscall_exit_rfi,
  205. * not syscall_exit. Signal no. in r20, in_syscall in r25 (see
  206. * trampoline code in signal.c).
  207. */
  208. ldi __NR_rt_sigreturn,%r2
  209. comb,= %r2,%r20,.Lrt_sigreturn
  210. .Lin_syscall:
  211. ldil L%syscall_exit,%r2
  212. be 0(%sr7,%r19)
  213. ldo R%syscall_exit(%r2),%r2
  214. .Lrt_sigreturn:
  215. comib,<> 0,%r25,.Lin_syscall
  216. ldil L%syscall_exit_rfi,%r2
  217. be 0(%sr7,%r19)
  218. ldo R%syscall_exit_rfi(%r2),%r2
  219. /* Note! Because we are not running where we were linked, any
  220. calls to functions external to this file must be indirect. To
  221. be safe, we apply the opposite rule to functions within this
  222. file, with local labels given to them to ensure correctness. */
  223. .Lsyscall_nosys:
  224. syscall_nosys:
  225. ldil L%syscall_exit,%r1
  226. be R%syscall_exit(%sr7,%r1)
  227. ldo -ENOSYS(%r0),%r28 /* set errno */
  228. /* Warning! This trace code is a virtual duplicate of the code above so be
  229. * sure to maintain both! */
  230. .Ltracesys:
  231. tracesys:
  232. /* Need to save more registers so the debugger can see where we
  233. * are. This saves only the lower 8 bits of PSW, so that the C
  234. * bit is still clear on syscalls, and the D bit is set if this
  235. * full register save path has been executed. We check the D
  236. * bit on syscall_return_rfi to determine which registers to
  237. * restore. An interrupt results in a full PSW saved with the
  238. * C bit set, a non-straced syscall entry results in C and D clear
  239. * in the saved PSW.
  240. */
  241. ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */
  242. LDREG TI_TASK(%r1), %r1
  243. ssm 0,%r2
  244. STREG %r2,TASK_PT_PSW(%r1) /* Lower 8 bits only!! */
  245. mfsp %sr0,%r2
  246. STREG %r2,TASK_PT_SR0(%r1)
  247. mfsp %sr1,%r2
  248. STREG %r2,TASK_PT_SR1(%r1)
  249. mfsp %sr2,%r2
  250. STREG %r2,TASK_PT_SR2(%r1)
  251. mfsp %sr3,%r2
  252. STREG %r2,TASK_PT_SR3(%r1)
  253. STREG %r2,TASK_PT_SR4(%r1)
  254. STREG %r2,TASK_PT_SR5(%r1)
  255. STREG %r2,TASK_PT_SR6(%r1)
  256. STREG %r2,TASK_PT_SR7(%r1)
  257. STREG %r2,TASK_PT_IASQ0(%r1)
  258. STREG %r2,TASK_PT_IASQ1(%r1)
  259. LDREG TASK_PT_GR31(%r1),%r2
  260. STREG %r2,TASK_PT_IAOQ0(%r1)
  261. ldo 4(%r2),%r2
  262. STREG %r2,TASK_PT_IAOQ1(%r1)
  263. ldo TASK_REGS(%r1),%r2
  264. /* reg_save %r2 */
  265. STREG %r3,PT_GR3(%r2)
  266. STREG %r4,PT_GR4(%r2)
  267. STREG %r5,PT_GR5(%r2)
  268. STREG %r6,PT_GR6(%r2)
  269. STREG %r7,PT_GR7(%r2)
  270. STREG %r8,PT_GR8(%r2)
  271. STREG %r9,PT_GR9(%r2)
  272. STREG %r10,PT_GR10(%r2)
  273. STREG %r11,PT_GR11(%r2)
  274. STREG %r12,PT_GR12(%r2)
  275. STREG %r13,PT_GR13(%r2)
  276. STREG %r14,PT_GR14(%r2)
  277. STREG %r15,PT_GR15(%r2)
  278. STREG %r16,PT_GR16(%r2)
  279. STREG %r17,PT_GR17(%r2)
  280. STREG %r18,PT_GR18(%r2)
  281. /* Finished saving things for the debugger */
  282. copy %r2,%r26
  283. ldil L%do_syscall_trace_enter,%r1
  284. ldil L%tracesys_next,%r2
  285. be R%do_syscall_trace_enter(%sr7,%r1)
  286. ldo R%tracesys_next(%r2),%r2
  287. tracesys_next:
  288. /* do_syscall_trace_enter either returned the syscallno, or -1L,
  289. * so we skip restoring the PT_GR20 below, since we pulled it from
  290. * task->thread.regs.gr[20] above.
  291. */
  292. copy %ret0,%r20
  293. ldil L%sys_call_table,%r1
  294. ldo R%sys_call_table(%r1), %r19
  295. ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */
  296. LDREG TI_TASK(%r1), %r1
  297. LDREG TASK_PT_GR28(%r1), %r28 /* Restore return value */
  298. LDREG TASK_PT_GR26(%r1), %r26 /* Restore the users args */
  299. LDREG TASK_PT_GR25(%r1), %r25
  300. LDREG TASK_PT_GR24(%r1), %r24
  301. LDREG TASK_PT_GR23(%r1), %r23
  302. LDREG TASK_PT_GR22(%r1), %r22
  303. LDREG TASK_PT_GR21(%r1), %r21
  304. #ifdef CONFIG_64BIT
  305. ldo -16(%r30),%r29 /* Reference param save area */
  306. #else
  307. stw %r22, -52(%r30) /* 5th argument */
  308. stw %r21, -56(%r30) /* 6th argument */
  309. #endif
  310. cmpib,COND(=),n -1,%r20,tracesys_exit /* seccomp may have returned -1 */
  311. comiclr,>> __NR_Linux_syscalls, %r20, %r0
  312. b,n .Ltracesys_nosys
  313. LDREGX %r20(%r19), %r19
  314. /* If this is a sys_rt_sigreturn call, and the signal was received
  315. * when not in_syscall, then we want to return via syscall_exit_rfi,
  316. * not syscall_exit. Signal no. in r20, in_syscall in r25 (see
  317. * trampoline code in signal.c).
  318. */
  319. ldi __NR_rt_sigreturn,%r2
  320. comb,= %r2,%r20,.Ltrace_rt_sigreturn
  321. .Ltrace_in_syscall:
  322. ldil L%tracesys_exit,%r2
  323. be 0(%sr7,%r19)
  324. ldo R%tracesys_exit(%r2),%r2
  325. .Ltracesys_nosys:
  326. ldo -ENOSYS(%r0),%r28 /* set errno */
  327. /* Do *not* call this function on the gateway page, because it
  328. makes a direct call to syscall_trace. */
  329. tracesys_exit:
  330. ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */
  331. LDREG TI_TASK(%r1), %r1
  332. #ifdef CONFIG_64BIT
  333. ldo -16(%r30),%r29 /* Reference param save area */
  334. #endif
  335. ldo TASK_REGS(%r1),%r26
  336. BL do_syscall_trace_exit,%r2
  337. STREG %r28,TASK_PT_GR28(%r1) /* save return value now */
  338. ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */
  339. LDREG TI_TASK(%r1), %r1
  340. LDREG TASK_PT_GR28(%r1), %r28 /* Restore return val. */
  341. ldil L%syscall_exit,%r1
  342. be,n R%syscall_exit(%sr7,%r1)
  343. .Ltrace_rt_sigreturn:
  344. comib,<> 0,%r25,.Ltrace_in_syscall
  345. ldil L%tracesys_sigexit,%r2
  346. be 0(%sr7,%r19)
  347. ldo R%tracesys_sigexit(%r2),%r2
  348. tracesys_sigexit:
  349. ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */
  350. LDREG TI_TASK(%r1), %r1
  351. #ifdef CONFIG_64BIT
  352. ldo -16(%r30),%r29 /* Reference param save area */
  353. #endif
  354. BL do_syscall_trace_exit,%r2
  355. ldo TASK_REGS(%r1),%r26
  356. ldil L%syscall_exit_rfi,%r1
  357. be,n R%syscall_exit_rfi(%sr7,%r1)
  358. /*********************************************************
  359. 32/64-bit Light-Weight-Syscall ABI
  360. * - Indicates a hint for userspace inline asm
  361. implementations.
  362. Syscall number (caller-saves)
  363. - %r20
  364. * In asm clobber.
  365. Argument registers (caller-saves)
  366. - %r26, %r25, %r24, %r23, %r22
  367. * In asm input.
  368. Return registers (caller-saves)
  369. - %r28 (return), %r21 (errno)
  370. * In asm output.
  371. Caller-saves registers
  372. - %r1, %r27, %r29
  373. - %r2 (return pointer)
  374. - %r31 (ble link register)
  375. * In asm clobber.
  376. Callee-saves registers
  377. - %r3-%r18
  378. - %r30 (stack pointer)
  379. * Not in asm clobber.
  380. If userspace is 32-bit:
  381. Callee-saves registers
  382. - %r19 (32-bit PIC register)
  383. Differences from 32-bit calling convention:
  384. - Syscall number in %r20
  385. - Additional argument register %r22 (arg4)
  386. - Callee-saves %r19.
  387. If userspace is 64-bit:
  388. Callee-saves registers
  389. - %r27 (64-bit PIC register)
  390. Differences from 64-bit calling convention:
  391. - Syscall number in %r20
  392. - Additional argument register %r22 (arg4)
  393. - Callee-saves %r27.
  394. Error codes returned by entry path:
  395. ENOSYS - r20 was an invalid LWS number.
  396. *********************************************************/
  397. lws_start:
  398. #ifdef CONFIG_64BIT
  399. /* FIXME: If we are a 64-bit kernel just
  400. * turn this on unconditionally.
  401. */
  402. ssm PSW_SM_W, %r1
  403. extrd,u %r1,PSW_W_BIT,1,%r1
  404. /* sp must be aligned on 4, so deposit the W bit setting into
  405. * the bottom of sp temporarily */
  406. or,ev %r1,%r30,%r30
  407. /* Clip LWS number to a 32-bit value always */
  408. depdi 0, 31, 32, %r20
  409. #endif
  410. /* Is the lws entry number valid? */
  411. comiclr,>> __NR_lws_entries, %r20, %r0
  412. b,n lws_exit_nosys
  413. /* Load table start */
  414. ldil L%lws_table, %r1
  415. ldo R%lws_table(%r1), %r28 /* Scratch use of r28 */
  416. LDREGX %r20(%sr2,r28), %r21 /* Scratch use of r21 */
  417. /* Jump to lws, lws table pointers already relocated */
  418. be,n 0(%sr2,%r21)
  419. lws_exit_nosys:
  420. ldo -ENOSYS(%r0),%r21 /* set errno */
  421. /* Fall through: Return to userspace */
  422. lws_exit:
  423. #ifdef CONFIG_64BIT
  424. /* decide whether to reset the wide mode bit
  425. *
  426. * For a syscall, the W bit is stored in the lowest bit
  427. * of sp. Extract it and reset W if it is zero */
  428. extrd,u,*<> %r30,63,1,%r1
  429. rsm PSW_SM_W, %r0
  430. /* now reset the lowest bit of sp if it was set */
  431. xor %r30,%r1,%r30
  432. #endif
  433. be,n 0(%sr7, %r31)
  434. /***************************************************
  435. Implementing 32bit CAS as an atomic operation:
  436. %r26 - Address to examine
  437. %r25 - Old value to check (old)
  438. %r24 - New value to set (new)
  439. %r28 - Return prev through this register.
  440. %r21 - Kernel error code
  441. If debugging is DISabled:
  442. %r21 has the following meanings:
  443. EAGAIN - CAS is busy, ldcw failed, try again.
  444. EFAULT - Read or write failed.
  445. If debugging is enabled:
  446. EDEADLOCK - CAS called recursively.
  447. EAGAIN && r28 == 1 - CAS is busy. Lock contended.
  448. EAGAIN && r28 == 2 - CAS is busy. ldcw failed.
  449. EFAULT - Read or write failed.
  450. Scratch: r20, r28, r1
  451. ****************************************************/
  452. /* Do not enable LWS debugging */
  453. #define ENABLE_LWS_DEBUG 0
  454. /* ELF64 Process entry path */
  455. lws_compare_and_swap64:
  456. #ifdef CONFIG_64BIT
  457. b,n lws_compare_and_swap
  458. #else
  459. /* If we are not a 64-bit kernel, then we don't
  460. * have 64-bit input registers, and calling
  461. * the 64-bit LWS CAS returns ENOSYS.
  462. */
  463. b,n lws_exit_nosys
  464. #endif
  465. /* ELF32 Process entry path */
  466. lws_compare_and_swap32:
  467. #ifdef CONFIG_64BIT
  468. /* Clip all the input registers */
  469. depdi 0, 31, 32, %r26
  470. depdi 0, 31, 32, %r25
  471. depdi 0, 31, 32, %r24
  472. #endif
  473. lws_compare_and_swap:
  474. /* Load start of lock table */
  475. ldil L%lws_lock_start, %r20
  476. ldo R%lws_lock_start(%r20), %r28
  477. /* Extract four bits from r26 and hash lock (Bits 4-7) */
  478. extru %r26, 27, 4, %r20
  479. /* Find lock to use, the hash is either one of 0 to
  480. 15, multiplied by 16 (keep it 16-byte aligned)
  481. and add to the lock table offset. */
  482. shlw %r20, 4, %r20
  483. add %r20, %r28, %r20
  484. # if ENABLE_LWS_DEBUG
  485. /*
  486. DEBUG, check for deadlock!
  487. If the thread register values are the same
  488. then we were the one that locked it last and
  489. this is a recurisve call that will deadlock.
  490. We *must* giveup this call and fail.
  491. */
  492. ldw 4(%sr2,%r20), %r28 /* Load thread register */
  493. /* WARNING: If cr27 cycles to the same value we have problems */
  494. mfctl %cr27, %r21 /* Get current thread register */
  495. cmpb,<>,n %r21, %r28, cas_lock /* Called recursive? */
  496. b lws_exit /* Return error! */
  497. ldo -EDEADLOCK(%r0), %r21
  498. cas_lock:
  499. cmpb,=,n %r0, %r28, cas_nocontend /* Is nobody using it? */
  500. ldo 1(%r0), %r28 /* 1st case */
  501. b lws_exit /* Contended... */
  502. ldo -EAGAIN(%r0), %r21 /* Spin in userspace */
  503. cas_nocontend:
  504. # endif
  505. /* ENABLE_LWS_DEBUG */
  506. rsm PSW_SM_I, %r0 /* Disable interrupts */
  507. /* COW breaks can cause contention on UP systems */
  508. LDCW 0(%sr2,%r20), %r28 /* Try to acquire the lock */
  509. cmpb,<>,n %r0, %r28, cas_action /* Did we get it? */
  510. cas_wouldblock:
  511. ldo 2(%r0), %r28 /* 2nd case */
  512. ssm PSW_SM_I, %r0
  513. b lws_exit /* Contended... */
  514. ldo -EAGAIN(%r0), %r21 /* Spin in userspace */
  515. /*
  516. prev = *addr;
  517. if ( prev == old )
  518. *addr = new;
  519. return prev;
  520. */
  521. /* NOTES:
  522. This all works becuse intr_do_signal
  523. and schedule both check the return iasq
  524. and see that we are on the kernel page
  525. so this process is never scheduled off
  526. or is ever sent any signal of any sort,
  527. thus it is wholly atomic from usrspaces
  528. perspective
  529. */
  530. cas_action:
  531. #if defined CONFIG_SMP && ENABLE_LWS_DEBUG
  532. /* DEBUG */
  533. mfctl %cr27, %r1
  534. stw %r1, 4(%sr2,%r20)
  535. #endif
  536. /* The load and store could fail */
  537. 1: ldw,ma 0(%r26), %r28
  538. sub,<> %r28, %r25, %r0
  539. 2: stw,ma %r24, 0(%r26)
  540. /* Free lock */
  541. stw,ma %r20, 0(%sr2,%r20)
  542. #if ENABLE_LWS_DEBUG
  543. /* Clear thread register indicator */
  544. stw %r0, 4(%sr2,%r20)
  545. #endif
  546. /* Enable interrupts */
  547. ssm PSW_SM_I, %r0
  548. /* Return to userspace, set no error */
  549. b lws_exit
  550. copy %r0, %r21
  551. 3:
  552. /* Error occurred on load or store */
  553. /* Free lock */
  554. stw %r20, 0(%sr2,%r20)
  555. #if ENABLE_LWS_DEBUG
  556. stw %r0, 4(%sr2,%r20)
  557. #endif
  558. ssm PSW_SM_I, %r0
  559. b lws_exit
  560. ldo -EFAULT(%r0),%r21 /* set errno */
  561. nop
  562. nop
  563. nop
  564. nop
  565. /* Two exception table entries, one for the load,
  566. the other for the store. Either return -EFAULT.
  567. Each of the entries must be relocated. */
  568. ASM_EXCEPTIONTABLE_ENTRY(1b-linux_gateway_page, 3b-linux_gateway_page)
  569. ASM_EXCEPTIONTABLE_ENTRY(2b-linux_gateway_page, 3b-linux_gateway_page)
  570. /***************************************************
  571. New CAS implementation which uses pointers and variable size
  572. information. The value pointed by old and new MUST NOT change
  573. while performing CAS. The lock only protect the value at %r26.
  574. %r26 - Address to examine
  575. %r25 - Pointer to the value to check (old)
  576. %r24 - Pointer to the value to set (new)
  577. %r23 - Size of the variable (0/1/2/3 for 8/16/32/64 bit)
  578. %r28 - Return non-zero on failure
  579. %r21 - Kernel error code
  580. %r21 has the following meanings:
  581. EAGAIN - CAS is busy, ldcw failed, try again.
  582. EFAULT - Read or write failed.
  583. Scratch: r20, r22, r28, r29, r1, fr4 (32bit for 64bit CAS only)
  584. ****************************************************/
  585. /* ELF32 Process entry path */
  586. lws_compare_and_swap_2:
  587. #ifdef CONFIG_64BIT
  588. /* Clip the input registers. We don't need to clip %r23 as we
  589. only use it for word operations */
  590. depdi 0, 31, 32, %r26
  591. depdi 0, 31, 32, %r25
  592. depdi 0, 31, 32, %r24
  593. #endif
  594. /* Check the validity of the size pointer */
  595. subi,>>= 3, %r23, %r0
  596. b,n lws_exit_nosys
  597. /* Jump to the functions which will load the old and new values into
  598. registers depending on the their size */
  599. shlw %r23, 2, %r29
  600. blr %r29, %r0
  601. nop
  602. /* 8bit load */
  603. 4: ldb 0(%r25), %r25
  604. b cas2_lock_start
  605. 5: ldb 0(%r24), %r24
  606. nop
  607. nop
  608. nop
  609. nop
  610. nop
  611. /* 16bit load */
  612. 6: ldh 0(%r25), %r25
  613. b cas2_lock_start
  614. 7: ldh 0(%r24), %r24
  615. nop
  616. nop
  617. nop
  618. nop
  619. nop
  620. /* 32bit load */
  621. 8: ldw 0(%r25), %r25
  622. b cas2_lock_start
  623. 9: ldw 0(%r24), %r24
  624. nop
  625. nop
  626. nop
  627. nop
  628. nop
  629. /* 64bit load */
  630. #ifdef CONFIG_64BIT
  631. 10: ldd 0(%r25), %r25
  632. 11: ldd 0(%r24), %r24
  633. #else
  634. /* Load old value into r22/r23 - high/low */
  635. 10: ldw 0(%r25), %r22
  636. 11: ldw 4(%r25), %r23
  637. /* Load new value into fr4 for atomic store later */
  638. 12: flddx 0(%r24), %fr4
  639. #endif
  640. cas2_lock_start:
  641. /* Load start of lock table */
  642. ldil L%lws_lock_start, %r20
  643. ldo R%lws_lock_start(%r20), %r28
  644. /* Extract four bits from r26 and hash lock (Bits 4-7) */
  645. extru %r26, 27, 4, %r20
  646. /* Find lock to use, the hash is either one of 0 to
  647. 15, multiplied by 16 (keep it 16-byte aligned)
  648. and add to the lock table offset. */
  649. shlw %r20, 4, %r20
  650. add %r20, %r28, %r20
  651. rsm PSW_SM_I, %r0 /* Disable interrupts */
  652. /* COW breaks can cause contention on UP systems */
  653. LDCW 0(%sr2,%r20), %r28 /* Try to acquire the lock */
  654. cmpb,<>,n %r0, %r28, cas2_action /* Did we get it? */
  655. cas2_wouldblock:
  656. ldo 2(%r0), %r28 /* 2nd case */
  657. ssm PSW_SM_I, %r0
  658. b lws_exit /* Contended... */
  659. ldo -EAGAIN(%r0), %r21 /* Spin in userspace */
  660. /*
  661. prev = *addr;
  662. if ( prev == old )
  663. *addr = new;
  664. return prev;
  665. */
  666. /* NOTES:
  667. This all works becuse intr_do_signal
  668. and schedule both check the return iasq
  669. and see that we are on the kernel page
  670. so this process is never scheduled off
  671. or is ever sent any signal of any sort,
  672. thus it is wholly atomic from usrspaces
  673. perspective
  674. */
  675. cas2_action:
  676. /* Jump to the correct function */
  677. blr %r29, %r0
  678. /* Set %r28 as non-zero for now */
  679. ldo 1(%r0),%r28
  680. /* 8bit CAS */
  681. 13: ldb,ma 0(%r26), %r29
  682. sub,= %r29, %r25, %r0
  683. b,n cas2_end
  684. 14: stb,ma %r24, 0(%r26)
  685. b cas2_end
  686. copy %r0, %r28
  687. nop
  688. nop
  689. /* 16bit CAS */
  690. 15: ldh,ma 0(%r26), %r29
  691. sub,= %r29, %r25, %r0
  692. b,n cas2_end
  693. 16: sth,ma %r24, 0(%r26)
  694. b cas2_end
  695. copy %r0, %r28
  696. nop
  697. nop
  698. /* 32bit CAS */
  699. 17: ldw,ma 0(%r26), %r29
  700. sub,= %r29, %r25, %r0
  701. b,n cas2_end
  702. 18: stw,ma %r24, 0(%r26)
  703. b cas2_end
  704. copy %r0, %r28
  705. nop
  706. nop
  707. /* 64bit CAS */
  708. #ifdef CONFIG_64BIT
  709. 19: ldd,ma 0(%r26), %r29
  710. sub,*= %r29, %r25, %r0
  711. b,n cas2_end
  712. 20: std,ma %r24, 0(%r26)
  713. copy %r0, %r28
  714. #else
  715. /* Compare first word */
  716. 19: ldw 0(%r26), %r29
  717. sub,= %r29, %r22, %r0
  718. b,n cas2_end
  719. /* Compare second word */
  720. 20: ldw 4(%r26), %r29
  721. sub,= %r29, %r23, %r0
  722. b,n cas2_end
  723. /* Perform the store */
  724. 21: fstdx %fr4, 0(%r26)
  725. copy %r0, %r28
  726. #endif
  727. cas2_end:
  728. /* Free lock */
  729. stw,ma %r20, 0(%sr2,%r20)
  730. /* Enable interrupts */
  731. ssm PSW_SM_I, %r0
  732. /* Return to userspace, set no error */
  733. b lws_exit
  734. copy %r0, %r21
  735. 22:
  736. /* Error occurred on load or store */
  737. /* Free lock */
  738. stw %r20, 0(%sr2,%r20)
  739. ssm PSW_SM_I, %r0
  740. ldo 1(%r0),%r28
  741. b lws_exit
  742. ldo -EFAULT(%r0),%r21 /* set errno */
  743. nop
  744. nop
  745. nop
  746. /* Exception table entries, for the load and store, return EFAULT.
  747. Each of the entries must be relocated. */
  748. ASM_EXCEPTIONTABLE_ENTRY(4b-linux_gateway_page, 22b-linux_gateway_page)
  749. ASM_EXCEPTIONTABLE_ENTRY(5b-linux_gateway_page, 22b-linux_gateway_page)
  750. ASM_EXCEPTIONTABLE_ENTRY(6b-linux_gateway_page, 22b-linux_gateway_page)
  751. ASM_EXCEPTIONTABLE_ENTRY(7b-linux_gateway_page, 22b-linux_gateway_page)
  752. ASM_EXCEPTIONTABLE_ENTRY(8b-linux_gateway_page, 22b-linux_gateway_page)
  753. ASM_EXCEPTIONTABLE_ENTRY(9b-linux_gateway_page, 22b-linux_gateway_page)
  754. ASM_EXCEPTIONTABLE_ENTRY(10b-linux_gateway_page, 22b-linux_gateway_page)
  755. ASM_EXCEPTIONTABLE_ENTRY(11b-linux_gateway_page, 22b-linux_gateway_page)
  756. ASM_EXCEPTIONTABLE_ENTRY(13b-linux_gateway_page, 22b-linux_gateway_page)
  757. ASM_EXCEPTIONTABLE_ENTRY(14b-linux_gateway_page, 22b-linux_gateway_page)
  758. ASM_EXCEPTIONTABLE_ENTRY(15b-linux_gateway_page, 22b-linux_gateway_page)
  759. ASM_EXCEPTIONTABLE_ENTRY(16b-linux_gateway_page, 22b-linux_gateway_page)
  760. ASM_EXCEPTIONTABLE_ENTRY(17b-linux_gateway_page, 22b-linux_gateway_page)
  761. ASM_EXCEPTIONTABLE_ENTRY(18b-linux_gateway_page, 22b-linux_gateway_page)
  762. ASM_EXCEPTIONTABLE_ENTRY(19b-linux_gateway_page, 22b-linux_gateway_page)
  763. ASM_EXCEPTIONTABLE_ENTRY(20b-linux_gateway_page, 22b-linux_gateway_page)
  764. #ifndef CONFIG_64BIT
  765. ASM_EXCEPTIONTABLE_ENTRY(12b-linux_gateway_page, 22b-linux_gateway_page)
  766. ASM_EXCEPTIONTABLE_ENTRY(21b-linux_gateway_page, 22b-linux_gateway_page)
  767. #endif
  768. /* Make sure nothing else is placed on this page */
  769. .align PAGE_SIZE
  770. END(linux_gateway_page)
  771. ENTRY(end_linux_gateway_page)
  772. /* Relocate symbols assuming linux_gateway_page is mapped
  773. to virtual address 0x0 */
  774. #define LWS_ENTRY(_name_) ASM_ULONG_INSN (lws_##_name_ - linux_gateway_page)
  775. .section .rodata,"a"
  776. .align 8
  777. /* Light-weight-syscall table */
  778. /* Start of lws table. */
  779. ENTRY(lws_table)
  780. LWS_ENTRY(compare_and_swap32) /* 0 - ELF32 Atomic 32bit CAS */
  781. LWS_ENTRY(compare_and_swap64) /* 1 - ELF64 Atomic 32bit CAS */
  782. LWS_ENTRY(compare_and_swap_2) /* 2 - ELF32 Atomic 64bit CAS */
  783. END(lws_table)
  784. /* End of lws table */
  785. .align 8
  786. ENTRY(sys_call_table)
  787. .export sys_call_table,data
  788. #include "syscall_table.S"
  789. END(sys_call_table)
  790. #ifdef CONFIG_64BIT
  791. .align 8
  792. ENTRY(sys_call_table64)
  793. #define SYSCALL_TABLE_64BIT
  794. #include "syscall_table.S"
  795. END(sys_call_table64)
  796. #endif
  797. /*
  798. All light-weight-syscall atomic operations
  799. will use this set of locks
  800. NOTE: The lws_lock_start symbol must be
  801. at least 16-byte aligned for safe use
  802. with ldcw.
  803. */
  804. .section .data
  805. .align L1_CACHE_BYTES
  806. ENTRY(lws_lock_start)
  807. /* lws locks */
  808. .rept 16
  809. /* Keep locks aligned at 16-bytes */
  810. .word 1
  811. .word 0
  812. .word 0
  813. .word 0
  814. .endr
  815. END(lws_lock_start)
  816. .previous
  817. .end