From 62b931d57648ff8518fcf324f7fcd93951af45ab Mon Sep 17 00:00:00 2001 From: WangRunji Date: Fri, 21 Dec 2018 01:50:52 +0800 Subject: [PATCH] print hello on K210 --- kernel/Cargo.toml | 2 +- kernel/run-qemu-script-custom-llc | 129 +++++++++++++-------- kernel/src/arch/riscv32/boot/boot.asm | 16 --- kernel/src/arch/riscv32/boot/boot_k210.asm | 36 ++++++ kernel/src/arch/riscv32/boot/linker64.ld | 20 +--- kernel/src/arch/riscv32/io.rs | 19 ++- kernel/src/arch/riscv32/mod.rs | 4 +- kernel/src/logging.rs | 4 +- 8 files changed, 134 insertions(+), 96 deletions(-) delete mode 100644 kernel/src/arch/riscv32/boot/boot.asm create mode 100644 kernel/src/arch/riscv32/boot/boot_k210.asm diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index aa429ce..d92e457 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -46,7 +46,7 @@ raw-cpuid = "6.0" uart_16550 = "0.1" pc-keyboard = "0.3" -[target.'cfg(target_arch = "riscv32")'.dependencies] +[target.'cfg(any(target_arch = "riscv32", target_arch = "riscv64"))'.dependencies] riscv = { git = "https://github.com/riscv-and-rust-and-decaf/riscv" } bbl = { path = "../crate/bbl" } diff --git a/kernel/run-qemu-script-custom-llc b/kernel/run-qemu-script-custom-llc index 3b2e552..91659bd 100755 --- a/kernel/run-qemu-script-custom-llc +++ b/kernel/run-qemu-script-custom-llc @@ -14,13 +14,15 @@ then fi # CONFIGURATIONS -export LLC=$PWD/../../llvm/build/bin/llc +export LLC=$PWD/../tools/llc export LLC_ARCH=riscv64 export RUST_SRC_PATH=$(rustc --print sysroot)/lib/rustlib/src/rust/src export TARGET_JSON=$PWD/riscv64-blog_os.json export CARGO_PATH=~/.cargo export CC=riscv64-unknown-elf-gcc export AR=riscv64-unknown-elf-ar +export OBJCOPY=riscv64-unknown-elf-objcopy +export QEMU=$PWD/../../@github/qemu/build/riscv64-softmmu/qemu-system-riscv64 #============================================================================ @@ -28,12 +30,9 @@ export AR=riscv64-unknown-elf-ar gen_full_rlib() { cd outdir - for X in ${CNAME}.*ll + for X in ${CNAME}.*bc do -# If using old LLVM because of asb's patch, uncomment the following line. -# It only affects debugging. -# sed 's/, retainedNodes: ![0-9]\+//' -i ${X} - $LLC -march=$LLC_ARCH -filetype=obj ${X} + ${LLC} -march=${LLC_ARCH} -filetype=obj ${X} done for X in ${CNAME}.*o do @@ -48,7 +47,7 @@ CNAME=core if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name ${CNAME} $RUST_SRC_PATH/libcore/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=3 \ -C debuginfo=2 \ -Z force-unstable-if-unmarked \ @@ -62,7 +61,7 @@ CNAME=compiler_builtins if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name compiler_builtins $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.2/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=3 \ -C debuginfo=2 \ -Z force-unstable-if-unmarked \ @@ -78,7 +77,7 @@ CNAME=alloc if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name alloc $RUST_SRC_PATH/liballoc/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=3 \ -C debuginfo=2 \ -Z force-unstable-if-unmarked \ @@ -93,7 +92,7 @@ CNAME=semver_parser if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name semver_parser $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/semver-parser-0.7.0/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -107,7 +106,7 @@ CNAME=cfg_if if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name cfg_if $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/cfg-if-0.1.6/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -122,7 +121,7 @@ CNAME=spin if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name spin $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/spin-0.4.10/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -141,7 +140,7 @@ CNAME=static_assertions if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name static_assertions $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/static_assertions-0.3.1/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -156,7 +155,7 @@ CNAME=bit_field if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name bit_field $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/bit_field-0.9.0/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -171,7 +170,7 @@ CNAME=zero if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name zero $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/zero-0.1.2/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -186,7 +185,7 @@ CNAME=bit_vec if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name bit_vec $CARGO_PATH/git/checkouts/bit-vec-437fa4a002bd318d/9861a58*/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -201,7 +200,7 @@ CNAME=bitflags if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name bitflags $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/bitflags-1.0.4/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -213,11 +212,26 @@ rustc --crate-name bitflags $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823 gen_full_rlib fi +CNAME=tock_registers +if ! [[ -f outdir/${CNAME}.o ]] +then +rustc --edition=2018 --crate-name tock_registers $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/tock-registers-0.3.0/src/lib.rs \ + --color always --crate-type lib --emit=metadata,llvm-bc \ + -C opt-level=1 \ + -C debuginfo=2 \ + -C debug-assertions=on \ + --out-dir $PWD/outdir \ + --target $TARGET_JSON \ + -L $PWD/outdir \ + --cap-lints allow +gen_full_rlib +fi + CNAME=volatile if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name volatile $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/volatile-0.2.5/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -232,7 +246,7 @@ CNAME=once if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name once $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/once-0.3.3/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -247,7 +261,7 @@ CNAME=bbl if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name bbl $PWD/../crate/bbl/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -261,7 +275,7 @@ CNAME=log if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name log $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/log-0.4.6/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -276,7 +290,7 @@ CNAME=linked_list_allocator if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name linked_list_allocator $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/linked_list_allocator-0.6.3/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -294,7 +308,7 @@ CNAME=lazy_static if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name lazy_static $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/lazy_static-1.2.0/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -312,7 +326,7 @@ CNAME=xmas_elf if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name xmas_elf $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/xmas-elf-0.6.2/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -327,7 +341,7 @@ CNAME=bit_allocator if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name bit_allocator $PWD/../crate/bit-allocator/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -342,7 +356,7 @@ CNAME=simple_filesystem if ! [[ -f outdir/${CNAME}.o ]] then rustc --edition=2018 --crate-name simple_filesystem $CARGO_PATH/git/checkouts/simplefilesystem-rust-868ccb44dbeefdea/249383f*/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -360,7 +374,7 @@ CNAME=ucore_process if ! [[ -f outdir/${CNAME}.o ]] then rustc --edition=2018 --crate-name ucore_process $PWD/../crate/process/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -376,7 +390,7 @@ CNAME=ucore_memory if ! [[ -f outdir/${CNAME}.o ]] then rustc --edition=2018 --crate-name ucore_memory $PWD/../crate/memory/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -391,7 +405,7 @@ CNAME=semver if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name semver $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/semver-0.9.0/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -408,7 +422,7 @@ CNAME=rustc_version if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name rustc_version $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/rustc_version-0.2.3/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -424,7 +438,7 @@ CNAME=bare_metal if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name bare_metal $CARGO_PATH/registry/src/github.com-1ecc6299db9ec823/bare-metal-0.2.4/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -439,7 +453,7 @@ CNAME=riscv if ! [[ -f outdir/${CNAME}.o ]] then rustc --crate-name riscv $CARGO_PATH/git/checkouts/riscv-1e845b622ce46f1d/1f59ef7*/src/lib.rs \ - --color always --crate-type lib --emit=metadata,llvm-ir \ + --color always --crate-type lib --emit=metadata,llvm-bc \ -C opt-level=1 \ -C debuginfo=2 \ -C debug-assertions=on \ @@ -455,32 +469,40 @@ fi # Hand generate build.rs +if ! [[ -f outdir/libatomic_rt.a ]] +then ${CC} src/arch/riscv32/compiler_rt.c -march=rv64ia -mabi=lp64 -c -o outdir/compiler_rt.o ${AR} r outdir/libatomic_rt.a outdir/compiler_rt.o +fi +if ! [[ -f outdir/libsfsimg.a ]] +then cat >outdir/sfsimg.S < dump \ No newline at end of file diff --git a/kernel/src/arch/riscv32/boot/boot.asm b/kernel/src/arch/riscv32/boot/boot.asm deleted file mode 100644 index b2ca901..0000000 --- a/kernel/src/arch/riscv32/boot/boot.asm +++ /dev/null @@ -1,16 +0,0 @@ - .section .text.boot -boot: - csrwi 0x304, 0 # mie - csrwi 0x344, 0 # mip - csrwi 0x340, 0 # mscratch - csrwi 0x180, 0 # satp - li t0, -1 - csrw 0x302, t0 # medeleg - csrw 0x303, t0 # mideleg - csrw 0x306, t0 # mcounteren - csrw 0x106, t0 # scounteren - li t0, 1 << 11 # MPP = S - csrw 0x300, t0 # mstatus - lui t0, 0x80020 - csrw 0x341, t0 # mepc - mret \ No newline at end of file diff --git a/kernel/src/arch/riscv32/boot/boot_k210.asm b/kernel/src/arch/riscv32/boot/boot_k210.asm new file mode 100644 index 0000000..0518142 --- /dev/null +++ b/kernel/src/arch/riscv32/boot/boot_k210.asm @@ -0,0 +1,36 @@ + .section .text.boot +boot: + //lui x1, 0x40000 + //jalr x0, x1, 8 + + csrwi mie, 0 + csrwi mip, 0 + csrwi mscratch, 0 + csrwi medeleg, 0 + csrwi mideleg, 0 + csrwi mstatus, 0 + + // uart init + lui x1, 0x38000 + + li t0, 3384 + sw t0, 0x18(x1) + + li t0, 1 + sw t0, 0x8(x1) + sw t0, 0xc(x1) + + li t0, 3 + sw t0, 0x14(x1) + sw x0, 0x10(x1) + + +1: // test + lw t0, 0(x1) + blt t0, zero, 1b + // write + li t0, 0x21 + sw t0, 0(x1) + + csrr a0, mhartid + j _start \ No newline at end of file diff --git a/kernel/src/arch/riscv32/boot/linker64.ld b/kernel/src/arch/riscv32/boot/linker64.ld index f3a50d1..8d4165e 100644 --- a/kernel/src/arch/riscv32/boot/linker64.ld +++ b/kernel/src/arch/riscv32/boot/linker64.ld @@ -6,30 +6,25 @@ OUTPUT_ARCH(riscv) ENTRY(_start) -BASE_ADDRESS = 0x40020000; +BASE_ADDRESS = 0x40000000; SECTIONS { - . = 0x40000000; - .boot : { - KEEP(*(.text.boot)) - } - /* Load the kernel at this address: "." means the current address */ . = BASE_ADDRESS; start = .; .text : { stext = .; - *(.text.entry) - *(.text .stub .text.* .gnu.linkonce.t.*) + KEEP(*(.text.boot)) + *(.text .text.*) . = ALIGN(4K); etext = .; } .rodata : { srodata = .; - *(.rodata .rodata.* .gnu.linkonce.r.*) + *(.rodata .rodata.*) . = ALIGN(4K); erodata = .; } @@ -43,15 +38,10 @@ SECTIONS .bss : { sbss = .; - *(.bss .bss.* .sbss*) + *(.bss .bss.*) . = ALIGN(4K); ebss = .; } - .got : { - *(.got .got.*) - . = ALIGN(4K); - } - PROVIDE(end = .); } diff --git a/kernel/src/arch/riscv32/io.rs b/kernel/src/arch/riscv32/io.rs index b2c49d2..b297e9c 100644 --- a/kernel/src/arch/riscv32/io.rs +++ b/kernel/src/arch/riscv32/io.rs @@ -1,5 +1,4 @@ use core::fmt::{Write, Result, Arguments}; -use core::ptr::{read_volatile, write_volatile}; use bbl::sbi; struct SerialPort; @@ -20,10 +19,10 @@ impl Write for SerialPort { } fn putchar(c: u8) { - if cfg!(feature = "no_bbl") { + if cfg!(feature = "board_k210") { unsafe { - while read_volatile(STATUS) & CAN_WRITE == 0 {} - write_volatile(DATA, c as u8); + while TXDATA.read_volatile() & (1 << 31) != 0 {} + (TXDATA as *mut u8).write_volatile(c as u8); } } else if cfg!(feature = "m_mode") { (super::BBL.mcall_console_putchar)(c); @@ -33,10 +32,10 @@ fn putchar(c: u8) { } pub fn getchar() -> char { - let c = if cfg!(feature = "no_bbl") { + let c = if cfg!(feature = "board_k210") { unsafe { - // while read_volatile(STATUS) & CAN_READ == 0 {} - read_volatile(DATA) + while RXDATA.read_volatile() & (1 << 31) == 0 {} + (RXDATA as *const u8).read_volatile() } } else if cfg!(feature = "m_mode") { (super::BBL.mcall_console_getchar)() as u8 @@ -54,7 +53,5 @@ pub fn putfmt(fmt: Arguments) { SerialPort.write_fmt(fmt).unwrap(); } -const DATA: *mut u8 = 0x10000000 as *mut u8; -const STATUS: *const u8 = 0x10000005 as *const u8; -const CAN_READ: u8 = 1 << 0; -const CAN_WRITE: u8 = 1 << 5; +const TXDATA: *mut u32 = 0x38000000 as *mut u32; +const RXDATA: *mut u32 = 0x38000004 as *mut u32; diff --git a/kernel/src/arch/riscv32/mod.rs b/kernel/src/arch/riscv32/mod.rs index eb2a17a..1cf3cfb 100644 --- a/kernel/src/arch/riscv32/mod.rs +++ b/kernel/src/arch/riscv32/mod.rs @@ -57,8 +57,8 @@ global_asm!(" .macro XRET\n sret\n .endm "); -#[cfg(feature = "no_bbl")] -global_asm!(include_str!("boot/boot.asm")); +#[cfg(feature = "board_k210")] +global_asm!(include_str!("boot/boot_k210.asm")); global_asm!(include_str!("boot/entry.asm")); global_asm!(include_str!("boot/trap.asm")); diff --git a/kernel/src/logging.rs b/kernel/src/logging.rs index 86bd1c2..8bb2f97 100644 --- a/kernel/src/logging.rs +++ b/kernel/src/logging.rs @@ -44,13 +44,13 @@ macro_rules! with_color { fn print_in_color(args: fmt::Arguments, color: Color) { use crate::arch::io; - let mutex = log_mutex.lock(); +// let mutex = log_mutex.lock(); io::putfmt(with_color!(args, color)); } pub fn print(args: fmt::Arguments) { use crate::arch::io; - let mutex = log_mutex.lock(); +// let mutex = log_mutex.lock(); io::putfmt(args); }