bug-mes
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 16/16] lib: Linux riscv64-mes-mescc support.


From: W. J. van der Laan
Subject: [PATCH 16/16] lib: Linux riscv64-mes-mescc support.
Date: Sat, 24 Apr 2021 14:29:45 +0000

lib/linux/riscv64-mes-mescc/_exit.c: Add support modules for
building for riscv64-mes-mescc (self-hosting).
lib/linux/riscv64-mes-mescc/_write.c: Same.
lib/linux/riscv64-mes-mescc/crt1.c: Same.
lib/linux/riscv64-mes-mescc/syscall-internal.c: Same.
lib/linux/riscv64-mes-mescc/syscall.c: Same.
---
 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 
0000000000000000000000000000000000000000..749a411862d03542696b0f518c22a9998ef64818
--- /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 <janneke@gnu.org>
+ * Copyright © 2021 W. J. van der Laan <laanwj@protonmail.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#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 
0000000000000000000000000000000000000000..bd314f76616609e35136da3095aaa23ba570bbc8
--- /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 <janneke@gnu.org>
+ * Copyright © 2021 W. J. van der Laan <laanwj@protonmail.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#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 
0000000000000000000000000000000000000000..9acff4a48959269e3db92a9331e8e42d65aa1f4d
--- /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 <janneke@gnu.org>
+ * Copyright © 2021 W. J. van der Laan <laanwj@protonmail.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#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 
0000000000000000000000000000000000000000..fb6f9f85b8456d3c09981cd6ba851c6ef26226ea
--- /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 
<janneke@gnu.org>
+ * Copyright © 2021 W. J. van der Laan <laanwj@protonmail.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <errno.h>
+#include <linux/riscv64/syscall.h>
+
+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 
0000000000000000000000000000000000000000..730203967f2cc10152cea45934d5f8c6e86a7e41
--- /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 
<janneke@gnu.org>
+ * Copyright © 2021 W. J. van der Laan <laanwj@protonmail.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <errno.h>
+#include <linux/riscv64/syscall.h>
+
+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;
+}
--
2.27.0





reply via email to

[Prev in Thread] Current Thread [Next in Thread]