Compare commits

...

7 Commits

16 changed files with 131 additions and 69 deletions

View File

@ -2,3 +2,9 @@
name = "metaverse" name = "metaverse"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
# 此Cargo.toml仅用于rust-analyzer识别rust部分的代码
# 不应使用cargo编译
[target.'cfg(target_arch = "x86_64")']
target = "x86_64-unknown-none"

View File

@ -56,7 +56,7 @@ make debug
* [x] 内存分配器 * [x] 内存分配器
* [x] raw_allocator * [x] raw_allocator
* [x] tty * [x] tty
* [ ] 内核日志 * [x] 内核日志
* [ ] 文件系统 * [ ] 文件系统
* [ ] vfs * [ ] vfs
* [ ] fat32驱动移植 * [ ] fat32驱动移植

View File

@ -139,9 +139,9 @@ allocator对象在进程与内核之间传递时一律使用内核空间的映
*/ */
void *memm_allocate(usize size, usize pid); void *memm_allocate(usize size, usize pid);
#define memm_addr_set_allocator(mem, allocator) \ #define memm_addr_set_allocator(mem, allocator) \
*(allocator_t **)(mem - 16) = allocator; *(allocator_t **)((void *)(mem) - 16) = allocator;
#define memm_addr_get_allocator(mem) \ #define memm_addr_get_allocator(mem) \
((*(allocator_t **)(mem - 16))) ((*(allocator_t **)((void *)(mem) - 16)))
void *memm_kernel_allocate(usize size); void *memm_kernel_allocate(usize size);

@ -1 +1 @@
Subproject commit a7d79f8d3d865c93dad38796530e6a4dc53a0377 Subproject commit 087c4795bbc23cd0baee060bda8c1159a971a542

View File

@ -22,12 +22,18 @@ endif
################################ ################################
# rust语言环境变量 # rust语言环境变量
RSCFLAGS = --emit obj --crate-type lib \ RSCFLAGS = --emit obj --crate-type staticlib --verbose \
--crate-name=metaverse \
--edition 2021 \
-L crate="${PWD}/../rustlib/${ARCH}/src/" \ -L crate="${PWD}/../rustlib/${ARCH}/src/" \
-C code-model=large \ -C code-model=large \
-C relocation-model=static \ -C relocation-model=static \
-C embed-bitcode=no -C embed-bitcode=no
ifeq (${ARCH},x86_64)
RSCFLAGS := ${RSCFLAGS} --target x86_64-unknown-none
endif
ifdef release ifdef release
RSCFLAGS := -O RSCFLAGS := -O
endif endif

View File

@ -1,8 +1,6 @@
use core::{cmp::Ordering, ops::Sub, time::Duration}; use core::{cmp::Ordering, ops::Sub, time::Duration};
use alloc::string::ToString; use alloc::{format, string::ToString};
use crate::format;
extern "C" { extern "C" {
fn system_time_get() -> usize; fn system_time_get() -> usize;
@ -98,7 +96,7 @@ impl ToString for SystemTime {
let milisec = second % second_dur; let milisec = second % second_dur;
second /= second_dur; second /= second_dur;
format!( format!(
"[ {}-{}-{} {}:{}:{}.{} ]", "[ {}-{}-{} {}:{}:{}.{} ] ",
year, month, day, hour, minute, second, milisec year, month, day, hour, minute, second, milisec
) )
} }

View File

@ -41,7 +41,7 @@ impl KernelLogger {
pub fn fatal(&mut self, msg: Message) { pub fn fatal(&mut self, msg: Message) {
let msg = MessageBuilder::new() let msg = MessageBuilder::new()
.message("Fatal: ") .message("Fatal: ")
.foreground_color(Color(0xee, 0xa, 0xa)) .foreground_color(Color::RED)
.append(MessageBuilder::from_message(msg)) .append(MessageBuilder::from_message(msg))
.build(); .build();
self.fatal_queue.push((SystemTime::now(), msg)); self.fatal_queue.push((SystemTime::now(), msg));
@ -50,7 +50,7 @@ impl KernelLogger {
pub fn error(&mut self, msg: Message) { pub fn error(&mut self, msg: Message) {
let msg = MessageBuilder::new() let msg = MessageBuilder::new()
.message("Error: ") .message("Error: ")
.foreground_color(Color(0xaa, 0x22, 0x22)) .foreground_color(Color::ORANGE)
.append(MessageBuilder::from_message(msg)) .append(MessageBuilder::from_message(msg))
.build(); .build();
self.error_queue.push((SystemTime::now(), msg)); self.error_queue.push((SystemTime::now(), msg));
@ -59,7 +59,7 @@ impl KernelLogger {
pub fn warning(&mut self, msg: Message) { pub fn warning(&mut self, msg: Message) {
let msg = MessageBuilder::new() let msg = MessageBuilder::new()
.message("Warning: ") .message("Warning: ")
.foreground_color(Color(0xaa, 0xa, 0xaa)) .foreground_color(Color::PURPLE)
.append(MessageBuilder::from_message(msg)) .append(MessageBuilder::from_message(msg))
.build(); .build();
self.warning_queue.push((SystemTime::now(), msg)); self.warning_queue.push((SystemTime::now(), msg));
@ -68,7 +68,7 @@ impl KernelLogger {
pub fn info(&mut self, msg: Message) { pub fn info(&mut self, msg: Message) {
let msg = MessageBuilder::new() let msg = MessageBuilder::new()
.message("Info: ") .message("Info: ")
.foreground_color(Color(0xa, 0xee, 0xa)) .foreground_color(Color::GREEN)
.append(MessageBuilder::from_message(msg)) .append(MessageBuilder::from_message(msg))
.build(); .build();
self.info_queue.push((SystemTime::now(), msg)); self.info_queue.push((SystemTime::now(), msg));
@ -77,7 +77,7 @@ impl KernelLogger {
pub fn debug(&mut self, msg: Message) { pub fn debug(&mut self, msg: Message) {
let msg = MessageBuilder::new() let msg = MessageBuilder::new()
.message("Debug: ") .message("Debug: ")
.foreground_color(Color(0xee, 0xee, 0xee)) .foreground_color(Color::WHITE)
.append(MessageBuilder::from_message(msg)) .append(MessageBuilder::from_message(msg))
.build(); .build();
self.debug_queue.push((SystemTime::now(), msg)); self.debug_queue.push((SystemTime::now(), msg));
@ -86,7 +86,7 @@ impl KernelLogger {
pub fn trace(&mut self, msg: Message) { pub fn trace(&mut self, msg: Message) {
let msg = MessageBuilder::new() let msg = MessageBuilder::new()
.message("Trace: ") .message("Trace: ")
.foreground_color(Color(0xee, 0xee, 0xee)) .foreground_color(Color::WHITE)
.append(MessageBuilder::from_message(msg)) .append(MessageBuilder::from_message(msg))
.build(); .build();
self.trace_queue.push((SystemTime::now(), msg)); self.trace_queue.push((SystemTime::now(), msg));

View File

@ -13,7 +13,7 @@ extern "C" fn kmain_rust() -> ! {
logger.info(message!( logger.info(message!(
Msg("Hello, "), Msg("Hello, "),
Msg("Metaverse"), Msg("Metaverse"),
FgColor(Color(0xa, 0xee, 0xa)), FgColor(Color::GREEN),
Msg("!\n") Msg("!\n")
)); ));
for msg in logger.iter(LoggerLevel::Info) { for msg in logger.iter(LoggerLevel::Info) {

View File

@ -200,7 +200,8 @@ void *memm_allocate(usize size, usize pid)
ptr = new_allocator->allocate(&new_allocator->allocator_instance, orgsize, 0); ptr = new_allocator->allocate(&new_allocator->allocator_instance, orgsize, 0);
after_allocation: after_allocation:
memm_addr_set_allocator(ptr, allocator); if (ptr != nullptr)
memm_addr_set_allocator(ptr, allocator);
return ptr; return ptr;
} }

View File

@ -1,6 +1,9 @@
extern crate core; extern crate core;
use core::alloc::{GlobalAlloc, Layout}; use core::{
alloc::{GlobalAlloc, Layout},
ptr::null_mut,
};
extern "C" { extern "C" {
pub fn memm_kernel_allocate(size: usize) -> *mut u8; pub fn memm_kernel_allocate(size: usize) -> *mut u8;
@ -12,7 +15,14 @@ pub struct KernelAllocator {}
unsafe impl GlobalAlloc for KernelAllocator { unsafe impl GlobalAlloc for KernelAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
memm_kernel_allocate(layout.size()) let res = memm_kernel_allocate(layout.size());
if res == null_mut() {
panic!(
"Kernel allocator failed to allocate {} byte(s) memory.",
layout.size()
);
}
res
} }
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {

View File

@ -169,6 +169,7 @@ void tty_text_print(tty *ttyx, char *string, u32 color, u32 bgcolor)
char c = *string; char c = *string;
if (c == '\n') if (c == '\n')
{ // 换行 { // 换行
putchar(ttyx, ' ', 0, 0);
newline(ttyx); newline(ttyx);
continue; continue;
} }

View File

@ -7,21 +7,21 @@ use alloc::{
}; };
extern "C" { extern "C" {
fn tty_new(tty_type: u8, mode: u8) -> *mut u8; pub fn tty_new(tty_type: u8, mode: u8) -> *mut u8;
fn tty_get(id: usize) -> *mut *mut u8; pub fn tty_get(id: usize) -> *mut *mut u8;
fn tty_text_print(ttyx: *mut u8, string: *mut u8, color: u32, bgcolor: u32); pub fn tty_text_print(ttyx: *mut u8, string: *mut u8, color: u32, bgcolor: u32);
fn tty_get_id(tty: *mut u8) -> usize; pub fn tty_get_id(tty: *mut u8) -> usize;
fn tty_get_width(tty: *mut u8) -> usize; pub fn tty_get_width(tty: *mut u8) -> usize;
fn tty_get_height(tty: *mut u8) -> usize; pub fn tty_get_height(tty: *mut u8) -> usize;
fn tty_get_type(tty: *mut u8) -> u8; pub fn tty_get_type(tty: *mut u8) -> u8;
fn tty_get_mode(tty: *mut u8) -> u8; pub fn tty_get_mode(tty: *mut u8) -> u8;
fn tty_is_enabled(tty: *mut u8) -> bool; pub fn tty_is_enabled(tty: *mut u8) -> bool;
fn tty_enable(tty: *mut u8) -> bool; pub fn tty_enable(tty: *mut u8) -> bool;
fn tty_disable(tty: *mut u8); pub fn tty_disable(tty: *mut u8);
} }
pub enum Type { pub enum Type {
@ -144,6 +144,18 @@ impl Tty {
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Color(pub u8, pub u8, pub u8); pub struct Color(pub u8, pub u8, pub u8);
impl Color {
pub const WHITE: Color = Color(0xee, 0xee, 0xee);
pub const BLACK: Color = Color(0, 0, 0);
pub const RED: Color = Color(0xee, 0x22, 0x22);
pub const GREEN: Color = Color(0x22, 0xee, 0x22);
pub const BLUE: Color = Color(0x22, 0x22, 0xee);
pub const YELLOW: Color = Color(0xee, 0x22, 0x22);
pub const ORANGE: Color = Color(0xee, 0xee, 0x22);
pub const PURPLE: Color = Color(0xee, 0, 0xee);
pub const PINK: Color = Color(0xee, 0x44, 0x66);
}
impl From<Color> for u32 { impl From<Color> for u32 {
fn from(value: Color) -> Self { fn from(value: Color) -> Self {
let res = (value.0 as u32) << 16 | (value.1 as u32) << 8 | (value.2 as u32); let res = (value.0 as u32) << 16 | (value.1 as u32) << 8 | (value.2 as u32);

View File

@ -2,5 +2,68 @@
extern crate alloc; extern crate alloc;
use alloc::{
string::{String, ToString},
vec::Vec,
};
use core::{panic::PanicInfo, ptr::null_mut};
use kernel::tty::tty::{self, tty_enable, tty_text_print, Color};
pub mod kernel; pub mod kernel;
pub mod libk; pub mod libk;
#[panic_handler]
unsafe fn kernel_panic_handler(info: &PanicInfo) -> ! {
let line_in_file = if let Some(loca) = info.location() {
loca.line().to_string()
} else {
String::new()
};
let info = {
let mut v = Vec::new();
v.push(("Kernel Panic: ", (Color::RED, Color::BLACK)));
v.push((
if let Some(loca) = info.location() {
loca.file()
} else {
"NoFile"
},
(Color::GREEN, Color::BLACK),
));
v.push((":", (Color::WHITE, Color::BLACK)));
v.push((
if let Some(_) = info.location() {
line_in_file.as_str()
} else {
"NoLine"
},
(Color::WHITE, Color::BLACK),
));
v.push((": ", (Color::WHITE, Color::BLACK)));
v.push((
if let Some(&s) = info.payload().downcast_ref::<&str>() {
s
} else {
"Unknown panic."
},
(Color::BLUE, Color::BLACK),
));
v.push(("\n", (Color::BLACK, Color::BLACK)));
v
};
let tty = tty::tty_get(0);
if tty != null_mut() {
let tty = *tty;
tty_enable(tty);
for (msgo, (fgc, bgc)) in info.into_iter() {
let msg = String::from(msgo).as_bytes_mut() as *mut [u8] as *mut u8;
let p = msg.offset(msgo.len() as isize);
let swp = *p;
*p = 0;
tty_text_print(tty, msg, u32::from(fgc), u32::from(bgc));
*p = swp;
}
}
loop {}
}

View File

@ -1,38 +0,0 @@
use alloc::string::{String, ToString};
#[macro_export]
macro_rules! format {
( $s : expr, $( $e : expr ),* ) => {{
use crate::libk::string::format::Format;
let mut res = $s.to_string();
$(
res.format($e);
)*
res
}};
() => {};
}
pub trait Format<T: ToString> {
fn format(&mut self, f: T);
}
impl<T: ToString> Format<T> for String {
fn format(&mut self, f: T) {
let mut res = String::new();
let mut formatting = false;
for c in self.chars().into_iter() {
if c == '{' {
formatting = true;
res += &f.to_string();
} else if c == '}' {
formatting = false;
}
if !formatting {
res.push(c);
}
}
self.clear();
self.push_str(&res);
}
}

View File

@ -1 +0,0 @@
pub mod format;

View File

@ -96,6 +96,10 @@ SECTIONS {
{ {
*(.debug_pubtypes) *(.debug_pubtypes)
} }
.debug_frame :
{
*(.debug_frame)
}
.kend : .kend :
{ {
*(.kend) *(.kend)