diff --git a/include/kernel/arch/x86_64/interrupt_procs.h b/include/kernel/arch/x86_64/interrupt_procs.h index cd6d2a0..f4a124d 100644 --- a/include/kernel/arch/x86_64/interrupt_procs.h +++ b/include/kernel/arch/x86_64/interrupt_procs.h @@ -46,13 +46,13 @@ typedef void (*interrupt_entry)(); /** * @name interrupt_request * @addindex 平台定制函数 - * + * * 中断请求处理程序。 - * + * * ```c * void interrupt_request(u64 rip, u64 rsp); * ``` - * + * * 由中断入口程序调用。 */ typedef void (*interrupt_request)(u64 rip, u64 rsp, u64 errcode); @@ -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 diff --git a/src/kernel/Makefile b/src/kernel/Makefile index 7346f5b..39d4401 100644 --- a/src/kernel/Makefile +++ b/src/kernel/Makefile @@ -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} ################################ diff --git a/src/kernel/arch.rs b/src/kernel/arch.rs new file mode 100644 index 0000000..832557e --- /dev/null +++ b/src/kernel/arch.rs @@ -0,0 +1,2 @@ +#[cfg(target_arch = "x86_64")] +pub mod x86_64; diff --git a/src/kernel/arch/x86_64/entry.s b/src/kernel/arch/x86_64/entry.s index 2647c42..4e700fc 100644 --- a/src/kernel/arch/x86_64/entry.s +++ b/src/kernel/arch/x86_64/entry.s @@ -11,7 +11,8 @@ init64: mov rsp, rax mov rdi, rbx - lidt [0x104010] + ; 加载idt + lidt [0x104010] ; idt_ptr ; 加载系统调用相关寄存器 ; IA32_STAR = 0x0018_0008_0000_0000 diff --git a/src/kernel/arch/x86_64/entry32.s b/src/kernel/arch/x86_64/entry32.s index d6f99f6..ee6820f 100644 --- a/src/kernel/arch/x86_64/entry32.s +++ b/src/kernel/arch/x86_64/entry32.s @@ -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寄存器 diff --git a/src/kernel/arch/x86_64/interrupt.rs b/src/kernel/arch/x86_64/interrupt.rs new file mode 100644 index 0000000..7fb3265 --- /dev/null +++ b/src/kernel/arch/x86_64/interrupt.rs @@ -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::(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) {} diff --git a/src/kernel/arch/x86_64/interrupt_procs.c b/src/kernel/arch/x86_64/interrupt_procs.c deleted file mode 100644 index 92359ac..0000000 --- a/src/kernel/arch/x86_64/interrupt_procs.c +++ /dev/null @@ -1,190 +0,0 @@ -#include -#include -#include -#include -#include -#include - -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(); - } -} diff --git a/src/kernel/arch/x86_64/interrupt_procs.s b/src/kernel/arch/x86_64/interrupt_procs.s index d83e19e..c5a3a02 100644 --- a/src/kernel/arch/x86_64/interrupt_procs.s +++ b/src/kernel/arch/x86_64/interrupt_procs.s @@ -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 diff --git a/src/kernel/arch/x86_64/interrupt_x86_64.c b/src/kernel/arch/x86_64/interrupt_x86_64.c index 6879f22..c2e2eb6 100644 --- a/src/kernel/arch/x86_64/interrupt_x86_64.c +++ b/src/kernel/arch/x86_64/interrupt_x86_64.c @@ -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(); } diff --git a/src/kernel/arch/x86_64/mod.rs b/src/kernel/arch/x86_64/mod.rs new file mode 100644 index 0000000..9fe4e4d --- /dev/null +++ b/src/kernel/arch/x86_64/mod.rs @@ -0,0 +1,2 @@ +pub mod interrupt; +pub mod proc; diff --git a/src/kernel/arch/x86_64/proc.rs b/src/kernel/arch/x86_64/proc.rs new file mode 100644 index 0000000..1961f31 --- /dev/null +++ b/src/kernel/arch/x86_64/proc.rs @@ -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, +} diff --git a/src/kernel/main.c b/src/kernel/main.c index 495e4b2..161fa04 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -48,6 +48,8 @@ void kmain(void *mb2_bootinfo) // 初始化中断管理 interrupt_init(); + int i = 1 / 0; + // 初始化系统调用 syscall_init(); diff --git a/src/kernel/mod.rs b/src/kernel/mod.rs index 583319c..fc90c48 100644 --- a/src/kernel/mod.rs +++ b/src/kernel/mod.rs @@ -3,4 +3,4 @@ pub mod klog; pub mod main; pub mod memm; pub mod tty; -pub mod sync; +pub mod arch;