1 /***************************************************************************
2 * Copyright (C) 2009 by Marvell Technology Group Ltd. *
3 * Written by Nicolas Pitre <nico@marvell.com> *
5 * Copyright (C) 2010 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
26 * Hold ARM semihosting support.
28 * Semihosting enables code running on an ARM target to use the I/O
29 * facilities on the host computer. The target application must be linked
30 * against a library that forwards operation requests by using the SVC
31 * instruction trapped at the Supervisor Call vector by the debugger.
32 * Details can be found in chapter 8 of DUI0203I_rvct_developer_guide.pdf
43 #include "arm_semihosting.h"
44 #include <helper/binarybuffer.h>
45 #include <helper/log.h>
48 static int open_modeflags
[12] = {
53 O_WRONLY
| O_CREAT
| O_TRUNC
,
54 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
55 O_RDWR
| O_CREAT
| O_TRUNC
,
56 O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
,
57 O_WRONLY
| O_CREAT
| O_APPEND
,
58 O_WRONLY
| O_CREAT
| O_APPEND
| O_BINARY
,
59 O_RDWR
| O_CREAT
| O_APPEND
,
60 O_RDWR
| O_CREAT
| O_APPEND
| O_BINARY
63 static int do_semihosting(struct target
*target
)
65 struct arm
*armv4_5
= target_to_arm(target
);
66 uint32_t r0
= buf_get_u32(armv4_5
->core_cache
->reg_list
[0].value
, 0, 32);
67 uint32_t r1
= buf_get_u32(armv4_5
->core_cache
->reg_list
[1].value
, 0, 32);
68 uint32_t lr
= buf_get_u32(ARMV4_5_CORE_REG_MODE(armv4_5
->core_cache
, ARM_MODE_SVC
, 14).value
, 0, 32);
69 uint32_t spsr
= buf_get_u32(armv4_5
->spsr
->value
, 0, 32);;
74 * TODO: lots of security issues are not considered yet, such as:
75 * - no validation on target provided file descriptors
76 * - no safety checks on opened/deleted/renamed file paths
77 * Beware the target app you use this support with.
79 * TODO: explore mapping requests to GDB's "File-I/O Remote
80 * Protocol Extension" ... when GDB is active.
83 case 0x01: /* SYS_OPEN */
84 retval
= target_read_memory(target
, r1
, 4, 3, params
);
85 if (retval
!= ERROR_OK
)
88 uint32_t a
= target_buffer_get_u32(target
, params
+0);
89 uint32_t m
= target_buffer_get_u32(target
, params
+4);
90 uint32_t l
= target_buffer_get_u32(target
, params
+8);
91 if (l
<= 255 && m
<= 11) {
93 retval
= target_read_memory(target
, a
, 1, l
, fn
);
94 if (retval
!= ERROR_OK
)
97 if (strcmp((char *)fn
, ":tt") == 0) {
99 result
= dup(STDIN_FILENO
);
101 result
= dup(STDOUT_FILENO
);
103 /* cygwin requires the permission setting
104 * otherwise it will fail to reopen a previously
106 result
= open((char *)fn
, open_modeflags
[m
], 0644);
108 armv4_5
->semihosting_errno
= errno
;
111 armv4_5
->semihosting_errno
= EINVAL
;
116 case 0x02: /* SYS_CLOSE */
117 retval
= target_read_memory(target
, r1
, 4, 1, params
);
118 if (retval
!= ERROR_OK
)
121 int fd
= target_buffer_get_u32(target
, params
+0);
123 armv4_5
->semihosting_errno
= errno
;
127 case 0x03: /* SYS_WRITEC */
130 retval
= target_read_memory(target
, r1
, 1, 1, &c
);
131 if (retval
!= ERROR_OK
)
138 case 0x04: /* SYS_WRITE0 */
141 retval
= target_read_memory(target
, r1
, 1, 1, &c
);
142 if (retval
!= ERROR_OK
)
151 case 0x05: /* SYS_WRITE */
152 retval
= target_read_memory(target
, r1
, 4, 3, params
);
153 if (retval
!= ERROR_OK
)
156 int fd
= target_buffer_get_u32(target
, params
+0);
157 uint32_t a
= target_buffer_get_u32(target
, params
+4);
158 size_t l
= target_buffer_get_u32(target
, params
+8);
159 uint8_t *buf
= malloc(l
);
162 armv4_5
->semihosting_errno
= ENOMEM
;
164 retval
= target_read_buffer(target
, a
, l
, buf
);
165 if (retval
!= ERROR_OK
) {
169 result
= write(fd
, buf
, l
);
170 armv4_5
->semihosting_errno
= errno
;
178 case 0x06: /* SYS_READ */
179 retval
= target_read_memory(target
, r1
, 4, 3, params
);
180 if (retval
!= ERROR_OK
)
183 int fd
= target_buffer_get_u32(target
, params
+0);
184 uint32_t a
= target_buffer_get_u32(target
, params
+4);
185 ssize_t l
= target_buffer_get_u32(target
, params
+8);
186 uint8_t *buf
= malloc(l
);
189 armv4_5
->semihosting_errno
= ENOMEM
;
191 result
= read(fd
, buf
, l
);
192 armv4_5
->semihosting_errno
= errno
;
194 retval
= target_write_buffer(target
, a
, result
, buf
);
195 if (retval
!= ERROR_OK
) {
206 case 0x07: /* SYS_READC */
210 case 0x08: /* SYS_ISERROR */
211 retval
= target_read_memory(target
, r1
, 4, 1, params
);
212 if (retval
!= ERROR_OK
)
214 result
= (target_buffer_get_u32(target
, params
+0) != 0);
217 case 0x09: /* SYS_ISTTY */
218 retval
= target_read_memory(target
, r1
, 4, 1, params
);
219 if (retval
!= ERROR_OK
)
221 result
= isatty(target_buffer_get_u32(target
, params
+0));
224 case 0x0a: /* SYS_SEEK */
225 retval
= target_read_memory(target
, r1
, 4, 2, params
);
226 if (retval
!= ERROR_OK
)
229 int fd
= target_buffer_get_u32(target
, params
+0);
230 off_t pos
= target_buffer_get_u32(target
, params
+4);
231 result
= lseek(fd
, pos
, SEEK_SET
);
232 armv4_5
->semihosting_errno
= errno
;
238 case 0x0c: /* SYS_FLEN */
239 retval
= target_read_memory(target
, r1
, 4, 1, params
);
240 if (retval
!= ERROR_OK
)
243 int fd
= target_buffer_get_u32(target
, params
+0);
245 result
= fstat(fd
, &buf
);
247 armv4_5
->semihosting_errno
= errno
;
251 result
= buf
.st_size
;
255 case 0x0e: /* SYS_REMOVE */
256 retval
= target_read_memory(target
, r1
, 4, 2, params
);
257 if (retval
!= ERROR_OK
)
260 uint32_t a
= target_buffer_get_u32(target
, params
+0);
261 uint32_t l
= target_buffer_get_u32(target
, params
+4);
264 retval
= target_read_memory(target
, a
, 1, l
, fn
);
265 if (retval
!= ERROR_OK
)
268 result
= remove((char *)fn
);
269 armv4_5
->semihosting_errno
= errno
;
272 armv4_5
->semihosting_errno
= EINVAL
;
277 case 0x0f: /* SYS_RENAME */
278 retval
= target_read_memory(target
, r1
, 4, 4, params
);
279 if (retval
!= ERROR_OK
)
282 uint32_t a1
= target_buffer_get_u32(target
, params
+0);
283 uint32_t l1
= target_buffer_get_u32(target
, params
+4);
284 uint32_t a2
= target_buffer_get_u32(target
, params
+8);
285 uint32_t l2
= target_buffer_get_u32(target
, params
+12);
286 if (l1
<= 255 && l2
<= 255) {
287 uint8_t fn1
[256], fn2
[256];
288 retval
= target_read_memory(target
, a1
, 1, l1
, fn1
);
289 if (retval
!= ERROR_OK
)
291 retval
= target_read_memory(target
, a2
, 1, l2
, fn2
);
292 if (retval
!= ERROR_OK
)
296 result
= rename((char *)fn1
, (char *)fn2
);
297 armv4_5
->semihosting_errno
= errno
;
300 armv4_5
->semihosting_errno
= EINVAL
;
305 case 0x11: /* SYS_TIME */
309 case 0x13: /* SYS_ERRNO */
310 result
= armv4_5
->semihosting_errno
;
313 case 0x15: /* SYS_GET_CMDLINE */
314 retval
= target_read_memory(target
, r1
, 4, 2, params
);
315 if (retval
!= ERROR_OK
)
318 uint32_t a
= target_buffer_get_u32(target
, params
+0);
319 uint32_t l
= target_buffer_get_u32(target
, params
+4);
320 char *arg
= "foobar";
321 uint32_t s
= strlen(arg
) + 1;
325 retval
= target_write_buffer(target
, a
, s
, (void*)arg
);
326 if (retval
!= ERROR_OK
)
333 case 0x16: /* SYS_HEAPINFO */
334 retval
= target_read_memory(target
, r1
, 4, 1, params
);
335 if (retval
!= ERROR_OK
)
338 uint32_t a
= target_buffer_get_u32(target
, params
+0);
339 /* tell the remote we have no idea */
340 memset(params
, 0, 4*4);
341 retval
= target_write_memory(target
, a
, 4, 4, params
);
342 if (retval
!= ERROR_OK
)
348 case 0x18: /* angel_SWIreason_ReportException */
350 case 0x20026: /* ADP_Stopped_ApplicationExit */
351 fprintf(stderr
, "semihosting: *** application exited ***\n");
353 case 0x20000: /* ADP_Stopped_BranchThroughZero */
354 case 0x20001: /* ADP_Stopped_UndefinedInstr */
355 case 0x20002: /* ADP_Stopped_SoftwareInterrupt */
356 case 0x20003: /* ADP_Stopped_PrefetchAbort */
357 case 0x20004: /* ADP_Stopped_DataAbort */
358 case 0x20005: /* ADP_Stopped_AddressException */
359 case 0x20006: /* ADP_Stopped_IRQ */
360 case 0x20007: /* ADP_Stopped_FIQ */
361 case 0x20020: /* ADP_Stopped_BreakPoint */
362 case 0x20021: /* ADP_Stopped_WatchPoint */
363 case 0x20022: /* ADP_Stopped_StepComplete */
364 case 0x20023: /* ADP_Stopped_RunTimeErrorUnknown */
365 case 0x20024: /* ADP_Stopped_InternalError */
366 case 0x20025: /* ADP_Stopped_UserInterruption */
367 case 0x20027: /* ADP_Stopped_StackOverflow */
368 case 0x20028: /* ADP_Stopped_DivisionByZero */
369 case 0x20029: /* ADP_Stopped_OSSpecific */
371 fprintf(stderr
, "semihosting: exception %#x\n",
374 return target_call_event_callbacks(target
, TARGET_EVENT_HALTED
);
376 case 0x0d: /* SYS_TMPNAM */
377 case 0x10: /* SYS_CLOCK */
378 case 0x12: /* SYS_SYSTEM */
379 case 0x17: /* angel_SWIreason_EnterSVC */
380 case 0x30: /* SYS_ELAPSED */
381 case 0x31: /* SYS_TICKFREQ */
383 fprintf(stderr
, "semihosting: unsupported call %#x\n",
386 armv4_5
->semihosting_errno
= ENOTSUP
;
389 /* resume execution to the original mode */
391 /* return value in R0 */
392 buf_set_u32(armv4_5
->core_cache
->reg_list
[0].value
, 0, 32, result
);
393 armv4_5
->core_cache
->reg_list
[0].dirty
= 1;
396 buf_set_u32(armv4_5
->core_cache
->reg_list
[15].value
, 0, 32, lr
);
397 armv4_5
->core_cache
->reg_list
[15].dirty
= 1;
399 /* saved PSR --> current PSR */
400 buf_set_u32(armv4_5
->cpsr
->value
, 0, 32, spsr
);
401 armv4_5
->cpsr
->dirty
= 1;
402 armv4_5
->core_mode
= spsr
& 0x1f;
404 armv4_5
->core_state
= ARM_STATE_THUMB
;
406 return target_resume(target
, 1, 0, 0, 0);
410 * Checks for and processes an ARM semihosting request. This is meant
411 * to be called when the target is stopped due to a debug mode entry.
412 * If the value 0 is returned then there was nothing to process. A non-zero
413 * return value signifies that a request was processed and the target resumed,
414 * or an error was encountered, in which case the caller must return
417 * @param target Pointer to the ARM target to process. This target must
418 * not represent an ARMv6-M or ARMv7-M processor.
419 * @param retval Pointer to a location where the return code will be stored
420 * @return non-zero value if a request was processed or an error encountered
422 int arm_semihosting(struct target
*target
, int *retval
)
424 struct arm
*arm
= target_to_arm(target
);
425 uint32_t pc
, lr
, spsr
;
428 if (!arm
->is_semihosting
|| arm
->core_mode
!= ARM_MODE_SVC
)
431 /* Check for PC == 0x00000008 or 0xffff0008: Supervisor Call vector. */
432 r
= arm
->core_cache
->reg_list
+ 15;
433 pc
= buf_get_u32(r
->value
, 0, 32);
434 if (pc
!= 0x00000008 && pc
!= 0xffff0008)
437 r
= arm_reg_current(arm
, 14);
438 lr
= buf_get_u32(r
->value
, 0, 32);
440 /* Core-specific code should make sure SPSR is retrieved
441 * when the above checks pass...
443 if (!arm
->spsr
->valid
) {
444 LOG_ERROR("SPSR not valid!");
445 *retval
= ERROR_FAIL
;
449 spsr
= buf_get_u32(arm
->spsr
->value
, 0, 32);
451 /* check instruction that triggered this trap */
452 if (spsr
& (1 << 5)) {
453 /* was in Thumb (or ThumbEE) mode */
457 *retval
= target_read_memory(target
, lr
-2, 2, 1, insn_buf
);
458 if (*retval
!= ERROR_OK
)
460 insn
= target_buffer_get_u16(target
, insn_buf
);
465 } else if (spsr
& (1 << 24)) {
466 /* was in Jazelle mode */
469 /* was in ARM mode */
473 *retval
= target_read_memory(target
, lr
-4, 4, 1, insn_buf
);
474 if (*retval
!= ERROR_OK
)
476 insn
= target_buffer_get_u32(target
, insn_buf
);
479 if (insn
!= 0xEF123456)
483 *retval
= do_semihosting(target
);