summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Santmyer <jon@jonsantmyer.com>2021-11-21 13:47:26 -0500
committerJon Santmyer <jon@jonsantmyer.com>2021-11-21 13:47:26 -0500
commitd2c79a9aad11acbb34e3c1d88cefa3568f1139e4 (patch)
treef877229f2f7b81f353373ae69f7b658fff811c34
parentae4a9d5bf564285502734c7c4f3eb67507bd6c56 (diff)
downloadmodit-slim-d2c79a9aad11acbb34e3c1d88cefa3568f1139e4.tar.gz
modit-slim-d2c79a9aad11acbb34e3c1d88cefa3568f1139e4.tar.bz2
modit-slim-d2c79a9aad11acbb34e3c1d88cefa3568f1139e4.zip
move libc sys to modit lib; add process concept to scheduler
-rw-r--r--Makefile8
-rw-r--r--app/init/Makefile4
-rw-r--r--app/init/ird.c0
-rw-r--r--app/init/main.c13
-rw-r--r--app/init/rpcsrv.c64
-rw-r--r--app/init/rpcsrv.h4
-rw-r--r--app/init/scheduler.c150
-rw-r--r--app/init/scheduler.h25
-rw-r--r--app/ld/Makefile2
m---------kernel0
-rw-r--r--lib/c/Makefile2
-rw-r--r--lib/c/stdlib/heap.c9
-rw-r--r--lib/c/sys/getcid.c10
-rw-r--r--lib/crt/Makefile6
-rw-r--r--lib/modit/Makefile17
-rw-r--r--lib/modit/cid/fork.c (renamed from lib/c/sys/fork.c)0
-rw-r--r--lib/modit/cid/getcid.c18
-rw-r--r--lib/modit/cid/settimer.c (renamed from lib/c/sys/settimer.c)0
-rw-r--r--lib/modit/cid/swapto.c (renamed from lib/c/sys/swapto.c)0
-rw-r--r--lib/modit/ipc/msg_get.c (renamed from lib/c/sys/msg_get.c)0
-rw-r--r--lib/modit/ipc/msg_recv.c (renamed from lib/c/sys/msg_recv.c)0
-rw-r--r--lib/modit/ipc/msg_send.c (renamed from lib/c/sys/msg_send.c)0
-rw-r--r--lib/modit/ipc/port.c (renamed from lib/c/sys/port.c)0
-rw-r--r--lib/modit/lock/mutex.c14
-rw-r--r--lib/modit/mem/alloc.c (renamed from lib/c/sys/alloc.c)0
-rw-r--r--lib/rpc/Makefile2
-rw-r--r--lib/rpc/fork.c15
-rw-r--r--lib/rpc/handle.c7
-rw-r--r--lib/rpc/rpc.c4
-rw-r--r--root/include/moditstd.h18
-rw-r--r--root/include/rpc/api.h4
-rw-r--r--root/include/rpc/rpc.h1
-rw-r--r--root/include/string.h2
-rw-r--r--root/include/sys/cid.h3
-rw-r--r--root/include/sys/lock.h13
-rw-r--r--root/include/sys/port.h6
-rw-r--r--tools/patches/gcc.patch2
37 files changed, 323 insertions, 100 deletions
diff --git a/Makefile b/Makefile
index 55682ba..17fab29 100644
--- a/Makefile
+++ b/Makefile
@@ -26,12 +26,14 @@ build: $(DISK1) iso
.PHONY: clean
clean:
$(MAKE) -C kernel clean
+ rm app/*/*.o
+ rm lib/*/*/*.o
.PHONY: kernel
kernel: tools/$(ARCH)_$(BITS) Makefile
$(MAKE) -C kernel ARCH=$(ARCH) BITS=$(BITS) CC=$(CC) LD=$(LD) AS=$(AS) AR=$(AR) MODORDER=$(PWD)/modorder LOGLEVEL=$(LOGLEVEL)
mv kernel/kernel.elf root/boot/
- mkdir -p $(PWD)/root/usr/include/modit
+ mkdir -p $(PWD)/root/include/modit
mkdir -p $(PWD)/initrd
cp kernel/include/arch_$(ARCH)/$(BITS)/* root/include/modit/
cp kernel/include/arch_$(ARCH)/common/* root/include/modit/
@@ -44,13 +46,13 @@ initrd: tools/$(ARCH)_$(BITS) apps Makefile
.PHONY: apps
apps: libs
@for app in ${APPS} ; do \
- $(MAKE) -C $$app ARCH=$(ARCH) BITS=$(BITS) CC=$(CC) LD=$(LD) AS=$(AS) AR=$(AR) INCDIR=$(PWD)/root/include LIBDIR=$(PWD)/root/lib OUTDIR=$(PWD)/initrd ; \
+ $(MAKE) -C $$app ARCH=$(ARCH) BITS=$(BITS) CC=$(CC) LD=$(LD) AS=$(AS) AR=$(AR) INCDIR=$(PWD)/root/include ROOTDIR=$(PWD)/root IRDDIR=$(PWD)/initrd ; \
done
.PHONY: libs
libs: kernel
@for lib in $(wildcard $(PWD)/lib/*) ; do \
- $(MAKE) -C $$lib ARCh=$(ARCH) BITS=$(BITS) CC=$(CC) LD=$(LD) AS=$(AS) AR=$(AR) INCDIR=$(PWD)/root/include LIBDIR=$(PWD)/root/lib ; \
+ $(MAKE) -C $$lib ARCh=$(ARCH) BITS=$(BITS) CC=$(CC) LD=$(LD) AS=$(AS) AR=$(AR) INCDIR=$(PWD)/root/include ROOTDIR=$(PWD)/root ; \
done
limine:
diff --git a/app/init/Makefile b/app/init/Makefile
index b8474b6..5dac7ba 100644
--- a/app/init/Makefile
+++ b/app/init/Makefile
@@ -4,9 +4,9 @@ CFILES += $(wildcard *.s)
CC ?= gcc
WARNS := -Wall -Wextra -Wduplicated-cond -Wduplicated-branches -Wlogical-op -Wrestrict -Wnull-dereference -Wdouble-promotion -Wshadow -Wcast-align
-CFLAGS := $(WARNS)
+CFLAGS := $(WARNS) -static
OUT := init
.PHONY: all
all: $(CFILES)
- $(CC) $(CFLAGS) $(CFILES) $(LIBDIR)/librpc.a -o $(OUTDIR)/init
+ $(CC) $(CFLAGS) $(CFILES) -o $(IRDDIR)/bin/init
diff --git a/app/init/ird.c b/app/init/ird.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/app/init/ird.c
diff --git a/app/init/main.c b/app/init/main.c
index 407959f..a72467a 100644
--- a/app/init/main.c
+++ b/app/init/main.c
@@ -1,5 +1,6 @@
#include "scheduler.h"
#include "rpcsrv.h"
+#include "spawner.h"
#include "sys/cid.h"
#include "sys/ipc.h"
@@ -14,10 +15,6 @@ void prints(const char *msg) {
_syscall_print(&data);
}
-intmax_t rpc_test(struct rpcargs *args) {
- return 0;
-}
-
int
main(int argc, char **argv)
{
@@ -26,11 +23,5 @@ main(int argc, char **argv)
rpcsrv_setup();
scheduler_setup();
- cid_t forked = fork_ghost();
- if(forked != 0) {
- scheduler_addctx(forked, "test", 1);
- }else{
- rpc_export(rpc_define(0, 0, "", 'i'), "test", rpc_test);
- }
- //rpc_handle(false);
+ rpc_handle(false);
}
diff --git a/app/init/rpcsrv.c b/app/init/rpcsrv.c
index 2808560..2faf843 100644
--- a/app/init/rpcsrv.c
+++ b/app/init/rpcsrv.c
@@ -1,4 +1,5 @@
#include "rpcsrv.h"
+#include "scheduler.h"
#include "rpc/rpc.h"
#include <stdlib.h>
#include <string.h>
@@ -34,24 +35,20 @@ rpcs_import(struct rpc_args *args)
rpc_returnvlv(NULL, 0);
}
-intmax_t
-rpcs_export(struct rpc_args *args)
-{
- char *name = rpc_getarg(args, char*);
- struct rpc_vlvpack *funcpk = rpc_getarg(args, struct rpc_vlvpack*);
-
- //Make sure rpcs aren't overwritten
+int
+rpcs_export_internal(const char *name, size_t funclen, RPCFUNC *func) {
RPCFUNC *exists = rpcs_find(name);
if(exists) return -1;
- //Expand srv_entries list, adding copy to list
+ //Make room for new rpc entry
struct rpcsrv_entry *tmp = malloc(sizeof(struct rpcsrv_entry) * (rpcc + 1));
- memcpy(tmp, srv_entries, sizeof(struct rpcsrv_entry) * rpcc);
+ if(srv_entries != NULL) memcpy(tmp, srv_entries, sizeof(struct rpcsrv_entry) * rpcc);
free(srv_entries);
srv_entries = tmp;
- exists = malloc(funcpk->len);
- memcpy(exists, funcpk->data, funcpk->len);
+ //Create persistent copy of func
+ exists = malloc(funclen);
+ memcpy(exists, func, funclen);
srv_entries[rpcc].func = exists;
srv_entries[rpcc].name = malloc(strlen(name));
@@ -59,6 +56,41 @@ rpcs_export(struct rpc_args *args)
rpcc++;
return rpcc - 1;
+
+}
+
+intmax_t
+rpcs_export(struct rpc_args *args)
+{
+ char *name = rpc_getarg(args, char*);
+ struct rpc_vlvpack *funcpk = rpc_getarg(args, struct rpc_vlvpack*);
+
+ return rpcs_export_internal(name, funcpk->len, (RPCFUNC*)funcpk->data);
+}
+
+intmax_t
+rpcs_scheduler_addctx(struct rpc_args *args)
+{
+ cid_t pntid = args->sender;
+ cid_t id = rpc_getarg(args, cid_t);
+
+ struct context *ctx = scheduler_findctx(root, pntid);
+ struct process *parent = ctx->parent;
+
+ return scheduler_addctx(parent->pid, id, 0);
+}
+
+intmax_t
+rpcs_scheduler_addproc(struct rpc_args *args)
+{
+ cid_t pntid = args->sender;
+ cid_t ctxid = rpc_getarg(args, cid_t);
+ char *name = rpc_getarg(args, char*);
+
+ struct context *ctx = scheduler_findctx(root, pntid);
+ struct process *parent = ctx->parent;
+
+ return scheduler_addproc(parent->pid, ctxid, name);
}
void
@@ -69,4 +101,14 @@ rpcsrv_setup(void)
rpc_export_internal(rpc_define(0, 0, "s", 'v'), rpcs_import);
rpc_export_internal(rpc_define(0, 1, "sv", 'i'), rpcs_export);
+
+ RPCFUNC *tmpfunc = rpc_define(0, 0, "i", 'i'); //register context (int[ctxid]) => int[success]
+ rpcs_export_internal("scheduler_addcontext", sizeof(RPCFUNC) + 3, tmpfunc);
+ rpc_export_internal(tmpfunc, rpcs_scheduler_addctx);
+ free(tmpfunc);
+
+ tmpfunc = rpc_define(0, 0, "is", 'i'); //register context (int[ctxid], str[arg0]) => int[success]
+ rpcs_export_internal("scheduler_addprocess", sizeof(RPCFUNC) + 3, tmpfunc);
+ rpc_export_internal(tmpfunc, rpcs_scheduler_addproc);
+ free(tmpfunc);
}
diff --git a/app/init/rpcsrv.h b/app/init/rpcsrv.h
index 8c899a7..a6ef330 100644
--- a/app/init/rpcsrv.h
+++ b/app/init/rpcsrv.h
@@ -1,6 +1,10 @@
#ifndef INIT_RPCSRV_H
#define INIT_RPCSRV_H 1
+#include "rpc/rpc.h"
+
+int rpcs_export_internal(const char *name, size_t funclen, RPCFUNC *func);
+
void rpcsrv_setup(void);
#endif
diff --git a/app/init/scheduler.c b/app/init/scheduler.c
index 1980ebb..90eae64 100644
--- a/app/init/scheduler.c
+++ b/app/init/scheduler.c
@@ -4,46 +4,97 @@
#include "sys/cid.h"
#include "rpc/rpc.h"
#include "rpc/api.h"
+#include "sys/lock.h"
+#include "rpcsrv.h"
#include <string.h>
-struct clnode { //Round list
- struct clnode *next;
- struct context *ctx;
-};
+struct process *root;
+static mutex_t proctree_mutex;
+static size_t nprocid = 1;
-struct clnode *context_tail = NULL;
-struct clnode *context_current = NULL;
+static struct clnode *list_tail = NULL;
+static struct clnode *list_current = NULL;
-static struct clnode*
-findbyid(cid_t id) {
- for(struct clnode *node = context_tail; node; node = node->next) {
- if(node->ctx->cid == id) return node;
+struct process *
+scheduler_findproc(struct process *base, pid_t id)
+{
+ if(base->pid == id) return base;
+ for(struct process *child = base->children; child; child = child->next) {
+ struct process *group = scheduler_findproc(child, id);
+ if(group != NULL) return group;
}
return NULL;
}
-static struct clnode*
-findbyname(char *name) {
- for(struct clnode *node = context_tail; node; node = node->next) {
- if(strcmp(node->ctx->name, name) == 0) return node;
+struct context*
+scheduler_findctx(struct process *base, cid_t id)
+{
+ for(struct context *ctx = base->contexts; ctx; ctx = ctx->next) {
+ if(ctx->cid == id) return ctx;
}
- return NULL;
+ for(struct process *child = base->children; child; child = child->next) {
+ struct context *ctx = scheduler_findctx(child, id);
+ if(ctx != NULL) return ctx;
+ }
+ return NULL;
}
-void
-scheduler_addctx(cid_t id, char *name, uint8_t p)
+int
+scheduler_addctx(pid_t pid, cid_t id, uint8_t p)
{
+ mutex_take(&proctree_mutex);
+ struct process *parent = scheduler_findproc(root, pid);
+ if(scheduler_findctx(root, id) != NULL) {
+ mutex_leave(&proctree_mutex);
+ return -1;
+ }
+ if(parent == NULL) parent = root;
+
+ struct context *new = malloc(sizeof(struct context));
+ new->parent = parent;
+ new->next = parent->contexts;
+ new->cid = id;
+ new->priority = p;
+
struct clnode *node = malloc(sizeof(struct clnode));
- struct context *ctx = malloc(sizeof(struct context) + strlen(name));
+ node->ctx = new;
+ node->next = list_tail;
- ctx->cid = id;
- ctx->priority = p;
- ctx->scheduled = true;
- strcpy(ctx->name, name);
+ list_tail = node;
+ parent->contexts = new;
+ mutex_leave(&proctree_mutex);
- node->ctx = ctx;
- node->next = context_tail;
- context_tail = node;
+ return 0;
+}
+
+int
+scheduler_addproc(pid_t pntid, cid_t cid, const char *name)
+{
+ mutex_take(&proctree_mutex);
+ struct process *parent = scheduler_findproc(root, pntid);
+ if(scheduler_findctx(root, cid) != NULL) {
+ mutex_leave(&proctree_mutex);
+ return -1;
+ }
+ if(parent == NULL) parent = root;
+
+ struct process *new = malloc(sizeof(struct process) + strlen(name));
+ new->pid = nprocid++;
+ new->children = NULL;
+ strcpy(new->name, name);
+
+ new->next = parent->children;
+ parent->children = new;
+ mutex_leave(&proctree_mutex);
+
+ new->contexts = malloc(sizeof(struct context));
+ new->contexts->cid = cid;
+ new->contexts->parent = new;
+ new->contexts->next = NULL;
+ new->contexts->scheduled = true;
+ new->contexts->priority = 0;
+
+ return 0;
}
void
@@ -51,33 +102,54 @@ scheduler_loop(void)
{
settimer(20);
swapto(1);
- while(1)
- {
- bool ready = false;
- while(!ready) {
- struct clnode *next = context_current->next;
- if(next == 0) next = context_tail;
- context_current = next;
- if(context_current->ctx->scheduled == false) continue;
- ready = true;
+ while(1) {
+ for(struct clnode *node = list_tail; node; node = node->next) {
+ struct context *ctx = node->ctx;
+ if(!ctx->scheduled) continue;
+ list_current = node;
+
+ settimer(PROC_QUANTUM(ctx->priority));
+ swapto(ctx->cid);
}
- settimer(PROC_QUANTUM(context_current->ctx->priority));
- swapto(context_current->ctx->cid);
+ //Sort by priority
}
}
void
scheduler_setup(void)
{
+ const char *initname = "/bin/init";
+ root = malloc(sizeof(struct process) + strlen(initname));
+ root->pid = nprocid++;
+ root->next = NULL;
+ root->contexts = NULL;
+ root->children = NULL;
+ strcpy(root->name, initname);
+
+ root->contexts = malloc(sizeof(struct context));
+ root->contexts->parent = root;
+ root->contexts->cid = 1;
+ root->contexts->scheduled = true;
+ root->contexts->priority = 0;
+
cid_t scid = fork_ghost();
if(scid != 0) {
swapto(scid);
return;
}
+
+ root->contexts->next = malloc(sizeof(struct context));
+ root->contexts->next->next = root->contexts;
+
+ root->contexts = root->contexts->next;
+ root->contexts->next->next = NULL;
+ root->contexts->cid = scid;
+ root->contexts->scheduled = false;
+ root->contexts->priority = 0;
- //Add init and scheduler to context list
- scheduler_addctx(1, "init", 1);
+ list_tail = list_current = malloc(sizeof(struct clnode));
+ list_tail->ctx = root->contexts->next;
+ list_tail->next = NULL;
- context_current = context_tail;
scheduler_loop();
}
diff --git a/app/init/scheduler.h b/app/init/scheduler.h
index 478f32b..559fb56 100644
--- a/app/init/scheduler.h
+++ b/app/init/scheduler.h
@@ -6,18 +6,41 @@
#include <stdbool.h>
#include <stdint.h>
+#include <moditstd.h>
#include "sys/cid.h"
struct context
{
+ struct process *parent;
+ struct context *next;
cid_t cid;
bool scheduled;
uint8_t priority;
+};
+
+struct process {
+ pid_t pid;
+ struct process *next;
+
+ struct context *contexts;
+ struct process *children;
+
char name[];
};
-void scheduler_addctx(cid_t id, char *name, uint8_t p);
+struct clnode { //Round list
+ struct clnode *next;
+ struct context *ctx;
+};
+
+extern struct process *root;
+
+struct process *scheduler_findproc(struct process *base, pid_t id);
+struct context *scheduler_findctx(struct process *base, cid_t id);
+
+int scheduler_addctx(pid_t pid, cid_t id, uint8_t p);
+int scheduler_addproc(pid_t pntid, cid_t id, const char *name);
/* Assumes init is CID 1 */
void scheduler_setup(void);
diff --git a/app/ld/Makefile b/app/ld/Makefile
index 3a755c2..17323c0 100644
--- a/app/ld/Makefile
+++ b/app/ld/Makefile
@@ -8,4 +8,4 @@ OUT := ld
.PHONY: all
all: $(CFILES)
- $(CC) $(CFLAGS) $(CFILES) -o $(OUTDIR)/ld
+ $(CC) $(CFLAGS) $(CFILES) -o $(IRDDIR)/bin/ld
diff --git a/kernel b/kernel
-Subproject a310ca40440e4798eea01107646b92253a3f788
+Subproject 5943a515fdda7b2c148122e779aed16312be7cb
diff --git a/lib/c/Makefile b/lib/c/Makefile
index a81e0e7..dc3c774 100644
--- a/lib/c/Makefile
+++ b/lib/c/Makefile
@@ -11,7 +11,7 @@ CFLAGS := $(WARNS) -I$(INCDIR)
.PHONY: all
all: ${OBJFILES}
- $(AR) rcs $(LIBDIR)/libc.a ${OBJFILES}
+ $(AR) rcs $(ROOTDIR)/lib/libc.a ${OBJFILES}
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
diff --git a/lib/c/stdlib/heap.c b/lib/c/stdlib/heap.c
index a535e40..e1e2ea9 100644
--- a/lib/c/stdlib/heap.c
+++ b/lib/c/stdlib/heap.c
@@ -1,5 +1,6 @@
#include <stdlib.h>
#include "sys/alloc.h"
+#include "sys/lock.h"
#include <stdint.h>
@@ -28,6 +29,8 @@ static struct bucket *bucket_head = NULL;
static struct bucket *free_tail = NULL;
static uintptr_t heapend;
+static mutex_t heap_lock;
+
uintptr_t __pbrk;
extern uintptr_t _end[];
@@ -127,6 +130,8 @@ void*
malloc(size_t size)
{
if(size == 0) return NULL;
+ mutex_take(&heap_lock);
+
size += (size % 2);
//Search blocks for empty buckets matching size
@@ -146,6 +151,7 @@ malloc(size_t size)
//Claim bucket
bucket->flags |= 1;
+ mutex_leave(&heap_lock);
return (void*)bucket->addr;
}
@@ -153,13 +159,16 @@ void
free(void *ptr)
{
if(ptr == NULL) return;
+ mutex_take(&heap_lock);
//Search blocklist for address at token
for(struct bucket *bucket = bucket_tail; bucket; bucket = bucket->next) {
if(bucket->addr == (uintptr_t)ptr) {
bucket->flags &= (~1);
+ mutex_leave(&heap_lock);
return;
}
}
+ mutex_leave(&heap_lock);
}
void
diff --git a/lib/c/sys/getcid.c b/lib/c/sys/getcid.c
deleted file mode 100644
index 8650504..0000000
--- a/lib/c/sys/getcid.c
+++ /dev/null
@@ -1,10 +0,0 @@
-#include "sys/cid.h"
-#include "modit/syscall.h"
-
-DEFN_SYSCALL(cid, SYSCALL_CID)
-
-cid_t
-getcid(void)
-{
- return _syscall_cid(NULL);
-}
diff --git a/lib/crt/Makefile b/lib/crt/Makefile
index aae76be..185627a 100644
--- a/lib/crt/Makefile
+++ b/lib/crt/Makefile
@@ -6,7 +6,7 @@ CC ?= gcc
.PHONY: all
all:
- $(CC) $(CFLAGS) -c $(ARCH)/$(BITS)/crt0.s -o $(LIBDIR)/crt0.o
- $(CC) $(CFLAGS) -c $(ARCH)/$(BITS)/crtn.s -o $(LIBDIR)/crtn.o
- $(CC) $(CFLAGS) -c $(ARCH)/$(BITS)/crti.s -o $(LIBDIR)/crti.o
+ $(CC) $(CFLAGS) -c $(ARCH)/$(BITS)/crt0.s -o $(ROOTDIR)/lib/crt0.o
+ $(CC) $(CFLAGS) -c $(ARCH)/$(BITS)/crtn.s -o $(ROOTDIR)/lib/crtn.o
+ $(CC) $(CFLAGS) -c $(ARCH)/$(BITS)/crti.s -o $(ROOTDIR)/lib/crti.o
diff --git a/lib/modit/Makefile b/lib/modit/Makefile
new file mode 100644
index 0000000..8c09b26
--- /dev/null
+++ b/lib/modit/Makefile
@@ -0,0 +1,17 @@
+CFILES := $(wildcard *.c)
+CFILES += $(wildcard */*.c)
+
+OBJFILES := $(patsubst %.c,%.o,$(CFILES))
+
+CC ?= gcc
+AR ?= ar
+
+WARNS := -Wall -Wextra -Wduplicated-cond -Wduplicated-branches -Wlogical-op -Wrestrict -Wnull-dereference -Wdouble-promotion -Wshadow -Wcast-align
+CFLAGS := $(WARNS) -I$(INCDIR)
+
+.PHONY: all
+all: ${OBJFILES}
+ $(AR) rcs $(ROOTDIR)/lib/libmodit.a ${OBJFILES}
+
+%.o:%.c
+ $(CC) $(CFLAGS) -c $< -o $@
diff --git a/lib/c/sys/fork.c b/lib/modit/cid/fork.c
index 6792fe7..6792fe7 100644
--- a/lib/c/sys/fork.c
+++ b/lib/modit/cid/fork.c
diff --git a/lib/modit/cid/getcid.c b/lib/modit/cid/getcid.c
new file mode 100644
index 0000000..09186ae
--- /dev/null
+++ b/lib/modit/cid/getcid.c
@@ -0,0 +1,18 @@
+#include "sys/cid.h"
+#include "modit/syscall.h"
+
+DEFN_SYSCALL(cid, SYSCALL_CID)
+
+cid_t
+getcid(void)
+{
+ struct syscall_cid_data data = { .id = 0 };
+ return _syscall_cid(&data);
+}
+
+cid_t
+cidexists(cid_t id)
+{
+ struct syscall_cid_data data = { .id = id };
+ return _syscall_cid(&data);
+}
diff --git a/lib/c/sys/settimer.c b/lib/modit/cid/settimer.c
index 5dbb853..5dbb853 100644
--- a/lib/c/sys/settimer.c
+++ b/lib/modit/cid/settimer.c
diff --git a/lib/c/sys/swapto.c b/lib/modit/cid/swapto.c
index a0b4181..a0b4181 100644
--- a/lib/c/sys/swapto.c
+++ b/lib/modit/cid/swapto.c
diff --git a/lib/c/sys/msg_get.c b/lib/modit/ipc/msg_get.c
index 6410293..6410293 100644
--- a/lib/c/sys/msg_get.c
+++ b/lib/modit/ipc/msg_get.c
diff --git a/lib/c/sys/msg_recv.c b/lib/modit/ipc/msg_recv.c
index 27dcf55..27dcf55 100644
--- a/lib/c/sys/msg_recv.c
+++ b/lib/modit/ipc/msg_recv.c
diff --git a/lib/c/sys/msg_send.c b/lib/modit/ipc/msg_send.c
index 65b5336..65b5336 100644
--- a/lib/c/sys/msg_send.c
+++ b/lib/modit/ipc/msg_send.c
diff --git a/lib/c/sys/port.c b/lib/modit/ipc/port.c
index 8fe5cc6..8fe5cc6 100644
--- a/lib/c/sys/port.c
+++ b/lib/modit/ipc/port.c
diff --git a/lib/modit/lock/mutex.c b/lib/modit/lock/mutex.c
new file mode 100644
index 0000000..c80e6fe
--- /dev/null
+++ b/lib/modit/lock/mutex.c
@@ -0,0 +1,14 @@
+#include "sys/lock.h"
+#include "sys/cid.h"
+
+void
+mutex_take(mutex_t *mutex)
+{
+ while(!__sync_bool_compare_and_swap(mutex, 0, 1)) swapto(2);
+}
+
+void
+mutex_leave(mutex_t *mutex)
+{
+ *mutex = 0;
+}
diff --git a/lib/c/sys/alloc.c b/lib/modit/mem/alloc.c
index 4212026..4212026 100644
--- a/lib/c/sys/alloc.c
+++ b/lib/modit/mem/alloc.c
diff --git a/lib/rpc/Makefile b/lib/rpc/Makefile
index 76df3d1..84cece2 100644
--- a/lib/rpc/Makefile
+++ b/lib/rpc/Makefile
@@ -11,7 +11,7 @@ CFLAGS := $(WARNS) -I$(INCDIR)
.PHONY: all
all: ${OBJFILES}
- $(AR) rcs $(LIBDIR)/librpc.a ${OBJFILES}
+ $(AR) rcs $(ROOTDIR)/lib/librpc.a ${OBJFILES}
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
diff --git a/lib/rpc/fork.c b/lib/rpc/fork.c
new file mode 100644
index 0000000..62a1389
--- /dev/null
+++ b/lib/rpc/fork.c
@@ -0,0 +1,15 @@
+#include "sys/cid.h"
+#include "rpc/api.h"
+
+RPC_IMPORT(scheduler_addctx)
+
+cid_t
+fork(void)
+{
+ cid_t new = fork_ghost();
+ if(new != 0 ) {
+ RPC_CALL(scheduler_addctx, new);
+ }
+
+ return new;
+}
diff --git a/lib/rpc/handle.c b/lib/rpc/handle.c
index 57a85cd..d82ca19 100644
--- a/lib/rpc/handle.c
+++ b/lib/rpc/handle.c
@@ -33,19 +33,18 @@ rpc_handle_return(MESSAGE *msg, RPCFUNC *func, intmax_t r)
void
rpc_handle(bool async)
{
- if(async) {
- //TODO: Create un-ghost fork and call that to make call async
- }
+ (void)async;
struct rpc_args *args = malloc(sizeof(struct rpc_args));
while(1) {
MESSAGE *msg = msg_recv();
struct rpc_message_packet *pkt = (struct rpc_message_packet*)msg_getdata(msg);
-
+
for(rpcid_t i = 0; i < rpcc; i++) {
struct rpc_handle *handle = &rpc_handles[i];
if(handle->func->id == pkt->id) {
args->func = handle->func;
+ args->sender = msg_getsender(msg);
args->arg = 0;
args->data = pkt->data;
diff --git a/lib/rpc/rpc.c b/lib/rpc/rpc.c
index e5ac718..e637d64 100644
--- a/lib/rpc/rpc.c
+++ b/lib/rpc/rpc.c
@@ -7,14 +7,10 @@ rpcid_t rpcc = 0;
RPCFUNC *rpc_init_import;
RPCFUNC *rpc_init_export;
-RPCFUNC *rpc_init_regc;
__attribute__((constructor))
void rpc_constructor(void)
{
- prints("Registered RPCs\n");
-
rpc_init_import = rpc_define(1, RPC_INIT_IMPORT, "s", 'v');
rpc_init_export = rpc_define(1, RPC_INIT_EXPORT, "sv", 'i');
- rpc_init_regc = rpc_define(1, RPC_INIT_REGC, "is", "i");
}
diff --git a/root/include/moditstd.h b/root/include/moditstd.h
new file mode 100644
index 0000000..68d5ddb
--- /dev/null
+++ b/root/include/moditstd.h
@@ -0,0 +1,18 @@
+#ifndef MODITSTD_H
+#define MODITSTD_H 1
+
+/* Stores definitions and functions that use all system libraries
+ * to create expexted behavior.
+ * Also stores definitions used by all system libraries
+ * */
+
+#include "sys/cid.h"
+typedef cid_t pid_t;
+
+/* Creates a copy of the current context
+ * Duplicates the user and kernel stack
+ * Calls scheduler RPC function to register new context with scheduler
+ * */
+cid_t fork(void);
+
+#endif
diff --git a/root/include/rpc/api.h b/root/include/rpc/api.h
index 7b4913e..21262b9 100644
--- a/root/include/rpc/api.h
+++ b/root/include/rpc/api.h
@@ -19,13 +19,13 @@ intmax_t rpc_func_##name (bool wait, ...) { \
va_list ap; \
va_start(ap, wait); \
/*TODO: Ignore result if wait is false */ \
- intmax_t r = rpc_call(rpc_##name, ap); \
+ intmax_t r = rpc_vcall(rpc_##name, ap); \
va_end(ap); \
return r; \
}
#define RPC_CALL(name, ...) \
- rpc_func_##name(false, __VA_ARGS__);
+ rpc_func_##name(false, __VA_ARGS__)
extern RPCFUNC *rpc_init_import;
extern RPCFUNC *rpc_init_export;
diff --git a/root/include/rpc/rpc.h b/root/include/rpc/rpc.h
index 509b243..5b2489e 100644
--- a/root/include/rpc/rpc.h
+++ b/root/include/rpc/rpc.h
@@ -30,6 +30,7 @@ typedef struct rpc_function {
struct rpc_args {
RPCFUNC *func;
+ cid_t sender;
int arg;
void *data;
};
diff --git a/root/include/string.h b/root/include/string.h
index 2e7f763..75ede23 100644
--- a/root/include/string.h
+++ b/root/include/string.h
@@ -4,6 +4,8 @@
#include <stddef.h>
size_t strlen(const char *s);
+int strcmp(const char *a, const char *b);
+
char *strcpy(char *restrict dest, const char *src);
#endif
diff --git a/root/include/sys/cid.h b/root/include/sys/cid.h
index af7899e..36b61fc 100644
--- a/root/include/sys/cid.h
+++ b/root/include/sys/cid.h
@@ -4,8 +4,11 @@
typedef int cid_t;
cid_t fork_ghost(void);
+cid_t fork(void);
cid_t getcid(void);
+cid_t cidexists(cid_t id);
+
void settimer(int ms);
int swapto(cid_t target);
diff --git a/root/include/sys/lock.h b/root/include/sys/lock.h
new file mode 100644
index 0000000..a444049
--- /dev/null
+++ b/root/include/sys/lock.h
@@ -0,0 +1,13 @@
+#ifndef SYS_LOCK_H
+#define SYS_LOCK_H
+
+/* Defines different types of mutual exclusion types and their
+ * respective functions
+ * */
+
+typedef volatile int mutex_t;
+
+void mutex_take(mutex_t *mutex);
+void mutex_leave(mutex_t *mutex);
+
+#endif
diff --git a/root/include/sys/port.h b/root/include/sys/port.h
deleted file mode 100644
index 45ee787..0000000
--- a/root/include/sys/port.h
+++ /dev/null
@@ -1,6 +0,0 @@
-#ifndef SYS_PORT_H
-#define SYS_PORT_H 1
-
-
-
-#endif
diff --git a/tools/patches/gcc.patch b/tools/patches/gcc.patch
index e63fe17..8f0a2ef 100644
--- a/tools/patches/gcc.patch
+++ b/tools/patches/gcc.patch
@@ -43,7 +43,7 @@ diff -rcN gcc/gcc/config/modit.h gcc-modified/gcc/config/modit.h
+ /* Default arguments you want when running your
+ * i686-modit-gcc/x86_64-modit-gcc toolchain */
+ #undef LIB_SPEC
-+ #define LIB_SPEC "-lc -lrpc" /* link against c and rpc library */
++ #define LIB_SPEC "-lrpc -lc -lmodit" /* link against c and rpc library */
+
+ #undef STARTFILE_SPEC
+ #define STARTFILE_SPEC "crt0.o%s crti.o%s crtbegin.o%s"