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 * 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, (void*)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 0x0d: /* SYS_TMPNAM */
378 case 0x10: /* SYS_CLOCK */
379 case 0x12: /* SYS_SYSTEM */
380 case 0x17: /* angel_SWIreason_EnterSVC */
381 case 0x30: /* SYS_ELAPSED */
382 case 0x31: /* SYS_TICKFREQ */
384 fprintf(stderr, "semihosting: unsupported call %#x\n",
387 arm->semihosting_errno = ENOTSUP;
390 /* resume execution to the original mode */
392 /* REVISIT this looks wrong ... ARM11 and Cortex-A8
393 * should work this way at least sometimes.
395 if (is_arm7_9(target_to_arm7_9(target)))
399 /* return value in R0 */
400 buf_set_u32(arm->core_cache->reg_list[0].value, 0, 32, result);
401 arm->core_cache->reg_list[0].dirty = 1;
404 buf_set_u32(arm->core_cache->reg_list[15].value, 0, 32,
405 buf_get_u32(arm_reg_current(arm,14)->value, 0, 32));
406 arm->core_cache->reg_list[15].dirty = 1;
408 /* saved PSR --> current PSR */
409 spsr = buf_get_u32(arm->spsr->value, 0, 32);
411 /* REVISIT should this be arm_set_cpsr(arm, spsr)
412 * instead of a partially unrolled version?
415 buf_set_u32(arm->cpsr->value, 0, 32, spsr);
416 arm->cpsr->dirty = 1;
417 arm->core_mode = spsr & 0x1f;
419 arm->core_state = ARM_STATE_THUMB;
424 /* resume execution, this will be pc+2 to skip over the
425 * bkpt instruction */
427 /* return result in R0 */
428 buf_set_u32(arm->core_cache->reg_list[0].value, 0, 32, result);
429 arm->core_cache->reg_list[0].dirty = 1;
432 return target_resume(target, 1, 0, 0, 0);
436 * Checks for and processes an ARM semihosting request. This is meant
437 * to be called when the target is stopped due to a debug mode entry.
438 * If the value 0 is returned then there was nothing to process. A non-zero
439 * return value signifies that a request was processed and the target resumed,
440 * or an error was encountered, in which case the caller must return
443 * @param target Pointer to the ARM target to process. This target must
444 * not represent an ARMv6-M or ARMv7-M processor.
445 * @param retval Pointer to a location where the return code will be stored
446 * @return non-zero value if a request was processed or an error encountered
448 int arm_semihosting(struct target *target, int *retval)
450 struct arm *arm = target_to_arm(target);
451 uint32_t pc, lr, spsr;
454 if (!arm->is_semihosting)
457 if (is_arm7_9(target_to_arm7_9(target)))
459 if (arm->core_mode != ARM_MODE_SVC)
462 /* Check for PC == 0x00000008 or 0xffff0008: Supervisor Call vector. */
464 pc = buf_get_u32(r->value, 0, 32);
465 if (pc != 0x00000008 && pc != 0xffff0008)
468 r = arm_reg_current(arm, 14);
469 lr = buf_get_u32(r->value, 0, 32);
471 /* Core-specific code should make sure SPSR is retrieved
472 * when the above checks pass...
474 if (!arm->spsr->valid) {
475 LOG_ERROR("SPSR not valid!");
476 *retval = ERROR_FAIL;
480 spsr = buf_get_u32(arm->spsr->value, 0, 32);
482 /* check instruction that triggered this trap */
483 if (spsr & (1 << 5)) {
484 /* was in Thumb (or ThumbEE) mode */
488 *retval = target_read_memory(target, lr-2, 2, 1, insn_buf);
489 if (*retval != ERROR_OK)
491 insn = target_buffer_get_u16(target, insn_buf);
496 } else if (spsr & (1 << 24)) {
497 /* was in Jazelle mode */
500 /* was in ARM mode */
504 *retval = target_read_memory(target, lr-4, 4, 1, insn_buf);
505 if (*retval != ERROR_OK)
507 insn = target_buffer_get_u32(target, insn_buf);
510 if (insn != 0xEF123456)
514 else if (is_armv7m(target_to_armv7m(target)))
518 if (target->debug_reason != DBG_REASON_BREAKPOINT)
522 pc = buf_get_u32(r->value, 0, 32);
525 *retval = target_read_u16(target, pc, &insn);
526 if (*retval != ERROR_OK)
535 LOG_ERROR("Unsupported semi-hosting Target");
539 *retval = do_semihosting(target);