From c6f3dfc3ec9cb7bbe6d1cca0e02bdd61ec3bc880 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Sat, 15 Feb 2020 15:55:43 +0100 Subject: [PATCH] Add some ANSI C procedures to base-common This is for the convenience of language runtimes. --- repos/base-hw/src/lib/base-common/Tupfile | 3 + repos/base-linux/src/lib/base-common/Tupfile | 3 + repos/base-nova/src/lib/base-common/Tupfile | 3 + repos/base/lib/symbols/ld | 5 + repos/base/src/lib/base-common/Tupfile.inc | 15 +++ repos/base/src/lib/base-common/ansi_c.cc | 28 ++++ repos/base/src/lib/base-common/arm/setjmp.S | 120 ++++++++++++++++++ .../base/src/lib/base-common/arm_64/setjmp.S | 108 ++++++++++++++++ .../base/src/lib/base-common/x86_32/setjmp.S | 79 ++++++++++++ .../base/src/lib/base-common/x86_64/setjmp.S | 93 ++++++++++++++ 10 files changed, 457 insertions(+) create mode 100644 repos/base/src/lib/base-common/Tupfile.inc create mode 100644 repos/base/src/lib/base-common/ansi_c.cc create mode 100644 repos/base/src/lib/base-common/arm/setjmp.S create mode 100644 repos/base/src/lib/base-common/arm_64/setjmp.S create mode 100644 repos/base/src/lib/base-common/x86_32/setjmp.S create mode 100644 repos/base/src/lib/base-common/x86_64/setjmp.S diff --git a/repos/base-hw/src/lib/base-common/Tupfile b/repos/base-hw/src/lib/base-common/Tupfile index c5b10c07a..b967d90bf 100644 --- a/repos/base-hw/src/lib/base-common/Tupfile +++ b/repos/base-hw/src/lib/base-common/Tupfile @@ -1,7 +1,9 @@ include_rules BASE_COMMON_DIR = $(BASE_DIR)/src/lib/base-common +include $(BASE_COMMON_DIR)/Tupfile.inc +SRC_CC += $(BASE_COMMON_DIR)/ansi_c.cc SRC_CC += $(BASE_COMMON_DIR)/allocator_avl.cc SRC_CC += $(BASE_COMMON_DIR)/avl_tree.cc SRC_CC += $(BASE_COMMON_DIR)/child.cc @@ -39,5 +41,6 @@ SRC_CC += signal_transmitter.cc SRC_CC += thread_bootstrap.cc : foreach $(SRC_CC) |> !cxx |> {obj} +: foreach $(SRC_S) |> !asm |> {obj} : {obj} |> !ar |> base-hw-common.lib.a | $(REP_DIR)/ diff --git a/repos/base-linux/src/lib/base-common/Tupfile b/repos/base-linux/src/lib/base-common/Tupfile index 1395eefb8..bda315b35 100644 --- a/repos/base-linux/src/lib/base-common/Tupfile +++ b/repos/base-linux/src/lib/base-common/Tupfile @@ -1,6 +1,8 @@ include_rules GENERIC_DIR = $(BASE_DIR)/src/lib/base-common +include $(GENERIC_DIR)/Tupfile.inc GENERIC_SRC_CC += \ + $(GENERIC_DIR)/ansi_c.cc \ $(GENERIC_DIR)/allocator_avl.cc \ $(GENERIC_DIR)/avl_tree.cc \ $(GENERIC_DIR)/capability.cc \ @@ -32,4 +34,5 @@ GENERIC_SRC_CC += \ : foreach $(GENERIC_SRC_CC) |> !cxx |> {obj} : foreach *.cc |> !cxx |> {obj} +: foreach $(SRC_S) |> !asm |> {obj} : {obj} |> !ar |> base-linux-common.lib.a | $(REP_DIR)/ diff --git a/repos/base-nova/src/lib/base-common/Tupfile b/repos/base-nova/src/lib/base-common/Tupfile index 5a824c492..db36c9bb9 100644 --- a/repos/base-nova/src/lib/base-common/Tupfile +++ b/repos/base-nova/src/lib/base-common/Tupfile @@ -1,8 +1,10 @@ include_rules GENERIC_DIR = $(BASE_DIR)/src/lib/base-common +include $(GENERIC_DIR)/Tupfile.inc GENERIC_SRC_CC += \ + $(GENERIC_DIR)/ansi_c.cc \ $(GENERIC_DIR)/allocator_avl.cc \ $(GENERIC_DIR)/avl_tree.cc \ $(GENERIC_DIR)/child.cc \ @@ -34,5 +36,6 @@ GENERIC_SRC_CC += \ $(GENERIC_DIR)/vm_session.cc \ : foreach $(GENERIC_SRC_CC) |> !cxx |> %B.o {obj} +: foreach $(SRC_S) |> !asm |> {obj} : foreach *.cc |> !cxx |> {obj} : {obj} |> !ar |> base-nova-common.lib.a | $(REP_DIR)/ diff --git a/repos/base/lib/symbols/ld b/repos/base/lib/symbols/ld index 2e76e157d..2f2daaa62 100644 --- a/repos/base/lib/symbols/ld +++ b/repos/base/lib/symbols/ld @@ -649,9 +649,14 @@ dl_unwind_find_exidx T genode_argc D 4 genode_argv D 8 genode_envp B 8 +longjmp W lx_environ B 8 +memcmp W memcpy W memmove W memset W +setjmp W stdout_reconnect T +strcmp W +strlen W wait_for_continue T diff --git a/repos/base/src/lib/base-common/Tupfile.inc b/repos/base/src/lib/base-common/Tupfile.inc new file mode 100644 index 000000000..dc5d65175 --- /dev/null +++ b/repos/base/src/lib/base-common/Tupfile.inc @@ -0,0 +1,15 @@ +ifeq (@(TUP_ARCH),arm) +SRC_S += $(BASE_DIR)/src/lib/base-common/arm/*.S +endif + +ifeq (@(TUP_ARCH),arm64) +SRC_S += $(BASE_DIR)/src/lib/base-common/arm_64/*.S +endif + +ifeq (@(TUP_ARCH),i386) +SRC_S += $(BASE_DIR)/src/lib/base-common/x86_32/*.S +endif + +ifeq (@(TUP_ARCH),x86_64) +SRC_S += $(BASE_DIR)/src/lib/base-common/x86_64/*.S +endif diff --git a/repos/base/src/lib/base-common/ansi_c.cc b/repos/base/src/lib/base-common/ansi_c.cc new file mode 100644 index 000000000..ba44bbfdc --- /dev/null +++ b/repos/base/src/lib/base-common/ansi_c.cc @@ -0,0 +1,28 @@ +/* + * \brief Implementation of basic C utilities + * \author Emery Hemingway + * \date 2020-01-14 + */ + +/* + * Copyright (C) 2020 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +#include + +using namespace Genode; + +extern "C" __attribute__((weak)) +int memcmp(const void *p0, const void *p1, size_t size) +{ + return Genode::memcmp(p0, p1, size); +} + +extern "C" __attribute__((weak)) +size_t strlen(const char *s) +{ + return Genode::strlen(s); +} diff --git a/repos/base/src/lib/base-common/arm/setjmp.S b/repos/base/src/lib/base-common/arm/setjmp.S new file mode 100644 index 000000000..95b6432be --- /dev/null +++ b/repos/base/src/lib/base-common/arm/setjmp.S @@ -0,0 +1,120 @@ +/* $NetBSD: setjmp.S,v 1.5 2003/04/05 23:08:51 bjh21 Exp $ */ + +/* + * Copyright (c) 1997 Mark Brinicombe + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Mark Brinicombe + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* needed parts from */ +#define __FBSDID(x) +#define ENTRY(x) .text; .align 0; .globl x; .type x,#function; x:; +#define WEAK_ALIAS(x,y) +#define RET mov pc, lr +#define _JB_MAGIC__SETJMP 0x4278f500 + +#define __SOFTFP__ 1 +#define _STANDALONE +/* end of */ + +__FBSDID("$FreeBSD: release/8.2.0/lib/libc/arm/gen/setjmp.S 193145 2009-05-31 02:03:40Z marcel $"); + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from the last call to + * setjmp(a) + * by restoring registers from the stack. + * The previous signal state is NOT restored. + * + * Note: r0 is the return value + * r1-r3 are scratch registers in functions + */ + +ENTRY(setjmp) + ldr r1, .Lsetjmp_magic + str r1, [r0], #4 +#ifdef __SOFTFP__ + add r0, r0, #52 +#else + /* Store fp registers */ + sfm f4, 4, [r0], #48 + /* Store fpsr */ + rfs r1 + str r1, [r0], #0x0004 +#endif /* __SOFTFP__ */ + /* Store integer registers */ + stmia r0, {r4-r14} + + mov r0, #0x00000000 + RET + +.Lsetjmp_magic: + .word _JB_MAGIC__SETJMP + +WEAK_ALIAS(__longjmp, longjmp) +ENTRY(longjmp) + ldr r2, .Lsetjmp_magic + ldr r3, [r0], #4 + teq r2, r3 + bne botch + +#ifdef __SOFTFP__ + add r0, r0, #52 +#else + /* Restore fp registers */ + lfm f4, 4, [r0], #48 + /* Restore fpsr */ + ldr r4, [r0], #0x0004 + wfs r4 +#endif /* __SOFTFP__ */ + /* Restore integer registers */ + ldmia r0, {r4-r14} + + /* Validate sp and r14 */ + teq sp, #0 + teqne r14, #0 + beq botch + + /* Set return value */ + mov r0, r1 + teq r0, #0x00000000 + moveq r0, #0x00000001 + RET + + /* validation failed, die die die. */ +botch: +#if !defined(_STANDALONE) + bl PIC_SYM(_C_LABEL(longjmperror), PLT) + bl PIC_SYM(_C_LABEL(abort), PLT) + b . - 8 /* Cannot get here */ +#else + b . +#endif diff --git a/repos/base/src/lib/base-common/arm_64/setjmp.S b/repos/base/src/lib/base-common/arm_64/setjmp.S new file mode 100644 index 000000000..9fb59245c --- /dev/null +++ b/repos/base/src/lib/base-common/arm_64/setjmp.S @@ -0,0 +1,108 @@ +/*- + * Copyright (c) 2014 Andrew Turner + * Copyright (c) 2014 The FreeBSD Foundation + * All rights reserved. + * + * Portions of this software were developed by Andrew Turner + * under sponsorship from the FreeBSD Foundation + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/* needed parts from */ +#define __FBSDID(x) +#define ENTRY(sym) .text; .globl sym; .align 2; .type sym,#function; sym: +#define END(sym) .size sym, . - sym +/* end of */ + +__FBSDID("$FreeBSD: releng/12.0/lib/libc/aarch64/gen/setjmp.S 313146 2017-02-03 11:51:06Z andrew $"); + +/* needed parts from */ +#define _JB_SIGMASK 22 +#define _JB_MAGIC__SETJMP 0xfb5d25837d7ff700 +/* end of */ + +ENTRY(setjmp) + /* Store the magic value and stack pointer */ + ldr x8, .Lmagic + mov x9, sp + stp x8, x9, [x0], #16 + + /* Store the general purpose registers and lr */ + stp x19, x20, [x0], #16 + stp x21, x22, [x0], #16 + stp x23, x24, [x0], #16 + stp x25, x26, [x0], #16 + stp x27, x28, [x0], #16 + stp x29, lr, [x0], #16 + +#ifndef _STANDALONE + /* Store the vfp registers */ + stp d8, d9, [x0], #16 + stp d10, d11, [x0], #16 + stp d12, d13, [x0], #16 + stp d14, d15, [x0] +#endif + + /* Return value */ + mov x0, #0 + ret + .align 3 +.Lmagic: + .quad _JB_MAGIC__SETJMP +END(setjmp) + +ENTRY(longjmp) + /* Check the magic value */ + ldr x8, [x0], #8 + ldr x9, .Lmagic + cmp x8, x9 + b.ne botch + + /* Restore the stack pointer */ + ldr x8, [x0], #8 + mov sp, x8 + + /* Restore the general purpose registers and lr */ + ldp x19, x20, [x0], #16 + ldp x21, x22, [x0], #16 + ldp x23, x24, [x0], #16 + ldp x25, x26, [x0], #16 + ldp x27, x28, [x0], #16 + ldp x29, lr, [x0], #16 + +#ifndef _STANDALONE + /* Restore the vfp registers */ + ldp d8, d9, [x0], #16 + ldp d10, d11, [x0], #16 + ldp d12, d13, [x0], #16 + ldp d14, d15, [x0] +#endif + + /* Load the return value */ + mov x0, x1 + ret + +botch: + b botch +END(longjmp) diff --git a/repos/base/src/lib/base-common/x86_32/setjmp.S b/repos/base/src/lib/base-common/x86_32/setjmp.S new file mode 100644 index 000000000..22977e5c8 --- /dev/null +++ b/repos/base/src/lib/base-common/x86_32/setjmp.S @@ -0,0 +1,79 @@ +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + .asciz "@(#)setjmp.s 5.1 (Berkeley) 4/23/90" +#endif /* LIBC_SCCS and not lint */ +//#include + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from the last call to + * setjmp(a) + * by restoring registers from the environment 'a'. + * The previous signal state is NOT restored. + */ + +.text; .p2align 2,0x90 +.globl setjmp; .type setjmp,@function; setjmp: + movl 4(%esp),%eax + movl 0(%esp),%edx + movl %edx, 0(%eax) /* rta */ + movl %ebx, 4(%eax) + movl %esp, 8(%eax) + movl %ebp,12(%eax) + movl %esi,16(%eax) + movl %edi,20(%eax) + fnstcw 24(%eax) + xorl %eax,%eax + ret +.size setjmp, . - setjmp + +.text; .p2align 2,0x90 +.globl longjmp; .type longjmp,@function; longjmp: + movl 4(%esp),%edx + movl 8(%esp),%eax + movl 0(%edx),%ecx + movl 4(%edx),%ebx + movl 8(%edx),%esp + movl 12(%edx),%ebp + movl 16(%edx),%esi + movl 20(%edx),%edi + fldcw 24(%edx) + testl %eax,%eax + jnz 1f + incl %eax +1: movl %ecx,0(%esp) + ret +.size longjmp, . - longjmp diff --git a/repos/base/src/lib/base-common/x86_64/setjmp.S b/repos/base/src/lib/base-common/x86_64/setjmp.S new file mode 100644 index 000000000..22cea5d90 --- /dev/null +++ b/repos/base/src/lib/base-common/x86_64/setjmp.S @@ -0,0 +1,93 @@ +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) + .asciz "@(#)setjmp.s 5.1 (Berkeley) 4/23/90" +#endif /* LIBC_SCCS and not lint */ +//#include + +/* + * C library -- setjmp, longjmp + * + * longjmp(a,v) + * will generate a "return(v)" from the last call to + * setjmp(a) + * by restoring registers from the environment 'a'. + * The previous signal state is NOT restored. + */ + +.text; .p2align 4,0x90 +.globl setjmp; .type setjmp,@function; setjmp: + movq %rdi,%rax + movq 0(%rsp),%rdx /* retval */ + movq %rdx, 0(%rax) /* 0; retval */ + movq %rbx, 8(%rax) /* 1; rbx */ + movq %rsp,16(%rax) /* 2; rsp */ + movq %rbp,24(%rax) /* 3; rbp */ + movq %r12,32(%rax) /* 4; r12 */ + movq %r13,40(%rax) /* 5; r13 */ + movq %r14,48(%rax) /* 6; r14 */ + movq %r15,56(%rax) /* 7; r15 */ + fnstcw 64(%rax) /* 8; fpu cw */ + stmxcsr 68(%rax) /* and mxcsr */ + xorq %rax,%rax + ret +.size setjmp, . - setjmp + +.text; .p2align 4,0x90 +.globl longjmp; .type longjmp,@function; longjmp: + movq %rdi,%rdx + /* Restore the mxcsr, but leave exception flags intact. */ + stmxcsr -4(%rsp) + movl 68(%rdx),%eax + andl $0xffffffc0,%eax + movl -4(%rsp),%edi + andl $0x3f,%edi + xorl %eax,%edi + movl %edi,-4(%rsp) + ldmxcsr -4(%rsp) + movq %rsi,%rax /* retval */ + movq 0(%rdx),%rcx + movq 8(%rdx),%rbx + movq 16(%rdx),%rsp + movq 24(%rdx),%rbp + movq 32(%rdx),%r12 + movq 40(%rdx),%r13 + movq 48(%rdx),%r14 + movq 56(%rdx),%r15 + fldcw 64(%rdx) + testq %rax,%rax + jnz 1f + incq %rax +1: movq %rcx,0(%rsp) + ret +.size longjmp, . - longjmp