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
42 #include "arm7_9_common.h"
44 #include "cortex_m3.h"
46 #include "arm_semihosting.h"
47 #include <helper/binarybuffer.h>
48 #include <helper/log.h>
51 static int open_modeflags[12] = {
56 O_WRONLY | O_CREAT | O_TRUNC,
57 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
58 O_RDWR | O_CREAT | O_TRUNC,
59 O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
60 O_WRONLY | O_CREAT | O_APPEND,
61 O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
62 O_RDWR | O_CREAT | O_APPEND,
63 O_RDWR | O_CREAT | O_APPEND | O_BINARY
66 static int do_semihosting(struct target *target)
68 struct arm *arm = target_to_arm(target);
69 uint32_t r0 = buf_get_u32(arm->core_cache->reg_list[0].value, 0, 32);
70 uint32_t r1 = buf_get_u32(arm->core_cache->reg_list[1].value, 0, 32);
75 if (is_arm7_9(target_to_arm7_9(target)))
77 lr = buf_get_u32(ARMV4_5_CORE_REG_MODE(arm->core_cache, ARM_MODE_SVC, 14).value, 0, 32);
78 spsr = buf_get_u32(arm->spsr->value, 0, 32);;
82 * TODO: lots of security issues are not considered yet, such as:
83 * - no validation on target provided file descriptors
84 * - no safety checks on opened/deleted/renamed file paths
85 * Beware the target app you use this support with.
87 * TODO: explore mapping requests to GDB's "File-I/O Remote
88 * Protocol Extension" ... when GDB is active.
91 case 0x01: /* SYS_OPEN */
92 retval = target_read_memory(target, r1, 4, 3, params);
93 if (retval != ERROR_OK)
96 uint32_t a = target_buffer_get_u32(target, params+0);
97 uint32_t m = target_buffer_get_u32(target, params+4);
98 uint32_t l = target_buffer_get_u32(target, params+8);
99 if (l <= 255 && m <= 11) {
101 retval = target_read_memory(target, a, 1, l, fn);
102 if (retval != ERROR_OK)
105 if (strcmp((char *)fn, ":tt") == 0) {
107 result = dup(STDIN_FILENO);
109 result = dup(STDOUT_FILENO);
111 /* cygwin requires the permission setting
112 * otherwise it will fail to reopen a previously
114 result = open((char *)fn, open_modeflags[m], 0644);
116 arm->semihosting_errno = errno;
119 arm->semihosting_errno = EINVAL;
124 case 0x02: /* SYS_CLOSE */
125 retval = target_read_memory(target, r1, 4, 1, params);
126 if (retval != ERROR_OK)
129 int fd = target_buffer_get_u32(target, params+0);
131 arm->semihosting_errno = errno;
135 case 0x03: /* SYS_WRITEC */
138 retval = target_read_memory(target, r1, 1, 1, &c);
139 if (retval != ERROR_OK)
146 case 0x04: /* SYS_WRITE0 */
149 retval = target_read_memory(target, r1, 1, 1, &c);
150 if (retval != ERROR_OK)
159 case 0x05: /* SYS_WRITE */
160 retval = target_read_memory(target, r1, 4, 3, params);
161 if (retval != ERROR_OK)
164 int fd = target_buffer_get_u32(target, params+0);
165 uint32_t a = target_buffer_get_u32(target, params+4);
166 size_t l = target_buffer_get_u32(target, params+8);
167 uint8_t *buf = malloc(l);
170 arm->semihosting_errno = ENOMEM;
172 retval = target_read_buffer(target, a, l, buf);
173 if (retval != ERROR_OK) {
177 result = write(fd, buf, l);
178 arm->semihosting_errno = errno;
186 case 0x06: /* SYS_READ */
187 retval = target_read_memory(target, r1, 4, 3, params);
188 if (retval != ERROR_OK)
191 int fd = target_buffer_get_u32(target, params+0);
192 uint32_t a = target_buffer_get_u32(target, params+4);
193 ssize_t l = target_buffer_get_u32(target, params+8);
194 uint8_t *buf = malloc(l);
197 arm->semihosting_errno = ENOMEM;
199 result = read(fd, buf, l);
200 arm->semihosting_errno = errno;
202 retval = target_write_buffer(target, a, result, buf);
203 if (retval != ERROR_OK) {
214 case 0x07: /* SYS_READC */
218 case 0x08: /* SYS_ISERROR */
219 retval = target_read_memory(target, r1, 4, 1, params);
220 if (retval != ERROR_OK)
222 result = (target_buffer_get_u32(target, params+0) != 0);
225 case 0x09: /* SYS_ISTTY */
226 retval = target_read_memory(target, r1, 4, 1, params);
227 if (retval != ERROR_OK)
229 result = isatty(target_buffer_get_u32(target, params+0));
232 case 0x0a: /* SYS_SEEK */
233 retval = target_read_memory(target, r1, 4, 2, params);
234 if (retval != ERROR_OK)
237 int fd = target_buffer_get_u32(target, params+0);
238 off_t pos = target_buffer_get_u32(target, params+4);
239 result = lseek(fd, pos, SEEK_SET);
240 arm->semihosting_errno = errno;
246 case 0x0c: /* SYS_FLEN */
247 retval = target_read_memory(target, r1, 4, 1, params);
248 if (retval != ERROR_OK)
251 int fd = target_buffer_get_u32(target, params+0);
253 result = fstat(fd, &buf);
255 arm->semihosting_errno = errno;
259 result = buf.st_size;
263 case 0x0e: /* SYS_REMOVE */
264 retval = target_read_memory(target, r1, 4, 2, params);
265 if (retval != ERROR_OK)
268 uint32_t a = target_buffer_get_u32(target, params+0);
269 uint32_t l = target_buffer_get_u32(target, params+4);
272 retval = target_read_memory(target, a, 1, l, fn);
273 if (retval != ERROR_OK)
276 result = remove((char *)fn);
277 arm->semihosting_errno = errno;
280 arm->semihosting_errno = EINVAL;
285 case 0x0f: /* SYS_RENAME */
286 retval = target_read_memory(target, r1, 4, 4, params);
287 if (retval != ERROR_OK)
290 uint32_t a1 = target_buffer_get_u32(target, params+0);
291 uint32_t l1 = target_buffer_get_u32(target, params+4);
292 uint32_t a2 = target_buffer_get_u32(target, params+8);
293 uint32_t l2 = target_buffer_get_u32(target, params+12);
294 if (l1 <= 255 && l2 <= 255) {
295 uint8_t fn1[256], fn2[256];
296 retval = target_read_memory(target, a1, 1, l1, fn1);
297 if (retval != ERROR_OK)
299 retval = target_read_memory(target, a2, 1, l2, fn2);
300 if (retval != ERROR_OK)
304 result = rename((char *)fn1, (char *)fn2);
305 arm->semihosting_errno = errno;
308 arm->semihosting_errno = EINVAL;
313 case 0x11: /* SYS_TIME */
317 case 0x13: /* SYS_ERRNO */
318 result = arm->semihosting_errno;
321 case 0x15: /* SYS_GET_CMDLINE */
322 retval = target_read_memory(target, r1, 4, 2, params);
323 if (retval != ERROR_OK)
326 uint32_t a = target_buffer_get_u32(target, params+0);
327 uint32_t l = target_buffer_get_u32(target, params+4);
328 char *arg = "foobar";
329 uint32_t s = strlen(arg) + 1;
333 retval = target_write_buffer(target, a, s, (void*)arg);
334 if (retval != ERROR_OK)
341 case 0x16: /* SYS_HEAPINFO */
342 retval = target_read_memory(target, r1, 4, 1, params);
343 if (retval != ERROR_OK)
346 uint32_t a = target_buffer_get_u32(target, params+0);
347 /* tell the remote we have no idea */
348 memset(params, 0, 4*4);
349 retval = target_write_memory(target, a, 4, 4, params);
350 if (retval != ERROR_OK)
356 case 0x18: /* angel_SWIreason_ReportException */
358 case 0x20026: /* ADP_Stopped_ApplicationExit */
359 fprintf(stderr, "semihosting: *** application exited ***\n");
361 case 0x20000: /* ADP_Stopped_BranchThroughZero */
362 case 0x20001: /* ADP_Stopped_UndefinedInstr */
363 case 0x20002: /* ADP_Stopped_SoftwareInterrupt */
364 case 0x20003: /* ADP_Stopped_PrefetchAbort */
365 case 0x20004: /* ADP_Stopped_DataAbort */
366 case 0x20005: /* ADP_Stopped_AddressException */
367 case 0x20006: /* ADP_Stopped_IRQ */
368 case 0x20007: /* ADP_Stopped_FIQ */
369 case 0x20020: /* ADP_Stopped_BreakPoint */
370 case 0x20021: /* ADP_Stopped_WatchPoint */
371 case 0x20022: /* ADP_Stopped_StepComplete */
372 case 0x20023: /* ADP_Stopped_RunTimeErrorUnknown */
373 case 0x20024: /* ADP_Stopped_InternalError */
374 case 0x20025: /* ADP_Stopped_UserInterruption */
375 case 0x20027: /* ADP_Stopped_StackOverflow */
376 case 0x20028: /* ADP_Stopped_DivisionByZero */
377 case 0x20029: /* ADP_Stopped_OSSpecific */
379 fprintf(stderr, "semihosting: exception %#x\n",
382 return target_call_event_callbacks(target, TARGET_EVENT_HALTED);
384 case 0x0d: /* SYS_TMPNAM */
385 case 0x10: /* SYS_CLOCK */
386 case 0x12: /* SYS_SYSTEM */
387 case 0x17: /* angel_SWIreason_EnterSVC */
388 case 0x30: /* SYS_ELAPSED */
389 case 0x31: /* SYS_TICKFREQ */
391 fprintf(stderr, "semihosting: unsupported call %#x\n",
394 arm->semihosting_errno = ENOTSUP;
397 /* resume execution to the original mode */
399 if (is_arm7_9(target_to_arm7_9(target)))
401 /* return value in R0 */
402 buf_set_u32(arm->core_cache->reg_list[0].value, 0, 32, result);
403 arm->core_cache->reg_list[0].dirty = 1;
406 buf_set_u32(arm->core_cache->reg_list[15].value, 0, 32, lr);
407 arm->core_cache->reg_list[15].dirty = 1;
409 /* saved PSR --> current PSR */
410 buf_set_u32(arm->cpsr->value, 0, 32, spsr);
411 arm->cpsr->dirty = 1;
412 arm->core_mode = spsr & 0x1f;
414 arm->core_state = ARM_STATE_THUMB;
418 /* resume execution, this will be pc+2 to skip over the
419 * bkpt instruction */
421 /* return result in R0 */
422 buf_set_u32(arm->core_cache->reg_list[0].value, 0, 32, result);
423 arm->core_cache->reg_list[0].dirty = 1;
426 return target_resume(target, 1, 0, 0, 0);
430 * Checks for and processes an ARM semihosting request. This is meant
431 * to be called when the target is stopped due to a debug mode entry.
432 * If the value 0 is returned then there was nothing to process. A non-zero
433 * return value signifies that a request was processed and the target resumed,
434 * or an error was encountered, in which case the caller must return
437 * @param target Pointer to the ARM target to process. This target must
438 * not represent an ARMv6-M or ARMv7-M processor.
439 * @param retval Pointer to a location where the return code will be stored
440 * @return non-zero value if a request was processed or an error encountered
442 int arm_semihosting(struct target *target, int *retval)
444 struct arm *arm = target_to_arm(target);
445 uint32_t pc, lr, spsr;
448 if (!arm->is_semihosting)
451 if (is_arm7_9(target_to_arm7_9(target)))
453 if (arm->core_mode != ARM_MODE_SVC)
456 /* Check for PC == 0x00000008 or 0xffff0008: Supervisor Call vector. */
458 pc = buf_get_u32(r->value, 0, 32);
459 if (pc != 0x00000008 && pc != 0xffff0008)
462 r = arm_reg_current(arm, 14);
463 lr = buf_get_u32(r->value, 0, 32);
465 /* Core-specific code should make sure SPSR is retrieved
466 * when the above checks pass...
468 if (!arm->spsr->valid) {
469 LOG_ERROR("SPSR not valid!");
470 *retval = ERROR_FAIL;
474 spsr = buf_get_u32(arm->spsr->value, 0, 32);
476 /* check instruction that triggered this trap */
477 if (spsr & (1 << 5)) {
478 /* was in Thumb (or ThumbEE) mode */
482 *retval = target_read_memory(target, lr-2, 2, 1, insn_buf);
483 if (*retval != ERROR_OK)
485 insn = target_buffer_get_u16(target, insn_buf);
490 } else if (spsr & (1 << 24)) {
491 /* was in Jazelle mode */
494 /* was in ARM mode */
498 *retval = target_read_memory(target, lr-4, 4, 1, insn_buf);
499 if (*retval != ERROR_OK)
501 insn = target_buffer_get_u32(target, insn_buf);
504 if (insn != 0xEF123456)
508 else if (is_armv7m(target_to_armv7m(target)))
512 if (target->debug_reason != DBG_REASON_BREAKPOINT)
516 pc = buf_get_u32(r->value, 0, 32);
519 *retval = target_read_u16(target, pc, &insn);
520 if (*retval != ERROR_OK)
529 LOG_ERROR("Unsupported semi-hosting Target");
533 *retval = do_semihosting(target);