Skip to content

Commit

Permalink
+ | Syscall ABI ready for future implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
BolvicBolvicovic committed Sep 8, 2024
1 parent d8b6105 commit 8580a08
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 23 deletions.
7 changes: 7 additions & 0 deletions drivers/descriptor/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ extern void irq12();
extern void irq13();
extern void irq14();
extern void irq15();
extern void syscall();

typedef struct {
uint16_t limit_low;
Expand Down Expand Up @@ -113,6 +114,12 @@ typedef struct {
#define IRQ13 45
#define IRQ14 46
#define IRQ15 47
#define SYSCALL 0x80

#define SLAVE_PORT 0xA0
#define MASTER_PORT 0x20
#define EOI 0x20 // End Of Interrupt
#define FIRST_SLAVE_PORT 40

void init_gdt();
void init_idt();
Expand Down
30 changes: 16 additions & 14 deletions drivers/descriptor/idt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

//INTERUPTION DESCRIPTOR TABLE

static idt_gate_t idt[256];
static idt_gate_t idt[256] = {0};
static idt_register_t idt_reg;

void init_idt() {
idt_reg.limit = sizeof(idt_gate_t) * 256 -1;
idt_reg.base = (uint32_t)&idt;

memset(&idt, 0, sizeof(idt_gate_t) * 256);

set_idt_gate(0, (uint32_t) isr0);
set_idt_gate(1, (uint32_t) isr1);
set_idt_gate(2, (uint32_t) isr2);
Expand Down Expand Up @@ -48,24 +46,23 @@ void init_idt() {
// We send them Initialization Command Words (ICW)

// ICW1
port_byte_out(0x20, 0x11);
port_byte_out(0xA0, 0x11);

port_byte_out(MASTER_PORT, 0x11);
port_byte_out(SLAVE_PORT, 0x11);
// ICW2
port_byte_out(0x21, 0x20);
port_byte_out(0xA1, 0x28);
port_byte_out(MASTER_PORT + 1, 0x20);
port_byte_out(SLAVE_PORT + 1, 0x28);

// ICW3
port_byte_out(0x21, 0x04);
port_byte_out(0xA1, 0x02);
port_byte_out(MASTER_PORT + 1, 0x04);
port_byte_out(SLAVE_PORT + 1, 0x02);

// ICW4
port_byte_out(0x21, 0x01);
port_byte_out(0xA1, 0x01);
port_byte_out(MASTER_PORT + 1, 0x01);
port_byte_out(MASTER_PORT + 1, 0x01);

// OCW1 (Operational Command Word) It enables all IRCs.
port_byte_out(0x21, 0x0);
port_byte_out(0xA1, 0x0);
port_byte_out(MASTER_PORT + 1, 0x0);
port_byte_out(MASTER_PORT + 1, 0x0);

// Install the IRQs
set_idt_gate(32, (uint32_t)irq0);
Expand All @@ -85,6 +82,11 @@ void init_idt() {
set_idt_gate(46, (uint32_t)irq14);
set_idt_gate(47, (uint32_t)irq15);


// Syscall

set_idt_gate(0x80, (uint32_t)syscall);

asm volatile("lidt (%0)" : : "r" (&idt_reg));
}

Expand Down
6 changes: 6 additions & 0 deletions drivers/descriptor/interrupt_handlers.s
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,9 @@ irq15:
push $15
push $47
jmp irq_common_stub

.global syscall
syscall:
push $0
push $0x80
jmp irq_common_stub
8 changes: 4 additions & 4 deletions drivers/descriptor/irq.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "descriptor.h"

static isr_t interrupt_handlers[256];
static isr_t interrupt_handlers[256] = {0};

void irq_handler(registers_t* r) {
if (r->int_no >= 40) {
port_byte_out(0xA0, 0x20); // primary EOI
if (r->int_no >= FIRST_SLAVE_PORT) {
port_byte_out(SLAVE_PORT, EOI);
}
port_byte_out(0x20, 0x20); // secondary end of interrupt (EOI)
port_byte_out(MASTER_PORT, EOI);
if (interrupt_handlers[r->int_no]) {
isr_t handler = interrupt_handlers[r->int_no];
handler(r);
Expand Down
8 changes: 5 additions & 3 deletions drivers/syscall/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ void sys_stat(registers_t* r) {
printf("Syscall stat : eax == %d\n", r->eax);
}

isr_t syscall_tab[] = {
#define NB_OF_SC 5

isr_t syscall_tab[NB_OF_SC] = {
sys_read,
sys_write,
sys_open,
Expand All @@ -30,9 +32,9 @@ isr_t syscall_tab[] = {
};

void syscall_callback(registers_t* r) {
syscall_tab[r->eax](r);
if (r->eax < NB_OF_SC) syscall_tab[r->eax](r);
}

void init_syscall() {
register_interrupt_handler(SYSCALL_INTERRUPT_NUMBER, &syscall_callback);
register_interrupt_handler(SYSCALL, &syscall_callback);
}
1 change: 0 additions & 1 deletion drivers/syscall/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "../descriptor/descriptor.h"

# define SYSCALL_INTERRUPT_NUMBER 0x80
void init_syscall();

#endif
2 changes: 1 addition & 1 deletion kernel/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void kernel_main(uint32_t magic, uint32_t addr) {
init_syscall();
asm volatile(
"mov $1, %eax\n"
"int $80"
"int $0x80"
);
pmm_init(mem_size, bitmap);

Expand Down

0 comments on commit 8580a08

Please sign in to comment.