xref: /qemu/target/m68k/m68k-semi.c (revision ab294b6c3adfc8a9241f2aaff0709c51acf0370b)
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
28c89a14adSRichard Henderson #include "semihosting/softmmu-uaccess.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 
48a87295e8Spbrook static int translate_openflags(int flags)
49a87295e8Spbrook {
50a87295e8Spbrook     int hf;
51a87295e8Spbrook 
52a87295e8Spbrook     if (flags & GDB_O_WRONLY)
53a87295e8Spbrook         hf = O_WRONLY;
54a87295e8Spbrook     else if (flags & GDB_O_RDWR)
55a87295e8Spbrook         hf = O_RDWR;
56a87295e8Spbrook     else
57a87295e8Spbrook         hf = O_RDONLY;
58a87295e8Spbrook 
59a87295e8Spbrook     if (flags & GDB_O_APPEND) hf |= O_APPEND;
60a87295e8Spbrook     if (flags & GDB_O_CREAT) hf |= O_CREAT;
61a87295e8Spbrook     if (flags & GDB_O_TRUNC) hf |= O_TRUNC;
62a87295e8Spbrook     if (flags & GDB_O_EXCL) hf |= O_EXCL;
63a87295e8Spbrook 
64a87295e8Spbrook     return hf;
65a87295e8Spbrook }
66a87295e8Spbrook 
6771fc85e8SAndreas Färber static void translate_stat(CPUM68KState *env, target_ulong addr, struct stat *s)
68a87295e8Spbrook {
697c56c2d3SRichard Henderson     struct gdb_stat *p;
70a87295e8Spbrook 
717c56c2d3SRichard Henderson     p = lock_user(VERIFY_WRITE, addr, sizeof(struct gdb_stat), 0);
727c56c2d3SRichard Henderson     if (!p) {
73579a97f7Sbellard         /* FIXME - should this return an error code? */
74579a97f7Sbellard         return;
757c56c2d3SRichard Henderson     }
76a87295e8Spbrook     p->gdb_st_dev = cpu_to_be32(s->st_dev);
77a87295e8Spbrook     p->gdb_st_ino = cpu_to_be32(s->st_ino);
78a87295e8Spbrook     p->gdb_st_mode = cpu_to_be32(s->st_mode);
79a87295e8Spbrook     p->gdb_st_nlink = cpu_to_be32(s->st_nlink);
80a87295e8Spbrook     p->gdb_st_uid = cpu_to_be32(s->st_uid);
81a87295e8Spbrook     p->gdb_st_gid = cpu_to_be32(s->st_gid);
82a87295e8Spbrook     p->gdb_st_rdev = cpu_to_be32(s->st_rdev);
83a87295e8Spbrook     p->gdb_st_size = cpu_to_be64(s->st_size);
8429b3a662Spbrook #ifdef _WIN32
8529b3a662Spbrook     /* Windows stat is missing some fields.  */
8629b3a662Spbrook     p->gdb_st_blksize = 0;
8729b3a662Spbrook     p->gdb_st_blocks = 0;
8829b3a662Spbrook #else
89a87295e8Spbrook     p->gdb_st_blksize = cpu_to_be64(s->st_blksize);
90a87295e8Spbrook     p->gdb_st_blocks = cpu_to_be64(s->st_blocks);
9129b3a662Spbrook #endif
92a87295e8Spbrook     p->gdb_st_atime = cpu_to_be32(s->st_atime);
93a87295e8Spbrook     p->gdb_st_mtime = cpu_to_be32(s->st_mtime);
94a87295e8Spbrook     p->gdb_st_ctime = cpu_to_be32(s->st_ctime);
957c56c2d3SRichard Henderson     unlock_user(p, addr, sizeof(struct gdb_stat));
96a87295e8Spbrook }
97a87295e8Spbrook 
98*ab294b6cSRichard Henderson static void m68k_semi_u32_cb(CPUState *cs, uint64_t ret, int err)
991073bfd8SPeter Maydell {
100*ab294b6cSRichard Henderson     M68kCPU *cpu = M68K_CPU(cs);
101*ab294b6cSRichard Henderson     CPUM68KState *env = &cpu->env;
102*ab294b6cSRichard Henderson 
1031073bfd8SPeter Maydell     target_ulong args = env->dregs[1];
1041073bfd8SPeter Maydell     if (put_user_u32(ret, args) ||
1051073bfd8SPeter Maydell         put_user_u32(err, args + 4)) {
106808d77bcSLucien Murray-Pitts         /*
107808d77bcSLucien Murray-Pitts          * The m68k semihosting ABI does not provide any way to report this
1081073bfd8SPeter Maydell          * error to the guest, so the best we can do is log it in qemu.
1091073bfd8SPeter Maydell          * It is always a guest error not to pass us a valid argument block.
1101073bfd8SPeter Maydell          */
1111073bfd8SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value "
1121073bfd8SPeter Maydell                       "discarded because argument block not writable\n");
1131073bfd8SPeter Maydell     }
1141073bfd8SPeter Maydell }
1151073bfd8SPeter Maydell 
116*ab294b6cSRichard Henderson static void m68k_semi_u64_cb(CPUState *cs, uint64_t ret, int err)
1171073bfd8SPeter Maydell {
118*ab294b6cSRichard Henderson     M68kCPU *cpu = M68K_CPU(cs);
119*ab294b6cSRichard Henderson     CPUM68KState *env = &cpu->env;
120*ab294b6cSRichard Henderson 
1211073bfd8SPeter Maydell     target_ulong args = env->dregs[1];
1221073bfd8SPeter Maydell     if (put_user_u32(ret >> 32, args) ||
1231073bfd8SPeter Maydell         put_user_u32(ret, args + 4) ||
1241073bfd8SPeter Maydell         put_user_u32(err, args + 8)) {
1251073bfd8SPeter Maydell         /* No way to report this via m68k semihosting ABI; just log it */
1261073bfd8SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value "
1271073bfd8SPeter Maydell                       "discarded because argument block not writable\n");
1281073bfd8SPeter Maydell     }
1291073bfd8SPeter Maydell }
1301073bfd8SPeter Maydell 
131808d77bcSLucien Murray-Pitts /*
132808d77bcSLucien Murray-Pitts  * Read the input value from the argument block; fail the semihosting
1337ba6c104SPeter Maydell  * call if the memory read fails.
1347ba6c104SPeter Maydell  */
1357ba6c104SPeter Maydell #define GET_ARG(n) do {                                 \
1367ba6c104SPeter Maydell     if (get_user_ual(arg ## n, args + (n) * 4)) {       \
1377ba6c104SPeter Maydell         result = -1;                                    \
1387ba6c104SPeter Maydell         errno = EFAULT;                                 \
1397ba6c104SPeter Maydell         goto failed;                                    \
1407ba6c104SPeter Maydell     }                                                   \
1417ba6c104SPeter Maydell } while (0)
1427ba6c104SPeter Maydell 
143a87295e8Spbrook void do_m68k_semihosting(CPUM68KState *env, int nr)
144a87295e8Spbrook {
145*ab294b6cSRichard Henderson     CPUState *cs = env_cpu(env);
146a87295e8Spbrook     uint32_t args;
1477ba6c104SPeter Maydell     target_ulong arg0, arg1, arg2, arg3;
148a87295e8Spbrook     void *p;
149a87295e8Spbrook     void *q;
150a87295e8Spbrook     uint32_t len;
151a87295e8Spbrook     uint32_t result;
152a87295e8Spbrook 
153a87295e8Spbrook     args = env->dregs[1];
154a87295e8Spbrook     switch (nr) {
155a87295e8Spbrook     case HOSTED_EXIT:
156ad9dcb20SAlex Bennée         gdb_exit(env->dregs[0]);
157a87295e8Spbrook         exit(env->dregs[0]);
158a87295e8Spbrook     case HOSTED_OPEN:
1597ba6c104SPeter Maydell         GET_ARG(0);
1607ba6c104SPeter Maydell         GET_ARG(1);
1617ba6c104SPeter Maydell         GET_ARG(2);
1627ba6c104SPeter Maydell         GET_ARG(3);
163a87295e8Spbrook         if (use_gdb_syscalls()) {
164*ab294b6cSRichard Henderson             gdb_do_syscall(m68k_semi_u32_cb, "open,%s,%x,%x", arg0, (int)arg1,
1657ba6c104SPeter Maydell                            arg2, arg3);
166a87295e8Spbrook             return;
167a87295e8Spbrook         } else {
1687ba6c104SPeter Maydell             p = lock_user_string(arg0);
1697ba6c104SPeter Maydell             if (!p) {
170579a97f7Sbellard                 /* FIXME - check error code? */
171579a97f7Sbellard                 result = -1;
172579a97f7Sbellard             } else {
1737ba6c104SPeter Maydell                 result = open(p, translate_openflags(arg2), arg3);
1747ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
175a87295e8Spbrook             }
176579a97f7Sbellard         }
177a87295e8Spbrook         break;
178a87295e8Spbrook     case HOSTED_CLOSE:
179a87295e8Spbrook         {
180a87295e8Spbrook             /* Ignore attempts to close stdin/out/err.  */
1817ba6c104SPeter Maydell             GET_ARG(0);
1827ba6c104SPeter Maydell             int fd = arg0;
183a87295e8Spbrook             if (fd > 2) {
184a87295e8Spbrook                 if (use_gdb_syscalls()) {
185*ab294b6cSRichard Henderson                     gdb_do_syscall(m68k_semi_u32_cb, "close,%x", arg0);
186a87295e8Spbrook                     return;
187a87295e8Spbrook                 } else {
188a87295e8Spbrook                     result = close(fd);
189a87295e8Spbrook                 }
190a87295e8Spbrook             } else {
191a87295e8Spbrook                 result = 0;
192a87295e8Spbrook             }
193a87295e8Spbrook             break;
194a87295e8Spbrook         }
195a87295e8Spbrook     case HOSTED_READ:
1967ba6c104SPeter Maydell         GET_ARG(0);
1977ba6c104SPeter Maydell         GET_ARG(1);
1987ba6c104SPeter Maydell         GET_ARG(2);
1997ba6c104SPeter Maydell         len = arg2;
200a87295e8Spbrook         if (use_gdb_syscalls()) {
201*ab294b6cSRichard Henderson             gdb_do_syscall(m68k_semi_u32_cb, "read,%x,%x,%x",
2027ba6c104SPeter Maydell                            arg0, arg1, len);
203a87295e8Spbrook             return;
204a87295e8Spbrook         } else {
2057ba6c104SPeter Maydell             p = lock_user(VERIFY_WRITE, arg1, len, 0);
2067ba6c104SPeter Maydell             if (!p) {
207579a97f7Sbellard                 /* FIXME - check error code? */
208579a97f7Sbellard                 result = -1;
209579a97f7Sbellard             } else {
2107ba6c104SPeter Maydell                 result = read(arg0, p, len);
2117ba6c104SPeter Maydell                 unlock_user(p, arg1, len);
212a87295e8Spbrook             }
213579a97f7Sbellard         }
214a87295e8Spbrook         break;
215a87295e8Spbrook     case HOSTED_WRITE:
2167ba6c104SPeter Maydell         GET_ARG(0);
2177ba6c104SPeter Maydell         GET_ARG(1);
2187ba6c104SPeter Maydell         GET_ARG(2);
2197ba6c104SPeter Maydell         len = arg2;
220a87295e8Spbrook         if (use_gdb_syscalls()) {
221*ab294b6cSRichard Henderson             gdb_do_syscall(m68k_semi_u32_cb, "write,%x,%x,%x",
2227ba6c104SPeter Maydell                            arg0, arg1, len);
223a87295e8Spbrook             return;
224a87295e8Spbrook         } else {
2257ba6c104SPeter Maydell             p = lock_user(VERIFY_READ, arg1, len, 1);
2267ba6c104SPeter Maydell             if (!p) {
227579a97f7Sbellard                 /* FIXME - check error code? */
228579a97f7Sbellard                 result = -1;
229579a97f7Sbellard             } else {
2307ba6c104SPeter Maydell                 result = write(arg0, p, len);
2317ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
232a87295e8Spbrook             }
233579a97f7Sbellard         }
234a87295e8Spbrook         break;
235a87295e8Spbrook     case HOSTED_LSEEK:
236a87295e8Spbrook         {
237a87295e8Spbrook             uint64_t off;
2387ba6c104SPeter Maydell             GET_ARG(0);
2397ba6c104SPeter Maydell             GET_ARG(1);
2407ba6c104SPeter Maydell             GET_ARG(2);
2417ba6c104SPeter Maydell             GET_ARG(3);
2427ba6c104SPeter Maydell             off = (uint32_t)arg2 | ((uint64_t)arg1 << 32);
243a87295e8Spbrook             if (use_gdb_syscalls()) {
244*ab294b6cSRichard Henderson                 gdb_do_syscall(m68k_semi_u64_cb, "fseek,%x,%lx,%x",
2457ba6c104SPeter Maydell                                arg0, off, arg3);
246a87295e8Spbrook             } else {
2477ba6c104SPeter Maydell                 off = lseek(arg0, off, arg3);
248*ab294b6cSRichard Henderson                 m68k_semi_u64_cb(cs, off, errno);
249a87295e8Spbrook             }
250a87295e8Spbrook             return;
251a87295e8Spbrook         }
252a87295e8Spbrook     case HOSTED_RENAME:
2537ba6c104SPeter Maydell         GET_ARG(0);
2547ba6c104SPeter Maydell         GET_ARG(1);
2557ba6c104SPeter Maydell         GET_ARG(2);
2567ba6c104SPeter Maydell         GET_ARG(3);
257a87295e8Spbrook         if (use_gdb_syscalls()) {
258*ab294b6cSRichard Henderson             gdb_do_syscall(m68k_semi_u32_cb, "rename,%s,%s",
2597ba6c104SPeter Maydell                            arg0, (int)arg1, arg2, (int)arg3);
260a87295e8Spbrook             return;
261a87295e8Spbrook         } else {
2627ba6c104SPeter Maydell             p = lock_user_string(arg0);
2637ba6c104SPeter Maydell             q = lock_user_string(arg2);
264579a97f7Sbellard             if (!p || !q) {
265579a97f7Sbellard                 /* FIXME - check error code? */
266579a97f7Sbellard                 result = -1;
267579a97f7Sbellard             } else {
268a87295e8Spbrook                 result = rename(p, q);
269579a97f7Sbellard             }
2707ba6c104SPeter Maydell             unlock_user(p, arg0, 0);
2717ba6c104SPeter Maydell             unlock_user(q, arg2, 0);
272a87295e8Spbrook         }
273a87295e8Spbrook         break;
274a87295e8Spbrook     case HOSTED_UNLINK:
2757ba6c104SPeter Maydell         GET_ARG(0);
2767ba6c104SPeter Maydell         GET_ARG(1);
277a87295e8Spbrook         if (use_gdb_syscalls()) {
278*ab294b6cSRichard Henderson             gdb_do_syscall(m68k_semi_u32_cb, "unlink,%s",
2797ba6c104SPeter Maydell                            arg0, (int)arg1);
280a87295e8Spbrook             return;
281a87295e8Spbrook         } else {
2827ba6c104SPeter Maydell             p = lock_user_string(arg0);
2837ba6c104SPeter Maydell             if (!p) {
284579a97f7Sbellard                 /* FIXME - check error code? */
285579a97f7Sbellard                 result = -1;
286579a97f7Sbellard             } else {
287a87295e8Spbrook                 result = unlink(p);
2887ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
289a87295e8Spbrook             }
290579a97f7Sbellard         }
291a87295e8Spbrook         break;
292a87295e8Spbrook     case HOSTED_STAT:
2937ba6c104SPeter Maydell         GET_ARG(0);
2947ba6c104SPeter Maydell         GET_ARG(1);
2957ba6c104SPeter Maydell         GET_ARG(2);
296a87295e8Spbrook         if (use_gdb_syscalls()) {
297*ab294b6cSRichard Henderson             gdb_do_syscall(m68k_semi_u32_cb, "stat,%s,%x",
2987ba6c104SPeter Maydell                            arg0, (int)arg1, arg2);
299a87295e8Spbrook             return;
300a87295e8Spbrook         } else {
301a87295e8Spbrook             struct stat s;
3027ba6c104SPeter Maydell             p = lock_user_string(arg0);
3037ba6c104SPeter Maydell             if (!p) {
304579a97f7Sbellard                 /* FIXME - check error code? */
305579a97f7Sbellard                 result = -1;
306579a97f7Sbellard             } else {
307a87295e8Spbrook                 result = stat(p, &s);
3087ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
309579a97f7Sbellard             }
310a87295e8Spbrook             if (result == 0) {
3117ba6c104SPeter Maydell                 translate_stat(env, arg2, &s);
312a87295e8Spbrook             }
313a87295e8Spbrook         }
314a87295e8Spbrook         break;
315a87295e8Spbrook     case HOSTED_FSTAT:
3167ba6c104SPeter Maydell         GET_ARG(0);
3177ba6c104SPeter Maydell         GET_ARG(1);
318a87295e8Spbrook         if (use_gdb_syscalls()) {
319*ab294b6cSRichard Henderson             gdb_do_syscall(m68k_semi_u32_cb, "fstat,%x,%x",
3207ba6c104SPeter Maydell                            arg0, arg1);
321a87295e8Spbrook             return;
322a87295e8Spbrook         } else {
323a87295e8Spbrook             struct stat s;
3247ba6c104SPeter Maydell             result = fstat(arg0, &s);
325a87295e8Spbrook             if (result == 0) {
3267ba6c104SPeter Maydell                 translate_stat(env, arg1, &s);
327a87295e8Spbrook             }
328a87295e8Spbrook         }
329a87295e8Spbrook         break;
330a87295e8Spbrook     case HOSTED_GETTIMEOFDAY:
3317ba6c104SPeter Maydell         GET_ARG(0);
3327ba6c104SPeter Maydell         GET_ARG(1);
333a87295e8Spbrook         if (use_gdb_syscalls()) {
334*ab294b6cSRichard Henderson             gdb_do_syscall(m68k_semi_u32_cb, "gettimeofday,%x,%x",
3357ba6c104SPeter Maydell                            arg0, arg1);
336a87295e8Spbrook             return;
337a87295e8Spbrook         } else {
338a87295e8Spbrook             struct gdb_timeval *p;
339f793dde0SMarc-André Lureau             int64_t rt = g_get_real_time();
340f793dde0SMarc-André Lureau             p = lock_user(VERIFY_WRITE, arg0, sizeof(struct gdb_timeval), 0);
341f793dde0SMarc-André Lureau             if (!p) {
342579a97f7Sbellard                 /* FIXME - check error code? */
343579a97f7Sbellard                 result = -1;
344579a97f7Sbellard             } else {
345f793dde0SMarc-André Lureau                 result = 0;
346f793dde0SMarc-André Lureau                 p->tv_sec = cpu_to_be32(rt / G_USEC_PER_SEC);
347f793dde0SMarc-André Lureau                 p->tv_usec = cpu_to_be64(rt % G_USEC_PER_SEC);
3487ba6c104SPeter Maydell                 unlock_user(p, arg0, sizeof(struct gdb_timeval));
349a87295e8Spbrook             }
350a87295e8Spbrook         }
351a87295e8Spbrook         break;
352a87295e8Spbrook     case HOSTED_ISATTY:
3537ba6c104SPeter Maydell         GET_ARG(0);
354a87295e8Spbrook         if (use_gdb_syscalls()) {
355*ab294b6cSRichard Henderson             gdb_do_syscall(m68k_semi_u32_cb, "isatty,%x", arg0);
356a87295e8Spbrook             return;
357a87295e8Spbrook         } else {
3587ba6c104SPeter Maydell             result = isatty(arg0);
359a87295e8Spbrook         }
360a87295e8Spbrook         break;
361a87295e8Spbrook     case HOSTED_SYSTEM:
3627ba6c104SPeter Maydell         GET_ARG(0);
3637ba6c104SPeter Maydell         GET_ARG(1);
364a87295e8Spbrook         if (use_gdb_syscalls()) {
365*ab294b6cSRichard Henderson             gdb_do_syscall(m68k_semi_u32_cb, "system,%s",
3667ba6c104SPeter Maydell                            arg0, (int)arg1);
367a87295e8Spbrook             return;
368a87295e8Spbrook         } else {
3697ba6c104SPeter Maydell             p = lock_user_string(arg0);
3707ba6c104SPeter Maydell             if (!p) {
371579a97f7Sbellard                 /* FIXME - check error code? */
372579a97f7Sbellard                 result = -1;
373579a97f7Sbellard             } else {
374a87295e8Spbrook                 result = system(p);
3757ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
376a87295e8Spbrook             }
377579a97f7Sbellard         }
378a87295e8Spbrook         break;
379a87295e8Spbrook     case HOSTED_INIT_SIM:
380a87295e8Spbrook #if defined(CONFIG_USER_ONLY)
381a87295e8Spbrook         {
382a8d92fd8SRichard Henderson         CPUState *cs = env_cpu(env);
3830429a971SAndreas Färber         TaskState *ts = cs->opaque;
384a87295e8Spbrook         /* Allocate the heap using sbrk.  */
385a87295e8Spbrook         if (!ts->heap_limit) {
3865382a012SPeter Maydell             abi_ulong ret;
387a87295e8Spbrook             uint32_t size;
388a87295e8Spbrook             uint32_t base;
389a87295e8Spbrook 
390a87295e8Spbrook             base = do_brk(0);
391a87295e8Spbrook             size = SEMIHOSTING_HEAP_SIZE;
392a87295e8Spbrook             /* Try a big heap, and reduce the size if that fails.  */
393a87295e8Spbrook             for (;;) {
394a87295e8Spbrook                 ret = do_brk(base + size);
3955382a012SPeter Maydell                 if (ret >= (base + size)) {
396a87295e8Spbrook                     break;
3975382a012SPeter Maydell                 }
398a87295e8Spbrook                 size >>= 1;
399a87295e8Spbrook             }
400a87295e8Spbrook             ts->heap_limit = base + size;
401a87295e8Spbrook         }
402808d77bcSLucien Murray-Pitts         /*
403808d77bcSLucien Murray-Pitts          * This call may happen before we have writable memory, so return
404808d77bcSLucien Murray-Pitts          * values directly in registers.
405808d77bcSLucien Murray-Pitts          */
406a87295e8Spbrook         env->dregs[1] = ts->heap_limit;
407a87295e8Spbrook         env->aregs[7] = ts->stack_base;
408a87295e8Spbrook         }
409a87295e8Spbrook #else
410808d77bcSLucien Murray-Pitts         /*
411808d77bcSLucien Murray-Pitts          * FIXME: This is wrong for boards where RAM does not start at
412808d77bcSLucien Murray-Pitts          * address zero.
413808d77bcSLucien Murray-Pitts          */
4145601d241SPaolo Bonzini         env->dregs[1] = current_machine->ram_size;
4155601d241SPaolo Bonzini         env->aregs[7] = current_machine->ram_size;
416a87295e8Spbrook #endif
417a87295e8Spbrook         return;
418a87295e8Spbrook     default:
419a8d92fd8SRichard Henderson         cpu_abort(env_cpu(env), "Unsupported semihosting syscall %d\n", nr);
420a87295e8Spbrook         result = 0;
421a87295e8Spbrook     }
4227ba6c104SPeter Maydell failed:
423*ab294b6cSRichard Henderson     m68k_semi_u32_cb(cs, result, errno);
424a87295e8Spbrook }
425