-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b33f8c0
commit e55f9d1
Showing
8 changed files
with
350 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "yslib" | ||
version = "0.7.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
syscall_def = { package = "ysos_syscall", path = "../syscall" } | ||
chrono = { version = "0.4", default-features = false } | ||
linked_list_allocator = { version = "0.10", optional = true } | ||
|
||
[features] | ||
default = ["kernel_alloc"] | ||
kernel_alloc = [] | ||
brk_alloc = ["dep:linked_list_allocator"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use linked_list_allocator::LockedHeap; | ||
|
||
use crate::*; | ||
|
||
const HEAP_SIZE: usize = 8 * 1024 - 8; // 8 KiB | ||
|
||
#[global_allocator] | ||
static ALLOCATOR: LockedHeap = LockedHeap::empty(); | ||
|
||
pub fn init() { | ||
let heap_start = sys_brk(None).unwrap(); | ||
let heap_end = heap_start + HEAP_SIZE; | ||
|
||
let ret = sys_brk(Some(heap_end)).expect("Failed to allocate heap"); | ||
|
||
assert!(ret == heap_end, "Failed to allocate heap"); | ||
|
||
unsafe { ALLOCATOR.lock().init(heap_start as *mut u8, HEAP_SIZE) }; | ||
} | ||
|
||
#[cfg(not(test))] | ||
#[alloc_error_handler] | ||
fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! { | ||
panic!("Allocation error: {:?}", layout) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
pub struct KernelAllocator; | ||
|
||
unsafe impl alloc::alloc::GlobalAlloc for KernelAllocator { | ||
unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 { | ||
crate::sys_allocate(&layout) | ||
} | ||
unsafe fn dealloc(&self, ptr: *mut u8, layout: core::alloc::Layout) { | ||
crate::sys_deallocate(ptr, &layout); | ||
} | ||
} | ||
|
||
#[global_allocator] | ||
static ALLOCATOR: KernelAllocator = KernelAllocator; | ||
|
||
#[cfg(not(test))] | ||
#[alloc_error_handler] | ||
fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! { | ||
panic!("Allocation error: {:?}", layout) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#[cfg(feature = "brk_alloc")] | ||
mod brk; | ||
|
||
#[cfg(feature = "brk_alloc")] | ||
pub use brk::*; | ||
|
||
#[cfg(feature = "kernel_alloc")] | ||
mod kernel; | ||
|
||
#[cfg(feature = "kernel_alloc")] | ||
pub use kernel::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#![allow(dead_code, unused_imports)] | ||
#![feature(panic_info_message)] | ||
#![feature(alloc_error_handler)] | ||
#![cfg_attr(not(test), no_std)] | ||
|
||
#[macro_use] | ||
pub mod macros; | ||
|
||
#[macro_use] | ||
extern crate syscall_def; | ||
|
||
#[macro_use] | ||
pub mod io; | ||
pub mod allocator; | ||
pub mod sync; | ||
pub extern crate alloc; | ||
|
||
mod syscall; | ||
mod utils; | ||
|
||
use core::fmt::*; | ||
|
||
pub use alloc::*; | ||
pub use chrono::*; | ||
pub use io::*; | ||
pub use sync::*; | ||
pub use syscall::*; | ||
pub use utils::*; | ||
|
||
pub fn init() { | ||
#[cfg(feature = "brk_alloc")] | ||
crate::allocator::init(); | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! print { | ||
($($arg:tt)*) => ($crate::_print(format_args!($($arg)*))); | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! println { | ||
() => ($crate::print!("\n")); | ||
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*))); | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! err { | ||
($($arg:tt)*) => ($crate::_err(format_args!($($arg)*))); | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! errln { | ||
() => ($crate::err!("\n")); | ||
($($arg:tt)*) => ($crate::err!("{}\n", format_args!($($arg)*))); | ||
} | ||
|
||
#[doc(hidden)] | ||
pub fn _print(args: Arguments) { | ||
stdout().write(format!("{}", args).as_str()); | ||
} | ||
|
||
#[doc(hidden)] | ||
pub fn _err(args: Arguments) { | ||
stderr().write(format!("{}", args).as_str()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::errln; | ||
use alloc::string::ToString; | ||
|
||
#[macro_export] | ||
macro_rules! entry { | ||
($fn:ident) => { | ||
#[export_name = "_start"] | ||
pub extern "C" fn __impl_start() { | ||
lib::init(); // THIS LINE IS NEW IN LAB 7 | ||
let ret = $fn(); | ||
lib::sys_exit(ret); | ||
} | ||
}; | ||
} | ||
|
||
#[cfg_attr(not(test), panic_handler)] | ||
fn panic(info: &core::panic::PanicInfo) -> ! { | ||
let location = if let Some(location) = info.location() { | ||
alloc::format!( | ||
"{}@{}:{}", | ||
location.file(), | ||
location.line(), | ||
location.column() | ||
) | ||
} else { | ||
"Unknown location".to_string() | ||
}; | ||
let msg = if let Some(msg) = info.message() { | ||
alloc::format!("{}", msg) | ||
} else { | ||
"No more message...".to_string() | ||
}; | ||
errln!("\n\n\rERROR: panicked at {}\n\n\r{}", location, msg); | ||
|
||
crate::sys_exit(1); | ||
} |