]>
Commit | Line | Data |
---|---|---|
a4f81979 FB |
1 | /* |
2 | * Arm "Angel" semihosting syscalls | |
5fafdf24 | 3 | * |
8e71621f PB |
4 | * Copyright (c) 2005, 2007 CodeSourcery. |
5 | * Written by Paul Brook. | |
a4f81979 FB |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
8167ee88 | 18 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
a4f81979 FB |
19 | */ |
20 | ||
21 | #include <sys/types.h> | |
22 | #include <sys/stat.h> | |
23 | #include <fcntl.h> | |
24 | #include <unistd.h> | |
25 | #include <stdlib.h> | |
26 | #include <stdio.h> | |
27 | #include <time.h> | |
28 | ||
8e71621f PB |
29 | #include "cpu.h" |
30 | #ifdef CONFIG_USER_ONLY | |
a4f81979 FB |
31 | #include "qemu.h" |
32 | ||
33 | #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024) | |
8e71621f | 34 | #else |
87ecb68b | 35 | #include "qemu-common.h" |
022c62cb | 36 | #include "exec/gdbstub.h" |
bd2be150 | 37 | #include "hw/arm/arm.h" |
8e71621f | 38 | #endif |
a4f81979 | 39 | |
3881725c SW |
40 | #define TARGET_SYS_OPEN 0x01 |
41 | #define TARGET_SYS_CLOSE 0x02 | |
42 | #define TARGET_SYS_WRITEC 0x03 | |
43 | #define TARGET_SYS_WRITE0 0x04 | |
44 | #define TARGET_SYS_WRITE 0x05 | |
45 | #define TARGET_SYS_READ 0x06 | |
46 | #define TARGET_SYS_READC 0x07 | |
47 | #define TARGET_SYS_ISTTY 0x09 | |
48 | #define TARGET_SYS_SEEK 0x0a | |
49 | #define TARGET_SYS_FLEN 0x0c | |
50 | #define TARGET_SYS_TMPNAM 0x0d | |
51 | #define TARGET_SYS_REMOVE 0x0e | |
52 | #define TARGET_SYS_RENAME 0x0f | |
53 | #define TARGET_SYS_CLOCK 0x10 | |
54 | #define TARGET_SYS_TIME 0x11 | |
55 | #define TARGET_SYS_SYSTEM 0x12 | |
56 | #define TARGET_SYS_ERRNO 0x13 | |
57 | #define TARGET_SYS_GET_CMDLINE 0x15 | |
58 | #define TARGET_SYS_HEAPINFO 0x16 | |
59 | #define TARGET_SYS_EXIT 0x18 | |
a4f81979 FB |
60 | |
61 | #ifndef O_BINARY | |
62 | #define O_BINARY 0 | |
63 | #endif | |
64 | ||
a2d1ebaf PB |
65 | #define GDB_O_RDONLY 0x000 |
66 | #define GDB_O_WRONLY 0x001 | |
67 | #define GDB_O_RDWR 0x002 | |
68 | #define GDB_O_APPEND 0x008 | |
69 | #define GDB_O_CREAT 0x200 | |
70 | #define GDB_O_TRUNC 0x400 | |
71 | #define GDB_O_BINARY 0 | |
72 | ||
73 | static int gdb_open_modeflags[12] = { | |
74 | GDB_O_RDONLY, | |
75 | GDB_O_RDONLY | GDB_O_BINARY, | |
76 | GDB_O_RDWR, | |
77 | GDB_O_RDWR | GDB_O_BINARY, | |
78 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC, | |
79 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY, | |
80 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC, | |
81 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY, | |
82 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND, | |
83 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY, | |
84 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND, | |
85 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY | |
86 | }; | |
87 | ||
88 | static int open_modeflags[12] = { | |
a4f81979 FB |
89 | O_RDONLY, |
90 | O_RDONLY | O_BINARY, | |
91 | O_RDWR, | |
92 | O_RDWR | O_BINARY, | |
93 | O_WRONLY | O_CREAT | O_TRUNC, | |
94 | O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, | |
95 | O_RDWR | O_CREAT | O_TRUNC, | |
96 | O_RDWR | O_CREAT | O_TRUNC | O_BINARY, | |
97 | O_WRONLY | O_CREAT | O_APPEND, | |
98 | O_WRONLY | O_CREAT | O_APPEND | O_BINARY, | |
99 | O_RDWR | O_CREAT | O_APPEND, | |
100 | O_RDWR | O_CREAT | O_APPEND | O_BINARY | |
101 | }; | |
102 | ||
8e71621f | 103 | #ifdef CONFIG_USER_ONLY |
a4f81979 FB |
104 | static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code) |
105 | { | |
8e71621f PB |
106 | if (code == (uint32_t)-1) |
107 | ts->swi_errno = errno; | |
108 | return code; | |
109 | } | |
110 | #else | |
81926f47 | 111 | static inline uint32_t set_swi_errno(CPUARMState *env, uint32_t code) |
8e71621f PB |
112 | { |
113 | return code; | |
114 | } | |
115 | ||
022c62cb | 116 | #include "exec/softmmu-semi.h" |
8e71621f | 117 | #endif |
a4f81979 | 118 | |
a2d1ebaf PB |
119 | static target_ulong arm_semi_syscall_len; |
120 | ||
33d9cc8a PB |
121 | #if !defined(CONFIG_USER_ONLY) |
122 | static target_ulong syscall_err; | |
123 | #endif | |
124 | ||
9e0c5422 | 125 | static void arm_semi_cb(CPUState *cs, target_ulong ret, target_ulong err) |
a2d1ebaf | 126 | { |
9e0c5422 AF |
127 | ARMCPU *cpu = ARM_CPU(cs); |
128 | CPUARMState *env = &cpu->env; | |
a2d1ebaf | 129 | #ifdef CONFIG_USER_ONLY |
0429a971 | 130 | TaskState *ts = cs->opaque; |
a2d1ebaf | 131 | #endif |
33d9cc8a | 132 | |
a2d1ebaf PB |
133 | if (ret == (target_ulong)-1) { |
134 | #ifdef CONFIG_USER_ONLY | |
135 | ts->swi_errno = err; | |
33d9cc8a PB |
136 | #else |
137 | syscall_err = err; | |
a2d1ebaf PB |
138 | #endif |
139 | env->regs[0] = ret; | |
140 | } else { | |
141 | /* Fixup syscalls that use nonstardard return conventions. */ | |
142 | switch (env->regs[0]) { | |
3881725c SW |
143 | case TARGET_SYS_WRITE: |
144 | case TARGET_SYS_READ: | |
a2d1ebaf PB |
145 | env->regs[0] = arm_semi_syscall_len - ret; |
146 | break; | |
3881725c | 147 | case TARGET_SYS_SEEK: |
a2d1ebaf PB |
148 | env->regs[0] = 0; |
149 | break; | |
150 | default: | |
151 | env->regs[0] = ret; | |
152 | break; | |
153 | } | |
154 | } | |
155 | } | |
156 | ||
9e0c5422 | 157 | static void arm_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err) |
33d9cc8a | 158 | { |
9e0c5422 AF |
159 | ARMCPU *cpu = ARM_CPU(cs); |
160 | CPUARMState *env = &cpu->env; | |
33d9cc8a PB |
161 | /* The size is always stored in big-endian order, extract |
162 | the value. We assume the size always fit in 32 bits. */ | |
163 | uint32_t size; | |
f17ec444 | 164 | cpu_memory_rw_debug(cs, env->regs[13]-64+32, (uint8_t *)&size, 4, 0); |
33d9cc8a PB |
165 | env->regs[0] = be32_to_cpu(size); |
166 | #ifdef CONFIG_USER_ONLY | |
0429a971 | 167 | ((TaskState *)cs->opaque)->swi_errno = err; |
33d9cc8a PB |
168 | #else |
169 | syscall_err = err; | |
170 | #endif | |
171 | } | |
172 | ||
f296c0d1 PM |
173 | /* Read the input value from the argument block; fail the semihosting |
174 | * call if the memory read fails. | |
175 | */ | |
176 | #define GET_ARG(n) do { \ | |
177 | if (get_user_ual(arg ## n, args + (n) * 4)) { \ | |
178 | return (uint32_t)-1; \ | |
179 | } \ | |
180 | } while (0) | |
181 | ||
2f619698 | 182 | #define SET_ARG(n, val) put_user_ual(val, args + (n) * 4) |
81926f47 | 183 | uint32_t do_arm_semihosting(CPUARMState *env) |
a4f81979 | 184 | { |
878096ee | 185 | ARMCPU *cpu = arm_env_get_cpu(env); |
0429a971 | 186 | CPUState *cs = CPU(cpu); |
53a5960a | 187 | target_ulong args; |
f296c0d1 | 188 | target_ulong arg0, arg1, arg2, arg3; |
a4f81979 FB |
189 | char * s; |
190 | int nr; | |
191 | uint32_t ret; | |
8e71621f PB |
192 | uint32_t len; |
193 | #ifdef CONFIG_USER_ONLY | |
0429a971 | 194 | TaskState *ts = cs->opaque; |
8e71621f | 195 | #else |
81926f47 | 196 | CPUARMState *ts = env; |
8e71621f | 197 | #endif |
a4f81979 FB |
198 | |
199 | nr = env->regs[0]; | |
53a5960a | 200 | args = env->regs[1]; |
a4f81979 | 201 | switch (nr) { |
3881725c | 202 | case TARGET_SYS_OPEN: |
f296c0d1 PM |
203 | GET_ARG(0); |
204 | GET_ARG(1); | |
205 | GET_ARG(2); | |
206 | s = lock_user_string(arg0); | |
207 | if (!s) { | |
579a97f7 FB |
208 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
209 | return (uint32_t)-1; | |
f296c0d1 PM |
210 | } |
211 | if (arg1 >= 12) { | |
212 | unlock_user(s, arg0, 0); | |
579a97f7 | 213 | return (uint32_t)-1; |
396bef4b | 214 | } |
a4f81979 | 215 | if (strcmp(s, ":tt") == 0) { |
f296c0d1 PM |
216 | int result_fileno = arg1 < 4 ? STDIN_FILENO : STDOUT_FILENO; |
217 | unlock_user(s, arg0, 0); | |
396bef4b | 218 | return result_fileno; |
a4f81979 | 219 | } |
a2d1ebaf | 220 | if (use_gdb_syscalls()) { |
f296c0d1 PM |
221 | gdb_do_syscall(arm_semi_cb, "open,%s,%x,1a4", arg0, |
222 | (int)arg2+1, gdb_open_modeflags[arg1]); | |
396bef4b | 223 | ret = env->regs[0]; |
a2d1ebaf | 224 | } else { |
f296c0d1 | 225 | ret = set_swi_errno(ts, open(s, open_modeflags[arg1], 0644)); |
a2d1ebaf | 226 | } |
f296c0d1 | 227 | unlock_user(s, arg0, 0); |
8e71621f | 228 | return ret; |
3881725c | 229 | case TARGET_SYS_CLOSE: |
f296c0d1 | 230 | GET_ARG(0); |
a2d1ebaf | 231 | if (use_gdb_syscalls()) { |
f296c0d1 | 232 | gdb_do_syscall(arm_semi_cb, "close,%x", arg0); |
a2d1ebaf PB |
233 | return env->regs[0]; |
234 | } else { | |
f296c0d1 | 235 | return set_swi_errno(ts, close(arg0)); |
a2d1ebaf | 236 | } |
3881725c | 237 | case TARGET_SYS_WRITEC: |
53a5960a | 238 | { |
2f619698 FB |
239 | char c; |
240 | ||
241 | if (get_user_u8(c, args)) | |
242 | /* FIXME - should this error code be -TARGET_EFAULT ? */ | |
243 | return (uint32_t)-1; | |
53a5960a | 244 | /* Write to debug console. stderr is near enough. */ |
a2d1ebaf PB |
245 | if (use_gdb_syscalls()) { |
246 | gdb_do_syscall(arm_semi_cb, "write,2,%x,1", args); | |
247 | return env->regs[0]; | |
248 | } else { | |
249 | return write(STDERR_FILENO, &c, 1); | |
250 | } | |
53a5960a | 251 | } |
3881725c | 252 | case TARGET_SYS_WRITE0: |
579a97f7 FB |
253 | if (!(s = lock_user_string(args))) |
254 | /* FIXME - should this error code be -TARGET_EFAULT ? */ | |
255 | return (uint32_t)-1; | |
a2d1ebaf PB |
256 | len = strlen(s); |
257 | if (use_gdb_syscalls()) { | |
258 | gdb_do_syscall(arm_semi_cb, "write,2,%x,%x\n", args, len); | |
259 | ret = env->regs[0]; | |
260 | } else { | |
261 | ret = write(STDERR_FILENO, s, len); | |
262 | } | |
53a5960a PB |
263 | unlock_user(s, args, 0); |
264 | return ret; | |
3881725c | 265 | case TARGET_SYS_WRITE: |
f296c0d1 PM |
266 | GET_ARG(0); |
267 | GET_ARG(1); | |
268 | GET_ARG(2); | |
269 | len = arg2; | |
a2d1ebaf PB |
270 | if (use_gdb_syscalls()) { |
271 | arm_semi_syscall_len = len; | |
f296c0d1 | 272 | gdb_do_syscall(arm_semi_cb, "write,%x,%x,%x", arg0, arg1, len); |
a2d1ebaf PB |
273 | return env->regs[0]; |
274 | } else { | |
f296c0d1 PM |
275 | s = lock_user(VERIFY_READ, arg1, len, 1); |
276 | if (!s) { | |
579a97f7 FB |
277 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
278 | return (uint32_t)-1; | |
f296c0d1 PM |
279 | } |
280 | ret = set_swi_errno(ts, write(arg0, s, len)); | |
281 | unlock_user(s, arg1, 0); | |
a2d1ebaf PB |
282 | if (ret == (uint32_t)-1) |
283 | return -1; | |
284 | return len - ret; | |
285 | } | |
3881725c | 286 | case TARGET_SYS_READ: |
f296c0d1 PM |
287 | GET_ARG(0); |
288 | GET_ARG(1); | |
289 | GET_ARG(2); | |
290 | len = arg2; | |
a2d1ebaf PB |
291 | if (use_gdb_syscalls()) { |
292 | arm_semi_syscall_len = len; | |
f296c0d1 | 293 | gdb_do_syscall(arm_semi_cb, "read,%x,%x,%x", arg0, arg1, len); |
a2d1ebaf PB |
294 | return env->regs[0]; |
295 | } else { | |
f296c0d1 PM |
296 | s = lock_user(VERIFY_WRITE, arg1, len, 0); |
297 | if (!s) { | |
579a97f7 FB |
298 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
299 | return (uint32_t)-1; | |
f296c0d1 PM |
300 | } |
301 | do { | |
302 | ret = set_swi_errno(ts, read(arg0, s, len)); | |
303 | } while (ret == -1 && errno == EINTR); | |
304 | unlock_user(s, arg1, len); | |
a2d1ebaf PB |
305 | if (ret == (uint32_t)-1) |
306 | return -1; | |
307 | return len - ret; | |
308 | } | |
3881725c | 309 | case TARGET_SYS_READC: |
b90372ad | 310 | /* XXX: Read from debug console. Not implemented. */ |
a4f81979 | 311 | return 0; |
3881725c | 312 | case TARGET_SYS_ISTTY: |
f296c0d1 | 313 | GET_ARG(0); |
a2d1ebaf | 314 | if (use_gdb_syscalls()) { |
f296c0d1 | 315 | gdb_do_syscall(arm_semi_cb, "isatty,%x", arg0); |
a2d1ebaf PB |
316 | return env->regs[0]; |
317 | } else { | |
f296c0d1 | 318 | return isatty(arg0); |
a2d1ebaf | 319 | } |
3881725c | 320 | case TARGET_SYS_SEEK: |
f296c0d1 PM |
321 | GET_ARG(0); |
322 | GET_ARG(1); | |
a2d1ebaf | 323 | if (use_gdb_syscalls()) { |
f296c0d1 | 324 | gdb_do_syscall(arm_semi_cb, "lseek,%x,%x,0", arg0, arg1); |
a2d1ebaf PB |
325 | return env->regs[0]; |
326 | } else { | |
f296c0d1 | 327 | ret = set_swi_errno(ts, lseek(arg0, arg1, SEEK_SET)); |
a2d1ebaf PB |
328 | if (ret == (uint32_t)-1) |
329 | return -1; | |
330 | return 0; | |
331 | } | |
3881725c | 332 | case TARGET_SYS_FLEN: |
f296c0d1 | 333 | GET_ARG(0); |
a2d1ebaf | 334 | if (use_gdb_syscalls()) { |
5fafdf24 | 335 | gdb_do_syscall(arm_semi_flen_cb, "fstat,%x,%x", |
f296c0d1 | 336 | arg0, env->regs[13]-64); |
33d9cc8a | 337 | return env->regs[0]; |
a2d1ebaf | 338 | } else { |
a4f81979 | 339 | struct stat buf; |
f296c0d1 | 340 | ret = set_swi_errno(ts, fstat(arg0, &buf)); |
a4f81979 FB |
341 | if (ret == (uint32_t)-1) |
342 | return -1; | |
343 | return buf.st_size; | |
344 | } | |
3881725c | 345 | case TARGET_SYS_TMPNAM: |
a4f81979 FB |
346 | /* XXX: Not implemented. */ |
347 | return -1; | |
3881725c | 348 | case TARGET_SYS_REMOVE: |
f296c0d1 PM |
349 | GET_ARG(0); |
350 | GET_ARG(1); | |
a2d1ebaf | 351 | if (use_gdb_syscalls()) { |
f296c0d1 | 352 | gdb_do_syscall(arm_semi_cb, "unlink,%s", arg0, (int)arg1+1); |
a2d1ebaf PB |
353 | ret = env->regs[0]; |
354 | } else { | |
f296c0d1 PM |
355 | s = lock_user_string(arg0); |
356 | if (!s) { | |
579a97f7 FB |
357 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
358 | return (uint32_t)-1; | |
f296c0d1 | 359 | } |
a2d1ebaf | 360 | ret = set_swi_errno(ts, remove(s)); |
f296c0d1 | 361 | unlock_user(s, arg0, 0); |
a2d1ebaf | 362 | } |
8e71621f | 363 | return ret; |
3881725c | 364 | case TARGET_SYS_RENAME: |
f296c0d1 PM |
365 | GET_ARG(0); |
366 | GET_ARG(1); | |
367 | GET_ARG(2); | |
368 | GET_ARG(3); | |
a2d1ebaf PB |
369 | if (use_gdb_syscalls()) { |
370 | gdb_do_syscall(arm_semi_cb, "rename,%s,%s", | |
f296c0d1 | 371 | arg0, (int)arg1+1, arg2, (int)arg3+1); |
a2d1ebaf PB |
372 | return env->regs[0]; |
373 | } else { | |
8e71621f | 374 | char *s2; |
f296c0d1 PM |
375 | s = lock_user_string(arg0); |
376 | s2 = lock_user_string(arg2); | |
579a97f7 FB |
377 | if (!s || !s2) |
378 | /* FIXME - should this error code be -TARGET_EFAULT ? */ | |
379 | ret = (uint32_t)-1; | |
380 | else | |
381 | ret = set_swi_errno(ts, rename(s, s2)); | |
382 | if (s2) | |
f296c0d1 | 383 | unlock_user(s2, arg2, 0); |
579a97f7 | 384 | if (s) |
f296c0d1 | 385 | unlock_user(s, arg0, 0); |
8e71621f PB |
386 | return ret; |
387 | } | |
3881725c | 388 | case TARGET_SYS_CLOCK: |
a4f81979 | 389 | return clock() / (CLOCKS_PER_SEC / 100); |
3881725c | 390 | case TARGET_SYS_TIME: |
a4f81979 | 391 | return set_swi_errno(ts, time(NULL)); |
3881725c | 392 | case TARGET_SYS_SYSTEM: |
f296c0d1 PM |
393 | GET_ARG(0); |
394 | GET_ARG(1); | |
a2d1ebaf | 395 | if (use_gdb_syscalls()) { |
f296c0d1 | 396 | gdb_do_syscall(arm_semi_cb, "system,%s", arg0, (int)arg1+1); |
a2d1ebaf PB |
397 | return env->regs[0]; |
398 | } else { | |
f296c0d1 PM |
399 | s = lock_user_string(arg0); |
400 | if (!s) { | |
579a97f7 FB |
401 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
402 | return (uint32_t)-1; | |
f296c0d1 | 403 | } |
a2d1ebaf | 404 | ret = set_swi_errno(ts, system(s)); |
f296c0d1 | 405 | unlock_user(s, arg0, 0); |
a982b531 | 406 | return ret; |
a2d1ebaf | 407 | } |
3881725c | 408 | case TARGET_SYS_ERRNO: |
8e71621f | 409 | #ifdef CONFIG_USER_ONLY |
a4f81979 | 410 | return ts->swi_errno; |
8e71621f | 411 | #else |
33d9cc8a | 412 | return syscall_err; |
8e71621f | 413 | #endif |
3881725c | 414 | case TARGET_SYS_GET_CMDLINE: |
38d0662a | 415 | { |
1c1b40c1 CV |
416 | /* Build a command-line from the original argv. |
417 | * | |
418 | * The inputs are: | |
f296c0d1 PM |
419 | * * arg0, pointer to a buffer of at least the size |
420 | * specified in arg1. | |
421 | * * arg1, size of the buffer pointed to by arg0 in | |
1c1b40c1 CV |
422 | * bytes. |
423 | * | |
424 | * The outputs are: | |
f296c0d1 | 425 | * * arg0, pointer to null-terminated string of the |
1c1b40c1 | 426 | * command line. |
f296c0d1 | 427 | * * arg1, length of the string pointed to by arg0. |
1c1b40c1 | 428 | */ |
579a97f7 | 429 | |
1c1b40c1 | 430 | char *output_buffer; |
f296c0d1 | 431 | size_t input_size; |
1c1b40c1 CV |
432 | size_t output_size; |
433 | int status = 0; | |
f296c0d1 PM |
434 | GET_ARG(0); |
435 | GET_ARG(1); | |
436 | input_size = arg1; | |
1c1b40c1 CV |
437 | /* Compute the size of the output string. */ |
438 | #if !defined(CONFIG_USER_ONLY) | |
439 | output_size = strlen(ts->boot_info->kernel_filename) | |
440 | + 1 /* Separating space. */ | |
441 | + strlen(ts->boot_info->kernel_cmdline) | |
442 | + 1; /* Terminating null byte. */ | |
443 | #else | |
444 | unsigned int i; | |
38d0662a | 445 | |
1c1b40c1 CV |
446 | output_size = ts->info->arg_end - ts->info->arg_start; |
447 | if (!output_size) { | |
2e8785ac WS |
448 | /* We special-case the "empty command line" case (argc==0). |
449 | Just provide the terminating 0. */ | |
1c1b40c1 CV |
450 | output_size = 1; |
451 | } | |
452 | #endif | |
38d0662a | 453 | |
1c1b40c1 CV |
454 | if (output_size > input_size) { |
455 | /* Not enough space to store command-line arguments. */ | |
456 | return -1; | |
38d0662a | 457 | } |
38d0662a | 458 | |
1c1b40c1 | 459 | /* Adjust the command-line length. */ |
f296c0d1 PM |
460 | if (SET_ARG(1, output_size - 1)) { |
461 | /* Couldn't write back to argument block */ | |
462 | return -1; | |
463 | } | |
38d0662a | 464 | |
1c1b40c1 | 465 | /* Lock the buffer on the ARM side. */ |
f296c0d1 | 466 | output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0); |
1c1b40c1 CV |
467 | if (!output_buffer) { |
468 | return -1; | |
469 | } | |
38d0662a | 470 | |
1c1b40c1 CV |
471 | /* Copy the command-line arguments. */ |
472 | #if !defined(CONFIG_USER_ONLY) | |
473 | pstrcpy(output_buffer, output_size, ts->boot_info->kernel_filename); | |
474 | pstrcat(output_buffer, output_size, " "); | |
475 | pstrcat(output_buffer, output_size, ts->boot_info->kernel_cmdline); | |
476 | #else | |
477 | if (output_size == 1) { | |
478 | /* Empty command-line. */ | |
479 | output_buffer[0] = '\0'; | |
480 | goto out; | |
481 | } | |
2e8785ac | 482 | |
1c1b40c1 CV |
483 | if (copy_from_user(output_buffer, ts->info->arg_start, |
484 | output_size)) { | |
485 | status = -1; | |
486 | goto out; | |
2e8785ac WS |
487 | } |
488 | ||
1c1b40c1 CV |
489 | /* Separate arguments by white spaces. */ |
490 | for (i = 0; i < output_size - 1; i++) { | |
491 | if (output_buffer[i] == 0) { | |
492 | output_buffer[i] = ' '; | |
493 | } | |
494 | } | |
495 | out: | |
496 | #endif | |
497 | /* Unlock the buffer on the ARM side. */ | |
f296c0d1 | 498 | unlock_user(output_buffer, arg0, output_size); |
2e8785ac | 499 | |
1c1b40c1 | 500 | return status; |
38d0662a | 501 | } |
3881725c | 502 | case TARGET_SYS_HEAPINFO: |
a4f81979 FB |
503 | { |
504 | uint32_t *ptr; | |
505 | uint32_t limit; | |
f296c0d1 | 506 | GET_ARG(0); |
a4f81979 | 507 | |
8e71621f PB |
508 | #ifdef CONFIG_USER_ONLY |
509 | /* Some C libraries assume the heap immediately follows .bss, so | |
a4f81979 FB |
510 | allocate it using sbrk. */ |
511 | if (!ts->heap_limit) { | |
206ae74a | 512 | abi_ulong ret; |
a4f81979 | 513 | |
53a5960a | 514 | ts->heap_base = do_brk(0); |
a4f81979 FB |
515 | limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE; |
516 | /* Try a big heap, and reduce the size if that fails. */ | |
517 | for (;;) { | |
53a5960a | 518 | ret = do_brk(limit); |
206ae74a | 519 | if (ret >= limit) { |
a4f81979 | 520 | break; |
206ae74a | 521 | } |
a4f81979 FB |
522 | limit = (ts->heap_base >> 1) + (limit >> 1); |
523 | } | |
524 | ts->heap_limit = limit; | |
525 | } | |
3b46e624 | 526 | |
f296c0d1 PM |
527 | ptr = lock_user(VERIFY_WRITE, arg0, 16, 0); |
528 | if (!ptr) { | |
579a97f7 FB |
529 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
530 | return (uint32_t)-1; | |
f296c0d1 | 531 | } |
a4f81979 FB |
532 | ptr[0] = tswap32(ts->heap_base); |
533 | ptr[1] = tswap32(ts->heap_limit); | |
534 | ptr[2] = tswap32(ts->stack_base); | |
535 | ptr[3] = tswap32(0); /* Stack limit. */ | |
f296c0d1 | 536 | unlock_user(ptr, arg0, 16); |
8e71621f PB |
537 | #else |
538 | limit = ram_size; | |
f296c0d1 PM |
539 | ptr = lock_user(VERIFY_WRITE, arg0, 16, 0); |
540 | if (!ptr) { | |
579a97f7 FB |
541 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
542 | return (uint32_t)-1; | |
f296c0d1 | 543 | } |
8e71621f PB |
544 | /* TODO: Make this use the limit of the loaded application. */ |
545 | ptr[0] = tswap32(limit / 2); | |
546 | ptr[1] = tswap32(limit); | |
547 | ptr[2] = tswap32(limit); /* Stack base */ | |
548 | ptr[3] = tswap32(0); /* Stack limit. */ | |
f296c0d1 | 549 | unlock_user(ptr, arg0, 16); |
8e71621f | 550 | #endif |
a4f81979 FB |
551 | return 0; |
552 | } | |
3881725c | 553 | case TARGET_SYS_EXIT: |
0e1c9c54 | 554 | gdb_exit(env, 0); |
a4f81979 FB |
555 | exit(0); |
556 | default: | |
557 | fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr); | |
0429a971 | 558 | cpu_dump_state(cs, stderr, fprintf, 0); |
a4f81979 FB |
559 | abort(); |
560 | } | |
561 | } |