1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#if !defined(__wasm__)
10
11#include "assembly.h"
12
13#define FROM_0_TO_15 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
14#define FROM_16_TO_31 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
15
16#define FROM_0_TO_31 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
17#define FROM_32_TO_63 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63
18
19#if defined(_AIX)
20 .toc
21#elif defined(__aarch64__) && defined(__ELF__) && defined(_LIBUNWIND_EXECUTE_ONLY_CODE)
22 .section .text,"axy",@progbits,unique,0
23#else
24 .text
25#endif
26
27#if !defined(__USING_SJLJ_EXCEPTIONS__)
28
29#if defined(__i386__)
30.att_syntax
31
32#
33# extern int __unw_getcontext(unw_context_t* thread_state)
34#
35# On entry:
36# + +
37# +-----------------------+
38# + thread_state pointer +
39# +-----------------------+
40# + return address +
41# +-----------------------+ <-- SP
42# + +
43#
44DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
45
46 _LIBUNWIND_CET_ENDBR
47 push %eax
48 movl 8(%esp), %eax
49 movl %ebx, 4(%eax)
50 movl %ecx, 8(%eax)
51 movl %edx, 12(%eax)
52 movl %edi, 16(%eax)
53 movl %esi, 20(%eax)
54 movl %ebp, 24(%eax)
55 movl %esp, %edx
56 addl $8, %edx
57 movl %edx, 28(%eax) # store what sp was at call site as esp
58 # skip ss
59 # skip eflags
60 movl 4(%esp), %edx
61 movl %edx, 40(%eax) # store return address as eip
62 # skip cs
63 # skip ds
64 # skip es
65 # skip fs
66 # skip gs
67 movl (%esp), %edx
68 movl %edx, (%eax) # store original eax
69 popl %eax
70 xorl %eax, %eax # return UNW_ESUCCESS
71 ret
72
73#elif defined(__arm64ec__)
74
75//
76// extern int __unw_getcontext(unw_context_t* thread_state)
77//
78// On entry:
79// thread_state pointer is in x0
80//
81 .section .text,"xr",discard,"#__unw_getcontext"
82 .p2align 2
83DEFINE_LIBUNWIND_FUNCTION("#__unw_getcontext")
84 stp x8, x27, [x0, #0x000] // rax, rbx
85 stp x0, x1, [x0, #0x010] // rcx, rdx
86 stp x26,x25, [x0, #0x020] // rdi, rsi
87 mov x1, sp
88 stp fp, x1, [x0, #0x030] // rbp, rsp
89 stp x2, x3, [x0, #0x040] // r8, r9
90 stp x4, x5, [x0, #0x050] // r10, r11
91 stp x19,x20, [x0, #0x060] // r12, r13
92 stp x21,x22, [x0, #0x070] // r14, r15
93 str x30, [x0, #0x080] // store return address as pc
94 stp q0, q1, [x0, #0x0b0] // xmm0, xmm1
95 stp q2, q3, [x0, #0x0d0] // xmm2, xmm3
96 stp q4, q5, [x0, #0x0f0] // xmm4, xmm5
97 stp q6, q7, [x0, #0x110] // xmm6, xmm7
98 stp q8, q9, [x0, #0x130] // xmm8, xmm9
99 stp q10,q11, [x0, #0x150] // xmm10,xmm11
100 stp q12,q13, [x0, #0x170] // xmm12,xmm13
101 stp q14,q15, [x0, #0x190] // xmm14,xmm15
102 mov x0, #0 // return UNW_ESUCCESS
103 ret
104
105 .weak_anti_dep __unw_getcontext
106 .set __unw_getcontext, "#__unw_getcontext"
107
108 .section .hybmp$x,"yi"
109 .symidx "#__unw_getcontext"
110 .symidx $ientry_thunk$cdecl$i8$i8
111 .word 1
112 .text
113
114#elif defined(__x86_64__)
115.att_syntax
116
117#
118# extern int __unw_getcontext(unw_context_t* thread_state)
119#
120# On entry:
121# thread_state pointer is in rdi
122#
123DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
124#if defined(_WIN64)
125#define PTR %rcx
126#define TMP %rdx
127#else
128#define PTR %rdi
129#define TMP %rsi
130#endif
131
132 _LIBUNWIND_CET_ENDBR
133 movq %rax, (PTR)
134 movq %rbx, 8(PTR)
135 movq %rcx, 16(PTR)
136 movq %rdx, 24(PTR)
137 movq %rdi, 32(PTR)
138 movq %rsi, 40(PTR)
139 movq %rbp, 48(PTR)
140 movq %rsp, 56(PTR)
141#if !defined(X86_64_BASE_SYSCALL)
142 addq $8, 56(PTR)
143#endif
144 movq %r8, 64(PTR)
145 movq %r9, 72(PTR)
146 movq %r10, 80(PTR)
147 movq %r11, 88(PTR)
148 movq %r12, 96(PTR)
149 movq %r13,104(PTR)
150# Skip spilling r14/r15 under LFI; reserved by LFI ABI.
151#if !defined(__LFI__)
152 movq %r14,112(PTR)
153 movq %r15,120(PTR)
154#endif
155 movq (%rsp),TMP
156 movq TMP,128(PTR) # store return address as rip
157#if defined(X86_64_BASE_SYSCALL)
158 # Capture flags before any instruction that changes them.
159 pushfq
160 popq TMP
161 movl %esi,136(PTR)
162 leaq 8(%rsp),TMP
163 movq TMP,56(PTR) # store what sp was at call site as rsp
164 movw %cs,140(PTR)
165 movw %fs,148(PTR)
166 movw %gs,150(PTR)
167 movw %es,146(PTR)
168 movw %ss,142(PTR)
169 movw %ds,144(PTR)
170 movq $0,152(PTR)
171 movq $0,160(PTR)
172
173 # Use syscalls rather than FSGSBASE instructions, which require both CPU
174 # support and OS enablement. All syscall-clobbered registers are saved above.
175 # FreeBSD may clear r8-r10 on syscall return. Keep the context pointer in
176 # the callee-saved rbx, preserving its value for our caller as well.
177 pushq %rbx
178 movq PTR,%rbx
179 movq $X86_64_BASE_SYSCALL,%rax
180 movq $X86_64_GET_FSBASE,%rdi
181 leaq 152(%rbx),%rsi
182 syscall
183 # FreeBSD reports errors in CF with a positive errno in rax.
184 jc 9f
185 movq $X86_64_BASE_SYSCALL,%rax
186 movq $X86_64_GET_GSBASE,%rdi
187 leaq 160(%rbx),%rsi
188 syscall
189 jc 9f
190 movq %rbx,PTR
191 popq %rbx
192#else
193 # skip rflags
194 # skip cs
195 # skip fs
196 # skip gs
197#endif
198
199#if defined(_WIN64)
200 movdqu %xmm0,176(PTR)
201 movdqu %xmm1,192(PTR)
202 movdqu %xmm2,208(PTR)
203 movdqu %xmm3,224(PTR)
204 movdqu %xmm4,240(PTR)
205 movdqu %xmm5,256(PTR)
206 movdqu %xmm6,272(PTR)
207 movdqu %xmm7,288(PTR)
208 movdqu %xmm8,304(PTR)
209 movdqu %xmm9,320(PTR)
210 movdqu %xmm10,336(PTR)
211 movdqu %xmm11,352(PTR)
212 movdqu %xmm12,368(PTR)
213 movdqu %xmm13,384(PTR)
214 movdqu %xmm14,400(PTR)
215 movdqu %xmm15,416(PTR)
216#endif
217 xorl %eax, %eax # return UNW_ESUCCESS
218 ret
219#if defined(X86_64_BASE_SYSCALL)
2209:
221 popq %rbx
222 movl $-6540,%eax # UNW_EUNSPEC: segment base capture failed
223 ret
224#endif
225
226#elif defined(__mips__) && defined(_ABIO32) && _MIPS_SIM == _ABIO32
227
228#
229# extern int __unw_getcontext(unw_context_t* thread_state)
230#
231# On entry:
232# thread_state pointer is in a0 ($4)
233#
234DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
235 .set push
236 .set noat
237 .set noreorder
238 .set nomacro
239 sw $1, (4 * 1)($4)
240 sw $2, (4 * 2)($4)
241 sw $3, (4 * 3)($4)
242 sw $4, (4 * 4)($4)
243 sw $5, (4 * 5)($4)
244 sw $6, (4 * 6)($4)
245 sw $7, (4 * 7)($4)
246 sw $8, (4 * 8)($4)
247 sw $9, (4 * 9)($4)
248 sw $10, (4 * 10)($4)
249 sw $11, (4 * 11)($4)
250 sw $12, (4 * 12)($4)
251 sw $13, (4 * 13)($4)
252 sw $14, (4 * 14)($4)
253 sw $15, (4 * 15)($4)
254 sw $16, (4 * 16)($4)
255 sw $17, (4 * 17)($4)
256 sw $18, (4 * 18)($4)
257 sw $19, (4 * 19)($4)
258 sw $20, (4 * 20)($4)
259 sw $21, (4 * 21)($4)
260 sw $22, (4 * 22)($4)
261 sw $23, (4 * 23)($4)
262 sw $24, (4 * 24)($4)
263 sw $25, (4 * 25)($4)
264 sw $26, (4 * 26)($4)
265 sw $27, (4 * 27)($4)
266 sw $28, (4 * 28)($4)
267 sw $29, (4 * 29)($4)
268 sw $30, (4 * 30)($4)
269 sw $31, (4 * 31)($4)
270 # Store return address to pc
271 sw $31, (4 * 32)($4)
272#if __mips_isa_rev < 6
273 # hi and lo
274 mfhi $8
275 sw $8, (4 * 33)($4)
276 mflo $8
277 sw $8, (4 * 34)($4)
278#endif
279#ifdef __mips_hard_float
280#if defined(__mips_single_float)
281 // A single-precision-only FPU has no sdc1. Each register still occupies
282 // an 8-byte slot so that the context layout matches the other o32 FPU
283 // configurations; only the low half of each slot is used.
284 swc1 $f0, (4 * 36 + 8 * 0)($4)
285 swc1 $f1, (4 * 36 + 8 * 1)($4)
286 swc1 $f2, (4 * 36 + 8 * 2)($4)
287 swc1 $f3, (4 * 36 + 8 * 3)($4)
288 swc1 $f4, (4 * 36 + 8 * 4)($4)
289 swc1 $f5, (4 * 36 + 8 * 5)($4)
290 swc1 $f6, (4 * 36 + 8 * 6)($4)
291 swc1 $f7, (4 * 36 + 8 * 7)($4)
292 swc1 $f8, (4 * 36 + 8 * 8)($4)
293 swc1 $f9, (4 * 36 + 8 * 9)($4)
294 swc1 $f10, (4 * 36 + 8 * 10)($4)
295 swc1 $f11, (4 * 36 + 8 * 11)($4)
296 swc1 $f12, (4 * 36 + 8 * 12)($4)
297 swc1 $f13, (4 * 36 + 8 * 13)($4)
298 swc1 $f14, (4 * 36 + 8 * 14)($4)
299 swc1 $f15, (4 * 36 + 8 * 15)($4)
300 swc1 $f16, (4 * 36 + 8 * 16)($4)
301 swc1 $f17, (4 * 36 + 8 * 17)($4)
302 swc1 $f18, (4 * 36 + 8 * 18)($4)
303 swc1 $f19, (4 * 36 + 8 * 19)($4)
304 swc1 $f20, (4 * 36 + 8 * 20)($4)
305 swc1 $f21, (4 * 36 + 8 * 21)($4)
306 swc1 $f22, (4 * 36 + 8 * 22)($4)
307 swc1 $f23, (4 * 36 + 8 * 23)($4)
308 swc1 $f24, (4 * 36 + 8 * 24)($4)
309 swc1 $f25, (4 * 36 + 8 * 25)($4)
310 swc1 $f26, (4 * 36 + 8 * 26)($4)
311 swc1 $f27, (4 * 36 + 8 * 27)($4)
312 swc1 $f28, (4 * 36 + 8 * 28)($4)
313 swc1 $f29, (4 * 36 + 8 * 29)($4)
314 swc1 $f30, (4 * 36 + 8 * 30)($4)
315 swc1 $f31, (4 * 36 + 8 * 31)($4)
316#elif __mips_fpr != 64
317 sdc1 $f0, (4 * 36 + 8 * 0)($4)
318 sdc1 $f2, (4 * 36 + 8 * 2)($4)
319 sdc1 $f4, (4 * 36 + 8 * 4)($4)
320 sdc1 $f6, (4 * 36 + 8 * 6)($4)
321 sdc1 $f8, (4 * 36 + 8 * 8)($4)
322 sdc1 $f10, (4 * 36 + 8 * 10)($4)
323 sdc1 $f12, (4 * 36 + 8 * 12)($4)
324 sdc1 $f14, (4 * 36 + 8 * 14)($4)
325 sdc1 $f16, (4 * 36 + 8 * 16)($4)
326 sdc1 $f18, (4 * 36 + 8 * 18)($4)
327 sdc1 $f20, (4 * 36 + 8 * 20)($4)
328 sdc1 $f22, (4 * 36 + 8 * 22)($4)
329 sdc1 $f24, (4 * 36 + 8 * 24)($4)
330 sdc1 $f26, (4 * 36 + 8 * 26)($4)
331 sdc1 $f28, (4 * 36 + 8 * 28)($4)
332 sdc1 $f30, (4 * 36 + 8 * 30)($4)
333#else
334 sdc1 $f0, (4 * 36 + 8 * 0)($4)
335 sdc1 $f1, (4 * 36 + 8 * 1)($4)
336 sdc1 $f2, (4 * 36 + 8 * 2)($4)
337 sdc1 $f3, (4 * 36 + 8 * 3)($4)
338 sdc1 $f4, (4 * 36 + 8 * 4)($4)
339 sdc1 $f5, (4 * 36 + 8 * 5)($4)
340 sdc1 $f6, (4 * 36 + 8 * 6)($4)
341 sdc1 $f7, (4 * 36 + 8 * 7)($4)
342 sdc1 $f8, (4 * 36 + 8 * 8)($4)
343 sdc1 $f9, (4 * 36 + 8 * 9)($4)
344 sdc1 $f10, (4 * 36 + 8 * 10)($4)
345 sdc1 $f11, (4 * 36 + 8 * 11)($4)
346 sdc1 $f12, (4 * 36 + 8 * 12)($4)
347 sdc1 $f13, (4 * 36 + 8 * 13)($4)
348 sdc1 $f14, (4 * 36 + 8 * 14)($4)
349 sdc1 $f15, (4 * 36 + 8 * 15)($4)
350 sdc1 $f16, (4 * 36 + 8 * 16)($4)
351 sdc1 $f17, (4 * 36 + 8 * 17)($4)
352 sdc1 $f18, (4 * 36 + 8 * 18)($4)
353 sdc1 $f19, (4 * 36 + 8 * 19)($4)
354 sdc1 $f20, (4 * 36 + 8 * 20)($4)
355 sdc1 $f21, (4 * 36 + 8 * 21)($4)
356 sdc1 $f22, (4 * 36 + 8 * 22)($4)
357 sdc1 $f23, (4 * 36 + 8 * 23)($4)
358 sdc1 $f24, (4 * 36 + 8 * 24)($4)
359 sdc1 $f25, (4 * 36 + 8 * 25)($4)
360 sdc1 $f26, (4 * 36 + 8 * 26)($4)
361 sdc1 $f27, (4 * 36 + 8 * 27)($4)
362 sdc1 $f28, (4 * 36 + 8 * 28)($4)
363 sdc1 $f29, (4 * 36 + 8 * 29)($4)
364 sdc1 $f30, (4 * 36 + 8 * 30)($4)
365 sdc1 $f31, (4 * 36 + 8 * 31)($4)
366#endif
367#endif
368 jr $31
369 # return UNW_ESUCCESS
370 or $2, $0, $0
371 .set pop
372
373#elif defined(__mips64)
374
375#
376# extern int __unw_getcontext(unw_context_t* thread_state)
377#
378# On entry:
379# thread_state pointer is in a0 ($4)
380#
381DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
382 .set push
383 .set noat
384 .set noreorder
385 .set nomacro
386 .irp i,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
387 sd $\i, (8 * \i)($4)
388 .endr
389 # Store return address to pc
390 sd $31, (8 * 32)($4)
391#if __mips_isa_rev < 6
392 # hi and lo
393 mfhi $8
394 sd $8, (8 * 33)($4)
395 mflo $8
396 sd $8, (8 * 34)($4)
397#endif
398#ifdef __mips_hard_float
399 .irp i,FROM_0_TO_31
400 sdc1 $f\i, (280+8*\i)($4)
401 .endr
402#endif
403 jr $31
404 # return UNW_ESUCCESS
405 or $2, $0, $0
406 .set pop
407
408# elif defined(__mips__)
409
410#
411# extern int __unw_getcontext(unw_context_t* thread_state)
412#
413# Just trap for the time being.
414DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
415 teq $0, $0
416
417#elif defined(__powerpc64__)
418
419//
420// extern int __unw_getcontext(unw_context_t* thread_state)
421//
422// On entry:
423// thread_state pointer is in r3
424//
425#if defined(_AIX)
426DEFINE_LIBUNWIND_FUNCTION_AND_WEAK_ALIAS(__unw_getcontext, unw_getcontext)
427#else
428DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
429#endif
430// store register (GPR)
431#define PPC64_STR(n) \
432 std n, (8 * (n + 2))(3)
433
434 // save GPRs
435 PPC64_STR(0)
436 mflr 0
437 std 0, PPC64_OFFS_SRR0(3) // store lr as ssr0
438 PPC64_STR(1)
439 PPC64_STR(4) // Save r4 first since it will be used for fixing r2.
440#if defined(_AIX)
441 // The TOC register (r2) was changed by the glue code if unw_getcontext
442 // is called from a different module. Save the original TOC register
443 // in the context if this is the case.
444 mflr 4
445 lwz 4, 0(4) // Get the first instruction at the return address.
446 xoris 0, 4, 0xe841 // Is it reloading the TOC register "ld 2,40(1)"?
447 cmplwi 0, 0x28
448 bne 0, LnoR2Fix // No need to fix up r2 if it is not.
449 ld 2, 40(1) // Use the saved TOC register in the stack.
450LnoR2Fix:
451#endif
452 PPC64_STR(2)
453 PPC64_STR(3)
454 PPC64_STR(5)
455 PPC64_STR(6)
456 PPC64_STR(7)
457 PPC64_STR(8)
458 PPC64_STR(9)
459 PPC64_STR(10)
460 PPC64_STR(11)
461 PPC64_STR(12)
462 PPC64_STR(13)
463 PPC64_STR(14)
464 PPC64_STR(15)
465 PPC64_STR(16)
466 PPC64_STR(17)
467 PPC64_STR(18)
468 PPC64_STR(19)
469 PPC64_STR(20)
470 PPC64_STR(21)
471 PPC64_STR(22)
472 PPC64_STR(23)
473 PPC64_STR(24)
474 PPC64_STR(25)
475 PPC64_STR(26)
476 PPC64_STR(27)
477 PPC64_STR(28)
478 PPC64_STR(29)
479 PPC64_STR(30)
480 PPC64_STR(31)
481
482 mfcr 0
483 std 0, PPC64_OFFS_CR(3)
484 mfxer 0
485 std 0, PPC64_OFFS_XER(3)
486#if defined(_AIX)
487 // LR value saved from the register is not used, initialize it to 0.
488 li 0, 0
489#else
490 mflr 0
491#endif
492 std 0, PPC64_OFFS_LR(3)
493 mfctr 0
494 std 0, PPC64_OFFS_CTR(3)
495 mfvrsave 0
496 std 0, PPC64_OFFS_VRSAVE(3)
497
498#if defined(__VSX__)
499 // save VS registers
500 // (note that this also saves floating point registers and V registers,
501 // because part of VS is mapped to these registers)
502
503 addi 4, 3, PPC64_OFFS_FP
504
505// store VS register
506#ifdef __LITTLE_ENDIAN__
507// For little-endian targets, we need a swap since stxvd2x will store the
508// register in the incorrect doubleword order.
509// FIXME: when supporting targets older than Power9 on LE is no longer required
510// this can be changed to simply `stxv n, 16 * n(4)`.
511// Restore the register after storing: xxswapd is its own inverse, so a second
512// xxswapd undoes the in-place corruption of callee-saved VSRs (f14-f31,
513// VR20-VR31) that would otherwise persist after unw_getcontext returns.
514#define PPC64_STVS(n) \
515 xxswapd n, n ;\
516 stxvd2x n, 0, 4 ;\
517 xxswapd n, n ;\
518 addi 4, 4, 16
519#else
520#define PPC64_STVS(n) \
521 stxvd2x n, 0, 4 ;\
522 addi 4, 4, 16
523#endif
524
525 PPC64_STVS(0)
526 PPC64_STVS(1)
527 PPC64_STVS(2)
528 PPC64_STVS(3)
529 PPC64_STVS(4)
530 PPC64_STVS(5)
531 PPC64_STVS(6)
532 PPC64_STVS(7)
533 PPC64_STVS(8)
534 PPC64_STVS(9)
535 PPC64_STVS(10)
536 PPC64_STVS(11)
537 PPC64_STVS(12)
538 PPC64_STVS(13)
539 PPC64_STVS(14)
540 PPC64_STVS(15)
541 PPC64_STVS(16)
542 PPC64_STVS(17)
543 PPC64_STVS(18)
544 PPC64_STVS(19)
545 PPC64_STVS(20)
546 PPC64_STVS(21)
547 PPC64_STVS(22)
548 PPC64_STVS(23)
549 PPC64_STVS(24)
550 PPC64_STVS(25)
551 PPC64_STVS(26)
552 PPC64_STVS(27)
553 PPC64_STVS(28)
554 PPC64_STVS(29)
555 PPC64_STVS(30)
556 PPC64_STVS(31)
557 PPC64_STVS(32)
558 PPC64_STVS(33)
559 PPC64_STVS(34)
560 PPC64_STVS(35)
561 PPC64_STVS(36)
562 PPC64_STVS(37)
563 PPC64_STVS(38)
564 PPC64_STVS(39)
565 PPC64_STVS(40)
566 PPC64_STVS(41)
567 PPC64_STVS(42)
568 PPC64_STVS(43)
569 PPC64_STVS(44)
570 PPC64_STVS(45)
571 PPC64_STVS(46)
572 PPC64_STVS(47)
573 PPC64_STVS(48)
574 PPC64_STVS(49)
575 PPC64_STVS(50)
576 PPC64_STVS(51)
577 PPC64_STVS(52)
578 PPC64_STVS(53)
579 PPC64_STVS(54)
580 PPC64_STVS(55)
581 PPC64_STVS(56)
582 PPC64_STVS(57)
583 PPC64_STVS(58)
584 PPC64_STVS(59)
585 PPC64_STVS(60)
586 PPC64_STVS(61)
587 PPC64_STVS(62)
588 PPC64_STVS(63)
589
590#else
591
592// store FP register
593#define PPC64_STF(n) \
594 stfd n, (PPC64_OFFS_FP + n * 16)(3)
595
596 // save float registers
597 PPC64_STF(0)
598 PPC64_STF(1)
599 PPC64_STF(2)
600 PPC64_STF(3)
601 PPC64_STF(4)
602 PPC64_STF(5)
603 PPC64_STF(6)
604 PPC64_STF(7)
605 PPC64_STF(8)
606 PPC64_STF(9)
607 PPC64_STF(10)
608 PPC64_STF(11)
609 PPC64_STF(12)
610 PPC64_STF(13)
611 PPC64_STF(14)
612 PPC64_STF(15)
613 PPC64_STF(16)
614 PPC64_STF(17)
615 PPC64_STF(18)
616 PPC64_STF(19)
617 PPC64_STF(20)
618 PPC64_STF(21)
619 PPC64_STF(22)
620 PPC64_STF(23)
621 PPC64_STF(24)
622 PPC64_STF(25)
623 PPC64_STF(26)
624 PPC64_STF(27)
625 PPC64_STF(28)
626 PPC64_STF(29)
627 PPC64_STF(30)
628 PPC64_STF(31)
629
630#if defined(__ALTIVEC__)
631 // save vector registers
632
633 // Use 16-bytes below the stack pointer as an
634 // aligned buffer to save each vector register.
635 // Note that the stack pointer is always 16-byte aligned.
636 subi 4, 1, 16
637
638#define PPC64_STV_UNALIGNED(n) \
639 stvx n, 0, 4 ;\
640 ld 5, 0(4) ;\
641 std 5, (PPC64_OFFS_V + n * 16)(3) ;\
642 ld 5, 8(4) ;\
643 std 5, (PPC64_OFFS_V + n * 16 + 8)(3)
644
645 PPC64_STV_UNALIGNED(0)
646 PPC64_STV_UNALIGNED(1)
647 PPC64_STV_UNALIGNED(2)
648 PPC64_STV_UNALIGNED(3)
649 PPC64_STV_UNALIGNED(4)
650 PPC64_STV_UNALIGNED(5)
651 PPC64_STV_UNALIGNED(6)
652 PPC64_STV_UNALIGNED(7)
653 PPC64_STV_UNALIGNED(8)
654 PPC64_STV_UNALIGNED(9)
655 PPC64_STV_UNALIGNED(10)
656 PPC64_STV_UNALIGNED(11)
657 PPC64_STV_UNALIGNED(12)
658 PPC64_STV_UNALIGNED(13)
659 PPC64_STV_UNALIGNED(14)
660 PPC64_STV_UNALIGNED(15)
661 PPC64_STV_UNALIGNED(16)
662 PPC64_STV_UNALIGNED(17)
663 PPC64_STV_UNALIGNED(18)
664 PPC64_STV_UNALIGNED(19)
665 PPC64_STV_UNALIGNED(20)
666 PPC64_STV_UNALIGNED(21)
667 PPC64_STV_UNALIGNED(22)
668 PPC64_STV_UNALIGNED(23)
669 PPC64_STV_UNALIGNED(24)
670 PPC64_STV_UNALIGNED(25)
671 PPC64_STV_UNALIGNED(26)
672 PPC64_STV_UNALIGNED(27)
673 PPC64_STV_UNALIGNED(28)
674 PPC64_STV_UNALIGNED(29)
675 PPC64_STV_UNALIGNED(30)
676 PPC64_STV_UNALIGNED(31)
677
678#endif
679#endif
680
681 li 3, 0 // return UNW_ESUCCESS
682 blr
683
684
685#elif defined(__powerpc__)
686
687//
688// extern int unw_getcontext(unw_context_t* thread_state)
689//
690// On entry:
691// thread_state pointer is in r3
692//
693#if defined(_AIX)
694DEFINE_LIBUNWIND_FUNCTION_AND_WEAK_ALIAS(__unw_getcontext, unw_getcontext)
695#else
696DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
697#endif
698 stw 0, 8(3)
699 mflr 0
700 stw 0, 0(3) // store lr as ssr0
701 stw 1, 12(3)
702 stw 4, 24(3) // Save r4 first since it will be used for fixing r2.
703#if defined(_AIX)
704 // The TOC register (r2) was changed by the glue code if unw_getcontext
705 // is called from a different module. Save the original TOC register
706 // in the context if this is the case.
707 mflr 4
708 lwz 4, 0(4) // Get the instruction at the return address.
709 xoris 0, 4, 0x8041 // Is it reloading the TOC register "lwz 2,20(1)"?
710 cmplwi 0, 0x14
711 bne 0, LnoR2Fix // No need to fix up r2 if it is not.
712 lwz 2, 20(1) // Use the saved TOC register in the stack.
713LnoR2Fix:
714#endif
715 stw 2, 16(3)
716 stw 3, 20(3)
717 stw 5, 28(3)
718 stw 6, 32(3)
719 stw 7, 36(3)
720 stw 8, 40(3)
721 stw 9, 44(3)
722 stw 10, 48(3)
723 stw 11, 52(3)
724 stw 12, 56(3)
725 stw 13, 60(3)
726 stw 14, 64(3)
727 stw 15, 68(3)
728 stw 16, 72(3)
729 stw 17, 76(3)
730 stw 18, 80(3)
731 stw 19, 84(3)
732 stw 20, 88(3)
733 stw 21, 92(3)
734 stw 22, 96(3)
735 stw 23,100(3)
736 stw 24,104(3)
737 stw 25,108(3)
738 stw 26,112(3)
739 stw 27,116(3)
740 stw 28,120(3)
741 stw 29,124(3)
742 stw 30,128(3)
743 stw 31,132(3)
744
745#if defined(__ALTIVEC__)
746 // save VRSave register
747 mfspr 0, 256
748 stw 0, 156(3)
749#endif
750 // save CR registers
751 mfcr 0
752 stw 0, 136(3)
753#if defined(_AIX)
754 // LR value from the register is not used, initialize it to 0.
755 li 0, 0
756 stw 0, 144(3)
757#endif
758 // save CTR register
759 mfctr 0
760 stw 0, 148(3)
761
762#if !defined(__NO_FPRS__)
763 // save float registers
764 stfd 0, 160(3)
765 stfd 1, 168(3)
766 stfd 2, 176(3)
767 stfd 3, 184(3)
768 stfd 4, 192(3)
769 stfd 5, 200(3)
770 stfd 6, 208(3)
771 stfd 7, 216(3)
772 stfd 8, 224(3)
773 stfd 9, 232(3)
774 stfd 10,240(3)
775 stfd 11,248(3)
776 stfd 12,256(3)
777 stfd 13,264(3)
778 stfd 14,272(3)
779 stfd 15,280(3)
780 stfd 16,288(3)
781 stfd 17,296(3)
782 stfd 18,304(3)
783 stfd 19,312(3)
784 stfd 20,320(3)
785 stfd 21,328(3)
786 stfd 22,336(3)
787 stfd 23,344(3)
788 stfd 24,352(3)
789 stfd 25,360(3)
790 stfd 26,368(3)
791 stfd 27,376(3)
792 stfd 28,384(3)
793 stfd 29,392(3)
794 stfd 30,400(3)
795 stfd 31,408(3)
796#endif
797
798#if defined(__ALTIVEC__)
799 // save vector registers
800
801 subi 4, 1, 16
802 rlwinm 4, 4, 0, 0, 27 // mask low 4-bits
803 // r4 is now a 16-byte aligned pointer into the red zone
804
805#define SAVE_VECTOR_UNALIGNED(_vec, _offset) \
806 stvx _vec, 0, 4 SEPARATOR \
807 lwz 5, 0(4) SEPARATOR \
808 stw 5, _offset(3) SEPARATOR \
809 lwz 5, 4(4) SEPARATOR \
810 stw 5, _offset+4(3) SEPARATOR \
811 lwz 5, 8(4) SEPARATOR \
812 stw 5, _offset+8(3) SEPARATOR \
813 lwz 5, 12(4) SEPARATOR \
814 stw 5, _offset+12(3)
815
816 SAVE_VECTOR_UNALIGNED( 0, 424+0x000)
817 SAVE_VECTOR_UNALIGNED( 1, 424+0x010)
818 SAVE_VECTOR_UNALIGNED( 2, 424+0x020)
819 SAVE_VECTOR_UNALIGNED( 3, 424+0x030)
820 SAVE_VECTOR_UNALIGNED( 4, 424+0x040)
821 SAVE_VECTOR_UNALIGNED( 5, 424+0x050)
822 SAVE_VECTOR_UNALIGNED( 6, 424+0x060)
823 SAVE_VECTOR_UNALIGNED( 7, 424+0x070)
824 SAVE_VECTOR_UNALIGNED( 8, 424+0x080)
825 SAVE_VECTOR_UNALIGNED( 9, 424+0x090)
826 SAVE_VECTOR_UNALIGNED(10, 424+0x0A0)
827 SAVE_VECTOR_UNALIGNED(11, 424+0x0B0)
828 SAVE_VECTOR_UNALIGNED(12, 424+0x0C0)
829 SAVE_VECTOR_UNALIGNED(13, 424+0x0D0)
830 SAVE_VECTOR_UNALIGNED(14, 424+0x0E0)
831 SAVE_VECTOR_UNALIGNED(15, 424+0x0F0)
832 SAVE_VECTOR_UNALIGNED(16, 424+0x100)
833 SAVE_VECTOR_UNALIGNED(17, 424+0x110)
834 SAVE_VECTOR_UNALIGNED(18, 424+0x120)
835 SAVE_VECTOR_UNALIGNED(19, 424+0x130)
836 SAVE_VECTOR_UNALIGNED(20, 424+0x140)
837 SAVE_VECTOR_UNALIGNED(21, 424+0x150)
838 SAVE_VECTOR_UNALIGNED(22, 424+0x160)
839 SAVE_VECTOR_UNALIGNED(23, 424+0x170)
840 SAVE_VECTOR_UNALIGNED(24, 424+0x180)
841 SAVE_VECTOR_UNALIGNED(25, 424+0x190)
842 SAVE_VECTOR_UNALIGNED(26, 424+0x1A0)
843 SAVE_VECTOR_UNALIGNED(27, 424+0x1B0)
844 SAVE_VECTOR_UNALIGNED(28, 424+0x1C0)
845 SAVE_VECTOR_UNALIGNED(29, 424+0x1D0)
846 SAVE_VECTOR_UNALIGNED(30, 424+0x1E0)
847 SAVE_VECTOR_UNALIGNED(31, 424+0x1F0)
848#endif
849
850 li 3, 0 // return UNW_ESUCCESS
851 blr
852
853
854#elif defined(__aarch64__)
855
856#ifndef __has_feature
857#define __has_feature(__feature) 0
858#endif
859
860//
861// extern int __unw_getcontext(unw_context_t* thread_state)
862//
863// On entry:
864// thread_state pointer is in x0
865//
866 .p2align 2
867DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
868
869#if __has_feature(ptrauth_calls)
870 pacibsp
871#endif
872
873 stp x0, x1, [x0, #0x000]
874 stp x2, x3, [x0, #0x010]
875 stp x4, x5, [x0, #0x020]
876 stp x6, x7, [x0, #0x030]
877 stp x8, x9, [x0, #0x040]
878 stp x10,x11, [x0, #0x050]
879 stp x12,x13, [x0, #0x060]
880 stp x14,x15, [x0, #0x070]
881 stp x16,x17, [x0, #0x080]
882 stp x18,x19, [x0, #0x090]
883 stp x20,x21, [x0, #0x0A0]
884 stp x22,x23, [x0, #0x0B0]
885#if defined(__LFI__)
886 str x24, [x0, #0x0C0]
887 // Skip spilling x25-x28; reserved by LFI ABI.
888 str x29, [x0, #0x0E8]
889#else
890 stp x24,x25, [x0, #0x0C0]
891 stp x26,x27, [x0, #0x0D0]
892 stp x28,x29, [x0, #0x0E0]
893#endif
894 str x30, [x0, #0x0F0]
895 mov x1,sp
896 str x1, [x0, #0x0F8]
897 str x30, [x0, #0x100] // store return address as pc
898 str xzr, [x0, #0x108] // zero __ra_sign_state
899 // skip cpsr
900#if defined(__ARM_FP) && __ARM_FP != 0
901 stp d0, d1, [x0, #0x110]
902 stp d2, d3, [x0, #0x120]
903 stp d4, d5, [x0, #0x130]
904 stp d6, d7, [x0, #0x140]
905 stp d8, d9, [x0, #0x150]
906 stp d10,d11, [x0, #0x160]
907 stp d12,d13, [x0, #0x170]
908 stp d14,d15, [x0, #0x180]
909 stp d16,d17, [x0, #0x190]
910 stp d18,d19, [x0, #0x1A0]
911 stp d20,d21, [x0, #0x1B0]
912 stp d22,d23, [x0, #0x1C0]
913 stp d24,d25, [x0, #0x1D0]
914 stp d26,d27, [x0, #0x1E0]
915 stp d28,d29, [x0, #0x1F0]
916 str d30, [x0, #0x200]
917 str d31, [x0, #0x208]
918#endif
919 mov x0, #0 // return UNW_ESUCCESS
920
921#if __has_feature(ptrauth_calls)
922 retab
923#else
924 ret
925#endif
926
927//
928// extern "C" int64_t __libunwind_Registers_arm64_za_disable()
929//
930// This function implements the requirements of the __arm_za_disable ABI
931// routine, except that it will not abort; it will return a non-zero value
932// to signify the routine failed.
933//
934// Note: This function uses SME instructions. It must only be called if SME
935// has been confirmed to be available.
936//
937// On return:
938//
939// A status is placed in x0. A zero value indicates success; any non-zero
940// value indicates failure.
941//
942 .p2align 2
943DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_arm64_za_disable)
944 .variant_pcs __libunwind_Registers_arm64_za_disable
945#if defined(__LFI__)
946 mov x0, #1
947 ret
948#else
949#if __has_feature(ptrauth_calls)
950 pacibsp
951#endif
952 // If TPIDR2_EL0 is null, the subroutine just disables ZA.
953 .inst 0xd53bd0b0 // mrs x16, TPIDR2_EL0
954 cbz x16, 1f
955
956 // If any of the reserved bytes in the first 16 bytes of the TPIDR2 block are
957 // nonzero, return a non-zero value (libunwind will then abort).
958 ldrh w0, [x16, #10]
959 cbnz w0, 2f
960 ldr w0, [x16, #12]
961 cbnz w0, 2f
962
963 // If num_za_save_slices is zero, the subroutine just disables ZA.
964 ldrh w0, [x16, #8]
965 cbz x0, 1f
966
967 // If za_save_buffer is NULL, the subroutine just disables ZA.
968 ldr x16, [x16]
969 cbz x16, 1f
970
971 // Store ZA to za_save_buffer.
972 mov x15, xzr
9730:
974 .inst 0xe1206200 // str za[w15,0], [x16]
975 .inst 0x04305830 // addsvl x16, x16, #1
976 add x15, x15, #1
977 cmp x0, x15
978 b.ne 0b
9791:
980 // * Set TPIDR2_EL0 to null.
981 .inst 0xd51bd0bf // msr TPIDR2_EL0, xzr
982 // * Set PSTATE.ZA to 0.
983 .inst 0xd503447f // smstop za
984 // * Return zero (success)
985 mov x0, xzr
9862:
987#if __has_feature(ptrauth_calls)
988 retab
989#else
990 ret
991#endif
992#endif
993
994#elif defined(__arm__) && !defined(__APPLE__)
995
996#if !defined(__ARM_ARCH_ISA_ARM)
997#if (__ARM_ARCH_ISA_THUMB == 2)
998 .syntax unified
999#endif
1000 .thumb
1001#endif
1002
1003@
1004@ extern int __unw_getcontext(unw_context_t* thread_state)
1005@
1006@ On entry:
1007@ thread_state pointer is in r0
1008@
1009@ Per EHABI #4.7 this only saves the core integer registers.
1010@ EHABI #7.4.5 notes that in general all VRS registers should be restored
1011@ however this is very hard to do for VFP registers because it is unknown
1012@ to the library how many registers are implemented by the architecture.
1013@ Instead, VFP registers are demand saved by logic external to __unw_getcontext.
1014@
1015 .p2align 2
1016DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
1017#if !defined(__ARM_ARCH_ISA_ARM) && __ARM_ARCH_ISA_THUMB == 1
1018 stm r0!, {r0-r7}
1019 mov r1, r8
1020 mov r2, r9
1021 mov r3, r10
1022 stm r0!, {r1-r3}
1023 mov r1, r11
1024 mov r2, sp
1025 mov r3, lr
1026 str r1, [r0, #0] @ r11
1027 @ r12 does not need storing, it it the intra-procedure-call scratch register
1028 str r2, [r0, #8] @ sp
1029 str r3, [r0, #12] @ lr
1030 str r3, [r0, #16] @ store return address as pc
1031 @ T1 does not have a non-cpsr-clobbering register-zeroing instruction.
1032 @ It is safe to use here though because we are about to return, and cpsr is
1033 @ not expected to be preserved.
1034 movs r0, #0 @ return UNW_ESUCCESS
1035#else
1036 @ 32bit thumb-2 restrictions for stm:
1037 @ . the sp (r13) cannot be in the list
1038 @ . the pc (r15) cannot be in the list in an STM instruction
1039 stm r0, {r0-r12}
1040 str sp, [r0, #52]
1041 str lr, [r0, #56]
1042 str lr, [r0, #60] @ store return address as pc
1043 mov r0, #0 @ return UNW_ESUCCESS
1044#endif
1045 JMP(lr)
1046
1047@
1048@ static void libunwind::Registers_arm::saveVFPWithFSTMD(unw_fpreg_t* values)
1049@
1050@ On entry:
1051@ values pointer is in r0
1052@
1053 .p2align 2
1054#if defined(__ELF__)
1055 .fpu vfpv3-d16
1056#endif
1057DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind13Registers_arm16saveVFPWithFSTMDEPv)
1058 vstmia r0, {d0-d15}
1059 JMP(lr)
1060
1061@
1062@ static void libunwind::Registers_arm::saveVFPWithFSTMX(unw_fpreg_t* values)
1063@
1064@ On entry:
1065@ values pointer is in r0
1066@
1067 .p2align 2
1068#if defined(__ELF__)
1069 .fpu vfpv3-d16
1070#endif
1071DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind13Registers_arm16saveVFPWithFSTMXEPv)
1072 vstmia r0, {d0-d15} @ fstmiax is deprecated in ARMv7+ and now behaves like vstmia
1073 JMP(lr)
1074
1075@
1076@ static void libunwind::Registers_arm::saveVFPv3(unw_fpreg_t* values)
1077@
1078@ On entry:
1079@ values pointer is in r0
1080@
1081 .p2align 2
1082#if defined(__ELF__)
1083 .fpu vfpv3
1084#endif
1085DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind13Registers_arm9saveVFPv3EPv)
1086 @ VFP and iwMMX instructions are only available when compiling with the flags
1087 @ that enable them. We do not want to do that in the library (because we do not
1088 @ want the compiler to generate instructions that access those) but this is
1089 @ only accessed if the personality routine needs these registers. Use of
1090 @ these registers implies they are, actually, available on the target, so
1091 @ it's ok to execute.
1092 @ So, generate the instructions using the corresponding coprocessor mnemonic.
1093 vstmia r0, {d16-d31}
1094 JMP(lr)
1095
1096#if defined(_LIBUNWIND_ARM_WMMX)
1097
1098@
1099@ static void libunwind::Registers_arm::saveiWMMX(unw_fpreg_t* values)
1100@
1101@ On entry:
1102@ values pointer is in r0
1103@
1104 .p2align 2
1105#if defined(__ELF__)
1106 .arch armv5te
1107#endif
1108DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind13Registers_arm9saveiWMMXEPv)
1109 stcl p1, cr0, [r0], #8 @ wstrd wR0, [r0], #8
1110 stcl p1, cr1, [r0], #8 @ wstrd wR1, [r0], #8
1111 stcl p1, cr2, [r0], #8 @ wstrd wR2, [r0], #8
1112 stcl p1, cr3, [r0], #8 @ wstrd wR3, [r0], #8
1113 stcl p1, cr4, [r0], #8 @ wstrd wR4, [r0], #8
1114 stcl p1, cr5, [r0], #8 @ wstrd wR5, [r0], #8
1115 stcl p1, cr6, [r0], #8 @ wstrd wR6, [r0], #8
1116 stcl p1, cr7, [r0], #8 @ wstrd wR7, [r0], #8
1117 stcl p1, cr8, [r0], #8 @ wstrd wR8, [r0], #8
1118 stcl p1, cr9, [r0], #8 @ wstrd wR9, [r0], #8
1119 stcl p1, cr10, [r0], #8 @ wstrd wR10, [r0], #8
1120 stcl p1, cr11, [r0], #8 @ wstrd wR11, [r0], #8
1121 stcl p1, cr12, [r0], #8 @ wstrd wR12, [r0], #8
1122 stcl p1, cr13, [r0], #8 @ wstrd wR13, [r0], #8
1123 stcl p1, cr14, [r0], #8 @ wstrd wR14, [r0], #8
1124 stcl p1, cr15, [r0], #8 @ wstrd wR15, [r0], #8
1125 JMP(lr)
1126
1127@
1128@ static void libunwind::Registers_arm::saveiWMMXControl(unw_uint32_t* values)
1129@
1130@ On entry:
1131@ values pointer is in r0
1132@
1133 .p2align 2
1134#if defined(__ELF__)
1135 .arch armv5te
1136#endif
1137DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind13Registers_arm16saveiWMMXControlEPj)
1138 stc2 p1, cr8, [r0], #4 @ wstrw wCGR0, [r0], #4
1139 stc2 p1, cr9, [r0], #4 @ wstrw wCGR1, [r0], #4
1140 stc2 p1, cr10, [r0], #4 @ wstrw wCGR2, [r0], #4
1141 stc2 p1, cr11, [r0], #4 @ wstrw wCGR3, [r0], #4
1142 JMP(lr)
1143
1144#endif
1145
1146#elif defined(__or1k__)
1147
1148#
1149# extern int __unw_getcontext(unw_context_t* thread_state)
1150#
1151# On entry:
1152# thread_state pointer is in r3
1153#
1154DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
1155 l.sw 0(r3), r0
1156 l.sw 4(r3), r1
1157 l.sw 8(r3), r2
1158 l.sw 12(r3), r3
1159 l.sw 16(r3), r4
1160 l.sw 20(r3), r5
1161 l.sw 24(r3), r6
1162 l.sw 28(r3), r7
1163 l.sw 32(r3), r8
1164 l.sw 36(r3), r9
1165 l.sw 40(r3), r10
1166 l.sw 44(r3), r11
1167 l.sw 48(r3), r12
1168 l.sw 52(r3), r13
1169 l.sw 56(r3), r14
1170 l.sw 60(r3), r15
1171 l.sw 64(r3), r16
1172 l.sw 68(r3), r17
1173 l.sw 72(r3), r18
1174 l.sw 76(r3), r19
1175 l.sw 80(r3), r20
1176 l.sw 84(r3), r21
1177 l.sw 88(r3), r22
1178 l.sw 92(r3), r23
1179 l.sw 96(r3), r24
1180 l.sw 100(r3), r25
1181 l.sw 104(r3), r26
1182 l.sw 108(r3), r27
1183 l.sw 112(r3), r28
1184 l.sw 116(r3), r29
1185 l.sw 120(r3), r30
1186 l.sw 124(r3), r31
1187 # store ra to pc
1188 l.sw 128(r3), r9
1189 # zero epcr
1190 l.sw 132(r3), r0
1191
1192#elif defined(__hexagon__)
1193#
1194# extern int unw_getcontext(unw_context_t* thread_state)
1195#
1196# On entry:
1197# thread_state pointer is in r0
1198#
1199# The context layout is hexagon_thread_state_t: unsigned int __r[35]
1200# __r[0..31] = r0..r31, __r[32] = predicates, __r[33] = PC, __r[34] = unused
1201# Offsets: __r[n] is at byte offset n*4.
1202#
1203#define OFFSET(offset) (offset/4)
1204DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
1205 // Save r1-r7 first (r0 is the context pointer, saved last)
1206 memw(r0+#4) = r1
1207 memw(r0+#8) = r2
1208 memw(r0+#12) = r3
1209 memw(r0+#16) = r4
1210 memw(r0+#20) = r5
1211 memw(r0+#24) = r6
1212 memw(r0+#28) = r7
1213
1214 memw(r0+#32) = r8
1215 memw(r0+#36) = r9
1216 memw(r0+#40) = r10
1217 memw(r0+#44) = r11
1218
1219 memw(r0+#48) = r12
1220 memw(r0+#52) = r13
1221 memw(r0+#56) = r14
1222 memw(r0+#60) = r15
1223
1224 memw(r0+#64) = r16
1225 memw(r0+#68) = r17
1226 memw(r0+#72) = r18
1227 memw(r0+#76) = r19
1228
1229 memw(r0+#80) = r20
1230 memw(r0+#84) = r21
1231 memw(r0+#88) = r22
1232 memw(r0+#92) = r23
1233
1234 memw(r0+#96) = r24
1235 memw(r0+#100) = r25
1236 memw(r0+#104) = r26
1237 memw(r0+#108) = r27
1238
1239 memw(r0+#112) = r28
1240 memw(r0+#116) = r29
1241 memw(r0+#120) = r30
1242 memw(r0+#124) = r31
1243
1244 r1 = c4 // Predicate register
1245 memw(r0+#128) = r1
1246
1247 r1 = r31 // Store return address as PC
1248 memw(r0+#132) = r1
1249
1250 // Save r0 (the context pointer itself) last
1251 memw(r0+#0) = r0
1252
1253 r0 = #0 // return UNW_ESUCCESS
1254 jumpr r31
1255
1256#elif defined(__sparc__) && defined(__arch64__)
1257
1258#
1259# extern int __unw_getcontext(unw_context_t* thread_state)
1260#
1261# On entry:
1262# thread_state pointer is in %o0
1263#
1264DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
1265 .register %g2, #scratch
1266 .register %g3, #scratch
1267 .register %g6, #scratch
1268 .register %g7, #scratch
1269 stx %g1, [%o0 + 0x08]
1270 stx %g2, [%o0 + 0x10]
1271 stx %g3, [%o0 + 0x18]
1272 stx %g4, [%o0 + 0x20]
1273 stx %g5, [%o0 + 0x28]
1274 stx %g6, [%o0 + 0x30]
1275 stx %g7, [%o0 + 0x38]
1276 stx %o0, [%o0 + 0x40]
1277 stx %o1, [%o0 + 0x48]
1278 stx %o2, [%o0 + 0x50]
1279 stx %o3, [%o0 + 0x58]
1280 stx %o4, [%o0 + 0x60]
1281 stx %o5, [%o0 + 0x68]
1282 stx %o6, [%o0 + 0x70]
1283 stx %o7, [%o0 + 0x78]
1284 stx %l0, [%o0 + 0x80]
1285 stx %l1, [%o0 + 0x88]
1286 stx %l2, [%o0 + 0x90]
1287 stx %l3, [%o0 + 0x98]
1288 stx %l4, [%o0 + 0xa0]
1289 stx %l5, [%o0 + 0xa8]
1290 stx %l6, [%o0 + 0xb0]
1291 stx %l7, [%o0 + 0xb8]
1292 stx %i0, [%o0 + 0xc0]
1293 stx %i1, [%o0 + 0xc8]
1294 stx %i2, [%o0 + 0xd0]
1295 stx %i3, [%o0 + 0xd8]
1296 stx %i4, [%o0 + 0xe0]
1297 stx %i5, [%o0 + 0xe8]
1298 stx %i6, [%o0 + 0xf0]
1299 stx %i7, [%o0 + 0xf8]
1300
1301 # save StackGhost cookie
1302 mov %i7, %g4
1303 save %sp, -176, %sp
1304 # register window flush necessary even without StackGhost
1305 flushw
1306 restore
1307 ldx [%sp + 2047 + 0x78], %g5
1308 xor %g4, %g5, %g4
1309 stx %g4, [%o0 + 0x100]
1310 retl
1311 # return UNW_ESUCCESS
1312 clr %o0
1313
1314#elif defined(__sparc__)
1315
1316#
1317# extern int __unw_getcontext(unw_context_t* thread_state)
1318#
1319# On entry:
1320# thread_state pointer is in o0
1321#
1322DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
1323 ta 3
1324 add %o7, 8, %o7
1325 std %g0, [%o0 + 0]
1326 std %g2, [%o0 + 8]
1327 std %g4, [%o0 + 16]
1328 std %g6, [%o0 + 24]
1329 std %o0, [%o0 + 32]
1330 std %o2, [%o0 + 40]
1331 std %o4, [%o0 + 48]
1332 std %o6, [%o0 + 56]
1333 std %l0, [%o0 + 64]
1334 std %l2, [%o0 + 72]
1335 std %l4, [%o0 + 80]
1336 std %l6, [%o0 + 88]
1337 std %i0, [%o0 + 96]
1338 std %i2, [%o0 + 104]
1339 std %i4, [%o0 + 112]
1340 std %i6, [%o0 + 120]
1341 jmp %o7
1342 clr %o0 // return UNW_ESUCCESS
1343
1344#elif defined(__riscv)
1345
1346#
1347# extern int __unw_getcontext(unw_context_t* thread_state)
1348#
1349# On entry:
1350# thread_state pointer is in a0
1351#
1352DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
1353 ISTORE x1, (RISCV_ISIZE * 0)(a0) // store ra as pc
1354#if defined(__riscv_32e)
1355 .irp i,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
1356#else
1357 .irp i,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
1358#endif
1359 ISTORE x\i, (RISCV_ISIZE * \i)(a0)
1360 .endr
1361
1362# if defined(__riscv_flen)
1363 .irp i,FROM_0_TO_31
1364 FSTORE f\i, (RISCV_FOFFSET + RISCV_FSIZE * \i)(a0)
1365 .endr
1366# endif
1367
1368 li a0, 0 // return UNW_ESUCCESS
1369 ret // jump to ra
1370
1371#elif defined(__s390x__)
1372
1373//
1374// extern int __unw_getcontext(unw_context_t* thread_state)
1375//
1376// On entry:
1377// thread_state pointer is in r2
1378//
1379DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
1380
1381 // Save GPRs
1382 stmg %r0, %r15, 16(%r2)
1383
1384 // Save PSWM
1385 epsw %r0, %r1
1386 stm %r0, %r1, 0(%r2)
1387
1388 // Store return address as PSWA
1389 stg %r14, 8(%r2)
1390
1391 // Save FPRs
1392 .irp i,FROM_0_TO_15
1393 std %f\i, (144+8*\i)(%r2)
1394 .endr
1395
1396 // Return UNW_ESUCCESS
1397 lghi %r2, 0
1398 br %r14
1399
1400#elif defined(__loongarch__) && __loongarch_grlen == 64
1401
1402#
1403# extern int __unw_getcontext(unw_context_t* thread_state)
1404#
1405# On entry:
1406# thread_state pointer is in $a0($r4)
1407#
1408DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
1409 .irp i,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
1410 st.d $r\i, $a0, (8*\i)
1411 .endr
1412 st.d $r1, $a0, (8 * 32) // store $ra to pc
1413
1414# if __loongarch_frlen == 64
1415 .irp i,FROM_0_TO_31
1416 fst.d $f\i, $a0, (8 * 33 + 8 * \i)
1417 .endr
1418# endif
1419
1420 move $a0, $zero // UNW_ESUCCESS
1421 jr $ra
1422
1423#endif
1424
1425#ifdef __arm64ec__
1426 .globl "#unw_getcontext"
1427 .set "#unw_getcontext", "#__unw_getcontext"
1428 .weak_anti_dep unw_getcontext
1429 .set unw_getcontext, "#unw_getcontext"
1430 EXPORT_SYMBOL(unw_getcontext)
1431#else
1432 WEAK_ALIAS(__unw_getcontext, unw_getcontext)
1433#endif
1434
1435#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) */
1436
1437NO_EXEC_STACK_DIRECTIVE
1438
1439#endif /* !defined(__wasm__) */
1440