mirror of
https://github.com/panpaul/tiny_os
synced 2024-09-20 09:45:19 +08:00
chore: code style
This commit is contained in:
parent
f897f4e558
commit
83158f5e4d
@ -1,2 +1,5 @@
|
||||
[build]
|
||||
target = "riscv64imac-unknown-none-elf"
|
||||
|
||||
[target.'cfg(all(target_arch = "riscv64", target_os = "none"))']
|
||||
runner = "qemu-system-riscv64 -nographic -machine virt -serial mon:stdio -smp 1 -kernel "
|
||||
|
@ -1,17 +1,24 @@
|
||||
use core::ptr::addr_of_mut;
|
||||
|
||||
#[naked]
|
||||
#[no_mangle]
|
||||
#[link_section = ".text.entry"]
|
||||
unsafe extern "C" fn _start(hart_id: usize, device_tree_addr: usize) -> ! {
|
||||
// simplest starter
|
||||
// no stack here
|
||||
// NOTE: no stack here
|
||||
// we should be launched by OpenSBI and running is S-mode
|
||||
|
||||
const STACK_SIZE: usize = 4096 * 16;
|
||||
#[link_section = ".bss.boot_stack"]
|
||||
static mut STACK: [u8; STACK_SIZE] = [0u8; STACK_SIZE];
|
||||
|
||||
// TODO: consider about relocations
|
||||
// TODO: should we emit `la gp, __global_pointer$` here?
|
||||
|
||||
core::arch::asm!(
|
||||
"la sp, {stack} + {stack_size}",
|
||||
"j {main}",
|
||||
"csrw sie, 0",
|
||||
"csrw sip, 0",
|
||||
"la sp, {stack} + {stack_size}",
|
||||
"j {main}",
|
||||
stack_size = const STACK_SIZE,
|
||||
stack = sym STACK,
|
||||
main = sym pre_main,
|
||||
@ -36,8 +43,8 @@ extern "C" {
|
||||
|
||||
#[inline(always)]
|
||||
unsafe fn zero_bss() {
|
||||
let cur = &mut __boot_stack_end as *mut u8;
|
||||
let end = &mut __bss_end as *mut u8;
|
||||
let cur = addr_of_mut!(__boot_stack_end) as *mut u8;
|
||||
let end = addr_of_mut!(__bss_end) as *mut u8;
|
||||
|
||||
core::slice::from_raw_parts_mut(cur, end.offset_from(cur) as usize).fill(0);
|
||||
}
|
||||
|
12
src/lang.rs
12
src/lang.rs
@ -8,15 +8,15 @@ use crate::utils::lowlevel::{Hardware, LowLevel};
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
error!("[lang] Kernel panic!");
|
||||
|
||||
if let Some(location) = info.location() {
|
||||
error!(
|
||||
"[lang] Panicked: [{}:{}] {}",
|
||||
match info.location() {
|
||||
Some(location) => error!(
|
||||
"[lang] Panicked: [{}:{}:{}] {}",
|
||||
location.file(),
|
||||
location.line(),
|
||||
location.column(),
|
||||
info.message().unwrap(),
|
||||
);
|
||||
} else {
|
||||
error!("[lang] Panicked: {}", info.message().unwrap());
|
||||
),
|
||||
None => error!("[lang] Panicked: {}", info.message().unwrap()),
|
||||
}
|
||||
|
||||
Hardware::shutdown(true);
|
||||
|
117
src/mm/addr.rs
117
src/mm/addr.rs
@ -4,22 +4,25 @@ use core::num::NonZeroUsize;
|
||||
use core::ops::{Add, AddAssign, Sub, SubAssign};
|
||||
|
||||
#[inline(always)]
|
||||
fn align_up(addr: usize, align: usize) -> usize {
|
||||
pub fn align_up(addr: usize, align: usize) -> usize {
|
||||
(addr + align - 1) & !(align - 1)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn align_down(addr: usize, align: usize) -> usize {
|
||||
pub fn align_down(addr: usize, align: usize) -> usize {
|
||||
addr & !(align - 1)
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[derive(Copy, Clone, PartialOrd, PartialEq)]
|
||||
pub struct PhysAddr(pub usize);
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[derive(Copy, Clone, PartialOrd, PartialEq)]
|
||||
pub struct VirtAddr(pub usize);
|
||||
|
||||
pub trait AddressOps {
|
||||
fn as_mut_ptr<T>(&self) -> *mut T {
|
||||
self.as_usize() as *mut T
|
||||
}
|
||||
fn as_u32(&self) -> u32;
|
||||
fn as_u64(&self) -> u64;
|
||||
fn as_usize(&self) -> usize;
|
||||
@ -114,26 +117,54 @@ impl Add<usize> for PhysAddr {
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<PhysAddr> for PhysAddr {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: PhysAddr) -> Self::Output {
|
||||
Self(self.0 + rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<usize> for PhysAddr {
|
||||
fn add_assign(&mut self, rhs: usize) {
|
||||
*self = PhysAddr::from(self.0 + rhs);
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<PhysAddr> for PhysAddr {
|
||||
fn add_assign(&mut self, rhs: PhysAddr) {
|
||||
*self = PhysAddr::from(self.0 + rhs.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub<usize> for PhysAddr {
|
||||
type Output = PhysAddr;
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, rhs: usize) -> Self::Output {
|
||||
PhysAddr(self.0 - rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub<PhysAddr> for PhysAddr {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, rhs: PhysAddr) -> Self::Output {
|
||||
PhysAddr(self.0 - rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign<usize> for PhysAddr {
|
||||
fn sub_assign(&mut self, rhs: usize) {
|
||||
*self = PhysAddr::from(self.0 - rhs);
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign<PhysAddr> for PhysAddr {
|
||||
fn sub_assign(&mut self, rhs: PhysAddr) {
|
||||
*self = PhysAddr::from(self.0 - rhs.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<usize> for VirtAddr {
|
||||
type Output = Self;
|
||||
|
||||
@ -142,12 +173,26 @@ impl Add<usize> for VirtAddr {
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<VirtAddr> for VirtAddr {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: VirtAddr) -> Self::Output {
|
||||
Self(self.0 + rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<usize> for VirtAddr {
|
||||
fn add_assign(&mut self, rhs: usize) {
|
||||
*self = VirtAddr::from(self.0 + rhs);
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<VirtAddr> for VirtAddr {
|
||||
fn add_assign(&mut self, rhs: VirtAddr) {
|
||||
*self = VirtAddr::from(self.0 + rhs.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub<usize> for VirtAddr {
|
||||
type Output = VirtAddr;
|
||||
|
||||
@ -156,45 +201,65 @@ impl Sub<usize> for VirtAddr {
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub<VirtAddr> for VirtAddr {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, rhs: VirtAddr) -> Self::Output {
|
||||
VirtAddr(self.0 - rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign<usize> for VirtAddr {
|
||||
fn sub_assign(&mut self, rhs: usize) {
|
||||
*self = VirtAddr::from(self.0 - rhs);
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign<VirtAddr> for VirtAddr {
|
||||
fn sub_assign(&mut self, rhs: VirtAddr) {
|
||||
*self = VirtAddr::from(self.0 - rhs.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl From<usize> for PhysAddr {
|
||||
fn from(addr: usize) -> Self {
|
||||
PhysAddr(addr)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PhysAddr> for usize {
|
||||
fn from(addr: PhysAddr) -> Self {
|
||||
addr.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NonZeroUsize> for PhysAddr {
|
||||
fn from(addr: NonZeroUsize) -> Self {
|
||||
PhysAddr(addr.get())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<*mut T> for PhysAddr {
|
||||
fn from(addr: *mut T) -> Self {
|
||||
PhysAddr(addr as usize)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PhysAddr> for usize {
|
||||
fn from(addr: PhysAddr) -> Self {
|
||||
addr.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PhysAddr> for NonZeroUsize {
|
||||
fn from(addr: PhysAddr) -> Self {
|
||||
NonZeroUsize::new(addr.0).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<usize> for VirtAddr {
|
||||
fn from(addr: usize) -> Self {
|
||||
VirtAddr(addr)
|
||||
impl<T> From<PhysAddr> for *mut T {
|
||||
fn from(addr: PhysAddr) -> Self {
|
||||
addr.0 as *mut T
|
||||
}
|
||||
}
|
||||
|
||||
impl From<VirtAddr> for usize {
|
||||
fn from(addr: VirtAddr) -> Self {
|
||||
addr.0
|
||||
impl From<usize> for VirtAddr {
|
||||
fn from(addr: usize) -> Self {
|
||||
VirtAddr(addr)
|
||||
}
|
||||
}
|
||||
|
||||
@ -204,12 +269,30 @@ impl From<NonZeroUsize> for VirtAddr {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<*mut T> for VirtAddr {
|
||||
fn from(addr: *mut T) -> Self {
|
||||
VirtAddr(addr as usize)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<VirtAddr> for usize {
|
||||
fn from(addr: VirtAddr) -> Self {
|
||||
addr.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<VirtAddr> for NonZeroUsize {
|
||||
fn from(addr: VirtAddr) -> Self {
|
||||
NonZeroUsize::new(addr.0).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<VirtAddr> for *mut T {
|
||||
fn from(addr: VirtAddr) -> Self {
|
||||
addr.0 as *mut T
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for PhysAddr {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||
write!(f, "PhysAddr({:#x})", self.0)
|
||||
|
@ -44,7 +44,8 @@ impl FrameAllocator {
|
||||
let addr = ALLOCATOR.lock().pop().map(|addr| addr.into());
|
||||
|
||||
trace!("[mm/frame] alloc frame: {:?}", addr);
|
||||
fill_mem(addr, 0xaa);
|
||||
#[cfg(debug_assertions)]
|
||||
fill_page(addr, 0xaa);
|
||||
|
||||
addr
|
||||
}
|
||||
@ -54,7 +55,8 @@ impl FrameAllocator {
|
||||
debug_assert!(addr.as_usize() >= KERNEL_MEM_START);
|
||||
|
||||
trace!("[mm/frame] dealloc frame: {:?}", addr);
|
||||
fill_mem(Some(addr), 0x55);
|
||||
#[cfg(debug_assertions)]
|
||||
fill_page(Some(addr), 0x55);
|
||||
|
||||
#[cfg(feature = "frame_bitmap")]
|
||||
crate::mm::frame::ALLOCATOR.lock().dealloc(
|
||||
@ -67,10 +69,9 @@ impl FrameAllocator {
|
||||
}
|
||||
}
|
||||
|
||||
fn fill_mem(addr: Option<PhysAddr>, val: u8) {
|
||||
#[cfg(debug_assertions)]
|
||||
fn fill_page(addr: Option<PhysAddr>, val: u8) {
|
||||
unsafe {
|
||||
// fill with junks
|
||||
// fill with val
|
||||
if let Some(addr) = addr {
|
||||
core::ptr::write_bytes(addr.as_usize() as *mut u8, val, PAGE_SIZE);
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
pub struct RawConsole;
|
||||
pub struct RawConsole
|
||||
where
|
||||
RawConsole: Printer;
|
||||
|
||||
pub trait Printer: core::fmt::Write {
|
||||
fn put_char(c: char);
|
||||
|
||||
#[inline]
|
||||
fn put_str(s: &str) {
|
||||
for c in s.chars() {
|
||||
Self::put_char(c);
|
||||
}
|
||||
s.chars().for_each(Self::put_char);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -62,13 +62,13 @@ pub trait Printer: core::fmt::Write {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Reader {
|
||||
fn get_char() -> char;
|
||||
}
|
||||
|
||||
impl core::fmt::Write for RawConsole {
|
||||
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
||||
Self::put_str(s);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Reader {
|
||||
fn get_char() -> char;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user