xref: /qemu/target/m68k/m68k-semi.c (revision 63c915526d6a54a95919ebece83fa9ca631b2508)
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"
23a87295e8Spbrook #if defined(CONFIG_USER_ONLY)
24a87295e8Spbrook #include "qemu.h"
25a87295e8Spbrook #define SEMIHOSTING_HEAP_SIZE (128 * 1024 * 1024)
26a87295e8Spbrook #else
2787ecb68bSpbrook #include "qemu-common.h"
28022c62cbSPaolo Bonzini #include "exec/gdbstub.h"
29022c62cbSPaolo Bonzini #include "exec/softmmu-semi.h"
30a87295e8Spbrook #endif
3163c91552SPaolo Bonzini #include "qemu/log.h"
329c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
33a87295e8Spbrook 
34a87295e8Spbrook #define HOSTED_EXIT  0
35a87295e8Spbrook #define HOSTED_INIT_SIM 1
36a87295e8Spbrook #define HOSTED_OPEN 2
37a87295e8Spbrook #define HOSTED_CLOSE 3
38a87295e8Spbrook #define HOSTED_READ 4
39a87295e8Spbrook #define HOSTED_WRITE 5
40a87295e8Spbrook #define HOSTED_LSEEK 6
41a87295e8Spbrook #define HOSTED_RENAME 7
42a87295e8Spbrook #define HOSTED_UNLINK 8
43a87295e8Spbrook #define HOSTED_STAT 9
44a87295e8Spbrook #define HOSTED_FSTAT 10
45a87295e8Spbrook #define HOSTED_GETTIMEOFDAY 11
46a87295e8Spbrook #define HOSTED_ISATTY 12
47a87295e8Spbrook #define HOSTED_SYSTEM 13
48a87295e8Spbrook 
49c227f099SAnthony Liguori typedef uint32_t gdb_mode_t;
50c227f099SAnthony Liguori typedef uint32_t gdb_time_t;
51a87295e8Spbrook 
52a87295e8Spbrook struct m68k_gdb_stat {
53a87295e8Spbrook   uint32_t    gdb_st_dev;     /* device */
54a87295e8Spbrook   uint32_t    gdb_st_ino;     /* inode */
55c227f099SAnthony Liguori   gdb_mode_t  gdb_st_mode;    /* protection */
56a87295e8Spbrook   uint32_t    gdb_st_nlink;   /* number of hard links */
57a87295e8Spbrook   uint32_t    gdb_st_uid;     /* user ID of owner */
58a87295e8Spbrook   uint32_t    gdb_st_gid;     /* group ID of owner */
59a87295e8Spbrook   uint32_t    gdb_st_rdev;    /* device type (if inode device) */
60a87295e8Spbrook   uint64_t    gdb_st_size;    /* total size, in bytes */
61a87295e8Spbrook   uint64_t    gdb_st_blksize; /* blocksize for filesystem I/O */
62a87295e8Spbrook   uint64_t    gdb_st_blocks;  /* number of blocks allocated */
63c227f099SAnthony Liguori   gdb_time_t  gdb_st_atime;   /* time of last access */
64c227f099SAnthony Liguori   gdb_time_t  gdb_st_mtime;   /* time of last modification */
65c227f099SAnthony Liguori   gdb_time_t  gdb_st_ctime;   /* time of last change */
66541dc0d4SStefan Weil } QEMU_PACKED;
67a87295e8Spbrook 
68a87295e8Spbrook struct gdb_timeval {
69c227f099SAnthony Liguori   gdb_time_t tv_sec;  /* second */
70a87295e8Spbrook   uint64_t tv_usec;   /* microsecond */
71541dc0d4SStefan Weil } QEMU_PACKED;
72a87295e8Spbrook 
73a87295e8Spbrook #define GDB_O_RDONLY   0x0
74a87295e8Spbrook #define GDB_O_WRONLY   0x1
75a87295e8Spbrook #define GDB_O_RDWR     0x2
76a87295e8Spbrook #define GDB_O_APPEND   0x8
77a87295e8Spbrook #define GDB_O_CREAT  0x200
78a87295e8Spbrook #define GDB_O_TRUNC  0x400
79a87295e8Spbrook #define GDB_O_EXCL   0x800
80a87295e8Spbrook 
81a87295e8Spbrook static int translate_openflags(int flags)
82a87295e8Spbrook {
83a87295e8Spbrook     int hf;
84a87295e8Spbrook 
85a87295e8Spbrook     if (flags & GDB_O_WRONLY)
86a87295e8Spbrook         hf = O_WRONLY;
87a87295e8Spbrook     else if (flags & GDB_O_RDWR)
88a87295e8Spbrook         hf = O_RDWR;
89a87295e8Spbrook     else
90a87295e8Spbrook         hf = O_RDONLY;
91a87295e8Spbrook 
92a87295e8Spbrook     if (flags & GDB_O_APPEND) hf |= O_APPEND;
93a87295e8Spbrook     if (flags & GDB_O_CREAT) hf |= O_CREAT;
94a87295e8Spbrook     if (flags & GDB_O_TRUNC) hf |= O_TRUNC;
95a87295e8Spbrook     if (flags & GDB_O_EXCL) hf |= O_EXCL;
96a87295e8Spbrook 
97a87295e8Spbrook     return hf;
98a87295e8Spbrook }
99a87295e8Spbrook 
10071fc85e8SAndreas Färber static void translate_stat(CPUM68KState *env, target_ulong addr, struct stat *s)
101a87295e8Spbrook {
102a87295e8Spbrook     struct m68k_gdb_stat *p;
103a87295e8Spbrook 
104579a97f7Sbellard     if (!(p = lock_user(VERIFY_WRITE, addr, sizeof(struct m68k_gdb_stat), 0)))
105579a97f7Sbellard         /* FIXME - should this return an error code? */
106579a97f7Sbellard         return;
107a87295e8Spbrook     p->gdb_st_dev = cpu_to_be32(s->st_dev);
108a87295e8Spbrook     p->gdb_st_ino = cpu_to_be32(s->st_ino);
109a87295e8Spbrook     p->gdb_st_mode = cpu_to_be32(s->st_mode);
110a87295e8Spbrook     p->gdb_st_nlink = cpu_to_be32(s->st_nlink);
111a87295e8Spbrook     p->gdb_st_uid = cpu_to_be32(s->st_uid);
112a87295e8Spbrook     p->gdb_st_gid = cpu_to_be32(s->st_gid);
113a87295e8Spbrook     p->gdb_st_rdev = cpu_to_be32(s->st_rdev);
114a87295e8Spbrook     p->gdb_st_size = cpu_to_be64(s->st_size);
11529b3a662Spbrook #ifdef _WIN32
11629b3a662Spbrook     /* Windows stat is missing some fields.  */
11729b3a662Spbrook     p->gdb_st_blksize = 0;
11829b3a662Spbrook     p->gdb_st_blocks = 0;
11929b3a662Spbrook #else
120a87295e8Spbrook     p->gdb_st_blksize = cpu_to_be64(s->st_blksize);
121a87295e8Spbrook     p->gdb_st_blocks = cpu_to_be64(s->st_blocks);
12229b3a662Spbrook #endif
123a87295e8Spbrook     p->gdb_st_atime = cpu_to_be32(s->st_atime);
124a87295e8Spbrook     p->gdb_st_mtime = cpu_to_be32(s->st_mtime);
125a87295e8Spbrook     p->gdb_st_ctime = cpu_to_be32(s->st_ctime);
126a87295e8Spbrook     unlock_user(p, addr, sizeof(struct m68k_gdb_stat));
127a87295e8Spbrook }
128a87295e8Spbrook 
1291073bfd8SPeter Maydell static void m68k_semi_return_u32(CPUM68KState *env, uint32_t ret, uint32_t err)
1301073bfd8SPeter Maydell {
1311073bfd8SPeter Maydell     target_ulong args = env->dregs[1];
1321073bfd8SPeter Maydell     if (put_user_u32(ret, args) ||
1331073bfd8SPeter Maydell         put_user_u32(err, args + 4)) {
1341073bfd8SPeter Maydell         /* 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) {
163a87295e8Spbrook         /* FIXME: We've already lost the high bits of the fseek
164a87295e8Spbrook            return value.  */
1651073bfd8SPeter Maydell         m68k_semi_return_u64(env, ret, err);
166a87295e8Spbrook         m68k_semi_is_fseek = 0;
1671073bfd8SPeter Maydell     } else {
1681073bfd8SPeter Maydell         m68k_semi_return_u32(env, ret, err);
169a87295e8Spbrook     }
170a87295e8Spbrook }
171a87295e8Spbrook 
1727ba6c104SPeter Maydell /* Read the input value from the argument block; fail the semihosting
1737ba6c104SPeter Maydell  * call if the memory read fails.
1747ba6c104SPeter Maydell  */
1757ba6c104SPeter Maydell #define GET_ARG(n) do {                                 \
1767ba6c104SPeter Maydell     if (get_user_ual(arg ## n, args + (n) * 4)) {       \
1777ba6c104SPeter Maydell         result = -1;                                    \
1787ba6c104SPeter Maydell         errno = EFAULT;                                 \
1797ba6c104SPeter Maydell         goto failed;                                    \
1807ba6c104SPeter Maydell     }                                                   \
1817ba6c104SPeter Maydell } while (0)
1827ba6c104SPeter Maydell 
183a87295e8Spbrook void do_m68k_semihosting(CPUM68KState *env, int nr)
184a87295e8Spbrook {
185a87295e8Spbrook     uint32_t args;
1867ba6c104SPeter Maydell     target_ulong arg0, arg1, arg2, arg3;
187a87295e8Spbrook     void *p;
188a87295e8Spbrook     void *q;
189a87295e8Spbrook     uint32_t len;
190a87295e8Spbrook     uint32_t result;
191a87295e8Spbrook 
192a87295e8Spbrook     args = env->dregs[1];
193a87295e8Spbrook     switch (nr) {
194a87295e8Spbrook     case HOSTED_EXIT:
1950e1c9c54SPaul Brook         gdb_exit(env, env->dregs[0]);
196a87295e8Spbrook         exit(env->dregs[0]);
197a87295e8Spbrook     case HOSTED_OPEN:
1987ba6c104SPeter Maydell         GET_ARG(0);
1997ba6c104SPeter Maydell         GET_ARG(1);
2007ba6c104SPeter Maydell         GET_ARG(2);
2017ba6c104SPeter Maydell         GET_ARG(3);
202a87295e8Spbrook         if (use_gdb_syscalls()) {
2037ba6c104SPeter Maydell             gdb_do_syscall(m68k_semi_cb, "open,%s,%x,%x", arg0, (int)arg1,
2047ba6c104SPeter Maydell                            arg2, arg3);
205a87295e8Spbrook             return;
206a87295e8Spbrook         } else {
2077ba6c104SPeter Maydell             p = lock_user_string(arg0);
2087ba6c104SPeter Maydell             if (!p) {
209579a97f7Sbellard                 /* FIXME - check error code? */
210579a97f7Sbellard                 result = -1;
211579a97f7Sbellard             } else {
2127ba6c104SPeter Maydell                 result = open(p, translate_openflags(arg2), arg3);
2137ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
214a87295e8Spbrook             }
215579a97f7Sbellard         }
216a87295e8Spbrook         break;
217a87295e8Spbrook     case HOSTED_CLOSE:
218a87295e8Spbrook         {
219a87295e8Spbrook             /* Ignore attempts to close stdin/out/err.  */
2207ba6c104SPeter Maydell             GET_ARG(0);
2217ba6c104SPeter Maydell             int fd = arg0;
222a87295e8Spbrook             if (fd > 2) {
223a87295e8Spbrook                 if (use_gdb_syscalls()) {
2247ba6c104SPeter Maydell                     gdb_do_syscall(m68k_semi_cb, "close,%x", arg0);
225a87295e8Spbrook                     return;
226a87295e8Spbrook                 } else {
227a87295e8Spbrook                     result = close(fd);
228a87295e8Spbrook                 }
229a87295e8Spbrook             } else {
230a87295e8Spbrook                 result = 0;
231a87295e8Spbrook             }
232a87295e8Spbrook             break;
233a87295e8Spbrook         }
234a87295e8Spbrook     case HOSTED_READ:
2357ba6c104SPeter Maydell         GET_ARG(0);
2367ba6c104SPeter Maydell         GET_ARG(1);
2377ba6c104SPeter Maydell         GET_ARG(2);
2387ba6c104SPeter Maydell         len = arg2;
239a87295e8Spbrook         if (use_gdb_syscalls()) {
240a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "read,%x,%x,%x",
2417ba6c104SPeter Maydell                            arg0, arg1, len);
242a87295e8Spbrook             return;
243a87295e8Spbrook         } else {
2447ba6c104SPeter Maydell             p = lock_user(VERIFY_WRITE, arg1, len, 0);
2457ba6c104SPeter Maydell             if (!p) {
246579a97f7Sbellard                 /* FIXME - check error code? */
247579a97f7Sbellard                 result = -1;
248579a97f7Sbellard             } else {
2497ba6c104SPeter Maydell                 result = read(arg0, p, len);
2507ba6c104SPeter Maydell                 unlock_user(p, arg1, len);
251a87295e8Spbrook             }
252579a97f7Sbellard         }
253a87295e8Spbrook         break;
254a87295e8Spbrook     case HOSTED_WRITE:
2557ba6c104SPeter Maydell         GET_ARG(0);
2567ba6c104SPeter Maydell         GET_ARG(1);
2577ba6c104SPeter Maydell         GET_ARG(2);
2587ba6c104SPeter Maydell         len = arg2;
259a87295e8Spbrook         if (use_gdb_syscalls()) {
260a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "write,%x,%x,%x",
2617ba6c104SPeter Maydell                            arg0, arg1, len);
262a87295e8Spbrook             return;
263a87295e8Spbrook         } else {
2647ba6c104SPeter Maydell             p = lock_user(VERIFY_READ, arg1, len, 1);
2657ba6c104SPeter Maydell             if (!p) {
266579a97f7Sbellard                 /* FIXME - check error code? */
267579a97f7Sbellard                 result = -1;
268579a97f7Sbellard             } else {
2697ba6c104SPeter Maydell                 result = write(arg0, p, len);
2707ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
271a87295e8Spbrook             }
272579a97f7Sbellard         }
273a87295e8Spbrook         break;
274a87295e8Spbrook     case HOSTED_LSEEK:
275a87295e8Spbrook         {
276a87295e8Spbrook             uint64_t off;
2777ba6c104SPeter Maydell             GET_ARG(0);
2787ba6c104SPeter Maydell             GET_ARG(1);
2797ba6c104SPeter Maydell             GET_ARG(2);
2807ba6c104SPeter Maydell             GET_ARG(3);
2817ba6c104SPeter Maydell             off = (uint32_t)arg2 | ((uint64_t)arg1 << 32);
282a87295e8Spbrook             if (use_gdb_syscalls()) {
283a87295e8Spbrook                 m68k_semi_is_fseek = 1;
284a87295e8Spbrook                 gdb_do_syscall(m68k_semi_cb, "fseek,%x,%lx,%x",
2857ba6c104SPeter Maydell                                arg0, off, arg3);
286a87295e8Spbrook             } else {
2877ba6c104SPeter Maydell                 off = lseek(arg0, off, arg3);
2881073bfd8SPeter Maydell                 m68k_semi_return_u64(env, off, errno);
289a87295e8Spbrook             }
290a87295e8Spbrook             return;
291a87295e8Spbrook         }
292a87295e8Spbrook     case HOSTED_RENAME:
2937ba6c104SPeter Maydell         GET_ARG(0);
2947ba6c104SPeter Maydell         GET_ARG(1);
2957ba6c104SPeter Maydell         GET_ARG(2);
2967ba6c104SPeter Maydell         GET_ARG(3);
297a87295e8Spbrook         if (use_gdb_syscalls()) {
298a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "rename,%s,%s",
2997ba6c104SPeter Maydell                            arg0, (int)arg1, arg2, (int)arg3);
300a87295e8Spbrook             return;
301a87295e8Spbrook         } else {
3027ba6c104SPeter Maydell             p = lock_user_string(arg0);
3037ba6c104SPeter Maydell             q = lock_user_string(arg2);
304579a97f7Sbellard             if (!p || !q) {
305579a97f7Sbellard                 /* FIXME - check error code? */
306579a97f7Sbellard                 result = -1;
307579a97f7Sbellard             } else {
308a87295e8Spbrook                 result = rename(p, q);
309579a97f7Sbellard             }
3107ba6c104SPeter Maydell             unlock_user(p, arg0, 0);
3117ba6c104SPeter Maydell             unlock_user(q, arg2, 0);
312a87295e8Spbrook         }
313a87295e8Spbrook         break;
314a87295e8Spbrook     case HOSTED_UNLINK:
3157ba6c104SPeter Maydell         GET_ARG(0);
3167ba6c104SPeter Maydell         GET_ARG(1);
317a87295e8Spbrook         if (use_gdb_syscalls()) {
318a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "unlink,%s",
3197ba6c104SPeter Maydell                            arg0, (int)arg1);
320a87295e8Spbrook             return;
321a87295e8Spbrook         } else {
3227ba6c104SPeter Maydell             p = lock_user_string(arg0);
3237ba6c104SPeter Maydell             if (!p) {
324579a97f7Sbellard                 /* FIXME - check error code? */
325579a97f7Sbellard                 result = -1;
326579a97f7Sbellard             } else {
327a87295e8Spbrook                 result = unlink(p);
3287ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
329a87295e8Spbrook             }
330579a97f7Sbellard         }
331a87295e8Spbrook         break;
332a87295e8Spbrook     case HOSTED_STAT:
3337ba6c104SPeter Maydell         GET_ARG(0);
3347ba6c104SPeter Maydell         GET_ARG(1);
3357ba6c104SPeter Maydell         GET_ARG(2);
336a87295e8Spbrook         if (use_gdb_syscalls()) {
337a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "stat,%s,%x",
3387ba6c104SPeter Maydell                            arg0, (int)arg1, arg2);
339a87295e8Spbrook             return;
340a87295e8Spbrook         } else {
341a87295e8Spbrook             struct stat s;
3427ba6c104SPeter Maydell             p = lock_user_string(arg0);
3437ba6c104SPeter Maydell             if (!p) {
344579a97f7Sbellard                 /* FIXME - check error code? */
345579a97f7Sbellard                 result = -1;
346579a97f7Sbellard             } else {
347a87295e8Spbrook                 result = stat(p, &s);
3487ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
349579a97f7Sbellard             }
350a87295e8Spbrook             if (result == 0) {
3517ba6c104SPeter Maydell                 translate_stat(env, arg2, &s);
352a87295e8Spbrook             }
353a87295e8Spbrook         }
354a87295e8Spbrook         break;
355a87295e8Spbrook     case HOSTED_FSTAT:
3567ba6c104SPeter Maydell         GET_ARG(0);
3577ba6c104SPeter Maydell         GET_ARG(1);
358a87295e8Spbrook         if (use_gdb_syscalls()) {
359a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "fstat,%x,%x",
3607ba6c104SPeter Maydell                            arg0, arg1);
361a87295e8Spbrook             return;
362a87295e8Spbrook         } else {
363a87295e8Spbrook             struct stat s;
3647ba6c104SPeter Maydell             result = fstat(arg0, &s);
365a87295e8Spbrook             if (result == 0) {
3667ba6c104SPeter Maydell                 translate_stat(env, arg1, &s);
367a87295e8Spbrook             }
368a87295e8Spbrook         }
369a87295e8Spbrook         break;
370a87295e8Spbrook     case HOSTED_GETTIMEOFDAY:
3717ba6c104SPeter Maydell         GET_ARG(0);
3727ba6c104SPeter Maydell         GET_ARG(1);
373a87295e8Spbrook         if (use_gdb_syscalls()) {
374a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "gettimeofday,%x,%x",
3757ba6c104SPeter Maydell                            arg0, arg1);
376a87295e8Spbrook             return;
377a87295e8Spbrook         } else {
37829b3a662Spbrook             qemu_timeval tv;
379a87295e8Spbrook             struct gdb_timeval *p;
38029b3a662Spbrook             result = qemu_gettimeofday(&tv);
381a87295e8Spbrook             if (result != 0) {
382579a97f7Sbellard                 if (!(p = lock_user(VERIFY_WRITE,
3837ba6c104SPeter Maydell                                     arg0, sizeof(struct gdb_timeval), 0))) {
384579a97f7Sbellard                     /* FIXME - check error code? */
385579a97f7Sbellard                     result = -1;
386579a97f7Sbellard                 } else {
387a87295e8Spbrook                     p->tv_sec = cpu_to_be32(tv.tv_sec);
388a87295e8Spbrook                     p->tv_usec = cpu_to_be64(tv.tv_usec);
3897ba6c104SPeter Maydell                     unlock_user(p, arg0, sizeof(struct gdb_timeval));
390a87295e8Spbrook                 }
391a87295e8Spbrook             }
392579a97f7Sbellard         }
393a87295e8Spbrook         break;
394a87295e8Spbrook     case HOSTED_ISATTY:
3957ba6c104SPeter Maydell         GET_ARG(0);
396a87295e8Spbrook         if (use_gdb_syscalls()) {
3977ba6c104SPeter Maydell             gdb_do_syscall(m68k_semi_cb, "isatty,%x", arg0);
398a87295e8Spbrook             return;
399a87295e8Spbrook         } else {
4007ba6c104SPeter Maydell             result = isatty(arg0);
401a87295e8Spbrook         }
402a87295e8Spbrook         break;
403a87295e8Spbrook     case HOSTED_SYSTEM:
4047ba6c104SPeter Maydell         GET_ARG(0);
4057ba6c104SPeter Maydell         GET_ARG(1);
406a87295e8Spbrook         if (use_gdb_syscalls()) {
407a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "system,%s",
4087ba6c104SPeter Maydell                            arg0, (int)arg1);
409a87295e8Spbrook             return;
410a87295e8Spbrook         } else {
4117ba6c104SPeter Maydell             p = lock_user_string(arg0);
4127ba6c104SPeter Maydell             if (!p) {
413579a97f7Sbellard                 /* FIXME - check error code? */
414579a97f7Sbellard                 result = -1;
415579a97f7Sbellard             } else {
416a87295e8Spbrook                 result = system(p);
4177ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
418a87295e8Spbrook             }
419579a97f7Sbellard         }
420a87295e8Spbrook         break;
421a87295e8Spbrook     case HOSTED_INIT_SIM:
422a87295e8Spbrook #if defined(CONFIG_USER_ONLY)
423a87295e8Spbrook         {
4240429a971SAndreas Färber         CPUState *cs = CPU(m68k_env_get_cpu(env));
4250429a971SAndreas Färber         TaskState *ts = cs->opaque;
426a87295e8Spbrook         /* Allocate the heap using sbrk.  */
427a87295e8Spbrook         if (!ts->heap_limit) {
4285382a012SPeter Maydell             abi_ulong ret;
429a87295e8Spbrook             uint32_t size;
430a87295e8Spbrook             uint32_t base;
431a87295e8Spbrook 
432a87295e8Spbrook             base = do_brk(0);
433a87295e8Spbrook             size = SEMIHOSTING_HEAP_SIZE;
434a87295e8Spbrook             /* Try a big heap, and reduce the size if that fails.  */
435a87295e8Spbrook             for (;;) {
436a87295e8Spbrook                 ret = do_brk(base + size);
4375382a012SPeter Maydell                 if (ret >= (base + size)) {
438a87295e8Spbrook                     break;
4395382a012SPeter Maydell                 }
440a87295e8Spbrook                 size >>= 1;
441a87295e8Spbrook             }
442a87295e8Spbrook             ts->heap_limit = base + size;
443a87295e8Spbrook         }
444a87295e8Spbrook         /* This call may happen before we have writable memory, so return
445a87295e8Spbrook            values directly in registers.  */
446a87295e8Spbrook         env->dregs[1] = ts->heap_limit;
447a87295e8Spbrook         env->aregs[7] = ts->stack_base;
448a87295e8Spbrook         }
449a87295e8Spbrook #else
450a87295e8Spbrook         /* FIXME: This is wrong for boards where RAM does not start at
451a87295e8Spbrook            address zero.  */
452a87295e8Spbrook         env->dregs[1] = ram_size;
453a87295e8Spbrook         env->aregs[7] = ram_size;
454a87295e8Spbrook #endif
455a87295e8Spbrook         return;
456a87295e8Spbrook     default:
457a47dddd7SAndreas Färber         cpu_abort(CPU(m68k_env_get_cpu(env)), "Unsupported semihosting syscall %d\n", nr);
458a87295e8Spbrook         result = 0;
459a87295e8Spbrook     }
4607ba6c104SPeter Maydell failed:
4611073bfd8SPeter Maydell     m68k_semi_return_u32(env, result, errno);
462a87295e8Spbrook }
463