]> Git Repo - qemu.git/blame - target/arm/arm-semi.c
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[qemu.git] / target / arm / arm-semi.c
CommitLineData
a4f81979
FB
1/*
2 * Arm "Angel" semihosting syscalls
5fafdf24 3 *
8e71621f 4 * Copyright (c) 2005, 2007 CodeSourcery.
4cb28db9 5 * Copyright (c) 2019 Linaro
8e71621f 6 * Written by Paul Brook.
a4f81979
FB
7 *
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.
12 *
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.
17 *
18 * You should have received a copy of the GNU General Public License
8167ee88 19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
4cb28db9
AB
20 *
21 * ARM Semihosting is documented in:
22 * Semihosting for AArch32 and AArch64 Release 2.0
23 * https://static.docs.arm.com/100863/0200/semihosting.pdf
a4f81979
FB
24 */
25
74c21bd0 26#include "qemu/osdep.h"
a4f81979 27
8e71621f 28#include "cpu.h"
f1672e6f 29#include "hw/semihosting/semihost.h"
0dc07721 30#include "hw/semihosting/console.h"
a131795f 31#include "qemu/log.h"
8e71621f 32#ifdef CONFIG_USER_ONLY
a4f81979
FB
33#include "qemu.h"
34
35#define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
8e71621f 36#else
022c62cb 37#include "exec/gdbstub.h"
f348b6d1 38#include "qemu/cutils.h"
8e71621f 39#endif
a4f81979 40
3881725c
SW
41#define TARGET_SYS_OPEN 0x01
42#define TARGET_SYS_CLOSE 0x02
43#define TARGET_SYS_WRITEC 0x03
44#define TARGET_SYS_WRITE0 0x04
45#define TARGET_SYS_WRITE 0x05
46#define TARGET_SYS_READ 0x06
47#define TARGET_SYS_READC 0x07
48#define TARGET_SYS_ISTTY 0x09
49#define TARGET_SYS_SEEK 0x0a
50#define TARGET_SYS_FLEN 0x0c
51#define TARGET_SYS_TMPNAM 0x0d
52#define TARGET_SYS_REMOVE 0x0e
53#define TARGET_SYS_RENAME 0x0f
54#define TARGET_SYS_CLOCK 0x10
55#define TARGET_SYS_TIME 0x11
56#define TARGET_SYS_SYSTEM 0x12
57#define TARGET_SYS_ERRNO 0x13
58#define TARGET_SYS_GET_CMDLINE 0x15
59#define TARGET_SYS_HEAPINFO 0x16
60#define TARGET_SYS_EXIT 0x18
e9ebfbfc 61#define TARGET_SYS_SYNCCACHE 0x19
22a43bb9 62#define TARGET_SYS_EXIT_EXTENDED 0x20
a4f81979 63
1ecc3a2d
LI
64/* ADP_Stopped_ApplicationExit is used for exit(0),
65 * anything else is implemented as exit(1) */
66#define ADP_Stopped_ApplicationExit (0x20026)
67
a4f81979
FB
68#ifndef O_BINARY
69#define O_BINARY 0
70#endif
71
a2d1ebaf
PB
72#define GDB_O_RDONLY 0x000
73#define GDB_O_WRONLY 0x001
74#define GDB_O_RDWR 0x002
75#define GDB_O_APPEND 0x008
76#define GDB_O_CREAT 0x200
77#define GDB_O_TRUNC 0x400
78#define GDB_O_BINARY 0
79
80static int gdb_open_modeflags[12] = {
81 GDB_O_RDONLY,
82 GDB_O_RDONLY | GDB_O_BINARY,
83 GDB_O_RDWR,
84 GDB_O_RDWR | GDB_O_BINARY,
85 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
86 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
87 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
88 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
89 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
90 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY,
91 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
92 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY
93};
94
95static int open_modeflags[12] = {
a4f81979
FB
96 O_RDONLY,
97 O_RDONLY | O_BINARY,
98 O_RDWR,
99 O_RDWR | O_BINARY,
100 O_WRONLY | O_CREAT | O_TRUNC,
101 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
102 O_RDWR | O_CREAT | O_TRUNC,
103 O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
104 O_WRONLY | O_CREAT | O_APPEND,
105 O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
106 O_RDWR | O_CREAT | O_APPEND,
107 O_RDWR | O_CREAT | O_APPEND | O_BINARY
108};
109
35e9a0a8
PM
110typedef enum GuestFDType {
111 GuestFDUnused = 0,
112 GuestFDHost = 1,
263eb621 113 GuestFDGDB = 2,
c46a653c 114 GuestFDFeatureFile = 3,
35e9a0a8
PM
115} GuestFDType;
116
117/*
118 * Guest file descriptors are integer indexes into an array of
119 * these structures (we will dynamically resize as necessary).
120 */
121typedef struct GuestFD {
122 GuestFDType type;
c46a653c
PM
123 union {
124 int hostfd;
125 target_ulong featurefile_offset;
126 };
35e9a0a8
PM
127} GuestFD;
128
129static GArray *guestfd_array;
130
131/*
132 * Allocate a new guest file descriptor and return it; if we
133 * couldn't allocate a new fd then return -1.
134 * This is a fairly simplistic implementation because we don't
135 * expect that most semihosting guest programs will make very
136 * heavy use of opening and closing fds.
137 */
138static int alloc_guestfd(void)
139{
140 guint i;
141
142 if (!guestfd_array) {
143 /* New entries zero-initialized, i.e. type GuestFDUnused */
144 guestfd_array = g_array_new(FALSE, TRUE, sizeof(GuestFD));
145 }
146
21bf9b06
MY
147 /* SYS_OPEN should return nonzero handle on success. Start guestfd from 1 */
148 for (i = 1; i < guestfd_array->len; i++) {
35e9a0a8
PM
149 GuestFD *gf = &g_array_index(guestfd_array, GuestFD, i);
150
151 if (gf->type == GuestFDUnused) {
152 return i;
153 }
154 }
155
156 /* All elements already in use: expand the array */
157 g_array_set_size(guestfd_array, i + 1);
158 return i;
159}
160
161/*
162 * Look up the guestfd in the data structure; return NULL
163 * for out of bounds, but don't check whether the slot is unused.
164 * This is used internally by the other guestfd functions.
165 */
166static GuestFD *do_get_guestfd(int guestfd)
167{
168 if (!guestfd_array) {
169 return NULL;
170 }
171
21bf9b06 172 if (guestfd <= 0 || guestfd >= guestfd_array->len) {
35e9a0a8
PM
173 return NULL;
174 }
175
176 return &g_array_index(guestfd_array, GuestFD, guestfd);
177}
178
179/*
180 * Associate the specified guest fd (which must have been
181 * allocated via alloc_fd() and not previously used) with
263eb621 182 * the specified host/gdb fd.
35e9a0a8
PM
183 */
184static void associate_guestfd(int guestfd, int hostfd)
185{
186 GuestFD *gf = do_get_guestfd(guestfd);
187
188 assert(gf);
263eb621 189 gf->type = use_gdb_syscalls() ? GuestFDGDB : GuestFDHost;
35e9a0a8
PM
190 gf->hostfd = hostfd;
191}
192
193/*
194 * Deallocate the specified guest file descriptor. This doesn't
195 * close the host fd, it merely undoes the work of alloc_fd().
196 */
197static void dealloc_guestfd(int guestfd)
198{
199 GuestFD *gf = do_get_guestfd(guestfd);
200
201 assert(gf);
202 gf->type = GuestFDUnused;
203}
204
205/*
206 * Given a guest file descriptor, get the associated struct.
207 * If the fd is not valid, return NULL. This is the function
208 * used by the various semihosting calls to validate a handle
209 * from the guest.
210 * Note: calling alloc_guestfd() or dealloc_guestfd() will
211 * invalidate any GuestFD* obtained by calling this function.
212 */
213static GuestFD *get_guestfd(int guestfd)
214{
215 GuestFD *gf = do_get_guestfd(guestfd);
216
217 if (!gf || gf->type == GuestFDUnused) {
218 return NULL;
219 }
220 return gf;
221}
222
6ed68455
PM
223/*
224 * The semihosting API has no concept of its errno being thread-safe,
225 * as the API design predates SMP CPUs and was intended as a simple
226 * real-hardware set of debug functionality. For QEMU, we make the
227 * errno be per-thread in linux-user mode; in softmmu it is a simple
228 * global, and we assume that the guest takes care of avoiding any races.
229 */
230#ifndef CONFIG_USER_ONLY
1b003821
PM
231static target_ulong syscall_err;
232
6ed68455
PM
233#include "exec/softmmu-semi.h"
234#endif
235
81926f47 236static inline uint32_t set_swi_errno(CPUARMState *env, uint32_t code)
8e71621f 237{
1b003821 238 if (code == (uint32_t)-1) {
6ed68455
PM
239#ifdef CONFIG_USER_ONLY
240 CPUState *cs = env_cpu(env);
241 TaskState *ts = cs->opaque;
242
243 ts->swi_errno = errno;
244#else
1b003821 245 syscall_err = errno;
6ed68455 246#endif
1b003821 247 }
8e71621f
PB
248 return code;
249}
250
6ed68455
PM
251static inline uint32_t get_swi_errno(CPUARMState *env)
252{
253#ifdef CONFIG_USER_ONLY
254 CPUState *cs = env_cpu(env);
255 TaskState *ts = cs->opaque;
256
257 return ts->swi_errno;
258#else
259 return syscall_err;
8e71621f 260#endif
6ed68455 261}
a4f81979 262
a2d1ebaf
PB
263static target_ulong arm_semi_syscall_len;
264
9e0c5422 265static void arm_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
a2d1ebaf 266{
9e0c5422
AF
267 ARMCPU *cpu = ARM_CPU(cs);
268 CPUARMState *env = &cpu->env;
faacc041 269 target_ulong reg0 = is_a64(env) ? env->xregs[0] : env->regs[0];
33d9cc8a 270
a2d1ebaf 271 if (ret == (target_ulong)-1) {
939f5b43
PM
272 errno = err;
273 set_swi_errno(env, -1);
bb19cbc9 274 reg0 = ret;
a2d1ebaf
PB
275 } else {
276 /* Fixup syscalls that use nonstardard return conventions. */
bb19cbc9 277 switch (reg0) {
3881725c
SW
278 case TARGET_SYS_WRITE:
279 case TARGET_SYS_READ:
bb19cbc9 280 reg0 = arm_semi_syscall_len - ret;
a2d1ebaf 281 break;
3881725c 282 case TARGET_SYS_SEEK:
bb19cbc9 283 reg0 = 0;
a2d1ebaf
PB
284 break;
285 default:
bb19cbc9 286 reg0 = ret;
a2d1ebaf
PB
287 break;
288 }
289 }
faacc041
PM
290 if (is_a64(env)) {
291 env->xregs[0] = reg0;
292 } else {
293 env->regs[0] = reg0;
294 }
295}
296
297static target_ulong arm_flen_buf(ARMCPU *cpu)
298{
299 /* Return an address in target memory of 64 bytes where the remote
300 * gdb should write its stat struct. (The format of this structure
301 * is defined by GDB's remote protocol and is not target-specific.)
302 * We put this on the guest's stack just below SP.
303 */
304 CPUARMState *env = &cpu->env;
305 target_ulong sp;
306
307 if (is_a64(env)) {
308 sp = env->xregs[31];
309 } else {
310 sp = env->regs[13];
311 }
312
313 return sp - 64;
a2d1ebaf
PB
314}
315
9e0c5422 316static void arm_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err)
33d9cc8a 317{
9e0c5422
AF
318 ARMCPU *cpu = ARM_CPU(cs);
319 CPUARMState *env = &cpu->env;
33d9cc8a
PB
320 /* The size is always stored in big-endian order, extract
321 the value. We assume the size always fit in 32 bits. */
322 uint32_t size;
faacc041
PM
323 cpu_memory_rw_debug(cs, arm_flen_buf(cpu) + 32, (uint8_t *)&size, 4, 0);
324 size = be32_to_cpu(size);
325 if (is_a64(env)) {
326 env->xregs[0] = size;
327 } else {
328 env->regs[0] = size;
329 }
939f5b43
PM
330 errno = err;
331 set_swi_errno(env, -1);
33d9cc8a
PB
332}
333
35e9a0a8
PM
334static int arm_semi_open_guestfd;
335
336static void arm_semi_open_cb(CPUState *cs, target_ulong ret, target_ulong err)
337{
338 ARMCPU *cpu = ARM_CPU(cs);
339 CPUARMState *env = &cpu->env;
35e9a0a8 340 if (ret == (target_ulong)-1) {
939f5b43
PM
341 errno = err;
342 set_swi_errno(env, -1);
35e9a0a8
PM
343 dealloc_guestfd(arm_semi_open_guestfd);
344 } else {
345 associate_guestfd(arm_semi_open_guestfd, ret);
346 ret = arm_semi_open_guestfd;
347 }
348
349 if (is_a64(env)) {
350 env->xregs[0] = ret;
351 } else {
352 env->regs[0] = ret;
353 }
354}
355
bb19cbc9
PM
356static target_ulong arm_gdb_syscall(ARMCPU *cpu, gdb_syscall_complete_cb cb,
357 const char *fmt, ...)
358{
359 va_list va;
360 CPUARMState *env = &cpu->env;
361
362 va_start(va, fmt);
363 gdb_do_syscallv(cb, fmt, va);
364 va_end(va);
365
f8ad2306
PM
366 /*
367 * FIXME: in softmmu mode, the gdbstub will schedule our callback
368 * to occur, but will not actually call it to complete the syscall
369 * until after this function has returned and we are back in the
370 * CPU main loop. Therefore callers to this function must not
371 * do anything with its return value, because it is not necessarily
372 * the result of the syscall, but could just be the old value of X0.
373 * The only thing safe to do with this is that the callers of
374 * do_arm_semihosting() will write it straight back into X0.
375 * (In linux-user mode, the callback will have happened before
376 * gdb_do_syscallv() returns.)
377 *
378 * We should tidy this up so neither this function nor
379 * do_arm_semihosting() return a value, so the mistake of
380 * doing something with the return value is not possible to make.
bb19cbc9
PM
381 */
382
faacc041 383 return is_a64(env) ? env->xregs[0] : env->regs[0];
bb19cbc9
PM
384}
385
263eb621
PM
386/*
387 * Types for functions implementing various semihosting calls
388 * for specific types of guest file descriptor. These must all
389 * do the work and return the required return value for the guest,
390 * setting the guest errno if appropriate.
391 */
392typedef uint32_t sys_closefn(ARMCPU *cpu, GuestFD *gf);
52c8a163
PM
393typedef uint32_t sys_writefn(ARMCPU *cpu, GuestFD *gf,
394 target_ulong buf, uint32_t len);
2c3a09a6
PM
395typedef uint32_t sys_readfn(ARMCPU *cpu, GuestFD *gf,
396 target_ulong buf, uint32_t len);
0213fa45 397typedef uint32_t sys_isattyfn(ARMCPU *cpu, GuestFD *gf);
45e88ffc
PM
398typedef uint32_t sys_seekfn(ARMCPU *cpu, GuestFD *gf,
399 target_ulong offset);
1631a7be 400typedef uint32_t sys_flenfn(ARMCPU *cpu, GuestFD *gf);
263eb621
PM
401
402static uint32_t host_closefn(ARMCPU *cpu, GuestFD *gf)
403{
404 CPUARMState *env = &cpu->env;
405
16ab12a9
PM
406 /*
407 * Only close the underlying host fd if it's one we opened on behalf
408 * of the guest in SYS_OPEN.
409 */
410 if (gf->hostfd == STDIN_FILENO ||
411 gf->hostfd == STDOUT_FILENO ||
412 gf->hostfd == STDERR_FILENO) {
413 return 0;
414 }
263eb621
PM
415 return set_swi_errno(env, close(gf->hostfd));
416}
417
52c8a163
PM
418static uint32_t host_writefn(ARMCPU *cpu, GuestFD *gf,
419 target_ulong buf, uint32_t len)
420{
421 uint32_t ret;
422 CPUARMState *env = &cpu->env;
423 char *s = lock_user(VERIFY_READ, buf, len, 1);
424 if (!s) {
425 /* Return bytes not written on error */
426 return len;
427 }
428 ret = set_swi_errno(env, write(gf->hostfd, s, len));
429 unlock_user(s, buf, 0);
430 if (ret == (uint32_t)-1) {
431 ret = 0;
432 }
433 /* Return bytes not written */
434 return len - ret;
435}
436
2c3a09a6
PM
437static uint32_t host_readfn(ARMCPU *cpu, GuestFD *gf,
438 target_ulong buf, uint32_t len)
439{
440 uint32_t ret;
441 CPUARMState *env = &cpu->env;
442 char *s = lock_user(VERIFY_WRITE, buf, len, 0);
443 if (!s) {
444 /* return bytes not read */
445 return len;
446 }
447 do {
448 ret = set_swi_errno(env, read(gf->hostfd, s, len));
449 } while (ret == -1 && errno == EINTR);
450 unlock_user(s, buf, len);
451 if (ret == (uint32_t)-1) {
452 ret = 0;
453 }
454 /* Return bytes not read */
455 return len - ret;
456}
457
0213fa45
PM
458static uint32_t host_isattyfn(ARMCPU *cpu, GuestFD *gf)
459{
460 return isatty(gf->hostfd);
461}
462
45e88ffc
PM
463static uint32_t host_seekfn(ARMCPU *cpu, GuestFD *gf, target_ulong offset)
464{
465 CPUARMState *env = &cpu->env;
466 uint32_t ret = set_swi_errno(env, lseek(gf->hostfd, offset, SEEK_SET));
467 if (ret == (uint32_t)-1) {
468 return -1;
469 }
470 return 0;
471}
472
1631a7be
PM
473static uint32_t host_flenfn(ARMCPU *cpu, GuestFD *gf)
474{
475 CPUARMState *env = &cpu->env;
476 struct stat buf;
477 uint32_t ret = set_swi_errno(env, fstat(gf->hostfd, &buf));
478 if (ret == (uint32_t)-1) {
479 return -1;
480 }
481 return buf.st_size;
482}
483
263eb621
PM
484static uint32_t gdb_closefn(ARMCPU *cpu, GuestFD *gf)
485{
486 return arm_gdb_syscall(cpu, arm_semi_cb, "close,%x", gf->hostfd);
487}
488
52c8a163
PM
489static uint32_t gdb_writefn(ARMCPU *cpu, GuestFD *gf,
490 target_ulong buf, uint32_t len)
491{
492 arm_semi_syscall_len = len;
493 return arm_gdb_syscall(cpu, arm_semi_cb, "write,%x,%x,%x",
494 gf->hostfd, buf, len);
495}
496
2c3a09a6
PM
497static uint32_t gdb_readfn(ARMCPU *cpu, GuestFD *gf,
498 target_ulong buf, uint32_t len)
499{
500 arm_semi_syscall_len = len;
501 return arm_gdb_syscall(cpu, arm_semi_cb, "read,%x,%x,%x",
502 gf->hostfd, buf, len);
503}
504
0213fa45
PM
505static uint32_t gdb_isattyfn(ARMCPU *cpu, GuestFD *gf)
506{
507 return arm_gdb_syscall(cpu, arm_semi_cb, "isatty,%x", gf->hostfd);
508}
509
45e88ffc
PM
510static uint32_t gdb_seekfn(ARMCPU *cpu, GuestFD *gf, target_ulong offset)
511{
512 return arm_gdb_syscall(cpu, arm_semi_cb, "lseek,%x,%x,0",
513 gf->hostfd, offset);
514}
515
1631a7be
PM
516static uint32_t gdb_flenfn(ARMCPU *cpu, GuestFD *gf)
517{
518 return arm_gdb_syscall(cpu, arm_semi_flen_cb, "fstat,%x,%x",
519 gf->hostfd, arm_flen_buf(cpu));
520}
521
c46a653c
PM
522#define SHFB_MAGIC_0 0x53
523#define SHFB_MAGIC_1 0x48
524#define SHFB_MAGIC_2 0x46
525#define SHFB_MAGIC_3 0x42
526
22a43bb9
PM
527/* Feature bits reportable in feature byte 0 */
528#define SH_EXT_EXIT_EXTENDED (1 << 0)
6ee18643 529#define SH_EXT_STDOUT_STDERR (1 << 1)
22a43bb9 530
c46a653c
PM
531static const uint8_t featurefile_data[] = {
532 SHFB_MAGIC_0,
533 SHFB_MAGIC_1,
534 SHFB_MAGIC_2,
535 SHFB_MAGIC_3,
6ee18643 536 SH_EXT_EXIT_EXTENDED | SH_EXT_STDOUT_STDERR, /* Feature byte 0 */
c46a653c
PM
537};
538
539static void init_featurefile_guestfd(int guestfd)
540{
541 GuestFD *gf = do_get_guestfd(guestfd);
542
543 assert(gf);
544 gf->type = GuestFDFeatureFile;
545 gf->featurefile_offset = 0;
546}
547
548static uint32_t featurefile_closefn(ARMCPU *cpu, GuestFD *gf)
549{
550 /* Nothing to do */
551 return 0;
552}
553
554static uint32_t featurefile_writefn(ARMCPU *cpu, GuestFD *gf,
555 target_ulong buf, uint32_t len)
556{
557 /* This fd can never be open for writing */
558 CPUARMState *env = &cpu->env;
559
560 errno = EBADF;
561 return set_swi_errno(env, -1);
562}
563
564static uint32_t featurefile_readfn(ARMCPU *cpu, GuestFD *gf,
565 target_ulong buf, uint32_t len)
566{
567 uint32_t i;
568#ifndef CONFIG_USER_ONLY
569 CPUARMState *env = &cpu->env;
570#endif
571 char *s;
572
573 s = lock_user(VERIFY_WRITE, buf, len, 0);
574 if (!s) {
575 return len;
576 }
577
578 for (i = 0; i < len; i++) {
579 if (gf->featurefile_offset >= sizeof(featurefile_data)) {
580 break;
581 }
582 s[i] = featurefile_data[gf->featurefile_offset];
583 gf->featurefile_offset++;
584 }
585
586 unlock_user(s, buf, len);
587
588 /* Return number of bytes not read */
589 return len - i;
590}
591
592static uint32_t featurefile_isattyfn(ARMCPU *cpu, GuestFD *gf)
593{
594 return 0;
595}
596
597static uint32_t featurefile_seekfn(ARMCPU *cpu, GuestFD *gf,
598 target_ulong offset)
599{
600 gf->featurefile_offset = offset;
601 return 0;
602}
603
604static uint32_t featurefile_flenfn(ARMCPU *cpu, GuestFD *gf)
605{
606 return sizeof(featurefile_data);
607}
608
263eb621
PM
609typedef struct GuestFDFunctions {
610 sys_closefn *closefn;
52c8a163 611 sys_writefn *writefn;
2c3a09a6 612 sys_readfn *readfn;
0213fa45 613 sys_isattyfn *isattyfn;
45e88ffc 614 sys_seekfn *seekfn;
1631a7be 615 sys_flenfn *flenfn;
263eb621
PM
616} GuestFDFunctions;
617
618static const GuestFDFunctions guestfd_fns[] = {
619 [GuestFDHost] = {
620 .closefn = host_closefn,
52c8a163 621 .writefn = host_writefn,
2c3a09a6 622 .readfn = host_readfn,
0213fa45 623 .isattyfn = host_isattyfn,
45e88ffc 624 .seekfn = host_seekfn,
1631a7be 625 .flenfn = host_flenfn,
263eb621
PM
626 },
627 [GuestFDGDB] = {
628 .closefn = gdb_closefn,
52c8a163 629 .writefn = gdb_writefn,
2c3a09a6 630 .readfn = gdb_readfn,
0213fa45 631 .isattyfn = gdb_isattyfn,
45e88ffc 632 .seekfn = gdb_seekfn,
1631a7be 633 .flenfn = gdb_flenfn,
263eb621 634 },
c46a653c
PM
635 [GuestFDFeatureFile] = {
636 .closefn = featurefile_closefn,
637 .writefn = featurefile_writefn,
638 .readfn = featurefile_readfn,
639 .isattyfn = featurefile_isattyfn,
640 .seekfn = featurefile_seekfn,
641 .flenfn = featurefile_flenfn,
642 },
263eb621
PM
643};
644
f296c0d1
PM
645/* Read the input value from the argument block; fail the semihosting
646 * call if the memory read fails.
647 */
648#define GET_ARG(n) do { \
faacc041
PM
649 if (is_a64(env)) { \
650 if (get_user_u64(arg ## n, args + (n) * 8)) { \
f7d38cf2 651 errno = EFAULT; \
6ed68455 652 return set_swi_errno(env, -1); \
faacc041
PM
653 } \
654 } else { \
655 if (get_user_u32(arg ## n, args + (n) * 4)) { \
f7d38cf2 656 errno = EFAULT; \
6ed68455 657 return set_swi_errno(env, -1); \
faacc041 658 } \
f296c0d1
PM
659 } \
660} while (0)
661
faacc041
PM
662#define SET_ARG(n, val) \
663 (is_a64(env) ? \
664 put_user_u64(val, args + (n) * 8) : \
665 put_user_u32(val, args + (n) * 4))
666
4cb28db9
AB
667/*
668 * Do a semihosting call.
669 *
670 * The specification always says that the "return register" either
671 * returns a specific value or is corrupted, so we don't need to
672 * report to our caller whether we are returning a value or trying to
673 * leave the register unchanged. We use 0xdeadbeef as the return value
674 * when there isn't a defined return value for the call.
675 */
faacc041 676target_ulong do_arm_semihosting(CPUARMState *env)
a4f81979 677{
2fc0cc0e
RH
678 ARMCPU *cpu = env_archcpu(env);
679 CPUState *cs = env_cpu(env);
53a5960a 680 target_ulong args;
f296c0d1 681 target_ulong arg0, arg1, arg2, arg3;
a4f81979
FB
682 char * s;
683 int nr;
684 uint32_t ret;
8e71621f 685 uint32_t len;
35e9a0a8 686 GuestFD *gf;
a4f81979 687
faacc041
PM
688 if (is_a64(env)) {
689 /* Note that the syscall number is in W0, not X0 */
690 nr = env->xregs[0] & 0xffffffffU;
691 args = env->xregs[1];
692 } else {
693 nr = env->regs[0];
694 args = env->regs[1];
695 }
696
a4f81979 697 switch (nr) {
3881725c 698 case TARGET_SYS_OPEN:
35e9a0a8
PM
699 {
700 int guestfd;
701
f296c0d1
PM
702 GET_ARG(0);
703 GET_ARG(1);
704 GET_ARG(2);
705 s = lock_user_string(arg0);
706 if (!s) {
f7d38cf2 707 errno = EFAULT;
6ed68455 708 return set_swi_errno(env, -1);
f296c0d1
PM
709 }
710 if (arg1 >= 12) {
711 unlock_user(s, arg0, 0);
f7d38cf2 712 errno = EINVAL;
6ed68455 713 return set_swi_errno(env, -1);
396bef4b 714 }
35e9a0a8
PM
715
716 guestfd = alloc_guestfd();
717 if (guestfd < 0) {
718 unlock_user(s, arg0, 0);
719 errno = EMFILE;
6ed68455 720 return set_swi_errno(env, -1);
35e9a0a8
PM
721 }
722
a4f81979 723 if (strcmp(s, ":tt") == 0) {
6ee18643
PM
724 int result_fileno;
725
726 /*
727 * We implement SH_EXT_STDOUT_STDERR, so:
728 * open for read == stdin
729 * open for write == stdout
730 * open for append == stderr
731 */
732 if (arg1 < 4) {
733 result_fileno = STDIN_FILENO;
734 } else if (arg1 < 8) {
735 result_fileno = STDOUT_FILENO;
736 } else {
737 result_fileno = STDERR_FILENO;
738 }
35e9a0a8 739 associate_guestfd(guestfd, result_fileno);
f296c0d1 740 unlock_user(s, arg0, 0);
35e9a0a8 741 return guestfd;
a4f81979 742 }
c46a653c
PM
743 if (strcmp(s, ":semihosting-features") == 0) {
744 unlock_user(s, arg0, 0);
745 /* We must fail opens for modes other than 0 ('r') or 1 ('rb') */
746 if (arg1 != 0 && arg1 != 1) {
747 dealloc_guestfd(guestfd);
748 errno = EACCES;
749 return set_swi_errno(env, -1);
750 }
751 init_featurefile_guestfd(guestfd);
752 return guestfd;
753 }
754
a2d1ebaf 755 if (use_gdb_syscalls()) {
35e9a0a8
PM
756 arm_semi_open_guestfd = guestfd;
757 ret = arm_gdb_syscall(cpu, arm_semi_open_cb, "open,%s,%x,1a4", arg0,
bb19cbc9 758 (int)arg2+1, gdb_open_modeflags[arg1]);
a2d1ebaf 759 } else {
6ed68455 760 ret = set_swi_errno(env, open(s, open_modeflags[arg1], 0644));
35e9a0a8
PM
761 if (ret == (uint32_t)-1) {
762 dealloc_guestfd(guestfd);
763 } else {
764 associate_guestfd(guestfd, ret);
765 ret = guestfd;
766 }
a2d1ebaf 767 }
f296c0d1 768 unlock_user(s, arg0, 0);
8e71621f 769 return ret;
35e9a0a8 770 }
3881725c 771 case TARGET_SYS_CLOSE:
f296c0d1 772 GET_ARG(0);
35e9a0a8
PM
773
774 gf = get_guestfd(arg0);
775 if (!gf) {
776 errno = EBADF;
6ed68455 777 return set_swi_errno(env, -1);
35e9a0a8
PM
778 }
779
263eb621 780 ret = guestfd_fns[gf->type].closefn(cpu, gf);
35e9a0a8
PM
781 dealloc_guestfd(arg0);
782 return ret;
3881725c 783 case TARGET_SYS_WRITEC:
78e24848 784 qemu_semihosting_console_outc(env, args);
0dc07721 785 return 0xdeadbeef;
3881725c 786 case TARGET_SYS_WRITE0:
78e24848 787 return qemu_semihosting_console_outs(env, args);
3881725c 788 case TARGET_SYS_WRITE:
f296c0d1
PM
789 GET_ARG(0);
790 GET_ARG(1);
791 GET_ARG(2);
792 len = arg2;
35e9a0a8
PM
793
794 gf = get_guestfd(arg0);
795 if (!gf) {
796 errno = EBADF;
6ed68455 797 return set_swi_errno(env, -1);
35e9a0a8
PM
798 }
799
52c8a163 800 return guestfd_fns[gf->type].writefn(cpu, gf, arg1, len);
3881725c 801 case TARGET_SYS_READ:
f296c0d1
PM
802 GET_ARG(0);
803 GET_ARG(1);
804 GET_ARG(2);
805 len = arg2;
35e9a0a8
PM
806
807 gf = get_guestfd(arg0);
808 if (!gf) {
809 errno = EBADF;
6ed68455 810 return set_swi_errno(env, -1);
35e9a0a8
PM
811 }
812
2c3a09a6 813 return guestfd_fns[gf->type].readfn(cpu, gf, arg1, len);
3881725c 814 case TARGET_SYS_READC:
8de702cb 815 return qemu_semihosting_console_inc(env);
3881725c 816 case TARGET_SYS_ISTTY:
f296c0d1 817 GET_ARG(0);
35e9a0a8
PM
818
819 gf = get_guestfd(arg0);
820 if (!gf) {
821 errno = EBADF;
6ed68455 822 return set_swi_errno(env, -1);
35e9a0a8
PM
823 }
824
0213fa45 825 return guestfd_fns[gf->type].isattyfn(cpu, gf);
3881725c 826 case TARGET_SYS_SEEK:
f296c0d1
PM
827 GET_ARG(0);
828 GET_ARG(1);
35e9a0a8
PM
829
830 gf = get_guestfd(arg0);
831 if (!gf) {
832 errno = EBADF;
6ed68455 833 return set_swi_errno(env, -1);
35e9a0a8
PM
834 }
835
45e88ffc 836 return guestfd_fns[gf->type].seekfn(cpu, gf, arg1);
3881725c 837 case TARGET_SYS_FLEN:
f296c0d1 838 GET_ARG(0);
35e9a0a8
PM
839
840 gf = get_guestfd(arg0);
841 if (!gf) {
842 errno = EBADF;
6ed68455 843 return set_swi_errno(env, -1);
35e9a0a8
PM
844 }
845
1631a7be 846 return guestfd_fns[gf->type].flenfn(cpu, gf);
3881725c 847 case TARGET_SYS_TMPNAM:
a131795f 848 qemu_log_mask(LOG_UNIMP, "%s: SYS_TMPNAM not implemented", __func__);
a4f81979 849 return -1;
3881725c 850 case TARGET_SYS_REMOVE:
f296c0d1
PM
851 GET_ARG(0);
852 GET_ARG(1);
a2d1ebaf 853 if (use_gdb_syscalls()) {
bb19cbc9
PM
854 ret = arm_gdb_syscall(cpu, arm_semi_cb, "unlink,%s",
855 arg0, (int)arg1+1);
a2d1ebaf 856 } else {
f296c0d1
PM
857 s = lock_user_string(arg0);
858 if (!s) {
f7d38cf2 859 errno = EFAULT;
6ed68455 860 return set_swi_errno(env, -1);
f296c0d1 861 }
6ed68455 862 ret = set_swi_errno(env, remove(s));
f296c0d1 863 unlock_user(s, arg0, 0);
a2d1ebaf 864 }
8e71621f 865 return ret;
3881725c 866 case TARGET_SYS_RENAME:
f296c0d1
PM
867 GET_ARG(0);
868 GET_ARG(1);
869 GET_ARG(2);
870 GET_ARG(3);
a2d1ebaf 871 if (use_gdb_syscalls()) {
bb19cbc9
PM
872 return arm_gdb_syscall(cpu, arm_semi_cb, "rename,%s,%s",
873 arg0, (int)arg1+1, arg2, (int)arg3+1);
a2d1ebaf 874 } else {
8e71621f 875 char *s2;
f296c0d1
PM
876 s = lock_user_string(arg0);
877 s2 = lock_user_string(arg2);
f7d38cf2
PM
878 if (!s || !s2) {
879 errno = EFAULT;
6ed68455 880 ret = set_swi_errno(env, -1);
f7d38cf2 881 } else {
6ed68455 882 ret = set_swi_errno(env, rename(s, s2));
f7d38cf2 883 }
579a97f7 884 if (s2)
f296c0d1 885 unlock_user(s2, arg2, 0);
579a97f7 886 if (s)
f296c0d1 887 unlock_user(s, arg0, 0);
8e71621f
PB
888 return ret;
889 }
3881725c 890 case TARGET_SYS_CLOCK:
a4f81979 891 return clock() / (CLOCKS_PER_SEC / 100);
3881725c 892 case TARGET_SYS_TIME:
6ed68455 893 return set_swi_errno(env, time(NULL));
3881725c 894 case TARGET_SYS_SYSTEM:
f296c0d1
PM
895 GET_ARG(0);
896 GET_ARG(1);
a2d1ebaf 897 if (use_gdb_syscalls()) {
bb19cbc9
PM
898 return arm_gdb_syscall(cpu, arm_semi_cb, "system,%s",
899 arg0, (int)arg1+1);
a2d1ebaf 900 } else {
f296c0d1
PM
901 s = lock_user_string(arg0);
902 if (!s) {
f7d38cf2 903 errno = EFAULT;
6ed68455 904 return set_swi_errno(env, -1);
f296c0d1 905 }
6ed68455 906 ret = set_swi_errno(env, system(s));
f296c0d1 907 unlock_user(s, arg0, 0);
a982b531 908 return ret;
a2d1ebaf 909 }
3881725c 910 case TARGET_SYS_ERRNO:
6ed68455 911 return get_swi_errno(env);
3881725c 912 case TARGET_SYS_GET_CMDLINE:
38d0662a 913 {
1c1b40c1
CV
914 /* Build a command-line from the original argv.
915 *
916 * The inputs are:
f296c0d1
PM
917 * * arg0, pointer to a buffer of at least the size
918 * specified in arg1.
919 * * arg1, size of the buffer pointed to by arg0 in
1c1b40c1
CV
920 * bytes.
921 *
922 * The outputs are:
f296c0d1 923 * * arg0, pointer to null-terminated string of the
1c1b40c1 924 * command line.
f296c0d1 925 * * arg1, length of the string pointed to by arg0.
1c1b40c1 926 */
579a97f7 927
1c1b40c1 928 char *output_buffer;
f296c0d1 929 size_t input_size;
1c1b40c1
CV
930 size_t output_size;
931 int status = 0;
f3c2bda2
LI
932#if !defined(CONFIG_USER_ONLY)
933 const char *cmdline;
6ed68455
PM
934#else
935 TaskState *ts = cs->opaque;
f3c2bda2 936#endif
f296c0d1
PM
937 GET_ARG(0);
938 GET_ARG(1);
939 input_size = arg1;
1c1b40c1
CV
940 /* Compute the size of the output string. */
941#if !defined(CONFIG_USER_ONLY)
f3c2bda2
LI
942 cmdline = semihosting_get_cmdline();
943 if (cmdline == NULL) {
944 cmdline = ""; /* Default to an empty line. */
945 }
946 output_size = strlen(cmdline) + 1; /* Count terminating 0. */
1c1b40c1
CV
947#else
948 unsigned int i;
38d0662a 949
1c1b40c1
CV
950 output_size = ts->info->arg_end - ts->info->arg_start;
951 if (!output_size) {
4cb28db9
AB
952 /*
953 * We special-case the "empty command line" case (argc==0).
954 * Just provide the terminating 0.
955 */
1c1b40c1
CV
956 output_size = 1;
957 }
958#endif
38d0662a 959
1c1b40c1 960 if (output_size > input_size) {
4cb28db9 961 /* Not enough space to store command-line arguments. */
f7d38cf2 962 errno = E2BIG;
6ed68455 963 return set_swi_errno(env, -1);
38d0662a 964 }
38d0662a 965
1c1b40c1 966 /* Adjust the command-line length. */
f296c0d1
PM
967 if (SET_ARG(1, output_size - 1)) {
968 /* Couldn't write back to argument block */
f7d38cf2 969 errno = EFAULT;
6ed68455 970 return set_swi_errno(env, -1);
f296c0d1 971 }
38d0662a 972
1c1b40c1 973 /* Lock the buffer on the ARM side. */
f296c0d1 974 output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0);
1c1b40c1 975 if (!output_buffer) {
f7d38cf2 976 errno = EFAULT;
6ed68455 977 return set_swi_errno(env, -1);
1c1b40c1 978 }
38d0662a 979
1c1b40c1
CV
980 /* Copy the command-line arguments. */
981#if !defined(CONFIG_USER_ONLY)
f3c2bda2 982 pstrcpy(output_buffer, output_size, cmdline);
1c1b40c1
CV
983#else
984 if (output_size == 1) {
985 /* Empty command-line. */
986 output_buffer[0] = '\0';
987 goto out;
988 }
2e8785ac 989
1c1b40c1
CV
990 if (copy_from_user(output_buffer, ts->info->arg_start,
991 output_size)) {
f7d38cf2 992 errno = EFAULT;
6ed68455 993 status = set_swi_errno(env, -1);
1c1b40c1 994 goto out;
2e8785ac
WS
995 }
996
1c1b40c1
CV
997 /* Separate arguments by white spaces. */
998 for (i = 0; i < output_size - 1; i++) {
999 if (output_buffer[i] == 0) {
1000 output_buffer[i] = ' ';
1001 }
1002 }
1003 out:
1004#endif
1005 /* Unlock the buffer on the ARM side. */
f296c0d1 1006 unlock_user(output_buffer, arg0, output_size);
2e8785ac 1007
1c1b40c1 1008 return status;
38d0662a 1009 }
3881725c 1010 case TARGET_SYS_HEAPINFO:
a4f81979 1011 {
f5666418 1012 target_ulong retvals[4];
90e26f5a 1013 target_ulong limit;
f5666418 1014 int i;
6ed68455
PM
1015#ifdef CONFIG_USER_ONLY
1016 TaskState *ts = cs->opaque;
1017#endif
f5666418 1018
f296c0d1 1019 GET_ARG(0);
a4f81979 1020
8e71621f 1021#ifdef CONFIG_USER_ONLY
4cb28db9
AB
1022 /*
1023 * Some C libraries assume the heap immediately follows .bss, so
1024 * allocate it using sbrk.
1025 */
a4f81979 1026 if (!ts->heap_limit) {
206ae74a 1027 abi_ulong ret;
a4f81979 1028
53a5960a 1029 ts->heap_base = do_brk(0);
a4f81979
FB
1030 limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
1031 /* Try a big heap, and reduce the size if that fails. */
1032 for (;;) {
53a5960a 1033 ret = do_brk(limit);
206ae74a 1034 if (ret >= limit) {
a4f81979 1035 break;
206ae74a 1036 }
a4f81979
FB
1037 limit = (ts->heap_base >> 1) + (limit >> 1);
1038 }
1039 ts->heap_limit = limit;
1040 }
3b46e624 1041
f5666418
PM
1042 retvals[0] = ts->heap_base;
1043 retvals[1] = ts->heap_limit;
1044 retvals[2] = ts->stack_base;
1045 retvals[3] = 0; /* Stack limit. */
8e71621f
PB
1046#else
1047 limit = ram_size;
8e71621f 1048 /* TODO: Make this use the limit of the loaded application. */
f5666418
PM
1049 retvals[0] = limit / 2;
1050 retvals[1] = limit;
1051 retvals[2] = limit; /* Stack base */
1052 retvals[3] = 0; /* Stack limit. */
8e71621f 1053#endif
f5666418
PM
1054
1055 for (i = 0; i < ARRAY_SIZE(retvals); i++) {
1056 bool fail;
1057
1058 if (is_a64(env)) {
1059 fail = put_user_u64(retvals[i], arg0 + i * 8);
1060 } else {
1061 fail = put_user_u32(retvals[i], arg0 + i * 4);
1062 }
1063
1064 if (fail) {
1065 /* Couldn't write back to argument block */
f7d38cf2 1066 errno = EFAULT;
6ed68455 1067 return set_swi_errno(env, -1);
f5666418
PM
1068 }
1069 }
a4f81979
FB
1070 return 0;
1071 }
3881725c 1072 case TARGET_SYS_EXIT:
22a43bb9
PM
1073 case TARGET_SYS_EXIT_EXTENDED:
1074 if (nr == TARGET_SYS_EXIT_EXTENDED || is_a64(env)) {
4cb28db9 1075 /*
22a43bb9 1076 * The A64 version of SYS_EXIT takes a parameter block,
7446d35e
PM
1077 * so the application-exit type can return a subcode which
1078 * is the exit status code from the application.
22a43bb9
PM
1079 * SYS_EXIT_EXTENDED is an a new-in-v2.0 optional function
1080 * which allows A32/T32 guests to also provide a status code.
7446d35e
PM
1081 */
1082 GET_ARG(0);
1083 GET_ARG(1);
1084
1085 if (arg0 == ADP_Stopped_ApplicationExit) {
1086 ret = arg1;
1087 } else {
1088 ret = 1;
1089 }
1090 } else {
4cb28db9 1091 /*
22a43bb9
PM
1092 * The A32/T32 version of SYS_EXIT specifies only
1093 * Stopped_ApplicationExit as normal exit, but does not
1094 * allow the guest to specify the exit status code.
1095 * Everything else is considered an error.
4cb28db9 1096 */
7446d35e
PM
1097 ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1;
1098 }
1ecc3a2d
LI
1099 gdb_exit(env, ret);
1100 exit(ret);
e9ebfbfc 1101 case TARGET_SYS_SYNCCACHE:
4cb28db9
AB
1102 /*
1103 * Clean the D-cache and invalidate the I-cache for the specified
e9ebfbfc
PM
1104 * virtual address range. This is a nop for us since we don't
1105 * implement caches. This is only present on A64.
1106 */
1107 if (is_a64(env)) {
1108 return 0;
1109 }
1110 /* fall through -- invalid for A32/T32 */
a4f81979
FB
1111 default:
1112 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
90c84c56 1113 cpu_dump_state(cs, stderr, 0);
a4f81979
FB
1114 abort();
1115 }
1116}
This page took 0.947812 seconds and 4 git commands to generate.