Working kernel written on C and userspace-ready #1
@@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <types.h>
|
||||||
|
|
||||||
static inline void CPUYield() {
|
static inline void CPUYield() {
|
||||||
__asm__ volatile ("yield" ::: "memory");
|
__asm__ volatile ("yield" ::: "memory");
|
||||||
@@ -7,3 +8,11 @@ static inline void CPUYield() {
|
|||||||
static inline void CPUWaitForInterrupt() {
|
static inline void CPUWaitForInterrupt() {
|
||||||
__asm__ volatile ("wfi" ::: "memory");
|
__asm__ volatile ("wfi" ::: "memory");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void CPUDisableInterrupts() {
|
||||||
|
__asm__ volatile ("msr daifset, #3" ::: "memory");
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void CPUEnableInterrupts() {
|
||||||
|
__asm__ volatile ("msr daifclr, #3" ::: "memory");
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <types.h>
|
||||||
|
#include <Arch/Exceptions.h>
|
||||||
|
|
||||||
|
__attribute__((noreturn)) void OSPanicException(ExceptionsContext* frame);
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
#include <Arch/Exceptions.h>
|
#include <Arch/Exceptions.h>
|
||||||
#include <Arch/CPU.h>
|
#include <Arch/CPU.h>
|
||||||
#include <IO/Serial.h>
|
#include <IO/Serial.h>
|
||||||
|
#include <OS/Panic.h>
|
||||||
|
|
||||||
void ExceptionsHandler(ExceptionsContext* context, ExceptionsType type) {
|
void ExceptionsHandler(ExceptionsContext* frame, ExceptionsType type) {
|
||||||
IOSerialPutString("Exception occurred");
|
OSPanicException(frame);
|
||||||
while (1) {
|
|
||||||
CPUWaitForInterrupt();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,4 @@
|
|||||||
|
|
||||||
void KernelMain(void) {
|
void KernelMain(void) {
|
||||||
IOSerialPutString("Meow nya!!\n");
|
IOSerialPutString("Meow nya!!\n");
|
||||||
__asm__ volatile ("brk #0");
|
|
||||||
IOSerialPutString("How\r\n");
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
#include <OS/Panic.h>
|
||||||
|
#include <IO/Serial.h>
|
||||||
|
#include <Arch/CPU.h>
|
||||||
|
|
||||||
|
static const ASCII* GetExceptionClassString(UInt32 class) {
|
||||||
|
switch (class) {
|
||||||
|
case 0x00: return "Unknown reason";
|
||||||
|
case 0x01: return "Trapped WFI | WFE instruction";
|
||||||
|
case 0x07: return "Trapped SIMD | FP instruction";
|
||||||
|
case 0x11: return "SVC from EL1";
|
||||||
|
case 0x15: return "SVC from EL0";
|
||||||
|
case 0x20: return "Instruction abort (LoEL)"; // User Execute Fault
|
||||||
|
case 0x21: return "Instruction abort (CurrEL)"; // Kernel Execute Fault
|
||||||
|
case 0x22: return "PC Alignment Fault"; // Jumped to a misaligned address
|
||||||
|
case 0x24: return "Data abort (LoEL)"; // User Memory Fault
|
||||||
|
case 0x25: return "Data abort (CurrEL)"; // Kernel Memory Fault
|
||||||
|
case 0x26: return "SP Alignment Fault"; // SP must be 16-byte aligned
|
||||||
|
case 0x3C: return "Breakpoint";
|
||||||
|
default: return "Reserved";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
__attribute__((noreturn)) void OSPanicException(ExceptionsContext* frame) {
|
||||||
|
UInt32 esr = frame->esr_el1;
|
||||||
|
UInt32 class = (esr >> 26) & 0x3F; // Exception class (Bits 31:26)
|
||||||
|
UInt32 length = (esr >> 25) & 0x1; // Instruction length (Bit 25)
|
||||||
|
UInt32 syndrome = esr & 0x1FFFFFF; // Syndrome (Bits 24:0)
|
||||||
|
|
||||||
|
// i cant show any info since i have no formatter yet :(
|
||||||
|
IOSerialPutString("\n-------------------\n");
|
||||||
|
IOSerialPutString("Kernel Panic! :(\n");
|
||||||
|
IOSerialPutString("-------------------\n");
|
||||||
|
IOSerialPutString("CPU Exception: ");
|
||||||
|
IOSerialPutString(GetExceptionClassString(class));
|
||||||
|
IOSerialPutString("\n-------------------\n");
|
||||||
|
IOSerialPutString("System halted.\n");
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
CPUDisableInterrupts();
|
||||||
|
CPUWaitForInterrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user