Load kernel on qemu/k210.

This commit is contained in:
Yifan Wu 2020-11-11 16:50:00 +08:00
parent 29f4683ad8
commit 5370f725be
10 changed files with 1588 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea/*
os/target/*
os/.idea/*

BIN
bootloader/rustsbi-k210.bin Executable file

Binary file not shown.

BIN
bootloader/rustsbi-qemu.bin Executable file

Binary file not shown.

View File

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

61
os/Makefile Normal file
View File

@ -0,0 +1,61 @@
# Building
TARGET := riscv64gc-unknown-none-elf
MODE := release
KERNEL_ELF := target/$(TARGET)/$(MODE)/os
KERNEL_BIN := $(KERNEL_ELF).bin
DISASM_TMP := target/$(TARGET)/$(MODE)/asm
# BOARD
BOARD ?= qemu
SBI ?= rustsbi
BOOTLOADER := ../bootloader/$(SBI)-$(BOARD).bin
# Run K210
K210-SERIALPORT = /dev/ttyUSB0
K210-BURNER = ../tools/kflash.py
# Binutils
OBJDUMP := rust-objdump --arch-name=riscv64
OBJCOPY := rust-objcopy --binary-architecture=riscv64
# Disassembly
DISASM ?= -x
build: $(KERNEL_BIN)
$(KERNEL_BIN): kernel
@$(OBJCOPY) $(KERNEL_ELF) --strip-all -O binary $@
kernel:
@cargo build --release
clean:
@cargo clean
disasm: kernel
@$(OBJDUMP) $(DISASM) $(KERNEL_ELF) | less
disasm-vim: kernel
@$(OBJDUMP) $(DISASM) $(KERNEL_ELF) > $(DISASM_TMP)
@vim $(DISASM_TMP)
@rm $(DISASM_TMP)
run: run-inner
run-inner: build
ifeq ($(BOARD),qemu)
@qemu-system-riscv64 \
-machine virt \
-nographic \
-bios $(BOOTLOADER) \
-device loader,file=$(KERNEL_BIN),addr=0x80020000
else
@cp $(BOOTLOADER) $(BOOTLOADER).copy
@dd if=$(KERNEL_BIN) of=$(BOOTLOADER).copy bs=128K seek=1
@mv $(BOOTLOADER).copy $(KERNEL_BIN)
@sudo chmod 777 $(K210-SERIALPORT)
python3 $(K210-BURNER) -p $(K210-SERIALPORT) -b 1500000 $(KERNEL_BIN)
miniterm --eol LF --dtr 0 --rts 0 --filter direct $(K210-SERIALPORT) 115200
endif
.PHONY: build kernel clean disasm disasm-vim run-inner

1
os/rust-toolchain Normal file
View File

@ -0,0 +1 @@
nightly-2020-11-01

12
os/src/entry.asm Normal file
View File

@ -0,0 +1,12 @@
.section .text.entry
.globl _start
_start:
la sp, boot_stack_top
call rust_main
.section .bss.stack
.globl boot_stack
boot_stack:
.space 4096 * 16
.globl boot_stack_top
boot_stack_top:

48
os/src/linker.ld Normal file
View File

@ -0,0 +1,48 @@
OUTPUT_ARCH(riscv)
ENTRY(_start)
BASE_ADDRESS = 0x80020000;
SECTIONS
{
. = BASE_ADDRESS;
skernel = .;
stext = .;
.text : {
*(.text.entry)
*(.text)
}
. = ALIGN(4K);
etext = .;
srodata = .;
.rodata : {
*(.rodata)
}
. = ALIGN(4K);
erodata = .;
sdata = .;
.data : {
*(.data)
}
. = ALIGN(4K);
edata = .;
sbss = .;
.bss : {
*(.bss)
}
. = ALIGN(4K);
ebss = .;
.stack : {
*(.bss.stack)
}
ekernel = .;
/DISCARD/ : {
*(.eh_frame)
}
}

View File

@ -1,5 +1,12 @@
#![no_std]
#![no_main]
#![feature(global_asm)]
mod lang_items;
global_asm!(include_str!("entry.asm"));
#[no_mangle]
pub extern "C" fn rust_main() -> ! {
loop {}
}

1452
tools/kflash.py Executable file

File diff suppressed because one or more lines are too long