From 19b61da1af730117a3d5f3cd2db32ff07b9c7f58 Mon Sep 17 00:00:00 2001 From: Greg Bowne Date: Fri, 9 Feb 2024 14:51:51 -0800 Subject: [PATCH] improving io.asm and isr.asm --- .vscode/browse.vc.db | Bin 21970944 -> 21970944 bytes .vscode/browse.vc.db-shm | Bin 32768 -> 32768 bytes src/drivers/io/io.asm | 22 +++-- src/kernel/arch/x86/include/memory.h | 24 ++--- src/kernel/arch/x86/isr/isr.asm | 45 ++++++++- src/kernel/arch/x86/memory/memory.c | 139 +++++++++++++++------------ 6 files changed, 151 insertions(+), 79 deletions(-) diff --git a/.vscode/browse.vc.db b/.vscode/browse.vc.db index 853f8d71214d8b84ea02546d6ae37335fd3b6c3e..4a5b325d627c96c622b78d10864a41bf3ad25220 100644 GIT binary patch delta 8236 zcmai(3tSZS_Q2=NV|QnFf!*b`%f1Fwe1H$+q2+@Wgb)R_t5G+(I!fv7#i>KdBb|NQm=*r)%!%jbK}ne#ipGv_z^ z<2Uo0b-VX!-N??^^l=g4OJkT`kyQmVi>FmrOsl9YF0Y<9zc_lSndxgOtSBm;R$5&= zw`y8pd3Dm@LVPQgZUpA%owXn;s|h-e_cc(cSY=o5GpCbhljCfqLQ1 zx2F|5!SS4}ra#!9%dHs*w&xzLN#Lw(UUazF6bC|R=D^H^gu=?o#27X&GV-~mH-$Gl zYO0)EOiv~co)c;W?7mLw$=Q9!Pt*vUjaa_UV12S~c^xdTqnFn)%j?*;>o^eDh%f2) zWo|!L#c50(;YAVph~cvPLvB%sieqTr*(ToplAGzbFN$TyxGjGBl4v^4_4C`8<&5Lp z2d_fC$hlQxG>!(xkIqHTWHH~Z`bExNs+ww7Q~bb^in+yN-9}YH%*@g`#Z`-|@FEVJ zSx}vn=9dEsotOV>dx`VXpS33sCjFBK4=8b7{IBhW&NlB}NrkB8y^^Nm5_1e4=I-?rXmFwuAV=ST(=hQ;`k_eaeTBPRQT4w z^q}v!sHFG&@`a`4MHt%TZAghm%yL7DyCu_(iGLB2QB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bMt}FM~nd8s8#g< literal 32768 zcmeI)$xake7>3~w8dRV`Wo#9-6&V~r5k+yrp&1lRT#d(L%ISZ2E}$|$`{ho$O0vJKdwyRpwWjh* z9czov-_3n^^|k%S%ijy%JI5Q}_GSX!cl8SA&p)T|uJFIw9#WRe?u8BV%d%oGbM|t^ z(o`#bvMgI(ACNv;zF5+A(r3%6W!*Anvrk&4Ezc}7mb_)w^2SoA@9wzgjqP3o)@#@@ zZ*l$dmKQbBH;e0?E$Z)jw;bH+zf#nHx4y2|Zhc%2w^wmpw^Q*BHsW6^N5w$^0R#|0 z009ILKmY**5I_I{1Q0*~0R#|0009ILKmY**5I_I{1Q0*~0R#|0009ILKmY**5I_I{ z1Q0*~0R#|0009ILKmY**5I_KdodVq&-T87)ZyI!QPrv8KQ4(m?$taDJ$XOaAu(f?U zq~vo(w@bbo0u>}st)~^V4Xcz#;FxaciTBo5H154-TnnajLZ>tm*YTMf z0TXCL0f90K9Ja3=j+L>0Ww$=9Ta~>a+eK91d_?DBECdkvUjh3NFsFCt`fH7STd5(SQGP)+LBqa-dL>dy7L5NdsjBY*$` z2!t -#include - -void init_heap(void *start, size_t size); -void *kmalloc(size_t size); -void kfree(void *ptr); - -#endif /* MEMORY_H */ \ No newline at end of file +#ifndef MEMORY_H +#define MEMORY_H + +#include +#include + +void init_heap(void *start, size_t size); +void *kmalloc(size_t size); +void kfree(void *ptr); +void *malloc(size_t size); // Adding malloc and free declarations +void free(void *ptr); + +#endif /* MEMORY_H */ diff --git a/src/kernel/arch/x86/isr/isr.asm b/src/kernel/arch/x86/isr/isr.asm index bdfeacc..142d6ef 100644 --- a/src/kernel/arch/x86/isr/isr.asm +++ b/src/kernel/arch/x86/isr/isr.asm @@ -1,11 +1,50 @@ +section .text global isr_handler global _isr_stub _isr_stub: - PUSH DWORD [ESP + 8] - PUSH DWORD [ESP + 8] + ; Save context + PUSHAD ; Preserve all general-purpose registers + ; Disable interrupts to handle nested interrupts + CLI + + ; Align stack to 16-byte boundary for performance (if needed) + ; AND ESP, 0xFFFFFFF0 + + ; Check if an error code is present (typically in EAX) + ; This is just a placeholder, actual implementation depends on your IDT setup + CMP BYTE [ISR_HAS_ERROR_CODE], 1 + JNE no_error_code + PUSH DWORD [ESP + 32] ; Push error code + JMP done_pushing_error_code +no_error_code: + PUSH DWORD 0 ; Push a dummy error code +done_pushing_error_code: + + ; Push interrupt number onto the stack + MOV EAX, [ESP + 36] ; Move interrupt number to EAX (accounting for the dummy/error code) + PUSH EAX ; Interrupt number + + ; Call isr_handler CALL isr_handler - ADD ESP, 8 + ; Send EOI to PIC only if it's a hardware interrupt + ; This is just a placeholder, actual implementation depends on your IDT setup + CMP BYTE [ISR_IS_HARDWARE_INTERRUPT], 1 + JNE skip_eoi + MOV AL, 0x20 + OUT 0x20, AL +skip_eoi: + + ; Enable interrupts + STI + + ; Clean up the stack + ADD ESP, 8 ; Remove error code and interrupt number + + ; Restore context + POPAD ; Restore all general-purpose registers + + ; Return from interrupt IRETD \ No newline at end of file diff --git a/src/kernel/arch/x86/memory/memory.c b/src/kernel/arch/x86/memory/memory.c index dbf2af9..7454070 100644 --- a/src/kernel/arch/x86/memory/memory.c +++ b/src/kernel/arch/x86/memory/memory.c @@ -1,59 +1,80 @@ -#include "../include/memory.h" -#include -#include - -/* The start of the kernel heap */ -static uint8_t *heap_start = NULL; - -/* The end of the kernel heap */ -static uint8_t *heap_end = NULL; - -/* The current position of the heap */ -static uint8_t *heap_pos = NULL; - -/* The size of the heap */ -static size_t heap_size = 0; - -void init_heap(void *start, size_t size) -{ - heap_start = (uint8_t *)start; - heap_end = heap_start + size; - heap_pos = heap_start; - heap_size = size; -} - -void *kmalloc(size_t size) -{ - /* Round up the size to a multiple of 4 bytes */ - size = (size + 3) & ~3; - - /* Check if there is enough space in the heap */ - if (heap_pos + size > heap_end) - { - return NULL; - } - - /* Allocate memory from the heap */ - void *ptr = heap_pos; - heap_pos += size; - - return ptr; -} - -void kfree(void *ptr) -{ - /* Check if the pointer is within the heap */ - if ((uint8_t *)ptr >= heap_start && ptr < (void *)heap_end) - { - /* Zero out the memory */ - volatile uint8_t *volatile_ptr = (volatile uint8_t *)ptr; - size_t size = (uint8_t *)heap_pos - (uint8_t *)ptr; - while (size--) - { - *volatile_ptr++ = 0; - } - - /* Free the memory by moving the heap position */ - heap_pos = (uint8_t *)ptr; - } -} \ No newline at end of file +#include "../include/memory.h" +#include +#include + +/* The start of the kernel heap */ +static uint8_t *heap_start = NULL; + +/* The end of the kernel heap */ +static uint8_t *heap_end = NULL; + +/* The current position of the heap */ +static uint8_t *heap_pos = NULL; + +/* The size of the heap */ +static size_t heap_size = 0; + +void init_heap(void *start, size_t size) +{ + heap_start = (uint8_t *)start; + heap_end = heap_start + size; + heap_pos = heap_start; + heap_size = size; +} + +void *kmalloc(size_t size) +{ + /* Round up the size to a multiple of 4 bytes */ + size = (size + 3) & ~3; + + /* Check if there is enough space in the heap */ + if (heap_pos + size > heap_end) + { + return NULL; + } + + /* Allocate memory from the heap */ + void *ptr = heap_pos; + heap_pos += size; + + return ptr; +} + +// Implementing a simple malloc-like function +void *malloc(size_t size) +{ + // Round up the size to a multiple of 4 bytes + size = (size + 3) & ~3; + + // Call kmalloc to allocate memory from the heap + return kmalloc(size); +} + +// Implementing a simple free-like function +void free(void *ptr) +{ + // Check if the pointer is within the heap + if ((uint8_t *)ptr >= heap_start && ptr < (void *)heap_end) + { + // Call kfree to deallocate the memory + kfree(ptr); + } +} + +void kfree(void *ptr) +{ + /* Check if the pointer is within the heap */ + if ((uint8_t *)ptr >= heap_start && ptr < (void *)heap_end) + { + /* Zero out the memory */ + volatile uint8_t *volatile_ptr = (volatile uint8_t *)ptr; + size_t size = (uint8_t *)heap_pos - (uint8_t *)ptr; + while (size--) + { + *volatile_ptr++ = 0; + } + + /* Free the memory by moving the heap position */ + heap_pos = (uint8_t *)ptr; + } +}