Keyboard input and write syscalls (VFS)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "sys/types.h"
|
||||
#include "sys/fs/vfs.h"
|
||||
|
||||
#define THREAD_KERNEL (1 << 31)
|
||||
|
||||
@@ -15,6 +16,9 @@ struct thread {
|
||||
uint32_t pid;
|
||||
uint32_t parent_pid;
|
||||
|
||||
struct vfs_ioctx ioctx;
|
||||
struct ofile fds[4];
|
||||
|
||||
mm_space_t space;
|
||||
|
||||
// TODO: maybe __sched_thread
|
||||
|
||||
+27
-1
@@ -1,6 +1,24 @@
|
||||
#include "sys/amd64/hw/ps2.h"
|
||||
#include "sys/amd64/hw/irq.h"
|
||||
#include "sys/amd64/hw/io.h"
|
||||
#include "sys/debug.h"
|
||||
#include "sys/tty.h"
|
||||
|
||||
static const char ps2_key_table_0[128] = {
|
||||
[0x00] = 0,
|
||||
|
||||
[0x01] = '\003',
|
||||
[0x02] = '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',
|
||||
[0x0F] = '\t',
|
||||
[0x10] = 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']',
|
||||
[0x1C] = '\n',
|
||||
[0x1E] = 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
|
||||
[0x29] = '`',
|
||||
[0x2B] = '\\',
|
||||
[0x2C] = 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
|
||||
[0x39] = ' ',
|
||||
[0x7F] = 0
|
||||
};
|
||||
|
||||
int ps2_irq_keyboard(void) {
|
||||
uint8_t st = inb(0x64);
|
||||
@@ -9,7 +27,15 @@ int ps2_irq_keyboard(void) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
inb(0x60);
|
||||
uint8_t key = inb(0x60);
|
||||
|
||||
if (!(key & 0x80)) {
|
||||
char key_char = ps2_key_table_0[key];
|
||||
|
||||
if (key_char != 0) {
|
||||
tty_buffer_write(0, key_char);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "sys/assert.h"
|
||||
#include "sys/string.h"
|
||||
#include "sys/binfmt_elf.h"
|
||||
#include "sys/tty.h"
|
||||
#include "sys/mm.h"
|
||||
|
||||
void sched_add_to(int cpu, struct thread *t);
|
||||
@@ -107,6 +108,15 @@ void init_func(void *arg) {
|
||||
panic("Failed to load binary: %s\n", kstrerror(res));
|
||||
}
|
||||
|
||||
// Set tty0 input
|
||||
init_thread->fds[0].flags = O_RDONLY;
|
||||
init_thread->fds[0].vnode = tty0;
|
||||
init_thread->fds[0].pos = 0;
|
||||
|
||||
init_thread->fds[1].flags = O_WRONLY;
|
||||
init_thread->fds[1].vnode = tty0;
|
||||
init_thread->fds[1].pos = 0;
|
||||
|
||||
kdebug("Done\n");
|
||||
sched_add_to(0, init_thread);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "sys/amd64/cpu.h"
|
||||
#include "sys/assert.h"
|
||||
#include "sys/thread.h"
|
||||
#include "sys/string.h"
|
||||
#include "sys/heap.h"
|
||||
#include "sys/vmalloc.h"
|
||||
#include "sys/debug.h"
|
||||
@@ -93,6 +94,9 @@ int thread_init(
|
||||
t->flags = flags;
|
||||
t->next = NULL;
|
||||
|
||||
memset(&t->ioctx, 0, sizeof(t->ioctx));
|
||||
memset(t->fds, 0, sizeof(t->fds));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+28
-10
@@ -1,5 +1,9 @@
|
||||
#include "sys/amd64/cpu.h"
|
||||
#include "sys/thread.h"
|
||||
#include "sys/assert.h"
|
||||
#include "sys/types.h"
|
||||
#include "sys/debug.h"
|
||||
#include "sys/errno.h"
|
||||
|
||||
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);
|
||||
@@ -19,19 +23,33 @@ intptr_t amd64_syscall(uintptr_t rdi, uintptr_t rsi, uintptr_t rdx, uintptr_t rc
|
||||
}
|
||||
|
||||
static ssize_t sys_read(int fd, void *buf, size_t lim) {
|
||||
irq1_key = 0;
|
||||
while (irq1_key == 0) {
|
||||
asm volatile ("hlt");
|
||||
if (fd >= 4 || fd < 0) {
|
||||
return -EBADF;
|
||||
}
|
||||
*((char *) buf) = irq1_key + '0' - 1;
|
||||
return 1;
|
||||
struct thread *thr = get_cpu()->thread;
|
||||
_assert(thr);
|
||||
|
||||
struct ofile *of = &thr->fds[fd];
|
||||
|
||||
if (!of->vnode) {
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
return vfs_read(&thr->ioctx, of, buf, lim);
|
||||
}
|
||||
|
||||
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]);
|
||||
if (fd >= 4 || fd < 0) {
|
||||
return -EBADF;
|
||||
}
|
||||
debugc(DEBUG_DEFAULT, '\n');
|
||||
return lim;
|
||||
struct thread *thr = get_cpu()->thread;
|
||||
_assert(thr);
|
||||
|
||||
struct ofile *of = &thr->fds[fd];
|
||||
|
||||
if (!of->vnode) {
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
return vfs_write(&thr->ioctx, of, buf, lim);
|
||||
}
|
||||
|
||||
+13
-1
@@ -19,9 +19,21 @@ ssize_t sys_write(int fd, const void *buf, size_t count) {
|
||||
return (ssize_t) ASM_SYSCALL3(1, fd, buf, count);
|
||||
}
|
||||
|
||||
ssize_t sys_read(int fd, void *buf, size_t count) {
|
||||
return (ssize_t) ASM_SYSCALL3(0, fd, buf, count);
|
||||
}
|
||||
|
||||
void _start(void) {
|
||||
sys_write(1, "Hello!\n", 7);
|
||||
char c;
|
||||
|
||||
while (1) {
|
||||
ssize_t sz = sys_read(0, &c, 1);
|
||||
|
||||
if (sz >= 0) {
|
||||
sys_write(1, "Char: ", 6);
|
||||
sys_write(1, &c, 1);
|
||||
c = '\n';
|
||||
sys_write(1, &c, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user