feat: add support for aarch64 timer/interrupts, port MontaukOS userspace

This commit is contained in:
daniel committed 2026-10-25 08:27:09 +01:00
1 parent 8b6379fb7e
commit 8c09ccef66
101 files changed
+1720 -629

No files matched your search

+17 -18
View File
@@ -6,7 +6,7 @@ MAKEFLAGS += -rR
# ---- Toolchain ----
TOOLCHAIN_PREFIX := $(shell cd ../../.. && pwd)/toolchain/local/bin/x86_64-montauk-
include ../../mk/arch.mk
ifneq ($(wildcard $(TOOLCHAIN_PREFIX)gcc),)
CC := $(TOOLCHAIN_PREFIX)gcc
AR := $(TOOLCHAIN_PREFIX)ar
@@ -20,7 +20,7 @@ endif
# ---- Paths ----
LIBC_INC := ../../include/libc
OBJDIR := obj
OBJDIR := obj$(ARCH_SUFFIX)
CRTDIR := crt
ASM_OBJS := $(OBJDIR)/setjmp.o
@@ -38,12 +38,8 @@ CFLAGS := \
-fno-PIC \
-ffunction-sections \
-fdata-sections \
-m64 \
-march=x86-64 \
-msse \
-msse2 \
-mno-red-zone \
-mcmodel=small \
$(ARCH_CFLAGS) \
$(ARCH_FP_FLAGS) \
-isystem $(LIBC_INC) \
-isystem $(shell $(CC) -print-file-name=include)
@@ -55,18 +51,16 @@ CRT_CFLAGS := \
-fno-stack-protector \
-fno-stack-check \
-fno-PIC \
-m64 \
-march=x86-64 \
-mno-red-zone \
$(ARCH_CFLAGS) \
-fno-tree-loop-distribute-patterns \
-isystem $(LIBC_INC) \
-isystem $(shell $(CC) -print-file-name=include)
# ---- Target ----
TARGET := liblibc.a
SDK_TARGET := liblibc-full.a
CRT_OBJS := crt1.o crti.o crtn.o
TARGET := $(ARCH_OUT)liblibc.a
SDK_TARGET := $(ARCH_OUT)liblibc-full.a
CRT_OBJS := $(ARCH_OUT)crt1.o $(ARCH_OUT)crti.o $(ARCH_OUT)crtn.o
.PHONY: all clean
@@ -77,6 +71,7 @@ all: $(TARGET) $(SDK_TARGET) $(CRT_OBJS)
# a float leave the decimal-conversion and pow/exp/log code behind. Programs
# that do need %e/%f/%g must link with -Wl,-u,_pf_putfloat to force it in.
$(TARGET): $(OBJDIR)/libc.o $(OBJDIR)/printf_float.o $(ASM_OBJS)
@mkdir -p $(dir $@)
$(AR) rcs $@ $^
$(OBJDIR)/libc.o: libc.c printf_internal.h Makefile
@@ -87,7 +82,7 @@ $(OBJDIR)/printf_float.o: printf_float.c printf_internal.h Makefile
@mkdir -p $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/setjmp.o: setjmp.S Makefile
$(OBJDIR)/setjmp.o: arch/setjmp-$(ARCH).S Makefile
@mkdir -p $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
@@ -97,18 +92,22 @@ $(OBJDIR)/setjmp.o: setjmp.S Makefile
# compiled ON MontaukOS. Function-level --gc-sections still applies to the
# rest; only the float path is pinned. Image size is not a concern here.
$(SDK_TARGET): $(OBJDIR)/libc-merged.o $(ASM_OBJS)
@mkdir -p $(dir $@)
$(AR) rcs $@ $^
$(OBJDIR)/libc-merged.o: $(OBJDIR)/libc.o $(OBJDIR)/printf_float.o
$(LD) -r -o $@ $(OBJDIR)/libc.o $(OBJDIR)/printf_float.o
crt1.o: $(CRTDIR)/crt1.c Makefile
$(ARCH_OUT)crt1.o: $(CRTDIR)/crt1.c Makefile
@mkdir -p $(dir $@)
$(CC) $(CRT_CFLAGS) -c $< -o $@
crti.o: $(CRTDIR)/crti.c Makefile
$(ARCH_OUT)crti.o: $(CRTDIR)/crti.c Makefile
@mkdir -p $(dir $@)
$(CC) $(CRT_CFLAGS) -c $< -o $@
crtn.o: $(CRTDIR)/crtn.c Makefile
$(ARCH_OUT)crtn.o: $(CRTDIR)/crtn.c Makefile
@mkdir -p $(dir $@)
$(CC) $(CRT_CFLAGS) -c $< -o $@
clean:
+49
View File
@@ -0,0 +1,49 @@
/*
* setjmp-aarch64.S
* setjmp/longjmp for AArch64 MontaukOS userspace
* Copyright (c) 2026 Daniel Hammer
*/
/* jmp_buf: x19-x28, x29 (fp), x30 (lr), sp, d8-d15 -- everything AAPCS64
requires a callee to preserve. */
.text
.global setjmp
.type setjmp, %function
setjmp:
stp x19, x20, [x0, #0]
stp x21, x22, [x0, #16]
stp x23, x24, [x0, #32]
stp x25, x26, [x0, #48]
stp x27, x28, [x0, #64]
stp x29, x30, [x0, #80]
mov x2, sp
str x2, [x0, #96]
stp d8, d9, [x0, #104]
stp d10, d11, [x0, #120]
stp d12, d13, [x0, #136]
stp d14, d15, [x0, #152]
mov w0, #0
ret
.size setjmp, . - setjmp
.global longjmp
.type longjmp, %function
longjmp:
ldp x19, x20, [x0, #0]
ldp x21, x22, [x0, #16]
ldp x23, x24, [x0, #32]
ldp x25, x26, [x0, #48]
ldp x27, x28, [x0, #64]
ldp x29, x30, [x0, #80]
ldr x2, [x0, #96]
mov sp, x2
ldp d8, d9, [x0, #104]
ldp d10, d11, [x0, #120]
ldp d12, d13, [x0, #136]
ldp d14, d15, [x0, #152]
cmp w1, #0
csinc w0, w1, wzr, ne
ret
.size longjmp, . - longjmp
File renamed without changes.
+23
View File
@@ -8,6 +8,8 @@
* calls main(), then exits with main()'s return code.
*/
#if defined(__x86_64__)
static inline long _sys1(long nr, long a1) {
long ret;
__asm__ volatile(
@@ -31,6 +33,27 @@ static inline long _sys2(long nr, long a1, long a2) {
return ret;
}
#elif defined(__aarch64__)
static inline long _sys1(long nr, long a1) {
register long x8 __asm__("x8") = nr;
register long x0 __asm__("x0") = a1;
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8) : "memory");
return x0;
}
static inline long _sys2(long nr, long a1, long a2) {
register long x8 __asm__("x8") = nr;
register long x0 __asm__("x0") = a1;
register long x1 __asm__("x1") = a2;
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1) : "memory");
return x0;
}
#else
#error "unsupported architecture"
#endif
#define SYS_EXIT 0
#define SYS_GETARGS 25
#define SYS_GETEXECPATH 151
+51
View File
@@ -32,6 +32,8 @@
Raw syscall wrappers (C versions matching kernel ABI)
======================================================================== */
#if defined(__x86_64__)
static inline long _mtk_syscall0(long nr) {
long ret;
__asm__ volatile("syscall" : "=a"(ret) : "a"(nr)
@@ -89,6 +91,55 @@ static inline long _mtk_syscall4(long nr, long a1, long a2, long a3, long a4) {
return ret;
}
#elif defined(__aarch64__)
// AArch64: number in x8, arguments in x0-x5, `svc #0`; the kernel
// preserves every register except x0, which carries the result.
static inline long _mtk_syscall0(long nr) {
register long x8 __asm__("x8") = nr;
register long x0 __asm__("x0");
__asm__ volatile("svc #0" : "=r"(x0) : "r"(x8) : "memory");
return x0;
}
static inline long _mtk_syscall1(long nr, long a1) {
register long x8 __asm__("x8") = nr;
register long x0 __asm__("x0") = a1;
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8) : "memory");
return x0;
}
static inline long _mtk_syscall2(long nr, long a1, long a2) {
register long x8 __asm__("x8") = nr;
register long x0 __asm__("x0") = a1;
register long x1 __asm__("x1") = a2;
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1) : "memory");
return x0;
}
static inline long _mtk_syscall3(long nr, long a1, long a2, long a3) {
register long x8 __asm__("x8") = nr;
register long x0 __asm__("x0") = a1;
register long x1 __asm__("x1") = a2;
register long x2 __asm__("x2") = a3;
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2) : "memory");
return x0;
}
static inline long _mtk_syscall4(long nr, long a1, long a2, long a3, long a4) {
register long x8 __asm__("x8") = nr;
register long x0 __asm__("x0") = a1;
register long x1 __asm__("x1") = a2;
register long x2 __asm__("x2") = a3;
register long x3 __asm__("x3") = a4;
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3) : "memory");
return x0;
}
#else
#error "unsupported architecture"
#endif
/* Syscall numbers */
#define SYS_EXIT 0
#define SYS_YIELD 1
+6 -10
View File
@@ -6,7 +6,7 @@ MAKEFLAGS += -rR
# ---- Toolchain ----
TOOLCHAIN_PREFIX := $(shell cd ../../.. && pwd)/toolchain/local/bin/x86_64-montauk-
include ../../mk/arch.mk
ifneq ($(wildcard $(TOOLCHAIN_PREFIX)gcc),)
CC := $(TOOLCHAIN_PREFIX)gcc
AR := $(TOOLCHAIN_PREFIX)ar
@@ -19,8 +19,7 @@ endif
LIBC_INC := ../../include/libc
PROG_INC := ../../include
OBJDIR := obj
OBJDIR := obj$(ARCH_SUFFIX)
# ---- Compiler flags ----
CFLAGS := \
@@ -36,25 +35,22 @@ CFLAGS := \
-fno-PIC \
-ffunction-sections \
-fdata-sections \
-m64 \
-march=x86-64 \
-msse \
-msse2 \
-mno-red-zone \
-mcmodel=small \
$(ARCH_CFLAGS) \
$(ARCH_FP_FLAGS) \
-I $(PROG_INC) \
-isystem $(LIBC_INC) \
-isystem $(shell $(CC) -print-file-name=include)
# ---- Target ----
TARGET := libjpeg.a
TARGET := $(ARCH_OUT)libjpeg.a
.PHONY: all clean
all: $(TARGET)
$(TARGET): $(OBJDIR)/stb_image_impl.o
@mkdir -p $(dir $@)
$(AR) rcs $@ $^
$(OBJDIR)/stb_image_impl.o: stb_image_impl.c Makefile
Binary file not shown.
Binary file not shown.
+6 -10
View File
@@ -6,7 +6,7 @@ MAKEFLAGS += -rR
# ---- Toolchain ----
TOOLCHAIN_PREFIX := $(shell cd ../../.. && pwd)/toolchain/local/bin/x86_64-montauk-
include ../../mk/arch.mk
ifneq ($(wildcard $(TOOLCHAIN_PREFIX)gcc),)
CC := $(TOOLCHAIN_PREFIX)gcc
AR := $(TOOLCHAIN_PREFIX)ar
@@ -19,8 +19,7 @@ endif
LIBC_INC := ../../include/libc
PROG_INC := ../../include
OBJDIR := obj
OBJDIR := obj$(ARCH_SUFFIX)
# ---- Compiler flags ----
CFLAGS := \
@@ -37,25 +36,22 @@ CFLAGS := \
-ffunction-sections \
-fdata-sections \
-fno-tree-loop-distribute-patterns \
-m64 \
-march=x86-64 \
-msse \
-msse2 \
-mno-red-zone \
-mcmodel=small \
$(ARCH_CFLAGS) \
$(ARCH_FP_FLAGS) \
-I $(PROG_INC) \
-isystem $(LIBC_INC) \
-isystem $(shell $(CC) -print-file-name=include)
# ---- Target ----
TARGET := libjpegwrite.a
TARGET := $(ARCH_OUT)libjpegwrite.a
.PHONY: all clean
all: $(TARGET)
$(TARGET): $(OBJDIR)/stb_image_write_impl.o
@mkdir -p $(dir $@)
$(AR) rcs $@ $^
$(OBJDIR)/stb_image_write_impl.o: stb_image_write_impl.c Makefile
Binary file not shown.
Binary file not shown.
+8 -14
View File
@@ -6,7 +6,7 @@ MAKEFLAGS += -rR
# ---- Toolchain ----
TOOLCHAIN_PREFIX := $(shell cd ../../.. && pwd)/toolchain/local/bin/x86_64-montauk-
include ../../mk/arch.mk
ifneq ($(wildcard $(TOOLCHAIN_PREFIX)gcc),)
CXX := $(TOOLCHAIN_PREFIX)g++
AR := $(TOOLCHAIN_PREFIX)ar
@@ -20,8 +20,7 @@ endif
BEARSSL := ../bearssl
LIBC_INC := ../../include/libc
PROG_INC := ../../include
OBJDIR := obj
OBJDIR := obj$(ARCH_SUFFIX)
# ---- Compiler flags ----
CXXFLAGS := \
@@ -39,29 +38,24 @@ CXXFLAGS := \
-fno-exceptions \
-ffunction-sections \
-fdata-sections \
-m64 \
-march=x86-64 \
-mno-80387 \
-mno-mmx \
-mno-sse \
-mno-sse2 \
-mno-red-zone \
-mcmodel=small \
$(ARCH_CFLAGS) \
$(ARCH_NOFP_FLAGS) \
-I $(PROG_INC) \
-isystem $(LIBC_INC) \
-I $(BEARSSL)/inc \
-isystem ../../../kernel/freestnd-c-hdrs/x86_64/include \
-isystem ../../../kernel/freestnd-cxx-hdrs/x86_64/include
-isystem ../../../kernel/freestnd-c-hdrs/$(ARCH)/include \
-isystem ../../../kernel/freestnd-cxx-hdrs/$(ARCH)/include
# ---- Target ----
TARGET := libtls.a
TARGET := $(ARCH_OUT)libtls.a
.PHONY: all clean
all: $(TARGET)
$(TARGET): $(OBJDIR)/tls.o
@mkdir -p $(dir $@)
$(AR) rcs $@ $^
$(OBJDIR)/tls.o: tls.cpp Makefile
Binary file not shown.
Binary file not shown.