1#![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
19pub const XLEN: usize = if cfg!(target_arch = "riscv64") {
22 64
23} else {
24 32
25};
26
27extern "C" {
28 static _estack: usize;
31 static _sstack: usize;
32
33 static mut _szero: usize;
35 static mut _ezero: usize;
36
37 static mut _etext: usize;
39
40 static mut _srelocate: usize;
42 static mut _erelocate: usize;
43
44 #[link_name = "__global_pointer$"]
46 static __global_pointer: usize;
47}
48
49#[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#[cfg(not(any(doc, all(target_arch = "riscv32", target_os = "none"))))]
146pub extern "C" fn _start() {
147 unimplemented!()
148}
149
150pub enum PermissionMode {
152 User = 0x0,
153 Supervisor = 0x1,
154 Reserved = 0x2,
155 Machine = 0x3,
156}
157
158pub unsafe fn configure_trap_handler() {
166 csr::CSR.mscratch.set(0);
168
169 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#[cfg(not(any(doc, all(target_arch = "riscv32", target_os = "none"))))]
180pub extern "C" fn _start_trap() {
181 unimplemented!()
182}
183
184#[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#[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#[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
460pub 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
545pub 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}