toaruos/kernel/cpu/idt.c

54 lines
1.3 KiB
C
Raw Normal View History

2014-06-08 10:51:01 +04:00
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2011-2013 Kevin Lange
2015-05-21 01:53:03 +03:00
* Copyright (C) 2015 Dale Weiler
*
* Interrupt Descriptor Tables
*
*/
2011-01-16 06:11:17 +03:00
#include <system.h>
2011-12-27 05:23:58 +04:00
#include <logging.h>
2011-01-16 06:11:17 +03:00
2015-05-21 01:53:03 +03:00
typedef struct {
uint16_t base_low;
uint16_t sel;
uint8_t zero;
uint8_t flags;
uint16_t base_high;
} __attribute__((packed)) idt_entry_t;
typedef struct {
uint16_t limit;
2011-01-18 03:34:50 +03:00
uintptr_t base;
2015-05-21 01:53:03 +03:00
} __attribute__((packed)) idt_pointer_t;
/* In the future we may need to put a lock on the access of this */
static struct {
idt_entry_t entries[256];
idt_pointer_t pointer;
} idt __attribute__((used));
2011-01-16 06:11:17 +03:00
2015-05-21 01:53:03 +03:00
#define ENTRY(X) (idt.entries[(X)])
typedef void (*idt_gate_t)(void);
2011-01-16 06:11:17 +03:00
extern void idt_load(uintptr_t);
2011-01-16 06:11:17 +03:00
2015-05-21 01:53:03 +03:00
void idt_set_gate(uint8_t num, idt_gate_t base, uint16_t sel, uint8_t flags) {
ENTRY(num).base_low = ((uintptr_t)base & 0xFFFF);
ENTRY(num).base_high = ((uintptr_t)base >> 16) & 0xFFFF;
ENTRY(num).sel = sel;
ENTRY(num).zero = 0;
ENTRY(num).flags = flags | 0x60;
2011-01-16 06:11:17 +03:00
}
2013-06-06 10:10:36 +04:00
void idt_install(void) {
2015-05-21 01:53:03 +03:00
idt_pointer_t *idtp = &idt.pointer;
idtp->limit = sizeof idt.entries - 1;
idtp->base = (uintptr_t)&ENTRY(0);
memset(&ENTRY(0), 0, sizeof idt.entries);
2011-01-16 06:11:17 +03:00
2015-05-21 01:53:03 +03:00
idt_load((uintptr_t)idtp);
2011-01-16 06:11:17 +03:00
}