xref: /qemu/target/m68k/m68k-semi.c (revision f793dde0914ae7f2605ee22c5bbc81dc79e23eee)
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 
20d8416665SPeter Maydell #include "qemu/osdep.h"
21a87295e8Spbrook 
22a87295e8Spbrook #include "cpu.h"
2385b4fa0cSPeter Maydell #include "exec/gdbstub.h"
24a87295e8Spbrook #if defined(CONFIG_USER_ONLY)
25a87295e8Spbrook #include "qemu.h"
26a87295e8Spbrook #define SEMIHOSTING_HEAP_SIZE (128 * 1024 * 1024)
27a87295e8Spbrook #else
28022c62cbSPaolo Bonzini #include "exec/softmmu-semi.h"
295601d241SPaolo Bonzini #include "hw/boards.h"
30a87295e8Spbrook #endif
3163c91552SPaolo Bonzini #include "qemu/log.h"
32a87295e8Spbrook 
33a87295e8Spbrook #define HOSTED_EXIT  0
34a87295e8Spbrook #define HOSTED_INIT_SIM 1
35a87295e8Spbrook #define HOSTED_OPEN 2
36a87295e8Spbrook #define HOSTED_CLOSE 3
37a87295e8Spbrook #define HOSTED_READ 4
38a87295e8Spbrook #define HOSTED_WRITE 5
39a87295e8Spbrook #define HOSTED_LSEEK 6
40a87295e8Spbrook #define HOSTED_RENAME 7
41a87295e8Spbrook #define HOSTED_UNLINK 8
42a87295e8Spbrook #define HOSTED_STAT 9
43a87295e8Spbrook #define HOSTED_FSTAT 10
44a87295e8Spbrook #define HOSTED_GETTIMEOFDAY 11
45a87295e8Spbrook #define HOSTED_ISATTY 12
46a87295e8Spbrook #define HOSTED_SYSTEM 13
47a87295e8Spbrook 
48c227f099SAnthony Liguori typedef uint32_t gdb_mode_t;
49c227f099SAnthony Liguori typedef uint32_t gdb_time_t;
50a87295e8Spbrook 
51a87295e8Spbrook struct m68k_gdb_stat {
52a87295e8Spbrook   uint32_t    gdb_st_dev;     /* device */
53a87295e8Spbrook   uint32_t    gdb_st_ino;     /* inode */
54c227f099SAnthony Liguori   gdb_mode_t  gdb_st_mode;    /* protection */
55a87295e8Spbrook   uint32_t    gdb_st_nlink;   /* number of hard links */
56a87295e8Spbrook   uint32_t    gdb_st_uid;     /* user ID of owner */
57a87295e8Spbrook   uint32_t    gdb_st_gid;     /* group ID of owner */
58a87295e8Spbrook   uint32_t    gdb_st_rdev;    /* device type (if inode device) */
59a87295e8Spbrook   uint64_t    gdb_st_size;    /* total size, in bytes */
60a87295e8Spbrook   uint64_t    gdb_st_blksize; /* blocksize for filesystem I/O */
61a87295e8Spbrook   uint64_t    gdb_st_blocks;  /* number of blocks allocated */
62c227f099SAnthony Liguori   gdb_time_t  gdb_st_atime;   /* time of last access */
63c227f099SAnthony Liguori   gdb_time_t  gdb_st_mtime;   /* time of last modification */
64c227f099SAnthony Liguori   gdb_time_t  gdb_st_ctime;   /* time of last change */
65541dc0d4SStefan Weil } QEMU_PACKED;
66a87295e8Spbrook 
67a87295e8Spbrook struct gdb_timeval {
68c227f099SAnthony Liguori   gdb_time_t tv_sec;  /* second */
69a87295e8Spbrook   uint64_t tv_usec;   /* microsecond */
70541dc0d4SStefan Weil } QEMU_PACKED;
71a87295e8Spbrook 
72a87295e8Spbrook #define GDB_O_RDONLY   0x0
73a87295e8Spbrook #define GDB_O_WRONLY   0x1
74a87295e8Spbrook #define GDB_O_RDWR     0x2
75a87295e8Spbrook #define GDB_O_APPEND   0x8
76a87295e8Spbrook #define GDB_O_CREAT  0x200
77a87295e8Spbrook #define GDB_O_TRUNC  0x400
78a87295e8Spbrook #define GDB_O_EXCL   0x800
79a87295e8Spbrook 
80a87295e8Spbrook static int translate_openflags(int flags)
81a87295e8Spbrook {
82a87295e8Spbrook     int hf;
83a87295e8Spbrook 
84a87295e8Spbrook     if (flags & GDB_O_WRONLY)
85a87295e8Spbrook         hf = O_WRONLY;
86a87295e8Spbrook     else if (flags & GDB_O_RDWR)
87a87295e8Spbrook         hf = O_RDWR;
88a87295e8Spbrook     else
89a87295e8Spbrook         hf = O_RDONLY;
90a87295e8Spbrook 
91a87295e8Spbrook     if (flags & GDB_O_APPEND) hf |= O_APPEND;
92a87295e8Spbrook     if (flags & GDB_O_CREAT) hf |= O_CREAT;
93a87295e8Spbrook     if (flags & GDB_O_TRUNC) hf |= O_TRUNC;
94a87295e8Spbrook     if (flags & GDB_O_EXCL) hf |= O_EXCL;
95a87295e8Spbrook 
96a87295e8Spbrook     return hf;
97a87295e8Spbrook }
98a87295e8Spbrook 
9971fc85e8SAndreas Färber static void translate_stat(CPUM68KState *env, target_ulong addr, struct stat *s)
100a87295e8Spbrook {
101a87295e8Spbrook     struct m68k_gdb_stat *p;
102a87295e8Spbrook 
103579a97f7Sbellard     if (!(p = lock_user(VERIFY_WRITE, addr, sizeof(struct m68k_gdb_stat), 0)))
104579a97f7Sbellard         /* FIXME - should this return an error code? */
105579a97f7Sbellard         return;
106a87295e8Spbrook     p->gdb_st_dev = cpu_to_be32(s->st_dev);
107a87295e8Spbrook     p->gdb_st_ino = cpu_to_be32(s->st_ino);
108a87295e8Spbrook     p->gdb_st_mode = cpu_to_be32(s->st_mode);
109a87295e8Spbrook     p->gdb_st_nlink = cpu_to_be32(s->st_nlink);
110a87295e8Spbrook     p->gdb_st_uid = cpu_to_be32(s->st_uid);
111a87295e8Spbrook     p->gdb_st_gid = cpu_to_be32(s->st_gid);
112a87295e8Spbrook     p->gdb_st_rdev = cpu_to_be32(s->st_rdev);
113a87295e8Spbrook     p->gdb_st_size = cpu_to_be64(s->st_size);
11429b3a662Spbrook #ifdef _WIN32
11529b3a662Spbrook     /* Windows stat is missing some fields.  */
11629b3a662Spbrook     p->gdb_st_blksize = 0;
11729b3a662Spbrook     p->gdb_st_blocks = 0;
11829b3a662Spbrook #else
119a87295e8Spbrook     p->gdb_st_blksize = cpu_to_be64(s->st_blksize);
120a87295e8Spbrook     p->gdb_st_blocks = cpu_to_be64(s->st_blocks);
12129b3a662Spbrook #endif
122a87295e8Spbrook     p->gdb_st_atime = cpu_to_be32(s->st_atime);
123a87295e8Spbrook     p->gdb_st_mtime = cpu_to_be32(s->st_mtime);
124a87295e8Spbrook     p->gdb_st_ctime = cpu_to_be32(s->st_ctime);
125a87295e8Spbrook     unlock_user(p, addr, sizeof(struct m68k_gdb_stat));
126a87295e8Spbrook }
127a87295e8Spbrook 
1281073bfd8SPeter Maydell static void m68k_semi_return_u32(CPUM68KState *env, uint32_t ret, uint32_t err)
1291073bfd8SPeter Maydell {
1301073bfd8SPeter Maydell     target_ulong args = env->dregs[1];
1311073bfd8SPeter Maydell     if (put_user_u32(ret, args) ||
1321073bfd8SPeter Maydell         put_user_u32(err, args + 4)) {
133808d77bcSLucien Murray-Pitts         /*
134808d77bcSLucien Murray-Pitts          * The m68k semihosting ABI does not provide any way to report this
1351073bfd8SPeter Maydell          * error to the guest, so the best we can do is log it in qemu.
1361073bfd8SPeter Maydell          * It is always a guest error not to pass us a valid argument block.
1371073bfd8SPeter Maydell          */
1381073bfd8SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value "
1391073bfd8SPeter Maydell                       "discarded because argument block not writable\n");
1401073bfd8SPeter Maydell     }
1411073bfd8SPeter Maydell }
1421073bfd8SPeter Maydell 
1431073bfd8SPeter Maydell static void m68k_semi_return_u64(CPUM68KState *env, uint64_t ret, uint32_t err)
1441073bfd8SPeter Maydell {
1451073bfd8SPeter Maydell     target_ulong args = env->dregs[1];
1461073bfd8SPeter Maydell     if (put_user_u32(ret >> 32, args) ||
1471073bfd8SPeter Maydell         put_user_u32(ret, args + 4) ||
1481073bfd8SPeter Maydell         put_user_u32(err, args + 8)) {
1491073bfd8SPeter Maydell         /* No way to report this via m68k semihosting ABI; just log it */
1501073bfd8SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value "
1511073bfd8SPeter Maydell                       "discarded because argument block not writable\n");
1521073bfd8SPeter Maydell     }
1531073bfd8SPeter Maydell }
1541073bfd8SPeter Maydell 
155a87295e8Spbrook static int m68k_semi_is_fseek;
156a87295e8Spbrook 
1579e0c5422SAndreas Färber static void m68k_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
158a87295e8Spbrook {
1599e0c5422SAndreas Färber     M68kCPU *cpu = M68K_CPU(cs);
1609e0c5422SAndreas Färber     CPUM68KState *env = &cpu->env;
1619e0c5422SAndreas Färber 
162a87295e8Spbrook     if (m68k_semi_is_fseek) {
163808d77bcSLucien Murray-Pitts         /*
164808d77bcSLucien Murray-Pitts          * FIXME: We've already lost the high bits of the fseek
165808d77bcSLucien Murray-Pitts          * return value.
166808d77bcSLucien Murray-Pitts          */
1671073bfd8SPeter Maydell         m68k_semi_return_u64(env, ret, err);
168a87295e8Spbrook         m68k_semi_is_fseek = 0;
1691073bfd8SPeter Maydell     } else {
1701073bfd8SPeter Maydell         m68k_semi_return_u32(env, ret, err);
171a87295e8Spbrook     }
172a87295e8Spbrook }
173a87295e8Spbrook 
174808d77bcSLucien Murray-Pitts /*
175808d77bcSLucien Murray-Pitts  * Read the input value from the argument block; fail the semihosting
1767ba6c104SPeter Maydell  * call if the memory read fails.
1777ba6c104SPeter Maydell  */
1787ba6c104SPeter Maydell #define GET_ARG(n) do {                                 \
1797ba6c104SPeter Maydell     if (get_user_ual(arg ## n, args + (n) * 4)) {       \
1807ba6c104SPeter Maydell         result = -1;                                    \
1817ba6c104SPeter Maydell         errno = EFAULT;                                 \
1827ba6c104SPeter Maydell         goto failed;                                    \
1837ba6c104SPeter Maydell     }                                                   \
1847ba6c104SPeter Maydell } while (0)
1857ba6c104SPeter Maydell 
186a87295e8Spbrook void do_m68k_semihosting(CPUM68KState *env, int nr)
187a87295e8Spbrook {
188a87295e8Spbrook     uint32_t args;
1897ba6c104SPeter Maydell     target_ulong arg0, arg1, arg2, arg3;
190a87295e8Spbrook     void *p;
191a87295e8Spbrook     void *q;
192a87295e8Spbrook     uint32_t len;
193a87295e8Spbrook     uint32_t result;
194a87295e8Spbrook 
195a87295e8Spbrook     args = env->dregs[1];
196a87295e8Spbrook     switch (nr) {
197a87295e8Spbrook     case HOSTED_EXIT:
198ad9dcb20SAlex Bennée         gdb_exit(env->dregs[0]);
199a87295e8Spbrook         exit(env->dregs[0]);
200a87295e8Spbrook     case HOSTED_OPEN:
2017ba6c104SPeter Maydell         GET_ARG(0);
2027ba6c104SPeter Maydell         GET_ARG(1);
2037ba6c104SPeter Maydell         GET_ARG(2);
2047ba6c104SPeter Maydell         GET_ARG(3);
205a87295e8Spbrook         if (use_gdb_syscalls()) {
2067ba6c104SPeter Maydell             gdb_do_syscall(m68k_semi_cb, "open,%s,%x,%x", arg0, (int)arg1,
2077ba6c104SPeter Maydell                            arg2, arg3);
208a87295e8Spbrook             return;
209a87295e8Spbrook         } else {
2107ba6c104SPeter Maydell             p = lock_user_string(arg0);
2117ba6c104SPeter Maydell             if (!p) {
212579a97f7Sbellard                 /* FIXME - check error code? */
213579a97f7Sbellard                 result = -1;
214579a97f7Sbellard             } else {
2157ba6c104SPeter Maydell                 result = open(p, translate_openflags(arg2), arg3);
2167ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
217a87295e8Spbrook             }
218579a97f7Sbellard         }
219a87295e8Spbrook         break;
220a87295e8Spbrook     case HOSTED_CLOSE:
221a87295e8Spbrook         {
222a87295e8Spbrook             /* Ignore attempts to close stdin/out/err.  */
2237ba6c104SPeter Maydell             GET_ARG(0);
2247ba6c104SPeter Maydell             int fd = arg0;
225a87295e8Spbrook             if (fd > 2) {
226a87295e8Spbrook                 if (use_gdb_syscalls()) {
2277ba6c104SPeter Maydell                     gdb_do_syscall(m68k_semi_cb, "close,%x", arg0);
228a87295e8Spbrook                     return;
229a87295e8Spbrook                 } else {
230a87295e8Spbrook                     result = close(fd);
231a87295e8Spbrook                 }
232a87295e8Spbrook             } else {
233a87295e8Spbrook                 result = 0;
234a87295e8Spbrook             }
235a87295e8Spbrook             break;
236a87295e8Spbrook         }
237a87295e8Spbrook     case HOSTED_READ:
2387ba6c104SPeter Maydell         GET_ARG(0);
2397ba6c104SPeter Maydell         GET_ARG(1);
2407ba6c104SPeter Maydell         GET_ARG(2);
2417ba6c104SPeter Maydell         len = arg2;
242a87295e8Spbrook         if (use_gdb_syscalls()) {
243a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "read,%x,%x,%x",
2447ba6c104SPeter Maydell                            arg0, arg1, len);
245a87295e8Spbrook             return;
246a87295e8Spbrook         } else {
2477ba6c104SPeter Maydell             p = lock_user(VERIFY_WRITE, arg1, len, 0);
2487ba6c104SPeter Maydell             if (!p) {
249579a97f7Sbellard                 /* FIXME - check error code? */
250579a97f7Sbellard                 result = -1;
251579a97f7Sbellard             } else {
2527ba6c104SPeter Maydell                 result = read(arg0, p, len);
2537ba6c104SPeter Maydell                 unlock_user(p, arg1, len);
254a87295e8Spbrook             }
255579a97f7Sbellard         }
256a87295e8Spbrook         break;
257a87295e8Spbrook     case HOSTED_WRITE:
2587ba6c104SPeter Maydell         GET_ARG(0);
2597ba6c104SPeter Maydell         GET_ARG(1);
2607ba6c104SPeter Maydell         GET_ARG(2);
2617ba6c104SPeter Maydell         len = arg2;
262a87295e8Spbrook         if (use_gdb_syscalls()) {
263a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "write,%x,%x,%x",
2647ba6c104SPeter Maydell                            arg0, arg1, len);
265a87295e8Spbrook             return;
266a87295e8Spbrook         } else {
2677ba6c104SPeter Maydell             p = lock_user(VERIFY_READ, arg1, len, 1);
2687ba6c104SPeter Maydell             if (!p) {
269579a97f7Sbellard                 /* FIXME - check error code? */
270579a97f7Sbellard                 result = -1;
271579a97f7Sbellard             } else {
2727ba6c104SPeter Maydell                 result = write(arg0, p, len);
2737ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
274a87295e8Spbrook             }
275579a97f7Sbellard         }
276a87295e8Spbrook         break;
277a87295e8Spbrook     case HOSTED_LSEEK:
278a87295e8Spbrook         {
279a87295e8Spbrook             uint64_t off;
2807ba6c104SPeter Maydell             GET_ARG(0);
2817ba6c104SPeter Maydell             GET_ARG(1);
2827ba6c104SPeter Maydell             GET_ARG(2);
2837ba6c104SPeter Maydell             GET_ARG(3);
2847ba6c104SPeter Maydell             off = (uint32_t)arg2 | ((uint64_t)arg1 << 32);
285a87295e8Spbrook             if (use_gdb_syscalls()) {
286a87295e8Spbrook                 m68k_semi_is_fseek = 1;
287a87295e8Spbrook                 gdb_do_syscall(m68k_semi_cb, "fseek,%x,%lx,%x",
2887ba6c104SPeter Maydell                                arg0, off, arg3);
289a87295e8Spbrook             } else {
2907ba6c104SPeter Maydell                 off = lseek(arg0, off, arg3);
2911073bfd8SPeter Maydell                 m68k_semi_return_u64(env, off, errno);
292a87295e8Spbrook             }
293a87295e8Spbrook             return;
294a87295e8Spbrook         }
295a87295e8Spbrook     case HOSTED_RENAME:
2967ba6c104SPeter Maydell         GET_ARG(0);
2977ba6c104SPeter Maydell         GET_ARG(1);
2987ba6c104SPeter Maydell         GET_ARG(2);
2997ba6c104SPeter Maydell         GET_ARG(3);
300a87295e8Spbrook         if (use_gdb_syscalls()) {
301a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "rename,%s,%s",
3027ba6c104SPeter Maydell                            arg0, (int)arg1, arg2, (int)arg3);
303a87295e8Spbrook             return;
304a87295e8Spbrook         } else {
3057ba6c104SPeter Maydell             p = lock_user_string(arg0);
3067ba6c104SPeter Maydell             q = lock_user_string(arg2);
307579a97f7Sbellard             if (!p || !q) {
308579a97f7Sbellard                 /* FIXME - check error code? */
309579a97f7Sbellard                 result = -1;
310579a97f7Sbellard             } else {
311a87295e8Spbrook                 result = rename(p, q);
312579a97f7Sbellard             }
3137ba6c104SPeter Maydell             unlock_user(p, arg0, 0);
3147ba6c104SPeter Maydell             unlock_user(q, arg2, 0);
315a87295e8Spbrook         }
316a87295e8Spbrook         break;
317a87295e8Spbrook     case HOSTED_UNLINK:
3187ba6c104SPeter Maydell         GET_ARG(0);
3197ba6c104SPeter Maydell         GET_ARG(1);
320a87295e8Spbrook         if (use_gdb_syscalls()) {
321a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "unlink,%s",
3227ba6c104SPeter Maydell                            arg0, (int)arg1);
323a87295e8Spbrook             return;
324a87295e8Spbrook         } else {
3257ba6c104SPeter Maydell             p = lock_user_string(arg0);
3267ba6c104SPeter Maydell             if (!p) {
327579a97f7Sbellard                 /* FIXME - check error code? */
328579a97f7Sbellard                 result = -1;
329579a97f7Sbellard             } else {
330a87295e8Spbrook                 result = unlink(p);
3317ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
332a87295e8Spbrook             }
333579a97f7Sbellard         }
334a87295e8Spbrook         break;
335a87295e8Spbrook     case HOSTED_STAT:
3367ba6c104SPeter Maydell         GET_ARG(0);
3377ba6c104SPeter Maydell         GET_ARG(1);
3387ba6c104SPeter Maydell         GET_ARG(2);
339a87295e8Spbrook         if (use_gdb_syscalls()) {
340a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "stat,%s,%x",
3417ba6c104SPeter Maydell                            arg0, (int)arg1, arg2);
342a87295e8Spbrook             return;
343a87295e8Spbrook         } else {
344a87295e8Spbrook             struct stat s;
3457ba6c104SPeter Maydell             p = lock_user_string(arg0);
3467ba6c104SPeter Maydell             if (!p) {
347579a97f7Sbellard                 /* FIXME - check error code? */
348579a97f7Sbellard                 result = -1;
349579a97f7Sbellard             } else {
350a87295e8Spbrook                 result = stat(p, &s);
3517ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
352579a97f7Sbellard             }
353a87295e8Spbrook             if (result == 0) {
3547ba6c104SPeter Maydell                 translate_stat(env, arg2, &s);
355a87295e8Spbrook             }
356a87295e8Spbrook         }
357a87295e8Spbrook         break;
358a87295e8Spbrook     case HOSTED_FSTAT:
3597ba6c104SPeter Maydell         GET_ARG(0);
3607ba6c104SPeter Maydell         GET_ARG(1);
361a87295e8Spbrook         if (use_gdb_syscalls()) {
362a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "fstat,%x,%x",
3637ba6c104SPeter Maydell                            arg0, arg1);
364a87295e8Spbrook             return;
365a87295e8Spbrook         } else {
366a87295e8Spbrook             struct stat s;
3677ba6c104SPeter Maydell             result = fstat(arg0, &s);
368a87295e8Spbrook             if (result == 0) {
3697ba6c104SPeter Maydell                 translate_stat(env, arg1, &s);
370a87295e8Spbrook             }
371a87295e8Spbrook         }
372a87295e8Spbrook         break;
373a87295e8Spbrook     case HOSTED_GETTIMEOFDAY:
3747ba6c104SPeter Maydell         GET_ARG(0);
3757ba6c104SPeter Maydell         GET_ARG(1);
376a87295e8Spbrook         if (use_gdb_syscalls()) {
377a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "gettimeofday,%x,%x",
3787ba6c104SPeter Maydell                            arg0, arg1);
379a87295e8Spbrook             return;
380a87295e8Spbrook         } else {
381a87295e8Spbrook             struct gdb_timeval *p;
382*f793dde0SMarc-André Lureau             int64_t rt = g_get_real_time();
383*f793dde0SMarc-André Lureau             p = lock_user(VERIFY_WRITE, arg0, sizeof(struct gdb_timeval), 0);
384*f793dde0SMarc-André Lureau             if (!p) {
385579a97f7Sbellard                 /* FIXME - check error code? */
386579a97f7Sbellard                 result = -1;
387579a97f7Sbellard             } else {
388*f793dde0SMarc-André Lureau                 result = 0;
389*f793dde0SMarc-André Lureau                 p->tv_sec = cpu_to_be32(rt / G_USEC_PER_SEC);
390*f793dde0SMarc-André Lureau                 p->tv_usec = cpu_to_be64(rt % G_USEC_PER_SEC);
3917ba6c104SPeter Maydell                 unlock_user(p, arg0, sizeof(struct gdb_timeval));
392a87295e8Spbrook             }
393a87295e8Spbrook         }
394a87295e8Spbrook         break;
395a87295e8Spbrook     case HOSTED_ISATTY:
3967ba6c104SPeter Maydell         GET_ARG(0);
397a87295e8Spbrook         if (use_gdb_syscalls()) {
3987ba6c104SPeter Maydell             gdb_do_syscall(m68k_semi_cb, "isatty,%x", arg0);
399a87295e8Spbrook             return;
400a87295e8Spbrook         } else {
4017ba6c104SPeter Maydell             result = isatty(arg0);
402a87295e8Spbrook         }
403a87295e8Spbrook         break;
404a87295e8Spbrook     case HOSTED_SYSTEM:
4057ba6c104SPeter Maydell         GET_ARG(0);
4067ba6c104SPeter Maydell         GET_ARG(1);
407a87295e8Spbrook         if (use_gdb_syscalls()) {
408a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "system,%s",
4097ba6c104SPeter Maydell                            arg0, (int)arg1);
410a87295e8Spbrook             return;
411a87295e8Spbrook         } else {
4127ba6c104SPeter Maydell             p = lock_user_string(arg0);
4137ba6c104SPeter Maydell             if (!p) {
414579a97f7Sbellard                 /* FIXME - check error code? */
415579a97f7Sbellard                 result = -1;
416579a97f7Sbellard             } else {
417a87295e8Spbrook                 result = system(p);
4187ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
419a87295e8Spbrook             }
420579a97f7Sbellard         }
421a87295e8Spbrook         break;
422a87295e8Spbrook     case HOSTED_INIT_SIM:
423a87295e8Spbrook #if defined(CONFIG_USER_ONLY)
424a87295e8Spbrook         {
425a8d92fd8SRichard Henderson         CPUState *cs = env_cpu(env);
4260429a971SAndreas Färber         TaskState *ts = cs->opaque;
427a87295e8Spbrook         /* Allocate the heap using sbrk.  */
428a87295e8Spbrook         if (!ts->heap_limit) {
4295382a012SPeter Maydell             abi_ulong ret;
430a87295e8Spbrook             uint32_t size;
431a87295e8Spbrook             uint32_t base;
432a87295e8Spbrook 
433a87295e8Spbrook             base = do_brk(0);
434a87295e8Spbrook             size = SEMIHOSTING_HEAP_SIZE;
435a87295e8Spbrook             /* Try a big heap, and reduce the size if that fails.  */
436a87295e8Spbrook             for (;;) {
437a87295e8Spbrook                 ret = do_brk(base + size);
4385382a012SPeter Maydell                 if (ret >= (base + size)) {
439a87295e8Spbrook                     break;
4405382a012SPeter Maydell                 }
441a87295e8Spbrook                 size >>= 1;
442a87295e8Spbrook             }
443a87295e8Spbrook             ts->heap_limit = base + size;
444a87295e8Spbrook         }
445808d77bcSLucien Murray-Pitts         /*
446808d77bcSLucien Murray-Pitts          * This call may happen before we have writable memory, so return
447808d77bcSLucien Murray-Pitts          * values directly in registers.
448808d77bcSLucien Murray-Pitts          */
449a87295e8Spbrook         env->dregs[1] = ts->heap_limit;
450a87295e8Spbrook         env->aregs[7] = ts->stack_base;
451a87295e8Spbrook         }
452a87295e8Spbrook #else
453808d77bcSLucien Murray-Pitts         /*
454808d77bcSLucien Murray-Pitts          * FIXME: This is wrong for boards where RAM does not start at
455808d77bcSLucien Murray-Pitts          * address zero.
456808d77bcSLucien Murray-Pitts          */
4575601d241SPaolo Bonzini         env->dregs[1] = current_machine->ram_size;
4585601d241SPaolo Bonzini         env->aregs[7] = current_machine->ram_size;
459a87295e8Spbrook #endif
460a87295e8Spbrook         return;
461a87295e8Spbrook     default:
462a8d92fd8SRichard Henderson         cpu_abort(env_cpu(env), "Unsupported semihosting syscall %d\n", nr);
463a87295e8Spbrook         result = 0;
464a87295e8Spbrook     }
4657ba6c104SPeter Maydell failed:
4661073bfd8SPeter Maydell     m68k_semi_return_u32(env, result, errno);
467a87295e8Spbrook }
468