Statistics
| Branch: | Revision:

root / arm-semi.c @ a74cdab4

History | View | Annotate | Download (15.1 kB)

1 a4f81979 bellard
/*
2 a4f81979 bellard
 *  Arm "Angel" semihosting syscalls
3 5fafdf24 ths
 *
4 8e71621f pbrook
 *  Copyright (c) 2005, 2007 CodeSourcery.
5 8e71621f pbrook
 *  Written by Paul Brook.
6 a4f81979 bellard
 *
7 a4f81979 bellard
 *  This program is free software; you can redistribute it and/or modify
8 a4f81979 bellard
 *  it under the terms of the GNU General Public License as published by
9 a4f81979 bellard
 *  the Free Software Foundation; either version 2 of the License, or
10 a4f81979 bellard
 *  (at your option) any later version.
11 a4f81979 bellard
 *
12 a4f81979 bellard
 *  This program is distributed in the hope that it will be useful,
13 a4f81979 bellard
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 a4f81979 bellard
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 a4f81979 bellard
 *  GNU General Public License for more details.
16 a4f81979 bellard
 *
17 a4f81979 bellard
 *  You should have received a copy of the GNU General Public License
18 8167ee88 Blue Swirl
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
19 a4f81979 bellard
 */
20 a4f81979 bellard
21 a4f81979 bellard
#include <sys/types.h>
22 a4f81979 bellard
#include <sys/stat.h>
23 a4f81979 bellard
#include <fcntl.h>
24 a4f81979 bellard
#include <unistd.h>
25 a4f81979 bellard
#include <stdlib.h>
26 a4f81979 bellard
#include <stdio.h>
27 a4f81979 bellard
#include <time.h>
28 a4f81979 bellard
29 8e71621f pbrook
#include "cpu.h"
30 8e71621f pbrook
#ifdef CONFIG_USER_ONLY
31 a4f81979 bellard
#include "qemu.h"
32 a4f81979 bellard
33 a4f81979 bellard
#define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
34 8e71621f pbrook
#else
35 87ecb68b pbrook
#include "qemu-common.h"
36 87ecb68b pbrook
#include "gdbstub.h"
37 8e71621f pbrook
#endif
38 a4f81979 bellard
39 a4f81979 bellard
#define SYS_OPEN        0x01
40 a4f81979 bellard
#define SYS_CLOSE       0x02
41 a4f81979 bellard
#define SYS_WRITEC      0x03
42 a4f81979 bellard
#define SYS_WRITE0      0x04
43 a4f81979 bellard
#define SYS_WRITE       0x05
44 a4f81979 bellard
#define SYS_READ        0x06
45 a4f81979 bellard
#define SYS_READC       0x07
46 a4f81979 bellard
#define SYS_ISTTY       0x09
47 a4f81979 bellard
#define SYS_SEEK        0x0a
48 a4f81979 bellard
#define SYS_FLEN        0x0c
49 a4f81979 bellard
#define SYS_TMPNAM      0x0d
50 a4f81979 bellard
#define SYS_REMOVE      0x0e
51 a4f81979 bellard
#define SYS_RENAME      0x0f
52 a4f81979 bellard
#define SYS_CLOCK       0x10
53 a4f81979 bellard
#define SYS_TIME        0x11
54 a4f81979 bellard
#define SYS_SYSTEM      0x12
55 a4f81979 bellard
#define SYS_ERRNO       0x13
56 a4f81979 bellard
#define SYS_GET_CMDLINE 0x15
57 a4f81979 bellard
#define SYS_HEAPINFO    0x16
58 a4f81979 bellard
#define SYS_EXIT        0x18
59 a4f81979 bellard
60 a4f81979 bellard
#ifndef O_BINARY
61 a4f81979 bellard
#define O_BINARY 0
62 a4f81979 bellard
#endif
63 a4f81979 bellard
64 a2d1ebaf pbrook
#define GDB_O_RDONLY  0x000
65 a2d1ebaf pbrook
#define GDB_O_WRONLY  0x001
66 a2d1ebaf pbrook
#define GDB_O_RDWR    0x002
67 a2d1ebaf pbrook
#define GDB_O_APPEND  0x008
68 a2d1ebaf pbrook
#define GDB_O_CREAT   0x200
69 a2d1ebaf pbrook
#define GDB_O_TRUNC   0x400
70 a2d1ebaf pbrook
#define GDB_O_BINARY  0
71 a2d1ebaf pbrook
72 a2d1ebaf pbrook
static int gdb_open_modeflags[12] = {
73 a2d1ebaf pbrook
    GDB_O_RDONLY,
74 a2d1ebaf pbrook
    GDB_O_RDONLY | GDB_O_BINARY,
75 a2d1ebaf pbrook
    GDB_O_RDWR,
76 a2d1ebaf pbrook
    GDB_O_RDWR | GDB_O_BINARY,
77 a2d1ebaf pbrook
    GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
78 a2d1ebaf pbrook
    GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
79 a2d1ebaf pbrook
    GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
80 a2d1ebaf pbrook
    GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
81 a2d1ebaf pbrook
    GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
82 a2d1ebaf pbrook
    GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY,
83 a2d1ebaf pbrook
    GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
84 a2d1ebaf pbrook
    GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY
85 a2d1ebaf pbrook
};
86 a2d1ebaf pbrook
87 a2d1ebaf pbrook
static int open_modeflags[12] = {
88 a4f81979 bellard
    O_RDONLY,
89 a4f81979 bellard
    O_RDONLY | O_BINARY,
90 a4f81979 bellard
    O_RDWR,
91 a4f81979 bellard
    O_RDWR | O_BINARY,
92 a4f81979 bellard
    O_WRONLY | O_CREAT | O_TRUNC,
93 a4f81979 bellard
    O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
94 a4f81979 bellard
    O_RDWR | O_CREAT | O_TRUNC,
95 a4f81979 bellard
    O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
96 a4f81979 bellard
    O_WRONLY | O_CREAT | O_APPEND,
97 a4f81979 bellard
    O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
98 a4f81979 bellard
    O_RDWR | O_CREAT | O_APPEND,
99 a4f81979 bellard
    O_RDWR | O_CREAT | O_APPEND | O_BINARY
100 a4f81979 bellard
};
101 a4f81979 bellard
102 8e71621f pbrook
#ifdef CONFIG_USER_ONLY
103 a4f81979 bellard
static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code)
104 a4f81979 bellard
{
105 8e71621f pbrook
    if (code == (uint32_t)-1)
106 8e71621f pbrook
        ts->swi_errno = errno;
107 8e71621f pbrook
    return code;
108 8e71621f pbrook
}
109 8e71621f pbrook
#else
110 8e71621f pbrook
static inline uint32_t set_swi_errno(CPUState *env, uint32_t code)
111 8e71621f pbrook
{
112 8e71621f pbrook
    return code;
113 8e71621f pbrook
}
114 8e71621f pbrook
115 a87295e8 pbrook
#include "softmmu-semi.h"
116 8e71621f pbrook
#endif
117 a4f81979 bellard
118 a2d1ebaf pbrook
static target_ulong arm_semi_syscall_len;
119 a2d1ebaf pbrook
120 33d9cc8a pbrook
#if !defined(CONFIG_USER_ONLY)
121 33d9cc8a pbrook
static target_ulong syscall_err;
122 33d9cc8a pbrook
#endif
123 33d9cc8a pbrook
124 a2d1ebaf pbrook
static void arm_semi_cb(CPUState *env, target_ulong ret, target_ulong err)
125 a2d1ebaf pbrook
{
126 a2d1ebaf pbrook
#ifdef CONFIG_USER_ONLY
127 a2d1ebaf pbrook
    TaskState *ts = env->opaque;
128 a2d1ebaf pbrook
#endif
129 33d9cc8a pbrook
130 a2d1ebaf pbrook
    if (ret == (target_ulong)-1) {
131 a2d1ebaf pbrook
#ifdef CONFIG_USER_ONLY
132 a2d1ebaf pbrook
        ts->swi_errno = err;
133 33d9cc8a pbrook
#else
134 33d9cc8a pbrook
        syscall_err = err;
135 a2d1ebaf pbrook
#endif
136 a2d1ebaf pbrook
        env->regs[0] = ret;
137 a2d1ebaf pbrook
    } else {
138 a2d1ebaf pbrook
        /* Fixup syscalls that use nonstardard return conventions.  */
139 a2d1ebaf pbrook
        switch (env->regs[0]) {
140 a2d1ebaf pbrook
        case SYS_WRITE:
141 a2d1ebaf pbrook
        case SYS_READ:
142 a2d1ebaf pbrook
            env->regs[0] = arm_semi_syscall_len - ret;
143 a2d1ebaf pbrook
            break;
144 a2d1ebaf pbrook
        case SYS_SEEK:
145 a2d1ebaf pbrook
            env->regs[0] = 0;
146 a2d1ebaf pbrook
            break;
147 a2d1ebaf pbrook
        default:
148 a2d1ebaf pbrook
            env->regs[0] = ret;
149 a2d1ebaf pbrook
            break;
150 a2d1ebaf pbrook
        }
151 a2d1ebaf pbrook
    }
152 a2d1ebaf pbrook
}
153 a2d1ebaf pbrook
154 33d9cc8a pbrook
static void arm_semi_flen_cb(CPUState *env, target_ulong ret, target_ulong err)
155 33d9cc8a pbrook
{
156 33d9cc8a pbrook
    /* The size is always stored in big-endian order, extract
157 33d9cc8a pbrook
       the value. We assume the size always fit in 32 bits.  */
158 33d9cc8a pbrook
    uint32_t size;
159 33d9cc8a pbrook
    cpu_memory_rw_debug(env, env->regs[13]-64+32, (uint8_t *)&size, 4, 0);
160 33d9cc8a pbrook
    env->regs[0] = be32_to_cpu(size);
161 33d9cc8a pbrook
#ifdef CONFIG_USER_ONLY
162 33d9cc8a pbrook
    ((TaskState *)env->opaque)->swi_errno = err;
163 33d9cc8a pbrook
#else
164 33d9cc8a pbrook
    syscall_err = err;
165 33d9cc8a pbrook
#endif
166 33d9cc8a pbrook
}
167 33d9cc8a pbrook
168 2f619698 bellard
#define ARG(n)                                        \
169 2f619698 bellard
({                                                \
170 2f619698 bellard
    target_ulong __arg;                                \
171 2f619698 bellard
    /* FIXME - handle get_user() failure */        \
172 2f619698 bellard
    get_user_ual(__arg, args + (n) * 4);        \
173 2f619698 bellard
    __arg;                                        \
174 2f619698 bellard
})
175 2f619698 bellard
#define SET_ARG(n, val) put_user_ual(val, args + (n) * 4)
176 a4f81979 bellard
uint32_t do_arm_semihosting(CPUState *env)
177 a4f81979 bellard
{
178 53a5960a pbrook
    target_ulong args;
179 a4f81979 bellard
    char * s;
180 a4f81979 bellard
    int nr;
181 a4f81979 bellard
    uint32_t ret;
182 8e71621f pbrook
    uint32_t len;
183 8e71621f pbrook
#ifdef CONFIG_USER_ONLY
184 a4f81979 bellard
    TaskState *ts = env->opaque;
185 8e71621f pbrook
#else
186 8e71621f pbrook
    CPUState *ts = env;
187 8e71621f pbrook
#endif
188 a4f81979 bellard
189 a4f81979 bellard
    nr = env->regs[0];
190 53a5960a pbrook
    args = env->regs[1];
191 a4f81979 bellard
    switch (nr) {
192 a4f81979 bellard
    case SYS_OPEN:
193 579a97f7 bellard
        if (!(s = lock_user_string(ARG(0))))
194 579a97f7 bellard
            /* FIXME - should this error code be -TARGET_EFAULT ? */
195 579a97f7 bellard
            return (uint32_t)-1;
196 a4f81979 bellard
        if (ARG(1) >= 12)
197 579a97f7 bellard
            return (uint32_t)-1;
198 a4f81979 bellard
        if (strcmp(s, ":tt") == 0) {
199 a4f81979 bellard
            if (ARG(1) < 4)
200 a4f81979 bellard
                return STDIN_FILENO;
201 a4f81979 bellard
            else
202 a4f81979 bellard
                return STDOUT_FILENO;
203 a4f81979 bellard
        }
204 a2d1ebaf pbrook
        if (use_gdb_syscalls()) {
205 5fafdf24 ths
            gdb_do_syscall(arm_semi_cb, "open,%s,%x,1a4", ARG(0),
206 33d9cc8a pbrook
                           (int)ARG(2)+1, gdb_open_modeflags[ARG(1)]);
207 a2d1ebaf pbrook
            return env->regs[0];
208 a2d1ebaf pbrook
        } else {
209 a2d1ebaf pbrook
            ret = set_swi_errno(ts, open(s, open_modeflags[ARG(1)], 0644));
210 a2d1ebaf pbrook
        }
211 8e71621f pbrook
        unlock_user(s, ARG(0), 0);
212 8e71621f pbrook
        return ret;
213 a4f81979 bellard
    case SYS_CLOSE:
214 a2d1ebaf pbrook
        if (use_gdb_syscalls()) {
215 a2d1ebaf pbrook
            gdb_do_syscall(arm_semi_cb, "close,%x", ARG(0));
216 a2d1ebaf pbrook
            return env->regs[0];
217 a2d1ebaf pbrook
        } else {
218 a2d1ebaf pbrook
            return set_swi_errno(ts, close(ARG(0)));
219 a2d1ebaf pbrook
        }
220 a4f81979 bellard
    case SYS_WRITEC:
221 53a5960a pbrook
        {
222 2f619698 bellard
          char c;
223 2f619698 bellard
224 2f619698 bellard
          if (get_user_u8(c, args))
225 2f619698 bellard
              /* FIXME - should this error code be -TARGET_EFAULT ? */
226 2f619698 bellard
              return (uint32_t)-1;
227 53a5960a pbrook
          /* Write to debug console.  stderr is near enough.  */
228 a2d1ebaf pbrook
          if (use_gdb_syscalls()) {
229 a2d1ebaf pbrook
                gdb_do_syscall(arm_semi_cb, "write,2,%x,1", args);
230 a2d1ebaf pbrook
                return env->regs[0];
231 a2d1ebaf pbrook
          } else {
232 a2d1ebaf pbrook
                return write(STDERR_FILENO, &c, 1);
233 a2d1ebaf pbrook
          }
234 53a5960a pbrook
        }
235 a4f81979 bellard
    case SYS_WRITE0:
236 579a97f7 bellard
        if (!(s = lock_user_string(args)))
237 579a97f7 bellard
            /* FIXME - should this error code be -TARGET_EFAULT ? */
238 579a97f7 bellard
            return (uint32_t)-1;
239 a2d1ebaf pbrook
        len = strlen(s);
240 a2d1ebaf pbrook
        if (use_gdb_syscalls()) {
241 a2d1ebaf pbrook
            gdb_do_syscall(arm_semi_cb, "write,2,%x,%x\n", args, len);
242 a2d1ebaf pbrook
            ret = env->regs[0];
243 a2d1ebaf pbrook
        } else {
244 a2d1ebaf pbrook
            ret = write(STDERR_FILENO, s, len);
245 a2d1ebaf pbrook
        }
246 53a5960a pbrook
        unlock_user(s, args, 0);
247 53a5960a pbrook
        return ret;
248 a4f81979 bellard
    case SYS_WRITE:
249 8e71621f pbrook
        len = ARG(2);
250 a2d1ebaf pbrook
        if (use_gdb_syscalls()) {
251 a2d1ebaf pbrook
            arm_semi_syscall_len = len;
252 a2d1ebaf pbrook
            gdb_do_syscall(arm_semi_cb, "write,%x,%x,%x", ARG(0), ARG(1), len);
253 a2d1ebaf pbrook
            return env->regs[0];
254 a2d1ebaf pbrook
        } else {
255 579a97f7 bellard
            if (!(s = lock_user(VERIFY_READ, ARG(1), len, 1)))
256 579a97f7 bellard
                /* FIXME - should this error code be -TARGET_EFAULT ? */
257 579a97f7 bellard
                return (uint32_t)-1;
258 a2d1ebaf pbrook
            ret = set_swi_errno(ts, write(ARG(0), s, len));
259 a2d1ebaf pbrook
            unlock_user(s, ARG(1), 0);
260 a2d1ebaf pbrook
            if (ret == (uint32_t)-1)
261 a2d1ebaf pbrook
                return -1;
262 a2d1ebaf pbrook
            return len - ret;
263 a2d1ebaf pbrook
        }
264 a4f81979 bellard
    case SYS_READ:
265 8e71621f pbrook
        len = ARG(2);
266 a2d1ebaf pbrook
        if (use_gdb_syscalls()) {
267 a2d1ebaf pbrook
            arm_semi_syscall_len = len;
268 a2d1ebaf pbrook
            gdb_do_syscall(arm_semi_cb, "read,%x,%x,%x", ARG(0), ARG(1), len);
269 a2d1ebaf pbrook
            return env->regs[0];
270 a2d1ebaf pbrook
        } else {
271 579a97f7 bellard
            if (!(s = lock_user(VERIFY_WRITE, ARG(1), len, 0)))
272 579a97f7 bellard
                /* FIXME - should this error code be -TARGET_EFAULT ? */
273 579a97f7 bellard
                return (uint32_t)-1;
274 a2d1ebaf pbrook
            do
275 a2d1ebaf pbrook
              ret = set_swi_errno(ts, read(ARG(0), s, len));
276 a2d1ebaf pbrook
            while (ret == -1 && errno == EINTR);
277 a2d1ebaf pbrook
            unlock_user(s, ARG(1), len);
278 a2d1ebaf pbrook
            if (ret == (uint32_t)-1)
279 a2d1ebaf pbrook
                return -1;
280 a2d1ebaf pbrook
            return len - ret;
281 a2d1ebaf pbrook
        }
282 a4f81979 bellard
    case SYS_READC:
283 a4f81979 bellard
       /* XXX: Read from debug cosole. Not implemented.  */
284 a4f81979 bellard
        return 0;
285 a4f81979 bellard
    case SYS_ISTTY:
286 a2d1ebaf pbrook
        if (use_gdb_syscalls()) {
287 a2d1ebaf pbrook
            gdb_do_syscall(arm_semi_cb, "isatty,%x", ARG(0));
288 a2d1ebaf pbrook
            return env->regs[0];
289 a2d1ebaf pbrook
        } else {
290 a2d1ebaf pbrook
            return isatty(ARG(0));
291 a2d1ebaf pbrook
        }
292 a4f81979 bellard
    case SYS_SEEK:
293 a2d1ebaf pbrook
        if (use_gdb_syscalls()) {
294 33d9cc8a pbrook
            gdb_do_syscall(arm_semi_cb, "lseek,%x,%x,0", ARG(0), ARG(1));
295 a2d1ebaf pbrook
            return env->regs[0];
296 a2d1ebaf pbrook
        } else {
297 a2d1ebaf pbrook
            ret = set_swi_errno(ts, lseek(ARG(0), ARG(1), SEEK_SET));
298 a2d1ebaf pbrook
            if (ret == (uint32_t)-1)
299 a2d1ebaf pbrook
              return -1;
300 a2d1ebaf pbrook
            return 0;
301 a2d1ebaf pbrook
        }
302 a4f81979 bellard
    case SYS_FLEN:
303 a2d1ebaf pbrook
        if (use_gdb_syscalls()) {
304 5fafdf24 ths
            gdb_do_syscall(arm_semi_flen_cb, "fstat,%x,%x",
305 33d9cc8a pbrook
                           ARG(0), env->regs[13]-64);
306 33d9cc8a pbrook
            return env->regs[0];
307 a2d1ebaf pbrook
        } else {
308 a4f81979 bellard
            struct stat buf;
309 a4f81979 bellard
            ret = set_swi_errno(ts, fstat(ARG(0), &buf));
310 a4f81979 bellard
            if (ret == (uint32_t)-1)
311 a4f81979 bellard
                return -1;
312 a4f81979 bellard
            return buf.st_size;
313 a4f81979 bellard
        }
314 a4f81979 bellard
    case SYS_TMPNAM:
315 a4f81979 bellard
        /* XXX: Not implemented.  */
316 a4f81979 bellard
        return -1;
317 a4f81979 bellard
    case SYS_REMOVE:
318 a2d1ebaf pbrook
        if (use_gdb_syscalls()) {
319 33d9cc8a pbrook
            gdb_do_syscall(arm_semi_cb, "unlink,%s", ARG(0), (int)ARG(1)+1);
320 a2d1ebaf pbrook
            ret = env->regs[0];
321 a2d1ebaf pbrook
        } else {
322 579a97f7 bellard
            if (!(s = lock_user_string(ARG(0))))
323 579a97f7 bellard
                /* FIXME - should this error code be -TARGET_EFAULT ? */
324 579a97f7 bellard
                return (uint32_t)-1;
325 a2d1ebaf pbrook
            ret =  set_swi_errno(ts, remove(s));
326 a2d1ebaf pbrook
            unlock_user(s, ARG(0), 0);
327 a2d1ebaf pbrook
        }
328 8e71621f pbrook
        return ret;
329 a4f81979 bellard
    case SYS_RENAME:
330 a2d1ebaf pbrook
        if (use_gdb_syscalls()) {
331 a2d1ebaf pbrook
            gdb_do_syscall(arm_semi_cb, "rename,%s,%s",
332 33d9cc8a pbrook
                           ARG(0), (int)ARG(1)+1, ARG(2), (int)ARG(3)+1);
333 a2d1ebaf pbrook
            return env->regs[0];
334 a2d1ebaf pbrook
        } else {
335 8e71621f pbrook
            char *s2;
336 8e71621f pbrook
            s = lock_user_string(ARG(0));
337 8e71621f pbrook
            s2 = lock_user_string(ARG(2));
338 579a97f7 bellard
            if (!s || !s2)
339 579a97f7 bellard
                /* FIXME - should this error code be -TARGET_EFAULT ? */
340 579a97f7 bellard
                ret = (uint32_t)-1;
341 579a97f7 bellard
            else
342 579a97f7 bellard
                ret = set_swi_errno(ts, rename(s, s2));
343 579a97f7 bellard
            if (s2)
344 579a97f7 bellard
                unlock_user(s2, ARG(2), 0);
345 579a97f7 bellard
            if (s)
346 579a97f7 bellard
                unlock_user(s, ARG(0), 0);
347 8e71621f pbrook
            return ret;
348 8e71621f pbrook
        }
349 a4f81979 bellard
    case SYS_CLOCK:
350 a4f81979 bellard
        return clock() / (CLOCKS_PER_SEC / 100);
351 a4f81979 bellard
    case SYS_TIME:
352 a4f81979 bellard
        return set_swi_errno(ts, time(NULL));
353 a4f81979 bellard
    case SYS_SYSTEM:
354 a2d1ebaf pbrook
        if (use_gdb_syscalls()) {
355 33d9cc8a pbrook
            gdb_do_syscall(arm_semi_cb, "system,%s", ARG(0), (int)ARG(1)+1);
356 a2d1ebaf pbrook
            return env->regs[0];
357 a2d1ebaf pbrook
        } else {
358 579a97f7 bellard
            if (!(s = lock_user_string(ARG(0))))
359 579a97f7 bellard
                /* FIXME - should this error code be -TARGET_EFAULT ? */
360 579a97f7 bellard
                return (uint32_t)-1;
361 a2d1ebaf pbrook
            ret = set_swi_errno(ts, system(s));
362 a2d1ebaf pbrook
            unlock_user(s, ARG(0), 0);
363 a982b531 ths
            return ret;
364 a2d1ebaf pbrook
        }
365 a4f81979 bellard
    case SYS_ERRNO:
366 8e71621f pbrook
#ifdef CONFIG_USER_ONLY
367 a4f81979 bellard
        return ts->swi_errno;
368 8e71621f pbrook
#else
369 33d9cc8a pbrook
        return syscall_err;
370 8e71621f pbrook
#endif
371 a4f81979 bellard
    case SYS_GET_CMDLINE:
372 8e71621f pbrook
#ifdef CONFIG_USER_ONLY
373 38d0662a pbrook
        /* Build a commandline from the original argv.  */
374 38d0662a pbrook
        {
375 2e8785ac Wolfgang Schildbach
            char *arm_cmdline_buffer;
376 2e8785ac Wolfgang Schildbach
            const char *host_cmdline_buffer;
377 579a97f7 bellard
378 2e8785ac Wolfgang Schildbach
            unsigned int i;
379 2e8785ac Wolfgang Schildbach
            unsigned int arm_cmdline_len = ARG(1);
380 2e8785ac Wolfgang Schildbach
            unsigned int host_cmdline_len =
381 2e8785ac Wolfgang Schildbach
                ts->info->arg_end-ts->info->arg_start;
382 2e8785ac Wolfgang Schildbach
383 2e8785ac Wolfgang Schildbach
            if (!arm_cmdline_len || host_cmdline_len > arm_cmdline_len) {
384 2e8785ac Wolfgang Schildbach
                return -1; /* not enough space to store command line */
385 2e8785ac Wolfgang Schildbach
            }
386 38d0662a pbrook
387 2e8785ac Wolfgang Schildbach
            if (!host_cmdline_len) {
388 2e8785ac Wolfgang Schildbach
                /* We special-case the "empty command line" case (argc==0).
389 2e8785ac Wolfgang Schildbach
                   Just provide the terminating 0. */
390 2e8785ac Wolfgang Schildbach
                arm_cmdline_buffer = lock_user(VERIFY_WRITE, ARG(0), 1, 0);
391 2e8785ac Wolfgang Schildbach
                arm_cmdline_buffer[0] = 0;
392 2e8785ac Wolfgang Schildbach
                unlock_user(arm_cmdline_buffer, ARG(0), 1);
393 38d0662a pbrook
394 2e8785ac Wolfgang Schildbach
                /* Adjust the commandline length argument. */
395 2e8785ac Wolfgang Schildbach
                SET_ARG(1, 0);
396 2e8785ac Wolfgang Schildbach
                return 0;
397 38d0662a pbrook
            }
398 38d0662a pbrook
399 2e8785ac Wolfgang Schildbach
            /* lock the buffers on the ARM side */
400 2e8785ac Wolfgang Schildbach
            arm_cmdline_buffer =
401 2e8785ac Wolfgang Schildbach
                lock_user(VERIFY_WRITE, ARG(0), host_cmdline_len, 0);
402 2e8785ac Wolfgang Schildbach
            host_cmdline_buffer =
403 2e8785ac Wolfgang Schildbach
                lock_user(VERIFY_READ, ts->info->arg_start,
404 2e8785ac Wolfgang Schildbach
                                       host_cmdline_len, 1);
405 38d0662a pbrook
406 2e8785ac Wolfgang Schildbach
            if (arm_cmdline_buffer && host_cmdline_buffer)
407 2e8785ac Wolfgang Schildbach
            {
408 2e8785ac Wolfgang Schildbach
                /* the last argument is zero-terminated;
409 2e8785ac Wolfgang Schildbach
                   no need for additional termination */
410 2e8785ac Wolfgang Schildbach
                memcpy(arm_cmdline_buffer, host_cmdline_buffer,
411 2e8785ac Wolfgang Schildbach
                       host_cmdline_len);
412 38d0662a pbrook
413 2e8785ac Wolfgang Schildbach
                /* separate arguments by white spaces */
414 2e8785ac Wolfgang Schildbach
                for (i = 0; i < host_cmdline_len-1; i++) {
415 2e8785ac Wolfgang Schildbach
                    if (arm_cmdline_buffer[i] == 0) {
416 2e8785ac Wolfgang Schildbach
                        arm_cmdline_buffer[i] = ' ';
417 2e8785ac Wolfgang Schildbach
                    }
418 2e8785ac Wolfgang Schildbach
                }
419 2e8785ac Wolfgang Schildbach
420 2e8785ac Wolfgang Schildbach
                /* Adjust the commandline length argument. */
421 2e8785ac Wolfgang Schildbach
                SET_ARG(1, host_cmdline_len-1);
422 2e8785ac Wolfgang Schildbach
            }
423 2e8785ac Wolfgang Schildbach
424 2e8785ac Wolfgang Schildbach
            /* Unlock the buffers on the ARM side.  */
425 2e8785ac Wolfgang Schildbach
            unlock_user(arm_cmdline_buffer, ARG(0), host_cmdline_len);
426 2e8785ac Wolfgang Schildbach
            unlock_user((void*)host_cmdline_buffer, ts->info->arg_start, 0);
427 2e8785ac Wolfgang Schildbach
428 2e8785ac Wolfgang Schildbach
            /* Return success if we could return a commandline.  */
429 2e8785ac Wolfgang Schildbach
            return (arm_cmdline_buffer && host_cmdline_buffer) ? 0 : -1;
430 38d0662a pbrook
        }
431 8e71621f pbrook
#else
432 2e8785ac Wolfgang Schildbach
        return -1;
433 8e71621f pbrook
#endif
434 a4f81979 bellard
    case SYS_HEAPINFO:
435 a4f81979 bellard
        {
436 a4f81979 bellard
            uint32_t *ptr;
437 a4f81979 bellard
            uint32_t limit;
438 a4f81979 bellard
439 8e71621f pbrook
#ifdef CONFIG_USER_ONLY
440 8e71621f pbrook
            /* Some C libraries assume the heap immediately follows .bss, so
441 a4f81979 bellard
               allocate it using sbrk.  */
442 a4f81979 bellard
            if (!ts->heap_limit) {
443 a4f81979 bellard
                long ret;
444 a4f81979 bellard
445 53a5960a pbrook
                ts->heap_base = do_brk(0);
446 a4f81979 bellard
                limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
447 a4f81979 bellard
                /* Try a big heap, and reduce the size if that fails.  */
448 a4f81979 bellard
                for (;;) {
449 53a5960a pbrook
                    ret = do_brk(limit);
450 a4f81979 bellard
                    if (ret != -1)
451 a4f81979 bellard
                        break;
452 a4f81979 bellard
                    limit = (ts->heap_base >> 1) + (limit >> 1);
453 a4f81979 bellard
                }
454 a4f81979 bellard
                ts->heap_limit = limit;
455 a4f81979 bellard
            }
456 3b46e624 ths
457 579a97f7 bellard
            if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0)))
458 579a97f7 bellard
                /* FIXME - should this error code be -TARGET_EFAULT ? */
459 579a97f7 bellard
                return (uint32_t)-1;
460 a4f81979 bellard
            ptr[0] = tswap32(ts->heap_base);
461 a4f81979 bellard
            ptr[1] = tswap32(ts->heap_limit);
462 a4f81979 bellard
            ptr[2] = tswap32(ts->stack_base);
463 a4f81979 bellard
            ptr[3] = tswap32(0); /* Stack limit.  */
464 8e71621f pbrook
            unlock_user(ptr, ARG(0), 16);
465 8e71621f pbrook
#else
466 8e71621f pbrook
            limit = ram_size;
467 579a97f7 bellard
            if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0)))
468 579a97f7 bellard
                /* FIXME - should this error code be -TARGET_EFAULT ? */
469 579a97f7 bellard
                return (uint32_t)-1;
470 8e71621f pbrook
            /* TODO: Make this use the limit of the loaded application.  */
471 8e71621f pbrook
            ptr[0] = tswap32(limit / 2);
472 8e71621f pbrook
            ptr[1] = tswap32(limit);
473 8e71621f pbrook
            ptr[2] = tswap32(limit); /* Stack base */
474 8e71621f pbrook
            ptr[3] = tswap32(0); /* Stack limit.  */
475 8e71621f pbrook
            unlock_user(ptr, ARG(0), 16);
476 8e71621f pbrook
#endif
477 a4f81979 bellard
            return 0;
478 a4f81979 bellard
        }
479 a4f81979 bellard
    case SYS_EXIT:
480 0e1c9c54 Paul Brook
        gdb_exit(env, 0);
481 a4f81979 bellard
        exit(0);
482 a4f81979 bellard
    default:
483 a4f81979 bellard
        fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
484 a4f81979 bellard
        cpu_dump_state(env, stderr, fprintf, 0);
485 a4f81979 bellard
        abort();
486 a4f81979 bellard
    }
487 a4f81979 bellard
}