Implemented ISRs

This commit is contained in:
lucic71 2022-06-28 19:40:51 +03:00
parent 0b6674afdb
commit 1c7483925c
4 changed files with 264 additions and 0 deletions

View File

@ -0,0 +1,143 @@
#include "isr.h"
#include "idt.h"
#include "types/idt_types.h"
#include "types/isr_types.h"
#include <stdio.h>
#include <stdint.h>
/* Extern symbol defined in idt.c */
extern idt_entry_t idt_entries[256];
/* External Interrupt Service Routines. */
extern void isr0();
extern void isr1();
extern void isr2();
extern void isr3();
extern void isr4();
extern void isr5();
extern void isr6();
extern void isr7();
extern void isr8();
extern void isr9();
extern void isr10();
extern void isr11();
extern void isr12();
extern void isr13();
extern void isr14();
extern void isr15();
extern void isr16();
extern void isr17();
extern void isr18();
extern void isr19();
extern void isr20();
extern void isr21();
extern void isr22();
extern void isr23();
extern void isr24();
extern void isr25();
extern void isr26();
extern void isr27();
extern void isr28();
extern void isr29();
extern void isr30();
extern void isr31();
/*
* isr_init:
* ---------
*
* Fill the first 32 entries in IDT.
*
*/
void isr_init(void) {
idt_entries[0] = idt_set_gate((uint32_t) isr0, 0x08, 0x8E);
idt_entries[1] = idt_set_gate((uint32_t) isr1, 0x08, 0x8E);
idt_entries[2] = idt_set_gate((uint32_t) isr2, 0x08, 0x8E);
idt_entries[3] = idt_set_gate((uint32_t) isr3, 0x08, 0x8E);
idt_entries[4] = idt_set_gate((uint32_t) isr4, 0x08, 0x8E);
idt_entries[5] = idt_set_gate((uint32_t) isr5, 0x08, 0x8E);
idt_entries[6] = idt_set_gate((uint32_t) isr6, 0x08, 0x8E);
idt_entries[7] = idt_set_gate((uint32_t) isr7, 0x08, 0x8E);
idt_entries[8] = idt_set_gate((uint32_t) isr8, 0x08, 0x8E);
idt_entries[9] = idt_set_gate((uint32_t) isr9, 0x08, 0x8E);
idt_entries[10] = idt_set_gate( (uint32_t) isr10, 0x08, 0x8E);
idt_entries[11] = idt_set_gate( (uint32_t) isr11, 0x08, 0x8E);
idt_entries[12] = idt_set_gate( (uint32_t) isr12, 0x08, 0x8E);
idt_entries[13] = idt_set_gate( (uint32_t) isr13, 0x08, 0x8E);
idt_entries[14] = idt_set_gate( (uint32_t) isr14, 0x08, 0x8E);
idt_entries[15] = idt_set_gate( (uint32_t) isr15, 0x08, 0x8E);
idt_entries[16] = idt_set_gate( (uint32_t) isr16, 0x08, 0x8E);
idt_entries[17] = idt_set_gate( (uint32_t) isr17, 0x08, 0x8E);
idt_entries[18] = idt_set_gate( (uint32_t) isr18, 0x08, 0x8E);
idt_entries[19] = idt_set_gate( (uint32_t) isr19, 0x08, 0x8E);
idt_entries[20] = idt_set_gate( (uint32_t) isr20, 0x08, 0x8E);
idt_entries[21] = idt_set_gate( (uint32_t) isr21, 0x08, 0x8E);
idt_entries[22] = idt_set_gate( (uint32_t) isr22, 0x08, 0x8E);
idt_entries[23] = idt_set_gate( (uint32_t) isr23, 0x08, 0x8E);
idt_entries[24] = idt_set_gate( (uint32_t) isr24, 0x08, 0x8E);
idt_entries[25] = idt_set_gate( (uint32_t) isr25, 0x08, 0x8E);
idt_entries[26] = idt_set_gate( (uint32_t) isr26, 0x08, 0x8E);
idt_entries[27] = idt_set_gate( (uint32_t) isr27, 0x08, 0x8E);
idt_entries[28] = idt_set_gate( (uint32_t) isr28, 0x08, 0x8E);
idt_entries[29] = idt_set_gate( (uint32_t) isr29, 0x08, 0x8E);
idt_entries[30] = idt_set_gate( (uint32_t) isr30, 0x08, 0x8E);
idt_entries[31] = idt_set_gate( (uint32_t) isr31, 0x08, 0x8E);
}
/* Exception messages for unhandled faults. */
char *exception_messages[] = {
"Division by zero",
"Debug",
"Non-maskable interrupt",
"Breakpoint",
"Detected overflow",
"Out-of-bounds",
"Invalid opcode",
"No coprocessor",
"Double fault",
"Coprocessor segment overrun",
"Bad TSS",
"Segment not present",
"Stack fault",
"General protection fault",
"Page fault",
"Unknown interrupt",
"Coprocessor fault",
"Alignment check",
"Machine check",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved"
};
/* isr_handler:
* Handles an incomming request by calling the appropiate handler or printing
* an exception message.
*
* @param context - the context of the caller
*
*/
void isr_handler(register_t context) {
printf("Unhandled exception: %s\n", exception_messages[context.int_no]);
}

View File

@ -0,0 +1,13 @@
#ifndef TABLES_ISR_H_
#define TABLES_ISR_H_
/*
* isr_init:
* Fills the first 32 entries in IDT after it was loaded with the predefined
* faults and interrupts.
*
*/
void isr_init(void);
#endif

View File

@ -0,0 +1,89 @@
.extern isr_handler
/* Macros that define the entry point in ISRs with and without an error code. */
.macro ISR_NO_ERRCODE _isr_nr
.global isr\_isr_nr
isr\_isr_nr:
cli
push $0x00
push $\_isr_nr
jmp isr_common_stub
.endm
.macro ISR_ERRCODE _isr_nr
.global isr\_isr_nr
isr\_isr_nr:
cli
push $\_isr_nr
jmp isr_common_stub
.endm
/* Expandation of the above mentioned macros. */
ISR_NO_ERRCODE 0
ISR_NO_ERRCODE 1
ISR_NO_ERRCODE 2
ISR_NO_ERRCODE 3
ISR_NO_ERRCODE 4
ISR_NO_ERRCODE 5
ISR_NO_ERRCODE 6
ISR_NO_ERRCODE 7
ISR_ERRCODE 8
ISR_NO_ERRCODE 9
ISR_ERRCODE 10
ISR_ERRCODE 11
ISR_ERRCODE 12
ISR_ERRCODE 13
ISR_ERRCODE 14
ISR_NO_ERRCODE 15
ISR_NO_ERRCODE 16
ISR_NO_ERRCODE 17
ISR_NO_ERRCODE 18
ISR_NO_ERRCODE 19
ISR_NO_ERRCODE 20
ISR_NO_ERRCODE 21
ISR_NO_ERRCODE 22
ISR_NO_ERRCODE 23
ISR_NO_ERRCODE 24
ISR_NO_ERRCODE 25
ISR_NO_ERRCODE 26
ISR_NO_ERRCODE 27
ISR_NO_ERRCODE 28
ISR_NO_ERRCODE 29
ISR_NO_ERRCODE 30
ISR_NO_ERRCODE 31
/*
* Stub for ISRs. It must push the context on the stack and change the PL.
* After the isr_handler was called the context will be restored and the
* error code and the request number will be cleared from the stack.
*
*/
isr_common_stub:
pusha
mov %ds, %ax
push %eax
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
call isr_handler
pop %eax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
popa
add $0x08, %esp
sti
iret

View File

@ -0,0 +1,19 @@
#ifndef TABLES_ISR_TYPES_H_
#define TABLES_ISR_TYPES_H_
#include "stdint.h"
/* register_t - data type containg the context before calling an ISR. */
struct registers {
uint32_t ds;
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax;
uint32_t int_no, err_code;
uint32_t eip, cs, eflags, useresp, ss;
} __attribute__((packed));
typedef struct registers register_t;
#endif