Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

K210 #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .cargo/linker-k210.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
OUTPUT_ARCH(riscv)
ENTRY(_start)

BASE_ADDRESS = 0xffffffc080020000;

__ENTRY_ADDR = 0x80020000;

SECTIONS
{
/* Load the kernel at this address: "." means the current address */
. = BASE_ADDRESS;
start = .;

.text ALIGN(4K): AT(__ENTRY_ADDR) {
stext = .;
*(.text.entry)
*(.text .text.*)
. = ALIGN(4K);
etext = .;
}

.rodata ALIGN(4K): {
srodata = .;
*(.rodata .rodata.*)
. = ALIGN(4K);
erodata = .;
}

.data ALIGN(4K): {
. = ALIGN(4K);
*(.data.prepage)
. = ALIGN(4K);
sdata = .;
*(.data .data.*)
*(.sdata .sdata.*)
edata = .;
}

.stack : {
*(.bss.stack)
}

.bss : {
*(.bss.stack)
sbss = .;
*(.bss .bss.*)
*(.sbss .sbss.*)
ebss = .;
}

PROVIDE(end = .);
}
52 changes: 52 additions & 0 deletions .cargo/linker-qemu.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
OUTPUT_ARCH(riscv)
ENTRY(_start)

BASE_ADDRESS = 0xffffffc080200000;

__ENTRY_ADDR = 0x80200000;

SECTIONS
{
/* Load the kernel at this address: "." means the current address */
. = BASE_ADDRESS;
start = .;

.text ALIGN(4K): AT(__ENTRY_ADDR) {
stext = .;
*(.text.entry)
*(.text .text.*)
. = ALIGN(4K);
etext = .;
}

.rodata ALIGN(4K): {
srodata = .;
*(.rodata .rodata.*)
. = ALIGN(4K);
erodata = .;
}

.data ALIGN(4K): {
. = ALIGN(4K);
*(.data.prepage)
. = ALIGN(4K);
sdata = .;
*(.data .data.*)
*(.sdata .sdata.*)
edata = .;
}

.stack : {
*(.bss.stack)
}

.bss : {
*(.bss.stack)
sbss = .;
*(.bss .bss.*)
*(.sbss .sbss.*)
ebss = .;
}

PROVIDE(end = .);
}
4 changes: 2 additions & 2 deletions .cargo/linker-riscv.ld
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
OUTPUT_ARCH(riscv)
ENTRY(_start)

BASE_ADDRESS = 0xffffffc080200000;
BASE_ADDRESS = 0xffffffc080020000;

__ENTRY_ADDR = 0x80200000;
__ENTRY_ADDR = 0x80020000;

SECTIONS
{
Expand Down
20 changes: 19 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RELEASE := release
KERNEL_ELF = target/$(ARCH)-unknown-none-elf/$(RELEASE)/kernel
# SBI := tools/rustsbi-qemu.bin
FS_IMG := mount.img
SBI := tools/opensbi-qemu.bin
SBI := tools/rustsbi-k210.bin
QEMU_EXEC := qemu-system-riscv64 \
-machine virt \
-kernel $(KERNEL_ELF) \
Expand All @@ -15,6 +15,9 @@ QEMU_EXEC := qemu-system-riscv64 \
-nographic \
-smp 2
RUST_BUILD_OPTIONS :=
K210-SERIALPORT = /dev/ttyUSB0
K210-BURNER = tools/k210/kflash.py
BIN_FILE = byteos.bin

ifeq ($(RELEASE), release)
RUST_BUILD_OPTIONS += --release
Expand All @@ -34,8 +37,17 @@ fs-img:
sudo umount $(FS_IMG)

build: fs-img
cp .cargo/linker-qemu.ld .cargo/linker-riscv.ld
RUST_BACKTRACE=1 LOG=$(LOG) cargo build $(RUST_BUILD_OPTIONS) $(OFFLINE)

k210-build:
cp .cargo/linker-k210.ld .cargo/linker-riscv.ld
RUST_BACKTRACE=1 LOG=$(LOG) cargo build $(RUST_BUILD_OPTIONS) $(OFFLINE)
rust-objcopy --binary-architecture=riscv64 $(KERNEL_ELF) --strip-all -O binary $(BIN_FILE)
@cp $(SBI) $(SBI).copy
@dd if=$(BIN_FILE) of=$(SBI).copy bs=131072 seek=1
@mv $(SBI).copy $(BIN_FILE)

run: build
$(QEMU_EXEC)

Expand All @@ -54,6 +66,12 @@ gdb:
-ex 'set arch riscv:rv64' \
-ex 'target remote localhost:1234'

flash: k210-build
(which $(K210-BURNER)) || (cd tools && git clone https://github.com/sipeed/kflash.py.git k210)
@sudo chmod 777 $(K210-SERIALPORT)
python3 $(K210-BURNER) -p $(K210-SERIALPORT) -b 1500000 $(BIN_FILE)
python3 -m serial.tools.miniterm --eol LF --dtr 0 --rts 0 --filter direct $(K210-SERIALPORT) 115200

addr2line:
addr2line -sfipe $(KERNEL_ELF) | rustfilt

Expand Down
3 changes: 2 additions & 1 deletion arch/src/riscv64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ extern "C" fn rust_main(hartid: usize, device_tree: usize) {
// 开启SUM位 让内核可以访问用户空间 踩坑:
// only in qemu. eg: qemu is riscv 1.10 NOTE: k210 is riscv 1.9.1
// in 1.10 is SUM but in 1.9.1 is PUM which is the opposite meaning with SUM
sstatus::set_sum();
// use default @ k210
// sstatus::set_sum();

main(hartid, device_tree);
}
Expand Down
Binary file added byteos.bin
Binary file not shown.
25 changes: 20 additions & 5 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,26 @@ use hal;
use kalloc;
use panic_handler as _;


fn clear_bss() {
extern "C" {
fn sbss();
fn ebss();
}
unsafe {
core::slice::from_raw_parts_mut(sbss as usize as *mut u8, ebss as usize - sbss as usize)
.fill(0);
}
}


#[no_mangle]
fn main(hart_id: usize, device_tree: usize) {
// if hart_id != 0 {
// loop {}
// }
if hart_id != 0 {
loop {}
}

clear_bss();

let str = include_str!("banner.txt");
println!("{}", str);
Expand All @@ -33,7 +48,7 @@ fn main(hart_id: usize, device_tree: usize) {
hal::interrupt::init();

// print boot info
info!("booting at kernel {}", hart_id);
println!("booting at kernel {}", hart_id);

// initialize kernel alloc module
kalloc::init();
Expand All @@ -48,7 +63,7 @@ fn main(hart_id: usize, device_tree: usize) {
devices::prepare_devices();

// initialize filesystem
fs::init();
// fs::init();

// init kernel threads and async executor
tasks::init();
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/tasks/initproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ pub async fn initproc() {

// command("busybox sh").await;
// command("busybox sh busybox_testcode.sh").await;
run_libc_test().await;
// run_libc_test().await;
// run_all().await;

// simple_shell().await;
simple_shell().await;
// command("helloworld").await;
// command("filelist").await;
}
5 changes: 4 additions & 1 deletion modules/logging/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ impl Write for Logger {
}

pub fn init() {
log::set_logger(&Logger).unwrap();
if let Err(e) = log::set_logger(&Logger) {
println!("log error: {:?}", e);
return;
}
log::set_max_level(match option_env!("LOG") {
Some("error") => LevelFilter::Error,
Some("warn") => LevelFilter::Warn,
Expand Down
Binary file added tools/rustsbi-k210.bin
Binary file not shown.