blob: 83f00c824108f6be3770147daf6085c9df7ef996 [file] [log] [blame]
//! Board file for Shodan's "Matcha" RISC-V development platform.
#![no_std]
#![no_main]
#![feature(llvm_asm)]
#![feature(const_fn)]
#![feature(naked_functions)]
use capsules::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
use core::panic::PanicInfo;
use kernel::capabilities;
use kernel::common::dynamic_deferred_call::{DynamicDeferredCall, DynamicDeferredCallClientState};
use kernel::component::Component;
use kernel::hil;
use kernel::hil::time::Alarm;
use kernel::Chip;
use kernel::Platform;
use kernel::{create_capability, debug, static_init};
use matcha_capsules::debug_uart::DebugUartCapsule;
use matcha_capsules::elf_loader::ElfLoaderCapsule;
use matcha_capsules::storage_manager::StorageManagerCapsule;
use matcha_config::*;
use matcha_hal::dprintf;
use rv32i::csr;
pub mod chip;
pub mod timer;
pub mod uart;
/// Panic handler.
#[cfg(not(test))]
#[no_mangle]
#[panic_handler]
pub unsafe extern "C" fn panic_fmt(_pi: &PanicInfo) -> ! {
dprintf!("panic panic panic!\n");
loop {}
}
/// These symbols are defined in the linker script.
extern "C" {
/// Beginning of the ROM region containing app images.
static _sapps: u8;
/// End of the ROM region containing app images.
static _eapps: u8;
/// Beginning of the RAM region for app memory.
static mut _sappmem: u8;
/// End of the RAM region for app memory.
static _eappmem: u8;
}
// Actual memory for holding the active process structures. Need an empty list
// at least.
static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; 4] =
[None, None, None, None];
/// Dummy buffer that causes the linker to reserve enough space for the stack.
/// Must be at least 16k in debug builds (@aappleby - not sure why, what's so large?)
#[no_mangle]
#[link_section = ".stack_buffer"]
pub static mut STACK_MEMORY: [u8; 0x4000] = [0; 0x4000];
/// A structure representing this platform that holds references to all
/// capsules for this platform. We've included an alarm and console.
struct MatchaPlatform {
console: &'static capsules::console::Console<'static>,
alarm: &'static capsules::alarm::AlarmDriver<
'static,
VirtualMuxAlarm<'static, crate::timer::RvTimer<'static>>,
>,
debug_uart: &'static DebugUartCapsule,
storage_manager: &'static StorageManagerCapsule,
elf_loader: &'static ElfLoaderCapsule,
}
/// Mapping of integer syscalls to objects that implement syscalls.
impl Platform for MatchaPlatform {
fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
where
F: FnOnce(Option<&dyn kernel::Driver>) -> R,
{
match driver_num {
DRIVER_NUM_CONSOLE => f(Some(self.console)),
DRIVER_NUM_ALARM => f(Some(self.alarm)),
DRIVER_NUM_DEBUG_UART => f(Some(self.debug_uart)),
DRIVER_NUM_STORAGE_MANAGER => f(Some(self.storage_manager)),
DRIVER_NUM_ELF_LOADER => f(Some(self.elf_loader)),
_ => f(None),
}
}
}
/// Reset Handler.
///
/// This function is called from the arch crate after some very basic RISC-V
/// setup.
#[no_mangle]
pub unsafe fn reset_handler() {
// Basic setup of the platform.
rv32i::init_memory();
// Ibex-specific handler
crate::chip::configure_trap_handler();
dprintf!("sw/matcha/platform/src/main.rs::reset_handler()\n");
// initialize capabilities
let process_mgmt_cap = create_capability!(capabilities::ProcessManagementCapability);
let memory_allocation_cap = create_capability!(capabilities::MemoryAllocationCapability);
let main_loop_cap = create_capability!(capabilities::MainLoopCapability);
let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&PROCESSES));
let dynamic_deferred_call_clients =
static_init!([DynamicDeferredCallClientState; 1], Default::default());
let dynamic_deferred_caller = static_init!(
DynamicDeferredCall,
DynamicDeferredCall::new(dynamic_deferred_call_clients)
);
DynamicDeferredCall::set_global_instance(dynamic_deferred_caller);
// Create a shared UART channel for the console and for kernel debug.
let uart_mux = components::console::UartMuxComponent::new(
&crate::uart::UART0,
crate::uart::UART0_BAUDRATE,
dynamic_deferred_caller,
)
.finalize(());
let alarm = &crate::timer::TIMER;
alarm.setup();
// Create a shared virtualization mux layer on top of a single hardware
// alarm.
let mux_alarm = static_init!(
MuxAlarm<'static, crate::timer::RvTimer>,
MuxAlarm::new(alarm)
);
hil::time::Alarm::set_alarm_client(&crate::timer::TIMER, mux_alarm);
// Alarm
let virtual_alarm_user = static_init!(
VirtualMuxAlarm<'static, crate::timer::RvTimer>,
VirtualMuxAlarm::new(mux_alarm)
);
let scheduler_timer_virtual_alarm = static_init!(
VirtualMuxAlarm<'static, crate::timer::RvTimer>,
VirtualMuxAlarm::new(mux_alarm)
);
let alarm = static_init!(
capsules::alarm::AlarmDriver<'static, VirtualMuxAlarm<'static, crate::timer::RvTimer>>,
capsules::alarm::AlarmDriver::new(
virtual_alarm_user,
board_kernel.create_grant(&memory_allocation_cap)
)
);
hil::time::Alarm::set_alarm_client(virtual_alarm_user, alarm);
let chip = static_init!(
crate::chip::Matcha<VirtualMuxAlarm<'static, crate::timer::RvTimer>>,
crate::chip::Matcha::new(scheduler_timer_virtual_alarm)
);
scheduler_timer_virtual_alarm.set_alarm_client(chip.scheduler_timer());
// Need to enable all interrupts for Tock Kernel
chip.enable_plic_interrupts();
// enable interrupts globally
csr::CSR
.mie
.modify(csr::mie::mie::msoft::SET + csr::mie::mie::mtimer::SET + csr::mie::mie::mext::SET);
csr::CSR.mstatus.modify(csr::mstatus::mstatus::mie::SET);
// Setup the console.
let console = components::console::ConsoleComponent::new(board_kernel, uart_mux).finalize(());
// Create the debugger object that handles calls to `debug!()`.
components::debug_writer::DebugWriterComponent::new(uart_mux).finalize(());
let debug_uart_capsule = static_init!(
DebugUartCapsule,
DebugUartCapsule {
app_data_grant: board_kernel.create_grant(&memory_allocation_cap)
}
);
let storage_manager = static_init!(
matcha_capsules::storage_manager::StorageManagerCapsule,
matcha_capsules::storage_manager::StorageManagerCapsule::new(
board_kernel.create_grant(&memory_allocation_cap)
)
);
let elf_loader = static_init!(
matcha_capsules::elf_loader::ElfLoaderCapsule,
matcha_capsules::elf_loader::ElfLoaderCapsule {}
);
let platform = MatchaPlatform {
console: console,
alarm: alarm,
debug_uart: debug_uart_capsule,
storage_manager: storage_manager,
elf_loader: elf_loader,
};
kernel::procs::load_processes(
board_kernel,
chip,
core::slice::from_raw_parts(
&_sapps as *const u8,
&_eapps as *const u8 as usize - &_sapps as *const u8 as usize,
),
core::slice::from_raw_parts_mut(
&mut _sappmem as *mut u8,
&_eappmem as *const u8 as usize - &_sappmem as *const u8 as usize,
),
&mut PROCESSES,
kernel::procs::FaultResponse::Panic,
&process_mgmt_cap,
)
.unwrap_or_else(|err| {
debug!("Error loading processes!");
debug!("{:?}", err);
});
debug!("MatchaPlatform initialisation complete. Entering main loop");
let scheduler = components::sched::priority::PriorityComponent::new(board_kernel).finalize(());
board_kernel.kernel_loop(&platform, chip, None, scheduler, &main_loop_cap);
}