_setjmp.S revision 85229
1// $FreeBSD: head/lib/libc/ia64/gen/_setjmp.S 85229 2001-10-20 15:19:08Z dfr $
2//
3// Copyright (c) 1999, 2000
4// Intel Corporation.
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12//    notice, this list of conditions and the following disclaimer.
13//
14// 2. Redistributions in binary form must reproduce the above copyright
15//   notice, this list of conditions and the following disclaimer in the
16//    documentation and/or other materials provided with the distribution.
17//
18// 3. All advertising materials mentioning features or use of this software
19//    must display the following acknowledgement:
20//
21//    This product includes software developed by Intel Corporation and
22//    its contributors.
23//
24// 4. Neither the name of Intel Corporation or its contributors may be
25//    used to endorse or promote products derived from this software
26//    without specific prior written permission.
27//
28// THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
29// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31// ARE DISCLAIMED.  IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
32// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
38// THE POSSIBILITY OF SUCH DAMAGE.
39//
40//
41
42//
43// Module Name:
44//
45//  setjmp.s
46//
47// Abstract:
48//
49//  Contains an implementation of setjmp and longjmp for the
50//  IA-64 architecture.
51
52    .file   "setjmp.s"
53
54#define     LOCORE
55#include    <machine/asm.h>
56#include    <machine/setjmp.h>
57
58// int _setjmp(struct jmp_buffer *)
59//
60//  Setup a non-local goto.
61//
62// Description:
63//
64//  SetJump stores the current register set in the area pointed to
65//  by "save".  It returns zero.  Subsequent calls to "LongJump" will
66//  restore the registers and return non-zero to the same location.
67//
68// On entry, r32 contains the pointer to the jmp_buffer
69//
70
71ENTRY(_setjmp, 1)
72    //
73    //  Make sure buffer is aligned at 16byte boundary
74    //
75    add     r10 = -0x10,r0  ;;  // mask the lower 4 bits
76    and     r32 = r32, r10;;
77    add     r32 = 0x10, r32;;   // move to next 16 byte boundary
78
79    add     r10 = J_PREDS, r32  // skip Unats & pfs save area
80    add     r11 = J_BSP, r32
81    //
82    //  save immediate context
83    //
84    mov     r2 = ar.bsp         // save backing store pointer
85    mov     r3 = pr             // save predicates
86    flushrs
87    ;;
88    //
89    // save user Unat register
90    //
91    mov     r16 = ar.lc         // save loop count register
92    mov     r14 = ar.unat       // save user Unat register
93
94    st8     [r10] = r3, J_LC-J_PREDS
95    st8     [r11] = r2, J_R4-J_BSP
96    ;;
97    st8     [r10] = r16, J_R5-J_LC
98    st8     [r32] = r14, J_NATS // Note: Unat at the
99                                // beginning of the save area
100    mov     r15 = ar.pfs
101    ;;
102    //
103    //  save preserved general registers & NaT's
104    //
105    st8.spill   [r11] = r4, J_R6-J_R4
106    ;;
107    st8.spill   [r10] = r5, J_R7-J_R5
108    ;;
109    st8.spill   [r11] = r6, J_SP-J_R6
110    ;;
111    st8.spill   [r10] = r7, J_F3-J_R7
112    ;;
113    st8.spill   [r11] = sp, J_F2-J_SP
114    ;;
115    //
116    // save spilled Unat and pfs registers
117    //
118    mov     r2 = ar.unat        // save Unat register after spill
119    ;;
120    st8     [r32] = r2, J_PFS-J_NATS    // save unat for spilled regs
121    ;;
122    st8     [r32] = r15         // save pfs
123    //
124    //  save floating registers
125    //
126    stf.spill   [r11] = f2, J_F4-J_F2
127    stf.spill   [r10] = f3, J_F5-J_F3
128    ;;
129    stf.spill   [r11] = f4, J_F16-J_F4
130    stf.spill   [r10] = f5, J_F17-J_F5
131    ;;
132    stf.spill   [r11] = f16, J_F18-J_F16
133    stf.spill   [r10] = f17, J_F19-J_F17
134    ;;
135    stf.spill   [r11] = f18, J_F20-J_F18
136    stf.spill   [r10] = f19, J_F21-J_F19
137    ;;
138    stf.spill   [r11] = f20, J_F22-J_F20
139    stf.spill   [r10] = f21, J_F23-J_F21
140    ;;
141    stf.spill   [r11] = f22, J_F24-J_F22
142    stf.spill   [r10] = f23, J_F25-J_F23
143    ;;
144    stf.spill   [r11] = f24, J_F26-J_F24
145    stf.spill   [r10] = f25, J_F27-J_F25
146    ;;
147    stf.spill   [r11] = f26, J_F28-J_F26
148    stf.spill   [r10] = f27, J_F29-J_F27
149    ;;
150    stf.spill   [r11] = f28, J_F30-J_F28
151    stf.spill   [r10] = f29, J_F31-J_F29
152    ;;
153    stf.spill   [r11] = f30, J_FPSR-J_F30
154    stf.spill   [r10] = f31, J_B0-J_F31     // size of f31 + fpsr
155    //
156    // save FPSR register & branch registers
157    //
158    mov     r2 = ar.fpsr    // save fpsr register
159    mov     r3 = b0
160    ;;
161    st8     [r11] = r2, J_B1-J_FPSR
162    st8     [r10] = r3, J_B2-J_B0
163    mov     r2 = b1
164    mov     r3 = b2
165    ;;
166    st8     [r11] = r2, J_B3-J_B1
167    st8     [r10] = r3, J_B4-J_B2
168    mov     r2 = b3
169    mov     r3 = b4
170    ;;
171    st8     [r11] = r2, J_B5-J_B3
172    st8     [r10] = r3
173    mov     r2 = b5
174    ;;
175    st8     [r11] = r2
176    ;;
177    //
178    // return
179    //
180    mov     r8 = r0         // return 0 from setjmp
181    mov     ar.unat = r14   // restore unat
182    br.ret.sptk b0
183
184END(_setjmp)
185
186
187//
188// void _longjmp(struct jmp_buffer *, int val)
189//
190//  Perform a non-local goto.
191//
192// Description:
193//
194//  LongJump initializes the register set to the values saved by a
195//  previous 'SetJump' and jumps to the return location saved by that
196//  'SetJump'.  This has the effect of unwinding the stack and returning
197//  for a second time to the 'SetJump'.
198//
199
200	WEAK_ALIAS(_longjmp,___longjmp)
201ENTRY(___longjmp, 2)
202    //
203    //  Make sure buffer is aligned at 16byte boundary
204    //
205    add     r10 = -0x10,r0  ;;  // mask the lower 4 bits
206    and     r32 = r32, r10;;
207    add     r32 = 0x10, r32;;   // move to next 16 byte boundary
208
209    //
210    // caching the return value as we do invala in the end
211    //
212    mov     r8 = r33            // return value
213
214    //
215    //  get immediate context
216    //
217    mov     r14 = ar.rsc        // get user RSC conf
218    add     r10 = J_PFS, r32    // get address of pfs
219    add     r11 = J_NATS, r32
220    ;;
221    ld8     r15 = [r10], J_BSP-J_PFS    // get pfs
222    ld8     r2 = [r11], J_LC-J_NATS     // get unat for spilled regs
223    ;;
224    mov     ar.unat = r2
225    ;;
226    ld8     r16 = [r10], J_PREDS-J_BSP  // get backing store pointer
227    mov     ar.rsc = r0         // put RSE in enforced lazy
228    mov     ar.pfs = r15
229    ;;
230
231    //
232    // while returning from longjmp the BSPSTORE and BSP needs to be
233    // same and discard all the registers allocated after we did
234    // setjmp. Also, we need to generate the RNAT register since we
235    // did not flushed the RSE on setjmp.
236    //
237    mov     r17 = ar.bspstore   // get current BSPSTORE
238    ;;
239    cmp.ltu p6,p7 = r17, r16    // is it less than BSP of
240(p6)    br.spnt.few .flush_rse
241    mov     r19 = ar.rnat       // get current RNAT
242    ;;
243    loadrs                      // invalidate dirty regs
244    br.sptk.many    .restore_rnat       // restore RNAT
245
246.flush_rse:
247    flushrs
248    ;;
249    mov     r19 = ar.rnat       // get current RNAT
250    mov     r17 = r16           // current BSPSTORE
251    ;;
252.restore_rnat:
253    //
254    // check if RNAT is saved between saved BSP and curr BSPSTORE
255    //
256    mov     r18 = 0x3f
257    ;;
258    dep     r18 = r18,r16,3,6   // get RNAT address
259    ;;
260    cmp.ltu p8,p9 = r18, r17    // RNAT saved on RSE
261    ;;
262(p8)    ld8     r19 = [r18]     // get RNAT from RSE
263    ;;
264    mov     ar.bspstore = r16   // set new BSPSTORE
265    ;;
266    mov     ar.rnat = r19       // restore RNAT
267    mov     ar.rsc = r14        // restore RSC conf
268
269
270    ld8     r3 = [r11], J_R4-J_LC       // get lc register
271    ld8     r2 = [r10], J_R5-J_PREDS    // get predicates
272    ;;
273    mov     pr = r2, -1
274    mov     ar.lc = r3
275    //
276    //  restore preserved general registers & NaT's
277    //
278    ld8.fill    r4 = [r11], J_R6-J_R4
279    ;;
280    ld8.fill    r5 = [r10], J_R7-J_R5
281    ld8.fill    r6 = [r11], J_SP-J_R6
282    ;;
283    ld8.fill    r7 = [r10], J_F2-J_R7
284    ld8.fill    sp = [r11], J_F3-J_SP
285    ;;
286    //
287    //  restore floating registers
288    //
289    ldf.fill    f2 = [r10], J_F4-J_F2
290    ldf.fill    f3 = [r11], J_F5-J_F3
291    ;;
292    ldf.fill    f4 = [r10], J_F16-J_F4
293    ldf.fill    f5 = [r11], J_F17-J_F5
294    ;;
295    ldf.fill    f16 = [r10], J_F18-J_F16
296    ldf.fill    f17 = [r11], J_F19-J_F17
297    ;;
298    ldf.fill    f18 = [r10], J_F20-J_F18
299    ldf.fill    f19 = [r11], J_F21-J_F19
300    ;;
301    ldf.fill    f20 = [r10], J_F22-J_F20
302    ldf.fill    f21 = [r11], J_F23-J_F21
303    ;;
304    ldf.fill    f22 = [r10], J_F24-J_F22
305    ldf.fill    f23 = [r11], J_F25-J_F23
306    ;;
307    ldf.fill    f24 = [r10], J_F26-J_F24
308    ldf.fill    f25 = [r11], J_F27-J_F25
309    ;;
310    ldf.fill    f26 = [r10], J_F28-J_F26
311    ldf.fill    f27 = [r11], J_F29-J_F27
312    ;;
313    ldf.fill    f28 = [r10], J_F30-J_F28
314    ldf.fill    f29 = [r11], J_F31-J_F29
315    ;;
316    ldf.fill    f30 = [r10], J_FPSR-J_F30
317    ldf.fill    f31 = [r11], J_B0-J_F31 ;;
318
319    //
320    // restore branch registers and fpsr
321    //
322    ld8     r16 = [r10], J_B1-J_FPSR    // get fpsr
323    ld8     r17 = [r11], J_B2-J_B0      // get return pointer
324    ;;
325    mov     ar.fpsr = r16
326    mov     b0 = r17
327    ld8     r2 = [r10], J_B3-J_B1
328    ld8     r3 = [r11], J_B4-J_B2
329    ;;
330    mov     b1 = r2
331    mov     b2 = r3
332    ld8     r2 = [r10], J_B5-J_B3
333    ld8     r3 = [r11]
334    ;;
335    mov     b3 = r2
336    mov     b4 = r3
337    ld8     r2 = [r10]
338    ld8     r21 = [r32]         // get user unat
339    ;;
340    mov     b5 = r2
341    mov     ar.unat = r21
342
343    //
344    // invalidate ALAT
345    //
346    invala ;;
347
348    br.ret.sptk b0
349
350END(___longjmp)
351