chiark / gitweb /
[PATCH] sync up with the 0.84 version of klibc
[elogind.git] / klibc / klibc / arch / ia64 / setjmp.S
1 /*
2  * IA-64 specific setjmp/longjmp routines
3  *
4  * Inspired by setjmp.s from the FreeBSD kernel.
5  */
6
7 #define J_UNAT          0
8 #define J_NATS          0x8
9 #define J_PFS           0x10
10 #define J_BSP           0x18
11 #define J_RNAT          0x20
12 #define J_PREDS         0x28
13 #define J_LC            0x30
14 #define J_R4            0x38
15 #define J_R5            0x40
16 #define J_R6            0x48
17 #define J_R7            0x50
18 #define J_SP            0x58
19 #define J_F2            0x60
20 #define J_F3            0x70
21 #define J_F4            0x80
22 #define J_F5            0x90
23 #define J_F16           0xa0
24 #define J_F17           0xb0
25 #define J_F18           0xc0
26 #define J_F19           0xd0
27 #define J_F20           0xe0
28 #define J_F21           0xf0
29 #define J_F22           0x100
30 #define J_F23           0x110
31 #define J_F24           0x120
32 #define J_F25           0x130
33 #define J_F26           0x140
34 #define J_F27           0x150
35 #define J_F28           0x160
36 #define J_F29           0x170
37 #define J_F30           0x180
38 #define J_F31           0x190
39 #define J_FPSR          0x1a0
40 #define J_B0            0x1a8
41 #define J_B1            0x1b0
42 #define J_B2            0x1b8
43 #define J_B3            0x1c0
44 #define J_B4            0x1c8
45 #define J_B5            0x1d0
46 #define J_SIGMASK       0x1d8
47 #define J_SIGSET        0x1e0
48 #define J_GP            0x1f0
49         
50 // int setjmp(struct jmp_buffer *)
51 //
52 //  Setup a non-local goto.
53 //
54 // Description:
55 //
56 //  SetJump stores the current register set in the area pointed to
57 //  by "save".  It returns zero.  Subsequent calls to "LongJump" will
58 //  restore the registers and return non-zero to the same location.
59 //
60 // On entry, r32 contains the pointer to the jmp_buffer
61 //
62         .align 32
63         .global setjmp
64 setjmp:
65     //
66     //  Make sure buffer is aligned at 16byte boundary
67     //
68     add     r10 = -0x10,r0  ;;  // mask the lower 4 bits
69     and     r32 = r32, r10;; 
70     add     r32 = 0x10, r32;;   // move to next 16 byte boundary
71
72     add     r10 = J_PREDS, r32  // skip Unats & pfs save area
73     add     r11 = J_BSP, r32
74     //
75     //  save immediate context
76     //
77     mov     r2 = ar.bsp         // save backing store pointer
78     mov     r3 = pr             // save predicates
79     flushrs
80     ;;
81     //
82     // save user Unat register
83     //
84     mov     r16 = ar.lc         // save loop count register
85     mov     r14 = ar.unat       // save user Unat register
86
87     st8     [r10] = r3, J_LC-J_PREDS
88     st8     [r11] = r2, J_R4-J_BSP
89     ;;
90     st8     [r10] = r16, J_R5-J_LC
91     st8     [r32] = r14, J_NATS // Note: Unat at the 
92                                 // beginning of the save area
93     mov     r15 = ar.pfs
94     ;;
95     //
96     //  save preserved general registers & NaT's
97     //
98     st8.spill   [r11] = r4, J_R6-J_R4
99     ;;
100     st8.spill   [r10] = r5, J_R7-J_R5 
101     ;;
102     st8.spill   [r11] = r6, J_SP-J_R6
103     ;;
104     st8.spill   [r10] = r7, J_F3-J_R7 
105     ;;
106     st8.spill   [r11] = sp, J_F2-J_SP
107     ;;
108     //
109     // save spilled Unat and pfs registers
110     //
111     mov     r2 = ar.unat        // save Unat register after spill
112     ;;
113     st8     [r32] = r2, J_PFS-J_NATS    // save unat for spilled regs
114     ;;
115     st8     [r32] = r15         // save pfs
116     //
117     //  save floating registers 
118     //
119     stf.spill   [r11] = f2, J_F4-J_F2
120     stf.spill   [r10] = f3, J_F5-J_F3 
121     ;;
122     stf.spill   [r11] = f4, J_F16-J_F4
123     stf.spill   [r10] = f5, J_F17-J_F5 
124     ;;
125     stf.spill   [r11] = f16, J_F18-J_F16
126     stf.spill   [r10] = f17, J_F19-J_F17 
127     ;;
128     stf.spill   [r11] = f18, J_F20-J_F18
129     stf.spill   [r10] = f19, J_F21-J_F19 
130     ;;
131     stf.spill   [r11] = f20, J_F22-J_F20
132     stf.spill   [r10] = f21, J_F23-J_F21 
133     ;;
134     stf.spill   [r11] = f22, J_F24-J_F22
135     stf.spill   [r10] = f23, J_F25-J_F23 
136     ;;
137     stf.spill   [r11] = f24, J_F26-J_F24
138     stf.spill   [r10] = f25, J_F27-J_F25 
139     ;;
140     stf.spill   [r11] = f26, J_F28-J_F26
141     stf.spill   [r10] = f27, J_F29-J_F27 
142     ;;
143     stf.spill   [r11] = f28, J_F30-J_F28
144     stf.spill   [r10] = f29, J_F31-J_F29 
145     ;;
146     stf.spill   [r11] = f30, J_FPSR-J_F30
147     stf.spill   [r10] = f31, J_B0-J_F31     // size of f31 + fpsr
148     //
149     // save FPSR register & branch registers
150     //
151     mov     r2 = ar.fpsr    // save fpsr register
152     mov     r3 = b0 
153     ;;
154     st8     [r11] = r2, J_B1-J_FPSR
155     st8     [r10] = r3, J_B2-J_B0
156     mov     r2 = b1
157     mov     r3 = b2 
158     ;;
159     st8     [r11] = r2, J_B3-J_B1
160     st8     [r10] = r3, J_B4-J_B2
161     mov     r2 = b3
162     mov     r3 = b4 
163     ;;
164     st8     [r11] = r2, J_B5-J_B3
165     st8     [r10] = r3
166     mov     r2 = b5 
167     ;;
168     st8     [r11] = r2
169     ;;
170     //
171     // return
172     //
173     mov     r8 = r0         // return 0 from setjmp
174     mov     ar.unat = r14   // restore unat
175     br.ret.sptk b0
176     .endp setjmp
177
178 //
179 // void longjmp(struct jmp_buffer *, int val)
180 //
181 //  Perform a non-local goto.
182 //
183 // Description:
184 //
185 //  LongJump initializes the register set to the values saved by a
186 //  previous 'SetJump' and jumps to the return location saved by that
187 //  'SetJump'.  This has the effect of unwinding the stack and returning
188 //  for a second time to the 'SetJump'.
189 //
190         
191         .align 32
192         .global longjmp
193 longjmp:
194     //
195     //  Make sure buffer is aligned at 16byte boundary
196     //
197     add     r10 = -0x10,r0  ;;  // mask the lower 4 bits
198     and     r32 = r32, r10;; 
199     add     r32 = 0x10, r32;;   // move to next 16 byte boundary
200
201     //
202     // caching the return value as we do invala in the end
203     //
204     mov     r8 = r33            // return value
205
206     //
207     //  get immediate context
208     //
209     mov     r14 = ar.rsc        // get user RSC conf 
210     add     r10 = J_PFS, r32    // get address of pfs
211     add     r11 = J_NATS, r32
212     ;;
213     ld8     r15 = [r10], J_BSP-J_PFS    // get pfs
214     ld8     r2 = [r11], J_LC-J_NATS     // get unat for spilled regs
215     ;;
216     mov     ar.unat = r2
217     ;;
218     ld8     r16 = [r10], J_PREDS-J_BSP  // get backing store pointer
219     mov     ar.rsc = r0         // put RSE in enforced lazy 
220     mov     ar.pfs = r15
221     ;;
222     
223     //
224     // while returning from longjmp the BSPSTORE and BSP needs to be
225     // same and discard all the registers allocated after we did
226     // setjmp. Also, we need to generate the RNAT register since we
227     // did not flushed the RSE on setjmp.
228     //
229     mov     r17 = ar.bspstore   // get current BSPSTORE
230     ;;
231     cmp.ltu p6,p7 = r17, r16    // is it less than BSP of 
232 (p6)    br.spnt.few .flush_rse
233     mov     r19 = ar.rnat       // get current RNAT
234     ;;
235     loadrs                      // invalidate dirty regs
236     br.sptk.many    .restore_rnat       // restore RNAT
237
238 .flush_rse:
239     flushrs
240     ;;
241     mov     r19 = ar.rnat       // get current RNAT
242     mov     r17 = r16           // current BSPSTORE
243     ;;
244 .restore_rnat:
245     //
246     // check if RNAT is saved between saved BSP and curr BSPSTORE
247     //
248     mov     r18 = 0x3f
249     ;; 
250     dep     r18 = r18,r16,3,6   // get RNAT address
251     ;;
252     cmp.ltu p8,p9 = r18, r17    // RNAT saved on RSE
253     ;;
254 (p8)    ld8     r19 = [r18]     // get RNAT from RSE
255     ;;
256     mov     ar.bspstore = r16   // set new BSPSTORE 
257     ;;
258     mov     ar.rnat = r19       // restore RNAT
259     mov     ar.rsc = r14        // restore RSC conf
260
261
262     ld8     r3 = [r11], J_R4-J_LC       // get lc register
263     ld8     r2 = [r10], J_R5-J_PREDS    // get predicates
264     ;;
265     mov     pr = r2, -1
266     mov     ar.lc = r3
267     //
268     //  restore preserved general registers & NaT's
269     //
270     ld8.fill    r4 = [r11], J_R6-J_R4
271     ;;
272     ld8.fill    r5 = [r10], J_R7-J_R5 
273     ld8.fill    r6 = [r11], J_SP-J_R6
274     ;;
275     ld8.fill    r7 = [r10], J_F2-J_R7
276     ld8.fill    sp = [r11], J_F3-J_SP
277     ;;
278     //
279     //  restore floating registers 
280     //
281     ldf.fill    f2 = [r10], J_F4-J_F2
282     ldf.fill    f3 = [r11], J_F5-J_F3 
283     ;;
284     ldf.fill    f4 = [r10], J_F16-J_F4
285     ldf.fill    f5 = [r11], J_F17-J_F5 
286     ;;
287     ldf.fill    f16 = [r10], J_F18-J_F16
288     ldf.fill    f17 = [r11], J_F19-J_F17
289     ;;
290     ldf.fill    f18 = [r10], J_F20-J_F18
291     ldf.fill    f19 = [r11], J_F21-J_F19
292     ;;
293     ldf.fill    f20 = [r10], J_F22-J_F20
294     ldf.fill    f21 = [r11], J_F23-J_F21
295     ;;
296     ldf.fill    f22 = [r10], J_F24-J_F22
297     ldf.fill    f23 = [r11], J_F25-J_F23 
298     ;;
299     ldf.fill    f24 = [r10], J_F26-J_F24
300     ldf.fill    f25 = [r11], J_F27-J_F25
301     ;;
302     ldf.fill    f26 = [r10], J_F28-J_F26
303     ldf.fill    f27 = [r11], J_F29-J_F27
304     ;;
305     ldf.fill    f28 = [r10], J_F30-J_F28
306     ldf.fill    f29 = [r11], J_F31-J_F29 
307     ;;
308     ldf.fill    f30 = [r10], J_FPSR-J_F30
309     ldf.fill    f31 = [r11], J_B0-J_F31 ;;
310
311     //
312     // restore branch registers and fpsr
313     //
314     ld8     r16 = [r10], J_B1-J_FPSR    // get fpsr
315     ld8     r17 = [r11], J_B2-J_B0      // get return pointer
316     ;;
317     mov     ar.fpsr = r16
318     mov     b0 = r17
319     ld8     r2 = [r10], J_B3-J_B1
320     ld8     r3 = [r11], J_B4-J_B2
321     ;;
322     mov     b1 = r2
323     mov     b2 = r3
324     ld8     r2 = [r10], J_B5-J_B3
325     ld8     r3 = [r11]
326     ;;
327     mov     b3 = r2
328     mov     b4 = r3 
329     ld8     r2 = [r10]
330     ld8     r21 = [r32]         // get user unat
331     ;;
332     mov     b5 = r2
333     mov     ar.unat = r21
334
335     //
336     // invalidate ALAT
337     //
338     invala ;;
339
340     br.ret.sptk b0
341     .endp longjmp