1fcf5ef2aSThomas Huth /* 2fcf5ef2aSThomas Huth * m68k/ColdFire Semihosting syscall interface 3fcf5ef2aSThomas Huth * 4fcf5ef2aSThomas Huth * Copyright (c) 2005-2007 CodeSourcery. 5fcf5ef2aSThomas Huth * 6fcf5ef2aSThomas Huth * This program is free software; you can redistribute it and/or modify 7fcf5ef2aSThomas Huth * it under the terms of the GNU General Public License as published by 8fcf5ef2aSThomas Huth * the Free Software Foundation; either version 2 of the License, or 9fcf5ef2aSThomas Huth * (at your option) any later version. 10fcf5ef2aSThomas Huth * 11fcf5ef2aSThomas Huth * This program is distributed in the hope that it will be useful, 12fcf5ef2aSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of 13fcf5ef2aSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14fcf5ef2aSThomas Huth * GNU General Public License for more details. 15fcf5ef2aSThomas Huth * 16fcf5ef2aSThomas Huth * You should have received a copy of the GNU General Public License 17fcf5ef2aSThomas Huth * along with this program; if not, see <http://www.gnu.org/licenses/>. 181321d844SPeter Maydell * 191321d844SPeter Maydell * The semihosting protocol implemented here is described in the 201321d844SPeter Maydell * libgloss sources: 211321d844SPeter Maydell * https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=libgloss/m68k/m68k-semi.txt;hb=HEAD 22fcf5ef2aSThomas Huth */ 23fcf5ef2aSThomas Huth 24fcf5ef2aSThomas Huth #include "qemu/osdep.h" 25fcf5ef2aSThomas Huth 26fcf5ef2aSThomas Huth #include "cpu.h" 27c566080cSAlex Bennée #include "gdbstub/syscalls.h" 284ea5fe99SAlex Bennée #include "gdbstub/helpers.h" 2995027250SRichard Henderson #include "semihosting/syscalls.h" 30f14eced5SPhilippe Mathieu-Daudé #include "semihosting/uaccess.h" 315601d241SPaolo Bonzini #include "hw/boards.h" 32fcf5ef2aSThomas Huth #include "qemu/log.h" 33fcf5ef2aSThomas Huth 34fcf5ef2aSThomas Huth #define HOSTED_EXIT 0 35fcf5ef2aSThomas Huth #define HOSTED_INIT_SIM 1 36fcf5ef2aSThomas Huth #define HOSTED_OPEN 2 37fcf5ef2aSThomas Huth #define HOSTED_CLOSE 3 38fcf5ef2aSThomas Huth #define HOSTED_READ 4 39fcf5ef2aSThomas Huth #define HOSTED_WRITE 5 40fcf5ef2aSThomas Huth #define HOSTED_LSEEK 6 41fcf5ef2aSThomas Huth #define HOSTED_RENAME 7 42fcf5ef2aSThomas Huth #define HOSTED_UNLINK 8 43fcf5ef2aSThomas Huth #define HOSTED_STAT 9 44fcf5ef2aSThomas Huth #define HOSTED_FSTAT 10 45fcf5ef2aSThomas Huth #define HOSTED_GETTIMEOFDAY 11 46fcf5ef2aSThomas Huth #define HOSTED_ISATTY 12 47fcf5ef2aSThomas Huth #define HOSTED_SYSTEM 13 48fcf5ef2aSThomas Huth 497327e602SRichard Henderson static int host_to_gdb_errno(int err) 507327e602SRichard Henderson { 517327e602SRichard Henderson #define E(X) case E##X: return GDB_E##X 527327e602SRichard Henderson switch (err) { 537327e602SRichard Henderson E(PERM); 547327e602SRichard Henderson E(NOENT); 557327e602SRichard Henderson E(INTR); 567327e602SRichard Henderson E(BADF); 577327e602SRichard Henderson E(ACCES); 587327e602SRichard Henderson E(FAULT); 597327e602SRichard Henderson E(BUSY); 607327e602SRichard Henderson E(EXIST); 617327e602SRichard Henderson E(NODEV); 627327e602SRichard Henderson E(NOTDIR); 637327e602SRichard Henderson E(ISDIR); 647327e602SRichard Henderson E(INVAL); 657327e602SRichard Henderson E(NFILE); 667327e602SRichard Henderson E(MFILE); 677327e602SRichard Henderson E(FBIG); 687327e602SRichard Henderson E(NOSPC); 697327e602SRichard Henderson E(SPIPE); 707327e602SRichard Henderson E(ROFS); 717327e602SRichard Henderson E(NAMETOOLONG); 727327e602SRichard Henderson default: 737327e602SRichard Henderson return GDB_EUNKNOWN; 747327e602SRichard Henderson } 757327e602SRichard Henderson #undef E 767327e602SRichard Henderson } 777327e602SRichard Henderson 78ab294b6cSRichard Henderson static void m68k_semi_u32_cb(CPUState *cs, uint64_t ret, int err) 79fcf5ef2aSThomas Huth { 80e22a4560SPhilippe Mathieu-Daudé CPUM68KState *env = cpu_env(cs); 81ab294b6cSRichard Henderson 82fcf5ef2aSThomas Huth target_ulong args = env->dregs[1]; 83fcf5ef2aSThomas Huth if (put_user_u32(ret, args) || 847327e602SRichard Henderson put_user_u32(host_to_gdb_errno(err), args + 4)) { 85808d77bcSLucien Murray-Pitts /* 86808d77bcSLucien Murray-Pitts * The m68k semihosting ABI does not provide any way to report this 87fcf5ef2aSThomas Huth * error to the guest, so the best we can do is log it in qemu. 88fcf5ef2aSThomas Huth * It is always a guest error not to pass us a valid argument block. 89fcf5ef2aSThomas Huth */ 90fcf5ef2aSThomas Huth qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value " 91fcf5ef2aSThomas Huth "discarded because argument block not writable\n"); 92fcf5ef2aSThomas Huth } 93fcf5ef2aSThomas Huth } 94fcf5ef2aSThomas Huth 95ab294b6cSRichard Henderson static void m68k_semi_u64_cb(CPUState *cs, uint64_t ret, int err) 96fcf5ef2aSThomas Huth { 97e22a4560SPhilippe Mathieu-Daudé CPUM68KState *env = cpu_env(cs); 98ab294b6cSRichard Henderson 99fcf5ef2aSThomas Huth target_ulong args = env->dregs[1]; 100fcf5ef2aSThomas Huth if (put_user_u32(ret >> 32, args) || 101fcf5ef2aSThomas Huth put_user_u32(ret, args + 4) || 1027327e602SRichard Henderson put_user_u32(host_to_gdb_errno(err), args + 8)) { 103fcf5ef2aSThomas Huth /* No way to report this via m68k semihosting ABI; just log it */ 104fcf5ef2aSThomas Huth qemu_log_mask(LOG_GUEST_ERROR, "m68k-semihosting: return value " 105fcf5ef2aSThomas Huth "discarded because argument block not writable\n"); 106fcf5ef2aSThomas Huth } 107fcf5ef2aSThomas Huth } 108fcf5ef2aSThomas Huth 109808d77bcSLucien Murray-Pitts /* 110808d77bcSLucien Murray-Pitts * Read the input value from the argument block; fail the semihosting 111fcf5ef2aSThomas Huth * call if the memory read fails. 112fcf5ef2aSThomas Huth */ 113fcf5ef2aSThomas Huth #define GET_ARG(n) do { \ 114fcf5ef2aSThomas Huth if (get_user_ual(arg ## n, args + (n) * 4)) { \ 115fcf5ef2aSThomas Huth goto failed; \ 116fcf5ef2aSThomas Huth } \ 117fcf5ef2aSThomas Huth } while (0) 118fcf5ef2aSThomas Huth 11995027250SRichard Henderson #define GET_ARG64(n) do { \ 12095027250SRichard Henderson if (get_user_ual(arg ## n, args + (n) * 4)) { \ 12195027250SRichard Henderson goto failed64; \ 12295027250SRichard Henderson } \ 12395027250SRichard Henderson } while (0) 12495027250SRichard Henderson 12595027250SRichard Henderson 126fcf5ef2aSThomas Huth void do_m68k_semihosting(CPUM68KState *env, int nr) 127fcf5ef2aSThomas Huth { 128ab294b6cSRichard Henderson CPUState *cs = env_cpu(env); 129fcf5ef2aSThomas Huth uint32_t args; 130fcf5ef2aSThomas Huth target_ulong arg0, arg1, arg2, arg3; 131fcf5ef2aSThomas Huth 132fcf5ef2aSThomas Huth args = env->dregs[1]; 133fcf5ef2aSThomas Huth switch (nr) { 134fcf5ef2aSThomas Huth case HOSTED_EXIT: 135ad9dcb20SAlex Bennée gdb_exit(env->dregs[0]); 136fcf5ef2aSThomas Huth exit(env->dregs[0]); 13795027250SRichard Henderson 138fcf5ef2aSThomas Huth case HOSTED_OPEN: 139fcf5ef2aSThomas Huth GET_ARG(0); 140fcf5ef2aSThomas Huth GET_ARG(1); 141fcf5ef2aSThomas Huth GET_ARG(2); 142fcf5ef2aSThomas Huth GET_ARG(3); 14395027250SRichard Henderson semihost_sys_open(cs, m68k_semi_u32_cb, arg0, arg1, arg2, arg3); 144fcf5ef2aSThomas Huth break; 14595027250SRichard Henderson 146fcf5ef2aSThomas Huth case HOSTED_CLOSE: 147fcf5ef2aSThomas Huth GET_ARG(0); 14895027250SRichard Henderson semihost_sys_close(cs, m68k_semi_u32_cb, arg0); 149fcf5ef2aSThomas Huth break; 15095027250SRichard Henderson 151fcf5ef2aSThomas Huth case HOSTED_READ: 152fcf5ef2aSThomas Huth GET_ARG(0); 153fcf5ef2aSThomas Huth GET_ARG(1); 154fcf5ef2aSThomas Huth GET_ARG(2); 15595027250SRichard Henderson semihost_sys_read(cs, m68k_semi_u32_cb, arg0, arg1, arg2); 156fcf5ef2aSThomas Huth break; 15795027250SRichard Henderson 158fcf5ef2aSThomas Huth case HOSTED_WRITE: 159fcf5ef2aSThomas Huth GET_ARG(0); 160fcf5ef2aSThomas Huth GET_ARG(1); 161fcf5ef2aSThomas Huth GET_ARG(2); 16295027250SRichard Henderson semihost_sys_write(cs, m68k_semi_u32_cb, arg0, arg1, arg2); 163fcf5ef2aSThomas Huth break; 16495027250SRichard Henderson 165fcf5ef2aSThomas Huth case HOSTED_LSEEK: 16695027250SRichard Henderson GET_ARG64(0); 16795027250SRichard Henderson GET_ARG64(1); 16895027250SRichard Henderson GET_ARG64(2); 16995027250SRichard Henderson GET_ARG64(3); 17095027250SRichard Henderson semihost_sys_lseek(cs, m68k_semi_u64_cb, arg0, 1718caaae73SPeter Maydell deposit64(arg2, 32, 32, arg1), arg3); 17295027250SRichard Henderson break; 17395027250SRichard Henderson 174fcf5ef2aSThomas Huth case HOSTED_RENAME: 175fcf5ef2aSThomas Huth GET_ARG(0); 176fcf5ef2aSThomas Huth GET_ARG(1); 177fcf5ef2aSThomas Huth GET_ARG(2); 178fcf5ef2aSThomas Huth GET_ARG(3); 17995027250SRichard Henderson semihost_sys_rename(cs, m68k_semi_u32_cb, arg0, arg1, arg2, arg3); 180fcf5ef2aSThomas Huth break; 18195027250SRichard Henderson 182fcf5ef2aSThomas Huth case HOSTED_UNLINK: 183fcf5ef2aSThomas Huth GET_ARG(0); 184fcf5ef2aSThomas Huth GET_ARG(1); 18595027250SRichard Henderson semihost_sys_remove(cs, m68k_semi_u32_cb, arg0, arg1); 186fcf5ef2aSThomas Huth break; 18795027250SRichard Henderson 188fcf5ef2aSThomas Huth case HOSTED_STAT: 189fcf5ef2aSThomas Huth GET_ARG(0); 190fcf5ef2aSThomas Huth GET_ARG(1); 191fcf5ef2aSThomas Huth GET_ARG(2); 19295027250SRichard Henderson semihost_sys_stat(cs, m68k_semi_u32_cb, arg0, arg1, arg2); 193fcf5ef2aSThomas Huth break; 19495027250SRichard Henderson 195fcf5ef2aSThomas Huth case HOSTED_FSTAT: 196fcf5ef2aSThomas Huth GET_ARG(0); 197fcf5ef2aSThomas Huth GET_ARG(1); 19895027250SRichard Henderson semihost_sys_fstat(cs, m68k_semi_u32_cb, arg0, arg1); 199fcf5ef2aSThomas Huth break; 20095027250SRichard Henderson 201fcf5ef2aSThomas Huth case HOSTED_GETTIMEOFDAY: 202fcf5ef2aSThomas Huth GET_ARG(0); 203fcf5ef2aSThomas Huth GET_ARG(1); 20495027250SRichard Henderson semihost_sys_gettimeofday(cs, m68k_semi_u32_cb, arg0, arg1); 205fcf5ef2aSThomas Huth break; 20695027250SRichard Henderson 207fcf5ef2aSThomas Huth case HOSTED_ISATTY: 208fcf5ef2aSThomas Huth GET_ARG(0); 20995027250SRichard Henderson semihost_sys_isatty(cs, m68k_semi_u32_cb, arg0); 210fcf5ef2aSThomas Huth break; 21195027250SRichard Henderson 212fcf5ef2aSThomas Huth case HOSTED_SYSTEM: 213fcf5ef2aSThomas Huth GET_ARG(0); 214fcf5ef2aSThomas Huth GET_ARG(1); 21595027250SRichard Henderson semihost_sys_system(cs, m68k_semi_u32_cb, arg0, arg1); 216fcf5ef2aSThomas Huth break; 21795027250SRichard Henderson 218fcf5ef2aSThomas Huth case HOSTED_INIT_SIM: 219808d77bcSLucien Murray-Pitts /* 220808d77bcSLucien Murray-Pitts * FIXME: This is wrong for boards where RAM does not start at 221808d77bcSLucien Murray-Pitts * address zero. 222808d77bcSLucien Murray-Pitts */ 2235601d241SPaolo Bonzini env->dregs[1] = current_machine->ram_size; 2245601d241SPaolo Bonzini env->aregs[7] = current_machine->ram_size; 225fcf5ef2aSThomas Huth return; 22695027250SRichard Henderson 227fcf5ef2aSThomas Huth default: 228a8d92fd8SRichard Henderson cpu_abort(env_cpu(env), "Unsupported semihosting syscall %d\n", nr); 22995027250SRichard Henderson 230fcf5ef2aSThomas Huth failed: 23195027250SRichard Henderson m68k_semi_u32_cb(cs, -1, EFAULT); 23295027250SRichard Henderson break; 23395027250SRichard Henderson failed64: 23495027250SRichard Henderson m68k_semi_u64_cb(cs, -1, EFAULT); 23595027250SRichard Henderson break; 23695027250SRichard Henderson } 237fcf5ef2aSThomas Huth } 238