Rework architecture

Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
juju committed 2021-07-26 08:50:31 +02:00
1 parent 7ceec0ee06
commit ac1b1f5376
8 files changed
+9 -7

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
#include "idt.h"
#include "stdio.h"
#include "pic/pic.h"
#include "io.h"
static struct idt idt = { 0 };
static struct idt_entry_descriptor idt_entries_descriptors[IDT_NB_ENTRIES] = {
{(uint32_t)&isr_divide, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 0
{(uint32_t)&isr_debug, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 1
{(uint32_t)&isr_nmi, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 2
{(uint32_t)&isr_breakpoint, KERNEL_CS_SEGMENT_SELECTOR, TRAP_TYPE}, // 3
{(uint32_t)&isr_overflow, KERNEL_CS_SEGMENT_SELECTOR, TRAP_TYPE}, // 4
{(uint32_t)&isr_bound_range_exceeded, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 5
{(uint32_t)&isr_invalid_opcode, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 6
{(uint32_t)&isr_device_not_available, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 7
{(uint32_t)&isr_double_fault, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 8
{(uint32_t)&isr_coprocessor_segment_overrun, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 9
{(uint32_t)&isr_invalid_tss, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 10
{(uint32_t)&isr_segment_not_present, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 11
{(uint32_t)&isr_stack_segment_fault, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 12
{(uint32_t)&isr_general_protection, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 13
{(uint32_t)&isr_page_fault, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 14
{0, {0, 0, 0}, 0}, // 15
{(uint32_t)&isr_fpu_floating_point_error, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 16
{(uint32_t)&isr_alignment_check, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 17
{(uint32_t)&isr_machine_check, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 18
{(uint32_t)&isr_simd_floating_point_exception, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 19
{(uint32_t)&isr_virtualization_exception, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 20
{(uint32_t)&isr_control_protection_exception, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 21
{0, {0, 0, 0}, 0}, // 22
{0, {0, 0, 0}, 0}, // 23
{0, {0, 0, 0}, 0}, // 24
{0, {0, 0, 0}, 0}, // 25
{0, {0, 0, 0}, 0}, // 26
{0, {0, 0, 0}, 0}, // 27
{0, {0, 0, 0}, 0}, // 28
{0, {0, 0, 0}, 0}, // 29
{0, {0, 0, 0}, 0}, // 30
{0, {0, 0, 0}, 0}, // 31
{0, {0, 0, 0}, 0}, // 32
{(uint32_t)&isr_keyboard, KERNEL_CS_SEGMENT_SELECTOR, INTERRUPT_TYPE}, // 33
};
void interrupt_handler(struct isr_param *isr_param)
{
printf("Oh no %d!\r\n", isr_param->int_vector);
if (isr_param->int_vector > IDT_RESERVED_ENTRIES
&& isr_param->int_vector < IDT_RESERVED_ENTRIES + 16)
acknowledge(isr_param->int_vector);
}
struct idt_entry create_idt_entry(struct idt_entry_descriptor descriptor)
{
struct idt_entry res = { 0 };
res.selector = descriptor.selector;
res.offset_1 = descriptor.offset & 0xffff;
res.offset_2 = (descriptor.offset >> 16) & 0xffff;
res.type = descriptor.type;
res.size = 1;
res.desc_priv = 0;
res.present = 1;
return res;
}
static void create_idt(void)
{
for (int i = 0; i < IDT_NB_ENTRIES; ++i)
{
struct idt_entry entry = create_idt_entry(idt_entries_descriptors[i]);
idt.entries[i] = entry;
}
}
static void load_idt(void)
{
struct idt_r idtr;
idtr.addr = (uint32_t)&idt;
idtr.limit = IDT_SIZE - 1;
asm volatile("lidt %0\n"
:
: "m" (idtr)
: "memory");
}
void init_idt(void)
{
create_idt();
load_idt();
}
+52
View File
@@ -0,0 +1,52 @@
#ifndef IDT_H
#define IDT_H
#include <stdint.h>
#include "gdt.h"
#include "isr.h"
#define IDT_RESERVED_ENTRIES 32
#define IDT_NB_ENTRIES 34
#define IDT_SIZE (IDT_NB_ENTRIES * sizeof(struct idt_entry))
#define INTERRUPT_TYPE 0x6
#define TRAP_TYPE 0x7
struct idt_entry
{
uint16_t offset_1 : 16;
struct segment_selector selector;
uint8_t zero_1 : 8; // unused, set to 0
uint8_t type : 3; // gate type
uint8_t size : 1;
uint8_t zero_2 : 1; // set to 0 for interrupt and trap gates
uint8_t desc_priv : 2; // Descriptor privilege
uint8_t present : 1;
uint16_t offset_2 : 16;
} __attribute__ ((packed));
struct idt
{
struct idt_entry entries[IDT_NB_ENTRIES];
};
struct idt_r
{
uint16_t limit;
uint32_t addr;
} __attribute__ ((packed));
struct idt_entry_descriptor
{
uint32_t offset;
struct segment_selector selector;
char type;
};
void init_idt(void);
void interrupt_handler(struct isr_param *isr_param);
#endif /* !IDT_H */
+47
View File
@@ -0,0 +1,47 @@
.section .text
isr:
pushal
pushl %esp
addl $32, (%esp)
call interrupt_handler
add $4, %esp
popal
add $8, %esp
iret
#define isr_callback_error(NAME, VECTOR)\
.global NAME; \
NAME: \
pushl $VECTOR; \
jmp isr
#define isr_callback_no_error(NAME, VECTOR)\
.global NAME ; \
NAME: \
pushl $0; \
pushl $VECTOR; \
jmp isr
isr_callback_no_error(isr_divide, 0)
isr_callback_no_error(isr_debug, 1)
isr_callback_no_error(isr_nmi, 2)
isr_callback_no_error(isr_breakpoint, 3)
isr_callback_no_error(isr_overflow, 4)
isr_callback_no_error(isr_bound_range_exceeded, 5)
isr_callback_no_error(isr_invalid_opcode, 6)
isr_callback_no_error(isr_device_not_available, 7)
isr_callback_no_error(isr_double_fault, 8)
isr_callback_no_error(isr_coprocessor_segment_overrun, 9)
isr_callback_error(isr_invalid_tss, 10)
isr_callback_error(isr_segment_not_present, 11)
isr_callback_error(isr_stack_segment_fault, 12)
isr_callback_error(isr_general_protection, 13)
isr_callback_error(isr_page_fault, 14)
isr_callback_no_error(isr_fpu_floating_point_error, 16)
isr_callback_error(isr_alignment_check, 17)
isr_callback_no_error(isr_machine_check, 18)
isr_callback_no_error(isr_simd_floating_point_exception, 19)
isr_callback_no_error(isr_virtualization_exception, 20)
isr_callback_error(isr_control_protection_exception, 21)
isr_callback_no_error(isr_keyboard, 33)
+33
View File
@@ -0,0 +1,33 @@
#ifndef ISR_H
#define ISR_H
struct isr_param
{
uint32_t int_vector;
uint32_t error_code;
} __attribute__ ((packed));
void isr_divide(void);
void isr_debug(void);
void isr_nmi(void);
void isr_breakpoint(void);
void isr_overflow(void);
void isr_bound_range_exceeded(void);
void isr_invalid_opcode(void);
void isr_device_not_available(void);
void isr_double_fault(void);
void isr_coprocessor_segment_overrun(void);
void isr_invalid_tss(void);
void isr_segment_not_present(void);
void isr_stack_segment_fault(void);
void isr_general_protection(void);
void isr_page_fault(void);
void isr_fpu_floating_point_error(void);
void isr_alignment_check(void);
void isr_machine_check(void);
void isr_simd_floating_point_exception(void);
void isr_virtualization_exception(void);
void isr_control_protection_exception(void);
void isr_keyboard(void);
#endif /* !ISR_H */
+54
View File
@@ -0,0 +1,54 @@
#include "events/idt.h"
#include "io.h"
#include "pic.h"
#include "stdio.h"
static void send_icw1(void)
{
outb(MASTER_PIC_A, ICW1);
outb(SLAVE_PIC_A, ICW1);
}
static void send_icw2(void)
{
outb(MASTER_PIC_B, IDT_RESERVED_ENTRIES);
outb(SLAVE_PIC_B, IDT_RESERVED_ENTRIES + 8);
}
static void send_icw3(void)
{
outb(MASTER_PIC_B, 0x4);
outb(SLAVE_PIC_B, 0x2);
}
static void send_icw4(void)
{
outb(MASTER_PIC_B, 0x1);
outb(SLAVE_PIC_B, 0x1);
}
static void mask_irqs(void)
{
outb(MASTER_PIC_B, 0xff ^ 0x2);
outb(SLAVE_PIC_B, 0xff);
}
void init_pic(void)
{
send_icw1();
send_icw2();
send_icw3();
send_icw4();
mask_irqs();
asm volatile("sti");
}
void acknowledge(uint32_t int_vector)
{
printf("acknowledging %d\r\n", int_vector);
uint8_t ocw2 = 0x20;
if (int_vector >= IDT_RESERVED_ENTRIES + 8)
outb(SLAVE_PIC_A, ocw2);
outb(MASTER_PIC_A, ocw2);
}
+14
View File
@@ -0,0 +1,14 @@
#ifndef PIC_H
#define PIC_H
#define MASTER_PIC_A 0x20
#define MASTER_PIC_B 0x21
#define SLAVE_PIC_A 0xa0
#define SLAVE_PIC_B 0xa1
#define ICW1 0x11
void init_pic(void);
void acknowledge(uint32_t int_vector);
#endif /* !PIC_H */
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.