syscall.S 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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 (2)
  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 256
  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. mfsp %sr7,%r1 /* save user sr7 */
  93. mtsp %r1,%sr3 /* and store it in sr3 */
  94. #ifdef CONFIG_64BIT
  95. /* for now we can *always* set the W bit on entry to the syscall
  96. * since we don't support wide userland processes. We could
  97. * also save the current SM other than in r0 and restore it on
  98. * exit from the syscall, and also use that value to know
  99. * whether to do narrow or wide syscalls. -PB
  100. */
  101. ssm PSW_SM_W, %r1
  102. extrd,u %r1,PSW_W_BIT,1,%r1
  103. /* sp must be aligned on 4, so deposit the W bit setting into
  104. * the bottom of sp temporarily */
  105. or,ev %r1,%r30,%r30
  106. b,n 1f
  107. /* The top halves of argument registers must be cleared on syscall
  108. * entry from narrow executable.
  109. */
  110. depdi 0, 31, 32, %r26
  111. depdi 0, 31, 32, %r25
  112. depdi 0, 31, 32, %r24
  113. depdi 0, 31, 32, %r23
  114. depdi 0, 31, 32, %r22
  115. depdi 0, 31, 32, %r21
  116. 1:
  117. #endif
  118. mfctl %cr30,%r1
  119. xor %r1,%r30,%r30 /* ye olde xor trick */
  120. xor %r1,%r30,%r1
  121. xor %r1,%r30,%r30
  122. ldo THREAD_SZ_ALGN+FRAME_SIZE(%r30),%r30 /* set up kernel stack */
  123. /* N.B.: It is critical that we don't set sr7 to 0 until r30
  124. * contains a valid kernel stack pointer. It is also
  125. * critical that we don't start using the kernel stack
  126. * until after sr7 has been set to 0.
  127. */
  128. mtsp %r0,%sr7 /* get kernel space into sr7 */
  129. STREGM %r1,FRAME_SIZE(%r30) /* save r1 (usp) here for now */
  130. mfctl %cr30,%r1 /* get task ptr in %r1 */
  131. LDREG TI_TASK(%r1),%r1
  132. /* Save some registers for sigcontext and potential task
  133. switch (see entry.S for the details of which ones are
  134. saved/restored). TASK_PT_PSW is zeroed so we can see whether
  135. a process is on a syscall or not. For an interrupt the real
  136. PSW value is stored. This is needed for gdb and sys_ptrace. */
  137. STREG %r0, TASK_PT_PSW(%r1)
  138. STREG %r2, TASK_PT_GR2(%r1) /* preserve rp */
  139. STREG %r19, TASK_PT_GR19(%r1)
  140. LDREGM -FRAME_SIZE(%r30), %r2 /* get users sp back */
  141. #ifdef CONFIG_64BIT
  142. extrd,u %r2,63,1,%r19 /* W hidden in bottom bit */
  143. #if 0
  144. xor %r19,%r2,%r2 /* clear bottom bit */
  145. depd,z %r19,1,1,%r19
  146. std %r19,TASK_PT_PSW(%r1)
  147. #endif
  148. #endif
  149. STREG %r2, TASK_PT_GR30(%r1) /* ... and save it */
  150. STREG %r20, TASK_PT_GR20(%r1) /* Syscall number */
  151. STREG %r21, TASK_PT_GR21(%r1)
  152. STREG %r22, TASK_PT_GR22(%r1)
  153. STREG %r23, TASK_PT_GR23(%r1) /* 4th argument */
  154. STREG %r24, TASK_PT_GR24(%r1) /* 3rd argument */
  155. STREG %r25, TASK_PT_GR25(%r1) /* 2nd argument */
  156. STREG %r26, TASK_PT_GR26(%r1) /* 1st argument */
  157. STREG %r27, TASK_PT_GR27(%r1) /* user dp */
  158. STREG %r28, TASK_PT_GR28(%r1) /* return value 0 */
  159. STREG %r0, TASK_PT_ORIG_R28(%r1) /* don't prohibit restarts */
  160. STREG %r29, TASK_PT_GR29(%r1) /* return value 1 */
  161. STREG %r31, TASK_PT_GR31(%r1) /* preserve syscall return ptr */
  162. ldo TASK_PT_FR0(%r1), %r27 /* save fpregs from the kernel */
  163. save_fp %r27 /* or potential task switch */
  164. mfctl %cr11, %r27 /* i.e. SAR */
  165. STREG %r27, TASK_PT_SAR(%r1)
  166. loadgp
  167. #ifdef CONFIG_64BIT
  168. ldo -16(%r30),%r29 /* Reference param save area */
  169. copy %r19,%r2 /* W bit back to r2 */
  170. #else
  171. /* no need to save these on stack in wide mode because the first 8
  172. * args are passed in registers */
  173. stw %r22, -52(%r30) /* 5th argument */
  174. stw %r21, -56(%r30) /* 6th argument */
  175. #endif
  176. /* Are we being ptraced? */
  177. mfctl %cr30, %r1
  178. LDREG TI_FLAGS(%r1),%r1
  179. ldi _TIF_SYSCALL_TRACE_MASK, %r19
  180. and,COND(=) %r1, %r19, %r0
  181. b,n .Ltracesys
  182. /* Note! We cannot use the syscall table that is mapped
  183. nearby since the gateway page is mapped execute-only. */
  184. #ifdef CONFIG_64BIT
  185. ldil L%sys_call_table, %r1
  186. or,= %r2,%r2,%r2
  187. addil L%(sys_call_table64-sys_call_table), %r1
  188. ldo R%sys_call_table(%r1), %r19
  189. or,= %r2,%r2,%r2
  190. ldo R%sys_call_table64(%r1), %r19
  191. #else
  192. ldil L%sys_call_table, %r1
  193. ldo R%sys_call_table(%r1), %r19
  194. #endif
  195. comiclr,>> __NR_Linux_syscalls, %r20, %r0
  196. b,n .Lsyscall_nosys
  197. LDREGX %r20(%r19), %r19
  198. /* If this is a sys_rt_sigreturn call, and the signal was received
  199. * when not in_syscall, then we want to return via syscall_exit_rfi,
  200. * not syscall_exit. Signal no. in r20, in_syscall in r25 (see
  201. * trampoline code in signal.c).
  202. */
  203. ldi __NR_rt_sigreturn,%r2
  204. comb,= %r2,%r20,.Lrt_sigreturn
  205. .Lin_syscall:
  206. ldil L%syscall_exit,%r2
  207. be 0(%sr7,%r19)
  208. ldo R%syscall_exit(%r2),%r2
  209. .Lrt_sigreturn:
  210. comib,<> 0,%r25,.Lin_syscall
  211. ldil L%syscall_exit_rfi,%r2
  212. be 0(%sr7,%r19)
  213. ldo R%syscall_exit_rfi(%r2),%r2
  214. /* Note! Because we are not running where we were linked, any
  215. calls to functions external to this file must be indirect. To
  216. be safe, we apply the opposite rule to functions within this
  217. file, with local labels given to them to ensure correctness. */
  218. .Lsyscall_nosys:
  219. syscall_nosys:
  220. ldil L%syscall_exit,%r1
  221. be R%syscall_exit(%sr7,%r1)
  222. ldo -ENOSYS(%r0),%r28 /* set errno */
  223. /* Warning! This trace code is a virtual duplicate of the code above so be
  224. * sure to maintain both! */
  225. .Ltracesys:
  226. tracesys:
  227. /* Need to save more registers so the debugger can see where we
  228. * are. This saves only the lower 8 bits of PSW, so that the C
  229. * bit is still clear on syscalls, and the D bit is set if this
  230. * full register save path has been executed. We check the D
  231. * bit on syscall_return_rfi to determine which registers to
  232. * restore. An interrupt results in a full PSW saved with the
  233. * C bit set, a non-straced syscall entry results in C and D clear
  234. * in the saved PSW.
  235. */
  236. ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */
  237. LDREG TI_TASK(%r1), %r1
  238. ssm 0,%r2
  239. STREG %r2,TASK_PT_PSW(%r1) /* Lower 8 bits only!! */
  240. mfsp %sr0,%r2
  241. STREG %r2,TASK_PT_SR0(%r1)
  242. mfsp %sr1,%r2
  243. STREG %r2,TASK_PT_SR1(%r1)
  244. mfsp %sr2,%r2
  245. STREG %r2,TASK_PT_SR2(%r1)
  246. mfsp %sr3,%r2
  247. STREG %r2,TASK_PT_SR3(%r1)
  248. STREG %r2,TASK_PT_SR4(%r1)
  249. STREG %r2,TASK_PT_SR5(%r1)
  250. STREG %r2,TASK_PT_SR6(%r1)
  251. STREG %r2,TASK_PT_SR7(%r1)
  252. STREG %r2,TASK_PT_IASQ0(%r1)
  253. STREG %r2,TASK_PT_IASQ1(%r1)
  254. LDREG TASK_PT_GR31(%r1),%r2
  255. STREG %r2,TASK_PT_IAOQ0(%r1)
  256. ldo 4(%r2),%r2
  257. STREG %r2,TASK_PT_IAOQ1(%r1)
  258. ldo TASK_REGS(%r1),%r2
  259. /* reg_save %r2 */
  260. STREG %r3,PT_GR3(%r2)
  261. STREG %r4,PT_GR4(%r2)
  262. STREG %r5,PT_GR5(%r2)
  263. STREG %r6,PT_GR6(%r2)
  264. STREG %r7,PT_GR7(%r2)
  265. STREG %r8,PT_GR8(%r2)
  266. STREG %r9,PT_GR9(%r2)
  267. STREG %r10,PT_GR10(%r2)
  268. STREG %r11,PT_GR11(%r2)
  269. STREG %r12,PT_GR12(%r2)
  270. STREG %r13,PT_GR13(%r2)
  271. STREG %r14,PT_GR14(%r2)
  272. STREG %r15,PT_GR15(%r2)
  273. STREG %r16,PT_GR16(%r2)
  274. STREG %r17,PT_GR17(%r2)
  275. STREG %r18,PT_GR18(%r2)
  276. /* Finished saving things for the debugger */
  277. copy %r2,%r26
  278. ldil L%do_syscall_trace_enter,%r1
  279. ldil L%tracesys_next,%r2
  280. be R%do_syscall_trace_enter(%sr7,%r1)
  281. ldo R%tracesys_next(%r2),%r2
  282. tracesys_next:
  283. /* do_syscall_trace_enter either returned the syscallno, or -1L,
  284. * so we skip restoring the PT_GR20 below, since we pulled it from
  285. * task->thread.regs.gr[20] above.
  286. */
  287. copy %ret0,%r20
  288. ldil L%sys_call_table,%r1
  289. ldo R%sys_call_table(%r1), %r19
  290. ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */
  291. LDREG TI_TASK(%r1), %r1
  292. LDREG TASK_PT_GR26(%r1), %r26 /* Restore the users args */
  293. LDREG TASK_PT_GR25(%r1), %r25
  294. LDREG TASK_PT_GR24(%r1), %r24
  295. LDREG TASK_PT_GR23(%r1), %r23
  296. LDREG TASK_PT_GR22(%r1), %r22
  297. LDREG TASK_PT_GR21(%r1), %r21
  298. #ifdef CONFIG_64BIT
  299. ldo -16(%r30),%r29 /* Reference param save area */
  300. #else
  301. stw %r22, -52(%r30) /* 5th argument */
  302. stw %r21, -56(%r30) /* 6th argument */
  303. #endif
  304. comiclr,>>= __NR_Linux_syscalls, %r20, %r0
  305. b,n .Lsyscall_nosys
  306. LDREGX %r20(%r19), %r19
  307. /* If this is a sys_rt_sigreturn call, and the signal was received
  308. * when not in_syscall, then we want to return via syscall_exit_rfi,
  309. * not syscall_exit. Signal no. in r20, in_syscall in r25 (see
  310. * trampoline code in signal.c).
  311. */
  312. ldi __NR_rt_sigreturn,%r2
  313. comb,= %r2,%r20,.Ltrace_rt_sigreturn
  314. .Ltrace_in_syscall:
  315. ldil L%tracesys_exit,%r2
  316. be 0(%sr7,%r19)
  317. ldo R%tracesys_exit(%r2),%r2
  318. /* Do *not* call this function on the gateway page, because it
  319. makes a direct call to syscall_trace. */
  320. tracesys_exit:
  321. ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */
  322. LDREG TI_TASK(%r1), %r1
  323. #ifdef CONFIG_64BIT
  324. ldo -16(%r30),%r29 /* Reference param save area */
  325. #endif
  326. ldo TASK_REGS(%r1),%r26
  327. bl do_syscall_trace_exit,%r2
  328. STREG %r28,TASK_PT_GR28(%r1) /* save return value now */
  329. ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */
  330. LDREG TI_TASK(%r1), %r1
  331. LDREG TASK_PT_GR28(%r1), %r28 /* Restore return val. */
  332. ldil L%syscall_exit,%r1
  333. be,n R%syscall_exit(%sr7,%r1)
  334. .Ltrace_rt_sigreturn:
  335. comib,<> 0,%r25,.Ltrace_in_syscall
  336. ldil L%tracesys_sigexit,%r2
  337. be 0(%sr7,%r19)
  338. ldo R%tracesys_sigexit(%r2),%r2
  339. tracesys_sigexit:
  340. ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */
  341. LDREG TI_TASK(%r1), %r1
  342. #ifdef CONFIG_64BIT
  343. ldo -16(%r30),%r29 /* Reference param save area */
  344. #endif
  345. bl do_syscall_trace_exit,%r2
  346. ldo TASK_REGS(%r1),%r26
  347. ldil L%syscall_exit_rfi,%r1
  348. be,n R%syscall_exit_rfi(%sr7,%r1)
  349. /*********************************************************
  350. 32/64-bit Light-Weight-Syscall ABI
  351. * - Indicates a hint for userspace inline asm
  352. implementations.
  353. Syscall number (caller-saves)
  354. - %r20
  355. * In asm clobber.
  356. Argument registers (caller-saves)
  357. - %r26, %r25, %r24, %r23, %r22
  358. * In asm input.
  359. Return registers (caller-saves)
  360. - %r28 (return), %r21 (errno)
  361. * In asm output.
  362. Caller-saves registers
  363. - %r1, %r27, %r29
  364. - %r2 (return pointer)
  365. - %r31 (ble link register)
  366. * In asm clobber.
  367. Callee-saves registers
  368. - %r3-%r18
  369. - %r30 (stack pointer)
  370. * Not in asm clobber.
  371. If userspace is 32-bit:
  372. Callee-saves registers
  373. - %r19 (32-bit PIC register)
  374. Differences from 32-bit calling convention:
  375. - Syscall number in %r20
  376. - Additional argument register %r22 (arg4)
  377. - Callee-saves %r19.
  378. If userspace is 64-bit:
  379. Callee-saves registers
  380. - %r27 (64-bit PIC register)
  381. Differences from 64-bit calling convention:
  382. - Syscall number in %r20
  383. - Additional argument register %r22 (arg4)
  384. - Callee-saves %r27.
  385. Error codes returned by entry path:
  386. ENOSYS - r20 was an invalid LWS number.
  387. *********************************************************/
  388. lws_start:
  389. #ifdef CONFIG_64BIT
  390. /* FIXME: If we are a 64-bit kernel just
  391. * turn this on unconditionally.
  392. */
  393. ssm PSW_SM_W, %r1
  394. extrd,u %r1,PSW_W_BIT,1,%r1
  395. /* sp must be aligned on 4, so deposit the W bit setting into
  396. * the bottom of sp temporarily */
  397. or,ev %r1,%r30,%r30
  398. /* Clip LWS number to a 32-bit value always */
  399. depdi 0, 31, 32, %r20
  400. #endif
  401. /* Is the lws entry number valid? */
  402. comiclr,>> __NR_lws_entries, %r20, %r0
  403. b,n lws_exit_nosys
  404. /* WARNING: Trashing sr2 and sr3 */
  405. mfsp %sr7,%r1 /* get userspace into sr3 */
  406. mtsp %r1,%sr3
  407. mtsp %r0,%sr2 /* get kernel space into sr2 */
  408. /* Load table start */
  409. ldil L%lws_table, %r1
  410. ldo R%lws_table(%r1), %r28 /* Scratch use of r28 */
  411. LDREGX %r20(%sr2,r28), %r21 /* Scratch use of r21 */
  412. /* Jump to lws, lws table pointers already relocated */
  413. be,n 0(%sr2,%r21)
  414. lws_exit_nosys:
  415. ldo -ENOSYS(%r0),%r21 /* set errno */
  416. /* Fall through: Return to userspace */
  417. lws_exit:
  418. #ifdef CONFIG_64BIT
  419. /* decide whether to reset the wide mode bit
  420. *
  421. * For a syscall, the W bit is stored in the lowest bit
  422. * of sp. Extract it and reset W if it is zero */
  423. extrd,u,*<> %r30,63,1,%r1
  424. rsm PSW_SM_W, %r0
  425. /* now reset the lowest bit of sp if it was set */
  426. xor %r30,%r1,%r30
  427. #endif
  428. be,n 0(%sr7, %r31)
  429. /***************************************************
  430. Implementing CAS as an atomic operation:
  431. %r26 - Address to examine
  432. %r25 - Old value to check (old)
  433. %r24 - New value to set (new)
  434. %r28 - Return prev through this register.
  435. %r21 - Kernel error code
  436. If debugging is DISabled:
  437. %r21 has the following meanings:
  438. EAGAIN - CAS is busy, ldcw failed, try again.
  439. EFAULT - Read or write failed.
  440. If debugging is enabled:
  441. EDEADLOCK - CAS called recursively.
  442. EAGAIN && r28 == 1 - CAS is busy. Lock contended.
  443. EAGAIN && r28 == 2 - CAS is busy. ldcw failed.
  444. EFAULT - Read or write failed.
  445. Scratch: r20, r28, r1
  446. ****************************************************/
  447. /* Do not enable LWS debugging */
  448. #define ENABLE_LWS_DEBUG 0
  449. /* ELF64 Process entry path */
  450. lws_compare_and_swap64:
  451. #ifdef CONFIG_64BIT
  452. b,n lws_compare_and_swap
  453. #else
  454. /* If we are not a 64-bit kernel, then we don't
  455. * have 64-bit input registers, and calling
  456. * the 64-bit LWS CAS returns ENOSYS.
  457. */
  458. b,n lws_exit_nosys
  459. #endif
  460. /* ELF32 Process entry path */
  461. lws_compare_and_swap32:
  462. #ifdef CONFIG_64BIT
  463. /* Clip all the input registers */
  464. depdi 0, 31, 32, %r26
  465. depdi 0, 31, 32, %r25
  466. depdi 0, 31, 32, %r24
  467. #endif
  468. lws_compare_and_swap:
  469. /* Load start of lock table */
  470. ldil L%lws_lock_start, %r20
  471. ldo R%lws_lock_start(%r20), %r28
  472. /* Extract four bits from r26 and hash lock (Bits 4-7) */
  473. extru %r26, 27, 4, %r20
  474. /* Find lock to use, the hash is either one of 0 to
  475. 15, multiplied by 16 (keep it 16-byte aligned)
  476. and add to the lock table offset. */
  477. shlw %r20, 4, %r20
  478. add %r20, %r28, %r20
  479. # if ENABLE_LWS_DEBUG
  480. /*
  481. DEBUG, check for deadlock!
  482. If the thread register values are the same
  483. then we were the one that locked it last and
  484. this is a recurisve call that will deadlock.
  485. We *must* giveup this call and fail.
  486. */
  487. ldw 4(%sr2,%r20), %r28 /* Load thread register */
  488. /* WARNING: If cr27 cycles to the same value we have problems */
  489. mfctl %cr27, %r21 /* Get current thread register */
  490. cmpb,<>,n %r21, %r28, cas_lock /* Called recursive? */
  491. b lws_exit /* Return error! */
  492. ldo -EDEADLOCK(%r0), %r21
  493. cas_lock:
  494. cmpb,=,n %r0, %r28, cas_nocontend /* Is nobody using it? */
  495. ldo 1(%r0), %r28 /* 1st case */
  496. b lws_exit /* Contended... */
  497. ldo -EAGAIN(%r0), %r21 /* Spin in userspace */
  498. cas_nocontend:
  499. # endif
  500. /* ENABLE_LWS_DEBUG */
  501. LDCW 0(%sr2,%r20), %r28 /* Try to acquire the lock */
  502. cmpb,<>,n %r0, %r28, cas_action /* Did we get it? */
  503. cas_wouldblock:
  504. ldo 2(%r0), %r28 /* 2nd case */
  505. b lws_exit /* Contended... */
  506. ldo -EAGAIN(%r0), %r21 /* Spin in userspace */
  507. /*
  508. prev = *addr;
  509. if ( prev == old )
  510. *addr = new;
  511. return prev;
  512. */
  513. /* NOTES:
  514. This all works becuse intr_do_signal
  515. and schedule both check the return iasq
  516. and see that we are on the kernel page
  517. so this process is never scheduled off
  518. or is ever sent any signal of any sort,
  519. thus it is wholly atomic from usrspaces
  520. perspective
  521. */
  522. cas_action:
  523. #if defined CONFIG_SMP && ENABLE_LWS_DEBUG
  524. /* DEBUG */
  525. mfctl %cr27, %r1
  526. stw %r1, 4(%sr2,%r20)
  527. #endif
  528. /* The load and store could fail */
  529. 1: ldw 0(%sr3,%r26), %r28
  530. sub,<> %r28, %r25, %r0
  531. 2: stw %r24, 0(%sr3,%r26)
  532. /* Free lock */
  533. stw %r20, 0(%sr2,%r20)
  534. #if ENABLE_LWS_DEBUG
  535. /* Clear thread register indicator */
  536. stw %r0, 4(%sr2,%r20)
  537. #endif
  538. /* Return to userspace, set no error */
  539. b lws_exit
  540. copy %r0, %r21
  541. 3:
  542. /* Error occurred on load or store */
  543. /* Free lock */
  544. stw %r20, 0(%sr2,%r20)
  545. #if ENABLE_LWS_DEBUG
  546. stw %r0, 4(%sr2,%r20)
  547. #endif
  548. b lws_exit
  549. ldo -EFAULT(%r0),%r21 /* set errno */
  550. nop
  551. nop
  552. nop
  553. nop
  554. /* Two exception table entries, one for the load,
  555. the other for the store. Either return -EFAULT.
  556. Each of the entries must be relocated. */
  557. .section __ex_table,"aw"
  558. ASM_ULONG_INSN (1b - linux_gateway_page), (3b - linux_gateway_page)
  559. ASM_ULONG_INSN (2b - linux_gateway_page), (3b - linux_gateway_page)
  560. .previous
  561. /* Make sure nothing else is placed on this page */
  562. .align PAGE_SIZE
  563. END(linux_gateway_page)
  564. ENTRY(end_linux_gateway_page)
  565. /* Relocate symbols assuming linux_gateway_page is mapped
  566. to virtual address 0x0 */
  567. #define LWS_ENTRY(_name_) ASM_ULONG_INSN (lws_##_name_ - linux_gateway_page)
  568. .section .rodata,"a"
  569. .align 8
  570. /* Light-weight-syscall table */
  571. /* Start of lws table. */
  572. ENTRY(lws_table)
  573. LWS_ENTRY(compare_and_swap32) /* 0 - ELF32 Atomic compare and swap */
  574. LWS_ENTRY(compare_and_swap64) /* 1 - ELF64 Atomic compare and swap */
  575. END(lws_table)
  576. /* End of lws table */
  577. .align 8
  578. ENTRY(sys_call_table)
  579. #include "syscall_table.S"
  580. END(sys_call_table)
  581. #ifdef CONFIG_64BIT
  582. .align 8
  583. ENTRY(sys_call_table64)
  584. #define SYSCALL_TABLE_64BIT
  585. #include "syscall_table.S"
  586. END(sys_call_table64)
  587. #endif
  588. /*
  589. All light-weight-syscall atomic operations
  590. will use this set of locks
  591. NOTE: The lws_lock_start symbol must be
  592. at least 16-byte aligned for safe use
  593. with ldcw.
  594. */
  595. .section .data
  596. .align L1_CACHE_BYTES
  597. ENTRY(lws_lock_start)
  598. /* lws locks */
  599. .rept 16
  600. /* Keep locks aligned at 16-bytes */
  601. .word 1
  602. .word 0
  603. .word 0
  604. .word 0
  605. .endr
  606. END(lws_lock_start)
  607. .previous
  608. .end