Implement support for Console IO

This commit is contained in:
Ali Mashtizadeh 2014-10-14 16:24:47 -07:00
parent 5852ce5727
commit 331b8aba6e
7 changed files with 120 additions and 12 deletions

View File

@ -11,5 +11,11 @@ void *SystemMemMap(void *addr, uint64_t len, int flags);
int SystemMemUnmap(void *addr, uint64_t len);
int SystemMemProtect(void *addr, uint64_t len, int flags);
// IO
int SystemRead(uint64_t fd, void *addr, uint64_t off, uint64_t length);
int SystemWrite(uint64_t fd, void *addr, uint64_t off, uint64_t length);
int SystemFlush(uint64_t fd);
uint64_t SystemOpen(const char *path, uint64_t flags);
#endif /* __SYSCALL_H__ */

View File

@ -61,3 +61,9 @@ SystemFlush(uint64_t fd)
return syscall(SYSCALL_FLUSH, fd);
}
uint64_t
SystemOpen(const char *path, uint64_t flags)
{
return syscall(SYSCALL_OPEN, path, flags);
}

View File

@ -3,6 +3,8 @@
#include <stdint.h>
#include <sys/spinlock.h>
#include <sys/kmem.h>
#include <sys/thread.h>
#include "console.h"
#include "x86/vgacons.h"
@ -131,3 +133,65 @@ Console_Puts(const char *str)
Spinlock_Unlock(&consoleLock);
}
int
Console_Read(Handle *handle, void *buf, uint64_t off, uint64_t len)
{
uintptr_t b = (uintptr_t)buf;
uint64_t i;
for (i = 0; i < len; i++)
{
char c = Console_Getc();
CopyOut(&c, b+i, 1);
}
return len;
}
int
Console_Write(Handle *handle, void *buf, uint64_t off, uint64_t len)
{
uintptr_t b = (uintptr_t)buf;
char kbuf[512];
uint64_t nbytes = 0;
while (len > nbytes) {
uint64_t chunksz = len > 512 ? 512 : len;
CopyIn(b + nbytes, &kbuf, chunksz);
nbytes += chunksz;
for (int i = 0; i < chunksz; i++)
Console_Putc(kbuf[i]);
}
return nbytes;
}
int
Console_Flush(Handle *handle)
{
return 0;
}
int
Console_Close(Handle *handle)
{
Handle_Free(handle);
return 0;
}
Handle *
Console_OpenHandle()
{
Handle *handle = Handle_Alloc();
if (!handle)
return NULL;
handle->read = &Console_Read;
handle->write = &Console_Write;
handle->flush = &Console_Flush;
handle->close = &Console_Close;
return handle;
}

View File

@ -21,5 +21,7 @@ typedef struct Handle {
int (*close)(Handle *); // Close
} Handle;
DECLARE_SLAB(Handle);
#endif /* __SYS_HANDLE_H__ */

View File

@ -9,6 +9,16 @@
#include <sys/thread.h>
#include <sys/syscall.h>
Slab handleSlab;
void
Handle_GlobalInit()
{
Slab_Init(&handleSlab, "Handle Objects", sizeof(Handle), 16);
}
DEFINE_SLAB(Handle, &handleSlab);
void
Handle_Init(Thread *thr)
{

View File

@ -1,6 +1,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
@ -49,15 +50,16 @@ uint64_t
Syscall_MMap(uint64_t addr, uint64_t len, uint64_t prot)
{
Thread *cur = Thread_Current();
uint64_t p;
uint64_t pgs = (len + PGSIZE - 1) / PGSIZE;
bool status;
for (p = 0; p < len; p += PGSIZE)
{
void *pg = PAlloc_AllocPage();
PMap_Map(cur->space, (uint64_t)DMVA2PA(pg), addr + p, 1, 0);
status = PMap_AllocMap(cur->space, addr, pgs, PTE_W);
if (!status) {
// XXX: Need to unmap PMap_Unmap(cur->space, addr, pgs);
return 0;
} else {
return addr;
}
return addr;
}
uint64_t
@ -93,8 +95,6 @@ Syscall_Read(uint64_t fd, uint64_t addr, uint64_t off, uint64_t length)
if (handle == NULL)
return -EBADF;
// Verify memory region and pin
return (handle->write)(handle, (void *)addr, off, length);;
}
@ -107,8 +107,6 @@ Syscall_Write(uint64_t fd, uint64_t addr, uint64_t off, uint64_t length)
if (handle == NULL)
return -EBADF;
// Verify memory region and pin
return (handle->write)(handle, (void *)addr, off, length);;
}
@ -124,9 +122,28 @@ Syscall_Flush(uint64_t fd)
return (handle->flush)(handle);
}
// XXX: Cleanup
Handle *Console_OpenHandle();
uint64_t
Syscall_Open(uint64_t path, uint64_t flags)
Syscall_Open(uint64_t user_path, uint64_t flags)
{
Thread *cur = Thread_Current();
int status;
char path[512];
// XXX: Use CopyInStr
status = CopyIn(user_path, &path, sizeof(path));
if (status != 0)
return status;
if (strncmp("/dev/", path, 5) == 0) {
if (strcmp("/dev/console", path) == 0) {
Handle *handle = Console_OpenHandle();
return Handle_Add(cur, handle);
}
}
NOT_IMPLEMENTED();
return 0;
}

View File

@ -20,6 +20,8 @@ TAILQ_HEAD(ThreadQueueHead, Thread) threadQueue;
Slab threadSlab;
void Handle_GlobalInit();
void
Thread_Init()
{
@ -35,6 +37,7 @@ Thread_Init()
TAILQ_INIT(&threadQueue);
Handle_GlobalInit();
}
Thread *