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 SRC = ./src
BIN = ./bin BIN = ./bin
C_FILES = calc editor C_FILES = calc edit
FONTS = testfont term16n FONTS = testfont term16n
.PHONY: os clean run .PHONY: os clean run
+111 -40
View File
@@ -6,58 +6,80 @@
#include <keyboard/keyboard.h> #include <keyboard/keyboard.h>
#include <memory/malloc.h> #include <memory/malloc.h>
#include <memory/addresses.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 <syscall.h>
#include <stdint.h> #include <stdint.h>
#include <fs/fs.h> #include <stdlib.h>
/* /*
* XXX WARNING - Update MAX_SYSCALLS in the handler XXX * XXX WARNING - Update MAX_SYSCALLS in the handler XXX
*/ */
// Args: ebx - number of miliseconds typedef struct {
void sys_sleep() { uint32_t esp;
uint32_t* sleepTimerTicks = (uint32_t*)SLEEP_TIMER; 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;
__asm__ volatile ("movl %%ebx, %0" : "=r"(*sleepTimerTicks)); 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... while (*sleepTimerTicks > 0) { // Wait...
__asm__ volatile ("sti;hlt;cli;"); __asm__ volatile ("sti;hlt;cli;");
} }
return 0;
} }
// Args: ebx - int fd // Args: ebx - int fd
// ecx - void* buf // ecx - void* buf
// edx - uint32_t len // edx - uint32_t len
// Rets: Number of bytes written // Rets: Number of bytes written
void sys_write() { int sys_write(sys_regs_t regs) {
int32_t fd = 0; int32_t fd = regs.ebx;
void* buf = NULL; void* buf = (void*)regs.ecx;
uint32_t len = 0; uint32_t len = regs.edx;
__asm__ volatile ("nop" : "=b"(fd), "=c"(buf), "=d"(len));
// TODO: Open fd table? // TODO: Open fd table?
if (fd == 1) { // "stdout" if (fd == 1) { // "stdout"
int32_t result = terminalWrite(buf, len); // Call a terminal print function return terminalWrite(buf, len); // Call a terminal print function
__asm__ volatile ("movl %0, %%eax" : : "r"(result)); } else {
} else { // Not implemented return -1;
__asm__ volatile ("movl $-1, %eax");
} }
} }
// TODO: Use a Read() sycall and read from stdin // TODO: Use a Read() sycall and read from stdin
// Args: ebx - char* buffer // Args: ebx - char* buffer
void sys_read() { int sys_read(sys_regs_t regs) {
char* s; char* s = (char*)regs.ebx;
__asm__ volatile ("movl %%ebx, %0" : "=r"(s));
reads(s); reads(s);
return 0;
} }
// Args: ebx - size_t count // Args: ebx - size_t count
// Rets: Pointer to malloc-ed memory // Rets: Pointer to malloc-ed memory
void sys_malloc() { int sys_malloc(sys_regs_t regs) {
uint32_t count = 0; uint32_t count = regs.ebx;
__asm__ volatile ("movl %%ebx, %0" : "=b"(count));
// First Malloc() // First Malloc()
if (!mallocListHead) { if (!mallocListHead) {
@@ -67,41 +89,88 @@ void sys_malloc() {
void* ptr = mallocNextNode(count); void* ptr = mallocNextNode(count);
mallocMergeFree(); mallocMergeFree();
// Return allocated address in eax return (uint32_t)ptr;
__asm__ volatile ("movl %0, %%eax" : : "r"(ptr));
} }
// Args: ebx - void* ptr // Args: ebx - void* ptr
void sys_free() { int sys_free(sys_regs_t regs) {
void* ptr = NULL; void* ptr = (void*)regs.ebx;
__asm__ volatile ("movl %%ebx, %0" : "=b"(ptr));
mallocFree(ptr); mallocFree(ptr);
return 0;
} }
// Args: ebx - path, // Args: ebx - path,
// ecx - oflag // ecx - oflag
// Rets: Opened file descriptor // Rets: Opened file descriptor
void sys_open() { int sys_open(sys_regs_t regs) {
uint32_t fd = 0; int fd = 0;
char* path = NULL; char* path = (char*)regs.ebx;
uint32_t oflag = 0; 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);
__asm__ volatile ("movl %0, %%eax" : : "a"(fd)); // 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
}*/
return fd;
} }
// Args: ebx - int fd // Args: ebx - int fd
// Rets: status // 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 // TODO
void sys_seek() {} int sys_seek(sys_regs_t regs) { return -1; }
// System call table // System call table
void (*syscalls[MAX_SYSCALLS])(void) = { int (*syscalls[MAX_SYSCALLS])(sys_regs_t) = {
[SYS_RESERVED] = sys_reserved,
[SYS_SLEEP] = sys_sleep, [SYS_SLEEP] = sys_sleep,
[SYS_WRITE] = sys_write, [SYS_WRITE] = sys_write,
[SYS_READ] = sys_read, [SYS_READ] = sys_read,
@@ -127,7 +196,7 @@ void syscallHandler(intFrame_t* iframe) {
"ja invalidSyscall\n" "ja invalidSyscall\n"
// Call the syscall // Call the syscall
"push eax\n" // Push required values "push eax\n"
"push gs\n" "push gs\n"
"push fs\n" "push fs\n"
"push es\n" "push es\n"
@@ -139,11 +208,13 @@ void syscallHandler(intFrame_t* iframe) {
"push ecx\n" "push ecx\n"
"push ebx\n" "push ebx\n"
"push esp\n" // Push stack pointer again (for variables and such idk) "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 "call [syscalls + eax * 4]\n" // Call syscall from table. Syscall number should be in eax
"add esp, 4\n" "add esp, 4\n" // Do not override ESP
"pop ebx\n" // Pop pushed values "pop ebx\n"
"pop ecx\n" "pop ecx\n"
"pop edx\n" "add esp, 4\n" // Do not override EDX, some syscalls use it
"pop esi\n" "pop esi\n"
"pop edi\n" "pop edi\n"
"pop ebp\n" "pop ebp\n"
@@ -159,7 +230,7 @@ void syscallHandler(intFrame_t* iframe) {
"mov eax, -1\n" // Current error thing "mov eax, -1\n" // Current error thing
"iretd\n" "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 #ifndef MEMORY_MALLOC_H
#define MEMORY_MALLOC_H #define MEMORY_MALLOC_H
#include <stdint.h>
#include <memory/virtual.h> #include <memory/virtual.h>
#include <memory/physical.h> #include <memory/physical.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
// Singly linked list! // Singly linked list!
typedef struct mallocNode_s { 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;
fileInfo_t files[] = { // TODO: Not have to list all of the files, loop through a directory or smth 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"stage1.bin" },
{ BIN_DIR"stage2.bin" }, { BIN_DIR"stage2.bin" },
{ BIN_DIR"stage3.bin" }, { BIN_DIR"stage3.bin" },
/* Kernel stuff */
{ BIN_DIR"kernel.bin" }, { BIN_DIR"kernel.bin" },
{ BIN_DIR"term16n.fnt" }, { BIN_DIR"term16n.fnt" },
{ BIN_DIR"testfont.fnt" }, { BIN_DIR"testfont.fnt" },
/* Userspace files */
{ "./src/fs/test.txt" }, { "./src/fs/test.txt" },
{ BIN_DIR"calc.bin" }, { BIN_DIR"calc.bin" },
{ BIN_DIR"editor.bin" }, { BIN_DIR"edit.bin" },
}; };
superblock_t superblock = { 0 }; superblock_t superblock = { 0 };
View File
View File
+14 -22
View File
@@ -29,8 +29,7 @@
// FS things // FS things
char* cwd; char* cwd;
FILE* openFileTable; FILE* openFileTable = (FILE*)OPEN_FILE_TABLE_LOC;
uint32_t maxOpenFiles;
uint32_t openFiles; uint32_t openFiles;
// Function declarations // Function declarations
@@ -94,7 +93,7 @@ void main() {
// Run Interactive Shell Program // Run Interactive Shell Program
// TODO: Make it in another file // TODO: Make it in another file
while (1) { while (1) {
printf("%s %s ", cwd, PROMPT); printf("\e[6M\e[8N%s \e[2M%s \e[8M", cwd, PROMPT);
char input[256]; char input[256];
char* inputPtr = input; char* inputPtr = input;
@@ -192,7 +191,6 @@ void main() {
printf(" regs | Prints register values\n"); printf(" regs | Prints register values\n");
printf(" sleep | Sleeps for input number of seconds\n"); printf(" sleep | Sleeps for input number of seconds\n");
printf(" soundtest | Plays a tune :)\n"); printf(" soundtest | Plays a tune :)\n");
printf(" test | Performs tests\n");
} else if (strcmp(argv[0], "ls") == 0) { } else if (strcmp(argv[0], "ls") == 0) {
listFiles(); listFiles();
} else if (strcmp(argv[0], "memmap") == 0) { } else if (strcmp(argv[0], "memmap") == 0) {
@@ -233,20 +231,15 @@ void main() {
rest(120); rest(120);
disableSpeaker(); disableSpeaker();
} else if (strcmp(argv[0], "test") == 0) {
printf("\x1b[1MError: 'test' is not available.\x1b[8M\n");
} else { // TODO: Use cwd for this } else { // TODO: Use cwd for this
inode_t* root = (inode_t*)impl_superblock->rootInodePtr; char* fileName = argv[0];
diskRead(root->extent[0].block * 8, root->extent[0].length * 8, (void*)SCRATCH_BLOCK_LOC); //FILE* fp = fopen(fileName, "rb");
dirEntry_t* dentry = (dirEntry_t*)SCRATCH_BLOCK_LOC;
inode_t* inode = getInode(argv[0], dentry); //if (fp == NULL) {
if (inode == NULL) { printf("Command not found: %s\n", fileName);
printf("Command not found: %s\n", argv[0]); // continue;
continue; //}
}
printf("%d: %s %dB\n", inode->id, argv[0], inode->sizeBytes);
} }
} }
@@ -344,23 +337,22 @@ void printPhysicalMemmap() {
} }
void initOpenFileTable() { void initOpenFileTable() {
maxOpenFiles = 256; openFileTable = malloc(sizeof(FILE) * MAX_OPEN_FILES);
if (openFileTable == NULL) { printf("\e[1MFailed to init file table! Malloc() error.\e[8M"); }
openFileTable = malloc(sizeof(FILE) * maxOpenFiles); memset(openFileTable, 0, sizeof(FILE) * MAX_OPEN_FILES);
memset(openFileTable, 0, sizeof(FILE) * maxOpenFiles);
// offset and ptr are unused in these // offset and ptr are unused in these
stdin = openFileTable + 0; stdin = openFileTable + 0;
stdin->_file = 0; stdin->_file = 0;
stdin->_oflag = O_RDONLY; stdin->_flag = O_RDONLY;
stdout = openFileTable + 1; stdout = openFileTable + 1;
stdout->_file = 1; stdout->_file = 1;
stdout->_oflag = O_WRONLY | O_APPEND; stdout->_flag = O_WRONLY | O_APPEND;
stderr = openFileTable + 2; stderr = openFileTable + 2;
stderr->_file = 2; stderr->_file = 2;
stderr->_oflag = O_WRONLY | O_APPEND; stderr->_flag = O_WRONLY | O_APPEND;
openFiles = 3; openFiles = 3;
} }