1 /* 2 * Unified Hosting Interface syscalls. 3 * 4 * Copyright (c) 2015 Imagination Technologies 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library 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 GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "qemu/osdep.h" 21 #include "cpu.h" 22 #include "qemu/log.h" 23 #include "gdbstub/syscalls.h" 24 #include "gdbstub/helpers.h" 25 #include "semihosting/uaccess.h" 26 #include "semihosting/semihost.h" 27 #include "semihosting/console.h" 28 #include "semihosting/syscalls.h" 29 #include "internal.h" 30 31 typedef enum UHIOp { 32 UHI_exit = 1, 33 UHI_open = 2, 34 UHI_close = 3, 35 UHI_read = 4, 36 UHI_write = 5, 37 UHI_lseek = 6, 38 UHI_unlink = 7, 39 UHI_fstat = 8, 40 UHI_argc = 9, 41 UHI_argnlen = 10, 42 UHI_argn = 11, 43 UHI_plog = 13, 44 UHI_assert = 14, 45 UHI_pread = 19, 46 UHI_pwrite = 20, 47 UHI_link = 22 48 } UHIOp; 49 50 typedef struct UHIStat { 51 int16_t uhi_st_dev; 52 uint16_t uhi_st_ino; 53 uint32_t uhi_st_mode; 54 uint16_t uhi_st_nlink; 55 uint16_t uhi_st_uid; 56 uint16_t uhi_st_gid; 57 int16_t uhi_st_rdev; 58 uint64_t uhi_st_size; 59 uint64_t uhi_st_atime; 60 uint64_t uhi_st_spare1; 61 uint64_t uhi_st_mtime; 62 uint64_t uhi_st_spare2; 63 uint64_t uhi_st_ctime; 64 uint64_t uhi_st_spare3; 65 uint64_t uhi_st_blksize; 66 uint64_t uhi_st_blocks; 67 uint64_t uhi_st_spare4[2]; 68 } UHIStat; 69 70 enum UHIOpenFlags { 71 UHIOpen_RDONLY = 0x0, 72 UHIOpen_WRONLY = 0x1, 73 UHIOpen_RDWR = 0x2, 74 UHIOpen_APPEND = 0x8, 75 UHIOpen_CREAT = 0x200, 76 UHIOpen_TRUNC = 0x400, 77 UHIOpen_EXCL = 0x800 78 }; 79 80 enum UHIErrno { 81 UHI_EACCESS = 13, 82 UHI_EAGAIN = 11, 83 UHI_EBADF = 9, 84 UHI_EBADMSG = 77, 85 UHI_EBUSY = 16, 86 UHI_ECONNRESET = 104, 87 UHI_EEXIST = 17, 88 UHI_EFBIG = 27, 89 UHI_EINTR = 4, 90 UHI_EINVAL = 22, 91 UHI_EIO = 5, 92 UHI_EISDIR = 21, 93 UHI_ELOOP = 92, 94 UHI_EMFILE = 24, 95 UHI_EMLINK = 31, 96 UHI_ENAMETOOLONG = 91, 97 UHI_ENETDOWN = 115, 98 UHI_ENETUNREACH = 114, 99 UHI_ENFILE = 23, 100 UHI_ENOBUFS = 105, 101 UHI_ENOENT = 2, 102 UHI_ENOMEM = 12, 103 UHI_ENOSPC = 28, 104 UHI_ENOSR = 63, 105 UHI_ENOTCONN = 128, 106 UHI_ENOTDIR = 20, 107 UHI_ENXIO = 6, 108 UHI_EOVERFLOW = 139, 109 UHI_EPERM = 1, 110 UHI_EPIPE = 32, 111 UHI_ERANGE = 34, 112 UHI_EROFS = 30, 113 UHI_ESPIPE = 29, 114 UHI_ETIMEDOUT = 116, 115 UHI_ETXTBSY = 26, 116 UHI_EWOULDBLOCK = 11, 117 UHI_EXDEV = 18, 118 }; 119 120 static void report_fault(CPUMIPSState *env) 121 { 122 int op = env->active_tc.gpr[25]; 123 error_report("Fault during UHI operation %d", op); 124 abort(); 125 } 126 127 static void uhi_cb(CPUState *cs, uint64_t ret, int err) 128 { 129 CPUMIPSState *env = cpu_env(cs); 130 131 #define E(N) case E##N: err = UHI_E##N; break 132 133 switch (err) { 134 case 0: 135 break; 136 E(PERM); 137 E(NOENT); 138 E(INTR); 139 E(BADF); 140 E(BUSY); 141 E(EXIST); 142 E(NOTDIR); 143 E(ISDIR); 144 E(INVAL); 145 E(NFILE); 146 E(MFILE); 147 E(FBIG); 148 E(NOSPC); 149 E(SPIPE); 150 E(ROFS); 151 E(NAMETOOLONG); 152 default: 153 err = UHI_EINVAL; 154 break; 155 case EFAULT: 156 report_fault(env); 157 } 158 159 #undef E 160 161 env->active_tc.gpr[2] = ret; 162 env->active_tc.gpr[3] = err; 163 } 164 165 static void uhi_fstat_cb(CPUState *cs, uint64_t ret, int err) 166 { 167 QEMU_BUILD_BUG_ON(sizeof(UHIStat) < sizeof(struct gdb_stat)); 168 169 if (!err) { 170 CPUMIPSState *env = cpu_env(cs); 171 target_ulong addr = env->active_tc.gpr[5]; 172 UHIStat *dst = lock_user(VERIFY_WRITE, addr, sizeof(UHIStat), 1); 173 struct gdb_stat s; 174 175 if (!dst) { 176 report_fault(env); 177 } 178 179 memcpy(&s, dst, sizeof(struct gdb_stat)); 180 memset(dst, 0, sizeof(UHIStat)); 181 182 dst->uhi_st_dev = tswap16(be32_to_cpu(s.gdb_st_dev)); 183 dst->uhi_st_ino = tswap16(be32_to_cpu(s.gdb_st_ino)); 184 dst->uhi_st_mode = tswap32(be32_to_cpu(s.gdb_st_mode)); 185 dst->uhi_st_nlink = tswap16(be32_to_cpu(s.gdb_st_nlink)); 186 dst->uhi_st_uid = tswap16(be32_to_cpu(s.gdb_st_uid)); 187 dst->uhi_st_gid = tswap16(be32_to_cpu(s.gdb_st_gid)); 188 dst->uhi_st_rdev = tswap16(be32_to_cpu(s.gdb_st_rdev)); 189 dst->uhi_st_size = tswap64(be64_to_cpu(s.gdb_st_size)); 190 dst->uhi_st_atime = tswap64(be32_to_cpu(s.gdb_st_atime)); 191 dst->uhi_st_mtime = tswap64(be32_to_cpu(s.gdb_st_mtime)); 192 dst->uhi_st_ctime = tswap64(be32_to_cpu(s.gdb_st_ctime)); 193 dst->uhi_st_blksize = tswap64(be64_to_cpu(s.gdb_st_blksize)); 194 dst->uhi_st_blocks = tswap64(be64_to_cpu(s.gdb_st_blocks)); 195 196 unlock_user(dst, addr, sizeof(UHIStat)); 197 } 198 199 uhi_cb(cs, ret, err); 200 } 201 202 void mips_semihosting(CPUMIPSState *env) 203 { 204 CPUState *cs = env_cpu(env); 205 target_ulong *gpr = env->active_tc.gpr; 206 const UHIOp op = gpr[25]; 207 char *p; 208 209 switch (op) { 210 case UHI_exit: 211 gdb_exit(gpr[4]); 212 exit(gpr[4]); 213 214 case UHI_open: 215 { 216 target_ulong fname = gpr[4]; 217 int ret = -1; 218 219 p = lock_user_string(fname); 220 if (!p) { 221 report_fault(env); 222 } 223 if (!strcmp("/dev/stdin", p)) { 224 ret = 0; 225 } else if (!strcmp("/dev/stdout", p)) { 226 ret = 1; 227 } else if (!strcmp("/dev/stderr", p)) { 228 ret = 2; 229 } 230 unlock_user(p, fname, 0); 231 232 /* FIXME: reusing a guest fd doesn't seem correct. */ 233 if (ret >= 0) { 234 gpr[2] = ret; 235 break; 236 } 237 238 semihost_sys_open(cs, uhi_cb, fname, 0, gpr[5], gpr[6]); 239 } 240 break; 241 242 case UHI_close: 243 semihost_sys_close(cs, uhi_cb, gpr[4]); 244 break; 245 case UHI_read: 246 semihost_sys_read(cs, uhi_cb, gpr[4], gpr[5], gpr[6]); 247 break; 248 case UHI_write: 249 semihost_sys_write(cs, uhi_cb, gpr[4], gpr[5], gpr[6]); 250 break; 251 case UHI_lseek: 252 semihost_sys_lseek(cs, uhi_cb, gpr[4], gpr[5], gpr[6]); 253 break; 254 case UHI_unlink: 255 semihost_sys_remove(cs, uhi_cb, gpr[4], 0); 256 break; 257 case UHI_fstat: 258 semihost_sys_fstat(cs, uhi_fstat_cb, gpr[4], gpr[5]); 259 break; 260 261 case UHI_argc: 262 gpr[2] = semihosting_get_argc(); 263 break; 264 case UHI_argnlen: 265 { 266 const char *s = semihosting_get_arg(gpr[4]); 267 gpr[2] = s ? strlen(s) : -1; 268 } 269 break; 270 case UHI_argn: 271 { 272 const char *s = semihosting_get_arg(gpr[4]); 273 target_ulong addr; 274 size_t len; 275 276 if (!s) { 277 gpr[2] = -1; 278 break; 279 } 280 len = strlen(s) + 1; 281 addr = gpr[5]; 282 p = lock_user(VERIFY_WRITE, addr, len, 0); 283 if (!p) { 284 report_fault(env); 285 } 286 memcpy(p, s, len); 287 unlock_user(p, addr, len); 288 gpr[2] = 0; 289 } 290 break; 291 292 case UHI_plog: 293 { 294 target_ulong addr = gpr[4]; 295 ssize_t len = target_strlen(addr); 296 GString *str; 297 char *pct_d; 298 299 if (len < 0) { 300 report_fault(env); 301 } 302 p = lock_user(VERIFY_READ, addr, len, 1); 303 if (!p) { 304 report_fault(env); 305 } 306 307 pct_d = strstr(p, "%d"); 308 if (!pct_d) { 309 unlock_user(p, addr, 0); 310 semihost_sys_write(cs, uhi_cb, 2, addr, len); 311 break; 312 } 313 314 str = g_string_new_len(p, pct_d - p); 315 g_string_append_printf(str, "%d%s", (int)gpr[5], pct_d + 2); 316 unlock_user(p, addr, 0); 317 318 /* 319 * When we're using gdb, we need a guest address, so 320 * drop the string onto the stack below the stack pointer. 321 */ 322 if (use_gdb_syscalls()) { 323 addr = gpr[29] - str->len; 324 p = lock_user(VERIFY_WRITE, addr, str->len, 0); 325 if (!p) { 326 report_fault(env); 327 } 328 memcpy(p, str->str, str->len); 329 unlock_user(p, addr, str->len); 330 semihost_sys_write(cs, uhi_cb, 2, addr, str->len); 331 } else { 332 gpr[2] = qemu_semihosting_console_write(str->str, str->len); 333 } 334 g_string_free(str, true); 335 } 336 break; 337 338 case UHI_assert: 339 { 340 const char *msg, *file; 341 342 msg = lock_user_string(gpr[4]); 343 if (!msg) { 344 msg = "<EFAULT>"; 345 } 346 file = lock_user_string(gpr[5]); 347 if (!file) { 348 file = "<EFAULT>"; 349 } 350 351 error_report("UHI assertion \"%s\": file \"%s\", line %d", 352 msg, file, (int)gpr[6]); 353 abort(); 354 } 355 356 default: 357 error_report("Unknown UHI operation %d", op); 358 abort(); 359 } 360 return; 361 } 362