diff --git a/Makefile b/Makefile index 85c943f..a1c9368 100644 --- a/Makefile +++ b/Makefile @@ -15,11 +15,14 @@ OBJCOPY := $(PREFIX)objcopy BUILD_DIR := build REPORTS_DIR := $(BUILD_DIR)/reports INITRDS_DIR := initrds -BUSYBOX_ROOTFS_DIR := $(BUILD_DIR)/rootfs-busybox -BASH_ROOTFS_DIR := $(BUILD_DIR)/rootfs-bash -BUSYBOX_CPIO := $(INITRDS_DIR)/busybox.cpio -BASH_CPIO := $(INITRDS_DIR)/bash.cpio -INITRD ?= $(BUSYBOX_CPIO) +BUSYBOX_ROOTFS_DIR := $(BUILD_DIR)/rootfs-busybox +BASH_ROOTFS_DIR := $(BUILD_DIR)/rootfs-bash +DEBIAN_BASH_ROOTFS_DIR := $(BUILD_DIR)/rootfs-debian-bash +DEBIAN_BASH_DEBS_DIR := $(BUILD_DIR)/debian-bash-debs +BUSYBOX_CPIO := $(INITRDS_DIR)/busybox.cpio +BASH_CPIO := $(INITRDS_DIR)/bash.cpio +DEBIAN_BASH_CPIO := $(INITRDS_DIR)/debian-bash.cpio +INITRD ?= $(BUSYBOX_CPIO) # Freestanding C++ flags CXXFLAGS := -std=c++20 -ffreestanding -fsized-deallocation -O2 -Wall -Wextra -fno-use-cxa-atexit \ @@ -83,7 +86,7 @@ HOST_COV_DEPS := $(HOST_COV_OBJS:.o=.d) HOST_COV_BIN := $(HOST_COV_DIR)/kernel_host_coverage .PHONY: all clean distclean clean-tests run debug gdb lint format format-check lint-report license-check quality \ - test-host compile_commands.json ai-dump setup-musl setup-busybox setup-bash setup-hooks loc export-ddk + test-host compile_commands.json ai-dump setup-musl setup-busybox setup-bash setup-debian-bash setup-hooks loc export-ddk all: $(BUILD_DIR)/$(TARGET) $(INITRD) @@ -343,6 +346,27 @@ $(BASH_CPIO): $(BUILD_DIR)/user/hello $(MUSL_USER_BIN) $(DYN_USER_BIN) $(FB_USER @cd $(BASH_ROOTFS_DIR) && find . | cpio -o -H newc > ../../$(BASH_CPIO) 2>/dev/null setup-bash: $(BASH_CPIO) +# --- Precompiled Debian arm64 GNU Bash Initrd (initrds/debian-bash.cpio) --- +$(DEBIAN_BASH_CPIO): $(BUILD_DIR)/user/hello $(MUSL_USER_BIN) $(DYN_USER_BIN) $(FB_USER_BIN) $(EVDEV_USER_BIN) $(UNIX_SRV_BIN) $(UNIX_CLI_BIN) $(UNIX_SMOKE_BIN) $(TEST_MILO_USER_BIN) scripts/setup_debian_bash.sh + @rm -rf $(DEBIAN_BASH_ROOTFS_DIR) + @mkdir -p $(INITRDS_DIR) $(DEBIAN_BASH_ROOTFS_DIR)/bin $(DEBIAN_BASH_ROOTFS_DIR)/lib $(DEBIAN_BASH_ROOTFS_DIR)/dev $(DEBIAN_BASH_ROOTFS_DIR)/proc $(DEBIAN_BASH_ROOTFS_DIR)/tmp + @echo "milo-initrd-v1" > $(DEBIAN_BASH_ROOTFS_DIR)/init.txt + @install -m 0755 $(BUILD_DIR)/user/hello $(DEBIAN_BASH_ROOTFS_DIR)/bin/hello + @for bin in hello_musl hello_dyn fb_test evdev_smoke unix_echo_server unix_echo_client unix_sock_smoke test_milo; do \ + if [ -f "$(BUILD_DIR)/user/$$bin" ]; then \ + install -m 0755 "$(BUILD_DIR)/user/$$bin" "$(DEBIAN_BASH_ROOTFS_DIR)/bin/$$bin"; \ + fi; \ + done + @if [ -f "$(MUSL_SYSROOT)/lib/libc.so" ]; then \ + install -m 0755 $(MUSL_SYSROOT)/lib/libc.so $(DEBIAN_BASH_ROOTFS_DIR)/lib/libc.so && \ + ln -sf libc.so $(DEBIAN_BASH_ROOTFS_DIR)/lib/ld-musl-aarch64.so.1 && \ + ln -sf ld-musl-aarch64.so.1 $(DEBIAN_BASH_ROOTFS_DIR)/lib/ld.so; \ + fi + @bash scripts/setup_debian_bash.sh $(DEBIAN_BASH_DEBS_DIR) $(DEBIAN_BASH_ROOTFS_DIR) + @cd $(DEBIAN_BASH_ROOTFS_DIR) && find . | cpio -o -H newc > ../../$(DEBIAN_BASH_CPIO) 2>/dev/null + +setup-debian-bash: $(DEBIAN_BASH_CPIO) + -include $(DEPS) -include $(HOST_TEST_DEPS) -include $(HOST_COV_DEPS) @@ -351,7 +375,7 @@ clean: rm -rf $(BUILD_DIR)/kernel $(BUILD_DIR)/arch $(BUILD_DIR)/drivers $(DDK_EXPORT_DIR) \ $(BUILD_DIR)/$(TARGET) $(BUILD_DIR)/kernel.elf \ $(HOST_BUILD_DIR) $(HOST_COV_DIR) $(REPORTS_DIR) \ - $(BUSYBOX_ROOTFS_DIR) $(BASH_ROOTFS_DIR) arch/memory_config.hpp + $(BUSYBOX_ROOTFS_DIR) $(BASH_ROOTFS_DIR) $(DEBIAN_BASH_ROOTFS_DIR) arch/memory_config.hpp distclean: rm -rf $(BUILD_DIR) arch/memory_config.hpp compile_commands.json $(DISK_IMG) \ diff --git a/arch/aarch64-linux-gnu/arch.mk b/arch/aarch64-linux-gnu/arch.mk index 492ab6b..c33d09b 100644 --- a/arch/aarch64-linux-gnu/arch.mk +++ b/arch/aarch64-linux-gnu/arch.mk @@ -9,4 +9,4 @@ SRCS_CXX += arch/$(ARCH)/uart_console.cpp \ arch/$(ARCH)/trap_aarch64.cpp \ arch/$(ARCH)/arch_test.cpp \ kernel/hal/targets/qemu_virt/timer.cpp -CXXFLAGS += -mcpu=cortex-a53 -mno-outline-atomics \ No newline at end of file +CXXFLAGS += -mcpu=cortex-a53 -mno-outline-atomics -mgeneral-regs-only \ No newline at end of file diff --git a/arch/aarch64-linux-gnu/boot.s b/arch/aarch64-linux-gnu/boot.s index 4b804a8..eab719e 100644 --- a/arch/aarch64-linux-gnu/boot.s +++ b/arch/aarch64-linux-gnu/boot.s @@ -85,8 +85,8 @@ vector_table: .text .balign 4 -trap_sync_common: - sub sp, sp, #288 +.macro save_trap_frame + sub sp, sp, #816 stp x0, x1, [sp, #0] stp x2, x3, [sp, #16] stp x4, x5, [sp, #32] @@ -112,6 +112,31 @@ trap_sync_common: str x2, [sp, #264] mrs x3, tpidr_el0 str x3, [sp, #272] + mrs x0, fpcr + str x0, [sp, #280] + mrs x1, fpsr + str x1, [sp, #288] + + stp q0, q1, [sp, #304] + stp q2, q3, [sp, #336] + stp q4, q5, [sp, #368] + stp q6, q7, [sp, #400] + stp q8, q9, [sp, #432] + stp q10, q11, [sp, #464] + stp q12, q13, [sp, #496] + stp q14, q15, [sp, #528] + stp q16, q17, [sp, #560] + stp q18, q19, [sp, #592] + stp q20, q21, [sp, #624] + stp q22, q23, [sp, #656] + stp q24, q25, [sp, #688] + stp q26, q27, [sp, #720] + stp q28, q29, [sp, #752] + stp q30, q31, [sp, #784] +.endm + +trap_sync_common: + save_trap_frame mov x0, sp // arg1: HAL::CpuContext* ctx mrs x1, esr_el1 // arg2: uint64_t esr @@ -120,38 +145,34 @@ trap_sync_common: b user_trap_exit trap_irq_common: - sub sp, sp, #288 - stp x0, x1, [sp, #0] - stp x2, x3, [sp, #16] - stp x4, x5, [sp, #32] - stp x6, x7, [sp, #48] - stp x8, x9, [sp, #64] - stp x10, x11, [sp, #80] - stp x12, x13, [sp, #96] - stp x14, x15, [sp, #112] - stp x16, x17, [sp, #128] - stp x18, x19, [sp, #144] - stp x20, x21, [sp, #160] - stp x22, x23, [sp, #176] - stp x24, x25, [sp, #192] - stp x26, x27, [sp, #208] - stp x28, x29, [sp, #224] - str x30, [sp, #240] - - mrs x0, sp_el0 - str x0, [sp, #248] - mrs x1, elr_el1 - str x1, [sp, #256] - mrs x2, spsr_el1 - str x2, [sp, #264] - mrs x3, tpidr_el0 - str x3, [sp, #272] + save_trap_frame mov x0, sp // arg1: HAL::CpuContext* ctx bl aarch64_handle_irq_exception .global user_trap_exit user_trap_exit: + ldp q0, q1, [sp, #304] + ldp q2, q3, [sp, #336] + ldp q4, q5, [sp, #368] + ldp q6, q7, [sp, #400] + ldp q8, q9, [sp, #432] + ldp q10, q11, [sp, #464] + ldp q12, q13, [sp, #496] + ldp q14, q15, [sp, #528] + ldp q16, q17, [sp, #560] + ldp q18, q19, [sp, #592] + ldp q20, q21, [sp, #624] + ldp q22, q23, [sp, #656] + ldp q24, q25, [sp, #688] + ldp q26, q27, [sp, #720] + ldp q28, q29, [sp, #752] + ldp q30, q31, [sp, #784] + + ldr x0, [sp, #280] + msr fpcr, x0 + ldr x1, [sp, #288] + msr fpsr, x1 ldr x0, [sp, #248] msr sp_el0, x0 ldr x1, [sp, #256] @@ -177,36 +198,11 @@ user_trap_exit: ldp x26, x27, [sp, #208] ldp x28, x29, [sp, #224] ldr x30, [sp, #240] - add sp, sp, #288 + add sp, sp, #816 eret trap_invalid_common: - sub sp, sp, #288 - stp x0, x1, [sp, #0] - stp x2, x3, [sp, #16] - stp x4, x5, [sp, #32] - stp x6, x7, [sp, #48] - stp x8, x9, [sp, #64] - stp x10, x11, [sp, #80] - stp x12, x13, [sp, #96] - stp x14, x15, [sp, #112] - stp x16, x17, [sp, #128] - stp x18, x19, [sp, #144] - stp x20, x21, [sp, #160] - stp x22, x23, [sp, #176] - stp x24, x25, [sp, #192] - stp x26, x27, [sp, #208] - stp x28, x29, [sp, #224] - str x30, [sp, #240] - - mrs x0, sp_el0 - str x0, [sp, #248] - mrs x1, elr_el1 - str x1, [sp, #256] - mrs x2, spsr_el1 - str x2, [sp, #264] - mrs x3, tpidr_el0 - str x3, [sp, #272] + save_trap_frame mov x0, sp // arg1: HAL::CpuContext* ctx mov x1, x9 // arg2: uint64_t type @@ -302,6 +298,27 @@ enter_user_context: msr spsr_el1, x1 ldr x1, [x9, #272] msr tpidr_el0, x1 + ldr x1, [x9, #280] + msr fpcr, x1 + ldr x1, [x9, #288] + msr fpsr, x1 + + ldp q0, q1, [x9, #304] + ldp q2, q3, [x9, #336] + ldp q4, q5, [x9, #368] + ldp q6, q7, [x9, #400] + ldp q8, q9, [x9, #432] + ldp q10, q11, [x9, #464] + ldp q12, q13, [x9, #496] + ldp q14, q15, [x9, #528] + ldp q16, q17, [x9, #560] + ldp q18, q19, [x9, #592] + ldp q20, q21, [x9, #624] + ldp q22, q23, [x9, #656] + ldp q24, q25, [x9, #688] + ldp q26, q27, [x9, #720] + ldp q28, q29, [x9, #752] + ldp q30, q31, [x9, #784] ldp x0, x1, [x9, #0] ldp x2, x3, [x9, #16] diff --git a/arch/aarch64-linux-gnu/cpu.cpp b/arch/aarch64-linux-gnu/cpu.cpp index 880dab2..5b42d83 100644 --- a/arch/aarch64-linux-gnu/cpu.cpp +++ b/arch/aarch64-linux-gnu/cpu.cpp @@ -123,7 +123,13 @@ class AArch64CpuControl : public HAL::CpuControl { arch_ctx->elr_el1 = entry_point; arch_ctx->spsr_el1 = 0x00ULL; // EL0t mode with DAIF unmasked arch_ctx->tpidr_el0 = 0ULL; + arch_ctx->fpcr = 0ULL; + arch_ctx->fpsr = 0ULL; arch_ctx->_pad = 0ULL; + for (size_t idx = 0U; idx < 32U; ++idx) { + arch_ctx->q[idx][0] = 0ULL; + arch_ctx->q[idx][1] = 0ULL; + } } void set_user_tls_pointer(HAL::CpuContext& ctx, uintptr_t tls) const noexcept override { diff --git a/arch/aarch64-linux-gnu/cpu_context.hpp b/arch/aarch64-linux-gnu/cpu_context.hpp index 526f1cf..4e5872b 100644 --- a/arch/aarch64-linux-gnu/cpu_context.hpp +++ b/arch/aarch64-linux-gnu/cpu_context.hpp @@ -13,7 +13,10 @@ struct alignas(16) AArch64CpuContext { uint64_t elr_el1; // Exception Link Register (offset 256) uint64_t spsr_el1; // Saved Program Status Register (offset 264) uint64_t tpidr_el0; // EL0 Thread Pointer / TLS base (offset 272) - uint64_t _pad; // 16-byte alignment padding (offset 280) + uint64_t fpcr; // Floating-point Control Register (offset 280) + uint64_t fpsr; // Floating-point Status Register (offset 288) + uint64_t _pad; // 16-byte alignment padding for q[] (offset 296) + uint64_t q[32][2]; // SIMD/FP registers q0-q31 (offsets 304..815) }; static_assert(sizeof(AArch64CpuContext) == sizeof(HAL::CpuContext), diff --git a/arch/aarch64-linux-gnu/user_test.s b/arch/aarch64-linux-gnu/user_test.s index 24d2a27..194ad26 100644 --- a/arch/aarch64-linux-gnu/user_test.s +++ b/arch/aarch64-linux-gnu/user_test.s @@ -526,10 +526,10 @@ el0_user_mm_suite_start: add x19, x19, #1 13: - // 7) mmap with unaligned length (1234) -> -22 (-EINVAL) + // 7a) mmap with zero length (0) -> -22 (-EINVAL) mov x8, #222 // SYS_MMAP mov x0, #0 - mov x1, #1234 + mov x1, #0 mov x2, #0x3 mov x3, #0x22 mov x4, #-1 @@ -540,6 +540,24 @@ el0_user_mm_suite_start: add x19, x19, #1 14: + // 7b) mmap with unaligned length (1234) -> rounds up to 1 page, then munmap(addr, 1234) -> 0 + mov x8, #222 // SYS_MMAP + mov x0, #0 + mov x1, #1234 + mov x2, #0x3 + mov x3, #0x22 + mov x4, #-1 + mov x5, #0 + svc #0 + tbnz x0, #63, 141f + mov x8, #215 // SYS_MUNMAP + mov x1, #1234 + svc #0 + cbz x0, 142f +141: + add x19, x19, #1 +142: + // 8) mmap without MAP_ANONYMOUS (flags = MAP_PRIVATE = 0x2) -> -22 (-EINVAL) mov x8, #222 // SYS_MMAP mov x0, #0 @@ -1073,4 +1091,4 @@ el0_demand_paging_suite_end: el0_uxn_branch_to_stack_start: mov x0, sp br x0 -el0_uxn_branch_to_stack_end: \ No newline at end of file +el0_uxn_branch_to_stack_end: diff --git a/drivers/ddk.mk b/drivers/ddk.mk index 293272b..d806f00 100644 --- a/drivers/ddk.mk +++ b/drivers/ddk.mk @@ -13,5 +13,5 @@ DDK_CXXFLAGS := -std=c++20 -ffreestanding -fsized-deallocation -O2 -Wall -Wextra -fno-use-cxa-atexit -Werror -Wpedantic -Wshadow -Wconversion \ -Wsign-conversion -Wnull-dereference -fno-exceptions -fno-rtti \ -fno-threadsafe-statics -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 \ - -mcpu=cortex-a53 -mno-outline-atomics -MMD -MP -g \ + -mcpu=cortex-a53 -mno-outline-atomics -mgeneral-regs-only -MMD -MP -g \ -I$(MILO_DDK_DIR)/include diff --git a/kernel/hal/cpu.hpp b/kernel/hal/cpu.hpp index dc4a7ba..76dc136 100644 --- a/kernel/hal/cpu.hpp +++ b/kernel/hal/cpu.hpp @@ -7,11 +7,11 @@ namespace HAL { -// Opaque 288-byte architecture trap/execution register frame. +// Opaque 816-byte architecture trap/execution register frame (GPRs + sysregs + FP/SIMD). struct alignas(16) CpuContext { - uint64_t opaque[36]; + uint64_t opaque[102]; }; -static_assert(sizeof(CpuContext) == 288U, "CpuContext must be 288 bytes"); +static_assert(sizeof(CpuContext) == 816U, "CpuContext must be 816 bytes"); // Opaque 112-byte EL1 callee-saved kernel thread context (x19..x30 + sp). struct alignas(16) KernelContext { diff --git a/kernel/mm/AddressSpace.cpp b/kernel/mm/AddressSpace.cpp index 0b50434..6b8a457 100644 --- a/kernel/mm/AddressSpace.cpp +++ b/kernel/mm/AddressSpace.cpp @@ -412,6 +412,14 @@ uintptr_t AddressSpace::current_root_paddr() noexcept { void AddressSpace::destroy() noexcept { clear_vmas(); if (m_root_paddr != 0U) { + const auto platform = HAL::get_platform_info(); + const uintptr_t sigreturn_vaddr = platform.user_stack_top; + uintptr_t tramp_paddr = 0U; + HAL::PageFlags tramp_flags = HAL::PageFlags::None; + if (query_user_page(sigreturn_vaddr, tramp_paddr, tramp_flags)) { + (void)unmap_user_page(sigreturn_vaddr); + Kernel::MM::page_put(tramp_paddr & ~(platform.page_size - 1U)); + } HAL::AddressSpaceBackend::destroy_root(m_root_paddr); m_root_paddr = 0U; } diff --git a/kernel/process/Signal.cpp b/kernel/process/Signal.cpp index b415d5a..b2bcc12 100644 --- a/kernel/process/Signal.cpp +++ b/kernel/process/Signal.cpp @@ -5,6 +5,7 @@ #include "hal/cpu.hpp" #include "mm/AddressSpace.hpp" +#include "mm/pfa.hpp" #include "process/Process.hpp" #include "process/scheduler.hpp" #include "sync/IrqGuard.hpp" @@ -285,19 +286,39 @@ void deliver_pending_signals(HAL::CpuContext& ctx) noexcept { exit_current_process(128 + static_cast(SIG_KILL)); } - // Flush instruction cache if fallback stack trampoline is used - uintptr_t tramp_paddr = 0U; - HAL::PageFlags tramp_flags = HAL::PageFlags::None; - const uintptr_t tramp_vaddr = frame_vaddr + offsetof(linux_rt_sigframe, trampoline); - if (proc->address_space().query_user_page(tramp_vaddr, tramp_paddr, tramp_flags)) { - cpu.flush_dcache_range(MM::phys_to_virt(tramp_paddr), sizeof(frame.trampoline)); - cpu.invalidate_icache(); + // Ensure a W^X-compliant R+X sigreturn trampoline page when SA_RESTORER is not provided + uintptr_t restorer_lr = 0U; + if ((act.flags & SA_FLAG_RESTORER) != 0ULL && act.restorer != 0ULL) { + restorer_lr = static_cast(act.restorer); + } else { + const auto platform = HAL::get_platform_info(); + const uintptr_t tramp_page_vaddr = platform.user_stack_top; + uintptr_t existing_paddr = 0U; + HAL::PageFlags existing_flags = HAL::PageFlags::None; + if (!proc->address_space().query_user_page(tramp_page_vaddr, existing_paddr, + existing_flags)) { + const uintptr_t tramp_frame = MM::alloc_frame(); + if (tramp_frame == 0U) { + exit_current_process(128 + static_cast(SIG_KILL)); + } + auto* const tramp_kva = reinterpret_cast(MM::phys_to_virt(tramp_frame)); + for (size_t idx = 0U; idx < platform.page_size; ++idx) { + tramp_kva[idx] = 0U; + } + cpu.write_sigreturn_trampoline(reinterpret_cast(tramp_kva), + platform.page_size / sizeof(uint32_t)); + cpu.flush_dcache_range(reinterpret_cast(tramp_kva), platform.page_size); + if (!proc->address_space().map_user_page(tramp_page_vaddr, tramp_frame, + HAL::PageFlags::User | HAL::PageFlags::Read | + HAL::PageFlags::Execute)) { + MM::free_frame(tramp_frame); + exit_current_process(128 + static_cast(SIG_KILL)); + } + cpu.invalidate_icache(); + } + restorer_lr = tramp_page_vaddr; } - const uintptr_t restorer_lr = ((act.flags & SA_FLAG_RESTORER) != 0ULL && act.restorer != 0ULL) - ? static_cast(act.restorer) - : tramp_vaddr; - const uint64_t block_self = ((act.flags & Kernel::UAPI::SA_NODEFER) != 0ULL) ? 0ULL : (1ULL << (sig - 1U)); proc->signals().block(act.mask | block_self); diff --git a/kernel/shell/shell.cpp b/kernel/shell/shell.cpp index 4165231..cf7ff58 100644 --- a/kernel/shell/shell.cpp +++ b/kernel/shell/shell.cpp @@ -3,6 +3,7 @@ #include "boot/fdt.hpp" #include "core/log.hpp" +#include "fs/Vfs.hpp" #include "fs/cpio.hpp" #include "hal/console.hpp" #include "hal/cpu.hpp" @@ -295,12 +296,89 @@ void launch_initrd_shell(const char* bin_path, const char* argv0, const char* sh return; } + const auto parsed_main = Kernel::Loader::Elf64View::parse(entry.data, entry.size); + if (!parsed_main) { + Kernel::Log::error(kShellTag, "Invalid ELF64 image for {}.", bin_path); + return; + } + + char pt_interp_path[Kernel::Process::MAX_PATH_LEN]{}; + const bool has_pt_interp = + parsed_main.value().extract_interpreter_path(pt_interp_path, sizeof(pt_interp_path)); + auto load_res = Kernel::Loader::load_elf(entry.data, entry.size); if (!load_res.is_ok()) { Kernel::Log::error(kShellTag, "Failed to load ELF64 image for {}.", bin_path); return; } + uintptr_t at_base = 0U; + uintptr_t initial_pc = load_res->entry_point; + const uintptr_t min_base_hint = + (load_res->max_vaddr != 0U) ? load_res->max_vaddr : HAL::get_platform_info().user_brk_base; + + if (has_pt_interp) { + Kernel::FS::File* interp_file = nullptr; + const int32_t orc = Kernel::FS::vfs_open(Kernel::FS::StringView(pt_interp_path), + Kernel::FS::kOpenReadOnly, 0U, &interp_file); + if (orc != Kernel::FS::kOk || interp_file == nullptr || + interp_file->get_inode() == nullptr || !interp_file->get_inode()->is_reg() || + interp_file->get_inode()->get_size() == 0U) { + if (interp_file != nullptr) { + (void)Kernel::FS::vfs_close(interp_file); + } + load_res->address_space.destroy(); + Kernel::Log::error(kShellTag, "Failed to open PT_INTERP '{}' for {}.", pt_interp_path, + bin_path); + return; + } + + const size_t page_size = HAL::get_platform_info().page_size; + const auto interp_size = static_cast(interp_file->get_inode()->get_size()); + const size_t interp_pages = (interp_size + page_size - 1U) / page_size; + const uintptr_t interp_paddr = Kernel::MM::alloc_frames(interp_pages); + if (interp_paddr == 0U) { + (void)Kernel::FS::vfs_close(interp_file); + load_res->address_space.destroy(); + Kernel::Log::error(kShellTag, "OOM reading PT_INTERP '{}' for {}.", pt_interp_path, + bin_path); + return; + } + + auto* const interp_kva = reinterpret_cast(Kernel::MM::phys_to_virt(interp_paddr)); + size_t total_read = 0U; + while (total_read < interp_size) { + const int64_t r = Kernel::FS::vfs_read(interp_file, interp_kva + total_read, + interp_size - total_read); + if (r <= 0) { + break; + } + total_read += static_cast(r); + } + (void)Kernel::FS::vfs_close(interp_file); + + if (total_read != interp_size) { + Kernel::MM::free_frames(interp_paddr, interp_pages); + load_res->address_space.destroy(); + Kernel::Log::error(kShellTag, "Short read on PT_INTERP '{}' for {}.", pt_interp_path, + bin_path); + return; + } + + auto interp_res = Kernel::Loader::load_elf_interpreter( + interp_kva, interp_size, load_res->address_space, min_base_hint); + Kernel::MM::free_frames(interp_paddr, interp_pages); + if (!interp_res.is_ok()) { + load_res->address_space.destroy(); + Kernel::Log::error(kShellTag, "Failed to load PT_INTERP '{}' for {}.", pt_interp_path, + bin_path); + return; + } + + at_base = interp_res->base_addr; + initial_pc = interp_res->entry_point; + } + auto* const proc = Kernel::Process::Process::create(0U); if (proc == nullptr) { load_res->address_space.destroy(); @@ -310,9 +388,9 @@ void launch_initrd_shell(const char* bin_path, const char* argv0, const char* sh proc->set_exe_path(bin_path); - Kernel::Loader::Elf64_auxv_t auxv[Kernel::Process::STANDARD_AUXV_COUNT]{}; + Kernel::Loader::Elf64_auxv_t auxv[Kernel::Process::INTERP_AUXV_COUNT]{}; const size_t auxv_count = Kernel::Process::populate_standard_auxv( - *load_res, auxv, Kernel::Process::STANDARD_AUXV_COUNT); + *load_res, auxv, Kernel::Process::INTERP_AUXV_COUNT, at_base); proc->attach_elf(static_cast(*load_res)); @@ -335,9 +413,8 @@ void launch_initrd_shell(const char* bin_path, const char* argv0, const char* sh return; } - Kernel::Process::init_user_cpu_context(proc->context(), proc->entry_point(), - proc->user_stack().final_sp_el0, proc->user_stack().argc, - proc->user_stack().argv_vaddr); + Kernel::Process::init_user_cpu_context(proc->context(), initial_pc, + proc->user_stack().final_sp_el0, 0ULL, 0ULL); const uint32_t prev_fg_pgid = Kernel::Process::get_foreground_pgid(); Kernel::Process::set_foreground_pgid(proc->pgid()); diff --git a/kernel/syscalls/MemorySyscalls.cpp b/kernel/syscalls/MemorySyscalls.cpp index 2ba41e9..d09d97d 100644 --- a/kernel/syscalls/MemorySyscalls.cpp +++ b/kernel/syscalls/MemorySyscalls.cpp @@ -91,9 +91,11 @@ int64_t sys_mmap(uintptr_t addr, uint64_t length, uint64_t prot, uint64_t flags, const uintptr_t stack_floor = platform.user_stack_top - Kernel::Process::DEFAULT_USER_STACK_SIZE; - if (length == 0U || (length & page_mask) != 0U) { + if (length == 0U || length > (UINT64_MAX - static_cast(page_mask))) { return ERR_EINVAL; } + const uint64_t aligned_len = + (length + static_cast(page_mask)) & ~static_cast(page_mask); if ((prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0U) { return ERR_EINVAL; } @@ -116,7 +118,7 @@ int64_t sys_mmap(uintptr_t addr, uint64_t length, uint64_t prot, uint64_t flags, if ((offset & static_cast(page_mask)) != 0ULL) { return ERR_EINVAL; } - if (offset > (UINT64_MAX - length)) { + if (offset > (UINT64_MAX - aligned_len)) { return ERR_EINVAL; } if (static_cast(raw_fd) < 0) { @@ -175,11 +177,11 @@ int64_t sys_mmap(uintptr_t addr, uint64_t length, uint64_t prot, uint64_t flags, if (is_fixed) { if (addr == 0U || (addr & page_mask) != 0U || addr >= va_limit || - length > (va_limit - addr)) { + aligned_len > (va_limit - addr)) { return ERR_EINVAL; } map_start = addr; - map_end = addr + static_cast(length); + map_end = addr + static_cast(aligned_len); if (!is_valid_mmap_span(map_start, map_end)) { return ERR_ENOMEM; } @@ -188,10 +190,10 @@ int64_t sys_mmap(uintptr_t addr, uint64_t length, uint64_t prot, uint64_t flags, return ERR_ENOMEM; } } else if (addr != 0U && (addr & page_mask) == 0U && addr < va_limit && - length <= (va_limit - addr) && - is_valid_mmap_span(addr, addr + static_cast(length))) { + aligned_len <= (va_limit - addr) && + is_valid_mmap_span(addr, addr + static_cast(aligned_len))) { bool hint_free = true; - for (uintptr_t page = addr; page < addr + static_cast(length); + for (uintptr_t page = addr; page < addr + static_cast(aligned_len); page += page_size) { uintptr_t tmp_pa = 0U; HAL::PageFlags tmp_fl = HAL::PageFlags::None; @@ -205,15 +207,15 @@ int64_t sys_mmap(uintptr_t addr, uint64_t length, uint64_t prot, uint64_t flags, } if (hint_free) { map_start = addr; - map_end = addr + static_cast(length); + map_end = addr + static_cast(aligned_len); } } if (map_start == 0U) { const uintptr_t arena_top = proc->mmap_next_vaddr(); - if (length <= arena_top && - (arena_top - static_cast(length)) >= MMAP_ARENA_FLOOR) { - const uintptr_t candidate_start = arena_top - static_cast(length); + if (aligned_len <= arena_top && + (arena_top - static_cast(aligned_len)) >= MMAP_ARENA_FLOOR) { + const uintptr_t candidate_start = arena_top - static_cast(aligned_len); if (candidate_start > proc->brk_current()) { bool bump_free = true; for (uintptr_t page = candidate_start; page < arena_top; page += page_size) { @@ -237,12 +239,12 @@ int64_t sys_mmap(uintptr_t addr, uint64_t length, uint64_t prot, uint64_t flags, } if (map_start == 0U && va_limit > HIGH_MMAP_ARENA_FLOOR && - length <= (va_limit - HIGH_MMAP_ARENA_FLOOR)) { - const uintptr_t max_start = va_limit - static_cast(length); + aligned_len <= (va_limit - HIGH_MMAP_ARENA_FLOOR)) { + const uintptr_t max_start = va_limit - static_cast(aligned_len); for (uintptr_t cand = HIGH_MMAP_ARENA_FLOOR; cand <= max_start; cand += HIGH_MMAP_SEARCH_STEP) { bool range_free = true; - for (uintptr_t probe = cand; probe < cand + static_cast(length); + for (uintptr_t probe = cand; probe < cand + static_cast(aligned_len); probe += page_size) { uintptr_t tmp_pa = 0U; HAL::PageFlags tmp_fl = HAL::PageFlags::None; @@ -257,7 +259,7 @@ int64_t sys_mmap(uintptr_t addr, uint64_t length, uint64_t prot, uint64_t flags, } if (range_free) { map_start = cand; - map_end = cand + static_cast(length); + map_end = cand + static_cast(aligned_len); break; } } @@ -284,7 +286,7 @@ int64_t sys_mmap(uintptr_t addr, uint64_t length, uint64_t prot, uint64_t flags, backing_file->get_f_ops()->mmap != nullptr) { const int32_t mmap_rc = backing_file->get_f_ops()->mmap(backing_file, proc->address_space(), map_start, - static_cast(length), page_flags, offset); + static_cast(aligned_len), page_flags, offset); if (mmap_rc != 0) { (void)proc->address_space().unmap_vma_range(map_start, map_end); return mmap_rc; @@ -312,14 +314,17 @@ int64_t sys_munmap(uintptr_t addr, uint64_t length) noexcept { const uintptr_t stack_floor = platform.user_stack_top - Kernel::Process::DEFAULT_USER_STACK_SIZE; - if ((addr & page_mask) != 0U || length == 0U || (length & page_mask) != 0U) { + if ((addr & page_mask) != 0U || length == 0U || + length > (UINT64_MAX - static_cast(page_mask))) { return ERR_EINVAL; } - if (addr >= va_limit || length > (va_limit - addr)) { + const uint64_t aligned_len = + (length + static_cast(page_mask)) & ~static_cast(page_mask); + if (addr >= va_limit || aligned_len > (va_limit - addr)) { return ERR_EINVAL; } - const uintptr_t end_addr = addr + static_cast(length); + const uintptr_t end_addr = addr + static_cast(aligned_len); const bool in_lower_arena = (addr >= MMAP_ARENA_FLOOR && end_addr <= stack_floor); const bool in_high_arena = (addr >= HIGH_MMAP_ARENA_FLOOR && end_addr <= va_limit); if (!in_lower_arena && !in_high_arena) { diff --git a/kernel/syscalls/SyscallDispatcher.cpp b/kernel/syscalls/SyscallDispatcher.cpp index 274bb0b..6b38172 100644 --- a/kernel/syscalls/SyscallDispatcher.cpp +++ b/kernel/syscalls/SyscallDispatcher.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-only // Copyright (c) 2026 Marco Buschini +#include "core/log.hpp" #include "hal/cpu.hpp" #include "hal/trap.hpp" #include "process/scheduler.hpp" @@ -48,6 +49,7 @@ constexpr uint64_t SYS_UTIMENSAT = 88U; constexpr uint64_t SYS_EXIT = 93U; constexpr uint64_t SYS_EXIT_GROUP = 94U; constexpr uint64_t SYS_SET_TID_ADDRESS = 96U; +constexpr uint64_t SYS_SET_ROBUST_LIST = 99U; constexpr uint64_t SYS_NANOSLEEP = 101U; constexpr uint64_t SYS_CLOCK_GETTIME = 113U; constexpr uint64_t SYS_SCHED_YIELD = 124U; @@ -63,6 +65,7 @@ constexpr uint64_t SYS_SETSID = 157U; constexpr uint64_t SYS_UNAME = 160U; constexpr uint64_t SYS_GETRUSAGE = 165U; constexpr uint64_t SYS_UMASK = 166U; +constexpr uint64_t SYS_GETTIMEOFDAY = 169U; constexpr uint64_t SYS_GETPID = 172U; constexpr uint64_t SYS_GETPPID = 173U; constexpr uint64_t SYS_GETUID = 174U; @@ -325,6 +328,10 @@ class SyscallDispatcher : public HAL::TrapHandler { ret_val = sys_set_tid_address(static_cast(cpu.get_syscall_arg(ctx, 0U))); break; + case SYS_SET_ROBUST_LIST: + ret_val = (cpu.get_syscall_arg(ctx, 1U) == 24ULL) ? 0 : ERR_EINVAL; + break; + case SYS_NANOSLEEP: ret_val = sys_nanosleep(static_cast(cpu.get_syscall_arg(ctx, 0U)), static_cast(cpu.get_syscall_arg(ctx, 1U))); @@ -400,6 +407,11 @@ class SyscallDispatcher : public HAL::TrapHandler { ret_val = sys_umask(cpu.get_syscall_arg(ctx, 0U)); break; + case SYS_GETTIMEOFDAY: + ret_val = sys_gettimeofday(static_cast(cpu.get_syscall_arg(ctx, 0U)), + static_cast(cpu.get_syscall_arg(ctx, 1U))); + break; + case SYS_GETPID: ret_val = sys_getpid(); break; @@ -536,6 +548,11 @@ class SyscallDispatcher : public HAL::TrapHandler { case SYS_MUNMAP: ret_val = sys_munmap(static_cast(cpu.get_syscall_arg(ctx, 0U)), cpu.get_syscall_arg(ctx, 1U)); + if (ret_val < 0) { + Kernel::Log::debug("sys", "munmap failed addr={:x} len={:x} rc={}", + Kernel::Log::hex(cpu.get_syscall_arg(ctx, 0U)), + Kernel::Log::hex(cpu.get_syscall_arg(ctx, 1U)), ret_val); + } break; case SYS_MMAP: @@ -543,11 +560,28 @@ class SyscallDispatcher : public HAL::TrapHandler { cpu.get_syscall_arg(ctx, 1U), cpu.get_syscall_arg(ctx, 2U), cpu.get_syscall_arg(ctx, 3U), cpu.get_syscall_arg(ctx, 4U), cpu.get_syscall_arg(ctx, 5U)); + if (ret_val < 0) { + Kernel::Log::debug("sys", + "mmap failed addr={:x} len={:x} prot={:x} flags={:x} fd={} " + "off={:x} rc={}", + Kernel::Log::hex(cpu.get_syscall_arg(ctx, 0U)), + Kernel::Log::hex(cpu.get_syscall_arg(ctx, 1U)), + Kernel::Log::hex(cpu.get_syscall_arg(ctx, 2U)), + Kernel::Log::hex(cpu.get_syscall_arg(ctx, 3U)), + static_cast(cpu.get_syscall_arg(ctx, 4U)), + Kernel::Log::hex(cpu.get_syscall_arg(ctx, 5U)), ret_val); + } break; case SYS_MPROTECT: ret_val = sys_mprotect(static_cast(cpu.get_syscall_arg(ctx, 0U)), cpu.get_syscall_arg(ctx, 1U), cpu.get_syscall_arg(ctx, 2U)); + if (ret_val < 0) { + Kernel::Log::debug("sys", "mprotect failed addr={:x} len={:x} prot={:x} rc={}", + Kernel::Log::hex(cpu.get_syscall_arg(ctx, 0U)), + Kernel::Log::hex(cpu.get_syscall_arg(ctx, 1U)), + Kernel::Log::hex(cpu.get_syscall_arg(ctx, 2U)), ret_val); + } break; case SYS_CLONE: @@ -588,6 +622,10 @@ class SyscallDispatcher : public HAL::TrapHandler { break; default: + Kernel::Log::debug("sys", "ENOSYS nr={} a0={:x} a1={:x} a2={:x}", syscall_num, + Kernel::Log::hex(cpu.get_syscall_arg(ctx, 0U)), + Kernel::Log::hex(cpu.get_syscall_arg(ctx, 1U)), + Kernel::Log::hex(cpu.get_syscall_arg(ctx, 2U))); ret_val = ERR_ENOSYS; break; } diff --git a/kernel/syscalls/SyscallInternal.hpp b/kernel/syscalls/SyscallInternal.hpp index 83a4b27..f4992a1 100644 --- a/kernel/syscalls/SyscallInternal.hpp +++ b/kernel/syscalls/SyscallInternal.hpp @@ -111,6 +111,7 @@ void build_dentry_path(const Kernel::FS::Dentry* target, char* out, size_t cap) // Time & system information syscalls (TimeSyscalls.cpp) [[nodiscard]] int64_t sys_clock_gettime(uint64_t clk_id, uintptr_t user_tp) noexcept; +[[nodiscard]] int64_t sys_gettimeofday(uintptr_t user_tv, uintptr_t user_tz) noexcept; [[nodiscard]] int64_t sys_nanosleep(uintptr_t user_req, uintptr_t user_rem) noexcept; [[nodiscard]] int64_t sys_times(uintptr_t user_buf) noexcept; [[nodiscard]] int64_t sys_getrusage(int32_t who, uintptr_t user_usage) noexcept; diff --git a/kernel/syscalls/TimeSyscalls.cpp b/kernel/syscalls/TimeSyscalls.cpp index 1e8f1fa..df1af41 100644 --- a/kernel/syscalls/TimeSyscalls.cpp +++ b/kernel/syscalls/TimeSyscalls.cpp @@ -192,6 +192,35 @@ int64_t sys_clock_gettime(uint64_t clk_id, uintptr_t user_tp) noexcept { return Kernel::MM::copy_to_user(proc->address_space(), user_tp, &time_spec, sizeof(time_spec)); } +int64_t sys_gettimeofday(uintptr_t user_tv, uintptr_t user_tz) noexcept { + auto* const proc = Kernel::Process::Process::current(); + if (proc == nullptr) { + return ERR_EINVAL; + } + + if (user_tv != 0U) { + const linux_timeval tv = monotonic_now().to_linux_timeval(); + const int64_t rc = + Kernel::MM::copy_to_user(proc->address_space(), user_tv, &tv, sizeof(tv)); + if (rc < 0) { + return rc; + } + } + if (user_tz != 0U) { + struct linux_timezone { + int32_t tz_minuteswest; + int32_t tz_dsttime; + }; + const linux_timezone tz{0, 0}; + const int64_t rc = + Kernel::MM::copy_to_user(proc->address_space(), user_tz, &tz, sizeof(tz)); + if (rc < 0) { + return rc; + } + } + return 0; +} + int64_t sys_sysinfo(uintptr_t user_info) noexcept { auto* const proc = Kernel::Process::Process::current(); if (proc == nullptr) { diff --git a/scripts/setup_debian_bash.sh b/scripts/setup_debian_bash.sh new file mode 100755 index 0000000..36183f4 --- /dev/null +++ b/scripts/setup_debian_bash.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: GPL-3.0-only +# Copyright (c) 2026 Marco Buschini + +# Fetch and stage an unmodified precompiled Debian Bookworm arm64 GNU bash +# binary together with its runtime shared libraries (ld-linux-aarch64.so.1, +# libc.so.6, libtinfo.so.6) into a rootfs directory for Milo initrd packaging. + +set -euo pipefail + +if [ "$#" -lt 2 ]; then + echo "Usage: $0 " >&2 + exit 1 +fi + +DEBS_DIR="$1" +ROOTFS_DIR="$2" + +DEBIAN_MIRROR="${DEBIAN_MIRROR:-https://deb.debian.org/debian}" +DEBIAN_SUITE="${DEBIAN_SUITE:-bookworm}" +DEBIAN_ARCH="arm64" +PACKAGES_INDEX_URL="${DEBIAN_MIRROR}/dists/${DEBIAN_SUITE}/main/binary-${DEBIAN_ARCH}/Packages.xz" + +mkdir -p "${DEBS_DIR}" + +fetch_deb() { + local pkg="$1" + local out_deb="${DEBS_DIR}/${pkg}_${DEBIAN_ARCH}.deb" + if [ -f "${out_deb}" ]; then + return 0 + fi + + local index_file="${DEBS_DIR}/Packages_${DEBIAN_SUITE}_${DEBIAN_ARCH}.txt" + if [ ! -f "${index_file}" ]; then + echo "[DEBIAN-BASH] Downloading Debian ${DEBIAN_SUITE} (${DEBIAN_ARCH}) package index..." + curl -fsSL "${PACKAGES_INDEX_URL}" | xz -dc > "${index_file}.tmp" + mv "${index_file}.tmp" "${index_file}" + fi + + local rel_path + rel_path="$(awk -v target="${pkg}" ' + $0 == "Package: " target { in_pkg = 1; next } + /^$/ { in_pkg = 0 } + in_pkg && /^Filename: / { print $2; exit } + ' "${index_file}")" + + if [ -z "${rel_path}" ]; then + echo "[DEBIAN-BASH] ERROR: Package '${pkg}' not found in ${DEBIAN_SUITE}/${DEBIAN_ARCH} index." >&2 + exit 1 + fi + + echo "[DEBIAN-BASH] Downloading ${pkg} (${rel_path})..." + curl -fsSL "${DEBIAN_MIRROR}/${rel_path}" -o "${out_deb}.tmp" + mv "${out_deb}.tmp" "${out_deb}" +} + +fetch_deb "bash" +fetch_deb "libc6" +fetch_deb "libtinfo6" + +EXTRACT_DIR="$(mktemp -d)" +trap 'rm -rf "${EXTRACT_DIR}"' EXIT + +dpkg-deb -x "${DEBS_DIR}/bash_${DEBIAN_ARCH}.deb" "${EXTRACT_DIR}" +dpkg-deb -x "${DEBS_DIR}/libc6_${DEBIAN_ARCH}.deb" "${EXTRACT_DIR}" +dpkg-deb -x "${DEBS_DIR}/libtinfo6_${DEBIAN_ARCH}.deb" "${EXTRACT_DIR}" + +mkdir -p "${ROOTFS_DIR}/bin" \ + "${ROOTFS_DIR}/etc" \ + "${ROOTFS_DIR}/lib/aarch64-linux-gnu" \ + "${ROOTFS_DIR}/usr/bin" \ + "${ROOTFS_DIR}/usr/lib/aarch64-linux-gnu" + +# Install unmodified Debian arm64 bash binary and /bin/sh symlink +install -m 0755 "${EXTRACT_DIR}/bin/bash" "${ROOTFS_DIR}/bin/bash" +ln -sf bash "${ROOTFS_DIR}/bin/sh" + +# Install glibc dynamic linker and libc.so.6 +install -m 0755 "${EXTRACT_DIR}/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1" \ + "${ROOTFS_DIR}/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1" +ln -sf aarch64-linux-gnu/ld-linux-aarch64.so.1 "${ROOTFS_DIR}/lib/ld-linux-aarch64.so.1" + +install -m 0755 "${EXTRACT_DIR}/lib/aarch64-linux-gnu/libc.so.6" \ + "${ROOTFS_DIR}/lib/aarch64-linux-gnu/libc.so.6" + +# Install libtinfo.so.6 (resolving the real target file dynamically) +TINFO_LINK="${EXTRACT_DIR}/lib/aarch64-linux-gnu/libtinfo.so.6" +TINFO_REAL="$(readlink -f "${TINFO_LINK}")" +TINFO_BASE="$(basename "${TINFO_REAL}")" +install -m 0755 "${TINFO_REAL}" "${ROOTFS_DIR}/lib/aarch64-linux-gnu/${TINFO_BASE}" +ln -sf "${TINFO_BASE}" "${ROOTFS_DIR}/lib/aarch64-linux-gnu/libtinfo.so.6" + +# Minimal /etc/passwd and /etc/group for glibc/bash user lookups +cat > "${ROOTFS_DIR}/etc/passwd" << 'EOF' +root:x:0:0:root:/:/bin/bash +EOF + +cat > "${ROOTFS_DIR}/etc/group" << 'EOF' +root:x:0: +EOF + +echo "[DEBIAN-BASH] Staged precompiled arm64 bash and runtime libraries in ${ROOTFS_DIR}" diff --git a/tests/host/distro_abi_test.cpp b/tests/host/distro_abi_test.cpp index 221a252..729a708 100644 --- a/tests/host/distro_abi_test.cpp +++ b/tests/host/distro_abi_test.cpp @@ -227,6 +227,25 @@ TEST_CASE("Distro ABI: sign-extended ioctl, mmap/mprotect, signals, and devfs/VF CHECK(Kernel::Syscalls::sys_munmap(static_cast(bump_map), 16384ULL) == 0); CHECK(Kernel::Syscalls::sys_munmap(0x3FFDC000ULL, 4096ULL) == 0); + // Non-page-aligned mmap/munmap lengths round up to page_size (required by glibc ld.so) + constexpr uint64_t kUnalignedLen = 0x508C0ULL; + constexpr uint64_t kAlignedLen = 0x51000ULL; + const int64_t unaligned_map = Kernel::Syscalls::sys_mmap( + 0U, kUnalignedLen, Kernel::UAPI::PROT_NONE, + Kernel::UAPI::MAP_PRIVATE | Kernel::UAPI::MAP_ANONYMOUS, static_cast(-1), 0ULL); + REQUIRE(unaligned_map > 0); + const auto unaligned_addr = static_cast(unaligned_map); + const auto* const unaligned_vma = proc->address_space().find_vma(unaligned_addr); + REQUIRE(unaligned_vma != nullptr); + CHECK(unaligned_vma->start_vaddr == unaligned_addr); + CHECK(unaligned_vma->end_vaddr == unaligned_addr + static_cast(kAlignedLen)); + CHECK(Kernel::Syscalls::sys_munmap(unaligned_addr, kUnalignedLen) == 0); + CHECK(proc->address_space().find_vma(unaligned_addr) == nullptr); + CHECK(Kernel::Syscalls::sys_mmap(0U, UINT64_MAX, Kernel::UAPI::PROT_READ, + Kernel::UAPI::MAP_PRIVATE | Kernel::UAPI::MAP_ANONYMOUS, + static_cast(-1), 0ULL) == Kernel::UAPI::ERR_EINVAL); + CHECK(Kernel::Syscalls::sys_munmap(0x3FFDC000ULL, UINT64_MAX) == Kernel::UAPI::ERR_EINVAL); + // 3. SignalTable unmaskability & SA_NODEFER / SA_RESETHAND CHECK_FALSE( proc->signals().set_action(Kernel::Process::SIG_KILL, Kernel::Process::SigAction{})); diff --git a/tests/host/process_subsystem_test.cpp b/tests/host/process_subsystem_test.cpp index 41e3d2b..bc11058 100644 --- a/tests/host/process_subsystem_test.cpp +++ b/tests/host/process_subsystem_test.cpp @@ -574,7 +574,7 @@ TEST_CASE("SignalTable masks, actions, wakeup, and frame delivery/restore", "[pr REQUIRE(cpu.get_user_pc(ctx) == 0x400123ULL); REQUIRE(cpu.get_reg(ctx, 5U) == 0x12345678ULL); - // Fallback trampoline restorer + SA_NODEFER + // Fallback W^X R+X trampoline page restorer + SA_NODEFER (glibc AArch64 without SA_RESTORER) SigAction tramp_act{ .handler = 0x400700ULL, .flags = Kernel::UAPI::SA_NODEFER, @@ -584,7 +584,28 @@ TEST_CASE("SignalTable masks, actions, wakeup, and frame delivery/restore", "[pr REQUIRE(child->signals().set_action(SIG_HUP, tramp_act)); send_signal(*child, SIG_HUP); deliver_pending_signals(ctx); + const uintptr_t expected_tramp_vaddr = HAL::get_platform_info().user_stack_top; REQUIRE(cpu.get_user_pc(ctx) == 0x400700ULL); + REQUIRE(cpu.get_reg(ctx, 30U) == expected_tramp_vaddr); + + uintptr_t tramp_paddr_first = 0U; + HAL::PageFlags tramp_flags = HAL::PageFlags::None; + REQUIRE(child->address_space().query_user_page(expected_tramp_vaddr, tramp_paddr_first, + tramp_flags)); + CHECK(HAL::has_flag(tramp_flags, HAL::PageFlags::User)); + CHECK(HAL::has_flag(tramp_flags, HAL::PageFlags::Read)); + CHECK(HAL::has_flag(tramp_flags, HAL::PageFlags::Execute)); + CHECK_FALSE(HAL::has_flag(tramp_flags, HAL::PageFlags::Write)); + + // A second signal delivery reuses the same trampoline page without leaking frames + REQUIRE(restore_signal_frame(ctx) == 42); + send_signal(*child, SIG_HUP); + deliver_pending_signals(ctx); + uintptr_t tramp_paddr_second = 0U; + REQUIRE(child->address_space().query_user_page(expected_tramp_vaddr, tramp_paddr_second, + tramp_flags)); + CHECK(tramp_paddr_second == tramp_paddr_first); + REQUIRE(cpu.get_reg(ctx, 30U) == expected_tramp_vaddr); child->address_space().unmap_user_page(kUserPageBase); Kernel::MM::free_frame(stack_pa);