xref: /qemu/target/m68k/m68k-semi.c (revision a47dddd7348d3e75ad650ef5e2ca9c3b13a600ac)
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"
36022c62cbSPaolo Bonzini #include "exec/gdbstub.h"
37022c62cbSPaolo Bonzini #include "exec/softmmu-semi.h"
38a87295e8Spbrook #endif
399c17d615SPaolo Bonzini #include "sysemu/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 
1649e0c5422SAndreas Färber static void m68k_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
165a87295e8Spbrook {
1669e0c5422SAndreas Färber     M68kCPU *cpu = M68K_CPU(cs);
1679e0c5422SAndreas Färber     CPUM68KState *env = &cpu->env;
1689e0c5422SAndreas Färber 
169a87295e8Spbrook     if (m68k_semi_is_fseek) {
170a87295e8Spbrook         /* FIXME: We've already lost the high bits of the fseek
171a87295e8Spbrook            return value.  */
1721073bfd8SPeter Maydell         m68k_semi_return_u64(env, ret, err);
173a87295e8Spbrook         m68k_semi_is_fseek = 0;
1741073bfd8SPeter Maydell     } else {
1751073bfd8SPeter Maydell         m68k_semi_return_u32(env, ret, err);
176a87295e8Spbrook     }
177a87295e8Spbrook }
178a87295e8Spbrook 
1797ba6c104SPeter Maydell /* Read the input value from the argument block; fail the semihosting
1807ba6c104SPeter Maydell  * call if the memory read fails.
1817ba6c104SPeter Maydell  */
1827ba6c104SPeter Maydell #define GET_ARG(n) do {                                 \
1837ba6c104SPeter Maydell     if (get_user_ual(arg ## n, args + (n) * 4)) {       \
1847ba6c104SPeter Maydell         result = -1;                                    \
1857ba6c104SPeter Maydell         errno = EFAULT;                                 \
1867ba6c104SPeter Maydell         goto failed;                                    \
1877ba6c104SPeter Maydell     }                                                   \
1887ba6c104SPeter Maydell } while (0)
1897ba6c104SPeter Maydell 
190a87295e8Spbrook void do_m68k_semihosting(CPUM68KState *env, int nr)
191a87295e8Spbrook {
192a87295e8Spbrook     uint32_t args;
1937ba6c104SPeter Maydell     target_ulong arg0, arg1, arg2, arg3;
194a87295e8Spbrook     void *p;
195a87295e8Spbrook     void *q;
196a87295e8Spbrook     uint32_t len;
197a87295e8Spbrook     uint32_t result;
198a87295e8Spbrook 
199a87295e8Spbrook     args = env->dregs[1];
200a87295e8Spbrook     switch (nr) {
201a87295e8Spbrook     case HOSTED_EXIT:
2020e1c9c54SPaul Brook         gdb_exit(env, env->dregs[0]);
203a87295e8Spbrook         exit(env->dregs[0]);
204a87295e8Spbrook     case HOSTED_OPEN:
2057ba6c104SPeter Maydell         GET_ARG(0);
2067ba6c104SPeter Maydell         GET_ARG(1);
2077ba6c104SPeter Maydell         GET_ARG(2);
2087ba6c104SPeter Maydell         GET_ARG(3);
209a87295e8Spbrook         if (use_gdb_syscalls()) {
2107ba6c104SPeter Maydell             gdb_do_syscall(m68k_semi_cb, "open,%s,%x,%x", arg0, (int)arg1,
2117ba6c104SPeter Maydell                            arg2, arg3);
212a87295e8Spbrook             return;
213a87295e8Spbrook         } else {
2147ba6c104SPeter Maydell             p = lock_user_string(arg0);
2157ba6c104SPeter Maydell             if (!p) {
216579a97f7Sbellard                 /* FIXME - check error code? */
217579a97f7Sbellard                 result = -1;
218579a97f7Sbellard             } else {
2197ba6c104SPeter Maydell                 result = open(p, translate_openflags(arg2), arg3);
2207ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
221a87295e8Spbrook             }
222579a97f7Sbellard         }
223a87295e8Spbrook         break;
224a87295e8Spbrook     case HOSTED_CLOSE:
225a87295e8Spbrook         {
226a87295e8Spbrook             /* Ignore attempts to close stdin/out/err.  */
2277ba6c104SPeter Maydell             GET_ARG(0);
2287ba6c104SPeter Maydell             int fd = arg0;
229a87295e8Spbrook             if (fd > 2) {
230a87295e8Spbrook                 if (use_gdb_syscalls()) {
2317ba6c104SPeter Maydell                     gdb_do_syscall(m68k_semi_cb, "close,%x", arg0);
232a87295e8Spbrook                     return;
233a87295e8Spbrook                 } else {
234a87295e8Spbrook                     result = close(fd);
235a87295e8Spbrook                 }
236a87295e8Spbrook             } else {
237a87295e8Spbrook                 result = 0;
238a87295e8Spbrook             }
239a87295e8Spbrook             break;
240a87295e8Spbrook         }
241a87295e8Spbrook     case HOSTED_READ:
2427ba6c104SPeter Maydell         GET_ARG(0);
2437ba6c104SPeter Maydell         GET_ARG(1);
2447ba6c104SPeter Maydell         GET_ARG(2);
2457ba6c104SPeter Maydell         len = arg2;
246a87295e8Spbrook         if (use_gdb_syscalls()) {
247a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "read,%x,%x,%x",
2487ba6c104SPeter Maydell                            arg0, arg1, len);
249a87295e8Spbrook             return;
250a87295e8Spbrook         } else {
2517ba6c104SPeter Maydell             p = lock_user(VERIFY_WRITE, arg1, len, 0);
2527ba6c104SPeter Maydell             if (!p) {
253579a97f7Sbellard                 /* FIXME - check error code? */
254579a97f7Sbellard                 result = -1;
255579a97f7Sbellard             } else {
2567ba6c104SPeter Maydell                 result = read(arg0, p, len);
2577ba6c104SPeter Maydell                 unlock_user(p, arg1, len);
258a87295e8Spbrook             }
259579a97f7Sbellard         }
260a87295e8Spbrook         break;
261a87295e8Spbrook     case HOSTED_WRITE:
2627ba6c104SPeter Maydell         GET_ARG(0);
2637ba6c104SPeter Maydell         GET_ARG(1);
2647ba6c104SPeter Maydell         GET_ARG(2);
2657ba6c104SPeter Maydell         len = arg2;
266a87295e8Spbrook         if (use_gdb_syscalls()) {
267a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "write,%x,%x,%x",
2687ba6c104SPeter Maydell                            arg0, arg1, len);
269a87295e8Spbrook             return;
270a87295e8Spbrook         } else {
2717ba6c104SPeter Maydell             p = lock_user(VERIFY_READ, arg1, len, 1);
2727ba6c104SPeter Maydell             if (!p) {
273579a97f7Sbellard                 /* FIXME - check error code? */
274579a97f7Sbellard                 result = -1;
275579a97f7Sbellard             } else {
2767ba6c104SPeter Maydell                 result = write(arg0, p, len);
2777ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
278a87295e8Spbrook             }
279579a97f7Sbellard         }
280a87295e8Spbrook         break;
281a87295e8Spbrook     case HOSTED_LSEEK:
282a87295e8Spbrook         {
283a87295e8Spbrook             uint64_t off;
2847ba6c104SPeter Maydell             GET_ARG(0);
2857ba6c104SPeter Maydell             GET_ARG(1);
2867ba6c104SPeter Maydell             GET_ARG(2);
2877ba6c104SPeter Maydell             GET_ARG(3);
2887ba6c104SPeter Maydell             off = (uint32_t)arg2 | ((uint64_t)arg1 << 32);
289a87295e8Spbrook             if (use_gdb_syscalls()) {
290a87295e8Spbrook                 m68k_semi_is_fseek = 1;
291a87295e8Spbrook                 gdb_do_syscall(m68k_semi_cb, "fseek,%x,%lx,%x",
2927ba6c104SPeter Maydell                                arg0, off, arg3);
293a87295e8Spbrook             } else {
2947ba6c104SPeter Maydell                 off = lseek(arg0, off, arg3);
2951073bfd8SPeter Maydell                 m68k_semi_return_u64(env, off, errno);
296a87295e8Spbrook             }
297a87295e8Spbrook             return;
298a87295e8Spbrook         }
299a87295e8Spbrook     case HOSTED_RENAME:
3007ba6c104SPeter Maydell         GET_ARG(0);
3017ba6c104SPeter Maydell         GET_ARG(1);
3027ba6c104SPeter Maydell         GET_ARG(2);
3037ba6c104SPeter Maydell         GET_ARG(3);
304a87295e8Spbrook         if (use_gdb_syscalls()) {
305a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "rename,%s,%s",
3067ba6c104SPeter Maydell                            arg0, (int)arg1, arg2, (int)arg3);
307a87295e8Spbrook             return;
308a87295e8Spbrook         } else {
3097ba6c104SPeter Maydell             p = lock_user_string(arg0);
3107ba6c104SPeter Maydell             q = lock_user_string(arg2);
311579a97f7Sbellard             if (!p || !q) {
312579a97f7Sbellard                 /* FIXME - check error code? */
313579a97f7Sbellard                 result = -1;
314579a97f7Sbellard             } else {
315a87295e8Spbrook                 result = rename(p, q);
316579a97f7Sbellard             }
3177ba6c104SPeter Maydell             unlock_user(p, arg0, 0);
3187ba6c104SPeter Maydell             unlock_user(q, arg2, 0);
319a87295e8Spbrook         }
320a87295e8Spbrook         break;
321a87295e8Spbrook     case HOSTED_UNLINK:
3227ba6c104SPeter Maydell         GET_ARG(0);
3237ba6c104SPeter Maydell         GET_ARG(1);
324a87295e8Spbrook         if (use_gdb_syscalls()) {
325a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "unlink,%s",
3267ba6c104SPeter Maydell                            arg0, (int)arg1);
327a87295e8Spbrook             return;
328a87295e8Spbrook         } else {
3297ba6c104SPeter Maydell             p = lock_user_string(arg0);
3307ba6c104SPeter Maydell             if (!p) {
331579a97f7Sbellard                 /* FIXME - check error code? */
332579a97f7Sbellard                 result = -1;
333579a97f7Sbellard             } else {
334a87295e8Spbrook                 result = unlink(p);
3357ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
336a87295e8Spbrook             }
337579a97f7Sbellard         }
338a87295e8Spbrook         break;
339a87295e8Spbrook     case HOSTED_STAT:
3407ba6c104SPeter Maydell         GET_ARG(0);
3417ba6c104SPeter Maydell         GET_ARG(1);
3427ba6c104SPeter Maydell         GET_ARG(2);
343a87295e8Spbrook         if (use_gdb_syscalls()) {
344a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "stat,%s,%x",
3457ba6c104SPeter Maydell                            arg0, (int)arg1, arg2);
346a87295e8Spbrook             return;
347a87295e8Spbrook         } else {
348a87295e8Spbrook             struct stat s;
3497ba6c104SPeter Maydell             p = lock_user_string(arg0);
3507ba6c104SPeter Maydell             if (!p) {
351579a97f7Sbellard                 /* FIXME - check error code? */
352579a97f7Sbellard                 result = -1;
353579a97f7Sbellard             } else {
354a87295e8Spbrook                 result = stat(p, &s);
3557ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
356579a97f7Sbellard             }
357a87295e8Spbrook             if (result == 0) {
3587ba6c104SPeter Maydell                 translate_stat(env, arg2, &s);
359a87295e8Spbrook             }
360a87295e8Spbrook         }
361a87295e8Spbrook         break;
362a87295e8Spbrook     case HOSTED_FSTAT:
3637ba6c104SPeter Maydell         GET_ARG(0);
3647ba6c104SPeter Maydell         GET_ARG(1);
365a87295e8Spbrook         if (use_gdb_syscalls()) {
366a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "fstat,%x,%x",
3677ba6c104SPeter Maydell                            arg0, arg1);
368a87295e8Spbrook             return;
369a87295e8Spbrook         } else {
370a87295e8Spbrook             struct stat s;
3717ba6c104SPeter Maydell             result = fstat(arg0, &s);
372a87295e8Spbrook             if (result == 0) {
3737ba6c104SPeter Maydell                 translate_stat(env, arg1, &s);
374a87295e8Spbrook             }
375a87295e8Spbrook         }
376a87295e8Spbrook         break;
377a87295e8Spbrook     case HOSTED_GETTIMEOFDAY:
3787ba6c104SPeter Maydell         GET_ARG(0);
3797ba6c104SPeter Maydell         GET_ARG(1);
380a87295e8Spbrook         if (use_gdb_syscalls()) {
381a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "gettimeofday,%x,%x",
3827ba6c104SPeter Maydell                            arg0, arg1);
383a87295e8Spbrook             return;
384a87295e8Spbrook         } else {
38529b3a662Spbrook             qemu_timeval tv;
386a87295e8Spbrook             struct gdb_timeval *p;
38729b3a662Spbrook             result = qemu_gettimeofday(&tv);
388a87295e8Spbrook             if (result != 0) {
389579a97f7Sbellard                 if (!(p = lock_user(VERIFY_WRITE,
3907ba6c104SPeter Maydell                                     arg0, sizeof(struct gdb_timeval), 0))) {
391579a97f7Sbellard                     /* FIXME - check error code? */
392579a97f7Sbellard                     result = -1;
393579a97f7Sbellard                 } else {
394a87295e8Spbrook                     p->tv_sec = cpu_to_be32(tv.tv_sec);
395a87295e8Spbrook                     p->tv_usec = cpu_to_be64(tv.tv_usec);
3967ba6c104SPeter Maydell                     unlock_user(p, arg0, sizeof(struct gdb_timeval));
397a87295e8Spbrook                 }
398a87295e8Spbrook             }
399579a97f7Sbellard         }
400a87295e8Spbrook         break;
401a87295e8Spbrook     case HOSTED_ISATTY:
4027ba6c104SPeter Maydell         GET_ARG(0);
403a87295e8Spbrook         if (use_gdb_syscalls()) {
4047ba6c104SPeter Maydell             gdb_do_syscall(m68k_semi_cb, "isatty,%x", arg0);
405a87295e8Spbrook             return;
406a87295e8Spbrook         } else {
4077ba6c104SPeter Maydell             result = isatty(arg0);
408a87295e8Spbrook         }
409a87295e8Spbrook         break;
410a87295e8Spbrook     case HOSTED_SYSTEM:
4117ba6c104SPeter Maydell         GET_ARG(0);
4127ba6c104SPeter Maydell         GET_ARG(1);
413a87295e8Spbrook         if (use_gdb_syscalls()) {
414a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "system,%s",
4157ba6c104SPeter Maydell                            arg0, (int)arg1);
416a87295e8Spbrook             return;
417a87295e8Spbrook         } else {
4187ba6c104SPeter Maydell             p = lock_user_string(arg0);
4197ba6c104SPeter Maydell             if (!p) {
420579a97f7Sbellard                 /* FIXME - check error code? */
421579a97f7Sbellard                 result = -1;
422579a97f7Sbellard             } else {
423a87295e8Spbrook                 result = system(p);
4247ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
425a87295e8Spbrook             }
426579a97f7Sbellard         }
427a87295e8Spbrook         break;
428a87295e8Spbrook     case HOSTED_INIT_SIM:
429a87295e8Spbrook #if defined(CONFIG_USER_ONLY)
430a87295e8Spbrook         {
4310429a971SAndreas Färber         CPUState *cs = CPU(m68k_env_get_cpu(env));
4320429a971SAndreas Färber         TaskState *ts = cs->opaque;
433a87295e8Spbrook         /* Allocate the heap using sbrk.  */
434a87295e8Spbrook         if (!ts->heap_limit) {
4355382a012SPeter Maydell             abi_ulong ret;
436a87295e8Spbrook             uint32_t size;
437a87295e8Spbrook             uint32_t base;
438a87295e8Spbrook 
439a87295e8Spbrook             base = do_brk(0);
440a87295e8Spbrook             size = SEMIHOSTING_HEAP_SIZE;
441a87295e8Spbrook             /* Try a big heap, and reduce the size if that fails.  */
442a87295e8Spbrook             for (;;) {
443a87295e8Spbrook                 ret = do_brk(base + size);
4445382a012SPeter Maydell                 if (ret >= (base + size)) {
445a87295e8Spbrook                     break;
4465382a012SPeter Maydell                 }
447a87295e8Spbrook                 size >>= 1;
448a87295e8Spbrook             }
449a87295e8Spbrook             ts->heap_limit = base + size;
450a87295e8Spbrook         }
451a87295e8Spbrook         /* This call may happen before we have writable memory, so return
452a87295e8Spbrook            values directly in registers.  */
453a87295e8Spbrook         env->dregs[1] = ts->heap_limit;
454a87295e8Spbrook         env->aregs[7] = ts->stack_base;
455a87295e8Spbrook         }
456a87295e8Spbrook #else
457a87295e8Spbrook         /* FIXME: This is wrong for boards where RAM does not start at
458a87295e8Spbrook            address zero.  */
459a87295e8Spbrook         env->dregs[1] = ram_size;
460a87295e8Spbrook         env->aregs[7] = ram_size;
461a87295e8Spbrook #endif
462a87295e8Spbrook         return;
463a87295e8Spbrook     default:
464a47dddd7SAndreas Färber         cpu_abort(CPU(m68k_env_get_cpu(env)), "Unsupported semihosting syscall %d\n", nr);
465a87295e8Spbrook         result = 0;
466a87295e8Spbrook     }
4677ba6c104SPeter Maydell failed:
4681073bfd8SPeter Maydell     m68k_semi_return_u32(env, result, errno);
469a87295e8Spbrook }
470