Skip to content

Commit df49901

Browse files
committed
初始化IDT表
1 parent 8a49048 commit df49901

File tree

7 files changed

+56
-3
lines changed

7 files changed

+56
-3
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"types.h": "c",
1111
"fcntl.h": "c",
1212
"_default_fcntl.h": "c",
13-
"loader.h": "c"
13+
"loader.h": "c",
14+
"cpu_instr.h": "c"
1415
},
1516
}

source/comm/cpu_instr.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ static inline void write_cr0(uint32_t v) {
5555
__asm__ __volatile__("mov %[v], %%cr0"::[v]"r"(v));
5656
}
5757

58+
static inline void lidt(uint32_t start, uint32_t size) {
59+
struct {
60+
uint16_t limit;
61+
uint16_t start15_0;
62+
uint16_t start31_16;
63+
} idt;
64+
65+
idt.start31_16 = start >> 16;
66+
idt.start15_0 = start & 0xFFFF;
67+
idt.limit = size - 1;
68+
69+
__asm__ __volatile__("lidt %[g]"::[g]"m"(idt));
70+
}
71+
72+
5873
static inline void far_jump(uint32_t selector, uint32_t offset) {
5974
uint32_t addr[] = {offset, selector };
6075
__asm__ __volatile__("ljmpl *(%[a])"::[a]"r"(addr));

source/kernel/cpu/cpu.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ void segment_desc_set(int selector, uint32_t base, uint32_t limit, uint16_t attr
1919
desc->base31_24 = (base >> 24) & 0xff;
2020
}
2121

22+
void gate_desc_set(gate_desc_t * desc, uint16_t selector, uint32_t offset, uint16_t attr){
23+
desc->offset15_0 = offset & 0xFFFF;
24+
desc->selector = selector;
25+
desc->attr = attr;
26+
desc->offset31_16 = (offset >> 16) & 0xFFFF;
27+
}
28+
2229
void init_gdt (void) {
2330
for(int i = 0; i < GDT_TABLE_SIZE; i++){
2431
segment_desc_set( i << 3, 0 , 0 , 0);

source/kernel/cpu/irq.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "cpu/irq.h"
2+
#include "cpu/cpu.h"
3+
#include "comm/cpu_instr.h"
4+
5+
#define IDE_TABLE_NR 128
6+
7+
8+
static gate_desc_t idt_table[IDE_TABLE_NR];
9+
10+
void irq_init(void){
11+
for(int i = 0; i < IDE_TABLE_NR; i++){
12+
gate_desc_set(idt_table + i,0,0,0);
13+
}
14+
lidt((uint32_t) idt_table, sizeof(idt_table));
15+
}

source/kernel/include/cpu/cpu.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ typedef struct _segment_desc_t {
1313
uint8_t base31_24;
1414
}segment_desc_t;
1515

16+
17+
typedef struct _gate_desc_t{
18+
uint16_t offset15_0;
19+
uint16_t selector;
20+
uint16_t attr;
21+
uint8_t offset31_16;
22+
}gate_desc_t;
23+
1624
#pragma pack()
1725

1826
#define SEG_G (1 << 15)
@@ -32,5 +40,5 @@ typedef struct _segment_desc_t {
3240

3341
void cpu_init(void);
3442
void segment_desc_set(int selector, uint32_t base, uint32_t limit, uint16_t attr);
35-
43+
void gate_desc_set(gate_desc_t * desc, uint16_t selector, uint32_t offset, uint16_t attr);
3644
#endif

source/kernel/include/cpu/irq.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef IRQ_H
2+
#define IRQ_H
3+
4+
void irq_init(void);
5+
6+
#endif

source/kernel/init/init.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
#include "comm/boot_info.h"
66
#include "cpu/cpu.h"
7+
#include "cpu/irq.h"
78

89

910
/**
@@ -12,7 +13,7 @@
1213
void kernel_init (boot_info_t * boot_info) {
1314
//初始化GDT表
1415
cpu_init();
15-
16+
irq_init();
1617
}
1718

1819
void init_main(void){

0 commit comments

Comments
 (0)