Debian Codegraph @

third_party/rust/crash-context/src/linux/getcontext/x86_64.rs

raw
1 /* The x64 implementation of breakpad_getcontext was derived in part
2 from the implementation of libunwind which requires the following
3 notice. */
4 /* libunwind - a platform-independent unwind library
5 Copyright (C) 2008 Google, Inc
6 Contributed by Paul Pluzhnikov <[email protected]>
7 Copyright (C) 2010 Konstantin Belousov <[email protected]>
8
9 This file is part of libunwind.
10
11 Permission is hereby granted, free of charge, to any person obtaining
12 a copy of this software and associated documentation files (the
13 "Software"), to deal in the Software without restriction, including
14 without limitation the rights to use, copy, modify, merge, publish,
15 distribute, sublicense, and/or sell copies of the Software, and to
16 permit persons to whom the Software is furnished to do so, subject to
17 the following conditions:
18
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
30 // Oof, unfortunately android libc and normal linux libc don't have the same
31 // fpregs offset in ucontext_t, but asm! in Rust is...not convenient for only
32 // adding certain lines/blocks of asm based using cfg https://github.com/rust-lang/rust/issues/15701
33 // and they're not really inputs, just literals, so...yah
34
35 #[cfg(any(target_os = "linux", target_os = "android"))]
36
37 // Unfortunately, the asm! macro has a few really annoying limitations at the
38 // moment
39 //
40 // 1. const operands are unstable
41 // 2. cfg attributes can't be used inside the asm macro at all
42 //
43 // and the worst part is we need it for literally only 1 thing, using a different
44 // offset to the fpstate in ucontext depending on whether we are targeting android
45 // or not :(
46 macro_rules! asm_func {
47 ($offset:expr) => {
48 std::arch::global_asm! {
49 ".text",
50 ".global crash_context_getcontext",
51 ".hidden crash_context_getcontext",
52 ".align 4",
53 ".type crash_context_getcontext, @function",
54 "crash_context_getcontext:",
55 ".cfi_startproc",
56 // Callee saved: RBX, RBP, R12-R15
57 "movq %r12, 0x48(%rdi)",
58 "movq %r13, 0x50(%rdi)",
59 "movq %r14, 0x58(%rdi)",
60 "movq %r15, 0x60(%rdi)",
61 "movq %rbp, 0x78(%rdi)",
62 "movq %rbx, 0x80(%rdi)",
63
64 // Save argument registers
65 "movq %r8, 0x28(%rdi)",
66 "movq %r9, 0x30(%rdi)",
67 "movq %rdi, 0x68(%rdi)",
68 "movq %rsi, 0x70(%rdi)",
69 "movq %rdx, 0x88(%rdi)",
70 "movq %rax, 0x90(%rdi)",
71 "movq %rcx, 0x98(%rdi)",
72
73 // Save fp state
74 stringify!(leaq $offset(%rdi),%r8),
75 "movq %r8, 0xe0(%rdi)",
76 "fnstenv (%r8)",
77 "stmxcsr 0x18(%r8)",
78
79 // Exclude this call
80 "leaq 8(%rsp), %rax",
81 "movq %rax, 0xa0(%rdi)",
82
83 "movq 0(%rsp), %rax",
84 "movq %rax, 0xa8(%rdi)",
85
86 // Save signal mask: sigprocmask(SIGBLOCK, NULL, &uc->uc_sigmask)
87 "leaq 0x128(%rdi), %rdx", // arg3
88 "xorq %rsi, %rsi", // arg2 NULL
89 "xorq %rdi, %rdi", // arg1 SIGBLOCK == 0
90 "call sigprocmask@PLT",
91
92 // Always return 0 for success, even if sigprocmask failed.
93 "xorl %eax, %eax",
94 "ret",
95 ".cfi_endproc",
96 ".size crash_context_getcontext, . - crash_context_getcontext",
97 options(att_syntax)
98 }
99 };
100 }
101
102 #[cfg(target_os = "linux")]
103 asm_func!(0x1a8);
104 #[cfg(target_os = "android")]
105 asm_func!(0x130);