Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Kernel/Core/Kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,10 @@ void KernelMain(uint32_t magic, uint32_t info) {
PicInstall();
MemoryInit();
ProcessInit();

// Create the security manager process (PID 1) - this is critical
uint32_t security_pid = CreateSecureProcess(SecureKernelIntegritySubsystem, PROC_PRIV_SYSTEM);
if (!security_pid) {
Panic("Cannot create SecureKernelIntegritySubsystem() - Critical security failure");
Panic("\nCannot create SecureKernelIntegritySubsystem() - Critical security failure\n");
}

PrintKernel("[SUCCESS] Security manager created with PID: ");
Expand Down
16 changes: 8 additions & 8 deletions Kernel/Drivers/Interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,47 @@ void itoa(uint64_t num, char* str) {
static void FastDisplayTicks(uint64_t ticks) {
uint16_t *vidptr = (uint16_t*)0xb8000;
int pos = 20 * 80; // Line 20
// Write "Ticks: "

// Write "Ticks: "
vidptr[pos++] = (0x03 << 8) | 'T';
vidptr[pos++] = (0x03 << 8) | 'i';
vidptr[pos++] = (0x03 << 8) | 'c';
vidptr[pos++] = (0x03 << 8) | 'k';
vidptr[pos++] = (0x03 << 8) | 's';
vidptr[pos++] = (0x03 << 8) | ':';
vidptr[pos++] = (0x03 << 8) | ' ';

// Fast number display
if (ticks == 0) {
vidptr[pos] = (0x03 << 8) | '0';
return;
}

char buf[20];
int i = 0;
uint64_t temp = ticks;

while (temp > 0) {
buf[i++] = '0' + (temp % 10);
temp /= 10;
}

while (i > 0) {
vidptr[pos++] = (0x03 << 8) | buf[--i];
}
}

// The C-level interrupt handler
void InterruptHandler(struct Registers* regs) {

// Handle timer interrupt (IRQ0, remapped to 32)
if (likely(regs->interrupt_number == 32)) {
tick_count++;
outb(0x20, 0x20);
ScheduleFromInterrupt(regs);
return;
}

// Send EOI to PICs for other hardware interrupts
if (regs->interrupt_number >= 40) {
outb(0xA0, 0x20); // EOI to slave PIC
Expand Down
30 changes: 24 additions & 6 deletions Kernel/Process/Process.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Memory.h"
#include "Panic.h"
#include "Io.h"
#define NULL ((void*)0)
static Process processes[MAX_PROCESSES];
static uint32_t next_pid = 1;
static uint32_t current_process = 0;
Expand Down Expand Up @@ -82,6 +83,11 @@ uint32_t CreateProcess(void (*entry_point)(void)) {
return CreateSecureProcess(entry_point, PROC_PRIV_USER);
}

void SecureProcessExitStub() {
PrintKernel("[SKIS] Process returned! This shouldn't happen!\n");
while (1) { __asm__ __volatile__("hlt"); }
}

uint32_t CreateSecureProcess(void (*entry_point)(void), uint8_t privilege) {
if (!entry_point) {
Panic("CreateSecureProcess: NULL entry point");
Expand Down Expand Up @@ -139,7 +145,16 @@ uint32_t CreateSecureProcess(void (*entry_point)(void), uint8_t privilege) {
}

// Set up initial context
processes[slot].context.rsp = (uint64_t)stack + STACK_SIZE - 8;
// Start RSP at the very top of the stack (STACK_SIZE)
// Then push the exit stub onto the stack
processes[slot].context.rsp = (uint64_t)processes[slot].stack + STACK_SIZE;
uint64_t* stack_ptr = (uint64_t*)processes[slot].context.rsp;
*(--stack_ptr) = (uint64_t)&SecureProcessExitStub;
processes[slot].context.rsp = (uint64_t)stack_ptr; // Update RSP after pushing

// Ensure RSP is 16-byte aligned for the entry_point function
processes[slot].context.rsp &= ~0xF; // Clear the last 4 bits to align to 16 bytes

processes[slot].context.rip = (uint64_t)entry_point;
processes[slot].context.rflags = 0x202;

Expand Down Expand Up @@ -172,8 +187,9 @@ void Schedule(void) {
PrintKernel("[SECURITY] Invalid token detected, terminating process\n");
processes[next].state = PROC_TERMINATED;
if (processes[next].stack) {
// FreePage(processes[next].stack); // Assuming you have this function
}
FreePage(processes[next].stack);
processes[next].stack = NULL;
}
process_count--;
}
}
Expand Down Expand Up @@ -326,6 +342,11 @@ void SecureKernelIntegritySubsystem(void) {
// Main security loop
while (1) {
// Periodic security checks
static int check_counter = 0;
if (++check_counter % 1000 != 0) {
for (volatile int i = 0; i < 1000; i++);
continue;
}
for (int i = 0; i < MAX_PROCESSES; i++) {
if (processes[i].state != PROC_TERMINATED) {
// Validate process tokens
Expand All @@ -339,10 +360,7 @@ void SecureKernelIntegritySubsystem(void) {
}
}
}

// Small delay to prevent excessive CPU usage
for (volatile int i = 0; i < 1000; i++);
}
}

// Example system service that can only be created by security manager
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,5 @@ iso = custom_target('VoidFrame.iso',

# Run target
run_target('run',
command : ['qemu-system-x86_64', '-cdrom', 'VoidFrame.iso', '-m', '1G']
command : ['qemu-system-x86_64', '-cdrom', 'VoidFrame.iso', '-no-reboot', '-no-shutdown' , '-m', '1G']
)