add: user library and some applications

This commit is contained in:
jackfiled 2024-07-16 15:48:34 +08:00
parent 4c9e93295c
commit 8eba5a3d9b
15 changed files with 263 additions and 0 deletions

View File

@ -3,7 +3,9 @@
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/os/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/user/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/os/target" />
<excludeFolder url="file://$MODULE_DIR$/user/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />

7
user/.cargo/config.toml Normal file
View File

@ -0,0 +1,7 @@
[build]
target = "riscv64gc-unknown-none-elf"
[target.riscv64gc-unknown-none-elf]
rustflags = [
"-Clink-args=-Tsrc/linker.ld", "-Cforce-frame-pointers=yes"
]

1
user/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
user/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "user_lib"
version = "0.1.0"

6
user/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "user_lib"
version = "0.1.0"
edition = "2021"
[dependencies]

18
user/Makefile Normal file
View File

@ -0,0 +1,18 @@
TARGET := riscv64gc-unknown-none-elf
MODE := release
APP_DIR := src/bin
TARGET_DIR := target/$(TARGET)/$(MODE)
APPS := $(wildcard $(APP_DIR)/*.rs)
ELFS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%, $(APPS))
BINS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%.bin, $(APPS))
OBJDUMP := rust-objdump --arch-name=riscv64
OBJCOPY := rust-objcopy --binary-architecture=riscv64
elf:
@cargo build --release
binary: elf
@$(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/%, $(TARGET_DIR)/%.bin, $(elf));)
build: binary

View File

@ -0,0 +1,12 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
#[no_mangle]
fn main() -> i32 {
println!("Hello, world!");
0
}

View File

@ -0,0 +1,17 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
use core::arch::asm;
#[no_mangle]
fn main() -> i32 {
println!("Try to execute privileged instruction in U Mode");
println!("Kernel should kill this application!");
unsafe {
asm!("sret");
}
0
}

27
user/src/bin/power.rs Normal file
View File

@ -0,0 +1,27 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
const SIZE: usize = 10;
const P: u32 = 3;
const STEP: usize = 100000;
const MOD: u32 = 10007;
#[no_mangle]
fn main() -> i32 {
let mut pow = [0u32; SIZE];
let mut index: usize = 0;
pow[index] = 1;
for i in 1..=STEP {
let last = pow[index];
index = (index + 1) % SIZE;
pow[index] = last * P % MOD;
if i % 10000 == 0 {
println!("{}^{}={}(MOD {})", P, i, pow[index], MOD);
}
}
println!("Test power OK!");
0
}

View File

@ -0,0 +1,15 @@
#![no_std]
#![no_main]
#[macro_use]
extern crate user_lib;
#[no_mangle]
fn main() -> i32 {
println!("Into Test store_fault, we will insert an invalid store operation...");
println!("Kernel should kill this application!");
unsafe {
core::ptr::null_mut::<u8>().write_volatile(0);
}
0
}

32
user/src/console.rs Normal file
View File

@ -0,0 +1,32 @@
use core::fmt::Write;
use crate::syscall::sys_write;
struct Stdout;
const STDOUT_FD: usize = 1;
impl Write for Stdout {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
sys_write(STDOUT_FD, s.as_bytes());
Ok(())
}
}
pub fn print(args: core::fmt::Arguments) {
Stdout.write_fmt(args).unwrap();
}
#[macro_export]
macro_rules! print {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!($fmt $(, $($arg)+)?));
}
}
#[macro_export]
macro_rules! println {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
}
}

31
user/src/lib.rs Normal file
View File

@ -0,0 +1,31 @@
#![no_std]
#![feature(linkage)]
use crate::syscall::{sys_exit, sys_write};
mod utils;
mod syscall;
#[macro_use]
pub mod console;
#[no_mangle]
#[link_section = ".text.entry"]
pub extern "C" fn _start() -> ! {
utils::clear_bss();
exit(main());
panic!("Unreachable after exit main!");
}
#[no_mangle]
#[linkage = "weak"]
pub fn main() -> i32 {
panic!("Cannot find main function!");
}
pub fn write(fd: usize, buf: &[u8]) -> isize {
sys_write(fd, buf)
}
pub fn exit(exit_code: i32) -> isize {
sys_exit(exit_code)
}

31
user/src/linker.ld Normal file
View File

@ -0,0 +1,31 @@
OUTPUT_ARCH(riscv)
ENTRY(_start)
BASE_ADDRESS = 0x80400000;
SECTIONS
{
. = BASE_ADDRESS;
.text : {
*(.text.entry)
*(.text .text.*)
}
.rodata : {
*(.rodata .rodata.*)
*(.srodata .srodata.*)
}
.data : {
*(.data .data.*)
*(.sdata .sdata.*)
}
.bss : {
start_bss = .;
*(.bss .bss.*)
*(.sbss .sbss.*)
end_bss = .;
}
/DISCARD/ : {
*(.eh_frame)
*(.debug*)
}
}

28
user/src/syscall.rs Normal file
View File

@ -0,0 +1,28 @@
use core::arch::asm;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_EXIT: usize = 93;
fn syscall(id: usize, args: [usize; 3]) -> isize {
let mut result : isize;
unsafe {
asm!(
"ecall",
inlateout("x10") args[0] => result,
in("x11") args[1],
in("x12") args[2],
in("x17") id
)
}
result
}
pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
syscall(SYSCALL_WRITE, [fd, buffer.as_ptr() as usize, buffer.len()])
}
pub fn sys_exit(exit_code: i32) -> isize {
syscall(SYSCALL_EXIT, [exit_code as usize, 0, 0])
}

29
user/src/utils.rs Normal file
View File

@ -0,0 +1,29 @@
use core::panic::PanicInfo;
use crate::println;
pub fn clear_bss() {
extern "C" {
fn start_bss();
fn end_bss();
}
for i in start_bss as usize..end_bss as usize {
unsafe {
(i as *mut u8).write_volatile(0)
}
}
}
#[panic_handler]
fn panic_handler(info: &PanicInfo) -> ! {
let message = info.message();
if let Some(location) = info.location() {
println!("Panicked at {}:{} {}", location.file(), location.line(), message);
} else {
println!("Panicked: {}", message);
}
unreachable!()
}