重写rust核心库,构建中断处理框架 #6

Merged
pointer-to-bios merged 18 commits from pointer-to-bios/metaverse-dev:main into main 2024-05-03 05:45:43 +08:00
13 changed files with 117 additions and 203 deletions
Showing only changes of commit 41dd0a0e00 - Show all commits

View File

@ -77,11 +77,13 @@ typedef void (*interrupt_request)(u64 rip, u64 rsp, u64 errcode);
#define DE
#define NMI
#define BP
#define OF
interrupt_entry_gen(UNSUPPORTED);
interrupt_entry_gen(DE);
interrupt_entry_gen(NMI);
interrupt_entry_gen(BP);
interrupt_entry_gen(DE); // irq0
interrupt_entry_gen(NMI); // irq2
interrupt_entry_gen(BP); // irq3
interrupt_entry_gen(OF); // ira4
#endif

View File

@ -13,8 +13,7 @@ ifdef release
CCFLAGS := ${CCFLAGS} -O2
endif
C_SRCS = main.c tty.c font.c memm.c memm_${ARCH}.c raw.c time.c syscall_${ARCH}.c interrupt_${ARCH}.c \
interrupt_procs.c
C_SRCS = main.c tty.c font.c memm.c memm_${ARCH}.c raw.c time.c syscall_${ARCH}.c interrupt_${ARCH}.c
C_OBJS = ${C_SRCS:.c=.c.o}
################################

2
src/kernel/arch.rs Normal file
View File

@ -0,0 +1,2 @@
#[cfg(target_arch = "x86_64")]
pub mod x86_64;

View File

@ -11,7 +11,8 @@ init64:
mov rsp, rax
mov rdi, rbx
lidt [0x104010]
; 加载idt
lidt [0x104010] ; idt_ptr
; 加载系统调用相关寄存器
; IA32_STAR = 0x0018_0008_0000_0000

View File

@ -21,17 +21,18 @@ init32:
mov dword [edi], 0
; 设置PD0中的PDE
mov ecx, 64
; 以2MB页映射前64MB
mov ecx, 32
mov eax, 0
mov edi, 0x103000 ; PD0
init32_loop0:
mov edx, eax
add edx, 0x183
mov dword [edi], edx
add eax, 0x200000 ; 2MB
add edi, 4
mov dword [edi], 0
add edi, 4
add eax, 0x200000 ; 2MB
loop init32_loop0
; 加载GDTR、段寄存器和TR寄存器

View File

@ -0,0 +1,49 @@
use crate::{kernel::tty::tty::Tty, libk::alloc::string::ToString, message, message_raw};
extern "C" {
pub fn interrupt_open();
pub fn interrupt_close();
// 以下两个函数签名的返回值用于使编译器保留rax寄存器
pub fn interrupt_rust_enter() -> usize;
pub fn interrupt_rust_leave() -> usize;
}
#[no_mangle]
unsafe extern "C" fn interrupt_req_UNSUPPORTED(rip: u64, rsp: u64, errcode: u64) -> ! {
interrupt_rust_enter();
let tty = Tty::from_id(0).unwrap();
tty.enable();
let msg = message_raw!(
Msg("Panic:"),
FgColor::<u8>(Color::RED),
Msg(" Kernel hit an unsupported interrupt\n\twith %rip=0x"),
Msg(rip.to_hex_string()),
Msg(" and %rsp=0x"),
Msg(rsp.to_hex_string())
);
tty.print(msg);
loop {}
}
#[no_mangle]
unsafe extern "C" fn interrupt_req_DE(rip: u64, rsp: u64, errcode: u64) {
interrupt_rust_enter();
let tty = Tty::from_id(0).unwrap();
tty.enable();
tty.print(message!(
"{Panic}: divided by zero. rip=0x{}.\n",
FmtMeta::Color(Color::RED),
FmtMeta::Pointer(rip as usize)
));
loop {}
interrupt_rust_leave();
}
#[no_mangle]
unsafe extern "C" fn interrupt_req_NMI(rip: u64, rsp: u64, errcode: u64) {}
#[no_mangle]
unsafe extern "C" fn interrupt_req_BP(rip: u64, rsp: u64, errcode: u64) {}
#[no_mangle]
unsafe extern "C" fn interrupt_req_OF(rip: u64, rsp: u64, errcode: u64) {}

View File

@ -1,190 +0,0 @@
#include <kernel/arch/x86_64/interrupt_procs.h>
#include <kernel/arch/x86_64/memm.h>
#include <kernel/arch/x86_64/proc.h>
#include <kernel/kernel.h>
#include <kernel/tty.h>
#include <libk/string.h>
interrupt_req_gen(UNSUPPORTED)
{
tty **tty0_option = tty_get(0);
if (tty0_option == nullptr)
{
KERNEL_TODO();
}
tty *tty0 = *tty0_option;
tty_text_print(tty0, "Panic: Unsupported interrupt.", RED, BLACK);
KERNEL_TODO();
}
static void rip_not_cannonical(u64 rip, tty *tty0)
{
char num[17];
memset(num, 0, sizeof(num));
pointer_to_string(rip, num);
tty_text_print(tty0, "Panic", RED, BLACK);
tty_text_print(tty0, ": Unexpected non-cannonical %rip value ", WHITE, BLACK);
tty_text_print(tty0, num, ORANGE, BLACK);
tty_text_print(tty0, ".\n", WHITE, BLACK);
KERNEL_TODO();
}
interrupt_req_gen(DE)
{
tty **tty0_option = tty_get(0);
if (tty0_option == nullptr)
{
KERNEL_TODO();
}
tty *tty0 = *tty0_option;
if (!is_cannonical(rip))
{
rip_not_cannonical(rip, tty0);
}
if (!is_user_address(rip))
{
char nums[34];
memset(nums, 0, sizeof(nums));
pointer_to_string(rip, nums);
pointer_to_string(rsp, nums + 17);
tty_text_print(tty0, "Panic", RED, BLACK);
tty_text_print(tty0, ": Divided by zero occurs in kernel,\n\t", WHITE, BLACK);
tty_text_print(tty0, "with %rip=", WHITE, BLACK);
tty_text_print(tty0, "0x", ORANGE, BLACK);
tty_text_print(tty0, nums, ORANGE, BLACK);
tty_text_print(tty0, ", %rsp=", WHITE, BLACK);
tty_text_print(tty0, "0x", ORANGE, BLACK);
tty_text_print(tty0, nums + 17, ORANGE, BLACK);
tty_text_print(tty0, ".\n", WHITE, BLACK);
KERNEL_TODO();
}
else
{ // TODO 转储并结束进程
KERNEL_TODO();
}
}
interrupt_req_gen(BP)
{
tty **tty0_option = tty_get(0);
if (tty0_option == nullptr)
{
KERNEL_TODO();
}
tty *tty0 = *tty0_option;
if (!is_cannonical(rip))
{
rip_not_cannonical(rip, tty0);
}
proc_texture_registers_t *texture = (void *)errcode;
if (!is_user_address(rip))
{
char nums[34];
memset(nums, 0, sizeof(nums));
pointer_to_string(rip, nums);
pointer_to_string(rsp, nums + 17);
tty_text_print(tty0, "Debug", PURPLE, BLACK);
tty_text_print(tty0, ": Kernel hit a breakpoint,\n\t", WHITE, BLACK);
tty_text_print(tty0, "with %rip=", WHITE, BLACK);
tty_text_print(tty0, "0x", ORANGE, BLACK);
tty_text_print(tty0, nums, ORANGE, BLACK);
tty_text_print(tty0, ", %rsp=", WHITE, BLACK);
tty_text_print(tty0, "0x", ORANGE, BLACK);
tty_text_print(tty0, nums + 17, ORANGE, BLACK);
tty_text_print(tty0, ",\n\t", WHITE, BLACK);
tty_text_print(tty0, "on texture: \n", WHITE, BLACK);
pointer_to_string(texture->rax, nums);
tty_text_print(tty0, "rax\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\t", WHITE, BLACK);
pointer_to_string(texture->rcx, nums);
tty_text_print(tty0, "rcx\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\n", WHITE, BLACK);
pointer_to_string(texture->rbx, nums);
tty_text_print(tty0, "rbx\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\t", WHITE, BLACK);
pointer_to_string(texture->rdx, nums);
tty_text_print(tty0, "rdx\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\n", WHITE, BLACK);
pointer_to_string(texture->rsi, nums);
tty_text_print(tty0, "rsi\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\t", WHITE, BLACK);
pointer_to_string(texture->rdi, nums);
tty_text_print(tty0, "rdi\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\n", WHITE, BLACK);
pointer_to_string(texture->rbp, nums);
tty_text_print(tty0, "rbp\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\t", WHITE, BLACK);
pointer_to_string(texture->rsp, nums);
tty_text_print(tty0, "rsp\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\n", WHITE, BLACK);
pointer_to_string(texture->rip, nums);
tty_text_print(tty0, "rip\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\t", WHITE, BLACK);
pointer_to_string(texture->rflags, nums);
tty_text_print(tty0, "rflags\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\n", WHITE, BLACK);
pointer_to_string(texture->r8, nums);
tty_text_print(tty0, "r8\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\t", WHITE, BLACK);
pointer_to_string(texture->r9, nums);
tty_text_print(tty0, "r9\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\n", WHITE, BLACK);
pointer_to_string(texture->r10, nums);
tty_text_print(tty0, "r10\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\t", WHITE, BLACK);
pointer_to_string(texture->r11, nums);
tty_text_print(tty0, "r11\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\n", WHITE, BLACK);
pointer_to_string(texture->r12, nums);
tty_text_print(tty0, "r12\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\t", WHITE, BLACK);
pointer_to_string(texture->r13, nums);
tty_text_print(tty0, "r13\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\n", WHITE, BLACK);
pointer_to_string(texture->r14, nums);
tty_text_print(tty0, "r14\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\t", WHITE, BLACK);
pointer_to_string(texture->r15, nums);
tty_text_print(tty0, "r15\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\n", WHITE, BLACK);
pointer_to_string(texture->cs, nums);
tty_text_print(tty0, "cs\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\t", WHITE, BLACK);
pointer_to_string(texture->ss, nums);
tty_text_print(tty0, "ss\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\n", WHITE, BLACK);
pointer_to_string(texture->ds, nums);
tty_text_print(tty0, "ds\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\t", WHITE, BLACK);
pointer_to_string(texture->es, nums);
tty_text_print(tty0, "es\t", WHITE, BLACK);
tty_text_print(tty0, nums, BLUE, BLACK);
tty_text_print(tty0, "\n", WHITE, BLACK);
KERNEL_TODO();
}
else
{ // TODO 将当前进程的状态设置为暂停并通知当前进程的调试程序
KERNEL_TODO();
}
}

View File

@ -77,7 +77,7 @@ interrupt_entry_DE:
interrupt_entry_leave
iret
global interrupt_entry_DE
global interrupt_entry_NMI
interrupt_entry_NMI:
; TODO 暂时不需要为这个中断实现任何功能
iret
@ -95,3 +95,15 @@ interrupt_entry_BP:
interrupt_entry_leave
iret
global interrupt_entry_OF
extern interrupt_req_OF
interrupt_entry_OF:
interrupt_entry_enter
mov rdi, [rsp + 128]
mov rsi, [rsp + 152]
call interrupt_req_OF
interrupt_entry_leave
iret

View File

@ -20,6 +20,8 @@ void interrupt_init()
interrupt_register_gate(gate, 2);
trap_gate_generate(gate, interrupt_entry_sym(BP));
interrupt_register_gate(gate, 3);
trap_gate_generate(gate, interrupt_entry_sym(OF));
interrupt_register_gate(gate, 4);
interrupt_open();
}

View File

@ -0,0 +1,2 @@
pub mod interrupt;
pub mod proc;

View File

@ -0,0 +1,32 @@
#[repr(C)]
#[derive(Debug)]
pub struct RegisterTexture {
es: u16,
ds: u16,
reserved: u32,
r15: u64,
r14: u64,
r13: u64,
r12: u64,
r11: u64,
r10: u64,
r9: u64,
r8: u64,
rdi: u64,
rsi: u64,
rdx: u64,
rbx: u64,
rcx: u64,
rax: u64,
rbp: u64,
rip: u64,
cs: u64,
rflags: u64,
rsp: u64,
ss: u64,
}
#[derive(Debug)]
pub struct ProcessTexture {
register: RegisterTexture,
}

View File

@ -48,6 +48,8 @@ void kmain(void *mb2_bootinfo)
// 初始化中断管理
interrupt_init();
int i = 1 / 0;
// 初始化系统调用
syscall_init();

View File

@ -3,4 +3,4 @@ pub mod klog;
pub mod main;
pub mod memm;
pub mod tty;
pub mod sync;
pub mod arch;