0017-third_party-crashpad-add-support-for-riscv.patch 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. From 62457fa5271a6b22210ec0adefe6fee944f785f7 Mon Sep 17 00:00:00 2001
  2. From: Rebecca Chang Swee Fun <rebecca.chang@starfivetech.com>
  3. Date: Thu, 31 Mar 2022 00:35:10 +0000
  4. Subject: [PATCH 17/22] third_party: crashpad: add support for riscv
  5. Credit to work contributed in Github.
  6. https://github.com/felixonmars/archriscv-packages/tree/master/chromium
  7. Signed-off-by: Rebecca Chang Swee Fun <rebecca.chang@starfivetech.com>
  8. ---
  9. .../crashpad/minidump/minidump_context.h | 35 +++++++++
  10. .../minidump/minidump_context_writer.cc | 44 +++++++++++
  11. .../minidump/minidump_context_writer.h | 41 ++++++++++
  12. .../minidump/minidump_misc_info_writer.cc | 4 +
  13. .../crashpad/snapshot/capture_memory.cc | 10 +++
  14. .../crashpad/snapshot/cpu_architecture.h | 8 +-
  15. .../crashpad/crashpad/snapshot/cpu_context.cc | 2 +
  16. .../crashpad/crashpad/snapshot/cpu_context.h | 16 ++++
  17. .../snapshot/linux/cpu_context_linux.cc | 24 ++++++
  18. .../snapshot/linux/cpu_context_linux.h | 16 ++++
  19. .../linux/exception_snapshot_linux.cc | 55 +++++++++++++
  20. .../snapshot/linux/exception_snapshot_linux.h | 3 +
  21. .../snapshot/linux/process_reader_linux.cc | 3 +
  22. .../crashpad/snapshot/linux/signal_context.h | 61 +++++++++++++++
  23. .../snapshot/linux/system_snapshot_linux.cc | 12 +++
  24. .../snapshot/linux/thread_snapshot_linux.cc | 16 ++++
  25. .../snapshot/linux/thread_snapshot_linux.h | 3 +
  26. .../crashpad/crashpad/util/linux/ptracer.cc | 44 +++++++++++
  27. .../crashpad/util/linux/thread_info.h | 78 ++++++++++++++++++-
  28. .../util/net/http_transport_libcurl.cc | 2 +
  29. 20 files changed, 475 insertions(+), 2 deletions(-)
  30. diff --git a/third_party/crashpad/crashpad/minidump/minidump_context.h b/third_party/crashpad/crashpad/minidump/minidump_context.h
  31. index c10743d84899d..616b43654d11f 100644
  32. --- a/third_party/crashpad/crashpad/minidump/minidump_context.h
  33. +++ b/third_party/crashpad/crashpad/minidump/minidump_context.h
  34. @@ -637,6 +637,41 @@ struct MinidumpContextMIPS64 {
  35. uint64_t fir;
  36. };
  37. +//! \brief 64bit RISC-V-specifc flags for MinidumpContextRISCV64::context_flags.
  38. +//! Based on minidump_cpu_riscv64.h from breakpad
  39. +enum MinidumpContextRISCV64Flags : uint32_t {
  40. + //! \brief Identifies the context structure as RISCV64.
  41. + kMinidumpContextRISCV64 = 0x00080000,
  42. +
  43. + //! \brief Indicates the validity of integer registers.
  44. + //!
  45. + //! Registers `x1`-`x31` and pc are valid.
  46. + kMinidumpContextRISCV64Integer = kMinidumpContextRISCV64 | 0x00000002,
  47. +
  48. + //! \brief Indicates the validity of floating point registers.
  49. + //!
  50. + //! Floating point registers `f0`-`f31`, and `fcsr` are valid
  51. + kMinidumpContextRISCV64FloatingPoint = kMinidumpContextRISCV64 | 0x00000004,
  52. +
  53. + //! \brief Indicates the validity of all registers.
  54. + kMinidumpContextRISCV64All = kMinidumpContextRISCV64Integer |
  55. + kMinidumpContextRISCV64FloatingPoint,
  56. +};
  57. +
  58. +//! \brief A 64bit RISCV CPU context (register state) carried in a minidump file.
  59. +struct MinidumpContextRISCV64 {
  60. + uint64_t context_flags;
  61. +
  62. + //! \brief General purpose registers.
  63. + uint64_t regs[32];
  64. +
  65. + //! \brief FPU registers.
  66. + uint64_t fpregs[32];
  67. +
  68. + //! \brief FPU status register.
  69. + uint64_t fcsr;
  70. +};
  71. +
  72. } // namespace crashpad
  73. #endif // CRASHPAD_MINIDUMP_MINIDUMP_CONTEXT_H_
  74. diff --git a/third_party/crashpad/crashpad/minidump/minidump_context_writer.cc b/third_party/crashpad/crashpad/minidump/minidump_context_writer.cc
  75. index 6ae25b8fe12f8..cfd86fa9f4d58 100644
  76. --- a/third_party/crashpad/crashpad/minidump/minidump_context_writer.cc
  77. +++ b/third_party/crashpad/crashpad/minidump/minidump_context_writer.cc
  78. @@ -102,6 +102,13 @@ MinidumpContextWriter::CreateFromSnapshot(const CPUContext* context_snapshot) {
  79. break;
  80. }
  81. + case kCPUArchitectureRISCV64: {
  82. + context = std::make_unique<MinidumpContextRISCV64Writer>();
  83. + reinterpret_cast<MinidumpContextRISCV64Writer*>(context.get())
  84. + ->InitializeFromSnapshot(context_snapshot->riscv64);
  85. + break;
  86. + }
  87. +
  88. default: {
  89. LOG(ERROR) << "unknown context architecture "
  90. << context_snapshot->architecture;
  91. @@ -557,4 +564,41 @@ size_t MinidumpContextMIPS64Writer::ContextSize() const {
  92. return sizeof(context_);
  93. }
  94. +MinidumpContextRISCV64Writer::MinidumpContextRISCV64Writer()
  95. + : MinidumpContextWriter(), context_() {
  96. + context_.context_flags = kMinidumpContextRISCV64;
  97. +}
  98. +
  99. +MinidumpContextRISCV64Writer::~MinidumpContextRISCV64Writer() = default;
  100. +
  101. +void MinidumpContextRISCV64Writer::InitializeFromSnapshot(
  102. + const CPUContextRISCV64* context_snapshot) {
  103. + DCHECK_EQ(state(), kStateMutable);
  104. + DCHECK_EQ(context_.context_flags, kMinidumpContextRISCV64);
  105. +
  106. + context_.context_flags = kMinidumpContextRISCV64All;
  107. +
  108. + static_assert(sizeof(context_.regs) == sizeof(context_snapshot->regs),
  109. + "GPRs size mismatch");
  110. + memcpy(context_.regs, context_snapshot->regs, sizeof(context_.regs));
  111. +
  112. + static_assert(sizeof(context_.fpregs) == sizeof(context_snapshot->fpregs),
  113. + "FPRs size mismatch");
  114. + memcpy(context_.fpregs,
  115. + context_snapshot->fpregs,
  116. + sizeof(context_.fpregs));
  117. + context_.fcsr = context_snapshot->fcsr;
  118. +}
  119. +
  120. +bool MinidumpContextRISCV64Writer::WriteObject(
  121. + FileWriterInterface* file_writer) {
  122. + DCHECK_EQ(state(), kStateWritable);
  123. + return file_writer->Write(&context_, sizeof(context_));
  124. +}
  125. +
  126. +size_t MinidumpContextRISCV64Writer::ContextSize() const {
  127. + DCHECK_GE(state(), kStateFrozen);
  128. + return sizeof(context_);
  129. +}
  130. +
  131. } // namespace crashpad
  132. diff --git a/third_party/crashpad/crashpad/minidump/minidump_context_writer.h b/third_party/crashpad/crashpad/minidump/minidump_context_writer.h
  133. index 9acc7fa012f84..da48628f5ae0d 100644
  134. --- a/third_party/crashpad/crashpad/minidump/minidump_context_writer.h
  135. +++ b/third_party/crashpad/crashpad/minidump/minidump_context_writer.h
  136. @@ -369,6 +369,47 @@ class MinidumpContextMIPS64Writer final : public MinidumpContextWriter {
  137. MinidumpContextMIPS64 context_;
  138. };
  139. +//! \brief The writer for a MinidumpContextRISCV64 structure in a minidump file.
  140. +class MinidumpContextRISCV64Writer final : public MinidumpContextWriter {
  141. + public:
  142. + MinidumpContextRISCV64Writer();
  143. + ~MinidumpContextRISCV64Writer() override;
  144. +
  145. + MinidumpContextRISCV64Writer(const MinidumpContextRISCV64Writer&) = delete;
  146. + void operator=(const MinidumpContextRISCV64Writer&) = delete;
  147. +
  148. + //! \brief Initializes the MinidumpContextRISCV based on \a context_snapshot.
  149. + //!
  150. + //! \param[in] context_snapshot The context snapshot to use as source data.
  151. + //!
  152. + //! \note Valid in #kStateMutable. No mutation of context() may be done before
  153. + //! calling this method, and it is not normally necessary to alter
  154. + //! context() after calling this method.
  155. + void InitializeFromSnapshot(const CPUContextRISCV64* context_snapshot);
  156. +
  157. + //! \brief Returns a pointer to the context structure that this object will
  158. + //! write.
  159. + //!
  160. + //! \attention This returns a non-`const` pointer to this object’s private
  161. + //! data so that a caller can populate the context structure directly.
  162. + //! This is done because providing setter interfaces to each field in the
  163. + //! context structure would be unwieldy and cumbersome. Care must be taken
  164. + //! to populate the context structure correctly. The context structure
  165. + //! must only be modified while this object is in the #kStateMutable
  166. + //! state.
  167. + MinidumpContextRISCV64* context() { return &context_; }
  168. +
  169. + protected:
  170. + // MinidumpWritable:
  171. + bool WriteObject(FileWriterInterface* file_writer) override;
  172. +
  173. + // MinidumpContextWriter:
  174. + size_t ContextSize() const override;
  175. +
  176. + private:
  177. + MinidumpContextRISCV64 context_;
  178. +};
  179. +
  180. } // namespace crashpad
  181. #endif // CRASHPAD_MINIDUMP_MINIDUMP_CONTEXT_WRITER_H_
  182. diff --git a/third_party/crashpad/crashpad/minidump/minidump_misc_info_writer.cc b/third_party/crashpad/crashpad/minidump/minidump_misc_info_writer.cc
  183. index 51c91c4c90e6f..4945b3976e5e3 100644
  184. --- a/third_party/crashpad/crashpad/minidump/minidump_misc_info_writer.cc
  185. +++ b/third_party/crashpad/crashpad/minidump/minidump_misc_info_writer.cc
  186. @@ -175,6 +175,10 @@ std::string MinidumpMiscInfoDebugBuildString() {
  187. static constexpr char kCPU[] = "mips";
  188. #elif defined(ARCH_CPU_MIPS64EL)
  189. static constexpr char kCPU[] = "mips64";
  190. +#elif defined(ARCH_CPU_RISCV)
  191. + static constexpr char kCPU[] = "riscv";
  192. +#elif defined(ARCH_CPU_RISCV64)
  193. + static constexpr char kCPU[] = "riscv64";
  194. #else
  195. #error define kCPU for this CPU
  196. #endif
  197. diff --git a/third_party/crashpad/crashpad/snapshot/capture_memory.cc b/third_party/crashpad/crashpad/snapshot/capture_memory.cc
  198. index cb8231bdb97ca..08de6392466f7 100644
  199. --- a/third_party/crashpad/crashpad/snapshot/capture_memory.cc
  200. +++ b/third_party/crashpad/crashpad/snapshot/capture_memory.cc
  201. @@ -117,6 +117,16 @@ void CaptureMemory::PointedToByContext(const CPUContext& context,
  202. for (size_t i = 0; i < std::size(context.mipsel->regs); ++i) {
  203. MaybeCaptureMemoryAround(delegate, context.mipsel->regs[i]);
  204. }
  205. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  206. + if (context.architecture == kCPUArchitectureRISCV) {
  207. + for (size_t i = 0; i < base::size(context.riscv->regs); ++i) {
  208. + MaybeCaptureMemoryAround(delegate, context.riscv->regs[i]);
  209. + }
  210. + } else {
  211. + for (size_t i = 0; i < base::size(context.riscv64->regs); ++i) {
  212. + MaybeCaptureMemoryAround(delegate, context.riscv64->regs[i]);
  213. + }
  214. + }
  215. #else
  216. #error Port.
  217. #endif
  218. diff --git a/third_party/crashpad/crashpad/snapshot/cpu_architecture.h b/third_party/crashpad/crashpad/snapshot/cpu_architecture.h
  219. index 811a7209587d2..5b09abdb96761 100644
  220. --- a/third_party/crashpad/crashpad/snapshot/cpu_architecture.h
  221. +++ b/third_party/crashpad/crashpad/snapshot/cpu_architecture.h
  222. @@ -43,7 +43,13 @@ enum CPUArchitecture {
  223. kCPUArchitectureMIPSEL,
  224. //! \brief 64-bit MIPSEL.
  225. - kCPUArchitectureMIPS64EL
  226. + kCPUArchitectureMIPS64EL,
  227. +
  228. + //! \brief 32-bit RISCV.
  229. + kCPUArchitectureRISCV,
  230. +
  231. + //! \brief 64-bit RISCV.
  232. + kCPUArchitectureRISCV64
  233. };
  234. } // namespace crashpad
  235. diff --git a/third_party/crashpad/crashpad/snapshot/cpu_context.cc b/third_party/crashpad/crashpad/snapshot/cpu_context.cc
  236. index 9b7b3f14f1164..b717e5ea007f9 100644
  237. --- a/third_party/crashpad/crashpad/snapshot/cpu_context.cc
  238. +++ b/third_party/crashpad/crashpad/snapshot/cpu_context.cc
  239. @@ -226,10 +226,12 @@ bool CPUContext::Is64Bit() const {
  240. case kCPUArchitectureX86_64:
  241. case kCPUArchitectureARM64:
  242. case kCPUArchitectureMIPS64EL:
  243. + case kCPUArchitectureRISCV64:
  244. return true;
  245. case kCPUArchitectureX86:
  246. case kCPUArchitectureARM:
  247. case kCPUArchitectureMIPSEL:
  248. + case kCPUArchitectureRISCV:
  249. return false;
  250. default:
  251. NOTREACHED();
  252. diff --git a/third_party/crashpad/crashpad/snapshot/cpu_context.h b/third_party/crashpad/crashpad/snapshot/cpu_context.h
  253. index 4e21b56c590da..42b405f863428 100644
  254. --- a/third_party/crashpad/crashpad/snapshot/cpu_context.h
  255. +++ b/third_party/crashpad/crashpad/snapshot/cpu_context.h
  256. @@ -362,6 +362,20 @@ struct CPUContextMIPS64 {
  257. uint64_t fir;
  258. };
  259. +//! \brief A context structure carrying RISC CPU state.
  260. +struct CPUContextRISCV {
  261. + uint32_t regs[32];
  262. + uint64_t fpregs[32];
  263. + uint32_t fcsr;
  264. +};
  265. +
  266. +//! \brief A context structure carrying RISC64 CPU state.
  267. +struct CPUContextRISCV64 {
  268. + uint64_t regs[32];
  269. + uint64_t fpregs[32];
  270. + uint32_t fcsr;
  271. +};
  272. +
  273. //! \brief A context structure capable of carrying the context of any supported
  274. //! CPU architecture.
  275. struct CPUContext {
  276. @@ -402,6 +416,8 @@ struct CPUContext {
  277. CPUContextARM64* arm64;
  278. CPUContextMIPS* mipsel;
  279. CPUContextMIPS64* mips64;
  280. + CPUContextRISCV* riscv;
  281. + CPUContextRISCV64* riscv64;
  282. };
  283. };
  284. diff --git a/third_party/crashpad/crashpad/snapshot/linux/cpu_context_linux.cc b/third_party/crashpad/crashpad/snapshot/linux/cpu_context_linux.cc
  285. index 8464a5a27b2dc..ec41216daa902 100644
  286. --- a/third_party/crashpad/crashpad/snapshot/linux/cpu_context_linux.cc
  287. +++ b/third_party/crashpad/crashpad/snapshot/linux/cpu_context_linux.cc
  288. @@ -266,6 +266,30 @@ void InitializeCPUContextARM64_OnlyFPSIMD(
  289. context->fpcr = float_context.fpcr;
  290. }
  291. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  292. +
  293. +template <typename Traits>
  294. +void InitializeCPUContextRISCV(
  295. + const typename Traits::SignalThreadContext& thread_context,
  296. + const typename Traits::SignalFloatContext& float_context,
  297. + typename Traits::CPUContext* context) {
  298. + static_assert(sizeof(context->regs) == sizeof(thread_context),
  299. + "registers size mismatch");
  300. + static_assert(sizeof(context->fpregs) == sizeof(float_context.f),
  301. + "fp registers size mismatch");
  302. + memcpy(&context->regs, &thread_context, sizeof(context->regs));
  303. + memcpy(&context->fpregs, &float_context, sizeof(context->fpregs));
  304. + context->fcsr = float_context.fcsr;
  305. +}
  306. +template void InitializeCPUContextRISCV<ContextTraits32>(
  307. + const ContextTraits32::SignalThreadContext& thread_context,
  308. + const ContextTraits32::SignalFloatContext& float_context,
  309. + ContextTraits32::CPUContext* context);
  310. +template void InitializeCPUContextRISCV<ContextTraits64>(
  311. + const ContextTraits64::SignalThreadContext& thread_context,
  312. + const ContextTraits64::SignalFloatContext& float_context,
  313. + ContextTraits64::CPUContext* context);
  314. +
  315. #endif // ARCH_CPU_X86_FAMILY
  316. } // namespace internal
  317. diff --git a/third_party/crashpad/crashpad/snapshot/linux/cpu_context_linux.h b/third_party/crashpad/crashpad/snapshot/linux/cpu_context_linux.h
  318. index 9f46a48977e12..1add07f81af88 100644
  319. --- a/third_party/crashpad/crashpad/snapshot/linux/cpu_context_linux.h
  320. +++ b/third_party/crashpad/crashpad/snapshot/linux/cpu_context_linux.h
  321. @@ -174,6 +174,22 @@ void InitializeCPUContextMIPS(
  322. #endif // ARCH_CPU_MIPS_FAMILY || DOXYGEN
  323. +#if defined(ARCH_CPU_RISCV_FAMILY) || DOXYGEN
  324. +
  325. +//! \brief Initializes a CPUContextRISCV structure from native context
  326. +//! structures on Linux.
  327. +//!
  328. +//! \param[in] thread_context The native thread context.
  329. +//! \param[in] float_context The native float context.
  330. +//! \param[out] context The CPUContextRISCV structure to initialize.
  331. +template <typename Traits>
  332. +void InitializeCPUContextRISCV(
  333. + const typename Traits::SignalThreadContext& thread_context,
  334. + const typename Traits::SignalFloatContext& float_context,
  335. + typename Traits::CPUContext* context);
  336. +
  337. +#endif // ARCH_CPU_RISCV_FAMILY || DOXYGEN
  338. +
  339. } // namespace internal
  340. } // namespace crashpad
  341. diff --git a/third_party/crashpad/crashpad/snapshot/linux/exception_snapshot_linux.cc b/third_party/crashpad/crashpad/snapshot/linux/exception_snapshot_linux.cc
  342. index efc9e5694ea8b..3d57d96756208 100644
  343. --- a/third_party/crashpad/crashpad/snapshot/linux/exception_snapshot_linux.cc
  344. +++ b/third_party/crashpad/crashpad/snapshot/linux/exception_snapshot_linux.cc
  345. @@ -325,6 +325,61 @@ bool ExceptionSnapshotLinux::ReadContext<ContextTraits64>(
  346. reader, context_address, context_.mips64);
  347. }
  348. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  349. +
  350. +template <typename Traits>
  351. +static bool ReadContext(ProcessReaderLinux* reader,
  352. + LinuxVMAddress context_address,
  353. + typename Traits::CPUContext* dest_context) {
  354. + const ProcessMemory* memory = reader->Memory();
  355. +
  356. + LinuxVMAddress gregs_address = context_address +
  357. + offsetof(UContext<Traits>, mcontext) +
  358. + offsetof(typename Traits::MContext, gregs);
  359. +
  360. + typename Traits::SignalThreadContext thread_context;
  361. + if (!memory->Read(gregs_address, sizeof(thread_context), &thread_context)) {
  362. + LOG(ERROR) << "Couldn't read gregs";
  363. + return false;
  364. + }
  365. +
  366. + LinuxVMAddress fpregs_address = context_address +
  367. + offsetof(UContext<Traits>, mcontext) +
  368. + offsetof(typename Traits::MContext, fpregs);
  369. +
  370. + typename Traits::SignalFloatContext fp_context;
  371. + if (!memory->Read(fpregs_address, sizeof(fp_context), &fp_context)) {
  372. + LOG(ERROR) << "Couldn't read fpregs";
  373. + return false;
  374. + }
  375. +
  376. + InitializeCPUContextRISCV<Traits>(thread_context, fp_context, dest_context);
  377. +
  378. + return true;
  379. +}
  380. +
  381. +template <>
  382. +bool ExceptionSnapshotLinux::ReadContext<ContextTraits32>(
  383. + ProcessReaderLinux* reader,
  384. + LinuxVMAddress context_address) {
  385. + context_.architecture = kCPUArchitectureRISCV;
  386. + context_.riscv = &context_union_.riscv;
  387. +
  388. + return internal::ReadContext<ContextTraits32>(
  389. + reader, context_address, context_.riscv);
  390. +}
  391. +
  392. +template <>
  393. +bool ExceptionSnapshotLinux::ReadContext<ContextTraits64>(
  394. + ProcessReaderLinux* reader,
  395. + LinuxVMAddress context_address) {
  396. + context_.architecture = kCPUArchitectureRISCV64;
  397. + context_.riscv64 = &context_union_.riscv64;
  398. +
  399. + return internal::ReadContext<ContextTraits64>(
  400. + reader, context_address, context_.riscv64);
  401. +}
  402. +
  403. #endif // ARCH_CPU_X86_FAMILY
  404. bool ExceptionSnapshotLinux::Initialize(
  405. diff --git a/third_party/crashpad/crashpad/snapshot/linux/exception_snapshot_linux.h b/third_party/crashpad/crashpad/snapshot/linux/exception_snapshot_linux.h
  406. index 05f6004e11d1e..0520af4ce9019 100644
  407. --- a/third_party/crashpad/crashpad/snapshot/linux/exception_snapshot_linux.h
  408. +++ b/third_party/crashpad/crashpad/snapshot/linux/exception_snapshot_linux.h
  409. @@ -89,6 +89,9 @@ class ExceptionSnapshotLinux final : public ExceptionSnapshot {
  410. #elif defined(ARCH_CPU_MIPS_FAMILY)
  411. CPUContextMIPS mipsel;
  412. CPUContextMIPS64 mips64;
  413. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  414. + CPUContextRISCV riscv;
  415. + CPUContextRISCV64 riscv64;
  416. #endif
  417. } context_union_;
  418. CPUContext context_;
  419. diff --git a/third_party/crashpad/crashpad/snapshot/linux/process_reader_linux.cc b/third_party/crashpad/crashpad/snapshot/linux/process_reader_linux.cc
  420. index 5711f343a46c4..abbb1662c3e0a 100644
  421. --- a/third_party/crashpad/crashpad/snapshot/linux/process_reader_linux.cc
  422. +++ b/third_party/crashpad/crashpad/snapshot/linux/process_reader_linux.cc
  423. @@ -108,6 +108,9 @@ void ProcessReaderLinux::Thread::InitializeStack(ProcessReaderLinux* reader) {
  424. #elif defined(ARCH_CPU_MIPS_FAMILY)
  425. stack_pointer = reader->Is64Bit() ? thread_info.thread_context.t64.regs[29]
  426. : thread_info.thread_context.t32.regs[29];
  427. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  428. + stack_pointer = reader->Is64Bit() ? thread_info.thread_context.t64.sp
  429. + : thread_info.thread_context.t32.sp;
  430. #else
  431. #error Port.
  432. #endif
  433. diff --git a/third_party/crashpad/crashpad/snapshot/linux/signal_context.h b/third_party/crashpad/crashpad/snapshot/linux/signal_context.h
  434. index c004f8f6dfab6..6aa9ee38f5b04 100644
  435. --- a/third_party/crashpad/crashpad/snapshot/linux/signal_context.h
  436. +++ b/third_party/crashpad/crashpad/snapshot/linux/signal_context.h
  437. @@ -422,6 +422,67 @@ static_assert(offsetof(UContext<ContextTraits64>, mcontext.fpregs) ==
  438. "context offset mismatch");
  439. #endif
  440. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  441. +
  442. +struct MContext32 {
  443. + uint32_t gregs[32];
  444. + uint64_t fpregs[32];
  445. + unsigned int fcsr;
  446. +};
  447. +
  448. +struct MContext64 {
  449. + uint64_t gregs[32];
  450. + uint64_t fpregs[32];
  451. + unsigned int fcsr;
  452. +};
  453. +
  454. +struct ContextTraits32 : public Traits32 {
  455. + using MContext = MContext32;
  456. + using SignalThreadContext = ThreadContext::t32_t;
  457. + using SignalFloatContext = FloatContext::f32_t;
  458. + using CPUContext = CPUContextRISCV;
  459. +};
  460. +
  461. +struct ContextTraits64 : public Traits64 {
  462. + using MContext = MContext64;
  463. + using SignalThreadContext = ThreadContext::t64_t;
  464. + using SignalFloatContext = FloatContext::f64_t;
  465. + using CPUContext = CPUContextRISCV64;
  466. +};
  467. +
  468. +template <typename Traits>
  469. +struct UContext {
  470. + typename Traits::ULong flags;
  471. + typename Traits::Address link;
  472. + SignalStack<Traits> stack;
  473. + Sigset<Traits> sigmask;
  474. + char padding[128 - sizeof(sigmask)];
  475. + typename Traits::Char_64Only padding2[8];
  476. + typename Traits::MContext mcontext;
  477. +};
  478. +
  479. +#if defined(ARCH_CPU_RISCV)
  480. +static_assert(offsetof(UContext<ContextTraits32>, mcontext) ==
  481. + offsetof(ucontext_t, uc_mcontext),
  482. + "context offset mismatch");
  483. +static_assert(offsetof(UContext<ContextTraits32>, mcontext.gregs) ==
  484. + offsetof(ucontext_t, uc_mcontext.__gregs),
  485. + "context offset mismatch");
  486. +static_assert(offsetof(UContext<ContextTraits32>, mcontext.fpregs) ==
  487. + offsetof(ucontext_t, uc_mcontext.__fpregs),
  488. + "context offset mismatch");
  489. +#elif defined(ARCH_CPU_RISCV64)
  490. +static_assert(offsetof(UContext<ContextTraits64>, mcontext) ==
  491. + offsetof(ucontext_t, uc_mcontext),
  492. + "context offset mismatch");
  493. +static_assert(offsetof(UContext<ContextTraits64>, mcontext.gregs) ==
  494. + offsetof(ucontext_t, uc_mcontext.__gregs),
  495. + "context offset mismatch");
  496. +static_assert(offsetof(UContext<ContextTraits64>, mcontext.fpregs) ==
  497. + offsetof(ucontext_t, uc_mcontext.__fpregs),
  498. + "context offset mismatch");
  499. +#endif
  500. +
  501. #else
  502. #error Port.
  503. #endif // ARCH_CPU_X86_FAMILY
  504. diff --git a/third_party/crashpad/crashpad/snapshot/linux/system_snapshot_linux.cc b/third_party/crashpad/crashpad/snapshot/linux/system_snapshot_linux.cc
  505. index e77bcafa9c6b5..55564ae4aeaf5 100644
  506. --- a/third_party/crashpad/crashpad/snapshot/linux/system_snapshot_linux.cc
  507. +++ b/third_party/crashpad/crashpad/snapshot/linux/system_snapshot_linux.cc
  508. @@ -205,6 +205,9 @@ CPUArchitecture SystemSnapshotLinux::GetCPUArchitecture() const {
  509. #elif defined(ARCH_CPU_MIPS_FAMILY)
  510. return process_reader_->Is64Bit() ? kCPUArchitectureMIPS64EL
  511. : kCPUArchitectureMIPSEL;
  512. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  513. + return process_reader_->Is64Bit() ? kCPUArchitectureRISCV64
  514. + : kCPUArchitectureRISCV;
  515. #else
  516. #error port to your architecture
  517. #endif
  518. @@ -220,6 +223,9 @@ uint32_t SystemSnapshotLinux::CPURevision() const {
  519. #elif defined(ARCH_CPU_MIPS_FAMILY)
  520. // Not implementable on MIPS
  521. return 0;
  522. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  523. + // Not implementable on RISCV
  524. + return 0;
  525. #else
  526. #error port to your architecture
  527. #endif
  528. @@ -240,6 +246,9 @@ std::string SystemSnapshotLinux::CPUVendor() const {
  529. #elif defined(ARCH_CPU_MIPS_FAMILY)
  530. // Not implementable on MIPS
  531. return std::string();
  532. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  533. + // Not implementable on RISCV
  534. + return std::string();
  535. #else
  536. #error port to your architecture
  537. #endif
  538. @@ -373,6 +382,9 @@ bool SystemSnapshotLinux::NXEnabled() const {
  539. #elif defined(ARCH_CPU_MIPS_FAMILY)
  540. // Not implementable on MIPS
  541. return false;
  542. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  543. + // Not implementable on RISCV
  544. + return false;
  545. #else
  546. #error Port.
  547. #endif // ARCH_CPU_X86_FAMILY
  548. diff --git a/third_party/crashpad/crashpad/snapshot/linux/thread_snapshot_linux.cc b/third_party/crashpad/crashpad/snapshot/linux/thread_snapshot_linux.cc
  549. index f279e0adaddba..8c133a7ca909f 100644
  550. --- a/third_party/crashpad/crashpad/snapshot/linux/thread_snapshot_linux.cc
  551. +++ b/third_party/crashpad/crashpad/snapshot/linux/thread_snapshot_linux.cc
  552. @@ -189,6 +189,22 @@ bool ThreadSnapshotLinux::Initialize(
  553. thread.thread_info.float_context.f32,
  554. context_.mipsel);
  555. }
  556. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  557. + if (process_reader->Is64Bit()) {
  558. + context_.architecture = kCPUArchitectureRISCV64;
  559. + context_.riscv64 = &context_union_.riscv64;
  560. + InitializeCPUContextRISCV<ContextTraits64>(
  561. + thread.thread_info.thread_context.t64,
  562. + thread.thread_info.float_context.f64,
  563. + context_.riscv64);
  564. + } else {
  565. + context_.architecture = kCPUArchitectureRISCV;
  566. + context_.riscv = &context_union_.riscv;
  567. + InitializeCPUContextRISCV<ContextTraits32>(
  568. + thread.thread_info.thread_context.t32,
  569. + thread.thread_info.float_context.f32,
  570. + context_.riscv);
  571. + }
  572. #else
  573. #error Port.
  574. #endif
  575. diff --git a/third_party/crashpad/crashpad/snapshot/linux/thread_snapshot_linux.h b/third_party/crashpad/crashpad/snapshot/linux/thread_snapshot_linux.h
  576. index 40cd7e7f54a39..aea1ce2047a6b 100644
  577. --- a/third_party/crashpad/crashpad/snapshot/linux/thread_snapshot_linux.h
  578. +++ b/third_party/crashpad/crashpad/snapshot/linux/thread_snapshot_linux.h
  579. @@ -73,6 +73,9 @@ class ThreadSnapshotLinux final : public ThreadSnapshot {
  580. #elif defined(ARCH_CPU_MIPS_FAMILY)
  581. CPUContextMIPS mipsel;
  582. CPUContextMIPS64 mips64;
  583. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  584. + CPUContextRISCV riscv;
  585. + CPUContextRISCV64 riscv64;
  586. #else
  587. #error Port.
  588. #endif // ARCH_CPU_X86_FAMILY
  589. diff --git a/third_party/crashpad/crashpad/util/linux/ptracer.cc b/third_party/crashpad/crashpad/util/linux/ptracer.cc
  590. index 557e0d3635752..d74dbc1ba1688 100644
  591. --- a/third_party/crashpad/crashpad/util/linux/ptracer.cc
  592. +++ b/third_party/crashpad/crashpad/util/linux/ptracer.cc
  593. @@ -397,6 +397,50 @@ bool GetThreadArea64(pid_t tid,
  594. *address = FromPointerCast<LinuxVMAddress>(result);
  595. return true;
  596. }
  597. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  598. +
  599. +template <typename Destination>
  600. +bool GetRegisterSet(pid_t tid, int set, Destination* dest, bool can_log) {
  601. + iovec iov;
  602. + iov.iov_base = dest;
  603. + iov.iov_len = sizeof(*dest);
  604. + if (ptrace(PTRACE_GETREGSET, tid, reinterpret_cast<void*>(set), &iov) != 0) {
  605. + PLOG_IF(ERROR, can_log) << "ptrace";
  606. + return false;
  607. + }
  608. + if (iov.iov_len != sizeof(*dest)) {
  609. + LOG_IF(ERROR, can_log) << "Unexpected registers size";
  610. + return false;
  611. + }
  612. + return true;
  613. +}
  614. +
  615. +bool GetFloatingPointRegisters32(pid_t tid,
  616. + FloatContext* context,
  617. + bool can_log) {
  618. + return false;
  619. +}
  620. +
  621. +bool GetFloatingPointRegisters64(pid_t tid,
  622. + FloatContext* context,
  623. + bool can_log) {
  624. + return GetRegisterSet(tid, NT_PRFPREG, &context->f64.f, can_log);
  625. +}
  626. +
  627. +bool GetThreadArea32(pid_t tid,
  628. + const ThreadContext& context,
  629. + LinuxVMAddress* address,
  630. + bool can_log) {
  631. + return false;
  632. +}
  633. +
  634. +bool GetThreadArea64(pid_t tid,
  635. + const ThreadContext& context,
  636. + LinuxVMAddress* address,
  637. + bool can_log) {
  638. + *address = context.t64.tp;
  639. + return true;
  640. +}
  641. #else
  642. #error Port.
  643. diff --git a/third_party/crashpad/crashpad/util/linux/thread_info.h b/third_party/crashpad/crashpad/util/linux/thread_info.h
  644. index d3f3b2c6953fa..6a649dc3d7ac7 100644
  645. --- a/third_party/crashpad/crashpad/util/linux/thread_info.h
  646. +++ b/third_party/crashpad/crashpad/util/linux/thread_info.h
  647. @@ -79,6 +79,40 @@ union ThreadContext {
  648. uint32_t cp0_status;
  649. uint32_t cp0_cause;
  650. uint32_t padding1_;
  651. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  652. + // Reflects user_regs_struct in asm/ptrace.h.
  653. + uint32_t pc;
  654. + uint32_t ra;
  655. + uint32_t sp;
  656. + uint32_t gp;
  657. + uint32_t tp;
  658. + uint32_t t0;
  659. + uint32_t t1;
  660. + uint32_t t2;
  661. + uint32_t s0;
  662. + uint32_t s1;
  663. + uint32_t a0;
  664. + uint32_t a1;
  665. + uint32_t a2;
  666. + uint32_t a3;
  667. + uint32_t a4;
  668. + uint32_t a5;
  669. + uint32_t a6;
  670. + uint32_t a7;
  671. + uint32_t s2;
  672. + uint32_t s3;
  673. + uint32_t s4;
  674. + uint32_t s5;
  675. + uint32_t s6;
  676. + uint32_t s7;
  677. + uint32_t s8;
  678. + uint32_t s9;
  679. + uint32_t s10;
  680. + uint32_t s11;
  681. + uint32_t t3;
  682. + uint32_t t4;
  683. + uint32_t t5;
  684. + uint32_t t6;
  685. #else
  686. #error Port.
  687. #endif // ARCH_CPU_X86_FAMILY
  688. @@ -132,6 +166,40 @@ union ThreadContext {
  689. uint64_t cp0_badvaddr;
  690. uint64_t cp0_status;
  691. uint64_t cp0_cause;
  692. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  693. + // Reflects user_regs_struct in asm/ptrace.h.
  694. + uint64_t pc;
  695. + uint64_t ra;
  696. + uint64_t sp;
  697. + uint64_t gp;
  698. + uint64_t tp;
  699. + uint64_t t0;
  700. + uint64_t t1;
  701. + uint64_t t2;
  702. + uint64_t s0;
  703. + uint64_t s1;
  704. + uint64_t a0;
  705. + uint64_t a1;
  706. + uint64_t a2;
  707. + uint64_t a3;
  708. + uint64_t a4;
  709. + uint64_t a5;
  710. + uint64_t a6;
  711. + uint64_t a7;
  712. + uint64_t s2;
  713. + uint64_t s3;
  714. + uint64_t s4;
  715. + uint64_t s5;
  716. + uint64_t s6;
  717. + uint64_t s7;
  718. + uint64_t s8;
  719. + uint64_t s9;
  720. + uint64_t s10;
  721. + uint64_t s11;
  722. + uint64_t t3;
  723. + uint64_t t4;
  724. + uint64_t t5;
  725. + uint64_t t6;
  726. #else
  727. #error Port.
  728. #endif // ARCH_CPU_X86_FAMILY
  729. @@ -143,11 +211,12 @@ union ThreadContext {
  730. using NativeThreadContext = user_regs;
  731. #elif defined(ARCH_CPU_MIPS_FAMILY)
  732. // No appropriate NativeThreadsContext type available for MIPS
  733. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  734. #else
  735. #error Port.
  736. #endif // ARCH_CPU_X86_FAMILY || ARCH_CPU_ARM64
  737. -#if !defined(ARCH_CPU_MIPS_FAMILY)
  738. +#if !defined(ARCH_CPU_MIPS_FAMILY) && !defined(ARCH_CPU_RISCV_FAMILY)
  739. #if defined(ARCH_CPU_32_BITS)
  740. static_assert(sizeof(t32_t) == sizeof(NativeThreadContext), "Size mismatch");
  741. #else // ARCH_CPU_64_BITS
  742. @@ -218,6 +287,9 @@ union FloatContext {
  743. } fpregs[32];
  744. uint32_t fpcsr;
  745. uint32_t fpu_id;
  746. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  747. + uint64_t f[32];
  748. + uint32_t fcsr;
  749. #else
  750. #error Port.
  751. #endif // ARCH_CPU_X86_FAMILY
  752. @@ -252,6 +324,9 @@ union FloatContext {
  753. double fpregs[32];
  754. uint32_t fpcsr;
  755. uint32_t fpu_id;
  756. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  757. + uint64_t f[32];
  758. + uint32_t fcsr;
  759. #else
  760. #error Port.
  761. #endif // ARCH_CPU_X86_FAMILY
  762. @@ -281,6 +356,7 @@ union FloatContext {
  763. static_assert(sizeof(f64) == sizeof(user_fpsimd_struct), "Size mismatch");
  764. #elif defined(ARCH_CPU_MIPS_FAMILY)
  765. // No appropriate floating point context native type for available MIPS.
  766. +#elif defined(ARCH_CPU_RISCV_FAMILY)
  767. #else
  768. #error Port.
  769. #endif // ARCH_CPU_X86
  770. diff --git a/third_party/crashpad/crashpad/util/net/http_transport_libcurl.cc b/third_party/crashpad/crashpad/util/net/http_transport_libcurl.cc
  771. index 7e3f41186f462..9568dc2e24f97 100644
  772. --- a/third_party/crashpad/crashpad/util/net/http_transport_libcurl.cc
  773. +++ b/third_party/crashpad/crashpad/util/net/http_transport_libcurl.cc
  774. @@ -237,6 +237,8 @@ std::string UserAgent() {
  775. #elif defined(ARCH_CPU_BIG_ENDIAN)
  776. static constexpr char arch[] = "aarch64_be";
  777. #endif
  778. +#elif defined(ARCH_CPU_RISCV64)
  779. + static constexpr char arch[] = "riscv64";
  780. #else
  781. #error Port
  782. #endif
  783. --
  784. 2.25.1