blob: e11ef68c3e2d3c86f20228185cd3e8c3b30c20c6 [file] [log] [blame]
Serban Constantinescue2104882013-09-26 11:37:10 +01001/*
2 * Copyright (C) 2013 The Android Open Source Project
Elliott Hughes54a74942014-01-03 16:40:37 -08003 * All rights reserved.
Serban Constantinescue2104882013-09-26 11:37:10 +01004 *
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 */
28
Elliott Hughes851e68a2014-02-19 16:53:20 -080029#include <private/bionic_asm.h>
Serban Constantinescue2104882013-09-26 11:37:10 +010030#include <machine/setjmp.h>
31
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 */
42
43ENTRY(_setjmp)
44 /* store magic number */
45 ldr w9, .L_setjmp_magic
46 str w9, [x0, #(_JB_MAGIC * 4)]
47
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)]
57
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)]
63
64 mov w0, wzr
65 ret
66END(_setjmp)
67
68.L_setjmp_magic:
69 .word _JB_MAGIC__SETJMP
70
71ENTRY(_longjmp)
72 /* check magic */
73 ldr w9, .L_setjmp_magic
74 ldr w10, [x0, #(_JB_MAGIC * 4)]
75 cmp w9, w10
Elliott Hughesfff3c0f2014-05-09 12:16:20 -070076 b.ne .L_fail
Serban Constantinescue2104882013-09-26 11:37:10 +010077
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)]
87
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)]
93
94 /* validate sp (sp mod 16 = 0) and lr (lr mod 4 = 0) */
95 tst x30, #3
Elliott Hughesfff3c0f2014-05-09 12:16:20 -070096 b.ne .L_fail
Serban Constantinescue2104882013-09-26 11:37:10 +010097 mov x10, sp
98 tst x10, #15
Elliott Hughesfff3c0f2014-05-09 12:16:20 -070099 b.ne .L_fail
Serban Constantinescue2104882013-09-26 11:37:10 +0100100
101 /* set return value */
102 cmp w1, wzr
103 csinc w0, w1, wzr, ne
104 ret
105
106 /* validation failed, die die die */
Elliott Hughesfff3c0f2014-05-09 12:16:20 -0700107.L_fail:
Elliott Hughes651a0682014-08-07 11:52:38 -0700108 bl longjmperror
109 bl abort
Serban Constantinescue2104882013-09-26 11:37:10 +0100110 b . - 8 /* Cannot get here */
111END(_longjmp)