-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterrupt.c
More file actions
405 lines (337 loc) · 9.48 KB
/
Copy pathinterrupt.c
File metadata and controls
405 lines (337 loc) · 9.48 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* interrupt.c -
*/
#include <types.h>
#include <interrupt.h>
#include <segment.h>
#include <hardware.h>
#include <io.h>
#include <entry.h>
#include <sched.h>
#include <utils.h>
#include <keyboard.h>
#include <devices.h>
Gate idt[IDT_ENTRIES];
Register idtR;
int temps;
char char_map[] = {
'\0', '\0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0', '\'', '¡', '\0', '\0',
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
'o', 'p', '`', '+', '\0', '\0', 'a', 's',
'd', 'f', 'g', 'h', 'j', 'k', 'l', 'ñ',
'\0', 'º', '\0', 'ç', 'z', 'x', 'c', 'v',
'b', 'n', 'm', ',', '.', '-', '\0', '*',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '7',
'8', '9', '-', '4', '5', '6', '+', '1',
'2', '3', '0', '\0', '\0', '\0', '<', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0'
};
void
setInterruptHandler (int vector, void (*handler) (), int maxAccessibleFromPL)
{
/***********************************************************************/
/* THE INTERRUPTION GATE FLAGS: R1: pg. 5-11 */
/* *************************** */
/* flags = x xx 0x110 000 ????? */
/* | | | */
/* | | \ D = Size of gate: 1 = 32 bits; 0 = 16 bits */
/* | \ DPL = Num. higher PL from which it is accessible */
/* \ P = Segment Present bit */
/***********************************************************************/
Word flags = (Word) (maxAccessibleFromPL << 13);
flags |= 0x8E00; /* P = 1, D = 1, Type = 1110 (Interrupt Gate) */
idt[vector].lowOffset = lowWord ((DWord) handler);
idt[vector].segmentSelector = __KERNEL_CS;
idt[vector].flags = flags;
idt[vector].highOffset = highWord ((DWord) handler);
}
void
setTrapHandler (int vector, void (*handler) (), int maxAccessibleFromPL)
{
/***********************************************************************/
/* THE TRAP GATE FLAGS: R1: pg. 5-11 */
/* ******************** */
/* flags = x xx 0x111 000 ????? */
/* | | | */
/* | | \ D = Size of gate: 1 = 32 bits; 0 = 16 bits */
/* | \ DPL = Num. higher PL from which it is accessible */
/* \ P = Segment Present bit */
/***********************************************************************/
Word flags = (Word) (maxAccessibleFromPL << 13);
//flags |= 0x8F00; /* P = 1, D = 1, Type = 1111 (Trap Gate) */
/* Changed to 0x8e00 to convert it to an 'interrupt gate' and so
the system calls will be thread-safe. */
flags |= 0x8E00; /* P = 1, D = 1, Type = 1110 (Interrupt Gate) */
idt[vector].lowOffset = lowWord ((DWord) handler);
idt[vector].segmentSelector = __KERNEL_CS;
idt[vector].flags = flags;
idt[vector].highOffset = highWord ((DWord) handler);
}
void
setIdt ()
{
/* Program interrups/exception service routines */
idtR.base = (DWord) idt;
idtR.limit = IDT_ENTRIES * sizeof (Gate) - 1;
/* Inicialitzar excepcions IDT */
setInterruptHandler (0, divide_error_handler, 0);
setInterruptHandler (1, debug_handler, 3);
setInterruptHandler (2, nmi_handler, 0);
setInterruptHandler (3, breakpoint_handler, 3);
setInterruptHandler (4, overflow_handler, 0);
setInterruptHandler (5, bounds_check_handler, 0);
setInterruptHandler (6, invalid_opcode_handler, 0);
setInterruptHandler (7, device_not_available_handler, 0);
setInterruptHandler (8, double_fault_handler, 0);
setInterruptHandler (9, coprocessor_segment_overrun_handler, 0);
setInterruptHandler (10, invalid_tss_handler, 0);
setInterruptHandler (11, segment_not_present_handler, 0);
setInterruptHandler (12, stack_exception_handler, 0);
setInterruptHandler (13, general_protection_handler, 0);
setInterruptHandler (14, page_fault_handler, 0);
setInterruptHandler (16, floating_point_error_handler, 0);
setInterruptHandler (17, alignment_check_handler, 0);
setInterruptHandler (32, clock_handler, 0);
setInterruptHandler (33, keyboard_handler, 0);
setTrapHandler (128, system_call_handler, 3);
/* ADD INITIALIZATION CODE FOR INTERRUPT VECTOR */
set_idt_reg (&idtR);
}
void
iniTemps ()
{
temps = -1;
}
/* IDT # 0 */
void
divide_error_routine ()
{
printk ("DIVIDE ERROR EXCEPTION");
while (1);
}
/* IDT # 1 */
void
debug_routine ()
{
printk ("DEBUG");
while (1);
}
/* IDT # 2 */
void
nmi_routine ()
{
printk ("NON MASCABLE INTERRUPT");
while (1);
}
/* IDT # 3 */
void
breakpoint_routine ()
{
printk ("BREAKPOINT");
while (1);
}
/* IDT # 4 */
void
overflow_routine ()
{
printk ("OVERFLOW");
while (1);
}
/* IDT # 5 */
void
bounds_check_routine ()
{
printk ("BOUNDS CHECK EXCEPTION");
while (1);
}
/* IDT # 6 */
void
invalid_opcode_routine ()
{
printk ("INVALID OPCODE");
while (1);
}
/* IDT # 7 */
void
device_not_available_routine ()
{
printk ("DEVICE NOT AVAILABLE");
while (1);
}
/* IDT # 8 amb parametre de 4 bytes */
void
double_fault_routine ()
{
printk ("DOUBLE FAULT");
while (1);
}
/* IDT # 9 */
void
coprocessor_segment_overrun_routine ()
{
printk ("COPROCESSOR SEGMENT EXCEPTION");
while (1);
}
/* IDT # 10 amb parametre de 4 bytes */
void
invalid_tss_routine ()
{
printk ("INVALID TSS");
while (1);
}
/* IDT # 11 amb parametre de 4 bytes */
void
segment_not_present_routine ()
{
printk ("SEGMENT NOT PRESENT");
while (1);
}
/* IDT # 12 amb parametre de 4 bytes */
void
stack_exception_routine ()
{
printk ("STACK EXCEPTION");
while (1);
}
/* IDT # 13 amb parametre de 4 bytes */
void
general_protection_routine ()
{
printk ("GENERAL PROTECTION EXCEPTION");
while (1);
}
/* IDT # 14 amb parametre de 4 bytes */
void
page_fault_routine ()
{
int pid_fault = current ()->pid;
printk ("PAGE FAULT:\nHa fallat el proces amb PID=");
//printk ("Ha fallat el proces amb PID=");
printc ('0' + pid_fault);
if (pid_fault != 0)
sys_exit ();
else
while (1);
}
/* IDT # 16 */
void
floating_point_error_routine ()
{
printk ("FLOATING POINT ERROR");
while (1);
}
/* IDT # 17 amb parametre de 4 bytes */
void
alignment_check_routine ()
{
printk ("ALIGNMENT CHECK EXCEPTION");
while (1);
}
void
clock_routine ()
{
int minuts;
int segons;
char hora[6] = { '0', '0', ':', '0', '0', '\0' };
struct task_struct *old_task;
union task_union *new_task;
/* Aqui hauria de ser un 18 tics/segon, pero els IPS del fitxer
* de configuracio del .bochsrc que esta a 600000 fa que el rellotge
* vaigi massa rapid. Per aixo ho hem modificat a 650 tics/segon,
* mesura aproximada. Una maquina real va com podem veure a uns 227t/s:
* cat /proc/interrupts
* CPU0
* 0: 227 IO-APIC-edge timer
*
*/
temps++;
vida--;
old_task = current ();
old_task->tics_cpu++;
if (vida == 0)
{
//Si nomes hi ha task0 restaurar vida i res mes
if (list_first (&runqueue) == list_last (&runqueue))
vida = old_task->quantum;
else
{
list_del (&(old_task->run_list));
list_add_tail (&(old_task->run_list), &runqueue);
new_task =
(union task_union *) (list_head_to_task_struct (runqueue.next));
call_from_int = 1;
task_switch (new_task);
}
}
if (temps % 650 == 0) //18 == 0)
{
segons = temps / 650; // / 18;
minuts = segons / 60; // / 60;
segons = segons % 60;
/* Si fa mes de 100 minuts, reiniciarem el comptador de temps.
* Hi ha altres alternatives com tenir una variable especifica
* per el rellotge, pero d'aquesta manera evitam que si es te la
* maquina engegada molt de temps es desbordi el contador. El mes
* adient seria posar diverses variables, o una estructura de dades
* per el clock del sistema que guardes els minuts, segons, hores,
* dies, etc.en diferents llocs. Com que aquest sistema es de prova
* ho hem trobat necessari implementar-ho.
*/
if (minuts > 99)
{
minuts = 0;
temps = 0;
}
itoa (minuts / 10, &hora[0]);
itoa (minuts % 10, &hora[1]);
hora[2] = ':';
itoa (segons / 10, &hora[3]);
itoa (segons % 10, &hora[4]);
printk_xy (70, 10, hora);
}
}
void
keyboard_routine ()
{
Byte b = inb (0x60);
char c;
struct task_struct *bloq;
c = char_map[b & 0x7f];
/* Si el buffer esta ple, aquest caracter es perd. */
if (!(b & 0x80)) //Make
{
printc (c);
buffer_circular[pos] = c;
avanzar ();
circ_chars++;
if (!list_empty (&keyboard_queue))
{
/* Hi ha algun proces bloquejat */
/* Volem llegir = hem fet un sys_read_keyboard */
bloq = list_head_to_task_struct (list_first (&keyboard_queue));
bloq->chars_pendents--;
if (bloq->chars_pendents <= 0)
{
/* El buffer no esta ple pero el primer proces de la cua de bloquejats
ja te tots els caracters que demanava. */
anar_al_circ (bloq, bloq->size); /* Copiem les dades */
circ_chars = 0;
avanzar ();
inici = pos;
desbloquejar_teclat (bloq);
}
else if (circ_chars >= CIRCULAR_SIZE)
{
anar_al_circ (bloq, CIRCULAR_SIZE); /* Copiem les dades */
bloq->size -= circ_chars;
bloq->buffer += circ_chars;
circ_chars = 0;
avanzar ();
inici = pos;
}
}
}
}