riscv/
lib.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Shared support for RISC-V architectures.
6
7#![no_std]
8
9use core::fmt::Write;
10
11use kernel::utilities::registers::interfaces::{Readable, Writeable};
12
13pub mod csr;
14pub mod pmp;
15pub mod support;
16pub mod syscall;
17pub mod thread_id;
18
19// Default to 32 bit if no architecture is specified of if this is being
20// compiled for docs or testing on a different architecture.
21pub const XLEN: usize = if cfg!(target_arch = "riscv64") {
22    64
23} else {
24    32
25};
26
27extern "C" {
28    // Where the end of the stack region is (and hence where the stack should
29    // start), and the start of the stack region.
30    static _estack: usize;
31    static _sstack: usize;
32
33    // Boundaries of the .bss section.
34    static mut _szero: usize;
35    static mut _ezero: usize;
36
37    // Where the .data section is stored in flash.
38    static mut _etext: usize;
39
40    // Boundaries of the .data section.
41    static mut _srelocate: usize;
42    static mut _erelocate: usize;
43
44    // The global pointer, value set in the linker script
45    #[link_name = "__global_pointer$"]
46    static __global_pointer: usize;
47}
48
49/// Entry point of all programs (`_start`).
50///
51/// This assembly does three functions:
52///
53/// 1. It initializes the stack pointer, the frame pointer (needed for closures
54///    to work in start_rust) and the global pointer.
55/// 2. It initializes the .bss and .data RAM segments. This must be done before
56///    any Rust code runs. See <https://github.com/tock/tock/issues/2222> for more
57///    information.
58/// 3. Finally it calls `main()`, the main entry point for Tock boards.
59#[cfg(any(doc, all(target_arch = "riscv32", target_os = "none")))]
60#[link_section = ".riscv.start"]
61#[unsafe(naked)]
62pub extern "C" fn _start() {
63    use core::arch::naked_asm;
64    naked_asm!(
65        "
66    // Set the global pointer register using the variable defined in the
67    // linker script. This register is only set once. The global pointer
68    // is a method for sharing state between the linker and the CPU so
69    // that the linker can emit code with offsets that are relative to
70    // the gp register, and the CPU can successfully execute them.
71    //
72    // https://gnu-mcu-eclipse.github.io/arch/riscv/programmer/#the-gp-global-pointer-register
73    // https://groups.google.com/a/groups.riscv.org/forum/#!msg/sw-dev/60IdaZj27dY/5MydPLnHAQAJ
74    // https://www.sifive.com/blog/2017/08/28/all-aboard-part-3-linker-relaxation-in-riscv-toolchain/
75    //
76    // Disable linker relaxation for code that sets up GP so that this doesn't
77    // get turned into `mv gp, gp`.
78    .option push
79    .option norelax
80
81    la gp, {gp}                 // Set the global pointer from linker script.
82
83    // Re-enable linker relaxations.
84    .option pop
85
86    // Initialize the stack pointer register. This comes directly from
87    // the linker script.
88    la sp, {estack}             // Set the initial stack pointer.
89
90    // Set s0 (the frame pointer) to the start of the stack.
91    add  s0, sp, zero           // s0 = sp
92
93    // Initialize mscratch to 0 so that we know that we are currently
94    // in the kernel. This is used for the check in the trap handler.
95    csrw 0x340, zero            // CSR=0x340=mscratch
96
97    // INITIALIZE MEMORY
98
99    // Start by initializing .bss memory. The Tock linker script defines
100    // `_szero` and `_ezero` to mark the .bss segment.
101    la a0, {sbss}               // a0 = first address of .bss
102    la a1, {ebss}               // a1 = first address after .bss
103
104100: // bss_init_loop
105    beq  a0, a1, 101f           // If a0 == a1, we are done.
106    sw   zero, 0(a0)            // *a0 = 0. Write 0 to the memory location in a0.
107    addi a0, a0, 4              // a0 = a0 + 4. Increment pointer to next word.
108    j 100b                      // Continue the loop.
109
110101: // bss_init_done
111
112    // Now initialize .data memory. This involves coping the values right at the
113    // end of the .text section (in flash) into the .data section (in RAM).
114    la a0, {sdata}              // a0 = first address of data section in RAM
115    la a1, {edata}              // a1 = first address after data section in RAM
116    la a2, {etext}              // a2 = address of stored data initial values
117
118200: // data_init_loop
119    beq  a0, a1, 201f           // If we have reached the end of the .data
120                                // section then we are done.
121    lw   a3, 0(a2)              // a3 = *a2. Load value from initial values into a3.
122    sw   a3, 0(a0)              // *a0 = a3. Store initial value into
123                                // next place in .data.
124    addi a0, a0, 4              // a0 = a0 + 4. Increment to next word in memory.
125    addi a2, a2, 4              // a2 = a2 + 4. Increment to next word in flash.
126    j 200b                      // Continue the loop.
127
128201: // data_init_done
129
130    // With that initial setup out of the way, we now branch to the main
131    // code, likely defined in a board's main.rs.
132    j main
133        ",
134        gp = sym __global_pointer,
135        estack = sym _estack,
136        sbss = sym _szero,
137        ebss = sym _ezero,
138        sdata = sym _srelocate,
139        edata = sym _erelocate,
140        etext = sym _etext,
141    );
142}
143
144// Mock implementation for tests on Travis-CI.
145#[cfg(not(any(doc, all(target_arch = "riscv32", target_os = "none"))))]
146pub extern "C" fn _start() {
147    unimplemented!()
148}
149
150/// The various privilege levels in RISC-V.
151pub enum PermissionMode {
152    User = 0x0,
153    Supervisor = 0x1,
154    Reserved = 0x2,
155    Machine = 0x3,
156}
157
158/// Tell the MCU what address the trap handler is located at, and initialize
159/// `mscratch` to zero, indicating kernel execution.
160///
161/// This is a generic implementation. There may be board specific versions as
162/// some platforms have added more bits to the `mtvec` register.
163///
164/// The trap handler is called on exceptions and for interrupts.
165pub unsafe fn configure_trap_handler() {
166    // Indicate to the trap handler that we are executing kernel code.
167    csr::CSR.mscratch.set(0);
168
169    // Set the machine-mode trap handler. By not configuing an S-mode or U-mode
170    // trap handler, this should ensure that all traps are handled by the M-mode
171    // handler.
172    csr::CSR.mtvec.write(
173        csr::mtvec::mtvec::trap_addr.val(_start_trap as usize >> 2)
174            + csr::mtvec::mtvec::mode::CLEAR,
175    );
176}
177
178// Mock implementation for tests on Travis-CI.
179#[cfg(not(any(doc, all(target_arch = "riscv32", target_os = "none"))))]
180pub extern "C" fn _start_trap() {
181    unimplemented!()
182}
183
184/// This is the trap handler function. This code is called on all traps,
185/// including interrupts, exceptions, and system calls from applications.
186///
187/// Tock uses only the single trap handler, and does not use any vectored
188/// interrupts or other exception handling. The trap handler has to
189/// determine why the trap handler was called, and respond
190/// accordingly. Generally, there are two reasons the trap handler gets
191/// called: an interrupt occurred or an application called a syscall.
192///
193/// In the case of an interrupt while the kernel was executing we only need
194/// to save the kernel registers and then run whatever interrupt handling
195/// code we need to. If the trap happens while an application was executing,
196/// we have to save the application state and then resume the `switch_to()`
197/// function to correctly return back to the kernel.
198///
199/// We implement this distinction through a branch on the value of the
200/// `mscratch` CSR. If, at the time the trap was taken, it contains `0`, we
201/// assume that the hart is currently executing kernel code.
202///
203/// If it contains any other value, we interpret it to be a memory address
204/// pointing to a particular data structure:
205///
206/// ```text
207/// mscratch           0               1               2               3
208///  \->|--------------------------------------------------------------|
209///     | scratch word, overwritten with s1 register contents          |
210///     |--------------------------------------------------------------|
211///     | trap handler address, continue trap handler execution here   |
212///     |--------------------------------------------------------------|
213/// ```
214///
215/// Thus, process implementations can define their own strategy for how
216/// traps should be handled when they occur during process execution. This
217/// global trap handler behavior is well defined. It will:
218///
219/// 1. atomically swap s0 and the mscratch CSR,
220///
221/// 2. execute the default kernel trap handler if s0 now contains `0` (meaning
222///    that the mscratch CSR contained `0` before entering this trap handler),
223///
224/// 3. otherwise, save s1 to `0*4(s0)`, and finally
225///
226/// 4. load the address at `1*4(s0)` into s1, and jump to it.
227///
228/// No registers other than s0, s1 and the mscratch CSR are to be clobbered
229/// before continuing execution at the address loaded into the mscratch CSR
230/// or the _start_kernel_trap kernel trap handler.  Execution with these
231/// second-stage trap handlers must continue in the same trap handler
232/// context as originally invoked by the trap (e.g., the global trap handler
233/// will not execute an mret instruction). It will not modify CSRs that
234/// contain information on the trap source or the system state prior to
235/// entering the trap handler.
236///
237/// Before a trap handler executes any Rust code, or code that can
238/// transititively call any Rust code, it must indicate that a trap handler is
239/// currently active by writing an MXLEN-sized (usize), non-zero value to the
240/// address at `_trap_handler_active + (MXLEN / 8) * mhartid`. Before leaving
241/// the trap handler using `mret`, but after any Rust code has run, it must
242/// reset the value at this address back to zero. This is used to accurately
243/// determine the running thread ID, taking trap handlers into account as a
244/// separate thread.
245///
246/// We deliberately clobber callee-saved instead of caller-saved registers,
247/// as this makes it easier to call other functions as part of the trap
248/// handler (for example to to disable interrupts from within Rust
249/// code). This global trap handler saves the previous values of these
250/// clobbered registers ensuring that they can be restored later.  It places
251/// new values into these clobbered registers (such as the previous `s0`
252/// register contents) that are required to be retained for correctly
253/// returning from the trap handler, and as such need to be saved across
254/// C-ABI function calls. Loading them into saved registers avoids the need
255/// to manually save them across such calls.
256///
257/// When a custom trap handler stack is registered in `mscratch`, the custom
258/// handler is responsible for restoring the kernel trap handler (by setting
259/// mscratch=0) before returning to kernel execution from the trap handler
260/// context.
261///
262/// If a board or chip must, for whichever reason, use a different global
263/// trap handler, it should abide to the above contract and emulate its
264/// behavior for all traps and interrupts that are required to be handled by
265/// the respective kernel or other trap handler as registered in mscratch.
266///
267/// For instance, a chip that does not support non-vectored trap handlers
268/// can register a vectored trap handler that routes each trap source to
269/// this global trap handler.
270///
271/// Alternatively, a board can be allowed to ignore certain traps or
272/// interrupts, some or all of the time, provided they are not vital to
273/// Tock's execution. These boards may choose to register an alternative
274/// handler for some or all trap sources. When this alternative handler is
275/// invoked, it may, for instance, choose to ignore a certain trap, access
276/// global state (subject to synchronization), etc. It must still abide to
277/// the contract as stated above.
278#[cfg(any(doc, all(target_arch = "riscv32", target_os = "none")))]
279#[link_section = ".riscv.trap"]
280#[unsafe(naked)]
281pub extern "C" fn _start_trap() {
282    use core::arch::naked_asm;
283    naked_asm!(
284        "
285    // This is the global trap handler. By default, Tock expects this
286    // trap handler to be registered at all times, and that all traps
287    // and interrupts occurring in all modes of execution (M-, S-, and
288    // U-mode) will cause this trap handler to be executed.
289    //
290    // For documentation of its behavior, and how process
291    // implementations can hook their own trap handler code, see the
292    // comment on the `extern C _start_trap` symbol above.
293
294    // Atomically swap s0 and mscratch:
295    csrrw s0, mscratch, s0        // s0 = mscratch; mscratch = s0
296
297    // If mscratch contained 0, invoke the kernel trap handler.
298    beq   s0, x0, 100f      // if s0==x0: goto 100
299
300    // Else, save the current value of s1 to `0*4(s0)`, load `1*4(s0)`
301    // into s1 and jump to it (invoking a custom trap handler).
302    sw    s1, 0*4(s0)       // *s0 = s1
303    lw    s1, 1*4(s0)       // s1 = *(s0+4)
304    jr    s1                // goto s1
305
306  100: // _start_kernel_trap
307
308    // The global trap handler has swapped s0 into mscratch. We can thus
309    // freely clobber s0 without losing any information.
310    //
311    // Since we want to use the stack to save kernel registers, we
312    // first need to make sure that the trap wasn't the result of a
313    // stack overflow, in which case we can't use the current stack
314    // pointer. Use s0 as a scratch register:
315
316    // Load the address of the bottom of the stack (`_sstack`) into our
317    // newly freed-up s0 register.
318    la s0, {sstack}                     // s0 = _sstack
319
320    // Compare the kernel stack pointer to the bottom of the stack. If
321    // the stack pointer is above the bottom of the stack, then continue
322    // handling the fault as normal.
323    bgtu sp, s0, 200f                   // branch if sp > s0
324
325    // If we get here, then we did encounter a stack overflow. We are
326    // going to panic at this point, but for that to work we need a
327    // valid stack to run the panic code. We do this by just starting
328    // over with the kernel stack and placing the stack pointer at the
329    // top of the original stack.
330    la sp, {estack}                     // sp = _estack
331
332200: // _start_kernel_trap_continue
333
334    // Restore s0. We reset mscratch to 0 (kernel trap handler mode)
335    csrrw s0, mscratch, zero    // s0 = mscratch; mscratch = 0
336
337    // Make room for the caller saved registers we need to restore after running
338    // any trap handler code.
339    addi sp, sp, -20*4
340
341    // Save all of the caller saved registers.
342    sw   ra, 0*4(sp)
343    sw   t0, 1*4(sp)
344    sw   t1, 2*4(sp)
345    sw   t2, 3*4(sp)
346    sw   t3, 4*4(sp)
347    sw   t4, 5*4(sp)
348    sw   t5, 6*4(sp)
349    sw   t6, 7*4(sp)
350    sw   a0, 8*4(sp)
351    sw   a1, 9*4(sp)
352    sw   a2, 10*4(sp)
353    sw   a3, 11*4(sp)
354    sw   a4, 12*4(sp)
355    sw   a5, 13*4(sp)
356    sw   a6, 14*4(sp)
357    sw   a7, 15*4(sp)
358
359    // Save one callee-saved register (s0), which we place the address of
360    // the hart-specific 'are we in a trap handler' flag in:
361    sw   s0, 16*4(sp)
362
363    // Determine the address of the hart-specific 'are we in a trap handler'
364    // flag as an offset to the _trap_handler_active symbol. The chip crate
365    // is responsible for defining this symbol, and ensuring it is large
366    // enough to fit `max(mhartid) * MXLEN` bytes.
367    la   s0, _trap_handler_active // s0 = addr(_trap_handler_active)
368    csrr t0, mhartid              // t0 = hartid
369    slli t0, t0, 2                // t0 = t0 * 4
370    add  s0, s0, t0               // s0 = addr(_trap_handler_active[hartid])
371
372    // Indicate that we are in a trap handler on this hart:
373    li   t0, 1
374    sw   t0, 0(s0)
375
376    // Jump to board-specific trap handler code. Likely this was an
377    // interrupt and we want to disable a particular interrupt, but each
378    // board/chip can customize this as needed.
379    jal ra, _start_trap_rust_from_kernel
380
381    // Indicate that we are no longer going to be in a trap handler on this
382    // hart:
383    sw   x0, 0(s0)
384
385    // Restore the caller saved registers from the stack.
386    lw   ra, 0*4(sp)
387    lw   t0, 1*4(sp)
388    lw   t1, 2*4(sp)
389    lw   t2, 3*4(sp)
390    lw   t3, 4*4(sp)
391    lw   t4, 5*4(sp)
392    lw   t5, 6*4(sp)
393    lw   t6, 7*4(sp)
394    lw   a0, 8*4(sp)
395    lw   a1, 9*4(sp)
396    lw   a2, 10*4(sp)
397    lw   a3, 11*4(sp)
398    lw   a4, 12*4(sp)
399    lw   a5, 13*4(sp)
400    lw   a6, 14*4(sp)
401    lw   a7, 15*4(sp)
402
403    // Restore the one callee-saved register (s0), which used to hold the
404    // address of the hart-specific 'are we in a trap handler flag':
405    lw   s0, 16*4(sp)
406
407    // Reset the stack pointer.
408    addi sp, sp, 20*4
409
410    // mret returns from the trap handler. The PC is set to what is in
411    // mepc and execution proceeds from there. Since we did not modify
412    // mepc we will return to where the exception occurred.
413    mret
414        ",
415        estack = sym _estack,
416        sstack = sym _sstack,
417    );
418}
419
420/// RISC-V semihosting needs three exact instructions in uncompressed form.
421///
422/// See <https://github.com/riscv/riscv-semihosting-spec/blob/main/riscv-semihosting-spec.adoc#11-semihosting-trap-instruction-sequence>
423/// for more details on the three instructions.
424///
425/// In order to work with semihosting we include the assembly here
426/// where we are able to disable compressed instruction support. This
427/// follows the example used in the Linux kernel:
428/// <https://elixir.bootlin.com/linux/v5.12.10/source/arch/riscv/include/asm/jump_label.h#L21>
429/// as suggested by the RISC-V developers:
430/// <https://groups.google.com/a/groups.riscv.org/g/isa-dev/c/XKkYacERM04/m/CdpOcqtRAgAJ>
431#[cfg(any(doc, all(target_arch = "riscv32", target_os = "none")))]
432pub unsafe fn semihost_command(command: usize, arg0: usize, arg1: usize) -> usize {
433    use core::arch::asm;
434    let res;
435    asm!(
436        "
437    .balign 16
438    .option push
439    .option norelax
440    .option norvc
441    slli x0, x0, 0x1f
442    ebreak
443    srai x0, x0, 7
444    .option pop
445        ",
446        in("a0") command,
447        in("a1") arg0,
448        in("a2") arg1,
449        lateout("a0") res,
450    );
451    res
452}
453
454// Mock implementation for tests on Travis-CI.
455#[cfg(not(any(doc, all(target_arch = "riscv32", target_os = "none"))))]
456pub unsafe fn semihost_command(_command: usize, _arg0: usize, _arg1: usize) -> usize {
457    unimplemented!()
458}
459
460/// Print a readable string for an mcause reason.
461pub unsafe fn print_mcause(mcval: csr::mcause::Trap, writer: &mut dyn Write) {
462    match mcval {
463        csr::mcause::Trap::Interrupt(interrupt) => match interrupt {
464            csr::mcause::Interrupt::UserSoft => {
465                let _ = writer.write_fmt(format_args!("User software interrupt"));
466            }
467            csr::mcause::Interrupt::SupervisorSoft => {
468                let _ = writer.write_fmt(format_args!("Supervisor software interrupt"));
469            }
470            csr::mcause::Interrupt::MachineSoft => {
471                let _ = writer.write_fmt(format_args!("Machine software interrupt"));
472            }
473            csr::mcause::Interrupt::UserTimer => {
474                let _ = writer.write_fmt(format_args!("User timer interrupt"));
475            }
476            csr::mcause::Interrupt::SupervisorTimer => {
477                let _ = writer.write_fmt(format_args!("Supervisor timer interrupt"));
478            }
479            csr::mcause::Interrupt::MachineTimer => {
480                let _ = writer.write_fmt(format_args!("Machine timer interrupt"));
481            }
482            csr::mcause::Interrupt::UserExternal => {
483                let _ = writer.write_fmt(format_args!("User external interrupt"));
484            }
485            csr::mcause::Interrupt::SupervisorExternal => {
486                let _ = writer.write_fmt(format_args!("Supervisor external interrupt"));
487            }
488            csr::mcause::Interrupt::MachineExternal => {
489                let _ = writer.write_fmt(format_args!("Machine external interrupt"));
490            }
491            csr::mcause::Interrupt::Unknown(_) => {
492                let _ = writer.write_fmt(format_args!("Reserved/Unknown"));
493            }
494        },
495        csr::mcause::Trap::Exception(exception) => match exception {
496            csr::mcause::Exception::InstructionMisaligned => {
497                let _ = writer.write_fmt(format_args!("Instruction access misaligned"));
498            }
499            csr::mcause::Exception::InstructionFault => {
500                let _ = writer.write_fmt(format_args!("Instruction access fault"));
501            }
502            csr::mcause::Exception::IllegalInstruction => {
503                let _ = writer.write_fmt(format_args!("Illegal instruction"));
504            }
505            csr::mcause::Exception::Breakpoint => {
506                let _ = writer.write_fmt(format_args!("Breakpoint"));
507            }
508            csr::mcause::Exception::LoadMisaligned => {
509                let _ = writer.write_fmt(format_args!("Load address misaligned"));
510            }
511            csr::mcause::Exception::LoadFault => {
512                let _ = writer.write_fmt(format_args!("Load access fault"));
513            }
514            csr::mcause::Exception::StoreMisaligned => {
515                let _ = writer.write_fmt(format_args!("Store/AMO address misaligned"));
516            }
517            csr::mcause::Exception::StoreFault => {
518                let _ = writer.write_fmt(format_args!("Store/AMO access fault"));
519            }
520            csr::mcause::Exception::UserEnvCall => {
521                let _ = writer.write_fmt(format_args!("Environment call from U-mode"));
522            }
523            csr::mcause::Exception::SupervisorEnvCall => {
524                let _ = writer.write_fmt(format_args!("Environment call from S-mode"));
525            }
526            csr::mcause::Exception::MachineEnvCall => {
527                let _ = writer.write_fmt(format_args!("Environment call from M-mode"));
528            }
529            csr::mcause::Exception::InstructionPageFault => {
530                let _ = writer.write_fmt(format_args!("Instruction page fault"));
531            }
532            csr::mcause::Exception::LoadPageFault => {
533                let _ = writer.write_fmt(format_args!("Load page fault"));
534            }
535            csr::mcause::Exception::StorePageFault => {
536                let _ = writer.write_fmt(format_args!("Store/AMO page fault"));
537            }
538            csr::mcause::Exception::Unknown => {
539                let _ = writer.write_fmt(format_args!("Reserved"));
540            }
541        },
542    }
543}
544
545/// Prints out RISCV machine state, including basic system registers
546/// (mcause, mstatus, mtvec, mepc, mtval, interrupt status).
547pub unsafe fn print_riscv_state(writer: &mut dyn Write) {
548    let mcval: csr::mcause::Trap = core::convert::From::from(csr::CSR.mcause.extract());
549    let _ = writer.write_fmt(format_args!("\r\n---| RISC-V Machine State |---\r\n"));
550    let _ = writer.write_fmt(format_args!("Last cause (mcause): "));
551    print_mcause(mcval, writer);
552    let interrupt = csr::CSR.mcause.read(csr::mcause::mcause::is_interrupt);
553    let code = csr::CSR.mcause.read(csr::mcause::mcause::reason);
554    let _ = writer.write_fmt(format_args!(
555        " (interrupt={}, exception code={:#010X})",
556        interrupt, code
557    ));
558    let _ = writer.write_fmt(format_args!(
559        "\r\nLast value (mtval):  {:#010X}\
560         \r\n\
561         \r\nSystem register dump:\
562         \r\n mepc:    {:#010X}    mstatus:     {:#010X}\
563         \r\n mcycle:  {:#010X}    minstret:    {:#010X}\
564         \r\n mtvec:   {:#010X}",
565        csr::CSR.mtval.get(),
566        csr::CSR.mepc.get(),
567        csr::CSR.mstatus.get(),
568        csr::CSR.mcycle.get(),
569        csr::CSR.minstret.get(),
570        csr::CSR.mtvec.get()
571    ));
572    let mstatus = csr::CSR.mstatus.extract();
573    let uie = mstatus.is_set(csr::mstatus::mstatus::uie);
574    let sie = mstatus.is_set(csr::mstatus::mstatus::sie);
575    let mie = mstatus.is_set(csr::mstatus::mstatus::mie);
576    let upie = mstatus.is_set(csr::mstatus::mstatus::upie);
577    let spie = mstatus.is_set(csr::mstatus::mstatus::spie);
578    let mpie = mstatus.is_set(csr::mstatus::mstatus::mpie);
579    let spp = mstatus.is_set(csr::mstatus::mstatus::spp);
580    let _ = writer.write_fmt(format_args!(
581        "\r\n mstatus: {:#010X}\
582         \r\n  uie:    {:5}  upie:   {}\
583         \r\n  sie:    {:5}  spie:   {}\
584         \r\n  mie:    {:5}  mpie:   {}\
585         \r\n  spp:    {}",
586        mstatus.get(),
587        uie,
588        upie,
589        sie,
590        spie,
591        mie,
592        mpie,
593        spp
594    ));
595    let e_usoft = csr::CSR.mie.is_set(csr::mie::mie::usoft);
596    let e_ssoft = csr::CSR.mie.is_set(csr::mie::mie::ssoft);
597    let e_msoft = csr::CSR.mie.is_set(csr::mie::mie::msoft);
598    let e_utimer = csr::CSR.mie.is_set(csr::mie::mie::utimer);
599    let e_stimer = csr::CSR.mie.is_set(csr::mie::mie::stimer);
600    let e_mtimer = csr::CSR.mie.is_set(csr::mie::mie::mtimer);
601    let e_uext = csr::CSR.mie.is_set(csr::mie::mie::uext);
602    let e_sext = csr::CSR.mie.is_set(csr::mie::mie::sext);
603    let e_mext = csr::CSR.mie.is_set(csr::mie::mie::mext);
604
605    let p_usoft = csr::CSR.mip.is_set(csr::mip::mip::usoft);
606    let p_ssoft = csr::CSR.mip.is_set(csr::mip::mip::ssoft);
607    let p_msoft = csr::CSR.mip.is_set(csr::mip::mip::msoft);
608    let p_utimer = csr::CSR.mip.is_set(csr::mip::mip::utimer);
609    let p_stimer = csr::CSR.mip.is_set(csr::mip::mip::stimer);
610    let p_mtimer = csr::CSR.mip.is_set(csr::mip::mip::mtimer);
611    let p_uext = csr::CSR.mip.is_set(csr::mip::mip::uext);
612    let p_sext = csr::CSR.mip.is_set(csr::mip::mip::sext);
613    let p_mext = csr::CSR.mip.is_set(csr::mip::mip::mext);
614    let _ = writer.write_fmt(format_args!(
615        "\r\n mie:   {:#010X}   mip:   {:#010X}\
616         \r\n  usoft:  {:6}              {:6}\
617         \r\n  ssoft:  {:6}              {:6}\
618         \r\n  msoft:  {:6}              {:6}\
619         \r\n  utimer: {:6}              {:6}\
620         \r\n  stimer: {:6}              {:6}\
621         \r\n  mtimer: {:6}              {:6}\
622         \r\n  uext:   {:6}              {:6}\
623         \r\n  sext:   {:6}              {:6}\
624         \r\n  mext:   {:6}              {:6}\r\n",
625        csr::CSR.mie.get(),
626        csr::CSR.mip.get(),
627        e_usoft,
628        p_usoft,
629        e_ssoft,
630        p_ssoft,
631        e_msoft,
632        p_msoft,
633        e_utimer,
634        p_utimer,
635        e_stimer,
636        p_stimer,
637        e_mtimer,
638        p_mtimer,
639        e_uext,
640        p_uext,
641        e_sext,
642        p_sext,
643        e_mext,
644        p_mext
645    ));
646}