]>
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 | |
e9ebfbfc | 61 | #define TARGET_SYS_SYNCCACHE 0x19 |
a4f81979 | 62 | |
1ecc3a2d LI |
63 | /* ADP_Stopped_ApplicationExit is used for exit(0), |
64 | * anything else is implemented as exit(1) */ | |
65 | #define ADP_Stopped_ApplicationExit (0x20026) | |
66 | ||
a4f81979 FB |
67 | #ifndef O_BINARY |
68 | #define O_BINARY 0 | |
69 | #endif | |
70 | ||
a2d1ebaf PB |
71 | #define GDB_O_RDONLY 0x000 |
72 | #define GDB_O_WRONLY 0x001 | |
73 | #define GDB_O_RDWR 0x002 | |
74 | #define GDB_O_APPEND 0x008 | |
75 | #define GDB_O_CREAT 0x200 | |
76 | #define GDB_O_TRUNC 0x400 | |
77 | #define GDB_O_BINARY 0 | |
78 | ||
79 | static int gdb_open_modeflags[12] = { | |
80 | GDB_O_RDONLY, | |
81 | GDB_O_RDONLY | GDB_O_BINARY, | |
82 | GDB_O_RDWR, | |
83 | GDB_O_RDWR | GDB_O_BINARY, | |
84 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC, | |
85 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY, | |
86 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC, | |
87 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY, | |
88 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND, | |
89 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY, | |
90 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND, | |
91 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY | |
92 | }; | |
93 | ||
94 | static int open_modeflags[12] = { | |
a4f81979 FB |
95 | O_RDONLY, |
96 | O_RDONLY | O_BINARY, | |
97 | O_RDWR, | |
98 | O_RDWR | O_BINARY, | |
99 | O_WRONLY | O_CREAT | O_TRUNC, | |
100 | O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, | |
101 | O_RDWR | O_CREAT | O_TRUNC, | |
102 | O_RDWR | O_CREAT | O_TRUNC | O_BINARY, | |
103 | O_WRONLY | O_CREAT | O_APPEND, | |
104 | O_WRONLY | O_CREAT | O_APPEND | O_BINARY, | |
105 | O_RDWR | O_CREAT | O_APPEND, | |
106 | O_RDWR | O_CREAT | O_APPEND | O_BINARY | |
107 | }; | |
108 | ||
8e71621f | 109 | #ifdef CONFIG_USER_ONLY |
a4f81979 FB |
110 | static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code) |
111 | { | |
8e71621f PB |
112 | if (code == (uint32_t)-1) |
113 | ts->swi_errno = errno; | |
114 | return code; | |
115 | } | |
116 | #else | |
81926f47 | 117 | static inline uint32_t set_swi_errno(CPUARMState *env, uint32_t code) |
8e71621f PB |
118 | { |
119 | return code; | |
120 | } | |
121 | ||
022c62cb | 122 | #include "exec/softmmu-semi.h" |
8e71621f | 123 | #endif |
a4f81979 | 124 | |
a2d1ebaf PB |
125 | static target_ulong arm_semi_syscall_len; |
126 | ||
33d9cc8a PB |
127 | #if !defined(CONFIG_USER_ONLY) |
128 | static target_ulong syscall_err; | |
129 | #endif | |
130 | ||
9e0c5422 | 131 | static void arm_semi_cb(CPUState *cs, target_ulong ret, target_ulong err) |
a2d1ebaf | 132 | { |
9e0c5422 AF |
133 | ARMCPU *cpu = ARM_CPU(cs); |
134 | CPUARMState *env = &cpu->env; | |
a2d1ebaf | 135 | #ifdef CONFIG_USER_ONLY |
0429a971 | 136 | TaskState *ts = cs->opaque; |
a2d1ebaf | 137 | #endif |
faacc041 | 138 | target_ulong reg0 = is_a64(env) ? env->xregs[0] : env->regs[0]; |
33d9cc8a | 139 | |
a2d1ebaf PB |
140 | if (ret == (target_ulong)-1) { |
141 | #ifdef CONFIG_USER_ONLY | |
142 | ts->swi_errno = err; | |
33d9cc8a PB |
143 | #else |
144 | syscall_err = err; | |
a2d1ebaf | 145 | #endif |
bb19cbc9 | 146 | reg0 = ret; |
a2d1ebaf PB |
147 | } else { |
148 | /* Fixup syscalls that use nonstardard return conventions. */ | |
bb19cbc9 | 149 | switch (reg0) { |
3881725c SW |
150 | case TARGET_SYS_WRITE: |
151 | case TARGET_SYS_READ: | |
bb19cbc9 | 152 | reg0 = arm_semi_syscall_len - ret; |
a2d1ebaf | 153 | break; |
3881725c | 154 | case TARGET_SYS_SEEK: |
bb19cbc9 | 155 | reg0 = 0; |
a2d1ebaf PB |
156 | break; |
157 | default: | |
bb19cbc9 | 158 | reg0 = ret; |
a2d1ebaf PB |
159 | break; |
160 | } | |
161 | } | |
faacc041 PM |
162 | if (is_a64(env)) { |
163 | env->xregs[0] = reg0; | |
164 | } else { | |
165 | env->regs[0] = reg0; | |
166 | } | |
167 | } | |
168 | ||
169 | static target_ulong arm_flen_buf(ARMCPU *cpu) | |
170 | { | |
171 | /* Return an address in target memory of 64 bytes where the remote | |
172 | * gdb should write its stat struct. (The format of this structure | |
173 | * is defined by GDB's remote protocol and is not target-specific.) | |
174 | * We put this on the guest's stack just below SP. | |
175 | */ | |
176 | CPUARMState *env = &cpu->env; | |
177 | target_ulong sp; | |
178 | ||
179 | if (is_a64(env)) { | |
180 | sp = env->xregs[31]; | |
181 | } else { | |
182 | sp = env->regs[13]; | |
183 | } | |
184 | ||
185 | return sp - 64; | |
a2d1ebaf PB |
186 | } |
187 | ||
9e0c5422 | 188 | static void arm_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err) |
33d9cc8a | 189 | { |
9e0c5422 AF |
190 | ARMCPU *cpu = ARM_CPU(cs); |
191 | CPUARMState *env = &cpu->env; | |
33d9cc8a PB |
192 | /* The size is always stored in big-endian order, extract |
193 | the value. We assume the size always fit in 32 bits. */ | |
194 | uint32_t size; | |
faacc041 PM |
195 | cpu_memory_rw_debug(cs, arm_flen_buf(cpu) + 32, (uint8_t *)&size, 4, 0); |
196 | size = be32_to_cpu(size); | |
197 | if (is_a64(env)) { | |
198 | env->xregs[0] = size; | |
199 | } else { | |
200 | env->regs[0] = size; | |
201 | } | |
33d9cc8a | 202 | #ifdef CONFIG_USER_ONLY |
0429a971 | 203 | ((TaskState *)cs->opaque)->swi_errno = err; |
33d9cc8a PB |
204 | #else |
205 | syscall_err = err; | |
206 | #endif | |
207 | } | |
208 | ||
bb19cbc9 PM |
209 | static target_ulong arm_gdb_syscall(ARMCPU *cpu, gdb_syscall_complete_cb cb, |
210 | const char *fmt, ...) | |
211 | { | |
212 | va_list va; | |
213 | CPUARMState *env = &cpu->env; | |
214 | ||
215 | va_start(va, fmt); | |
216 | gdb_do_syscallv(cb, fmt, va); | |
217 | va_end(va); | |
218 | ||
219 | /* FIXME: we are implicitly relying on the syscall completing | |
220 | * before this point, which is not guaranteed. We should | |
221 | * put in an explicit synchronization between this and | |
222 | * the callback function. | |
223 | */ | |
224 | ||
faacc041 | 225 | return is_a64(env) ? env->xregs[0] : env->regs[0]; |
bb19cbc9 PM |
226 | } |
227 | ||
f296c0d1 PM |
228 | /* Read the input value from the argument block; fail the semihosting |
229 | * call if the memory read fails. | |
230 | */ | |
231 | #define GET_ARG(n) do { \ | |
faacc041 PM |
232 | if (is_a64(env)) { \ |
233 | if (get_user_u64(arg ## n, args + (n) * 8)) { \ | |
234 | return -1; \ | |
235 | } \ | |
236 | } else { \ | |
237 | if (get_user_u32(arg ## n, args + (n) * 4)) { \ | |
238 | return -1; \ | |
239 | } \ | |
f296c0d1 PM |
240 | } \ |
241 | } while (0) | |
242 | ||
faacc041 PM |
243 | #define SET_ARG(n, val) \ |
244 | (is_a64(env) ? \ | |
245 | put_user_u64(val, args + (n) * 8) : \ | |
246 | put_user_u32(val, args + (n) * 4)) | |
247 | ||
248 | target_ulong do_arm_semihosting(CPUARMState *env) | |
a4f81979 | 249 | { |
878096ee | 250 | ARMCPU *cpu = arm_env_get_cpu(env); |
0429a971 | 251 | CPUState *cs = CPU(cpu); |
53a5960a | 252 | target_ulong args; |
f296c0d1 | 253 | target_ulong arg0, arg1, arg2, arg3; |
a4f81979 FB |
254 | char * s; |
255 | int nr; | |
256 | uint32_t ret; | |
8e71621f PB |
257 | uint32_t len; |
258 | #ifdef CONFIG_USER_ONLY | |
0429a971 | 259 | TaskState *ts = cs->opaque; |
8e71621f | 260 | #else |
81926f47 | 261 | CPUARMState *ts = env; |
8e71621f | 262 | #endif |
a4f81979 | 263 | |
faacc041 PM |
264 | if (is_a64(env)) { |
265 | /* Note that the syscall number is in W0, not X0 */ | |
266 | nr = env->xregs[0] & 0xffffffffU; | |
267 | args = env->xregs[1]; | |
268 | } else { | |
269 | nr = env->regs[0]; | |
270 | args = env->regs[1]; | |
271 | } | |
272 | ||
a4f81979 | 273 | switch (nr) { |
3881725c | 274 | case TARGET_SYS_OPEN: |
f296c0d1 PM |
275 | GET_ARG(0); |
276 | GET_ARG(1); | |
277 | GET_ARG(2); | |
278 | s = lock_user_string(arg0); | |
279 | if (!s) { | |
579a97f7 FB |
280 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
281 | return (uint32_t)-1; | |
f296c0d1 PM |
282 | } |
283 | if (arg1 >= 12) { | |
284 | unlock_user(s, arg0, 0); | |
579a97f7 | 285 | return (uint32_t)-1; |
396bef4b | 286 | } |
a4f81979 | 287 | if (strcmp(s, ":tt") == 0) { |
f296c0d1 PM |
288 | int result_fileno = arg1 < 4 ? STDIN_FILENO : STDOUT_FILENO; |
289 | unlock_user(s, arg0, 0); | |
396bef4b | 290 | return result_fileno; |
a4f81979 | 291 | } |
a2d1ebaf | 292 | if (use_gdb_syscalls()) { |
bb19cbc9 PM |
293 | ret = arm_gdb_syscall(cpu, arm_semi_cb, "open,%s,%x,1a4", arg0, |
294 | (int)arg2+1, gdb_open_modeflags[arg1]); | |
a2d1ebaf | 295 | } else { |
f296c0d1 | 296 | ret = set_swi_errno(ts, open(s, open_modeflags[arg1], 0644)); |
a2d1ebaf | 297 | } |
f296c0d1 | 298 | unlock_user(s, arg0, 0); |
8e71621f | 299 | return ret; |
3881725c | 300 | case TARGET_SYS_CLOSE: |
f296c0d1 | 301 | GET_ARG(0); |
a2d1ebaf | 302 | if (use_gdb_syscalls()) { |
bb19cbc9 | 303 | return arm_gdb_syscall(cpu, arm_semi_cb, "close,%x", arg0); |
a2d1ebaf | 304 | } else { |
f296c0d1 | 305 | return set_swi_errno(ts, close(arg0)); |
a2d1ebaf | 306 | } |
3881725c | 307 | case TARGET_SYS_WRITEC: |
53a5960a | 308 | { |
2f619698 FB |
309 | char c; |
310 | ||
311 | if (get_user_u8(c, args)) | |
312 | /* FIXME - should this error code be -TARGET_EFAULT ? */ | |
313 | return (uint32_t)-1; | |
53a5960a | 314 | /* Write to debug console. stderr is near enough. */ |
a2d1ebaf | 315 | if (use_gdb_syscalls()) { |
bb19cbc9 | 316 | return arm_gdb_syscall(cpu, arm_semi_cb, "write,2,%x,1", args); |
a2d1ebaf PB |
317 | } else { |
318 | return write(STDERR_FILENO, &c, 1); | |
319 | } | |
53a5960a | 320 | } |
3881725c | 321 | case TARGET_SYS_WRITE0: |
579a97f7 FB |
322 | if (!(s = lock_user_string(args))) |
323 | /* FIXME - should this error code be -TARGET_EFAULT ? */ | |
324 | return (uint32_t)-1; | |
a2d1ebaf PB |
325 | len = strlen(s); |
326 | if (use_gdb_syscalls()) { | |
bb19cbc9 PM |
327 | return arm_gdb_syscall(cpu, arm_semi_cb, "write,2,%x,%x", |
328 | args, len); | |
a2d1ebaf PB |
329 | } else { |
330 | ret = write(STDERR_FILENO, s, len); | |
331 | } | |
53a5960a PB |
332 | unlock_user(s, args, 0); |
333 | return ret; | |
3881725c | 334 | case TARGET_SYS_WRITE: |
f296c0d1 PM |
335 | GET_ARG(0); |
336 | GET_ARG(1); | |
337 | GET_ARG(2); | |
338 | len = arg2; | |
a2d1ebaf PB |
339 | if (use_gdb_syscalls()) { |
340 | arm_semi_syscall_len = len; | |
bb19cbc9 PM |
341 | return arm_gdb_syscall(cpu, arm_semi_cb, "write,%x,%x,%x", |
342 | arg0, arg1, len); | |
a2d1ebaf | 343 | } else { |
f296c0d1 PM |
344 | s = lock_user(VERIFY_READ, arg1, len, 1); |
345 | if (!s) { | |
579a97f7 FB |
346 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
347 | return (uint32_t)-1; | |
f296c0d1 PM |
348 | } |
349 | ret = set_swi_errno(ts, write(arg0, s, len)); | |
350 | unlock_user(s, arg1, 0); | |
a2d1ebaf PB |
351 | if (ret == (uint32_t)-1) |
352 | return -1; | |
353 | return len - ret; | |
354 | } | |
3881725c | 355 | case TARGET_SYS_READ: |
f296c0d1 PM |
356 | GET_ARG(0); |
357 | GET_ARG(1); | |
358 | GET_ARG(2); | |
359 | len = arg2; | |
a2d1ebaf PB |
360 | if (use_gdb_syscalls()) { |
361 | arm_semi_syscall_len = len; | |
bb19cbc9 PM |
362 | return arm_gdb_syscall(cpu, arm_semi_cb, "read,%x,%x,%x", |
363 | arg0, arg1, len); | |
a2d1ebaf | 364 | } else { |
f296c0d1 PM |
365 | s = lock_user(VERIFY_WRITE, arg1, len, 0); |
366 | if (!s) { | |
579a97f7 FB |
367 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
368 | return (uint32_t)-1; | |
f296c0d1 PM |
369 | } |
370 | do { | |
371 | ret = set_swi_errno(ts, read(arg0, s, len)); | |
372 | } while (ret == -1 && errno == EINTR); | |
373 | unlock_user(s, arg1, len); | |
a2d1ebaf PB |
374 | if (ret == (uint32_t)-1) |
375 | return -1; | |
376 | return len - ret; | |
377 | } | |
3881725c | 378 | case TARGET_SYS_READC: |
b90372ad | 379 | /* XXX: Read from debug console. Not implemented. */ |
a4f81979 | 380 | return 0; |
3881725c | 381 | case TARGET_SYS_ISTTY: |
f296c0d1 | 382 | GET_ARG(0); |
a2d1ebaf | 383 | if (use_gdb_syscalls()) { |
bb19cbc9 | 384 | return arm_gdb_syscall(cpu, arm_semi_cb, "isatty,%x", arg0); |
a2d1ebaf | 385 | } else { |
f296c0d1 | 386 | return isatty(arg0); |
a2d1ebaf | 387 | } |
3881725c | 388 | case TARGET_SYS_SEEK: |
f296c0d1 PM |
389 | GET_ARG(0); |
390 | GET_ARG(1); | |
a2d1ebaf | 391 | if (use_gdb_syscalls()) { |
bb19cbc9 PM |
392 | return arm_gdb_syscall(cpu, arm_semi_cb, "lseek,%x,%x,0", |
393 | arg0, arg1); | |
a2d1ebaf | 394 | } else { |
f296c0d1 | 395 | ret = set_swi_errno(ts, lseek(arg0, arg1, SEEK_SET)); |
a2d1ebaf PB |
396 | if (ret == (uint32_t)-1) |
397 | return -1; | |
398 | return 0; | |
399 | } | |
3881725c | 400 | case TARGET_SYS_FLEN: |
f296c0d1 | 401 | GET_ARG(0); |
a2d1ebaf | 402 | if (use_gdb_syscalls()) { |
bb19cbc9 | 403 | return arm_gdb_syscall(cpu, arm_semi_flen_cb, "fstat,%x,%x", |
faacc041 | 404 | arg0, arm_flen_buf(cpu)); |
a2d1ebaf | 405 | } else { |
a4f81979 | 406 | struct stat buf; |
f296c0d1 | 407 | ret = set_swi_errno(ts, fstat(arg0, &buf)); |
a4f81979 FB |
408 | if (ret == (uint32_t)-1) |
409 | return -1; | |
410 | return buf.st_size; | |
411 | } | |
3881725c | 412 | case TARGET_SYS_TMPNAM: |
a4f81979 FB |
413 | /* XXX: Not implemented. */ |
414 | return -1; | |
3881725c | 415 | case TARGET_SYS_REMOVE: |
f296c0d1 PM |
416 | GET_ARG(0); |
417 | GET_ARG(1); | |
a2d1ebaf | 418 | if (use_gdb_syscalls()) { |
bb19cbc9 PM |
419 | ret = arm_gdb_syscall(cpu, arm_semi_cb, "unlink,%s", |
420 | arg0, (int)arg1+1); | |
a2d1ebaf | 421 | } else { |
f296c0d1 PM |
422 | s = lock_user_string(arg0); |
423 | if (!s) { | |
579a97f7 FB |
424 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
425 | return (uint32_t)-1; | |
f296c0d1 | 426 | } |
a2d1ebaf | 427 | ret = set_swi_errno(ts, remove(s)); |
f296c0d1 | 428 | unlock_user(s, arg0, 0); |
a2d1ebaf | 429 | } |
8e71621f | 430 | return ret; |
3881725c | 431 | case TARGET_SYS_RENAME: |
f296c0d1 PM |
432 | GET_ARG(0); |
433 | GET_ARG(1); | |
434 | GET_ARG(2); | |
435 | GET_ARG(3); | |
a2d1ebaf | 436 | if (use_gdb_syscalls()) { |
bb19cbc9 PM |
437 | return arm_gdb_syscall(cpu, arm_semi_cb, "rename,%s,%s", |
438 | arg0, (int)arg1+1, arg2, (int)arg3+1); | |
a2d1ebaf | 439 | } else { |
8e71621f | 440 | char *s2; |
f296c0d1 PM |
441 | s = lock_user_string(arg0); |
442 | s2 = lock_user_string(arg2); | |
579a97f7 FB |
443 | if (!s || !s2) |
444 | /* FIXME - should this error code be -TARGET_EFAULT ? */ | |
445 | ret = (uint32_t)-1; | |
446 | else | |
447 | ret = set_swi_errno(ts, rename(s, s2)); | |
448 | if (s2) | |
f296c0d1 | 449 | unlock_user(s2, arg2, 0); |
579a97f7 | 450 | if (s) |
f296c0d1 | 451 | unlock_user(s, arg0, 0); |
8e71621f PB |
452 | return ret; |
453 | } | |
3881725c | 454 | case TARGET_SYS_CLOCK: |
a4f81979 | 455 | return clock() / (CLOCKS_PER_SEC / 100); |
3881725c | 456 | case TARGET_SYS_TIME: |
a4f81979 | 457 | return set_swi_errno(ts, time(NULL)); |
3881725c | 458 | case TARGET_SYS_SYSTEM: |
f296c0d1 PM |
459 | GET_ARG(0); |
460 | GET_ARG(1); | |
a2d1ebaf | 461 | if (use_gdb_syscalls()) { |
bb19cbc9 PM |
462 | return arm_gdb_syscall(cpu, arm_semi_cb, "system,%s", |
463 | arg0, (int)arg1+1); | |
a2d1ebaf | 464 | } else { |
f296c0d1 PM |
465 | s = lock_user_string(arg0); |
466 | if (!s) { | |
579a97f7 FB |
467 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
468 | return (uint32_t)-1; | |
f296c0d1 | 469 | } |
a2d1ebaf | 470 | ret = set_swi_errno(ts, system(s)); |
f296c0d1 | 471 | unlock_user(s, arg0, 0); |
a982b531 | 472 | return ret; |
a2d1ebaf | 473 | } |
3881725c | 474 | case TARGET_SYS_ERRNO: |
8e71621f | 475 | #ifdef CONFIG_USER_ONLY |
a4f81979 | 476 | return ts->swi_errno; |
8e71621f | 477 | #else |
33d9cc8a | 478 | return syscall_err; |
8e71621f | 479 | #endif |
3881725c | 480 | case TARGET_SYS_GET_CMDLINE: |
38d0662a | 481 | { |
1c1b40c1 CV |
482 | /* Build a command-line from the original argv. |
483 | * | |
484 | * The inputs are: | |
f296c0d1 PM |
485 | * * arg0, pointer to a buffer of at least the size |
486 | * specified in arg1. | |
487 | * * arg1, size of the buffer pointed to by arg0 in | |
1c1b40c1 CV |
488 | * bytes. |
489 | * | |
490 | * The outputs are: | |
f296c0d1 | 491 | * * arg0, pointer to null-terminated string of the |
1c1b40c1 | 492 | * command line. |
f296c0d1 | 493 | * * arg1, length of the string pointed to by arg0. |
1c1b40c1 | 494 | */ |
579a97f7 | 495 | |
1c1b40c1 | 496 | char *output_buffer; |
f296c0d1 | 497 | size_t input_size; |
1c1b40c1 CV |
498 | size_t output_size; |
499 | int status = 0; | |
f3c2bda2 LI |
500 | #if !defined(CONFIG_USER_ONLY) |
501 | const char *cmdline; | |
502 | #endif | |
f296c0d1 PM |
503 | GET_ARG(0); |
504 | GET_ARG(1); | |
505 | input_size = arg1; | |
1c1b40c1 CV |
506 | /* Compute the size of the output string. */ |
507 | #if !defined(CONFIG_USER_ONLY) | |
f3c2bda2 LI |
508 | cmdline = semihosting_get_cmdline(); |
509 | if (cmdline == NULL) { | |
510 | cmdline = ""; /* Default to an empty line. */ | |
511 | } | |
512 | output_size = strlen(cmdline) + 1; /* Count terminating 0. */ | |
1c1b40c1 CV |
513 | #else |
514 | unsigned int i; | |
38d0662a | 515 | |
1c1b40c1 CV |
516 | output_size = ts->info->arg_end - ts->info->arg_start; |
517 | if (!output_size) { | |
2e8785ac WS |
518 | /* We special-case the "empty command line" case (argc==0). |
519 | Just provide the terminating 0. */ | |
1c1b40c1 CV |
520 | output_size = 1; |
521 | } | |
522 | #endif | |
38d0662a | 523 | |
1c1b40c1 CV |
524 | if (output_size > input_size) { |
525 | /* Not enough space to store command-line arguments. */ | |
526 | return -1; | |
38d0662a | 527 | } |
38d0662a | 528 | |
1c1b40c1 | 529 | /* Adjust the command-line length. */ |
f296c0d1 PM |
530 | if (SET_ARG(1, output_size - 1)) { |
531 | /* Couldn't write back to argument block */ | |
532 | return -1; | |
533 | } | |
38d0662a | 534 | |
1c1b40c1 | 535 | /* Lock the buffer on the ARM side. */ |
f296c0d1 | 536 | output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0); |
1c1b40c1 CV |
537 | if (!output_buffer) { |
538 | return -1; | |
539 | } | |
38d0662a | 540 | |
1c1b40c1 CV |
541 | /* Copy the command-line arguments. */ |
542 | #if !defined(CONFIG_USER_ONLY) | |
f3c2bda2 | 543 | pstrcpy(output_buffer, output_size, cmdline); |
1c1b40c1 CV |
544 | #else |
545 | if (output_size == 1) { | |
546 | /* Empty command-line. */ | |
547 | output_buffer[0] = '\0'; | |
548 | goto out; | |
549 | } | |
2e8785ac | 550 | |
1c1b40c1 CV |
551 | if (copy_from_user(output_buffer, ts->info->arg_start, |
552 | output_size)) { | |
553 | status = -1; | |
554 | goto out; | |
2e8785ac WS |
555 | } |
556 | ||
1c1b40c1 CV |
557 | /* Separate arguments by white spaces. */ |
558 | for (i = 0; i < output_size - 1; i++) { | |
559 | if (output_buffer[i] == 0) { | |
560 | output_buffer[i] = ' '; | |
561 | } | |
562 | } | |
563 | out: | |
564 | #endif | |
565 | /* Unlock the buffer on the ARM side. */ | |
f296c0d1 | 566 | unlock_user(output_buffer, arg0, output_size); |
2e8785ac | 567 | |
1c1b40c1 | 568 | return status; |
38d0662a | 569 | } |
3881725c | 570 | case TARGET_SYS_HEAPINFO: |
a4f81979 FB |
571 | { |
572 | uint32_t *ptr; | |
573 | uint32_t limit; | |
f296c0d1 | 574 | GET_ARG(0); |
a4f81979 | 575 | |
8e71621f PB |
576 | #ifdef CONFIG_USER_ONLY |
577 | /* Some C libraries assume the heap immediately follows .bss, so | |
a4f81979 FB |
578 | allocate it using sbrk. */ |
579 | if (!ts->heap_limit) { | |
206ae74a | 580 | abi_ulong ret; |
a4f81979 | 581 | |
53a5960a | 582 | ts->heap_base = do_brk(0); |
a4f81979 FB |
583 | limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE; |
584 | /* Try a big heap, and reduce the size if that fails. */ | |
585 | for (;;) { | |
53a5960a | 586 | ret = do_brk(limit); |
206ae74a | 587 | if (ret >= limit) { |
a4f81979 | 588 | break; |
206ae74a | 589 | } |
a4f81979 FB |
590 | limit = (ts->heap_base >> 1) + (limit >> 1); |
591 | } | |
592 | ts->heap_limit = limit; | |
593 | } | |
3b46e624 | 594 | |
f296c0d1 PM |
595 | ptr = lock_user(VERIFY_WRITE, arg0, 16, 0); |
596 | if (!ptr) { | |
579a97f7 FB |
597 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
598 | return (uint32_t)-1; | |
f296c0d1 | 599 | } |
a4f81979 FB |
600 | ptr[0] = tswap32(ts->heap_base); |
601 | ptr[1] = tswap32(ts->heap_limit); | |
602 | ptr[2] = tswap32(ts->stack_base); | |
603 | ptr[3] = tswap32(0); /* Stack limit. */ | |
f296c0d1 | 604 | unlock_user(ptr, arg0, 16); |
8e71621f PB |
605 | #else |
606 | limit = ram_size; | |
f296c0d1 PM |
607 | ptr = lock_user(VERIFY_WRITE, arg0, 16, 0); |
608 | if (!ptr) { | |
579a97f7 FB |
609 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
610 | return (uint32_t)-1; | |
f296c0d1 | 611 | } |
8e71621f PB |
612 | /* TODO: Make this use the limit of the loaded application. */ |
613 | ptr[0] = tswap32(limit / 2); | |
614 | ptr[1] = tswap32(limit); | |
615 | ptr[2] = tswap32(limit); /* Stack base */ | |
616 | ptr[3] = tswap32(0); /* Stack limit. */ | |
f296c0d1 | 617 | unlock_user(ptr, arg0, 16); |
8e71621f | 618 | #endif |
a4f81979 FB |
619 | return 0; |
620 | } | |
3881725c | 621 | case TARGET_SYS_EXIT: |
7446d35e PM |
622 | if (is_a64(env)) { |
623 | /* The A64 version of this call takes a parameter block, | |
624 | * so the application-exit type can return a subcode which | |
625 | * is the exit status code from the application. | |
626 | */ | |
627 | GET_ARG(0); | |
628 | GET_ARG(1); | |
629 | ||
630 | if (arg0 == ADP_Stopped_ApplicationExit) { | |
631 | ret = arg1; | |
632 | } else { | |
633 | ret = 1; | |
634 | } | |
635 | } else { | |
636 | /* ARM specifies only Stopped_ApplicationExit as normal | |
637 | * exit, everything else is considered an error */ | |
638 | ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1; | |
639 | } | |
1ecc3a2d LI |
640 | gdb_exit(env, ret); |
641 | exit(ret); | |
e9ebfbfc PM |
642 | case TARGET_SYS_SYNCCACHE: |
643 | /* Clean the D-cache and invalidate the I-cache for the specified | |
644 | * virtual address range. This is a nop for us since we don't | |
645 | * implement caches. This is only present on A64. | |
646 | */ | |
647 | if (is_a64(env)) { | |
648 | return 0; | |
649 | } | |
650 | /* fall through -- invalid for A32/T32 */ | |
a4f81979 FB |
651 | default: |
652 | fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr); | |
0429a971 | 653 | cpu_dump_state(cs, stderr, fprintf, 0); |
a4f81979 FB |
654 | abort(); |
655 | } | |
656 | } |