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