xref: /aosp_15_r20/external/pigweed/pw_cpu_exception_risc_v/support.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_cpu_exception/support.h"
16 
17 #include <cinttypes>
18 #include <cstdint>
19 
20 #include "pw_cpu_exception_risc_v/cpu_state.h"
21 #include "pw_cpu_exception_risc_v_private/config.h"
22 #include "pw_log/log.h"
23 
24 namespace pw::cpu_exception {
25 
26 // Using this function adds approximately 100 bytes to binary size.
LogCpuState(const pw_cpu_exception_State & cpu_state)27 void LogCpuState(const pw_cpu_exception_State& cpu_state) {
28   const risc_v::ExceptionRegisters& base = cpu_state.base;
29   const risc_v::ExtraRegisters& extended = cpu_state.extended;
30   PW_LOG_INFO("All captured CPU registers:");
31 
32 #define _PW_LOG_REGISTER(state_section, name) \
33   PW_LOG_INFO("  %-10s 0x%08" PRIx32, #name, state_section.name)
34 
35   _PW_LOG_REGISTER(extended, mepc);
36   _PW_LOG_REGISTER(extended, mcause);
37   _PW_LOG_REGISTER(extended, mtval);
38   _PW_LOG_REGISTER(extended, mstatus);
39 
40   // General purpose registers.
41   _PW_LOG_REGISTER(base, ra);
42   _PW_LOG_REGISTER(base, sp);
43   _PW_LOG_REGISTER(base, t0);
44   _PW_LOG_REGISTER(base, t1);
45   _PW_LOG_REGISTER(base, t2);
46   _PW_LOG_REGISTER(base, fp);
47   _PW_LOG_REGISTER(base, s1);
48   _PW_LOG_REGISTER(base, a0);
49   _PW_LOG_REGISTER(base, a1);
50   _PW_LOG_REGISTER(base, a2);
51   _PW_LOG_REGISTER(base, a3);
52   _PW_LOG_REGISTER(base, a4);
53   _PW_LOG_REGISTER(base, a5);
54   _PW_LOG_REGISTER(base, a6);
55   _PW_LOG_REGISTER(base, a7);
56   _PW_LOG_REGISTER(base, s2);
57   _PW_LOG_REGISTER(base, s3);
58   _PW_LOG_REGISTER(base, s4);
59   _PW_LOG_REGISTER(base, s5);
60   _PW_LOG_REGISTER(base, s6);
61   _PW_LOG_REGISTER(base, s7);
62   _PW_LOG_REGISTER(base, s8);
63   _PW_LOG_REGISTER(base, s9);
64   _PW_LOG_REGISTER(base, s10);
65   _PW_LOG_REGISTER(base, s11);
66   _PW_LOG_REGISTER(base, t3);
67   _PW_LOG_REGISTER(base, t4);
68   _PW_LOG_REGISTER(base, t5);
69   _PW_LOG_REGISTER(base, t6);
70 
71 #undef _PW_LOG_REGISTER
72 }
73 
74 }  // namespace pw::cpu_exception
75