WKern
Loading...
Searching...
No Matches
idt.c
Go to the documentation of this file.
1/*
2WKern - A Bare Metal OS / Kernel I am maKing (For Fun)
3Copyright (C) 2025 Wdboyes13
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#include <global.h>
20#include <idt/idtirq.h>
21#include <io/kio.h>
22#include <mem/kmem.h>
23#include <types/nums.h>
24#include <utils/util.h>
25
28
30struct IdtPtr idt_ptrn __attribute__((aligned(16)));
31
40void IdtSetGate(u8 num, u32 base, u16 sel, u8 flags) {
41 idt[num].base_lo = base & 0xFFFF;
42 idt[num].sel = sel;
43 idt[num].always0 = 0;
44 idt[num].flags = flags;
45 idt[num].base_hi = (base >> 16) & 0xFFFF;
46}
47
49extern void IdtLoad(void);
50
57 u32 base = (idt[i].base_hi << 16) | idt[i].base_lo;
58 char buf[25];
59 Kitoa(i, buf);
60 Kprintf("IDT[%s] = %x\n", buf, base);
61}
62
73void AllIdt() {
74
75 GdtInstall();
76
77 Kprintf("Calling PIC Remap\n");
78 _picr();
79
80 MaskAllIrqs();
81
82 UnmaskIrq(1);
83 UnmaskIrq(0);
84
85 Kprintf("Setting IDT to 0x00\n");
86 Kmemset(&idt, 0,
87 sizeof(struct IdtEntry) *
89
90 Kprintf("Setting IDT Limit + Base\n");
91 idt_ptrn.limit =
92 (sizeof(struct IdtEntry) * IDT_ENTRIES) - 1;
93 idt_ptrn.base = (uptr)idt;
94
95 Kprintf("Setting up IDT gate 32 (IRQ0)\n");
97 0x8E);
99
100 Kprintf("Setting up IDT gate 33 (IRQ1)\n");
102 0x8E);
104
105 Kprintf("Disabling interrupts\n");
106 __asm__ volatile("cli");
107
108 struct {
109 u16 limit;
110 u32 base;
111 } PKG gdtr;
112
113 __asm__ volatile("sgdt %0" : "=m"(gdtr));
114
115 Kprintf("GDT base = %x, limit = %x\n", gdtr.base, gdtr.limit);
116 Kprintf("IDT base = %x, limit = %x\n", idt_ptrn.base, idt_ptrn.limit);
117
118 Kprintf("Loading IDT\n");
119 __asm__ volatile("lidt %[idt]" ::[idt] "m"(idt_ptrn)
120 : "memory");
121
122 Kprintf("Enabling interrupts");
123 __asm__ volatile("sti");
124
125 PitInit(
126 PIT_FREQ);
127}
void GdtInstall()
Initialize and load the Global Descriptor Table (GDT)
Definition gdt.c:56
#define PKG
Add attribute packed.
Definition global.h:35
void debug_print_idt_entry(int i)
Debug function to print an IDT entry's handler address.
Definition idt.c:56
struct IdtEntry idt[IDT_ENTRIES]
Definition idt.c:27
void IdtLoad(void)
void IdtSetGate(u8 num, u32 base, u16 sel, u8 flags)
Set an IDT gate (entry) at given index.
Definition idt.c:40
void AllIdt()
Initialize and load the Interrupt Descriptor Table (IDT)
Definition idt.c:73
#define IDT_ENTRIES
Definition idtirq.h:92
void _picr(void)
#define KERNEL_CODE_SEGMENT
Definition idtirq.h:24
struct IdtPtr idt_ptrn
#define PIT_FREQ
Definition idtirq.h:107
void Irq0Handler(void)
void UnmaskIrq(u8 irq)
Unmask an IRQ.
Definition masker.c:34
void Irq1Handler(void)
void PitInit(u32 freq)
Definition pit.c:32
void MaskAllIrqs(void)
Mask all IRQs.
Definition masker.c:26
void Kprintf(const char *fmt,...)
Formatted output to the screen.
Definition printer.c:152
void Kmemset(void *ptr, unsigned char value, unsigned int num)
Set memory to a value.
Definition memutil.c:26
unsigned int u32
32-Bit Unsigned Int
Definition nums.h:30
unsigned short u16
16-Bit Unsigned Int
Definition nums.h:36
unsigned long uptr
Long Unsigned Int.
Definition nums.h:34
unsigned char u8
8-Bit Unsigned Int
Definition nums.h:32
void Kitoa(unsigned int num, char *buf)
Convert Integer to ASCII Text.
Definition numtools.c:73
__attribute__((noreturn))
Kernel Panics with Message.
Definition panic.c:24
Definition idtirq.h:50
u32 base
Definition idtirq.h:74