From 991b13424ee6e20a64a425abe3d5c5227252c972 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Wed, 9 Oct 2019 22:28:56 -0700 Subject: [PATCH] Initial building of the vdso. Not actually integrating with the kernel yet. --- modules.yaml | 11 +++++ scripts/templates/build.ninja.j2 | 1 + scripts/templates/exe.vdso.j2 | 9 ++++ src/arch/x86_64/vdso.ld | 67 ++++++++++++++++++++++++++++ src/{kernel => include}/syscalls.inc | 4 +- src/vdso/overrides.cpp | 17 +++++++ src/vdso/syscalls.cpp | 17 +++++++ src/vdso/vdso_internal.h | 16 +++++++ 8 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 scripts/templates/exe.vdso.j2 create mode 100644 src/arch/x86_64/vdso.ld rename src/{kernel => include}/syscalls.inc (75%) create mode 100644 src/vdso/overrides.cpp create mode 100644 src/vdso/syscalls.cpp create mode 100644 src/vdso/vdso_internal.h diff --git a/modules.yaml b/modules.yaml index df3d73f..e035658 100644 --- a/modules.yaml +++ b/modules.yaml @@ -9,6 +9,7 @@ modules: - elf - initrd - kutil + - vdso includes: - src/kernel source: @@ -64,6 +65,16 @@ modules: - src/boot/reloc.cpp - src/boot/utility.cpp + vdso: + kind: exe + target: host + output: libvdso.so + extra: + - src/arch/x86_64/vdso.ld + source: + - src/vdso/syscalls.cpp + - src/vdso/overrides.cpp + nulldrv: kind: exe target: user diff --git a/scripts/templates/build.ninja.j2 b/scripts/templates/build.ninja.j2 index f003df8..e15cab4 100644 --- a/scripts/templates/build.ninja.j2 +++ b/scripts/templates/build.ninja.j2 @@ -43,6 +43,7 @@ ccflags = $ $warnflags asflags = $ + -I${srcroot}/src/include $ -DVERSION_MAJOR={{ version_major }} $ -DVERSION_MINOR={{ version_minor }} $ -DVERSION_PATCH={{ version_patch }} $ diff --git a/scripts/templates/exe.vdso.j2 b/scripts/templates/exe.vdso.j2 new file mode 100644 index 0000000..c0bd699 --- /dev/null +++ b/scripts/templates/exe.vdso.j2 @@ -0,0 +1,9 @@ +{% extends "exe.default.j2" %} +{% block variables %} +{{ super() }} +ccflags = $ccflags -fPIC -mcmodel=large +ldflags = $ldflags -shared -znotext -T ${srcroot}/src/arch/x86_64/vdso.ld +{% endblock %} + +# vim: ft=ninja et ts=4 sts=4 sw=4 + diff --git a/src/arch/x86_64/vdso.ld b/src/arch/x86_64/vdso.ld new file mode 100644 index 0000000..83e1383 --- /dev/null +++ b/src/arch/x86_64/vdso.ld @@ -0,0 +1,67 @@ +PHDRS { + rodata PT_LOAD FLAGS(4) FILEHDR PHDRS ; + text PT_LOAD FLAGS(5) ; + dynamic PT_DYNAMIC FLAGS(4) ; + note PT_NOTE ; + eh_frame PT_GNU_EH_FRAME ; +} + +SECTIONS { + . = SIZEOF_HEADERS; + + /DISCARD/ : { + *(.got*) + *(.plt*) + *(.note.*) + *(.hash*) + } + + .illegal.relocations : { + *(.rel*) + } + + .illegal.writeable : { + *(.data*) + *(.bss*) + } + + .rodata : { + *(.rodata*) + } :rodata + + .dynamic : { + *(.dynamic) + } :dynamic + + .dynsym : { + *(.dynsym*) + } :rodata :dynamic + + .dynstr : { + *(.dynstr*) + } :rodata :dynamic + + .gnu.hash : { + *(.gnu.hash*) + } :rodata + + .eh_frame_hdr : { + __eh_frame_start = .; + KEEP(*(.eh_frame)) + __eh_frame_end = .; + + KEEP(*(.eh_frame_hdr)) + } :eh_frame + + .text ALIGN(0x1000) : { + *(.text*) + *(.init*) + *(.fini*) + . = ALIGN(0x1000); + } :text + + ASSERT(SIZEOF(.illegal.relocations) == 0, + "Code has introduced relocations into the VDSO") + ASSERT(SIZEOF(.illegal.writeable) == 0, + "Code has introduced writeable data into the VDSO") +} diff --git a/src/kernel/syscalls.inc b/src/include/syscalls.inc similarity index 75% rename from src/kernel/syscalls.inc rename to src/include/syscalls.inc index b726252..00e7567 100644 --- a/src/kernel/syscalls.inc +++ b/src/include/syscalls.inc @@ -2,8 +2,8 @@ SYSCALL(0x00, object_noop, void) SYSCALL(0x01, object_wait, j6_handle_t, j6_signal_t, j6_signal_t *) SYSCALL(0x11, process_exit, int64_t) -SYSCALL(0x12, process_fork, pid_t*) -SYSCALL(0x13, process_getpid, pid_t*) +//SYSCALL(0x12, process_fork, j6_koid_t*) +//SYSCALL(0x13, process_getpid, j6_koid_t*) SYSCALL(0x14, process_log, const char *) SYSCALL(0x15, process_pause, void) SYSCALL(0x16, process_sleep, uint64_t) diff --git a/src/vdso/overrides.cpp b/src/vdso/overrides.cpp new file mode 100644 index 0000000..d2bbfe4 --- /dev/null +++ b/src/vdso/overrides.cpp @@ -0,0 +1,17 @@ +#include +#include +#include "vdso_internal.h" + +extern "C" { + +j6_status_t j6_object_noop() { + // Skip the kernel + return j6_status_ok; +} + +// An example of overriding a weak default symbol with a custom function +j6_status_t j6_object_wait(j6_handle_t target, j6_signal_t signals, j6_signal_t *triggered) { + return __sys_j6_object_wait(target, signals, triggered); +} + +} diff --git a/src/vdso/syscalls.cpp b/src/vdso/syscalls.cpp new file mode 100644 index 0000000..f69aa56 --- /dev/null +++ b/src/vdso/syscalls.cpp @@ -0,0 +1,17 @@ +#include +#include +#include "vdso_internal.h" + +#define SYSCALL(num, name, ...) \ + j6_status_t __sys_j6_##name (__VA_ARGS__) { \ + j6_status_t result = 0; \ + __asm__ __volatile__ ( "syscall" : "=a"(result) : "a"(num) ); \ + return result; \ + } \ + j6_status_t j6_ ## name(__VA_ARGS__) __weak_alias("__sys_j6_" #name); + +extern "C" { +#include "syscalls.inc" +} + +#undef SYSCALL diff --git a/src/vdso/vdso_internal.h b/src/vdso/vdso_internal.h new file mode 100644 index 0000000..f32906f --- /dev/null +++ b/src/vdso/vdso_internal.h @@ -0,0 +1,16 @@ +#pragma once +/// \file vdso_internal.h +/// VDSO syscall forward-declares and linker utils + +#define __weak_alias(name) __attribute__((weak, alias(name))); +#define __local __attribute__((__visibility__("hidden"))) + +#define SYSCALL(num, name, ...) \ + j6_status_t __sys_j6_ ## name (__VA_ARGS__) __local; \ + +extern "C" { +#include "syscalls.inc" +} + +#undef SYSCALL +