Skip to content

Commit 3d19acf

Browse files
committed
lesson 22
1 parent 1ec003f commit 3d19acf

File tree

14 files changed

+1045
-0
lines changed

14 files changed

+1045
-0
lines changed

22-malloc/cpu/idt.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "idt.h"
2+
3+
void set_idt_gate(int n, u32 handler) {
4+
idt[n].low_offset = low_16(handler);
5+
idt[n].sel = KERNEL_CS;
6+
idt[n].always0 = 0;
7+
idt[n].flags = 0x8E;
8+
idt[n].high_offset = high_16(handler);
9+
}
10+
11+
void set_idt() {
12+
idt_reg.base = (u32) &idt;
13+
idt_reg.limit = IDT_ENTRIES * sizeof(idt_gate_t) - 1;
14+
/* Don't make the mistake of loading &idt -- always load &idt_reg */
15+
__asm__ __volatile__("lidtl (%0)" : : "r" (&idt_reg));
16+
}

22-malloc/cpu/idt.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef IDT_H
2+
#define IDT_H
3+
4+
#include "type.h"
5+
6+
/* Segment selectors */
7+
#define KERNEL_CS 0x08
8+
9+
/* How every interrupt gate (handler) is defined */
10+
typedef struct {
11+
u16 low_offset; /* Lower 16 bits of handler function address */
12+
u16 sel; /* Kernel segment selector */
13+
u8 always0;
14+
/* First byte
15+
* Bit 7: "Interrupt is present"
16+
* Bits 6-5: Privilege level of caller (0=kernel..3=user)
17+
* Bit 4: Set to 0 for interrupt gates
18+
* Bits 3-0: bits 1110 = decimal 14 = "32 bit interrupt gate" */
19+
u8 flags;
20+
u16 high_offset; /* Higher 16 bits of handler function address */
21+
} __attribute__((packed)) idt_gate_t ;
22+
23+
/* A pointer to the array of interrupt handlers.
24+
* Assembly instruction 'lidt' will read it */
25+
typedef struct {
26+
u16 limit;
27+
u32 base;
28+
} __attribute__((packed)) idt_register_t;
29+
30+
#define IDT_ENTRIES 256
31+
idt_gate_t idt[IDT_ENTRIES];
32+
idt_register_t idt_reg;
33+
34+
35+
/* Functions implemented in idt.c */
36+
void set_idt_gate(int n, u32 handler);
37+
void set_idt();
38+
39+
#endif

0 commit comments

Comments
 (0)