xref: /qemu/target/m68k/m68k-semi.c (revision 1073bfd8c0829be06e6ad1cbb4a2f48f8bb594bc)
1a87295e8Spbrook /*
2a87295e8Spbrook  *  m68k/ColdFire Semihosting syscall interface
3a87295e8Spbrook  *
4a87295e8Spbrook  *  Copyright (c) 2005-2007 CodeSourcery.
5a87295e8Spbrook  *
6a87295e8Spbrook  *  This program is free software; you can redistribute it and/or modify
7a87295e8Spbrook  *  it under the terms of the GNU General Public License as published by
8a87295e8Spbrook  *  the Free Software Foundation; either version 2 of the License, or
9a87295e8Spbrook  *  (at your option) any later version.
10a87295e8Spbrook  *
11a87295e8Spbrook  *  This program is distributed in the hope that it will be useful,
12a87295e8Spbrook  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13a87295e8Spbrook  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14a87295e8Spbrook  *  GNU General Public License for more details.
15a87295e8Spbrook  *
16a87295e8Spbrook  *  You should have received a copy of the GNU General Public License
178167ee88SBlue Swirl  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18a87295e8Spbrook  */
19a87295e8Spbrook 
20a87295e8Spbrook #include <sys/types.h>
21a87295e8Spbrook #include <sys/stat.h>
22a87295e8Spbrook #include <errno.h>
23a87295e8Spbrook #include <fcntl.h>
24a87295e8Spbrook #include <unistd.h>
25a87295e8Spbrook #include <stdlib.h>
26a87295e8Spbrook #include <stdio.h>
27a87295e8Spbrook #include <sys/time.h>
28a87295e8Spbrook #include <time.h>
29a87295e8Spbrook 
30a87295e8Spbrook #include "cpu.h"
31a87295e8Spbrook #if defined(CONFIG_USER_ONLY)
32a87295e8Spbrook #include "qemu.h"
33a87295e8Spbrook #define SEMIHOSTING_HEAP_SIZE (128 * 1024 * 1024)
34a87295e8Spbrook #else
3587ecb68bSpbrook #include "qemu-common.h"
3687ecb68bSpbrook #include "gdbstub.h"
37a87295e8Spbrook #include "softmmu-semi.h"
38a87295e8Spbrook #endif
39dc786bc9SJes Sorensen #include "sysemu.h"
40a87295e8Spbrook 
41a87295e8Spbrook #define HOSTED_EXIT  0
42a87295e8Spbrook #define HOSTED_INIT_SIM 1
43a87295e8Spbrook #define HOSTED_OPEN 2
44a87295e8Spbrook #define HOSTED_CLOSE 3
45a87295e8Spbrook #define HOSTED_READ 4
46a87295e8Spbrook #define HOSTED_WRITE 5
47a87295e8Spbrook #define HOSTED_LSEEK 6
48a87295e8Spbrook #define HOSTED_RENAME 7
49a87295e8Spbrook #define HOSTED_UNLINK 8
50a87295e8Spbrook #define HOSTED_STAT 9
51a87295e8Spbrook #define HOSTED_FSTAT 10
52a87295e8Spbrook #define HOSTED_GETTIMEOFDAY 11
53a87295e8Spbrook #define HOSTED_ISATTY 12
54a87295e8Spbrook #define HOSTED_SYSTEM 13
55a87295e8Spbrook 
56c227f099SAnthony Liguori typedef uint32_t gdb_mode_t;
57c227f099SAnthony Liguori typedef uint32_t gdb_time_t;
58a87295e8Spbrook 
59a87295e8Spbrook struct m68k_gdb_stat {
60a87295e8Spbrook   uint32_t    gdb_st_dev;     /* device */
61a87295e8Spbrook   uint32_t    gdb_st_ino;     /* inode */
62c227f099SAnthony Liguori   gdb_mode_t  gdb_st_mode;    /* protection */
63a87295e8Spbrook   uint32_t    gdb_st_nlink;   /* number of hard links */
64a87295e8Spbrook   uint32_t    gdb_st_uid;     /* user ID of owner */
65a87295e8Spbrook   uint32_t    gdb_st_gid;     /* group ID of owner */
66a87295e8Spbrook   uint32_t    gdb_st_rdev;    /* device type (if inode device) */
67a87295e8Spbrook   uint64_t    gdb_st_size;    /* total size, in bytes */
68a87295e8Spbrook   uint64_t    gdb_st_blksize; /* blocksize for filesystem I/O */
69a87295e8Spbrook   uint64_t    gdb_st_blocks;  /* number of blocks allocated */
70c227f099SAnthony Liguori   gdb_time_t  gdb_st_atime;   /* time of last access */
71c227f099SAnthony Liguori   gdb_time_t  gdb_st_mtime;   /* time of last modification */
72c227f099SAnthony Liguori   gdb_time_t  gdb_st_ctime;   /* time of last change */
73541dc0d4SStefan Weil } QEMU_PACKED;
74a87295e8Spbrook 
75a87295e8Spbrook struct gdb_timeval {
76c227f099SAnthony Liguori   gdb_time_t tv_sec;  /* second */
77a87295e8Spbrook   uint64_t tv_usec;   /* microsecond */
78541dc0d4SStefan Weil } QEMU_PACKED;
79a87295e8Spbrook 
80a87295e8Spbrook #define GDB_O_RDONLY   0x0
81a87295e8Spbrook #define GDB_O_WRONLY   0x1
82a87295e8Spbrook #define GDB_O_RDWR     0x2
83a87295e8Spbrook #define GDB_O_APPEND   0x8
84a87295e8Spbrook #define GDB_O_CREAT  0x200
85a87295e8Spbrook #define GDB_O_TRUNC  0x400
86a87295e8Spbrook #define GDB_O_EXCL   0x800
87a87295e8Spbrook 
88a87295e8Spbrook static int translate_openflags(int flags)
89a87295e8Spbrook {
90a87295e8Spbrook     int hf;
91a87295e8Spbrook 
92a87295e8Spbrook     if (flags & GDB_O_WRONLY)
93a87295e8Spbrook         hf = O_WRONLY;
94a87295e8Spbrook     else if (flags & GDB_O_RDWR)
95a87295e8Spbrook         hf = O_RDWR;
96a87295e8Spbrook     else
97a87295e8Spbrook         hf = O_RDONLY;
98a87295e8Spbrook 
99a87295e8Spbrook     if (flags & GDB_O_APPEND) hf |= O_APPEND;
100a87295e8Spbrook     if (flags & GDB_O_CREAT) hf |= O_CREAT;
101a87295e8Spbrook     if (flags & GDB_O_TRUNC) hf |= O_TRUNC;
102a87295e8Spbrook     if (flags & GDB_O_EXCL) hf |= O_EXCL;
103a87295e8Spbrook 
104a87295e8Spbrook     return hf;
105a87295e8Spbrook }
106a87295e8Spbrook 
10771fc85e8SAndreas Färber static void translate_stat(CPUM68KState *env, target_ulong addr, struct stat *s)
108a87295e8Spbrook {
109a87295e8Spbrook     struct m68k_gdb_stat *p;
110a87295e8Spbrook 
111579a97f7Sbellard     if (!(p = lock_user(VERIFY_WRITE, addr, sizeof(struct m68k_gdb_stat), 0)))
112579a97f7Sbellard         /* FIXME - should this return an error code? */
113579a97f7Sbellard         return;
114a87295e8Spbrook     p->gdb_st_dev = cpu_to_be32(s->st_dev);
115a87295e8Spbrook     p->gdb_st_ino = cpu_to_be32(s->st_ino);
116a87295e8Spbrook     p->gdb_st_mode = cpu_to_be32(s->st_mode);
117a87295e8Spbrook     p->gdb_st_nlink = cpu_to_be32(s->st_nlink);
118a87295e8Spbrook     p->gdb_st_uid = cpu_to_be32(s->st_uid);
119a87295e8Spbrook     p->gdb_st_gid = cpu_to_be32(s->st_gid);
120a87295e8Spbrook     p->gdb_st_rdev = cpu_to_be32(s->st_rdev);
121a87295e8Spbrook     p->gdb_st_size = cpu_to_be64(s->st_size);
12229b3a662Spbrook #ifdef _WIN32
12329b3a662Spbrook     /* Windows stat is missing some fields.  */
12429b3a662Spbrook     p->gdb_st_blksize = 0;
12529b3a662Spbrook     p->gdb_st_blocks = 0;
12629b3a662Spbrook #else
127a87295e8Spbrook     p->gdb_st_blksize = cpu_to_be64(s->st_blksize);
128a87295e8Spbrook     p->gdb_st_blocks = cpu_to_be64(s->st_blocks);
12929b3a662Spbrook #endif
130a87295e8Spbrook     p->gdb_st_atime = cpu_to_be32(s->st_atime);
131a87295e8Spbrook     p->gdb_st_mtime = cpu_to_be32(s->st_mtime);
132a87295e8Spbrook     p->gdb_st_ctime = cpu_to_be32(s->st_ctime);
133a87295e8Spbrook     unlock_user(p, addr, sizeof(struct m68k_gdb_stat));
134a87295e8Spbrook }
135a87295e8Spbrook 
1361073bfd8SPeter Maydell static void m68k_semi_return_u32(CPUM68KState *env, uint32_t ret, uint32_t err)
1371073bfd8SPeter Maydell {
1381073bfd8SPeter Maydell     target_ulong args = env->dregs[1];
1391073bfd8SPeter Maydell     if (put_user_u32(ret, args) ||
1401073bfd8SPeter Maydell         put_user_u32(err, args + 4)) {
1411073bfd8SPeter Maydell         /* The m68k semihosting ABI does not provide any way to report this
1421073bfd8SPeter Maydell          * error to the guest, so the best we can do is log it in qemu.
1431073bfd8SPeter Maydell          * It is always a guest error not to pass us a valid argument block.
1441073bfd8SPeter Maydell          */
1451073bfd8SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value "
1461073bfd8SPeter Maydell                       "discarded because argument block not writable\n");
1471073bfd8SPeter Maydell     }
1481073bfd8SPeter Maydell }
1491073bfd8SPeter Maydell 
1501073bfd8SPeter Maydell static void m68k_semi_return_u64(CPUM68KState *env, uint64_t ret, uint32_t err)
1511073bfd8SPeter Maydell {
1521073bfd8SPeter Maydell     target_ulong args = env->dregs[1];
1531073bfd8SPeter Maydell     if (put_user_u32(ret >> 32, args) ||
1541073bfd8SPeter Maydell         put_user_u32(ret, args + 4) ||
1551073bfd8SPeter Maydell         put_user_u32(err, args + 8)) {
1561073bfd8SPeter Maydell         /* No way to report this via m68k semihosting ABI; just log it */
1571073bfd8SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value "
1581073bfd8SPeter Maydell                       "discarded because argument block not writable\n");
1591073bfd8SPeter Maydell     }
1601073bfd8SPeter Maydell }
1611073bfd8SPeter Maydell 
162a87295e8Spbrook static int m68k_semi_is_fseek;
163a87295e8Spbrook 
16471fc85e8SAndreas Färber static void m68k_semi_cb(CPUM68KState *env, target_ulong ret, target_ulong err)
165a87295e8Spbrook {
166a87295e8Spbrook     if (m68k_semi_is_fseek) {
167a87295e8Spbrook         /* FIXME: We've already lost the high bits of the fseek
168a87295e8Spbrook            return value.  */
1691073bfd8SPeter Maydell         m68k_semi_return_u64(env, ret, err);
170a87295e8Spbrook         m68k_semi_is_fseek = 0;
1711073bfd8SPeter Maydell     } else {
1721073bfd8SPeter Maydell         m68k_semi_return_u32(env, ret, err);
173a87295e8Spbrook     }
174a87295e8Spbrook }
175a87295e8Spbrook 
1767ba6c104SPeter Maydell /* Read the input value from the argument block; fail the semihosting
1777ba6c104SPeter Maydell  * call if the memory read fails.
1787ba6c104SPeter Maydell  */
1797ba6c104SPeter Maydell #define GET_ARG(n) do {                                 \
1807ba6c104SPeter Maydell     if (get_user_ual(arg ## n, args + (n) * 4)) {       \
1817ba6c104SPeter Maydell         result = -1;                                    \
1827ba6c104SPeter Maydell         errno = EFAULT;                                 \
1837ba6c104SPeter Maydell         goto failed;                                    \
1847ba6c104SPeter Maydell     }                                                   \
1857ba6c104SPeter Maydell } while (0)
1867ba6c104SPeter Maydell 
187a87295e8Spbrook void do_m68k_semihosting(CPUM68KState *env, int nr)
188a87295e8Spbrook {
189a87295e8Spbrook     uint32_t args;
1907ba6c104SPeter Maydell     target_ulong arg0, arg1, arg2, arg3;
191a87295e8Spbrook     void *p;
192a87295e8Spbrook     void *q;
193a87295e8Spbrook     uint32_t len;
194a87295e8Spbrook     uint32_t result;
195a87295e8Spbrook 
196a87295e8Spbrook     args = env->dregs[1];
197a87295e8Spbrook     switch (nr) {
198a87295e8Spbrook     case HOSTED_EXIT:
1990e1c9c54SPaul Brook         gdb_exit(env, env->dregs[0]);
200a87295e8Spbrook         exit(env->dregs[0]);
201a87295e8Spbrook     case HOSTED_OPEN:
2027ba6c104SPeter Maydell         GET_ARG(0);
2037ba6c104SPeter Maydell         GET_ARG(1);
2047ba6c104SPeter Maydell         GET_ARG(2);
2057ba6c104SPeter Maydell         GET_ARG(3);
206a87295e8Spbrook         if (use_gdb_syscalls()) {
2077ba6c104SPeter Maydell             gdb_do_syscall(m68k_semi_cb, "open,%s,%x,%x", arg0, (int)arg1,
2087ba6c104SPeter Maydell                            arg2, arg3);
209a87295e8Spbrook             return;
210a87295e8Spbrook         } else {
2117ba6c104SPeter Maydell             p = lock_user_string(arg0);
2127ba6c104SPeter Maydell             if (!p) {
213579a97f7Sbellard                 /* FIXME - check error code? */
214579a97f7Sbellard                 result = -1;
215579a97f7Sbellard             } else {
2167ba6c104SPeter Maydell                 result = open(p, translate_openflags(arg2), arg3);
2177ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
218a87295e8Spbrook             }
219579a97f7Sbellard         }
220a87295e8Spbrook         break;
221a87295e8Spbrook     case HOSTED_CLOSE:
222a87295e8Spbrook         {
223a87295e8Spbrook             /* Ignore attempts to close stdin/out/err.  */
2247ba6c104SPeter Maydell             GET_ARG(0);
2257ba6c104SPeter Maydell             int fd = arg0;
226a87295e8Spbrook             if (fd > 2) {
227a87295e8Spbrook                 if (use_gdb_syscalls()) {
2287ba6c104SPeter Maydell                     gdb_do_syscall(m68k_semi_cb, "close,%x", arg0);
229a87295e8Spbrook                     return;
230a87295e8Spbrook                 } else {
231a87295e8Spbrook                     result = close(fd);
232a87295e8Spbrook                 }
233a87295e8Spbrook             } else {
234a87295e8Spbrook                 result = 0;
235a87295e8Spbrook             }
236a87295e8Spbrook             break;
237a87295e8Spbrook         }
238a87295e8Spbrook     case HOSTED_READ:
2397ba6c104SPeter Maydell         GET_ARG(0);
2407ba6c104SPeter Maydell         GET_ARG(1);
2417ba6c104SPeter Maydell         GET_ARG(2);
2427ba6c104SPeter Maydell         len = arg2;
243a87295e8Spbrook         if (use_gdb_syscalls()) {
244a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "read,%x,%x,%x",
2457ba6c104SPeter Maydell                            arg0, arg1, len);
246a87295e8Spbrook             return;
247a87295e8Spbrook         } else {
2487ba6c104SPeter Maydell             p = lock_user(VERIFY_WRITE, arg1, len, 0);
2497ba6c104SPeter Maydell             if (!p) {
250579a97f7Sbellard                 /* FIXME - check error code? */
251579a97f7Sbellard                 result = -1;
252579a97f7Sbellard             } else {
2537ba6c104SPeter Maydell                 result = read(arg0, p, len);
2547ba6c104SPeter Maydell                 unlock_user(p, arg1, len);
255a87295e8Spbrook             }
256579a97f7Sbellard         }
257a87295e8Spbrook         break;
258a87295e8Spbrook     case HOSTED_WRITE:
2597ba6c104SPeter Maydell         GET_ARG(0);
2607ba6c104SPeter Maydell         GET_ARG(1);
2617ba6c104SPeter Maydell         GET_ARG(2);
2627ba6c104SPeter Maydell         len = arg2;
263a87295e8Spbrook         if (use_gdb_syscalls()) {
264a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "write,%x,%x,%x",
2657ba6c104SPeter Maydell                            arg0, arg1, len);
266a87295e8Spbrook             return;
267a87295e8Spbrook         } else {
2687ba6c104SPeter Maydell             p = lock_user(VERIFY_READ, arg1, len, 1);
2697ba6c104SPeter Maydell             if (!p) {
270579a97f7Sbellard                 /* FIXME - check error code? */
271579a97f7Sbellard                 result = -1;
272579a97f7Sbellard             } else {
2737ba6c104SPeter Maydell                 result = write(arg0, p, len);
2747ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
275a87295e8Spbrook             }
276579a97f7Sbellard         }
277a87295e8Spbrook         break;
278a87295e8Spbrook     case HOSTED_LSEEK:
279a87295e8Spbrook         {
280a87295e8Spbrook             uint64_t off;
2817ba6c104SPeter Maydell             GET_ARG(0);
2827ba6c104SPeter Maydell             GET_ARG(1);
2837ba6c104SPeter Maydell             GET_ARG(2);
2847ba6c104SPeter Maydell             GET_ARG(3);
2857ba6c104SPeter Maydell             off = (uint32_t)arg2 | ((uint64_t)arg1 << 32);
286a87295e8Spbrook             if (use_gdb_syscalls()) {
287a87295e8Spbrook                 m68k_semi_is_fseek = 1;
288a87295e8Spbrook                 gdb_do_syscall(m68k_semi_cb, "fseek,%x,%lx,%x",
2897ba6c104SPeter Maydell                                arg0, off, arg3);
290a87295e8Spbrook             } else {
2917ba6c104SPeter Maydell                 off = lseek(arg0, off, arg3);
2921073bfd8SPeter Maydell                 m68k_semi_return_u64(env, off, errno);
293a87295e8Spbrook             }
294a87295e8Spbrook             return;
295a87295e8Spbrook         }
296a87295e8Spbrook     case HOSTED_RENAME:
2977ba6c104SPeter Maydell         GET_ARG(0);
2987ba6c104SPeter Maydell         GET_ARG(1);
2997ba6c104SPeter Maydell         GET_ARG(2);
3007ba6c104SPeter Maydell         GET_ARG(3);
301a87295e8Spbrook         if (use_gdb_syscalls()) {
302a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "rename,%s,%s",
3037ba6c104SPeter Maydell                            arg0, (int)arg1, arg2, (int)arg3);
304a87295e8Spbrook             return;
305a87295e8Spbrook         } else {
3067ba6c104SPeter Maydell             p = lock_user_string(arg0);
3077ba6c104SPeter Maydell             q = lock_user_string(arg2);
308579a97f7Sbellard             if (!p || !q) {
309579a97f7Sbellard                 /* FIXME - check error code? */
310579a97f7Sbellard                 result = -1;
311579a97f7Sbellard             } else {
312a87295e8Spbrook                 result = rename(p, q);
313579a97f7Sbellard             }
3147ba6c104SPeter Maydell             unlock_user(p, arg0, 0);
3157ba6c104SPeter Maydell             unlock_user(q, arg2, 0);
316a87295e8Spbrook         }
317a87295e8Spbrook         break;
318a87295e8Spbrook     case HOSTED_UNLINK:
3197ba6c104SPeter Maydell         GET_ARG(0);
3207ba6c104SPeter Maydell         GET_ARG(1);
321a87295e8Spbrook         if (use_gdb_syscalls()) {
322a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "unlink,%s",
3237ba6c104SPeter Maydell                            arg0, (int)arg1);
324a87295e8Spbrook             return;
325a87295e8Spbrook         } else {
3267ba6c104SPeter Maydell             p = lock_user_string(arg0);
3277ba6c104SPeter Maydell             if (!p) {
328579a97f7Sbellard                 /* FIXME - check error code? */
329579a97f7Sbellard                 result = -1;
330579a97f7Sbellard             } else {
331a87295e8Spbrook                 result = unlink(p);
3327ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
333a87295e8Spbrook             }
334579a97f7Sbellard         }
335a87295e8Spbrook         break;
336a87295e8Spbrook     case HOSTED_STAT:
3377ba6c104SPeter Maydell         GET_ARG(0);
3387ba6c104SPeter Maydell         GET_ARG(1);
3397ba6c104SPeter Maydell         GET_ARG(2);
340a87295e8Spbrook         if (use_gdb_syscalls()) {
341a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "stat,%s,%x",
3427ba6c104SPeter Maydell                            arg0, (int)arg1, arg2);
343a87295e8Spbrook             return;
344a87295e8Spbrook         } else {
345a87295e8Spbrook             struct stat s;
3467ba6c104SPeter Maydell             p = lock_user_string(arg0);
3477ba6c104SPeter Maydell             if (!p) {
348579a97f7Sbellard                 /* FIXME - check error code? */
349579a97f7Sbellard                 result = -1;
350579a97f7Sbellard             } else {
351a87295e8Spbrook                 result = stat(p, &s);
3527ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
353579a97f7Sbellard             }
354a87295e8Spbrook             if (result == 0) {
3557ba6c104SPeter Maydell                 translate_stat(env, arg2, &s);
356a87295e8Spbrook             }
357a87295e8Spbrook         }
358a87295e8Spbrook         break;
359a87295e8Spbrook     case HOSTED_FSTAT:
3607ba6c104SPeter Maydell         GET_ARG(0);
3617ba6c104SPeter Maydell         GET_ARG(1);
362a87295e8Spbrook         if (use_gdb_syscalls()) {
363a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "fstat,%x,%x",
3647ba6c104SPeter Maydell                            arg0, arg1);
365a87295e8Spbrook             return;
366a87295e8Spbrook         } else {
367a87295e8Spbrook             struct stat s;
3687ba6c104SPeter Maydell             result = fstat(arg0, &s);
369a87295e8Spbrook             if (result == 0) {
3707ba6c104SPeter Maydell                 translate_stat(env, arg1, &s);
371a87295e8Spbrook             }
372a87295e8Spbrook         }
373a87295e8Spbrook         break;
374a87295e8Spbrook     case HOSTED_GETTIMEOFDAY:
3757ba6c104SPeter Maydell         GET_ARG(0);
3767ba6c104SPeter Maydell         GET_ARG(1);
377a87295e8Spbrook         if (use_gdb_syscalls()) {
378a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "gettimeofday,%x,%x",
3797ba6c104SPeter Maydell                            arg0, arg1);
380a87295e8Spbrook             return;
381a87295e8Spbrook         } else {
38229b3a662Spbrook             qemu_timeval tv;
383a87295e8Spbrook             struct gdb_timeval *p;
38429b3a662Spbrook             result = qemu_gettimeofday(&tv);
385a87295e8Spbrook             if (result != 0) {
386579a97f7Sbellard                 if (!(p = lock_user(VERIFY_WRITE,
3877ba6c104SPeter Maydell                                     arg0, sizeof(struct gdb_timeval), 0))) {
388579a97f7Sbellard                     /* FIXME - check error code? */
389579a97f7Sbellard                     result = -1;
390579a97f7Sbellard                 } else {
391a87295e8Spbrook                     p->tv_sec = cpu_to_be32(tv.tv_sec);
392a87295e8Spbrook                     p->tv_usec = cpu_to_be64(tv.tv_usec);
3937ba6c104SPeter Maydell                     unlock_user(p, arg0, sizeof(struct gdb_timeval));
394a87295e8Spbrook                 }
395a87295e8Spbrook             }
396579a97f7Sbellard         }
397a87295e8Spbrook         break;
398a87295e8Spbrook     case HOSTED_ISATTY:
3997ba6c104SPeter Maydell         GET_ARG(0);
400a87295e8Spbrook         if (use_gdb_syscalls()) {
4017ba6c104SPeter Maydell             gdb_do_syscall(m68k_semi_cb, "isatty,%x", arg0);
402a87295e8Spbrook             return;
403a87295e8Spbrook         } else {
4047ba6c104SPeter Maydell             result = isatty(arg0);
405a87295e8Spbrook         }
406a87295e8Spbrook         break;
407a87295e8Spbrook     case HOSTED_SYSTEM:
4087ba6c104SPeter Maydell         GET_ARG(0);
4097ba6c104SPeter Maydell         GET_ARG(1);
410a87295e8Spbrook         if (use_gdb_syscalls()) {
411a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "system,%s",
4127ba6c104SPeter Maydell                            arg0, (int)arg1);
413a87295e8Spbrook             return;
414a87295e8Spbrook         } else {
4157ba6c104SPeter Maydell             p = lock_user_string(arg0);
4167ba6c104SPeter Maydell             if (!p) {
417579a97f7Sbellard                 /* FIXME - check error code? */
418579a97f7Sbellard                 result = -1;
419579a97f7Sbellard             } else {
420a87295e8Spbrook                 result = system(p);
4217ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
422a87295e8Spbrook             }
423579a97f7Sbellard         }
424a87295e8Spbrook         break;
425a87295e8Spbrook     case HOSTED_INIT_SIM:
426a87295e8Spbrook #if defined(CONFIG_USER_ONLY)
427a87295e8Spbrook         {
428a87295e8Spbrook         TaskState *ts = env->opaque;
429a87295e8Spbrook         /* Allocate the heap using sbrk.  */
430a87295e8Spbrook         if (!ts->heap_limit) {
4315382a012SPeter Maydell             abi_ulong ret;
432a87295e8Spbrook             uint32_t size;
433a87295e8Spbrook             uint32_t base;
434a87295e8Spbrook 
435a87295e8Spbrook             base = do_brk(0);
436a87295e8Spbrook             size = SEMIHOSTING_HEAP_SIZE;
437a87295e8Spbrook             /* Try a big heap, and reduce the size if that fails.  */
438a87295e8Spbrook             for (;;) {
439a87295e8Spbrook                 ret = do_brk(base + size);
4405382a012SPeter Maydell                 if (ret >= (base + size)) {
441a87295e8Spbrook                     break;
4425382a012SPeter Maydell                 }
443a87295e8Spbrook                 size >>= 1;
444a87295e8Spbrook             }
445a87295e8Spbrook             ts->heap_limit = base + size;
446a87295e8Spbrook         }
447a87295e8Spbrook         /* This call may happen before we have writable memory, so return
448a87295e8Spbrook            values directly in registers.  */
449a87295e8Spbrook         env->dregs[1] = ts->heap_limit;
450a87295e8Spbrook         env->aregs[7] = ts->stack_base;
451a87295e8Spbrook         }
452a87295e8Spbrook #else
453a87295e8Spbrook         /* FIXME: This is wrong for boards where RAM does not start at
454a87295e8Spbrook            address zero.  */
455a87295e8Spbrook         env->dregs[1] = ram_size;
456a87295e8Spbrook         env->aregs[7] = ram_size;
457a87295e8Spbrook #endif
458a87295e8Spbrook         return;
459a87295e8Spbrook     default:
460a87295e8Spbrook         cpu_abort(env, "Unsupported semihosting syscall %d\n", nr);
461a87295e8Spbrook         result = 0;
462a87295e8Spbrook     }
4637ba6c104SPeter Maydell failed:
4641073bfd8SPeter Maydell     m68k_semi_return_u32(env, result, errno);
465a87295e8Spbrook }
466