Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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) \
Expand Down
2 changes: 1 addition & 1 deletion arch/aarch64-linux-gnu/arch.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
CXXFLAGS += -mcpu=cortex-a53 -mno-outline-atomics -mgeneral-regs-only
127 changes: 72 additions & 55 deletions arch/aarch64-linux-gnu/boot.s
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down
6 changes: 6 additions & 0 deletions arch/aarch64-linux-gnu/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion arch/aarch64-linux-gnu/cpu_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
24 changes: 21 additions & 3 deletions arch/aarch64-linux-gnu/user_test.s
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
el0_uxn_branch_to_stack_end:
2 changes: 1 addition & 1 deletion drivers/ddk.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions kernel/hal/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions kernel/mm/AddressSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading
Loading