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, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
24 * Hold ARM semihosting support.
26 * Semihosting enables code running on an ARM target to use the I/O
27 * facilities on the host computer. The target application must be linked
28 * against a library that forwards operation requests by using the SVC
29 * instruction trapped at the Supervisor Call vector by the debugger.
30 * Details can be found in chapter 8 of DUI0203I_rvct_developer_guide.pdf
40 #include "arm7_9_common.h"
45 #include "arm_opcodes.h"
46 #include "arm_semihosting.h"
47 #include <helper/binarybuffer.h>
48 #include <helper/log.h>
51 static const 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 * TODO: lots of security issues are not considered yet, such as:
76 * - no validation on target provided file descriptors
77 * - no safety checks on opened/deleted/renamed file paths
78 * Beware the target app you use this support with.
80 * TODO: explore mapping requests to GDB's "File-I/O Remote
81 * Protocol Extension" ... when GDB is active.
84 case 0x01: /* SYS_OPEN */
85 retval = target_read_memory(target, r1, 4, 3, params);
86 if (retval != ERROR_OK)
89 uint32_t a = target_buffer_get_u32(target, params+0);
90 uint32_t m = target_buffer_get_u32(target, params+4);
91 uint32_t l = target_buffer_get_u32(target, params+8);
92 if (l <= 255 && m <= 11) {
94 retval = target_read_memory(target, a, 1, l, fn);
95 if (retval != ERROR_OK)
98 if (strcmp((char *)fn, ":tt") == 0) {
100 result = dup(STDIN_FILENO);
102 result = dup(STDOUT_FILENO);
104 /* cygwin requires the permission setting
105 * otherwise it will fail to reopen a previously
107 result = open((char *)fn, open_modeflags[m], 0644);
109 arm->semihosting_errno = errno;
112 arm->semihosting_errno = EINVAL;
117 case 0x02: /* SYS_CLOSE */
118 retval = target_read_memory(target, r1, 4, 1, params);
119 if (retval != ERROR_OK)
122 int fd = target_buffer_get_u32(target, params+0);
124 arm->semihosting_errno = errno;
128 case 0x03: /* SYS_WRITEC */
131 retval = target_read_memory(target, r1, 1, 1, &c);
132 if (retval != ERROR_OK)
139 case 0x04: /* SYS_WRITE0 */
142 retval = target_read_memory(target, r1++, 1, 1, &c);
143 if (retval != ERROR_OK)
152 case 0x05: /* SYS_WRITE */
153 retval = target_read_memory(target, r1, 4, 3, params);
154 if (retval != ERROR_OK)
157 int fd = target_buffer_get_u32(target, params+0);
158 uint32_t a = target_buffer_get_u32(target, params+4);
159 size_t l = target_buffer_get_u32(target, params+8);
160 uint8_t *buf = malloc(l);
163 arm->semihosting_errno = ENOMEM;
165 retval = target_read_buffer(target, a, l, buf);
166 if (retval != ERROR_OK) {
170 result = write(fd, buf, l);
171 arm->semihosting_errno = errno;
179 case 0x06: /* SYS_READ */
180 retval = target_read_memory(target, r1, 4, 3, params);
181 if (retval != ERROR_OK)
184 int fd = target_buffer_get_u32(target, params+0);
185 uint32_t a = target_buffer_get_u32(target, params+4);
186 ssize_t l = target_buffer_get_u32(target, params+8);
187 uint8_t *buf = malloc(l);
190 arm->semihosting_errno = ENOMEM;
192 result = read(fd, buf, l);
193 arm->semihosting_errno = errno;
195 retval = target_write_buffer(target, a, result, buf);
196 if (retval != ERROR_OK) {
207 case 0x07: /* SYS_READC */
211 case 0x08: /* SYS_ISERROR */
212 retval = target_read_memory(target, r1, 4, 1, params);
213 if (retval != ERROR_OK)
215 result = (target_buffer_get_u32(target, params+0) != 0);
218 case 0x09: /* SYS_ISTTY */
219 retval = target_read_memory(target, r1, 4, 1, params);
220 if (retval != ERROR_OK)
222 result = isatty(target_buffer_get_u32(target, params+0));
225 case 0x0a: /* SYS_SEEK */
226 retval = target_read_memory(target, r1, 4, 2, params);
227 if (retval != ERROR_OK)
230 int fd = target_buffer_get_u32(target, params+0);
231 off_t pos = target_buffer_get_u32(target, params+4);
232 result = lseek(fd, pos, SEEK_SET);
233 arm->semihosting_errno = errno;
239 case 0x0c: /* SYS_FLEN */
240 retval = target_read_memory(target, r1, 4, 1, params);
241 if (retval != ERROR_OK)
244 int fd = target_buffer_get_u32(target, params+0);
246 result = fstat(fd, &buf);
248 arm->semihosting_errno = errno;
252 result = buf.st_size;
256 case 0x0e: /* SYS_REMOVE */
257 retval = target_read_memory(target, r1, 4, 2, params);
258 if (retval != ERROR_OK)
261 uint32_t a = target_buffer_get_u32(target, params+0);
262 uint32_t l = target_buffer_get_u32(target, params+4);
265 retval = target_read_memory(target, a, 1, l, fn);
266 if (retval != ERROR_OK)
269 result = remove((char *)fn);
270 arm->semihosting_errno = errno;
273 arm->semihosting_errno = EINVAL;
278 case 0x0f: /* SYS_RENAME */
279 retval = target_read_memory(target, r1, 4, 4, params);
280 if (retval != ERROR_OK)
283 uint32_t a1 = target_buffer_get_u32(target, params+0);
284 uint32_t l1 = target_buffer_get_u32(target, params+4);
285 uint32_t a2 = target_buffer_get_u32(target, params+8);
286 uint32_t l2 = target_buffer_get_u32(target, params+12);
287 if (l1 <= 255 && l2 <= 255) {
288 uint8_t fn1[256], fn2[256];
289 retval = target_read_memory(target, a1, 1, l1, fn1);
290 if (retval != ERROR_OK)
292 retval = target_read_memory(target, a2, 1, l2, fn2);
293 if (retval != ERROR_OK)
297 result = rename((char *)fn1, (char *)fn2);
298 arm->semihosting_errno = errno;
301 arm->semihosting_errno = EINVAL;
306 case 0x11: /* SYS_TIME */
310 case 0x13: /* SYS_ERRNO */
311 result = arm->semihosting_errno;
314 case 0x15: /* SYS_GET_CMDLINE */
315 retval = target_read_memory(target, r1, 4, 2, params);
316 if (retval != ERROR_OK)
319 uint32_t a = target_buffer_get_u32(target, params+0);
320 uint32_t l = target_buffer_get_u32(target, params+4);
321 char *arg = "foobar";
322 uint32_t s = strlen(arg) + 1;
326 retval = target_write_buffer(target, a, s, (uint8_t *)arg);
327 if (retval != ERROR_OK)
334 case 0x16: /* SYS_HEAPINFO */
335 retval = target_read_memory(target, r1, 4, 1, params);
336 if (retval != ERROR_OK)
339 uint32_t a = target_buffer_get_u32(target, params+0);
340 /* tell the remote we have no idea */
341 memset(params, 0, 4*4);
342 retval = target_write_memory(target, a, 4, 4, params);
343 if (retval != ERROR_OK)
349 case 0x18: /* angel_SWIreason_ReportException */
351 case 0x20026: /* ADP_Stopped_ApplicationExit */
352 fprintf(stderr, "semihosting: *** application exited ***\n");
354 case 0x20000: /* ADP_Stopped_BranchThroughZero */
355 case 0x20001: /* ADP_Stopped_UndefinedInstr */
356 case 0x20002: /* ADP_Stopped_SoftwareInterrupt */
357 case 0x20003: /* ADP_Stopped_PrefetchAbort */
358 case 0x20004: /* ADP_Stopped_DataAbort */
359 case 0x20005: /* ADP_Stopped_AddressException */
360 case 0x20006: /* ADP_Stopped_IRQ */
361 case 0x20007: /* ADP_Stopped_FIQ */
362 case 0x20020: /* ADP_Stopped_BreakPoint */
363 case 0x20021: /* ADP_Stopped_WatchPoint */
364 case 0x20022: /* ADP_Stopped_StepComplete */
365 case 0x20023: /* ADP_Stopped_RunTimeErrorUnknown */
366 case 0x20024: /* ADP_Stopped_InternalError */
367 case 0x20025: /* ADP_Stopped_UserInterruption */
368 case 0x20027: /* ADP_Stopped_StackOverflow */
369 case 0x20028: /* ADP_Stopped_DivisionByZero */
370 case 0x20029: /* ADP_Stopped_OSSpecific */
372 fprintf(stderr, "semihosting: exception %#x\n",
375 return target_call_event_callbacks(target, TARGET_EVENT_HALTED);
377 case 0x12: /* SYS_SYSTEM */
378 /* Provide SYS_SYSTEM functionality. Uses the
379 * libc system command, there may be a reason *NOT*
380 * to use this, but as I can't think of one, I
381 * implemented it this way.
383 retval = target_read_memory(target, r1, 4, 2, params);
384 if (retval != ERROR_OK)
387 uint32_t len = target_buffer_get_u32(target, params+4);
388 uint32_t c_ptr = target_buffer_get_u32(target, params);
392 arm->semihosting_errno = EINVAL;
394 memset(cmd, 0x0, 256);
395 retval = target_read_memory(target, c_ptr, 1, len, cmd);
396 if (retval != ERROR_OK)
399 result = system((const char *)cmd);
403 case 0x0d: /* SYS_TMPNAM */
404 case 0x10: /* SYS_CLOCK */
405 case 0x17: /* angel_SWIreason_EnterSVC */
406 case 0x30: /* SYS_ELAPSED */
407 case 0x31: /* SYS_TICKFREQ */
409 fprintf(stderr, "semihosting: unsupported call %#x\n",
412 arm->semihosting_errno = ENOTSUP;
415 /* resume execution to the original mode */
417 /* REVISIT this looks wrong ... ARM11 and Cortex-A8
418 * should work this way at least sometimes.
420 if (is_arm7_9(target_to_arm7_9(target)) ||
421 is_armv7a(target_to_armv7a(target))) {
424 /* return value in R0 */
425 buf_set_u32(arm->core_cache->reg_list[0].value, 0, 32, result);
426 arm->core_cache->reg_list[0].dirty = 1;
429 buf_set_u32(arm->core_cache->reg_list[15].value, 0, 32,
430 buf_get_u32(arm_reg_current(arm, 14)->value, 0, 32));
431 arm->core_cache->reg_list[15].dirty = 1;
433 /* saved PSR --> current PSR */
434 spsr = buf_get_u32(arm->spsr->value, 0, 32);
436 /* REVISIT should this be arm_set_cpsr(arm, spsr)
437 * instead of a partially unrolled version?
440 buf_set_u32(arm->cpsr->value, 0, 32, spsr);
441 arm->cpsr->dirty = 1;
442 arm->core_mode = spsr & 0x1f;
444 arm->core_state = ARM_STATE_THUMB;
447 /* resume execution, this will be pc+2 to skip over the
448 * bkpt instruction */
450 /* return result in R0 */
451 buf_set_u32(arm->core_cache->reg_list[0].value, 0, 32, result);
452 arm->core_cache->reg_list[0].dirty = 1;
455 return target_resume(target, 1, 0, 0, 0);
459 * Checks for and processes an ARM semihosting request. This is meant
460 * to be called when the target is stopped due to a debug mode entry.
461 * If the value 0 is returned then there was nothing to process. A non-zero
462 * return value signifies that a request was processed and the target resumed,
463 * or an error was encountered, in which case the caller must return
466 * @param target Pointer to the ARM target to process. This target must
467 * not represent an ARMv6-M or ARMv7-M processor.
468 * @param retval Pointer to a location where the return code will be stored
469 * @return non-zero value if a request was processed or an error encountered
471 int arm_semihosting(struct target *target, int *retval)
473 struct arm *arm = target_to_arm(target);
474 struct armv7a_common *armv7a = target_to_armv7a(target);
475 uint32_t pc, lr, spsr;
478 if (!arm->is_semihosting)
481 if (is_arm7_9(target_to_arm7_9(target)) ||
483 uint32_t vbar = 0x00000000;
485 if (arm->core_mode != ARM_MODE_SVC)
488 if (is_armv7a(armv7a)) {
489 struct arm_dpm *dpm = armv7a->arm.dpm;
491 *retval = dpm->prepare(dpm);
492 if (*retval == ERROR_OK) {
493 *retval = dpm->instr_read_data_r0(dpm,
494 ARMV4_5_MRC(15, 0, 0, 12, 0, 0),
499 if (*retval != ERROR_OK)
506 /* Check for PC == 0x00000008 or 0xffff0008: Supervisor Call vector. */
508 pc = buf_get_u32(r->value, 0, 32);
509 if (pc != (vbar + 0x00000008) && pc != 0xffff0008)
512 r = arm_reg_current(arm, 14);
513 lr = buf_get_u32(r->value, 0, 32);
515 /* Core-specific code should make sure SPSR is retrieved
516 * when the above checks pass...
518 if (!arm->spsr->valid) {
519 LOG_ERROR("SPSR not valid!");
520 *retval = ERROR_FAIL;
524 spsr = buf_get_u32(arm->spsr->value, 0, 32);
526 /* check instruction that triggered this trap */
527 if (spsr & (1 << 5)) {
528 /* was in Thumb (or ThumbEE) mode */
532 *retval = target_read_memory(target, lr-2, 2, 1, insn_buf);
533 if (*retval != ERROR_OK)
535 insn = target_buffer_get_u16(target, insn_buf);
540 } else if (spsr & (1 << 24)) {
541 /* was in Jazelle mode */
544 /* was in ARM mode */
548 *retval = target_read_memory(target, lr-4, 4, 1, insn_buf);
549 if (*retval != ERROR_OK)
551 insn = target_buffer_get_u32(target, insn_buf);
554 if (insn != 0xEF123456)
557 } else if (is_armv7m(target_to_armv7m(target))) {
560 if (target->debug_reason != DBG_REASON_BREAKPOINT)
564 pc = buf_get_u32(r->value, 0, 32);
567 *retval = target_read_u16(target, pc, &insn);
568 if (*retval != ERROR_OK)
575 LOG_ERROR("Unsupported semi-hosting Target");
579 *retval = do_semihosting(target);