1 | /* |
2 | * Copyright (C) 2017 Yusuke Suzuki <[email protected]> |
3 | * Copyright (C) 2019 Apple Inc. 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 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY GOOGLE, INC. ``AS IS'' AND ANY |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | |
27 | #pragma once |
28 | |
29 | #include <wtf/Platform.h> |
30 | #include <wtf/StdLibExtras.h> |
31 | |
32 | #if OS(DARWIN) |
33 | #include <mach/thread_act.h> |
34 | #include <signal.h> |
35 | #elif OS(WINDOWS) |
36 | #include <windows.h> |
37 | #else |
38 | #include <ucontext.h> |
39 | #endif |
40 | |
41 | namespace WTF { |
42 | |
43 | #if OS(DARWIN) |
44 | |
45 | #if CPU(X86) |
46 | typedef i386_thread_state_t PlatformRegisters; |
47 | #elif CPU(X86_64) |
48 | typedef x86_thread_state64_t PlatformRegisters; |
49 | #elif CPU(PPC) |
50 | typedef ppc_thread_state_t PlatformRegisters; |
51 | #elif CPU(PPC64) |
52 | typedef ppc_thread_state64_t PlatformRegisters; |
53 | #elif CPU(ARM) |
54 | typedef arm_thread_state_t PlatformRegisters; |
55 | #elif CPU(ARM64) |
56 | typedef arm_thread_state64_t PlatformRegisters; |
57 | #else |
58 | #error Unknown Architecture |
59 | #endif |
60 | |
61 | inline PlatformRegisters& registersFromUContext(ucontext_t* ucontext) |
62 | { |
63 | return ucontext->uc_mcontext->__ss; |
64 | } |
65 | |
66 | #elif OS(WINDOWS) |
67 | |
68 | using PlatformRegisters = CONTEXT; |
69 | |
70 | #elif HAVE(MACHINE_CONTEXT) |
71 | |
72 | struct PlatformRegisters { |
73 | mcontext_t machineContext; |
74 | }; |
75 | |
76 | inline PlatformRegisters& registersFromUContext(ucontext_t* ucontext) |
77 | { |
78 | #if CPU(PPC) |
79 | return *bitwise_cast<PlatformRegisters*>(ucontext->uc_mcontext.uc_regs); |
80 | #else |
81 | return *bitwise_cast<PlatformRegisters*>(&ucontext->uc_mcontext); |
82 | #endif |
83 | } |
84 | |
85 | #else |
86 | |
87 | struct PlatformRegisters { |
88 | void* stackPointer; |
89 | }; |
90 | |
91 | #endif |
92 | |
93 | } // namespace WTF |
94 | |
95 | #if USE(PLATFORM_REGISTERS_WITH_PROFILE) |
96 | #if CPU(ARM64E) |
97 | |
98 | namespace WTF { |
99 | |
100 | extern void* threadStateLRInternal(PlatformRegisters&); |
101 | extern void* threadStatePCInternal(PlatformRegisters&); |
102 | |
103 | } // namespace WTF |
104 | |
105 | using WTF::threadStateLRInternal; |
106 | using WTF::threadStatePCInternal; |
107 | |
108 | #else // not CPU(ARM64E) |
109 | |
110 | #define threadStateLRInternal(regs) bitwise_cast<void*>(arm_thread_state64_get_lr(regs)) |
111 | #define threadStatePCInternal(regs) bitwise_cast<void*>(arm_thread_state64_get_pc(regs)) |
112 | |
113 | #endif // CPU(ARM64E) |
114 | |
115 | #define WTF_READ_PLATFORM_REGISTERS_SP_WITH_PROFILE(regs) \ |
116 | reinterpret_cast<void*>(arm_thread_state64_get_sp(const_cast<PlatformRegisters&>(regs))) |
117 | |
118 | #define WTF_WRITE_PLATFORM_REGISTERS_SP_WITH_PROFILE(regs, newPointer) \ |
119 | arm_thread_state64_set_sp(regs, reinterpret_cast<uintptr_t>(newPointer)) |
120 | |
121 | #define WTF_READ_PLATFORM_REGISTERS_FP_WITH_PROFILE(regs) \ |
122 | reinterpret_cast<void*>(arm_thread_state64_get_fp(const_cast<PlatformRegisters&>(regs))) |
123 | |
124 | #define WTF_WRITE_PLATFORM_REGISTERS_FP_WITH_PROFILE(regs, newPointer) \ |
125 | arm_thread_state64_set_fp(regs, reinterpret_cast<uintptr_t>(newPointer)) |
126 | |
127 | #define WTF_READ_PLATFORM_REGISTERS_LR_WITH_PROFILE(regs) \ |
128 | threadStateLRInternal(const_cast<PlatformRegisters&>(regs)) |
129 | |
130 | #define WTF_WRITE_PLATFORM_REGISTERS_LR_WITH_PROFILE(regs, newPointer) \ |
131 | arm_thread_state64_set_lr_fptr(regs, newPointer) |
132 | |
133 | #define WTF_READ_PLATFORM_REGISTERS_PC_WITH_PROFILE(regs) \ |
134 | threadStatePCInternal(const_cast<PlatformRegisters&>(regs)) |
135 | |
136 | #define WTF_WRITE_PLATFORM_REGISTERS_PC_WITH_PROFILE(regs, newPointer) \ |
137 | arm_thread_state64_set_pc_fptr(regs, newPointer) |
138 | |
139 | #define WTF_READ_MACHINE_CONTEXT_SP_WITH_PROFILE(machineContext) \ |
140 | WTF_READ_PLATFORM_REGISTERS_SP_WITH_PROFILE(machineContext->__ss) |
141 | |
142 | #define WTF_WRITE_MACHINE_CONTEXT_SP_WITH_PROFILE(machineContext, newPointer) \ |
143 | WTF_WRITE_PLATFORM_REGISTERS_SP_WITH_PROFILE(machineContext->__ss, newPointer) |
144 | |
145 | #define WTF_READ_MACHINE_CONTEXT_FP_WITH_PROFILE(machineContext) \ |
146 | WTF_READ_PLATFORM_REGISTERS_FP_WITH_PROFILE(machineContext->__ss) |
147 | |
148 | #define WTF_WRITE_MACHINE_CONTEXT_FP_WITH_PROFILE(machineContext, newPointer) \ |
149 | WTF_WRITE_PLATFORM_REGISTERS_FP_WITH_PROFILE(machineContext->__ss, newPointer) |
150 | |
151 | #define WTF_WRITE_MACHINE_CONTEXT_LR_WITH_PROFILE(machineContext, newPointer) \ |
152 | WTF_WRITE_PLATFORM_REGISTERS_LR_WITH_PROFILE(machineContext->__ss, newPointer) |
153 | |
154 | #define WTF_READ_MACHINE_CONTEXT_PC_WITH_PROFILE(machineContext) \ |
155 | WTF_READ_PLATFORM_REGISTERS_PC_WITH_PROFILE(machineContext->__ss) |
156 | |
157 | #define WTF_WRITE_MACHINE_CONTEXT_PC_WITH_PROFILE(machineContext, newPointer) \ |
158 | WTF_WRITE_PLATFORM_REGISTERS_PC_WITH_PROFILE(machineContext->__ss, newPointer) |
159 | |
160 | #endif // USE(PLATFORM_REGISTERS_WITH_PROFILE) |
161 | |
162 | using WTF::PlatformRegisters; |
163 | |