gate.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * This file contains the code that gets mapped at the upper end of each task's text
  4. * region. For now, it contains the signal trampoline code only.
  5. *
  6. * Copyright (C) 1999-2003 Hewlett-Packard Co
  7. * David Mosberger-Tang <davidm@hpl.hp.com>
  8. */
  9. #include <asm/asmmacro.h>
  10. #include <asm/errno.h>
  11. #include <asm/asm-offsets.h>
  12. #include <asm/sigcontext.h>
  13. #include <asm/unistd.h>
  14. #include <asm/kregs.h>
  15. #include <asm/page.h>
  16. #include <asm/native/inst.h>
  17. /*
  18. * We can't easily refer to symbols inside the kernel. To avoid full runtime relocation,
  19. * complications with the linker (which likes to create PLT stubs for branches
  20. * to targets outside the shared object) and to avoid multi-phase kernel builds, we
  21. * simply create minimalistic "patch lists" in special ELF sections.
  22. */
  23. .section ".data..patch.fsyscall_table", "a"
  24. .previous
  25. #define LOAD_FSYSCALL_TABLE(reg) \
  26. [1:] movl reg=0; \
  27. .xdata4 ".data..patch.fsyscall_table", 1b-.
  28. .section ".data..patch.brl_fsys_bubble_down", "a"
  29. .previous
  30. #define BRL_COND_FSYS_BUBBLE_DOWN(pr) \
  31. [1:](pr)brl.cond.sptk 0; \
  32. ;; \
  33. .xdata4 ".data..patch.brl_fsys_bubble_down", 1b-.
  34. GLOBAL_ENTRY(__kernel_syscall_via_break)
  35. .prologue
  36. .altrp b6
  37. .body
  38. /*
  39. * Note: for (fast) syscall restart to work, the break instruction must be
  40. * the first one in the bundle addressed by syscall_via_break.
  41. */
  42. { .mib
  43. break 0x100000
  44. nop.i 0
  45. br.ret.sptk.many b6
  46. }
  47. END(__kernel_syscall_via_break)
  48. # define ARG0_OFF (16 + IA64_SIGFRAME_ARG0_OFFSET)
  49. # define ARG1_OFF (16 + IA64_SIGFRAME_ARG1_OFFSET)
  50. # define ARG2_OFF (16 + IA64_SIGFRAME_ARG2_OFFSET)
  51. # define SIGHANDLER_OFF (16 + IA64_SIGFRAME_HANDLER_OFFSET)
  52. # define SIGCONTEXT_OFF (16 + IA64_SIGFRAME_SIGCONTEXT_OFFSET)
  53. # define FLAGS_OFF IA64_SIGCONTEXT_FLAGS_OFFSET
  54. # define CFM_OFF IA64_SIGCONTEXT_CFM_OFFSET
  55. # define FR6_OFF IA64_SIGCONTEXT_FR6_OFFSET
  56. # define BSP_OFF IA64_SIGCONTEXT_AR_BSP_OFFSET
  57. # define RNAT_OFF IA64_SIGCONTEXT_AR_RNAT_OFFSET
  58. # define UNAT_OFF IA64_SIGCONTEXT_AR_UNAT_OFFSET
  59. # define FPSR_OFF IA64_SIGCONTEXT_AR_FPSR_OFFSET
  60. # define PR_OFF IA64_SIGCONTEXT_PR_OFFSET
  61. # define RP_OFF IA64_SIGCONTEXT_IP_OFFSET
  62. # define SP_OFF IA64_SIGCONTEXT_R12_OFFSET
  63. # define RBS_BASE_OFF IA64_SIGCONTEXT_RBS_BASE_OFFSET
  64. # define LOADRS_OFF IA64_SIGCONTEXT_LOADRS_OFFSET
  65. # define base0 r2
  66. # define base1 r3
  67. /*
  68. * When we get here, the memory stack looks like this:
  69. *
  70. * +===============================+
  71. * | |
  72. * // struct sigframe //
  73. * | |
  74. * +-------------------------------+ <-- sp+16
  75. * | 16 byte of scratch |
  76. * | space |
  77. * +-------------------------------+ <-- sp
  78. *
  79. * The register stack looks _exactly_ the way it looked at the time the signal
  80. * occurred. In other words, we're treading on a potential mine-field: each
  81. * incoming general register may be a NaT value (including sp, in which case the
  82. * process ends up dying with a SIGSEGV).
  83. *
  84. * The first thing need to do is a cover to get the registers onto the backing
  85. * store. Once that is done, we invoke the signal handler which may modify some
  86. * of the machine state. After returning from the signal handler, we return
  87. * control to the previous context by executing a sigreturn system call. A signal
  88. * handler may call the rt_sigreturn() function to directly return to a given
  89. * sigcontext. However, the user-level sigreturn() needs to do much more than
  90. * calling the rt_sigreturn() system call as it needs to unwind the stack to
  91. * restore preserved registers that may have been saved on the signal handler's
  92. * call stack.
  93. */
  94. #define SIGTRAMP_SAVES \
  95. .unwabi 3, 's'; /* mark this as a sigtramp handler (saves scratch regs) */ \
  96. .unwabi @svr4, 's'; /* backwards compatibility with old unwinders (remove in v2.7) */ \
  97. .savesp ar.unat, UNAT_OFF+SIGCONTEXT_OFF; \
  98. .savesp ar.fpsr, FPSR_OFF+SIGCONTEXT_OFF; \
  99. .savesp pr, PR_OFF+SIGCONTEXT_OFF; \
  100. .savesp rp, RP_OFF+SIGCONTEXT_OFF; \
  101. .savesp ar.pfs, CFM_OFF+SIGCONTEXT_OFF; \
  102. .vframesp SP_OFF+SIGCONTEXT_OFF
  103. GLOBAL_ENTRY(__kernel_sigtramp)
  104. // describe the state that is active when we get here:
  105. .prologue
  106. SIGTRAMP_SAVES
  107. .body
  108. .label_state 1
  109. adds base0=SIGHANDLER_OFF,sp
  110. adds base1=RBS_BASE_OFF+SIGCONTEXT_OFF,sp
  111. br.call.sptk.many rp=1f
  112. 1:
  113. ld8 r17=[base0],(ARG0_OFF-SIGHANDLER_OFF) // get pointer to signal handler's plabel
  114. ld8 r15=[base1] // get address of new RBS base (or NULL)
  115. cover // push args in interrupted frame onto backing store
  116. ;;
  117. cmp.ne p1,p0=r15,r0 // do we need to switch rbs? (note: pr is saved by kernel)
  118. mov.m r9=ar.bsp // fetch ar.bsp
  119. .spillsp.p p1, ar.rnat, RNAT_OFF+SIGCONTEXT_OFF
  120. (p1) br.cond.spnt setup_rbs // yup -> (clobbers p8, r14-r16, and r18-r20)
  121. back_from_setup_rbs:
  122. alloc r8=ar.pfs,0,0,3,0
  123. ld8 out0=[base0],16 // load arg0 (signum)
  124. adds base1=(ARG1_OFF-(RBS_BASE_OFF+SIGCONTEXT_OFF)),base1
  125. ;;
  126. ld8 out1=[base1] // load arg1 (siginfop)
  127. ld8 r10=[r17],8 // get signal handler entry point
  128. ;;
  129. ld8 out2=[base0] // load arg2 (sigcontextp)
  130. ld8 gp=[r17] // get signal handler's global pointer
  131. adds base0=(BSP_OFF+SIGCONTEXT_OFF),sp
  132. ;;
  133. .spillsp ar.bsp, BSP_OFF+SIGCONTEXT_OFF
  134. st8 [base0]=r9 // save sc_ar_bsp
  135. adds base0=(FR6_OFF+SIGCONTEXT_OFF),sp
  136. adds base1=(FR6_OFF+16+SIGCONTEXT_OFF),sp
  137. ;;
  138. stf.spill [base0]=f6,32
  139. stf.spill [base1]=f7,32
  140. ;;
  141. stf.spill [base0]=f8,32
  142. stf.spill [base1]=f9,32
  143. mov b6=r10
  144. ;;
  145. stf.spill [base0]=f10,32
  146. stf.spill [base1]=f11,32
  147. ;;
  148. stf.spill [base0]=f12,32
  149. stf.spill [base1]=f13,32
  150. ;;
  151. stf.spill [base0]=f14,32
  152. stf.spill [base1]=f15,32
  153. br.call.sptk.many rp=b6 // call the signal handler
  154. .ret0: adds base0=(BSP_OFF+SIGCONTEXT_OFF),sp
  155. ;;
  156. ld8 r15=[base0] // fetch sc_ar_bsp
  157. mov r14=ar.bsp
  158. ;;
  159. cmp.ne p1,p0=r14,r15 // do we need to restore the rbs?
  160. (p1) br.cond.spnt restore_rbs // yup -> (clobbers r14-r18, f6 & f7)
  161. ;;
  162. back_from_restore_rbs:
  163. adds base0=(FR6_OFF+SIGCONTEXT_OFF),sp
  164. adds base1=(FR6_OFF+16+SIGCONTEXT_OFF),sp
  165. ;;
  166. ldf.fill f6=[base0],32
  167. ldf.fill f7=[base1],32
  168. ;;
  169. ldf.fill f8=[base0],32
  170. ldf.fill f9=[base1],32
  171. ;;
  172. ldf.fill f10=[base0],32
  173. ldf.fill f11=[base1],32
  174. ;;
  175. ldf.fill f12=[base0],32
  176. ldf.fill f13=[base1],32
  177. ;;
  178. ldf.fill f14=[base0],32
  179. ldf.fill f15=[base1],32
  180. mov r15=__NR_rt_sigreturn
  181. .restore sp // pop .prologue
  182. break __BREAK_SYSCALL
  183. .prologue
  184. SIGTRAMP_SAVES
  185. setup_rbs:
  186. mov ar.rsc=0 // put RSE into enforced lazy mode
  187. ;;
  188. .save ar.rnat, r19
  189. mov r19=ar.rnat // save RNaT before switching backing store area
  190. adds r14=(RNAT_OFF+SIGCONTEXT_OFF),sp
  191. mov r18=ar.bspstore
  192. mov ar.bspstore=r15 // switch over to new register backing store area
  193. ;;
  194. .spillsp ar.rnat, RNAT_OFF+SIGCONTEXT_OFF
  195. st8 [r14]=r19 // save sc_ar_rnat
  196. .body
  197. mov.m r16=ar.bsp // sc_loadrs <- (new bsp - new bspstore) << 16
  198. adds r14=(LOADRS_OFF+SIGCONTEXT_OFF),sp
  199. ;;
  200. invala
  201. sub r15=r16,r15
  202. extr.u r20=r18,3,6
  203. ;;
  204. mov ar.rsc=0xf // set RSE into eager mode, pl 3
  205. cmp.eq p8,p0=63,r20
  206. shl r15=r15,16
  207. ;;
  208. st8 [r14]=r15 // save sc_loadrs
  209. (p8) st8 [r18]=r19 // if bspstore points at RNaT slot, store RNaT there now
  210. .restore sp // pop .prologue
  211. br.cond.sptk back_from_setup_rbs
  212. .prologue
  213. SIGTRAMP_SAVES
  214. .spillsp ar.rnat, RNAT_OFF+SIGCONTEXT_OFF
  215. .body
  216. restore_rbs:
  217. // On input:
  218. // r14 = bsp1 (bsp at the time of return from signal handler)
  219. // r15 = bsp0 (bsp at the time the signal occurred)
  220. //
  221. // Here, we need to calculate bspstore0, the value that ar.bspstore needs
  222. // to be set to, based on bsp0 and the size of the dirty partition on
  223. // the alternate stack (sc_loadrs >> 16). This can be done with the
  224. // following algorithm:
  225. //
  226. // bspstore0 = rse_skip_regs(bsp0, -rse_num_regs(bsp1 - (loadrs >> 19), bsp1));
  227. //
  228. // This is what the code below does.
  229. //
  230. alloc r2=ar.pfs,0,0,0,0 // alloc null frame
  231. adds r16=(LOADRS_OFF+SIGCONTEXT_OFF),sp
  232. adds r18=(RNAT_OFF+SIGCONTEXT_OFF),sp
  233. ;;
  234. ld8 r17=[r16]
  235. ld8 r16=[r18] // get new rnat
  236. extr.u r18=r15,3,6 // r18 <- rse_slot_num(bsp0)
  237. ;;
  238. mov ar.rsc=r17 // put RSE into enforced lazy mode
  239. shr.u r17=r17,16
  240. ;;
  241. sub r14=r14,r17 // r14 (bspstore1) <- bsp1 - (sc_loadrs >> 16)
  242. shr.u r17=r17,3 // r17 <- (sc_loadrs >> 19)
  243. ;;
  244. loadrs // restore dirty partition
  245. extr.u r14=r14,3,6 // r14 <- rse_slot_num(bspstore1)
  246. ;;
  247. add r14=r14,r17 // r14 <- rse_slot_num(bspstore1) + (sc_loadrs >> 19)
  248. ;;
  249. shr.u r14=r14,6 // r14 <- (rse_slot_num(bspstore1) + (sc_loadrs >> 19))/0x40
  250. ;;
  251. sub r14=r14,r17 // r14 <- -rse_num_regs(bspstore1, bsp1)
  252. movl r17=0x8208208208208209
  253. ;;
  254. add r18=r18,r14 // r18 (delta) <- rse_slot_num(bsp0) - rse_num_regs(bspstore1,bsp1)
  255. setf.sig f7=r17
  256. cmp.lt p7,p0=r14,r0 // p7 <- (r14 < 0)?
  257. ;;
  258. (p7) adds r18=-62,r18 // delta -= 62
  259. ;;
  260. setf.sig f6=r18
  261. ;;
  262. xmpy.h f6=f6,f7
  263. ;;
  264. getf.sig r17=f6
  265. ;;
  266. add r17=r17,r18
  267. shr r18=r18,63
  268. ;;
  269. shr r17=r17,5
  270. ;;
  271. sub r17=r17,r18 // r17 = delta/63
  272. ;;
  273. add r17=r14,r17 // r17 <- delta/63 - rse_num_regs(bspstore1, bsp1)
  274. ;;
  275. shladd r15=r17,3,r15 // r15 <- bsp0 + 8*(delta/63 - rse_num_regs(bspstore1, bsp1))
  276. ;;
  277. mov ar.bspstore=r15 // switch back to old register backing store area
  278. ;;
  279. mov ar.rnat=r16 // restore RNaT
  280. mov ar.rsc=0xf // (will be restored later on from sc_ar_rsc)
  281. // invala not necessary as that will happen when returning to user-mode
  282. br.cond.sptk back_from_restore_rbs
  283. END(__kernel_sigtramp)
  284. /*
  285. * On entry:
  286. * r11 = saved ar.pfs
  287. * r15 = system call #
  288. * b0 = saved return address
  289. * b6 = return address
  290. * On exit:
  291. * r11 = saved ar.pfs
  292. * r15 = system call #
  293. * b0 = saved return address
  294. * all other "scratch" registers: undefined
  295. * all "preserved" registers: same as on entry
  296. */
  297. GLOBAL_ENTRY(__kernel_syscall_via_epc)
  298. .prologue
  299. .altrp b6
  300. .body
  301. {
  302. /*
  303. * Note: the kernel cannot assume that the first two instructions in this
  304. * bundle get executed. The remaining code must be safe even if
  305. * they do not get executed.
  306. */
  307. adds r17=-1024,r15 // A
  308. mov r10=0 // A default to successful syscall execution
  309. epc // B causes split-issue
  310. }
  311. ;;
  312. RSM_PSR_BE_I(r20, r22) // M2 (5 cyc to srlz.d)
  313. LOAD_FSYSCALL_TABLE(r14) // X
  314. ;;
  315. mov r16=IA64_KR(CURRENT) // M2 (12 cyc)
  316. shladd r18=r17,3,r14 // A
  317. mov r19=NR_syscalls-1 // A
  318. ;;
  319. lfetch [r18] // M0|1
  320. MOV_FROM_PSR(p0, r29, r8) // M2 (12 cyc)
  321. // If r17 is a NaT, p6 will be zero
  322. cmp.geu p6,p7=r19,r17 // A (sysnr > 0 && sysnr < 1024+NR_syscalls)?
  323. ;;
  324. mov r21=ar.fpsr // M2 (12 cyc)
  325. tnat.nz p10,p9=r15 // I0
  326. mov.i r26=ar.pfs // I0 (would stall anyhow due to srlz.d...)
  327. ;;
  328. srlz.d // M0 (forces split-issue) ensure PSR.BE==0
  329. (p6) ld8 r18=[r18] // M0|1
  330. nop.i 0
  331. ;;
  332. nop.m 0
  333. (p6) tbit.z.unc p8,p0=r18,0 // I0 (dual-issues with "mov b7=r18"!)
  334. nop.i 0
  335. ;;
  336. SSM_PSR_I(p8, p14, r25)
  337. (p6) mov b7=r18 // I0
  338. (p8) br.dptk.many b7 // B
  339. mov r27=ar.rsc // M2 (12 cyc)
  340. /*
  341. * brl.cond doesn't work as intended because the linker would convert this branch
  342. * into a branch to a PLT. Perhaps there will be a way to avoid this with some
  343. * future version of the linker. In the meantime, we just use an indirect branch
  344. * instead.
  345. */
  346. #ifdef CONFIG_ITANIUM
  347. (p6) add r14=-8,r14 // r14 <- addr of fsys_bubble_down entry
  348. ;;
  349. (p6) ld8 r14=[r14] // r14 <- fsys_bubble_down
  350. ;;
  351. (p6) mov b7=r14
  352. (p6) br.sptk.many b7
  353. #else
  354. BRL_COND_FSYS_BUBBLE_DOWN(p6)
  355. #endif
  356. SSM_PSR_I(p0, p14, r10)
  357. mov r10=-1
  358. (p10) mov r8=EINVAL
  359. (p9) mov r8=ENOSYS
  360. FSYS_RETURN
  361. END(__kernel_syscall_via_epc)