-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup32.h
188 lines (161 loc) · 6.87 KB
/
setup32.h
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*****************************************************************************/
/* File: setup32.h */
/* */
/* Description: Header file for x86 32 bit data structure for GDT, IDT, TSS */
/* and related macros. */
/* */
/* Author: Shoily O Rahman <[email protected]> */
/* */
/* Date: Mar 15, 2020 */
/* */
/*****************************************************************************/
#ifndef _SETUP_32_H
#define _SETUP_32_H
#define USER_CODE_SEG 0x10
#define USER_DATA_SEG 0x20
#define KERNEL_CODE_SEG 0x30
#define KERNEL_DATA_SEG 0x40
#define LDT_SELECTOR 0x50
#define FIRST_TASK_SEG 0x60
#define LAST_SEG (FIRST_TASK_SEG+(MAX_NUM_SMPS*8))
#define USER_CODE_SEG_IN_LDT 0x0
#define USER_DATA_SEG_IN_LDT 0x8
struct gdt_entry {
unsigned short segment_limit_low;
unsigned short base_address_low;
unsigned char base_address_mid;
unsigned int type:4;
unsigned int system:1;
unsigned int dpl:2;
unsigned int present:1;
unsigned char segment_limit_high:4;
unsigned int avl:1;
unsigned int l:1;
unsigned int db:1;
unsigned int granularity:1;
unsigned char base_address_high;
}__attribute__((packed));
struct idt_entry {
unsigned short offset_low;
unsigned short segment_selector;
unsigned char unused;
unsigned char type:4;
unsigned char s:1;
unsigned char dpl:2;
unsigned char present:1;
unsigned short offset_high;
}__attribute__((packed));
struct tss_entry {
unsigned short prev_task;
unsigned short reserved1;
unsigned int esp0;
unsigned short ss0;
unsigned short reserved2;
unsigned int esp1;
unsigned short ss1;
unsigned short reserved3;
unsigned int esp2;
unsigned short ss2;
unsigned short reserved4;
unsigned int cr3;
unsigned int eip;
unsigned int eflags;
unsigned int eax;
unsigned int ecx;
unsigned int edx;
unsigned int ebx;
unsigned int esp;
unsigned int ebp;
unsigned int esi;
unsigned int edi;
unsigned short es;
unsigned short reserved5;
unsigned short cs;
unsigned short reserved6;
unsigned short ss;
unsigned short reserved7;
unsigned short ds;
unsigned short reserved8;
unsigned short fs;
unsigned short reserved9;
unsigned short gs;
unsigned short reserved10;
unsigned short ldt;
unsigned short reserved11;
unsigned short reserved12;
unsigned short ioremapaddr;
}__attribute__((packed));
#define SET_GDT_ENTRY(g, index, segment_limit, base_address, _type, _system, _dpl, _db, _granularity) { \
struct gdt_entry *entry = g+index; \
memset(entry, 0, sizeof(struct gdt_entry)); \
entry->segment_limit_low = segment_limit & 0xffff; \
entry->segment_limit_high = (segment_limit >> 16) & 0xf; \
entry->base_address_low = base_address & 0xffff; \
entry->base_address_mid = (base_address >> 16) & 0xff; \
entry->base_address_high = (base_address >> 24) & 0xff; \
entry->type = _type; \
entry->system = _system; \
entry->dpl = _dpl; \
entry->present = 1; \
entry->avl = 0; \
entry->l = 0; \
entry->db = _db; \
entry->granularity = _granularity; \
}
#define SET_KERNEL_CODE_SEGMENT(g, segment_limit, base_address) SET_GDT_ENTRY(g, (KERNEL_CODE_SEG/8), segment_limit, base_address, 0xa, 1, 0, 1, 1)
#define SET_KERNEL_DATA_SEGMENT(g, segment_limit, base_address) SET_GDT_ENTRY(g, (KERNEL_DATA_SEG/8), segment_limit, base_address, 2, 1, 0, 1, 1)
#define SET_USER_CODE_SEGMENT(g, segment_limit, base_address) SET_GDT_ENTRY(g, (USER_CODE_SEG/8), segment_limit, base_address, 0xa, 1, 3, 1, 1)
#define SET_USER_DATA_SEGMENT(g, segment_limit, base_address) SET_GDT_ENTRY(g, (USER_DATA_SEG/8), segment_limit, base_address, 2, 1, 3, 1, 1)
#define SET_USER_CODE_SEGMENT_IN_LDT(l, segment_limit, base_address) SET_GDT_ENTRY(l, (USER_CODE_SEG_IN_LDT/8), segment_limit, base_address, 0xa, 1, 3, 1, 1)
#define SET_USER_DATA_SEGMENT_IN_LDT(l, segment_limit, base_address) SET_GDT_ENTRY(l, (USER_DATA_SEG_IN_LDT/8), segment_limit, base_address, 2, 1, 3, 1, 1)
#define SET_TASK_SEGMENT(g, segment_limit, base_address, segment) SET_GDT_ENTRY(g, (segment/8), segment_limit, base_address, 9, 0, 3, 0, 0)
#define SET_LDT_DESCRIPTOR(g, segment_limit, base_address) SET_GDT_ENTRY(g, (LDT_SELECTOR/8), segment_limit, base_address, 2, 0, 3, 0, 0)
#define SET_IDT_ENTRY(idt, index, offset, _segment_selector, _type, _dpl, _present) { \
struct idt_entry *entry = idt+index; \
memset(entry, 0, sizeof(struct idt_entry)); \
entry->offset_low = (int)offset & 0xffff; \
entry->offset_high = ((int)offset >> 16) & 0xffff; \
entry->segment_selector = _segment_selector; \
entry->type = _type; \
entry->dpl = _dpl; \
entry->present = _present; \
}
#define SET_INTERRUPT_GATE(idt, index, offset) { \
SET_IDT_ENTRY(idt, index, offset, KERNEL_CODE_SEG, 0xe, 3, 1); \
}
#define SET_TRAP_GATE(idt, index, offset) { \
SET_IDT_ENTRY(idt, index, offset, KERNEL_CODE_SEG, 0xf, 3, 1); \
}
#define SET_TASK_GATE(idt, index) { \
SET_IDT_ENTRY(idt, index, 0, KERNEL_CODE_SEG, 0x5, 3, 1); \
}
struct regs_frame {
u32 trap_nr;
u32 gs;
u32 fs;
u32 es;
u32 ds;
u32 edi;
u32 esi;
u32 ebp;
u32 orig_esp;
u32 ebx;
u32 edx;
u32 ecx;
u32 eax;
u32 code_nr;
u32 eip;
u32 cs;
u32 eflags;
u32 esp;
u32 ss;
}__attribute__((packed));
typedef void (*idt_function_type)();
void setup32();
void set_idt(int vector, idt_function_type idt_function);
void loadGDT32();
void loadLDT32();
void loadIDT32();
void initializeTSS32(int smp_id);
void loadTSS32(int smp_id);
#endif