1 /*
2 * Copyright (C) 2016 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 */
28
29 #pragma once
30
31 namespace unwindstack {
32
33 #if defined(__arm__)
34
AsmGetRegs(void * reg_data)35 inline __attribute__((__always_inline__)) void AsmGetRegs(void* reg_data) {
36 asm volatile(
37 #if defined(__thumb__)
38 ".align 2\n"
39 "bx pc\n"
40 "nop\n"
41 #endif
42 ".code 32\n"
43 "stmia %[base], {r0-r12}\n"
44 "add r2, %[base], #52\n"
45 "mov r3, r13\n"
46 "mov r4, r14\n"
47 "mov r5, r15\n"
48 "stmia r2, {r3-r5}\n"
49 #if defined(__thumb__)
50 "orr %[base], pc, #1\n"
51 "bx %[base]\n"
52 #endif
53 : [base] "+r"(reg_data)
54 :
55 : "r2", "r3", "r4", "r5", "memory");
56 }
57
58 #elif defined(__aarch64__)
59
60 inline __attribute__((__always_inline__)) void AsmGetRegs(void* reg_data) {
61 asm volatile(
62 "1:\n"
63 "stp x0, x1, [%[base], #0]\n"
64 "stp x2, x3, [%[base], #16]\n"
65 "stp x4, x5, [%[base], #32]\n"
66 "stp x6, x7, [%[base], #48]\n"
67 "stp x8, x9, [%[base], #64]\n"
68 "stp x10, x11, [%[base], #80]\n"
69 "stp x12, x13, [%[base], #96]\n"
70 "stp x14, x15, [%[base], #112]\n"
71 "stp x16, x17, [%[base], #128]\n"
72 "stp x18, x19, [%[base], #144]\n"
73 "stp x20, x21, [%[base], #160]\n"
74 "stp x22, x23, [%[base], #176]\n"
75 "stp x24, x25, [%[base], #192]\n"
76 "stp x26, x27, [%[base], #208]\n"
77 "stp x28, x29, [%[base], #224]\n"
78 "str x30, [%[base], #240]\n"
79 "mov x12, sp\n"
80 "adr x13, 1b\n"
81 "stp x12, x13, [%[base], #248]\n"
82 : [base] "+r"(reg_data)
83 :
84 : "x12", "x13", "memory");
85 }
86
87 #elif defined(__riscv)
88
89 inline __attribute__((__always_inline__)) void AsmGetRegs(void* reg_data) {
90 asm volatile(
91 "1:\n"
92 "sd ra, 8(%[base])\n"
93 "sd sp, 16(%[base])\n"
94 "sd gp, 24(%[base])\n"
95 "sd tp, 32(%[base])\n"
96 "sd t0, 40(%[base])\n"
97 "sd t1, 48(%[base])\n"
98 "sd t2, 56(%[base])\n"
99 "sd s0, 64(%[base])\n"
100 "sd s1, 72(%[base])\n"
101 "sd a0, 80(%[base])\n"
102 "sd a1, 88(%[base])\n"
103 "sd a2, 96(%[base])\n"
104 "sd a3, 104(%[base])\n"
105 "sd a4, 112(%[base])\n"
106 "sd a5, 120(%[base])\n"
107 "sd a6, 128(%[base])\n"
108 "sd a7, 136(%[base])\n"
109 "sd s2, 144(%[base])\n"
110 "sd s3, 152(%[base])\n"
111 "sd s4, 160(%[base])\n"
112 "sd s5, 168(%[base])\n"
113 "sd s6, 176(%[base])\n"
114 "sd s7, 184(%[base])\n"
115 "sd s8, 192(%[base])\n"
116 "sd s9, 200(%[base])\n"
117 "sd s10, 208(%[base])\n"
118 "sd s11, 216(%[base])\n"
119 "sd t3, 224(%[base])\n"
120 "sd t4, 232(%[base])\n"
121 "sd t5, 240(%[base])\n"
122 "sd t6, 248(%[base])\n"
123 "csrr t1, 0xc22\n"
124 "sd t1, 256(%[base])\n"
125 "la t1, 1b\n"
126 "sd t1, 0(%[base])\n"
127 : [base] "+r"(reg_data)
128 :
129 : "t1", "memory");
130 }
131
132 #elif defined(__i386__) || defined(__x86_64__)
133
134 // Do not change this, some libraries depend on this function existing on
135 // these architectures.
136 extern "C" void AsmGetRegs(void* regs);
137
138 #endif
139
RegsGetLocal(Regs * regs)140 inline __attribute__((__always_inline__)) void RegsGetLocal(Regs* regs) {
141 AsmGetRegs(regs->RawData());
142 }
143
144 } // namespace unwindstack
145