Fixed all but the isr.c and isr.h files. Still some mintor issues to fixe here.

This commit is contained in:
2023-09-21 15:40:56 -07:00
parent 74d12e70ef
commit 6d5229a0f7
9 changed files with 366 additions and 182 deletions

View File

@@ -5,15 +5,26 @@
#include <stdint.h>
// Keyboard input buffer
#define KEYBOARD_BUFFER_SIZE 32
#define KEYBOARD_BUFFER_SIZE 32
#define KEYBOARD_DATA_PORT 0x60
#define KEYBOARD_INTERRUPT_VECTOR 0x09
#define KEYBOARD_COMMAND_PORT 0x64
#define KEYBOARD_DATA_PORT 0x60
#define KEYBOARD_ENABLE_COMMAND 0xAE
#define KEYBOARD_ENABLE_SCANCODE 0xF4
#define KEYBOARD_ACKNOWLEDGE_SCANCODE 0xFA
static uint8_t keyboard_buffer[KEYBOARD_BUFFER_SIZE];
static size_t keyboard_buffer_head = 0;
static size_t keyboard_buffer_tail = 0;
void set_interrupt_vector(uint8_t vector, void (*handler)());
void enable_interrupt(uint8_t vector);
// Keyboard interrupt handler
void keyboard_interrupt_handler()
{
uint8_t scancode = inb(0x60);
uint8_t scancode = inb(KEYBOARD_DATA_PORT);
// Add scancode to buffer
keyboard_buffer[keyboard_buffer_head] = scancode;
@@ -24,15 +35,15 @@ void keyboard_interrupt_handler()
void keyboard_init()
{
// Install keyboard interrupt handler
set_interrupt_vector(0x09, keyboard_interrupt_handler);
enable_interrupt(0x09);
set_interrupt_vector(KEYBOARD_INTERRUPT_VECTOR, keyboard_interrupt_handler);
enable_interrupt(KEYBOARD_INTERRUPT_VECTOR);
// Enable keyboard
outb(0x64, 0xAE);
while (inb(0x64) & 0x02)
outb(KEYBOARD_COMMAND_PORT, KEYBOARD_ENABLE_COMMAND);
while (inb(KEYBOARD_COMMAND_PORT) & 0x02)
;
outb(0x60, 0xF4);
while (inb(0x60) != 0xFA)
outb(KEYBOARD_DATA_PORT, KEYBOARD_ENABLE_SCANCODE);
while (inb(KEYBOARD_DATA_PORT) != KEYBOARD_ACKNOWLEDGE_SCANCODE)
;
}

View File

@@ -1,11 +1,13 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
void keyboard_init();
bool keyboard_buffer_empty();
void keyboard_init();
bool keyboard_buffer_empty();
uint8_t keyboard_read_scancode();
void set_interrupt_vector(uint8_t vector, void (*handler)());
void enable_interrupt(uint8_t vector);
#endif

View File

@@ -1,29 +1,34 @@
// gdt.h
#ifndef GDT_H
#define GDT_H
#include <stdint.h>
// GDT table
extern struct gdt_entry *gdt;
// GDT entry structure
struct gdt_entry
{
uint16_t limit_low; // Lower 16 bits of segment limit
uint16_t base_low; // Lower 16 bits of segment base address
uint8_t base_middle; // Middle 8 bits of segment base address
uint8_t access; // Access flags
uint8_t granularity; // Granularity and segment limit flags
uint8_t base_high; // Upper 8 bits of segment base address
uint16_t limit_low; // The lower 16 bits of the limit
uint16_t base_low; // The lower 16 bits of the base
uint8_t base_middle; // The next 8 bits of the base
uint8_t access; // Access flags, determine what ring this segment can be used in
uint8_t granularity;
uint8_t base_high; // The last 8 bits of the base
} __attribute__((packed));
// GDT pointer structure
struct gdt_ptr
{
uint16_t limit; // Size of GDT in bytes - 1
uint32_t base; // Address of GDT
uint16_t limit; // The upper 16 bits of all selector limits
uint32_t base; // The address of the first gdt_entry_t struct
} __attribute__((packed));
// Initialize the GDT
void gdt_init();
// Set a GDT entry
void gdt_set_gate(uint32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran);
#endif

View File

@@ -37,7 +37,7 @@ extern void serial_port_handler();
// Initialize an IDT entry
void idt_set_gate(uint8_t num, void *base, uint16_t sel, uint8_t flags)
{
uint32_t base_addr = (uint32_t)base;
uintptr_t base_addr = (uintptr_t)base;
idt[num].base_lo = base_addr & BYTE_MASK;
idt[num].base_hi = (base_addr >> BASE_MIDDLE_SHIFT) & 0xFFFF;
idt[num].sel = sel;

View File

@@ -3,16 +3,10 @@
#include <stdint.h>
// Structure for storing register values during an ISR
struct isr_regs
// Define the isr_regs structure
struct idt_regs
{
uint32_t ds; // Data segment selector
uint32_t edi, esi, ebp, esp, ebx, edx, ecx,
eax; // Pushed by pusha instruction
uint32_t int_no,
err_code; // Interrupt number and error code (if applicable)
uint32_t eip, cs, eflags, useresp,
ss; // Pushed by the processor automatically
// Add the necessary members for your ISR context
};
// IDT entry structure
@@ -34,12 +28,12 @@ struct idt_ptr
// Exception handlers
void divide_error();
void page_fault(struct isr_regs *regs);
void general_protection_fault(struct isr_regs *regs);
void page_fault(struct idt_regs *);
void general_protection_fault(struct idt_regs *);
void double_fault();
// Interrupt handlers
void system_call(struct isr_regs *regs);
void system_call(struct idt_regs *);
void timer();
void keyboard();
void device();

View File

@@ -2,14 +2,21 @@
#include "idt.h"
#include <stdio.h>
enum {
TIMER_INTERRUPT = 0x20,
KEYBOARD_INTERRUPT = 0x21,
NETWORK_INTERRUPT = 0x22,
DISK_INTERRUPT = 0x23,
enum
{
TIMER_INTERRUPT = 0x20,
KEYBOARD_INTERRUPT = 0x21,
NETWORK_INTERRUPT = 0x22,
DISK_INTERRUPT = 0x23,
SERIAL_PORT_INTERRUPT = 0x24
};
struct isr_regs
{
// Define the structure of the isr_regs here
// ...
};
// ISR table
void (*isr_table[256])(struct isr_regs *regs);
@@ -19,26 +26,29 @@ void isr_register(uint8_t num, void (*handler)(struct isr_regs *regs))
isr_table[num] = handler;
}
// ISR handler
// ISR handler
void isr_handler(struct isr_regs *regs)
{
switch (regs->int_no) {
case 0:
divide_error();
break;
case 13:
general_protection_fault(regs);
break;
case 14:
page_fault(regs);
break;
default:
void (*handler)(struct isr_regs *regs);
handler = isr_table[regs->int_no];
if (handler) {
handler(regs);
}
break;
switch (regs->int_no)
{
case 0:
divide_error();
break;
case 13:
general_protection_fault(regs);
break;
case 14:
page_fault(regs);
break;
default:
void (*handler)(struct isr_regs *regs);
handler = isr_table[regs->int_no];
if (handler)
{
handler(regs);
}
break;
}
}
@@ -81,29 +91,30 @@ void system_call(struct isr_regs *regs)
uint32_t syscall_number = regs->eax;
// Handle different system call numbers
switch (syscall_number) {
case 1:
// Handle open() system call
// ...
break;
case 2:
// Handle read() system call
// ...
break;
case 3:
// Handle write() system call
// ...
break;
case 4:
// Handle close() system call
// ...
break;
switch (syscall_number)
{
case 1:
// Handle open() system call
// ...
break;
case 2:
// Handle read() system call
// ...
break;
case 3:
// Handle write() system call
// ...
break;
case 4:
// Handle close() system call
// ...
break;
// Add more cases for other system calls
default:
// Unknown system call number
printf("Unknown system call number: %d\n", syscall_number);
break;
default:
// Unknown system call number
printf("Unknown system call number: %d\n", syscall_number);
break;
}
}
@@ -123,27 +134,28 @@ void device()
uint32_t interrupt_type = read_interrupt_type();
// Call the appropriate interrupt handler
switch (interrupt_type) {
case TIMER_INTERRUPT:
timer();
break;
case KEYBOARD_INTERRUPT:
keyboard();
break;
case NETWORK_INTERRUPT:
network();
break;
case DISK_INTERRUPT:
disk();
break;
case SERIAL_PORT_INTERRUPT:
serial_port();
break;
switch (interrupt_type)
{
case TIMER_INTERRUPT:
timer();
break;
case KEYBOARD_INTERRUPT:
keyboard();
break;
case NETWORK_INTERRUPT:
network();
break;
case DISK_INTERRUPT:
disk();
break;
case SERIAL_PORT_INTERRUPT:
serial_port();
break;
// Add more cases for other types of device interrupts
default:
// Unknown interrupt type
printf("Unknown device interrupt type: %d\n", interrupt_type);
break;
default:
// Unknown interrupt type
printf("Unknown device interrupt type: %d\n", interrupt_type);
break;
}
}