Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/feat-net' into feat…
Browse files Browse the repository at this point in the history
…-net
  • Loading branch information
Stone749990226 committed Jul 20, 2024
2 parents 2ca6366 + 225f8af commit a1ee2ac
Show file tree
Hide file tree
Showing 44 changed files with 469 additions and 507 deletions.
32 changes: 30 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions config/src/mm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ pub const KERNEL_OFFSET: usize = 0x20_0000;
pub const KERNEL_START_PHYS: usize = RAM_START + KERNEL_OFFSET;
pub const KERNEL_START: usize = VIRT_START + KERNEL_OFFSET;

pub const KERNEL_STACK_SIZE: usize = 64 * 1024; // 64K
pub const KERNEL_HEAP_SIZE: usize = 32 * 1024 * 1024; // 32M
pub const KERNEL_STACK_SIZE: usize = 64 * 1024;
pub const KERNEL_HEAP_SIZE: usize = 64 * 1024 * 1024;

register_mut_const!(pub DTB_ADDR, usize, 0);

/// boot
pub const HART_START_ADDR: usize = 0x80200000;

pub const USER_STACK_SIZE: usize = 8 * 1024 * 1024; // 8M
pub const USER_STACK_SIZE: usize = 8 * 1024 * 1024;
pub const USER_STACK_PRE_ALLOC_SIZE: usize = 4 * PAGE_SIZE;

pub const USER_ELF_PRE_ALLOC_PAGE_CNT: usize = 0;

pub const PAGE_SIZE: usize = 1 << PAGE_SIZE_BITS;
pub const PAGE_MASK: usize = PAGE_SIZE - 1;
pub const PAGE_SIZE_BITS: usize = 12;
Expand All @@ -39,7 +41,8 @@ pub const PAGE_TABLE_LEVEL_NUM: usize = 3;
/// Dynamic linked interpreter address range in user space
pub const DL_INTERP_OFFSET: usize = 0x20_0000_0000;

pub const MAX_BUFFER_CACHE: usize = 0x100;
pub const MAX_BUFFER_HEADS: usize = 0x30000;
pub const MAX_BUFFER_CACHE: usize = 0x1000;
pub const MAX_BUFFER_PAGES: usize = MAX_BUFFER_CACHE / MAX_BUFFERS_PER_PAGE;
pub const MAX_BUFFERS_PER_PAGE: usize = PAGE_SIZE / BLOCK_SIZE;
pub const BUFFER_NEED_CACHE_CNT: usize = 8;
Expand Down
10 changes: 10 additions & 0 deletions crates/backtrace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "backtrace"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
arch = { path = "../../arch/" }
early-print = { path = "../early-print/" }
23 changes: 23 additions & 0 deletions crates/backtrace/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![no_std]
#![no_main]

use core::mem::size_of;

use early_print::early_println;

pub fn backtrace() {
extern "C" {
fn _stext();
fn _etext();
}
unsafe {
let mut current_pc = arch::register::ra();
let mut current_fp = arch::register::fp();

while current_pc >= _stext as usize && current_pc <= _etext as usize && current_fp != 0 {
early_println!("{:#018x}", current_pc - size_of::<usize>());
current_fp = *(current_fp as *const usize).offset(-2);
current_pc = *(current_fp as *const usize).offset(-1);
}
}
}
9 changes: 9 additions & 0 deletions crates/early-print/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "early-print"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sbi-rt = { version = "0.0.3", features = ["legacy"] }
32 changes: 32 additions & 0 deletions crates/early-print/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![no_std]
#![no_main]

use core::{fmt, fmt::Write};

pub struct EarlyStdout;

impl fmt::Write for EarlyStdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
for s in s.as_bytes() {
sbi_rt::legacy::console_putchar(*s as usize);
}
Ok(())
}
}

pub fn early_print(args: fmt::Arguments<'_>) {
EarlyStdout.write_fmt(args).unwrap();
}

#[macro_export]
macro_rules! early_print {
($($arg:tt)*) => {{
$crate::early_print(format_args!($($arg)*));
}};
}

#[macro_export]
macro_rules! early_println {
() => ($crate::early_print!("\n"));
($($arg:tt)*) => ($crate::early_print!("{}\n", format_args!($($arg)*)));
}
1 change: 1 addition & 0 deletions driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ memory = { path = "../modules/memory/" }
systype = { path = "../modules/systype/" }
page = { path = "../modules/page/" }
device-core = { path = "../modules/device-core/" }
early-print = { path = "../crates/early-print/" }
net = { path = "../modules/net/" }

bitflags = "2.5"
Expand Down
31 changes: 8 additions & 23 deletions driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use core::fmt::{self, Write};
use async_utils::block_on;
use crate_interface::call_interface;
use device_core::{BlockDriverOps, CharDevice, DeviceMajor};
use early_print::EarlyStdout;
use manager::DeviceManager;
use memory::PageTable;
use spin::Once;
Expand Down Expand Up @@ -94,42 +95,26 @@ impl Write for Stdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
if let Some(serial) = unsafe { UART0.lock().as_mut() } {
block_on(async { serial.write(s.as_bytes()).await });
return Ok(());
Ok(())
} else {
EarlyStdout.write_str(s)
}
for s in s.as_bytes() {
console_putchar(*s as usize);
}
Ok(())
}
}

pub fn print(args: fmt::Arguments<'_>) {
pub fn _print(args: fmt::Arguments) {
Stdout.write_fmt(args).unwrap();
}

/// print string macro
#[macro_export]
macro_rules! print {
($($arg:tt)*) => {{
$crate::print(format_args!($($arg)*));
$crate::_print(format_args!($($arg)*));
}};
}

/// println string macro
#[macro_export]
macro_rules! println {
() => {
$crate::print!("\n")
};
($($arg:tt)*) => {{
$crate::print(format_args_nl!($($arg)*));
}};
}

pub fn shutdown() -> ! {
sbi::shutdown()
}

pub fn set_timer(timer: usize) {
sbi::set_timer(timer)
() => ($crate::print!("\n"));
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
}
20 changes: 10 additions & 10 deletions driver/src/serial/uart8250.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ bitflags! {
#[derive(Debug)]
pub struct Uart {
/// UART MMIO base address
mmio_base: usize,
mmio_base_vaddr: usize,
clock_frequency: u32,
baud_rate: u32,
reg_io_width: usize,
Expand All @@ -71,15 +71,15 @@ impl Uart {
/// base address really points to a serial port device.
#[rustversion::attr(since(1.61), const)]
pub unsafe fn new(
mmio_base: usize,
mmio_base_vaddr: usize,
clock_frequency: usize,
baud_rate: usize,
reg_io_width: usize,
reg_shift: usize,
is_snps: bool,
) -> Self {
Self {
mmio_base,
mmio_base_vaddr,
clock_frequency: clock_frequency as u32,
baud_rate: baud_rate as u32,
reg_io_width,
Expand Down Expand Up @@ -111,7 +111,7 @@ impl Uart {
}

fn init_u8(&mut self) {
let reg = self.mmio_base as *mut u8;
let reg = self.mmio_base_vaddr as *mut u8;

unsafe {
// Disable Interrupt
Expand Down Expand Up @@ -144,7 +144,7 @@ impl Uart {
}

fn init_u32(&mut self) {
let reg = self.mmio_base as *mut u32;
let reg = self.mmio_base_vaddr as *mut u32;

unsafe {
// Disable Interrupt
Expand Down Expand Up @@ -180,14 +180,14 @@ impl Uart {
}

fn line_sts_u8(&self) -> LineStsFlags {
let ptr = self.mmio_base as *mut u8;
let ptr = self.mmio_base_vaddr as *mut u8;
unsafe {
LineStsFlags::from_bits_truncate(ptr.byte_add(LSR << self.reg_shift).read_volatile())
}
}

fn line_sts_u32(&self) -> LineStsFlags {
let ptr = self.mmio_base as *mut u32;
let ptr = self.mmio_base_vaddr as *mut u32;
unsafe {
LineStsFlags::from_bits_truncate(
ptr.byte_add(LSR << self.reg_shift).read_volatile() as u8
Expand All @@ -197,7 +197,7 @@ impl Uart {

/// Sends a byte on the serial port.
pub fn send_u8(&mut self, c: u8) {
let ptr = self.mmio_base as *mut u8;
let ptr = self.mmio_base_vaddr as *mut u8;
unsafe {
match c {
8 | 0x7F => {
Expand All @@ -220,7 +220,7 @@ impl Uart {
}

pub fn send_u32(&mut self, c: u8) {
let ptr = self.mmio_base as *mut u32;
let ptr = self.mmio_base_vaddr as *mut u32;
unsafe {
match c {
8 | 0x7F => {
Expand All @@ -244,7 +244,7 @@ impl Uart {

/// Receives a byte on the serial port.
pub fn receive(&mut self) -> u8 {
let ptr = self.mmio_base as *mut u32;
let ptr = self.mmio_base_vaddr as *mut u32;
unsafe {
wait_for!(self.line_sts_u8().contains(LineStsFlags::INPUT_FULL));
if self.is_snps {
Expand Down
11 changes: 9 additions & 2 deletions driver/src/virtio/virtio_blk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ impl BlockDriverOps for VirtIoBlkDev {
BLOCK_SIZE
}

fn buffer_head_cnts(&self) -> usize {
self.cache.lock().buffer_heads.len()
}

fn remove_buffer_page(&self, block_id: usize) {
self.cache.lock().pages.pop(&block_id);
}

fn base_read_block(&self, block_id: usize, buf: &mut [u8]) {
// log::error!("read blk id {}", block_id);
let res = self.device.lock().read_blocks(block_id, buf);
if res.is_err() {
panic!(
Expand Down Expand Up @@ -76,7 +83,7 @@ impl VirtIoBlkDev {
name: "virtio-blk".to_string(),
mmio_base,
mmio_size,
irq_no: None, // TODO: block device don't support interrupts in this system
irq_no: None, // TODO: support interrupt for block device
dtype: DeviceType::Block,
};
let blk_dev = Arc::new(Self {
Expand Down
2 changes: 2 additions & 0 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ time = { path = "../modules/time/" }
timer = { path = "../modules/timer/" }
page = { path = "../modules/page/" }
net = { path = "../modules/net/" }
early-print = { path = "../crates/early-print/" }
backtrace = { path = "../crates/backtrace/" }

cfg-if = "1.0"
crate_interface = "0.1"
Expand Down
Loading

0 comments on commit a1ee2ac

Please sign in to comment.