semihosting.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (C) 2013 Uwe Bonnes
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH
  21. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /* @file dev/semihost.c
  34. * @brief Virtual console using ARM Semihosting
  35. *
  36. * Parts of the code taken from
  37. * gcc-arm-none-eabi-4_7-2013q2-20130614/src/newlib/libgloss/arm
  38. * syscalls.c and swi.h.
  39. *
  40. * Program will only run with semihosting capable debugger attached!
  41. *
  42. * Use like:
  43. * E.g. with stm32 L1Discovery:
  44. * Start debugger like: openocd -f board/stm32ldiscovery.cfg
  45. * Start openocd controlling telnet session: telnet localhost 4444
  46. * In telnet session, enable semihosting: "arm semihosting enable"
  47. * Start debug session : arm-none-eabi-gdb uart.elf
  48. * Attach debugger to Openocd: tar ext :3333
  49. * Load programm in debugger: load
  50. * Run program in debugger: r
  51. * Watch console output in terminal where openocd was started.
  52. */
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <sys/file.h>
  56. #include <sys/timer.h>
  57. #include <sys/device.h>
  58. struct semihosting_handles {
  59. int monitor_stdin;
  60. int monitor_stdout;
  61. int monitor_stderr;
  62. };
  63. static struct semihosting_handles sh;
  64. /*!
  65. * \brief Local Semihosting information.
  66. */
  67. /*!
  68. * \addtogroup xgDevice
  69. */
  70. /*@{*/
  71. #define AngelSWI_ARM 0x123456
  72. #ifdef __thumb__
  73. #define AngelSWI 0xAB
  74. #else
  75. #define AngelSWI AngelSWI_ARM
  76. #endif
  77. /* For thumb only architectures use the BKPT instruction instead of SWI. */
  78. #ifdef THUMB_V7M_V6M
  79. #define AngelSWIInsn "bkpt"
  80. #define AngelSWIAsm bkpt
  81. #else
  82. #define AngelSWIInsn "swi"
  83. #define AngelSWIAsm swi
  84. #endif
  85. /* The reason codes: */
  86. #define AngelSWI_Reason_Open 0x01
  87. #define AngelSWI_Reason_Close 0x02
  88. #define AngelSWI_Reason_WriteC 0x03
  89. #define AngelSWI_Reason_Write0 0x04
  90. #define AngelSWI_Reason_Write 0x05
  91. #define AngelSWI_Reason_Read 0x06
  92. #define AngelSWI_Reason_ReadC 0x07
  93. #define AngelSWI_Reason_IsTTY 0x09
  94. #define AngelSWI_Reason_Seek 0x0A
  95. #define AngelSWI_Reason_FLen 0x0C
  96. #define AngelSWI_Reason_TmpNam 0x0D
  97. #define AngelSWI_Reason_Remove 0x0E
  98. #define AngelSWI_Reason_Rename 0x0F
  99. #define AngelSWI_Reason_Clock 0x10
  100. #define AngelSWI_Reason_Time 0x11
  101. #define AngelSWI_Reason_System 0x12
  102. #define AngelSWI_Reason_Errno 0x13
  103. #define AngelSWI_Reason_GetCmdLine 0x15
  104. #define AngelSWI_Reason_HeapInfo 0x16
  105. #define AngelSWI_Reason_EnterSVC 0x17
  106. #define AngelSWI_Reason_ReportException 0x18
  107. #define ADP_Stopped_ApplicationExit ((2 << 16) + 38)
  108. #define ADP_Stopped_RunTimeError ((2 << 16) + 35)
  109. static inline int
  110. do_AngelSWI (int reason, void *arg)
  111. {
  112. int value;
  113. asm volatile ("mov r0, %1; mov r1, %2; " AngelSWIInsn " %a3; mov %0, r0"
  114. : "=r" (value) /* Outputs */
  115. : "r" (reason), "r" (arg), "i" (AngelSWI) /* Inputs */
  116. : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc"
  117. /* Clobbers r0 and r1, and lr if in supervisor mode */);
  118. /* Accordingly to page 13-77 of ARM DUI 0040D other registers
  119. can also be clobbered. Some memory positions may also be
  120. changed by a system call, so they should not be kept in
  121. registers. Note: we are assuming the manual is right and
  122. Angel is respecting the APCS. */
  123. return value;
  124. }
  125. static int SemihostingInit(NUTDEVICE * dev)
  126. {
  127. int block[3];
  128. struct semihosting_handles *sh = (struct semihosting_handles *)dev->dev_icb;
  129. block[0] = (int) ":tt";
  130. block[2] = 3; /* length of filename */
  131. block[1] = 0; /* mode "r" */
  132. sh->monitor_stdin = do_AngelSWI (AngelSWI_Reason_Open, (void *) block);
  133. block[0] = (int) ":tt";
  134. block[2] = 3; /* length of filename */
  135. block[1] = 4; /* mode "w" */
  136. sh->monitor_stdout = do_AngelSWI (AngelSWI_Reason_Open, (void *) block);
  137. block[0] = (int) ":tt";
  138. block[2] = 3; /* length of filename */
  139. block[1] = 8; /* mode "a" */
  140. sh->monitor_stderr = do_AngelSWI (AngelSWI_Reason_Open, (void *) block);
  141. if (sh->monitor_stderr == -1)
  142. sh->monitor_stderr = sh->monitor_stdout;
  143. return 0;
  144. }
  145. /*!
  146. * \brief Open UnixDev
  147. *
  148. * \return Pointer to a static NUTFILE structure.
  149. */
  150. static NUTFILE *SemihostingOpen(NUTDEVICE * dev, const char *name, int mode, int acc)
  151. {
  152. NUTFILE *nf;
  153. nf = malloc(sizeof(NUTFILE));
  154. if (nf) {
  155. nf->nf_dev = dev;
  156. }
  157. return nf;
  158. }
  159. /*!
  160. * \brief Blocking write bytes to file
  161. *
  162. * \return Number of characters sent.
  163. */
  164. static int SemihostingWrite(NUTFILE * nf, const void *buffer, int len)
  165. {
  166. struct semihosting_handles *sh = (struct semihosting_handles *)nf->nf_dev->dev_icb;
  167. int block[3];
  168. block[0] = sh->monitor_stdout;
  169. block[1] = (int) buffer;
  170. block[2] = len;
  171. do_AngelSWI (AngelSWI_Reason_Write, (void *) block);
  172. return len;
  173. }
  174. /*!
  175. * \brief Blocking write bytes to file
  176. *
  177. * \return Number of characters sent.
  178. */
  179. static int SemihostingRead(NUTFILE * nf, void *buffer, int len)
  180. {
  181. struct semihosting_handles *sh = (struct semihosting_handles *)nf->nf_dev->dev_icb;
  182. int block[3];
  183. int res = -1;
  184. block[0] = sh->monitor_stdin;
  185. block[1] = (int) buffer;
  186. block[2] = len;
  187. while (res < 0)
  188. res = do_AngelSWI (AngelSWI_Reason_Read, (void *) block);
  189. return len;
  190. }
  191. #ifdef __HARVARD_ARCH__
  192. static int SemihostingWriteP(NUTFILE * nf, PGM_P buffer, int len)
  193. {
  194. return len;
  195. }
  196. #endif
  197. /*!
  198. * \brief Close ...
  199. *
  200. * \return Always 0.
  201. */
  202. static int SemihostingClose(NUTFILE * nf)
  203. {
  204. if (nf) {
  205. free (nf);
  206. }
  207. return 0;
  208. }
  209. /*!
  210. * \brief Perform control functions.
  211. *
  212. * This function is called by the ioctl() function of the C runtime
  213. * library.
  214. *
  215. * \param dev Identifies the device that receives the device-control
  216. * function.
  217. * \param req Requested control function. We do return ok for any function
  218. * \param conf Points to a buffer that contains any data required for
  219. * the given control function or receives data from that
  220. * function.
  221. * \return 0 on success, -1 otherwise.
  222. *
  223. */
  224. int SemihostingIOCTL(NUTDEVICE * dev, int req, void *conf)
  225. {
  226. return 0;
  227. }
  228. /* ======================= Devices ======================== */
  229. /*!
  230. * \brief Null device information structure.
  231. */
  232. NUTDEVICE devSemihosting = {
  233. 0, /*!< Pointer to next device. */
  234. {'S', 'e', 'm', 'i', 'h', 'o', 's', 't', 0}, /*!< Unique device name. */
  235. IFTYP_CHAR, /*!< Type of device. */
  236. 0, /*!< Base address. */
  237. 0, /*!< First interrupt number. */
  238. &sh, /*!< Interface control block. */
  239. 0, /*!< Driver control block. */
  240. SemihostingInit, /*!< Driver initialization routine. */
  241. SemihostingIOCTL, /*!< Driver specific control function. */
  242. SemihostingRead,
  243. SemihostingWrite,
  244. #ifdef __HARVARD_ARCH__
  245. SemihostingWriteP,
  246. #endif
  247. SemihostingOpen,
  248. SemihostingClose,
  249. 0, /*!< Return file size, dev_size. */
  250. 0, /*!< Select function, optional, not yet implemented */
  251. };
  252. /*@}*/