summaryrefslogtreecommitdiff
path: root/src/interrupts.h
blob: 42f6a5b0c194f06c0c9141cfd31812acd9881e77 (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
#ifndef INTERRUPTS_H
#define INTERRUPTS_H

#include <stdint.h>

// TCC bug (TODO: check, still there?), gcc croaks about
// member pack alignments of things which cannot be packed like
// uint8_t, so we define a macro depending on the compiler
#if defined( __clang__ )
#define MEMBER_ATTRIBUTE_PACKED 
#endif

#if defined( __GNUC__ )
#define MEMBER_ATTRIBUTE_PACKED 
#endif

#if defined( __TINYC__ )
#define MEMBER_ATTRIBUTE_PACKED __attribute__( ( packed ) )
#endif

// packed IDT structure, note TCC needs every member to have a packed
// attribute, GCC/Clang are happy with the packed attribute at the
// structure level
typedef struct {
	uint16_t handler_address_low_bits MEMBER_ATTRIBUTE_PACKED;
	uint16_t gdt_code_segment_selector MEMBER_ATTRIBUTE_PACKED;
	uint8_t reserved MEMBER_ATTRIBUTE_PACKED;
	uint8_t access MEMBER_ATTRIBUTE_PACKED;
	uint16_t handler_address_high_bits MEMBER_ATTRIBUTE_PACKED;
} __attribute__( ( packed ) ) interrupt_gate_descriptor_t;

#define NOF_INTERRUPT_GATES 256

typedef struct {
	interrupt_gate_descriptor_t descriptor_table[NOF_INTERRUPT_GATES];
} interrupt_t;

typedef struct {
	uint16_t size MEMBER_ATTRIBUTE_PACKED;
	uint32_t base MEMBER_ATTRIBUTE_PACKED;	
} __attribute__( ( packed ) ) interrupt_descriptor_table_pointer_t;

void interrupts_enable( void );
void interrupts_disable( void );
void interrupts_init( interrupt_t *interrupt );
void interrupts_register_interrupt( interrupt_t *interrupt,
	uint8_t interrupt_no, uint16_t gdt_code_segment_selector,
	void (*handler)( ), uint8_t privilege_level, uint8_t descriptor_type );
void interrupts_load_idt( interrupt_descriptor_table_pointer_t *idt_pointer );

uint32_t interrupts_handle_interrupt( uint8_t interrupt_no, uint32_t esp );

void interrupts_ignore_request( );
void interrupts_handle_request_0x00( );

// initialize IDT
// handle gates
// assembly trampolines calling C (static) functions
// later: tasks and stacks, queues

#endif // INTERRUPTS_H