From 8bb1e65ba706f2cbb09e30de4ed22c29c920982c Mon Sep 17 00:00:00 2001 From: equation314 Date: Wed, 6 Mar 2019 00:46:01 +0800 Subject: [PATCH] load kernel elf in bootloader --- bootloader/Cargo.lock | 213 +++++++++++++++++++ bootloader/Cargo.toml | 8 + bootloader/Makefile | 31 ++- bootloader/build.rs | 40 ++++ bootloader/src/arch/aarch64/boot.S | 20 +- bootloader/src/arch/aarch64/boot.ld | 24 +-- bootloader/src/arch/aarch64/mod.rs | 23 ++ bootloader/src/lang_items.rs | 7 + bootloader/src/main.rs | 54 ++++- kernel/Makefile | 18 +- kernel/build.rs | 138 ++++++------ kernel/src/arch/aarch64/{ => boot}/entry.S | 0 kernel/src/arch/aarch64/{ => boot}/linker.ld | 2 +- kernel/src/arch/aarch64/memory.rs | 1 + kernel/src/arch/aarch64/mod.rs | 2 +- kernel/targets/aarch64.json | 2 +- 16 files changed, 452 insertions(+), 131 deletions(-) create mode 100644 bootloader/build.rs create mode 100644 bootloader/src/lang_items.rs rename kernel/src/arch/aarch64/{ => boot}/entry.S (100%) rename kernel/src/arch/aarch64/{ => boot}/linker.ld (89%) diff --git a/bootloader/Cargo.lock b/bootloader/Cargo.lock index 505c019..42d8ed0 100644 --- a/bootloader/Cargo.lock +++ b/bootloader/Cargo.lock @@ -1,6 +1,219 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "aarch64" +version = "2.2.2" +source = "git+https://github.com/equation314/aarch64#b6a0f4a3be6f74927c88305a6af5ad2be079bccd" +dependencies = [ + "bit_field 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "register 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "usize_conversions 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ux 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bit_field" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bitflags" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cc" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "fixedvec" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "skeptic 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "getopts" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.50" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "pulldown-cmark" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rcore-bootloader" version = "0.1.0" +dependencies = [ + "aarch64 2.2.2 (git+https://github.com/equation314/aarch64)", + "cc 1.0.30 (registry+https://github.com/rust-lang/crates.io-index)", + "fixedvec 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "xmas-elf 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "register" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "tock-registers 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "remove_dir_all" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "skeptic" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "pulldown-cmark 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "tempdir" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "tock-registers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-width" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "usize_conversions" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "ux" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "xmas-elf" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "zero 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "zero" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +[metadata] +"checksum aarch64 2.2.2 (git+https://github.com/equation314/aarch64)" = "" +"checksum bit_field 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ed8765909f9009617974ab6b7d332625b320b33c326b1e9321382ef1999b5d56" +"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" +"checksum cc 1.0.30 (registry+https://github.com/rust-lang/crates.io-index)" = "d01c69d08ff207f231f07196e30f84c70f1c815b04f980f8b7b01ff01f05eb92" +"checksum fixedvec 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7c6c16d316ccdac21a4dd648e314e76facbbaf316e83ca137d0857a9c07419d0" +"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" +"checksum getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "0a7292d30132fb5424b354f5dc02512a86e4c516fe544bb7a25e7f266951b797" +"checksum libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)" = "aab692d7759f5cd8c859e169db98ae5b52c924add2af5fbbca11d12fefb567c1" +"checksum pulldown-cmark 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8361e81576d2e02643b04950e487ec172b687180da65c731c03cf336784e6c07" +"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +"checksum rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0e7a549d590831370895ab7ba4ea0c1b6b011d106b5ff2da6eee112615e6dc0" +"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +"checksum register 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e10f31b6d2299e5620986ad9fcdd66463e125ad72af4f403f9aedf7592d5ccdb" +"checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" +"checksum skeptic 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "061203a849117b0f7090baf8157aa91dac30545208fbb85166ac58b4ca33d89c" +"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" +"checksum tock-registers 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3a385d94f3f62e60445a0adb9ff8d9621faa272234530d4c0f848ec98f88e316" +"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" +"checksum usize_conversions 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f70329e2cbe45d6c97a5112daad40c34cd9a4e18edb5a2a18fefeb584d8d25e5" +"checksum ux 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "88dfeb711b61ce620c0cb6fd9f8e3e678622f0c971da2a63c4b3e25e88ed012f" +"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +"checksum xmas-elf 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "22678df5df766e8d1e5d609da69f0c3132d794edf6ab5e75e7abcd2270d4cf58" +"checksum zero 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5f1bc8a6b2005884962297587045002d8cfb8dcec9db332f4ca216ddc5de82c5" diff --git a/bootloader/Cargo.toml b/bootloader/Cargo.toml index dde8976..5a77dff 100644 --- a/bootloader/Cargo.toml +++ b/bootloader/Cargo.toml @@ -5,3 +5,11 @@ authors = ["equation314 "] edition = "2018" [dependencies] +xmas-elf = "0.6.2" +fixedvec = "0.2.3" + +[target.'cfg(target_arch = "aarch64")'.dependencies] +aarch64 = { git = "https://github.com/equation314/aarch64" } + +[build-dependencies] +cc = "1.0" diff --git a/bootloader/Makefile b/bootloader/Makefile index 0545bbd..e675023 100644 --- a/bootloader/Makefile +++ b/bootloader/Makefile @@ -1,9 +1,36 @@ arch ?= aarch64 mode ?= debug target := $(arch) +payload ?= bootloader := target/$(target)/$(mode)/rcore-bootloader +ifeq ($(arch), x86_64) +ifeq ($(uname), Darwin) +prefix := x86_64-elf- +endif +else ifeq ($(arch), riscv32) +prefix := riscv64-unknown-elf- +else ifeq ($(arch), riscv64) +prefix := riscv64-unknown-elf- +else ifeq ($(arch), aarch64) +prefix ?= aarch64-none-elf- +ifeq (,$(shell which $(prefix)ld)) + prefix := aarch64-elf- +endif +endif + +ld := $(prefix)ld +objdump := $(prefix)objdump +objcopy := $(prefix)objcopy +cc := $(prefix)gcc +as := $(prefix)as +gdb := $(prefix)gdb +strip := $(prefix)strip + +export CC = $(cc) +export PAYLOAD = $(payload) + build_args := --target=targets/$(arch).json ifeq ($(mode), release) build_args += --release @@ -11,9 +38,9 @@ endif .PHONY: all clean -all: $(bootloader) +all: bootloader -$(bootloader): +bootloader: $(payload) @cargo xbuild $(build_args) clean: diff --git a/bootloader/build.rs b/bootloader/build.rs new file mode 100644 index 0000000..b4e87e8 --- /dev/null +++ b/bootloader/build.rs @@ -0,0 +1,40 @@ +extern crate cc; + +use std::fs::File; +use std::io::{Result, Write}; +use std::path::Path; + +fn main() { + if let Ok(file_path) = gen_payload_asm() { + cc::Build::new().file(&file_path).compile("payload"); + } +} + +fn gen_payload_asm() -> Result { + let out_dir = std::env::var("OUT_DIR").unwrap(); + let payload = std::env::var("PAYLOAD").unwrap(); + + if !Path::new(&payload).is_file() { + panic!("Kernel payload `{}` not found", payload) + } + + let file_path = Path::new(&out_dir).join("payload.S"); + let mut f = File::create(&file_path).unwrap(); + + println!("{:x?} {:x?}", payload, file_path); + + write!(f, "# generated by build.rs - do not edit")?; + write!(f, r#" + .section .rodata + .align 12 + .global _kernel_payload_start, _kernel_payload_end +_kernel_payload_start: + .incbin "{}" +_kernel_payload_end: +"#, payload)?; + + println!("cargo:rerun-if-changed={}", payload); + println!("cargo:rerun-if-env-changed=PAYLOAD"); + + Ok(file_path) +} diff --git a/bootloader/src/arch/aarch64/boot.S b/bootloader/src/arch/aarch64/boot.S index 4f66df3..dc393c7 100644 --- a/bootloader/src/arch/aarch64/boot.S +++ b/bootloader/src/arch/aarch64/boot.S @@ -1,5 +1,3 @@ -# TODO rewrite in Rust, use crate cortex-a - .section .text.boot .globl _start @@ -92,20 +90,6 @@ set_stack: # set the current stack pointer mov sp, x1 -zero_bss: - # load the start address and number of bytes in BSS section - ldr x1, =sbss - ldr x2, =__bss_length - -zero_bss_loop: - # zero out the BSS section, 64-bits at a time - cbz x2, zero_bss_loop_end - str xzr, [x1], #8 - sub x2, x2, #8 - cbnz x2, zero_bss_loop - -zero_bss_loop_end: - # jump to rust_main, which shouldn't return. halt if it does - mov x9, #0x100000 - blr x9 +goto_main: + bl boot_main b halt diff --git a/bootloader/src/arch/aarch64/boot.ld b/bootloader/src/arch/aarch64/boot.ld index 57e2c4b..7c9b8cb 100644 --- a/bootloader/src/arch/aarch64/boot.ld +++ b/bootloader/src/arch/aarch64/boot.ld @@ -4,42 +4,20 @@ SECTIONS { . = 0x80000; /* Raspbery Pi 3 AArch64 (kernel8.img) load address */ .text : { - stext = .; KEEP(*(.text.boot)) /* from boot.S */ *(.text .text.* .gnu.linkonce.t*) . = ALIGN(4K); - etext = .; } .rodata : { - srodata = .; *(.rodata .rodata.* .gnu.linkonce.r*) . = ALIGN(4K); - erodata = .; } - .data : { - sdata = .; + .data : {= *(.data .data.* .gnu.linkonce.d*) . = ALIGN(4K); - edata = .; } - .bss (NOLOAD) : { - . = ALIGN(32); - sbss = .; - *(.bss .bss.*) - *(COMMON) - . = ALIGN(4K); - ebss = .; - } - - /* end of the binary */ - _end = ALIGN(8); - - /* number of bytes in BSS section and complete binary */ - __bss_length = (ebss - sbss); - __binary_length = (_end - _start); - /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } } diff --git a/bootloader/src/arch/aarch64/mod.rs b/bootloader/src/arch/aarch64/mod.rs index 8ffaf76..6eafc58 100644 --- a/bootloader/src/arch/aarch64/mod.rs +++ b/bootloader/src/arch/aarch64/mod.rs @@ -1 +1,24 @@ +use core::slice; +use fixedvec::FixedVec; +use xmas_elf::program::{ProgramHeader64, Type}; + global_asm!(include_str!("boot.S")); + +pub fn map_kernel(kernel_start: u64, segments: &FixedVec) { + for segment in segments { + if segment.get_type() != Ok(Type::Load) { + continue; + } + let virt_addr = segment.virtual_addr; + let offset = segment.offset; + let file_size = segment.file_size as usize; + let mem_size = segment.mem_size as usize; + + unsafe { + let target = slice::from_raw_parts_mut(virt_addr as *mut u8, mem_size); + let source = slice::from_raw_parts((kernel_start + offset) as *const u8, file_size); + target.copy_from_slice(source); + target[file_size..].iter_mut().for_each(|x| *x = 0); + } + } +} diff --git a/bootloader/src/lang_items.rs b/bootloader/src/lang_items.rs new file mode 100644 index 0000000..52e5deb --- /dev/null +++ b/bootloader/src/lang_items.rs @@ -0,0 +1,7 @@ +use core::panic::PanicInfo; + +#[panic_handler] +#[no_mangle] +pub extern "C" fn panic(_info: &PanicInfo) -> ! { + loop {} +} diff --git a/bootloader/src/main.rs b/bootloader/src/main.rs index 46acd38..2c80a39 100644 --- a/bootloader/src/main.rs +++ b/bootloader/src/main.rs @@ -1,15 +1,57 @@ #![no_std] #![no_main] +#![feature(lang_items)] #![feature(global_asm)] -use core::panic::PanicInfo; +#[macro_use] +extern crate fixedvec; +extern crate xmas_elf; -#[panic_handler] -#[no_mangle] -pub extern "C" fn panic(_info: &PanicInfo) -> ! { - loop {} -} +use core::mem::transmute; +use core::slice; +use fixedvec::FixedVec; +use xmas_elf::{ + header, + program::{ProgramHeader, ProgramHeader64}, + ElfFile, +}; #[cfg(target_arch = "aarch64")] #[path = "arch/aarch64/mod.rs"] pub mod arch; +pub mod lang_items; + +extern "C" { + fn _kernel_payload_start(); + fn _kernel_payload_end(); +} + +/// The entry point of bootloader +#[no_mangle] // don't mangle the name of this function +pub extern "C" fn boot_main() -> ! { + let kernel_size = _kernel_payload_end as usize - _kernel_payload_start as usize; + let kernel = unsafe { slice::from_raw_parts(_kernel_payload_start as *const u8, kernel_size) }; + let kernel_elf = ElfFile::new(kernel).unwrap(); + header::sanity_check(&kernel_elf).unwrap(); + + let mut preallocated_space = alloc_stack!([ProgramHeader64; 32]); + let mut segments = FixedVec::new(&mut preallocated_space); + + for program_header in kernel_elf.program_iter() { + match program_header { + ProgramHeader::Ph64(header) => segments + .push(*header) + .expect("does not support more than 32 program segments"), + ProgramHeader::Ph32(_) => panic!("does not support 32 bit elf files"), + } + } + + arch::map_kernel(_kernel_payload_start as u64, &segments); + + let entry = kernel_elf.header.pt2.entry_point(); + let kernel_main: extern "C" fn() = unsafe { transmute(entry) }; + + kernel_main(); + + loop {} +} diff --git a/kernel/Makefile b/kernel/Makefile index 14963de..0c2a118 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -32,8 +32,6 @@ m_mode ?= target := $(arch) kernel := target/$(target)/$(mode)/rcore -boot_bin := target/$(target)/$(mode)/boot.bin -kernel_bin := target/$(target)/$(mode)/kernel.bin kernel_img := target/$(target)/$(mode)/kernel.img bootimage := target/$(target)/bootimage.bin bootloader_dir = ../bootloader @@ -173,7 +171,8 @@ objcopy := $(prefix)objcopy cc := $(prefix)gcc as := $(prefix)as gdb := $(prefix)gdb -export CC=$(cc) +strip := $(prefix)strip +export CC = $(cc) .PHONY: all clean run build asm doc justrun debug kernel sfsimg install runnet @@ -218,13 +217,15 @@ header: sym: @$(objdump) -t $(kernel) | less -$(bootloader): +$(bootloader): $(kernel) ifeq ($(arch), aarch64) @echo Building $(arch) bootloader - cd $(bootloader_dir) && make arch=$(arch) mode=$(mode) + @$(strip) $(kernel) -o $(kernel)_stripped + @cd $(bootloader_dir) && make arch=$(arch) mode=$(mode) payload=../kernel/$(kernel)_stripped + @rm $(kernel)_stripped endif -$(kernel_img): $(bootloader) kernel +$(kernel_img): kernel $(bootloader) ifeq ($(arch), riscv32) @mkdir -p target/$(target)/bbl && \ cd target/$(target)/bbl && \ @@ -252,10 +253,7 @@ else cp bbl $(abspath $@) endif else ifeq ($(arch), aarch64) - @$(objcopy) $(bootloader) --strip-all -O binary $(boot_bin) - @$(objcopy) $(kernel) --strip-all -O binary $(kernel_bin) - dd if=$(boot_bin) of=$@ conv=notrunc - dd if=$(kernel_bin) of=$@ seek=1024 conv=notrunc + @$(objcopy) $(bootloader) --strip-all -O binary $@ endif kernel: diff --git a/kernel/build.rs b/kernel/build.rs index 7573489..fd3864b 100644 --- a/kernel/build.rs +++ b/kernel/build.rs @@ -5,90 +5,90 @@ use std::path::Path; use std::io::{Result, Write}; fn main() { - println!("cargo:rerun-if-env-changed=LOG"); - println!("cargo:rerun-if-env-changed=BOARD"); + println!("cargo:rerun-if-env-changed=LOG"); + println!("cargo:rerun-if-env-changed=BOARD"); - let arch: String = std::env::var("ARCH").unwrap(); - let board: String = std::env::var("BOARD").unwrap(); - match arch.as_str() { - "x86_64" => { - gen_vector_asm().unwrap(); - } - "riscv32" => { - if let Ok(file_path) = gen_sfsimg_asm() { - cc::Build::new() - .file(&file_path) - .flag("-march=rv32imac") - .flag("-mabi=ilp32") - .compile("sfsimg"); - } - } - "riscv64" => { - if let Ok(file_path) = gen_sfsimg_asm() { - cc::Build::new() - .file(&file_path) - .flag("-march=rv64imac") - .flag("-mabi=lp64") - .compile("sfsimg"); - } - if board == "k210" { - println!("cargo:rustc-link-search=native={}", "../tools/k210"); - println!("cargo:rustc-link-lib=static=kendryte"); - } - } - "aarch64" => { - if let Ok(file_path) = gen_sfsimg_asm() { - cc::Build::new().file(&file_path).compile("sfsimg"); - } - } - _ => panic!("Unknown arch {}", arch), - } + let arch: String = std::env::var("ARCH").unwrap(); + let board: String = std::env::var("BOARD").unwrap(); + match arch.as_str() { + "x86_64" => { + gen_vector_asm().unwrap(); + } + "riscv32" => { + if let Ok(file_path) = gen_sfsimg_asm() { + cc::Build::new() + .file(&file_path) + .flag("-march=rv32imac") + .flag("-mabi=ilp32") + .compile("sfsimg"); + } + } + "riscv64" => { + if let Ok(file_path) = gen_sfsimg_asm() { + cc::Build::new() + .file(&file_path) + .flag("-march=rv64imac") + .flag("-mabi=lp64") + .compile("sfsimg"); + } + if board == "k210" { + println!("cargo:rustc-link-search=native={}", "../tools/k210"); + println!("cargo:rustc-link-lib=static=kendryte"); + } + } + "aarch64" => { + if let Ok(file_path) = gen_sfsimg_asm() { + cc::Build::new().file(&file_path).compile("sfsimg"); + } + } + _ => panic!("Unknown arch {}", arch), + } } fn gen_vector_asm() -> Result<()> { - let mut f = File::create("src/arch/x86_64/interrupt/vector.asm").unwrap(); + let mut f = File::create("src/arch/x86_64/interrupt/vector.asm").unwrap(); - writeln!(f, "# generated by build.rs - do not edit")?; - writeln!(f, ".section .text")?; - writeln!(f, ".intel_syntax noprefix")?; - for i in 0..256 { - writeln!(f, "vector{}:", i)?; - if !(i == 8 || (i >= 10 && i <= 14) || i == 17) { - writeln!(f, "\tpush 0")?; - } - writeln!(f, "\tpush {}", i)?; - writeln!(f, "\tjmp __alltraps")?; - } + writeln!(f, "# generated by build.rs - do not edit")?; + writeln!(f, ".section .text")?; + writeln!(f, ".intel_syntax noprefix")?; + for i in 0..256 { + writeln!(f, "vector{}:", i)?; + if !(i == 8 || (i >= 10 && i <= 14) || i == 17) { + writeln!(f, "\tpush 0")?; + } + writeln!(f, "\tpush {}", i)?; + writeln!(f, "\tjmp __alltraps")?; + } - writeln!(f, "\n.section .rodata")?; - writeln!(f, ".global __vectors")?; - writeln!(f, "__vectors:")?; - for i in 0..256 { - writeln!(f, "\t.quad vector{}", i)?; - } - Ok(()) + writeln!(f, "\n.section .rodata")?; + writeln!(f, ".global __vectors")?; + writeln!(f, "__vectors:")?; + for i in 0..256 { + writeln!(f, "\t.quad vector{}", i)?; + } + Ok(()) } fn gen_sfsimg_asm() -> Result { - let out_dir = std::env::var("OUT_DIR").unwrap(); - let sfsimg = std::env::var("SFSIMG").unwrap(); + let out_dir = std::env::var("OUT_DIR").unwrap(); + let sfsimg = std::env::var("SFSIMG").unwrap(); - let file_path = Path::new(&out_dir).join("sfsimg.S"); - let mut f = File::create(&file_path).unwrap(); + let file_path = Path::new(&out_dir).join("sfsimg.S"); + let mut f = File::create(&file_path).unwrap(); - write!(f, "# generated by build.rs - do not edit")?; - write!(f, r#" - .section .rodata - .align 12 - .global _user_img_start - .global _user_img_end + write!(f, "# generated by build.rs - do not edit")?; + write!(f, r#" + .section .rodata + .align 12 + .global _user_img_start + .global _user_img_end _user_img_start: .incbin "{}" _user_img_end: "#, sfsimg)?; - println!("cargo:rerun-if-changed={}", sfsimg); - println!("cargo:rerun-if-env-changed=SFSIMG"); + println!("cargo:rerun-if-changed={}", sfsimg); + println!("cargo:rerun-if-env-changed=SFSIMG"); - Ok(file_path) + Ok(file_path) } diff --git a/kernel/src/arch/aarch64/entry.S b/kernel/src/arch/aarch64/boot/entry.S similarity index 100% rename from kernel/src/arch/aarch64/entry.S rename to kernel/src/arch/aarch64/boot/entry.S diff --git a/kernel/src/arch/aarch64/linker.ld b/kernel/src/arch/aarch64/boot/linker.ld similarity index 89% rename from kernel/src/arch/aarch64/linker.ld rename to kernel/src/arch/aarch64/boot/linker.ld index ef4c7dd..399f3fa 100644 --- a/kernel/src/arch/aarch64/linker.ld +++ b/kernel/src/arch/aarch64/boot/linker.ld @@ -1,7 +1,7 @@ ENTRY(_start) SECTIONS { - . = 0x100000; /* Load the kernel at this address. It's also kernel stack top address */ + . = 0x2000000; /* Load the kernel at this address. It's also kernel stack top address */ bootstacktop = .; .text : { diff --git a/kernel/src/arch/aarch64/memory.rs b/kernel/src/arch/aarch64/memory.rs index f4b7cc0..c2a1b87 100644 --- a/kernel/src/arch/aarch64/memory.rs +++ b/kernel/src/arch/aarch64/memory.rs @@ -112,6 +112,7 @@ fn remap_the_kernel() { use super::board::{IO_REMAP_BASE, IO_REMAP_END}; ms.push(IO_REMAP_BASE, IO_REMAP_END, Linear::new(0, MemoryAttr::default().mmio(MMIOType::Device as u8)), "io_remap"); + info!("{:#x?}", ms); unsafe { ms.get_page_table_mut().activate_as_kernel() } unsafe { KERNEL_MEMORY_SET = Some(ms) } info!("kernel remap end"); diff --git a/kernel/src/arch/aarch64/mod.rs b/kernel/src/arch/aarch64/mod.rs index 4346ed3..370e1c0 100644 --- a/kernel/src/arch/aarch64/mod.rs +++ b/kernel/src/arch/aarch64/mod.rs @@ -12,7 +12,7 @@ pub mod driver; #[path = "board/raspi3/mod.rs"] pub mod board; -global_asm!(include_str!("entry.S")); +global_asm!(include_str!("boot/entry.S")); /// The entry point of kernel #[no_mangle] // don't mangle the name of this function diff --git a/kernel/targets/aarch64.json b/kernel/targets/aarch64.json index 02cbe21..149246a 100644 --- a/kernel/targets/aarch64.json +++ b/kernel/targets/aarch64.json @@ -15,7 +15,7 @@ "linker-is-gnu": true, "pre-link-args": { "ld.lld": [ - "-Tsrc/arch/aarch64/linker.ld" + "-Tsrc/arch/aarch64/boot/linker.ld" ] }, "llvm-target": "aarch64-unknown-none",