almost got the term emu working and shell

This commit is contained in:
2026-07-08 21:15:30 -05:00
parent ec3cbb4794
commit 4d98d73f5e
20 changed files with 214 additions and 228 deletions

View File

@@ -1,15 +1,18 @@
#include <stdint.h>
#include <stddef.h>
#include <drivers/pty/pty.h>
#include "lib/memory.h"
#include "lib/ringbuf.h"
#include "memory/mm.h"
#include "process.h"
#include "scheduler.h"
#include "vfs.h"
#include "elf.h"
#include <drivers/vga/vga.h>
#include <drivers/pty/pty.h>
#include <lib/print.h>
#include <lib/string.h>
@@ -53,11 +56,13 @@ static int32_t sys_read(int fd, void* buf, size_t count) {
}
static int32_t sys_open(const char* filename, int flags) {
return -1;
return open(filename, flags);
}
static int32_t sys_close(int fd) {
return -1;
process_t* task = current_task();
task->fd_table[fd] = NULL;
return 1;
}
static int32_t sys_brk(uint32_t new_break) {
@@ -155,6 +160,42 @@ static int32_t sys_fork(registers_t* parent_regs) {
return child->pid;
}
static int32_t sys_exec(registers_t* regs, const char* prgm) {
clear_current_pd();
uint32_t eip = load_elf_program(prgm);
uint32_t stack_sz = PAGE_SIZE * 16;
uint32_t stack_top = 0xBFFF0000;
uint32_t stack_bottom = stack_top - stack_sz;
for (uint32_t vaddr = stack_bottom; vaddr < stack_top; vaddr += PAGE_SIZE) {
void* phys_frame = p_alloc_frame();
map_page(vaddr, (uint32_t)phys_frame, PAGE_PRESENT | PAGE_WRITABLE | PAGE_USER);
}
uint32_t* u_esp = (uint32_t*)stack_top;
*(--u_esp) = 0; // envp = NULL
*(--u_esp) = 0; // argv = NULL
*(--u_esp) = 0; // argc = 0
regs->eax = 0;
regs->ecx = 0;
regs->edx = 0;
regs->ebp = 0;
regs->esi = 0;
regs->edi = 0;
regs->eip = eip;
regs->useresp = (uint32_t)u_esp;
return 0;
}
static int32_t sys_dup2(int src, int dst) {
process_t* task = current_task();
task->fd_table[dst] = task->fd_table[src];
return dst;
}
void syscall(registers_t* regs) {
int32_t ret;
switch(regs->eax) {
@@ -166,6 +207,8 @@ void syscall(registers_t* regs) {
case 12: ret = sys_brk(regs->ebx); break;
case 20: ret = sys_pty((int*)regs->ebx); break;
case 21: ret = sys_fork(regs); break;
case 22: ret = sys_exec(regs, (const char*)regs->ebx); break;
case 23: ret = sys_dup2(regs->ebx, regs->ecx); break;
default: ret = -1;
}
regs->eax = ret;