From 0fae19165180f95b8e7d5a7b427c709cc95463b7 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 3 Jun 2020 11:01:50 +0800 Subject: [PATCH] examples: shell: get building in a hosted env Signed-off-by: Sean Cross --- examples/shell/.cargo/config | 4 +- examples/shell/build.rs | 17 ++++++ examples/shell/src/baremetal.rs | 68 ++++++++++++++++++++++++ examples/shell/src/main.rs | 91 +++++++++------------------------ examples/shell/src/start.rs | 20 -------- examples/shell/src/timer.rs | 2 +- 6 files changed, 112 insertions(+), 90 deletions(-) create mode 100644 examples/shell/build.rs create mode 100644 examples/shell/src/baremetal.rs delete mode 100644 examples/shell/src/start.rs diff --git a/examples/shell/.cargo/config b/examples/shell/.cargo/config index 478c166b7..08edeb83c 100644 --- a/examples/shell/.cargo/config +++ b/examples/shell/.cargo/config @@ -9,5 +9,5 @@ rustflags = [ "-C", "linker-plugin-lto", ] -[build] -target = "riscv32imac-unknown-none-elf" +#[build] +#target = "riscv32imac-unknown-none-elf" diff --git a/examples/shell/build.rs b/examples/shell/build.rs new file mode 100644 index 000000000..43c3deb4f --- /dev/null +++ b/examples/shell/build.rs @@ -0,0 +1,17 @@ +// NOTE: Adapted from cortex-m/build.rs +use std::env; + +fn main() { + let target = env::var("TARGET").unwrap(); + + let target_os = target.split('-').nth(2).unwrap_or("none"); + + // If we're not running on a desktop-class operating system, emit the "baremetal" + // config setting. This will enable software to do tasks such as + // managing memory. + if target_os == "none" { + println!("cargo:rustc-cfg=baremetal"); + } + + println!("cargo:rerun-if-changed=build.rs"); +} diff --git a/examples/shell/src/baremetal.rs b/examples/shell/src/baremetal.rs new file mode 100644 index 000000000..087f1b70f --- /dev/null +++ b/examples/shell/src/baremetal.rs @@ -0,0 +1,68 @@ +use core::panic::PanicInfo; + +#[panic_handler] +fn handle_panic(arg: &PanicInfo) -> ! { + println!("PANIC!"); + println!("Details: {:?}", arg); + xous::syscall::wait_event(); + loop {} +} + +fn handle_irq(irq_no: usize, arg: *mut usize) { + print!("Handling IRQ {} (arg: {:08x}): ", irq_no, arg as usize); + + while let Some(c) = crate::debug::DEFAULT.getc() { + print!("0x{:02x}", c); + } + println!(); +} + +/// Rust entry point (_start_rust) +/// +/// Zeros bss section, initializes data section and calls main. This function +/// never returns. +#[link_section = ".init.rust"] +#[export_name = "_start"] +pub unsafe extern "C" fn start_rust() -> ! { + extern "Rust" { + // This symbol will be provided by the kernel + fn main(); + } + + let uart = xous::syscall::map_memory( + xous::MemoryAddress::new(0xf000_1000), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map uart"); + unsafe { crate::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() }; + println!("Mapped UART @ {:08x}", uart.addr.get()); + + xous::syscall::map_memory( + xous::MemoryAddress::new(0xf000_2000), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .map(|_| println!("!!!WARNING: managed to steal kernel's memory")) + .ok(); + println!("Process: map success!"); + + crate::debug::DEFAULT.enable_rx(); + println!("Allocating IRQ..."); + xous::rsyscall(xous::SysCall::ClaimInterrupt( + 2, + handle_irq as *mut usize, + core::ptr::null_mut::(), + )) + .expect("couldn't claim interrupt"); + + main(); + panic!("exited main"); +} + +#[export_name = "abort"] +pub extern "C" fn abort() -> ! { + panic!("aborted"); +} diff --git a/examples/shell/src/main.rs b/examples/shell/src/main.rs index 3b383ab57..b051b005e 100644 --- a/examples/shell/src/main.rs +++ b/examples/shell/src/main.rs @@ -1,33 +1,18 @@ -#![no_std] -#![no_main] +#![cfg_attr(baremetal, no_main)] +#![cfg_attr(baremetal, no_std)] +#[cfg(baremetal)] #[macro_use] mod debug; -mod start; + +#[cfg(baremetal)] +mod baremetal; mod timer; - mod logstr; +#[cfg(baremetal)] use core::fmt::Write; -use core::panic::PanicInfo; - -#[panic_handler] -fn handle_panic(arg: &PanicInfo) -> ! { - println!("PANIC!"); - println!("Details: {:?}", arg); - xous::syscall::wait_event(); - loop {} -} - -fn handle_irq(irq_no: usize, arg: *mut usize) { - print!("Handling IRQ {} (arg: {:08x}): ", irq_no, arg as usize); - - while let Some(c) = debug::DEFAULT.getc() { - print!("0x{:02x}", c); - } - println!(); -} // fn print_and_yield(index: *mut usize) -> ! { // let num = index as usize; @@ -37,37 +22,8 @@ fn handle_irq(irq_no: usize, arg: *mut usize) { // } // } -#[no_mangle] +#[cfg_attr(baremetal, no_mangle)] fn main() { - let uart = xous::syscall::map_memory( - xous::MemoryAddress::new(0xf000_1000), - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map uart"); - unsafe { debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() }; - println!("Mapped UART @ {:08x}", uart.addr.get() ); - - xous::syscall::map_memory( - xous::MemoryAddress::new(0xf000_2000), - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .map(|_| println!("!!!WARNING: managed to steal kernel's memory")) - .ok(); - println!("Process: map success!"); - - debug::DEFAULT.enable_rx(); - println!("Allocating IRQ..."); - xous::rsyscall(xous::SysCall::ClaimInterrupt( - 2, - handle_irq as *mut usize, - 0 as *mut usize, - )) - .expect("couldn't claim interrupt"); - // println!("Allocating a ton of space on the stack..."); // { // let _big_array = [42u8; 131072]; @@ -96,6 +52,7 @@ fn main() { // println!("Unexpected return value: {:?}", heap); // } + println!("Starting to initialize the timer"); timer::init(); // for i in 1usize..5 { @@ -123,22 +80,22 @@ fn main() { let connection = connection.unwrap(); println!("Connected: {:?}", connection); - let mut counter = 0; - let mut ls = logstr::LogStr::new(); + let mut counter: usize = 0; + // let ls = logstr::LogStr::new(); loop { - // println!("Sending a scalar message with id {}...", counter + 4096); - // xous::syscall::send_message( - // connection, - // xous::Message::Scalar(xous::ScalarMessage { - // id: counter + 4096, - // arg1: counter, - // arg2: counter * 2, - // arg3: !counter, - // arg4: counter + 1, - // }), - // ) - // .expect("couldn't send scalar message"); - if counter & 0xfff == 0 { + println!("Sending a scalar message with id {}...", counter + 4096); + xous::syscall::send_message( + connection, + xous::Message::Scalar(xous::ScalarMessage { + id: counter + 4096, + arg1: counter, + arg2: counter * 2, + arg3: !counter, + arg4: counter + 1, + }), + ) + .expect("couldn't send scalar message"); + if counter.trailing_zeros() >= 12 { println!("Loop {}", counter); } counter += 1; diff --git a/examples/shell/src/start.rs b/examples/shell/src/start.rs deleted file mode 100644 index 62a51d364..000000000 --- a/examples/shell/src/start.rs +++ /dev/null @@ -1,20 +0,0 @@ - -/// Rust entry point (_start_rust) -/// -/// Zeros bss section, initializes data section and calls main. This function -/// never returns. -#[link_section = ".init.rust"] -#[export_name = "_start"] -pub unsafe extern "C" fn start_rust() -> ! { - extern "Rust" { - // This symbol will be provided by the kernel - fn main(); - } - main(); - panic!("exited main"); -} - -#[export_name = "abort"] -pub extern "C" fn abort() -> ! { - panic!("aborted"); -} diff --git a/examples/shell/src/timer.rs b/examples/shell/src/timer.rs index dd402c7f5..fa58414d4 100644 --- a/examples/shell/src/timer.rs +++ b/examples/shell/src/timer.rs @@ -25,7 +25,7 @@ pub fn init() { xous::rsyscall(xous::SysCall::ClaimInterrupt( 1, timer_tick as *mut usize, - 0 as *mut usize, + core::ptr::null_mut::(), )) .expect("timer: couldn't claim interrupt");