Some changes, syscalls use reg structs and have return values

This commit is contained in:
2024-08-25 19:40:10 +02:00
parent da1171983b
commit 105b58a684
8 changed files with 133 additions and 67 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ LDFLAGS = -z notext -melf_i386
SRC = ./src
BIN = ./bin
C_FILES = calc editor
C_FILES = calc edit
FONTS = testfont term16n
.PHONY: os clean run
+112 -41
View File
@@ -6,58 +6,80 @@
#include <keyboard/keyboard.h>
#include <memory/malloc.h>
#include <memory/addresses.h>
#include <memory/physical.h>
#include <fs/fs.h>
#include <fs/impl.h>
#include <error/bigerr.h>
#include <syscall.h>
#include <stdint.h>
#include <fs/fs.h>
#include <stdlib.h>
/*
* XXX WARNING - Update MAX_SYSCALLS in the handler XXX
*/
// Args: ebx - number of miliseconds
void sys_sleep() {
uint32_t* sleepTimerTicks = (uint32_t*)SLEEP_TIMER;
__asm__ volatile ("movl %%ebx, %0" : "=r"(*sleepTimerTicks));
typedef struct {
uint32_t esp;
uint32_t ebx;
uint32_t ecx;
uint32_t edx;
uint32_t esi;
uint32_t edi;
uint32_t ebp;
uint32_t ds;
uint32_t es;
uint32_t fs;
uint32_t gs;
uint32_t eax;
} __attribute__ ((packed)) sys_regs_t;
int sys_reserved(sys_regs_t) {
printf("\e[1N\e[6M Illegal use of Reserved() system call! \e[8M\e[8N\n");
return -1;
}
// Args: ebx - number of miliseconds
int sys_sleep(sys_regs_t regs) {
uint32_t* sleepTimerTicks = (uint32_t*)SLEEP_TIMER;
*sleepTimerTicks = regs.ebx;
while (*sleepTimerTicks > 0) { // Wait...
__asm__ volatile ("sti;hlt;cli;");
}
return 0;
}
// Args: ebx - int fd
// ecx - void* buf
// edx - uint32_t len
// Rets: Number of bytes written
void sys_write() {
int32_t fd = 0;
void* buf = NULL;
uint32_t len = 0;
__asm__ volatile ("nop" : "=b"(fd), "=c"(buf), "=d"(len));
int sys_write(sys_regs_t regs) {
int32_t fd = regs.ebx;
void* buf = (void*)regs.ecx;
uint32_t len = regs.edx;
// TODO: Open fd table?
if (fd == 1) { // "stdout"
int32_t result = terminalWrite(buf, len); // Call a terminal print function
__asm__ volatile ("movl %0, %%eax" : : "r"(result));
} else { // Not implemented
__asm__ volatile ("movl $-1, %eax");
return terminalWrite(buf, len); // Call a terminal print function
} else {
return -1;
}
}
// TODO: Use a Read() sycall and read from stdin
// Args: ebx - char* buffer
void sys_read() {
char* s;
__asm__ volatile ("movl %%ebx, %0" : "=r"(s));
int sys_read(sys_regs_t regs) {
char* s = (char*)regs.ebx;
reads(s);
return 0;
}
// Args: ebx - size_t count
// Rets: Pointer to malloc-ed memory
void sys_malloc() {
uint32_t count = 0;
__asm__ volatile ("movl %%ebx, %0" : "=b"(count));
int sys_malloc(sys_regs_t regs) {
uint32_t count = regs.ebx;
// First Malloc()
if (!mallocListHead) {
@@ -67,41 +89,88 @@ void sys_malloc() {
void* ptr = mallocNextNode(count);
mallocMergeFree();
// Return allocated address in eax
__asm__ volatile ("movl %0, %%eax" : : "r"(ptr));
return (uint32_t)ptr;
}
// Args: ebx - void* ptr
void sys_free() {
void* ptr = NULL;
__asm__ volatile ("movl %%ebx, %0" : "=b"(ptr));
int sys_free(sys_regs_t regs) {
void* ptr = (void*)regs.ebx;
mallocFree(ptr);
return 0;
}
// Args: ebx - path,
// ecx - oflag
// Rets: Opened file descriptor
void sys_open() {
uint32_t fd = 0;
char* path = NULL;
uint32_t oflag = 0;
int sys_open(sys_regs_t regs) {
int fd = 0;
char* path = (char*)regs.ebx;
uint32_t oflag = regs.ecx;
__asm__ volatile ("nop" : "=b"(path), "=c"(oflag));
extern FILE* openFileTable;
extern uint32_t openFiles;
// TODO: better file finding
inode_t* root = (inode_t*)impl_superblock->rootInodePtr;
loadFile(root, (void*)SCRATCH_BLOCK_LOC);
inode_t* inode = getInode(path, (dirEntry_t*)SCRATCH_BLOCK_LOC);
// TODO: check if max open files have'nt been reached
fd = openFiles++;
FILE* fp = openFileTable + fd;
fp->_ptr = 0;
fp->_offset = 0;
fp->_size = inode->sizeBytes;
fp->_flag = oflag;
fp->_file = fd;
/*fp->_ptr = (unsigned char*)nextAvailableVirtualAddress;
// Read file from disk
uint32_t pageCount = BYTES2BLOCKS(inode->sizeBytes);
if (pageCount == 0) { pageCount++; }
for (size_t i = 0; i < pageCount; i++) {
ptEntry_t page = 0;
uint32_t physAddr = (uint32_t)allocPage(&page);
if (!mapPage((void*)physAddr, (void*)nextAvailableVirtualAddress)) {
fd = -1; // TODO errno
break;
}
nextAvailableVirtualAddress += PAGE_SIZE;
}
if (!loadFile(inode, fp->_ptr)) {
fd = -1; // TODO errno
}*/
__asm__ volatile ("movl %0, %%eax" : : "a"(fd));
return fd;
}
// Args: ebx - int fd
// Rets: status
void sys_close() {}
int sys_close(sys_regs_t regs) {
int fd = regs.ebx;
extern FILE* openFileTable;
extern uint32_t openFiles;
FILE* fp = openFileTable + fd;
//free(fp->_ptr);
openFiles--;
return 0;
}
// TODO
void sys_seek() {}
int sys_seek(sys_regs_t regs) { return -1; }
// System call table
void (*syscalls[MAX_SYSCALLS])(void) = {
int (*syscalls[MAX_SYSCALLS])(sys_regs_t) = {
[SYS_RESERVED] = sys_reserved,
[SYS_SLEEP] = sys_sleep,
[SYS_WRITE] = sys_write,
[SYS_READ] = sys_read,
@@ -127,7 +196,7 @@ void syscallHandler(intFrame_t* iframe) {
"ja invalidSyscall\n"
// Call the syscall
"push eax\n" // Push required values
"push eax\n"
"push gs\n"
"push fs\n"
"push es\n"
@@ -139,11 +208,13 @@ void syscallHandler(intFrame_t* iframe) {
"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
"add esp, 4\n" // Do not override ESP
"pop ebx\n"
"pop ecx\n"
"pop edx\n"
"add esp, 4\n" // Do not override EDX, some syscalls use it
"pop esi\n"
"pop edi\n"
"pop ebp\n"
@@ -159,7 +230,7 @@ void syscallHandler(intFrame_t* iframe) {
"mov eax, -1\n" // Current error thing
"iretd\n"
".att_syntax" // Switch back to Att Syntax (Just in case)
".att_syntax" // Switch back to AT&T Syntax (Just in case)
);
}
+1 -1
View File
@@ -1,10 +1,10 @@
#ifndef MEMORY_MALLOC_H
#define MEMORY_MALLOC_H
#include <stdint.h>
#include <memory/virtual.h>
#include <memory/physical.h>
#include <stdbool.h>
#include <stdint.h>
// Singly linked list!
typedef struct mallocNode_s {
BIN
View File
Binary file not shown.
+4 -1
View File
@@ -20,15 +20,18 @@ typedef struct {
} fileInfo_t;
fileInfo_t files[] = { // TODO: Not have to list all of the files, loop through a directory or smth
/* Bootloader */
{ BIN_DIR"stage1.bin" },
{ BIN_DIR"stage2.bin" },
{ BIN_DIR"stage3.bin" },
/* Kernel stuff */
{ BIN_DIR"kernel.bin" },
{ BIN_DIR"term16n.fnt" },
{ BIN_DIR"testfont.fnt" },
/* Userspace files */
{ "./src/fs/test.txt" },
{ BIN_DIR"calc.bin" },
{ BIN_DIR"editor.bin" },
{ BIN_DIR"edit.bin" },
};
superblock_t superblock = { 0 };
View File
View File
+15 -23
View File
@@ -29,8 +29,7 @@
// FS things
char* cwd;
FILE* openFileTable;
uint32_t maxOpenFiles;
FILE* openFileTable = (FILE*)OPEN_FILE_TABLE_LOC;
uint32_t openFiles;
// Function declarations
@@ -94,7 +93,7 @@ void main() {
// Run Interactive Shell Program
// TODO: Make it in another file
while (1) {
printf("%s %s ", cwd, PROMPT);
printf("\e[6M\e[8N%s \e[2M%s \e[8M", cwd, PROMPT);
char input[256];
char* inputPtr = input;
@@ -192,7 +191,6 @@ void main() {
printf(" regs | Prints register values\n");
printf(" sleep | Sleeps for input number of seconds\n");
printf(" soundtest | Plays a tune :)\n");
printf(" test | Performs tests\n");
} else if (strcmp(argv[0], "ls") == 0) {
listFiles();
} else if (strcmp(argv[0], "memmap") == 0) {
@@ -233,20 +231,15 @@ void main() {
rest(120);
disableSpeaker();
} else if (strcmp(argv[0], "test") == 0) {
printf("\x1b[1MError: 'test' is not available.\x1b[8M\n");
} else { // TODO: Use cwd for this
inode_t* root = (inode_t*)impl_superblock->rootInodePtr;
diskRead(root->extent[0].block * 8, root->extent[0].length * 8, (void*)SCRATCH_BLOCK_LOC);
dirEntry_t* dentry = (dirEntry_t*)SCRATCH_BLOCK_LOC;
inode_t* inode = getInode(argv[0], dentry);
if (inode == NULL) {
printf("Command not found: %s\n", argv[0]);
continue;
}
char* fileName = argv[0];
//FILE* fp = fopen(fileName, "rb");
//if (fp == NULL) {
printf("Command not found: %s\n", fileName);
// continue;
//}
printf("%d: %s %dB\n", inode->id, argv[0], inode->sizeBytes);
}
}
@@ -344,23 +337,22 @@ void printPhysicalMemmap() {
}
void initOpenFileTable() {
maxOpenFiles = 256;
openFileTable = malloc(sizeof(FILE) * maxOpenFiles);
memset(openFileTable, 0, sizeof(FILE) * maxOpenFiles);
openFileTable = malloc(sizeof(FILE) * MAX_OPEN_FILES);
if (openFileTable == NULL) { printf("\e[1MFailed to init file table! Malloc() error.\e[8M"); }
memset(openFileTable, 0, sizeof(FILE) * MAX_OPEN_FILES);
// offset and ptr are unused in these
stdin = openFileTable + 0;
stdin->_file = 0;
stdin->_oflag = O_RDONLY;
stdin->_flag = O_RDONLY;
stdout = openFileTable + 1;
stdout->_file = 1;
stdout->_oflag = O_WRONLY | O_APPEND;
stdout->_flag = O_WRONLY | O_APPEND;
stderr = openFileTable + 2;
stderr->_file = 2;
stderr->_oflag = O_WRONLY | O_APPEND;
stderr->_flag = O_WRONLY | O_APPEND;
openFiles = 3;
}