summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--arch/x86_64/hal/idt.c1
-rw-r--r--arch/x86_64/hal/obj.c6
-rw-r--r--arch/x86_64/hal/paging.c8
-rw-r--r--arch/x86_64/hal/process.c77
-rw-r--r--arch/x86_64/hal/smp.c15
-rw-r--r--arch/x86_64/hal/smp.h4
-rw-r--r--arch/x86_64/main.c4
-rw-r--r--arch/x86_64/panic/panic.c45
-rw-r--r--arch/x86_64/proc/init.c (renamed from arch/x86_64/sys/init.c)10
-rw-r--r--arch/x86_64/proc/process.c64
-rw-r--r--arch/x86_64/proc/process.h (renamed from arch/x86_64/hal/process.h)6
-rw-r--r--arch/x86_64/sys/syscall.c2
-rw-r--r--boot/limine/memorymap.c2
-rw-r--r--config.h2
-rw-r--r--hal/smp.h1
-rw-r--r--lib/panic.c18
-rw-r--r--lib/panic.h8
-rw-r--r--lib/rbtree.h17
-rw-r--r--link/x86_64-jove.ld (renamed from link/x86_64-elf.ld)0
-rw-r--r--panic/panic.c40
-rw-r--r--panic/panic.h16
-rw-r--r--proc/process.c8
-rw-r--r--proc/process.h (renamed from hal/process.h)11
-rw-r--r--sys/link.c23
-rw-r--r--sys/link.h6
-rw-r--r--sys/syscall.c23
-rw-r--r--sys/syscall_objdir.c2
-rw-r--r--sys/syscall_pagedir.c27
-rw-r--r--sys/syscall_untyped.c23
30 files changed, 303 insertions, 168 deletions
diff --git a/Makefile b/Makefile
index 99f625d..4d57c9f 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
include $(CONFIG)
include config.mk
-CDIRS := boot lib hal sys api
+CDIRS := boot lib hal proc sys api panic
CDIRS += boot/$(TARGET_BOOTLOADER)
CFILES := $(wildcard *.c)
diff --git a/arch/x86_64/hal/idt.c b/arch/x86_64/hal/idt.c
index 235d8c6..ff7a8a0 100644
--- a/arch/x86_64/hal/idt.c
+++ b/arch/x86_64/hal/idt.c
@@ -4,6 +4,7 @@
#include "jove.h"
#include "lib/printf.h"
#include "lib/string.h"
+#include "panic/panic.h"
ALIGN(8)
static struct idt_entry s_idt[IDT_ENTRIES];
diff --git a/arch/x86_64/hal/obj.c b/arch/x86_64/hal/obj.c
index 9844a54..103b53d 100644
--- a/arch/x86_64/hal/obj.c
+++ b/arch/x86_64/hal/obj.c
@@ -97,9 +97,6 @@ objdir_getlocal(
jove_owned_obj_t *objdir_obj,
uint16_t entry)
{
- while(obj_gettype(objdir_obj) == JOVE_OBJ_LINK) {
- objdir_obj = link_deref(objdir_obj);
- }
if(obj_gettype(objdir_obj) != JOVE_OBJ_OBJDIR) {
reterr(EBADOBJ);
}
@@ -116,9 +113,6 @@ objdir_trygetlocal(
jove_owned_obj_t *objdir_obj,
uint16_t entry)
{
- while(obj_gettype(objdir_obj) == JOVE_OBJ_LINK) {
- objdir_obj = link_deref(objdir_obj);
- }
if(obj_gettype(objdir_obj) != JOVE_OBJ_OBJDIR) {
reterr(EBADOBJ);
}
diff --git a/arch/x86_64/hal/paging.c b/arch/x86_64/hal/paging.c
index 2fbf26c..770cdff 100644
--- a/arch/x86_64/hal/paging.c
+++ b/arch/x86_64/hal/paging.c
@@ -7,7 +7,7 @@
#include "hal/obj.h"
#include "lib/string.h"
#include "lib/printf.h"
-#include "lib/panic.h"
+#include "panic/panic.h"
#include "idt.h"
#include "api/syscall.h"
@@ -69,11 +69,7 @@ s_int_handle_pf_early(struct int_stackframe *frame)
uintptr_t cr2 = 0;
__asm__ volatile("movq %%cr2, %0": "=r"(cr2));
- kprintf("Faulting address: %p\n", cr2);
- kprintf("RIP: %p | RSP: %p\n", frame->int_rip, frame->int_rsp);
- print_state(&frame->state);
-
- hcf();
+ kpanic_istate(frame, "Faulting address: %p\n", cr2);
}
static page_t*
diff --git a/arch/x86_64/hal/process.c b/arch/x86_64/hal/process.c
deleted file mode 100644
index aa51f45..0000000
--- a/arch/x86_64/hal/process.c
+++ /dev/null
@@ -1,77 +0,0 @@
-#include "process.h"
-#include "arch/x86_64/hal/obj.h"
-#include "hal/obj.h"
-#include "hal/process.h"
-#include "hal/smp.h"
-#include "lib/string.h"
-#include "init.h"
-#include "smp.h"
-#include "paging.h"
-#include "jove.h"
-
-PAGEALIGN uint8_t s_init_iopb[0x2000];
-PAGEALIGN char s_init_pcb_bytes[0x1000] = { 0 };
-static pcb_t *s_init_pcb = (pcb_t*)&s_init_pcb_bytes;
-
-jove_owned_obj_t*
-process_current(void)
-{
- return obj_take(((processor_t*)processor_current())->pcb_obj);
-}
-
-pcb_t*
-pcb_from_obj(jove_owned_obj_t *obj)
-{
- if(obj_gettype(obj) != JOVE_OBJ_PCB) {
- reterr(EBADOBJ);
- }
- return obj_deref(obj);
-}
-
-pid_t
-pcb_pid(pcb_t *pcb)
-{
- return pcb->pid;
-}
-
-jove_obj_t*
-pcb_rootdir(pcb_t *pcb)
-{
- return pcb->rootdir;
-}
-
-void
-processing_setup(void (*process_main))
-{
- memset(s_init_iopb, 0xFF, 0x2000);
- uintptr_t ksp = ((uintptr_t)&s_init_pcb_bytes) + 0xFF8;
-
- s_init_pcb->pid = 1;
- s_init_pcb->ksp = (void*)ksp;
- s_init_pcb->pml4 = paging_get_pml4();
- s_init_pcb->rootdir = _rootdir_obj;
-
- jove_owned_obj_t *iopb_obj = objdir_getlocal(_rootdir_obj, ROOTDIR_IOPB_I);
- obj_settype(iopb_obj, JOVE_OBJ_IOPB);
- obj_setvalue(iopb_obj, (uintptr_t)s_init_iopb);
- s_init_pcb->iopb = iopb_obj;
-
- processor_t *proc = processor_current();
- proc->tss.rsp[0] = (uintptr_t)s_init_pcb->ksp;
- memcpy(proc->iopb, s_init_iopb, 0x2000);
-
- {
- jove_owned_obj_t *process_obj = objdir_getlocal(_rootdir_obj, ROOTDIR_PCB_I);
- obj_settype(process_obj, JOVE_OBJ_PCB);
- obj_setvalue(process_obj, (uintptr_t)s_init_pcb);
-
- proc->pcb_obj = obj_release(process_obj);
- proc->pcb = s_init_pcb;
- }
-
- __asm__ volatile("\
- movq %0, %%rsp; \
- movq $0, %%rbp; \
- jmpq %1"::
- "r"(s_init_pcb->ksp), "r"(process_main));
-}
diff --git a/arch/x86_64/hal/smp.c b/arch/x86_64/hal/smp.c
index f67ce0b..4c79d79 100644
--- a/arch/x86_64/hal/smp.c
+++ b/arch/x86_64/hal/smp.c
@@ -1,13 +1,13 @@
#include "smp.h"
-#include "hal/process.h"
#include "init.h"
-#include "hal/obj.h"
#include "config.h"
#include "jove.h"
-#include "lib/printf.h"
-#include "lib/string.h"
#include "paging.h"
#include "msr.h"
+#include "hal/obj.h"
+#include "proc/process.h"
+#include "lib/printf.h"
+#include "lib/string.h"
processor_t s_processors[SMP_PROCESSORS_MAX];
@@ -54,7 +54,7 @@ smp_init_bsp(void)
obj_release(processor_objdir_obj);
}
- //idt_init();
+ idt_init();
paging_pml4_init();
}
@@ -62,3 +62,8 @@ processor_t *processor_current(void)
{
return (processor_t*)msr_get_gs_base();
}
+
+jove_owned_obj_t *processor_pcb(processor_t *processor)
+{
+ return processor->pcb_obj;
+}
diff --git a/arch/x86_64/hal/smp.h b/arch/x86_64/hal/smp.h
index b208e92..b26d4ac 100644
--- a/arch/x86_64/hal/smp.h
+++ b/arch/x86_64/hal/smp.h
@@ -1,8 +1,8 @@
#ifndef _JOVE_x86_64_HAL_SMP_H
#define _JOVE_x86_64_HAL_SMP_H 1
+#include "arch/x86_64/proc/process.h"
#include "gdt.h"
-#include "process.h"
#include "hal/obj.h"
#include "hal/smp.h"
@@ -12,7 +12,7 @@ typedef struct processor
struct tss tss;
uint8_t iopb[0x2000];
- jove_obj_t *pcb_obj;
+ jove_owned_obj_t *pcb_obj;
pcb_t *pcb;
} processor_t;
diff --git a/arch/x86_64/main.c b/arch/x86_64/main.c
index 644f154..f5a9d39 100644
--- a/arch/x86_64/main.c
+++ b/arch/x86_64/main.c
@@ -4,14 +4,14 @@
#include "lib/string.h"
#include "lib/printf.h"
#include "lib/virtterm.h"
-#include "lib/panic.h"
+#include "panic/panic.h"
#include "../../hal/obj.h"
#include "hal/msr.h"
#include "hal/serial_com.h"
#include "hal/smp.h"
#include "hal/memory.h"
#include "hal/framebuffer.h"
-#include "hal/process.h"
+#include "proc/process.h"
#include "boot/initrd.h"
#include "boot/cmdline.h"
diff --git a/arch/x86_64/panic/panic.c b/arch/x86_64/panic/panic.c
new file mode 100644
index 0000000..a19f807
--- /dev/null
+++ b/arch/x86_64/panic/panic.c
@@ -0,0 +1,45 @@
+#include "panic/panic.h"
+#include "arch/x86_64/hal/idt.h"
+#include "arch/x86_64/x86_64.h"
+#include "arch/x86_64/hal/paging.h"
+#include "lib/printf.h"
+
+__attribute__((naked))
+void*
+__panic_get_bp(void)
+{
+ asm("movq %%rbp, %%rax; \
+ retq"::: "memory");
+}
+
+void*
+__panic_print_istate(void *state)
+{
+ struct int_stackframe *int_state = state;
+ kprintf("RIP: %p | RSP: %p\n", int_state->int_rip, int_state->int_rsp);
+
+ print_state(&int_state->state);
+ return (void*)int_state->state.rbp;
+}
+
+void
+__panic_stack_trace(void *bp)
+{
+ uintmax_t *rbp = (uintmax_t*)bp;
+ kprintf("\nStack trace:\n");
+
+ while(rbp) {
+ kprintf("%p:\n", rbp[1]);
+ uintmax_t *next = (uintmax_t*)rbp[0];
+ ptrdiff_t dist = (next - rbp);
+
+ if((uintptr_t)next < IDENT_VIRT_BASE) break;
+ if(dist > 0) {
+ for(size_t i = 0; i < dist; i++) {
+ kprintf(" %p\n", rbp[i]);
+ }
+ }
+
+ rbp = next;
+ }
+}
diff --git a/arch/x86_64/sys/init.c b/arch/x86_64/proc/init.c
index b411fd7..4c86038 100644
--- a/arch/x86_64/sys/init.c
+++ b/arch/x86_64/proc/init.c
@@ -1,18 +1,18 @@
#include "init.h"
#include "arch/x86_64/hal/msr.h"
#include "config.h"
-#include "hal/process.h"
+#include "proc/process.h"
#include "jove.h"
#include "hal/obj.h"
#include "lib/string.h"
-#include "lib/panic.h"
+#include "panic/panic.h"
#include "lib/printf.h"
#include "arch/x86_64/lib/elf.h"
#include "arch/x86_64/hal/paging.h"
#include "boot/cmdline.h"
#include "boot/initrd.h"
-PAGEALIGN static uint8_t s_init_program_pages[INIT_PROGRAM_PAGES_MAX + 3][0x1000];
+PAGEALIGN static uint8_t s_init_program_pages[INIT_PROGRAM_PAGES_MAX][0x1000];
PAGEALIGN static uint8_t s_init_stack_pages[INIT_STACK_PAGES + 3][0x1000];
PAGEALIGN static uint8_t s_init_message_page[0x1000];
@@ -93,8 +93,8 @@ init_setup(void)
for(size_t i = 0; i < pages; i++) {
uintptr_t page_virt = page_base + (i << 12);
- if(last_page > INIT_PROGRAM_PAGES_MAX) {
- kpanic("init: Program exceeds maximum program pages in the same hugepage frame!\n");
+ if(last_page >= INIT_PROGRAM_PAGES_MAX) {
+ kpanic("Program exceeds size limit! (%i > %i)\n", last_page, INIT_PROGRAM_PAGES_MAX);
}
int tblres = 0;
diff --git a/arch/x86_64/proc/process.c b/arch/x86_64/proc/process.c
new file mode 100644
index 0000000..55af85a
--- /dev/null
+++ b/arch/x86_64/proc/process.c
@@ -0,0 +1,64 @@
+#include "process.h"
+#include "proc/process.h"
+#include "arch/x86_64/hal/obj.h"
+#include "arch/x86_64/hal/smp.h"
+#include "arch/x86_64/hal/paging.h"
+#include "hal/obj.h"
+#include "hal/smp.h"
+#include "lib/string.h"
+#include "init.h"
+#include "jove.h"
+
+PAGEALIGN uint8_t s_init_iopb[0x2000];
+PAGEALIGN char _init_pcb_bytes[0x1000] = { 0 };
+pcb_t *_init_pcb = (pcb_t*)&_init_pcb_bytes;
+
+pid_t
+pcb_pid(pcb_t *pcb)
+{
+ return pcb->pid;
+}
+
+jove_obj_t*
+pcb_rootdir(pcb_t *pcb)
+{
+ return pcb->rootdir;
+}
+
+void
+processing_setup(void (*process_main))
+{
+ memset(s_init_iopb, 0xFF, 0x2000);
+ uintptr_t ksp = ((uintptr_t)&_init_pcb_bytes) + 0xFF8;
+
+ _init_pcb->tree_node.get_key = (void*)pcb_pid;
+
+ _init_pcb->pid = 1;
+ _init_pcb->ksp = (void*)ksp;
+ _init_pcb->pml4 = paging_get_pml4();
+ _init_pcb->rootdir = _rootdir_obj;
+
+ jove_owned_obj_t *iopb_obj = objdir_getlocal(_rootdir_obj, ROOTDIR_IOPB_I);
+ obj_settype(iopb_obj, JOVE_OBJ_IOPB);
+ obj_setvalue(iopb_obj, (uintptr_t)s_init_iopb);
+ _init_pcb->iopb = iopb_obj;
+
+ processor_t *proc = processor_current();
+ proc->tss.rsp[0] = (uintptr_t)_init_pcb->ksp;
+ memcpy(proc->iopb, s_init_iopb, 0x2000);
+
+ {
+ jove_owned_obj_t *process_obj = objdir_getlocal(_rootdir_obj, ROOTDIR_PCB_I);
+ obj_settype(process_obj, JOVE_OBJ_PCB);
+ obj_setvalue(process_obj, (uintptr_t)_init_pcb);
+
+ proc->pcb_obj = process_obj;
+ proc->pcb = _init_pcb;
+ }
+
+ __asm__ volatile("\
+ movq %0, %%rsp; \
+ xorq %%rbp, %%rbp; \
+ jmpq %1"::
+ "r"(_init_pcb->ksp), "r"(process_main));
+}
diff --git a/arch/x86_64/hal/process.h b/arch/x86_64/proc/process.h
index f903153..cf4d8a8 100644
--- a/arch/x86_64/hal/process.h
+++ b/arch/x86_64/proc/process.h
@@ -1,11 +1,15 @@
#ifndef _JOVE_x86_64_HAL_PROCESS_H
#define _JOVE_x86_64_HAL_PROCESS_H 1
-#include "hal/process.h"
+#include "proc/process.h"
#include "arch/x86_64/x86_64.h"
+#include "lib/rbtree.h"
typedef struct process_control_block
{
+ struct rbtree_node tree_node;
+ struct process_control_block *sched_next;
+
pid_t pid;
jove_obj_t *rootdir;
jove_owned_obj_t *iopb;
diff --git a/arch/x86_64/sys/syscall.c b/arch/x86_64/sys/syscall.c
index 7c5727f..e3fa81b 100644
--- a/arch/x86_64/sys/syscall.c
+++ b/arch/x86_64/sys/syscall.c
@@ -1,5 +1,5 @@
#include "arch/x86_64/hal/obj.h"
-#include "arch/x86_64/hal/process.h"
+#include "arch/x86_64/proc/process.h"
#include "arch/x86_64/hal/smp.h"
#include "lib/printf.h"
diff --git a/boot/limine/memorymap.c b/boot/limine/memorymap.c
index 98405fd..c6a9064 100644
--- a/boot/limine/memorymap.c
+++ b/boot/limine/memorymap.c
@@ -3,7 +3,7 @@
#include "limine.h"
#include "hal/memory.h"
#include "hal/obj.h"
-#include "lib/panic.h"
+#include "panic/panic.h"
#include "init.h"
#include "lib/printf.h"
diff --git a/config.h b/config.h
index 50f35c1..498a842 100644
--- a/config.h
+++ b/config.h
@@ -11,7 +11,7 @@
#define FRAMEBUFFER_MAX_WIDTH 1920
#define FRAMEBUFFER_MAX_HEIGHT 1080
-#define INIT_PROGRAM_PAGES_MAX 32
+#define INIT_PROGRAM_PAGES_MAX 96
#define INIT_STACK_PAGES 2
#define LOCKSTACK_MAX 64
diff --git a/hal/smp.h b/hal/smp.h
index ba4f7d2..6713665 100644
--- a/hal/smp.h
+++ b/hal/smp.h
@@ -8,5 +8,6 @@ typedef struct processor processor_t;
processor_t *processor_current(void);
jove_owned_obj_t *processor_objdir(processor_t *processor);
+jove_owned_obj_t *processor_pcb(processor_t *processor);
#endif
diff --git a/lib/panic.c b/lib/panic.c
deleted file mode 100644
index 3b39fbf..0000000
--- a/lib/panic.c
+++ /dev/null
@@ -1,18 +0,0 @@
-#include "panic.h"
-#include "lib/printf.h"
-
-int _kernel_panic_unlock = 0;
-
-void
-kpanic(const char *fmt, ...)
-{
- _kernel_panic_unlock = 1;
-
- va_list ap;
- va_start(ap, fmt);
-
- kvprintf(fmt, ap);
-
- va_end(ap);
- hcf();
-}
diff --git a/lib/panic.h b/lib/panic.h
deleted file mode 100644
index 8f3b409..0000000
--- a/lib/panic.h
+++ /dev/null
@@ -1,8 +0,0 @@
-#ifndef _JOVE_LIB_PANIC_H
-#define _JOVE_LIB_PANIC_H 1
-
-#include "jove.h"
-
-NORETURN void kpanic(const char *fmt, ...);
-
-#endif
diff --git a/lib/rbtree.h b/lib/rbtree.h
new file mode 100644
index 0000000..1dbd5b2
--- /dev/null
+++ b/lib/rbtree.h
@@ -0,0 +1,17 @@
+#ifndef _JOVE_LIB_RBTREE_H
+#define _JOVE_LIB_RBTREE_H 1
+
+#include <stdint.h>
+
+struct rbtree_node
+{
+ struct rbtree_node *parent;
+ struct rbtree_node *children[2];
+
+ char color;
+ intmax_t (*get_key)(struct rbtree_node*);
+};
+
+
+
+#endif
diff --git a/link/x86_64-elf.ld b/link/x86_64-jove.ld
index 6c38b47..6c38b47 100644
--- a/link/x86_64-elf.ld
+++ b/link/x86_64-jove.ld
diff --git a/panic/panic.c b/panic/panic.c
new file mode 100644
index 0000000..5bde05b
--- /dev/null
+++ b/panic/panic.c
@@ -0,0 +1,40 @@
+#include "panic.h"
+#include "lib/printf.h"
+
+int _kernel_panic_unlock = 0;
+
+void
+__panic_reason(const char *fmt, va_list ap)
+{
+ _kernel_panic_unlock = 1;
+
+ kvprintf(fmt, ap);
+}
+
+void
+__panic_end(void)
+{
+ hcf();
+}
+
+void
+kpanic(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+
+ __panic_reason(fmt, ap);
+ __panic_stack_trace(__panic_get_bp());
+ __panic_end();
+}
+
+void
+kpanic_istate(void *state, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+
+ __panic_reason(fmt, ap);
+ __panic_stack_trace(__panic_print_istate(state));
+ __panic_end();
+}
diff --git a/panic/panic.h b/panic/panic.h
new file mode 100644
index 0000000..4ca4a40
--- /dev/null
+++ b/panic/panic.h
@@ -0,0 +1,16 @@
+#ifndef _JOVE_PANIC_H
+#define _JOVE_PANIC_H 1
+
+#include "jove.h"
+#include <stdarg.h>
+
+void __panic_reason(const char *fmt, va_list ap);
+void *__panic_print_istate(void *state);
+void *__panic_get_bp(void);
+void __panic_stack_trace(void *bp);
+NORETURN void __panic_end(void);
+
+NORETURN void kpanic(const char *fmt, ...);
+NORETURN void kpanic_istate(void *state, const char *fmt, ...);
+
+#endif
diff --git a/proc/process.c b/proc/process.c
new file mode 100644
index 0000000..3687c8a
--- /dev/null
+++ b/proc/process.c
@@ -0,0 +1,8 @@
+#include "process.h"
+#include "hal/smp.h"
+
+jove_owned_obj_t*
+process_current(void)
+{
+ return processor_pcb(((processor_t*)processor_current()));
+}
diff --git a/hal/process.h b/proc/process.h
index da3e050..5099476 100644
--- a/hal/process.h
+++ b/proc/process.h
@@ -2,6 +2,7 @@
#define _JOVE_HAL_PROCESS_H 1
#include "hal/obj.h"
+#include "lib/rbtree.h"
typedef uintmax_t pid_t;
@@ -16,14 +17,22 @@ struct pcb_mailbox
pid_t pid;
};
+struct pcb_expected_header
+{
+ struct rbtree_node tree_node;
+ struct process_control_block *sched_next;
+};
+
typedef struct process_control_block pcb_t;
jove_owned_obj_t *process_current(void);
+void pcb_new(pcb_t *pcb);
/*Returns the process ID of the passed PCB. */
pid_t pcb_pid(pcb_t *pcb);
-
/*Returns the root kernel object directory of this PCB. */
jove_obj_t *pcb_rootdir(pcb_t *pcb);
+extern pcb_t *_init_pcb;
+
#endif
diff --git a/sys/link.c b/sys/link.c
index 592fe8f..45c118a 100644
--- a/sys/link.c
+++ b/sys/link.c
@@ -1,6 +1,8 @@
#include "link.h"
#include "arch/x86_64/hal/obj.h"
#include "hal/memory.h"
+#include "lib/printf.h"
+#include "jove.h"
uint8_t
link_gettype(jove_owned_obj_t *obj)
@@ -57,6 +59,27 @@ link_try_traverse(jove_owned_obj_t *link, lockstack_t *lockstack)
return perm_obj;
}
+jove_perm_obj_t
+link_try_traverse_objdir(jove_owned_obj_t *objdir, lockstack_t *lockstack,
+ size_t pathlen, objpath_part_t *path)
+{
+ /*Do a traversal through the passed objdir to get objdir permissions.*/
+ jove_perm_obj_t perm_obj = link_try_traverse(objdir, lockstack);
+ if(perm_obj.obj == 0) return perm_obj;
+ if(pathlen == 0) return perm_obj;
+
+ /*Get the object pointed to by the head of the path. */
+ perm_obj.obj = lockstack_push(lockstack, objdir_trygetlocal(perm_obj.obj, path[0]));
+ if(pathlen > 1) lockstack_pop(lockstack); /*Don't lock the directory we're traversing. */
+
+ /*Recurse through the remaining path, resolving as we go. */
+ jove_perm_obj_t traverse_obj = link_try_traverse_objdir(perm_obj.obj, lockstack, pathlen - 1, &path[1]);
+
+ /*Mask permissions against the head object. */
+ traverse_obj.permissions &= perm_obj.permissions;
+ return traverse_obj;
+}
+
jove_owned_obj_t *link_new(jove_obj_t *tgt, jove_owned_obj_t *dst, uint16_t permissions)
{
struct obj_link *linkval = obj_deref(dst);
diff --git a/sys/link.h b/sys/link.h
index 3f7331f..d4bc8d8 100644
--- a/sys/link.h
+++ b/sys/link.h
@@ -27,6 +27,12 @@ jove_owned_obj_t *link_try_deref(jove_owned_obj_t *link);
jove_perm_obj_t link_traverse(jove_owned_obj_t *link, lockstack_t *lockstack);
jove_perm_obj_t link_try_traverse(jove_owned_obj_t *link, lockstack_t *lockstack);
+/**Try to retrieve an object from the object directory, resolving each link as it's encountered.
+ * Permissions are preserved through traversal, meaning objects are limited by their
+ * objdir permissions.*/
+jove_perm_obj_t link_try_traverse_objdir(jove_owned_obj_t *objdir,
+ lockstack_t *lockstack, size_t pathlen, objpath_part_t *path);
+
jove_obj_t link_unlink(jove_owned_obj_t *link);
jove_owned_obj_t *link_new(jove_obj_t *tgt, jove_owned_obj_t *dst, uint16_t permissions);
diff --git a/sys/syscall.c b/sys/syscall.c
index 31f9078..5cef3d6 100644
--- a/sys/syscall.c
+++ b/sys/syscall.c
@@ -1,7 +1,7 @@
#include "sys/syscall.h"
#include "arch/x86_64/hal/obj.h"
#include "hal/smp.h"
-#include "hal/process.h"
+#include "proc/process.h"
#include "jove.h"
#include "lib/printf.h"
#include "sys/link.h"
@@ -83,14 +83,12 @@ syscall_handler(uintmax_t message_path_raw, uint16_t message_pathlen)
int sysret = 0;
- ctx.pcb = lockstack_push(&ctx.lockstack, process_current());
+ ctx.pcb = process_current();
pcb_t *pcb = obj_deref(ctx.pcb);
ctx.objdir = lockstack_push(&ctx.lockstack, obj_take(pcb_rootdir(pcb)));
- ctx.message = link_try_traverse(
- lockstack_push(&ctx.lockstack, objdir_trygetobj(
- ctx.objdir, message_path, message_pathlen)),
- &ctx.lockstack);
+ ctx.message = link_try_traverse_objdir(
+ ctx.objdir, &ctx.lockstack, message_pathlen, message_path);
if(ctx.message.obj == NULL) {
klogf("Process %i failed to get message %P : %s\n",
@@ -106,6 +104,13 @@ syscall_handler(uintmax_t message_path_raw, uint16_t message_pathlen)
goto release_return;
}
+ if((ctx.message.permissions & JOVE_PERM_READ) == 0) {
+ klogf("Process %i tried to call syscall with message object %P without read permissions\n",
+ pcb_pid(pcb), message_path, message_pathlen);
+ sysret = -EPERM;
+ goto release_return;
+ }
+
size_t messagelen = message_len(ctx.message.obj);
void *messagedata = message_data(ctx.message.obj);
struct syscall_header *header = messagedata;
@@ -122,8 +127,9 @@ syscall_handler(uintmax_t message_path_raw, uint16_t message_pathlen)
ctx.bytes_left = messagelen - bytes_needed;
ctx.payload = (void*)((uintptr_t)header + bytes_needed);
- ctx.target.obj = lockstack_push(&ctx.lockstack,
- objdir_trygetobj(ctx.objdir, header->path, header->pathlen));
+ ctx.target = link_try_traverse_objdir(
+ ctx.objdir, &ctx.lockstack, ctx.header->pathlen, ctx.header->path);
+
if(ctx.target.obj == 0) {
//Could not find payload target in pcb rootdir.
klogf("Process %i tried to call syscall with bad target path %P : %s\n",
@@ -132,7 +138,6 @@ syscall_handler(uintmax_t message_path_raw, uint16_t message_pathlen)
goto release_return;
}
- ctx.target.permissions = JOVE_PERM_ALL;
sysret = _syscall_handle_switch(&ctx);
release_return:
diff --git a/sys/syscall_objdir.c b/sys/syscall_objdir.c
index b4d3800..9243af1 100644
--- a/sys/syscall_objdir.c
+++ b/sys/syscall_objdir.c
@@ -1,5 +1,5 @@
#include "arch/x86_64/hal/obj.h"
-#include "hal/process.h"
+#include "proc/process.h"
#include "jove.h"
#include "lib/printf.h"
#include "syscall.h"
diff --git a/sys/syscall_pagedir.c b/sys/syscall_pagedir.c
index 961c477..7b8f315 100644
--- a/sys/syscall_pagedir.c
+++ b/sys/syscall_pagedir.c
@@ -28,10 +28,9 @@ s_get_untyped(
"Message too small for untyped path.");
errno = 0;
- jove_perm_obj_t untyped = link_try_traverse(
- lockstack_push(lockstack, objdir_trygetobj(
- objdir, payload->untyped_path, payload->untyped_pathlen)),
- lockstack);
+ jove_perm_obj_t untyped = link_try_traverse_objdir(
+ objdir, lockstack, payload->untyped_pathlen, payload->untyped_path);
+
if(errno) {
kprintf("%s: Untyped obj failed for the reason: %s\n", __func__, errstr(errno));
return errno;
@@ -81,10 +80,9 @@ s_syscall_pagedir_unmap(
CHECK_TOOSMALL(bytes_needed, payload_bytes,
"Message too small for none path");
- jove_perm_obj_t none = link_try_traverse(
- lockstack_push(lockstack, objdir_trygetobj(
- objdir, payload->none_path, payload->none_pathlen)),
- lockstack);
+ jove_perm_obj_t none = link_try_traverse_objdir(
+ objdir, lockstack, payload->none_pathlen, payload->none_path);
+
if(errno) {
kprintf("%s: None obj retrieval failed for the reason: %s\n",
__func__, errstr(errno));
@@ -163,15 +161,14 @@ s_syscall_pagedir_map_shmem(
bytes_needed += payload->shmem_pathlen << 1;
CHECK_TOOSMALL(bytes_needed, payload_bytes,
- "Message too small for untyped path.");
+ "Message too small for shared memory path.");
errno = 0;
- jove_perm_obj_t shmem = link_try_traverse(
- lockstack_push(lockstack, objdir_trygetobj(
- objdir, payload->shmem_path, payload->shmem_pathlen)),
- lockstack);
+ jove_perm_obj_t shmem = link_try_traverse_objdir(
+ objdir, lockstack, payload->shmem_pathlen, payload->shmem_path);
if(errno) {
- klogf("Shared memory obj failed for the reason: %s\n", errstr(errno));
+ klogf("Unable to retrieve shared memory object %P : %s\n",
+ payload->shmem_path, payload->shmem_pathlen, errstr(errno));
return errno;
}
@@ -179,7 +176,7 @@ s_syscall_pagedir_map_shmem(
"Shared memory path does not point to a shared memory object.");
uintmax_t shmem_value = obj_getvalue(shmem.obj);
- uint8_t shmem_pages = shmem_value & 0xFF;
+ uint8_t shmem_pages = shmem_value & 0xFFF;
if(payload->page_offset >= shmem_pages) {
klogf("Page offset goes beyond size of shared memory object: (%i >= %i)\n",
diff --git a/sys/syscall_untyped.c b/sys/syscall_untyped.c
index a117387..8fe592d 100644
--- a/sys/syscall_untyped.c
+++ b/sys/syscall_untyped.c
@@ -19,10 +19,13 @@ s_syscall_untyped_split(
bytes_needed += payload->dest_pathlen * 2;
CHECK_TOOSMALL(bytes_needed, payload_bytes, "Message is too small for destination path!");
- jove_perm_obj_t dest = link_try_traverse(
- lockstack_push(lockstack,
- objdir_trygetobj(objdir, payload->dest_path, payload->dest_pathlen)),
- lockstack);
+ jove_perm_obj_t dest = link_try_traverse_objdir(
+ objdir, lockstack, payload->dest_pathlen, payload->dest_path);
+ if(errno) {
+ klogf("Unable to get destination object %P: %s\n",
+ payload->dest_path, payload->dest_pathlen, errstr(errno));
+ return errno;
+ }
CHECK_OBJTYPE(dest.obj, JOVE_OBJ_NONE, "Destination path is occupied!");
@@ -49,10 +52,14 @@ s_syscall_untyped_merge(
bytes_needed += payload->pathlen * 2;
CHECK_TOOSMALL(bytes_needed, payload_bytes, "Message is too small for second path!");
- jove_perm_obj_t untyped = link_try_traverse(
- lockstack_push(lockstack,
- objdir_getobj(objdir, payload->path, payload->pathlen)),
- lockstack);
+ jove_perm_obj_t untyped = link_try_traverse_objdir(
+ objdir, lockstack, payload->pathlen, payload->path);
+ if(errno) {
+ klogf("Unable to retrieve untyped object %P : %s\n",
+ payload->path, payload->pathlen, errstr(errno));
+ return errno;
+ }
+
CHECK_OBJTYPE(untyped.obj, JOVE_OBJ_UNTYPED, "Second object is not an untyped!");
CHECK_OBJPERM(untyped, SYSCALL_PERM_UNTYPED_MERGE, "Second untyped does not have permission to merge!");