xref: /DragonOS/kernel/src/arch/x86_64/interrupt/trap.rs (revision a17651b14b86dd70655090381db4a2f710853aa1)
1f2022a8aSLoGin use system_error::SystemError;
2f2022a8aSLoGin 
3f2022a8aSLoGin use crate::{
4*a17651b1SMemoryShore     arch::{CurrentIrqArch, MMArch},
5*a17651b1SMemoryShore     exception::InterruptArch,
6*a17651b1SMemoryShore     kerror, kwarn,
7*a17651b1SMemoryShore     mm::VirtAddr,
8*a17651b1SMemoryShore     process::ProcessManager,
9*a17651b1SMemoryShore     smp::core::smp_get_processor_id,
10f2022a8aSLoGin };
11f2022a8aSLoGin 
12f2022a8aSLoGin use super::{
13f2022a8aSLoGin     entry::{set_intr_gate, set_system_trap_gate},
14f2022a8aSLoGin     TrapFrame,
15f2022a8aSLoGin };
16f2022a8aSLoGin 
17f2022a8aSLoGin extern "C" {
18f2022a8aSLoGin     fn trap_divide_error();
19f2022a8aSLoGin     fn trap_debug();
20f2022a8aSLoGin     fn trap_nmi();
21f2022a8aSLoGin     fn trap_int3();
22f2022a8aSLoGin     fn trap_overflow();
23f2022a8aSLoGin     fn trap_bounds();
24f2022a8aSLoGin     fn trap_undefined_opcode();
25f2022a8aSLoGin     fn trap_dev_not_avaliable();
26f2022a8aSLoGin     fn trap_double_fault();
27f2022a8aSLoGin     fn trap_coprocessor_segment_overrun();
28f2022a8aSLoGin     fn trap_invalid_TSS();
29f2022a8aSLoGin     fn trap_segment_not_exists();
30f2022a8aSLoGin     fn trap_stack_segment_fault();
31f2022a8aSLoGin     fn trap_general_protection();
32f2022a8aSLoGin     fn trap_page_fault();
33f2022a8aSLoGin     fn trap_x87_FPU_error();
34f2022a8aSLoGin     fn trap_alignment_check();
35f2022a8aSLoGin     fn trap_machine_check();
36f2022a8aSLoGin     fn trap_SIMD_exception();
37f2022a8aSLoGin     fn trap_virtualization_exception();
38f2022a8aSLoGin }
39f2022a8aSLoGin 
40*a17651b1SMemoryShore bitflags! {
41*a17651b1SMemoryShore     pub struct TrapNr: u64 {
42*a17651b1SMemoryShore         const X86_TRAP_DE = 0;
43*a17651b1SMemoryShore         const X86_TRAP_DB = 1;
44*a17651b1SMemoryShore         const X86_TRAP_NMI = 2;
45*a17651b1SMemoryShore         const X86_TRAP_BP = 3;
46*a17651b1SMemoryShore         const X86_TRAP_OF = 4;
47*a17651b1SMemoryShore         const X86_TRAP_BR = 5;
48*a17651b1SMemoryShore         const X86_TRAP_UD = 6;
49*a17651b1SMemoryShore         const X86_TRAP_NM = 7;
50*a17651b1SMemoryShore         const X86_TRAP_DF = 8;
51*a17651b1SMemoryShore         const X86_TRAP_OLD_MF = 9;
52*a17651b1SMemoryShore         const X86_TRAP_TS = 10;
53*a17651b1SMemoryShore         const X86_TRAP_NP = 11;
54*a17651b1SMemoryShore         const X86_TRAP_SS = 12;
55*a17651b1SMemoryShore         const X86_TRAP_GP = 13;
56*a17651b1SMemoryShore         const X86_TRAP_PF = 14;
57*a17651b1SMemoryShore         const X86_TRAP_SPURIOUS = 15;
58*a17651b1SMemoryShore         const X86_TRAP_MF = 16;
59*a17651b1SMemoryShore         const X86_TRAP_AC = 17;
60*a17651b1SMemoryShore         const X86_TRAP_MC = 18;
61*a17651b1SMemoryShore         const X86_TRAP_XF = 19;
62*a17651b1SMemoryShore         const X86_TRAP_VE = 20;
63*a17651b1SMemoryShore         const X86_TRAP_CP = 21;
64*a17651b1SMemoryShore         const X86_TRAP_VC = 29;
65*a17651b1SMemoryShore         const X86_TRAP_IRET = 32;
66*a17651b1SMemoryShore     }
67*a17651b1SMemoryShore 
68*a17651b1SMemoryShore         pub struct X86PfErrorCode : u32{
69*a17651b1SMemoryShore         const X86_PF_PROT = 1 << 0;
70*a17651b1SMemoryShore         const X86_PF_WRITE = 1 << 1;
71*a17651b1SMemoryShore         const X86_PF_USER = 1 << 2;
72*a17651b1SMemoryShore         const X86_PF_RSVD = 1 << 3;
73*a17651b1SMemoryShore         const X86_PF_INSTR = 1 << 4;
74*a17651b1SMemoryShore         const X86_PF_PK = 1 << 5;
75*a17651b1SMemoryShore         const X86_PF_SHSTK = 1 << 6;
76*a17651b1SMemoryShore         const X86_PF_SGX = 1 << 15;
77*a17651b1SMemoryShore     }
78*a17651b1SMemoryShore }
79*a17651b1SMemoryShore 
80f2022a8aSLoGin #[inline(never)]
81f2022a8aSLoGin pub fn arch_trap_init() -> Result<(), SystemError> {
82f2022a8aSLoGin     unsafe {
83f2022a8aSLoGin         set_intr_gate(0, 0, VirtAddr::new(trap_divide_error as usize));
84f2022a8aSLoGin         set_intr_gate(1, 0, VirtAddr::new(trap_debug as usize));
85f2022a8aSLoGin         set_intr_gate(2, 0, VirtAddr::new(trap_nmi as usize));
86f2022a8aSLoGin         set_system_trap_gate(3, 0, VirtAddr::new(trap_int3 as usize));
87f2022a8aSLoGin         set_system_trap_gate(4, 0, VirtAddr::new(trap_overflow as usize));
88f2022a8aSLoGin         set_system_trap_gate(5, 0, VirtAddr::new(trap_bounds as usize));
89f2022a8aSLoGin         set_intr_gate(6, 0, VirtAddr::new(trap_undefined_opcode as usize));
90f2022a8aSLoGin         set_intr_gate(7, 0, VirtAddr::new(trap_dev_not_avaliable as usize));
91f2022a8aSLoGin         set_intr_gate(8, 0, VirtAddr::new(trap_double_fault as usize));
92f2022a8aSLoGin         set_intr_gate(
93f2022a8aSLoGin             9,
94f2022a8aSLoGin             0,
95f2022a8aSLoGin             VirtAddr::new(trap_coprocessor_segment_overrun as usize),
96f2022a8aSLoGin         );
97f2022a8aSLoGin         set_intr_gate(10, 0, VirtAddr::new(trap_invalid_TSS as usize));
98f2022a8aSLoGin         set_intr_gate(11, 0, VirtAddr::new(trap_segment_not_exists as usize));
99f2022a8aSLoGin         set_intr_gate(12, 0, VirtAddr::new(trap_stack_segment_fault as usize));
100f2022a8aSLoGin         set_intr_gate(13, 0, VirtAddr::new(trap_general_protection as usize));
101f2022a8aSLoGin         set_intr_gate(14, 0, VirtAddr::new(trap_page_fault as usize));
102f2022a8aSLoGin         // 中断号15由Intel保留,不能使用
103f2022a8aSLoGin         set_intr_gate(16, 0, VirtAddr::new(trap_x87_FPU_error as usize));
104f2022a8aSLoGin         set_intr_gate(17, 0, VirtAddr::new(trap_alignment_check as usize));
105f2022a8aSLoGin         set_intr_gate(18, 0, VirtAddr::new(trap_machine_check as usize));
106f2022a8aSLoGin         set_intr_gate(19, 0, VirtAddr::new(trap_SIMD_exception as usize));
107f2022a8aSLoGin         set_intr_gate(20, 0, VirtAddr::new(trap_virtualization_exception as usize));
108f2022a8aSLoGin     }
109f2022a8aSLoGin     return Ok(());
110f2022a8aSLoGin }
111f2022a8aSLoGin 
112f2022a8aSLoGin /// 处理除法错误 0 #DE
113f2022a8aSLoGin #[no_mangle]
114f2022a8aSLoGin unsafe extern "C" fn do_divide_error(regs: &'static TrapFrame, error_code: u64) {
115f2022a8aSLoGin     kerror!(
116f2022a8aSLoGin         "do_divide_error(0), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
117f2022a8aSLoGin         error_code,
118f2022a8aSLoGin         regs.rsp,
119f2022a8aSLoGin         regs.rip,
120e2841179SLoGin         smp_get_processor_id().data(),
121f2022a8aSLoGin         ProcessManager::current_pid()
122f2022a8aSLoGin     );
123f2022a8aSLoGin     panic!("Divide Error");
124f2022a8aSLoGin }
125f2022a8aSLoGin 
126f2022a8aSLoGin /// 处理调试异常 1 #DB
127f2022a8aSLoGin #[no_mangle]
128f2022a8aSLoGin unsafe extern "C" fn do_debug(regs: &'static TrapFrame, error_code: u64) {
129f2022a8aSLoGin     kerror!(
130f2022a8aSLoGin         "do_debug(1), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
131f2022a8aSLoGin         error_code,
132f2022a8aSLoGin         regs.rsp,
133f2022a8aSLoGin         regs.rip,
134e2841179SLoGin         smp_get_processor_id().data(),
135f2022a8aSLoGin         ProcessManager::current_pid()
136f2022a8aSLoGin     );
137f2022a8aSLoGin     panic!("Debug Exception");
138f2022a8aSLoGin }
139f2022a8aSLoGin 
140f2022a8aSLoGin /// 处理NMI中断 2 NMI
141f2022a8aSLoGin #[no_mangle]
142f2022a8aSLoGin unsafe extern "C" fn do_nmi(regs: &'static TrapFrame, error_code: u64) {
143f2022a8aSLoGin     kerror!(
144f2022a8aSLoGin         "do_nmi(2), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
145f2022a8aSLoGin         error_code,
146f2022a8aSLoGin         regs.rsp,
147f2022a8aSLoGin         regs.rip,
148e2841179SLoGin         smp_get_processor_id().data(),
149f2022a8aSLoGin         ProcessManager::current_pid()
150f2022a8aSLoGin     );
151f2022a8aSLoGin     panic!("NMI Interrupt");
152f2022a8aSLoGin }
153f2022a8aSLoGin 
154f2022a8aSLoGin /// 处理断点异常 3 #BP
155f2022a8aSLoGin #[no_mangle]
156f2022a8aSLoGin unsafe extern "C" fn do_int3(regs: &'static TrapFrame, error_code: u64) {
157f2022a8aSLoGin     kerror!(
158f2022a8aSLoGin         "do_int3(3), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
159f2022a8aSLoGin         error_code,
160f2022a8aSLoGin         regs.rsp,
161f2022a8aSLoGin         regs.rip,
162e2841179SLoGin         smp_get_processor_id().data(),
163f2022a8aSLoGin         ProcessManager::current_pid()
164f2022a8aSLoGin     );
165f2022a8aSLoGin     panic!("Int3");
166f2022a8aSLoGin }
167f2022a8aSLoGin 
168f2022a8aSLoGin /// 处理溢出异常 4 #OF
169f2022a8aSLoGin #[no_mangle]
170f2022a8aSLoGin unsafe extern "C" fn do_overflow(regs: &'static TrapFrame, error_code: u64) {
171f2022a8aSLoGin     kerror!(
172f2022a8aSLoGin         "do_overflow(4), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
173f2022a8aSLoGin         error_code,
174f2022a8aSLoGin         regs.rsp,
175f2022a8aSLoGin         regs.rip,
176e2841179SLoGin         smp_get_processor_id().data(),
177f2022a8aSLoGin         ProcessManager::current_pid()
178f2022a8aSLoGin     );
179f2022a8aSLoGin     panic!("Overflow Exception");
180f2022a8aSLoGin }
181f2022a8aSLoGin 
182f2022a8aSLoGin /// 处理BOUND指令检查异常 5 #BR
183f2022a8aSLoGin #[no_mangle]
184f2022a8aSLoGin unsafe extern "C" fn do_bounds(regs: &'static TrapFrame, error_code: u64) {
185f2022a8aSLoGin     kerror!(
186f2022a8aSLoGin         "do_bounds(5), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
187f2022a8aSLoGin         error_code,
188f2022a8aSLoGin         regs.rsp,
189f2022a8aSLoGin         regs.rip,
190e2841179SLoGin         smp_get_processor_id().data(),
191f2022a8aSLoGin         ProcessManager::current_pid()
192f2022a8aSLoGin     );
193f2022a8aSLoGin     panic!("Bounds Check");
194f2022a8aSLoGin }
195f2022a8aSLoGin 
196f2022a8aSLoGin /// 处理未定义操作码异常 6 #UD
197f2022a8aSLoGin #[no_mangle]
198f2022a8aSLoGin unsafe extern "C" fn do_undefined_opcode(regs: &'static TrapFrame, error_code: u64) {
199f2022a8aSLoGin     kerror!(
200f2022a8aSLoGin         "do_undefined_opcode(6), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
201f2022a8aSLoGin         error_code,
202f2022a8aSLoGin         regs.rsp,
203f2022a8aSLoGin         regs.rip,
204e2841179SLoGin         smp_get_processor_id().data(),
205f2022a8aSLoGin         ProcessManager::current_pid()
206f2022a8aSLoGin     );
207f2022a8aSLoGin     panic!("Undefined Opcode");
208f2022a8aSLoGin }
209f2022a8aSLoGin 
210f2022a8aSLoGin /// 处理设备不可用异常(FPU不存在) 7 #NM
211f2022a8aSLoGin #[no_mangle]
212f2022a8aSLoGin unsafe extern "C" fn do_dev_not_avaliable(regs: &'static TrapFrame, error_code: u64) {
213f2022a8aSLoGin     kerror!(
214f2022a8aSLoGin         "do_dev_not_avaliable(7), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
215f2022a8aSLoGin         error_code,
216f2022a8aSLoGin         regs.rsp,
217f2022a8aSLoGin         regs.rip,
218e2841179SLoGin         smp_get_processor_id().data(),
219f2022a8aSLoGin         ProcessManager::current_pid()
220f2022a8aSLoGin     );
221f2022a8aSLoGin     panic!("Device Not Available");
222f2022a8aSLoGin }
223f2022a8aSLoGin 
224f2022a8aSLoGin /// 处理双重错误 8 #DF
225f2022a8aSLoGin #[no_mangle]
226f2022a8aSLoGin unsafe extern "C" fn do_double_fault(regs: &'static TrapFrame, error_code: u64) {
227f2022a8aSLoGin     kerror!(
228f2022a8aSLoGin         "do_double_fault(8), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
229f2022a8aSLoGin         error_code,
230f2022a8aSLoGin         regs.rsp,
231f2022a8aSLoGin         regs.rip,
232e2841179SLoGin         smp_get_processor_id().data(),
233f2022a8aSLoGin         ProcessManager::current_pid()
234f2022a8aSLoGin     );
235f2022a8aSLoGin     panic!("Double Fault");
236f2022a8aSLoGin }
237f2022a8aSLoGin 
238f2022a8aSLoGin /// 处理协处理器段越界 9 #MF
239f2022a8aSLoGin #[no_mangle]
240f2022a8aSLoGin unsafe extern "C" fn do_coprocessor_segment_overrun(regs: &'static TrapFrame, error_code: u64) {
241f2022a8aSLoGin     kerror!(
242f2022a8aSLoGin         "do_coprocessor_segment_overrun(9), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
243f2022a8aSLoGin         error_code,
244f2022a8aSLoGin         regs.rsp,
245f2022a8aSLoGin         regs.rip,
246e2841179SLoGin         smp_get_processor_id().data(),
247f2022a8aSLoGin         ProcessManager::current_pid()
248f2022a8aSLoGin     );
249f2022a8aSLoGin     panic!("Coprocessor Segment Overrun");
250f2022a8aSLoGin }
251f2022a8aSLoGin 
252f2022a8aSLoGin /// 处理无效TSS 10 #TS
253f2022a8aSLoGin #[no_mangle]
254f2022a8aSLoGin unsafe extern "C" fn do_invalid_TSS(regs: &'static TrapFrame, error_code: u64) {
255f2022a8aSLoGin     const ERR_MSG_1: &str =
256f2022a8aSLoGin         "The exception occurred during delivery of an event external to the program.\n";
257f2022a8aSLoGin     const ERR_MSG_2: &str = "Refers to a descriptor in the IDT.\n";
258f2022a8aSLoGin     const ERR_MSG_3: &str = "Refers to a descriptor in the current LDT.\n";
259f2022a8aSLoGin     const ERR_MSG_4: &str = "Refers to a descriptor in the GDT.\n";
260f2022a8aSLoGin 
261b5b571e0SLoGin     let msg1: &str = if (error_code & 0x1) != 0 {
262b5b571e0SLoGin         ERR_MSG_1
263f2022a8aSLoGin     } else {
264b5b571e0SLoGin         ""
265b5b571e0SLoGin     };
266f2022a8aSLoGin 
267b5b571e0SLoGin     let msg2: &str = if (error_code & 0x02) != 0 {
268b5b571e0SLoGin         ERR_MSG_2
269b5b571e0SLoGin     } else if (error_code & 0x04) != 0 {
270b5b571e0SLoGin         ERR_MSG_3
271f2022a8aSLoGin     } else {
272b5b571e0SLoGin         ERR_MSG_4
273b5b571e0SLoGin     };
274b5b571e0SLoGin 
275f2022a8aSLoGin     kerror!(
276f2022a8aSLoGin         "do_invalid_TSS(10), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}\n{}{}",
277f2022a8aSLoGin         error_code,
278f2022a8aSLoGin         regs.rsp,
279f2022a8aSLoGin         regs.rip,
280e2841179SLoGin         smp_get_processor_id().data(),
281f2022a8aSLoGin         ProcessManager::current_pid(),
282f2022a8aSLoGin         msg1,
283f2022a8aSLoGin         msg2
284f2022a8aSLoGin     );
285f2022a8aSLoGin     panic!("Invalid TSS");
286f2022a8aSLoGin }
287f2022a8aSLoGin 
288f2022a8aSLoGin /// 处理段不存在 11 #NP
289f2022a8aSLoGin #[no_mangle]
290f2022a8aSLoGin unsafe extern "C" fn do_segment_not_exists(regs: &'static TrapFrame, error_code: u64) {
291f2022a8aSLoGin     kerror!(
292f2022a8aSLoGin         "do_segment_not_exists(11), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
293f2022a8aSLoGin         error_code,
294f2022a8aSLoGin         regs.rsp,
295f2022a8aSLoGin         regs.rip,
296e2841179SLoGin         smp_get_processor_id().data(),
297f2022a8aSLoGin         ProcessManager::current_pid()
298f2022a8aSLoGin     );
299f2022a8aSLoGin     panic!("Segment Not Exists");
300f2022a8aSLoGin }
301f2022a8aSLoGin 
302f2022a8aSLoGin /// 处理栈段错误 12 #SS
303f2022a8aSLoGin #[no_mangle]
304f2022a8aSLoGin unsafe extern "C" fn do_stack_segment_fault(regs: &'static TrapFrame, error_code: u64) {
305f2022a8aSLoGin     kerror!(
306f2022a8aSLoGin         "do_stack_segment_fault(12), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
307f2022a8aSLoGin         error_code,
308f2022a8aSLoGin         regs.rsp,
309f2022a8aSLoGin         regs.rip,
310e2841179SLoGin         smp_get_processor_id().data(),
311f2022a8aSLoGin         ProcessManager::current_pid()
312f2022a8aSLoGin     );
313f2022a8aSLoGin     panic!("Stack Segment Fault");
314f2022a8aSLoGin }
315f2022a8aSLoGin 
316f2022a8aSLoGin /// 处理一般保护异常 13 #GP
317f2022a8aSLoGin #[no_mangle]
318f2022a8aSLoGin unsafe extern "C" fn do_general_protection(regs: &'static TrapFrame, error_code: u64) {
319f2022a8aSLoGin     const ERR_MSG_1: &str = "The exception occurred during delivery of an event external to the program, such as an interrupt or an earlier exception.";
320f2022a8aSLoGin     const ERR_MSG_2: &str = "Refers to a gate descriptor in the IDT;\n";
321f2022a8aSLoGin     const ERR_MSG_3: &str = "Refers to a descriptor in the GDT or the current LDT;\n";
322f2022a8aSLoGin     const ERR_MSG_4: &str = "Refers to a segment or gate descriptor in the LDT;\n";
323f2022a8aSLoGin     const ERR_MSG_5: &str = "Refers to a descriptor in the current GDT;\n";
324f2022a8aSLoGin 
325b5b571e0SLoGin     let msg1: &str = if (error_code & 0x1) != 0 {
326b5b571e0SLoGin         ERR_MSG_1
327f2022a8aSLoGin     } else {
328b5b571e0SLoGin         ""
329b5b571e0SLoGin     };
330f2022a8aSLoGin 
331b5b571e0SLoGin     let msg2: &str = if (error_code & 0x02) != 0 {
332b5b571e0SLoGin         ERR_MSG_2
333f2022a8aSLoGin     } else {
334b5b571e0SLoGin         ERR_MSG_3
335b5b571e0SLoGin     };
336f2022a8aSLoGin 
337b5b571e0SLoGin     let msg3: &str = if (error_code & 0x02) == 0 {
338f2022a8aSLoGin         if (error_code & 0x04) != 0 {
339b5b571e0SLoGin             ERR_MSG_4
340f2022a8aSLoGin         } else {
341b5b571e0SLoGin             ERR_MSG_5
342f2022a8aSLoGin         }
343f2022a8aSLoGin     } else {
344b5b571e0SLoGin         ""
345b5b571e0SLoGin     };
346f2022a8aSLoGin     kerror!(
3473959e94dS曾俊         "do_general_protection(13), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t rflags: {:#x}\t CPU: {}, \tpid: {:?}
348f2022a8aSLoGin {}{}{}
349f2022a8aSLoGin Segment Selector Index: {:#x}\n
350f2022a8aSLoGin ",
351f2022a8aSLoGin         error_code,
352f2022a8aSLoGin         regs.rsp,
353f2022a8aSLoGin         regs.rip,
3543959e94dS曾俊         regs.rflags,
355e2841179SLoGin         smp_get_processor_id().data(),
356f2022a8aSLoGin         ProcessManager::current_pid(),
357f2022a8aSLoGin         msg1, msg2, msg3,
358f2022a8aSLoGin         error_code & 0xfff8
359f2022a8aSLoGin     );
360f2022a8aSLoGin     panic!("General Protection");
361f2022a8aSLoGin }
362f2022a8aSLoGin 
363f2022a8aSLoGin /// 处理页错误 14 #PF
364f2022a8aSLoGin #[no_mangle]
365f2022a8aSLoGin unsafe extern "C" fn do_page_fault(regs: &'static TrapFrame, error_code: u64) {
366*a17651b1SMemoryShore     // kerror!(
367*a17651b1SMemoryShore     //     "do_page_fault(14), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}, \nFault Address: {:#x}",
368*a17651b1SMemoryShore     //     error_code,
369*a17651b1SMemoryShore     //     regs.rsp,
370*a17651b1SMemoryShore     //     regs.rip,
371*a17651b1SMemoryShore     //     smp_get_processor_id().data(),
372*a17651b1SMemoryShore     //     ProcessManager::current_pid(),
373*a17651b1SMemoryShore     //     x86::controlregs::cr2()
374*a17651b1SMemoryShore     // );
375f2022a8aSLoGin 
376*a17651b1SMemoryShore     // if (error_code & 0x01) == 0 {
377*a17651b1SMemoryShore     //     print!("Page Not Present,\t");
378*a17651b1SMemoryShore     // }
379*a17651b1SMemoryShore     // if (error_code & 0x02) != 0 {
380*a17651b1SMemoryShore     //     print!("Write Access,\t");
381*a17651b1SMemoryShore     // } else {
382*a17651b1SMemoryShore     //     print!("Read Access,\t");
383*a17651b1SMemoryShore     // }
384*a17651b1SMemoryShore 
385*a17651b1SMemoryShore     // if (error_code & 0x04) != 0 {
386*a17651b1SMemoryShore     //     print!("Fault in user(3),\t");
387*a17651b1SMemoryShore     // } else {
388*a17651b1SMemoryShore     //     print!("Fault in supervisor(0,1,2),\t");
389*a17651b1SMemoryShore     // }
390*a17651b1SMemoryShore 
391*a17651b1SMemoryShore     // if (error_code & 0x08) != 0 {
392*a17651b1SMemoryShore     //     print!("Reserved bit violation cause fault,\t");
393*a17651b1SMemoryShore     // }
394*a17651b1SMemoryShore 
395*a17651b1SMemoryShore     // if (error_code & 0x10) != 0 {
396*a17651b1SMemoryShore     //     print!("Instruction fetch cause fault,\t");
397*a17651b1SMemoryShore     // }
398*a17651b1SMemoryShore     // print!("\n");
399*a17651b1SMemoryShore 
400*a17651b1SMemoryShore     // CurrentIrqArch::interrupt_enable();
401*a17651b1SMemoryShore     // panic!("Page Fault");
402*a17651b1SMemoryShore     CurrentIrqArch::interrupt_disable();
403*a17651b1SMemoryShore     let address = x86::controlregs::cr2();
404*a17651b1SMemoryShore     // crate::kinfo!(
405*a17651b1SMemoryShore     //     "fault address: {:#x}, error_code: {:#b}, pid: {}\n",
406*a17651b1SMemoryShore     //     address,
407*a17651b1SMemoryShore     //     error_code,
408*a17651b1SMemoryShore     //     ProcessManager::current_pid().data()
409*a17651b1SMemoryShore     // );
410*a17651b1SMemoryShore 
411*a17651b1SMemoryShore     let address = VirtAddr::new(address);
412*a17651b1SMemoryShore     let error_code = X86PfErrorCode::from_bits_truncate(error_code as u32);
413*a17651b1SMemoryShore     if address.check_user() {
414*a17651b1SMemoryShore         MMArch::do_user_addr_fault(regs, error_code, address);
415f2022a8aSLoGin     } else {
416*a17651b1SMemoryShore         MMArch::do_kern_addr_fault(regs, error_code, address);
417f2022a8aSLoGin     }
418f2022a8aSLoGin     CurrentIrqArch::interrupt_enable();
419f2022a8aSLoGin }
420f2022a8aSLoGin 
421f2022a8aSLoGin /// 处理x87 FPU错误 16 #MF
422f2022a8aSLoGin #[no_mangle]
423f2022a8aSLoGin unsafe extern "C" fn do_x87_FPU_error(regs: &'static TrapFrame, error_code: u64) {
424f2022a8aSLoGin     kerror!(
425f2022a8aSLoGin         "do_x87_FPU_error(16), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
426f2022a8aSLoGin         error_code,
427f2022a8aSLoGin         regs.rsp,
428f2022a8aSLoGin         regs.rip,
429e2841179SLoGin         smp_get_processor_id().data(),
430f2022a8aSLoGin         ProcessManager::current_pid()
431f2022a8aSLoGin     );
432f2022a8aSLoGin     panic!("x87 FPU Error");
433f2022a8aSLoGin }
434f2022a8aSLoGin 
435f2022a8aSLoGin /// 处理对齐检查 17 #AC
436f2022a8aSLoGin #[no_mangle]
437f2022a8aSLoGin unsafe extern "C" fn do_alignment_check(regs: &'static TrapFrame, error_code: u64) {
438f2022a8aSLoGin     kerror!(
439f2022a8aSLoGin         "do_alignment_check(17), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
440f2022a8aSLoGin         error_code,
441f2022a8aSLoGin         regs.rsp,
442f2022a8aSLoGin         regs.rip,
443e2841179SLoGin         smp_get_processor_id().data(),
444f2022a8aSLoGin         ProcessManager::current_pid()
445f2022a8aSLoGin     );
446f2022a8aSLoGin     panic!("Alignment Check");
447f2022a8aSLoGin }
448f2022a8aSLoGin 
449f2022a8aSLoGin /// 处理机器检查 18 #MC
450f2022a8aSLoGin #[no_mangle]
451f2022a8aSLoGin unsafe extern "C" fn do_machine_check(regs: &'static TrapFrame, error_code: u64) {
452f2022a8aSLoGin     kerror!(
453f2022a8aSLoGin         "do_machine_check(18), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
454f2022a8aSLoGin         error_code,
455f2022a8aSLoGin         regs.rsp,
456f2022a8aSLoGin         regs.rip,
457e2841179SLoGin         smp_get_processor_id().data(),
458f2022a8aSLoGin         ProcessManager::current_pid()
459f2022a8aSLoGin     );
460f2022a8aSLoGin     panic!("Machine Check");
461f2022a8aSLoGin }
462f2022a8aSLoGin 
463f2022a8aSLoGin /// 处理SIMD异常 19 #XM
464f2022a8aSLoGin #[no_mangle]
465f2022a8aSLoGin unsafe extern "C" fn do_SIMD_exception(regs: &'static TrapFrame, error_code: u64) {
466f2022a8aSLoGin     kerror!(
467f2022a8aSLoGin         "do_SIMD_exception(19), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
468f2022a8aSLoGin         error_code,
469f2022a8aSLoGin         regs.rsp,
470f2022a8aSLoGin         regs.rip,
471e2841179SLoGin         smp_get_processor_id().data(),
472f2022a8aSLoGin         ProcessManager::current_pid()
473f2022a8aSLoGin     );
474f2022a8aSLoGin     panic!("SIMD Exception");
475f2022a8aSLoGin }
476f2022a8aSLoGin 
477f2022a8aSLoGin /// 处理虚拟化异常 20 #VE
478f2022a8aSLoGin #[no_mangle]
479f2022a8aSLoGin unsafe extern "C" fn do_virtualization_exception(regs: &'static TrapFrame, error_code: u64) {
480f2022a8aSLoGin     kerror!(
481f2022a8aSLoGin         "do_virtualization_exception(20), \tError code: {:#x},\trsp: {:#x},\trip: {:#x},\t CPU: {}, \tpid: {:?}",
482f2022a8aSLoGin         error_code,
483f2022a8aSLoGin         regs.rsp,
484f2022a8aSLoGin         regs.rip,
485e2841179SLoGin         smp_get_processor_id().data(),
486f2022a8aSLoGin         ProcessManager::current_pid()
487f2022a8aSLoGin     );
488f2022a8aSLoGin     panic!("Virtualization Exception");
489f2022a8aSLoGin }
490f2022a8aSLoGin 
491f2022a8aSLoGin #[no_mangle]
492f2022a8aSLoGin unsafe extern "C" fn ignore_int_handler(_regs: &'static TrapFrame, _error_code: u64) {
493f2022a8aSLoGin     kwarn!("Unknown interrupt.");
494f2022a8aSLoGin }
495