[PATCH v1 4/5] cpukit: Add signal mapping support

Kinsey Moore kinsey.moore at oarcorp.com
Mon Aug 23 23:50:44 UTC 2021


This adds a confdef option allowing an application to request mapping
machine exceptions to POSIX signals. This is required for some languages
such as Ada.
---
 cpukit/doxygen/appl-config.h               | 25 +++++-
 cpukit/include/rtems/confdefs/extensions.h |  7 ++
 cpukit/include/rtems/exception.h           |  8 ++
 cpukit/score/src/exceptionmapping.c        | 93 ++++++++++++++++++++++
 spec/build/cpukit/librtemscpu.yml          |  2 +
 spec/build/cpukit/objexceptionmapping.yml  | 15 ++++
 6 files changed, 149 insertions(+), 1 deletion(-)
 create mode 100644 cpukit/score/src/exceptionmapping.c
 create mode 100644 spec/build/cpukit/objexceptionmapping.yml

diff --git a/cpukit/doxygen/appl-config.h b/cpukit/doxygen/appl-config.h
index bbeb438bec..8ad3a3c70e 100644
--- a/cpukit/doxygen/appl-config.h
+++ b/cpukit/doxygen/appl-config.h
@@ -3,7 +3,7 @@
 /*
  * Copyright (C) 2019, 2021 embedded brains GmbH (http://www.embedded-brains.de)
  * Copyright (C) 2010 Gedare Bloom
- * Copyright (C) 1988, 2008 On-Line Applications Research Corporation (OAR)
+ * Copyright (C) 1988, 2021 On-Line Applications Research Corporation (OAR)
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -1773,6 +1773,29 @@
  */
 #define CONFIGURE_ATA_DRIVER_TASK_PRIORITY
 
+/* Generated from spec:/acfg/if/exception-to-signal-mapping */
+
+/**
+ * @brief This configuration option is a boolean feature define.
+ *
+ * In case this configuration option is defined, then the machine exception to
+ * POSIX signal mapping is configured during system initialization.
+ *
+ * @par Default Configuration
+ * If this configuration option is undefined, then the described feature is not
+ * enabled.
+ *
+ * @par Notes
+ * @parblock
+ * This device driver is responsible for setting up a mapping from machine
+ * exceptions to POSIX signals so that applications may consume them and alter
+ * task execution as necessary.
+ *
+ * This is especially useful for applications written in Ada or C++.
+ * @endparblock
+ */
+#define CONFIGURE_EXCEPTION_TO_SIGNAL_MAPPING
+
 /* Generated from spec:/acfg/if/max-drivers */
 
 /**
diff --git a/cpukit/include/rtems/confdefs/extensions.h b/cpukit/include/rtems/confdefs/extensions.h
index 83d690d50a..e7185aa9d1 100644
--- a/cpukit/include/rtems/confdefs/extensions.h
+++ b/cpukit/include/rtems/confdefs/extensions.h
@@ -93,6 +93,10 @@
   #include <rtems/stackchk.h>
 #endif
 
+#ifdef CONFIGURE_EXCEPTION_TO_SIGNAL_MAPPING
+  #include <rtems/exception.h>
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -139,6 +143,9 @@ extern "C" {
     #ifdef CONFIGURE_INITIAL_EXTENSIONS
       CONFIGURE_INITIAL_EXTENSIONS,
     #endif
+    #ifdef CONFIGURE_EXCEPTION_TO_SIGNAL_MAPPING
+      { .exception = _Exception_map_signal },
+    #endif
     #ifdef BSP_INITIAL_EXTENSION
       BSP_INITIAL_EXTENSION
     #endif
diff --git a/cpukit/include/rtems/exception.h b/cpukit/include/rtems/exception.h
index dea1712409..5bf11127e7 100644
--- a/cpukit/include/rtems/exception.h
+++ b/cpukit/include/rtems/exception.h
@@ -152,6 +152,14 @@ void Exception_Manager_Copy_CPU_Exception_frame(
   CPU_Exception_frame *old_ef
 );
 
+/**
+ * @brief Handle an exception frame for mapping signals
+ *
+ * See CONFIGURE_EXCEPTION_TO_SIGNAL_MAPPING documentation in the
+ * "RTEMS Classic API Guide".
+ */
+void _Exception_map_signal( CPU_Exception_frame *ef, bool *handled );
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/cpukit/score/src/exceptionmapping.c b/cpukit/score/src/exceptionmapping.c
new file mode 100644
index 0000000000..ef88feadfe
--- /dev/null
+++ b/cpukit/score/src/exceptionmapping.c
@@ -0,0 +1,93 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+/**
+ * @file
+ *
+ * @ingroup RTEMSScoreExceptionMapping
+ *
+ * @brief AArch64 machine exception to POSIX signal mapping.
+ */
+
+/*
+ * Copyright (C) 2021 On-Line Applications Research Corporation (OAR)
+ * Written by Kinsey Moore <kinsey.moore at oarcorp.com>
+ *
+ * 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 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.
+ */
+
+#include <pthread.h>
+#include <signal.h>
+#include <rtems/exception.h>
+
+/*
+ * Exception handler. Map the exception class to SIGFPE, SIGSEGV
+ * or SIGILL for Ada or other runtimes.
+ */
+void _Exception_map_signal( CPU_Exception_frame *ef, bool *handled )
+{
+  /* Don't consume exceptions that have already been handled */
+  if ( *handled ) {
+    return;
+  }
+
+  uint32_t         signal;
+  Exception_Class eClass = Exception_Manager_Get_class( ef );
+
+  switch ( eClass ) {
+    /* Don't handle exceptions other handlers are likely to be interested in */
+    case EXCEPTION_SUPERVISOR:
+    case EXCEPTION_TRAPPED_INSTRUCTION:
+    case EXCEPTION_BREAKPOINT:
+    case EXCEPTION_BREAK_INSTRUCTION:
+    case EXCEPTION_STEP:
+    case EXCEPTION_WATCHPOINT:
+      return;
+
+    case EXCEPTION_FPU:
+    case EXCEPTION_TAGGED_OVERFLOW:
+    case EXCEPTION_DIV_ZERO:
+      signal = SIGFPE;
+      break;
+
+    case EXCEPTION_DATA_ABORT_READ:
+    case EXCEPTION_DATA_ABORT_WRITE:
+    case EXCEPTION_DATA_ABORT_UNSPECIFIED:
+    case EXCEPTION_INSTRUCTION_ABORT:
+    case EXCEPTION_MMU_UNSPECIFIED:
+    case EXCEPTION_ACCESS_ALIGNMENT:
+      signal = SIGSEGV;
+      break;
+
+    default:
+      /*
+       * Covers unknown, PC/SP alignment, illegal execution state, and any new
+       * exception classes that get added.
+       */
+      signal = SIGILL;
+      break;
+  }
+
+  /* ISRs must be enabled during kill() since thread dispatch can occur. */
+  pthread_kill( pthread_self(), signal );
+
+  *handled = true;
+}
diff --git a/spec/build/cpukit/librtemscpu.yml b/spec/build/cpukit/librtemscpu.yml
index e69b67f368..e614a2b80c 100644
--- a/spec/build/cpukit/librtemscpu.yml
+++ b/spec/build/cpukit/librtemscpu.yml
@@ -496,6 +496,8 @@ links:
   uid: objdl
 - role: build-dependency
   uid: objdrvmgr
+- role: build-dependency
+  uid: objexceptionmapping
 - role: build-dependency
   uid: objgnat
 - role: build-dependency
diff --git a/spec/build/cpukit/objexceptionmapping.yml b/spec/build/cpukit/objexceptionmapping.yml
new file mode 100644
index 0000000000..028cc237bd
--- /dev/null
+++ b/spec/build/cpukit/objexceptionmapping.yml
@@ -0,0 +1,15 @@
+SPDX-License-Identifier: CC-BY-SA-4.0 OR BSD-2-Clause
+build-type: objects
+cflags: []
+copyrights:
+- Copyright (C) 2021 On-Line Applications Research (OAR)
+cppflags: []
+cxxflags: []
+enabled-by:
+- RTEMS_EXCEPTION_MANAGER
+includes: []
+install: []
+links: []
+source:
+- cpukit/score/src/exceptionmapping.c
+type: build
-- 
2.20.1



More information about the devel mailing list