m68k: is_mem is useless
[qemu.git] / target-mips / mips-semi.c
blob1162c76df9df02b6141997ab53752946591e2936
1 /*
2 * Unified Hosting Interface syscalls.
4 * Copyright (c) 2015 Imagination Technologies
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 of the License, or (at your option) any later version.
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.
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/>.
20 #include <sys/stat.h>
21 #include "cpu.h"
22 #include "exec/helper-proto.h"
23 #include "exec/softmmu-semi.h"
24 #include "exec/semihost.h"
26 typedef enum UHIOp {
27 UHI_exit = 1,
28 UHI_open = 2,
29 UHI_close = 3,
30 UHI_read = 4,
31 UHI_write = 5,
32 UHI_lseek = 6,
33 UHI_unlink = 7,
34 UHI_fstat = 8,
35 UHI_argc = 9,
36 UHI_argnlen = 10,
37 UHI_argn = 11,
38 UHI_plog = 13,
39 UHI_assert = 14,
40 UHI_pread = 19,
41 UHI_pwrite = 20,
42 UHI_link = 22
43 } UHIOp;
45 typedef struct UHIStat {
46 int16_t uhi_st_dev;
47 uint16_t uhi_st_ino;
48 uint32_t uhi_st_mode;
49 uint16_t uhi_st_nlink;
50 uint16_t uhi_st_uid;
51 uint16_t uhi_st_gid;
52 int16_t uhi_st_rdev;
53 uint64_t uhi_st_size;
54 uint64_t uhi_st_atime;
55 uint64_t uhi_st_spare1;
56 uint64_t uhi_st_mtime;
57 uint64_t uhi_st_spare2;
58 uint64_t uhi_st_ctime;
59 uint64_t uhi_st_spare3;
60 uint64_t uhi_st_blksize;
61 uint64_t uhi_st_blocks;
62 uint64_t uhi_st_spare4[2];
63 } UHIStat;
65 enum UHIOpenFlags {
66 UHIOpen_RDONLY = 0x0,
67 UHIOpen_WRONLY = 0x1,
68 UHIOpen_RDWR = 0x2,
69 UHIOpen_APPEND = 0x8,
70 UHIOpen_CREAT = 0x200,
71 UHIOpen_TRUNC = 0x400,
72 UHIOpen_EXCL = 0x800
75 /* Errno values taken from asm-mips/errno.h */
76 static uint16_t host_to_mips_errno[] = {
77 [ENAMETOOLONG] = 78,
78 #ifdef EOVERFLOW
79 [EOVERFLOW] = 79,
80 #endif
81 #ifdef ELOOP
82 [ELOOP] = 90,
83 #endif
86 static int errno_mips(int err)
88 if (err < 0 || err >= ARRAY_SIZE(host_to_mips_errno)) {
89 return EINVAL;
90 } else if (host_to_mips_errno[err]) {
91 return host_to_mips_errno[err];
92 } else {
93 return err;
97 static int copy_stat_to_target(CPUMIPSState *env, const struct stat *src,
98 target_ulong vaddr)
100 hwaddr len = sizeof(struct UHIStat);
101 UHIStat *dst = lock_user(VERIFY_WRITE, vaddr, len, 0);
102 if (!dst) {
103 errno = EFAULT;
104 return -1;
107 dst->uhi_st_dev = tswap16(src->st_dev);
108 dst->uhi_st_ino = tswap16(src->st_ino);
109 dst->uhi_st_mode = tswap32(src->st_mode);
110 dst->uhi_st_nlink = tswap16(src->st_nlink);
111 dst->uhi_st_uid = tswap16(src->st_uid);
112 dst->uhi_st_gid = tswap16(src->st_gid);
113 dst->uhi_st_rdev = tswap16(src->st_rdev);
114 dst->uhi_st_size = tswap64(src->st_size);
115 dst->uhi_st_atime = tswap64(src->st_atime);
116 dst->uhi_st_mtime = tswap64(src->st_mtime);
117 dst->uhi_st_ctime = tswap64(src->st_ctime);
118 #ifdef _WIN32
119 dst->uhi_st_blksize = 0;
120 dst->uhi_st_blocks = 0;
121 #else
122 dst->uhi_st_blksize = tswap64(src->st_blksize);
123 dst->uhi_st_blocks = tswap64(src->st_blocks);
124 #endif
125 unlock_user(dst, vaddr, len);
126 return 0;
129 static int get_open_flags(target_ulong target_flags)
131 int open_flags = 0;
133 if (target_flags & UHIOpen_RDWR) {
134 open_flags |= O_RDWR;
135 } else if (target_flags & UHIOpen_WRONLY) {
136 open_flags |= O_WRONLY;
137 } else {
138 open_flags |= O_RDONLY;
141 open_flags |= (target_flags & UHIOpen_APPEND) ? O_APPEND : 0;
142 open_flags |= (target_flags & UHIOpen_CREAT) ? O_CREAT : 0;
143 open_flags |= (target_flags & UHIOpen_TRUNC) ? O_TRUNC : 0;
144 open_flags |= (target_flags & UHIOpen_EXCL) ? O_EXCL : 0;
146 return open_flags;
149 static int write_to_file(CPUMIPSState *env, target_ulong fd, target_ulong vaddr,
150 target_ulong len, target_ulong offset)
152 int num_of_bytes;
153 void *dst = lock_user(VERIFY_READ, vaddr, len, 1);
154 if (!dst) {
155 errno = EFAULT;
156 return -1;
159 if (offset) {
160 #ifdef _WIN32
161 num_of_bytes = 0;
162 #else
163 num_of_bytes = pwrite(fd, dst, len, offset);
164 #endif
165 } else {
166 num_of_bytes = write(fd, dst, len);
169 unlock_user(dst, vaddr, 0);
170 return num_of_bytes;
173 static int read_from_file(CPUMIPSState *env, target_ulong fd,
174 target_ulong vaddr, target_ulong len,
175 target_ulong offset)
177 int num_of_bytes;
178 void *dst = lock_user(VERIFY_WRITE, vaddr, len, 0);
179 if (!dst) {
180 errno = EFAULT;
181 return -1;
184 if (offset) {
185 #ifdef _WIN32
186 num_of_bytes = 0;
187 #else
188 num_of_bytes = pread(fd, dst, len, offset);
189 #endif
190 } else {
191 num_of_bytes = read(fd, dst, len);
194 unlock_user(dst, vaddr, len);
195 return num_of_bytes;
198 static int copy_argn_to_target(CPUMIPSState *env, int arg_num,
199 target_ulong vaddr)
201 int strsize = strlen(semihosting_get_arg(arg_num)) + 1;
202 char *dst = lock_user(VERIFY_WRITE, vaddr, strsize, 0);
203 if (!dst) {
204 return -1;
207 strcpy(dst, semihosting_get_arg(arg_num));
209 unlock_user(dst, vaddr, strsize);
210 return 0;
213 #define GET_TARGET_STRING(p, addr) \
214 do { \
215 p = lock_user_string(addr); \
216 if (!p) { \
217 gpr[2] = -1; \
218 gpr[3] = EFAULT; \
219 goto uhi_done; \
221 } while (0)
223 #define FREE_TARGET_STRING(p, gpr) \
224 do { \
225 unlock_user(p, gpr, 0); \
226 } while (0)
228 void helper_do_semihosting(CPUMIPSState *env)
230 target_ulong *gpr = env->active_tc.gpr;
231 const UHIOp op = gpr[25];
232 char *p, *p2;
234 switch (op) {
235 case UHI_exit:
236 qemu_log("UHI(%d): exit(%d)\n", op, (int)gpr[4]);
237 exit(gpr[4]);
238 case UHI_open:
239 GET_TARGET_STRING(p, gpr[4]);
240 if (!strcmp("/dev/stdin", p)) {
241 gpr[2] = 0;
242 } else if (!strcmp("/dev/stdout", p)) {
243 gpr[2] = 1;
244 } else if (!strcmp("/dev/stderr", p)) {
245 gpr[2] = 2;
246 } else {
247 gpr[2] = open(p, get_open_flags(gpr[5]), gpr[6]);
248 gpr[3] = errno_mips(errno);
250 FREE_TARGET_STRING(p, gpr[4]);
251 break;
252 case UHI_close:
253 if (gpr[4] < 3) {
254 /* ignore closing stdin/stdout/stderr */
255 gpr[2] = 0;
256 goto uhi_done;
258 gpr[2] = close(gpr[4]);
259 gpr[3] = errno_mips(errno);
260 break;
261 case UHI_read:
262 gpr[2] = read_from_file(env, gpr[4], gpr[5], gpr[6], 0);
263 gpr[3] = errno_mips(errno);
264 break;
265 case UHI_write:
266 gpr[2] = write_to_file(env, gpr[4], gpr[5], gpr[6], 0);
267 gpr[3] = errno_mips(errno);
268 break;
269 case UHI_lseek:
270 gpr[2] = lseek(gpr[4], gpr[5], gpr[6]);
271 gpr[3] = errno_mips(errno);
272 break;
273 case UHI_unlink:
274 GET_TARGET_STRING(p, gpr[4]);
275 gpr[2] = remove(p);
276 gpr[3] = errno_mips(errno);
277 FREE_TARGET_STRING(p, gpr[4]);
278 break;
279 case UHI_fstat:
281 struct stat sbuf;
282 memset(&sbuf, 0, sizeof(sbuf));
283 gpr[2] = fstat(gpr[4], &sbuf);
284 gpr[3] = errno_mips(errno);
285 if (gpr[2]) {
286 goto uhi_done;
288 gpr[2] = copy_stat_to_target(env, &sbuf, gpr[5]);
289 gpr[3] = errno_mips(errno);
291 break;
292 case UHI_argc:
293 gpr[2] = semihosting_get_argc();
294 break;
295 case UHI_argnlen:
296 if (gpr[4] >= semihosting_get_argc()) {
297 gpr[2] = -1;
298 goto uhi_done;
300 gpr[2] = strlen(semihosting_get_arg(gpr[4]));
301 break;
302 case UHI_argn:
303 if (gpr[4] >= semihosting_get_argc()) {
304 gpr[2] = -1;
305 goto uhi_done;
307 gpr[2] = copy_argn_to_target(env, gpr[4], gpr[5]);
308 break;
309 case UHI_plog:
310 GET_TARGET_STRING(p, gpr[4]);
311 p2 = strstr(p, "%d");
312 if (p2) {
313 int char_num = p2 - p;
314 char *buf = g_malloc(char_num + 1);
315 strncpy(buf, p, char_num);
316 buf[char_num] = '\0';
317 gpr[2] = printf("%s%d%s", buf, (int)gpr[5], p2 + 2);
318 g_free(buf);
319 } else {
320 gpr[2] = printf("%s", p);
322 FREE_TARGET_STRING(p, gpr[4]);
323 break;
324 case UHI_assert:
325 GET_TARGET_STRING(p, gpr[4]);
326 GET_TARGET_STRING(p2, gpr[5]);
327 printf("assertion '");
328 printf("\"%s\"", p);
329 printf("': file \"%s\", line %d\n", p2, (int)gpr[6]);
330 FREE_TARGET_STRING(p2, gpr[5]);
331 FREE_TARGET_STRING(p, gpr[4]);
332 abort();
333 break;
334 case UHI_pread:
335 gpr[2] = read_from_file(env, gpr[4], gpr[5], gpr[6], gpr[7]);
336 gpr[3] = errno_mips(errno);
337 break;
338 case UHI_pwrite:
339 gpr[2] = write_to_file(env, gpr[4], gpr[5], gpr[6], gpr[7]);
340 gpr[3] = errno_mips(errno);
341 break;
342 #ifndef _WIN32
343 case UHI_link:
344 GET_TARGET_STRING(p, gpr[4]);
345 GET_TARGET_STRING(p2, gpr[5]);
346 gpr[2] = link(p, p2);
347 gpr[3] = errno_mips(errno);
348 FREE_TARGET_STRING(p2, gpr[5]);
349 FREE_TARGET_STRING(p, gpr[4]);
350 break;
351 #endif
352 default:
353 fprintf(stderr, "Unknown UHI operation %d\n", op);
354 abort();
356 uhi_done:
357 return;