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