summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorIan McFarlane <i.mcfarlane2002@gmail.com>2025-05-02 00:37:08 -0400
committerIan McFarlane <i.mcfarlane2002@gmail.com>2025-05-02 00:37:08 -0400
commit74c2b180769ef14ad42fe666207271bf344b11b9 (patch)
tree1957f4cdccc46b440898032f79350dfb113b19e4 /kernel
parentfmt (diff)
downloadcomus-74c2b180769ef14ad42fe666207271bf344b11b9.tar.gz
comus-74c2b180769ef14ad42fe666207271bf344b11b9.tar.bz2
comus-74c2b180769ef14ad42fe666207271bf344b11b9.zip
Add allocshared() and popsharedmem() syscalls
Diffstat (limited to 'kernel')
-rw-r--r--kernel/include/comus/procs.h5
-rw-r--r--kernel/include/comus/syscalls.h4
-rw-r--r--kernel/syscall.c105
3 files changed, 103 insertions, 11 deletions
diff --git a/kernel/include/comus/procs.h b/kernel/include/comus/procs.h
index 7b1a70a..717f27a 100644
--- a/kernel/include/comus/procs.h
+++ b/kernel/include/comus/procs.h
@@ -71,6 +71,11 @@ struct pcb {
uint64_t syscall;
uint64_t wakeup;
uint8_t exit_status;
+
+ // pipe to check for shared memory
+ void* shared_mem;
+ size_t shared_mem_pages;
+ pid_t shared_mem_source;
};
/// ordering of pcb queues
diff --git a/kernel/include/comus/syscalls.h b/kernel/include/comus/syscalls.h
index f714184..f27b879 100644
--- a/kernel/include/comus/syscalls.h
+++ b/kernel/include/comus/syscalls.h
@@ -30,9 +30,11 @@
#define SYS_poweroff 17
#define SYS_drm 18
#define SYS_ticks 19
+#define SYS_allocshared 20
+#define SYS_popsharedmem 21
// UPDATE THIS DEFINITION IF MORE SYSCALLS ARE ADDED!
-#define N_SYSCALLS 20
+#define N_SYSCALLS 22
// interrupt vector entry for system calls
#define VEC_SYSCALL 0x80
diff --git a/kernel/syscall.c b/kernel/syscall.c
index 96d2fcf..2b98ec1 100644
--- a/kernel/syscall.c
+++ b/kernel/syscall.c
@@ -324,17 +324,102 @@ static int sys_ticks(void)
return 0;
}
+static int sys_popsharedmem(void)
+{
+ RET(void *, res_mem);
+ *res_mem = NULL;
+
+ if (pcb->shared_mem == NULL) {
+ return 1;
+ }
+
+ struct pcb *const sharer = pcb_find_pid(pcb->shared_mem_source);
+ if (sharer == NULL) {
+ // process died or something since sharing
+ pcb->shared_mem = NULL;
+ return 1;
+ }
+
+ void *result =
+ mem_mapaddr(pcb->memctx, mem_get_phys(sharer->memctx, pcb->shared_mem),
+ pcb->shared_mem, pcb->shared_mem_pages * PAGE_SIZE,
+ F_WRITEABLE | F_UNPRIVILEGED);
+
+ // if (!result) {
+ // alert the other process that we cannot get its allocation?
+ // mem_free_pages(pcb->memctx, alloced);
+ // return 1;
+ // }
+
+ *res_mem = result;
+
+ pcb->shared_mem = NULL;
+
+ return 0;
+}
+
+static int sys_allocshared(void)
+{
+ ARG1(size_t, num_pages);
+ ARG2(unsigned short, otherpid); // same as pid_t
+ RET(void *, res_mem);
+ *res_mem = NULL;
+ assert(sizeof(unsigned short) == sizeof(pid_t),
+ "out of date sys_memshare syscall, pid_t changed?");
+
+ if (otherpid == pcb->pid || otherpid == 0) {
+ return 1;
+ }
+
+ struct pcb *const otherpcb = pcb_find_pid(otherpid);
+ if (otherpcb == NULL) {
+ // no such target process exists
+ return 1;
+ }
+ if (otherpcb->shared_mem != NULL) {
+ // it has yet to consume the last allocshared given to it
+ return 1;
+ }
+
+ void *alloced =
+ mem_alloc_pages(pcb->memctx, num_pages, F_WRITEABLE | F_UNPRIVILEGED);
+
+ if (!alloced) {
+ return 1;
+ }
+
+ otherpcb->shared_mem = alloced;
+ otherpcb->shared_mem_source = pcb->pid;
+ otherpcb->shared_mem_pages = num_pages;
+
+ *res_mem = alloced;
+
+ return 0;
+}
+
static int (*syscall_tbl[N_SYSCALLS])(void) = {
- [SYS_exit] = sys_exit, [SYS_waitpid] = sys_waitpid,
- [SYS_fork] = sys_fork, [SYS_exec] = NULL,
- [SYS_open] = NULL, [SYS_close] = NULL,
- [SYS_read] = NULL, [SYS_write] = sys_write,
- [SYS_getpid] = sys_getpid, [SYS_getppid] = sys_getppid,
- [SYS_gettime] = sys_gettime, [SYS_getprio] = sys_getprio,
- [SYS_setprio] = sys_setprio, [SYS_kill] = sys_kill,
- [SYS_sleep] = sys_sleep, [SYS_brk] = sys_brk,
- [SYS_sbrk] = sys_sbrk, [SYS_poweroff] = sys_poweroff,
- [SYS_drm] = sys_drm, [SYS_ticks] = sys_ticks,
+ [SYS_exit] = sys_exit,
+ [SYS_waitpid] = sys_waitpid,
+ [SYS_fork] = sys_fork,
+ [SYS_exec] = NULL,
+ [SYS_open] = NULL,
+ [SYS_close] = NULL,
+ [SYS_read] = NULL,
+ [SYS_write] = sys_write,
+ [SYS_getpid] = sys_getpid,
+ [SYS_getppid] = sys_getppid,
+ [SYS_gettime] = sys_gettime,
+ [SYS_getprio] = sys_getprio,
+ [SYS_setprio] = sys_setprio,
+ [SYS_kill] = sys_kill,
+ [SYS_sleep] = sys_sleep,
+ [SYS_brk] = sys_brk,
+ [SYS_sbrk] = sys_sbrk,
+ [SYS_poweroff] = sys_poweroff,
+ [SYS_drm] = sys_drm,
+ [SYS_ticks] = sys_ticks,
+ [SYS_allocshared] = sys_allocshared,
+ [SYS_popsharedmem] = sys_popsharedmem,
};
void syscall_handler(void)