mirror of
https://github.com/gbowne1/ClassicOS.git
synced 2024-11-21 22:06:51 -08:00
improving io.asm and isr.asm
This commit is contained in:
parent
e6ad2dbc20
commit
19b61da1af
BIN
.vscode/browse.vc.db
vendored
BIN
.vscode/browse.vc.db
vendored
Binary file not shown.
BIN
.vscode/browse.vc.db-shm
vendored
BIN
.vscode/browse.vc.db-shm
vendored
Binary file not shown.
@ -1,14 +1,24 @@
|
||||
section .text
|
||||
global inb
|
||||
global outb
|
||||
|
||||
section .text
|
||||
; Read a byte from the specified port
|
||||
; Input: DX = port number
|
||||
; Output: AL = data read from the port
|
||||
inb:
|
||||
MOV DX, WORD [ESP + 16]
|
||||
IN AL, DX
|
||||
PUSH DX ; Preserve DX
|
||||
IN AL, DX ; Read from port
|
||||
POP DX ; Restore DX
|
||||
RET
|
||||
|
||||
; Write a byte to the specified port
|
||||
; Input: DX = port number, AL = data to write
|
||||
outb:
|
||||
MOV DX, WORD [ESP + 16]
|
||||
MOV AL, BYTE [ESP + 24]
|
||||
OUT DX, AL
|
||||
PUSH DX ; Preserve DX
|
||||
PUSH AX ; Preserve AX
|
||||
MOV DX, [ESP + 4] ; Get port number from stack
|
||||
MOV AL, [ESP + 6] ; Get data from stack
|
||||
OUT DX, AL ; Write to port
|
||||
POP AX ; Restore AX
|
||||
POP DX ; Restore DX
|
||||
RET
|
@ -4,8 +4,10 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void init_heap(void *start, size_t size);
|
||||
void init_heap(void *start, size_t size);
|
||||
void *kmalloc(size_t size);
|
||||
void kfree(void *ptr);
|
||||
void kfree(void *ptr);
|
||||
void *malloc(size_t size); // Adding malloc and free declarations
|
||||
void free(void *ptr);
|
||||
|
||||
#endif /* MEMORY_H */
|
@ -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
|
@ -16,44 +16,65 @@ 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;
|
||||
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;
|
||||
/* 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;
|
||||
}
|
||||
/* 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;
|
||||
/* Allocate memory from the heap */
|
||||
void *ptr = heap_pos;
|
||||
heap_pos += size;
|
||||
|
||||
return ptr;
|
||||
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;
|
||||
}
|
||||
/* 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;
|
||||
}
|
||||
/* Free the memory by moving the heap position */
|
||||
heap_pos = (uint8_t *)ptr;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user