summaryrefslogtreecommitdiff
path: root/src/interrupts.c
blob: 77ba0d04ea56523450727e5456c2937eaaa44518 (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
#include "interrupts.h"

#include "string.h"
#include "stdio.h"

#include "kernel.h"

// TODO: for now the code segment is the first entry in the boot GDT	
#define GDT_CODE_SEGMENT_SELECTOR 8

// the Present bit in a IDT entry
#define IDT_PRESENT_BIT 0x80

// DPL rings
#define KERNEL_RING 0

// types of IDT entries
#define IDT_TYPE_INTERRUPT_GATE 0xE

void interrupts_init( interrupt_t *interrupt )
{
	memset( interrupt, 0, sizeof( interrupt_t ) );

	for( int i = 0; i < NOF_INTERRUPT_GATES; i++ ) {
		interrupts_register_interrupt( interrupt, i, GDT_CODE_SEGMENT_SELECTOR,
			&interrupts_ignore_request, KERNEL_RING, IDT_TYPE_INTERRUPT_GATE );
	}

	// divide-by-zero exception 0x00
	interrupts_register_interrupt( interrupt, 0x00, GDT_CODE_SEGMENT_SELECTOR,
			&interrupts_handle_request_0x00, KERNEL_RING, IDT_TYPE_INTERRUPT_GATE );

	interrupt->idt_pointer.size = 256 * sizeof( interrupt_gate_descriptor_t ) - 1;
	interrupt->idt_pointer.base = (uint32_t)interrupt->descriptor_table;
		
	interrupts_load_idt( &interrupt->idt_pointer );
}

void interrupts_register_interrupt( interrupt_t *interrupts, 
	uint8_t interrupt_no, uint16_t gdt_code_segment_selector,
	void (*handler)( ), uint8_t privilege_level, uint8_t descriptor_type )
{
	interrupt_gate_descriptor_t *descr = &interrupts->descriptor_table[interrupt_no];
	
	descr->handler_address_low_bits = ( (uint32_t )handler ) & 0xFFFF;
	descr->handler_address_high_bits = ( ( (uint32_t )handler ) >> 16 ) & 0xFFFF;
	descr->gdt_code_segment_selector = gdt_code_segment_selector;
	descr->access = IDT_PRESENT_BIT | (( privilege_level & 0x3 ) << 5 ) | descriptor_type;
	descr->reserved = 0;
}

uint32_t interrupts_handle_interrupt( uint8_t interrupt_no, uint32_t esp )
{
	kernel_panic( "Unhandled interrupt 0x%X with ESP 0x%X\n", interrupt_no, esp );
	
	// for now, we are using the same stack for kernel and interrupt
	// handlers (n task switching)
	return esp;
}