Skip to content

Commit

Permalink
Worked on riscv32/64 syscalls.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanMcMillan54 committed Aug 26, 2021
1 parent 09671fa commit a32ebfc
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 10 deletions.
2 changes: 2 additions & 0 deletions arch/riscv/src/include/asm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ global_asm!(include_str!("risc.S"));
#[cfg(target_arch = "riscv64")]
global_asm!(include_str!("trap.S"));

pub mod syscall;

extern "C" {
fn wait_for_interrupt() -> !;
}
Expand Down
9 changes: 9 additions & 0 deletions arch/riscv/src/include/asm/syscall.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::kernel::syscalls::*;

#[no_mangle]
pub unsafe extern "C" fn syscall(sys_num: i32, sys_arg: u8) {
match sys_num {
64 => sys_write(sys_arg),
_ => return,
}
}
2 changes: 2 additions & 0 deletions arch/riscv/src/kernel/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ pub trait Device {
fn name(&mut self) -> &'static str {
return "None";
}

const UART0: usize = 0;
}
6 changes: 6 additions & 0 deletions arch/riscv/src/kernel/hifive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use hifive1::hal::time::U32Ext;
pub struct Board;

impl Board {
pub fn new() -> Self {
return Board;
}

pub fn peripherals(&mut self) -> DevicePeripherals {
let dr = DeviceResources::take().unwrap();
let p = dr.peripherals;
Expand Down Expand Up @@ -36,4 +40,6 @@ impl device::Device for Board {
fn name(&mut self) -> &'static str {
return "HiFive";
}

const UART0: usize = 0x1001_3000;
}
1 change: 1 addition & 0 deletions arch/riscv/src/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod device;
pub mod panic;
pub mod printk;
pub mod riscv;
pub mod syscalls;
pub mod uart;

#[cfg(feature = "hifive")]
Expand Down
6 changes: 6 additions & 0 deletions arch/riscv/src/kernel/printk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ pub unsafe extern "C" fn _riscv_printk(fmt: Arguments) {
write!(Uart::new(uart), "{}{}", fmt, "\n");
}

#[export_name = "_kernel_main_print"]
#[no_mangle]
pub extern "C" fn _riscv_main_print(fmt: Arguments) {

}

#[macro_export]
macro_rules! riscv_printk {
($($arg:tt)*) => {$crate::kernel::printk::_riscv_printk(format_args!($($arg)*))};
Expand Down
4 changes: 2 additions & 2 deletions arch/riscv/src/kernel/riscv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use kinfo::info::set_info;
use kinfo::status::set_status;
use modules::modules::KernelModules;
use modules::start::arch_modules_init;
use crate::riscv_printk;
Expand All @@ -9,7 +9,7 @@ unsafe fn mm_init() {
let (mut sbss, mut ebss) = bss_memory.bss_values();

if sbss < ebss {
set_info("not ok");
set_status("not ok");
riscv_printk!(" bss start is less than bss end");
riscv_printk!(" sbss {} < ebss {}", sbss, ebss);
r0::zero_bss(&mut sbss, &mut ebss);
Expand Down
17 changes: 17 additions & 0 deletions arch/riscv/src/kernel/syscalls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use super::board::Board;
use super::uart::Uart;
use crate::kernel::device::Device;

#[no_mangle]
pub unsafe extern "C" fn sys_write(write: u8) {
let mut uart = Uart::new(Board::UART0);

uart.write_byte(write);
}

#[no_mangle]
pub unsafe extern "C" fn sys_read() -> u8 {
let mut uart = Uart::new(Board::UART0);

return uart.read();
}
13 changes: 9 additions & 4 deletions arch/riscv/src/kernel/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,25 @@ impl Uart {
}

// I/O
pub unsafe fn write_byte(&mut self, byte: u8) {
pub fn write_byte(&mut self, byte: u8) {
let mut uart = self.address as *mut u8;
uart.add(0).write_volatile(byte);
unsafe { uart.add(0).write_volatile(byte); }
}

pub unsafe fn write_bytes(&mut self, bytes: &[u8]) {
pub fn write_bytes(&mut self, bytes: &[u8]) {
for byte in bytes {
self.write_byte(*byte);
}
}

pub unsafe fn write_string(&mut self, string: &str) {
pub fn write_string(&mut self, string: &str) {
self.write_bytes(string.as_bytes());
}

pub fn read(&mut self) -> u8 {
let mut uart = self.address as *mut u8;
return unsafe { uart.add(0).read_volatile() };
}
}

impl Write for Uart {
Expand Down
8 changes: 8 additions & 0 deletions arch/riscv/src/kernel/virt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ use super::{device, uart};

pub struct Board;

impl Board {
pub fn new() -> Self {
return Board;
}
}

impl device::Device for Board {
fn device_init(&mut self) {
self.io_init();
Expand All @@ -14,4 +20,6 @@ impl device::Device for Board {
fn name(&mut self) -> &'static str {
return "Qemu Virt";
}

const UART0: usize = 0x1000_0000;
}
2 changes: 1 addition & 1 deletion arch/riscv/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_std]
#![feature(panic_info_message)]
#![feature(asm, global_asm)]
#![feature(alloc_error_handler, asm, global_asm)]

#[macro_use] extern crate kinfo;
#[macro_use] extern crate riscv_rt;
Expand Down
25 changes: 25 additions & 0 deletions arch/riscv/src/mm/alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use core::alloc::{GlobalAlloc, Layout};
use core::ptr::null_mut;
use crate::riscv_printk;

pub struct Allocator;

#[global_allocator]
pub static GLOBAL_ALLOCATOR: Allocator = Allocator;

unsafe impl GlobalAlloc for Allocator {
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
null_mut()
}

unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
loop { }
}
}

#[alloc_error_handler]
unsafe fn alloc_error(_layout: Layout) -> ! {
riscv_printk!("Alloc memory layout:");
riscv_printk!(" {:?}", _layout);
panic!("Alloc memory error");
}
1 change: 1 addition & 0 deletions arch/riscv/src/mm/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod alloc;
pub mod bss;
10 changes: 10 additions & 0 deletions lib/libc/src/unistd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern "C" {
pub fn syscall(sys_num: i32, sys_arg: u8);
pub fn sys_write(write: u8);
pub fn sys_read() -> u8;
}

#[cfg(target_arch = "x86_64")]
Expand All @@ -19,3 +20,12 @@ pub mod aarch64 {

#[cfg(target_arch = "aarch64")]
pub use aarch64::*;

#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
pub mod riscv {
pub const READ: i32 = 63;
pub const WRITE: i32 = 64;
}

#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
pub use riscv::*;
3 changes: 0 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ pub use novusk::arm;

#[cfg(target_arch = "x86_64")]
pub use novusk::x86_64;
use libc::unistd::{syscall, WRITE};

#[no_mangle]
pub unsafe extern "C" fn kernel_main() {
printk::printk!("\nKernel Main");

syscall(WRITE, 69);
}

0 comments on commit a32ebfc

Please sign in to comment.