This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH, OR1K] Baremetal system for OpenRISC


On 11.12.2014 18:02, Stefan Wallentowitz wrote:
> I am really sorry, but somehow the aclocal file made its way into this
> patch.
>
> This is the correct patch.
Hi all,

can you please comment on this patch also? It is a rather important part
for the or1k newlib port..

Thanks a lot,
Stefan
commit 51c4e42fffe5265f90d624e5ed838dc7a5fa3c7f
Author: Stefan Wallentowitz <stefan.wallentowitz@tum.de>
Date:   Thu Dec 11 17:59:47 2014 +0100

    OR1K: Baremetal system support
    
    This adds the OpenRISC baremetal as a system (or1k-elf), that depends
    on libgloss (mainly reentrancy) and thereby keeps other toolchains
    like or1k-rtems intact.
    
    newlib/Changelog:
    	* configure.host: Add extra system for OpenRISC baremetal
    	* libc/include/sys/config.h: Dynamic reentrancy for or1k sys targets
    	* libc/sys/or1k/: New system for or1k baremetal
    	* libc/sys/or1k/Makefile.am: New file
    	* libc/sys/or1k/configure.in: New file
    	* libc/sys/or1k/getreent.S: New file
    	* libc/sys/or1k/mlock.S: New file
    	* libc/sys/or1k/or1k-asm.S: New file

diff --git a/newlib/configure.host b/newlib/configure.host
index d405d22..e218242 100644
--- a/newlib/configure.host
+++ b/newlib/configure.host
@@ -502,8 +502,10 @@ case "${host}" in
   nios2*)
 	sys_dir=
 	;;
-  or1k-*-* | or1knd-*-*)
-        syscall_dir=syscalls
+  or1k-*-elf | or1knd-*-elf)
+	sys_dir=or1k
+	newlib_cflags="${newlib_cflags} -DREENTRANT_SYSCALLS_PROVIDED "
+	have_crt0="no"
 	;;
   powerpcle-*-pe)
 	posix_dir=posix
@@ -748,6 +750,9 @@ case "${host}" in
   nds32*)
 	syscall_dir=syscalls
 	;;
+  or1k*|or1knd*)
+	syscall_dir=syscalls
+	;;
   powerpc*-*-eabialtivec*)
 	default_newlib_io_long_long="yes"
 	newlib_cflags="${newlib_cflags} -DMISSING_SYSCALL_NAMES"
diff --git a/newlib/libc/include/sys/config.h b/newlib/libc/include/sys/config.h
index 0d27d6c..5297bef 100644
--- a/newlib/libc/include/sys/config.h
+++ b/newlib/libc/include/sys/config.h
@@ -185,6 +185,10 @@
 #define __CUSTOM_FILE_IO__
 #endif
 
+#if defined(__or1k__) || defined(__or1knd__)
+#define __DYNAMIC_REENT__
+#endif
+
 /* This block should be kept in sync with GCC's limits.h.  The point
    of having these definitions here is to not include limits.h, which
    would pollute the user namespace, while still using types of the
diff --git a/newlib/libc/sys/configure.in b/newlib/libc/sys/configure.in
index d388bb8..4c6266d 100644
--- a/newlib/libc/sys/configure.in
+++ b/newlib/libc/sys/configure.in
@@ -34,6 +34,7 @@ if test -n "${sys_dir}"; then
 	m88kbug) AC_CONFIG_SUBDIRS(m88kbug) ;;
 	mmixware) AC_CONFIG_SUBDIRS(mmixware) ;;
 	netware) AC_CONFIG_SUBDIRS(netware) ;;
+	or1k) AC_CONFIG_SUBDIRS(or1k) ;;
 	rdos) AC_CONFIG_SUBDIRS(rdos) ;;
 	rtems) AC_CONFIG_SUBDIRS(rtems) ;;
 	sh) AC_CONFIG_SUBDIRS(sh) ;;
diff --git a/newlib/libc/sys/or1k/Makefile.am b/newlib/libc/sys/or1k/Makefile.am
new file mode 100644
index 0000000..855941c
--- /dev/null
+++ b/newlib/libc/sys/or1k/Makefile.am
@@ -0,0 +1,16 @@
+## Process this file with automake to generate Makefile.in
+
+AUTOMAKE_OPTIONS = cygnus
+
+INCLUDES = $(NEWLIB_CFLAGS) $(CROSS_CFLAGS) $(TARGET_CFLAGS)
+
+AM_CCASFLAGS = $(INCLUDES)
+
+noinst_LIBRARIES = lib.a
+
+lib_a_SOURCES = getreent.S mlock.c
+lib_a_CCASFLAGS=$(AM_CCASFLAGS)
+lib_a_CFLAGS=$(AM_CFLAGS)
+
+ACLOCAL_AMFLAGS = -I ../../.. -I ../../../..
+CONFIG_STATUS_DEPENDENCIES = $(newlib_basedir)/configure.host
\ No newline at end of file
diff --git a/newlib/libc/sys/or1k/configure.in b/newlib/libc/sys/or1k/configure.in
new file mode 100644
index 0000000..c855a8d
--- /dev/null
+++ b/newlib/libc/sys/or1k/configure.in
@@ -0,0 +1,14 @@
+dnl This is the newlib/libc/sys/arm configure.in file.
+dnl Process this file with autoconf to produce a configure script.
+
+AC_PREREQ(2.59)
+AC_INIT([newlib],[NEWLIB_VERSION])
+AC_CONFIG_SRCDIR([getreent.S])
+
+dnl Can't be done in NEWLIB_CONFIGURE because that confuses automake. 
+AC_CONFIG_AUX_DIR(../../../..)
+
+NEWLIB_CONFIGURE(../../..)
+
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
diff --git a/newlib/libc/sys/or1k/getreent.S b/newlib/libc/sys/or1k/getreent.S
new file mode 100644
index 0000000..4bc50c8
--- /dev/null
+++ b/newlib/libc/sys/or1k/getreent.S
@@ -0,0 +1,26 @@
+/* getreent.S. Return reentrancy pointer.
+
+   Copyright (C) 2014, Authors
+
+   Contributor Stefan Wallentowitz <stefan.wallentowitz@tum.de>
+
+ * The authors hereby grant permission to use, copy, modify, distribute,
+ * and license this software and its documentation for any purpose, provided
+ * that existing copyright notices are retained in all copies and that this
+ * notice is included verbatim in any distributions. No written agreement,
+ * license, or royalty fee is required for any of the authorized uses.
+ * Modifications to this software may be copyrighted by their authors
+ * and need not follow the licensing terms described here, provided that
+ * the new terms are clearly indicated on the first page of each file where
+ * they apply.
+ */
+
+#include "or1k-asm.h"
+
+	.extern	_or1k_libc_getreent
+
+	.global	__getreent
+	.type	__getreent,@function
+
+__getreent:
+	OR1K_DELAYED_NOP(OR1K_INST(l.j _or1k_libc_getreent))
diff --git a/newlib/libc/sys/or1k/mlock.c b/newlib/libc/sys/or1k/mlock.c
new file mode 100644
index 0000000..660c3f0
--- /dev/null
+++ b/newlib/libc/sys/or1k/mlock.c
@@ -0,0 +1,90 @@
+/* malloc-lock.c. Lock malloc.
+ *
+ * Copyright (C) 2014, Authors
+ *
+ * Contributor Stefan Wallentowitz <stefan.wallentowitz@tum.de>
+ *
+ * The authors hereby grant permission to use, copy, modify, distribute,
+ * and license this software and its documentation for any purpose, provided
+ * that existing copyright notices are retained in all copies and that this
+ * notice is included verbatim in any distributions. No written agreement,
+ * license, or royalty fee is required for any of the authorized uses.
+ * Modifications to this software may be copyrighted by their authors
+ * and need not follow the licensing terms described here, provided that
+ * the new terms are clearly indicated on the first page of each file where
+ * they apply.
+ */
+
+#include <reent.h>
+#include <or1k-support.h>
+
+/* Lock calls from different cores, but allows recursive calls from the same
+ * core. The lock is not only atomic to other cores calling malloc, but also
+ * disables all external interrupts. This is necessary as it could otherwise
+ * lead to a deadlock to interrupt while in malloc and then call it from an
+ * exception. But as we want the exceptions to be flexible to use all library
+ * calls and especially memory management this is necessary.
+ */
+
+// The lock. It is zero when unlocked and contains a unique value for each core.
+// This value is not the core id (to avoid id zero), but the pointer value of
+// the core specific struct _reent.
+volatile uint32_t _or1k_malloc_lock;
+
+// Count how often the current holder has entered the lock
+volatile uint32_t _or1k_malloc_lock_cnt;
+// The exception enable restore of the current mutex holder
+volatile uint32_t _or1k_malloc_lock_restore;
+
+/**
+ * Recursive lock of the malloc
+ */
+void __malloc_lock(struct _reent *ptr) {
+	uint32_t restore;
+	uint32_t id;
+
+	// Each core is identified by its struct _reent pointer
+	id = (uint32_t) ptr;
+
+	// Disable timer and interrupt exception, save TEE and IEE flag
+	// temporarily to restore them later on unlock
+	restore = or1k_critical_begin();
+
+	// We cannot be disturbed by an interrupt or timer exception from here
+
+	// Check if we currently don't hold the lock
+	if (_or1k_malloc_lock != id) {
+		do {
+			// Repeatedly check the lock until it is set to zero
+			while (_or1k_malloc_lock != 0) {}
+			// .. and then try to set it atomically. As this may
+			// fail, we need to repeat this
+		} while (or1k_sync_cas(&_or1k_malloc_lock, 0, id) != 0);
+	}
+
+	// Store the TEE and IEE flags for later restore
+	_or1k_malloc_lock_restore = restore;
+
+	// Increment counter. The lock may be accessed recursively
+	_or1k_malloc_lock_cnt++;
+
+	return;
+}
+
+void __malloc_unlock(struct _reent *ptr) {
+	// Decrement counter. The lock may be unlocked recursively
+	_or1k_malloc_lock_cnt--;
+
+	// If this was the last recursive unlock call
+	if(_or1k_malloc_lock_cnt == 0){
+		// We need to temporarily store the value to avoid a race
+		// condition between unlocking and reading restore
+		uint32_t restore = _or1k_malloc_lock_restore;
+		// unset lock
+		_or1k_malloc_lock = 0;
+		// Restore flags
+		or1k_critical_end(_or1k_malloc_lock_restore);
+	}
+
+	return;
+}
diff --git a/newlib/libc/sys/or1k/or1k-asm.h b/newlib/libc/sys/or1k/or1k-asm.h
new file mode 100644
index 0000000..c218c64
--- /dev/null
+++ b/newlib/libc/sys/or1k/or1k-asm.h
@@ -0,0 +1,83 @@
+/* or1k-asm.h -- OR1K assembly helper macros
+
+   Copyright (c) 2014 OpenRISC Project Maintainers
+   Copyright (C) 2012-2014 Peter Gavin <pgavin@gmail.com>
+   All rights reserved.
+
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following condition
+   is met:
+
+   1. Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
+   COPYRIGHT HOLDER 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.
+
+   */
+
+#ifndef OR1K_ASM_H
+#define OR1K_ASM_H
+
+/* The purpose of the OR1K_INST macro is simply to protect the commas
+   embedded within an instruction from the C preprocessor.  An entire
+   instruction can be safely embedded within its arguments, including
+   an arbitrary number of commas, and it will be reproduced
+   exactly. */
+#define OR1K_INST(...) __VA_ARGS__
+
+/* OR1K_DELAYED takes two arguments which must be instructions.  They
+   should be wrapped in OR1K_INST if the instructions require commas.
+   The second argument should be a jump or branch instruction.  If we
+   are assembling the code in delay-slot mode (e.g., for the standard
+   OR1K) the first instruction will be emitted in the delay slot of
+   the second instruction.  In no-delay-slot mode they will be emitted
+   in order.  If we are using compat-delay mode, they will be emitted
+   in order, but an l.nop instruction will be emitted immediately
+   after. */
+
+/* OR1K_DELAYED_NOP takes a single argument, which should be a
+   branch/jump instruction.  In delay-slot or compat-delay modes, the
+   instruction will be emitted with an l.nop in its delay slot. In
+   no-delay mode, the instruction will be emitted by itself. */
+
+#if defined(__OR1K_NODELAY__)
+
+#define OR1K_DELAYED(a, b) a; b
+#define OR1K_DELAYED_NOP(a) a
+
+/* Go ahead and emit the .nodelay directive when in no-delay mode, so
+   that the flags are appropriately set in the binary. */
+.nodelay
+
+#elif defined(__OR1K_DELAY__)
+
+#define OR1K_DELAYED(a, b) b; a
+#define OR1K_DELAYED_NOP(a) a; l.nop
+
+#elif defined(__OR1K_DELAY_COMPAT__)
+
+#define OR1K_DELAYED(a, b) a; b; l.nop
+#define OR1K_DELAYED_NOP(a) a; l.nop
+
+#else
+
+#error One of __OR1K_NODELAY__, __OR1K_DELAY__, or __OR1K_DELAY_COMPAT__ must be defined
+
+#endif
+
+#define LOAD_SYMBOL_2_GPR(gpr,symbol)  \
+	.global symbol ;               \
+	l.movhi gpr, hi(symbol) ;      \
+	l.ori   gpr, gpr, lo(symbol)
+#endif

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]