xref: /qemu/target/m68k/m68k-semi.c (revision 7c56c2d3da2d25cb202e6b47ad7348a42b950b76)
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 {
69*7c56c2d3SRichard Henderson     struct gdb_stat *p;
70a87295e8Spbrook 
71*7c56c2d3SRichard Henderson     p = lock_user(VERIFY_WRITE, addr, sizeof(struct gdb_stat), 0);
72*7c56c2d3SRichard Henderson     if (!p) {
73579a97f7Sbellard         /* FIXME - should this return an error code? */
74579a97f7Sbellard         return;
75*7c56c2d3SRichard 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);
95*7c56c2d3SRichard Henderson     unlock_user(p, addr, sizeof(struct gdb_stat));
96a87295e8Spbrook }
97a87295e8Spbrook 
981073bfd8SPeter Maydell static void m68k_semi_return_u32(CPUM68KState *env, uint32_t ret, uint32_t err)
991073bfd8SPeter Maydell {
1001073bfd8SPeter Maydell     target_ulong args = env->dregs[1];
1011073bfd8SPeter Maydell     if (put_user_u32(ret, args) ||
1021073bfd8SPeter Maydell         put_user_u32(err, args + 4)) {
103808d77bcSLucien Murray-Pitts         /*
104808d77bcSLucien Murray-Pitts          * The m68k semihosting ABI does not provide any way to report this
1051073bfd8SPeter Maydell          * error to the guest, so the best we can do is log it in qemu.
1061073bfd8SPeter Maydell          * It is always a guest error not to pass us a valid argument block.
1071073bfd8SPeter Maydell          */
1081073bfd8SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value "
1091073bfd8SPeter Maydell                       "discarded because argument block not writable\n");
1101073bfd8SPeter Maydell     }
1111073bfd8SPeter Maydell }
1121073bfd8SPeter Maydell 
1131073bfd8SPeter Maydell static void m68k_semi_return_u64(CPUM68KState *env, uint64_t ret, uint32_t err)
1141073bfd8SPeter Maydell {
1151073bfd8SPeter Maydell     target_ulong args = env->dregs[1];
1161073bfd8SPeter Maydell     if (put_user_u32(ret >> 32, args) ||
1171073bfd8SPeter Maydell         put_user_u32(ret, args + 4) ||
1181073bfd8SPeter Maydell         put_user_u32(err, args + 8)) {
1191073bfd8SPeter Maydell         /* No way to report this via m68k semihosting ABI; just log it */
1201073bfd8SPeter Maydell         qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value "
1211073bfd8SPeter Maydell                       "discarded because argument block not writable\n");
1221073bfd8SPeter Maydell     }
1231073bfd8SPeter Maydell }
1241073bfd8SPeter Maydell 
125a87295e8Spbrook static int m68k_semi_is_fseek;
126a87295e8Spbrook 
1279e0c5422SAndreas Färber static void m68k_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
128a87295e8Spbrook {
1299e0c5422SAndreas Färber     M68kCPU *cpu = M68K_CPU(cs);
1309e0c5422SAndreas Färber     CPUM68KState *env = &cpu->env;
1319e0c5422SAndreas Färber 
132a87295e8Spbrook     if (m68k_semi_is_fseek) {
133808d77bcSLucien Murray-Pitts         /*
134808d77bcSLucien Murray-Pitts          * FIXME: We've already lost the high bits of the fseek
135808d77bcSLucien Murray-Pitts          * return value.
136808d77bcSLucien Murray-Pitts          */
1371073bfd8SPeter Maydell         m68k_semi_return_u64(env, ret, err);
138a87295e8Spbrook         m68k_semi_is_fseek = 0;
1391073bfd8SPeter Maydell     } else {
1401073bfd8SPeter Maydell         m68k_semi_return_u32(env, ret, err);
141a87295e8Spbrook     }
142a87295e8Spbrook }
143a87295e8Spbrook 
144808d77bcSLucien Murray-Pitts /*
145808d77bcSLucien Murray-Pitts  * Read the input value from the argument block; fail the semihosting
1467ba6c104SPeter Maydell  * call if the memory read fails.
1477ba6c104SPeter Maydell  */
1487ba6c104SPeter Maydell #define GET_ARG(n) do {                                 \
1497ba6c104SPeter Maydell     if (get_user_ual(arg ## n, args + (n) * 4)) {       \
1507ba6c104SPeter Maydell         result = -1;                                    \
1517ba6c104SPeter Maydell         errno = EFAULT;                                 \
1527ba6c104SPeter Maydell         goto failed;                                    \
1537ba6c104SPeter Maydell     }                                                   \
1547ba6c104SPeter Maydell } while (0)
1557ba6c104SPeter Maydell 
156a87295e8Spbrook void do_m68k_semihosting(CPUM68KState *env, int nr)
157a87295e8Spbrook {
158a87295e8Spbrook     uint32_t args;
1597ba6c104SPeter Maydell     target_ulong arg0, arg1, arg2, arg3;
160a87295e8Spbrook     void *p;
161a87295e8Spbrook     void *q;
162a87295e8Spbrook     uint32_t len;
163a87295e8Spbrook     uint32_t result;
164a87295e8Spbrook 
165a87295e8Spbrook     args = env->dregs[1];
166a87295e8Spbrook     switch (nr) {
167a87295e8Spbrook     case HOSTED_EXIT:
168ad9dcb20SAlex Bennée         gdb_exit(env->dregs[0]);
169a87295e8Spbrook         exit(env->dregs[0]);
170a87295e8Spbrook     case HOSTED_OPEN:
1717ba6c104SPeter Maydell         GET_ARG(0);
1727ba6c104SPeter Maydell         GET_ARG(1);
1737ba6c104SPeter Maydell         GET_ARG(2);
1747ba6c104SPeter Maydell         GET_ARG(3);
175a87295e8Spbrook         if (use_gdb_syscalls()) {
1767ba6c104SPeter Maydell             gdb_do_syscall(m68k_semi_cb, "open,%s,%x,%x", arg0, (int)arg1,
1777ba6c104SPeter Maydell                            arg2, arg3);
178a87295e8Spbrook             return;
179a87295e8Spbrook         } else {
1807ba6c104SPeter Maydell             p = lock_user_string(arg0);
1817ba6c104SPeter Maydell             if (!p) {
182579a97f7Sbellard                 /* FIXME - check error code? */
183579a97f7Sbellard                 result = -1;
184579a97f7Sbellard             } else {
1857ba6c104SPeter Maydell                 result = open(p, translate_openflags(arg2), arg3);
1867ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
187a87295e8Spbrook             }
188579a97f7Sbellard         }
189a87295e8Spbrook         break;
190a87295e8Spbrook     case HOSTED_CLOSE:
191a87295e8Spbrook         {
192a87295e8Spbrook             /* Ignore attempts to close stdin/out/err.  */
1937ba6c104SPeter Maydell             GET_ARG(0);
1947ba6c104SPeter Maydell             int fd = arg0;
195a87295e8Spbrook             if (fd > 2) {
196a87295e8Spbrook                 if (use_gdb_syscalls()) {
1977ba6c104SPeter Maydell                     gdb_do_syscall(m68k_semi_cb, "close,%x", arg0);
198a87295e8Spbrook                     return;
199a87295e8Spbrook                 } else {
200a87295e8Spbrook                     result = close(fd);
201a87295e8Spbrook                 }
202a87295e8Spbrook             } else {
203a87295e8Spbrook                 result = 0;
204a87295e8Spbrook             }
205a87295e8Spbrook             break;
206a87295e8Spbrook         }
207a87295e8Spbrook     case HOSTED_READ:
2087ba6c104SPeter Maydell         GET_ARG(0);
2097ba6c104SPeter Maydell         GET_ARG(1);
2107ba6c104SPeter Maydell         GET_ARG(2);
2117ba6c104SPeter Maydell         len = arg2;
212a87295e8Spbrook         if (use_gdb_syscalls()) {
213a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "read,%x,%x,%x",
2147ba6c104SPeter Maydell                            arg0, arg1, len);
215a87295e8Spbrook             return;
216a87295e8Spbrook         } else {
2177ba6c104SPeter Maydell             p = lock_user(VERIFY_WRITE, arg1, len, 0);
2187ba6c104SPeter Maydell             if (!p) {
219579a97f7Sbellard                 /* FIXME - check error code? */
220579a97f7Sbellard                 result = -1;
221579a97f7Sbellard             } else {
2227ba6c104SPeter Maydell                 result = read(arg0, p, len);
2237ba6c104SPeter Maydell                 unlock_user(p, arg1, len);
224a87295e8Spbrook             }
225579a97f7Sbellard         }
226a87295e8Spbrook         break;
227a87295e8Spbrook     case HOSTED_WRITE:
2287ba6c104SPeter Maydell         GET_ARG(0);
2297ba6c104SPeter Maydell         GET_ARG(1);
2307ba6c104SPeter Maydell         GET_ARG(2);
2317ba6c104SPeter Maydell         len = arg2;
232a87295e8Spbrook         if (use_gdb_syscalls()) {
233a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "write,%x,%x,%x",
2347ba6c104SPeter Maydell                            arg0, arg1, len);
235a87295e8Spbrook             return;
236a87295e8Spbrook         } else {
2377ba6c104SPeter Maydell             p = lock_user(VERIFY_READ, arg1, len, 1);
2387ba6c104SPeter Maydell             if (!p) {
239579a97f7Sbellard                 /* FIXME - check error code? */
240579a97f7Sbellard                 result = -1;
241579a97f7Sbellard             } else {
2427ba6c104SPeter Maydell                 result = write(arg0, p, len);
2437ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
244a87295e8Spbrook             }
245579a97f7Sbellard         }
246a87295e8Spbrook         break;
247a87295e8Spbrook     case HOSTED_LSEEK:
248a87295e8Spbrook         {
249a87295e8Spbrook             uint64_t off;
2507ba6c104SPeter Maydell             GET_ARG(0);
2517ba6c104SPeter Maydell             GET_ARG(1);
2527ba6c104SPeter Maydell             GET_ARG(2);
2537ba6c104SPeter Maydell             GET_ARG(3);
2547ba6c104SPeter Maydell             off = (uint32_t)arg2 | ((uint64_t)arg1 << 32);
255a87295e8Spbrook             if (use_gdb_syscalls()) {
256a87295e8Spbrook                 m68k_semi_is_fseek = 1;
257a87295e8Spbrook                 gdb_do_syscall(m68k_semi_cb, "fseek,%x,%lx,%x",
2587ba6c104SPeter Maydell                                arg0, off, arg3);
259a87295e8Spbrook             } else {
2607ba6c104SPeter Maydell                 off = lseek(arg0, off, arg3);
2611073bfd8SPeter Maydell                 m68k_semi_return_u64(env, off, errno);
262a87295e8Spbrook             }
263a87295e8Spbrook             return;
264a87295e8Spbrook         }
265a87295e8Spbrook     case HOSTED_RENAME:
2667ba6c104SPeter Maydell         GET_ARG(0);
2677ba6c104SPeter Maydell         GET_ARG(1);
2687ba6c104SPeter Maydell         GET_ARG(2);
2697ba6c104SPeter Maydell         GET_ARG(3);
270a87295e8Spbrook         if (use_gdb_syscalls()) {
271a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "rename,%s,%s",
2727ba6c104SPeter Maydell                            arg0, (int)arg1, arg2, (int)arg3);
273a87295e8Spbrook             return;
274a87295e8Spbrook         } else {
2757ba6c104SPeter Maydell             p = lock_user_string(arg0);
2767ba6c104SPeter Maydell             q = lock_user_string(arg2);
277579a97f7Sbellard             if (!p || !q) {
278579a97f7Sbellard                 /* FIXME - check error code? */
279579a97f7Sbellard                 result = -1;
280579a97f7Sbellard             } else {
281a87295e8Spbrook                 result = rename(p, q);
282579a97f7Sbellard             }
2837ba6c104SPeter Maydell             unlock_user(p, arg0, 0);
2847ba6c104SPeter Maydell             unlock_user(q, arg2, 0);
285a87295e8Spbrook         }
286a87295e8Spbrook         break;
287a87295e8Spbrook     case HOSTED_UNLINK:
2887ba6c104SPeter Maydell         GET_ARG(0);
2897ba6c104SPeter Maydell         GET_ARG(1);
290a87295e8Spbrook         if (use_gdb_syscalls()) {
291a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "unlink,%s",
2927ba6c104SPeter Maydell                            arg0, (int)arg1);
293a87295e8Spbrook             return;
294a87295e8Spbrook         } else {
2957ba6c104SPeter Maydell             p = lock_user_string(arg0);
2967ba6c104SPeter Maydell             if (!p) {
297579a97f7Sbellard                 /* FIXME - check error code? */
298579a97f7Sbellard                 result = -1;
299579a97f7Sbellard             } else {
300a87295e8Spbrook                 result = unlink(p);
3017ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
302a87295e8Spbrook             }
303579a97f7Sbellard         }
304a87295e8Spbrook         break;
305a87295e8Spbrook     case HOSTED_STAT:
3067ba6c104SPeter Maydell         GET_ARG(0);
3077ba6c104SPeter Maydell         GET_ARG(1);
3087ba6c104SPeter Maydell         GET_ARG(2);
309a87295e8Spbrook         if (use_gdb_syscalls()) {
310a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "stat,%s,%x",
3117ba6c104SPeter Maydell                            arg0, (int)arg1, arg2);
312a87295e8Spbrook             return;
313a87295e8Spbrook         } else {
314a87295e8Spbrook             struct stat s;
3157ba6c104SPeter Maydell             p = lock_user_string(arg0);
3167ba6c104SPeter Maydell             if (!p) {
317579a97f7Sbellard                 /* FIXME - check error code? */
318579a97f7Sbellard                 result = -1;
319579a97f7Sbellard             } else {
320a87295e8Spbrook                 result = stat(p, &s);
3217ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
322579a97f7Sbellard             }
323a87295e8Spbrook             if (result == 0) {
3247ba6c104SPeter Maydell                 translate_stat(env, arg2, &s);
325a87295e8Spbrook             }
326a87295e8Spbrook         }
327a87295e8Spbrook         break;
328a87295e8Spbrook     case HOSTED_FSTAT:
3297ba6c104SPeter Maydell         GET_ARG(0);
3307ba6c104SPeter Maydell         GET_ARG(1);
331a87295e8Spbrook         if (use_gdb_syscalls()) {
332a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "fstat,%x,%x",
3337ba6c104SPeter Maydell                            arg0, arg1);
334a87295e8Spbrook             return;
335a87295e8Spbrook         } else {
336a87295e8Spbrook             struct stat s;
3377ba6c104SPeter Maydell             result = fstat(arg0, &s);
338a87295e8Spbrook             if (result == 0) {
3397ba6c104SPeter Maydell                 translate_stat(env, arg1, &s);
340a87295e8Spbrook             }
341a87295e8Spbrook         }
342a87295e8Spbrook         break;
343a87295e8Spbrook     case HOSTED_GETTIMEOFDAY:
3447ba6c104SPeter Maydell         GET_ARG(0);
3457ba6c104SPeter Maydell         GET_ARG(1);
346a87295e8Spbrook         if (use_gdb_syscalls()) {
347a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "gettimeofday,%x,%x",
3487ba6c104SPeter Maydell                            arg0, arg1);
349a87295e8Spbrook             return;
350a87295e8Spbrook         } else {
351a87295e8Spbrook             struct gdb_timeval *p;
352f793dde0SMarc-André Lureau             int64_t rt = g_get_real_time();
353f793dde0SMarc-André Lureau             p = lock_user(VERIFY_WRITE, arg0, sizeof(struct gdb_timeval), 0);
354f793dde0SMarc-André Lureau             if (!p) {
355579a97f7Sbellard                 /* FIXME - check error code? */
356579a97f7Sbellard                 result = -1;
357579a97f7Sbellard             } else {
358f793dde0SMarc-André Lureau                 result = 0;
359f793dde0SMarc-André Lureau                 p->tv_sec = cpu_to_be32(rt / G_USEC_PER_SEC);
360f793dde0SMarc-André Lureau                 p->tv_usec = cpu_to_be64(rt % G_USEC_PER_SEC);
3617ba6c104SPeter Maydell                 unlock_user(p, arg0, sizeof(struct gdb_timeval));
362a87295e8Spbrook             }
363a87295e8Spbrook         }
364a87295e8Spbrook         break;
365a87295e8Spbrook     case HOSTED_ISATTY:
3667ba6c104SPeter Maydell         GET_ARG(0);
367a87295e8Spbrook         if (use_gdb_syscalls()) {
3687ba6c104SPeter Maydell             gdb_do_syscall(m68k_semi_cb, "isatty,%x", arg0);
369a87295e8Spbrook             return;
370a87295e8Spbrook         } else {
3717ba6c104SPeter Maydell             result = isatty(arg0);
372a87295e8Spbrook         }
373a87295e8Spbrook         break;
374a87295e8Spbrook     case HOSTED_SYSTEM:
3757ba6c104SPeter Maydell         GET_ARG(0);
3767ba6c104SPeter Maydell         GET_ARG(1);
377a87295e8Spbrook         if (use_gdb_syscalls()) {
378a87295e8Spbrook             gdb_do_syscall(m68k_semi_cb, "system,%s",
3797ba6c104SPeter Maydell                            arg0, (int)arg1);
380a87295e8Spbrook             return;
381a87295e8Spbrook         } else {
3827ba6c104SPeter Maydell             p = lock_user_string(arg0);
3837ba6c104SPeter Maydell             if (!p) {
384579a97f7Sbellard                 /* FIXME - check error code? */
385579a97f7Sbellard                 result = -1;
386579a97f7Sbellard             } else {
387a87295e8Spbrook                 result = system(p);
3887ba6c104SPeter Maydell                 unlock_user(p, arg0, 0);
389a87295e8Spbrook             }
390579a97f7Sbellard         }
391a87295e8Spbrook         break;
392a87295e8Spbrook     case HOSTED_INIT_SIM:
393a87295e8Spbrook #if defined(CONFIG_USER_ONLY)
394a87295e8Spbrook         {
395a8d92fd8SRichard Henderson         CPUState *cs = env_cpu(env);
3960429a971SAndreas Färber         TaskState *ts = cs->opaque;
397a87295e8Spbrook         /* Allocate the heap using sbrk.  */
398a87295e8Spbrook         if (!ts->heap_limit) {
3995382a012SPeter Maydell             abi_ulong ret;
400a87295e8Spbrook             uint32_t size;
401a87295e8Spbrook             uint32_t base;
402a87295e8Spbrook 
403a87295e8Spbrook             base = do_brk(0);
404a87295e8Spbrook             size = SEMIHOSTING_HEAP_SIZE;
405a87295e8Spbrook             /* Try a big heap, and reduce the size if that fails.  */
406a87295e8Spbrook             for (;;) {
407a87295e8Spbrook                 ret = do_brk(base + size);
4085382a012SPeter Maydell                 if (ret >= (base + size)) {
409a87295e8Spbrook                     break;
4105382a012SPeter Maydell                 }
411a87295e8Spbrook                 size >>= 1;
412a87295e8Spbrook             }
413a87295e8Spbrook             ts->heap_limit = base + size;
414a87295e8Spbrook         }
415808d77bcSLucien Murray-Pitts         /*
416808d77bcSLucien Murray-Pitts          * This call may happen before we have writable memory, so return
417808d77bcSLucien Murray-Pitts          * values directly in registers.
418808d77bcSLucien Murray-Pitts          */
419a87295e8Spbrook         env->dregs[1] = ts->heap_limit;
420a87295e8Spbrook         env->aregs[7] = ts->stack_base;
421a87295e8Spbrook         }
422a87295e8Spbrook #else
423808d77bcSLucien Murray-Pitts         /*
424808d77bcSLucien Murray-Pitts          * FIXME: This is wrong for boards where RAM does not start at
425808d77bcSLucien Murray-Pitts          * address zero.
426808d77bcSLucien Murray-Pitts          */
4275601d241SPaolo Bonzini         env->dregs[1] = current_machine->ram_size;
4285601d241SPaolo Bonzini         env->aregs[7] = current_machine->ram_size;
429a87295e8Spbrook #endif
430a87295e8Spbrook         return;
431a87295e8Spbrook     default:
432a8d92fd8SRichard Henderson         cpu_abort(env_cpu(env), "Unsupported semihosting syscall %d\n", nr);
433a87295e8Spbrook         result = 0;
434a87295e8Spbrook     }
4357ba6c104SPeter Maydell failed:
4361073bfd8SPeter Maydell     m68k_semi_return_u32(env, result, errno);
437a87295e8Spbrook }
438