From 06b685b5887831bdd0c6c7a2b5056aaa69c73426 Mon Sep 17 00:00:00 2001 From: pitust Date: Sat, 18 Feb 2023 07:30:08 +0100 Subject: [PATCH] luajit not working --- Makefile | 4 ++-- guesstrace.py | 40 ++++++++++++++++++++++++++++++++ inc/lj-libc/limits.h | 3 ++- inc/luck/memory/manager.h | 6 +++++ makelj.sh | 10 ++++---- qint.py | 22 ++++++++++++++++++ src/arch/x86_64/map.c | 49 +++++++++++++++++++++++++++++++++++++-- src/limine.c | 1 + src/ljsupport.c | 9 +++++-- src/memory/kalloc.c | 12 +++++++++- src/start.c | 21 +++++++++++++++++ 11 files changed, 165 insertions(+), 12 deletions(-) create mode 100644 guesstrace.py create mode 100644 qint.py diff --git a/Makefile b/Makefile index 81ff81b..5609e60 100644 --- a/Makefile +++ b/Makefile @@ -89,7 +89,7 @@ extern/limine: $(MAKE) -C $@ extern/luajit: - git clone https://luajit.org/git/luajit.git $@ + git clone https://github.com/TheLuaOSProject/LuaJIT.git $@ extern/terminal/../luajit/src/lua.h: extern/luajit @@ -114,7 +114,7 @@ build/bin/luaos.iso: extern/limine build/bin/luck.elf res/limine.cfg extern/limine/limine-deploy $@ -extern/luajit/src/libluajit_luck.o: extern/luajit +extern/luajit/src/libluajit_luck.o: extern/luajit makelj.sh sh makelj.sh build/bin/luck.elf: $(COBJS) $(ASOBJS) extern/luajit/src/libluajit_luck.o diff --git a/guesstrace.py b/guesstrace.py new file mode 100644 index 0000000..0a9f446 --- /dev/null +++ b/guesstrace.py @@ -0,0 +1,40 @@ +import sys, os, subprocess + +# guesstrace - guess stack traces (macOS only, really damn cool) + +proc = subprocess.Popen('pbpaste', + shell=True, + stdout=subprocess.PIPE, + bufsize=-1) + +clip = proc.stdout.read().decode().strip().splitlines() +proc.kill() +clist = [] +def try_bt(p): + if 0xfffffffffff00000 & p == 0xffffffff80000000: + print('got code ptr:', hex(p - 1)) + clist.append(hex(p - (1 if len(clist) else 0))) + +for line in clip: + line = line.split(':')[1].strip() + g1 = line.split(' ')[0].split('\t')[0] + g2 = line.split(' ')[-1].split('\t')[-1] + g1p = int(g1, 16) + g2p = int(g2, 16) + try_bt(g1p) + try_bt(g2p) + +proc = subprocess.Popen( + 'llvm-addr2line -p -e ' + sys.argv[1] + ' ' + ' '.join(clist) + ' | grep -v \'??:0\'', + shell=True, + stdout=subprocess.PIPE, + bufsize=-1) + +bt = proc.stdout.read().decode().strip().splitlines() +proc.kill() +if len(sys.argv) == 3 and sys.argv[2] == 'srht': + bt = [ + entry + for entry in bt + ] +print('\n'.join(bt)) diff --git a/inc/lj-libc/limits.h b/inc/lj-libc/limits.h index 877251e..e16d85b 100644 --- a/inc/lj-libc/limits.h +++ b/inc/lj-libc/limits.h @@ -81,4 +81,5 @@ double pow(double x, double y); // memchr memcmp memcpy memmove memset // putchar setvbuf strchr strcmp strcpy strerror strlen strncpy strrchr strstr strtoul tmpfile ungetc - +void fflush(FILE *f); +int fputc(int c, FILE *stream); diff --git a/inc/luck/memory/manager.h b/inc/luck/memory/manager.h index 8a79623..016dc5e 100644 --- a/inc/luck/memory/manager.h +++ b/inc/luck/memory/manager.h @@ -28,12 +28,18 @@ enum PageType { }; qword page_alloc(enum PageType pty); +void page_free(enum PageType pty, qword addr); /// mapping pages /// void pmap_map(qword addr, qword phys); void pmap_map_rwx(qword addr, qword phys); +qword pmap_unmap(qword addr); + +void pmap_remap_rw(qword addr); +void pmap_remap_rwx(qword addr); /// kalloc /// void kalloc_init(void); void* kalloc(qword size); void kfree(void* ptr, qword size); +uint64_t kvirtalloc(qword size); diff --git a/makelj.sh b/makelj.sh index 976be3d..aa781ff 100644 --- a/makelj.sh +++ b/makelj.sh @@ -7,9 +7,11 @@ ALL_LIB="lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_o gcc host/minilua.c -o minilua.exe -./minilua.exe ../dynasm/dynasm.lua -LN -D P64 -D NO_UNWIND -o host/buildvm_arch.h vm_x64.dasc +./minilua.exe ../dynasm/dynasm.lua -LN -D P64 -D NO_UNWIND -D JIT -o host/buildvm_arch.h vm_x64.dasc -gcc host/buildvm*.c -o buildvm.exe -DLUAJIT_TARGET=LUAJIT_ARCH_X64 -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI -DLUAJIT_NO_UNWIND -I. -DTARGET_OS_IPHONE=0 +CONFIG="-DLUAJIT_DISABLE_FFI -DLUAJIT_USE_SYSMALLOC -DLUAJIT_TARGET=LUAJIT_ARCH_X64 -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_FFI -DLUAJIT_NO_UNWIND -I. -DTARGET_OS_IPHONE=0 -DLUAJIT_SECURITY_PRNG=0" + +gcc host/buildvm*.c -o buildvm.exe $CONFIG -I. ./buildvm.exe -m elfasm -o lj_vm.s ./buildvm.exe -m bcdef -o lj_bcdef.h $ALL_LIB @@ -19,13 +21,13 @@ gcc host/buildvm*.c -o buildvm.exe -DLUAJIT_TARGET=LUAJIT_ARCH_X64 -DLUAJIT_OS=L ./buildvm.exe -m vmdef -o jit/vmdef.lua $ALL_LIB ./buildvm.exe -m folddef -o lj_folddef.h lj_opt_fold.c -LJCOMPILE="clang -target x86_64-elf -nostdinc -Wno-duplicate-decl-specifier -Wno-unused-command-line-argument -Wno-unknown-attributes -I../../../inc -I../../../inc/lj-libc -DLUAJIT_DISABLE_FFI -DLUAJIT_USE_SYSMALLOC -DLUAJIT_TARGET=LUAJIT_ARCH_X64 -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI -DLUAJIT_NO_UNWIND -I. -DTARGET_OS_IPHONE=0 -DLUAJIT_SECURITY_PRNG=0 -g -mcmodel=kernel -fno-omit-frame-pointer" +LJCOMPILE="clang -target x86_64-elf -nostdinc -Wno-duplicate-decl-specifier -Wno-unused-command-line-argument -Wno-unknown-attributes -I../../../inc -I../../../inc/lj-libc $CONFIG -g -mcmodel=kernel -fno-omit-frame-pointer" rm -f lj_*.o lib_*.o $LJCOMPILE -c -o lj_vm.o lj_vm.s -for f in lj_*.c lib_aux.c lib_base.c lib_bit.c lib_buffer.c lib_debug.c lib_math.c lib_string.c lib_table.c; do +for f in lj_*.c lib_aux.c lib_base.c lib_bit.c lib_jit.c lib_buffer.c lib_debug.c lib_math.c lib_string.c lib_table.c; do $LJCOMPILE -c $f done diff --git a/qint.py b/qint.py new file mode 100644 index 0000000..9ab6927 --- /dev/null +++ b/qint.py @@ -0,0 +1,22 @@ + +r = [] +def xlat(ent): + while len(ent): + a0 = ent[0:3].strip() + a1 = ent[4:20] + ent = ent[21:] + r.append(f'${a0.lower()}=0x{a1}') + +xlat(input()) +xlat(input()) +xlat(input()) +xlat(input()) +xlat(input()) + +print('set ' + ','.join(r)) + +# RAX=0000000000000002 RBX=00007000004531d0 RCX=0000000000000014 RDX=0000700000454d40 +# RSI=0000700000454d40 RDI=0000700000453000 RBP=000000000000005f RSP=ffff80007fdf7f30 +# R8 =0000000000000000 R9 =0000000000000009 R10=0000700000008200 R11=fffffffffffffff7 +# R12=0000000000000002 R13=0000000000000002 R14=0000700000453fc0 R15=0000000000000000 +# RIP=0000000000000000 \ No newline at end of file diff --git a/src/arch/x86_64/map.c b/src/arch/x86_64/map.c index b8bdb8f..746964d 100644 --- a/src/arch/x86_64/map.c +++ b/src/arch/x86_64/map.c @@ -18,6 +18,7 @@ */ #include "common.h" +#include "luck/io/log.h" #include "luck/memory/manager.h" static qword* get_pte(qword addr) @@ -44,12 +45,56 @@ static qword* get_pte(qword addr) STEP(1) } +static void tlbinval(qword addr) +{ + asm volatile("invlpg (%0)" :: "r"(addr) : "memory"); +} + void pmap_map(qword addr, qword phys) { - *get_pte(addr) = 3 | phys; + tlbinval(addr); + *get_pte(addr) = 3 | phys | (1ULL << 63); } void pmap_map_rwx(qword addr, qword phys) { - *get_pte(addr) = 7 | phys; + tlbinval(addr); + *get_pte(addr) = 3 | phys; +} + +static void set_bits(qword addr, qword bits) +{ + qword *pte = get_pte(addr); + qword pval = *pte; + pval &= ~0xfff; + pval &= ~(1ULL << 63); + pval |= bits; + *pte = pval; + tlbinval(addr); +} + +void pmap_remap_rw(qword addr) +{ + (void)addr; + + set_bits(addr, 3 | (1ULL << 63)); +} + +void pmap_remap_rwx(qword addr) +{ + (void)addr; + + set_bits(addr, 3); +} + +qword pmap_unmap(qword addr) +{ + (void)addr; + tlbinval(addr); + + qword* pte = get_pte(addr); + qword pa = *pte & 0x0000fffffffff000; + *pte = 0; + // panic("whats unmap for"); + return pa; } diff --git a/src/limine.c b/src/limine.c index 8d9eb1c..91b538c 100644 --- a/src/limine.c +++ b/src/limine.c @@ -22,6 +22,7 @@ static volatile struct limine_hhdm_request hhdm_request = {LIMINE_HHDM_REQUEST, 0, nullptr}; static volatile struct limine_kernel_address_request kaddr = {LIMINE_KERNEL_ADDRESS_REQUEST, 0, nullptr}; +static __attribute__((used)) volatile struct limine_stack_size_request plzzstack = {LIMINE_STACK_SIZE_REQUEST, 0, nullptr, 16*1024*1024}; uint64_t _limine__virt_to_phys(uint64_t virt) { if (virt >= kaddr.response->virtual_base) return virt - kaddr.response->virtual_base + kaddr.response->physical_base; diff --git a/src/ljsupport.c b/src/ljsupport.c index f618f7b..4879e49 100644 --- a/src/ljsupport.c +++ b/src/ljsupport.c @@ -20,6 +20,7 @@ double sin(double x) { panic("todo: sin"); } double sinh(double x) { panic("todo: sinh"); } double tan(double x) { panic("todo: tan"); } double tanh(double x) { panic("todo: tanh"); } +double sqrt(double x) { panic("todo: sqrt"); } double frexp(double value, int *exp) { panic("todo: frexp"); } double fmod(double x, double y) { panic("todo: fmod"); } @@ -32,12 +33,12 @@ void feof() {panic("todo:feof");} void ferror() {panic("todo:ferror");} void fopen() {panic("todo:fopen");} void fputs() {panic("todo:fputs");} -void fputc() {panic("todo:fputc");} void fread() {panic("todo:fread");} -void fflush() {panic("todo:fflush");} +void fflush(FILE *f) {panic("todo:fflush");} void fgets() {panic("todo:fgets");} void strncpy() {panic("todo:strncpy");} void memchr() {panic("todo:memchr");} +void strncmp() {panic("todo:strncmp");} void strstr() {panic("todo:strstr");} void strerror() {panic("todo:strerror");} void strtoul() {panic("todo:strtoul");} @@ -55,6 +56,10 @@ size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restri stream->write(ptr, (int)(size * nitems)); return (int)(size * nitems); } +int fputc(int c, FILE *stream) { + fwrite(&c, 1, 1, stream); + return 1; +} void putchar(char c) { fwrite(&c, 1, 1, stdout); } diff --git a/src/memory/kalloc.c b/src/memory/kalloc.c index db760a0..c2446a7 100644 --- a/src/memory/kalloc.c +++ b/src/memory/kalloc.c @@ -70,7 +70,11 @@ static Magazine* page_mag = nullptr; static Magazine* kalloc_mags[32] = {nullptr}; static qword kalloc_heads[32] = {0}; static atomic_ullong addr = 0x0000700000000000; +static atomic_ullong kaddr = 0xffffffffA0000000; +qword kvirtalloc(qword size) { + return atomic_fetch_add(&kaddr, size); +} static qword kalloc_inner(void* ctx) { qword i = (qword)ctx; if (kalloc_heads[i] == 0) { @@ -116,13 +120,19 @@ void kalloc_init(void) { } qword page_alloc(enum PageType pty) { - (void)pty; // for now :P + (void)pty; // for now :P qword addr = mag_get(page_mag); memory_set(virt(addr, void), 0, 4096); return addr; } +void page_free(enum PageType pty, qword addr) { + (void)pty; + + mag_put(page_mag, addr); +} + static qword find_kalloc_mag(qword size) { if (size > kalloc_size_arr[15]) panic("cannot kalloc() or kfree() more than {} bytes! (attempted to kalloc/kfree {})", kalloc_size_arr[15], size); for (qword i = 0;i < 32;i++) { diff --git a/src/start.c b/src/start.c index e895b46..894dcb4 100644 --- a/src/start.c +++ b/src/start.c @@ -56,6 +56,7 @@ LUALIB_API int luaopen_string(lua_State *L); LUALIB_API int luaopen_table(lua_State *L); LUALIB_API int luaopen_debug(lua_State *L); LUALIB_API int luaopen_bit(lua_State *L); +LUALIB_API int luaopen_jit(lua_State *L); void stdout_write(const char *str, int siz) { while (siz) { @@ -64,6 +65,16 @@ void stdout_write(const char *str, int siz) { } } +static long long fib(long long x) { + if (x == 0) return 0; + if (x == 1) return 1; + return fib(x-1)+fib(x-2); +} +// static long long rdtsc() { +// long long res; +// } + + attribute(used) noreturn void kernel_start() { asm( @@ -150,6 +161,15 @@ attribute(used) noreturn void kernel_start() panic("cant open lua"); } const char *in = "print('what is the best language? it\\'s LUA, of course!')"; + in = +"function fib(x)\n" +" if x == 0 then return 0 end\n" +" if x == 1 then return 1 end\n" +" return fib(x-1)+fib(x-2)\n" +"end\n" +"print('fib 30?')\n" +"print(fib(33))\n" +; _lua_openmodule("", base); lua_openmodule(table); @@ -157,6 +177,7 @@ attribute(used) noreturn void kernel_start() lua_openmodule(math); lua_openmodule(debug); lua_openmodule(bit); + lua_openmodule(jit); FILE* stdout = _get_pcb()->stdout = kalloc(sizeof(FILE)); stdout->write = stdout_write;