From f5208d1641480003dcc38f1ba48b81530ff210d5 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Wed, 12 Jul 2023 19:38:31 -0700 Subject: [PATCH] [all] Remove dependencies on non-freestanding libc This is the first of two rather big changes to clean up includes throughout the project. In this commit, the implicit semi-dependency on libc that bonnibel adds to every module is removed. Previously, I was sloppy with includes of libc headers and include directory order. Now, the freestanding headers from libc are split out into libc_free, and an implicit real dependency is added onto this module, unless `no_libc` is set to `True`. The full libc needs to be explicitly specified as a dependency to be used. Several things needed to change in order to do this: - Many places use `memset` or `memcpy` that cannot depend on libc. The kernel has basic implementations of them itself for this reason. Now those functions are moved into the lower-level `j6/memutils.h`, and libc merely references them. Other modules are now free to reference those functions from libj6 instead. - The kernel's `assert.h` was renamed kassert.h (matching its `kassert` function) so that the new `util/assert.h` can use `__has_include` to detect it and make sure the `assert` macro is usable in libutil code. - Several implementation header files under `__libj6/` also moved under the new libc_free. - A new `include_phase` property has been added to modules for Bonnibel, which can be "normal" (default) or "late" which uses `-idirafter` instead of `-I` for includes. - Since `` and `` are not freestanding, implementations of `remove_reference`, `forward`, `move`, and `swap` were added to the `util` namespace to replace those from `std`, and `util/new.h` was added to declare `operator new` and `operator delete`. --- assets/build/base.yaml | 1 - assets/build/kernel-debug.yaml | 4 +- assets/build/rules.ninja | 12 +++--- assets/build/user-debug.yaml | 4 +- external/zstd.module | 2 +- scripts/bonnibel/module.py | 38 +++++++++++++---- src/kernel/apic.cpp | 2 +- src/kernel/cpprt.cpp | 2 +- src/kernel/cpu.cpp | 5 +-- src/kernel/device_manager.cpp | 3 +- src/kernel/frame_allocator.cpp | 2 +- src/kernel/gdt.cpp | 6 +-- src/kernel/heap_allocator.cpp | 5 +-- src/kernel/hpet.cpp | 2 +- src/kernel/idt.cpp | 3 +- src/kernel/idt.h | 2 +- src/kernel/interrupts.cpp | 2 +- src/kernel/{assert.cpp => kassert.cpp} | 2 +- src/kernel/{assert.h => kassert.h} | 0 src/kernel/kernel.module | 2 +- src/kernel/kernel_main.cpp | 4 +- src/kernel/logger.cpp | 6 +-- src/kernel/memory.cpp | 25 +---------- src/kernel/memory_bootstrap.cpp | 5 +-- src/kernel/objects/kobject.cpp | 2 +- src/kernel/objects/process.cpp | 4 +- src/kernel/objects/thread.cpp | 1 + src/kernel/objects/vm_area.cpp | 2 +- src/kernel/page_table.cpp | 4 +- src/kernel/page_tree.cpp | 5 +-- src/kernel/panic.serial/main.cpp | 2 +- src/kernel/scheduler.cpp | 2 +- src/kernel/slab_allocated.h | 3 +- src/kernel/syscall.cpp.cog | 2 +- src/kernel/syscalls/futex.cpp | 5 ++- src/kernel/syscalls/object.cpp | 2 +- src/kernel/sysconf.cpp | 4 +- src/kernel/tss.cpp | 4 +- src/kernel/vm_space.cpp | 5 +-- src/kernel/wait_queue.cpp | 6 +-- src/libraries/j6/channel.cpp | 3 +- src/libraries/{libc/__j6libc => j6}/copy.h | 21 ++-------- src/libraries/j6/j6.module | 2 + src/libraries/j6/j6/memutils.h | 18 ++++++++ .../string/memset.cpp => j6/memutils.cpp} | 41 +++++++++++++------ src/libraries/j6/thread.cpp | 2 +- src/libraries/libc/libc.module | 1 + src/libraries/libc/string/memcpy.cpp | 17 -------- src/libraries/libc/string/memmove.cpp | 33 --------------- .../{libc => libc_free}/__j6libc/bits.h | 0 .../{libc => libc_free}/__j6libc/casts.h | 0 .../{libc => libc_free}/__j6libc/null.h | 0 .../{libc => libc_free}/__j6libc/restrict.h | 0 .../{libc => libc_free}/__j6libc/size_t.h | 0 .../{libc => libc_free}/__j6libc/wchar_t.h | 0 src/libraries/{libc => libc_free}/float.h | 0 src/libraries/{libc => libc_free}/iso646.h | 0 src/libraries/libc_free/libc_free.module | 24 +++++++++++ src/libraries/{libc => libc_free}/limits.h | 0 src/libraries/{libc => libc_free}/stdalign.h | 0 src/libraries/{libc => libc_free}/stdarg.h | 0 src/libraries/{libc => libc_free}/stdbool.h | 0 src/libraries/{libc => libc_free}/stddef.h | 0 .../{libc => libc_free}/stdint.h.cog | 0 .../{libc => libc_free}/stdnoreturn.h | 0 src/libraries/util/util.module | 2 + src/libraries/util/util/assert.h | 13 ++++++ src/libraries/util/util/basic_types.h | 17 ++++++++ src/libraries/util/util/deque.h | 10 ++--- src/libraries/util/util/map.h | 2 +- src/libraries/util/util/new.h | 14 +++++++ src/libraries/util/util/node_map.h | 16 ++++---- src/libraries/util/util/radix_tree.h | 2 +- src/libraries/util/util/vector.h | 13 +++--- 74 files changed, 233 insertions(+), 210 deletions(-) rename src/kernel/{assert.cpp => kassert.cpp} (98%) rename src/kernel/{assert.h => kassert.h} (100%) rename src/libraries/{libc/__j6libc => j6}/copy.h (61%) create mode 100644 src/libraries/j6/j6/memutils.h rename src/libraries/{libc/string/memset.cpp => j6/memutils.cpp} (61%) delete mode 100644 src/libraries/libc/string/memcpy.cpp delete mode 100644 src/libraries/libc/string/memmove.cpp rename src/libraries/{libc => libc_free}/__j6libc/bits.h (100%) rename src/libraries/{libc => libc_free}/__j6libc/casts.h (100%) rename src/libraries/{libc => libc_free}/__j6libc/null.h (100%) rename src/libraries/{libc => libc_free}/__j6libc/restrict.h (100%) rename src/libraries/{libc => libc_free}/__j6libc/size_t.h (100%) rename src/libraries/{libc => libc_free}/__j6libc/wchar_t.h (100%) rename src/libraries/{libc => libc_free}/float.h (100%) rename src/libraries/{libc => libc_free}/iso646.h (100%) create mode 100644 src/libraries/libc_free/libc_free.module rename src/libraries/{libc => libc_free}/limits.h (100%) rename src/libraries/{libc => libc_free}/stdalign.h (100%) rename src/libraries/{libc => libc_free}/stdarg.h (100%) rename src/libraries/{libc => libc_free}/stdbool.h (100%) rename src/libraries/{libc => libc_free}/stddef.h (100%) rename src/libraries/{libc => libc_free}/stdint.h.cog (100%) rename src/libraries/{libc => libc_free}/stdnoreturn.h (100%) create mode 100644 src/libraries/util/util/assert.h create mode 100644 src/libraries/util/util/new.h diff --git a/assets/build/base.yaml b/assets/build/base.yaml index 3c7ea0e..3517274 100644 --- a/assets/build/base.yaml +++ b/assets/build/base.yaml @@ -9,7 +9,6 @@ variables: ccflags: [ "-I${source_root}/src/include", - "-I${source_root}/src/include/x86_64", "-fcolor-diagnostics", "-U__STDCPP_THREADS__", "-D_LIBCPP_HAS_NO_THREADS", diff --git a/assets/build/kernel-debug.yaml b/assets/build/kernel-debug.yaml index 1d60d7b..49cc388 100644 --- a/assets/build/kernel-debug.yaml +++ b/assets/build/kernel-debug.yaml @@ -35,8 +35,6 @@ variables: "-DPRINTF_ALIAS_STANDARD_FUNCTION_NAMES=1", "-DPRINTF_INCLUDE_CONFIG_H=1", - "-isystem${build_root}/include/libc", - "-isystem${source_root}/sysroot/include", "--sysroot='${source_root}/sysroot'" ] @@ -46,7 +44,7 @@ variables: "-fno-exceptions", "-fno-rtti", "-nostdinc", - "-isystem${source_root}/sysroot/include/c++/v1" ] + ] ldflags: [ "-g", diff --git a/assets/build/rules.ninja b/assets/build/rules.ninja index ecfe1df..7cf5290 100644 --- a/assets/build/rules.ninja +++ b/assets/build/rules.ninja @@ -2,7 +2,7 @@ rule compile.c command = $cc -MMD -MF $out.d $cflags $ccflags -o $out -c $in - description = Compiling $name + description = Compiling [$target]:$name depfile = $out.d deps = gcc @@ -17,7 +17,7 @@ rule dump_c_run rule compile.cpp command = $cxx -MMD -MF $out.d $cxxflags $ccflags -o $out -c $in - description = Compiling $name + description = Compiling [$target]:$name depfile = $out.d deps = gcc @@ -32,13 +32,13 @@ rule dump_cpp_run rule compile.s command = $nasm -o $out -felf64 -MD $out.d $asflags $in - description = Assembling $name + description = Assembling [$target]:$name depfile = $out.d deps = gcc rule parse.cog command = cog -o $out -d -D target=$target $cogflags $in - description = Parsing $name + description = Parsing [$target]:$name rule exe command = $ld $ldflags -o $out $in $libs @@ -46,11 +46,11 @@ rule exe rule lib command = $ar qcs $out $in - description = Archiving $name + description = Archiving [$target]:$name rule cp command = cp $in $out - description = Copying $name + description = Copying [$target]:$name rule dump command = objdump -DSC -M intel $in > $out diff --git a/assets/build/user-debug.yaml b/assets/build/user-debug.yaml index 356358f..2d0b735 100644 --- a/assets/build/user-debug.yaml +++ b/assets/build/user-debug.yaml @@ -21,15 +21,13 @@ variables: "-U__linux", "-U__linux__", - "-isystem${source_root}/sysroot/include", - "-isystem${build_root}/include/libc", "--sysroot='${source_root}/sysroot'" ] cxxflags: [ "-fno-exceptions", "-fno-rtti", - "-isystem${source_root}/sysroot/include/c++/v1" ] + ] ldflags: [ "-g", diff --git a/external/zstd.module b/external/zstd.module index 27b9fe8..d35ecb2 100644 --- a/external/zstd.module +++ b/external/zstd.module @@ -3,7 +3,7 @@ zstd = module("zstd", root = "${source_root}/external/zstd", kind = "lib", - deps = [ ], + deps = [ "libc" ], output = "libzstd.a", sources = [ "decompress/zstd_decompress.c", diff --git a/scripts/bonnibel/module.py b/scripts/bonnibel/module.py index 9b3da4f..2db3716 100644 --- a/scripts/bonnibel/module.py +++ b/scripts/bonnibel/module.py @@ -7,8 +7,10 @@ def resolve(path): return str(Path(path).resolve()) class BuildOptions: - def __init__(self, includes=tuple(), libs=tuple(), order_only=tuple(), ld_script=None): + def __init__(self, includes=tuple(), local=tuple(), late=tuple(), libs=tuple(), order_only=tuple(), ld_script=None): self.includes = list(includes) + self.local = list(local) + self.late = list(late) self.libs = list(libs) self.order_only = list(order_only) self.ld_script = ld_script and str(ld_script) @@ -30,6 +32,7 @@ class Module: "deps": (set, ()), "public_headers": (set, ()), "includes": (tuple, ()), + "include_phase": (str, "normal"), "sources": (tuple, ()), "drivers": (tuple, ()), "variables": (dict, ()), @@ -59,6 +62,9 @@ class Module: if not name in self.__fields: raise AttributeError(f"No attribute named {name} on Module") + if not self.no_libc: + self.deps.add("libc_free") + # Turn strings into real Source objects self.sources = [make_source(root, f) for f in self.sources] self.public_headers = [make_source(root, f) for f in self.public_headers] @@ -132,8 +138,6 @@ class Module: header_targets = [] if add_headers: - if not self.no_libc: - header_targets.append(f"${{build_root}}/include/libc/{phony}") if self.public_headers: header_targets.append(f"${{build_root}}/include/{self.name}/{phony}") @@ -184,7 +188,7 @@ class Module: build.variable("module_dir", target_rel(self.name + ".dir")) modopts = BuildOptions( - includes = [self.root, "${module_dir}"], + local = [self.root, "${module_dir}"], ld_script = self.ld_script and self.root / self.ld_script, ) if self.public_headers: @@ -207,19 +211,37 @@ class Module: modopts.includes.append(str(incpath)) modopts.includes.append(destpath) - all_deps = walk_deps(self.depmods) for dep in all_deps: if dep.public_headers: - modopts.includes += [f"${{build_root}}/include/{dep.name}"] + if dep.include_phase == "normal": + modopts.includes += [f"${{build_root}}/include/{dep.name}"] + elif dep.include_phase == "late": + modopts.late += [f"${{build_root}}/include/{dep.name}"] + else: + from . import BonnibelError + raise BonnibelError(f"Module {dep.name} has invalid include_phase={dep.include_phase}") if dep.kind == "lib": modopts.libs.append(target_rel(dep.output)) else: modopts.order_only.append(target_rel(dep.output)) + cc_includes = [] + if modopts.local: + cc_includes += [f"-iquote{i}" for i in modopts.local] + if modopts.includes: - build.variable("ccflags", ["${ccflags}"] + [f"-I{i}" for i in modopts.includes]) - build.variable("asflags", ["${asflags}"] + [f"-I{i}" for i in modopts.includes]) + cc_includes += [f"-I{i}" for i in modopts.includes] + + if modopts.late: + cc_includes += [f"-idirafter{i}" for i in modopts.late] + + if cc_includes: + build.variable("ccflags", ["${ccflags}"] + cc_includes) + + as_includes = [f"-I{d}" for d in modopts.local + modopts.includes + modopts.late] + if as_includes: + build.variable("asflags", ["${asflags}"] + as_includes) if modopts.libs: build.variable("libs", ["${libs}"] + modopts.libs) diff --git a/src/kernel/apic.cpp b/src/kernel/apic.cpp index 8609ce7..ac46817 100644 --- a/src/kernel/apic.cpp +++ b/src/kernel/apic.cpp @@ -1,5 +1,5 @@ #include "apic.h" -#include "assert.h" +#include "kassert.h" #include "clock.h" #include "interrupts.h" #include "io.h" diff --git a/src/kernel/cpprt.cpp b/src/kernel/cpprt.cpp index 415d1b5..5804a04 100644 --- a/src/kernel/cpprt.cpp +++ b/src/kernel/cpprt.cpp @@ -1,4 +1,4 @@ -#include "assert.h" +#include "kassert.h" using __exit_func = void (*)(void *); diff --git a/src/kernel/cpu.cpp b/src/kernel/cpu.cpp index 7033b65..21c631c 100644 --- a/src/kernel/cpu.cpp +++ b/src/kernel/cpu.cpp @@ -1,10 +1,9 @@ -#include #include -#include +#include #include #include -#include "assert.h" +#include "kassert.h" #include "cpu.h" #include "device_manager.h" #include "gdt.h" diff --git a/src/kernel/device_manager.cpp b/src/kernel/device_manager.cpp index fce472f..bbd2f24 100644 --- a/src/kernel/device_manager.cpp +++ b/src/kernel/device_manager.cpp @@ -1,11 +1,10 @@ -#include #include #include #include // for checksum #include #include -#include "assert.h" +#include "kassert.h" #include "apic.h" #include "clock.h" #include "device_manager.h" diff --git a/src/kernel/frame_allocator.cpp b/src/kernel/frame_allocator.cpp index 9b40e6f..09517c5 100644 --- a/src/kernel/frame_allocator.cpp +++ b/src/kernel/frame_allocator.cpp @@ -1,6 +1,6 @@ #include -#include "assert.h" +#include "kassert.h" #include "debugcon.h" #include "frame_allocator.h" #include "logger.h" diff --git a/src/kernel/gdt.cpp b/src/kernel/gdt.cpp index 1ca20b8..179a014 100644 --- a/src/kernel/gdt.cpp +++ b/src/kernel/gdt.cpp @@ -1,9 +1,7 @@ -#include -#include - +#include #include -#include "assert.h" +#include "kassert.h" #include "cpu.h" #include "gdt.h" #include "logger.h" diff --git a/src/kernel/heap_allocator.cpp b/src/kernel/heap_allocator.cpp index a8e5fbc..eb62465 100644 --- a/src/kernel/heap_allocator.cpp +++ b/src/kernel/heap_allocator.cpp @@ -1,11 +1,10 @@ -#include #include -#include +#include #include #include -#include "assert.h" +#include "kassert.h" #include "heap_allocator.h" #include "memory.h" #include "objects/vm_area.h" diff --git a/src/kernel/hpet.cpp b/src/kernel/hpet.cpp index e96a9cc..be33a1f 100644 --- a/src/kernel/hpet.cpp +++ b/src/kernel/hpet.cpp @@ -1,4 +1,4 @@ -#include "assert.h" +#include "kassert.h" #include "device_manager.h" #include "hpet.h" #include "io.h" diff --git a/src/kernel/idt.cpp b/src/kernel/idt.cpp index 1c4f077..368098b 100644 --- a/src/kernel/idt.cpp +++ b/src/kernel/idt.cpp @@ -1,5 +1,4 @@ -#include - +#include #include #include "cpu.h" diff --git a/src/kernel/idt.h b/src/kernel/idt.h index 05a9666..99a7106 100644 --- a/src/kernel/idt.h +++ b/src/kernel/idt.h @@ -2,7 +2,7 @@ /// \file idt.h /// Definitions relating to a CPU's IDT table #include -#include "assert.h" +#include "kassert.h" class IDT { diff --git a/src/kernel/interrupts.cpp b/src/kernel/interrupts.cpp index 7de3710..89705a6 100644 --- a/src/kernel/interrupts.cpp +++ b/src/kernel/interrupts.cpp @@ -1,7 +1,7 @@ #include #include -#include "assert.h" +#include "kassert.h" #include "cpu.h" #include "device_manager.h" #include "idt.h" diff --git a/src/kernel/assert.cpp b/src/kernel/kassert.cpp similarity index 98% rename from src/kernel/assert.cpp rename to src/kernel/kassert.cpp index f607876..93f3ea7 100644 --- a/src/kernel/assert.cpp +++ b/src/kernel/kassert.cpp @@ -1,4 +1,4 @@ -#include "assert.h" +#include "kassert.h" #include "idt.h" namespace panic { diff --git a/src/kernel/assert.h b/src/kernel/kassert.h similarity index 100% rename from src/kernel/assert.h rename to src/kernel/kassert.h diff --git a/src/kernel/kernel.module b/src/kernel/kernel.module index 6680182..e62c43f 100644 --- a/src/kernel/kernel.module +++ b/src/kernel/kernel.module @@ -10,7 +10,7 @@ kernel = module("kernel", ld_script = "kernel.ld", sources = [ "apic.cpp", - "assert.cpp", + "kassert.cpp", "boot.s", "capabilities.cpp", "clock.cpp", diff --git a/src/kernel/kernel_main.cpp b/src/kernel/kernel_main.cpp index 4b3f5ad..2947aa6 100644 --- a/src/kernel/kernel_main.cpp +++ b/src/kernel/kernel_main.cpp @@ -1,11 +1,11 @@ #include #include -#include +#include #include #include -#include "assert.h" +#include "kassert.h" #include "capabilities.h" #include "cpu.h" #include "debugcon.h" diff --git a/src/kernel/logger.cpp b/src/kernel/logger.cpp index 5affb25..5bc63df 100644 --- a/src/kernel/logger.cpp +++ b/src/kernel/logger.cpp @@ -1,10 +1,8 @@ -#include -#include - +#include #include #include -#include "assert.h" +#include "kassert.h" #include "logger.h" #include "objects/system.h" #include "objects/thread.h" diff --git a/src/kernel/memory.cpp b/src/kernel/memory.cpp index 1a5c4c2..b00c48d 100644 --- a/src/kernel/memory.cpp +++ b/src/kernel/memory.cpp @@ -2,27 +2,4 @@ namespace std { enum class __attribute__ ((__type_visibility("default"))) align_val_t : size_t { }; -} - -// Implementation of memset and memcpy because we're not -// linking libc into the kernel -extern "C" { - -void * -memset(void *s, uint8_t v, size_t n) -{ - uint8_t *p = reinterpret_cast(s); - for (size_t i = 0; i < n; ++i) p[i] = v; - return s; -} - -void * -memcpy(void *dest, const void *src, size_t n) -{ - const uint8_t *s = reinterpret_cast(src); - uint8_t *d = reinterpret_cast(dest); - for (size_t i = 0; i < n; ++i) d[i] = s[i]; - return d; -} - -} +} \ No newline at end of file diff --git a/src/kernel/memory_bootstrap.cpp b/src/kernel/memory_bootstrap.cpp index 3bb5139..c0fc967 100644 --- a/src/kernel/memory_bootstrap.cpp +++ b/src/kernel/memory_bootstrap.cpp @@ -1,11 +1,8 @@ -#include -#include - #include #include #include -#include "assert.h" +#include "kassert.h" #include "capabilities.h" #include "device_manager.h" #include "frame_allocator.h" diff --git a/src/kernel/objects/kobject.cpp b/src/kernel/objects/kobject.cpp index 503028f..57d2308 100644 --- a/src/kernel/objects/kobject.cpp +++ b/src/kernel/objects/kobject.cpp @@ -1,7 +1,7 @@ #include #include -#include "assert.h" +#include "kassert.h" #include "logger.h" #include "objects/kobject.h" #include "objects/thread.h" diff --git a/src/kernel/objects/process.cpp b/src/kernel/objects/process.cpp index b2e3e9e..df742b0 100644 --- a/src/kernel/objects/process.cpp +++ b/src/kernel/objects/process.cpp @@ -1,8 +1,6 @@ -#include - #include -#include "assert.h" +#include "kassert.h" #include "capabilities.h" #include "cpu.h" #include "objects/process.h" diff --git a/src/kernel/objects/thread.cpp b/src/kernel/objects/thread.cpp index 9475092..4f9c9a6 100644 --- a/src/kernel/objects/thread.cpp +++ b/src/kernel/objects/thread.cpp @@ -1,5 +1,6 @@ #include +#include "kassert.h" #include "capabilities.h" #include "cpu.h" #include "logger.h" diff --git a/src/kernel/objects/vm_area.cpp b/src/kernel/objects/vm_area.cpp index c972c91..8679fe6 100644 --- a/src/kernel/objects/vm_area.cpp +++ b/src/kernel/objects/vm_area.cpp @@ -1,5 +1,5 @@ -#include "assert.h" +#include "kassert.h" #include "frame_allocator.h" #include "memory.h" #include "objects/vm_area.h" diff --git a/src/kernel/page_table.cpp b/src/kernel/page_table.cpp index dff83e0..8bd2290 100644 --- a/src/kernel/page_table.cpp +++ b/src/kernel/page_table.cpp @@ -1,7 +1,7 @@ -#include +#include #include -#include "assert.h" +#include "kassert.h" #include "memory.h" #include "frame_allocator.h" #include "page_table.h" diff --git a/src/kernel/page_tree.cpp b/src/kernel/page_tree.cpp index b6019ef..de920a3 100644 --- a/src/kernel/page_tree.cpp +++ b/src/kernel/page_tree.cpp @@ -1,8 +1,7 @@ -#include - +#include #include -#include "assert.h" +#include "kassert.h" #include "frame_allocator.h" #include "page_tree.h" diff --git a/src/kernel/panic.serial/main.cpp b/src/kernel/panic.serial/main.cpp index c2cc3df..c5e1833 100644 --- a/src/kernel/panic.serial/main.cpp +++ b/src/kernel/panic.serial/main.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include "cpu.h" diff --git a/src/kernel/scheduler.cpp b/src/kernel/scheduler.cpp index c959a1e..20af63c 100644 --- a/src/kernel/scheduler.cpp +++ b/src/kernel/scheduler.cpp @@ -3,7 +3,7 @@ #include #include "apic.h" -#include "assert.h" +#include "kassert.h" #include "clock.h" #include "cpu.h" #include "debug.h" diff --git a/src/kernel/slab_allocated.h b/src/kernel/slab_allocated.h index 942d54a..ee162ee 100644 --- a/src/kernel/slab_allocated.h +++ b/src/kernel/slab_allocated.h @@ -2,10 +2,11 @@ /// \file slab_allocated.h /// A parent template class for slab-allocated objects -#include +#include #include #include +#include "kassert.h" #include "memory.h" template diff --git a/src/kernel/syscall.cpp.cog b/src/kernel/syscall.cpp.cog index 2f173b2..ac69d90 100644 --- a/src/kernel/syscall.cpp.cog +++ b/src/kernel/syscall.cpp.cog @@ -1,6 +1,6 @@ // vim: ft=cpp #include -#include +#include #include "debug.h" diff --git a/src/kernel/syscalls/futex.cpp b/src/kernel/syscalls/futex.cpp index 965cd35..7132834 100644 --- a/src/kernel/syscalls/futex.cpp +++ b/src/kernel/syscalls/futex.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -20,11 +21,11 @@ struct futex futex() = default; futex(futex &&other) : address {other.address}, - queue {std::move(other.queue)} {} + queue {util::move(other.queue)} {} futex & operator=(futex &&other) { address = other.address; - queue = std::move(other.queue); + queue = util::move(other.queue); return *this; } }; diff --git a/src/kernel/syscalls/object.cpp b/src/kernel/syscalls/object.cpp index f838497..5e8a55b 100644 --- a/src/kernel/syscalls/object.cpp +++ b/src/kernel/syscalls/object.cpp @@ -2,7 +2,7 @@ #include #include -#include "assert.h" +#include "kassert.h" #include "logger.h" #include "objects/thread.h" #include "syscalls/helpers.h" diff --git a/src/kernel/sysconf.cpp b/src/kernel/sysconf.cpp index 41157af..bc80f25 100644 --- a/src/kernel/sysconf.cpp +++ b/src/kernel/sysconf.cpp @@ -1,6 +1,6 @@ -#include +#include -#include "assert.h" +#include "kassert.h" #include "cpu.h" #include "frame_allocator.h" #include "memory.h" diff --git a/src/kernel/tss.cpp b/src/kernel/tss.cpp index 8fc3a5d..9816678 100644 --- a/src/kernel/tss.cpp +++ b/src/kernel/tss.cpp @@ -1,7 +1,7 @@ -#include +#include #include -#include "assert.h" +#include "kassert.h" #include "cpu.h" #include "logger.h" #include "memory.h" diff --git a/src/kernel/vm_space.cpp b/src/kernel/vm_space.cpp index 5d2ed9a..9df3a5a 100644 --- a/src/kernel/vm_space.cpp +++ b/src/kernel/vm_space.cpp @@ -1,8 +1,7 @@ -#include - +#include #include -#include "assert.h" +#include "kassert.h" #include "frame_allocator.h" #include "logger.h" #include "memory.h" diff --git a/src/kernel/wait_queue.cpp b/src/kernel/wait_queue.cpp index f226073..09cc163 100644 --- a/src/kernel/wait_queue.cpp +++ b/src/kernel/wait_queue.cpp @@ -1,9 +1,9 @@ -#include +#include #include "objects/thread.h" #include "wait_queue.h" wait_queue::wait_queue(wait_queue &&other) : - m_threads {std::move(other.m_threads)} {} + m_threads {util::move(other.m_threads)} {} wait_queue::~wait_queue() { clear(); } @@ -11,7 +11,7 @@ wait_queue & wait_queue::operator=(wait_queue &&other) { clear(); - m_threads = std::move(other.m_threads); + m_threads = util::move(other.m_threads); return *this; } diff --git a/src/libraries/j6/channel.cpp b/src/libraries/j6/channel.cpp index b6f1858..d3e4ceb 100644 --- a/src/libraries/j6/channel.cpp +++ b/src/libraries/j6/channel.cpp @@ -2,13 +2,12 @@ // but should not include the user-specific code. #ifndef __j6kernel -#include - #include #include #include #include #include +#include #include #include #include diff --git a/src/libraries/libc/__j6libc/copy.h b/src/libraries/j6/copy.h similarity index 61% rename from src/libraries/libc/__j6libc/copy.h rename to src/libraries/j6/copy.h index 7453737..f5f5357 100644 --- a/src/libraries/libc/__j6libc/copy.h +++ b/src/libraries/j6/copy.h @@ -1,23 +1,10 @@ #pragma once -/** \file j6libc/copy.h - * Internal implementations to aid in implementing mem* functions - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#ifndef __cplusplus -#error "__j6libc/copy.h included by non-C++ code" -#endif +/// \file copy.h +/// Internal implementations to aid in implementing mem* functions #include -#include <__j6libc/size_t.h> -namespace __j6libc { +namespace j6 { template inline void do_copy(char *s1, const char *s2) { @@ -52,4 +39,4 @@ inline void do_backward_copy(char *s1, const char *s2, size_t n) { s1[i] = s2[i]; } -} // namespace __j6libc +} // namespace j6 diff --git a/src/libraries/j6/j6.module b/src/libraries/j6/j6.module index 1c34bae..eefc5a5 100644 --- a/src/libraries/j6/j6.module +++ b/src/libraries/j6/j6.module @@ -8,6 +8,7 @@ j6 = module("j6", "condition.cpp", "init.cpp", "init.s", + "memutils.cpp", "mutex.cpp", "protocol_ids.cpp", "syscalls.s.cog", @@ -23,6 +24,7 @@ j6 = module("j6", "j6/flags.h", "j6/init.h", "j6/mutex.hh", + "j6/memutils.h", "j6/protocols.h", "j6/protocols/service_locator.h", "j6/syscalls.h.cog", diff --git a/src/libraries/j6/j6/memutils.h b/src/libraries/j6/j6/memutils.h new file mode 100644 index 0000000..510e888 --- /dev/null +++ b/src/libraries/j6/j6/memutils.h @@ -0,0 +1,18 @@ +#pragma once +/// \file memutils.h +/// Standard mem*() library functions + +#include <__j6libc/restrict.h> +#include <__j6libc/size_t.h> + +#ifdef __cplusplus +extern "C" { +#endif + +void *memcpy(void * restrict s1, const void * restrict s2, size_t n); +void *memmove(void * restrict s1, const void * restrict s2, size_t n); +void *memset(void *s, int c, size_t n); + +#ifdef __cplusplus +} // extern "C" +#endif \ No newline at end of file diff --git a/src/libraries/libc/string/memset.cpp b/src/libraries/j6/memutils.cpp similarity index 61% rename from src/libraries/libc/string/memset.cpp rename to src/libraries/j6/memutils.cpp index c45ac0e..65b49ec 100644 --- a/src/libraries/libc/string/memset.cpp +++ b/src/libraries/j6/memutils.cpp @@ -1,19 +1,35 @@ -/** \file memset.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include +#include +#include #include <__j6libc/bits.h> #include <__j6libc/casts.h> +#include "copy.h" +using namespace j6; using namespace __j6libc; +void *memcpy(void * restrict s1, const void * restrict s2, size_t n) { + asm volatile ("rep movsb" : "+D"(s1), "+S"(s2), "+c"(n) :: "memory"); + return s1; +} + +static void memmove_dispatch(char *s1, const char *s2, size_t n) { + if (s1 == s2) return; + + if (s1 < s2 || s1 > s2 + n) + memcpy(s1, s2, n); + else + do_backward_copy(s1, s2, n); +} + +void *memmove(void * restrict s1, const void * restrict s2, size_t n) { + memmove_dispatch( + reinterpret_cast(s1), + reinterpret_cast(s1), + n); + + return s1; +} + #if __UINTPTR_WIDTH__ != 64 && __UINTPTR_WIDTH__ != 32 #error "memset: uintptr_t isn't 4 or 8 bytes" #endif @@ -28,7 +44,6 @@ static inline uintptr_t repval(uint8_t c) { return r; } - void *memset(void *s, int c, size_t n) { if (!s) return nullptr; @@ -54,4 +69,4 @@ void *memset(void *s, int c, size_t n) { *b++ = bval; return s; -} +} \ No newline at end of file diff --git a/src/libraries/j6/thread.cpp b/src/libraries/j6/thread.cpp index aa912e6..dc7c25c 100644 --- a/src/libraries/j6/thread.cpp +++ b/src/libraries/j6/thread.cpp @@ -2,9 +2,9 @@ // but should not include the user-specific code. #ifndef __j6kernel -#include #include #include +#include #include #include diff --git a/src/libraries/libc/libc.module b/src/libraries/libc/libc.module index c0c60e0..362565d 100644 --- a/src/libraries/libc/libc.module +++ b/src/libraries/libc/libc.module @@ -26,6 +26,7 @@ libc = module("libc", kind = "lib", deps = [ "j6" ], output = "libc.a", + include_phase = "late", sources = sources, public_headers = headers, ) diff --git a/src/libraries/libc/string/memcpy.cpp b/src/libraries/libc/string/memcpy.cpp deleted file mode 100644 index 47cb222..0000000 --- a/src/libraries/libc/string/memcpy.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/** \file memcpy.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include -#include - -void *memcpy(void * restrict s1, const void * restrict s2, size_t n) { - asm volatile ("rep movsb" : "+D"(s1), "+S"(s2), "+c"(n) :: "memory"); - return s1; -} diff --git a/src/libraries/libc/string/memmove.cpp b/src/libraries/libc/string/memmove.cpp deleted file mode 100644 index ff33f92..0000000 --- a/src/libraries/libc/string/memmove.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/** \file memmove.cpp - * - * This file is part of the C standard library for the jsix operating - * system. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include -#include <__j6libc/copy.h> - -using namespace __j6libc; - -void memmove_dispatch(char *s1, const char *s2, size_t n) { - if (s1 == s2) return; - - if (s1 < s2 || s1 > s2 + n) - memcpy(s1, s2, n); - else - do_backward_copy(s1, s2, n); -} - -void *memmove(void * restrict s1, const void * restrict s2, size_t n) { - memmove_dispatch( - reinterpret_cast(s1), - reinterpret_cast(s2), - n); - - return s1; -} - diff --git a/src/libraries/libc/__j6libc/bits.h b/src/libraries/libc_free/__j6libc/bits.h similarity index 100% rename from src/libraries/libc/__j6libc/bits.h rename to src/libraries/libc_free/__j6libc/bits.h diff --git a/src/libraries/libc/__j6libc/casts.h b/src/libraries/libc_free/__j6libc/casts.h similarity index 100% rename from src/libraries/libc/__j6libc/casts.h rename to src/libraries/libc_free/__j6libc/casts.h diff --git a/src/libraries/libc/__j6libc/null.h b/src/libraries/libc_free/__j6libc/null.h similarity index 100% rename from src/libraries/libc/__j6libc/null.h rename to src/libraries/libc_free/__j6libc/null.h diff --git a/src/libraries/libc/__j6libc/restrict.h b/src/libraries/libc_free/__j6libc/restrict.h similarity index 100% rename from src/libraries/libc/__j6libc/restrict.h rename to src/libraries/libc_free/__j6libc/restrict.h diff --git a/src/libraries/libc/__j6libc/size_t.h b/src/libraries/libc_free/__j6libc/size_t.h similarity index 100% rename from src/libraries/libc/__j6libc/size_t.h rename to src/libraries/libc_free/__j6libc/size_t.h diff --git a/src/libraries/libc/__j6libc/wchar_t.h b/src/libraries/libc_free/__j6libc/wchar_t.h similarity index 100% rename from src/libraries/libc/__j6libc/wchar_t.h rename to src/libraries/libc_free/__j6libc/wchar_t.h diff --git a/src/libraries/libc/float.h b/src/libraries/libc_free/float.h similarity index 100% rename from src/libraries/libc/float.h rename to src/libraries/libc_free/float.h diff --git a/src/libraries/libc/iso646.h b/src/libraries/libc_free/iso646.h similarity index 100% rename from src/libraries/libc/iso646.h rename to src/libraries/libc_free/iso646.h diff --git a/src/libraries/libc_free/libc_free.module b/src/libraries/libc_free/libc_free.module new file mode 100644 index 0000000..81d6967 --- /dev/null +++ b/src/libraries/libc_free/libc_free.module @@ -0,0 +1,24 @@ +# vim: ft=python + +libc_free = module("libc_free", + kind = "lib", + no_libc = True, + include_phase = "late", + public_headers = [ + 'float.h', + 'iso646.h', + 'limits.h', + 'stdalign.h', + 'stdarg.h', + 'stdbool.h', + 'stddef.h', + 'stdint.h.cog', + 'stdnoreturn.h', + + '__j6libc/bits.h', + '__j6libc/casts.h', + '__j6libc/null.h', + '__j6libc/restrict.h', + '__j6libc/size_t.h', + '__j6libc/wchar_t.h', + ]) diff --git a/src/libraries/libc/limits.h b/src/libraries/libc_free/limits.h similarity index 100% rename from src/libraries/libc/limits.h rename to src/libraries/libc_free/limits.h diff --git a/src/libraries/libc/stdalign.h b/src/libraries/libc_free/stdalign.h similarity index 100% rename from src/libraries/libc/stdalign.h rename to src/libraries/libc_free/stdalign.h diff --git a/src/libraries/libc/stdarg.h b/src/libraries/libc_free/stdarg.h similarity index 100% rename from src/libraries/libc/stdarg.h rename to src/libraries/libc_free/stdarg.h diff --git a/src/libraries/libc/stdbool.h b/src/libraries/libc_free/stdbool.h similarity index 100% rename from src/libraries/libc/stdbool.h rename to src/libraries/libc_free/stdbool.h diff --git a/src/libraries/libc/stddef.h b/src/libraries/libc_free/stddef.h similarity index 100% rename from src/libraries/libc/stddef.h rename to src/libraries/libc_free/stddef.h diff --git a/src/libraries/libc/stdint.h.cog b/src/libraries/libc_free/stdint.h.cog similarity index 100% rename from src/libraries/libc/stdint.h.cog rename to src/libraries/libc_free/stdint.h.cog diff --git a/src/libraries/libc/stdnoreturn.h b/src/libraries/libc_free/stdnoreturn.h similarity index 100% rename from src/libraries/libc/stdnoreturn.h rename to src/libraries/libc_free/stdnoreturn.h diff --git a/src/libraries/util/util.module b/src/libraries/util/util.module index 72648ad..11d5e6f 100644 --- a/src/libraries/util/util.module +++ b/src/libraries/util/util.module @@ -10,6 +10,7 @@ module("util", ], public_headers = [ "util/allocator.h", + "util/assert.h", "util/basic_types.h", "util/bip_buffer.h", "util/bitset.h", @@ -22,6 +23,7 @@ module("util", "util/linked_list.h", "util/map.h", "util/misc.h", + "util/new.h", "util/no_construct.h", "util/node_map.h", "util/pointers.h", diff --git a/src/libraries/util/util/assert.h b/src/libraries/util/util/assert.h new file mode 100644 index 0000000..a0c7113 --- /dev/null +++ b/src/libraries/util/util/assert.h @@ -0,0 +1,13 @@ +#pragma once +/// \file assert.h +/// Utility header to include the right assert.h for the environment + +#if __has_include("kassert.h") + #include "kassert.h" +#elif __has_include() + #include +#endif + +#if !defined(assert) + #define assert(x) ((void)0) +#endif \ No newline at end of file diff --git a/src/libraries/util/util/basic_types.h b/src/libraries/util/util/basic_types.h index 850f017..7494fc5 100644 --- a/src/libraries/util/util/basic_types.h +++ b/src/libraries/util/util/basic_types.h @@ -73,4 +73,21 @@ template struct sized_uint { using type = typename sized_uint_type::type; }; +template struct remove_reference { using type = T; }; +template struct remove_reference { using type = T; }; +template struct remove_reference { using type = T; }; + } // namespace types + +namespace util { + +template +typename types::remove_reference::type&& +move( T&& x ) { return (typename types::remove_reference::type&&)x; } + +template T&& forward(typename types::remove_reference::type&& param) { return static_cast(param); } +template T&& forward(typename types::remove_reference::type& param) { return static_cast(param); } + +template void swap(T &t1, T &t2) { T tmp = move(t1); t1 = move(t2); t2 = move(tmp); } + +} // namespace util \ No newline at end of file diff --git a/src/libraries/util/util/deque.h b/src/libraries/util/util/deque.h index ed947ea..76a9f4a 100644 --- a/src/libraries/util/util/deque.h +++ b/src/libraries/util/util/deque.h @@ -2,9 +2,9 @@ /// \file linked_list.h /// A generic templatized linked list. -#include -#include -#include +#include +#include +#include #include namespace util { @@ -45,7 +45,7 @@ public: deque(deque &&other) : m_first {other.m_first}, m_next {other.m_next}, - m_list {std::move(other.m_list)} {} + m_list {util::move(other.m_list)} {} ~deque() { clear(); } @@ -53,7 +53,7 @@ public: clear(); m_first = other.m_first; m_next = other.m_next; - m_list = std::move(other.m_list); + m_list = util::move(other.m_list); return *this; } diff --git a/src/libraries/util/util/map.h b/src/libraries/util/util/map.h index fd1c23b..89aee1f 100644 --- a/src/libraries/util/util/map.h +++ b/src/libraries/util/util/map.h @@ -13,8 +13,8 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/libraries/util/util/new.h b/src/libraries/util/util/new.h new file mode 100644 index 0000000..1d65686 --- /dev/null +++ b/src/libraries/util/util/new.h @@ -0,0 +1,14 @@ +#pragma once +/// \file new.h +/// Declarations for `operator new` to avoid including + +#include + +void *operator new (size_t); +void *operator new [] (size_t); +void *operator new (size_t, void *) noexcept; +void *operator new [] (size_t, void *) noexcept; +void operator delete (void *) noexcept; +void operator delete [] (void *) noexcept; +void operator delete (void *, void *) noexcept; +void operator delete [] (void *, void *) noexcept; \ No newline at end of file diff --git a/src/libraries/util/util/node_map.h b/src/libraries/util/util/node_map.h index e7e34b4..677f3cc 100644 --- a/src/libraries/util/util/node_map.h +++ b/src/libraries/util/util/node_map.h @@ -13,10 +13,10 @@ /// http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/ #include -#include -#include +#include #include +#include #include #include @@ -118,7 +118,7 @@ public: node_type new_node; get_map_key(new_node) = key; - return insert(std::move(new_node)); + return insert(util::move(new_node)); } node_type * find(const key_type &key) { @@ -150,7 +150,7 @@ public: key_type &key_at_slot = get_map_key(node_at_slot); if (open(key_at_slot)) { - node_at_slot = std::move(node); + node_at_slot = util::move(node); if (!found) inserted_at = slot; return m_nodes[inserted_at]; @@ -162,7 +162,7 @@ public: found = true; inserted_at = slot; } - std::swap(node, node_at_slot); + util::swap(node, node_at_slot); dist = psl_at_slot; } @@ -202,7 +202,7 @@ protected: if (open(next_key) || psl(next_key, next_slot) == 0) return; - m_nodes[slot] = std::move(next); + m_nodes[slot] = util::move(next); next.~node_type(); next_key = invalid_id; ++slot; @@ -236,7 +236,7 @@ protected: continue; --m_count; - insert(std::move(node)); + insert(util::move(node)); node.~node_type(); key = invalid_id; } @@ -297,7 +297,7 @@ public: bool add(item_type item) { if (contains(item)) return false; - m_map.insert(std::move(item)); + m_map.insert(util::move(item)); return true; } diff --git a/src/libraries/util/util/radix_tree.h b/src/libraries/util/util/radix_tree.h index 62d76b0..fc7a18c 100644 --- a/src/libraries/util/util/radix_tree.h +++ b/src/libraries/util/util/radix_tree.h @@ -3,7 +3,7 @@ /// Definition of a generic radix_tree structure #include -#include +#include #include namespace util { diff --git a/src/libraries/util/util/vector.h b/src/libraries/util/util/vector.h index 9c5f544..faa9d06 100644 --- a/src/libraries/util/util/vector.h +++ b/src/libraries/util/util/vector.h @@ -2,12 +2,11 @@ /// \file vector.h /// Definition of a simple dynamic vector collection for use in kernel space -#include -#include -#include -#include - +#include #include +#include +#include +#include #include namespace util { @@ -123,7 +122,7 @@ public: T & append(T &&item) { ensure_capacity(m_size + 1); - m_elements[m_size] = std::move(item); + m_elements[m_size] = util::move(item); return m_elements[m_size++]; } @@ -133,7 +132,7 @@ public: T & emplace(Args&&... args) { ensure_capacity(m_size + 1); - new (&m_elements[m_size]) T(std::forward(args)...); + new (&m_elements[m_size]) T(util::forward(args)...); return m_elements[m_size++]; }