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
|
#include "interrupts.h"
#include "kernel.h"
#include "log.h"
#include "e1000.h"
#include "asm_int.h"
#include "asm_pit.h"
#include "driver/mouse.h"
#include "driver/keyboard.h"
#include "scheduler.h"
#include "asm_x86.h"
#include "smp.h"
#include "apic.h"
#include "ringbuffer.h"
#include "compositor.h"
#include "syscalls.h"
/** The size of our interrupts table */
#define INT_MAX 256 // 0-255
/** Addresses of the registered interrupt handlers */
static uint32_t handlers[INT_MAX];
/** The interrupt descriptor table */
struct int_desc
{
uint16_t addrLo;
uint16_t sel;
uint8_t zeros;
uint8_t flags;
uint16_t addrHi;
} idt[INT_MAX];
/** The interrupt descriptor table descriptor */
struct idt_desc
{
uint16_t size;
uint16_t baseLo;
uint16_t baseHi;
} idtd;
/** Sets a handler for a specific interrupt */
static void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr)
{
uint64_t base=(uint32_t)&(*(uint32_t*)addr);
idt[irq].addrLo = base & 0xffff;
idt[irq].addrHi = (base >> 16) & 0xffff;
idt[irq].zeros=0;
idt[irq].flags=flags;
idt[irq].sel=sel;
}
/**
* Register an interrupt handler for a given irq number.
*
* Some general notes on interrupts
* (TODO: consider moving this somewehre else)
*
* 1) In case of a syscall (coming from userspace) the handler should:
* a) For a blocking call, set required params and call the scheduler!
* b) Otherweise call x86_sti() by itself as soon as possiblle to
* reenable interrupts.
*
* 2) In case of APIC Timer interrupt call the scheduler.
*
* 3) Keyboard, Mouse, E1000, PIT
* just push your stuff somewhere for later processing as fast as
* you can and reschedule to a kernel worker OR just iret.
*
* 4) IPI
* we use them to force rescheduling only now
* treated in the same way as a APIC Timer interrupt...
* TODO: NOT CALL THIS IPI!! since we send it only on same cpu now :P
*
* NOTE: apic_eoi() should be called in all cases except the syscalls,
* to signal the "end of interrupt" to the APIC.
*
* TODO: REMEMBER THAT WE WILL HAVE TO CHECK ALL OUR SYSCALL ARE REENTRANT!
* ALSO IN MULTICPU SETTINGS! (check the userspace wrappers as well!)
* EACH prog requires a sufficient kernel-stack as well! (check this!)
* WE CAN GUARD with spinlocks / disabling interrupts when really needed....
*
*/
void interrupt_register(uint32_t irq, uint32_t func_addr)
{
if(irq<128||irq>160)kpanic("irq number out of range!");
if(handlers[irq]!=0)kpanic("handler already registered!");
handlers[irq]=func_addr;
}
/*
* Interrupt dispatcher
* Remeber that we are inside an interrupt here!
*/
uint32_t interrupt_handler(uint32_t esp, uint32_t irq)
{
uint32_t cpu=smp_get(SMP_APIC_ID);
if(handlers[irq]!=0)// kb,mouse,e1000,pit,...
{
uint32_t (*f)(uint32_t esp)=handlers[irq];
esp=f(esp);
if(irq!=INTERRUPT_SYSCALL)apic_eoi();
return esp;
}
if(irq==INTERRUPT_APIC_TIMER || irq==INTERRUPT_IPI)
{
if(cpu==0) // thi
{
// limit compositor to APIC_TIMER freq.(60hz?)
if(irq==INTERRUPT_APIC_TIMER)compositor_wake2();
}
esp=scheduler_run(esp,-1); // just schedule to next task
apic_eoi();
return esp;
}
if(irq==INTERRUPT_SYSCALL)
{
int pid=task_get_current_pid();
uint32_t *stack;
stack=esp;
// extract variables from stack
// (NOTE we pass them in some fucked up order,
// they were pushed via pusha!
uint32_t eax=stack[11];
uint32_t ebx=stack[8];
uint32_t ecx=stack[10];
uint32_t edx=stack[9];
// only chance to do somehting before we reenable interrupts!
syscall_generic_prep(eax,edx,ecx,ebx,pid);
// now we don't give a shit about gettting interrupted, yeah!
// this is guaranteed to cause heavy troubles... since we are
// not reentrant and do not guard anythin..
// keep brute force rescheduling till' we get through....
// TODO: implement some waiting queue and wake only if there
// is any chance this will not fail again and again and again..
// TODO: get rid of this big KERNEL LOCK
// it will also not work with SMP
while(true)
{
// x86_cli();
int ok=syscall_generic_test(eax,edx,ecx,ebx,pid);
// x86_sti();
if(ok)break;
else __asm__("int $0x81");
}
// uff, once we got through we can do the syscall and get out
// of this place...
// x86_cli();
uint32_t ret=syscall_generic(eax,edx,ecx,ebx,pid);
// x86_sti();
stack[12]=0x1; // indicate with 0x1 we WANT to set a return
// value in ebx (0x0 will skip it)
stack[13]=ret; // and THIS is our return value!
#ifdef LOG_SYSCALLS
klog("syscall ret=0x%08X",ret);
#endif
//__asm__("int $0x81");
return esp; // return to asm interrupt handler... and iret.
}
kpanic("unhandled interrupt %d",irq);
}
/**
* init interrupt descriptor table (TODO; do we seriously have to hardcode this?)
*/
void interrupts_init()
{
klog("Initializing. IDT: 0x%08x, IDTD: 0x%08X",&idt,&idtd);
// Default interrupt handling
for(int i=0; i<INT_MAX; i++)
{
int_install_ir(i, 0b10001110, 0x08,&int255);
}
// Exceptions
int_install_ir(0, 0b10001110, 0x08,&exc0);
int_install_ir(1, 0b10001110, 0x08,&exc1);
int_install_ir(2, 0b10001110, 0x08,&exc2);
int_install_ir(3, 0b10001110, 0x08,&exc3);
int_install_ir(4, 0b10001110, 0x08,&exc4);
int_install_ir(5, 0b10001110, 0x08,&exc5);
int_install_ir(6, 0b10001110, 0x08,&exc6);
int_install_ir(7, 0b10001110, 0x08,&exc7);
int_install_ir(8, 0b10001110, 0x08,&exc8);
int_install_ir(9, 0b10001110, 0x08,&exc9);
int_install_ir(10, 0b10001110, 0x08,&exc10);
int_install_ir(11, 0b10001110, 0x08,&exc11);
int_install_ir(12, 0b10001110, 0x08,&exc12);
int_install_ir(13, 0b10001110, 0x08,&exc13);
int_install_ir(14, 0b10001110, 0x08,&exc14);
int_install_ir(15, 0b10001110, 0x08,&exc15);
int_install_ir(16, 0b10001110, 0x08,&exc16);
int_install_ir(17, 0b10001110, 0x08,&exc17);
int_install_ir(18, 0b10001110, 0x08,&exc18);
int_install_ir(19, 0b10001110, 0x08,&exc19);
int_install_ir(20, 0b10001110, 0x08,&exc20);
int_install_ir(21, 0b10001110, 0x08,&exc21);
int_install_ir(22, 0b10001110, 0x08,&exc22);
int_install_ir(23, 0b10001110, 0x08,&exc23);
int_install_ir(24, 0b10001110, 0x08,&exc24);
int_install_ir(25, 0b10001110, 0x08,&exc25);
int_install_ir(26, 0b10001110, 0x08,&exc26);
int_install_ir(27, 0b10001110, 0x08,&exc27);
int_install_ir(28, 0b10001110, 0x08,&exc28);
int_install_ir(29, 0b10001110, 0x08,&exc29);
int_install_ir(30, 0b10001110, 0x08,&exc30);
int_install_ir(31, 0b10001110, 0x08,&exc31);
// System Calls (User Software / Ring 3)
int_install_ir(128, 0b11101110, 0x08,&int128);
// Ring 1 only
int_install_ir(129, 0b10001110, 0x08,&int129);
int_install_ir(130, 0b10001110, 0x08,&int130);
int_install_ir(131, 0b10001110, 0x08,&int131);
int_install_ir(132, 0b10001110, 0x08,&int132);
int_install_ir(133, 0b10001110, 0x08,&int133);
int_install_ir(134, 0b10001110, 0x08,&int134);
int_install_ir(135, 0b10001110, 0x08,&int135);
int_install_ir(136, 0b10001110, 0x08,&int136);
int_install_ir(137, 0b10001110, 0x08,&int137);
int_install_ir(138, 0b10001110, 0x08,&int138);
int_install_ir(139, 0b10001110, 0x08,&int139);
int_install_ir(140, 0b10001110, 0x08,&int140);
int_install_ir(141, 0b10001110, 0x08,&int141);
int_install_ir(142, 0b10001110, 0x08,&int142);
int_install_ir(143, 0b10001110, 0x08,&int143);
int_install_ir(144, 0b10001110, 0x08,&int144);
int_install_ir(145, 0b10001110, 0x08,&int145);
int_install_ir(146, 0b10001110, 0x08,&int146);
int_install_ir(147, 0b10001110, 0x08,&int147);
int_install_ir(148, 0b10001110, 0x08,&int148);
int_install_ir(149, 0b10001110, 0x08,&int149);
int_install_ir(150, 0b10001110, 0x08,&int150);
int_install_ir(151, 0b10001110, 0x08,&int151);
int_install_ir(152, 0b10001110, 0x08,&int152);
int_install_ir(153, 0b10001110, 0x08,&int153);
int_install_ir(154, 0b10001110, 0x08,&int154);
int_install_ir(155, 0b10001110, 0x08,&int155);
int_install_ir(156, 0b10001110, 0x08,&int156);
int_install_ir(157, 0b10001110, 0x08,&int157);
int_install_ir(158, 0b10001110, 0x08,&int158);
int_install_ir(159, 0b10001110, 0x08,&int159);
int_install_ir(160, 0b10001110, 0x08,&int160);
}
/** Installs the interrupt table */
void interrupts_install()
{
idtd.size=sizeof(struct int_desc)*INT_MAX;
uint32_t addr=(uint32_t)&idt[0];
idtd.baseHi=addr>>16;
idtd.baseLo=0xffff&addr;
__asm__("lidt %0"::"m" (idtd));
}
|