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