hacked up stdio

This commit is contained in:
2024-03-29 23:46:31 +01:00
parent 394f25028d
commit cfca4a7185
8 changed files with 100 additions and 35 deletions

View File

@@ -27,6 +27,7 @@
#define SYSCALL_UNLINK_ID 13
#define SYSCALL_EXECVE_ID 50
#define SYSCALL_SBRK_ID 100
#define SYSCALL_PRINT_MEM 1000
@@ -53,9 +54,13 @@ int execve(char *name, char **argv, char **env) {
return _do_syscall(SYSCALL_EXECVE_ID, (uint64_t) name, (uint64_t) argv, (uint64_t) env);
}
int fork() {}
int fstat(int file, struct stat *st) {}
int getpid() {}
int isatty(int file) {}
int isatty(int file) { return file == 0 || file == 1 || file == 2; }
int fstat(int file, struct stat *st) {
if (isatty(file)) st->st_mode = S_IFCHR;
}
int kill(int pid, int sig) {}
int link(char *old, char *new) {}
int lseek(int file, int ptr, int dir) {
@@ -67,7 +72,9 @@ int open(const char *name, int flags, ...) {
int read(int file, char *ptr, int len) {
return _do_syscall(SYSCALL_READ_ID, file, (uint64_t) ptr, len);
}
caddr_t sbrk(int incr) {}
caddr_t sbrk(int incr) {
return (caddr_t) _do_syscall(SYSCALL_SBRK_ID, (int64_t) incr, 0, 0);
}
int stat(const char *file, struct stat *st) {}
clock_t times(struct tms *buf) {}
int unlink(char *name) {}

View File

@@ -0,0 +1,18 @@
//
// Created by Stepan Usatiuk on 29.03.2024.
//
#include "task.hpp"
extern "C" __attribute__((noreturn)) void exception_handler(void) {
//FIXME:
if (Scheduler::cur_task()->_mode == Task::TaskMode::TASKMODE_USER) {
writestr_no_yield("Task ded");
Scheduler::cur_task()->_state = Task::TaskState::TS_BLOCKED;
_yield_self_kern();
} else {
writestr_no_yield("Kernel ded");
_hcf();
}
}

View File

@@ -10,10 +10,6 @@
__attribute__((aligned(0x10))) static idt_entry_t idt[256]; // Create an array of IDT entries; aligned for performance
static idtr_t idtr;
extern "C" __attribute__((noreturn)) void exception_handler(void) {
_hcf();
}
extern "C" void pic1_irq_0();
extern "C" void pic1_irq_1();
extern "C" void pic1_irq_2();

View File

@@ -86,13 +86,34 @@ uint64_t syscall_close(uint64_t FD) {
return 1;
}
//FIXME:
uint64_t syscall_read(uint64_t fd, char *buf, uint64_t len) {
if (fd == 0) {
auto c = buf;
while ((c - buf) < len) {
*c = GlobalTtyManager.get_tty(0)->readchar();
if (*c == '\r') {
*(c) = '\n';
*(++c) = '0';
break;
}
c++;
}
return (c-buf);
}
auto f = FDT::current()->get(fd);
if (!f) return -1;
return f->read(buf, len);
}
uint64_t syscall_write(uint64_t fd, const char *buf, uint64_t len) {
if (fd == 1) {
auto c = buf;
while (*c != '\0' && (c - buf) < len) {
GlobalTtyManager.all_tty_putchar(*c);
c++;
}
}
auto f = FDT::current()->get(fd);
if (!f) return -1;
return f->write(buf, len);
@@ -188,6 +209,26 @@ uint64_t syscall_execve(const char *pathname, char *const argv[], char *const en
return 0;
}
char *syscall_sbrk(int brk) {
auto vma = Scheduler::cur_task()->_vma.get();
char *ret = reinterpret_cast<char *>(-1);
if (!vma) return reinterpret_cast<char *>(-1);
if (!vma->brk_start) {
vma->brk_start = (char *) vma->mmap_mem(nullptr, 16ULL * 1024ULL * 1024ULL /* 16MB */, 0, PAGE_RW | PAGE_USER);
if (!vma->brk_start) return reinterpret_cast<char *>(-1); // FIXME:
vma->brk_end_real = *vma->brk_start + 16ULL * 1024ULL * 1024ULL;
vma->brk_end_fake = vma->brk_start;
}
ret = *vma->brk_end_fake;
vma->brk_end_fake = *vma->brk_end_fake + brk;
return ret;
}
extern "C" uint64_t syscall_impl(uint64_t id_rdi, uint64_t a1_rsi, uint64_t a2_rdx, uint64_t a3_rcx) {
assert2(are_interrupts_enabled(), "why wouldn't they be?");
switch (id_rdi) {
@@ -213,6 +254,8 @@ extern "C" uint64_t syscall_impl(uint64_t id_rdi, uint64_t a1_rsi, uint64_t a2_r
return syscall_lseek(a1_rsi, a2_rdx, a3_rcx);
case SYSCALL_EXECVE_ID:
return syscall_execve(reinterpret_cast<const char *>(a1_rsi), reinterpret_cast<char *const *>(a2_rdx), reinterpret_cast<char *const *>(a3_rcx));
case SYSCALL_SBRK_ID:
return reinterpret_cast<uint64_t>(syscall_sbrk(static_cast<int64_t>(a1_rsi)));
case SYSCALL_OPENDIR_ID:
case SYSCALL_READDIR_ID:
case SYSCALL_CLOSEDIR_ID:

View File

@@ -10,6 +10,7 @@
#include "mutex.hpp"
#include <cstddef>
#include <cstdint>
#include <optional>
class AddressSpace;
@@ -31,6 +32,10 @@ public:
void *mmap_mem(void *v_addr, size_t length, int prot, int flags);
int munmap(void *addr, size_t length);
std::optional<char*> brk_start;
std::optional<char*> brk_end_fake;
std::optional<char*> brk_end_real;
private:
AddressSpace *space = nullptr;
Mutex space_lock;

View File

@@ -1,17 +1,7 @@
#include "syscalls_interface.h"
volatile char asdfasdf[323];
volatile int x = 3;
volatile int w = 0;
#include "stdio.h"
volatile const char *hello = "hello xd";
//
int main() {
if (x == 3) sputchar('x');
if (w == 2) sputchar('w');
if (asdfasdf[0] == '\0') sputchar('a');
sputchar('h');
sputchar('i');
sputchar('\n');
printf("Hi!\n");
}

View File

@@ -1,11 +1,24 @@
#include "syscalls_interface.h"
#include "stdio.h"
#include "stdlib.h"
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <sys/unistd.h>
int main() {
malloc(100);
malloc(100);
malloc(100);
malloc(100);
printf("Who are you?\n");
char *line = NULL;
size_t len = 0;
// __getline(&line, &len, stdin);
printf("hi %s\n", line);
// sputchar('h');
// sputchar('i');
// sputchar('\n');
@@ -18,26 +31,19 @@ int main() {
char buf[123];
read(test123, buf, 9);
sputchar('\n');
for (int i = 0; i < 8; i++) {
sputchar(buf[i]);
}
sputchar('\n');
printf("\n %s \n", buf);
usleep(100);
execve("/hello2", 0, 0);
while (1) {
// sputchar('h');
// sputchar('i');
sputchar('\n');
char read = sreadchar();
if (read == 'm') print_mem();
if (read == 't') print_tasks();
if (read == 'h') execve("/hello2", 0, 0);
sputchar('\n');
sputchar(read);
// sleep(100000);
printf("\n");
char c;
scanf(" %c", &c);
if (c == 'm') print_mem();
if (c == 't') print_tasks();
if (c == 'h') execve("/hello2", 0, 0);
printf("%c", c);
}
}

View File

@@ -34,7 +34,7 @@ extern "C" {
#define SYSCALL_UNLINK_ID 13
#define SYSCALL_EXECVE_ID 50
#define SYSCALL_SBRK_ID 100
#define SYSCALL_PRINT_MEM 1000
#define SYSCALL_PRINT_TASKS 1001