Mini51 BSP  V3.02.002
The Board Support Package for Mini51 Series
semihosting.h
Go to the documentation of this file.
1 
2 #ifndef ARM_SEMIHOSTING_H_
3 #define ARM_SEMIHOSTING_H_
4 
5 // ----------------------------------------------------------------------------
6 
7 // Semihosting operations.
9 {
10  // Regular operations
35 
36  // Codes returned by SEMIHOSTING_ReportException
37  ADP_Stopped_ApplicationExit = ((2 << 16) + 38),
38  ADP_Stopped_RunTimeError = ((2 << 16) + 35),
39 
40 };
41 
42 // ----------------------------------------------------------------------------
43 
44 // SWI numbers and reason codes for RDI (Angel) monitors.
45 #define AngelSWI_ARM 0x123456
46 #ifdef __thumb__
47 #define AngelSWI 0xAB
48 #else
49 #define AngelSWI AngelSWI_ARM
50 #endif
51 // For thumb only architectures use the BKPT instruction instead of SWI.
52 #if defined(__ARM_ARCH_7M__) \
53  || defined(__ARM_ARCH_7EM__) \
54  || defined(__ARM_ARCH_6M__)
55 #define AngelSWIInsn "bkpt"
56 #define AngelSWIAsm bkpt
57 #else
58 #define AngelSWIInsn "swi"
59 #define AngelSWIAsm swi
60 #endif
61 
62 #if defined(OS_DEBUG_SEMIHOSTING_FAULTS)
63 // Testing the local semihosting handler cannot use another BKPT, since this
64 // configuration cannot trigger HaedFault exceptions while the debugger is
65 // connected, so we use an illegal op code, that will trigger an
66 // UsageFault exception.
67 #define AngelSWITestFault "setend be"
68 #define AngelSWITestFaultOpCode (0xB658)
69 #endif
70 
71 static inline int
72 __attribute__ ((always_inline))
73 call_host (int reason, void* arg)
74 {
75  int value;
76  asm volatile (
77 
78  " mov r0, %[rsn] \n"
79  " mov r1, %[arg] \n"
80 #if defined(OS_DEBUG_SEMIHOSTING_FAULTS)
81  " " AngelSWITestFault " \n"
82 #else
83  " " AngelSWIInsn " %[swi] \n"
84 #endif
85  " mov %[val], r0"
86 
87  : [val] "=r" (value) /* Outputs */
88  : [rsn] "r" (reason), [arg] "r" (arg), [swi] "i" (AngelSWI) /* Inputs */
89  : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc"
90  // Clobbers r0 and r1, and lr if in supervisor mode
91  );
92 
93  // Accordingly to page 13-77 of ARM DUI 0040D other registers
94  // can also be clobbered. Some memory positions may also be
95  // changed by a system call, so they should not be kept in
96  // registers. Note: we are assuming the manual is right and
97  // Angel is respecting the APCS.
98  return value;
99 }
100 
101 // ----------------------------------------------------------------------------
102 
103 // Function used in _exit() to return the status code as Angel exception.
104 static inline void
105 __attribute__ ((always_inline,noreturn))
106 report_exception (int reason)
107 {
108  call_host (SEMIHOSTING_ReportException, (void*) reason);
109 
110  for (;;)
111  ;
112 }
113 
114 // ----------------------------------------------------------------------------
115 
116 #endif // ARM_SEMIHOSTING_H_
static int __attribute__((always_inline)) call_host(int reason
#define AngelSWIInsn
Definition: semihosting.h:58
OperationNumber
Definition: semihosting.h:8
#define AngelSWI
Definition: semihosting.h:49
return value
Definition: semihosting.h:98
static int void * arg
Definition: semihosting.h:74