Skip to content

Commit

Permalink
feat: add more support for vf2
Browse files Browse the repository at this point in the history
  • Loading branch information
Godones committed Jul 29, 2023
1 parent 0dbb5b0 commit 4b10c82
Show file tree
Hide file tree
Showing 32 changed files with 322 additions and 134 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ oscomp-debian
tools/siglibc
tools/testsuits-for-oskernel
tools/bash-5.1.16
alien*
alien-*
25 changes: 20 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ FEATURES :=
QEMU_ARGS :=
MEMORY_SIZE := 128M
img ?=fat32
SLAB ?=n
TALLOC ?=y
BUDDY ?=n

comma:= ,
empty:=
space:= $(empty) $(empty)

ifeq ($(GUI),y)
QEMU_ARGS += -device virtio-gpu-device \
Expand All @@ -40,7 +46,16 @@ else
FEATURES += qemu
endif

ifeq ($(SLAB),y)
FEATURES += slab
else ifeq ($(TALLOC),y)
FEATURES += talloc
else ifeq ($(BUDDY),y)
FEATURES += buddy
endif


FEATURES := $(subst $(space),$(comma),$(FEATURES))

define boot_qemu
qemu-system-riscv64 \
Expand All @@ -64,11 +79,11 @@ build:compile


compile:
@cargo build --release -p boot --target riscv64gc-unknown-none-elf --features $(FEATURES)
cargo build --release -p boot --target riscv64gc-unknown-none-elf --features $(FEATURES)
@(nm -n ${KERNEL_FILE} | $(TRACE_EXE) > kernel/src/trace/kernel_symbol.S)
@#call trace_info
@cargo build --release -p boot --target riscv64gc-unknown-none-elf --features $(FEATURES)
@$(OBJCOPY) $(KERNEL_FILE) --strip-all -O binary $(KERNEL_BIN)
cargo build --release -p boot --target riscv64gc-unknown-none-elf --features $(FEATURES)
@#$(OBJCOPY) $(KERNEL_FILE) --strip-all -O binary $(KERNEL_BIN)
@cp $(KERNEL_FILE) ./kernel-qemu

trace_info:
Expand Down Expand Up @@ -97,13 +112,13 @@ board:install compile

vf2:board
@mkimage -f ./tools/vf2.its ./alien-vf2.itb
@rm ./alien.bin
@#rm ./alien.bin
@cp ./alien-vf2.itb /home/godones/projects/tftpboot/


cv1811h:board
@mkimage -f ./tools/cv1811h.its ./alien-cv1811h.itb
@rm ./alien.bin
@#rm ./alien.bin
@cp ./alien-cv1811h.itb /home/godones/projects/tftpboot/


Expand Down
Binary file added alien.bin
Binary file not shown.
16 changes: 10 additions & 6 deletions boot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
kernel = { path = "../kernel" }
cfg-if = "1.0.0"
riscv = "0.10.0"
preprint = "0.1.0"
basemachine = { path = "../modules/basemachine" }
kernel = { version = "0.1.0", path = "../kernel" }


[features]
default = ["qemu"]
vf2 = []
cv1811h = []
qemu = []
default = []
vf2 = ["kernel/vf2"]
cv1811h = ["kernel/cv1811h"]
qemu = ["kernel/qemu"]
sivife = ["kernel/sifive"]

slab = ["kernel/slab"]
talloc = ["kernel/talloc"]
buddy = ["kernel/buddy"]

[build-dependencies]
[build-dependencies]
6 changes: 3 additions & 3 deletions boot/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{env, fs};
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::{env, fs};

fn main() {
// 指定target
Expand All @@ -13,9 +13,9 @@ fn main() {
let ld = fs::read_to_string(ld_path).unwrap();

#[cfg(not(feature = "vf2"))]
let base_addr = 0x80200000usize;
let base_addr = 0x80200000usize;
#[cfg(feature = "vf2")]
let base_addr = 0x40200000;
let base_addr: usize = 0x80200000;
let base_addr = format!("BASE_ADDRESS = {};", base_addr);
let mut new_config = String::new();
for line in ld.lines() {
Expand Down
52 changes: 43 additions & 9 deletions boot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@
#![no_main]
#![feature(naked_functions)]
#![feature(asm_const)]
#![feature(stmt_expr_attributes)]

use core::arch::asm;
use core::hint::spin_loop;
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

use cfg_if::cfg_if;

use basemachine::machine_info_from_dtb;
use kernel::{config, init_machine_info, println, syscall, task, thread_local_init, timer, trap};
use kernel::arch::hart_id;
#[cfg(not(feature = "qemu"))]
use kernel::board;
use kernel::config::{CPU_NUM, STACK_SIZE};
use kernel::fs::vfs::init_vfs;
use kernel::memory::{init_memory_system, kernel_info};
use kernel::print::init_print;
use kernel::sbi::hart_start;
use kernel::task::init_per_cpu;
use kernel::trap::set_kernel_trap_entry;
use kernel::{
config, driver, init_machine_info, println, syscall, task, thread_local_init, timer, trap,
};

// 多核启动标志
static STARTED: AtomicBool = AtomicBool::new(false);
static CPUS: AtomicUsize = AtomicUsize::new(0);

#[inline]
fn clear_bss() {
extern "C" {
fn sbss();
Expand All @@ -44,6 +48,9 @@ extern "C" fn _start() {
unsafe {
asm!("\
mv tp, a0
csrw sscratch, a1
csrci sstatus, 0x02
csrw sie, zero
add t0, a0, 1
slli t0, t0, 13
la sp, {boot_stack}
Expand All @@ -56,41 +63,68 @@ extern "C" fn _start() {
}
}

#[inline]
fn device_tree_addr() -> usize {
let mut res: usize;
unsafe {
asm!(
" csrr {}, sscratch",
out(reg) res,
)
}
res
}

/// rust_main is the entry of the kernel
#[no_mangle]
fn main(hart_id: usize, device_tree_addr: usize) -> ! {
set_kernel_trap_entry();
extern "C" fn main(_: usize, _: usize) -> ! {
if !STARTED.load(Ordering::Relaxed) {
// this will clear the kernel stack, so if we want get hartid or device_tree_addr,
// clear_bss will cause error using vf2
clear_bss();
println!("{}", config::FLAG);
let mut device_tree_addr = device_tree_addr();
#[cfg(feature = "vf2")]
device_tree_addr = board::FDT.as_ptr() as usize;
init_print();
println!("boot hart id: {}, device tree addr: {:#x}", hart_id(), device_tree_addr);
let machine_info = machine_info_from_dtb(device_tree_addr);
println!("{:#x?}", machine_info);
init_machine_info(machine_info.clone());
kernel_info(machine_info.memory.end);
init_print();
init_memory_system(machine_info.memory.end, true);
thread_local_init();
#[cfg(feature = "qemu")]
driver::init_dt(device_tree_addr);
trap::init_trap_subsystem();
init_per_cpu();
cfg_if! {
if #[cfg(not(feature = "qemu"))]{
board::checkout_fs_img();
use kernel::driver::init_fake_disk;
init_fake_disk();
}
}
init_vfs();
syscall::register_all_syscall();
println!("register syscall success");
task::init_process();
CPUS.fetch_add(1, Ordering::Release);
STARTED.store(true, Ordering::Relaxed);
init_other_hart(hart_id);
init_other_hart(hart_id());
} else {
while !STARTED.load(Ordering::Relaxed) {
spin_loop();
}
thread_local_init();
println!("hart {:#x} start", kernel::arch::hart_id());
println!("hart {:#x} start", hart_id());
init_memory_system(0, false);
thread_local_init();
trap::init_trap_subsystem();
CPUS.fetch_add(1, Ordering::Release);
}
timer::set_next_trigger();
println!("begin run task...");
task::schedule::first_into_user();
}

Expand Down
Binary file modified kernel-qemu
Binary file not shown.
4 changes: 3 additions & 1 deletion kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ talc = { version = "1.0", optional = true }
buddy_system_allocator = { version = "0.9.0", optional = true }

[features]
default = ["talloc"]
default = []
vf2 = []
cv1811h = []
sifive = []
qemu = []

slab = []
talloc = ["talc"]
buddy = ["buddy_system_allocator"]
4 changes: 3 additions & 1 deletion kernel/src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use self::riscv::sstatus;
pub mod riscv;

pub fn hart_id() -> usize {
let id: usize;
let mut id: usize;
unsafe {
asm!(
"mv {},tp", out(reg)id,
);
}
#[cfg(any(feature = "vf2", feature = "sifive"))]
id -= 1;
id
}

Expand Down
3 changes: 3 additions & 0 deletions kernel/src/arch/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub mod sstatus {
SPP::User
}
}
pub fn set_spie(&mut self) {
self.0.set_bit(5, true);
}
pub fn sie(&self) -> bool {
self.0.get_bit(1)
}
Expand Down
35 changes: 35 additions & 0 deletions kernel/src/board/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,37 @@
use core::arch::global_asm;

#[cfg(feature = "cv1811")]
pub use cv1811::*;
#[cfg(feature = "vf2")]
pub use vf2::*;

mod cv1811;
mod vf2;


// pub static FAT32_IMG: &[u8] = include_bytes!("../../../tools/sdcard.img");
#[cfg(any(feature = "vf2", feature = "cv1811h"))]
global_asm!(
r#"
.section .data
.global img_start
.global img_end
.align 12
img_start:
.incbin "./tools/sdcard.img"
img_end:
"#
);

#[cfg(any(feature = "vf2", feature = "cv1811h"))]
extern "C" {
pub fn img_start();
pub fn img_end();
}

pub fn checkout_fs_img() {
let img_start = img_start as usize;
let img_end = img_end as usize;
let img_size = img_end - img_start;
println!("img_start: {:#x}, img_end: {:#x}, img_size: {:#x}", img_start, img_end, img_size);
}
4 changes: 4 additions & 0 deletions kernel/src/board/vf2.rs
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
#[repr(align(4))]
struct _Wrapper<T>(T);

pub const FDT: &[u8] = &_Wrapper(*include_bytes!("../../../tools/jh7110-visionfive-v2.dtb")).0;

15 changes: 12 additions & 3 deletions kernel/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ pub const FLAG: &str = r"
/ ___ \ | | | | | __/ | | | |
/_/ \_\ |_| |_| \___| |_| |_|
";
#[cfg(not(feature = "vf2"))]
#[cfg(feature = "qemu")]
pub const CLOCK_FREQ: usize = 12500000;
#[cfg(feature = "vf2")]
pub const CLOCK_FREQ: usize = 4000000;
pub const CLOCK_FREQ: usize = 400_0000;

#[cfg(feature = "sifive")]
pub const CLOCK_FREQ: usize = 100_0000;
Expand All @@ -22,8 +22,9 @@ pub const STACK_SIZE: usize = 1024 * 8;
pub const STACK_SIZE_BITS: usize = 16;

pub const TIMER_FREQ: usize = CLOCK_FREQ;
pub const CPU_NUM: usize = 4;
pub const CPU_NUM: usize = 1;

#[cfg(feature = "qemu")]
pub const MMIO: &[(usize, usize)] = &[
(0x0010_0000, 0x00_2000), // VIRT_TEST/RTC in virt machine
(0x2000000, 0x10000),
Expand All @@ -32,6 +33,14 @@ pub const MMIO: &[(usize, usize)] = &[
(0x3000_0000, 0x1000_0000),
];


#[cfg(feature = "vf2")]
pub const MMIO: &[(usize, usize)] = &[
(0x17040000, 0x10000), // RTC
(0xc000000, 0x4000000), //PLIC
(0x00_1000_0000, 0x10000) // UART
];

pub const FRAME_MAX_ORDER: usize = 16;

// todo!(if the app linker script changed, this should be changed too)
Expand Down
Loading

0 comments on commit 4b10c82

Please sign in to comment.