mirror of
https://github.com/Tom-on64/kern.git
synced 2026-11-03 18:22:22 +00:00
125 lines
3.3 KiB
C
125 lines
3.3 KiB
C
#ifndef SYSCALL_H
|
|
#define SYSCALL_H
|
|
|
|
#include <interrupt/idt.h>
|
|
#include <interrupt/pit.h>
|
|
#include <screen/text.h>
|
|
#include <keyboard/keyboard.h>
|
|
#include <memory/malloc.h>
|
|
|
|
// System call count
|
|
#define MAX_SYSCALLS 5
|
|
|
|
// Args: ebx - number of miliseconds
|
|
void sys_sleep() {
|
|
uint32_t ms;
|
|
__asm__ volatile ("movl %%ebx, %0" : "=r"(ms));
|
|
|
|
uint32_t requiredTicks = *timerTicks + ms;
|
|
while (*timerTicks != requiredTicks) { // Wait...
|
|
__asm__ volatile ("sti;hlt;cli;");
|
|
}
|
|
}
|
|
|
|
// TODO: Use a Write() syscall and write to stdout
|
|
// Args: ebx - char* string
|
|
void sys_puts() {
|
|
char* s;
|
|
__asm__ volatile ("movl %%ebx, %0" : "=r"(s));
|
|
print(s);
|
|
}
|
|
|
|
// TODO: Use a Read() sycall and read from stdin
|
|
// Args: ebx - char* buffer
|
|
void sys_gets() {
|
|
char* s;
|
|
__asm__ volatile ("movl %%ebx, %0" : "=r"(s));
|
|
read(s);
|
|
}
|
|
|
|
// Args: ebx - amount of bytes to malloc
|
|
void sys_malloc() {
|
|
uint32_t size = 0;
|
|
__asm__ volatile ("movl %%ebx, %0" : "=b"(size));
|
|
|
|
// First Malloc()
|
|
if (!mallocListHead) {
|
|
mallocInit(size); // Setup malloc linked list
|
|
}
|
|
|
|
void* ptr = mallocNextNode(size);
|
|
mallocMergeFree();
|
|
|
|
// Return allocated address in eax
|
|
__asm__ volatile ("movl %0, %%eax" : : "r"(ptr));
|
|
}
|
|
|
|
// Args: ebx - allocated pointer
|
|
void sys_free() {
|
|
void* ptr = NULL;
|
|
__asm__ volatile ("movl %%ebx, %0" : "=b"(ptr));
|
|
mallocFree(ptr);
|
|
}
|
|
|
|
// System call table
|
|
void* syscalls[MAX_SYSCALLS] = {
|
|
sys_sleep, // Syscall(0)
|
|
sys_puts, // Syscall(1)
|
|
sys_gets, // Syscall(2)
|
|
sys_malloc, // Syscall(3)
|
|
sys_free, // Syscall(4)
|
|
};
|
|
|
|
// int 0x80 - Syscall interrupt, handled by this function
|
|
// eax : Syscall number
|
|
__attribute__ ((naked)) // No prologue or epilogue, just assembly
|
|
void syscallHandler(intFrame_t* iframe) {
|
|
// Interrupt - Stack should contain: ss, esp, eflags, cs, eip
|
|
// We should also push these regs: ax, gs, fs, es, ds, bp, di, si, dx, cx, bx
|
|
__asm__ volatile (
|
|
".intel_syntax noprefix\n" // We use intel syntax
|
|
".equ MAX_SYSCALLS, 5\n" // Define MAX_SYSCALLS again TODO: I don't want to have to do this
|
|
|
|
// Check if syscall exists
|
|
"cmp eax, MAX_SYSCALLS - 1\n"
|
|
"ja invalidSyscall\n"
|
|
|
|
// Call the syscall
|
|
"push eax\n" // Push required values
|
|
"push gs\n"
|
|
"push fs\n"
|
|
"push es\n"
|
|
"push ds\n"
|
|
"push ebp\n"
|
|
"push edi\n"
|
|
"push esi\n"
|
|
"push edx\n"
|
|
"push ecx\n"
|
|
"push ebx\n"
|
|
"push esp\n" // Push stack pointer again (for variables and such idk)
|
|
"call [syscalls + eax * 4]\n" // Call syscall from table. Syscall number should be in eax
|
|
"add esp, 4\n"
|
|
"pop ebx\n" // Pop pushed values
|
|
"pop ecx\n"
|
|
"pop edx\n"
|
|
"pop esi\n"
|
|
"pop edi\n"
|
|
"pop ebp\n"
|
|
"pop ds\n"
|
|
"pop es\n"
|
|
"pop fs\n"
|
|
"pop gs\n"
|
|
"add esp, 4\n" // Save eax value
|
|
"iretd\n" // Return from interrupt (32 bit)
|
|
|
|
// TODO: Send an error or something here
|
|
"invalidSyscall:\n"
|
|
"mov eax, -1\n" // Current error thing
|
|
"iretd\n"
|
|
|
|
".att_syntax" // Switch back to Att Syntax (Just in case)
|
|
);
|
|
}
|
|
|
|
#endif
|