diff --git a/drivers/Pic.c b/drivers/Pic.c index 8cf4da8..6e12a50 100644 --- a/drivers/Pic.c +++ b/drivers/Pic.c @@ -20,7 +20,23 @@ void PitInstall() { outb(0x40, (divisor >> 8) & 0xFF); // High byte } - +void PitSetFrequency(uint16_t hz) { + // Save current interrupt state + irq_flags_t flags = save_irq_flags(); + cli(); + + PIT_FREQUENCY_HZ = hz; + // Safer divisor calculation + uint32_t div32 = 1193180u / (hz ? hz : 1u); + uint16_t divisor = (uint16_t)div32; + + outb(0x43, 0x36); + outb(0x40, divisor & 0xFF); + outb(0x40, (divisor >> 8) & 0xFF); + + // Restore previous interrupt state + restore_irq_flags(flags); +} void PicInstall() { uint8_t a1, a2; @@ -52,7 +68,4 @@ void PicInstall() { // Read current mask, clear bit 0, write back uint8_t pic1_mask = inb(PIC1_DATA); outb(PIC1_DATA, pic1_mask & ~0x01); // Clear bit 0 (IRQ0) - - // Initialize PIT after PIC - PitInstall(); } diff --git a/drivers/Pic.h b/drivers/Pic.h index 7d9531f..37e0ad6 100644 --- a/drivers/Pic.h +++ b/drivers/Pic.h @@ -1,7 +1,7 @@ #ifndef PIC_H #define PIC_H - +#include "stdint.h" void PicInstall(); void PitInstall(); - +void PitSetFrequency(uint16_t hz); #endif \ No newline at end of file diff --git a/drivers/VesaBIOSExtension.c b/drivers/VesaBIOSExtension.c index eff8223..2b4cfd5 100644 --- a/drivers/VesaBIOSExtension.c +++ b/drivers/VesaBIOSExtension.c @@ -342,7 +342,7 @@ void VBEDrawStringCentered(uint32_t center_x, uint32_t center_y, const char* str void VBEShowSplash(void) { if (!vbe_initialized) return; - for (unsigned int i = 0; i < num_splash_images * 6; i++) { // Loop + for (unsigned int i = 0; i < num_splash_images * 4; i++) { // Loop const uint32_t* image_data = (const uint32_t*)splash_images[i % num_splash_images]; for (uint32_t y = 0; y < vbe_info.height; y++) { diff --git a/kernel/core/Kernel.c b/kernel/core/Kernel.c index 7fc8750..9fc2a7d 100644 --- a/kernel/core/Kernel.c +++ b/kernel/core/Kernel.c @@ -321,9 +321,10 @@ static InitResultT SystemInitS2(void) { PrintKernelSuccess("[SYSTEM] System calls initialized\n"); // Initialize PIC - PrintKernel("[INFO] Initializing PIC...\n"); + PrintKernel("[INFO] Initializing PIC & PIT...\n"); PicInstall(); - PrintKernelSuccess("[SYSTEM] PIC initialized\n"); + PitInstall(); + PrintKernelSuccess("[SYSTEM] PIC & PIT initialized\n"); // Initialize keyboard PrintKernel("[INFO] Initializing keyboard...\n"); diff --git a/kernel/etc/Shell.c b/kernel/etc/Shell.c index 869e088..5f13cbd 100644 --- a/kernel/etc/Shell.c +++ b/kernel/etc/Shell.c @@ -7,15 +7,15 @@ #include "Memory.h" #include "PCI/PCI.h" #include "PS2.h" +#include "Packet.h" #include "Panic.h" #include "Process.h" +#include "RTL8139.h" #include "StringOps.h" #include "VFS.h" #include "VMem.h" #include "VesaBIOSExtension.h" #include "stdlib.h" -#include "Packet.h" -#include "RTL8139.h" static char command_buffer[256]; static int cmd_pos = 0; @@ -29,10 +29,7 @@ static void Version() { void info(void) { if (!VBEIsInitialized()) return; VBEShowInfo(); - int count = 10000000; - while (--count) { - Yield(); - } + Yield(); } static char* GetArg(const char* cmd, int arg_num) { diff --git a/kernel/memory/MemOps.c b/kernel/memory/MemOps.c index 5fdf279..6d0d6c4 100644 --- a/kernel/memory/MemOps.c +++ b/kernel/memory/MemOps.c @@ -38,6 +38,10 @@ void itoa(uint64_t n, char* buffer) { strcpy(buffer, p); } +void* memset(void* dest, int value, unsigned long size) { // FCK GCC + return FastMemset(dest, value, size); +} + void* FastMemset(void* dest, int value, uint64_t size) { ASSERT(dest != NULL); CpuFeatures* features = GetCpuFeatures(); diff --git a/kernel/memory/MemOps.h b/kernel/memory/MemOps.h index 083e389..1395556 100644 --- a/kernel/memory/MemOps.h +++ b/kernel/memory/MemOps.h @@ -7,6 +7,7 @@ void* FastMemset(void* dest, int value, uint64_t size); void* FastMemcpy(void* dest, const void* src, uint64_t size); int FastMemcmp(const void* ptr1, const void* ptr2, uint64_t size); void FastZeroPage(void* page); +void* memset(void* dest, int value, unsigned long size); void strcpy(char* dest, const char* src); void strcat(char* dest, const char* src); diff --git a/kernel/process/Process.c b/kernel/process/Process.c index 1efdf63..7672742 100644 --- a/kernel/process/Process.c +++ b/kernel/process/Process.c @@ -5,8 +5,8 @@ #include "Io.h" #include "Ipc.h" #include "MemOps.h" -#include "Memory.h" #include "Panic.h" +#include "Pic.h" #include "Shell.h" #include "Spinlock.h" #include "VMem.h" @@ -65,10 +65,6 @@ static uint64_t scheduler_calls = 0; extern uint16_t PIT_FREQUENCY_HZ; -static void UpdatePIT(const uint16_t hz) { - PIT_FREQUENCY_HZ = hz; -} - static int FastFFS(const uint64_t value) { return __builtin_ctzll(value); } @@ -907,6 +903,7 @@ void RequestSchedule(void) { } void Yield() { + irq_flags_t flags = SpinLockIrqSave(&scheduler_lock); Process* current = GetCurrentProcess(); if (current) { // A process that yields is ready to run again, just giving up its timeslice. @@ -916,6 +913,7 @@ void Yield() { RequestSchedule(); // This instruction halts the CPU until the next interrupt (e.g., the timer), // which will then trigger the scheduler. + SpinUnlockIrqRestore(&scheduler_lock, flags); __asm__ __volatile__("hlt"); } @@ -1225,7 +1223,7 @@ void SystemService(void) { // Hysteresis - only change if difference is significant if (ABSi(new_freq - controller.current_freq) > 10) { - UpdatePIT(new_freq); + PitSetFrequency(new_freq); controller.current_freq = new_freq; // Log the change for analysis diff --git a/kernel/process/Process.h b/kernel/process/Process.h index 71105e2..0236727 100644 --- a/kernel/process/Process.h +++ b/kernel/process/Process.h @@ -5,9 +5,8 @@ #include "Ipc.h" #include "Cpu.h" - // ============================================================================= -// MLFQ SCHEDULER TUNING PARAMETERS +// MLFQ SCHEDULER PARAMETERS // ============================================================================= // Core Queue Configuration @@ -83,8 +82,8 @@ #define FAIRNESS_BOOST_ACTUAL_INTERVAL (FAIRNESS_BOOST_INTERVAL * FAIRNESS_BOOST_MULTIPLIER) #define LOAD_BALANCE_ACTUAL_THRESHOLD (LOAD_BALANCE_THRESHOLD * LOAD_BALANCE_MULTIPLIER) -#define PROC_PRIV_SYSTEM 0 // Highest privilege (kernel services) -#define PROC_PRIV_USER 1 // User processes +#define PROC_PRIV_SYSTEM 0 // Highest privilege (kernel services) +#define PROC_PRIV_USER 1 // User processes #define PROC_PRIV_RESTRICTED 2 // Restricted processes #define PIT_BUFF 20 diff --git a/meson.build b/meson.build index 97ab237..776e363 100644 --- a/meson.build +++ b/meson.build @@ -25,6 +25,7 @@ c_flags = [ '-finline-functions', '-foptimize-sibling-calls', '-nostdinc', + '-nostdlib', '-fno-builtin', '-ffreestanding', '-fstack-protector-strong',