-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interrupt.c
296 lines (280 loc) · 7.47 KB
/
Interrupt.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "Interrupt.h"
#include "base_io.h"
#include "task.h"
#include "keybord.h"
#include "c_basic.h"
#include "screen_ctrl.h"
#include "basic_info.h"
#include "base_type.h"
int InstallInterrupt(int interrupt_id, struct DESCRIPTOR des, struct DESCRIPTOR_REG *addr)
{
// 中断向量表大小
int INTERRUPT_TABLE_SIZE = 256;
// 检查中断号是否有效
if (interrupt_id >= INTERRUPT_TABLE_SIZE)
return -1;
struct DESCRIPTOR *ptr = (struct DESCRIPTOR *)(addr->base_addr + interrupt_id * 8);
*ptr = des;
return 0;
}
void FlashInterrupt(struct DESCRIPTOR_REG *addr)
{
asm volatile(
"lidt (%%edx)\n\t"
:
: "d"(addr));
}
void Init8259A()
{
WritePort(0x20, 0x11);
WritePort(0x21, 0x20);
WritePort(0x21, 0x04);
WritePort(0x21, 0x01);
WritePort(0xA0, 0x11);
WritePort(0xA1, 0x70);
WritePort(0xA1, 0x02);
WritePort(0xA1, 0x01);
}
void __cdecl deal_soft_interrupt(unsigned int num, unsigned int b, unsigned int c, unsigned int d)
{
switch (num)
{
case 0:
{
struct TASK_INFO_BLOCK *info = GetNowTaskInfoPtr();
if (((struct TASK_EXTRA_INFO *)info->extra_info)->session)
{
Session_PutString(((struct TASK_EXTRA_INFO *)info->extra_info)->session, (const char *)b);
}
}
break;
case 1:
{
struct TASK_INFO_BLOCK *info = GetNowTaskInfoPtr();
if (((struct TASK_EXTRA_INFO *)info->extra_info)->session)
{
Session_PrintNumber(((struct TASK_EXTRA_INFO *)info->extra_info)->session, b,c);
}
}
break;
case 2:
CloseNowTask();
/*终止进程*/
case 3:
SleepNowTask(b);
break;
case 4:
*((unsigned int *)b) = WaitForKeyboardInput();
break;
case 5:
*((unsigned int *)c) = KB_GetKeyStatus(b);
break;
case 6:
{
struct TASK_INFO_BLOCK *info = GetNowTaskInfoPtr();
if (((struct TASK_EXTRA_INFO *)info->extra_info)->session)
{
LoadTask(b,3,((struct TASK_EXTRA_INFO *)info->extra_info)->session);
}
}
break;
case 7:
{
struct TASK_INFO_BLOCK *info = GetNowTaskInfoPtr();
if (((struct TASK_EXTRA_INFO *)info->extra_info)->session)
{
short int x,y;
Session_GetCursor(((struct TASK_EXTRA_INFO *)info->extra_info)->session,&x,&y);
*(int*)b = x;
*(int*)c = y;
}
}
break;
case 8:
{
struct TASK_INFO_BLOCK *info = GetNowTaskInfoPtr();
if (((struct TASK_EXTRA_INFO *)info->extra_info)->session)
{
Session_SetCursor(((struct TASK_EXTRA_INFO *)info->extra_info)->session,b,c);
}
}
break;
case 9:
*(void**)b = TaskHeapUp(c);
break;
case 10:
TaskHeapDown(c);
break;
default:
Puts("Unknown soft interrupt:");
PrintHex(num);
CloseInterrupt();
WaitForInterrupt();
break;
}
}
void DealKeyboartInterrupt(void)
{
unsigned char status, keycode;
// 读取键盘控制器状态寄存器
status = ReadPort(0x64);
// 检查键盘缓冲区是否有数据可供读取
if (status & 0x01)
{
// 读取键盘扫描码或字符数据
keycode = ReadPort(0x60);
if (keycode & 0x80)
{
KB_DealKeyUp(keycode & 0x7F);
}
else
{
KB_DealKeyDown(keycode & 0x7F);
}
}
}
__attribute__((interrupt)) void int0(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x00\r\n");
}
__attribute__((interrupt)) void int1(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x01\r\n");
}
__attribute__((interrupt)) void int2(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x02\r\n");
}
__attribute__((interrupt)) void int3(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x03\r\n");
}
__attribute__((interrupt)) void int4(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x04\r\n");
}
__attribute__((interrupt)) void int5(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x05\r\n");
}
__attribute__((interrupt)) void int6(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x06");
}
__attribute__((interrupt)) void int7(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x07\r\n");
}
__attribute__((interrupt)) void int8(struct interrupt_frame *frame, unsigned int error_code)
{
Puts("Interrupt: 0x08\r\n");
}
__attribute__((interrupt)) void int9(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x09\r\n");
}
__attribute__((interrupt)) void int10(struct interrupt_frame *frame, unsigned int error_code)
{
Puts("Interrupt: 0x0A\r\n");
}
// 编写中断号11-20的函数类似,只需修改相应的ASCII码和中断号
__attribute__((interrupt)) void int11(struct interrupt_frame *frame, unsigned int error_code)
{
Puts("Interrupt: 0x0B\r\n");
}
// 中断号12-20的函数依次类推
// 中断号12
__attribute__((interrupt)) void int12(struct interrupt_frame *frame, unsigned int error_code)
{
Puts("Interrupt: 0x0C\r\n");
}
__attribute__((interrupt)) void int13(struct interrupt_frame *frame, unsigned int error_code)
{
Puts("GP Exception\r\nAt: ");
PrintHex(frame->eip);
Puts("Press any key to continue with close now process...\r\n");
SetEFLAGS(frame->eflags);
WaitForKeyboardInput();
CloseNowTask();
}
__attribute__((interrupt)) void int14(struct interrupt_frame *frame, unsigned int error_code)
{
unsigned int lineraddr;
asm volatile(
"mov %%cr2,%%eax\n\t"
: "=a"(lineraddr)
:);
char str[256] = "Page error at 0x";
char temp[9] = {0};
Strcat(str, UIntToHex(frame->eip, temp));
Strcat(str, " Error lineraddr is 0x");
Strcat(str, UIntToHex(lineraddr, temp));
Strcat(str, "\r\n");
Puts(str);
if (GetNowTaskInfoPtr()->task_id==0)
{
Puts("Kernel crashed!\r\n");
CloseCPU();
}
Puts("Press any key to continue with close now process...\r\n");
SetEFLAGS(frame->eflags);
WaitForKeyboardInput();
CloseNowTask();
}
__attribute__((interrupt)) void int15(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x0F\r\n");
}
__attribute__((interrupt)) void int16(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x10\r\n");
}
__attribute__((interrupt)) void int17(struct interrupt_frame *frame, unsigned int error_code)
{
Puts("Interrupt: 0x11\r\n");
}
__attribute__((interrupt)) void int18(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x12\r\n");
}
__attribute__((interrupt)) void int19(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x13\r\n");
}
__attribute__((interrupt)) void int20(struct interrupt_frame *frame)
{
Puts("Interrupt: 0x14\r\n");
}
__attribute__((interrupt)) volatile void int0x66(struct interrupt_frame *frame)
{
unsigned int int_num, b, c, d, esi, edi;
asm volatile(
""
: "=D"(int_num), "=b"(b), "=c"(c), "=d"(d), "=S"(esi)
:);
deal_soft_interrupt(int_num, b, c, d);
}
__attribute__((interrupt)) void int0x21(struct interrupt_frame *frame)
{
WritePort(0xa0, 0x20);
WritePort(0x20, 0x20);
DealKeyboartInterrupt();
}
__attribute__((interrupt)) void int0x70(struct interrupt_frame *frame)
{
WritePort(0xA0, 0x20);
WritePort(0x20, 0x20);
WritePort(0x70, 0x0C);
ReadPort(0x71);
asm volatile(
""
:
:
: "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi");
switch_task();
}
void int_default(struct interrupt_frame *frame)
{
WritePort(0xA0, 0x20);
WritePort(0x20, 0x20);
}