File tree 18 files changed +177
-61
lines changed
18 files changed +177
-61
lines changed Original file line number Diff line number Diff line change 18
18
target /
19
19
Cargo.lock
20
20
** /* .rs.bk
21
- * .pdb
21
+ * .pdb
22
+
23
+ kernel_img
24
+ map.txt
25
+ kernel_lib.S
Original file line number Diff line number Diff line change @@ -19,5 +19,8 @@ mm = { path = "crates/mm" }
19
19
net = { path = " crates/net" }
20
20
types = { path = " crates/types" }
21
21
22
+ spin = " 0.9.8"
23
+ bitflags = " 2.6.0"
24
+
22
25
[profile .release ]
23
26
lto = true
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ KERNEL_LIB_OUT := $(KERNEL_LIBS)/libkernel.a
24
24
KERNEL_LIB_ASM := kernel_lib.S
25
25
RUSTFLAGS := -C soft-float -C panic=abort
26
26
27
- LDSCRIPT := $(KERNEL_SRC ) /kernel.ld
27
+ LDSCRIPT := $(KERNEL_SRC ) /linker/ kernel.ld
28
28
LDFLAGS = -z max-page-size=0x1000 --gc-sections -Map map.txt
29
29
30
30
CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb -gdwarf-2
Original file line number Diff line number Diff line change @@ -4,9 +4,9 @@ version = "0.0.1"
4
4
authors = [
" squeakbug <[email protected] >" ]
5
5
edition = " 2021"
6
6
7
- [lib ]
8
- name = " dev"
9
- path = " src/lib.rs"
10
- crate-type = [" staticlib" ]
7
+ [dependencies ]
8
+ spin.workspace = true
11
9
12
- [dependencies ]
10
+ [dependencies .lazy_static ]
11
+ version = " 1.0"
12
+ features = [" spin_no_std" ]
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- // TBD
1
+ #![ no_main]
2
+ #![ no_std]
3
+
4
+ pub mod uart16550;
5
+ pub mod console;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 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
+ }
Original file line number Diff line number Diff line change @@ -10,3 +10,8 @@ path = "src/lib.rs"
10
10
crate-type = [" staticlib" ]
11
11
12
12
[dependencies ]
13
+ dev.workspace = true
14
+ fs.workspace = true
15
+ mm.workspace = true
16
+ net.workspace = true
17
+ types.workspace = true
Original file line number Diff line number Diff line change 3
3
4
4
use core:: panic:: PanicInfo ;
5
5
6
- pub const UART_BASE : usize = 0x1000_0000 ;
6
+ use dev :: uart16550 :: { putchar , UART_BASE } ;
7
7
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
-
28
8
#[ no_mangle]
29
9
pub unsafe extern "C" fn kmain ( ) -> ! {
30
- print ( "Hello world!\r \n " ) ;
10
+ mm:: init ( ) ;
11
+
12
+ dev:: println!( "Hello, world!" ) ;
31
13
let uart = UART_BASE as * const u8 ;
32
14
loop {
33
- putchar ( uart. read_volatile ( ) ) ;
15
+ putchar ( uart. read_volatile ( ) ) ;
34
16
}
35
17
}
36
18
Original file line number Diff line number Diff line change @@ -4,19 +4,7 @@ version = "0.1.0"
4
4
edition = " 2021"
5
5
6
6
[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
11
9
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
Original file line number Diff line number Diff line change
1
+ pub const KERNBASE : usize = 0x8000_0000 ;
2
+ pub const PHYSTOP : usize = KERNBASE + 32 * 1024 * 1024 ;
Original file line number Diff line number Diff line change
1
+ #![ no_main]
2
+ #![ no_std]
3
+
1
4
pub mod page;
2
5
pub mod zone;
6
+ pub mod layout;
7
+
8
+ use layout:: PHYSTOP ;
3
9
pub use zone:: Zone ;
4
10
5
11
pub mod locked_zone;
6
12
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
+ }
Original file line number Diff line number Diff line change 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;
5
4
6
5
use spin:: Mutex ;
7
6
@@ -40,3 +39,24 @@ impl Deref for LockedZone {
40
39
& self . 0
41
40
}
42
41
}
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
+ }
Original file line number Diff line number Diff line change 1
- use std :: {
1
+ use core :: {
2
2
cmp:: min, fmt,
3
3
ptr:: NonNull ,
4
4
} ;
@@ -66,10 +66,10 @@ impl Zone {
66
66
67
67
let mut current_start = start;
68
68
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
+ }
73
73
74
74
self . free_area [ order] . push_front ( current_start as * mut usize ) ;
75
75
@@ -153,8 +153,11 @@ impl Zone {
153
153
154
154
impl fmt:: Debug for Zone {
155
155
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 > ( ) )
158
161
. field ( "managed" , & self . _managed_pages )
159
162
. field ( "present" , & self . _present_pages )
160
163
. field ( "sizes" , & sizes)
Original file line number Diff line number Diff line change 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
+ }
Original file line number Diff line number Diff line change
1
+ #![ no_main]
2
+ #![ no_std]
3
+
1
4
pub mod linked_list;
2
5
pub mod safe_linked_list;
Original file line number Diff line number Diff line change 1
- use core:: { fmt, ptr} ;
1
+ use core:: {
2
+ { fmt, ptr}
3
+ } ;
2
4
3
5
#[ derive( Copy , Clone ) ]
4
6
pub struct List {
@@ -16,7 +18,7 @@ pub struct ListHead {
16
18
17
19
impl fmt:: Debug for ListHead {
18
20
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 > ( ) )
20
22
. field ( "next" , & self . next )
21
23
. field ( "prev" , & self . prev )
22
24
. finish ( )
You can’t perform that action at this time.
0 commit comments