aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/idt.c
diff options
context:
space:
mode:
authorGravatar ornitorrincos 2026-08-18 12:15:05 +0200
committerGravatar ornitorrincos 2026-08-18 12:15:05 +0200
commit132d3236bb884433bf94e961f6d32264e0334aec (patch)
tree06638976b06f019ea4037175ff228f8271057c44 /kernel/idt.c
Diffstat (limited to '')
-rw-r--r--kernel/idt.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/kernel/idt.c b/kernel/idt.c
new file mode 100644
index 0000000..01cee06
--- /dev/null
+++ b/kernel/idt.c
@@ -0,0 +1,131 @@
+#include "idt.h"
+#include "memops.h"
+#include "interrupts/generic.h"
+#include "asm_inline.h"
+#include "kprintf.h"
+
+extern void asm_generic_interrupt();
+
+idt_load idt_l;
+__attribute__((aligned(0x08)))
+uint64_t idt[512] = {0};
+
+#define VNM(x) vector##x
+
+void fill_idt();
+
+void init_idt()
+{
+ /*
+ for(int i = 0; i < 512; i+=2)
+ {
+ gate g = {
+ // .target_offset = (uint64_t)asm_generic_interrupt,
+ .target_offset = (uint64_t)vector1,
+ .target_selector = 0x8,
+ .ist = 0x0,
+ .type = INT_INT,
+ .dpl = 0x0,
+ .p = true
+ };
+
+ uint64_t converted_gate[2];
+
+ make_int_gate(&g, converted_gate);
+
+ kmemcpy(&converted_gate, &idt[i], 2*sizeof(uint64_t));
+ }
+ */
+
+ fill_idt();
+
+ /*
+ kprintf("IDT ADDR: %p\n", idt);
+ kprintf("INTERRUPT ADDR: %p\n", asm_generic_interrupt);
+ */
+
+ idt_l.size = 256 * 2 * sizeof(uint64_t) - 1;
+ idt_l.ptr = (uint64_t)(idt);
+
+ lidt(&idt_l);
+}
+
+void make_call_gate(gate* g, uint64_t* rg)
+{
+ uint32_t* srg = (uint32_t*)rg;
+ // lower 64
+ rg[0] = 0x0;
+ rg[1] = 0x0;
+
+ srg[0] |= g->target_offset & 0xFFFF;
+ srg[0] |= (g->target_selector & 0xFFFF) << 16;
+
+ // srg[1] |= (g->ist & 0xFF);
+ srg[1] |= (g->type & 0xFF ) << 8;
+ srg[1] |= (g->dpl & 0xFF) << 13;
+ srg[1] |= (g->p & 0xF) << 15; // <- this +16 (next field side is 61, so I did this wrong) <- this should offset by 47, to end in 48
+ srg[1] |= ((g->target_offset >> 16) & 0xFFFF) << 16;
+
+ srg[2] |= ((g->target_offset >> 32) & 0xFFFFFFFF);
+}
+
+void make_int_gate(gate* g, uint64_t* rg)
+{
+ uint32_t* srg = (uint32_t*)rg;
+ // lower 64
+ rg[0] = 0x0;
+ rg[1] = 0x0;
+
+ srg[0] |= g->target_offset & 0xFFFF;
+ srg[0] |= (g->target_selector & 0xFFFF) << 16;
+
+ srg[1] |= (g->ist & 0xFF);
+ srg[1] |= (g->type & 0xFF ) << 8;
+ srg[1] |= (g->dpl & 0xFF) << 13;
+ srg[1] |= (g->p & 0xF) << 15; // <- this +16 (next field side is 61, so I did this wrong) <- this should offset by 47, to end in 48
+ // srg[1] |= ((g->target_offset >> 16) & 0xFFFF) << 16;
+ srg[1] |= g->target_offset & 0xFFFF0000;
+
+ srg[2] |= ((g->target_offset >> 32) & 0xFFFFFFFF);
+}
+
+#define MKI(x) make_gate((uint64_t) vector##x, x)
+
+void make_gate(uint64_t vector, uint32_t i)
+{
+ gate g = {
+ // .target_offset = (uint64_t)asm_generic_interrupt,
+ .target_offset = vector,
+ .target_selector = 0x8,
+ .ist = 0x0,
+ .type = INT_INT,
+ .dpl = 0x0,
+ .p = true
+ };
+
+ uint64_t converted_gate[2];
+
+ make_int_gate(&g, converted_gate);
+
+ kmemcpy(&converted_gate, &idt[2*i], 2*sizeof(uint64_t));
+}
+
+void fill_idt()
+{
+ MKI(0); MKI(1); MKI(2); MKI(3); MKI(4); MKI(5); MKI(6); MKI(7); MKI(8); MKI(9); MKI(10); MKI(11); MKI(12); MKI(13); MKI(14); MKI(15);
+ MKI(16); MKI(17); MKI(18); MKI(19); MKI(20); MKI(21); MKI(22); MKI(23); MKI(24); MKI(25); MKI(26); MKI(27); MKI(28); MKI(29); MKI(30); MKI(31);
+ MKI(32); MKI(33); MKI(34); MKI(35); MKI(36); MKI(37); MKI(38); MKI(39); MKI(40); MKI(41); MKI(42); MKI(43); MKI(44); MKI(45); MKI(46); MKI(47);
+ MKI(48); MKI(49); MKI(50); MKI(51); MKI(52); MKI(53); MKI(54); MKI(55); MKI(56); MKI(57); MKI(58); MKI(59); MKI(60); MKI(61); MKI(62); MKI(63);
+ MKI(64); MKI(65); MKI(66); MKI(67); MKI(68); MKI(69); MKI(70); MKI(71); MKI(72); MKI(73); MKI(74); MKI(75); MKI(76); MKI(77); MKI(78); MKI(79);
+ MKI(80); MKI(81); MKI(82); MKI(83); MKI(84); MKI(85); MKI(86); MKI(87); MKI(88); MKI(89); MKI(90); MKI(91); MKI(92); MKI(93); MKI(94); MKI(95);
+ MKI(96); MKI(97); MKI(98); MKI(99); MKI(100); MKI(101); MKI(102); MKI(103); MKI(104); MKI(105); MKI(106); MKI(107); MKI(108); MKI(109); MKI(110); MKI(111);
+ MKI(112); MKI(113); MKI(114); MKI(115); MKI(116); MKI(117); MKI(118); MKI(119); MKI(120); MKI(121); MKI(122); MKI(123); MKI(124); MKI(125); MKI(126); MKI(127);
+ MKI(128); MKI(129); MKI(130); MKI(131); MKI(132); MKI(133); MKI(134); MKI(135); MKI(136); MKI(137); MKI(138); MKI(139); MKI(140); MKI(141); MKI(142); MKI(143);
+ MKI(144); MKI(145); MKI(146); MKI(147); MKI(148); MKI(149); MKI(150); MKI(151); MKI(152); MKI(153); MKI(154); MKI(155); MKI(156); MKI(157); MKI(158); MKI(159);
+ MKI(160); MKI(161); MKI(162); MKI(163); MKI(164); MKI(165); MKI(166); MKI(167); MKI(168); MKI(169); MKI(170); MKI(171); MKI(172); MKI(173); MKI(174); MKI(175);
+ MKI(176); MKI(177); MKI(178); MKI(179); MKI(180); MKI(181); MKI(182); MKI(183); MKI(184); MKI(185); MKI(186); MKI(187); MKI(188); MKI(189); MKI(190); MKI(191);
+ MKI(192); MKI(193); MKI(194); MKI(195); MKI(196); MKI(197); MKI(198); MKI(199); MKI(200); MKI(201); MKI(202); MKI(203); MKI(204); MKI(205); MKI(206); MKI(207);
+ MKI(208); MKI(209); MKI(210); MKI(211); MKI(212); MKI(213); MKI(214); MKI(215); MKI(216); MKI(217); MKI(218); MKI(219); MKI(220); MKI(221); MKI(222); MKI(223);
+ MKI(224); MKI(225); MKI(226); MKI(227); MKI(228); MKI(229); MKI(230); MKI(231); MKI(232); MKI(233); MKI(234); MKI(235); MKI(236); MKI(237); MKI(238); MKI(239);
+ MKI(240); MKI(241); MKI(242); MKI(243); MKI(244); MKI(245); MKI(246); MKI(247); MKI(248); MKI(249); MKI(250); MKI(251); MKI(252); MKI(253); MKI(254); MKI(255);
+}