cpu_context_linux.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2017 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_LINUX_CPU_CONTEXT_LINUX_H_
  15. #define CRASHPAD_SNAPSHOT_LINUX_CPU_CONTEXT_LINUX_H_
  16. #include "build/build_config.h"
  17. #include "snapshot/cpu_context.h"
  18. #include "snapshot/linux/signal_context.h"
  19. #include "util/linux/thread_info.h"
  20. namespace crashpad {
  21. namespace internal {
  22. #if defined(ARCH_CPU_X86_FAMILY) || DOXYGEN
  23. //! \{
  24. //! \brief Initializes a CPUContextX86 structure from native context structures
  25. //! on Linux.
  26. //!
  27. //! \param[in] thread_context The native thread context.
  28. //! \param[in] float_context The native float context.
  29. //! \param[out] context The CPUContextX86 structure to initialize.
  30. void InitializeCPUContextX86(const ThreadContext::t32_t& thread_context,
  31. const FloatContext::f32_t& float_context,
  32. CPUContextX86* context);
  33. void InitializeCPUContextX86(const SignalThreadContext32& thread_context,
  34. const SignalFloatContext32& float_context,
  35. CPUContextX86* context);
  36. //! \}
  37. //! \brief Initializes GPR and debug state in a CPUContextX86 from a native
  38. //! signal context structure on Linux.
  39. //!
  40. //! Floating point state and debug registers are initialized to zero.
  41. //!
  42. //! \param[in] thread_context The native thread context.
  43. //! \param[out] context The CPUContextX86 structure to initialize.
  44. void InitializeCPUContextX86_NoFloatingPoint(
  45. const SignalThreadContext32& thread_context,
  46. CPUContextX86* context);
  47. //! \{
  48. //! \brief Initializes a CPUContextX86_64 structure from native context
  49. //! structures on Linux.
  50. //!
  51. //! \param[in] thread_context The native thread context.
  52. //! \param[in] float_context The native float context.
  53. //! \param[out] context The CPUContextX86_64 structure to initialize.
  54. void InitializeCPUContextX86_64(const ThreadContext::t64_t& thread_context,
  55. const FloatContext::f64_t& float_context,
  56. CPUContextX86_64* context);
  57. void InitializeCPUContextX86_64(const SignalThreadContext64& thread_context,
  58. const SignalFloatContext64& float_context,
  59. CPUContextX86_64* context);
  60. //! \}
  61. //! \brief Initializes GPR and debug state in a CPUContextX86_64 from a native
  62. //! signal context structure on Linux.
  63. //!
  64. //! Floating point state and debug registers are initialized to zero.
  65. //!
  66. //! \param[in] thread_context The native thread context.
  67. //! \param[out] context The CPUContextX86_64 structure to initialize.
  68. void InitializeCPUContextX86_64_NoFloatingPoint(
  69. const SignalThreadContext64& thread_context,
  70. CPUContextX86_64* context);
  71. #endif // ARCH_CPU_X86_FAMILY || DOXYGEN
  72. #if defined(ARCH_CPU_ARM_FAMILY) || DOXYGEN
  73. //! \brief Initializes a CPUContextARM structure from native context structures
  74. //! on Linux.
  75. //!
  76. //! \param[in] thread_context The native thread context.
  77. //! \param[in] float_context The native float context.
  78. //! \param[out] context The CPUContextARM structure to initialize.
  79. void InitializeCPUContextARM(const ThreadContext::t32_t& thread_context,
  80. const FloatContext::f32_t& float_context,
  81. CPUContextARM* context);
  82. //! \brief Initializes GPR state in a CPUContextARM from a native signal context
  83. //! structure on Linux.
  84. //!
  85. //! Floating point state is initialized to zero.
  86. //!
  87. //! \param[in] thread_context The native thread context.
  88. //! \param[out] context The CPUContextARM structure to initialize.
  89. void InitializeCPUContextARM_NoFloatingPoint(
  90. const SignalThreadContext32& thread_context,
  91. CPUContextARM* context);
  92. //! \brief Initializes a CPUContextARM64 structure from native context
  93. //! structures on Linux.
  94. //!
  95. //! \param[in] thread_context The native thread context.
  96. //! \param[in] float_context The native float context.
  97. //! \param[out] context The CPUContextARM64 structure to initialize.
  98. void InitializeCPUContextARM64(const ThreadContext::t64_t& thread_context,
  99. const FloatContext::f64_t& float_context,
  100. CPUContextARM64* context);
  101. //! \brief Initializes GPR state in a CPUContextARM64 from a native context
  102. //! structure on Linux.
  103. //!
  104. //! Floating point state is initialized to zero.
  105. //!
  106. //! \param[in] thread_context The native thread context.
  107. //! \param[out] context The CPUContextARM64 structure to initialize.
  108. void InitializeCPUContextARM64_NoFloatingPoint(
  109. const ThreadContext::t64_t& thread_context,
  110. CPUContextARM64* context);
  111. //! \brief Initializes FPSIMD state in a CPUContextARM64 from a native fpsimd
  112. //! signal context structure on Linux.
  113. //!
  114. //! General purpose registers are not initialized.
  115. //!
  116. //! \param[in] float_context The native fpsimd context.
  117. //! \param[out] context The CPUContextARM64 structure to initialize.
  118. void InitializeCPUContextARM64_OnlyFPSIMD(
  119. const SignalFPSIMDContext& float_context,
  120. CPUContextARM64* context);
  121. #endif // ARCH_CPU_ARM_FAMILY || DOXYGEN
  122. #if defined(ARCH_CPU_MIPS_FAMILY) || DOXYGEN
  123. //! \brief Initializes a CPUContextMIPS structure from native context
  124. //! structures on Linux.
  125. //!
  126. //! This function has template specializations for MIPSEL and MIPS64EL
  127. //! architecture contexts, using ContextTraits32 or ContextTraits64 as template
  128. //! parameter, respectively.
  129. //!
  130. //! \param[in] thread_context The native thread context.
  131. //! \param[in] float_context The native float context.
  132. //! \param[out] context The CPUContextMIPS structure to initialize.
  133. template <typename Traits>
  134. void InitializeCPUContextMIPS(
  135. const typename Traits::SignalThreadContext& thread_context,
  136. const typename Traits::SignalFloatContext& float_context,
  137. typename Traits::CPUContext* context) {
  138. static_assert(sizeof(context->regs) == sizeof(thread_context.regs),
  139. "registers size mismatch");
  140. static_assert(sizeof(context->fpregs) == sizeof(float_context.fpregs),
  141. "fp registers size mismatch");
  142. memcpy(&context->regs, &thread_context.regs, sizeof(context->regs));
  143. context->mdlo = thread_context.lo;
  144. context->mdhi = thread_context.hi;
  145. context->cp0_epc = thread_context.cp0_epc;
  146. context->cp0_badvaddr = thread_context.cp0_badvaddr;
  147. context->cp0_status = thread_context.cp0_status;
  148. context->cp0_cause = thread_context.cp0_cause;
  149. memcpy(&context->fpregs, &float_context.fpregs, sizeof(context->fpregs));
  150. context->fpcsr = float_context.fpcsr;
  151. context->fir = float_context.fpu_id;
  152. }
  153. #endif // ARCH_CPU_MIPS_FAMILY || DOXYGEN
  154. #if defined(ARCH_CPU_RISCV_FAMILY) || DOXYGEN
  155. //! \brief Initializes a CPUContextRISCV structure from native context
  156. //! structures on Linux.
  157. //!
  158. //! \param[in] thread_context The native thread context.
  159. //! \param[in] float_context The native float context.
  160. //! \param[out] context The CPUContextRISCV structure to initialize.
  161. template <typename Traits>
  162. void InitializeCPUContextRISCV(
  163. const typename Traits::SignalThreadContext& thread_context,
  164. const typename Traits::SignalFloatContext& float_context,
  165. typename Traits::CPUContext* context);
  166. #endif // ARCH_CPU_RISCV_FAMILY || DOXYGEN
  167. } // namespace internal
  168. } // namespace crashpad
  169. #endif // CRASHPAD_SNAPSHOT_LINUX_CPU_CONTEXT_LINUX_H_