feat: TSS with a cup of my tears; Maan i fucking HATE intel.- Implemented TSS (it works!.. mostly..) - fun messages in panic - my first issue in this os! rectest() issue! running it causes triplefault without panic i plan to resolve rectest() when i get memory manager

This commit is contained in:
Karina
2025-12-23 04:16:31 +04:00
parent e834122e08
commit d7da0a8046
10 changed files with 141 additions and 19 deletions
+5
View File
@@ -1,6 +1,11 @@
bits 64
section .bss
align 4096
guard_page:
resb 4096
section .stack nobits
align 16
stack_bottom:
resb 16384
+13 -3
View File
@@ -12,6 +12,11 @@
#include "kfetch.h"
int rectest(int a) {
volatile int b = a + 1;
return rectest(b * 2);
}
void kmain(Bootinfo* info) {
u32 *fb = (u32*)info->framebuffer.base;
if (!fb) return;
@@ -32,11 +37,16 @@ void kmain(Bootinfo* info) {
SG_Point logo_point = {sg_ctx.width-100, 100};
sg_put_img(&sg_ctx, &logo_point, &logo_img);
// kprintf("Welcome to ^ptermOS^0!!!\n");
// kprintf("MemoryMap located at ^g%x^0; \nMemory map size is ^g%x^0\n", (u64)info->mem.map, (u64)info->mem.map_size);
kprintf("Welcome to ^ptermOS^0!!!\n");
kprintf("MemoryMap located at ^g%x^0; \nMemory map size is ^g%x^0\n", (u64)info->mem.map, (u64)info->mem.map_size);
// kfetch();
kfetch();
// kprintf("I cant do anything yet lol");
kprintf("stack overflow protection test");
// rectest(0);
__asm__("ud2"); // panic :(
+30 -8
View File
@@ -2,8 +2,10 @@
#include "gdt.h"
GDTDescriptor gdt[3];
GDTDescriptor gdt[5];
GDTPtr gdt_ptr;
TSS tss;
u8 double_fault_stack[4096] = {0};
void gdt_set_gate(int num, u64 base, u64 limit, u8 access, u8 gran) {
gdt[num].base_low = (base & 0xFFFF); // left only low 16bit
@@ -15,17 +17,27 @@ void gdt_set_gate(int num, u64 base, u64 limit, u8 access, u8 gran) {
gdt[num].granularity |= gran & 0xF0; // get flags and concate two pieces so FFFFLLLL (L -- Limit; F -- flags)
gdt[num].access = access;
// intel,
// please
// FUCK YOU
// cancer of industry
// AMD FOREVER
// fuck intel
}
void write_tss(int num) {
u64 base = (u64)&tss;
u64 limit = sizeof(tss);
u64 access = 0x89; // TSS_PRESENT | TSS_EXECUTABLE | TSS_ACCESSED;
u64 gran = 0;
gdt_set_gate(num, base, limit, access, gran);
*((u64*)&gdt[num+1]) = (base >> 32); // really scary but not really hard
// just direct write 8bytes to num+1
// basically a hack but at this point i dont care
}
extern void gdt_flush(u64 gdt_ptr_addr); // entry.asm
void gdt_init() {
gdt_ptr.limit = (sizeof(GDTDescriptor) * 3) - 1;
gdt_ptr.limit = (sizeof(GDTDescriptor) * 5) - 1;
gdt_ptr.base = (u64)&gdt;
// 0: Null
@@ -40,5 +52,15 @@ void gdt_init() {
// Access: 0x92 (Present, Ring0, Read/Write)
gdt_set_gate(2, 0, 0, 0x92, 0); // 0x92 can't exec
for (u64 i = 0; i < sizeof(TSS); i++) ((u8*)&tss)[i] = 0; // hack. zeroifying tss struct
tss.iomap_base = sizeof(TSS);
tss.ist1 = (u64)double_fault_stack + sizeof(double_fault_stack);
write_tss(3);
gdt_flush((u64)&gdt_ptr);
}
// telling cpu that TSS info located at gdt[3]; offset = 3 * 8 = 24 = 0x18
__asm__ volatile ("ltr %%ax" :: "a" (0x18));
}
+8 -3
View File
@@ -32,6 +32,7 @@ void idt_init() {
const u8 flags = 0x8E;
const u8 selector = 0x08;
idt_set_gate(0, (u64)isr0, selector, flags);
idt_set_gate(1, (u64)isr1, selector, flags);
@@ -46,8 +47,8 @@ void idt_init() {
idt_set_gate(10, (u64)isr10, selector, flags);
idt_set_gate(11, (u64)isr11, selector, flags);
idt_set_gate(12, (u64)isr12, selector, flags);
idt_set_gate(13, (u64)isr13, selector, flags); // General Protection Fault
idt_set_gate(14, (u64)isr14, selector, flags); // Page Fault
idt_set_gate(13, (u64)isr13, selector, flags);
idt_set_gate(14, (u64)isr14, selector, flags);
idt_set_gate(15, (u64)isr15, selector, flags);
idt_set_gate(16, (u64)isr16, selector, flags);
idt_set_gate(17, (u64)isr17, selector, flags);
@@ -66,5 +67,9 @@ void idt_init() {
idt_set_gate(30, (u64)isr30, selector, flags);
idt_set_gate(31, (u64)isr31, selector, flags);
idt[8].ist = 1; // TSS
idt[13].ist = 1;
idt[14].ist = 1;
__asm__ volatile ("lidt %0" : : "m"(idt_ptr));
}
}
+41 -4
View File
@@ -1,8 +1,33 @@
#include "panic.h"
#include "console.h"
#include "rand.h"
#include "types.h"
#include "shitgui.h"
const char* fun_messages[] = {
"Ooops! All red!",
"Execution finished abnormally with code: KYS",
"It's definitely your fault.",
"No more Roblox!",
"Call your mom 4 help!",
"2bad4u",
"Touch grass",
"Skill issue",
"You should just go outside actually",
"Perfect opportunity to take a shower",
"404 not found",
"Windows is locked! Password:___ Time left: 5:45:41",
"GOOOOOAL",
"\"NAM PIZDA\": hackers dropped our registry",
"geeked vs locked",
"HEEEELP!",
"LET ME GO!",
"You little asshole, make it better",
"Go and read books for programming u idiot",
"TIME TO BRAKE! :3 Take a tea and try again later cutie",
"Code have been eaten by Aliens"
};
const char* exception_messages[] = {
"Division By Zero",
@@ -50,17 +75,29 @@ __attribute__((noreturn)) void panic(Registers *regs) {
console_set_cursor_pos(&p);
u64 msg_count = sizeof(fun_messages) / sizeof(fun_messages[0]);
u8 rand_num = shitrand() % msg_count;
kprintf("\n\n");
kprintf("\t\t\tKERNEL PANIC :( \n");
kprintf("\t\t-------------------------\n");
kprintf("\t\t\t\tKERNEL PANIC :( \n");
kprintf("\t\t--------------------------------\n");
kprintf("\t\t\t\t%s\n", fun_messages[rand_num]);
kprintf("\t\t--------------------------------\n");
kprintf("\t\tCPU EXCEPTION: %s (%d)\n", exception_messages[regs->int_no], regs->int_no);
if (regs->err_code) kprintf("\t\tError Code: %x\n", regs->err_code);
kprintf("\t\tInstruction Pointer (RIP): %x\n", regs->rip);
kprintf("\t\tCode Segment (CS): %x\n", regs->cs);
kprintf("\t\tFlags (RFLAGS): %x\n", regs->rflags);
kprintf("\t\tStack Pointer (RSP): %x\n", regs->rsp);
kprintf("\t\t-------------------------\n");
kprintf("\t\t\tSystem halted.\n");
kprintf("\t\t--------------------------------\n");
kprintf("\t\t\t\tREGSISTERS\n");
kprintf("\t\t--------------------------------\n");
kprintf("\t\tRAX=%x, RBX=%x, RCX=%x, RDX=%x\n", regs->rax, regs->rbx, regs->rcx, regs->rdx);
kprintf("\t\tRSI=%x, RDI=%x, RBP=%x, R8=%x\n", regs->rsi, regs->rdi, regs->rbp, regs->r8);
kprintf("\t\tR9=%x, R10=%x, R11=%x, R12=%x\n", regs->r9, regs->r10, regs->r11, regs->r12);
kprintf("\t\tR13=%x, R14=%x, R15=%x\n", regs->r13, regs->r14, regs->r15);
kprintf("\t\t--------------------------------\n");
kprintf("\t\t\t\tSystem halted.\n");
while (1) {
__asm__ volatile ("cli; hlt");
+12
View File
@@ -0,0 +1,12 @@
#include "rand.h"
#include "types.h"
// really shiity rand lol.
// not shitty.. basic.
u64 shitrand() {
int lo, hi;
__asm__ volatile ("rdtsc" : "=a" (lo), "=d" (hi)); // why fucking asm refusing to eat my u32
return ((u64)hi << 32) | lo;
}