Skip to main content

regio/riscv64/
encoding.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#![allow(non_camel_case_types)]
6
7//! This module stamps out [`CsrEncoding`] instances corresponding to known
8//! riscv64 CSRs, named by their official mnemonic (e.g., `sstatus`).
9
10use super::CsrEncoding;
11use crate::{Ro, RwSafe, RwUnsafe};
12
13// This information is primarily found in the "Control and Status Registers (CSRs)"
14// chapter of Volume II: RISC-V Privileged Architectures and Volume I: Unprivileged
15// Architecture in the RISC-V Instruction Set Manual.
16//
17// The usage of RwSafe vs. RwUnsafe depends on the nature of the particular
18// CSR.
19macro_rules! for_each_encoding {
20    ($macro:path) => {
21        //
22        // Unprivileged Floating-Point CSRs
23        //
24
25        $macro!(fflags, 0x001, RwSafe);
26        $macro!(frm, 0x002, RwSafe);
27        $macro!(fcsr, 0x003, RwSafe);
28
29        //
30        // Unprivileged Vector CSRs
31        //
32
33        $macro!(vstart, 0x008, RwSafe);
34        $macro!(vxsat, 0x009, RwSafe);
35        $macro!(vxrm, 0x00a, RwSafe);
36        $macro!(vcsr, 0x00f, RwSafe);
37        $macro!(vl, 0xc20, Ro);
38        $macro!(vtype, 0xc21, Ro);
39        $macro!(vlenb, 0xc22, Ro);
40
41        //
42        // Unprivileged Zicfiss extension CSR
43        //
44
45        // Can change active shadow stack pointer.
46        $macro!(ssp, 0x011, RwUnsafe);
47
48        //
49        // Unprivileged Entropy Source Extension CSR
50        //
51
52        $macro!(seed, 0x015, RwSafe);
53
54        //
55        // Unprivileged Zcmt Extension CSR
56        //
57
58        // Can change jump table base address.
59        $macro!(jvt, 0x017, RwUnsafe);
60
61        //
62        // Unprivileged Counter/Timers
63        //
64
65        $macro!(cycle, 0xc00, Ro);
66        $macro!(time, 0xc01, Ro);
67        $macro!(instret, 0xc02, Ro);
68        $macro!(hpmcounter3, 0xc03, Ro);
69        $macro!(hpmcounter4, 0xc04, Ro);
70        $macro!(hpmcounter31, 0xc1f, Ro);
71        $macro!(cycleh, 0xc80, Ro);
72        $macro!(timeh, 0xc81, Ro);
73        $macro!(instreth, 0xc82, Ro);
74        $macro!(hpmcounter3h, 0xc83, Ro);
75        $macro!(hpmcounter4h, 0xc84, Ro);
76        $macro!(hpmcounter31h, 0xc9f, Ro);
77
78        //
79        // Supervisor Trap Setup
80        //
81
82        $macro!(sstatus, 0x100, RwSafe);
83        $macro!(sie, 0x104, RwSafe);
84        $macro!(stvec, 0x105, RwSafe);
85        $macro!(scounteren, 0x106, RwSafe);
86
87        //
88        // Supervisor Configuration
89        //
90
91        $macro!(senvcfg, 0x10a, RwSafe);
92
93        //
94        // Supervisor Counter Setup
95        //
96
97        $macro!(scountinhibit, 0x120, RwSafe);
98
99        //
100        // Supervisor Trap Handling
101        //
102
103        $macro!(sscratch, 0x140, RwSafe);
104        // Can change the return address.
105        $macro!(sepc, 0x141, RwUnsafe);
106
107        $macro!(scause, 0x142, RwSafe);
108        $macro!(stval, 0x143, RwSafe);
109        $macro!(sip, 0x144, RwSafe);
110        $macro!(scountovf, 0xda0, Ro);
111
112        //
113        // Supervisor Indirect
114        //
115
116        $macro!(siselect, 0x150, RwSafe);
117        $macro!(sireg, 0x151, RwSafe);
118        $macro!(sireg2, 0x152, RwSafe);
119        $macro!(sireg3, 0x153, RwSafe);
120        $macro!(sireg4, 0x155, RwSafe);
121        $macro!(sireg5, 0x156, RwSafe);
122        $macro!(sireg6, 0x157, RwSafe);
123
124        //
125        // Supervisor Protection and Translation
126        //
127
128        // Can change memory mappings.
129        $macro!(satp, 0x180, RwUnsafe);
130
131        //
132        // Supervisor Timer Compare
133        //
134
135        $macro!(stimecmp, 0x14d, RwSafe);
136        $macro!(stimecmph, 0x15d, RwSafe);
137
138        //
139        // Debug/Trace Registers
140        //
141
142        $macro!(scontext, 0x5a8, RwSafe);
143
144        //
145        // Supervisor Resource Management Configuration
146        //
147
148        $macro!(srmcfg, 0x181, RwSafe);
149
150        //
151        // Supervisor State Enable Registers
152        //
153
154        $macro!(sstateen0, 0x10c, RwSafe);
155        $macro!(sstateen1, 0x10d, RwSafe);
156        $macro!(sstateen2, 0x10e, RwSafe);
157        $macro!(sstateen3, 0x10f, RwSafe);
158
159        //
160        // Supervisor Control Transfer Records Configuration
161        //
162
163        $macro!(sctrctl, 0x14e, RwSafe);
164        $macro!(sctrstatus, 0x14f, RwSafe);
165        $macro!(sctrdepth, 0x15f, RwSafe);
166
167        //
168        // Hypervisor Trap Setup
169        //
170
171        $macro!(hstatus, 0x600, RwSafe);
172        $macro!(hedeleg, 0x602, RwSafe);
173        $macro!(hideleg, 0x603, RwSafe);
174        $macro!(hie, 0x604, RwSafe);
175        $macro!(hcounteren, 0x606, RwSafe);
176        $macro!(hgeie, 0x607, RwSafe);
177        $macro!(hedelegh, 0x612, RwSafe);
178
179        //
180        // Hypervisor Trap Handling
181        //
182
183        $macro!(htval, 0x643, RwSafe);
184        $macro!(hip, 0x644, RwSafe);
185        $macro!(hvip, 0x645, RwSafe);
186        $macro!(htinst, 0x64a, RwSafe);
187        $macro!(hgeip, 0xe12, Ro);
188
189        //
190        // Hypervisor Configuration
191        //
192
193        $macro!(henvcfg, 0x60a, RwSafe);
194        $macro!(henvcfgh, 0x61a, RwSafe);
195
196        //
197        // Hypervisor Protection and Translation
198        //
199
200        // Safe-writable, since the writer's execution context is not subject to
201        // the related guest memory mappings.
202        $macro!(hgatp, 0x680, RwSafe);
203
204        //
205        // Debug/Trace Registers
206        //
207
208        $macro!(hcontext, 0x6a8, RwSafe);
209
210        //
211        // Hypervisor Counter/Timer Virtualization Registers
212        //
213
214        $macro!(htimedelta, 0x605, RwSafe);
215        $macro!(htimedeltah, 0x615, RwSafe);
216
217        //
218        // Hypervisor State Enable Registers
219        //
220
221        $macro!(hstateen0, 0x60c, RwSafe);
222        $macro!(hstateen1, 0x60d, RwSafe);
223        $macro!(hstateen2, 0x60e, RwSafe);
224        $macro!(hstateen3, 0x60f, RwSafe);
225        $macro!(hstateen0h, 0x61c, RwSafe);
226        $macro!(hstateen1h, 0x61d, RwSafe);
227        $macro!(hstateen2h, 0x61e, RwSafe);
228        $macro!(hstateen3h, 0x61f, RwSafe);
229
230        //
231        // Virtual Supervisor Registers
232        //
233
234        $macro!(vsstatus, 0x200, RwSafe);
235        $macro!(vsie, 0x204, RwSafe);
236        $macro!(vstvec, 0x205, RwSafe);
237        $macro!(vsscratch, 0x240, RwSafe);
238
239        // Can change the return address.
240        $macro!(vsepc, 0x241, RwUnsafe);
241        $macro!(vscause, 0x242, RwSafe);
242        $macro!(vstval, 0x243, RwSafe);
243        $macro!(vsip, 0x244, RwSafe);
244
245        // Can change memory mappings.
246        $macro!(vsatp, 0x280, RwUnsafe);
247
248        //
249        // Virtual Supervisor Indirect
250        //
251
252        $macro!(vsiselect, 0x250, RwSafe);
253        $macro!(vsireg, 0x251, RwSafe);
254        $macro!(vsireg2, 0x252, RwSafe);
255        $macro!(vsireg3, 0x253, RwSafe);
256        $macro!(vsireg4, 0x255, RwSafe);
257        $macro!(vsireg5, 0x256, RwSafe);
258        $macro!(vsireg6, 0x257, RwSafe);
259
260        //
261        // Virtual Supervisor Timer Compare
262        //
263
264        $macro!(vstimecmp, 0x24d, RwSafe);
265        $macro!(vstimecmph, 0x25d, RwSafe);
266
267        //
268        // Virtual Supervisor Control Transfer Records Configuration
269        //
270
271        $macro!(vsctrctl, 0x24e, RwSafe);
272
273        //
274        // Machine Information Registers
275        //
276
277        $macro!(mvendorid, 0xf11, Ro);
278        $macro!(marchid, 0xf12, Ro);
279        $macro!(mimpid, 0xf13, Ro);
280        $macro!(mhartid, 0xf14, Ro);
281        $macro!(mconfigptr, 0xf15, Ro);
282
283        //
284        // Machine Trap Setup
285        //
286
287        $macro!(mstatus, 0x300, RwSafe);
288        $macro!(misa, 0x301, RwSafe);
289        $macro!(medeleg, 0x302, RwSafe);
290        $macro!(mideleg, 0x303, RwSafe);
291        $macro!(mie, 0x304, RwSafe);
292        $macro!(mtvec, 0x305, RwSafe);
293        $macro!(mcounteren, 0x306, RwSafe);
294        $macro!(mstatush, 0x310, RwSafe);
295        $macro!(medelegh, 0x312, RwSafe);
296
297        //
298        // Machine Trap Handling
299        //
300
301        $macro!(mscratch, 0x340, RwSafe);
302
303        // Can change the return address.
304        $macro!(mepc, 0x341, RwUnsafe);
305        $macro!(mcause, 0x342, RwSafe);
306        $macro!(mtval, 0x343, RwSafe);
307        $macro!(mip, 0x344, RwSafe);
308        $macro!(mtinst, 0x34a, RwSafe);
309        $macro!(mtval2, 0x34b, RwSafe);
310
311        //
312        // Machine Indirect
313        //
314
315        $macro!(miselect, 0x350, RwSafe);
316        $macro!(mireg, 0x351, RwSafe);
317        $macro!(mireg2, 0x352, RwSafe);
318        $macro!(mireg3, 0x353, RwSafe);
319        $macro!(mireg4, 0x355, RwSafe);
320        $macro!(mireg5, 0x356, RwSafe);
321        $macro!(mireg6, 0x357, RwSafe);
322
323        //
324        // Machine Configuration
325        //
326
327        $macro!(menvcfg, 0x30a, RwSafe);
328        $macro!(menvcfgh, 0x31a, RwSafe);
329        $macro!(mseccfg, 0x747, RwSafe);
330        $macro!(mseccfgh, 0x757, RwSafe);
331
332        //
333        // Machine Memory Protection
334        //
335
336        $macro!(pmpcfg0, 0x3a0, RwSafe);
337        $macro!(pmpcfg1, 0x3a1, RwSafe);
338        $macro!(pmpcfg2, 0x3a2, RwSafe);
339        $macro!(pmpcfg3, 0x3a3, RwSafe);
340        $macro!(pmpcfg14, 0x3ae, RwSafe);
341        $macro!(pmpcfg15, 0x3af, RwSafe);
342        $macro!(pmpaddr0, 0x3b0, RwSafe);
343        $macro!(pmpaddr1, 0x3b1, RwSafe);
344        $macro!(pmpaddr63, 0x3ef, RwSafe);
345
346        //
347        // Machine State Enable Registers
348        //
349
350        $macro!(mstateen0, 0x30c, RwSafe);
351        $macro!(mstateen1, 0x30d, RwSafe);
352        $macro!(mstateen2, 0x30e, RwSafe);
353        $macro!(mstateen3, 0x30f, RwSafe);
354        $macro!(mstateen0h, 0x31c, RwSafe);
355        $macro!(mstateen1h, 0x31d, RwSafe);
356        $macro!(mstateen2h, 0x31e, RwSafe);
357        $macro!(mstateen3h, 0x31f, RwSafe);
358
359        //
360        // Machine Non-Maskable Interrupt Handling
361        //
362
363        $macro!(mnscratch, 0x740, RwSafe);
364
365        // Can change the return address.
366        $macro!(mnepc, 0x741, RwUnsafe);
367
368        $macro!(mncause, 0x742, RwSafe);
369        $macro!(mnstatus, 0x744, RwSafe);
370
371        //
372        // Machine Counter/Timers
373        //
374
375        $macro!(mcycle, 0xb00, RwSafe);
376        $macro!(minstret, 0xb02, RwSafe);
377        $macro!(mhpmcounter3, 0xb03, RwSafe);
378        $macro!(mhpmcounter4, 0xb04, RwSafe);
379        $macro!(mhpmcounter31, 0xb1f, RwSafe);
380        $macro!(mcycleh, 0xb80, RwSafe);
381        $macro!(minstreth, 0xb82, RwSafe);
382        $macro!(mhpmcounter3h, 0xb83, RwSafe);
383        $macro!(mhpmcounter4h, 0xb84, RwSafe);
384        $macro!(mhpmcounter31h, 0xb9f, RwSafe);
385
386        //
387        // Machine Counter Setup
388        //
389
390        $macro!(mcountinhibit, 0x320, RwSafe);
391        $macro!(mcyclecfg, 0x321, RwSafe);
392        $macro!(minstretcfg, 0x322, RwSafe);
393        $macro!(mhpmevent3, 0x323, RwSafe);
394        $macro!(mhpmevent4, 0x324, RwSafe);
395        $macro!(mhpmevent31, 0x33f, RwSafe);
396        $macro!(mcyclecfgh, 0x721, RwSafe);
397        $macro!(minstretcfgh, 0x722, RwSafe);
398        $macro!(mhpmevent3h, 0x723, RwSafe);
399        $macro!(mhpmevent4h, 0x724, RwSafe);
400        $macro!(mhpmevent31h, 0x73f, RwSafe);
401
402        //
403        // Machine Control Transfer Records Configuration
404        //
405
406        $macro!(mctrctl, 0x34e, RwSafe);
407
408        //
409        // Debug/Trace Registers (shared with Debug Mode)
410        //
411
412        $macro!(tselect, 0x7a0, RwSafe);
413        $macro!(tdata1, 0x7a1, RwSafe);
414        $macro!(tdata2, 0x7a2, RwSafe);
415        $macro!(tdata3, 0x7a3, RwSafe);
416        $macro!(mcontext, 0x7a8, RwSafe);
417
418        //
419        // Debug Mode Registers
420        //
421
422        $macro!(dcsr, 0x7b0, RwSafe);
423
424        // Can change the return address.
425        $macro!(dpc, 0x7b1, RwUnsafe);
426
427        $macro!(dscratch0, 0x7b2, RwSafe);
428        $macro!(dscratch1, 0x7b3, RwSafe);
429    };
430}
431
432macro_rules! define_encoding {
433    ($name:ident, $encoding:literal, $access:ty) => {
434        pub type $name = CsrEncoding<$encoding, $access>;
435    };
436}
437
438for_each_encoding!(define_encoding);