Skip to content

Commit fa6447f

Browse files
committed
add: println macro
Signed-off-by: squeakbug <[email protected]>
1 parent cb8cd21 commit fa6447f

File tree

18 files changed

+177
-61
lines changed

18 files changed

+177
-61
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ debug/
1818
target/
1919
Cargo.lock
2020
**/*.rs.bk
21-
*.pdb
21+
*.pdb
22+
23+
kernel_img
24+
map.txt
25+
kernel_lib.S

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ mm = { path = "crates/mm" }
1919
net = { path = "crates/net" }
2020
types = { path = "crates/types" }
2121

22+
spin = "0.9.8"
23+
bitflags = "2.6.0"
24+
2225
[profile.release]
2326
lto = true

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ KERNEL_LIB_OUT := $(KERNEL_LIBS)/libkernel.a
2424
KERNEL_LIB_ASM := kernel_lib.S
2525
RUSTFLAGS := -C soft-float -C panic=abort
2626

27-
LDSCRIPT := $(KERNEL_SRC)/kernel.ld
27+
LDSCRIPT := $(KERNEL_SRC)/linker/kernel.ld
2828
LDFLAGS = -z max-page-size=0x1000 --gc-sections -Map map.txt
2929

3030
CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb -gdwarf-2

crates/dev/Cargo.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ version = "0.0.1"
44
authors = ["squeakbug <[email protected]>"]
55
edition = "2021"
66

7-
[lib]
8-
name = "dev"
9-
path = "src/lib.rs"
10-
crate-type = ["staticlib"]
7+
[dependencies]
8+
spin.workspace = true
119

12-
[dependencies]
10+
[dependencies.lazy_static]
11+
version = "1.0"
12+
features = ["spin_no_std"]

crates/dev/src/console.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use core::fmt;
2+
3+
use lazy_static::lazy_static;
4+
use spin::Mutex;
5+
6+
use crate::uart16550::putchar;
7+
8+
pub struct Console {}
9+
10+
lazy_static! {
11+
pub static ref GLOBAL_TTY: Mutex<Console> = Mutex::new(Console { });
12+
}
13+
14+
impl Console {
15+
#[no_mangle]
16+
pub fn write_string(&mut self, str: &str) {
17+
unsafe {
18+
let mut str_ptr = str.as_ptr();
19+
while *str_ptr != b'\0' {
20+
putchar(*str_ptr);
21+
str_ptr = str_ptr.add(1);
22+
}
23+
}
24+
return;
25+
}
26+
}
27+
28+
impl fmt::Write for Console {
29+
fn write_str(&mut self, s: &str) -> fmt::Result {
30+
self.write_string(s);
31+
Ok(())
32+
}
33+
}
34+
35+
pub fn _print(args: fmt::Arguments) {
36+
use core::fmt::Write;
37+
GLOBAL_TTY.lock().write_fmt(args).unwrap();
38+
}
39+
40+
#[macro_export]
41+
macro_rules! print {
42+
($($arg:tt)*) => ($crate::console::_print(format_args!($($arg)*)));
43+
}
44+
45+
#[macro_export]
46+
macro_rules! println {
47+
() => ($crate::print!("\n"));
48+
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
49+
}

crates/dev/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
// TBD
1+
#![no_main]
2+
#![no_std]
3+
4+
pub mod uart16550;
5+
pub mod console;

crates/dev/src/uart16550.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub const UART_BASE: usize = 0x1000_0000;
2+
3+
#[no_mangle]
4+
pub unsafe fn putchar(c: u8) {
5+
let uart = UART_BASE as *mut u8;
6+
unsafe {
7+
uart.write_volatile(c);
8+
}
9+
}

crates/fs/src/lib.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
// TBD
1+
#![no_main]
2+
#![no_std]
3+
4+
use core::panic::PanicInfo;
5+
6+
#[panic_handler]
7+
#[no_mangle]
8+
fn panic(_info: &PanicInfo) -> ! {
9+
loop {}
10+
}

crates/kernel/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ path = "src/lib.rs"
1010
crate-type = ["staticlib"]
1111

1212
[dependencies]
13+
dev.workspace = true
14+
fs.workspace = true
15+
mm.workspace = true
16+
net.workspace = true
17+
types.workspace = true

crates/kernel/src/lib.rs

+5-23
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,16 @@
33

44
use core::panic::PanicInfo;
55

6-
pub const UART_BASE: usize = 0x1000_0000;
6+
use dev::uart16550::{putchar, UART_BASE};
77

8-
#[no_mangle]
9-
pub unsafe fn putchar(c: u8) {
10-
let uart = UART_BASE as *mut u8;
11-
unsafe {
12-
uart.write_volatile(c);
13-
}
14-
}
15-
16-
#[no_mangle]
17-
pub unsafe fn print(str: &str) {
18-
unsafe {
19-
let mut str_ptr = str.as_ptr();
20-
while *str_ptr != b'\0' {
21-
putchar(*str_ptr);
22-
str_ptr = str_ptr.add(1);
23-
}
24-
}
25-
return;
26-
}
27-
288
#[no_mangle]
299
pub unsafe extern "C" fn kmain() -> ! {
30-
print("Hello world!\r\n");
10+
mm::init();
11+
12+
dev::println!("Hello, world!");
3113
let uart = UART_BASE as *const u8;
3214
loop {
33-
putchar(uart.read_volatile());
15+
putchar(uart.read_volatile());
3416
}
3517
}
3618

crates/mm/Cargo.toml

+3-15
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
ctor = "0.2.8"
8-
rand = "0.8.5"
9-
spin = "0.9.8"
10-
bitflags = "2.6.0"
7+
spin.workspace = true
8+
bitflags.workspace = true
119
types.workspace = true
12-
13-
[dev-dependencies]
14-
criterion = "0.5.1"
15-
16-
[[bench]]
17-
name = "one_thread"
18-
harness = false
19-
20-
[[bench]]
21-
name = "one_thread_std"
22-
harness = false
10+
dev.workspace = true

crates/mm/src/layout.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub const KERNBASE: usize = 0x8000_0000;
2+
pub const PHYSTOP: usize = KERNBASE + 32 * 1024 * 1024;

crates/mm/src/lib.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
1+
#![no_main]
2+
#![no_std]
3+
14
pub mod page;
25
pub mod zone;
6+
pub mod layout;
7+
8+
use layout::PHYSTOP;
39
pub use zone::Zone;
410

511
pub mod locked_zone;
612
pub use locked_zone::LockedZone;
13+
use zone::{next_aligned_by, PAGE_SIZE};
14+
15+
#[global_allocator]
16+
pub static KMEM: LockedZone = LockedZone::new();
17+
18+
unsafe extern "C" {
19+
// first address after kernel, defined by kernel.ld
20+
unsafe static mut end: [u8; 0];
21+
}
22+
23+
pub fn init() {
24+
unsafe {
25+
KMEM.lock().add_to_heap(
26+
next_aligned_by(end.as_ptr() as usize, PAGE_SIZE),
27+
next_aligned_by(PHYSTOP, PAGE_SIZE)
28+
)
29+
}
30+
}

crates/mm/src/locked_zone.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use std::{
2-
ops::Deref,
3-
ptr::NonNull,
4-
};
1+
use core::{ops::Deref, ptr::NonNull};
2+
use core::alloc::{GlobalAlloc, Layout};
3+
use core::ptr;
54

65
use spin::Mutex;
76

@@ -40,3 +39,24 @@ impl Deref for LockedZone {
4039
&self.0
4140
}
4241
}
42+
43+
unsafe impl GlobalAlloc for LockedZone {
44+
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
45+
self.0
46+
.lock()
47+
.alloc_pages(layout.size().next_power_of_two())
48+
.map_or(ptr::null_mut(), |p| p.as_ptr())
49+
}
50+
51+
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
52+
match NonNull::new(ptr) {
53+
Some(nptr) => {
54+
self.0
55+
.lock()
56+
.free_pages(nptr, layout.size().next_power_of_two())
57+
},
58+
None => { },
59+
};
60+
61+
}
62+
}

crates/mm/src/zone.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{
1+
use core::{
22
cmp::min, fmt,
33
ptr::NonNull,
44
};
@@ -66,10 +66,10 @@ impl Zone {
6666

6767
let mut current_start = start;
6868
while current_start + PAGE_SIZE <= end {
69-
let mut order = prev_two_order(end - current_start) - PAGE_SHIFT;
70-
if order > MAX_PAGE_ORDER - 1 {
71-
order = MAX_PAGE_ORDER - 1;
72-
}
69+
let mut order = prev_two_order(end - current_start) - PAGE_SHIFT;
70+
if order > MAX_PAGE_ORDER - 1 {
71+
order = MAX_PAGE_ORDER - 1;
72+
}
7373

7474
self.free_area[order].push_front(current_start as *mut usize);
7575

@@ -153,8 +153,11 @@ impl Zone {
153153

154154
impl fmt::Debug for Zone {
155155
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
156-
let sizes = self.free_area.iter().map(|area| area.count()).collect::<Vec<_>>();
157-
fmt.debug_struct(std::any::type_name::<Self>())
156+
let sizes = &mut [0usize; MAX_PAGE_ORDER];
157+
for (i, area) in self.free_area.iter().enumerate() {
158+
sizes[i] = area.count();
159+
}
160+
fmt.debug_struct(core::any::type_name::<Self>())
158161
.field("managed", &self._managed_pages)
159162
.field("present", &self._present_pages)
160163
.field("sizes", &sizes)

crates/net/src/lib.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
// TBD
1+
#![no_main]
2+
#![no_std]
3+
4+
use core::panic::PanicInfo;
5+
6+
#[panic_handler]
7+
#[no_mangle]
8+
fn panic(_info: &PanicInfo) -> ! {
9+
loop {}
10+
}

crates/types/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
#![no_main]
2+
#![no_std]
3+
14
pub mod linked_list;
25
pub mod safe_linked_list;

crates/types/src/linked_list.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use core::{fmt, ptr};
1+
use core::{
2+
{fmt, ptr}
3+
};
24

35
#[derive(Copy, Clone)]
46
pub struct List {
@@ -16,7 +18,7 @@ pub struct ListHead {
1618

1719
impl fmt::Debug for ListHead {
1820
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
19-
fmt.debug_struct(std::any::type_name::<Self>())
21+
fmt.debug_struct(core::any::type_name::<Self>())
2022
.field("next", &self.next)
2123
.field("prev", &self.prev)
2224
.finish()

0 commit comments

Comments
 (0)