summaryrefslogtreecommitdiff
path: root/kernel/interrupts.c
blob: 088c94e37f9f28744849810d5a45359525419853 (plain)
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
#define FOOLOS_MODULE_NAME "interrupts"

#include "lib/logger/log.h"	// logger facilities

#include "asm/asm.h"
#include "interrupts.h"
#include "console.h"
#include "x86.h"

void errlog(uint32_t error_code)
{
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"error_code: 0x%08X",error_code);
}

void deflog(uint32_t eip, uint16_t cs, uint32_t flags)
{
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"eip: 0x%08X",eip);
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"segment: 0x%08X",cs);
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"eflags: 0x%08X",flags);
}

void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr);
//void mouse_handler(); 


// the interrupt descriptor table
static struct int_desc
{
    uint16_t addrLo;
    uint16_t sel;
    uint8_t zeros;
    uint8_t flags;
    uint16_t addrHi;

} idt[INT_MAX];

// interrupt descriptor table descriptor
static struct idt_desc
{
    uint16_t size;
    uint16_t baseLo;
    uint16_t baseHi;

} idtd;

void exception_handle()
{
   panic(FOOLOS_MODULE_NAME,"exception interrupt");
}

void int_default()
{
   log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"default handler");
}


void show_error(uint32_t err)
{
   log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"interrupt error code:  0x%08x",err);
   log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"External Event: %x",err&0b001);
   log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Location: %x",err&0b110);
   log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Selector: %x",err&0b1111111111111000);
}


void exception_handle_0(){   panic(FOOLOS_MODULE_NAME,"Divide by 0");    }
void exception_handle_1(){   panic(FOOLOS_MODULE_NAME,"Single step (debugger)");    }
void exception_handle_2(){   panic(FOOLOS_MODULE_NAME,"Non Maskable Interrupt");    }
void exception_handle_3(){   panic(FOOLOS_MODULE_NAME,"Breakpoint (debugger)");    }
void exception_handle_4(){   panic(FOOLOS_MODULE_NAME,"Overflow");    }
void exception_handle_5(){   panic(FOOLOS_MODULE_NAME,"Bounds check");    }
void exception_handle_6(){   panic(FOOLOS_MODULE_NAME,"Undefined OP Code");    }
void exception_handle_7(){   panic(FOOLOS_MODULE_NAME,"No coprocessor");    }
void exception_handle_8(){   panic(FOOLOS_MODULE_NAME,"Double Fault");    }
void exception_handle_9(){   panic(FOOLOS_MODULE_NAME,"Coprocessor Segment Overrun");    }
void exception_handle_10(){   panic(FOOLOS_MODULE_NAME,"Invalid TSS");    }
void exception_handle_11(){   panic(FOOLOS_MODULE_NAME,"Segment Not Present");    }
void exception_handle_12(){   panic(FOOLOS_MODULE_NAME,"Stack Segment Overrun");    }

void exception_handle_13(uint32_t error_code,uint32_t eip,uint16_t cs,uint16_t unused, uint32_t flags)
{
    errlog(error_code);
    deflog(eip,cs,flags);

    panic(FOOLOS_MODULE_NAME,"Exception: Fault: General Protection Fault");   
}

void exception_handle_14(uint32_t error_code,uint32_t eip,uint16_t cs,uint16_t unused, uint32_t flags)
{
   errlog(error_code);
   log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"error_code_P: %d",error_code&1?1:0);
   log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"error_code_W/R: %d",error_code&2?1:0);
   log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"error_code_U/S: %d",error_code&4?1:0);
   log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"error_code_RSVD: %d",error_code&8?1:0);
   log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"error_code_I/D: %d",error_code&16?1:0);
   log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"at addr: 0x%08X",x86_get_cr2());
   
    deflog(eip,cs,flags);
   panic(FOOLOS_MODULE_NAME,"Exception: Fault: Page Fault");
}

void exception_handle_15(){   panic(FOOLOS_MODULE_NAME,"Unassigned");    }
void exception_handle_16(){   panic(FOOLOS_MODULE_NAME,"Coprocessor error");    }
void exception_handle_17(){   panic(FOOLOS_MODULE_NAME,"Alignment Check");    }
void exception_handle_18(){   panic(FOOLOS_MODULE_NAME,"Machine Check");    }


//set a handler for a specific interrupt
void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr)
{

   uint64_t base=(uint64_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;

}

// set default handler for all interrupts for a start
void int_init(uint16_t sel)
{
    //
    //  Setup PIC
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"setting up PIC",&idt,&idtd);
    pic_setup();

    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"initializing. IDT: 0x%08x, IDTD: 0x%08X",&idt,&idtd);

    int i;
    for(i=0; i<INT_MAX; i++)
    {
	int_install_ir(i, 0b10001110, sel,&int_default_handler);
    }

    // exceptions
    
    int_install_ir(0, 0b10001110, 0x08,&int_irq0);
    int_install_ir(1, 0b10001110, 0x08,&int_irq1);
    int_install_ir(2, 0b10001110, 0x08,&int_irq2);
    int_install_ir(3, 0b10001110, 0x08,&int_irq3);
    int_install_ir(4, 0b10001110, 0x08,&int_irq4);
    int_install_ir(5, 0b10001110, 0x08,&int_irq5);
    int_install_ir(6, 0b10001110, 0x08,&int_irq6);
    int_install_ir(7, 0b10001110, 0x08,&int_irq7);
    int_install_ir(8, 0b10001110, 0x08,&int_irq8);
    int_install_ir(9, 0b10001110, 0x08,&int_irq9);
    int_install_ir(10, 0b10001110, 0x08,&int_irq10);
    int_install_ir(11, 0b10001110, 0x08,&int_irq11);
    int_install_ir(12, 0b10001110, 0x08,&int_irq12);
    int_install_ir(13, 0b10001110, 0x08,&int_irq13);
    int_install_ir(14, 0b10001110, 0x08,&int_irq14);
    int_install_ir(15, 0b10001110, 0x08,&int_irq15);
    int_install_ir(16, 0b10001110, 0x08,&int_irq16);
    int_install_ir(17, 0b10001110, 0x08,&int_irq17);
    int_install_ir(18, 0b10001110, 0x08,&int_irq18);

    // setup some custom interrupts
    // remember that we shifted all interrupts with the pic by 32

    // install PIT interrupt handler (irq 0 => 32)
    int_install_ir(32, 0b10001110, 0x08,&int_clock_handler);

    // install keyboard interrupt handler (irq 1 => 33)
    int_install_ir(33, 0b10001110, 0x08,&int_kb_handler);

    //mouse 
//    int_install_ir(44, 0b10001110, 0x08,&mouse_handler);

    //system calls
    int_install_ir(0x80, 0b11101110, 0x08,&int_syscall_handler);


  int_install();

    // now we can enable interrupts back again
   x86_int_enable();

}

void int_install()
{

    idtd.size=sizeof(struct int_desc)*INT_MAX;

    uint32_t addr=&idt[0];
    idtd.baseHi=addr>>16;
    idtd.baseLo=0xffff&addr;

    __asm__("lidt %0"::"m" (idtd));
}