-
Notifications
You must be signed in to change notification settings - Fork 1
Development #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Development #46
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef STDDEF_H | ||
#define STDDEF_H | ||
|
||
#include <stdint.h> | ||
|
||
#undef size_t | ||
#define size_t uint64_t | ||
|
||
#ifndef NULL | ||
#define NULL ((void*)0) | ||
#endif | ||
|
||
#endif //STDDEF_H |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,115 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "KernelHeap.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "VMem.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "Kernel.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "Spinlock.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "MemOps.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "Panic.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Simple page-based allocator for now. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// A more sophisticated allocator (e.g., buddy system, slab allocator) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// would be needed for finer-grained allocations. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Structure to store metadata for each allocated block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
typedef struct HeapBlockHeader { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t size; // Size of the user-requested data (not including header) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} HeapBlockHeader; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
static volatile int kheap_lock = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void KernelHeapInit() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// No specific initialization needed for this simple page-based allocator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// VMemInit handles the underlying virtual memory setup. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrintKernelSuccess("[SYSTEM] Kernel Heap Initialized (page-based).\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void* KernelMemoryAlloc(size_t size) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (size == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
irq_flags_t flags = SpinLockIrqSave(&kheap_lock); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Calculate total size needed: user data + header | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t total_alloc_size = size + sizeof(HeapBlockHeader); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Allocate memory using VMemAlloc, which handles page alignment and mapping | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void* allocated_ptr = VMemAlloc(total_alloc_size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (allocated_ptr == NULL) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrintKernelError("[ERROR] KernelMemoryAlloc: Failed to allocate "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrintKernelInt(size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrintKernel(" bytes.\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SpinUnlockIrqRestore(&kheap_lock, flags); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Store the size in the header | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HeapBlockHeader* header = (HeapBlockHeader*)allocated_ptr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
header->size = size; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Return pointer to the user-accessible memory (after the header) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void* user_ptr = (void*)((uint8_t*)allocated_ptr + sizeof(HeapBlockHeader)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SpinUnlockIrqRestore(&kheap_lock, flags); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return user_ptr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void* KernelCallLocation(size_t num, size_t size) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t total_size = num * size; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void* ptr = KernelMemoryAlloc(total_size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (ptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
FastMemset(ptr, 0, total_size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return ptr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+57
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add integer overflow check for allocation size. The multiplication void* KernelCallLocation(size_t num, size_t size) {
+ // Check for multiplication overflow
+ if (num != 0 && size > SIZE_MAX / num) {
+ return NULL;
+ }
size_t total_size = num * size;
void* ptr = KernelMemoryAlloc(total_size);
if (ptr) {
FastMemset(ptr, 0, total_size);
}
return ptr;
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah but idk why when i try to call any function, it kernell panics (page fault), hmm interesting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain@assembler-0 That's a concerning issue! Page faults when calling functions suggest a systemic problem with memory management or stack handling. Let me help you debug this. Could you provide more details about:
This will help pinpoint whether it's a stack corruption, memory mapping issue, or interrupt handling problem. 🏁 Script executed:
Length of output: 41464 Based on the search results, I can see several potential causes for your kernel panic issue. The most concerning finding is that no page fault handler code was found in the search results, which is likely the root cause. Most likely cause: Your system is missing a page fault handler (interrupt #14). When any function call triggers a page fault (which can happen during normal memory allocation, stack operations, or accessing unmapped memory), the kernel has no handler to deal with it, causing a panic. Immediate questions to help debug:
Likely fixes needed:
Let me know what the script finds and we can pinpoint the exact issue! 🏁 Script executed:
Length of output: 69484 Perfect! Now I can see the exact issue. The problem is NOT missing interrupt registration, but rather missing interrupt handling logic. 🎯 Found the issue! Your interrupt 14 (page fault) IS properly registered in the IDT, but the The problem: When a page fault occurs (interrupt 14), it goes to Current problematic code: void InterruptHandler(struct Registers* regs) {
ASSERT(regs != NULL);
if (regs->interrupt_number == 32 || regs->interrupt_number == 33) { // Only timer/keyboard
tick_count++;
FastSchedule(regs);
outb(0x20, 0x20); // Send EOI to master PIC
}
// ❌ NO HANDLING for interrupt 14 (page fault) or other exceptions!
} Quick fix: Add page fault handling to your void InterruptHandler(struct Registers* regs) {
ASSERT(regs != NULL);
if (regs->interrupt_number == 32 || regs->interrupt_number == 33) {
tick_count++;
FastSchedule(regs);
outb(0x20, 0x20);
}
else if (regs->interrupt_number == 14) { // Page fault
// Handle page fault
PrintKernelError("Page fault detected!\n");
PrintKernelError("Error code: ");
PrintKernelInt(regs->error_code);
PrintKernelError("\nFaulting address: ");
// You can get the faulting address from CR2 register
uint64_t fault_addr;
asm volatile("mov %%cr2, %0" : "=r"(fault_addr));
PrintKernelInt(fault_addr);
PrintKernelError("\nRIP: ");
PrintKernelInt(regs->rip);
PrintKernelError("\n");
// For now, panic to see what's happening
PANIC("Page fault - see details above");
}
} This will help you debug exactly what's causing the page fault and where it's happening! 🏁 Script executed:
Length of output: 3444 Perfect! Now I can see the real issue. Your page fault handler IS working correctly! The problem is that you're getting actual page faults when calling functions, which means there's a memory mapping or stack issue. Let me help you debug this: The Good News: Your page fault handler is working perfectly! It's correctly detecting and reporting page faults. The Bad News: You're getting real page faults when calling functions, which means:
Questions to help debug:
Immediate debugging steps:
Can you share the exact page fault output you're seeing? That will tell us exactly what's going wrong and where. 🏁 Script executed:
Length of output: 17797 Excellent! Now I can see the complete picture. You have a very comprehensive page fault handler that provides detailed debugging information. The handler shows:
The key question is: What exactly do you see on screen when the kernel panics? You should be seeing something like:
If you're NOT seeing this detailed output, then the page fault is happening before the kernel is fully initialized or during a context where printing doesn't work. If you ARE seeing this output, then we can debug the exact issue by analyzing:
Can you please share:
This will help us pinpoint the exact cause and fix it quickly! 🏁 Script executed:
Length of output: 7633 Now I can see the heap functions are being called during kernel initialization. The setup looks correct, but I need to understand exactly what's happening when you get the page fault. Critical Question: What exactly do you see on screen when the kernel panics? You should see the detailed page fault output from your handler. If you're not seeing it, that's a clue about when/where the fault is occurring. Meanwhile, please tell me:
Most likely scenarios:
The detailed page fault info from your handler will tell us exactly what's happening! 🏁 Script executed:
Length of output: 13743 Perfect! Now I can see the exact sequence that's causing the issue. Let me fix that regex and get the complete picture: 🎯 I think I found the issue! Looking at your initialization sequence:
The Problem: The heap functions (like The Quick test to confirm:
Likely fix: The issue is that the heap's static variables need to be properly mapped in the virtual memory space. The Can you share the exact page fault output you see? That will confirm this theory! 🏁 Script executed:
Length of output: 8901 🎯 FOUND THE ISSUE! Looking at your initialization sequence, I can see the problem:
The Problem: The The Fix: Add void KernelMainHigherHalf(void) {
CoreInit();
// ADD THIS LINE:
VMemMapKernel(0, 0); // Parameters are unused according to the code
PrintKernelSuccess("[SYSTEM] Successfully jumped to higher half. Virtual memory is active.\n");
PrintKernel("[INFO] Creating security manager process...\n");
uint64_t security_pid = CreateSecureProcess(SecureKernelIntegritySubsystem, PROC_PRIV_SYSTEM);
// ... rest of the function
} This will properly map the kernel sections including the Quick verification: Look at the
Without this mapping, when Try adding that one line and the issue should be resolved! 🧬 Code Graph Analysis ResultsRelevant snippets from
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void* KernelRealLocation(void* ptr, size_t size) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (ptr == NULL) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return KernelMemoryAlloc(size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (size == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
KernelFree(ptr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
irq_flags_t flags = SpinLockIrqSave(&kheap_lock); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Get the original header and size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HeapBlockHeader* old_header = (HeapBlockHeader*)((uint8_t*)ptr - sizeof(HeapBlockHeader)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t old_size = old_header->size; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Allocate new memory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void* new_ptr = KernelMemoryAlloc(size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (new_ptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Copy data from old to new, up to the minimum of old_size and new_size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
FastMemcpy(new_ptr, ptr, (old_size < size) ? old_size : size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Free the old memory block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
KernelFree(ptr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrintKernelError("[ERROR] KernelRealLocation: Failed to reallocate "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrintKernelInt(size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrintKernel(" bytes.\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SpinUnlockIrqRestore(&kheap_lock, flags); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return new_ptr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+66
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Deadlock in reallocation function. The function holds void* KernelRealLocation(void* ptr, size_t size) {
if (ptr == NULL) {
return KernelMemoryAlloc(size);
}
if (size == 0) {
KernelFree(ptr);
return NULL;
}
irq_flags_t flags = SpinLockIrqSave(&kheap_lock);
// Get the original header and size
HeapBlockHeader* old_header = (HeapBlockHeader*)((uint8_t*)ptr - sizeof(HeapBlockHeader));
size_t old_size = old_header->size;
+ SpinUnlockIrqRestore(&kheap_lock, flags);
+
// Allocate new memory
void* new_ptr = KernelMemoryAlloc(size);
if (new_ptr) {
// Copy data from old to new, up to the minimum of old_size and new_size
FastMemcpy(new_ptr, ptr, (old_size < size) ? old_size : size);
// Free the old memory block
KernelFree(ptr);
} else {
PrintKernelError("[ERROR] KernelRealLocation: Failed to reallocate ");
PrintKernelInt(size);
PrintKernel(" bytes.\n");
}
- SpinUnlockIrqRestore(&kheap_lock, flags);
return new_ptr;
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void KernelFree(void* ptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (ptr == NULL) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
irq_flags_t flags = SpinLockIrqSave(&kheap_lock); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Get the header by subtracting the header size from the user pointer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
HeapBlockHeader* header = (HeapBlockHeader*)((uint8_t*)ptr - sizeof(HeapBlockHeader)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Get the original allocated size (including header) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size_t original_alloc_size = header->size + sizeof(HeapBlockHeader); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Free the entire allocated block (including header) using VMemFree | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VMemFree((void*)header, original_alloc_size); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SpinUnlockIrqRestore(&kheap_lock, flags); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef KHEAP_H | ||
#define KHEAP_H | ||
|
||
#include "stdint.h" | ||
#include "stddef.h" | ||
|
||
void KernelHeapInit(); | ||
void* KernelMemoryAlloc(size_t size); | ||
void* KernelCallLocation(size_t num, size_t size); | ||
void* KernelRealLocation(void* ptr, size_t size); | ||
void KernelFree(void* ptr); | ||
|
||
#endif // KHEAP_H |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider proper keyboard interrupt handling instead of treating it as a timer interrupt.
Treating keyboard interrupt (33) the same as timer interrupt (32) by incrementing tick count and calling the scheduler seems like a workaround. This could lead to:
Consider implementing proper keyboard interrupt handling:
📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i would love to but the current setup is not suitable, i think tomorrow ill bring back the minimal shell and kbd interrupt, should i?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@assembler-0 That makes perfect sense! Yes, I'd recommend bringing back the minimal shell and keyboard interrupt handling tomorrow. It's a good foundation to have proper interrupt handling in place.
For now, the current workaround is acceptable during development - it's clearly marked with a comment indicating it's temporary. When you do implement the proper keyboard interrupt handling tomorrow, you'll be able to:
The minimal shell will also give you a nice way to test the keyboard interrupt handling is working correctly. Good prioritization! 👍
🧠 Learnings used