xref: /DragonOS/kernel/src/arch/x86_64/interrupt/entry.rs (revision e28411791f090c421fe4b6fa5956fb1bd362a8d9)
1f2022a8aSLoGin use crate::{
2f2022a8aSLoGin     arch::MMArch,
3f2022a8aSLoGin     mm::{MemoryManagementArch, PhysAddr, VirtAddr},
4f2022a8aSLoGin };
5f2022a8aSLoGin 
6f2022a8aSLoGin extern "C" {
7f2022a8aSLoGin     static mut IDT_Table: [usize; 0usize];
8f2022a8aSLoGin }
9f2022a8aSLoGin macro_rules! save_all_regs {
10f2022a8aSLoGin     () => {
11f2022a8aSLoGin         "
12f2022a8aSLoGin         cld
13f2022a8aSLoGin         push rax
14f2022a8aSLoGin         push rax
15f2022a8aSLoGin         mov rax, es
16f2022a8aSLoGin         push rax
17f2022a8aSLoGin         mov rax, ds
18f2022a8aSLoGin         push rax
19f2022a8aSLoGin         xor rax, rax
20f2022a8aSLoGin         push rbp
21f2022a8aSLoGin         push rdi
22f2022a8aSLoGin         push rsi
23f2022a8aSLoGin         push rdx
24f2022a8aSLoGin         push rcx
25f2022a8aSLoGin         push rbx
26f2022a8aSLoGin         push r8
27f2022a8aSLoGin         push r9
28f2022a8aSLoGin         push r10
29f2022a8aSLoGin         push r11
30f2022a8aSLoGin         push r12
31f2022a8aSLoGin         push r13
32f2022a8aSLoGin         push r14
33f2022a8aSLoGin         push r15
34f2022a8aSLoGin         mov rdx, 0x10
35f2022a8aSLoGin         mov ds, rdx
36f2022a8aSLoGin         mov es, rdx
37f2022a8aSLoGin         "
38f2022a8aSLoGin     };
39f2022a8aSLoGin }
40f2022a8aSLoGin 
41f2022a8aSLoGin macro_rules! interrupt_handler {
42f2022a8aSLoGin     ($name:expr) => {
43f2022a8aSLoGin         paste::paste! {
44f2022a8aSLoGin 
45f2022a8aSLoGin             #[naked]
46f2022a8aSLoGin             #[no_mangle]
47f2022a8aSLoGin             unsafe extern "C" fn [<irq_handler $name>]() {
48f2022a8aSLoGin                 core::arch::asm!(
49f2022a8aSLoGin                     concat!(
50f2022a8aSLoGin                         "
51f2022a8aSLoGin                         push 0x0
52f2022a8aSLoGin                         ",
53f2022a8aSLoGin                         save_all_regs!(),
54f2022a8aSLoGin                         "\n",
55f2022a8aSLoGin                         "
56f2022a8aSLoGin                         mov rdi, rsp
57f2022a8aSLoGin                         lea rax, ret_from_intr[rip]
58f2022a8aSLoGin                         push rax
59f2022a8aSLoGin                         mov rsi, {irqnum}
60*e2841179SLoGin                         jmp x86_64_do_irq
61*e2841179SLoGin                         // jmp do_IRQ
62f2022a8aSLoGin                         "
63f2022a8aSLoGin                     ),
64f2022a8aSLoGin                     irqnum = const($name),
65f2022a8aSLoGin                     options(noreturn)
66f2022a8aSLoGin                 );
67f2022a8aSLoGin             }
68f2022a8aSLoGin         }
69f2022a8aSLoGin     };
70f2022a8aSLoGin }
71f2022a8aSLoGin 
72f2022a8aSLoGin interrupt_handler!(32);
73f2022a8aSLoGin interrupt_handler!(33);
74f2022a8aSLoGin interrupt_handler!(34);
75f2022a8aSLoGin interrupt_handler!(35);
76f2022a8aSLoGin interrupt_handler!(36);
77f2022a8aSLoGin interrupt_handler!(37);
78f2022a8aSLoGin interrupt_handler!(38);
79f2022a8aSLoGin interrupt_handler!(39);
80f2022a8aSLoGin interrupt_handler!(40);
81f2022a8aSLoGin interrupt_handler!(41);
82f2022a8aSLoGin interrupt_handler!(42);
83f2022a8aSLoGin interrupt_handler!(43);
84f2022a8aSLoGin interrupt_handler!(44);
85f2022a8aSLoGin interrupt_handler!(45);
86f2022a8aSLoGin interrupt_handler!(46);
87f2022a8aSLoGin interrupt_handler!(47);
88f2022a8aSLoGin interrupt_handler!(48);
89f2022a8aSLoGin interrupt_handler!(49);
90f2022a8aSLoGin interrupt_handler!(50);
91f2022a8aSLoGin interrupt_handler!(51);
92f2022a8aSLoGin interrupt_handler!(52);
93f2022a8aSLoGin interrupt_handler!(53);
94f2022a8aSLoGin interrupt_handler!(54);
95f2022a8aSLoGin interrupt_handler!(55);
96f2022a8aSLoGin interrupt_handler!(56);
97f2022a8aSLoGin interrupt_handler!(57);
98f2022a8aSLoGin interrupt_handler!(58);
99f2022a8aSLoGin interrupt_handler!(59);
100f2022a8aSLoGin interrupt_handler!(60);
101f2022a8aSLoGin interrupt_handler!(61);
102f2022a8aSLoGin interrupt_handler!(62);
103f2022a8aSLoGin interrupt_handler!(63);
104f2022a8aSLoGin interrupt_handler!(64);
105f2022a8aSLoGin interrupt_handler!(65);
106f2022a8aSLoGin interrupt_handler!(66);
107f2022a8aSLoGin interrupt_handler!(67);
108f2022a8aSLoGin interrupt_handler!(68);
109f2022a8aSLoGin interrupt_handler!(69);
110f2022a8aSLoGin interrupt_handler!(70);
111f2022a8aSLoGin interrupt_handler!(71);
112f2022a8aSLoGin interrupt_handler!(72);
113f2022a8aSLoGin interrupt_handler!(73);
114f2022a8aSLoGin interrupt_handler!(74);
115f2022a8aSLoGin interrupt_handler!(75);
116f2022a8aSLoGin interrupt_handler!(76);
117f2022a8aSLoGin interrupt_handler!(77);
118f2022a8aSLoGin interrupt_handler!(78);
119f2022a8aSLoGin interrupt_handler!(79);
120f2022a8aSLoGin interrupt_handler!(80);
121f2022a8aSLoGin interrupt_handler!(81);
122f2022a8aSLoGin interrupt_handler!(82);
123f2022a8aSLoGin interrupt_handler!(83);
124f2022a8aSLoGin interrupt_handler!(84);
125f2022a8aSLoGin interrupt_handler!(85);
126f2022a8aSLoGin interrupt_handler!(86);
127f2022a8aSLoGin interrupt_handler!(87);
128f2022a8aSLoGin interrupt_handler!(88);
129f2022a8aSLoGin interrupt_handler!(89);
130f2022a8aSLoGin interrupt_handler!(90);
131f2022a8aSLoGin interrupt_handler!(91);
132f2022a8aSLoGin interrupt_handler!(92);
133f2022a8aSLoGin interrupt_handler!(93);
134f2022a8aSLoGin interrupt_handler!(94);
135f2022a8aSLoGin interrupt_handler!(95);
136f2022a8aSLoGin interrupt_handler!(96);
137f2022a8aSLoGin interrupt_handler!(97);
138f2022a8aSLoGin interrupt_handler!(98);
139f2022a8aSLoGin interrupt_handler!(99);
140f2022a8aSLoGin interrupt_handler!(100);
141f2022a8aSLoGin interrupt_handler!(101);
142f2022a8aSLoGin interrupt_handler!(102);
143f2022a8aSLoGin interrupt_handler!(103);
144f2022a8aSLoGin interrupt_handler!(104);
145f2022a8aSLoGin interrupt_handler!(105);
146f2022a8aSLoGin interrupt_handler!(106);
147f2022a8aSLoGin interrupt_handler!(107);
148f2022a8aSLoGin interrupt_handler!(108);
149f2022a8aSLoGin interrupt_handler!(109);
150f2022a8aSLoGin interrupt_handler!(110);
151f2022a8aSLoGin interrupt_handler!(111);
152f2022a8aSLoGin interrupt_handler!(112);
153f2022a8aSLoGin interrupt_handler!(113);
154f2022a8aSLoGin interrupt_handler!(114);
155f2022a8aSLoGin interrupt_handler!(115);
156f2022a8aSLoGin interrupt_handler!(116);
157f2022a8aSLoGin interrupt_handler!(117);
158f2022a8aSLoGin interrupt_handler!(118);
159f2022a8aSLoGin interrupt_handler!(119);
160f2022a8aSLoGin interrupt_handler!(120);
161f2022a8aSLoGin interrupt_handler!(121);
162f2022a8aSLoGin interrupt_handler!(122);
163f2022a8aSLoGin interrupt_handler!(123);
164f2022a8aSLoGin interrupt_handler!(124);
165f2022a8aSLoGin interrupt_handler!(125);
166f2022a8aSLoGin interrupt_handler!(126);
167f2022a8aSLoGin interrupt_handler!(127);
168f2022a8aSLoGin // 128号为系统调用,因此不需要设置中断处理函数
169f2022a8aSLoGin interrupt_handler!(129);
170f2022a8aSLoGin interrupt_handler!(130);
171f2022a8aSLoGin interrupt_handler!(131);
172f2022a8aSLoGin interrupt_handler!(132);
173f2022a8aSLoGin interrupt_handler!(133);
174f2022a8aSLoGin interrupt_handler!(134);
175f2022a8aSLoGin interrupt_handler!(135);
176f2022a8aSLoGin interrupt_handler!(136);
177f2022a8aSLoGin interrupt_handler!(137);
178f2022a8aSLoGin interrupt_handler!(138);
179f2022a8aSLoGin interrupt_handler!(139);
180f2022a8aSLoGin interrupt_handler!(140);
181f2022a8aSLoGin interrupt_handler!(141);
182f2022a8aSLoGin interrupt_handler!(142);
183f2022a8aSLoGin interrupt_handler!(143);
184f2022a8aSLoGin interrupt_handler!(144);
185f2022a8aSLoGin interrupt_handler!(145);
186f2022a8aSLoGin interrupt_handler!(146);
187f2022a8aSLoGin interrupt_handler!(147);
188f2022a8aSLoGin interrupt_handler!(148);
189f2022a8aSLoGin interrupt_handler!(149);
190f2022a8aSLoGin interrupt_handler!(150);
191f2022a8aSLoGin interrupt_handler!(151);
192f2022a8aSLoGin interrupt_handler!(152);
193f2022a8aSLoGin interrupt_handler!(153);
194f2022a8aSLoGin interrupt_handler!(154);
195f2022a8aSLoGin interrupt_handler!(155);
196f2022a8aSLoGin interrupt_handler!(156);
197f2022a8aSLoGin interrupt_handler!(157);
198f2022a8aSLoGin interrupt_handler!(158);
199f2022a8aSLoGin interrupt_handler!(159);
200f2022a8aSLoGin interrupt_handler!(160);
201f2022a8aSLoGin interrupt_handler!(161);
202f2022a8aSLoGin interrupt_handler!(162);
203f2022a8aSLoGin interrupt_handler!(163);
204f2022a8aSLoGin interrupt_handler!(164);
205f2022a8aSLoGin interrupt_handler!(165);
206f2022a8aSLoGin interrupt_handler!(166);
207f2022a8aSLoGin interrupt_handler!(167);
208f2022a8aSLoGin interrupt_handler!(168);
209f2022a8aSLoGin interrupt_handler!(169);
210f2022a8aSLoGin interrupt_handler!(170);
211f2022a8aSLoGin interrupt_handler!(171);
212f2022a8aSLoGin interrupt_handler!(172);
213f2022a8aSLoGin interrupt_handler!(173);
214f2022a8aSLoGin interrupt_handler!(174);
215f2022a8aSLoGin interrupt_handler!(175);
216f2022a8aSLoGin interrupt_handler!(176);
217f2022a8aSLoGin interrupt_handler!(177);
218f2022a8aSLoGin interrupt_handler!(178);
219f2022a8aSLoGin interrupt_handler!(179);
220f2022a8aSLoGin interrupt_handler!(180);
221f2022a8aSLoGin interrupt_handler!(181);
222f2022a8aSLoGin interrupt_handler!(182);
223f2022a8aSLoGin interrupt_handler!(183);
224f2022a8aSLoGin interrupt_handler!(184);
225f2022a8aSLoGin interrupt_handler!(185);
226f2022a8aSLoGin interrupt_handler!(186);
227f2022a8aSLoGin interrupt_handler!(187);
228f2022a8aSLoGin interrupt_handler!(188);
229f2022a8aSLoGin interrupt_handler!(189);
230f2022a8aSLoGin interrupt_handler!(190);
231f2022a8aSLoGin interrupt_handler!(191);
232f2022a8aSLoGin interrupt_handler!(192);
233f2022a8aSLoGin interrupt_handler!(193);
234f2022a8aSLoGin interrupt_handler!(194);
235f2022a8aSLoGin interrupt_handler!(195);
236f2022a8aSLoGin interrupt_handler!(196);
237f2022a8aSLoGin interrupt_handler!(197);
238f2022a8aSLoGin interrupt_handler!(198);
239f2022a8aSLoGin interrupt_handler!(199);
240f2022a8aSLoGin interrupt_handler!(200);
241f2022a8aSLoGin interrupt_handler!(201);
242f2022a8aSLoGin interrupt_handler!(202);
243f2022a8aSLoGin interrupt_handler!(203);
244f2022a8aSLoGin interrupt_handler!(204);
245f2022a8aSLoGin interrupt_handler!(205);
246f2022a8aSLoGin interrupt_handler!(206);
247f2022a8aSLoGin interrupt_handler!(207);
248f2022a8aSLoGin interrupt_handler!(208);
249f2022a8aSLoGin interrupt_handler!(209);
250f2022a8aSLoGin interrupt_handler!(210);
251f2022a8aSLoGin interrupt_handler!(211);
252f2022a8aSLoGin interrupt_handler!(212);
253f2022a8aSLoGin interrupt_handler!(213);
254f2022a8aSLoGin interrupt_handler!(214);
255f2022a8aSLoGin interrupt_handler!(215);
256f2022a8aSLoGin interrupt_handler!(216);
257f2022a8aSLoGin interrupt_handler!(217);
258f2022a8aSLoGin interrupt_handler!(218);
259f2022a8aSLoGin interrupt_handler!(219);
260f2022a8aSLoGin interrupt_handler!(220);
261f2022a8aSLoGin interrupt_handler!(221);
262f2022a8aSLoGin interrupt_handler!(222);
263f2022a8aSLoGin interrupt_handler!(223);
264f2022a8aSLoGin interrupt_handler!(224);
265f2022a8aSLoGin interrupt_handler!(225);
266f2022a8aSLoGin interrupt_handler!(226);
267f2022a8aSLoGin interrupt_handler!(227);
268f2022a8aSLoGin interrupt_handler!(228);
269f2022a8aSLoGin interrupt_handler!(229);
270f2022a8aSLoGin interrupt_handler!(230);
271f2022a8aSLoGin interrupt_handler!(231);
272f2022a8aSLoGin interrupt_handler!(232);
273f2022a8aSLoGin interrupt_handler!(233);
274f2022a8aSLoGin interrupt_handler!(234);
275f2022a8aSLoGin interrupt_handler!(235);
276f2022a8aSLoGin interrupt_handler!(236);
277f2022a8aSLoGin interrupt_handler!(237);
278f2022a8aSLoGin interrupt_handler!(238);
279f2022a8aSLoGin interrupt_handler!(239);
280f2022a8aSLoGin interrupt_handler!(240);
281f2022a8aSLoGin interrupt_handler!(241);
282f2022a8aSLoGin interrupt_handler!(242);
283f2022a8aSLoGin interrupt_handler!(243);
284f2022a8aSLoGin interrupt_handler!(244);
285f2022a8aSLoGin interrupt_handler!(245);
286f2022a8aSLoGin interrupt_handler!(246);
287f2022a8aSLoGin interrupt_handler!(247);
288f2022a8aSLoGin interrupt_handler!(248);
289f2022a8aSLoGin interrupt_handler!(249);
290f2022a8aSLoGin interrupt_handler!(250);
291f2022a8aSLoGin interrupt_handler!(251);
292f2022a8aSLoGin interrupt_handler!(252);
293f2022a8aSLoGin interrupt_handler!(253);
294f2022a8aSLoGin interrupt_handler!(254);
295f2022a8aSLoGin interrupt_handler!(255);
296f2022a8aSLoGin 
297f2022a8aSLoGin #[inline(never)]
298*e2841179SLoGin pub unsafe fn arch_setup_interrupt_gate() {
299f2022a8aSLoGin     set_intr_gate(32, 0, VirtAddr::new(irq_handler32 as usize));
300f2022a8aSLoGin     set_intr_gate(33, 0, VirtAddr::new(irq_handler33 as usize));
301f2022a8aSLoGin     set_intr_gate(34, 0, VirtAddr::new(irq_handler34 as usize));
302f2022a8aSLoGin     set_intr_gate(35, 0, VirtAddr::new(irq_handler35 as usize));
303f2022a8aSLoGin     set_intr_gate(36, 0, VirtAddr::new(irq_handler36 as usize));
304f2022a8aSLoGin     set_intr_gate(37, 0, VirtAddr::new(irq_handler37 as usize));
305f2022a8aSLoGin     set_intr_gate(38, 0, VirtAddr::new(irq_handler38 as usize));
306f2022a8aSLoGin     set_intr_gate(39, 0, VirtAddr::new(irq_handler39 as usize));
307f2022a8aSLoGin     set_intr_gate(40, 0, VirtAddr::new(irq_handler40 as usize));
308f2022a8aSLoGin 
309f2022a8aSLoGin     set_intr_gate(41, 0, VirtAddr::new(irq_handler41 as usize));
310f2022a8aSLoGin     set_intr_gate(42, 0, VirtAddr::new(irq_handler42 as usize));
311f2022a8aSLoGin     set_intr_gate(43, 0, VirtAddr::new(irq_handler43 as usize));
312f2022a8aSLoGin     set_intr_gate(44, 0, VirtAddr::new(irq_handler44 as usize));
313f2022a8aSLoGin     set_intr_gate(45, 0, VirtAddr::new(irq_handler45 as usize));
314f2022a8aSLoGin     set_intr_gate(46, 0, VirtAddr::new(irq_handler46 as usize));
315f2022a8aSLoGin     set_intr_gate(47, 0, VirtAddr::new(irq_handler47 as usize));
316f2022a8aSLoGin     set_intr_gate(48, 0, VirtAddr::new(irq_handler48 as usize));
317f2022a8aSLoGin     set_intr_gate(49, 0, VirtAddr::new(irq_handler49 as usize));
318f2022a8aSLoGin     set_intr_gate(50, 0, VirtAddr::new(irq_handler50 as usize));
319f2022a8aSLoGin 
320f2022a8aSLoGin     set_intr_gate(51, 0, VirtAddr::new(irq_handler51 as usize));
321f2022a8aSLoGin     set_intr_gate(52, 0, VirtAddr::new(irq_handler52 as usize));
322f2022a8aSLoGin     set_intr_gate(53, 0, VirtAddr::new(irq_handler53 as usize));
323f2022a8aSLoGin     set_intr_gate(54, 0, VirtAddr::new(irq_handler54 as usize));
324f2022a8aSLoGin     set_intr_gate(55, 0, VirtAddr::new(irq_handler55 as usize));
325f2022a8aSLoGin     set_intr_gate(56, 0, VirtAddr::new(irq_handler56 as usize));
326f2022a8aSLoGin     set_intr_gate(57, 0, VirtAddr::new(irq_handler57 as usize));
327f2022a8aSLoGin     set_intr_gate(58, 0, VirtAddr::new(irq_handler58 as usize));
328f2022a8aSLoGin     set_intr_gate(59, 0, VirtAddr::new(irq_handler59 as usize));
329f2022a8aSLoGin     set_intr_gate(60, 0, VirtAddr::new(irq_handler60 as usize));
330f2022a8aSLoGin 
331f2022a8aSLoGin     set_intr_gate(61, 0, VirtAddr::new(irq_handler61 as usize));
332f2022a8aSLoGin     set_intr_gate(62, 0, VirtAddr::new(irq_handler62 as usize));
333f2022a8aSLoGin     set_intr_gate(63, 0, VirtAddr::new(irq_handler63 as usize));
334f2022a8aSLoGin     set_intr_gate(64, 0, VirtAddr::new(irq_handler64 as usize));
335f2022a8aSLoGin     set_intr_gate(65, 0, VirtAddr::new(irq_handler65 as usize));
336f2022a8aSLoGin     set_intr_gate(66, 0, VirtAddr::new(irq_handler66 as usize));
337f2022a8aSLoGin     set_intr_gate(67, 0, VirtAddr::new(irq_handler67 as usize));
338f2022a8aSLoGin     set_intr_gate(68, 0, VirtAddr::new(irq_handler68 as usize));
339f2022a8aSLoGin     set_intr_gate(69, 0, VirtAddr::new(irq_handler69 as usize));
340f2022a8aSLoGin     set_intr_gate(70, 0, VirtAddr::new(irq_handler70 as usize));
341f2022a8aSLoGin 
342f2022a8aSLoGin     set_intr_gate(71, 0, VirtAddr::new(irq_handler71 as usize));
343f2022a8aSLoGin     set_intr_gate(72, 0, VirtAddr::new(irq_handler72 as usize));
344f2022a8aSLoGin     set_intr_gate(73, 0, VirtAddr::new(irq_handler73 as usize));
345f2022a8aSLoGin     set_intr_gate(74, 0, VirtAddr::new(irq_handler74 as usize));
346f2022a8aSLoGin     set_intr_gate(75, 0, VirtAddr::new(irq_handler75 as usize));
347f2022a8aSLoGin     set_intr_gate(76, 0, VirtAddr::new(irq_handler76 as usize));
348f2022a8aSLoGin     set_intr_gate(77, 0, VirtAddr::new(irq_handler77 as usize));
349f2022a8aSLoGin     set_intr_gate(78, 0, VirtAddr::new(irq_handler78 as usize));
350f2022a8aSLoGin     set_intr_gate(79, 0, VirtAddr::new(irq_handler79 as usize));
351f2022a8aSLoGin     set_intr_gate(80, 0, VirtAddr::new(irq_handler80 as usize));
352f2022a8aSLoGin 
353f2022a8aSLoGin     set_intr_gate(81, 0, VirtAddr::new(irq_handler81 as usize));
354f2022a8aSLoGin     set_intr_gate(82, 0, VirtAddr::new(irq_handler82 as usize));
355f2022a8aSLoGin     set_intr_gate(83, 0, VirtAddr::new(irq_handler83 as usize));
356f2022a8aSLoGin     set_intr_gate(84, 0, VirtAddr::new(irq_handler84 as usize));
357f2022a8aSLoGin     set_intr_gate(85, 0, VirtAddr::new(irq_handler85 as usize));
358f2022a8aSLoGin     set_intr_gate(86, 0, VirtAddr::new(irq_handler86 as usize));
359f2022a8aSLoGin     set_intr_gate(87, 0, VirtAddr::new(irq_handler87 as usize));
360f2022a8aSLoGin     set_intr_gate(88, 0, VirtAddr::new(irq_handler88 as usize));
361f2022a8aSLoGin     set_intr_gate(89, 0, VirtAddr::new(irq_handler89 as usize));
362f2022a8aSLoGin     set_intr_gate(90, 0, VirtAddr::new(irq_handler90 as usize));
363f2022a8aSLoGin 
364f2022a8aSLoGin     set_intr_gate(91, 0, VirtAddr::new(irq_handler91 as usize));
365f2022a8aSLoGin     set_intr_gate(92, 0, VirtAddr::new(irq_handler92 as usize));
366f2022a8aSLoGin     set_intr_gate(93, 0, VirtAddr::new(irq_handler93 as usize));
367f2022a8aSLoGin     set_intr_gate(94, 0, VirtAddr::new(irq_handler94 as usize));
368f2022a8aSLoGin     set_intr_gate(95, 0, VirtAddr::new(irq_handler95 as usize));
369f2022a8aSLoGin     set_intr_gate(96, 0, VirtAddr::new(irq_handler96 as usize));
370f2022a8aSLoGin     set_intr_gate(97, 0, VirtAddr::new(irq_handler97 as usize));
371f2022a8aSLoGin     set_intr_gate(98, 0, VirtAddr::new(irq_handler98 as usize));
372f2022a8aSLoGin     set_intr_gate(99, 0, VirtAddr::new(irq_handler99 as usize));
373f2022a8aSLoGin     set_intr_gate(100, 0, VirtAddr::new(irq_handler100 as usize));
374f2022a8aSLoGin 
375f2022a8aSLoGin     set_intr_gate(101, 0, VirtAddr::new(irq_handler101 as usize));
376f2022a8aSLoGin     set_intr_gate(102, 0, VirtAddr::new(irq_handler102 as usize));
377f2022a8aSLoGin     set_intr_gate(103, 0, VirtAddr::new(irq_handler103 as usize));
378f2022a8aSLoGin     set_intr_gate(104, 0, VirtAddr::new(irq_handler104 as usize));
379f2022a8aSLoGin     set_intr_gate(105, 0, VirtAddr::new(irq_handler105 as usize));
380f2022a8aSLoGin     set_intr_gate(106, 0, VirtAddr::new(irq_handler106 as usize));
381f2022a8aSLoGin     set_intr_gate(107, 0, VirtAddr::new(irq_handler107 as usize));
382f2022a8aSLoGin     set_intr_gate(108, 0, VirtAddr::new(irq_handler108 as usize));
383f2022a8aSLoGin     set_intr_gate(109, 0, VirtAddr::new(irq_handler109 as usize));
384f2022a8aSLoGin     set_intr_gate(110, 0, VirtAddr::new(irq_handler110 as usize));
385f2022a8aSLoGin 
386f2022a8aSLoGin     set_intr_gate(111, 0, VirtAddr::new(irq_handler111 as usize));
387f2022a8aSLoGin     set_intr_gate(112, 0, VirtAddr::new(irq_handler112 as usize));
388f2022a8aSLoGin     set_intr_gate(113, 0, VirtAddr::new(irq_handler113 as usize));
389f2022a8aSLoGin     set_intr_gate(114, 0, VirtAddr::new(irq_handler114 as usize));
390f2022a8aSLoGin     set_intr_gate(115, 0, VirtAddr::new(irq_handler115 as usize));
391f2022a8aSLoGin     set_intr_gate(116, 0, VirtAddr::new(irq_handler116 as usize));
392f2022a8aSLoGin     set_intr_gate(117, 0, VirtAddr::new(irq_handler117 as usize));
393f2022a8aSLoGin     set_intr_gate(118, 0, VirtAddr::new(irq_handler118 as usize));
394f2022a8aSLoGin     set_intr_gate(119, 0, VirtAddr::new(irq_handler119 as usize));
395f2022a8aSLoGin     set_intr_gate(120, 0, VirtAddr::new(irq_handler120 as usize));
396f2022a8aSLoGin 
397f2022a8aSLoGin     set_intr_gate(121, 0, VirtAddr::new(irq_handler121 as usize));
398f2022a8aSLoGin     set_intr_gate(122, 0, VirtAddr::new(irq_handler122 as usize));
399f2022a8aSLoGin     set_intr_gate(123, 0, VirtAddr::new(irq_handler123 as usize));
400f2022a8aSLoGin     set_intr_gate(124, 0, VirtAddr::new(irq_handler124 as usize));
401f2022a8aSLoGin     set_intr_gate(125, 0, VirtAddr::new(irq_handler125 as usize));
402f2022a8aSLoGin     set_intr_gate(126, 0, VirtAddr::new(irq_handler126 as usize));
403f2022a8aSLoGin     set_intr_gate(127, 0, VirtAddr::new(irq_handler127 as usize));
404f2022a8aSLoGin     set_intr_gate(129, 0, VirtAddr::new(irq_handler129 as usize));
405f2022a8aSLoGin     set_intr_gate(130, 0, VirtAddr::new(irq_handler130 as usize));
406f2022a8aSLoGin 
407f2022a8aSLoGin     set_intr_gate(131, 0, VirtAddr::new(irq_handler131 as usize));
408f2022a8aSLoGin     set_intr_gate(132, 0, VirtAddr::new(irq_handler132 as usize));
409f2022a8aSLoGin     set_intr_gate(133, 0, VirtAddr::new(irq_handler133 as usize));
410f2022a8aSLoGin     set_intr_gate(134, 0, VirtAddr::new(irq_handler134 as usize));
411f2022a8aSLoGin     set_intr_gate(135, 0, VirtAddr::new(irq_handler135 as usize));
412f2022a8aSLoGin     set_intr_gate(136, 0, VirtAddr::new(irq_handler136 as usize));
413f2022a8aSLoGin     set_intr_gate(137, 0, VirtAddr::new(irq_handler137 as usize));
414f2022a8aSLoGin     set_intr_gate(138, 0, VirtAddr::new(irq_handler138 as usize));
415f2022a8aSLoGin     set_intr_gate(139, 0, VirtAddr::new(irq_handler139 as usize));
416f2022a8aSLoGin     set_intr_gate(140, 0, VirtAddr::new(irq_handler140 as usize));
417f2022a8aSLoGin 
418f2022a8aSLoGin     set_intr_gate(141, 0, VirtAddr::new(irq_handler141 as usize));
419f2022a8aSLoGin     set_intr_gate(142, 0, VirtAddr::new(irq_handler142 as usize));
420f2022a8aSLoGin     set_intr_gate(143, 0, VirtAddr::new(irq_handler143 as usize));
421f2022a8aSLoGin     set_intr_gate(144, 0, VirtAddr::new(irq_handler144 as usize));
422f2022a8aSLoGin     set_intr_gate(145, 0, VirtAddr::new(irq_handler145 as usize));
423f2022a8aSLoGin     set_intr_gate(146, 0, VirtAddr::new(irq_handler146 as usize));
424f2022a8aSLoGin     set_intr_gate(147, 0, VirtAddr::new(irq_handler147 as usize));
425f2022a8aSLoGin     set_intr_gate(148, 0, VirtAddr::new(irq_handler148 as usize));
426f2022a8aSLoGin     set_intr_gate(149, 0, VirtAddr::new(irq_handler149 as usize));
427f2022a8aSLoGin     set_intr_gate(150, 0, VirtAddr::new(irq_handler150 as usize));
428f2022a8aSLoGin 
429f2022a8aSLoGin     set_intr_gate(151, 0, VirtAddr::new(irq_handler151 as usize));
430f2022a8aSLoGin     set_intr_gate(152, 0, VirtAddr::new(irq_handler152 as usize));
431f2022a8aSLoGin     set_intr_gate(153, 0, VirtAddr::new(irq_handler153 as usize));
432f2022a8aSLoGin     set_intr_gate(154, 0, VirtAddr::new(irq_handler154 as usize));
433f2022a8aSLoGin     set_intr_gate(155, 0, VirtAddr::new(irq_handler155 as usize));
434f2022a8aSLoGin     set_intr_gate(156, 0, VirtAddr::new(irq_handler156 as usize));
435f2022a8aSLoGin     set_intr_gate(157, 0, VirtAddr::new(irq_handler157 as usize));
436f2022a8aSLoGin     set_intr_gate(158, 0, VirtAddr::new(irq_handler158 as usize));
437f2022a8aSLoGin     set_intr_gate(159, 0, VirtAddr::new(irq_handler159 as usize));
438f2022a8aSLoGin     set_intr_gate(160, 0, VirtAddr::new(irq_handler160 as usize));
439f2022a8aSLoGin 
440f2022a8aSLoGin     set_intr_gate(161, 0, VirtAddr::new(irq_handler161 as usize));
441f2022a8aSLoGin     set_intr_gate(162, 0, VirtAddr::new(irq_handler162 as usize));
442f2022a8aSLoGin     set_intr_gate(163, 0, VirtAddr::new(irq_handler163 as usize));
443f2022a8aSLoGin     set_intr_gate(164, 0, VirtAddr::new(irq_handler164 as usize));
444f2022a8aSLoGin     set_intr_gate(165, 0, VirtAddr::new(irq_handler165 as usize));
445f2022a8aSLoGin     set_intr_gate(166, 0, VirtAddr::new(irq_handler166 as usize));
446f2022a8aSLoGin     set_intr_gate(167, 0, VirtAddr::new(irq_handler167 as usize));
447f2022a8aSLoGin     set_intr_gate(168, 0, VirtAddr::new(irq_handler168 as usize));
448f2022a8aSLoGin     set_intr_gate(169, 0, VirtAddr::new(irq_handler169 as usize));
449f2022a8aSLoGin     set_intr_gate(170, 0, VirtAddr::new(irq_handler170 as usize));
450f2022a8aSLoGin 
451f2022a8aSLoGin     set_intr_gate(171, 0, VirtAddr::new(irq_handler171 as usize));
452f2022a8aSLoGin     set_intr_gate(172, 0, VirtAddr::new(irq_handler172 as usize));
453f2022a8aSLoGin     set_intr_gate(173, 0, VirtAddr::new(irq_handler173 as usize));
454f2022a8aSLoGin     set_intr_gate(174, 0, VirtAddr::new(irq_handler174 as usize));
455f2022a8aSLoGin     set_intr_gate(175, 0, VirtAddr::new(irq_handler175 as usize));
456f2022a8aSLoGin     set_intr_gate(176, 0, VirtAddr::new(irq_handler176 as usize));
457f2022a8aSLoGin     set_intr_gate(177, 0, VirtAddr::new(irq_handler177 as usize));
458f2022a8aSLoGin     set_intr_gate(178, 0, VirtAddr::new(irq_handler178 as usize));
459f2022a8aSLoGin     set_intr_gate(179, 0, VirtAddr::new(irq_handler179 as usize));
460f2022a8aSLoGin     set_intr_gate(180, 0, VirtAddr::new(irq_handler180 as usize));
461f2022a8aSLoGin 
462f2022a8aSLoGin     set_intr_gate(181, 0, VirtAddr::new(irq_handler181 as usize));
463f2022a8aSLoGin     set_intr_gate(182, 0, VirtAddr::new(irq_handler182 as usize));
464f2022a8aSLoGin     set_intr_gate(183, 0, VirtAddr::new(irq_handler183 as usize));
465f2022a8aSLoGin     set_intr_gate(184, 0, VirtAddr::new(irq_handler184 as usize));
466f2022a8aSLoGin     set_intr_gate(185, 0, VirtAddr::new(irq_handler185 as usize));
467f2022a8aSLoGin     set_intr_gate(186, 0, VirtAddr::new(irq_handler186 as usize));
468f2022a8aSLoGin     set_intr_gate(187, 0, VirtAddr::new(irq_handler187 as usize));
469f2022a8aSLoGin     set_intr_gate(188, 0, VirtAddr::new(irq_handler188 as usize));
470f2022a8aSLoGin     set_intr_gate(189, 0, VirtAddr::new(irq_handler189 as usize));
471f2022a8aSLoGin     set_intr_gate(190, 0, VirtAddr::new(irq_handler190 as usize));
472f2022a8aSLoGin 
473f2022a8aSLoGin     set_intr_gate(191, 0, VirtAddr::new(irq_handler191 as usize));
474f2022a8aSLoGin     set_intr_gate(192, 0, VirtAddr::new(irq_handler192 as usize));
475f2022a8aSLoGin     set_intr_gate(193, 0, VirtAddr::new(irq_handler193 as usize));
476f2022a8aSLoGin     set_intr_gate(194, 0, VirtAddr::new(irq_handler194 as usize));
477f2022a8aSLoGin     set_intr_gate(195, 0, VirtAddr::new(irq_handler195 as usize));
478f2022a8aSLoGin     set_intr_gate(196, 0, VirtAddr::new(irq_handler196 as usize));
479f2022a8aSLoGin     set_intr_gate(197, 0, VirtAddr::new(irq_handler197 as usize));
480f2022a8aSLoGin     set_intr_gate(198, 0, VirtAddr::new(irq_handler198 as usize));
481f2022a8aSLoGin     set_intr_gate(199, 0, VirtAddr::new(irq_handler199 as usize));
482f2022a8aSLoGin 
483f2022a8aSLoGin     set_intr_gate(200, 0, VirtAddr::new(irq_handler200 as usize));
484f2022a8aSLoGin     set_intr_gate(201, 0, VirtAddr::new(irq_handler201 as usize));
485f2022a8aSLoGin     set_intr_gate(202, 0, VirtAddr::new(irq_handler202 as usize));
486f2022a8aSLoGin     set_intr_gate(203, 0, VirtAddr::new(irq_handler203 as usize));
487f2022a8aSLoGin     set_intr_gate(204, 0, VirtAddr::new(irq_handler204 as usize));
488f2022a8aSLoGin     set_intr_gate(205, 0, VirtAddr::new(irq_handler205 as usize));
489f2022a8aSLoGin     set_intr_gate(206, 0, VirtAddr::new(irq_handler206 as usize));
490f2022a8aSLoGin     set_intr_gate(207, 0, VirtAddr::new(irq_handler207 as usize));
491f2022a8aSLoGin     set_intr_gate(208, 0, VirtAddr::new(irq_handler208 as usize));
492f2022a8aSLoGin     set_intr_gate(209, 0, VirtAddr::new(irq_handler209 as usize));
493f2022a8aSLoGin     set_intr_gate(210, 0, VirtAddr::new(irq_handler210 as usize));
494f2022a8aSLoGin 
495f2022a8aSLoGin     set_intr_gate(211, 0, VirtAddr::new(irq_handler211 as usize));
496f2022a8aSLoGin     set_intr_gate(212, 0, VirtAddr::new(irq_handler212 as usize));
497f2022a8aSLoGin     set_intr_gate(213, 0, VirtAddr::new(irq_handler213 as usize));
498f2022a8aSLoGin     set_intr_gate(214, 0, VirtAddr::new(irq_handler214 as usize));
499f2022a8aSLoGin     set_intr_gate(215, 0, VirtAddr::new(irq_handler215 as usize));
500f2022a8aSLoGin     set_intr_gate(216, 0, VirtAddr::new(irq_handler216 as usize));
501f2022a8aSLoGin     set_intr_gate(217, 0, VirtAddr::new(irq_handler217 as usize));
502f2022a8aSLoGin     set_intr_gate(218, 0, VirtAddr::new(irq_handler218 as usize));
503f2022a8aSLoGin     set_intr_gate(219, 0, VirtAddr::new(irq_handler219 as usize));
504f2022a8aSLoGin     set_intr_gate(220, 0, VirtAddr::new(irq_handler220 as usize));
505f2022a8aSLoGin 
506f2022a8aSLoGin     set_intr_gate(221, 0, VirtAddr::new(irq_handler221 as usize));
507f2022a8aSLoGin     set_intr_gate(222, 0, VirtAddr::new(irq_handler222 as usize));
508f2022a8aSLoGin     set_intr_gate(223, 0, VirtAddr::new(irq_handler223 as usize));
509f2022a8aSLoGin     set_intr_gate(224, 0, VirtAddr::new(irq_handler224 as usize));
510f2022a8aSLoGin     set_intr_gate(225, 0, VirtAddr::new(irq_handler225 as usize));
511f2022a8aSLoGin     set_intr_gate(226, 0, VirtAddr::new(irq_handler226 as usize));
512f2022a8aSLoGin     set_intr_gate(227, 0, VirtAddr::new(irq_handler227 as usize));
513f2022a8aSLoGin     set_intr_gate(228, 0, VirtAddr::new(irq_handler228 as usize));
514f2022a8aSLoGin     set_intr_gate(229, 0, VirtAddr::new(irq_handler229 as usize));
515f2022a8aSLoGin     set_intr_gate(230, 0, VirtAddr::new(irq_handler230 as usize));
516f2022a8aSLoGin 
517f2022a8aSLoGin     set_intr_gate(231, 0, VirtAddr::new(irq_handler231 as usize));
518f2022a8aSLoGin     set_intr_gate(232, 0, VirtAddr::new(irq_handler232 as usize));
519f2022a8aSLoGin     set_intr_gate(233, 0, VirtAddr::new(irq_handler233 as usize));
520f2022a8aSLoGin     set_intr_gate(234, 0, VirtAddr::new(irq_handler234 as usize));
521f2022a8aSLoGin     set_intr_gate(235, 0, VirtAddr::new(irq_handler235 as usize));
522f2022a8aSLoGin     set_intr_gate(236, 0, VirtAddr::new(irq_handler236 as usize));
523f2022a8aSLoGin     set_intr_gate(237, 0, VirtAddr::new(irq_handler237 as usize));
524f2022a8aSLoGin     set_intr_gate(238, 0, VirtAddr::new(irq_handler238 as usize));
525f2022a8aSLoGin     set_intr_gate(239, 0, VirtAddr::new(irq_handler239 as usize));
526f2022a8aSLoGin     set_intr_gate(240, 0, VirtAddr::new(irq_handler240 as usize));
527f2022a8aSLoGin 
528f2022a8aSLoGin     set_intr_gate(241, 0, VirtAddr::new(irq_handler241 as usize));
529f2022a8aSLoGin     set_intr_gate(242, 0, VirtAddr::new(irq_handler242 as usize));
530f2022a8aSLoGin     set_intr_gate(243, 0, VirtAddr::new(irq_handler243 as usize));
531f2022a8aSLoGin     set_intr_gate(244, 0, VirtAddr::new(irq_handler244 as usize));
532f2022a8aSLoGin     set_intr_gate(245, 0, VirtAddr::new(irq_handler245 as usize));
533f2022a8aSLoGin     set_intr_gate(246, 0, VirtAddr::new(irq_handler246 as usize));
534f2022a8aSLoGin     set_intr_gate(247, 0, VirtAddr::new(irq_handler247 as usize));
535f2022a8aSLoGin     set_intr_gate(248, 0, VirtAddr::new(irq_handler248 as usize));
536f2022a8aSLoGin     set_intr_gate(249, 0, VirtAddr::new(irq_handler249 as usize));
537f2022a8aSLoGin     set_intr_gate(250, 0, VirtAddr::new(irq_handler250 as usize));
538f2022a8aSLoGin 
539f2022a8aSLoGin     set_intr_gate(251, 0, VirtAddr::new(irq_handler251 as usize));
540f2022a8aSLoGin     set_intr_gate(252, 0, VirtAddr::new(irq_handler252 as usize));
541f2022a8aSLoGin     set_intr_gate(253, 0, VirtAddr::new(irq_handler253 as usize));
542f2022a8aSLoGin     set_intr_gate(254, 0, VirtAddr::new(irq_handler254 as usize));
543f2022a8aSLoGin     set_intr_gate(255, 0, VirtAddr::new(irq_handler255 as usize));
544f2022a8aSLoGin }
545f2022a8aSLoGin 
546f2022a8aSLoGin /// 设置中断门(DPL=0)
547f2022a8aSLoGin #[allow(dead_code)]
548f2022a8aSLoGin pub unsafe fn set_intr_gate(irq: u32, ist: u8, vaddr: VirtAddr) {
549f2022a8aSLoGin     let idt_entry = get_idt_entry(irq);
550f2022a8aSLoGin     set_gate(idt_entry, 0x8E, ist, vaddr);
551f2022a8aSLoGin }
552f2022a8aSLoGin 
553f2022a8aSLoGin /// 设置陷阱门(DPL=0)
554f2022a8aSLoGin #[allow(dead_code)]
555f2022a8aSLoGin pub unsafe fn set_trap_gate(irq: u32, ist: u8, vaddr: VirtAddr) {
556f2022a8aSLoGin     let idt_entry = get_idt_entry(irq);
557f2022a8aSLoGin     set_gate(idt_entry, 0x8F, ist, vaddr);
558f2022a8aSLoGin }
559f2022a8aSLoGin 
560f2022a8aSLoGin /// 设置系统调用门(DPL=3)
561f2022a8aSLoGin #[allow(dead_code)]
562f2022a8aSLoGin pub unsafe fn set_system_trap_gate(irq: u32, ist: u8, vaddr: VirtAddr) {
563f2022a8aSLoGin     let idt_entry = get_idt_entry(irq);
564f2022a8aSLoGin     set_gate(idt_entry, 0xEF, ist, vaddr);
565f2022a8aSLoGin }
566f2022a8aSLoGin 
567f2022a8aSLoGin unsafe fn get_idt_entry(irq: u32) -> &'static mut [u64] {
568f2022a8aSLoGin     assert!(irq < 256);
569f2022a8aSLoGin     let mut idt_vaddr =
570f2022a8aSLoGin         MMArch::phys_2_virt(PhysAddr::new(&IDT_Table as *const usize as usize)).unwrap();
571f2022a8aSLoGin 
572f2022a8aSLoGin     idt_vaddr += irq as usize * 16;
573f2022a8aSLoGin 
574f2022a8aSLoGin     let idt_entry = core::slice::from_raw_parts_mut(idt_vaddr.data() as *mut u64, 2);
575f2022a8aSLoGin 
576f2022a8aSLoGin     idt_entry
577f2022a8aSLoGin }
578f2022a8aSLoGin unsafe fn set_gate(gate: &mut [u64], attr: u8, ist: u8, handler: VirtAddr) {
579f2022a8aSLoGin     assert_eq!(gate.len(), 2);
580f2022a8aSLoGin     let mut d0: u64 = 0;
581f2022a8aSLoGin     let mut d1: u64 = 0;
582f2022a8aSLoGin 
583f2022a8aSLoGin     // 设置P、DPL、GateType
584f2022a8aSLoGin     d0 |= (attr as u64) << 40;
585f2022a8aSLoGin     // 设置IST
586f2022a8aSLoGin     d0 |= ((ist & 0x7) as u64) << 32;
587f2022a8aSLoGin     // 设置段选择子为0x10 ????
588f2022a8aSLoGin     d0 |= 0x8 << 16;
589f2022a8aSLoGin 
590f2022a8aSLoGin     let mut handler = handler.data() as u64;
591f2022a8aSLoGin     // 设置偏移地址[0:15]
592f2022a8aSLoGin 
593f2022a8aSLoGin     d0 |= handler & 0xFFFF;
594f2022a8aSLoGin     // 设置偏移地址[16:31]
595f2022a8aSLoGin     handler >>= 16;
596f2022a8aSLoGin     d0 |= (0xffff & handler) << 48;
597f2022a8aSLoGin 
598f2022a8aSLoGin     // 设置偏移地址[32:63]
599f2022a8aSLoGin     handler >>= 16;
600f2022a8aSLoGin     d1 |= handler & 0xFFFFFFFF;
601f2022a8aSLoGin 
602f2022a8aSLoGin     gate[0] = d0;
603f2022a8aSLoGin     gate[1] = d1;
604f2022a8aSLoGin }
605