Compare commits

...

4 Commits

7 changed files with 24 additions and 67 deletions

View File

@ -28,9 +28,6 @@ vscode下的`rust-analyzer`插件会在`src/lib.rs`中误报`can't find crate fo
]
```
## 需要注意的还未解决的问题
## 需要注意的问题
* rust中所有有关字符串格式化的宏中出现超过一个不止是`{}`的格式化占位符时内核必然崩溃。
* rust中所有有关字符串格式化的宏中出现需要调用自定义的`Debug trait`的类型时内核必然崩溃,推荐定义`ToString trait`并调用`.to_string()`后传入宏参数。
* 鉴于以上两条原因,不建议在复杂的字符串格式化任务中使用`format!()`宏。推荐通过使用`::kernel::tty::tty::MessageBuilder`构造`kernel::tty::tty::Message`对象,或使用
`message`宏,并调用此对象的`.to_string()`方法实现格式化字符串。
* 在任何情况下不要使用Rust的panic。

View File

@ -1,4 +1,4 @@
use crate::{kernel::tty::tty::Tty, libk::alloc::string::ToString, message, message_raw};
use crate::{kernel::tty::tty::Tty, message};
extern "C" {
pub fn interrupt_open();
@ -14,9 +14,11 @@ unsafe extern "C" fn interrupt_req_UNSUPPORTED(rip: u64, rsp: u64, errcode: u64)
let tty = Tty::from_id(0).unwrap();
tty.enable();
tty.print(message!(
"{Panic}: Kernel hit an {Unsupported} interrupt. \n",
"{Panic}: Kernel hit an {Unsupported} interrupt on rip=0x{} and rsp=0x{}.\n",
FmtMeta::Color(Color::RED),
FmtMeta::Color(Color::YELLOW)
FmtMeta::Color(Color::YELLOW),
FmtMeta::Pointer(rip as usize),
FmtMeta::Pointer(rsp as usize)
));
loop {}
}
@ -27,8 +29,8 @@ unsafe extern "C" fn interrupt_req_DE(rip: u64, rsp: u64, errcode: u64) {
let tty = Tty::from_id(0).unwrap();
tty.enable();
tty.print(message!(
"{Panic}: Kernel hit {Divid Error} on rip=0x{} and rsp=0x{}.\n",
FmtMeta::Color(Color::RED),
"{Warning}: Kernel hit {Divid Error} on rip=0x{} and rsp=0x{}.\n",
FmtMeta::Color(Color::PURPLE),
FmtMeta::Color(Color::YELLOW),
FmtMeta::Pointer(rip as usize),
FmtMeta::Pointer(rsp as usize)

View File

@ -63,7 +63,7 @@ interrupt_entry_UNSUPPORTED:
call interrupt_req_UNSUPPORTED
interrupt_entry_leave
iret
iretq
global interrupt_entry_DE
extern interrupt_req_DE
@ -75,12 +75,12 @@ interrupt_entry_DE:
call interrupt_req_DE
interrupt_entry_leave
iret
iretq
global interrupt_entry_NMI
interrupt_entry_NMI:
; TODO 暂时不需要为这个中断实现任何功能
iret
iretq
global interrupt_entry_BP
extern interrupt_req_BP
@ -94,7 +94,7 @@ interrupt_entry_BP:
call interrupt_req_BP
interrupt_entry_leave
iret
iretq
global interrupt_entry_OF
extern interrupt_req_OF
@ -106,4 +106,4 @@ interrupt_entry_OF:
call interrupt_req_OF
interrupt_entry_leave
iret
iretq

View File

@ -1,10 +1,4 @@
use crate::{
kernel::{
klog::{KernelLogger, LoggerLevel},
tty::tty::Tty,
},
message_raw,
};
use crate::kernel::tty::tty::Tty;
#[no_mangle]
extern "C" fn kmain_rust() -> ! {

View File

@ -1,50 +1,5 @@
pub mod tty;
#[macro_export]
macro_rules! message_msg {
() => {};
( $builder : expr, $e : expr) => {
$builder.message_mut($e);
};
}
#[macro_export]
macro_rules! message_fgc {
() => {};
( $builder : expr, $e : expr) => {
$builder.foreground_color_mut($e);
};
}
#[macro_export]
macro_rules! message_bgc {
() => {};
( $builder : expr, $e : expr ) => {
$builder.background_color_mut($e);
};
}
#[macro_export]
macro_rules! message_raw {
( $( $e : expr ),* ) => {{
use crate::{
kernel::tty::tty::{MessageBuilder, BuilderFunctions::*, Color},
message_msg, message_fgc, message_bgc
};
let mut tmp_builder = MessageBuilder::new();
$(
if let Msg(e) = $e {
message_msg!(tmp_builder, &e);
} else if let FgColor(c) = $e {
message_fgc!(tmp_builder, c);
} else if let BgColor(c) = $e {
message_bgc!(tmp_builder, c);
}
)*
tmp_builder.build()
}};
}
#[macro_export]
macro_rules! message {
( $fmtter : expr ) => {{

View File

@ -77,6 +77,7 @@ inline static void scroll_buffer(
usize dist)
{
memcpy(buffer, buffer + width * pixsize * dist, (height - dist) * width * pixsize);
memset(buffer + (height - dist) * width * pixsize, 0, dist * width * pixsize);
}
inline static void putchar(

View File

@ -6,10 +6,18 @@ extern crate alloc;
use core::panic::PanicInfo;
use kernel::tty::tty::Tty;
pub mod kernel;
pub mod libk;
#[panic_handler]
unsafe fn kernel_panic_handler(_info: &PanicInfo) -> ! {
let tty = Tty::from_id(0).unwrap();
tty.enable();
tty.print(message!(
"Hit a {Rust} panic.",
FmtMeta::Color(Color::ORANGE)
));
loop {}
}