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_u32_cb(CPUState *cs, uint64_t ret, int err) 99 { 100 M68kCPU *cpu = M68K_CPU(cs); 101 CPUM68KState *env = &cpu->env; 102 103 target_ulong args = env->dregs[1]; 104 if (put_user_u32(ret, args) || 105 put_user_u32(err, args + 4)) { 106 /* 107 * The m68k semihosting ABI does not provide any way to report this 108 * error to the guest, so the best we can do is log it in qemu. 109 * It is always a guest error not to pass us a valid argument block. 110 */ 111 qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value " 112 "discarded because argument block not writable\n"); 113 } 114 } 115 116 static void m68k_semi_u64_cb(CPUState *cs, uint64_t ret, int err) 117 { 118 M68kCPU *cpu = M68K_CPU(cs); 119 CPUM68KState *env = &cpu->env; 120 121 target_ulong args = env->dregs[1]; 122 if (put_user_u32(ret >> 32, args) || 123 put_user_u32(ret, args + 4) || 124 put_user_u32(err, args + 8)) { 125 /* No way to report this via m68k semihosting ABI; just log it */ 126 qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value " 127 "discarded because argument block not writable\n"); 128 } 129 } 130 131 /* 132 * Read the input value from the argument block; fail the semihosting 133 * call if the memory read fails. 134 */ 135 #define GET_ARG(n) do { \ 136 if (get_user_ual(arg ## n, args + (n) * 4)) { \ 137 result = -1; \ 138 errno = EFAULT; \ 139 goto failed; \ 140 } \ 141 } while (0) 142 143 void do_m68k_semihosting(CPUM68KState *env, int nr) 144 { 145 CPUState *cs = env_cpu(env); 146 uint32_t args; 147 target_ulong arg0, arg1, arg2, arg3; 148 void *p; 149 void *q; 150 uint32_t len; 151 uint32_t result; 152 153 args = env->dregs[1]; 154 switch (nr) { 155 case HOSTED_EXIT: 156 gdb_exit(env->dregs[0]); 157 exit(env->dregs[0]); 158 case HOSTED_OPEN: 159 GET_ARG(0); 160 GET_ARG(1); 161 GET_ARG(2); 162 GET_ARG(3); 163 if (use_gdb_syscalls()) { 164 gdb_do_syscall(m68k_semi_u32_cb, "open,%s,%x,%x", arg0, (int)arg1, 165 arg2, arg3); 166 return; 167 } else { 168 p = lock_user_string(arg0); 169 if (!p) { 170 /* FIXME - check error code? */ 171 result = -1; 172 } else { 173 result = open(p, translate_openflags(arg2), arg3); 174 unlock_user(p, arg0, 0); 175 } 176 } 177 break; 178 case HOSTED_CLOSE: 179 { 180 /* Ignore attempts to close stdin/out/err. */ 181 GET_ARG(0); 182 int fd = arg0; 183 if (fd > 2) { 184 if (use_gdb_syscalls()) { 185 gdb_do_syscall(m68k_semi_u32_cb, "close,%x", arg0); 186 return; 187 } else { 188 result = close(fd); 189 } 190 } else { 191 result = 0; 192 } 193 break; 194 } 195 case HOSTED_READ: 196 GET_ARG(0); 197 GET_ARG(1); 198 GET_ARG(2); 199 len = arg2; 200 if (use_gdb_syscalls()) { 201 gdb_do_syscall(m68k_semi_u32_cb, "read,%x,%x,%x", 202 arg0, arg1, len); 203 return; 204 } else { 205 p = lock_user(VERIFY_WRITE, arg1, len, 0); 206 if (!p) { 207 /* FIXME - check error code? */ 208 result = -1; 209 } else { 210 result = read(arg0, p, len); 211 unlock_user(p, arg1, len); 212 } 213 } 214 break; 215 case HOSTED_WRITE: 216 GET_ARG(0); 217 GET_ARG(1); 218 GET_ARG(2); 219 len = arg2; 220 if (use_gdb_syscalls()) { 221 gdb_do_syscall(m68k_semi_u32_cb, "write,%x,%x,%x", 222 arg0, arg1, len); 223 return; 224 } else { 225 p = lock_user(VERIFY_READ, arg1, len, 1); 226 if (!p) { 227 /* FIXME - check error code? */ 228 result = -1; 229 } else { 230 result = write(arg0, p, len); 231 unlock_user(p, arg0, 0); 232 } 233 } 234 break; 235 case HOSTED_LSEEK: 236 { 237 uint64_t off; 238 GET_ARG(0); 239 GET_ARG(1); 240 GET_ARG(2); 241 GET_ARG(3); 242 off = (uint32_t)arg2 | ((uint64_t)arg1 << 32); 243 if (use_gdb_syscalls()) { 244 gdb_do_syscall(m68k_semi_u64_cb, "fseek,%x,%lx,%x", 245 arg0, off, arg3); 246 } else { 247 off = lseek(arg0, off, arg3); 248 m68k_semi_u64_cb(cs, off, errno); 249 } 250 return; 251 } 252 case HOSTED_RENAME: 253 GET_ARG(0); 254 GET_ARG(1); 255 GET_ARG(2); 256 GET_ARG(3); 257 if (use_gdb_syscalls()) { 258 gdb_do_syscall(m68k_semi_u32_cb, "rename,%s,%s", 259 arg0, (int)arg1, arg2, (int)arg3); 260 return; 261 } else { 262 p = lock_user_string(arg0); 263 q = lock_user_string(arg2); 264 if (!p || !q) { 265 /* FIXME - check error code? */ 266 result = -1; 267 } else { 268 result = rename(p, q); 269 } 270 unlock_user(p, arg0, 0); 271 unlock_user(q, arg2, 0); 272 } 273 break; 274 case HOSTED_UNLINK: 275 GET_ARG(0); 276 GET_ARG(1); 277 if (use_gdb_syscalls()) { 278 gdb_do_syscall(m68k_semi_u32_cb, "unlink,%s", 279 arg0, (int)arg1); 280 return; 281 } else { 282 p = lock_user_string(arg0); 283 if (!p) { 284 /* FIXME - check error code? */ 285 result = -1; 286 } else { 287 result = unlink(p); 288 unlock_user(p, arg0, 0); 289 } 290 } 291 break; 292 case HOSTED_STAT: 293 GET_ARG(0); 294 GET_ARG(1); 295 GET_ARG(2); 296 if (use_gdb_syscalls()) { 297 gdb_do_syscall(m68k_semi_u32_cb, "stat,%s,%x", 298 arg0, (int)arg1, arg2); 299 return; 300 } else { 301 struct stat s; 302 p = lock_user_string(arg0); 303 if (!p) { 304 /* FIXME - check error code? */ 305 result = -1; 306 } else { 307 result = stat(p, &s); 308 unlock_user(p, arg0, 0); 309 } 310 if (result == 0) { 311 translate_stat(env, arg2, &s); 312 } 313 } 314 break; 315 case HOSTED_FSTAT: 316 GET_ARG(0); 317 GET_ARG(1); 318 if (use_gdb_syscalls()) { 319 gdb_do_syscall(m68k_semi_u32_cb, "fstat,%x,%x", 320 arg0, arg1); 321 return; 322 } else { 323 struct stat s; 324 result = fstat(arg0, &s); 325 if (result == 0) { 326 translate_stat(env, arg1, &s); 327 } 328 } 329 break; 330 case HOSTED_GETTIMEOFDAY: 331 GET_ARG(0); 332 GET_ARG(1); 333 if (use_gdb_syscalls()) { 334 gdb_do_syscall(m68k_semi_u32_cb, "gettimeofday,%x,%x", 335 arg0, arg1); 336 return; 337 } else { 338 struct gdb_timeval *p; 339 int64_t rt = g_get_real_time(); 340 p = lock_user(VERIFY_WRITE, arg0, sizeof(struct gdb_timeval), 0); 341 if (!p) { 342 /* FIXME - check error code? */ 343 result = -1; 344 } else { 345 result = 0; 346 p->tv_sec = cpu_to_be32(rt / G_USEC_PER_SEC); 347 p->tv_usec = cpu_to_be64(rt % G_USEC_PER_SEC); 348 unlock_user(p, arg0, sizeof(struct gdb_timeval)); 349 } 350 } 351 break; 352 case HOSTED_ISATTY: 353 GET_ARG(0); 354 if (use_gdb_syscalls()) { 355 gdb_do_syscall(m68k_semi_u32_cb, "isatty,%x", arg0); 356 return; 357 } else { 358 result = isatty(arg0); 359 } 360 break; 361 case HOSTED_SYSTEM: 362 GET_ARG(0); 363 GET_ARG(1); 364 if (use_gdb_syscalls()) { 365 gdb_do_syscall(m68k_semi_u32_cb, "system,%s", 366 arg0, (int)arg1); 367 return; 368 } else { 369 p = lock_user_string(arg0); 370 if (!p) { 371 /* FIXME - check error code? */ 372 result = -1; 373 } else { 374 result = system(p); 375 unlock_user(p, arg0, 0); 376 } 377 } 378 break; 379 case HOSTED_INIT_SIM: 380 #if defined(CONFIG_USER_ONLY) 381 { 382 CPUState *cs = env_cpu(env); 383 TaskState *ts = cs->opaque; 384 /* Allocate the heap using sbrk. */ 385 if (!ts->heap_limit) { 386 abi_ulong ret; 387 uint32_t size; 388 uint32_t base; 389 390 base = do_brk(0); 391 size = SEMIHOSTING_HEAP_SIZE; 392 /* Try a big heap, and reduce the size if that fails. */ 393 for (;;) { 394 ret = do_brk(base + size); 395 if (ret >= (base + size)) { 396 break; 397 } 398 size >>= 1; 399 } 400 ts->heap_limit = base + size; 401 } 402 /* 403 * This call may happen before we have writable memory, so return 404 * values directly in registers. 405 */ 406 env->dregs[1] = ts->heap_limit; 407 env->aregs[7] = ts->stack_base; 408 } 409 #else 410 /* 411 * FIXME: This is wrong for boards where RAM does not start at 412 * address zero. 413 */ 414 env->dregs[1] = current_machine->ram_size; 415 env->aregs[7] = current_machine->ram_size; 416 #endif 417 return; 418 default: 419 cpu_abort(env_cpu(env), "Unsupported semihosting syscall %d\n", nr); 420 result = 0; 421 } 422 failed: 423 m68k_semi_u32_cb(cs, result, errno); 424 } 425