-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.c
121 lines (105 loc) · 2.09 KB
/
kernel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "types.h"
#include "kernel.h"
#include "lib.h"
#include "interupt.h"
#include "protected.h"
#include "task.h"
#include "sys_call.h"
#include "time.h"
#include "keyboard.h"
#define MEMCHK_NUM_ADDR 0x7e00
#define ADDR_RANGE_DESC_TBL_ADDR (MEMCHK_NUM_ADDR+4)
#define GRAPHIC_ADDR 0xb800
#define PAGE_DIR_TBL_ADDR 0x100000
#define PAGE_TBL_ADDR (PAGE_DIR_TBL_ADDR+0x1000)
int *tmp_dbg;
int ticks;
struct addr_range_desc {
u32 baselow;
u32 basehigh;
u32 lenlow;
u32 lenhigh;
u32 type;
};
static void init_global_var();
static void get_gdt_info();
static int get_total_mem();
static int setup_paging();
/*
* do not put any function defination upon cstart
*/
void cstart()
{
/* do not put any function upon */
init_global_var();
get_gdt_info();
init_pit();
init_idtr();
init_8259A();
setup_idt();
init_sys_call_tbl();
init_keyboard();
init_tasks();
set_interupt();
printf("hello %x %x:%x", 1, 2, 3);
start_task();
while (1) {}
}
static void init_global_var()
{
tmp_dbg = (int *)0x7dfc;
k_reenter = -1;
ticks = 0;
}
static void get_gdt_info()
{
/* save gdt limit&base to gdtr */
save_gdtr();
gdt = (struct descriptor *)gdtr.base;
}
static int get_total_mem()
{
struct addr_range_desc *ar;
int *memchk_num;
int total_mem = 0;
int i;
ar = (struct addr_range_desc *)ADDR_RANGE_DESC_TBL_ADDR;
memchk_num = (int *)MEMCHK_NUM_ADDR;
for (i=0; i<*memchk_num; i++) {
total_mem += ar[i].lenlow;
}
return total_mem;
}
static int setup_paging()
{
int total_mem;
int page_tbl_num;
int page_dir_tbl_num;
pde_t *pde;
pte_t *pte;
long pt_base;
long pte_base;
int i;
total_mem = get_total_mem();
if (total_mem <= 0) {
return ERR;
}
pde = (pde_t *)PAGE_DIR_TBL_ADDR;
pte = (pte_t *)PAGE_TBL_ADDR;
page_tbl_num = (total_mem+4095) / 4096;
page_dir_tbl_num = (page_tbl_num+1023) / 1024;
/* initiate page directory table */
pt_base = PAGE_TBL_ADDR;
for (i=0; i<page_dir_tbl_num; i++) {
pde[i] = pt_base | 0x7;
pt_base += 4096;
}
/* initiate page tables */
pte_base = 0;
for (i=0; i<page_tbl_num; i++) {
pte[i] = pte_base | 0x7;
pte_base += 4096;
}
apply_paging();
return OK;
}