aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2024-08-30 19:20:21 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2024-08-30 19:20:21 +0300
commit314bca3dc19a864c76153bfcd5a58be8c40000f9 (patch)
tree9aba30284fce66e149a7472adea1cb4037e49463 /tests
parent1a2e1fff7ce4b8fd8db46549d81e71e76b842da3 (diff)
downloadkmi-314bca3dc19a864c76153bfcd5a58be8c40000f9.tar.gz
kmi-314bca3dc19a864c76153bfcd5a58be8c40000f9.zip
improvements to shared memory handling
+ Now each shared region is reference counter (technically each region is refernce counted, private regions just have a count of 1). Also, syscalls that touch the TLB get flushed, but should probably look into where else this flushing might be needed.
Diffstat (limited to 'tests')
-rw-r--r--tests/common/arch/riscv64/source.mk5
-rw-r--r--tests/common/sys.h5
-rw-r--r--tests/dangling_shmem/init.c50
-rw-r--r--tests/dangling_shmem/source.mk2
-rw-r--r--tests/shmem/init.c36
-rw-r--r--tests/shmem/source.mk2
6 files changed, 97 insertions, 3 deletions
diff --git a/tests/common/arch/riscv64/source.mk b/tests/common/arch/riscv64/source.mk
index 517c1c7..e3c0e55 100644
--- a/tests/common/arch/riscv64/source.mk
+++ b/tests/common/arch/riscv64/source.mk
@@ -1 +1,4 @@
-ARCH_FLAGS += -march=rv64imac -mabi=lp64
+# -mno-relax to disable the compiler from using the gp register as a shorthand
+# for __global_pointer$, which I currently don't do anything with. Should
+# probably fix at some point
+ARCH_FLAGS += -march=rv64imac -mabi=lp64 -mno-relax
diff --git a/tests/common/sys.h b/tests/common/sys.h
index 6d5e493..3e98abf 100644
--- a/tests/common/sys.h
+++ b/tests/common/sys.h
@@ -86,9 +86,10 @@ static inline void *sys_ref_sharedmem(id_t tid, uintptr_t addr, vmflags_t flags)
return (void *)r.a0;
}
-static inline void sys_free_mem(uintptr_t start)
+static inline enum sys_status sys_free_mem(uintptr_t start)
{
- syscall1(SYS_FREE_MEM, start);
+ struct sys_ret r = syscall1(SYS_FREE_MEM, start);
+ return r.s;
}
static inline uint64_t sys_timebase()
diff --git a/tests/dangling_shmem/init.c b/tests/dangling_shmem/init.c
new file mode 100644
index 0000000..df211a7
--- /dev/null
+++ b/tests/dangling_shmem/init.c
@@ -0,0 +1,50 @@
+#include <common/test.h>
+
+static id_t new_id = 0;
+static char *refmem = NULL;
+
+START(pid, tid, d0, d1, d2, d3)
+{
+ UNUSED(pid);
+ UNUSED(tid);
+ UNUSED(d0);
+ UNUSED(d1);
+ UNUSED(d2);
+ UNUSED(d3);
+
+ if (pid == 0 && d0 == SYS_USER_SPAWNED) {
+ printf("forking\n");
+ id_t our_id = 0;
+ new_id = sys_fork(&our_id);
+ if (new_id == 0) {
+ printf("in child, setting up shared memory\n");
+ void *p = sys_req_sharedmem(1, VM_R | VM_W);
+ check(p, "failed getting shared memory\n");
+
+ printf("referencing shared memory for pid 1\n");
+ void *r = sys_ref_sharedmem(1, (uintptr_t)p, VM_R | VM_W);
+ check(r, "failed referencing shared memory\n");
+
+ sys_ipc_req1(1, (uintptr_t)r);
+ sys_exit(1);
+ }
+
+ printf("swapping to child\n");
+ enum sys_status r = sys_swap(new_id);
+ check(r == OK, "failed swapping to child\n");
+
+ printf("returned from child\n");
+ /* child should now be dead */
+ check(refmem[0] == 'p', "child dying affected our shared memory?\n");
+
+ printf("freeing shared memory\n");
+ r = sys_free_mem((uintptr_t)refmem);
+ check(r == OK, "failed freeing referenced memory\n");
+ }
+ else if (pid == new_id) {
+ refmem = (char *)d0;
+ refmem[0] = 'p';
+ sys_ipc_resp0();
+ }
+ ok();
+}
diff --git a/tests/dangling_shmem/source.mk b/tests/dangling_shmem/source.mk
new file mode 100644
index 0000000..2ecd7a6
--- /dev/null
+++ b/tests/dangling_shmem/source.mk
@@ -0,0 +1,2 @@
+DO != ./scripts/gen-prog -n dangling_shmem -p init init.c
+DO != ./scripts/gen-simple -n dangling_shmem -p init
diff --git a/tests/shmem/init.c b/tests/shmem/init.c
new file mode 100644
index 0000000..13e1667
--- /dev/null
+++ b/tests/shmem/init.c
@@ -0,0 +1,36 @@
+#include <common/test.h>
+
+START(pid, tid, d0, d1, d2, d3)
+{
+ UNUSED(pid);
+ UNUSED(tid);
+ UNUSED(d0);
+ UNUSED(d1);
+ UNUSED(d2);
+ UNUSED(d3);
+
+ printf("allocating shared memory\n");
+ volatile char *shmem = sys_req_sharedmem(1, VM_R | VM_W);
+ check(shmem, "no shared memory?\n");
+
+ *shmem = 'p';
+
+ printf("sharing memory to ourselves\n");
+ volatile char *refmem = sys_ref_sharedmem(1, (uintptr_t)shmem, VM_R | VM_W);
+ check(refmem[0] == 'p', "wrong mapping?\n");
+
+ printf("freeing shared memory (should fail)\n");
+ enum sys_status r = sys_free_mem((uintptr_t)shmem);
+ check(r != OK, "illegal freeing succeeded?\n");
+
+ printf("freeing referenced memory\n");
+ r = sys_free_mem((uintptr_t)refmem);
+ check(r == OK, "legal ref freeing failed?\n");
+ check(shmem[0] == 'p', "freeing ref freed owned memory?\n");
+
+ printf("freeing recently nonshared memory\n");
+ r = sys_free_mem((uintptr_t)shmem);
+ check(r == OK, "legal freeing failed?\n");
+
+ ok();
+}
diff --git a/tests/shmem/source.mk b/tests/shmem/source.mk
new file mode 100644
index 0000000..da84808
--- /dev/null
+++ b/tests/shmem/source.mk
@@ -0,0 +1,2 @@
+DO != ./scripts/gen-prog -n shmem -p init init.c
+DO != ./scripts/gen-simple -n shmem -p init