OLD | NEW |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "snapshot/win/cpu_context_win.h" | 15 #include "snapshot/win/cpu_context_win.h" |
16 | 16 |
17 #include <stdint.h> | 17 #include <stdint.h> |
18 #include <string.h> | 18 #include <string.h> |
19 | 19 |
20 #include "base/logging.h" | 20 #include "base/logging.h" |
21 #include "snapshot/cpu_context.h" | 21 #include "snapshot/cpu_context.h" |
22 | 22 |
23 namespace crashpad { | 23 namespace crashpad { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 // Validation for casts used with CPUContextX86::FsaveToFxsave(). | |
28 static_assert(sizeof(CPUContextX86::Fsave) == | |
29 offsetof(WOW64_FLOATING_SAVE_AREA, Cr0NpxState), | |
30 "WoW64 fsave types must be equivalent"); | |
31 #if defined(ARCH_CPU_X86) | |
32 static_assert(sizeof(CPUContextX86::Fsave) == | |
33 offsetof(FLOATING_SAVE_AREA, Spare0), | |
34 "fsave types must be equivalent"); | |
35 #endif // ARCH_CPU_X86 | |
36 | |
37 template <typename T> | |
38 bool HasContextPart(const T& context, uint32_t bits) { | |
39 return (context.ContextFlags & bits) == bits; | |
40 } | |
41 | |
42 template <class T> | 27 template <class T> |
43 void CommonInitializeX86Context(const T& context, CPUContextX86* out) { | 28 void CommonInitializeX86Context(const T& context, CPUContextX86* out) { |
44 // This function assumes that the WOW64_CONTEXT_* and x86 CONTEXT_* values | 29 LOG_IF(ERROR, !(context.ContextFlags & WOW64_CONTEXT_i386)) |
45 // for ContextFlags are identical. This can be tested when targeting 32-bit | 30 << "non-x86 context"; |
46 // x86. | |
47 #if defined(ARCH_CPU_X86) | |
48 static_assert(sizeof(CONTEXT) == sizeof(WOW64_CONTEXT), | |
49 "type mismatch: CONTEXT"); | |
50 #define ASSERT_WOW64_EQUIVALENT(x) \ | |
51 do { \ | |
52 static_assert(x == WOW64_##x, "value mismatch: " #x); \ | |
53 } while (false) | |
54 ASSERT_WOW64_EQUIVALENT(CONTEXT_i386); | |
55 ASSERT_WOW64_EQUIVALENT(CONTEXT_i486); | |
56 ASSERT_WOW64_EQUIVALENT(CONTEXT_CONTROL); | |
57 ASSERT_WOW64_EQUIVALENT(CONTEXT_INTEGER); | |
58 ASSERT_WOW64_EQUIVALENT(CONTEXT_SEGMENTS); | |
59 ASSERT_WOW64_EQUIVALENT(CONTEXT_FLOATING_POINT); | |
60 ASSERT_WOW64_EQUIVALENT(CONTEXT_DEBUG_REGISTERS); | |
61 ASSERT_WOW64_EQUIVALENT(CONTEXT_EXTENDED_REGISTERS); | |
62 ASSERT_WOW64_EQUIVALENT(CONTEXT_FULL); | |
63 ASSERT_WOW64_EQUIVALENT(CONTEXT_ALL); | |
64 ASSERT_WOW64_EQUIVALENT(CONTEXT_XSTATE); | |
65 #undef ASSERT_WOW64_EQUIVALENT | |
66 #endif // ARCH_CPU_X86 | |
67 | |
68 memset(out, 0, sizeof(*out)); | 31 memset(out, 0, sizeof(*out)); |
69 | 32 |
70 LOG_IF(ERROR, !HasContextPart(context, WOW64_CONTEXT_i386)) | 33 // We assume in this function that the WOW64_CONTEXT_* and x86 CONTEXT_* |
71 << "non-x86 context"; | 34 // values for ContextFlags are identical. |
72 | 35 |
73 if (HasContextPart(context, WOW64_CONTEXT_CONTROL)) { | 36 if (context.ContextFlags & WOW64_CONTEXT_CONTROL) { |
74 out->ebp = context.Ebp; | 37 out->ebp = context.Ebp; |
75 out->eip = context.Eip; | 38 out->eip = context.Eip; |
76 out->cs = static_cast<uint16_t>(context.SegCs); | 39 out->cs = static_cast<uint16_t>(context.SegCs); |
77 out->eflags = context.EFlags; | 40 out->eflags = context.EFlags; |
78 out->esp = context.Esp; | 41 out->esp = context.Esp; |
79 out->ss = static_cast<uint16_t>(context.SegSs); | 42 out->ss = static_cast<uint16_t>(context.SegSs); |
80 } | 43 } |
81 | 44 |
82 if (HasContextPart(context, WOW64_CONTEXT_INTEGER)) { | 45 if (context.ContextFlags & WOW64_CONTEXT_INTEGER) { |
83 out->eax = context.Eax; | 46 out->eax = context.Eax; |
84 out->ebx = context.Ebx; | 47 out->ebx = context.Ebx; |
85 out->ecx = context.Ecx; | 48 out->ecx = context.Ecx; |
86 out->edx = context.Edx; | 49 out->edx = context.Edx; |
87 out->edi = context.Edi; | 50 out->edi = context.Edi; |
88 out->esi = context.Esi; | 51 out->esi = context.Esi; |
89 } | 52 } |
90 | 53 |
91 if (HasContextPart(context, WOW64_CONTEXT_SEGMENTS)) { | 54 if (context.ContextFlags & WOW64_CONTEXT_SEGMENTS) { |
92 out->ds = static_cast<uint16_t>(context.SegDs); | 55 out->ds = static_cast<uint16_t>(context.SegDs); |
93 out->es = static_cast<uint16_t>(context.SegEs); | 56 out->es = static_cast<uint16_t>(context.SegEs); |
94 out->fs = static_cast<uint16_t>(context.SegFs); | 57 out->fs = static_cast<uint16_t>(context.SegFs); |
95 out->gs = static_cast<uint16_t>(context.SegGs); | 58 out->gs = static_cast<uint16_t>(context.SegGs); |
96 } | 59 } |
97 | 60 |
98 if (HasContextPart(context, WOW64_CONTEXT_DEBUG_REGISTERS)) { | 61 if (context.ContextFlags & WOW64_CONTEXT_DEBUG_REGISTERS) { |
99 out->dr0 = context.Dr0; | 62 out->dr0 = context.Dr0; |
100 out->dr1 = context.Dr1; | 63 out->dr1 = context.Dr1; |
101 out->dr2 = context.Dr2; | 64 out->dr2 = context.Dr2; |
102 out->dr3 = context.Dr3; | 65 out->dr3 = context.Dr3; |
103 | |
104 // DR4 and DR5 are obsolete synonyms for DR6 and DR7, see | 66 // DR4 and DR5 are obsolete synonyms for DR6 and DR7, see |
105 // https://en.wikipedia.org/wiki/X86_debug_register. | 67 // https://en.wikipedia.org/wiki/X86_debug_register. |
106 out->dr4 = context.Dr6; | 68 out->dr4 = context.Dr6; |
107 out->dr5 = context.Dr7; | 69 out->dr5 = context.Dr7; |
108 | |
109 out->dr6 = context.Dr6; | 70 out->dr6 = context.Dr6; |
110 out->dr7 = context.Dr7; | 71 out->dr7 = context.Dr7; |
111 } | 72 } |
112 | 73 |
113 if (HasContextPart(context, WOW64_CONTEXT_EXTENDED_REGISTERS)) { | 74 if (context.ContextFlags & WOW64_CONTEXT_EXTENDED_REGISTERS) { |
114 static_assert(sizeof(out->fxsave) == sizeof(context.ExtendedRegisters), | 75 static_assert(sizeof(out->fxsave) == sizeof(context.ExtendedRegisters), |
115 "fxsave types must be equivalent"); | 76 "types must be equivalent"); |
116 memcpy(&out->fxsave, &context.ExtendedRegisters, sizeof(out->fxsave)); | 77 memcpy(&out->fxsave, &context.ExtendedRegisters, sizeof(out->fxsave)); |
117 } else if (HasContextPart(context, WOW64_CONTEXT_FLOATING_POINT)) { | 78 } else if (context.ContextFlags & WOW64_CONTEXT_FLOATING_POINT) { |
118 // The static_assert that validates this cast can’t be here because it | 79 CHECK(false) << "TODO(scottmg): extract x87 data"; |
119 // relies on field names that vary based on the template parameter. | |
120 CPUContextX86::FsaveToFxsave( | |
121 *reinterpret_cast<const CPUContextX86::Fsave*>(&context.FloatSave), | |
122 &out->fxsave); | |
123 } | 80 } |
124 } | 81 } |
125 | 82 |
126 } // namespace | 83 } // namespace |
127 | 84 |
128 #if defined(ARCH_CPU_64_BITS) | 85 #if defined(ARCH_CPU_64_BITS) |
129 | 86 |
130 void InitializeX86Context(const WOW64_CONTEXT& context, CPUContextX86* out) { | 87 void InitializeX86Context(const WOW64_CONTEXT& context, CPUContextX86* out) { |
131 CommonInitializeX86Context(context, out); | 88 CommonInitializeX86Context(context, out); |
132 } | 89 } |
133 | 90 |
134 void InitializeX64Context(const CONTEXT& context, CPUContextX86_64* out) { | 91 void InitializeX64Context(const CONTEXT& context, CPUContextX86_64* out) { |
135 memset(out, 0, sizeof(*out)); | 92 memset(out, 0, sizeof(*out)); |
136 | 93 |
137 LOG_IF(ERROR, !HasContextPart(context, CONTEXT_AMD64)) << "non-x64 context"; | 94 LOG_IF(ERROR, !(context.ContextFlags & CONTEXT_AMD64)) << "non-x64 context"; |
138 | 95 |
139 if (HasContextPart(context, CONTEXT_CONTROL)) { | 96 if (context.ContextFlags & CONTEXT_CONTROL) { |
140 out->cs = context.SegCs; | 97 out->cs = context.SegCs; |
141 out->rflags = context.EFlags; | 98 out->rflags = context.EFlags; |
142 out->rip = context.Rip; | 99 out->rip = context.Rip; |
143 out->rsp = context.Rsp; | 100 out->rsp = context.Rsp; |
144 // SegSs ignored. | 101 // SegSs ignored. |
145 } | 102 } |
146 | 103 |
147 if (HasContextPart(context, CONTEXT_INTEGER)) { | 104 if (context.ContextFlags & CONTEXT_INTEGER) { |
148 out->rax = context.Rax; | 105 out->rax = context.Rax; |
149 out->rbx = context.Rbx; | 106 out->rbx = context.Rbx; |
150 out->rcx = context.Rcx; | 107 out->rcx = context.Rcx; |
151 out->rdx = context.Rdx; | 108 out->rdx = context.Rdx; |
152 out->rdi = context.Rdi; | 109 out->rdi = context.Rdi; |
153 out->rsi = context.Rsi; | 110 out->rsi = context.Rsi; |
154 out->rbp = context.Rbp; | 111 out->rbp = context.Rbp; |
155 out->r8 = context.R8; | 112 out->r8 = context.R8; |
156 out->r9 = context.R9; | 113 out->r9 = context.R9; |
157 out->r10 = context.R10; | 114 out->r10 = context.R10; |
158 out->r11 = context.R11; | 115 out->r11 = context.R11; |
159 out->r12 = context.R12; | 116 out->r12 = context.R12; |
160 out->r13 = context.R13; | 117 out->r13 = context.R13; |
161 out->r14 = context.R14; | 118 out->r14 = context.R14; |
162 out->r15 = context.R15; | 119 out->r15 = context.R15; |
163 } | 120 } |
164 | 121 |
165 if (HasContextPart(context, CONTEXT_SEGMENTS)) { | 122 if (context.ContextFlags & CONTEXT_SEGMENTS) { |
166 out->fs = context.SegFs; | 123 out->fs = context.SegFs; |
167 out->gs = context.SegGs; | 124 out->gs = context.SegGs; |
168 // SegDs ignored. | 125 // SegDs ignored. |
169 // SegEs ignored. | 126 // SegEs ignored. |
170 } | 127 } |
171 | 128 |
172 if (HasContextPart(context, CONTEXT_DEBUG_REGISTERS)) { | 129 if (context.ContextFlags & CONTEXT_DEBUG_REGISTERS) { |
173 out->dr0 = context.Dr0; | 130 out->dr0 = context.Dr0; |
174 out->dr1 = context.Dr1; | 131 out->dr1 = context.Dr1; |
175 out->dr2 = context.Dr2; | 132 out->dr2 = context.Dr2; |
176 out->dr3 = context.Dr3; | 133 out->dr3 = context.Dr3; |
177 | |
178 // DR4 and DR5 are obsolete synonyms for DR6 and DR7, see | 134 // DR4 and DR5 are obsolete synonyms for DR6 and DR7, see |
179 // https://en.wikipedia.org/wiki/X86_debug_register. | 135 // https://en.wikipedia.org/wiki/X86_debug_register. |
180 out->dr4 = context.Dr6; | 136 out->dr4 = context.Dr6; |
181 out->dr5 = context.Dr7; | 137 out->dr5 = context.Dr7; |
182 | |
183 out->dr6 = context.Dr6; | 138 out->dr6 = context.Dr6; |
184 out->dr7 = context.Dr7; | 139 out->dr7 = context.Dr7; |
185 } | 140 } |
186 | 141 |
187 if (HasContextPart(context, CONTEXT_FLOATING_POINT)) { | 142 if (context.ContextFlags & CONTEXT_FLOATING_POINT) { |
188 static_assert(sizeof(out->fxsave) == sizeof(context.FltSave), | 143 static_assert(sizeof(out->fxsave) == sizeof(context.FltSave), |
189 "types must be equivalent"); | 144 "types must be equivalent"); |
190 memcpy(&out->fxsave, &context.FltSave, sizeof(out->fxsave)); | 145 memcpy(&out->fxsave, &context.FltSave.ControlWord, sizeof(out->fxsave)); |
191 } | 146 } |
192 } | 147 } |
193 | 148 |
194 #else // ARCH_CPU_64_BITS | 149 #else // ARCH_CPU_64_BITS |
195 | 150 |
196 void InitializeX86Context(const CONTEXT& context, CPUContextX86* out) { | 151 void InitializeX86Context(const CONTEXT& context, CPUContextX86* out) { |
197 CommonInitializeX86Context(context, out); | 152 CommonInitializeX86Context(context, out); |
198 } | 153 } |
199 | 154 |
200 #endif // ARCH_CPU_64_BITS | 155 #endif // ARCH_CPU_64_BITS |
201 | 156 |
202 } // namespace crashpad | 157 } // namespace crashpad |
OLD | NEW |