From a1bc54bce8ddc17832f5c15bf55355496abff6ba Mon Sep 17 00:00:00 2001 From: "W. J. van der Laan" Date: Fri, 23 Apr 2021 17:20:27 +0000 Subject: [PATCH] lib: Linux riscv64-mes-mescc support. * lib/linux/riscv64-mes-mescc/_exit.c, lib/linux/riscv64-mes-mescc/_write.c, lib/linux/riscv64-mes-mescc/crt1.c, lib/linux/riscv64-mes-mescc/syscall-internal.c, lib/linux/riscv64-mes-mescc/syscall.c: New files. These add support for building for riscv64-mes-mescc and make it self-hosting. --- lib/linux/riscv64-mes-mescc/_exit.c | 31 +++ lib/linux/riscv64-mes-mescc/_write.c | 33 ++++ lib/linux/riscv64-mes-mescc/crt1.c | 70 +++++++ .../riscv64-mes-mescc/syscall-internal.c | 56 ++++++ lib/linux/riscv64-mes-mescc/syscall.c | 182 ++++++++++++++++++ 5 files changed, 372 insertions(+) create mode 100644 lib/linux/riscv64-mes-mescc/_exit.c create mode 100644 lib/linux/riscv64-mes-mescc/_write.c create mode 100644 lib/linux/riscv64-mes-mescc/crt1.c create mode 100644 lib/linux/riscv64-mes-mescc/syscall-internal.c create mode 100644 lib/linux/riscv64-mes-mescc/syscall.c diff --git a/lib/linux/riscv64-mes-mescc/_exit.c b/lib/linux/riscv64-mes-mescc/_exit.c new file mode 100644 index 00000000..749a4118 --- /dev/null +++ b/lib/linux/riscv64-mes-mescc/_exit.c @@ -0,0 +1,31 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018,2020 Jan (janneke) Nieuwenhuizen + * Copyright © 2021 W. J. van der Laan + * + * 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 ("ld_____%a0,0x10(%fp)"); + asm ("li_____%a7,SYS_exit"); + asm ("ecall"); + // no need to read return value +} diff --git a/lib/linux/riscv64-mes-mescc/_write.c b/lib/linux/riscv64-mes-mescc/_write.c new file mode 100644 index 00000000..bd314f76 --- /dev/null +++ b/lib/linux/riscv64-mes-mescc/_write.c @@ -0,0 +1,33 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018,2020 Jan (janneke) Nieuwenhuizen + * Copyright © 2021 W. J. van der Laan + * + * 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 ("ld_____%a0,0x10(%fp)"); + asm ("ld_____%a1,0x18(%fp)"); + asm ("ld_____%a2,0x20(%fp)"); + asm ("li_____%a7,SYS_write"); + asm ("ecall"); + asm ("mv_____%t0,%a0"); +} diff --git a/lib/linux/riscv64-mes-mescc/crt1.c b/lib/linux/riscv64-mes-mescc/crt1.c new file mode 100644 index 00000000..9acff4a4 --- /dev/null +++ b/lib/linux/riscv64-mes-mescc/crt1.c @@ -0,0 +1,70 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018 Jan (janneke) Nieuwenhuizen + * Copyright © 2021 W. J. van der Laan + * + * 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 main (int argc, char *argv[], char *envp[]); + +/* mesc will generate the following preamble: + + push ra + push fp +*/ +int +_start () +{ + asm ("li_____%t0,$i16_0000 @0"); + asm ("li_____%t1,$i32 &__stdin"); + asm ("sw_____%t0,0(%t1)"); + + asm ("li_____%t0,$i16_0000 @1"); + asm ("srai___%t0,16"); + asm ("li_____%t1,$i32 &__stdout"); + asm ("sw_____%t0,0(%t1)"); + + asm ("li_____%t0,$i16_0000 @2"); + asm ("srai___%t0,16"); + asm ("li_____%t1,$i32 &__stderr"); + asm ("sw_____%t0,0(%t1)"); + + // environ is &argv[argc + 1] + asm ("mv_____%t1,%fp"); + asm ("addi___%t1,%t1,$i8_8 !0x1"); // 0x10 to skip over pushed fp+ra, 0x8 to skip over argc + asm ("addi___%t5,%fp,$i8_0 !0x1"); // 0x10 to skip over pushed fp+ra + asm ("ld_____%t0,0(%t5)"); + asm ("addi___%t0,%t0,1"); + asm ("li_____%t5,$i32 %0x3"); // skip over all arguments and the final NULL + asm ("sll____%t0,%t0,%t5"); + asm ("add____%t0,%t0,%t1"); + asm ("push___%t0"); // envp + asm ("push___%t1"); // argv + asm ("li_____%t1,$i32 &environ"); + asm ("sd_____%t0,0(%t1)"); + asm ("addi___%t5,%fp,$i8_0 !0x1"); // 0x10 to skip over pushed fp+ra + asm ("ld_____%t0,0(%t5)"); + asm ("push___%t0"); // argc + + main (); + + asm ("mv_____%a0,%t0"); + asm ("li_____%a7,SYS_exit"); + asm ("ecall"); + asm ("ebreak"); +} diff --git a/lib/linux/riscv64-mes-mescc/syscall-internal.c b/lib/linux/riscv64-mes-mescc/syscall-internal.c new file mode 100644 index 00000000..fb6f9f85 --- /dev/null +++ b/lib/linux/riscv64-mes-mescc/syscall-internal.c @@ -0,0 +1,56 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2020 Jan (janneke) Nieuwenhuizen + * Copyright © 2021 W. J. van der Laan + * + * 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 + +static long +__sys_call_internal (long sys_call) +{ + asm ("ld_____%a7,0x10(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +static long +__sys_call2_internal (long sys_call, long one, long two) +{ + asm ("ld_____%a7,0x10(%fp)"); + asm ("ld_____%a0,0x18(%fp)"); + asm ("ld_____%a1,0x20(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +/* Return < 0 on error (errno-like value from kernel), or 0 on success */ +int +__raise (int signum) +{ + long pid = __sys_call_internal (SYS_getpid); + if (pid < 0) + return pid; + else + return __sys_call2_internal (SYS_kill, pid, signum); +} diff --git a/lib/linux/riscv64-mes-mescc/syscall.c b/lib/linux/riscv64-mes-mescc/syscall.c new file mode 100644 index 00000000..73020396 --- /dev/null +++ b/lib/linux/riscv64-mes-mescc/syscall.c @@ -0,0 +1,182 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2020 Jan (janneke) Nieuwenhuizen + * Copyright © 2021 W. J. van der Laan + * + * 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 ("ld_____%a7,0x10(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +long +__sys_call1 (long sys_call, long one) +{ + asm ("ld_____%a7,0x10(%fp)"); + asm ("ld_____%a0,0x18(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +long +__sys_call2 (long sys_call, long one, long two) +{ + asm ("ld_____%a7,0x10(%fp)"); + asm ("ld_____%a0,0x18(%fp)"); + asm ("ld_____%a1,0x20(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +long +__sys_call3 (long sys_call, long one, long two, long three) +{ + asm ("ld_____%a7,0x10(%fp)"); + asm ("ld_____%a0,0x18(%fp)"); + asm ("ld_____%a1,0x20(%fp)"); + asm ("ld_____%a2,0x28(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +long +__sys_call4 (long sys_call, long one, long two, long three, long four) +{ + asm ("ld_____%a7,0x10(%fp)"); + asm ("ld_____%a0,0x18(%fp)"); + asm ("ld_____%a1,0x20(%fp)"); + asm ("ld_____%a2,0x28(%fp)"); + asm ("ld_____%a3,0x30(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +long +__sys_call5 (long sys_call, long one, long two, long three, long four, long five) +{ + asm ("ld_____%a7,0x10(%fp)"); + asm ("ld_____%a0,0x18(%fp)"); + asm ("ld_____%a1,0x20(%fp)"); + asm ("ld_____%a2,0x28(%fp)"); + asm ("ld_____%a3,0x30(%fp)"); + asm ("ld_____%a4,0x38(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +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; +} + +long +_sys_call5 (long sys_call, long one, long two, long three, long four, long five) +{ + long r = __sys_call5 (sys_call, one, two, three, four, five); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +}