| 1 | /* |
| 2 | * Semihosting support for systems modeled on the Arm "Angel" |
| 3 | * semihosting syscalls design. This includes Arm and RISC-V processors |
| 4 | * |
| 5 | * Copyright (c) 2005, 2007 CodeSourcery. |
| 6 | * Copyright (c) 2019 Linaro |
| 7 | * Written by Paul Brook. |
| 8 | * |
| 9 | * Copyright © 2020 by Keith Packard <keithp@keithp.com> |
| 10 | * Adapted for systems other than ARM, including RISC-V, by Keith Packard |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * ARM Semihosting is documented in: |
| 26 | * Semihosting for AArch32 and AArch64 Release 2.0 |
| 27 | * https://github.com/ARM-software/abi-aa/blob/main/semihosting/semihosting.rst |
| 28 | * |
| 29 | * RISC-V Semihosting is documented in: |
| 30 | * RISC-V Semihosting |
| 31 | * https://github.com/riscv/riscv-semihosting-spec/blob/main/riscv-semihosting-spec.adoc |
| 32 | */ |
| 33 | |
| 34 | #include "qemu/osdep.h" |
| 35 | #include "qemu/timer.h" |
| 36 | #include "exec/gdbstub.h" |
| 37 | #include "gdbstub/syscalls.h" |
| 38 | #include "semihosting/semihost.h" |
| 39 | #include "semihosting/console.h" |
| 40 | #include "semihosting/common-semi.h" |
| 41 | #include "semihosting/guestfd.h" |
| 42 | #include "semihosting/syscalls.h" |
| 43 | |
| 44 | #ifdef CONFIG_USER_ONLY |
| 45 | #include "qemu.h" |
| 46 | |
| 47 | #define COMMON_SEMI_HEAP_SIZE (128 * 1024 * 1024) |
| 48 | #else |
| 49 | #include "qemu/cutils.h" |
| 50 | #include "hw/core/loader.h" |
| 51 | #include "hw/core/boards.h" |
| 52 | #endif |
| 53 | |
| 54 | #define TARGET_SYS_OPEN 0x01 |
| 55 | #define TARGET_SYS_CLOSE 0x02 |
| 56 | #define TARGET_SYS_WRITEC 0x03 |
| 57 | #define TARGET_SYS_WRITE0 0x04 |
| 58 | #define TARGET_SYS_WRITE 0x05 |
| 59 | #define TARGET_SYS_READ 0x06 |
| 60 | #define TARGET_SYS_READC 0x07 |
| 61 | #define TARGET_SYS_ISERROR 0x08 |
| 62 | #define TARGET_SYS_ISTTY 0x09 |
| 63 | #define TARGET_SYS_SEEK 0x0a |
| 64 | #define TARGET_SYS_FLEN 0x0c |
| 65 | #define TARGET_SYS_TMPNAM 0x0d |
| 66 | #define TARGET_SYS_REMOVE 0x0e |
| 67 | #define TARGET_SYS_RENAME 0x0f |
| 68 | #define TARGET_SYS_CLOCK 0x10 |
| 69 | #define TARGET_SYS_TIME 0x11 |
| 70 | #define TARGET_SYS_SYSTEM 0x12 |
| 71 | #define TARGET_SYS_ERRNO 0x13 |
| 72 | #define TARGET_SYS_GET_CMDLINE 0x15 |
| 73 | #define TARGET_SYS_HEAPINFO 0x16 |
| 74 | #define TARGET_SYS_EXIT 0x18 |
| 75 | #define TARGET_SYS_SYNCCACHE 0x19 |
| 76 | #define TARGET_SYS_EXIT_EXTENDED 0x20 |
| 77 | #define TARGET_SYS_ELAPSED 0x30 |
| 78 | #define TARGET_SYS_TICKFREQ 0x31 |
| 79 | |
| 80 | /* ADP_Stopped_ApplicationExit is used for exit(0), |
| 81 | * anything else is implemented as exit(1) */ |
| 82 | #define ADP_Stopped_ApplicationExit (0x20026) |
| 83 | |
| 84 | #ifndef O_BINARY |
| 85 | #define O_BINARY 0 |
| 86 | #endif |
| 87 | |
| 88 | static int gdb_open_modeflags[12] = { |
| 89 | GDB_O_RDONLY, |
| 90 | GDB_O_RDONLY, |
| 91 | GDB_O_RDWR, |
| 92 | GDB_O_RDWR, |
| 93 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC, |
| 94 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC, |
| 95 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC, |
| 96 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC, |
| 97 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND, |
| 98 | GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND, |
| 99 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND, |
| 100 | GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND, |
| 101 | }; |
| 102 | |
| 103 | /* |
| 104 | * For ARM semihosting, we have a separate structure for routing |
| 105 | * data for the console which is outside the guest fd address space. |
| 106 | */ |
| 107 | static GuestFD console_in_gf; |
| 108 | static GuestFD console_out_gf; |
| 109 | |
| 110 | #ifndef CONFIG_USER_ONLY |
| 111 | |
| 112 | /** |
| 113 | * common_semi_find_bases: find information about ram and heap base |
| 114 | * |
| 115 | * This function attempts to provide meaningful numbers for RAM and |
| 116 | * HEAP base addresses. The rambase is simply the lowest addressable |
| 117 | * RAM position. For the heapbase we ask the loader to scan the |
| 118 | * address space and the largest available gap by querying the "ROM" |
| 119 | * regions. |
| 120 | * |
| 121 | * Returns: a structure with the numbers we need. |
| 122 | */ |
| 123 | |
| 124 | typedef struct LayoutInfo { |
| 125 | vaddr rambase; |
| 126 | size_t ramsize; |
| 127 | hwaddr heapbase; |
| 128 | hwaddr heaplimit; |
| 129 | } LayoutInfo; |
| 130 | |
| 131 | static bool find_ram_cb(Int128 start, Int128 len, const MemoryRegion *mr, |
| 132 | hwaddr offset_in_region, void *opaque) |
| 133 | { |
| 134 | LayoutInfo *info = (LayoutInfo *) opaque; |
| 135 | uint64_t size = int128_get64(len); |
| 136 | |
| 137 | if (!mr->ram || mr->readonly) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | if (size > info->ramsize) { |
| 142 | info->rambase = int128_get64(start); |
| 143 | info->ramsize = size; |
| 144 | } |
| 145 | |
| 146 | /* search exhaustively for largest RAM */ |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | static LayoutInfo common_semi_find_bases(CPUState *cs) |
| 151 | { |
| 152 | FlatView *fv; |
| 153 | LayoutInfo info = { 0, 0, 0, 0 }; |
| 154 | |
| 155 | RCU_READ_LOCK_GUARD(); |
| 156 | |
| 157 | fv = address_space_to_flatview(cs->as); |
| 158 | flatview_for_each_range(fv, find_ram_cb, &info); |
| 159 | |
| 160 | /* |
| 161 | * If we have found the RAM lets iterate through the ROM blobs to |
| 162 | * work out the best place for the remainder of RAM and split it |
| 163 | * equally between stack and heap. |
| 164 | */ |
| 165 | if (info.rambase || info.ramsize > 0) { |
| 166 | RomGap gap = rom_find_largest_gap_between(info.rambase, info.ramsize); |
| 167 | info.heapbase = gap.base; |
| 168 | info.heaplimit = gap.base + gap.size; |
| 169 | } |
| 170 | |
| 171 | return info; |
| 172 | } |
| 173 | |
| 174 | #endif |
| 175 | |
| 176 | #include "semihosting/common-semi.h" |
| 177 | |
| 178 | /* |
| 179 | * Read the input value from the argument block; fail the semihosting |
| 180 | * call if the memory read fails. Eventually we could use a generic |
| 181 | * CPUState helper function here. |
| 182 | * Note that GET_ARG() handles memory access errors by jumping to |
| 183 | * do_fault, so must be used as the first thing done in handling a |
| 184 | * semihosting call, to avoid accidentally leaking allocated resources. |
| 185 | * SET_ARG(), since it unavoidably happens late, instead returns an |
| 186 | * error indication (0 on success, non-0 for error) which the caller |
| 187 | * should check. |
| 188 | */ |
| 189 | |
| 190 | #define GET_ARG(n) do { \ |
| 191 | if (is_64bit_semihosting(env)) { \ |
| 192 | if (get_user_u64(arg ## n, args + (n) * 8)) { \ |
| 193 | goto do_fault; \ |
| 194 | } \ |
| 195 | } else { \ |
| 196 | if (get_user_u32(arg ## n, args + (n) * 4)) { \ |
| 197 | goto do_fault; \ |
| 198 | } \ |
| 199 | } \ |
| 200 | } while (0) |
| 201 | |
| 202 | #define SET_ARG(n, val) \ |
| 203 | (is_64bit_semihosting(env) ? \ |
| 204 | put_user_u64(val, args + (n) * 8) : \ |
| 205 | put_user_u32(val, args + (n) * 4)) |
| 206 | |
| 207 | |
| 208 | /* |
| 209 | * The semihosting API has no concept of its errno being thread-safe, |
| 210 | * as the API design predates SMP CPUs and was intended as a simple |
| 211 | * real-hardware set of debug functionality. For QEMU, we make the |
| 212 | * errno be per-thread in linux-user mode; in system-mode it is a simple |
| 213 | * global, and we assume that the guest takes care of avoiding any races. |
| 214 | */ |
| 215 | #ifndef CONFIG_USER_ONLY |
| 216 | static uint64_t syscall_err; |
| 217 | |
| 218 | #include "semihosting/uaccess.h" |
| 219 | #endif |
| 220 | |
| 221 | static inline uint32_t get_swi_errno(CPUState *cs) |
| 222 | { |
| 223 | #ifdef CONFIG_USER_ONLY |
| 224 | TaskState *ts = get_task_state(cs); |
| 225 | |
| 226 | return ts->swi_errno; |
| 227 | #else |
| 228 | return syscall_err; |
| 229 | #endif |
| 230 | } |
| 231 | |
| 232 | static void common_semi_cb(CPUState *cs, uint64_t ret, int err) |
| 233 | { |
| 234 | if (err) { |
| 235 | #ifdef CONFIG_USER_ONLY |
| 236 | TaskState *ts = get_task_state(cs); |
| 237 | ts->swi_errno = err; |
| 238 | #else |
| 239 | syscall_err = err; |
| 240 | #endif |
| 241 | } |
| 242 | common_semi_set_ret(cs, ret); |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Use 0xdeadbeef as the return value when there isn't a defined |
| 247 | * return value for the call. |
| 248 | */ |
| 249 | static void common_semi_dead_cb(CPUState *cs, uint64_t ret, int err) |
| 250 | { |
| 251 | common_semi_set_ret(cs, 0xdeadbeef); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * SYS_READ and SYS_WRITE always return the number of bytes not read/written. |
| 256 | * There is no error condition, other than returning the original length. |
| 257 | */ |
| 258 | static void common_semi_rw_cb(CPUState *cs, uint64_t ret, int err) |
| 259 | { |
| 260 | /* Recover the original length from the third argument. */ |
| 261 | CPUArchState *env G_GNUC_UNUSED = cpu_env(cs); |
| 262 | uint64_t args = common_semi_arg(cs, 1); |
| 263 | uint64_t arg2; |
| 264 | GET_ARG(2); |
| 265 | |
| 266 | if (err) { |
| 267 | do_fault: |
| 268 | ret = 0; /* error: no bytes transmitted */ |
| 269 | } |
| 270 | common_semi_set_ret(cs, arg2 - ret); |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Convert from Posix ret+errno to Arm SYS_ISTTY return values. |
| 275 | * With gdbstub, err is only ever set for protocol errors to EIO. |
| 276 | */ |
| 277 | static void common_semi_istty_cb(CPUState *cs, uint64_t ret, int err) |
| 278 | { |
| 279 | if (err) { |
| 280 | ret = (err == ENOTTY ? 0 : -1); |
| 281 | } |
| 282 | common_semi_cb(cs, ret, err); |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * SYS_SEEK returns 0 on success, not the resulting offset. |
| 287 | */ |
| 288 | static void common_semi_seek_cb(CPUState *cs, uint64_t ret, int err) |
| 289 | { |
| 290 | if (!err) { |
| 291 | ret = 0; |
| 292 | } |
| 293 | common_semi_cb(cs, ret, err); |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Return an address in target memory of 64 bytes where the remote |
| 298 | * gdb should write its stat struct. (The format of this structure |
| 299 | * is defined by GDB's remote protocol and is not target-specific.) |
| 300 | * We put this on the guest's stack just below SP. |
| 301 | */ |
| 302 | static uint64_t common_semi_flen_buf(CPUState *cs) |
| 303 | { |
| 304 | vaddr sp = common_semi_stack_bottom(cs); |
| 305 | return sp - 64; |
| 306 | } |
| 307 | |
| 308 | static void |
| 309 | common_semi_flen_fstat_cb(CPUState *cs, uint64_t ret, int err) |
| 310 | { |
| 311 | if (!err) { |
| 312 | /* The size is always stored in big-endian order, extract the value. */ |
| 313 | uint64_t size; |
| 314 | if (cpu_memory_rw_debug(cs, common_semi_flen_buf(cs) + |
| 315 | offsetof(struct gdb_stat, gdb_st_size), |
| 316 | &size, 8, 0)) { |
| 317 | ret = -1, err = EFAULT; |
| 318 | } else { |
| 319 | ret = be64_to_cpu(size); |
| 320 | } |
| 321 | } |
| 322 | common_semi_cb(cs, ret, err); |
| 323 | } |
| 324 | |
| 325 | static void |
| 326 | common_semi_readc_cb(CPUState *cs, uint64_t ret, int err) |
| 327 | { |
| 328 | if (!err) { |
| 329 | CPUArchState *env G_GNUC_UNUSED = cpu_env(cs); |
| 330 | uint8_t ch; |
| 331 | |
| 332 | if (get_user_u8(ch, common_semi_stack_bottom(cs) - 1)) { |
| 333 | ret = -1, err = EFAULT; |
| 334 | } else { |
| 335 | ret = ch; |
| 336 | } |
| 337 | } |
| 338 | common_semi_cb(cs, ret, err); |
| 339 | } |
| 340 | |
| 341 | #define SHFB_MAGIC_0 0x53 |
| 342 | #define SHFB_MAGIC_1 0x48 |
| 343 | #define SHFB_MAGIC_2 0x46 |
| 344 | #define SHFB_MAGIC_3 0x42 |
| 345 | |
| 346 | /* Feature bits reportable in feature byte 0 */ |
| 347 | #define SH_EXT_EXIT_EXTENDED (1 << 0) |
| 348 | #define SH_EXT_STDOUT_STDERR (1 << 1) |
| 349 | |
| 350 | static const uint8_t featurefile_data[] = { |
| 351 | SHFB_MAGIC_0, |
| 352 | SHFB_MAGIC_1, |
| 353 | SHFB_MAGIC_2, |
| 354 | SHFB_MAGIC_3, |
| 355 | SH_EXT_EXIT_EXTENDED | SH_EXT_STDOUT_STDERR, /* Feature byte 0 */ |
| 356 | }; |
| 357 | |
| 358 | bool semihosting_arm_compatible(void) |
| 359 | { |
| 360 | return true; |
| 361 | } |
| 362 | |
| 363 | void semihosting_arm_compatible_init(void) |
| 364 | { |
| 365 | /* For ARM-compat, the console is in a separate namespace. */ |
| 366 | if (use_gdb_syscalls()) { |
| 367 | console_in_gf.type = GuestFDGDB; |
| 368 | console_in_gf.hostfd = 0; |
| 369 | console_out_gf.type = GuestFDGDB; |
| 370 | console_out_gf.hostfd = 2; |
| 371 | } else { |
| 372 | console_in_gf.type = GuestFDConsole; |
| 373 | console_out_gf.type = GuestFDConsole; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Do a semihosting call. |
| 379 | * |
| 380 | * The specification always says that the "return register" either |
| 381 | * returns a specific value or is corrupted, so we don't need to |
| 382 | * report to our caller whether we are returning a value or trying to |
| 383 | * leave the register unchanged. |
| 384 | */ |
| 385 | void do_common_semihosting(CPUState *cs) |
| 386 | { |
| 387 | CPUArchState *env = cpu_env(cs); |
| 388 | uint64_t args; |
| 389 | uint64_t arg0, arg1, arg2, arg3; |
| 390 | uint64_t ul_ret; |
| 391 | char * s; |
| 392 | int nr; |
| 393 | int64_t elapsed; |
| 394 | |
| 395 | nr = common_semi_arg(cs, 0) & 0xffffffffU; |
| 396 | args = common_semi_arg(cs, 1); |
| 397 | |
| 398 | switch (nr) { |
| 399 | case TARGET_SYS_OPEN: |
| 400 | { |
| 401 | int ret, err = 0; |
| 402 | int hostfd; |
| 403 | |
| 404 | GET_ARG(0); |
| 405 | GET_ARG(1); |
| 406 | GET_ARG(2); |
| 407 | s = lock_user_string(arg0); |
| 408 | if (!s) { |
| 409 | goto do_fault; |
| 410 | } |
| 411 | if (arg1 >= 12) { |
| 412 | unlock_user(s, arg0, 0); |
| 413 | common_semi_cb(cs, -1, EINVAL); |
| 414 | break; |
| 415 | } |
| 416 | |
| 417 | if (strcmp(s, ":tt") == 0) { |
| 418 | /* |
| 419 | * We implement SH_EXT_STDOUT_STDERR, so: |
| 420 | * open for read == stdin |
| 421 | * open for write == stdout |
| 422 | * open for append == stderr |
| 423 | */ |
| 424 | if (arg1 < 4) { |
| 425 | hostfd = STDIN_FILENO; |
| 426 | } else if (arg1 < 8) { |
| 427 | hostfd = STDOUT_FILENO; |
| 428 | } else { |
| 429 | hostfd = STDERR_FILENO; |
| 430 | } |
| 431 | ret = alloc_guestfd(); |
| 432 | associate_guestfd(ret, hostfd); |
| 433 | } else if (strcmp(s, ":semihosting-features") == 0) { |
| 434 | /* We must fail opens for modes other than 0 ('r') or 1 ('rb') */ |
| 435 | if (arg1 != 0 && arg1 != 1) { |
| 436 | ret = -1; |
| 437 | err = EACCES; |
| 438 | } else { |
| 439 | ret = alloc_guestfd(); |
| 440 | staticfile_guestfd(ret, featurefile_data, |
| 441 | sizeof(featurefile_data)); |
| 442 | } |
| 443 | } else { |
| 444 | unlock_user(s, arg0, 0); |
| 445 | semihost_sys_open(cs, common_semi_cb, arg0, arg2 + 1, |
| 446 | gdb_open_modeflags[arg1], 0644); |
| 447 | break; |
| 448 | } |
| 449 | unlock_user(s, arg0, 0); |
| 450 | common_semi_cb(cs, ret, err); |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | case TARGET_SYS_CLOSE: |
| 455 | GET_ARG(0); |
| 456 | semihost_sys_close(cs, common_semi_cb, arg0); |
| 457 | break; |
| 458 | |
| 459 | case TARGET_SYS_WRITEC: |
| 460 | /* |
| 461 | * FIXME: the byte to be written is in a uint64_t slot, |
| 462 | * which means this is wrong for a big-endian guest. |
| 463 | */ |
| 464 | semihost_sys_write_gf(cs, common_semi_dead_cb, |
| 465 | &console_out_gf, args, 1); |
| 466 | break; |
| 467 | |
| 468 | case TARGET_SYS_WRITE0: |
| 469 | { |
| 470 | ssize_t len = target_strlen(args); |
| 471 | if (len < 0) { |
| 472 | common_semi_dead_cb(cs, -1, EFAULT); |
| 473 | } else { |
| 474 | semihost_sys_write_gf(cs, common_semi_dead_cb, |
| 475 | &console_out_gf, args, len); |
| 476 | } |
| 477 | } |
| 478 | break; |
| 479 | |
| 480 | case TARGET_SYS_WRITE: |
| 481 | GET_ARG(0); |
| 482 | GET_ARG(1); |
| 483 | GET_ARG(2); |
| 484 | semihost_sys_write(cs, common_semi_rw_cb, arg0, arg1, arg2); |
| 485 | break; |
| 486 | |
| 487 | case TARGET_SYS_READ: |
| 488 | GET_ARG(0); |
| 489 | GET_ARG(1); |
| 490 | GET_ARG(2); |
| 491 | semihost_sys_read(cs, common_semi_rw_cb, arg0, arg1, arg2); |
| 492 | break; |
| 493 | |
| 494 | case TARGET_SYS_READC: |
| 495 | semihost_sys_read_gf(cs, common_semi_readc_cb, &console_in_gf, |
| 496 | common_semi_stack_bottom(cs) - 1, 1); |
| 497 | break; |
| 498 | |
| 499 | case TARGET_SYS_ISERROR: |
| 500 | { |
| 501 | GET_ARG(0); |
| 502 | bool ret = is_64bit_semihosting(env) ? |
| 503 | (int64_t)arg0 < 0 : (int32_t)arg0 < 0; |
| 504 | common_semi_set_ret(cs, ret); |
| 505 | break; |
| 506 | } |
| 507 | case TARGET_SYS_ISTTY: |
| 508 | GET_ARG(0); |
| 509 | semihost_sys_isatty(cs, common_semi_istty_cb, arg0); |
| 510 | break; |
| 511 | |
| 512 | case TARGET_SYS_SEEK: |
| 513 | GET_ARG(0); |
| 514 | GET_ARG(1); |
| 515 | semihost_sys_lseek(cs, common_semi_seek_cb, arg0, arg1, GDB_SEEK_SET); |
| 516 | break; |
| 517 | |
| 518 | case TARGET_SYS_FLEN: |
| 519 | GET_ARG(0); |
| 520 | semihost_sys_flen(cs, common_semi_flen_fstat_cb, common_semi_cb, |
| 521 | arg0, common_semi_flen_buf(cs)); |
| 522 | break; |
| 523 | |
| 524 | case TARGET_SYS_TMPNAM: |
| 525 | { |
| 526 | int len; |
| 527 | char *p; |
| 528 | |
| 529 | GET_ARG(0); |
| 530 | GET_ARG(1); |
| 531 | GET_ARG(2); |
| 532 | len = asprintf(&s, "%s/qemu-%x%02x", g_get_tmp_dir(), |
| 533 | getpid(), (int)arg1 & 0xff); |
| 534 | if (len < 0) { |
| 535 | common_semi_set_ret(cs, -1); |
| 536 | break; |
| 537 | } |
| 538 | |
| 539 | /* Allow for trailing NUL */ |
| 540 | len++; |
| 541 | /* Make sure there's enough space in the buffer */ |
| 542 | if (len > arg2) { |
| 543 | free(s); |
| 544 | common_semi_set_ret(cs, -1); |
| 545 | break; |
| 546 | } |
| 547 | p = lock_user(VERIFY_WRITE, arg0, len, 0); |
| 548 | if (!p) { |
| 549 | free(s); |
| 550 | goto do_fault; |
| 551 | } |
| 552 | memcpy(p, s, len); |
| 553 | unlock_user(p, arg0, len); |
| 554 | free(s); |
| 555 | common_semi_set_ret(cs, 0); |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | case TARGET_SYS_REMOVE: |
| 560 | GET_ARG(0); |
| 561 | GET_ARG(1); |
| 562 | semihost_sys_remove(cs, common_semi_cb, arg0, arg1 + 1); |
| 563 | break; |
| 564 | |
| 565 | case TARGET_SYS_RENAME: |
| 566 | GET_ARG(0); |
| 567 | GET_ARG(1); |
| 568 | GET_ARG(2); |
| 569 | GET_ARG(3); |
| 570 | semihost_sys_rename(cs, common_semi_cb, arg0, arg1 + 1, arg2, arg3 + 1); |
| 571 | break; |
| 572 | |
| 573 | case TARGET_SYS_CLOCK: |
| 574 | common_semi_set_ret(cs, clock() / (CLOCKS_PER_SEC / 100)); |
| 575 | break; |
| 576 | |
| 577 | case TARGET_SYS_TIME: |
| 578 | ul_ret = time(NULL); |
| 579 | common_semi_cb(cs, ul_ret, ul_ret == -1 ? errno : 0); |
| 580 | break; |
| 581 | |
| 582 | case TARGET_SYS_SYSTEM: |
| 583 | GET_ARG(0); |
| 584 | GET_ARG(1); |
| 585 | semihost_sys_system(cs, common_semi_cb, arg0, arg1 + 1); |
| 586 | break; |
| 587 | |
| 588 | case TARGET_SYS_ERRNO: |
| 589 | common_semi_set_ret(cs, get_swi_errno(cs)); |
| 590 | break; |
| 591 | |
| 592 | case TARGET_SYS_GET_CMDLINE: |
| 593 | { |
| 594 | /* Build a command-line from the original argv. |
| 595 | * |
| 596 | * The inputs are: |
| 597 | * * arg0, pointer to a buffer of at least the size |
| 598 | * specified in arg1. |
| 599 | * * arg1, size of the buffer pointed to by arg0 in |
| 600 | * bytes. |
| 601 | * |
| 602 | * The outputs are: |
| 603 | * * arg0, pointer to null-terminated string of the |
| 604 | * command line. |
| 605 | * * arg1, length of the string pointed to by arg0. |
| 606 | */ |
| 607 | |
| 608 | char *output_buffer; |
| 609 | size_t input_size; |
| 610 | size_t output_size; |
| 611 | int status = 0; |
| 612 | #if !defined(CONFIG_USER_ONLY) |
| 613 | const char *cmdline; |
| 614 | #else |
| 615 | TaskState *ts = get_task_state(cs); |
| 616 | #endif |
| 617 | GET_ARG(0); |
| 618 | GET_ARG(1); |
| 619 | input_size = arg1; |
| 620 | /* Compute the size of the output string. */ |
| 621 | #if !defined(CONFIG_USER_ONLY) |
| 622 | cmdline = semihosting_get_cmdline(); |
| 623 | if (cmdline == NULL) { |
| 624 | cmdline = ""; /* Default to an empty line. */ |
| 625 | } |
| 626 | output_size = strlen(cmdline) + 1; /* Count terminating 0. */ |
| 627 | #else |
| 628 | unsigned int i; |
| 629 | |
| 630 | output_size = ts->info->env_strings - ts->info->arg_strings; |
| 631 | if (!output_size) { |
| 632 | /* |
| 633 | * We special-case the "empty command line" case (argc==0). |
| 634 | * Just provide the terminating 0. |
| 635 | */ |
| 636 | output_size = 1; |
| 637 | } |
| 638 | #endif |
| 639 | |
| 640 | if (output_size > input_size) { |
| 641 | /* Not enough space to store command-line arguments. */ |
| 642 | common_semi_cb(cs, -1, E2BIG); |
| 643 | break; |
| 644 | } |
| 645 | |
| 646 | /* Adjust the command-line length. */ |
| 647 | if (SET_ARG(1, output_size - 1)) { |
| 648 | /* Couldn't write back to argument block */ |
| 649 | goto do_fault; |
| 650 | } |
| 651 | |
| 652 | /* Lock the buffer on the ARM side. */ |
| 653 | output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0); |
| 654 | if (!output_buffer) { |
| 655 | goto do_fault; |
| 656 | } |
| 657 | |
| 658 | /* Copy the command-line arguments. */ |
| 659 | #if !defined(CONFIG_USER_ONLY) |
| 660 | pstrcpy(output_buffer, output_size, cmdline); |
| 661 | #else |
| 662 | if (output_size == 1) { |
| 663 | /* Empty command-line. */ |
| 664 | output_buffer[0] = '\0'; |
| 665 | goto out; |
| 666 | } |
| 667 | |
| 668 | if (copy_from_user(output_buffer, ts->info->arg_strings, |
| 669 | output_size)) { |
| 670 | unlock_user(output_buffer, arg0, 0); |
| 671 | goto do_fault; |
| 672 | } |
| 673 | |
| 674 | /* Separate arguments by white spaces. */ |
| 675 | for (i = 0; i < output_size - 1; i++) { |
| 676 | if (output_buffer[i] == 0) { |
| 677 | output_buffer[i] = ' '; |
| 678 | } |
| 679 | } |
| 680 | out: |
| 681 | #endif |
| 682 | /* Unlock the buffer on the ARM side. */ |
| 683 | unlock_user(output_buffer, arg0, output_size); |
| 684 | common_semi_cb(cs, status, 0); |
| 685 | } |
| 686 | break; |
| 687 | |
| 688 | case TARGET_SYS_HEAPINFO: |
| 689 | { |
| 690 | uint64_t retvals[4]; |
| 691 | int i; |
| 692 | #ifdef CONFIG_USER_ONLY |
| 693 | TaskState *ts = get_task_state(cs); |
| 694 | static abi_ulong heapbase, heaplimit; |
| 695 | #else |
| 696 | LayoutInfo info = common_semi_find_bases(cs); |
| 697 | #endif |
| 698 | |
| 699 | GET_ARG(0); |
| 700 | |
| 701 | #ifdef CONFIG_USER_ONLY |
| 702 | /* |
| 703 | * Some C libraries assume the heap immediately follows .bss, so |
| 704 | * allocate it using sbrk. |
| 705 | */ |
| 706 | if (!heaplimit) { |
| 707 | heapbase = do_brk(0); |
| 708 | /* Try a big heap, and reduce the size if that fails. */ |
| 709 | for (abi_ulong size = COMMON_SEMI_HEAP_SIZE; ; size >>= 1) { |
| 710 | abi_ulong limit = heapbase + size; |
| 711 | abi_ulong ret = do_brk(limit); |
| 712 | if (ret >= limit) { |
| 713 | heaplimit = limit; |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | retvals[0] = heapbase; |
| 719 | retvals[1] = heaplimit; |
| 720 | /* |
| 721 | * Note that semihosting is *not* thread aware. |
| 722 | * Always return the stack base of the main thread. |
| 723 | */ |
| 724 | retvals[2] = ts->info->start_stack; |
| 725 | retvals[3] = 0; /* Stack limit. */ |
| 726 | #else |
| 727 | retvals[0] = info.heapbase; /* Heap Base */ |
| 728 | retvals[1] = info.heaplimit; /* Heap Limit */ |
| 729 | retvals[2] = info.heaplimit; /* Stack base */ |
| 730 | retvals[3] = info.heapbase; /* Stack limit. */ |
| 731 | #endif |
| 732 | |
| 733 | for (i = 0; i < ARRAY_SIZE(retvals); i++) { |
| 734 | bool fail; |
| 735 | |
| 736 | if (is_64bit_semihosting(env)) { |
| 737 | fail = put_user_u64(retvals[i], arg0 + i * 8); |
| 738 | } else { |
| 739 | fail = put_user_u32(retvals[i], arg0 + i * 4); |
| 740 | } |
| 741 | |
| 742 | if (fail) { |
| 743 | /* Couldn't write back to argument block */ |
| 744 | goto do_fault; |
| 745 | } |
| 746 | } |
| 747 | common_semi_set_ret(cs, 0); |
| 748 | } |
| 749 | break; |
| 750 | |
| 751 | case TARGET_SYS_EXIT: |
| 752 | case TARGET_SYS_EXIT_EXTENDED: |
| 753 | { |
| 754 | uint32_t ret; |
| 755 | |
| 756 | if (nr == TARGET_SYS_EXIT_EXTENDED || |
| 757 | common_semi_sys_exit_is_extended(cs)) { |
| 758 | /* |
| 759 | * The A64 version of SYS_EXIT takes a parameter block, |
| 760 | * so the application-exit type can return a subcode which |
| 761 | * is the exit status code from the application. |
| 762 | * SYS_EXIT_EXTENDED is an a new-in-v2.0 optional function |
| 763 | * which allows A32/T32 guests to also provide a status code. |
| 764 | */ |
| 765 | GET_ARG(0); |
| 766 | GET_ARG(1); |
| 767 | |
| 768 | if (arg0 == ADP_Stopped_ApplicationExit) { |
| 769 | ret = arg1; |
| 770 | } else { |
| 771 | ret = 1; |
| 772 | } |
| 773 | } else { |
| 774 | /* |
| 775 | * The A32/T32 version of SYS_EXIT specifies only |
| 776 | * Stopped_ApplicationExit as normal exit, but does not |
| 777 | * allow the guest to specify the exit status code. |
| 778 | * Everything else is considered an error. |
| 779 | */ |
| 780 | ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1; |
| 781 | } |
| 782 | gdb_exit(ret); |
| 783 | exit(ret); |
| 784 | } |
| 785 | |
| 786 | case TARGET_SYS_ELAPSED: |
| 787 | elapsed = get_clock() - clock_start; |
| 788 | if (is_64bit_semihosting(env)) { |
| 789 | if (SET_ARG(0, elapsed)) { |
| 790 | goto do_fault; |
| 791 | } |
| 792 | } else { |
| 793 | if (SET_ARG(0, (uint32_t) elapsed) || |
| 794 | SET_ARG(1, (uint32_t) (elapsed >> 32))) { |
| 795 | goto do_fault; |
| 796 | } |
| 797 | } |
| 798 | common_semi_set_ret(cs, 0); |
| 799 | break; |
| 800 | |
| 801 | case TARGET_SYS_TICKFREQ: |
| 802 | /* qemu always uses nsec */ |
| 803 | common_semi_set_ret(cs, 1000000000); |
| 804 | break; |
| 805 | |
| 806 | case TARGET_SYS_SYNCCACHE: |
| 807 | /* |
| 808 | * Clean the D-cache and invalidate the I-cache for the specified |
| 809 | * virtual address range. This is a nop for us since we don't |
| 810 | * implement caches. This is only present on A64. |
| 811 | */ |
| 812 | if (common_semi_has_synccache(env)) { |
| 813 | common_semi_set_ret(cs, 0); |
| 814 | break; |
| 815 | } |
| 816 | /* fall through */ |
| 817 | default: |
| 818 | fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr); |
| 819 | cpu_dump_state(cs, stderr, 0); |
| 820 | abort(); |
| 821 | |
| 822 | do_fault: |
| 823 | common_semi_cb(cs, -1, EFAULT); |
| 824 | break; |
| 825 | } |
| 826 | } |