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 #include "pw_cpu_exception_risc_v/cpu_state.h" 15 #include "pw_cpu_exception_risc_v_protos/cpu_state.pwpb.h" 16 #include "pw_protobuf/encoder.h" 17 18 namespace pw::cpu_exception::risc_v { 19 DumpCpuStateProto(protobuf::StreamEncoder & dest,const pw_cpu_exception_State & cpu_state)20Status DumpCpuStateProto(protobuf::StreamEncoder& dest, 21 const pw_cpu_exception_State& cpu_state) { 22 // Note that the encoder's status is checked at the end and ignored at the 23 // Write*() calls to reduce the number of branches. 24 risc_v::RiscvCpuState::StreamEncoder& state_encoder = 25 *static_cast<risc_v::RiscvCpuState::StreamEncoder*>(&dest); 26 27 const ExtraRegisters& extended = cpu_state.extended; 28 const ExceptionRegisters& base = cpu_state.base; 29 30 state_encoder.WriteMepc(extended.mepc).IgnoreError(); 31 state_encoder.WriteMcause(extended.mcause).IgnoreError(); 32 state_encoder.WriteMtval(extended.mtval).IgnoreError(); 33 state_encoder.WriteMstatus(extended.mstatus).IgnoreError(); 34 35 // General purpose registers. 36 state_encoder.WriteRa(base.ra).IgnoreError(); 37 state_encoder.WriteSp(base.sp).IgnoreError(); 38 state_encoder.WriteT0(base.t0).IgnoreError(); 39 state_encoder.WriteT1(base.t1).IgnoreError(); 40 state_encoder.WriteT2(base.t2).IgnoreError(); 41 state_encoder.WriteFp(base.fp).IgnoreError(); 42 state_encoder.WriteS1(base.s1).IgnoreError(); 43 state_encoder.WriteA0(base.a0).IgnoreError(); 44 state_encoder.WriteA1(base.a1).IgnoreError(); 45 state_encoder.WriteA2(base.a2).IgnoreError(); 46 state_encoder.WriteA3(base.a3).IgnoreError(); 47 state_encoder.WriteA4(base.a4).IgnoreError(); 48 state_encoder.WriteA5(base.a5).IgnoreError(); 49 state_encoder.WriteA6(base.a6).IgnoreError(); 50 state_encoder.WriteA7(base.a7).IgnoreError(); 51 state_encoder.WriteS2(base.s2).IgnoreError(); 52 state_encoder.WriteS3(base.s3).IgnoreError(); 53 state_encoder.WriteS4(base.s4).IgnoreError(); 54 state_encoder.WriteS5(base.s5).IgnoreError(); 55 state_encoder.WriteS6(base.s6).IgnoreError(); 56 state_encoder.WriteS7(base.s7).IgnoreError(); 57 state_encoder.WriteS8(base.s8).IgnoreError(); 58 state_encoder.WriteS9(base.s9).IgnoreError(); 59 state_encoder.WriteS10(base.s10).IgnoreError(); 60 state_encoder.WriteS11(base.s11).IgnoreError(); 61 state_encoder.WriteT3(base.t3).IgnoreError(); 62 state_encoder.WriteT4(base.t4).IgnoreError(); 63 state_encoder.WriteT5(base.t5).IgnoreError(); 64 state_encoder.WriteT6(base.t6).IgnoreError(); 65 66 // If the encode buffer was exhausted in an earlier write, it will be 67 // reflected here. 68 if (const Status status = state_encoder.status(); !status.ok()) { 69 return status.IsResourceExhausted() ? Status::ResourceExhausted() 70 : Status::Unknown(); 71 } 72 73 return OkStatus(); 74 } 75 76 } // namespace pw::cpu_exception::risc_v 77