cpu_context.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright 2014 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (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
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef CRASHPAD_SNAPSHOT_SNAPSHOT_CPU_CONTEXT_H_
  15. #define CRASHPAD_SNAPSHOT_SNAPSHOT_CPU_CONTEXT_H_
  16. #include <stdint.h>
  17. #include "snapshot/cpu_architecture.h"
  18. namespace crashpad {
  19. //! \brief A context structure carrying 32-bit x86 CPU state.
  20. struct CPUContextX86 {
  21. using X87Register = uint8_t[10];
  22. struct Fsave {
  23. uint16_t fcw; // FPU control word
  24. uint16_t reserved_1;
  25. uint16_t fsw; // FPU status word
  26. uint16_t reserved_2;
  27. uint16_t ftw; // full FPU tag word
  28. uint16_t reserved_3;
  29. uint32_t fpu_ip; // FPU instruction pointer offset
  30. uint16_t fpu_cs; // FPU instruction pointer segment selector
  31. uint16_t fop; // FPU opcode
  32. uint32_t fpu_dp; // FPU data pointer offset
  33. uint16_t fpu_ds; // FPU data pointer segment selector
  34. uint16_t reserved_4;
  35. X87Register st[8];
  36. };
  37. union X87OrMMXRegister {
  38. struct {
  39. X87Register st;
  40. uint8_t st_reserved[6];
  41. };
  42. struct {
  43. uint8_t mm_value[8];
  44. uint8_t mm_reserved[8];
  45. };
  46. };
  47. using XMMRegister = uint8_t[16];
  48. struct Fxsave {
  49. uint16_t fcw; // FPU control word
  50. uint16_t fsw; // FPU status word
  51. uint8_t ftw; // abridged FPU tag word
  52. uint8_t reserved_1;
  53. uint16_t fop; // FPU opcode
  54. uint32_t fpu_ip; // FPU instruction pointer offset
  55. uint16_t fpu_cs; // FPU instruction pointer segment selector
  56. uint16_t reserved_2;
  57. uint32_t fpu_dp; // FPU data pointer offset
  58. uint16_t fpu_ds; // FPU data pointer segment selector
  59. uint16_t reserved_3;
  60. uint32_t mxcsr; // multimedia extensions status and control register
  61. uint32_t mxcsr_mask; // valid bits in mxcsr
  62. X87OrMMXRegister st_mm[8];
  63. XMMRegister xmm[8];
  64. uint8_t reserved_4[176];
  65. uint8_t available[48];
  66. };
  67. //! \brief Converts an `fxsave` area to an `fsave` area.
  68. //!
  69. //! `fsave` state is restricted to the x87 FPU, while `fxsave` state includes
  70. //! state related to the x87 FPU as well as state specific to SSE.
  71. //!
  72. //! As the `fxsave` format is a superset of the `fsave` format, this operation
  73. //! fully populates the `fsave` area. `fsave` uses the full 16-bit form for
  74. //! the x87 floating-point tag word, so FxsaveToFsaveTagWord() is used to
  75. //! derive Fsave::ftw from the abridged 8-bit form used by `fxsave`. Reserved
  76. //! fields in \a fsave are set to `0`.
  77. //!
  78. //! \param[in] fxsave The `fxsave` area to convert.
  79. //! \param[out] fsave The `fsave` area to populate.
  80. //!
  81. //! \sa FsaveToFxsave()
  82. static void FxsaveToFsave(const Fxsave& fxsave, Fsave* fsave);
  83. //! \brief Converts an `fsave` area to an `fxsave` area.
  84. //!
  85. //! `fsave` state is restricted to the x87 FPU, while `fxsave` state includes
  86. //! state related to the x87 FPU as well as state specific to SSE.
  87. //!
  88. //! As the `fsave` format is a subset of the `fxsave` format, this operation
  89. //! cannot fully populate the `fxsave` area. Fields in \a fxsave that have no
  90. //! equivalent in \a fsave are set to `0`, including Fxsave::mxcsr,
  91. //! Fxsave::mxcsr_mask, Fxsave::xmm, and Fxsave::available.
  92. //! FsaveToFxsaveTagWord() is used to derive Fxsave::ftw from the full 16-bit
  93. //! form used by `fsave`. Reserved fields in \a fxsave are set to `0`.
  94. //!
  95. //! \param[in] fsave The `fsave` area to convert.
  96. //! \param[out] fxsave The `fxsave` area to populate.
  97. //!
  98. //! \sa FxsaveToFsave()
  99. static void FsaveToFxsave(const Fsave& fsave, Fxsave* fxsave);
  100. //! \brief Converts x87 floating-point tag words from `fxsave` (abridged,
  101. //! 8-bit) to `fsave` (full, 16-bit) form.
  102. //!
  103. //! `fxsave` stores the x87 floating-point tag word in abridged 8-bit form,
  104. //! and `fsave` stores it in full 16-bit form. Some users, notably
  105. //! CPUContextX86::Fsave::ftw, require the full 16-bit form, where most other
  106. //! contemporary code uses `fxsave` and thus the abridged 8-bit form found in
  107. //! CPUContextX86::Fxsave::ftw.
  108. //!
  109. //! This function converts an abridged tag word to the full version by using
  110. //! the abridged tag word and the contents of the registers it describes. See
  111. //! Intel Software Developer’s Manual, Volume 2A: Instruction Set Reference
  112. //! A-M (253666-052), 3.2 “FXSAVE”, specifically, the notes on the abridged
  113. //! FTW and recreating the FSAVE format, and AMD Architecture Programmer’s
  114. //! Manual, Volume 2: System Programming (24593-3.24), “FXSAVE Format for x87
  115. //! Tag Word”.
  116. //!
  117. //! \sa FsaveToFxsaveTagWord()
  118. //!
  119. //! \param[in] fsw The FPU status word, used to map logical \a st_mm registers
  120. //! to their physical counterparts. This can be taken from
  121. //! CPUContextX86::Fxsave::fsw.
  122. //! \param[in] fxsave_tag The abridged FPU tag word. This can be taken from
  123. //! CPUContextX86::Fxsave::ftw.
  124. //! \param[in] st_mm The floating-point registers in logical order. This can
  125. //! be taken from CPUContextX86::Fxsave::st_mm.
  126. //!
  127. //! \return The full FPU tag word.
  128. static uint16_t FxsaveToFsaveTagWord(
  129. uint16_t fsw, uint8_t fxsave_tag, const X87OrMMXRegister st_mm[8]);
  130. //! \brief Converts x87 floating-point tag words from `fsave` (full, 16-bit)
  131. //! to `fxsave` (abridged, 8-bit) form.
  132. //!
  133. //! This function performs the inverse operation of FxsaveToFsaveTagWord().
  134. //!
  135. //! \param[in] fsave_tag The full FPU tag word.
  136. //!
  137. //! \return The abridged FPU tag word.
  138. static uint8_t FsaveToFxsaveTagWord(uint16_t fsave_tag);
  139. // Integer registers.
  140. uint32_t eax;
  141. uint32_t ebx;
  142. uint32_t ecx;
  143. uint32_t edx;
  144. uint32_t edi; // destination index
  145. uint32_t esi; // source index
  146. uint32_t ebp; // base pointer
  147. uint32_t esp; // stack pointer
  148. uint32_t eip; // instruction pointer
  149. uint32_t eflags;
  150. uint16_t cs; // code segment selector
  151. uint16_t ds; // data segment selector
  152. uint16_t es; // extra segment selector
  153. uint16_t fs;
  154. uint16_t gs;
  155. uint16_t ss; // stack segment selector
  156. // Floating-point and vector registers.
  157. Fxsave fxsave;
  158. // Debug registers.
  159. uint32_t dr0;
  160. uint32_t dr1;
  161. uint32_t dr2;
  162. uint32_t dr3;
  163. uint32_t dr4; // obsolete, normally an alias for dr6
  164. uint32_t dr5; // obsolete, normally an alias for dr7
  165. uint32_t dr6;
  166. uint32_t dr7;
  167. };
  168. //! \brief A context structure carrying x86_64 CPU state.
  169. struct CPUContextX86_64 {
  170. using X87Register = CPUContextX86::X87Register;
  171. using X87OrMMXRegister = CPUContextX86::X87OrMMXRegister;
  172. using XMMRegister = CPUContextX86::XMMRegister;
  173. struct Fxsave {
  174. uint16_t fcw; // FPU control word
  175. uint16_t fsw; // FPU status word
  176. uint8_t ftw; // abridged FPU tag word
  177. uint8_t reserved_1;
  178. uint16_t fop; // FPU opcode
  179. union {
  180. // The expression of these union members is determined by the use of
  181. // fxsave/fxrstor or fxsave64/fxrstor64 (fxsaveq/fxrstorq). macOS and
  182. // Windows systems use the traditional fxsave/fxrstor structure.
  183. struct {
  184. // fxsave/fxrstor
  185. uint32_t fpu_ip; // FPU instruction pointer offset
  186. uint16_t fpu_cs; // FPU instruction pointer segment selector
  187. uint16_t reserved_2;
  188. uint32_t fpu_dp; // FPU data pointer offset
  189. uint16_t fpu_ds; // FPU data pointer segment selector
  190. uint16_t reserved_3;
  191. };
  192. struct {
  193. // fxsave64/fxrstor64 (fxsaveq/fxrstorq)
  194. uint64_t fpu_ip_64; // FPU instruction pointer
  195. uint64_t fpu_dp_64; // FPU data pointer
  196. };
  197. };
  198. uint32_t mxcsr; // multimedia extensions status and control register
  199. uint32_t mxcsr_mask; // valid bits in mxcsr
  200. X87OrMMXRegister st_mm[8];
  201. XMMRegister xmm[16];
  202. uint8_t reserved_4[48];
  203. uint8_t available[48];
  204. };
  205. // Integer registers.
  206. uint64_t rax;
  207. uint64_t rbx;
  208. uint64_t rcx;
  209. uint64_t rdx;
  210. uint64_t rdi; // destination index
  211. uint64_t rsi; // source index
  212. uint64_t rbp; // base pointer
  213. uint64_t rsp; // stack pointer
  214. uint64_t r8;
  215. uint64_t r9;
  216. uint64_t r10;
  217. uint64_t r11;
  218. uint64_t r12;
  219. uint64_t r13;
  220. uint64_t r14;
  221. uint64_t r15;
  222. uint64_t rip; // instruction pointer
  223. uint64_t rflags;
  224. uint16_t cs; // code segment selector
  225. uint16_t fs;
  226. uint16_t gs;
  227. // Floating-point and vector registers.
  228. Fxsave fxsave;
  229. // Debug registers.
  230. uint64_t dr0;
  231. uint64_t dr1;
  232. uint64_t dr2;
  233. uint64_t dr3;
  234. uint64_t dr4; // obsolete, normally an alias for dr6
  235. uint64_t dr5; // obsolete, normally an alias for dr7
  236. uint64_t dr6;
  237. uint64_t dr7;
  238. };
  239. //! \brief A context structure capable of carrying the context of any supported
  240. //! CPU architecture.
  241. struct CPUContext {
  242. //! \brief Returns the instruction pointer value from the context structure.
  243. //!
  244. //! This is a CPU architecture-independent method that is capable of
  245. //! recovering the instruction pointer from any supported CPU architecture’s
  246. //! context structure.
  247. uint64_t InstructionPointer() const;
  248. //! \brief The CPU architecture of a context structure. This field controls
  249. //! the expression of the union.
  250. CPUArchitecture architecture;
  251. union {
  252. CPUContextX86* x86;
  253. CPUContextX86_64* x86_64;
  254. };
  255. };
  256. } // namespace crashpad
  257. #endif // CRASHPAD_SNAPSHOT_SNAPSHOT_CPU_CONTEXT_H_