os/kernel/cpu/i386/idt.c

58 lines
1.8 KiB
C
Raw Normal View History

2020-07-25 16:54:37 -05:00
/**
* \file
*/
2019-02-10 14:11:07 -06:00
#include "idt.h"
#include <stdint.h>
2020-07-25 16:54:37 -05:00
#define KERNEL_CS 0x08 //!< Kernel code segemnt selector
2019-03-11 09:32:55 -05:00
2020-07-25 16:54:37 -05:00
/**
* Defines an interrupt gate
*/
2019-03-11 09:32:55 -05:00
typedef struct {
2020-07-25 16:54:37 -05:00
uint16_t low_offset; //!< Lower 16 bits of handler function address
uint16_t sel; //!< Kernel segment selector
uint8_t always0; //!< Must be 0.
uint8_t flags; /**<
Flags byte. Gives info about the descriptor
* Bit 7: Present. Must be 1 for all valid selectors.
* Bits 6-5: Privilege. Contains the minimum ring level for the caller. 0 for kernel mode, 3 for user mode.
* Bit 4: Set to 0 for interrupt gates.
* Bits 3-0: 1110 = "32 bit interrupt gate".
*/
uint16_t high_offset; //!< Higher 16 bits of handler function address
} __attribute__((packed)) idt_gate_t;
/**
* Pointed to by the IDTR to tell the processor the IDT's size and address.
*/
2019-03-11 09:32:55 -05:00
typedef struct {
2020-07-25 16:54:37 -05:00
uint16_t limit; //!< Size of the IDT.
idt_gate_t* base; //!< Address of the IDT.
2019-03-11 09:32:55 -05:00
} __attribute__((packed)) idt_register_t;
2020-07-25 16:54:37 -05:00
#define IDT_ENTRIES 256 //!< Number of entries in the IDT
2019-03-11 09:32:55 -05:00
2020-07-25 16:54:37 -05:00
static idt_gate_t idt[IDT_ENTRIES]; //!< The IDT
static idt_register_t idt_reg; //!< The value to load into the IDTR
2019-02-10 14:11:07 -06:00
2020-07-25 18:00:53 -05:00
#define LOW_16(address) (uint16_t)((address) & 0xFFFF) //!< Macro to get the low 16 bits of an address
#define HIGH_16(address) (uint16_t)(((address) >> 16) & 0xFFFF) //!< Macro to get the high 16 bits of an address
2019-03-11 09:32:55 -05:00
void idt_set_gate(int n,uint32_t handler) {
2020-07-25 18:00:53 -05:00
idt[n].low_offset=LOW_16(handler);
2020-07-25 16:54:37 -05:00
idt[n].sel=0x08;
idt[n].always0=0;
idt[n].flags=0xEE;
2020-07-25 18:00:53 -05:00
idt[n].high_offset=HIGH_16(handler);
2019-02-10 14:11:07 -06:00
}
2019-03-11 09:32:55 -05:00
void load_idt() {
2020-07-25 16:54:37 -05:00
idt_reg.base=&idt[0];
idt_reg.limit=IDT_ENTRIES * sizeof(idt_gate_t) - 1;
/* Don't make the mistake of loading &idt -- always load &idt_reg */
asm volatile("lidtl (%0)":: "r" (&idt_reg));
2019-02-10 14:11:07 -06:00
}