From 93af9a589980403f9887936963a6ce4f189dc020 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 8 Mar 2015 13:46:41 +0100 Subject: Channels. --- src/common/include/fs.h | 2 +- src/common/include/token.h | 11 +++ src/kernel/Makefile | 2 +- src/kernel/include/ipc.h | 21 ++++++ src/kernel/user/ipc.c | 162 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 src/common/include/token.h create mode 100644 src/kernel/include/ipc.h create mode 100644 src/kernel/user/ipc.c (limited to 'src') diff --git a/src/common/include/fs.h b/src/common/include/fs.h index 880202b..431c16f 100644 --- a/src/common/include/fs.h +++ b/src/common/include/fs.h @@ -8,7 +8,7 @@ #define FT_DEV (0x02) #define FT_BLOCKDEV (0x04) #define FT_CHARDEV (0x08) -#define FT_FIFO (0x10) +#define FT_CHANNEL (0x10) // dual-pipe // #define FT_SOCKET (0x20) // Not yet! Semantics not defined. (TODO) // FM_* enum describes modes for opening a file as well as authorized operations in stat_t diff --git a/src/common/include/token.h b/src/common/include/token.h new file mode 100644 index 0000000..9ec1aff --- /dev/null +++ b/src/common/include/token.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +#define TOKEN_LENGTH 16 +typedef struct { + char bytes[TOKEN_LENGTH]; +} token_t; + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/kernel/Makefile b/src/kernel/Makefile index 1a7c12e..f7cee23 100644 --- a/src/kernel/Makefile +++ b/src/kernel/Makefile @@ -3,7 +3,7 @@ OBJ = core/loader.o core/dbglog.o \ core/gdt.o core/idt.o core/interrupt.o core/context_switch.o core/thread.o \ core/frame.o core/paging.o core/freemem.o core/region.o core/kmalloc.o \ core/worker.o core/prng.o \ - user/vfs.o user/nullfs.o user/process.o user/elf.o user/syscall.o \ + user/vfs.o user/nullfs.o user/process.o user/elf.o user/syscall.o user/ipc.o \ dev/pci.o dev/pciide.o \ fs/iso9660.o diff --git a/src/kernel/include/ipc.h b/src/kernel/include/ipc.h new file mode 100644 index 0000000..5558120 --- /dev/null +++ b/src/kernel/include/ipc.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +// ---- Communication channels + +#define CHANNEL_BUFFER_SIZE 1000 // 1000 + other channel_t fields = 1024 - epsilon + +typedef struct { + fs_handle_t *a, *b; +} fs_handle_pair_t; + +fs_handle_pair_t make_channel(); + +// ---- Tokens for sharing file descriptors between processes + +token_t gen_token_for(fs_handle_t *h); +fs_handle_t *use_token(token_t tok); + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/kernel/user/ipc.c b/src/kernel/user/ipc.c new file mode 100644 index 0000000..b23af56 --- /dev/null +++ b/src/kernel/user/ipc.c @@ -0,0 +1,162 @@ +#include + +#include +#include +#include + +static size_t channel_read(fs_node_ptr c, size_t offset, size_t len, char* buf); +static size_t channel_write(fs_node_ptr c, size_t offset, size_t len, const char* buf); +static bool channel_stat(fs_node_ptr c, stat_t *st); +static void channel_close(fs_node_ptr c); + +static fs_node_ops_t channel_ops = { + .read = channel_read, + .write = channel_write, + .close = channel_close, + .open = 0, + .readdir = 0, + .ioctl = 0, + .stat = channel_stat, + .walk = 0, + .create = 0, + .move = 0, + .delete = 0, + .dispose = 0, +}; + +typedef struct channel { + struct channel *other_side; + + mutex_t lock; + + char buf[CHANNEL_BUFFER_SIZE]; + size_t buf_use_begin, buf_used; // circular buffer +} channel_t; + +fs_handle_pair_t make_channel() { + fs_handle_pair_t ret = { .a = 0, .b = 0 }; + channel_t *a = 0, *b = 0; + + a = (channel_t*)malloc(sizeof(channel_t)); + if (a == 0) goto error; + + b = (channel_t*)malloc(sizeof(channel_t)); + if (b == 0) goto error; + + ret.a = (fs_handle_t*)malloc(sizeof(fs_handle_t)); + if (ret.a == 0) goto error; + + ret.b = (fs_handle_t*)malloc(sizeof(fs_handle_t)); + if (ret.b == 0) goto error; + + a->other_side = b; + b->other_side = a; + a->lock = b->lock = MUTEX_UNLOCKED; + a->buf_use_begin = a->buf_used = 0; + b->buf_use_begin = b->buf_used = 0; + + ret.a->fs = ret.b->fs = 0; + ret.a->node = ret.b->node = 0; + ret.a->ops = ret.b->ops = &channel_ops; + ret.a->data = a; + ret.b->data = b; + ret.a->refs = ret.b->refs = 1; + ret.a->mode = ret.b->mode = FM_READ | FM_WRITE; + + return ret; + +error: + if (a) free(a); + if (b) free(b); + if (ret.a) free(ret.a); + if (ret.b) free(ret.b); + ret.a = ret.b = 0; + return ret; +} + +size_t channel_read(fs_node_ptr ch, size_t offset, size_t len, char* buf) { + channel_t *c = (channel_t*)ch; + + mutex_lock(&c->lock); + + if (c->buf_used < len) len = c->buf_used; + + if (len) { + size_t len0 = len, len1 = 0; + + if (c->buf_use_begin + len > CHANNEL_BUFFER_SIZE) { + len0 = CHANNEL_BUFFER_SIZE - c->buf_use_begin; + len1 = len - len0; + } + memcpy(buf, c->buf + c->buf_use_begin, len0); + if (len1) memcpy(buf + len0, c->buf, len1); + + c->buf_use_begin = (c->buf_use_begin + len) % CHANNEL_BUFFER_SIZE; + c->buf_used -= len; + + if (c->buf_used == 0) c->buf_use_begin = 0; + } + + mutex_unlock(&c->lock); + + return len; +} + +size_t channel_write(fs_node_ptr ch, size_t offset, size_t len, const char* buf) { + channel_t *tc = (channel_t*)ch; + channel_t *c = tc->other_side; + + if (c == 0) return 0; + + while (!mutex_try_lock(&c->lock)) { + yield(); + if (tc->other_side == 0) return 0; + } + + if (c->buf_used + len > CHANNEL_BUFFER_SIZE) len = CHANNEL_BUFFER_SIZE - c->buf_used; + + if (len) { + size_t len0 = len, len1 = 0; + + if (c->buf_use_begin + c->buf_used + len > CHANNEL_BUFFER_SIZE) { + len0 = CHANNEL_BUFFER_SIZE - c->buf_use_begin - c->buf_used; + len1 = len - len0; + } + memcpy(c->buf + c->buf_use_begin + c->buf_used, buf, len0); + if (len1) memcpy(c->buf, buf + len0, len1); + + c->buf_used += len; + } + + mutex_unlock(&c->lock); + + if (len) resume_on(c); // notify processes that may be waiting for data + + return len; +} + +bool channel_stat(fs_node_ptr ch, stat_t *st) { + channel_t *c = (channel_t*)ch; + + mutex_lock(&c->lock); + + st->type = FT_CHANNEL; + st->size = c->buf_used; + st->access = FM_READ | FM_WRITE; + + mutex_unlock(&c->lock); + + return true; +} + +void channel_close(fs_node_ptr ch) { + channel_t *c = (channel_t*)ch; + + mutex_lock(&c->lock); + + c->other_side->other_side = 0; + free(c); +} + + +/* vim: set ts=4 sw=4 tw=0 noet :*/ -- cgit v1.2.3