This commit is contained in:
nub31
2025-09-02 18:55:46 +02:00
parent a5bd7ad9a6
commit b533485614
9 changed files with 101 additions and 92 deletions

View File

@@ -43,13 +43,15 @@ static const char* exception_messages[32] = {
static irq_handler_t irq_handlers[16] = {0}; static irq_handler_t irq_handlers[16] = {0};
bool cpu_has_apic() { bool cpu_has_apic()
{
uint32_t eax, edx; uint32_t eax, edx;
cpuid(1, &eax, &edx); cpuid(1, &eax, &edx);
return (edx & CPUID_FEAT_EDX_APIC) != 0; return (edx & CPUID_FEAT_EDX_APIC) != 0;
} }
void enable_apic() { void enable_apic()
{
uint64_t apic_base = rdmsr(0x1B); uint64_t apic_base = rdmsr(0x1B);
apic_base |= (1 << 11); apic_base |= (1 << 11);
wrmsr(0x1B, apic_base); wrmsr(0x1B, apic_base);
@@ -75,7 +77,8 @@ void remap_pic()
kprintf("PIC remapped\n"); kprintf("PIC remapped\n");
} }
void disable_pic() { void disable_pic()
{
outb(PIC1_DATA, 0xFF); outb(PIC1_DATA, 0xFF);
outb(PIC2_DATA, 0xFF); outb(PIC2_DATA, 0xFF);
kprintf("PIC disabled\n"); kprintf("PIC disabled\n");
@@ -83,7 +86,8 @@ void disable_pic() {
void handle_exception(const isr_frame_t* frame) void handle_exception(const isr_frame_t* frame)
{ {
kprintf("exception[%d]: %s, error code: %d\n", frame->int_no, exception_messages[frame->int_no], frame->err_code); kprintf("exception[%d]: %s, error code: %d\n", frame->int_no, exception_messages[frame->int_no],
frame->err_code);
kpanic(); kpanic();
} }

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "kernel.h" #include "kernel.h"
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
typedef struct typedef struct

View File

@@ -1,10 +1,10 @@
#include "kernel.h" #include "kernel.h"
#include "interrupts.h" #include "interrupts.h"
#include "keyboard.h" #include "keyboard.h"
#include "multiboot.h"
#include "string.h" #include "string.h"
#include "vga.h" #include "vga.h"
#include <stdarg.h> #include <stdarg.h>
#include "multiboot.h"
void handle_keypress(const keyboard_event_t* event) void handle_keypress(const keyboard_event_t* event)
{ {

View File

@@ -6,7 +6,7 @@ static inline void khalt()
{ {
while (true) while (true)
{ {
__asm__ volatile ("hlt"); __asm__ volatile("hlt");
} }
} }

View File

@@ -1,7 +1,6 @@
#include "keyboard.h" #include "keyboard.h"
#include "interrupts.h" #include "interrupts.h"
#include "kernel.h" #include "kernel.h"
#include "vga.h"
#include "util.h" #include "util.h"
#include <stddef.h> #include <stddef.h>

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
typedef struct typedef struct

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
int strcmp(const char* a, const char* b); int strcmp(const char* a, const char* b);
void reverse(char* str, size_t length); void reverse(char* str, size_t length);

View File

@@ -2,7 +2,8 @@
#include <stdint.h> #include <stdint.h>
enum { enum
{
CPUID_FEAT_ECX_SSE3 = 1 << 0, CPUID_FEAT_ECX_SSE3 = 1 << 0,
CPUID_FEAT_ECX_PCLMUL = 1 << 1, CPUID_FEAT_ECX_PCLMUL = 1 << 1,
CPUID_FEAT_ECX_DTES64 = 1 << 2, CPUID_FEAT_ECX_DTES64 = 1 << 2,
@@ -84,17 +85,20 @@ static inline void io_wait()
outb(0x80, 0); outb(0x80, 0);
} }
static inline void cpuid(uint32_t code, uint32_t* a, uint32_t* d) { static inline void cpuid(uint32_t code, uint32_t* a, uint32_t* d)
{
__asm__ volatile("cpuid" : "=a"(*a), "=d"(*d) : "a"(code) : "ecx", "ebx"); __asm__ volatile("cpuid" : "=a"(*a), "=d"(*d) : "a"(code) : "ecx", "ebx");
} }
static inline uint64_t rdmsr(uint32_t msr) { static inline uint64_t rdmsr(uint32_t msr)
{
uint32_t lo, hi; uint32_t lo, hi;
__asm__ volatile("rdmsr" : "=a"(lo), "=d"(hi) : "c"(msr)); __asm__ volatile("rdmsr" : "=a"(lo), "=d"(hi) : "c"(msr));
return ((uint64_t)hi << 32) | lo; return ((uint64_t)hi << 32) | lo;
} }
static inline void wrmsr(uint32_t msr, uint64_t value) { static inline void wrmsr(uint32_t msr, uint64_t value)
{
uint32_t lo = (uint32_t)value; uint32_t lo = (uint32_t)value;
uint32_t hi = (uint32_t)(value >> 32); uint32_t hi = (uint32_t)(value >> 32);
__asm__ volatile("wrmsr" : : "c"(msr), "a"(lo), "d"(hi)); __asm__ volatile("wrmsr" : : "c"(msr), "a"(lo), "d"(hi));