Collect the cpu specific stuff

This commit is contained in:
2018-02-13 12:46:36 +01:00
parent b801c7cf3c
commit 96d2ebd977
7 changed files with 13 additions and 3 deletions

7
src/kernel/cpu/cpu.c Normal file
View File

@@ -0,0 +1,7 @@
#include <cpu.h>
#include <interrupts.h>
void cpu_init()
{
interrupt_init();
}

View File

@@ -0,0 +1,44 @@
#include <interrupts.h>
#include <stdint.h>
#include <memory.h>
#include <debug.h>
struct int_gate_descriptor idt[NUM_INTERRUPTS];
struct idtr idtr;
extern uintptr_t isr_table[];
void idt_set_gate(uint32_t num, uintptr_t vector, uint16_t cs, uint8_t ist, uint8_t flags)
{
idt[num].base_l = vector & 0xFFFF;
idt[num].base_m = (vector >> 16) & 0xFFFF;
idt[num].base_h = (vector >> 32) & 0xFFFFFFFF;
idt[num].cs = cs;
idt[num].ist = ist;
idt[num].flags = flags;
}
void interrupt_init()
{
memset(idt, 0, sizeof(idt));
for(int i=0; i < NUM_INTERRUPTS; i++)
{
idt_set_gate(i, isr_table[i], 0x8, 0, IDT_PRESENT | IDT_DPL0 | IDT_INTERRUPT);
}
idtr.addr = idt;
idtr.len = sizeof(idt)-1;
load_idt(&idtr);
}
registers *int_handler(registers *r)
{
(void)r;
debug("Unhandled interrupt occurred\n");
debug("Interrupt number: %d Error code: %d\n", r->int_no, r->err_code);
debug_print_registers(r);
PANIC("Unhandled interrupt occurred");
for(;;);
}

View File

@@ -0,0 +1,33 @@
// vim: ft=c
#include <ttest.h>
#include <debug.h>
#undef debug
#define debug(...)
#include "interrupts.c"
uintptr_t isr_table[] ={};
void load_idt(struct idtr *_)
{
(void)_;
}
uint8_t *idt_raw = (uint8_t *)idt;
TEST(idt_set_gate_correctly_sets_address_L)
{
idt_set_gate(1, 0x1234567890ABCDEF, 0, 0, 0);
ASSERT_EQ_INT(*(uint16_t *)&idt_raw[16+0], 0xCDEF);
}
TEST(idt_set_gate_correctly_sets_address_M)
{
idt_set_gate(1, 0x1234567890ABCDEF, 0, 0, 0);
ASSERT_EQ_INT(*(uint16_t *)&idt_raw[16+6], 0x90AB);
}
TEST(idt_set_gate_correctly_sets_address_H)
{
idt_set_gate(1, 0x1234567890ABCDEF, 0, 0, 0);
ASSERT_EQ_INT(*(uint32_t *)&idt_raw[16+8], 0x12345678);
}

33
src/kernel/cpu/isr.S.py Normal file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from __future__ import print_function
num_isr = 256
pushes_error = [8, 10, 11, 12, 13, 14, 17]
print('''
.intel_syntax noprefix
.extern isr_common
''')
print('// Interrupt Service Routines')
for i in range(num_isr):
print('''isr{0}:
cli
{1}
push {0}
jmp isr_common '''.format(i,
'push 0' if i not in pushes_error else 'nop'))
print('')
print('''
// Vector table
.section .data
.global isr_table
isr_table:''')
for i in range(num_isr):
print(' .quad isr{}'.format(i))

View File

@@ -0,0 +1,50 @@
.intel_syntax noprefix
.global load_idt
load_idt:
lidt [rdi]
ret
.extern int_handler
.global isr_common
.global isr_return
isr_common:
push r15
push r14
push r13
push r12
push r11
push r10
push r9
push r8
push rbp
push rdi
push rsi
push rdx
push rcx
push rbx
push rax
mov rdi, rsp
call int_handler
mov rdi, rax
isr_return:
mov rsp, rdi
pop rax
pop rbx
pop rcx
pop rdx
pop rsi
pop rdi
pop rbp
pop r8
pop r9
pop r10
pop r11
pop r12
pop r13
pop r14
pop r15
add rsp, 0x10
iretq