bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Arm "Angel" semihosting syscalls |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 18 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 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 | |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 29 | #include "cpu.h" |
| 30 | #ifdef CONFIG_USER_ONLY |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 31 | #include "qemu.h" |
| 32 | |
| 33 | #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024) |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 34 | #else |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 35 | #include "qemu-common.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 36 | #include "gdbstub.h" |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 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 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 168 | #define ARG(n) \ |
| 169 | ({ \ |
| 170 | target_ulong __arg; \ |
| 171 | /* FIXME - handle get_user() failure */ \ |
| 172 | get_user_ual(__arg, args + (n) * 4); \ |
| 173 | __arg; \ |
| 174 | }) |
| 175 | #define SET_ARG(n, val) put_user_ual(val, args + (n) * 4) |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 176 | uint32_t do_arm_semihosting(CPUState *env) |
| 177 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 178 | target_ulong args; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 179 | char * s; |
| 180 | int nr; |
| 181 | uint32_t ret; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 182 | uint32_t len; |
| 183 | #ifdef CONFIG_USER_ONLY |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 184 | TaskState *ts = env->opaque; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 185 | #else |
| 186 | CPUState *ts = env; |
| 187 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 188 | |
| 189 | nr = env->regs[0]; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 190 | args = env->regs[1]; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 191 | switch (nr) { |
| 192 | case SYS_OPEN: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 193 | if (!(s = lock_user_string(ARG(0)))) |
| 194 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 195 | return (uint32_t)-1; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 196 | if (ARG(1) >= 12) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 197 | return (uint32_t)-1; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 198 | if (strcmp(s, ":tt") == 0) { |
| 199 | if (ARG(1) < 4) |
| 200 | return STDIN_FILENO; |
| 201 | else |
| 202 | return STDOUT_FILENO; |
| 203 | } |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 204 | if (use_gdb_syscalls()) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 205 | gdb_do_syscall(arm_semi_cb, "open,%s,%x,1a4", ARG(0), |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 206 | (int)ARG(2)+1, gdb_open_modeflags[ARG(1)]); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 207 | return env->regs[0]; |
| 208 | } else { |
| 209 | ret = set_swi_errno(ts, open(s, open_modeflags[ARG(1)], 0644)); |
| 210 | } |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 211 | unlock_user(s, ARG(0), 0); |
| 212 | return ret; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 213 | case SYS_CLOSE: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 214 | if (use_gdb_syscalls()) { |
| 215 | gdb_do_syscall(arm_semi_cb, "close,%x", ARG(0)); |
| 216 | return env->regs[0]; |
| 217 | } else { |
| 218 | return set_swi_errno(ts, close(ARG(0))); |
| 219 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 220 | case SYS_WRITEC: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 221 | { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 222 | char c; |
| 223 | |
| 224 | if (get_user_u8(c, args)) |
| 225 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 226 | return (uint32_t)-1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 227 | /* Write to debug console. stderr is near enough. */ |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 228 | if (use_gdb_syscalls()) { |
| 229 | gdb_do_syscall(arm_semi_cb, "write,2,%x,1", args); |
| 230 | return env->regs[0]; |
| 231 | } else { |
| 232 | return write(STDERR_FILENO, &c, 1); |
| 233 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 234 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 235 | case SYS_WRITE0: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 236 | if (!(s = lock_user_string(args))) |
| 237 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 238 | return (uint32_t)-1; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 239 | len = strlen(s); |
| 240 | if (use_gdb_syscalls()) { |
| 241 | gdb_do_syscall(arm_semi_cb, "write,2,%x,%x\n", args, len); |
| 242 | ret = env->regs[0]; |
| 243 | } else { |
| 244 | ret = write(STDERR_FILENO, s, len); |
| 245 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 246 | unlock_user(s, args, 0); |
| 247 | return ret; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 248 | case SYS_WRITE: |
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, "write,%x,%x,%x", ARG(0), ARG(1), len); |
| 253 | return env->regs[0]; |
| 254 | } else { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 255 | if (!(s = lock_user(VERIFY_READ, ARG(1), len, 1))) |
| 256 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 257 | return (uint32_t)-1; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 258 | ret = set_swi_errno(ts, write(ARG(0), s, len)); |
| 259 | unlock_user(s, ARG(1), 0); |
| 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_READ: |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 265 | len = ARG(2); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 266 | if (use_gdb_syscalls()) { |
| 267 | arm_semi_syscall_len = len; |
| 268 | gdb_do_syscall(arm_semi_cb, "read,%x,%x,%x", ARG(0), ARG(1), len); |
| 269 | return env->regs[0]; |
| 270 | } else { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 271 | if (!(s = lock_user(VERIFY_WRITE, ARG(1), len, 0))) |
| 272 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 273 | return (uint32_t)-1; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 274 | do |
| 275 | ret = set_swi_errno(ts, read(ARG(0), s, len)); |
| 276 | while (ret == -1 && errno == EINTR); |
| 277 | unlock_user(s, ARG(1), len); |
| 278 | if (ret == (uint32_t)-1) |
| 279 | return -1; |
| 280 | return len - ret; |
| 281 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 282 | case SYS_READC: |
| 283 | /* XXX: Read from debug cosole. Not implemented. */ |
| 284 | return 0; |
| 285 | case SYS_ISTTY: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 286 | if (use_gdb_syscalls()) { |
| 287 | gdb_do_syscall(arm_semi_cb, "isatty,%x", ARG(0)); |
| 288 | return env->regs[0]; |
| 289 | } else { |
| 290 | return isatty(ARG(0)); |
| 291 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 292 | case SYS_SEEK: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 293 | if (use_gdb_syscalls()) { |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 294 | 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] | 295 | return env->regs[0]; |
| 296 | } else { |
| 297 | ret = set_swi_errno(ts, lseek(ARG(0), ARG(1), SEEK_SET)); |
| 298 | if (ret == (uint32_t)-1) |
| 299 | return -1; |
| 300 | return 0; |
| 301 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 302 | case SYS_FLEN: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 303 | if (use_gdb_syscalls()) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 304 | gdb_do_syscall(arm_semi_flen_cb, "fstat,%x,%x", |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 305 | ARG(0), env->regs[13]-64); |
| 306 | return env->regs[0]; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 307 | } else { |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 308 | struct stat buf; |
| 309 | ret = set_swi_errno(ts, fstat(ARG(0), &buf)); |
| 310 | if (ret == (uint32_t)-1) |
| 311 | return -1; |
| 312 | return buf.st_size; |
| 313 | } |
| 314 | case SYS_TMPNAM: |
| 315 | /* XXX: Not implemented. */ |
| 316 | return -1; |
| 317 | case SYS_REMOVE: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 318 | if (use_gdb_syscalls()) { |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 319 | 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] | 320 | ret = env->regs[0]; |
| 321 | } else { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 322 | if (!(s = lock_user_string(ARG(0)))) |
| 323 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 324 | return (uint32_t)-1; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 325 | ret = set_swi_errno(ts, remove(s)); |
| 326 | unlock_user(s, ARG(0), 0); |
| 327 | } |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 328 | return ret; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 329 | case SYS_RENAME: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 330 | if (use_gdb_syscalls()) { |
| 331 | gdb_do_syscall(arm_semi_cb, "rename,%s,%s", |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 332 | ARG(0), (int)ARG(1)+1, ARG(2), (int)ARG(3)+1); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 333 | return env->regs[0]; |
| 334 | } else { |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 335 | char *s2; |
| 336 | s = lock_user_string(ARG(0)); |
| 337 | s2 = lock_user_string(ARG(2)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 338 | if (!s || !s2) |
| 339 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 340 | ret = (uint32_t)-1; |
| 341 | else |
| 342 | ret = set_swi_errno(ts, rename(s, s2)); |
| 343 | if (s2) |
| 344 | unlock_user(s2, ARG(2), 0); |
| 345 | if (s) |
| 346 | unlock_user(s, ARG(0), 0); |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 347 | return ret; |
| 348 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 349 | case SYS_CLOCK: |
| 350 | return clock() / (CLOCKS_PER_SEC / 100); |
| 351 | case SYS_TIME: |
| 352 | return set_swi_errno(ts, time(NULL)); |
| 353 | case SYS_SYSTEM: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 354 | if (use_gdb_syscalls()) { |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 355 | 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] | 356 | return env->regs[0]; |
| 357 | } else { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 358 | if (!(s = lock_user_string(ARG(0)))) |
| 359 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 360 | return (uint32_t)-1; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 361 | ret = set_swi_errno(ts, system(s)); |
| 362 | unlock_user(s, ARG(0), 0); |
ths | a982b53 | 2008-07-01 16:40:04 +0000 | [diff] [blame] | 363 | return ret; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 364 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 365 | case SYS_ERRNO: |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 366 | #ifdef CONFIG_USER_ONLY |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 367 | return ts->swi_errno; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 368 | #else |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 369 | return syscall_err; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 370 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 371 | case SYS_GET_CMDLINE: |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 372 | #ifdef CONFIG_USER_ONLY |
pbrook | 38d0662 | 2006-11-19 20:29:35 +0000 | [diff] [blame] | 373 | /* Build a commandline from the original argv. */ |
| 374 | { |
Wolfgang Schildbach | 2e8785a | 2010-12-06 15:06:05 +0000 | [diff] [blame] | 375 | char *arm_cmdline_buffer; |
| 376 | const char *host_cmdline_buffer; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 377 | |
Wolfgang Schildbach | 2e8785a | 2010-12-06 15:06:05 +0000 | [diff] [blame] | 378 | unsigned int i; |
| 379 | unsigned int arm_cmdline_len = ARG(1); |
| 380 | unsigned int host_cmdline_len = |
| 381 | ts->info->arg_end-ts->info->arg_start; |
pbrook | 38d0662 | 2006-11-19 20:29:35 +0000 | [diff] [blame] | 382 | |
Wolfgang Schildbach | 2e8785a | 2010-12-06 15:06:05 +0000 | [diff] [blame] | 383 | if (!arm_cmdline_len || host_cmdline_len > arm_cmdline_len) { |
| 384 | return -1; /* not enough space to store command line */ |
pbrook | 38d0662 | 2006-11-19 20:29:35 +0000 | [diff] [blame] | 385 | } |
pbrook | 38d0662 | 2006-11-19 20:29:35 +0000 | [diff] [blame] | 386 | |
Wolfgang Schildbach | 2e8785a | 2010-12-06 15:06:05 +0000 | [diff] [blame] | 387 | if (!host_cmdline_len) { |
| 388 | /* We special-case the "empty command line" case (argc==0). |
| 389 | Just provide the terminating 0. */ |
| 390 | arm_cmdline_buffer = lock_user(VERIFY_WRITE, ARG(0), 1, 0); |
| 391 | arm_cmdline_buffer[0] = 0; |
| 392 | unlock_user(arm_cmdline_buffer, ARG(0), 1); |
pbrook | 38d0662 | 2006-11-19 20:29:35 +0000 | [diff] [blame] | 393 | |
Wolfgang Schildbach | 2e8785a | 2010-12-06 15:06:05 +0000 | [diff] [blame] | 394 | /* Adjust the commandline length argument. */ |
| 395 | SET_ARG(1, 0); |
| 396 | return 0; |
| 397 | } |
pbrook | 38d0662 | 2006-11-19 20:29:35 +0000 | [diff] [blame] | 398 | |
Wolfgang Schildbach | 2e8785a | 2010-12-06 15:06:05 +0000 | [diff] [blame] | 399 | /* lock the buffers on the ARM side */ |
| 400 | arm_cmdline_buffer = |
| 401 | lock_user(VERIFY_WRITE, ARG(0), host_cmdline_len, 0); |
| 402 | host_cmdline_buffer = |
| 403 | lock_user(VERIFY_READ, ts->info->arg_start, |
| 404 | host_cmdline_len, 1); |
| 405 | |
| 406 | if (arm_cmdline_buffer && host_cmdline_buffer) |
| 407 | { |
| 408 | /* the last argument is zero-terminated; |
| 409 | no need for additional termination */ |
| 410 | memcpy(arm_cmdline_buffer, host_cmdline_buffer, |
| 411 | host_cmdline_len); |
| 412 | |
| 413 | /* separate arguments by white spaces */ |
| 414 | for (i = 0; i < host_cmdline_len-1; i++) { |
| 415 | if (arm_cmdline_buffer[i] == 0) { |
| 416 | arm_cmdline_buffer[i] = ' '; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /* Adjust the commandline length argument. */ |
| 421 | SET_ARG(1, host_cmdline_len-1); |
| 422 | } |
| 423 | |
| 424 | /* Unlock the buffers on the ARM side. */ |
| 425 | unlock_user(arm_cmdline_buffer, ARG(0), host_cmdline_len); |
| 426 | unlock_user((void*)host_cmdline_buffer, ts->info->arg_start, 0); |
| 427 | |
| 428 | /* Return success if we could return a commandline. */ |
| 429 | return (arm_cmdline_buffer && host_cmdline_buffer) ? 0 : -1; |
pbrook | 38d0662 | 2006-11-19 20:29:35 +0000 | [diff] [blame] | 430 | } |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 431 | #else |
Wolfgang Schildbach | 2e8785a | 2010-12-06 15:06:05 +0000 | [diff] [blame] | 432 | return -1; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 433 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 434 | case SYS_HEAPINFO: |
| 435 | { |
| 436 | uint32_t *ptr; |
| 437 | uint32_t limit; |
| 438 | |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 439 | #ifdef CONFIG_USER_ONLY |
| 440 | /* Some C libraries assume the heap immediately follows .bss, so |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 441 | allocate it using sbrk. */ |
| 442 | if (!ts->heap_limit) { |
| 443 | long ret; |
| 444 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 445 | ts->heap_base = do_brk(0); |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 446 | limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE; |
| 447 | /* Try a big heap, and reduce the size if that fails. */ |
| 448 | for (;;) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 449 | ret = do_brk(limit); |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 450 | if (ret != -1) |
| 451 | break; |
| 452 | limit = (ts->heap_base >> 1) + (limit >> 1); |
| 453 | } |
| 454 | ts->heap_limit = limit; |
| 455 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 456 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 457 | if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0))) |
| 458 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 459 | return (uint32_t)-1; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 460 | ptr[0] = tswap32(ts->heap_base); |
| 461 | ptr[1] = tswap32(ts->heap_limit); |
| 462 | ptr[2] = tswap32(ts->stack_base); |
| 463 | ptr[3] = tswap32(0); /* Stack limit. */ |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 464 | unlock_user(ptr, ARG(0), 16); |
| 465 | #else |
| 466 | limit = ram_size; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 467 | if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0))) |
| 468 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 469 | return (uint32_t)-1; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 470 | /* TODO: Make this use the limit of the loaded application. */ |
| 471 | ptr[0] = tswap32(limit / 2); |
| 472 | ptr[1] = tswap32(limit); |
| 473 | ptr[2] = tswap32(limit); /* Stack base */ |
| 474 | ptr[3] = tswap32(0); /* Stack limit. */ |
| 475 | unlock_user(ptr, ARG(0), 16); |
| 476 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | case SYS_EXIT: |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 480 | gdb_exit(env, 0); |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 481 | exit(0); |
| 482 | default: |
| 483 | fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr); |
| 484 | cpu_dump_state(env, stderr, fprintf, 0); |
| 485 | abort(); |
| 486 | } |
| 487 | } |