Skip to content

Commit

Permalink
Moved interrupt stuff to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
oystedal committed Apr 14, 2017
1 parent aaa29b0 commit 57116df
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 69 deletions.
File renamed without changes.
60 changes: 60 additions & 0 deletions loader/interrupts.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "interrupts.h"

struct idt_gate {
uint16_t offset_low;
uint16_t segment;
uint16_t flags;
uint16_t offset_high;
};

struct idtr_reg {
uint8_t push_it_to_the[sizeof(uintptr_t)-sizeof(uint16_t)];
uint16_t limit;
uint32_t base;
};

struct idt_gate idt[32];

void
init_idt(void)
{
MAP_INTERRUPT(0, interrupt0);
MAP_INTERRUPT(1, interrupt1);
MAP_INTERRUPT(2, interrupt2);
MAP_INTERRUPT(3, interrupt3);
MAP_INTERRUPT(4, interrupt4);
MAP_INTERRUPT(5, interrupt5);
MAP_INTERRUPT(6, interrupt6);
MAP_INTERRUPT(7, interrupt7);
MAP_INTERRUPT(8, interrupt8);
MAP_INTERRUPT(9, interrupt9);
MAP_INTERRUPT(10, interrupt10);
MAP_INTERRUPT(11, interrupt11);
MAP_INTERRUPT(12, interrupt12);
MAP_INTERRUPT(13, interrupt13);
MAP_INTERRUPT(14, interrupt14);
MAP_INTERRUPT(15, interrupt15);
MAP_INTERRUPT(16, interrupt16);
MAP_INTERRUPT(17, interrupt17);
MAP_INTERRUPT(18, interrupt18);
MAP_INTERRUPT(19, interrupt19);
MAP_INTERRUPT(20, interrupt20);
MAP_INTERRUPT(21, interrupt21);
MAP_INTERRUPT(22, interrupt22);
MAP_INTERRUPT(23, interrupt23);
MAP_INTERRUPT(24, interrupt24);
MAP_INTERRUPT(25, interrupt25);
MAP_INTERRUPT(26, interrupt26);
MAP_INTERRUPT(27, interrupt27);
MAP_INTERRUPT(28, interrupt28);
MAP_INTERRUPT(29, interrupt29);
MAP_INTERRUPT(30, interrupt30);
MAP_INTERRUPT(31, interrupt31);

struct idtr_reg reg;
reg.limit = sizeof(struct idt_gate) * 32;
reg.base = (uint32_t)idt;

__asm__ volatile ("lidt %0" :: "m"(reg.limit));
}

21 changes: 21 additions & 0 deletions loader/interrupts.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
#ifndef INTERRUPTS_INCLUDE_H
#define INTERRUPTS_INCLUDE_H

#include "stdint-gcc.h"

void init_idt(void);

#define MAP_INTERRUPT(vector, address) \
do { \
idt[vector].offset_high = (uint16_t)(((uint32_t)address >> 16) & 0xFFFF); \
idt[vector].offset_low = (uint16_t)((uint32_t)address & 0xFFFF); \
idt[vector].segment = 0x8; \
idt[vector].flags = (1 << 15) | /* segment present */ \
/* Descriptor Privilege level = 0 */ \
(1 << 11) | /* Descpiptor size = 32 bit */ \
(1 << 10) | /* always 1 */ \
(1 << 9); /* always 1 */ \
} while (0);

void interrupt0(void);
void interrupt1(void);
void interrupt2(void);
Expand Down Expand Up @@ -30,3 +49,5 @@ void interrupt28(void);
void interrupt29(void);
void interrupt30(void);
void interrupt31(void);

#endif /* ifndef INTERRUPTS_INCLUDE_H */
69 changes: 0 additions & 69 deletions loader/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,9 @@
#include "../util.h"
#include "interrupts.h"

struct idt_gate {
uint16_t offset_low;
uint16_t segment;
uint16_t flags;
uint16_t offset_high;
};

struct idtr_reg {
uint8_t push_it_to_the[sizeof(uintptr_t)-sizeof(uint16_t)];
uint16_t limit;
uint32_t base;
};

struct idt_gate idt[32];

void halt(void);
void init_idt(void);

void loader_main(uint32_t magic, uint32_t addr)
{
Expand Down Expand Up @@ -57,61 +43,6 @@ void loader_main(uint32_t magic, uint32_t addr)
halt();
}

#define MAP_INTERRUPT(vector, address) \
do { \
idt[vector].offset_high = (uint16_t)(((uint32_t)address >> 16) & 0xFFFF); \
idt[vector].offset_low = (uint16_t)((uint32_t)address & 0xFFFF); \
idt[vector].segment = 0x8; \
idt[vector].flags = (1 << 15) | /* segment present */ \
/* Descriptor Privilege level = 0 */ \
(1 << 11) | /* Descpiptor size = 32 bit */ \
(1 << 10) | /* always 1 */ \
(1 << 9); /* always 1 */ \
} while (0);

void
init_idt(void)
{
MAP_INTERRUPT(0, interrupt0);
MAP_INTERRUPT(1, interrupt1);
MAP_INTERRUPT(2, interrupt2);
MAP_INTERRUPT(3, interrupt3);
MAP_INTERRUPT(4, interrupt4);
MAP_INTERRUPT(5, interrupt5);
MAP_INTERRUPT(6, interrupt6);
MAP_INTERRUPT(7, interrupt7);
MAP_INTERRUPT(8, interrupt8);
MAP_INTERRUPT(9, interrupt9);
MAP_INTERRUPT(10, interrupt10);
MAP_INTERRUPT(11, interrupt11);
MAP_INTERRUPT(12, interrupt12);
MAP_INTERRUPT(13, interrupt13);
MAP_INTERRUPT(14, interrupt14);
MAP_INTERRUPT(15, interrupt15);
MAP_INTERRUPT(16, interrupt16);
MAP_INTERRUPT(17, interrupt17);
MAP_INTERRUPT(18, interrupt18);
MAP_INTERRUPT(19, interrupt19);
MAP_INTERRUPT(20, interrupt20);
MAP_INTERRUPT(21, interrupt21);
MAP_INTERRUPT(22, interrupt22);
MAP_INTERRUPT(23, interrupt23);
MAP_INTERRUPT(24, interrupt24);
MAP_INTERRUPT(25, interrupt25);
MAP_INTERRUPT(26, interrupt26);
MAP_INTERRUPT(27, interrupt27);
MAP_INTERRUPT(28, interrupt28);
MAP_INTERRUPT(29, interrupt29);
MAP_INTERRUPT(30, interrupt30);
MAP_INTERRUPT(31, interrupt31);

struct idtr_reg reg;
reg.limit = sizeof(struct idt_gate) * 32;
reg.base = (uint32_t)idt;

__asm__ volatile ("lidt %0" :: "m"(reg.limit));
}

void halt(void)
{
kprintf("Halting.\n");
Expand Down

0 comments on commit 57116df

Please sign in to comment.