feat: port lua scripting language
This commit is contained in:
@@ -19,6 +19,8 @@ endif
|
||||
|
||||
LIBC_INC := ../../include/libc
|
||||
OBJDIR := obj
|
||||
CRTDIR := crt
|
||||
ASM_OBJS := $(OBJDIR)/setjmp.o
|
||||
|
||||
# ---- Compiler flags ----
|
||||
|
||||
@@ -43,20 +45,49 @@ CFLAGS := \
|
||||
-isystem $(LIBC_INC) \
|
||||
-isystem $(shell $(CC) -print-file-name=include)
|
||||
|
||||
CRT_CFLAGS := \
|
||||
-std=gnu11 \
|
||||
-O2 \
|
||||
-nostdinc \
|
||||
-ffreestanding \
|
||||
-fno-stack-protector \
|
||||
-fno-stack-check \
|
||||
-fno-PIC \
|
||||
-m64 \
|
||||
-march=x86-64 \
|
||||
-mno-red-zone \
|
||||
-fno-tree-loop-distribute-patterns \
|
||||
-isystem $(LIBC_INC) \
|
||||
-isystem $(shell $(CC) -print-file-name=include)
|
||||
|
||||
# ---- Target ----
|
||||
|
||||
TARGET := liblibc.a
|
||||
CRT_OBJS := crt1.o crti.o crtn.o
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
all: $(TARGET)
|
||||
all: $(TARGET) $(CRT_OBJS)
|
||||
|
||||
$(TARGET): $(OBJDIR)/libc.o
|
||||
$(TARGET): $(OBJDIR)/libc.o $(ASM_OBJS)
|
||||
$(AR) rcs $@ $^
|
||||
|
||||
$(OBJDIR)/libc.o: libc.c Makefile
|
||||
@mkdir -p $(OBJDIR)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(OBJDIR)/setjmp.o: setjmp.S Makefile
|
||||
@mkdir -p $(OBJDIR)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
crt1.o: $(CRTDIR)/crt1.c Makefile
|
||||
$(CC) $(CRT_CFLAGS) -c $< -o $@
|
||||
|
||||
crti.o: $(CRTDIR)/crti.c Makefile
|
||||
$(CC) $(CRT_CFLAGS) -c $< -o $@
|
||||
|
||||
crtn.o: $(CRTDIR)/crtn.c Makefile
|
||||
$(CC) $(CRT_CFLAGS) -c $< -o $@
|
||||
|
||||
clean:
|
||||
rm -rf $(OBJDIR) $(TARGET)
|
||||
rm -rf $(OBJDIR) $(TARGET) $(CRT_OBJS)
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* crt1.c
|
||||
* Minimal C runtime startup for MontaukOS userspace programs
|
||||
* Copyright (c) 2026 Daniel Hammer
|
||||
*
|
||||
* This startup shim is intentionally small: it fetches the raw
|
||||
* command-line buffer from the kernel, tokenizes it into argc/argv,
|
||||
* calls main(), then exits with main()'s return code.
|
||||
*/
|
||||
|
||||
static inline long _sys1(long nr, long a1) {
|
||||
long ret;
|
||||
__asm__ volatile(
|
||||
"mov %[a1], %%rdi\n\t"
|
||||
"syscall"
|
||||
: "=a"(ret)
|
||||
: "a"(nr), [a1] "r"(a1)
|
||||
: "rcx", "r11", "rdi", "rsi", "rdx", "r8", "r9", "r10", "memory");
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline long _sys2(long nr, long a1, long a2) {
|
||||
long ret;
|
||||
__asm__ volatile(
|
||||
"mov %[a1], %%rdi\n\t"
|
||||
"mov %[a2], %%rsi\n\t"
|
||||
"syscall"
|
||||
: "=a"(ret)
|
||||
: "a"(nr), [a1] "r"(a1), [a2] "r"(a2)
|
||||
: "rcx", "r11", "rdi", "rsi", "rdx", "r8", "r9", "r10", "memory");
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define SYS_EXIT 0
|
||||
#define SYS_GETARGS 25
|
||||
|
||||
extern int main(int argc, char** argv);
|
||||
|
||||
void _start(void) {
|
||||
char argbuf[256];
|
||||
int len = (int)_sys2(SYS_GETARGS, (long)argbuf, (long)sizeof(argbuf));
|
||||
|
||||
char* argv[32];
|
||||
int argc = 0;
|
||||
|
||||
argv[argc++] = (char*)"prog";
|
||||
|
||||
if (len > 0) {
|
||||
if (len > 255) {
|
||||
len = 255;
|
||||
}
|
||||
|
||||
argbuf[len] = '\0';
|
||||
|
||||
char* p = argbuf;
|
||||
while (*p != '\0' && argc < 31) {
|
||||
while (*p == ' ') {
|
||||
p++;
|
||||
}
|
||||
|
||||
if (*p == '\0') {
|
||||
break;
|
||||
}
|
||||
|
||||
argv[argc++] = p;
|
||||
|
||||
while (*p != '\0' && *p != ' ') {
|
||||
p++;
|
||||
}
|
||||
|
||||
if (*p != '\0') {
|
||||
*p++ = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
argv[argc] = 0;
|
||||
|
||||
_sys1(SYS_EXIT, (long)main(argc, argv));
|
||||
__builtin_unreachable();
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
/*
|
||||
* crti.c
|
||||
* Init section prologue placeholder for MontaukOS userspace
|
||||
* Copyright (c) 2026 Daniel Hammer
|
||||
*/
|
||||
|
||||
/* Empty for now: generic userspace CRT does not run global constructors yet. */
|
||||
@@ -0,0 +1,7 @@
|
||||
/*
|
||||
* crtn.c
|
||||
* Init section epilogue placeholder for MontaukOS userspace
|
||||
* Copyright (c) 2026 Daniel Hammer
|
||||
*/
|
||||
|
||||
/* Empty for now: generic userspace CRT does not run global destructors yet. */
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1121
-35
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* setjmp.S
|
||||
* setjmp/longjmp for x86_64 MontaukOS userspace
|
||||
* Copyright (c) 2026 Daniel Hammer
|
||||
*/
|
||||
|
||||
.text
|
||||
|
||||
.global setjmp
|
||||
.type setjmp, @function
|
||||
setjmp:
|
||||
mov %rbx, (%rdi)
|
||||
mov %rbp, 8(%rdi)
|
||||
mov %r12, 16(%rdi)
|
||||
mov %r13, 24(%rdi)
|
||||
mov %r14, 32(%rdi)
|
||||
mov %r15, 40(%rdi)
|
||||
lea 8(%rsp), %rax
|
||||
mov %rax, 48(%rdi)
|
||||
mov (%rsp), %rax
|
||||
mov %rax, 56(%rdi)
|
||||
xor %eax, %eax
|
||||
ret
|
||||
|
||||
.global longjmp
|
||||
.type longjmp, @function
|
||||
longjmp:
|
||||
mov %esi, %eax
|
||||
test %eax, %eax
|
||||
jnz 1f
|
||||
inc %eax
|
||||
1:
|
||||
mov (%rdi), %rbx
|
||||
mov 8(%rdi), %rbp
|
||||
mov 16(%rdi), %r12
|
||||
mov 24(%rdi), %r13
|
||||
mov 32(%rdi), %r14
|
||||
mov 40(%rdi), %r15
|
||||
mov 48(%rdi), %rsp
|
||||
jmp *56(%rdi)
|
||||
Reference in New Issue
Block a user