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