From 6d77663313dbe484a0447028bed227b2d881eaac Mon Sep 17 00:00:00 2001
From: Ashish Kapania <akapania@ti.com>
Date: Fri, 28 Mar 2014 18:32:04 -0700
Subject: [PATCH] Add support for TIRTOS

---
 ChangeLog                                 |   5 ++
 config.sub                                |   4 +-
 configure.ac                              |   2 +-
 libgloss/ChangeLog                        |   5 ++
 libgloss/arm/configure.in                 |   2 +-
 newlib/ChangeLog                          |  12 ++++
 newlib/configure.host                     |  20 ++++++
 newlib/libc/stdio/local.h                 |   2 +-
 newlib/libc/sys/configure.in              |   1 +
 newlib/libc/sys/tirtos/Makefile.am        |  16 +++++
 newlib/libc/sys/tirtos/configure.in       |  14 ++++
 newlib/libc/sys/tirtos/include/sys/lock.h | 109 ++++++++++++++++++++++++++++++
 newlib/libc/sys/tirtos/lock.c             |  92 +++++++++++++++++++++++++
 13 files changed, 279 insertions(+), 5 deletions(-)
 create mode 100644 newlib/libc/sys/tirtos/Makefile.am
 create mode 100644 newlib/libc/sys/tirtos/configure.in
 create mode 100644 newlib/libc/sys/tirtos/include/sys/lock.h
 create mode 100644 newlib/libc/sys/tirtos/lock.c

diff --git a/ChangeLog b/ChangeLog
index 9585977..20e9668 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2014-03-28  Ashish Kapania  <akapania@ti.com>
+
+	* config.sub: Accept -tirtos*.
+	* configure.ac: (*-*-tirtos*) Add support for TI-RTOS target.
+
 2014-02-04  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
 
 	PR target/59788
diff --git a/config.sub b/config.sub
index 8b612ab..cc49460 100755
--- a/config.sub
+++ b/config.sub
@@ -2,7 +2,7 @@
 # Configuration validation subroutine script.
 #   Copyright 1992-2013 Free Software Foundation, Inc.
 
-timestamp='2013-04-24'
+timestamp='2014-03-28'
 
 # This file is free software; you can redistribute it and/or modify it
 # under the terms of the GNU General Public License as published by
@@ -1374,7 +1374,7 @@ case $os in
 	      | -os2* | -vos* | -palmos* | -uclinux* | -nucleus* \
 	      | -morphos* | -superux* | -rtmk* | -rtmk-nova* | -windiss* \
 	      | -powermax* | -dnix* | -nx6 | -nx7 | -sei* | -dragonfly* \
-	      | -skyos* | -haiku* | -rdos* | -toppers* | -drops* | -es*)
+	      | -skyos* | -haiku* | -rdos* | -toppers* | -drops* | -es* | -tirtos*)
 	# Remember, each alternative MUST END IN *, to match a version number.
 		;;
 	-qnx*)
diff --git a/configure.ac b/configure.ac
index 4c23652..9a2abae 100644
--- a/configure.ac
+++ b/configure.ac
@@ -318,7 +318,7 @@ case "${ENABLE_GOLD}" in
       *-*-elf* | *-*-sysv4* | *-*-unixware* | *-*-eabi* | hppa*64*-*-hpux* \
       | *-*-linux* | *-*-gnu* | frv-*-uclinux* | *-*-irix5* | *-*-irix6* \
       | *-*-netbsd* | *-*-openbsd* | *-*-freebsd* | *-*-dragonfly* \
-      | *-*-solaris2* | *-*-nto* | *-*-nacl*)
+      | *-*-solaris2* | *-*-nto* | *-*-nacl* | *-*-tirtos*)
         case "${target}" in
           *-*-linux*aout* | *-*-linux*oldld*)
             ;;
diff --git a/libgloss/ChangeLog b/libgloss/ChangeLog
index f14432d..631abf1 100644
--- a/libgloss/ChangeLog
+++ b/libgloss/ChangeLog
@@ -1,3 +1,8 @@
+2014-03-28  Ashish Kapania  <akapania@ti.com>
+
+	* arm/configure.in: (*-*-tirtos*) Accept TIRTOS target when setting
+	objtype
+
 2014-03-21  Sabrini Ni  <sabrinanitw@gmail.com>
 
 	* nds32/_sbrk.S: Add .size and .type directive.
diff --git a/libgloss/arm/configure.in b/libgloss/arm/configure.in
index d9408ad..39c15d7 100644
--- a/libgloss/arm/configure.in
+++ b/libgloss/arm/configure.in
@@ -49,7 +49,7 @@ AC_PROG_RANLIB
 LIB_AM_PROG_AS
 
 case "${target}" in
-  *-*-elf | *-*-eabi)
+  *-*-elf | *-*-eabi | *-*-tirtos*)
 	objtype=elf-
 	;;
   *-*-coff)
diff --git a/newlib/ChangeLog b/newlib/ChangeLog
index 84e3956..b32b671 100644
--- a/newlib/ChangeLog
+++ b/newlib/ChangeLog
@@ -1,3 +1,15 @@
+2014-03-28  Ashish Kapania  <akapania@ti.com>
+
+	* configure.host (sys_dir, newlib_cflags): Set sys_dir to tirtos and use
+	-D__DYNAMIC_REENT__ and -DMALLOC_PROVIDED compiler options for TIRTOS
+	target.
+	* libc/stdio/local.h (_STDIO_CLOSE_PER_REENT_STD_STREAMS): Change #ifdef
+	to not define this macro when __tirtos__ is defined.
+	* libc/sys/tirtos : Add support for TIRTOS.
+	* libc/sys/tirtos/Makefile.am, libc/sys/tirtos/lock.c: New files.
+	* libc/sys/tirtos/configure.in, libc/sys/tirtos/config.h: Ditto.
+	* libc/sys/tirtos/include/sys/lock.h: Ditto.
+
 2014-03-27  Richard Earnshaw  <rearnsha@arm.com>
 
 	* libc/machine/arm/acle-compat.h: New file.
diff --git a/newlib/configure.host b/newlib/configure.host
index 58faa95..7dbe20c 100644
--- a/newlib/configure.host
+++ b/newlib/configure.host
@@ -381,6 +381,10 @@ case "${host}" in
 	posix_dir=posix
 	unix_dir=unix
 	;;
+  *-*-tirtos*)
+	sys_dir=tirtos
+	have_crt0="no"
+	;;
   a29k-*-*)
 	sys_dir=a29khif
 	signal_dir=
@@ -577,6 +581,22 @@ case "${host}" in
   *-wrs-vxworks*)
 	newlib_cflags="${newlib_cflags} -DMALLOC_PROVIDED -DMISSING_SYSCALL_NAMES -DHAVE_FCNTL"
 	;;
+# TIRTOS supplies its own version of malloc
+  *-*-tirtos*)
+	newlib_cflags="${newlib_cflags} -D__DYNAMIC_REENT__ -DMALLOC_PROVIDED"
+	syscall_dir=syscalls
+	case "${host_cpu}" in
+      # At the moment TIRTOS supports newlib for ARM only. More CPU
+      # architectures will be supported in the future.
+	  arm*)
+		if [ "x${newlib_may_supply_syscalls}" = "xyes" ] ; then
+		  newlib_cflags="${newlib_cflags} -DARM_RDI_MONITOR"
+		fi
+		;;
+	  *)
+		;;
+	esac
+	;;
 # UDI doesn't have exec, so system() should fail the right way
   a29k-amd-udi)
 	newlib_cflags="${newlib_cflags} -DNO_EXEC"
diff --git a/newlib/libc/stdio/local.h b/newlib/libc/stdio/local.h
index ad47748..24d2136 100644
--- a/newlib/libc/stdio/local.h
+++ b/newlib/libc/stdio/local.h
@@ -38,7 +38,7 @@
    case _STDIO_CLOSE_PER_REENT_STD_STREAMS is defined these file descriptors
    will be closed via close() provided the owner of the reent structure
    triggerd the on demand reent initilization, see CHECK_INIT(). */
-#ifndef __rtems__
+#if !defined(__rtems__) && !defined(__tirtos__)
 #define _STDIO_CLOSE_PER_REENT_STD_STREAMS
 #endif
 
diff --git a/newlib/libc/sys/configure.in b/newlib/libc/sys/configure.in
index c355659..d388bb8 100644
--- a/newlib/libc/sys/configure.in
+++ b/newlib/libc/sys/configure.in
@@ -45,6 +45,7 @@ if test -n "${sys_dir}"; then
 	sysvi386) AC_CONFIG_SUBDIRS(sysvi386) ;;
 	sysvnecv70) AC_CONFIG_SUBDIRS(sysvnecv70) ;;
 	tic80) AC_CONFIG_SUBDIRS(tic80) ;;
+	tirtos) AC_CONFIG_SUBDIRS(tirtos) ;;
 	w65) AC_CONFIG_SUBDIRS(w65) ;;
 	z8ksim) AC_CONFIG_SUBDIRS(z8ksim) ;;
   esac;
diff --git a/newlib/libc/sys/tirtos/Makefile.am b/newlib/libc/sys/tirtos/Makefile.am
new file mode 100644
index 0000000..fc6d1f3
--- /dev/null
+++ b/newlib/libc/sys/tirtos/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 = lock.c 
+lib_a_CCASFLAGS = $(AM_CCASFLAGS)
+lib_a_CFLAGS = $(AM_CFLAGS)
+
+ACLOCAL_AMFLAGS = -I ../../.. -I ../../../..
+CONFIG_STATUS_DEPENDENCIES = $(newlib_basedir)/configure.host
diff --git a/newlib/libc/sys/tirtos/configure.in b/newlib/libc/sys/tirtos/configure.in
new file mode 100644
index 0000000..461301f
--- /dev/null
+++ b/newlib/libc/sys/tirtos/configure.in
@@ -0,0 +1,14 @@
+dnl This is the newlib/libc/sys/tirtos 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([lock.c])
+
+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/tirtos/include/sys/lock.h b/newlib/libc/sys/tirtos/include/sys/lock.h
new file mode 100644
index 0000000..4f2f816
--- /dev/null
+++ b/newlib/libc/sys/tirtos/include/sys/lock.h
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2014, Texas Instruments Incorporated
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * *  Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * *  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.
+ *
+ * *  Neither the name of Texas Instruments Incorporated 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 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 OWNER 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 __SYS_LOCK_H__
+#define __SYS_LOCK_H__
+
+/* 
+ * Lock routines for TI-RTOS multi-threaded apps. The implementation
+ * is provided by TI-RTOS kernel.
+ */
+
+/*
+ * Dummy semaphore structure of size 32 bytes. This will be
+ * type casted to Semaphore_struct in TI-RTOS.
+ */
+typedef struct {
+    void *dummy[8];
+} __dummy_sem_t;
+
+typedef struct {
+    __dummy_sem_t sem;
+    unsigned char init_done;
+} __lock_mutex_t;
+
+typedef struct {
+    void *owner;
+    __dummy_sem_t sem;
+    unsigned int count;
+    unsigned char init_done;
+} __lock_recursive_mutex_t;
+
+typedef __lock_mutex_t _LOCK_T;
+typedef __lock_recursive_mutex_t _LOCK_RECURSIVE_T;
+ 
+#include <_ansi.h>
+
+#define __LOCK_INIT(class,lock) \
+    class _LOCK_T lock = { .sem.dummy = {0, 0, 0, 0, 0, 0, 0, 0}, \
+        .init_done = 0 }
+
+#define __LOCK_INIT_RECURSIVE(class,lock) \
+    class _LOCK_RECURSIVE_T lock = { .owner = 0, \
+        .sem.dummy = {0, 0, 0, 0, 0, 0, 0, 0}, .count = 0, \
+        .init_done = 0 }
+
+extern void __libc_lock_init(_LOCK_T *lock);
+extern void __libc_lock_init_recursive(_LOCK_RECURSIVE_T *lock);
+extern void __libc_lock_close(_LOCK_T *lock);
+extern void __libc_lock_close_recursive(_LOCK_RECURSIVE_T *lock);
+extern void __libc_lock_acquire(_LOCK_T *lock);
+extern void __libc_lock_acquire_recursive(_LOCK_RECURSIVE_T *lock);
+extern void __libc_lock_release(_LOCK_T *lock);
+extern void __libc_lock_release_recursive(_LOCK_RECURSIVE_T *lock);
+
+/* Returns 0 for success and non-zero for failure */
+extern int __libc_lock_try_acquire(_LOCK_T *lock);
+extern int __libc_lock_try_acquire_recursive(_LOCK_RECURSIVE_T *lock);
+
+#define __lock_init(lock) \
+    __libc_lock_init(&(lock))
+#define __lock_init_recursive(lock) \
+    __libc_lock_init_recursive(&(lock))
+#define __lock_close(lock) \
+    __libc_lock_close(&(lock))
+#define __lock_close_recursive(lock) \
+    __libc_lock_close_recursive(&(lock))
+#define __lock_acquire(lock) \
+    __libc_lock_acquire(&(lock))
+#define __lock_acquire_recursive(lock) \
+    __libc_lock_acquire_recursive(&(lock))
+#define __lock_try_acquire(lock) \
+    __libc_lock_try_acquire(&(lock))
+#define __lock_try_acquire_recursive(lock) \
+    __libc_lock_try_acquire_recursive(&(lock))
+#define __lock_release(lock) \
+    __libc_lock_release(&(lock))
+#define __lock_release_recursive(lock) \
+    __libc_lock_release_recursive(&(lock))
+
+#endif /* __SYS_LOCK_H__ */
diff --git a/newlib/libc/sys/tirtos/lock.c b/newlib/libc/sys/tirtos/lock.c
new file mode 100644
index 0000000..afd4d01
--- /dev/null
+++ b/newlib/libc/sys/tirtos/lock.c
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2014, Texas Instruments Incorporated
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * *  Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * *  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.
+ *
+ * *  Neither the name of Texas Instruments Incorporated 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 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 OWNER 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.
+ */
+/*
+ *  ======== lock.c ========
+ *  TIRTOS provides an implementation for the sys/lock APIs if re-entrancy
+ *  support is enabled. If re-entrancy support is disabled or an app is built
+ *  without TIRTOS but using the libraries built for TIRTOS, these empty
+ *  stubs are required to succesfully link the app.
+ */
+
+#include <sys/lock.h>
+
+/* Empty stubs for sys/lock APIs */
+
+void __libc_lock_init(_LOCK_T *lock)
+{
+    return;
+}
+
+void __libc_lock_init_recursive(_LOCK_RECURSIVE_T *lock)
+{
+    return;
+}
+
+void __libc_lock_close(_LOCK_T *lock)
+{
+    return;
+}
+
+void __libc_lock_close_recursive(_LOCK_RECURSIVE_T *lock)
+{
+    return;
+}
+
+void __libc_lock_acquire(_LOCK_T *lock)
+{
+    return;
+}
+
+void __libc_lock_acquire_recursive(_LOCK_RECURSIVE_T *lock)
+{
+    return;
+}
+
+void __libc_lock_release(_LOCK_T *lock)
+{
+    return;
+}
+
+void __libc_lock_release_recursive(_LOCK_RECURSIVE_T *lock)\
+{
+    return;
+}
+
+int __libc_lock_try_acquire(_LOCK_T *lock)
+{
+    return -1;
+}
+
+int __libc_lock_try_acquire_recursive(_LOCK_RECURSIVE_T *lock)
+{
+    return -1;
+}
-- 
1.8.2.2

