xref: /qemu/target/mips/tcg/system/mips-semi.c (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
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         bool swap_needed = HOST_BIG_ENDIAN != mips_env_is_bigendian(env);
172         target_ulong addr = env->active_tc.gpr[5];
173         UHIStat *dst = lock_user(VERIFY_WRITE, addr, sizeof(UHIStat), 1);
174         struct gdb_stat s;
175 
176         if (!dst) {
177             report_fault(env);
178         }
179 
180         memcpy(&s, dst, sizeof(struct gdb_stat));
181         memset(dst, 0, sizeof(UHIStat));
182 
183         dst->uhi_st_dev = be32_to_cpu(s.gdb_st_dev);
184         dst->uhi_st_ino = be32_to_cpu(s.gdb_st_ino);
185         dst->uhi_st_mode = be32_to_cpu(s.gdb_st_mode);
186         dst->uhi_st_nlink = be32_to_cpu(s.gdb_st_nlink);
187         dst->uhi_st_uid = be32_to_cpu(s.gdb_st_uid);
188         dst->uhi_st_gid = be32_to_cpu(s.gdb_st_gid);
189         dst->uhi_st_rdev = be32_to_cpu(s.gdb_st_rdev);
190         dst->uhi_st_size = be64_to_cpu(s.gdb_st_size);
191         dst->uhi_st_atime = be32_to_cpu(s.gdb_st_atime);
192         dst->uhi_st_mtime = be32_to_cpu(s.gdb_st_mtime);
193         dst->uhi_st_ctime = be32_to_cpu(s.gdb_st_ctime);
194         dst->uhi_st_blksize = be64_to_cpu(s.gdb_st_blksize);
195         dst->uhi_st_blocks = be64_to_cpu(s.gdb_st_blocks);
196 
197         if (swap_needed) {
198             dst->uhi_st_dev = bswap16(dst->uhi_st_dev);
199             dst->uhi_st_ino = bswap16(dst->uhi_st_ino);
200             dst->uhi_st_mode = bswap32(dst->uhi_st_mode);
201             dst->uhi_st_nlink = bswap16(dst->uhi_st_nlink);
202             dst->uhi_st_uid = bswap16(dst->uhi_st_uid);
203             dst->uhi_st_gid = bswap16(dst->uhi_st_gid);
204             dst->uhi_st_rdev = bswap16(dst->uhi_st_rdev);
205             dst->uhi_st_size = bswap64(dst->uhi_st_size);
206             dst->uhi_st_atime = bswap64(dst->uhi_st_atime);
207             dst->uhi_st_mtime = bswap64(dst->uhi_st_mtime);
208             dst->uhi_st_ctime = bswap64(dst->uhi_st_ctime);
209             dst->uhi_st_blksize = bswap64(dst->uhi_st_blksize);
210             dst->uhi_st_blocks = bswap64(dst->uhi_st_blocks);
211         }
212 
213         unlock_user(dst, addr, sizeof(UHIStat));
214     }
215 
216     uhi_cb(cs, ret, err);
217 }
218 
219 void mips_semihosting(CPUMIPSState *env)
220 {
221     CPUState *cs = env_cpu(env);
222     target_ulong *gpr = env->active_tc.gpr;
223     const UHIOp op = gpr[25];
224     char *p;
225 
226     switch (op) {
227     case UHI_exit:
228         gdb_exit(gpr[4]);
229         exit(gpr[4]);
230 
231     case UHI_open:
232         {
233             target_ulong fname = gpr[4];
234             int ret = -1;
235 
236             p = lock_user_string(fname);
237             if (!p) {
238                 report_fault(env);
239             }
240             if (!strcmp("/dev/stdin", p)) {
241                 ret = 0;
242             } else if (!strcmp("/dev/stdout", p)) {
243                 ret = 1;
244             } else if (!strcmp("/dev/stderr", p)) {
245                 ret = 2;
246             }
247             unlock_user(p, fname, 0);
248 
249             /* FIXME: reusing a guest fd doesn't seem correct. */
250             if (ret >= 0) {
251                 gpr[2] = ret;
252                 break;
253             }
254 
255             semihost_sys_open(cs, uhi_cb, fname, 0, gpr[5], gpr[6]);
256         }
257         break;
258 
259     case UHI_close:
260         semihost_sys_close(cs, uhi_cb, gpr[4]);
261         break;
262     case UHI_read:
263         semihost_sys_read(cs, uhi_cb, gpr[4], gpr[5], gpr[6]);
264         break;
265     case UHI_write:
266         semihost_sys_write(cs, uhi_cb, gpr[4], gpr[5], gpr[6]);
267         break;
268     case UHI_lseek:
269         semihost_sys_lseek(cs, uhi_cb, gpr[4], gpr[5], gpr[6]);
270         break;
271     case UHI_unlink:
272         semihost_sys_remove(cs, uhi_cb, gpr[4], 0);
273         break;
274     case UHI_fstat:
275         semihost_sys_fstat(cs, uhi_fstat_cb, gpr[4], gpr[5]);
276         break;
277 
278     case UHI_argc:
279         gpr[2] = semihosting_get_argc();
280         break;
281     case UHI_argnlen:
282         {
283             const char *s = semihosting_get_arg(gpr[4]);
284             gpr[2] = s ? strlen(s) : -1;
285         }
286         break;
287     case UHI_argn:
288         {
289             const char *s = semihosting_get_arg(gpr[4]);
290             target_ulong addr;
291             size_t len;
292 
293             if (!s) {
294                 gpr[2] = -1;
295                 break;
296             }
297             len = strlen(s) + 1;
298             addr = gpr[5];
299             p = lock_user(VERIFY_WRITE, addr, len, 0);
300             if (!p) {
301                 report_fault(env);
302             }
303             memcpy(p, s, len);
304             unlock_user(p, addr, len);
305             gpr[2] = 0;
306         }
307         break;
308 
309     case UHI_plog:
310         {
311             target_ulong addr = gpr[4];
312             ssize_t len = target_strlen(addr);
313             GString *str;
314             char *pct_d;
315 
316             if (len < 0) {
317                 report_fault(env);
318             }
319             p = lock_user(VERIFY_READ, addr, len, 1);
320             if (!p) {
321                 report_fault(env);
322             }
323 
324             pct_d = strstr(p, "%d");
325             if (!pct_d) {
326                 unlock_user(p, addr, 0);
327                 semihost_sys_write(cs, uhi_cb, 2, addr, len);
328                 break;
329             }
330 
331             str = g_string_new_len(p, pct_d - p);
332             g_string_append_printf(str, "%d%s", (int)gpr[5], pct_d + 2);
333             unlock_user(p, addr, 0);
334 
335             /*
336              * When we're using gdb, we need a guest address, so
337              * drop the string onto the stack below the stack pointer.
338              */
339             if (use_gdb_syscalls()) {
340                 addr = gpr[29] - str->len;
341                 p = lock_user(VERIFY_WRITE, addr, str->len, 0);
342                 if (!p) {
343                     report_fault(env);
344                 }
345                 memcpy(p, str->str, str->len);
346                 unlock_user(p, addr, str->len);
347                 semihost_sys_write(cs, uhi_cb, 2, addr, str->len);
348             } else {
349                 gpr[2] = qemu_semihosting_console_write(str->str, str->len);
350             }
351             g_string_free(str, true);
352         }
353         break;
354 
355     case UHI_assert:
356         {
357             const char *msg, *file;
358 
359             msg = lock_user_string(gpr[4]);
360             if (!msg) {
361                 msg = "<EFAULT>";
362             }
363             file = lock_user_string(gpr[5]);
364             if (!file) {
365                 file = "<EFAULT>";
366             }
367 
368             error_report("UHI assertion \"%s\": file \"%s\", line %d",
369                          msg, file, (int)gpr[6]);
370             abort();
371         }
372 
373     default:
374         error_report("Unknown UHI operation %d", op);
375         abort();
376     }
377     return;
378 }
379