Syscalls totally seem to work

This commit is contained in:
Mark
2019-10-14 13:27:39 +03:00
parent 3dfa5a54f4
commit 5f820044e5
4 changed files with 60 additions and 42 deletions
-1
View File
@@ -3,7 +3,6 @@ HEADERS=$(shell find ./include -name "*.h")
CFLAGS+=-Wall \
-Wextra \
-Werror \
-Wpedantic \
-Wno-unused-parameter \
-Iinclude \
-Wno-unused-variable \
+1
View File
@@ -18,6 +18,7 @@ amd64_irq1:
// Read from keyboard buffer
inb $0x60, %al
movb %al, irq1_key(%rip)
irq_eoi_lapic 1
+27 -40
View File
@@ -18,61 +18,48 @@ static char t_stack3[IDLE_STACK * AMD64_MAX_SMP] = {0};
#endif
static struct thread t_idle[AMD64_MAX_SMP] = {0};
#define ASM_SYSCALL3(n, r0, r1, r2) ( \
{ \
register int64_t rax asm ("rax") = (int64_t) (n); \
register int64_t rdi asm ("rdi") = (int64_t) (r0); \
register int64_t rsi asm ("rsi") = (int64_t) (r1); \
register int64_t rdx asm ("rdx") = (int64_t) (r2); \
asm volatile ("syscall");\
rax; \
} \
)
static inline int sc_write(int fd, const void *buf, size_t count) {
return ASM_SYSCALL3(1, fd, buf, count);
}
static inline int sc_read(int fd, void *buf, size_t count) {
return ASM_SYSCALL3(0, fd, buf, count);
}
void func0(uintptr_t id, int state) {
uintptr_t a0 = 0, a1 = 0;
uint16_t attr = (id + 0x2) << 8;
uint16_t letter = '0' + id;
*((uint16_t *) MM_VIRTUALIZE(0xB8000) + 80 * 0 + id) = state * attr | letter;
{
register uintptr_t rsp asm ("rsp");
a0 = rsp;
}
asm volatile ("syscall");
{
register uintptr_t rsp asm ("rsp");
a1 = rsp;
}
if (a0 != a1) {
while (1);
}
}
void idle_func(uintptr_t id) {
#if defined(AMD64_IDLE_RING3)
int state = 0;
uintptr_t a0 = 0, a1 = 0;
char c;
*((uint16_t *) MM_VIRTUALIZE(0xB8000) + 80 * 1 + id) = (id + 'A') | ((id + 0x9) << 8);
{
register uintptr_t rsp asm ("rsp");
a0 = rsp;
}
asm volatile ("syscall");
{
register uintptr_t rsp asm ("rsp");
a1 = rsp;
}
if (a0 != a1) {
while (1);
}
while (1) {
func0(id, state);
{
register uintptr_t rsp asm ("rsp");
a0 = rsp;
}
asm volatile ("syscall");
{
register uintptr_t rsp asm ("rsp");
a1 = rsp;
}
if (id == 0) {
sc_read(0, &c, 1);
if (a0 != a1) {
while (1);
}
sc_write(1, "Yeah\n", 5);
sc_write(1, &c, 1);
}
func0(id, state);
state = !state;
for (size_t i = 0; i < 10000000; ++i);
}
+32 -1
View File
@@ -1,6 +1,37 @@
#include "sys/types.h"
#include "sys/debug.h"
int amd64_syscall(uintptr_t rdi, uintptr_t rsi, uintptr_t rdx, uintptr_t rcx, uintptr_t r10, uintptr_t rax) {
static ssize_t sys_read(int fd, void *buf, size_t lim);
static ssize_t sys_write(int fd, const void *buf, size_t lim);
uint8_t irq1_key = 0;
intptr_t amd64_syscall(uintptr_t rdi, uintptr_t rsi, uintptr_t rdx, uintptr_t rcx, uintptr_t r10, uintptr_t rax) {
switch (rax) {
case 0:
return sys_read((int) rdi, (void *) rsi, (size_t) rdx);
case 1:
return sys_write((int) rdi, (const void *) rsi, (size_t) rdx);
default:
return -1;
}
return 0;
}
static ssize_t sys_read(int fd, void *buf, size_t lim) {
irq1_key = 0;
while (irq1_key == 0) {
asm volatile ("hlt");
}
*((char *) buf) = irq1_key + '0' - 1;
return 1;
}
static ssize_t sys_write(int fd, const void *buf, size_t lim) {
kdebug("write(%d, ..., %lu)\n", fd, lim);
for (size_t i = 0; i < lim; ++i) {
debugc(DEBUG_DEFAULT, ((const char *) buf)[i]);
}
debugc(DEBUG_DEFAULT, '\n');
return lim;
}