main.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #![no_std]
  2. #![no_main]
  3. #![feature(alloc_error_handler)]
  4. #![feature(global_asm)]
  5. use core::alloc::Layout;
  6. use core::panic::PanicInfo;
  7. use k210_hal::{clock::Clocks, fpioa, pac, prelude::*};
  8. use linked_list_allocator::LockedHeap;
  9. use rustsbi::{enter_privileged, println};
  10. use riscv::register::{
  11. mepc, mhartid,
  12. mstatus::{self, MPP},
  13. };
  14. #[global_allocator]
  15. static ALLOCATOR: LockedHeap = LockedHeap::empty();
  16. #[panic_handler]
  17. fn panic(_info: &PanicInfo) -> ! {
  18. loop {}
  19. }
  20. #[alloc_error_handler]
  21. fn oom(_layout: Layout) -> ! {
  22. loop {}
  23. }
  24. #[export_name = "start"]
  25. fn main() -> ! {
  26. if mhartid::read() == 0 {
  27. extern "C" {
  28. fn _sheap();
  29. fn _heap_size();
  30. }
  31. let sheap = &mut _sheap as *mut _ as usize;
  32. let heap_size = &_heap_size as *const _ as usize;
  33. unsafe {
  34. ALLOCATOR.lock().init(sheap, heap_size);
  35. }
  36. let p = pac::Peripherals::take().unwrap();
  37. let mut sysctl = p.SYSCTL.constrain();
  38. let fpioa = p.FPIOA.split(&mut sysctl.apb0);
  39. let clocks = Clocks::new();
  40. let _uarths_tx = fpioa.io5.into_function(fpioa::UARTHS_TX);
  41. let _uarths_rx = fpioa.io4.into_function(fpioa::UARTHS_RX);
  42. // Configure UART
  43. let serial = p.UARTHS.configure(115_200.bps(), &clocks);
  44. let (tx, rx) = serial.split();
  45. use rustsbi::legacy_stdio::init_legacy_stdio_embedded_hal_fuse;
  46. init_legacy_stdio_embedded_hal_fuse(tx, rx);
  47. println!("[rustsbi] Version 0.1.0");
  48. println!("{}", rustsbi::LOGO);
  49. println!("[rustsbi] Target device: K210");
  50. println!("[rustsbi] Kernel entry: 0x80200000");
  51. }
  52. extern "C" {
  53. fn _s_mode_start();
  54. }
  55. unsafe {
  56. mepc::write(_s_mode_start as usize);
  57. mstatus::set_mpp(MPP::Supervisor);
  58. enter_privileged(mhartid::read(), 0x2333333366666666);
  59. }
  60. }
  61. global_asm!(
  62. "
  63. .section .text
  64. .globl _s_mode_start
  65. _s_mode_start:
  66. 1: auipc ra, %pcrel_hi(1f)
  67. ld ra, %pcrel_lo(1b)(ra)
  68. jr ra
  69. .align 3
  70. 1: .dword 0x80200000
  71. "
  72. );
  73. // todo: configurable target address