bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Arm "Angel" semihosting syscalls |
| 3 | * |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 4 | * Copyright (c) 2005, 2007 CodeSourcery. |
| 5 | * Written by Paul Brook. |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 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 |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <time.h> |
| 29 | |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 30 | #include "cpu.h" |
| 31 | #ifdef CONFIG_USER_ONLY |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 32 | #include "qemu.h" |
| 33 | |
| 34 | #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024) |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 35 | #else |
| 36 | #include "vl.h" |
| 37 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 38 | |
| 39 | #define SYS_OPEN 0x01 |
| 40 | #define SYS_CLOSE 0x02 |
| 41 | #define SYS_WRITEC 0x03 |
| 42 | #define SYS_WRITE0 0x04 |
| 43 | #define SYS_WRITE 0x05 |
| 44 | #define SYS_READ 0x06 |
| 45 | #define SYS_READC 0x07 |
| 46 | #define SYS_ISTTY 0x09 |
| 47 | #define SYS_SEEK 0x0a |
| 48 | #define SYS_FLEN 0x0c |
| 49 | #define SYS_TMPNAM 0x0d |
| 50 | #define SYS_REMOVE 0x0e |
| 51 | #define SYS_RENAME 0x0f |
| 52 | #define SYS_CLOCK 0x10 |
| 53 | #define SYS_TIME 0x11 |
| 54 | #define SYS_SYSTEM 0x12 |
| 55 | #define SYS_ERRNO 0x13 |
| 56 | #define SYS_GET_CMDLINE 0x15 |
| 57 | #define SYS_HEAPINFO 0x16 |
| 58 | #define SYS_EXIT 0x18 |
| 59 | |
| 60 | #ifndef O_BINARY |
| 61 | #define O_BINARY 0 |
| 62 | #endif |
| 63 | |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 64 | #define GDB_O_RDONLY 0x000 |
| 65 | #define GDB_O_WRONLY 0x001 |
| 66 | #define GDB_O_RDWR 0x002 |
| 67 | #define GDB_O_APPEND 0x008 |
| 68 | #define GDB_O_CREAT 0x200 |
| 69 | #define GDB_O_TRUNC 0x400 |
| 70 | #define GDB_O_BINARY 0 |
| 71 | |
| 72 | static int gdb_open_modeflags[12] = { |
| 73 | GDB_O_RDONLY, |
| 74 | GDB_O_RDONLY | GDB_O_BINARY, |
| 75 | GDB_O_RDWR, |
| 76 | GDB_O_RDWR | GDB_O_BINARY, |
| 77 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC, |
| 78 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY, |
| 79 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC, |
| 80 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY, |
| 81 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND, |
| 82 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY, |
| 83 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND, |
| 84 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY |
| 85 | }; |
| 86 | |
| 87 | static int open_modeflags[12] = { |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 88 | O_RDONLY, |
| 89 | O_RDONLY | O_BINARY, |
| 90 | O_RDWR, |
| 91 | O_RDWR | O_BINARY, |
| 92 | O_WRONLY | O_CREAT | O_TRUNC, |
| 93 | O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
| 94 | O_RDWR | O_CREAT | O_TRUNC, |
| 95 | O_RDWR | O_CREAT | O_TRUNC | O_BINARY, |
| 96 | O_WRONLY | O_CREAT | O_APPEND, |
| 97 | O_WRONLY | O_CREAT | O_APPEND | O_BINARY, |
| 98 | O_RDWR | O_CREAT | O_APPEND, |
| 99 | O_RDWR | O_CREAT | O_APPEND | O_BINARY |
| 100 | }; |
| 101 | |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 102 | #ifdef CONFIG_USER_ONLY |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 103 | static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code) |
| 104 | { |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 105 | if (code == (uint32_t)-1) |
| 106 | ts->swi_errno = errno; |
| 107 | return code; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 108 | } |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 109 | #else |
| 110 | static inline uint32_t set_swi_errno(CPUState *env, uint32_t code) |
| 111 | { |
| 112 | return code; |
| 113 | } |
| 114 | |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 115 | #include "softmmu-semi.h" |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 116 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 117 | |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 118 | static target_ulong arm_semi_syscall_len; |
| 119 | |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 120 | #if !defined(CONFIG_USER_ONLY) |
| 121 | static target_ulong syscall_err; |
| 122 | #endif |
| 123 | |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 124 | static void arm_semi_cb(CPUState *env, target_ulong ret, target_ulong err) |
| 125 | { |
| 126 | #ifdef CONFIG_USER_ONLY |
| 127 | TaskState *ts = env->opaque; |
| 128 | #endif |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 129 | |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 130 | if (ret == (target_ulong)-1) { |
| 131 | #ifdef CONFIG_USER_ONLY |
| 132 | ts->swi_errno = err; |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 133 | #else |
| 134 | syscall_err = err; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 135 | #endif |
| 136 | env->regs[0] = ret; |
| 137 | } else { |
| 138 | /* Fixup syscalls that use nonstardard return conventions. */ |
| 139 | switch (env->regs[0]) { |
| 140 | case SYS_WRITE: |
| 141 | case SYS_READ: |
| 142 | env->regs[0] = arm_semi_syscall_len - ret; |
| 143 | break; |
| 144 | case SYS_SEEK: |
| 145 | env->regs[0] = 0; |
| 146 | break; |
| 147 | default: |
| 148 | env->regs[0] = ret; |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 154 | static void arm_semi_flen_cb(CPUState *env, target_ulong ret, target_ulong err) |
| 155 | { |
| 156 | /* The size is always stored in big-endian order, extract |
| 157 | the value. We assume the size always fit in 32 bits. */ |
| 158 | uint32_t size; |
| 159 | cpu_memory_rw_debug(env, env->regs[13]-64+32, (uint8_t *)&size, 4, 0); |
| 160 | env->regs[0] = be32_to_cpu(size); |
| 161 | #ifdef CONFIG_USER_ONLY |
| 162 | ((TaskState *)env->opaque)->swi_errno = err; |
| 163 | #else |
| 164 | syscall_err = err; |
| 165 | #endif |
| 166 | } |
| 167 | |
pbrook | 38d0662 | 2006-11-19 20:29:35 +0000 | [diff] [blame] | 168 | #define ARG(n) tget32(args + (n) * 4) |
| 169 | #define SET_ARG(n, val) tput32(args + (n) * 4,val) |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 170 | uint32_t do_arm_semihosting(CPUState *env) |
| 171 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 172 | target_ulong args; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 173 | char * s; |
| 174 | int nr; |
| 175 | uint32_t ret; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 176 | uint32_t len; |
| 177 | #ifdef CONFIG_USER_ONLY |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 178 | TaskState *ts = env->opaque; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 179 | #else |
| 180 | CPUState *ts = env; |
| 181 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 182 | |
| 183 | nr = env->regs[0]; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 184 | args = env->regs[1]; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 185 | switch (nr) { |
| 186 | case SYS_OPEN: |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 187 | s = lock_user_string(ARG(0)); |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 188 | if (ARG(1) >= 12) |
| 189 | return (uint32_t)-1; |
| 190 | if (strcmp(s, ":tt") == 0) { |
| 191 | if (ARG(1) < 4) |
| 192 | return STDIN_FILENO; |
| 193 | else |
| 194 | return STDOUT_FILENO; |
| 195 | } |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 196 | if (use_gdb_syscalls()) { |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 197 | gdb_do_syscall(arm_semi_cb, "open,%s,%x,1a4", ARG(0), |
| 198 | (int)ARG(2)+1, gdb_open_modeflags[ARG(1)]); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 199 | return env->regs[0]; |
| 200 | } else { |
| 201 | ret = set_swi_errno(ts, open(s, open_modeflags[ARG(1)], 0644)); |
| 202 | } |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 203 | unlock_user(s, ARG(0), 0); |
| 204 | return ret; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 205 | case SYS_CLOSE: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 206 | if (use_gdb_syscalls()) { |
| 207 | gdb_do_syscall(arm_semi_cb, "close,%x", ARG(0)); |
| 208 | return env->regs[0]; |
| 209 | } else { |
| 210 | return set_swi_errno(ts, close(ARG(0))); |
| 211 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 212 | case SYS_WRITEC: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 213 | { |
| 214 | char c = tget8(args); |
| 215 | /* Write to debug console. stderr is near enough. */ |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 216 | if (use_gdb_syscalls()) { |
| 217 | gdb_do_syscall(arm_semi_cb, "write,2,%x,1", args); |
| 218 | return env->regs[0]; |
| 219 | } else { |
| 220 | return write(STDERR_FILENO, &c, 1); |
| 221 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 222 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 223 | case SYS_WRITE0: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 224 | s = lock_user_string(args); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 225 | len = strlen(s); |
| 226 | if (use_gdb_syscalls()) { |
| 227 | gdb_do_syscall(arm_semi_cb, "write,2,%x,%x\n", args, len); |
| 228 | ret = env->regs[0]; |
| 229 | } else { |
| 230 | ret = write(STDERR_FILENO, s, len); |
| 231 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 232 | unlock_user(s, args, 0); |
| 233 | return ret; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 234 | case SYS_WRITE: |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 235 | len = ARG(2); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 236 | if (use_gdb_syscalls()) { |
| 237 | arm_semi_syscall_len = len; |
| 238 | gdb_do_syscall(arm_semi_cb, "write,%x,%x,%x", ARG(0), ARG(1), len); |
| 239 | return env->regs[0]; |
| 240 | } else { |
| 241 | s = lock_user(ARG(1), len, 1); |
| 242 | ret = set_swi_errno(ts, write(ARG(0), s, len)); |
| 243 | unlock_user(s, ARG(1), 0); |
| 244 | if (ret == (uint32_t)-1) |
| 245 | return -1; |
| 246 | return len - ret; |
| 247 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 248 | case SYS_READ: |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 249 | len = ARG(2); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 250 | if (use_gdb_syscalls()) { |
| 251 | arm_semi_syscall_len = len; |
| 252 | gdb_do_syscall(arm_semi_cb, "read,%x,%x,%x", ARG(0), ARG(1), len); |
| 253 | return env->regs[0]; |
| 254 | } else { |
| 255 | s = lock_user(ARG(1), len, 0); |
| 256 | do |
| 257 | ret = set_swi_errno(ts, read(ARG(0), s, len)); |
| 258 | while (ret == -1 && errno == EINTR); |
| 259 | unlock_user(s, ARG(1), len); |
| 260 | if (ret == (uint32_t)-1) |
| 261 | return -1; |
| 262 | return len - ret; |
| 263 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 264 | case SYS_READC: |
| 265 | /* XXX: Read from debug cosole. Not implemented. */ |
| 266 | return 0; |
| 267 | case SYS_ISTTY: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 268 | if (use_gdb_syscalls()) { |
| 269 | gdb_do_syscall(arm_semi_cb, "isatty,%x", ARG(0)); |
| 270 | return env->regs[0]; |
| 271 | } else { |
| 272 | return isatty(ARG(0)); |
| 273 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 274 | case SYS_SEEK: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 275 | if (use_gdb_syscalls()) { |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 276 | gdb_do_syscall(arm_semi_cb, "lseek,%x,%x,0", ARG(0), ARG(1)); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 277 | return env->regs[0]; |
| 278 | } else { |
| 279 | ret = set_swi_errno(ts, lseek(ARG(0), ARG(1), SEEK_SET)); |
| 280 | if (ret == (uint32_t)-1) |
| 281 | return -1; |
| 282 | return 0; |
| 283 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 284 | case SYS_FLEN: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 285 | if (use_gdb_syscalls()) { |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 286 | gdb_do_syscall(arm_semi_flen_cb, "fstat,%x,%x", |
| 287 | ARG(0), env->regs[13]-64); |
| 288 | return env->regs[0]; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 289 | } else { |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 290 | struct stat buf; |
| 291 | ret = set_swi_errno(ts, fstat(ARG(0), &buf)); |
| 292 | if (ret == (uint32_t)-1) |
| 293 | return -1; |
| 294 | return buf.st_size; |
| 295 | } |
| 296 | case SYS_TMPNAM: |
| 297 | /* XXX: Not implemented. */ |
| 298 | return -1; |
| 299 | case SYS_REMOVE: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 300 | if (use_gdb_syscalls()) { |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 301 | gdb_do_syscall(arm_semi_cb, "unlink,%s", ARG(0), (int)ARG(1)+1); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 302 | ret = env->regs[0]; |
| 303 | } else { |
| 304 | s = lock_user_string(ARG(0)); |
| 305 | ret = set_swi_errno(ts, remove(s)); |
| 306 | unlock_user(s, ARG(0), 0); |
| 307 | } |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 308 | return ret; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 309 | case SYS_RENAME: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 310 | if (use_gdb_syscalls()) { |
| 311 | gdb_do_syscall(arm_semi_cb, "rename,%s,%s", |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 312 | ARG(0), (int)ARG(1)+1, ARG(2), (int)ARG(3)+1); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 313 | return env->regs[0]; |
| 314 | } else { |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 315 | char *s2; |
| 316 | s = lock_user_string(ARG(0)); |
| 317 | s2 = lock_user_string(ARG(2)); |
| 318 | ret = set_swi_errno(ts, rename(s, s2)); |
| 319 | unlock_user(s2, ARG(2), 0); |
| 320 | unlock_user(s, ARG(0), 0); |
| 321 | return ret; |
| 322 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 323 | case SYS_CLOCK: |
| 324 | return clock() / (CLOCKS_PER_SEC / 100); |
| 325 | case SYS_TIME: |
| 326 | return set_swi_errno(ts, time(NULL)); |
| 327 | case SYS_SYSTEM: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 328 | if (use_gdb_syscalls()) { |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 329 | gdb_do_syscall(arm_semi_cb, "system,%s", ARG(0), (int)ARG(1)+1); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 330 | return env->regs[0]; |
| 331 | } else { |
| 332 | s = lock_user_string(ARG(0)); |
| 333 | ret = set_swi_errno(ts, system(s)); |
| 334 | unlock_user(s, ARG(0), 0); |
| 335 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 336 | case SYS_ERRNO: |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 337 | #ifdef CONFIG_USER_ONLY |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 338 | return ts->swi_errno; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 339 | #else |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 340 | return syscall_err; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 341 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 342 | case SYS_GET_CMDLINE: |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 343 | #ifdef CONFIG_USER_ONLY |
pbrook | 38d0662 | 2006-11-19 20:29:35 +0000 | [diff] [blame] | 344 | /* Build a commandline from the original argv. */ |
| 345 | { |
| 346 | char **arg = ts->info->host_argv; |
| 347 | int len = ARG(1); |
| 348 | /* lock the buffer on the ARM side */ |
| 349 | char *cmdline_buffer = (char*)lock_user(ARG(0), len, 0); |
| 350 | |
| 351 | s = cmdline_buffer; |
| 352 | while (*arg && len > 2) { |
| 353 | int n = strlen(*arg); |
| 354 | |
| 355 | if (s != cmdline_buffer) { |
| 356 | *(s++) = ' '; |
| 357 | len--; |
| 358 | } |
| 359 | if (n >= len) |
| 360 | n = len - 1; |
| 361 | memcpy(s, *arg, n); |
| 362 | s += n; |
| 363 | len -= n; |
| 364 | arg++; |
| 365 | } |
| 366 | /* Null terminate the string. */ |
| 367 | *s = 0; |
| 368 | len = s - cmdline_buffer; |
| 369 | |
| 370 | /* Unlock the buffer on the ARM side. */ |
| 371 | unlock_user(cmdline_buffer, ARG(0), len); |
| 372 | |
| 373 | /* Adjust the commandline length argument. */ |
| 374 | SET_ARG(1, len); |
| 375 | |
| 376 | /* Return success if commandline fit into buffer. */ |
| 377 | return *arg ? -1 : 0; |
| 378 | } |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 379 | #else |
| 380 | return -1; |
| 381 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 382 | case SYS_HEAPINFO: |
| 383 | { |
| 384 | uint32_t *ptr; |
| 385 | uint32_t limit; |
| 386 | |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 387 | #ifdef CONFIG_USER_ONLY |
| 388 | /* Some C libraries assume the heap immediately follows .bss, so |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 389 | allocate it using sbrk. */ |
| 390 | if (!ts->heap_limit) { |
| 391 | long ret; |
| 392 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 393 | ts->heap_base = do_brk(0); |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 394 | limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE; |
| 395 | /* Try a big heap, and reduce the size if that fails. */ |
| 396 | for (;;) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 397 | ret = do_brk(limit); |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 398 | if (ret != -1) |
| 399 | break; |
| 400 | limit = (ts->heap_base >> 1) + (limit >> 1); |
| 401 | } |
| 402 | ts->heap_limit = limit; |
| 403 | } |
| 404 | |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 405 | ptr = lock_user(ARG(0), 16, 0); |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 406 | ptr[0] = tswap32(ts->heap_base); |
| 407 | ptr[1] = tswap32(ts->heap_limit); |
| 408 | ptr[2] = tswap32(ts->stack_base); |
| 409 | ptr[3] = tswap32(0); /* Stack limit. */ |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 410 | unlock_user(ptr, ARG(0), 16); |
| 411 | #else |
| 412 | limit = ram_size; |
| 413 | ptr = lock_user(ARG(0), 16, 0); |
| 414 | /* TODO: Make this use the limit of the loaded application. */ |
| 415 | ptr[0] = tswap32(limit / 2); |
| 416 | ptr[1] = tswap32(limit); |
| 417 | ptr[2] = tswap32(limit); /* Stack base */ |
| 418 | ptr[3] = tswap32(0); /* Stack limit. */ |
| 419 | unlock_user(ptr, ARG(0), 16); |
| 420 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | case SYS_EXIT: |
| 424 | exit(0); |
| 425 | default: |
| 426 | fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr); |
| 427 | cpu_dump_state(env, stderr, fprintf, 0); |
| 428 | abort(); |
| 429 | } |
| 430 | } |