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" |
Cédric VINCENT | 1c1b40c | 2011-06-29 12:49:41 +0200 | [diff] [blame] | 37 | #include "hw/arm-misc.h" |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 38 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 39 | |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 40 | #define TARGET_SYS_OPEN 0x01 |
| 41 | #define TARGET_SYS_CLOSE 0x02 |
| 42 | #define TARGET_SYS_WRITEC 0x03 |
| 43 | #define TARGET_SYS_WRITE0 0x04 |
| 44 | #define TARGET_SYS_WRITE 0x05 |
| 45 | #define TARGET_SYS_READ 0x06 |
| 46 | #define TARGET_SYS_READC 0x07 |
| 47 | #define TARGET_SYS_ISTTY 0x09 |
| 48 | #define TARGET_SYS_SEEK 0x0a |
| 49 | #define TARGET_SYS_FLEN 0x0c |
| 50 | #define TARGET_SYS_TMPNAM 0x0d |
| 51 | #define TARGET_SYS_REMOVE 0x0e |
| 52 | #define TARGET_SYS_RENAME 0x0f |
| 53 | #define TARGET_SYS_CLOCK 0x10 |
| 54 | #define TARGET_SYS_TIME 0x11 |
| 55 | #define TARGET_SYS_SYSTEM 0x12 |
| 56 | #define TARGET_SYS_ERRNO 0x13 |
| 57 | #define TARGET_SYS_GET_CMDLINE 0x15 |
| 58 | #define TARGET_SYS_HEAPINFO 0x16 |
| 59 | #define TARGET_SYS_EXIT 0x18 |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 60 | |
| 61 | #ifndef O_BINARY |
| 62 | #define O_BINARY 0 |
| 63 | #endif |
| 64 | |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 65 | #define GDB_O_RDONLY 0x000 |
| 66 | #define GDB_O_WRONLY 0x001 |
| 67 | #define GDB_O_RDWR 0x002 |
| 68 | #define GDB_O_APPEND 0x008 |
| 69 | #define GDB_O_CREAT 0x200 |
| 70 | #define GDB_O_TRUNC 0x400 |
| 71 | #define GDB_O_BINARY 0 |
| 72 | |
| 73 | static int gdb_open_modeflags[12] = { |
| 74 | GDB_O_RDONLY, |
| 75 | GDB_O_RDONLY | GDB_O_BINARY, |
| 76 | GDB_O_RDWR, |
| 77 | GDB_O_RDWR | GDB_O_BINARY, |
| 78 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC, |
| 79 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY, |
| 80 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC, |
| 81 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY, |
| 82 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND, |
| 83 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY, |
| 84 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND, |
| 85 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY |
| 86 | }; |
| 87 | |
| 88 | static int open_modeflags[12] = { |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 89 | O_RDONLY, |
| 90 | O_RDONLY | O_BINARY, |
| 91 | O_RDWR, |
| 92 | O_RDWR | O_BINARY, |
| 93 | O_WRONLY | O_CREAT | O_TRUNC, |
| 94 | O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
| 95 | O_RDWR | O_CREAT | O_TRUNC, |
| 96 | O_RDWR | O_CREAT | O_TRUNC | O_BINARY, |
| 97 | O_WRONLY | O_CREAT | O_APPEND, |
| 98 | O_WRONLY | O_CREAT | O_APPEND | O_BINARY, |
| 99 | O_RDWR | O_CREAT | O_APPEND, |
| 100 | O_RDWR | O_CREAT | O_APPEND | O_BINARY |
| 101 | }; |
| 102 | |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 103 | #ifdef CONFIG_USER_ONLY |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 104 | static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code) |
| 105 | { |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 106 | if (code == (uint32_t)-1) |
| 107 | ts->swi_errno = errno; |
| 108 | return code; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 109 | } |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 110 | #else |
Andreas Färber | 81926f4 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 111 | static inline uint32_t set_swi_errno(CPUARMState *env, uint32_t code) |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 112 | { |
| 113 | return code; |
| 114 | } |
| 115 | |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 116 | #include "softmmu-semi.h" |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 117 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 118 | |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 119 | static target_ulong arm_semi_syscall_len; |
| 120 | |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 121 | #if !defined(CONFIG_USER_ONLY) |
| 122 | static target_ulong syscall_err; |
| 123 | #endif |
| 124 | |
Andreas Färber | 81926f4 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 125 | static void arm_semi_cb(CPUARMState *env, target_ulong ret, target_ulong err) |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 126 | { |
| 127 | #ifdef CONFIG_USER_ONLY |
| 128 | TaskState *ts = env->opaque; |
| 129 | #endif |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 130 | |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 131 | if (ret == (target_ulong)-1) { |
| 132 | #ifdef CONFIG_USER_ONLY |
| 133 | ts->swi_errno = err; |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 134 | #else |
| 135 | syscall_err = err; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 136 | #endif |
| 137 | env->regs[0] = ret; |
| 138 | } else { |
| 139 | /* Fixup syscalls that use nonstardard return conventions. */ |
| 140 | switch (env->regs[0]) { |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 141 | case TARGET_SYS_WRITE: |
| 142 | case TARGET_SYS_READ: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 143 | env->regs[0] = arm_semi_syscall_len - ret; |
| 144 | break; |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 145 | case TARGET_SYS_SEEK: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 146 | env->regs[0] = 0; |
| 147 | break; |
| 148 | default: |
| 149 | env->regs[0] = ret; |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Andreas Färber | 81926f4 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 155 | static void arm_semi_flen_cb(CPUARMState *env, target_ulong ret, target_ulong err) |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 156 | { |
| 157 | /* The size is always stored in big-endian order, extract |
| 158 | the value. We assume the size always fit in 32 bits. */ |
| 159 | uint32_t size; |
| 160 | cpu_memory_rw_debug(env, env->regs[13]-64+32, (uint8_t *)&size, 4, 0); |
| 161 | env->regs[0] = be32_to_cpu(size); |
| 162 | #ifdef CONFIG_USER_ONLY |
| 163 | ((TaskState *)env->opaque)->swi_errno = err; |
| 164 | #else |
| 165 | syscall_err = err; |
| 166 | #endif |
| 167 | } |
| 168 | |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 169 | #define ARG(n) \ |
| 170 | ({ \ |
| 171 | target_ulong __arg; \ |
| 172 | /* FIXME - handle get_user() failure */ \ |
| 173 | get_user_ual(__arg, args + (n) * 4); \ |
| 174 | __arg; \ |
| 175 | }) |
| 176 | #define SET_ARG(n, val) put_user_ual(val, args + (n) * 4) |
Andreas Färber | 81926f4 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 177 | uint32_t do_arm_semihosting(CPUARMState *env) |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 178 | { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 179 | target_ulong args; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 180 | char * s; |
| 181 | int nr; |
| 182 | uint32_t ret; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 183 | uint32_t len; |
| 184 | #ifdef CONFIG_USER_ONLY |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 185 | TaskState *ts = env->opaque; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 186 | #else |
Andreas Färber | 81926f4 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 187 | CPUARMState *ts = env; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 188 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 189 | |
| 190 | nr = env->regs[0]; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 191 | args = env->regs[1]; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 192 | switch (nr) { |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 193 | case TARGET_SYS_OPEN: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 194 | if (!(s = lock_user_string(ARG(0)))) |
| 195 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 196 | return (uint32_t)-1; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 197 | if (ARG(1) >= 12) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 198 | return (uint32_t)-1; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 199 | if (strcmp(s, ":tt") == 0) { |
| 200 | if (ARG(1) < 4) |
| 201 | return STDIN_FILENO; |
| 202 | else |
| 203 | return STDOUT_FILENO; |
| 204 | } |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 205 | if (use_gdb_syscalls()) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 206 | gdb_do_syscall(arm_semi_cb, "open,%s,%x,1a4", ARG(0), |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 207 | (int)ARG(2)+1, gdb_open_modeflags[ARG(1)]); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 208 | return env->regs[0]; |
| 209 | } else { |
| 210 | ret = set_swi_errno(ts, open(s, open_modeflags[ARG(1)], 0644)); |
| 211 | } |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 212 | unlock_user(s, ARG(0), 0); |
| 213 | return ret; |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 214 | case TARGET_SYS_CLOSE: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 215 | if (use_gdb_syscalls()) { |
| 216 | gdb_do_syscall(arm_semi_cb, "close,%x", ARG(0)); |
| 217 | return env->regs[0]; |
| 218 | } else { |
| 219 | return set_swi_errno(ts, close(ARG(0))); |
| 220 | } |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 221 | case TARGET_SYS_WRITEC: |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 222 | { |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 223 | char c; |
| 224 | |
| 225 | if (get_user_u8(c, args)) |
| 226 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 227 | return (uint32_t)-1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 228 | /* Write to debug console. stderr is near enough. */ |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 229 | if (use_gdb_syscalls()) { |
| 230 | gdb_do_syscall(arm_semi_cb, "write,2,%x,1", args); |
| 231 | return env->regs[0]; |
| 232 | } else { |
| 233 | return write(STDERR_FILENO, &c, 1); |
| 234 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 235 | } |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 236 | case TARGET_SYS_WRITE0: |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 237 | if (!(s = lock_user_string(args))) |
| 238 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 239 | return (uint32_t)-1; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 240 | len = strlen(s); |
| 241 | if (use_gdb_syscalls()) { |
| 242 | gdb_do_syscall(arm_semi_cb, "write,2,%x,%x\n", args, len); |
| 243 | ret = env->regs[0]; |
| 244 | } else { |
| 245 | ret = write(STDERR_FILENO, s, len); |
| 246 | } |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 247 | unlock_user(s, args, 0); |
| 248 | return ret; |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 249 | case TARGET_SYS_WRITE: |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 250 | len = ARG(2); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 251 | if (use_gdb_syscalls()) { |
| 252 | arm_semi_syscall_len = len; |
| 253 | gdb_do_syscall(arm_semi_cb, "write,%x,%x,%x", ARG(0), ARG(1), len); |
| 254 | return env->regs[0]; |
| 255 | } else { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 256 | if (!(s = lock_user(VERIFY_READ, ARG(1), len, 1))) |
| 257 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 258 | return (uint32_t)-1; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 259 | ret = set_swi_errno(ts, write(ARG(0), s, len)); |
| 260 | unlock_user(s, ARG(1), 0); |
| 261 | if (ret == (uint32_t)-1) |
| 262 | return -1; |
| 263 | return len - ret; |
| 264 | } |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 265 | case TARGET_SYS_READ: |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 266 | len = ARG(2); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 267 | if (use_gdb_syscalls()) { |
| 268 | arm_semi_syscall_len = len; |
| 269 | gdb_do_syscall(arm_semi_cb, "read,%x,%x,%x", ARG(0), ARG(1), len); |
| 270 | return env->regs[0]; |
| 271 | } else { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 272 | if (!(s = lock_user(VERIFY_WRITE, ARG(1), len, 0))) |
| 273 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 274 | return (uint32_t)-1; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 275 | do |
| 276 | ret = set_swi_errno(ts, read(ARG(0), s, len)); |
| 277 | while (ret == -1 && errno == EINTR); |
| 278 | unlock_user(s, ARG(1), len); |
| 279 | if (ret == (uint32_t)-1) |
| 280 | return -1; |
| 281 | return len - ret; |
| 282 | } |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 283 | case TARGET_SYS_READC: |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 284 | /* XXX: Read from debug cosole. Not implemented. */ |
| 285 | return 0; |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 286 | case TARGET_SYS_ISTTY: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 287 | if (use_gdb_syscalls()) { |
| 288 | gdb_do_syscall(arm_semi_cb, "isatty,%x", ARG(0)); |
| 289 | return env->regs[0]; |
| 290 | } else { |
| 291 | return isatty(ARG(0)); |
| 292 | } |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 293 | case TARGET_SYS_SEEK: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 294 | if (use_gdb_syscalls()) { |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 295 | 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] | 296 | return env->regs[0]; |
| 297 | } else { |
| 298 | ret = set_swi_errno(ts, lseek(ARG(0), ARG(1), SEEK_SET)); |
| 299 | if (ret == (uint32_t)-1) |
| 300 | return -1; |
| 301 | return 0; |
| 302 | } |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 303 | case TARGET_SYS_FLEN: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 304 | if (use_gdb_syscalls()) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 305 | gdb_do_syscall(arm_semi_flen_cb, "fstat,%x,%x", |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 306 | ARG(0), env->regs[13]-64); |
| 307 | return env->regs[0]; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 308 | } else { |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 309 | struct stat buf; |
| 310 | ret = set_swi_errno(ts, fstat(ARG(0), &buf)); |
| 311 | if (ret == (uint32_t)-1) |
| 312 | return -1; |
| 313 | return buf.st_size; |
| 314 | } |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 315 | case TARGET_SYS_TMPNAM: |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 316 | /* XXX: Not implemented. */ |
| 317 | return -1; |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 318 | case TARGET_SYS_REMOVE: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 319 | if (use_gdb_syscalls()) { |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 320 | 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] | 321 | ret = env->regs[0]; |
| 322 | } else { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 323 | if (!(s = lock_user_string(ARG(0)))) |
| 324 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 325 | return (uint32_t)-1; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 326 | ret = set_swi_errno(ts, remove(s)); |
| 327 | unlock_user(s, ARG(0), 0); |
| 328 | } |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 329 | return ret; |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 330 | case TARGET_SYS_RENAME: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 331 | if (use_gdb_syscalls()) { |
| 332 | gdb_do_syscall(arm_semi_cb, "rename,%s,%s", |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 333 | ARG(0), (int)ARG(1)+1, ARG(2), (int)ARG(3)+1); |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 334 | return env->regs[0]; |
| 335 | } else { |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 336 | char *s2; |
| 337 | s = lock_user_string(ARG(0)); |
| 338 | s2 = lock_user_string(ARG(2)); |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 339 | if (!s || !s2) |
| 340 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 341 | ret = (uint32_t)-1; |
| 342 | else |
| 343 | ret = set_swi_errno(ts, rename(s, s2)); |
| 344 | if (s2) |
| 345 | unlock_user(s2, ARG(2), 0); |
| 346 | if (s) |
| 347 | unlock_user(s, ARG(0), 0); |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 348 | return ret; |
| 349 | } |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 350 | case TARGET_SYS_CLOCK: |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 351 | return clock() / (CLOCKS_PER_SEC / 100); |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 352 | case TARGET_SYS_TIME: |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 353 | return set_swi_errno(ts, time(NULL)); |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 354 | case TARGET_SYS_SYSTEM: |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 355 | if (use_gdb_syscalls()) { |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 356 | 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] | 357 | return env->regs[0]; |
| 358 | } else { |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 359 | if (!(s = lock_user_string(ARG(0)))) |
| 360 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 361 | return (uint32_t)-1; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 362 | ret = set_swi_errno(ts, system(s)); |
| 363 | unlock_user(s, ARG(0), 0); |
ths | a982b53 | 2008-07-01 16:40:04 +0000 | [diff] [blame] | 364 | return ret; |
pbrook | a2d1eba | 2007-01-28 03:10:55 +0000 | [diff] [blame] | 365 | } |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 366 | case TARGET_SYS_ERRNO: |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 367 | #ifdef CONFIG_USER_ONLY |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 368 | return ts->swi_errno; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 369 | #else |
pbrook | 33d9cc8 | 2007-06-09 14:44:00 +0000 | [diff] [blame] | 370 | return syscall_err; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 371 | #endif |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 372 | case TARGET_SYS_GET_CMDLINE: |
pbrook | 38d0662 | 2006-11-19 20:29:35 +0000 | [diff] [blame] | 373 | { |
Cédric VINCENT | 1c1b40c | 2011-06-29 12:49:41 +0200 | [diff] [blame] | 374 | /* Build a command-line from the original argv. |
| 375 | * |
| 376 | * The inputs are: |
| 377 | * * ARG(0), pointer to a buffer of at least the size |
| 378 | * specified in ARG(1). |
| 379 | * * ARG(1), size of the buffer pointed to by ARG(0) in |
| 380 | * bytes. |
| 381 | * |
| 382 | * The outputs are: |
| 383 | * * ARG(0), pointer to null-terminated string of the |
| 384 | * command line. |
| 385 | * * ARG(1), length of the string pointed to by ARG(0). |
| 386 | */ |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 387 | |
Cédric VINCENT | 1c1b40c | 2011-06-29 12:49:41 +0200 | [diff] [blame] | 388 | char *output_buffer; |
| 389 | size_t input_size = ARG(1); |
| 390 | size_t output_size; |
| 391 | int status = 0; |
| 392 | |
| 393 | /* Compute the size of the output string. */ |
| 394 | #if !defined(CONFIG_USER_ONLY) |
| 395 | output_size = strlen(ts->boot_info->kernel_filename) |
| 396 | + 1 /* Separating space. */ |
| 397 | + strlen(ts->boot_info->kernel_cmdline) |
| 398 | + 1; /* Terminating null byte. */ |
| 399 | #else |
Wolfgang Schildbach | 2e8785a | 2010-12-06 15:06:05 +0000 | [diff] [blame] | 400 | unsigned int i; |
pbrook | 38d0662 | 2006-11-19 20:29:35 +0000 | [diff] [blame] | 401 | |
Cédric VINCENT | 1c1b40c | 2011-06-29 12:49:41 +0200 | [diff] [blame] | 402 | output_size = ts->info->arg_end - ts->info->arg_start; |
| 403 | if (!output_size) { |
Wolfgang Schildbach | 2e8785a | 2010-12-06 15:06:05 +0000 | [diff] [blame] | 404 | /* We special-case the "empty command line" case (argc==0). |
| 405 | Just provide the terminating 0. */ |
Cédric VINCENT | 1c1b40c | 2011-06-29 12:49:41 +0200 | [diff] [blame] | 406 | output_size = 1; |
Wolfgang Schildbach | 2e8785a | 2010-12-06 15:06:05 +0000 | [diff] [blame] | 407 | } |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 408 | #endif |
Cédric VINCENT | 1c1b40c | 2011-06-29 12:49:41 +0200 | [diff] [blame] | 409 | |
| 410 | if (output_size > input_size) { |
| 411 | /* Not enough space to store command-line arguments. */ |
| 412 | return -1; |
| 413 | } |
| 414 | |
| 415 | /* Adjust the command-line length. */ |
| 416 | SET_ARG(1, output_size - 1); |
| 417 | |
| 418 | /* Lock the buffer on the ARM side. */ |
| 419 | output_buffer = lock_user(VERIFY_WRITE, ARG(0), output_size, 0); |
| 420 | if (!output_buffer) { |
| 421 | return -1; |
| 422 | } |
| 423 | |
| 424 | /* Copy the command-line arguments. */ |
| 425 | #if !defined(CONFIG_USER_ONLY) |
| 426 | pstrcpy(output_buffer, output_size, ts->boot_info->kernel_filename); |
| 427 | pstrcat(output_buffer, output_size, " "); |
| 428 | pstrcat(output_buffer, output_size, ts->boot_info->kernel_cmdline); |
| 429 | #else |
| 430 | if (output_size == 1) { |
| 431 | /* Empty command-line. */ |
| 432 | output_buffer[0] = '\0'; |
| 433 | goto out; |
| 434 | } |
| 435 | |
| 436 | if (copy_from_user(output_buffer, ts->info->arg_start, |
| 437 | output_size)) { |
| 438 | status = -1; |
| 439 | goto out; |
| 440 | } |
| 441 | |
| 442 | /* Separate arguments by white spaces. */ |
| 443 | for (i = 0; i < output_size - 1; i++) { |
| 444 | if (output_buffer[i] == 0) { |
| 445 | output_buffer[i] = ' '; |
| 446 | } |
| 447 | } |
| 448 | out: |
| 449 | #endif |
| 450 | /* Unlock the buffer on the ARM side. */ |
| 451 | unlock_user(output_buffer, ARG(0), output_size); |
| 452 | |
| 453 | return status; |
| 454 | } |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 455 | case TARGET_SYS_HEAPINFO: |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 456 | { |
| 457 | uint32_t *ptr; |
| 458 | uint32_t limit; |
| 459 | |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 460 | #ifdef CONFIG_USER_ONLY |
| 461 | /* Some C libraries assume the heap immediately follows .bss, so |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 462 | allocate it using sbrk. */ |
| 463 | if (!ts->heap_limit) { |
Peter Maydell | 206ae74 | 2011-04-18 16:34:25 +0100 | [diff] [blame] | 464 | abi_ulong ret; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 465 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 466 | ts->heap_base = do_brk(0); |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 467 | limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE; |
| 468 | /* Try a big heap, and reduce the size if that fails. */ |
| 469 | for (;;) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 470 | ret = do_brk(limit); |
Peter Maydell | 206ae74 | 2011-04-18 16:34:25 +0100 | [diff] [blame] | 471 | if (ret >= limit) { |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 472 | break; |
Peter Maydell | 206ae74 | 2011-04-18 16:34:25 +0100 | [diff] [blame] | 473 | } |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 474 | limit = (ts->heap_base >> 1) + (limit >> 1); |
| 475 | } |
| 476 | ts->heap_limit = limit; |
| 477 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 478 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 479 | if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0))) |
| 480 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 481 | return (uint32_t)-1; |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 482 | ptr[0] = tswap32(ts->heap_base); |
| 483 | ptr[1] = tswap32(ts->heap_limit); |
| 484 | ptr[2] = tswap32(ts->stack_base); |
| 485 | ptr[3] = tswap32(0); /* Stack limit. */ |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 486 | unlock_user(ptr, ARG(0), 16); |
| 487 | #else |
| 488 | limit = ram_size; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 489 | if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0))) |
| 490 | /* FIXME - should this error code be -TARGET_EFAULT ? */ |
| 491 | return (uint32_t)-1; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 492 | /* TODO: Make this use the limit of the loaded application. */ |
| 493 | ptr[0] = tswap32(limit / 2); |
| 494 | ptr[1] = tswap32(limit); |
| 495 | ptr[2] = tswap32(limit); /* Stack base */ |
| 496 | ptr[3] = tswap32(0); /* Stack limit. */ |
| 497 | unlock_user(ptr, ARG(0), 16); |
| 498 | #endif |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 499 | return 0; |
| 500 | } |
Stefan Weil | 3881725 | 2012-04-28 05:07:47 +0000 | [diff] [blame^] | 501 | case TARGET_SYS_EXIT: |
Paul Brook | 0e1c9c5 | 2010-06-16 13:03:51 +0100 | [diff] [blame] | 502 | gdb_exit(env, 0); |
bellard | a4f8197 | 2005-04-23 18:25:41 +0000 | [diff] [blame] | 503 | exit(0); |
| 504 | default: |
| 505 | fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr); |
| 506 | cpu_dump_state(env, stderr, fprintf, 0); |
| 507 | abort(); |
| 508 | } |
| 509 | } |