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.
5 * Copyright (C) 1999-2003 Hewlett-Packard Co
6 * David Mosberger-Tang <davidm@hpl.hp.com>
10 #include <asm/asmmacro.h>
11 #include <asm/errno.h>
12 #include <asm/asm-offsets.h>
13 #include <asm/sigcontext.h>
14 #include <asm/system.h>
15 #include <asm/unistd.h>
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.
23 .section ".data.patch.fsyscall_table", "a"
25 #define LOAD_FSYSCALL_TABLE(reg) \
27 .xdata4 ".data.patch.fsyscall_table", 1b-.
29 .section ".data.patch.brl_fsys_bubble_down", "a"
31 #define BRL_COND_FSYS_BUBBLE_DOWN(pr) \
32 [1:](pr)brl.cond.sptk 0; \
34 .xdata4 ".data.patch.brl_fsys_bubble_down", 1b-.
36 GLOBAL_ENTRY(__kernel_syscall_via_break)
41 * Note: for (fast) syscall restart to work, the break instruction must be
42 * the first one in the bundle addressed by syscall_via_break.
49 END(__kernel_syscall_via_break)
51 # define ARG0_OFF (16 + IA64_SIGFRAME_ARG0_OFFSET)
52 # define ARG1_OFF (16 + IA64_SIGFRAME_ARG1_OFFSET)
53 # define ARG2_OFF (16 + IA64_SIGFRAME_ARG2_OFFSET)
54 # define SIGHANDLER_OFF (16 + IA64_SIGFRAME_HANDLER_OFFSET)
55 # define SIGCONTEXT_OFF (16 + IA64_SIGFRAME_SIGCONTEXT_OFFSET)
57 # define FLAGS_OFF IA64_SIGCONTEXT_FLAGS_OFFSET
58 # define CFM_OFF IA64_SIGCONTEXT_CFM_OFFSET
59 # define FR6_OFF IA64_SIGCONTEXT_FR6_OFFSET
60 # define BSP_OFF IA64_SIGCONTEXT_AR_BSP_OFFSET
61 # define RNAT_OFF IA64_SIGCONTEXT_AR_RNAT_OFFSET
62 # define UNAT_OFF IA64_SIGCONTEXT_AR_UNAT_OFFSET
63 # define FPSR_OFF IA64_SIGCONTEXT_AR_FPSR_OFFSET
64 # define PR_OFF IA64_SIGCONTEXT_PR_OFFSET
65 # define RP_OFF IA64_SIGCONTEXT_IP_OFFSET
66 # define SP_OFF IA64_SIGCONTEXT_R12_OFFSET
67 # define RBS_BASE_OFF IA64_SIGCONTEXT_RBS_BASE_OFFSET
68 # define LOADRS_OFF IA64_SIGCONTEXT_LOADRS_OFFSET
72 * When we get here, the memory stack looks like this:
74 * +===============================+
76 * // struct sigframe //
78 * +-------------------------------+ <-- sp+16
79 * | 16 byte of scratch |
81 * +-------------------------------+ <-- sp
83 * The register stack looks _exactly_ the way it looked at the time the signal
84 * occurred. In other words, we're treading on a potential mine-field: each
85 * incoming general register may be a NaT value (including sp, in which case the
86 * process ends up dying with a SIGSEGV).
88 * The first thing need to do is a cover to get the registers onto the backing
89 * store. Once that is done, we invoke the signal handler which may modify some
90 * of the machine state. After returning from the signal handler, we return
91 * control to the previous context by executing a sigreturn system call. A signal
92 * handler may call the rt_sigreturn() function to directly return to a given
93 * sigcontext. However, the user-level sigreturn() needs to do much more than
94 * calling the rt_sigreturn() system call as it needs to unwind the stack to
95 * restore preserved registers that may have been saved on the signal handler's
99 #define SIGTRAMP_SAVES \
100 .unwabi 3, 's'; /* mark this as a sigtramp handler (saves scratch regs) */ \
101 .unwabi @svr4, 's'; /* backwards compatibility with old unwinders (remove in v2.7) */ \
102 .savesp ar.unat, UNAT_OFF+SIGCONTEXT_OFF; \
103 .savesp ar.fpsr, FPSR_OFF+SIGCONTEXT_OFF; \
104 .savesp pr, PR_OFF+SIGCONTEXT_OFF; \
105 .savesp rp, RP_OFF+SIGCONTEXT_OFF; \
106 .savesp ar.pfs, CFM_OFF+SIGCONTEXT_OFF; \
107 .vframesp SP_OFF+SIGCONTEXT_OFF
109 GLOBAL_ENTRY(__kernel_sigtramp)
110 // describe the state that is active when we get here:
117 adds base0=SIGHANDLER_OFF,sp
118 adds base1=RBS_BASE_OFF+SIGCONTEXT_OFF,sp
119 br.call.sptk.many rp=1f
121 ld8 r17=[base0],(ARG0_OFF-SIGHANDLER_OFF) // get pointer to signal handler's plabel
122 ld8 r15=[base1] // get address of new RBS base (or NULL)
123 cover // push args in interrupted frame onto backing store
125 cmp.ne p1,p0=r15,r0 // do we need to switch rbs? (note: pr is saved by kernel)
126 mov.m r9=ar.bsp // fetch ar.bsp
127 .spillsp.p p1, ar.rnat, RNAT_OFF+SIGCONTEXT_OFF
128 (p1) br.cond.spnt setup_rbs // yup -> (clobbers p8, r14-r16, and r18-r20)
130 alloc r8=ar.pfs,0,0,3,0
131 ld8 out0=[base0],16 // load arg0 (signum)
132 adds base1=(ARG1_OFF-(RBS_BASE_OFF+SIGCONTEXT_OFF)),base1
134 ld8 out1=[base1] // load arg1 (siginfop)
135 ld8 r10=[r17],8 // get signal handler entry point
137 ld8 out2=[base0] // load arg2 (sigcontextp)
138 ld8 gp=[r17] // get signal handler's global pointer
139 adds base0=(BSP_OFF+SIGCONTEXT_OFF),sp
141 .spillsp ar.bsp, BSP_OFF+SIGCONTEXT_OFF
142 st8 [base0]=r9 // save sc_ar_bsp
143 adds base0=(FR6_OFF+SIGCONTEXT_OFF),sp
144 adds base1=(FR6_OFF+16+SIGCONTEXT_OFF),sp
146 stf.spill [base0]=f6,32
147 stf.spill [base1]=f7,32
149 stf.spill [base0]=f8,32
150 stf.spill [base1]=f9,32
153 stf.spill [base0]=f10,32
154 stf.spill [base1]=f11,32
156 stf.spill [base0]=f12,32
157 stf.spill [base1]=f13,32
159 stf.spill [base0]=f14,32
160 stf.spill [base1]=f15,32
161 br.call.sptk.many rp=b6 // call the signal handler
162 .ret0: adds base0=(BSP_OFF+SIGCONTEXT_OFF),sp
164 ld8 r15=[base0] // fetch sc_ar_bsp
167 cmp.ne p1,p0=r14,r15 // do we need to restore the rbs?
168 (p1) br.cond.spnt restore_rbs // yup -> (clobbers r14-r18, f6 & f7)
170 back_from_restore_rbs:
171 adds base0=(FR6_OFF+SIGCONTEXT_OFF),sp
172 adds base1=(FR6_OFF+16+SIGCONTEXT_OFF),sp
174 ldf.fill f6=[base0],32
175 ldf.fill f7=[base1],32
177 ldf.fill f8=[base0],32
178 ldf.fill f9=[base1],32
180 ldf.fill f10=[base0],32
181 ldf.fill f11=[base1],32
183 ldf.fill f12=[base0],32
184 ldf.fill f13=[base1],32
186 ldf.fill f14=[base0],32
187 ldf.fill f15=[base1],32
188 mov r15=__NR_rt_sigreturn
189 .restore sp // pop .prologue
190 break __BREAK_SYSCALL
195 mov ar.rsc=0 // put RSE into enforced lazy mode
198 mov r19=ar.rnat // save RNaT before switching backing store area
199 adds r14=(RNAT_OFF+SIGCONTEXT_OFF),sp
202 mov ar.bspstore=r15 // switch over to new register backing store area
205 .spillsp ar.rnat, RNAT_OFF+SIGCONTEXT_OFF
206 st8 [r14]=r19 // save sc_ar_rnat
208 mov.m r16=ar.bsp // sc_loadrs <- (new bsp - new bspstore) << 16
209 adds r14=(LOADRS_OFF+SIGCONTEXT_OFF),sp
215 mov ar.rsc=0xf // set RSE into eager mode, pl 3
219 st8 [r14]=r15 // save sc_loadrs
220 (p8) st8 [r18]=r19 // if bspstore points at RNaT slot, store RNaT there now
221 .restore sp // pop .prologue
222 br.cond.sptk back_from_setup_rbs
226 .spillsp ar.rnat, RNAT_OFF+SIGCONTEXT_OFF
230 // r14 = bsp1 (bsp at the time of return from signal handler)
231 // r15 = bsp0 (bsp at the time the signal occurred)
233 // Here, we need to calculate bspstore0, the value that ar.bspstore needs
234 // to be set to, based on bsp0 and the size of the dirty partition on
235 // the alternate stack (sc_loadrs >> 16). This can be done with the
236 // following algorithm:
238 // bspstore0 = rse_skip_regs(bsp0, -rse_num_regs(bsp1 - (loadrs >> 19), bsp1));
240 // This is what the code below does.
242 alloc r2=ar.pfs,0,0,0,0 // alloc null frame
243 adds r16=(LOADRS_OFF+SIGCONTEXT_OFF),sp
244 adds r18=(RNAT_OFF+SIGCONTEXT_OFF),sp
247 ld8 r16=[r18] // get new rnat
248 extr.u r18=r15,3,6 // r18 <- rse_slot_num(bsp0)
250 mov ar.rsc=r17 // put RSE into enforced lazy mode
253 sub r14=r14,r17 // r14 (bspstore1) <- bsp1 - (sc_loadrs >> 16)
254 shr.u r17=r17,3 // r17 <- (sc_loadrs >> 19)
256 loadrs // restore dirty partition
257 extr.u r14=r14,3,6 // r14 <- rse_slot_num(bspstore1)
259 add r14=r14,r17 // r14 <- rse_slot_num(bspstore1) + (sc_loadrs >> 19)
261 shr.u r14=r14,6 // r14 <- (rse_slot_num(bspstore1) + (sc_loadrs >> 19))/0x40
263 sub r14=r14,r17 // r14 <- -rse_num_regs(bspstore1, bsp1)
264 movl r17=0x8208208208208209
266 add r18=r18,r14 // r18 (delta) <- rse_slot_num(bsp0) - rse_num_regs(bspstore1,bsp1)
268 cmp.lt p7,p0=r14,r0 // p7 <- (r14 < 0)?
270 (p7) adds r18=-62,r18 // delta -= 62
283 sub r17=r17,r18 // r17 = delta/63
285 add r17=r14,r17 // r17 <- delta/63 - rse_num_regs(bspstore1, bsp1)
287 shladd r15=r17,3,r15 // r15 <- bsp0 + 8*(delta/63 - rse_num_regs(bspstore1, bsp1))
289 mov ar.bspstore=r15 // switch back to old register backing store area
291 mov ar.rnat=r16 // restore RNaT
292 mov ar.rsc=0xf // (will be restored later on from sc_ar_rsc)
293 // invala not necessary as that will happen when returning to user-mode
294 br.cond.sptk back_from_restore_rbs
295 END(__kernel_sigtramp)
300 * r15 = system call #
301 * b0 = saved return address
302 * b6 = return address
305 * r15 = system call #
306 * b0 = saved return address
307 * all other "scratch" registers: undefined
308 * all "preserved" registers: same as on entry
311 GLOBAL_ENTRY(__kernel_syscall_via_epc)
317 * Note: the kernel cannot assume that the first two instructions in this
318 * bundle get executed. The remaining code must be safe even if
319 * they do not get executed.
321 adds r17=-1024,r15 // A
322 mov r10=0 // A default to successful syscall execution
323 epc // B causes split-issue
326 rsm psr.be | psr.i // M2 (5 cyc to srlz.d)
327 LOAD_FSYSCALL_TABLE(r14) // X
329 mov r16=IA64_KR(CURRENT) // M2 (12 cyc)
330 shladd r18=r17,3,r14 // A
331 mov r19=NR_syscalls-1 // A
334 mov r29=psr // M2 (12 cyc)
335 // If r17 is a NaT, p6 will be zero
336 cmp.geu p6,p7=r19,r17 // A (sysnr > 0 && sysnr < 1024+NR_syscalls)?
338 mov r21=ar.fpsr // M2 (12 cyc)
339 tnat.nz p10,p9=r15 // I0
340 mov.i r26=ar.pfs // I0 (would stall anyhow due to srlz.d...)
342 srlz.d // M0 (forces split-issue) ensure PSR.BE==0
343 (p6) ld8 r18=[r18] // M0|1
347 (p6) tbit.z.unc p8,p0=r18,0 // I0 (dual-issues with "mov b7=r18"!)
351 (p6) mov b7=r18 // I0
352 (p8) br.dptk.many b7 // B
354 mov r27=ar.rsc // M2 (12 cyc)
356 * brl.cond doesn't work as intended because the linker would convert this branch
357 * into a branch to a PLT. Perhaps there will be a way to avoid this with some
358 * future version of the linker. In the meantime, we just use an indirect branch
361 #ifdef CONFIG_ITANIUM
362 (p6) add r14=-8,r14 // r14 <- addr of fsys_bubble_down entry
364 (p6) ld8 r14=[r14] // r14 <- fsys_bubble_down
369 BRL_COND_FSYS_BUBBLE_DOWN(p6)
376 END(__kernel_syscall_via_epc)