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.
This commit is contained in:
W. J. van der Laan 2021-04-23 17:20:27 +00:00 committed by Janneke Nieuwenhuizen
parent e7aac5e8ba
commit a1bc54bce8
5 changed files with 372 additions and 0 deletions

View file

@ -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
}

View file

@ -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");
}

View file

@ -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");
}

View file

@ -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);
}

View file

@ -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;
}