Skip to content

Commit

Permalink
feat: add initrd for vf2
Browse files Browse the repository at this point in the history
  • Loading branch information
Godones committed Feb 27, 2024
1 parent e08b422 commit 52f095b
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ fat:
@mkfs.fat -F 32 $(IMG)

ext:
$if [ `ls -l $(IMG) | awk '{print $$5}' ` -lt 2147483648 ]; then \
@if [ `ls -l $(IMG) | awk '{print $$5}' ` -lt 2147483648 ]; then \
echo "resize img to 2G"; \
dd if=/dev/zero bs=1M count=2048 >> $(IMG); \
fi
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ A simple operating system implemented in rust. The purpose is to explore how to

1. install qemu 7.0.0
2. install rust nightly
3. install riscv64-linux-musl [toolchain](https://musl.cc/)

```
make help
Expand Down
2 changes: 1 addition & 1 deletion kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ netcore = { git = "https://github.com/os-module/simple-net" }
default = ["smp","test"]
qemu = ["platform/qemu_riscv","interrupt/qemu","mem/initrd"]
vf2 = ["platform/vf2","interrupt/vf2", "devices/vf2","mem/initrd"]
hifive = ["platform/hifive","interrupt/hifive"]
hifive = ["platform/hifive","interrupt/hifive","devices/hifive"]
smp = ["platform/smp"]

fat = ["vfs/fat"]
Expand Down
1 change: 1 addition & 0 deletions subsystems/devices/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ default = ["test"]
ramdisk = []
test = []
vf2 = [] # enable to probe vf2's sdcard
hifive = []
85 changes: 41 additions & 44 deletions subsystems/devices/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ use fdt::Fdt;
pub use gpu::{GPUDevice, GPU_DEVICE};
pub use input::{INPUTDevice, KEYBOARD_INPUT_DEVICE, MOUSE_INPUT_DEVICE};
use interrupt::register_device_to_plic;
use ksync::Mutex;
use log::info;
use platform::println;
pub use rtc::{RTCDevice, RTC_DEVICE};
use spin::{Lazy, Once};
use spin::Once;
pub use uart::{UARTDevice, UART_DEVICE};
use virtio_drivers::transport::mmio::{MmioTransport, VirtIOHeader};
use virtio_drivers::transport::{DeviceType, Transport};
Expand All @@ -41,8 +40,6 @@ pub struct DeviceInfo {
pub need_register: bool,
}

pub static INITIALIZED_DEVICES: Lazy<Mutex<Vec<DeviceInfo>>> = Lazy::new(|| Mutex::new(Vec::new()));

static TASK_FUNC: Once<Box<dyn DeviceWithTask>> = Once::new();

pub trait DeviceWithTask: Send + Sync {
Expand Down Expand Up @@ -122,12 +119,37 @@ pub fn init_device(task_func: Box<dyn DeviceWithTask>) {
}
}

match dtb.probe_virtio() {
Some(virtio_mmio_devices) => init_virtio_mmio(virtio_mmio_devices),
#[cfg(not(all(feature = "vf2", feature = "hifive")))]
{
match dtb.probe_virtio() {
Some(virtio_mmio_devices) => init_virtio_mmio(virtio_mmio_devices),
None => {
println!("There is no virtio-mmio device");
}
}
}

#[cfg(feature = "vf2")]
match dtb.probe_sdio() {
Some(sdio) => init_block_device(sdio, None),
None => {
println!("There is no virtio-mmio device");
panic!("There is no sdio device");
}
}

#[cfg(feature = "hifive")]
init_block_device(
prob::DeviceInfo::new(
alloc::string::String::new(),
0,
0,
alloc::string::String::new(),
),
None,
);

#[cfg(any(feature = "hifive", feature = "vf2"))]
init_net(None);
}

fn init_rtc(rtc: prob::DeviceInfo) {
Expand All @@ -142,11 +164,6 @@ fn init_rtc(rtc: prob::DeviceInfo) {
let current_time = rtc.read_time_string();
rtc::init_rtc(rtc.clone());
register_device_to_plic(info.irq, rtc);
// INITIALIZED_DEVICES.lock().push(DeviceInfo {
// device: rtc,
// irq: info.irq,
// need_register: true,
// });
println!("Init rtc success, current time: {:?}", current_time);
}
name => {
Expand All @@ -165,27 +182,16 @@ fn init_uart(uart: prob::DeviceInfo) {
let uart = Arc::new(Uart::new(Box::new(uart)));
uart::init_uart(uart.clone());
register_device_to_plic(irq, uart);
// INITIALIZED_DEVICES.lock().push(DeviceInfo {
// device: uart,
// irq,
// need_register: true,
// });
}
"snps,dw-apb-uart" => {
// vf2
let uart = Uart8250::new(base_addr);
let uart = Arc::new(Uart::new(Box::new(uart)));
uart::init_uart(uart.clone());
register_device_to_plic(irq, uart);
// INITIALIZED_DEVICES.lock().push(DeviceInfo {
// device: uart,
// irq,
// need_register: true,
// });
}
name => {
println!("Don't support uart: {}", name);
return;
panic!("Don't support uart: {}", name);
}
}
println!("Init uart success");
Expand Down Expand Up @@ -221,7 +227,7 @@ pub fn init_virtio_mmio(devices: Vec<prob::DeviceInfo>) {
}
DeviceType::Block => init_block_device(device, Some(transport)),
DeviceType::GPU => init_gpu(device, Some(transport)),
DeviceType::Network => init_net(device),
DeviceType::Network => init_net(Some(device)),
ty => {
println!("Don't support virtio device type: {:?}", ty);
}
Expand All @@ -238,10 +244,7 @@ static RAMDISK: &'static [u8] = include_bytes!("../../../tools/sdcard.img");
pub fn checkout_fs_img() {
let img_start = RAMDISK.as_ptr() as usize;
let img_size = RAMDISK.len();
println!(
"img_start: {:#x}, img_size: {:#x}",
img_start, img_size
);
println!("img_start: {:#x}, img_size: {:#x}", img_start, img_size);
}

fn init_block_device(blk: prob::DeviceInfo, mmio_transport: Option<MmioTransport>) {
Expand All @@ -261,7 +264,7 @@ fn init_block_device(blk: prob::DeviceInfo, mmio_transport: Option<MmioTransport
println!("Init block device success");
}
"starfive,jh7110-sdio" => {
// visionfi ve2/starfive2
// starfive2
#[cfg(not(feature = "ramdisk"))]
{
use arch::read_timer;
Expand Down Expand Up @@ -302,9 +305,8 @@ fn init_block_device(blk: prob::DeviceInfo, mmio_transport: Option<MmioTransport
fn init_ramdisk() {
use drivers::block_device::MemoryFat32Img;
checkout_fs_img();
let data = unsafe {
core::slice::from_raw_parts_mut(RAMDISK.as_ptr() as *mut u8, RAMDISK.len())
};
let data =
unsafe { core::slice::from_raw_parts_mut(RAMDISK.as_ptr() as *mut u8, RAMDISK.len()) };
let block_device = GenericBlockDevice::new(Box::new(MemoryFat32Img::new(data)));
let block_device = Arc::new(block_device);
block::init_block_device(block_device);
Expand Down Expand Up @@ -351,11 +353,6 @@ fn init_input_device(input: prob::DeviceInfo, name: &str, mmio_transport: Option
_ => panic!("Don't support {} input device", name),
}
let _ = register_device_to_plic(irq, input);
// INITIALIZED_DEVICES.lock().push(DeviceInfo {
// device: input,
// irq,
// need_register: true,
// });
println!("Init keyboard input device success");
}
name => {
Expand All @@ -364,18 +361,19 @@ fn init_input_device(input: prob::DeviceInfo, name: &str, mmio_transport: Option
}
}

fn init_net(_nic: prob::DeviceInfo) {
// If we need run test, we should only init loop device because no we can't route packet
fn init_net(_nic: Option<prob::DeviceInfo>) {
// If we need run test, we should init loop device because no we can't route packet
#[cfg(feature = "test")]
{
init_loop_device();
}
#[cfg(not(feature = "test"))]
{
let (base_addr, irq) = (_nic.base_addr, _nic.irq);
let nic = _nic.unwrap();
let (base_addr, irq) = (nic.base_addr, nic.irq);
println!("Init net device, base_addr:{:#x},irq:{}", base_addr, irq);

match _nic.compatible.as_str() {
match nic.compatible.as_str() {
"virtio,mmio" => {
use config::{QEMU_GATEWAY, QEMU_IP};
use core::str::FromStr;
Expand All @@ -393,8 +391,7 @@ fn init_net(_nic: prob::DeviceInfo) {
println!("Init net device success");
}
name => {
println!("Don't support net device: {}", name);
return;
panic!("Don't support net device: {}", name);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion subsystems/mem/src/data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{free_frames};
use crate::free_frames;
use config::FRAME_SIZE;
use ksync::Mutex;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
//! This crate provides a way to get machine information from a device-tree.
//!
//! # Example
//! ```
//! let machine_info = machine_info_from_dtb(0x80700000);
//! ```
use core::cmp::min;
use core::fmt::Debug;
use core::ops::Range;
Expand Down
1 change: 1 addition & 0 deletions subsystems/platform/src/common_riscv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod basic;
pub mod boot;
pub mod sbi;
15 changes: 15 additions & 0 deletions subsystems/platform/src/hifive_riscv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod config;

use crate::PlatformInfo;
use core::ops::Range;
use spin::Once;

const FDT: &[u8] = include_bytes!("../../../../tools/hifive-unmatched-a00.dtb");
Expand All @@ -11,6 +13,19 @@ pub fn init_dtb(dtb: Option<usize>) {
DTB.call_once(|| dtb_ptr);
}

pub fn basic_machine_info() -> PlatformInfo {
let mut info = crate::common_riscv::basic::machine_info_from_dtb(
*crate::starfive2_riscv::DTB.get().unwrap(),
);
info.initrd = Some(Range {
start: INITRD.as_ptr() as usize,
end: INITRD.as_ptr() as usize + INITRD.len(),
});
info
}

static INITRD: &'static [u8] = include_bytes!("../../../../tools/initrd/initramfs.cpio.gz");

/// 设置定时器
pub fn set_timer(time: usize) {
crate::common_riscv::sbi::set_timer(time);
Expand Down
19 changes: 9 additions & 10 deletions subsystems/platform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ mod common_riscv;
mod hifive_riscv;

use ::config::CPU_NUM;
pub use basic::MachineInfo as PlatformInfo;
pub use common_riscv::basic::MachineInfo as PlatformInfo;
use spin::Once;
mod basic;

pub mod logging;
#[cfg(feature = "qemu_riscv")]
Expand All @@ -21,33 +20,33 @@ mod qemu_riscv;
mod starfive2_riscv;

#[cfg(feature = "qemu_riscv")]
use qemu_riscv::console_putchar;
use qemu_riscv::{basic_machine_info, console_putchar};
#[cfg(feature = "qemu_riscv")]
pub use qemu_riscv::{config, set_timer, system_shutdown};

#[cfg(feature = "vf2")]
use starfive2_riscv::console_putchar;
use starfive2_riscv::{basic_machine_info, console_putchar};
#[cfg(feature = "vf2")]
pub use starfive2_riscv::{config, set_timer, system_shutdown};

#[cfg(feature = "hifive")]
use hifive_riscv::console_putchar;
use hifive_riscv::{basic_machine_info, console_putchar};

use crate::common_riscv::sbi::hart_start;
use crate::console::PrePrint;
#[cfg(feature = "hifive")]
pub use hifive_riscv::{config, set_timer, system_shutdown};

#[no_mangle]
pub fn platform_init(hart_id: usize, dtb: usize) {
pub fn platform_init(hart_id: usize, _dtb: usize) {
println!("{}", ::config::FLAG);
#[cfg(feature = "hifive")]
hifive_riscv::init_dtb(Some(dtb));
hifive_riscv::init_dtb(None);
#[cfg(feature = "vf2")]
starfive2_riscv::init_dtb(Some(dtb));
starfive2_riscv::init_dtb(None);
#[cfg(feature = "qemu_riscv")]
qemu_riscv::init_dtb(Some(dtb));
let machine_info = basic::machine_info_from_dtb(platform_dtb_ptr());
qemu_riscv::init_dtb(Some(_dtb));
let machine_info = basic_machine_info();
MACHINE_INFO.call_once(|| machine_info);
logging::init_logger();
preprint::init_print(&PrePrint);
Expand Down
6 changes: 5 additions & 1 deletion subsystems/platform/src/qemu_riscv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod config;

use crate::PlatformInfo;
use spin::Once;

pub static DTB: Once<usize> = Once::new();
Expand All @@ -9,6 +9,10 @@ pub fn init_dtb(dtb: Option<usize>) {
DTB.call_once(|| dtb_ptr);
}

pub fn basic_machine_info() -> PlatformInfo {
crate::common_riscv::basic::machine_info_from_dtb(*DTB.get().unwrap())
}

/// 设置定时器
pub fn set_timer(time: usize) {
crate::common_riscv::sbi::set_timer(time);
Expand Down
13 changes: 13 additions & 0 deletions subsystems/platform/src/starfive2_riscv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod config;

use crate::PlatformInfo;
use core::ops::Range;
use spin::Once;

pub const FDT: &[u8] = include_bytes!("../../../../tools/jh7110-visionfive-v2.dtb");
Expand All @@ -11,6 +13,17 @@ pub fn init_dtb(dtb: Option<usize>) {
DTB.call_once(|| dtb_ptr);
}

pub fn basic_machine_info() -> PlatformInfo {
let mut info = crate::common_riscv::basic::machine_info_from_dtb(*DTB.get().unwrap());
info.initrd = Some(Range {
start: INITRD.as_ptr() as usize,
end: INITRD.as_ptr() as usize + INITRD.len(),
});
info
}

static INITRD: &'static [u8] = include_bytes!("../../../../tools/initrd/initramfs.cpio.gz");

/// 设置定时器
pub fn set_timer(time: usize) {
crate::common_riscv::sbi::set_timer(time);
Expand Down

0 comments on commit 52f095b

Please sign in to comment.