From 189ea8eb9d1f19508415dcdacf9e6426760bea24 Mon Sep 17 00:00:00 2001 From: "Jan (janneke) Nieuwenhuizen" Date: Thu, 17 Nov 2022 22:48:28 +0100 Subject: [PATCH] x86_64: build: Cater for M2-Planet. * build-aux/cflags.sh: Set cc_cpu. * build-aux/config.sh.in (cc_cpu): Export. * build-aux/export.make (cc_cpu): Likewise. * kaem.arm (cc_cpu): New variable. * kaem.x86 (cc_cpu): New variable. * kaem.run (cc_cpu): New variable. Define ____. * scaffold/argv.kaem (cc_cpu): Likewise. Define __linux__ * scaffold/global-array.kaem (cc_cpu): Likewise. * scaffold/hello.kaem (cc_cpu): Likewise. * scaffold/local-array.kaem (cc_cpu): Likewise. * scaffold/local-static-array.kaem (cc_cpu): Likewise. * scaffold/main.kaem (cc_cpu): Likewise. * scaffold/read.kaem (cc_cpu): Likewise. * kaem.x86_64: New file. lib/m2/x86_64/ELF-x86_64.hex2, lib/m2/x86_64/x86_64_defs.M1: New files, imported from M2Libc. 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: New files, adapted for M2-Planet calling convention from ... * lib/linux/x86_64-mes-mescc: ... here. * build-aux/build.sh.in: Also allow kaem build for x86_64. * include/mes/lib-mini.h[__M2__]: Define as sizeof (void*). * include/mes/mes.h[__M2__](M2_CELL_SIZE): Define as sizeof (struct scm). * lib/x86_64-mes/x86_64.M1 (add____$i8,%rbp, mov____%rdi,%rbp): New defines. * src/posix.c (TIME_UNITS_PER_SECOND): Cater for 64bit M2-Planet. --- build-aux/build.sh.in | 3 +- build-aux/cflags.sh | 4 + build-aux/config.sh.in | 1 + build-aux/export.make | 3 +- include/mes/lib-mini.h | 2 +- include/mes/mes.h | 2 +- kaem.arm | 1 + kaem.run | 2 + kaem.x86 | 1 + kaem.x86_64 | 25 ++++++ 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 | 56 ++++++++++++ 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 + scaffold/argv.kaem | 3 + scaffold/global-array.kaem | 6 +- scaffold/hello.kaem | 5 +- scaffold/local-array.kaem | 6 +- scaffold/local-static-array.kaem | 6 +- scaffold/main.kaem | 5 +- scaffold/read.kaem | 5 +- src/posix.c | 11 +-- 26 files changed, 540 insertions(+), 15 deletions(-) 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 b8900b1b..2a2cb3d0 100644 --- a/build-aux/build.sh.in +++ b/build-aux/build.sh.in @@ -102,7 +102,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/build-aux/cflags.sh b/build-aux/cflags.sh index da98b4cb..e19bd1dc 100644 --- a/build-aux/cflags.sh +++ b/build-aux/cflags.sh @@ -18,16 +18,20 @@ case "$mes_cpu" in arm) + cc_cpu=arm stage0_cpu=armv7l ;; x86_64) + cc_cpu=x86_64 stage0_cpu=amd64 ;; x86) + cc_cpu=i386 stage0_cpu=x86 ;; *) stage0_cpu=$mes_cpu + cc_cpu=$mes_cpu ;; esac diff --git a/build-aux/config.sh.in b/build-aux/config.sh.in index ae4c1ce7..a919f141 100644 --- a/build-aux/config.sh.in +++ b/build-aux/config.sh.in @@ -97,6 +97,7 @@ export CC export DIFF export SHELL export V +export cc_cpu export colors export compiler export courageous diff --git a/build-aux/export.make b/build-aux/export.make index 3bfb0bff..05b85611 100644 --- a/build-aux/export.make +++ b/build-aux/export.make @@ -1,5 +1,5 @@ # GNU Mes --- Maxwell Equations of Software -# Copyright © 2018 Jan (janneke) Nieuwenhuizen +# Copyright © 2018,2023 Jan (janneke) Nieuwenhuizen # # This file is part of GNU Mes. # @@ -43,6 +43,7 @@ export moduledir export sysconfdir export bootstrap +export cc_cpu export compiler export courageous export mes_system diff --git a/include/mes/lib-mini.h b/include/mes/lib-mini.h index eb994f79..48d91362 100644 --- a/include/mes/lib-mini.h +++ b/include/mes/lib-mini.h @@ -41,7 +41,7 @@ for that by multiplying with M2_PTR_SIZE when using (char) pointers. */ #if __M2__ -#define M2_PTR_SIZE 4 +#define M2_PTR_SIZE sizeof (void*) #else #define M2_PTR_SIZE 1 #endif diff --git a/include/mes/mes.h b/include/mes/mes.h index 0323b2dc..c189ab69 100644 --- a/include/mes/mes.h +++ b/include/mes/mes.h @@ -57,7 +57,7 @@ struct scm for that by multiplying with M2_CELL_SIZE when using cell pointers. */ #if __M2__ -#define M2_CELL_SIZE 12 +#define M2_CELL_SIZE sizeof (struct scm) #else #define M2_CELL_SIZE 1 #endif diff --git a/kaem.arm b/kaem.arm index 4e973a1f..4357c62b 100644 --- a/kaem.arm +++ b/kaem.arm @@ -19,6 +19,7 @@ # Usage: # kaem --verbose --strict +cc_cpu=arm mes_cpu=arm stage0_cpu=armv7l kaem --verbose --strict diff --git a/kaem.run b/kaem.run index 1179aa23..d4d51b54 100644 --- a/kaem.run +++ b/kaem.run @@ -19,6 +19,7 @@ # Usage: # kaem --verbose --strict +cc_cpu=${cc_cpu:-i386} mes_cpu=${mes_cpu:-x86} stage0_cpu=${stage0_cpu:-x86} @@ -27,6 +28,7 @@ mkdir -p m2 M2-Planet \ --debug \ --architecture ${stage0_cpu} \ + -D __${cc_cpu}__=1 \ -D __linux__=1 \ -f include/mes/config.h \ -f include/mes/lib-mini.h \ diff --git a/kaem.x86 b/kaem.x86 index 60ab6e6b..caea7f87 100644 --- a/kaem.x86 +++ b/kaem.x86 @@ -19,6 +19,7 @@ # Usage: # kaem --verbose --strict +cc_cpu=i386 mes_cpu=x86 stage0_cpu=x86 kaem --verbose --strict diff --git a/kaem.x86_64 b/kaem.x86_64 new file mode 100644 index 00000000..b20aecc3 --- /dev/null +++ b/kaem.x86_64 @@ -0,0 +1,25 @@ +#! /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 + +cc_cpu=x86_64 +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..5a792ff2 --- /dev/null +++ b/lib/linux/x86_64-mes-m2/crt1.M1 @@ -0,0 +1,56 @@ +### GNU Mes --- Maxwell Equations of Software +### Copyright © 2017,2018,2019,2022,2023 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 . + +:_start + push___%rbp + mov____%rsp,%rbp + + ;; 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 + + ;; 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____%rbp,%rax ;envp + add____$i8,%rax !8 + mov____(%rax),%rax + add____$i8,%rax !0x03 + shl____$i8,%rax !0x03 + add____%rbp,%rax + push___%rax + + ;; mes c lib environ + mov____%rax,0x32 &GLOBAL_environ + + call32 %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 +### !