Update how syscalls accept arguments in registers, change how syscalls are defined in common.c and common.h

This commit is contained in:
Xnoe 2022-04-08 18:00:56 +01:00
parent 7e5f20ef66
commit bb69d6b713
Signed by: xnoe
GPG Key ID: 45AC398F44F0DAFE
4 changed files with 66 additions and 98 deletions

View File

@ -1,5 +1,24 @@
#include "common.h"
#define syscall_hdlr_0(a, b, c) \
a b() { \
asm volatile("mov $" c ", %%eax; int $0x80" : : :); \
}
#define syscall_hdlr_1(a, b, c, d, e) \
a b(d e) { \
asm volatile("mov $" c ", %%eax; mov %0, %%ebx; int $0x80" : : "m" (e) : "ebx"); \
}
#define syscall_hdlr_2(a, b, c, d, e, f, g) \
a b(d e, f g) { \
asm volatile("mov $" c ", %%eax; mov %0, %%ebx; mov %1, %%ecx; int $0x80" : : "m" (e), "m" (g) : "ebx", "ecx"); \
}
#define syscall_hdlr_3(a, b, c, d, e, f, g, h, i) \
a b(d e, f g, h i) { \
asm volatile("mov $" c ", %%eax; mov %0, %%ebx; mov %1, %%ecx; mov %2, %%edx; int $0x80" : : "m" (e), "m" (g), "m" (i) : "ebx", "ecx", "edx"); \
}
#include "syscalls.h"
void print(char* string) {
char* c = string;
int i=0;
@ -8,58 +27,6 @@ void print(char* string) {
write(i, 0, (uint8_t*)string);
}
void* localalloc(uint32_t size) {
asm volatile ("mov $4, %%eax; mov %0, %%esi; int $0x7f" : : "m" (size) : "esi");
}
void localdelete(void* ptr) {
asm volatile ("mov $5, %%eax; mov %0, %%esi; int $0x7f" : : "m" (ptr) : "esi");
}
uint32_t getPID() {
asm volatile ("mov $8, %%eax; int $0x7f" : : :);
}
int read(uint32_t count, void* filehanlder, uint8_t* buffer) {
asm volatile ("mov $10, %%eax; mov %0, %%ebx; mov %1, %%esi; mov %2, %%edi; int $0x7f" : : "m" (count), "m" (filehanlder), "m" (buffer): "ebx", "esi", "edi");
}
int write(uint32_t count, void* filehanlder, uint8_t* buffer) {
asm volatile ("mov $11, %%eax; mov %0, %%ebx; mov %1, %%esi; mov %2, %%edi; int $0x7f" : : "m" (count), "m" (filehanlder), "m" (buffer): "ebx", "esi", "edi");
}
uint32_t fork(uint32_t fh) {
asm volatile("mov $7, %%eax; mov %0, %%esi; int $0x7f" : : "m" (fh) : "esi");
}
uint32_t bindStdout(uint32_t PID) {
asm volatile("mov $13, %%eax; mov %0, %%esi; int $0x7f" : : "m" (PID) : "esi");
}
uint32_t bindStdin(uint32_t PID) {
asm volatile("mov $14, %%eax; mov %0, %%esi; int $0x7f" : : "m" (PID) : "esi");
}
int fopen(char* filename) {
asm volatile("mov $15, %%eax; mov %0, %%esi; int $0x7f" : : "m" (filename) : "esi");
}
void fclose(uint32_t fh) {
asm volatile("mov $16, %%eax; mov %0, %%esi; int $0x7f" : : "m" (fh) : "esi");
}
void kill(uint32_t pid) {
asm volatile("mov $17, %%eax; mov %0, %%esi; int $0x7f" : : "m" (pid) : "esi");
}
void sleep(uint32_t time) {
asm volatile("mov $18, %%eax; mov %0, %%esi; int $0x7f" : : "m" (time) : "esi");
}
void bindToKeyboard() {
asm volatile ("mov $12, %%eax; int $0x7f" : : :);
}
int int_to_decimal(unsigned int number, char* string_buffer) {
for (int i=0; i<11; i++)
string_buffer[i] = 0;

View File

@ -3,27 +3,18 @@
#include "../kernel/types.h"
#define syscall_hdlr_0(a, b, c) \
a b();
#define syscall_hdlr_1(a, b, c, d, e) \
a b(d e);
#define syscall_hdlr_2(a, b, c, d, e, f, g) \
a b(d e, f g);
#define syscall_hdlr_3(a, b, c, d, e, f, g, h, i) \
a b(d e, f g, h i);
#include "syscalls.h"
void print(char* string);
void readfile(char* filename, uint8_t* buffer);
void* localalloc(uint32_t size);
void localdelete(void* ptr);
uint32_t filesize(char* filename);
uint32_t fork(uint32_t fh);
uint32_t bindStdout(uint32_t PID);
uint32_t bindStdin(uint32_t PID);
uint32_t getPID();
int read(uint32_t count, void* filehandler, uint8_t* buffer);
int write(uint32_t count, void* filehandler, uint8_t* buffer);
void bindToKeyboard();
int fopen(char* filename);
void fclose(uint32_t fh);
void kill(uint32_t pid);
void sleep(uint32_t time);
int int_to_decimal(unsigned int number, char* string_buffer);
int int_to_hex(unsigned int number, char* string_buffer);

13
src/common/syscalls.h Normal file
View File

@ -0,0 +1,13 @@
syscall_hdlr_1(void*, localalloc, "4", uint32_t, size);
syscall_hdlr_1(void, localdelete, "5", void*, ptr);
syscall_hdlr_0(uint32_t, getPID, "8");
syscall_hdlr_3(int, read, "10", uint32_t, count, void*, filehandler, uint8_t*, buffer);
syscall_hdlr_3(int, write, "11", uint32_t, count, void*, filehandler, uint8_t*, buffer);
syscall_hdlr_1(uint32_t, fork, "7", uint32_t, filehandler);
syscall_hdlr_1(uint32_t, bindStdout, "13", uint32_t, PID);
syscall_hdlr_1(uint32_t, bindStdin, "14", uint32_t, PID);
syscall_hdlr_1(uint32_t, fopen, "15", char*, filename);
syscall_hdlr_1(void, fclose, "16", uint32_t, filehandler);
syscall_hdlr_1(void, kill, "17", uint32_t, PID);
syscall_hdlr_1(void, sleep, "18", uint32_t, time);
syscall_hdlr_0(void, bindToKeyboard, "12");

View File

@ -178,7 +178,7 @@ void syscall(frame_struct* frame) {
// 4: localalloc: LocalAlloc: Allocate under current process (in esi: size; out eax void* ptr)
// 5: localdelete: LocalDelete: Deallocate under current process (in esi: pointer)
// 6: X
// 7: fork :: char* filename esi -> int PID // Spawns a process and returns its PID.
// 7: fork :: void* filehandler esi -> int PID // Spawns a process and returns its PID.
// 8: getPID: returns the current process's PID (out eax: uint32_t)
// 9: getFileHandler :: char* path esi -> void* eax // Returns a file handlers for a specific file
// 10: read :: uint32_t count ebx -> void* filehandler esi -> uint8_t* outputbuffer edi -> int read // Reads from a file handler in to a buffer, returns successful read
@ -203,9 +203,6 @@ void syscall(frame_struct* frame) {
uint32_t rval = frame->eax;
uint32_t esi = frame->esi;
uint32_t edi = frame->edi;
Process* currentProc = Global::currentProc;
switch (frame->eax) {
@ -218,16 +215,16 @@ void syscall(frame_struct* frame) {
case 3:
break;
case 4:
rval = currentProc->allocate(esi);
rval = currentProc->allocate(frame->ebx);
break;
case 5:
currentProc->deallocate(esi);
currentProc->deallocate(frame->ebx);
break;
case 6:
break;
case 7: {
asm("cli");
Process* p = Global::kernel->createProcess(esi);
Process* p = Global::kernel->createProcess(frame->ebx);
rval = p->PID;
asm("sti");
break;
@ -240,41 +237,41 @@ void syscall(frame_struct* frame) {
break;
case 10: {
if (esi == 1) {
if (frame->ecx == 1) {
ReadWriter* stdin = currentProc->stdin;
if (!stdin)
break;
rval = stdin->read(frame->ebx, edi);
rval = stdin->read(frame->ebx, frame->edx);
} else {
xnoe::Maybe<ReadWriter*> fh = Global::FH->get(esi);
xnoe::Maybe<ReadWriter*> fh = Global::FH->get(frame->ecx);
if (!fh.is_ok()) {
rval = 0;
break;
}
ReadWriter* rw = fh.get();
rval = rw->read(frame->ebx, edi);
rval = rw->read(frame->ebx, frame->edx);
}
break;
}
case 11: {
if (esi == 0) {
if (frame->ecx == 0) {
ReadWriter* stdout = currentProc->stdout;
if (!stdout)
break;
rval = stdout->write(frame->ebx, edi);
rval = stdout->write(frame->ebx, frame->edx);
} else {
xnoe::Maybe<ReadWriter*> fh = Global::FH->get(esi);
xnoe::Maybe<ReadWriter*> fh = Global::FH->get(frame->ecx);
if (!fh.is_ok()) {
rval = 0;
break;
}
ReadWriter* rw = fh.get();
rval = rw->write(frame->ebx, edi);
rval = rw->write(frame->ebx, frame->edx);
}
break;
}
@ -288,12 +285,12 @@ void syscall(frame_struct* frame) {
break;
case 13: {
xnoe::Maybe<Process*> pm = Global::kernel->pid_map->get(esi);
xnoe::Maybe<Process*> pm = Global::kernel->pid_map->get(frame->ebx);
if (!pm.is_ok())
break;
Process* p = pm.get();
if (!p->stdout) {
ReadWriter* buffer = new CircularRWBuffer(currentProc->PID, esi);
ReadWriter* buffer = new CircularRWBuffer(currentProc->PID, frame->ebx);
p->stdout = buffer;
rval = Global::kernel->mapFH(buffer);
}
@ -301,12 +298,12 @@ void syscall(frame_struct* frame) {
}
case 14: {
xnoe::Maybe<Process*> pm = Global::kernel->pid_map->get(esi);
xnoe::Maybe<Process*> pm = Global::kernel->pid_map->get(frame->ebx);
if (!pm.is_ok())
break;
Process* p = pm.get();
if (!p->stdin) {
ReadWriter* buffer = new CircularRWBuffer(esi, currentProc->PID);
ReadWriter* buffer = new CircularRWBuffer(frame->ebx, currentProc->PID);
p->stdin = buffer;
rval = Global::kernel->mapFH(buffer);
}
@ -314,24 +311,24 @@ void syscall(frame_struct* frame) {
}
case 15: {
ReadWriter* file = Global::kernel->rootfs->open(createPathFromString(esi));
ReadWriter* file = Global::kernel->rootfs->open(createPathFromString(frame->ebx));
if (file)
rval = Global::kernel->mapFH(file);
break;
}
case 16: {
xnoe::Maybe<ReadWriter*> f = Global::FH->get(esi);
xnoe::Maybe<ReadWriter*> f = Global::FH->get(frame->ebx);
if (f.is_ok()) {
delete f.get();
Global::kernel->unmapFH(esi);
Global::kernel->unmapFH(frame->ebx);
}
break;
}
case 17: {
asm("cli");
xnoe::Maybe<Process*> p = Global::kernel->pid_map->get(esi);
xnoe::Maybe<Process*> p = Global::kernel->pid_map->get(frame->ebx);
if (p.is_ok()) {
Process* proc = p.get();
Global::kernel->destroyProcess(proc);
@ -342,7 +339,7 @@ void syscall(frame_struct* frame) {
case 18: {
Global::currentProc->state = Suspended;
Timer::register_event(esi, &awaken, (void*)Global::currentProc, true);
Timer::register_event(frame->ebx, &awaken, (void*)Global::currentProc, true);
context_switch(frame);
break;
}
@ -384,9 +381,9 @@ void init_idt() {
gates[29] = &handle_fault;
gates[30] = &handle_fault;
gates[31] = &handle_fault;
gates[127] = &syscall;
gates[128] = &syscall;
idt[127].privilege = 3;
idt[128].privilege = 3;
outb(0x20, 0x11);
outb(0xA0, 0x11);