1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
29 #include <private/bionic_asm.h>
30 #include <machine/setjmp.h>
32 /*
33 * C library - _setjmp, _longjmp
34 *
35 * _longjmp(jmp_buf state, int value)
36 * will generate a "return(v)" from the last call to _setjmp(state) by restoring
37 * registers from the stack. The previous signal state is NOT restored.
38 *
39 * NOTE: x0 return value
40 * x9-x15 temporary registers
41 */
43 ENTRY(_setjmp)
44 /* store magic number */
45 ldr w9, .L_setjmp_magic
46 str w9, [x0, #(_JB_MAGIC * 4)]
48 /* store core registers */
49 mov x10, sp
50 stp x30, x10, [x0, #(_JB_CORE_BASE * 4 + 16 * 0)]
51 stp x28, x29, [x0, #(_JB_CORE_BASE * 4 + 16 * 1)]
52 stp x26, x27, [x0, #(_JB_CORE_BASE * 4 + 16 * 2)]
53 stp x24, x25, [x0, #(_JB_CORE_BASE * 4 + 16 * 3)]
54 stp x22, x23, [x0, #(_JB_CORE_BASE * 4 + 16 * 4)]
55 stp x20, x21, [x0, #(_JB_CORE_BASE * 4 + 16 * 5)]
56 str x19, [x0, #(_JB_CORE_BASE * 4 + 16 * 6)]
58 /* store floating point registers */
59 stp d14, d15, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 0)]
60 stp d12, d13, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 1)]
61 stp d10, d11, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 2)]
62 stp d8, d9, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 3)]
64 mov w0, wzr
65 ret
66 END(_setjmp)
68 .L_setjmp_magic:
69 .word _JB_MAGIC__SETJMP
71 ENTRY(_longjmp)
72 /* check magic */
73 ldr w9, .L_setjmp_magic
74 ldr w10, [x0, #(_JB_MAGIC * 4)]
75 cmp w9, w10
76 b.ne .L_fail
78 /* restore core registers */
79 ldp x30, x10, [x0, #(_JB_CORE_BASE * 4 + 16 * 0)]
80 mov sp, x10
81 ldp x28, x29, [x0, #(_JB_CORE_BASE * 4 + 16 * 1)]
82 ldp x26, x27, [x0, #(_JB_CORE_BASE * 4 + 16 * 2)]
83 ldp x24, x25, [x0, #(_JB_CORE_BASE * 4 + 16 * 3)]
84 ldp x22, x23, [x0, #(_JB_CORE_BASE * 4 + 16 * 4)]
85 ldp x20, x21, [x0, #(_JB_CORE_BASE * 4 + 16 * 5)]
86 ldr x19, [x0, #(_JB_CORE_BASE * 4 + 16 * 6)]
88 /* restore floating point registers */
89 ldp d14, d15, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 0)]
90 ldp d12, d13, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 1)]
91 ldp d10, d11, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 2)]
92 ldp d8, d9, [x0, #(_JB_FLOAT_BASE * 4 + 16 * 3)]
94 /* validate sp (sp mod 16 = 0) and lr (lr mod 4 = 0) */
95 tst x30, #3
96 b.ne .L_fail
97 mov x10, sp
98 tst x10, #15
99 b.ne .L_fail
101 /* set return value */
102 cmp w1, wzr
103 csinc w0, w1, wzr, ne
104 ret
106 /* validation failed, die die die */
107 .L_fail:
108 bl PIC_SYM(longjmperror, PLT)
109 bl PIC_SYM(abort, PLT)
110 b . - 8 /* Cannot get here */
111 END(_longjmp)