From c4e58b5a185416b6ba9aa107809637781919def2 Mon Sep 17 00:00:00 2001 From: "Jan (janneke) Nieuwenhuizen" Date: Sat, 7 May 2022 08:08:28 +0200 Subject: [PATCH] DRAFT x86_64: build: Cater for M2-Planet. XXX scaffold tests run, * MES_DEBUG=3 bin/mes-m2 segfaults in gc_init. * kaem.run: Define ____. * kaem.x86_64, lib/linux/x86_64-mes-m2/_exit.c, lib/linux/x86_64-mes-m2/_write.c, lib/linux/x86_64-mes-m2/crt1.M1, lib/linux/x86_64-mes-m2/crt1.c, lib/linux/x86_64-mes-m2/syscall.c, lib/m2/x86_64/ELF-x86_64.hex2, lib/m2/x86_64/x86_64_defs.M1: New files. * build-aux/build.sh.in: Also allow kaem build for X86_64. * include/mes/lib-mini.h[__M2__ && __x86_64__](M2_PTR_SIZE): Define as 8. * include/mes/mes.h[__M2__ && __x86_64__](M2_CELL_SIZE): Define as 24. * lib/x86_64-mes/x86_64.M1 (add____$i8,%rbp, mov____%rdi,%rbp): New defines. --- build-aux/build.sh.in | 3 +- include/mes/lib-mini.h | 4 + include/mes/mes.h | 4 + kaem.run | 1 + kaem.x86_64 | 24 ++++++ lib/linux/x86_64-mes-m2/_exit.c | 29 +++++++ lib/linux/x86_64-mes-m2/_write.c | 31 +++++++ lib/linux/x86_64-mes-m2/crt1.M1 | 118 +++++++++++++++++++++++++ lib/linux/x86_64-mes-m2/crt1.c | 34 ++++++++ lib/linux/x86_64-mes-m2/syscall.c | 137 ++++++++++++++++++++++++++++++ lib/m2/x86_64/ELF-x86_64.hex2 | 74 ++++++++++++++++ lib/m2/x86_64/x86_64_defs.M1 | 101 ++++++++++++++++++++++ lib/x86_64-mes/x86_64.M1 | 2 + 13 files changed, 561 insertions(+), 1 deletion(-) create mode 100644 kaem.x86_64 create mode 100644 lib/linux/x86_64-mes-m2/_exit.c create mode 100644 lib/linux/x86_64-mes-m2/_write.c create mode 100644 lib/linux/x86_64-mes-m2/crt1.M1 create mode 100644 lib/linux/x86_64-mes-m2/crt1.c create mode 100644 lib/linux/x86_64-mes-m2/syscall.c create mode 100644 lib/m2/x86_64/ELF-x86_64.hex2 create mode 100644 lib/m2/x86_64/x86_64_defs.M1 diff --git a/build-aux/build.sh.in b/build-aux/build.sh.in index bbde993c..65846f0a 100644 --- a/build-aux/build.sh.in +++ b/build-aux/build.sh.in @@ -103,7 +103,8 @@ fi ) if test -n "$M2_PLANET" && (test "$mes_cpu" == "x86" \ - || test "$mes_cpu" == "arm"); then + || test "$mes_cpu" == "x86_64" \ + || test "$mes_cpu" == "arm" ); then ( . ${srcdest}build-aux/trace.sh if [ -z "$V" -o "$V" = 0 ]; then diff --git a/include/mes/lib-mini.h b/include/mes/lib-mini.h index eb994f79..28acb926 100644 --- a/include/mes/lib-mini.h +++ b/include/mes/lib-mini.h @@ -41,7 +41,11 @@ for that by multiplying with M2_PTR_SIZE when using (char) pointers. */ #if __M2__ +#if __x86_64__ +#define M2_PTR_SIZE 8 +#else #define M2_PTR_SIZE 4 +#endif #else #define M2_PTR_SIZE 1 #endif diff --git a/include/mes/mes.h b/include/mes/mes.h index 0323b2dc..3f5e64b5 100644 --- a/include/mes/mes.h +++ b/include/mes/mes.h @@ -57,7 +57,11 @@ struct scm for that by multiplying with M2_CELL_SIZE when using cell pointers. */ #if __M2__ +#if __x86_64__ +#define M2_CELL_SIZE 24 +#else #define M2_CELL_SIZE 12 +#endif #else #define M2_CELL_SIZE 1 #endif diff --git a/kaem.run b/kaem.run index e607b5c0..ccbc773a 100644 --- a/kaem.run +++ b/kaem.run @@ -27,6 +27,7 @@ mkdir -p m2 M2-Planet \ --debug \ --architecture ${stage0_cpu} \ + -D __${mes_cpu}__=1 \ -D __linux__=1 \ -f include/mes/config.h \ -f include/mes/lib-mini.h \ diff --git a/kaem.x86_64 b/kaem.x86_64 new file mode 100644 index 00000000..e6329ba8 --- /dev/null +++ b/kaem.x86_64 @@ -0,0 +1,24 @@ +#! /bin/sh +# Copyright © 2022 Jan (janneke) Nieuwenhuizen +# +# This file is part of GNU Mes. +# +# GNU Mes is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or (at +# your option) any later version. +# +# GNU Mes is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Mes. If not, see . + +# Usage: +# kaem --verbose --strict + +mes_cpu=x86_64 +stage0_cpu=amd64 +kaem --verbose --strict diff --git a/lib/linux/x86_64-mes-m2/_exit.c b/lib/linux/x86_64-mes-m2/_exit.c new file mode 100644 index 00000000..3396ebb7 --- /dev/null +++ b/lib/linux/x86_64-mes-m2/_exit.c @@ -0,0 +1,29 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018,2020,2022 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include "mes/lib-mini.h" + +void +_exit (int status) +{ + asm ("mov____$i32,%rax SYS_exit"); + asm ("mov____0x8(%rbp),%rdi !-8"); + asm ("syscall"); +} diff --git a/lib/linux/x86_64-mes-m2/_write.c b/lib/linux/x86_64-mes-m2/_write.c new file mode 100644 index 00000000..25b0faed --- /dev/null +++ b/lib/linux/x86_64-mes-m2/_write.c @@ -0,0 +1,31 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018,2020,2022 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include "mes/lib-mini.h" + +void +_write (int filedes, void const *buffer, size_t size) +{ + asm ("mov____$i32,%rax SYS_write"); + asm ("mov____0x8(%rbp),%rdi !-8"); + asm ("mov____0x8(%rbp),%rsi !-16"); + asm ("mov____0x8(%rbp),%rdx !-24"); + asm ("syscall"); +} diff --git a/lib/linux/x86_64-mes-m2/crt1.M1 b/lib/linux/x86_64-mes-m2/crt1.M1 new file mode 100644 index 00000000..bea82b75 --- /dev/null +++ b/lib/linux/x86_64-mes-m2/crt1.M1 @@ -0,0 +1,118 @@ +### GNU Mes --- Maxwell Equations of Software +### Copyright (C) 2016 Jeremiah Orians +### Copyright © 2017,2018,2019,2022 Jan (janneke) Nieuwenhuizen +### +### This file is part of GNU Mes. +### +### GNU Mes is free software; you can redistribute it and/or modify it +### under the terms of the GNU General Public License as published by +### the Free Software Foundation; either version 3 of the License, or (at +### your option) any later version. +### +### GNU Mes is distributed in the hope that it will be useful, but +### WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with GNU Mes. If not, see . + +:UNUSED_start + push___%rbp + mov____%rsp,%rbp + sub____$i32,%rsp %0x10a4 + mov____$i8,%rax !0 + movl___%eax,0x32 &GLOBAL___stdin + mov____$i8,%rax !1 + movl___%eax,0x32 &GLOBAL___stdout + mov____$i8,%rax !2 + movl___%eax,0x32 &GLOBAL___stderr + + mov____%rbp,%rax + add____$i8,%rax !8 + mov____(%rax),%rax + add____$i8,%rax !0x03 + shl____$i8,%rax !0x03 + add____%rbp,%rax + mov____%rax,0x32 &GLOBAL_environ + ;; push___%rax ;M2-Planet pushes forward + + mov____%rbp,%rax ;argc + add____$i8,%rax !8 + mov____(%rax),%rax + push___%rax + + mov____%rbp,%rax ;argv + add____$i8,%rax !16 + push___%rax + + mov____0x32,%rax &GLOBAL_environ + push___%rax ;env + + mov____%rdi,%rbp ;M2-Planet calling convention + add____$i8,%rbp !8 + + call32 %FUNCTION_main + add____$i8,%rsp !0x0 + test___%rax,%rax + mov____%rax,%rdi + mov____$i32,%rax %0x3c + syscall + hlt + mov____%rbp,%rsp + pop____%rbp + ret + +:_start + + COPY_rsp_to_rbp ; Protect rsp + + ;; Prepare argv + LOAD_BASE_ADDRESS_rax %8 ; ARGV_address = RBP + 8 + PUSH_RAX ; Put argv on the stack + + ;; Prepare envp + COPY_rbp_to_rax ; Address we need to load from + LOAD_INTEGER ; Get ARGC + ADD_IMMEDIATE_to_rax %2 ; OFFSET = ARGC + 2 + SAL_rax_Immediate8 !3 ; OFFSET = OFFSET * WORDSIZE + ADD_rbp_to_rax ; ENVP_address = RSP + OFFSET + PUSH_RAX ; Put envp on the stack + + ;; mes c lib environ + mov____%rax,0x32 &GLOBAL_environ + ;; mes c lib io + mov____$i8,%rax !0 + movl___%eax,0x32 &GLOBAL___stdin + mov____$i8,%rax !1 + movl___%eax,0x32 &GLOBAL___stdout + mov____$i8,%rax !2 + movl___%eax,0x32 &GLOBAL___stderr + + LOAD_IMMEDIATE_rax &GLOBAL___stdin + PUSH_RAX #_common_recursion + LOAD_IMMEDIATE_rax %0 + POP_RBX # _common_recursion + STORE_INTEGER + + LOAD_IMMEDIATE_rax &GLOBAL___stdout + PUSH_RAX #_common_recursion + LOAD_IMMEDIATE_rax %1 + POP_RBX # _common_recursion + STORE_INTEGER + + LOAD_IMMEDIATE_rax &GLOBAL___stderr + PUSH_RAX #_common_recursion + LOAD_IMMEDIATE_rax %2 + POP_RBX # _common_recursion + STORE_INTEGER + + ;; Stack offset + ADD_IMMEDIATE_to_rbp %8 ; Fix rbp + + ;; Perform the main loop + CALL_IMMEDIATE %FUNCTION_main + + mov____%rax,%rdi + mov____$i32,%rax SYS_exit + syscall diff --git a/lib/linux/x86_64-mes-m2/crt1.c b/lib/linux/x86_64-mes-m2/crt1.c new file mode 100644 index 00000000..da4c4824 --- /dev/null +++ b/lib/linux/x86_64-mes-m2/crt1.c @@ -0,0 +1,34 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018,2022 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include "mes/lib-mini.h" + +int __stdin; +int __stdout; +int __stderr; +char **environ; +int main (int argc, char **argv, char **envp); + +/* FIXME: this is going to be called `FUNCTION__start' */ +//#int +//#_start () +//#{ +//# .. +//#} diff --git a/lib/linux/x86_64-mes-m2/syscall.c b/lib/linux/x86_64-mes-m2/syscall.c new file mode 100644 index 00000000..0675be0b --- /dev/null +++ b/lib/linux/x86_64-mes-m2/syscall.c @@ -0,0 +1,137 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2020,2022 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include +#include + +long +__sys_call (long sys_call) +{ + asm ("mov____0x8(%rbp),%rax !-8"); + asm ("syscall"); +} + +long +__sys_call1 (long sys_call, long one) +{ + asm ("mov____0x8(%rbp),%rax !-8"); + asm ("mov____0x8(%rbp),%rdi !-16"); + asm ("syscall"); +} + +long +__sys_call2 (long sys_call, long one, long two) +{ + asm ("mov____0x8(%rbp),%rax !-8"); + asm ("mov____0x8(%rbp),%rdi !-16"); + asm ("mov____0x8(%rbp),%rsi !-24"); + asm ("syscall"); +} + +long +__sys_call3 (long sys_call, long one, long two, long three) +{ + asm ("mov____0x8(%rbp),%rax !-8"); + asm ("mov____0x8(%rbp),%rdi !-16"); + asm ("mov____0x8(%rbp),%rsi !-24"); + asm ("mov____0x8(%rbp),%rdx !-32"); + asm ("syscall"); +} + +long +__sys_call4 (long sys_call, long one, long two, long three, long four) +{ + asm ("mov____0x8(%rbp),%rax !-8"); + asm ("mov____0x8(%rbp),%rdi !-16"); + asm ("mov____0x8(%rbp),%rsi !-24"); + asm ("mov____0x8(%rbp),%rdx !-32"); + asm ("mov____0x8(%rbp),%r10 !-40"); + asm ("syscall"); +} + +long +_sys_call (long sys_call) +{ + long r = __sys_call (sys_call); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} + +long +_sys_call1 (long sys_call, long one) +{ + long r = __sys_call1 (sys_call, one); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} + +long +_sys_call2 (long sys_call, long one, long two) +{ + long r = __sys_call2 (sys_call, one, two); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} + +long +_sys_call3 (long sys_call, long one, long two, long three) +{ + long r = __sys_call3 (sys_call, one, two, three); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} + +long +_sys_call4 (long sys_call, long one, long two, long three, long four) +{ + long r = __sys_call4 (sys_call, one, two, three, four); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} diff --git a/lib/m2/x86_64/ELF-x86_64.hex2 b/lib/m2/x86_64/ELF-x86_64.hex2 new file mode 100644 index 00000000..32ccc7c7 --- /dev/null +++ b/lib/m2/x86_64/ELF-x86_64.hex2 @@ -0,0 +1,74 @@ +### Copyright (C) 2016 Jeremiah Orians +### Copyright (C) 2017 Jan Nieuwenhuizen +### This file is part of M2-Planet. +### +### M2-Planet is free software: you can redistribute it and/or modify +### it under the terms of the GNU General Public License as published by +### the Free Software Foundation, either version 3 of the License, or +### (at your option) any later version. +### +### M2-Planet is distributed in the hope that it will be useful, +### but WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with M2-Planet. If not, see . + +### stage0's hex2 format +### !