µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
semihosting.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2015-2025 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
12#ifndef CMSIS_PLUS_ARM_SEMIHOSTING_H_
13#define CMSIS_PLUS_ARM_SEMIHOSTING_H_
14
15// ----------------------------------------------------------------------------
16
17#if defined(OS_USE_OS_APP_CONFIG_H)
18#include <cmsis-plus/os-app-config.h>
19#endif
20
21// ----------------------------------------------------------------------------
22
23#if defined(__cplusplus)
24extern "C"
25{
26#endif // defined(__cplusplus)
27
28 // --------------------------------------------------------------------------
29
30 // Semihosting operations.
32 {
33 // Regular operations
58
59 // Codes returned by SEMIHOSTING_ReportException
60 ADP_Stopped_ApplicationExit = ((2 << 16) + 38),
61 ADP_Stopped_RunTimeError = ((2 << 16) + 35),
62
63 };
64
65// ----------------------------------------------------------------------------
66
67// SWI numbers and reason codes for RDI (Angel) monitors.
68#define AngelSWI_ARM 0x123456
69#ifdef __thumb__
70#define AngelSWI 0xAB
71#else
72#define AngelSWI AngelSWI_ARM
73#endif
74// For thumb only architectures use the BKPT instruction instead of SWI.
75#if defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__) \
76 || defined(__ARM_ARCH_6M__)
77#define AngelSWIInsn "bkpt"
78#define AngelSWIAsm bkpt
79#else
80#define AngelSWIInsn "swi"
81#define AngelSWIAsm swi
82#endif
83
84#if defined(OS_DEBUG_SEMIHOSTING_FAULTS)
85// Testing the local semihosting handler cannot use another BKPT, since this
86// configuration cannot trigger HaedFault exceptions while the debugger is
87// connected, so we use an illegal op code, that will trigger an
88// UsageFault exception.
89#define AngelSWITestFault "setend be"
90#define AngelSWITestFaultOpCode (0xB658)
91#endif
92
93#pragma GCC diagnostic push
94#pragma GCC diagnostic ignored "-Wunused-parameter"
95
96 static inline int __attribute__ ((always_inline))
97 call_host (int reason, void* arg)
98 {
99 int value;
100 __asm__ volatile (
101
102 " mov r0, %[rsn] \n"
103 " mov r1, %[arg] \n"
104#if defined(OS_DEBUG_SEMIHOSTING_FAULTS)
105 " " AngelSWITestFault " \n"
106#else
107 " " AngelSWIInsn " %[swi] \n"
108#endif
109 " mov %[val], r0"
110
111 : [val] "=r"(value) /* Outputs */
112 : [rsn] "r"(reason), [arg] "r"(arg), [swi] "i"(AngelSWI) /* Inputs */
113 : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc"
114 // Clobbers r0 and r1, and lr if in supervisor mode
115 );
116
117 // Accordingly to page 13-77 of ARM DUI 0040D other registers
118 // can also be clobbered. Some memory positions may also be
119 // changed by a system call, so they should not be kept in
120 // registers. Note: we are assuming the manual is right and
121 // Angel is respecting the APCS.
122 return value;
123 }
124
125#pragma GCC diagnostic pop
126
127 // --------------------------------------------------------------------------
128
129 // Function used in _exit() to return the status code as Angel exception.
130 static inline void __attribute__ ((always_inline, noreturn))
131 report_exception (int reason)
132 {
133 call_host (SEMIHOSTING_ReportException, (void*)reason);
134
135 for (;;)
136 ;
137 }
138
139 // --------------------------------------------------------------------------
140
141#if defined(__cplusplus)
142}
143#endif // defined(__cplusplus)
144
145// ----------------------------------------------------------------------------
146
147#endif /* CMSIS_PLUS_ARM_SEMIHOSTING_H_ */
#define AngelSWIInsn
Definition semihosting.h:80
static void report_exception(int reason)
OperationNumber
Definition semihosting.h:32
@ SEMIHOSTING_SYS_TICKFREQ
Definition semihosting.h:52
@ ADP_Stopped_RunTimeError
Definition semihosting.h:61
@ SEMIHOSTING_ReportException
Definition semihosting.h:35
@ SEMIHOSTING_SYS_ISERROR
Definition semihosting.h:43
@ SEMIHOSTING_EnterSVC
Definition semihosting.h:34
@ SEMIHOSTING_SYS_WRITE
Definition semihosting.h:55
@ SEMIHOSTING_SYS_FLEN
Definition semihosting.h:40
@ SEMIHOSTING_SYS_GET_CMDLINE
Definition semihosting.h:41
@ SEMIHOSTING_SYS_REMOVE
Definition semihosting.h:48
@ SEMIHOSTING_SYS_SEEK
Definition semihosting.h:50
@ SEMIHOSTING_SYS_TMPNAM
Definition semihosting.h:54
@ ADP_Stopped_ApplicationExit
Definition semihosting.h:60
@ SEMIHOSTING_SYS_TIME
Definition semihosting.h:53
@ SEMIHOSTING_SYS_CLOCK
Definition semihosting.h:37
@ SEMIHOSTING_SYS_WRITEC
Definition semihosting.h:56
@ SEMIHOSTING_SYS_ERRNO
Definition semihosting.h:39
@ SEMIHOSTING_SYS_ISTTY
Definition semihosting.h:44
@ SEMIHOSTING_SYS_HEAPINFO
Definition semihosting.h:42
@ SEMIHOSTING_SYS_WRITE0
Definition semihosting.h:57
@ SEMIHOSTING_SYS_ELAPSED
Definition semihosting.h:38
@ SEMIHOSTING_SYS_READ
Definition semihosting.h:46
@ SEMIHOSTING_SYS_SYSTEM
Definition semihosting.h:51
@ SEMIHOSTING_SYS_CLOSE
Definition semihosting.h:36
@ SEMIHOSTING_SYS_READC
Definition semihosting.h:47
@ SEMIHOSTING_SYS_OPEN
Definition semihosting.h:45
@ SEMIHOSTING_SYS_RENAME
Definition semihosting.h:49
#define AngelSWI
Definition semihosting.h:72
static int call_host(int reason, void *arg)
Definition semihosting.h:97