xref: /aosp_15_r20/external/pigweed/pw_cpu_exception_risc_v/public/pw_cpu_exception_risc_v/cpu_state.h (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 #pragma once
15 
16 #ifdef __cplusplus
17 
18 #include <cstdint>
19 
20 namespace pw::cpu_exception::risc_v {
21 
22 // This is dictated by this module, and shouldn't change often.
23 // Note that the order of entries in the following structs is very important
24 // (as the values are populated in assembly). The registers are typically stored
25 // onto the stack in a specific order by the exception entry
26 struct ExceptionRegisters {
27   uint32_t ra;
28   uint32_t sp;
29   uint32_t t0;
30   uint32_t t1;
31   uint32_t t2;
32   uint32_t fp;
33   uint32_t s1;
34   uint32_t a0;
35   uint32_t a1;
36   uint32_t a2;
37   uint32_t a3;
38   uint32_t a4;
39   uint32_t a5;
40   uint32_t a6;
41   uint32_t a7;
42   uint32_t s2;
43   uint32_t s3;
44   uint32_t s4;
45   uint32_t s5;
46   uint32_t s6;
47   uint32_t s7;
48   uint32_t s8;
49   uint32_t s9;
50   uint32_t s10;
51   uint32_t s11;
52   uint32_t t3;
53   uint32_t t4;
54   uint32_t t5;
55   uint32_t t6;
56 };
57 static_assert(sizeof(ExceptionRegisters) == (sizeof(uint32_t) * 29),
58               "There's unexpected padding.");
59 
60 // This is dictated by this module, and shouldn't change often.
61 //
62 // NOTE: Memory mapped registers are NOT restored upon fault return!
63 struct ExtraRegisters {
64   uint32_t mepc;
65   uint32_t mcause;
66   uint32_t mstatus;
67   uint32_t mtval;
68 };
69 static_assert(sizeof(ExtraRegisters) == (sizeof(uint32_t) * 4),
70               "There's unexpected padding.");
71 
72 }  // namespace pw::cpu_exception::risc_v
73 
74 // This is dictated by this module, and shouldn't change often.
75 struct pw_cpu_exception_State {
76   pw::cpu_exception::risc_v::ExceptionRegisters base;
77   pw::cpu_exception::risc_v::ExtraRegisters extended;
78 };
79 
80 #else  // !__cplusplus
81 
82 typedef struct pw_cpu_exception_State pw_cpu_exception_State;
83 
84 #endif  // __cplusplus
85