gate.S 11 KB

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