|
| 1 | +//! Multi-hart example demonstrating IPI-based hart synchronization. |
| 2 | +//! |
| 3 | +//! Hart 0 initializes UART and wakes Hart 1 via software interrupt (CLINT). |
| 4 | +//! Both harts print messages and synchronize before exit. |
| 5 | +
|
| 6 | +#![no_std] |
| 7 | +#![no_main] |
| 8 | + |
| 9 | +extern crate panic_halt; |
| 10 | + |
| 11 | +use core::arch::global_asm; |
| 12 | +use core::sync::atomic::{AtomicBool, Ordering}; |
| 13 | +use riscv_rt::entry; |
| 14 | +use riscv_semihosting::debug::{self, EXIT_SUCCESS}; |
| 15 | + |
| 16 | +const UART_BASE: usize = 0x1000_0000; |
| 17 | +const UART_THR: usize = UART_BASE; |
| 18 | +const UART_LCR: usize = UART_BASE + 3; |
| 19 | +const UART_LSR: usize = UART_BASE + 5; |
| 20 | +const LCR_DLAB: u8 = 1 << 7; |
| 21 | +const LCR_8N1: u8 = 0x03; |
| 22 | +const LSR_THRE: u8 = 1 << 5; |
| 23 | + |
| 24 | +static UART_LOCK: AtomicBool = AtomicBool::new(false); |
| 25 | +static HART1_DONE: AtomicBool = AtomicBool::new(false); |
| 26 | + |
| 27 | +fn uart_lock() { |
| 28 | + while UART_LOCK |
| 29 | + .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) |
| 30 | + .is_err() |
| 31 | + { |
| 32 | + core::hint::spin_loop(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +fn uart_unlock() { |
| 37 | + UART_LOCK.store(false, Ordering::Release); |
| 38 | +} |
| 39 | + |
| 40 | +unsafe fn uart_write_reg(off: usize, v: u8) { |
| 41 | + (off as *mut u8).write_volatile(v); |
| 42 | +} |
| 43 | + |
| 44 | +unsafe fn uart_read_reg(off: usize) -> u8 { |
| 45 | + (off as *const u8).read_volatile() |
| 46 | +} |
| 47 | + |
| 48 | +fn uart_init() { |
| 49 | + unsafe { |
| 50 | + uart_write_reg(UART_LCR, LCR_DLAB); |
| 51 | + uart_write_reg(UART_THR, 0x01); |
| 52 | + uart_write_reg(UART_BASE + 1, 0x00); |
| 53 | + uart_write_reg(UART_LCR, LCR_8N1); |
| 54 | + uart_write_reg(UART_BASE + 2, 0x07); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +fn uart_write_byte(b: u8) { |
| 59 | + unsafe { |
| 60 | + while (uart_read_reg(UART_LSR) & LSR_THRE) == 0 {} |
| 61 | + uart_write_reg(UART_THR, b); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +fn uart_print(s: &str) { |
| 66 | + uart_lock(); |
| 67 | + for &b in s.as_bytes() { |
| 68 | + uart_write_byte(b); |
| 69 | + } |
| 70 | + uart_unlock(); |
| 71 | +} |
| 72 | + |
| 73 | +// Custom _mp_hook implementation in assembly |
| 74 | +// Hart 0 returns 1 (true) to initialize RAM |
| 75 | +// Hart 1 polls for IPI via CLINT, then returns 0 (false) to skip RAM init |
| 76 | +global_asm!( |
| 77 | + r#" |
| 78 | +.section .init.mp_hook, "ax" |
| 79 | +.global _mp_hook |
| 80 | +_mp_hook: |
| 81 | + beqz a0, 2f // if hart 0, return true |
| 82 | +
|
| 83 | + // Hart 1: Poll for IPI (no interrupts, just polling) |
| 84 | + // Clear any pending software interrupt first |
| 85 | + li t0, 0x02000004 // CLINT msip address for hart 1 |
| 86 | + sw zero, 0(t0) |
| 87 | +
|
| 88 | +1: // Poll mip register for software interrupt pending |
| 89 | + csrr t0, mip |
| 90 | + andi t0, t0, 8 // Check MSIP bit |
| 91 | + beqz t0, 1b // If not set, keep polling |
| 92 | +
|
| 93 | + // Clear the software interrupt |
| 94 | + li t0, 0x02000004 |
| 95 | + sw zero, 0(t0) |
| 96 | +
|
| 97 | + // Return false (0) - don't initialize RAM again |
| 98 | + li a0, 0 |
| 99 | + ret |
| 100 | +
|
| 101 | +2: // Hart 0: return true to initialize RAM |
| 102 | + li a0, 1 |
| 103 | + ret |
| 104 | +"# |
| 105 | +); |
| 106 | + |
| 107 | +#[entry] |
| 108 | +fn main(hartid: usize) -> ! { |
| 109 | + if hartid == 0 { |
| 110 | + uart_init(); |
| 111 | + uart_print("Hart 0: Initializing\n"); |
| 112 | + |
| 113 | + // Send IPI to Hart 1 (write to CLINT msip register for hart 1) |
| 114 | + unsafe { |
| 115 | + (0x02000004usize as *mut u32).write_volatile(1); |
| 116 | + } |
| 117 | + |
| 118 | + while !HART1_DONE.load(Ordering::Acquire) { |
| 119 | + core::hint::spin_loop(); |
| 120 | + } |
| 121 | + |
| 122 | + uart_print("Hart 0: Both harts done\n"); |
| 123 | + debug::exit(EXIT_SUCCESS); |
| 124 | + } else { |
| 125 | + // Hart 1 reaches here after _mp_hook detects IPI |
| 126 | + uart_print("Hart 1: Running\n"); |
| 127 | + HART1_DONE.store(true, Ordering::Release); |
| 128 | + } |
| 129 | + |
| 130 | + loop { |
| 131 | + core::hint::spin_loop(); |
| 132 | + } |
| 133 | +} |
0 commit comments