1 /* 2 * m68k/ColdFire Semihosting syscall interface 3 * 4 * Copyright (c) 2005-2007 CodeSourcery. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "qemu/osdep.h" 21 22 #include "cpu.h" 23 #include "exec/gdbstub.h" 24 #if defined(CONFIG_USER_ONLY) 25 #include "qemu.h" 26 #define SEMIHOSTING_HEAP_SIZE (128 * 1024 * 1024) 27 #else 28 #include "semihosting/softmmu-uaccess.h" 29 #include "hw/boards.h" 30 #endif 31 #include "qemu/log.h" 32 33 #define HOSTED_EXIT 0 34 #define HOSTED_INIT_SIM 1 35 #define HOSTED_OPEN 2 36 #define HOSTED_CLOSE 3 37 #define HOSTED_READ 4 38 #define HOSTED_WRITE 5 39 #define HOSTED_LSEEK 6 40 #define HOSTED_RENAME 7 41 #define HOSTED_UNLINK 8 42 #define HOSTED_STAT 9 43 #define HOSTED_FSTAT 10 44 #define HOSTED_GETTIMEOFDAY 11 45 #define HOSTED_ISATTY 12 46 #define HOSTED_SYSTEM 13 47 48 static int translate_openflags(int flags) 49 { 50 int hf; 51 52 if (flags & GDB_O_WRONLY) 53 hf = O_WRONLY; 54 else if (flags & GDB_O_RDWR) 55 hf = O_RDWR; 56 else 57 hf = O_RDONLY; 58 59 if (flags & GDB_O_APPEND) hf |= O_APPEND; 60 if (flags & GDB_O_CREAT) hf |= O_CREAT; 61 if (flags & GDB_O_TRUNC) hf |= O_TRUNC; 62 if (flags & GDB_O_EXCL) hf |= O_EXCL; 63 64 return hf; 65 } 66 67 static void translate_stat(CPUM68KState *env, target_ulong addr, struct stat *s) 68 { 69 struct gdb_stat *p; 70 71 p = lock_user(VERIFY_WRITE, addr, sizeof(struct gdb_stat), 0); 72 if (!p) { 73 /* FIXME - should this return an error code? */ 74 return; 75 } 76 p->gdb_st_dev = cpu_to_be32(s->st_dev); 77 p->gdb_st_ino = cpu_to_be32(s->st_ino); 78 p->gdb_st_mode = cpu_to_be32(s->st_mode); 79 p->gdb_st_nlink = cpu_to_be32(s->st_nlink); 80 p->gdb_st_uid = cpu_to_be32(s->st_uid); 81 p->gdb_st_gid = cpu_to_be32(s->st_gid); 82 p->gdb_st_rdev = cpu_to_be32(s->st_rdev); 83 p->gdb_st_size = cpu_to_be64(s->st_size); 84 #ifdef _WIN32 85 /* Windows stat is missing some fields. */ 86 p->gdb_st_blksize = 0; 87 p->gdb_st_blocks = 0; 88 #else 89 p->gdb_st_blksize = cpu_to_be64(s->st_blksize); 90 p->gdb_st_blocks = cpu_to_be64(s->st_blocks); 91 #endif 92 p->gdb_st_atime = cpu_to_be32(s->st_atime); 93 p->gdb_st_mtime = cpu_to_be32(s->st_mtime); 94 p->gdb_st_ctime = cpu_to_be32(s->st_ctime); 95 unlock_user(p, addr, sizeof(struct gdb_stat)); 96 } 97 98 static void m68k_semi_return_u32(CPUM68KState *env, uint32_t ret, int err) 99 { 100 target_ulong args = env->dregs[1]; 101 if (put_user_u32(ret, args) || 102 put_user_u32(err, args + 4)) { 103 /* 104 * The m68k semihosting ABI does not provide any way to report this 105 * error to the guest, so the best we can do is log it in qemu. 106 * It is always a guest error not to pass us a valid argument block. 107 */ 108 qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value " 109 "discarded because argument block not writable\n"); 110 } 111 } 112 113 static void m68k_semi_return_u64(CPUM68KState *env, uint64_t ret, int err) 114 { 115 target_ulong args = env->dregs[1]; 116 if (put_user_u32(ret >> 32, args) || 117 put_user_u32(ret, args + 4) || 118 put_user_u32(err, args + 8)) { 119 /* No way to report this via m68k semihosting ABI; just log it */ 120 qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value " 121 "discarded because argument block not writable\n"); 122 } 123 } 124 125 static int m68k_semi_is_fseek; 126 127 static void m68k_semi_cb(CPUState *cs, uint64_t ret, int err) 128 { 129 M68kCPU *cpu = M68K_CPU(cs); 130 CPUM68KState *env = &cpu->env; 131 132 if (m68k_semi_is_fseek) { 133 m68k_semi_return_u64(env, ret, err); 134 m68k_semi_is_fseek = 0; 135 } else { 136 m68k_semi_return_u32(env, ret, err); 137 } 138 } 139 140 /* 141 * Read the input value from the argument block; fail the semihosting 142 * call if the memory read fails. 143 */ 144 #define GET_ARG(n) do { \ 145 if (get_user_ual(arg ## n, args + (n) * 4)) { \ 146 result = -1; \ 147 errno = EFAULT; \ 148 goto failed; \ 149 } \ 150 } while (0) 151 152 void do_m68k_semihosting(CPUM68KState *env, int nr) 153 { 154 uint32_t args; 155 target_ulong arg0, arg1, arg2, arg3; 156 void *p; 157 void *q; 158 uint32_t len; 159 uint32_t result; 160 161 args = env->dregs[1]; 162 switch (nr) { 163 case HOSTED_EXIT: 164 gdb_exit(env->dregs[0]); 165 exit(env->dregs[0]); 166 case HOSTED_OPEN: 167 GET_ARG(0); 168 GET_ARG(1); 169 GET_ARG(2); 170 GET_ARG(3); 171 if (use_gdb_syscalls()) { 172 gdb_do_syscall(m68k_semi_cb, "open,%s,%x,%x", arg0, (int)arg1, 173 arg2, arg3); 174 return; 175 } else { 176 p = lock_user_string(arg0); 177 if (!p) { 178 /* FIXME - check error code? */ 179 result = -1; 180 } else { 181 result = open(p, translate_openflags(arg2), arg3); 182 unlock_user(p, arg0, 0); 183 } 184 } 185 break; 186 case HOSTED_CLOSE: 187 { 188 /* Ignore attempts to close stdin/out/err. */ 189 GET_ARG(0); 190 int fd = arg0; 191 if (fd > 2) { 192 if (use_gdb_syscalls()) { 193 gdb_do_syscall(m68k_semi_cb, "close,%x", arg0); 194 return; 195 } else { 196 result = close(fd); 197 } 198 } else { 199 result = 0; 200 } 201 break; 202 } 203 case HOSTED_READ: 204 GET_ARG(0); 205 GET_ARG(1); 206 GET_ARG(2); 207 len = arg2; 208 if (use_gdb_syscalls()) { 209 gdb_do_syscall(m68k_semi_cb, "read,%x,%x,%x", 210 arg0, arg1, len); 211 return; 212 } else { 213 p = lock_user(VERIFY_WRITE, arg1, len, 0); 214 if (!p) { 215 /* FIXME - check error code? */ 216 result = -1; 217 } else { 218 result = read(arg0, p, len); 219 unlock_user(p, arg1, len); 220 } 221 } 222 break; 223 case HOSTED_WRITE: 224 GET_ARG(0); 225 GET_ARG(1); 226 GET_ARG(2); 227 len = arg2; 228 if (use_gdb_syscalls()) { 229 gdb_do_syscall(m68k_semi_cb, "write,%x,%x,%x", 230 arg0, arg1, len); 231 return; 232 } else { 233 p = lock_user(VERIFY_READ, arg1, len, 1); 234 if (!p) { 235 /* FIXME - check error code? */ 236 result = -1; 237 } else { 238 result = write(arg0, p, len); 239 unlock_user(p, arg0, 0); 240 } 241 } 242 break; 243 case HOSTED_LSEEK: 244 { 245 uint64_t off; 246 GET_ARG(0); 247 GET_ARG(1); 248 GET_ARG(2); 249 GET_ARG(3); 250 off = (uint32_t)arg2 | ((uint64_t)arg1 << 32); 251 if (use_gdb_syscalls()) { 252 m68k_semi_is_fseek = 1; 253 gdb_do_syscall(m68k_semi_cb, "fseek,%x,%lx,%x", 254 arg0, off, arg3); 255 } else { 256 off = lseek(arg0, off, arg3); 257 m68k_semi_return_u64(env, off, errno); 258 } 259 return; 260 } 261 case HOSTED_RENAME: 262 GET_ARG(0); 263 GET_ARG(1); 264 GET_ARG(2); 265 GET_ARG(3); 266 if (use_gdb_syscalls()) { 267 gdb_do_syscall(m68k_semi_cb, "rename,%s,%s", 268 arg0, (int)arg1, arg2, (int)arg3); 269 return; 270 } else { 271 p = lock_user_string(arg0); 272 q = lock_user_string(arg2); 273 if (!p || !q) { 274 /* FIXME - check error code? */ 275 result = -1; 276 } else { 277 result = rename(p, q); 278 } 279 unlock_user(p, arg0, 0); 280 unlock_user(q, arg2, 0); 281 } 282 break; 283 case HOSTED_UNLINK: 284 GET_ARG(0); 285 GET_ARG(1); 286 if (use_gdb_syscalls()) { 287 gdb_do_syscall(m68k_semi_cb, "unlink,%s", 288 arg0, (int)arg1); 289 return; 290 } else { 291 p = lock_user_string(arg0); 292 if (!p) { 293 /* FIXME - check error code? */ 294 result = -1; 295 } else { 296 result = unlink(p); 297 unlock_user(p, arg0, 0); 298 } 299 } 300 break; 301 case HOSTED_STAT: 302 GET_ARG(0); 303 GET_ARG(1); 304 GET_ARG(2); 305 if (use_gdb_syscalls()) { 306 gdb_do_syscall(m68k_semi_cb, "stat,%s,%x", 307 arg0, (int)arg1, arg2); 308 return; 309 } else { 310 struct stat s; 311 p = lock_user_string(arg0); 312 if (!p) { 313 /* FIXME - check error code? */ 314 result = -1; 315 } else { 316 result = stat(p, &s); 317 unlock_user(p, arg0, 0); 318 } 319 if (result == 0) { 320 translate_stat(env, arg2, &s); 321 } 322 } 323 break; 324 case HOSTED_FSTAT: 325 GET_ARG(0); 326 GET_ARG(1); 327 if (use_gdb_syscalls()) { 328 gdb_do_syscall(m68k_semi_cb, "fstat,%x,%x", 329 arg0, arg1); 330 return; 331 } else { 332 struct stat s; 333 result = fstat(arg0, &s); 334 if (result == 0) { 335 translate_stat(env, arg1, &s); 336 } 337 } 338 break; 339 case HOSTED_GETTIMEOFDAY: 340 GET_ARG(0); 341 GET_ARG(1); 342 if (use_gdb_syscalls()) { 343 gdb_do_syscall(m68k_semi_cb, "gettimeofday,%x,%x", 344 arg0, arg1); 345 return; 346 } else { 347 struct gdb_timeval *p; 348 int64_t rt = g_get_real_time(); 349 p = lock_user(VERIFY_WRITE, arg0, sizeof(struct gdb_timeval), 0); 350 if (!p) { 351 /* FIXME - check error code? */ 352 result = -1; 353 } else { 354 result = 0; 355 p->tv_sec = cpu_to_be32(rt / G_USEC_PER_SEC); 356 p->tv_usec = cpu_to_be64(rt % G_USEC_PER_SEC); 357 unlock_user(p, arg0, sizeof(struct gdb_timeval)); 358 } 359 } 360 break; 361 case HOSTED_ISATTY: 362 GET_ARG(0); 363 if (use_gdb_syscalls()) { 364 gdb_do_syscall(m68k_semi_cb, "isatty,%x", arg0); 365 return; 366 } else { 367 result = isatty(arg0); 368 } 369 break; 370 case HOSTED_SYSTEM: 371 GET_ARG(0); 372 GET_ARG(1); 373 if (use_gdb_syscalls()) { 374 gdb_do_syscall(m68k_semi_cb, "system,%s", 375 arg0, (int)arg1); 376 return; 377 } else { 378 p = lock_user_string(arg0); 379 if (!p) { 380 /* FIXME - check error code? */ 381 result = -1; 382 } else { 383 result = system(p); 384 unlock_user(p, arg0, 0); 385 } 386 } 387 break; 388 case HOSTED_INIT_SIM: 389 #if defined(CONFIG_USER_ONLY) 390 { 391 CPUState *cs = env_cpu(env); 392 TaskState *ts = cs->opaque; 393 /* Allocate the heap using sbrk. */ 394 if (!ts->heap_limit) { 395 abi_ulong ret; 396 uint32_t size; 397 uint32_t base; 398 399 base = do_brk(0); 400 size = SEMIHOSTING_HEAP_SIZE; 401 /* Try a big heap, and reduce the size if that fails. */ 402 for (;;) { 403 ret = do_brk(base + size); 404 if (ret >= (base + size)) { 405 break; 406 } 407 size >>= 1; 408 } 409 ts->heap_limit = base + size; 410 } 411 /* 412 * This call may happen before we have writable memory, so return 413 * values directly in registers. 414 */ 415 env->dregs[1] = ts->heap_limit; 416 env->aregs[7] = ts->stack_base; 417 } 418 #else 419 /* 420 * FIXME: This is wrong for boards where RAM does not start at 421 * address zero. 422 */ 423 env->dregs[1] = current_machine->ram_size; 424 env->aregs[7] = current_machine->ram_size; 425 #endif 426 return; 427 default: 428 cpu_abort(env_cpu(env), "Unsupported semihosting syscall %d\n", nr); 429 result = 0; 430 } 431 failed: 432 m68k_semi_return_u32(env, result, errno); 433 } 434