Added free page count to panic()

This commit is contained in:
2026-03-27 15:05:47 +01:00
parent f316916ff1
commit ad45de562a
4 changed files with 40 additions and 6 deletions
+3
View File
@@ -3,6 +3,9 @@
#include <kernel.h> #include <kernel.h>
extern size_t pmm_total_pages;
extern size_t pmm_free_pages;
int pmm_init(void); int pmm_init(void);
void* pmm_alloc(void); void* pmm_alloc(void);
int pmm_free(void*); int pmm_free(void*);
+4
View File
@@ -8,6 +8,8 @@
#include <isr.h> #include <isr.h>
#include <pmm.h> #include <pmm.h>
void a(void) { *(char*)(1234) = 0; }
__noreturn __noreturn
void _start(void) { void _start(void) {
int err = 0; int err = 0;
@@ -35,6 +37,8 @@ void _start(void) {
// The most important function in the world. // The most important function in the world.
splash(); splash();
a();
// Patiently wait for things to do :) // Patiently wait for things to do :)
while (1); while (1);
}; };
+25 -2
View File
@@ -2,9 +2,28 @@
#include <kernel.h> #include <kernel.h>
#include <stdarg.h> #include <stdarg.h>
#include <system.h> #include <system.h>
#include <pmm.h>
static int crashing = 0; static int crashing = 0;
void panic_call_trace(void) {
uintptr_t* rbp;
__asm__ volatile ("movq %%rbp, %0" : "=r"(rbp));
pr_emerg("Call trace:\n");
for (size_t i = 0; i < 16 && (uintptr_t)rbp > 0x1000; i++) {
uintptr_t retaddr = rbp[1];
offset_t offset = 0;
// TODO: Parse ELF and print function names
char* fname = "???";
pr_emerg(" [<%p>] %s+0x%x\n", retaddr, fname, offset);
rbp = (uintptr_t*)*rbp;
}
}
void panic(const char *fmt, ...) { void panic(const char *fmt, ...) {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
@@ -24,8 +43,12 @@ void vpanic(const char *fmt, va_list ap) {
len = vsnprintf(buf, sizeof(buf), fmt, ap); len = vsnprintf(buf, sizeof(buf), fmt, ap);
if (len && buf[len - 1] == '\n') buf[len - 1] = '\0'; if (len && buf[len - 1] == '\n') buf[len - 1] = '\0';
pr_emerg("--- [ Kernel panic: %s ] ---\n", buf); pr_emerg("--- [ Kernel panic: %s ] ---\n\n", buf);
//pr_emerg("--- [ End kernel panic: %s ] ---\n", buf);
pr_emerg("%d / %d pages free.\n\n", pmm_free_pages, pmm_total_pages);
panic_call_trace();
pr_emerg("\n--- [ End kernel panic: %s ] ---\n", buf);
// We can add some kind of LED blink or smth later :) // We can add some kind of LED blink or smth later :)
for (;;) __asm__ volatile ("hlt"); for (;;) __asm__ volatile ("hlt");
+8 -4
View File
@@ -7,7 +7,8 @@
#include <pmm.h> #include <pmm.h>
uint64_t* pmm_head = NULL; uint64_t* pmm_head = NULL;
size_t pmm_total_mem = 0; size_t pmm_total_pages = 0;
size_t pmm_free_pages = 0;
int pmm_init(void) { int pmm_init(void) {
pr_info("[pmm] Physical memory map:\n"); // We're gonna use the loop to also print the memmap pr_info("[pmm] Physical memory map:\n"); // We're gonna use the loop to also print the memmap
@@ -33,12 +34,13 @@ int pmm_init(void) {
uint64_t* addr = TO_VIRT(entry->base) + offset; uint64_t* addr = TO_VIRT(entry->base) + offset;
*addr = (uint64_t)pmm_head; *addr = (uint64_t)pmm_head;
pmm_head = addr; pmm_head = addr;
pmm_total_mem += PAGE_SIZE; pmm_total_pages++;
} }
} }
pr_info("[pmm] %d bytes of available memory.\n", pmm_total_mem); pr_info("[pmm] %d free pages.\n", pmm_total_pages);
if (pmm_total_mem == 0) return -ENOMEM; if (pmm_total_pages == 0) return -ENOMEM;
pmm_free_pages = pmm_total_pages;
return SUCCESS; return SUCCESS;
} }
@@ -49,6 +51,7 @@ void* pmm_alloc() {
uint64_t* addr = pmm_head; uint64_t* addr = pmm_head;
if ((uintptr_t)addr % PAGE_SIZE != 0) panic("pmm_head corrupted."); if ((uintptr_t)addr % PAGE_SIZE != 0) panic("pmm_head corrupted.");
pmm_head = (uint64_t*)(*addr); pmm_head = (uint64_t*)(*addr);
pmm_free_pages--;
return TO_PHYS(addr); return TO_PHYS(addr);
} }
@@ -60,6 +63,7 @@ int pmm_free(void* page) {
uint64_t* addr = TO_VIRT(page); uint64_t* addr = TO_VIRT(page);
*addr = (uint64_t)pmm_head; *addr = (uint64_t)pmm_head;
pmm_head = addr; pmm_head = addr;
pmm_free_pages++;
return SUCCESS; return SUCCESS;
} }