]>
Commit | Line | Data |
---|---|---|
3b3c1694 LA |
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 | |
89975214 | 9 | * version 2.1 of the License, or (at your option) any later version. |
3b3c1694 LA |
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 | ||
c684822a | 20 | #include "qemu/osdep.h" |
3b3c1694 | 21 | #include "cpu.h" |
63c91552 | 22 | #include "qemu/log.h" |
c566080c | 23 | #include "gdbstub/syscalls.h" |
4ea5fe99 | 24 | #include "gdbstub/helpers.h" |
f14eced5 | 25 | #include "semihosting/uaccess.h" |
6b5fe137 PMD |
26 | #include "semihosting/semihost.h" |
27 | #include "semihosting/console.h" | |
18639a28 | 28 | #include "semihosting/syscalls.h" |
8ec7e3c5 | 29 | #include "internal.h" |
3b3c1694 LA |
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 | ||
7ba6e53a RH |
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 | ||
d53a3ed4 RH |
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 | ||
18639a28 | 127 | static void uhi_cb(CPUState *cs, uint64_t ret, int err) |
d859a77d | 128 | { |
b77af26e | 129 | CPUMIPSState *env = cpu_env(cs); |
2c44b19c | 130 | |
18639a28 RH |
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: | |
d53a3ed4 | 156 | report_fault(env); |
3b3c1694 LA |
157 | } |
158 | ||
18639a28 RH |
159 | #undef E |
160 | ||
161 | env->active_tc.gpr[2] = ret; | |
162 | env->active_tc.gpr[3] = err; | |
3b3c1694 LA |
163 | } |
164 | ||
18639a28 | 165 | static void uhi_fstat_cb(CPUState *cs, uint64_t ret, int err) |
3b3c1694 | 166 | { |
18639a28 | 167 | QEMU_BUILD_BUG_ON(sizeof(UHIStat) < sizeof(struct gdb_stat)); |
3b3c1694 | 168 | |
18639a28 | 169 | if (!err) { |
b77af26e | 170 | CPUMIPSState *env = cpu_env(cs); |
18639a28 RH |
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; | |
3b3c1694 | 174 | |
18639a28 RH |
175 | if (!dst) { |
176 | report_fault(env); | |
177 | } | |
3b3c1694 | 178 | |
18639a28 RH |
179 | memcpy(&s, dst, sizeof(struct gdb_stat)); |
180 | memset(dst, 0, sizeof(UHIStat)); | |
3b3c1694 | 181 | |
18639a28 RH |
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)); | |
3b3c1694 | 195 | |
18639a28 | 196 | unlock_user(dst, addr, sizeof(UHIStat)); |
3b3c1694 LA |
197 | } |
198 | ||
18639a28 | 199 | uhi_cb(cs, ret, err); |
3b3c1694 LA |
200 | } |
201 | ||
8ec7e3c5 | 202 | void mips_semihosting(CPUMIPSState *env) |
3b3c1694 | 203 | { |
18639a28 | 204 | CPUState *cs = env_cpu(env); |
3b3c1694 LA |
205 | target_ulong *gpr = env->active_tc.gpr; |
206 | const UHIOp op = gpr[25]; | |
412411b3 | 207 | char *p; |
3b3c1694 LA |
208 | |
209 | switch (op) { | |
210 | case UHI_exit: | |
18639a28 | 211 | gdb_exit(gpr[4]); |
3b3c1694 | 212 | exit(gpr[4]); |
18639a28 | 213 | |
3b3c1694 | 214 | case UHI_open: |
18639a28 | 215 | { |
b10ccec1 | 216 | target_ulong fname = gpr[4]; |
18639a28 RH |
217 | int ret = -1; |
218 | ||
b10ccec1 RH |
219 | p = lock_user_string(fname); |
220 | if (!p) { | |
221 | report_fault(env); | |
222 | } | |
18639a28 RH |
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 | } | |
b10ccec1 | 230 | unlock_user(p, fname, 0); |
18639a28 RH |
231 | |
232 | /* FIXME: reusing a guest fd doesn't seem correct. */ | |
233 | if (ret >= 0) { | |
234 | gpr[2] = ret; | |
235 | break; | |
236 | } | |
237 | ||
b10ccec1 | 238 | semihost_sys_open(cs, uhi_cb, fname, 0, gpr[5], gpr[6]); |
3b3c1694 | 239 | } |
3b3c1694 | 240 | break; |
18639a28 | 241 | |
3b3c1694 | 242 | case UHI_close: |
18639a28 | 243 | semihost_sys_close(cs, uhi_cb, gpr[4]); |
3b3c1694 LA |
244 | break; |
245 | case UHI_read: | |
18639a28 | 246 | semihost_sys_read(cs, uhi_cb, gpr[4], gpr[5], gpr[6]); |
3b3c1694 LA |
247 | break; |
248 | case UHI_write: | |
18639a28 | 249 | semihost_sys_write(cs, uhi_cb, gpr[4], gpr[5], gpr[6]); |
3b3c1694 LA |
250 | break; |
251 | case UHI_lseek: | |
18639a28 | 252 | semihost_sys_lseek(cs, uhi_cb, gpr[4], gpr[5], gpr[6]); |
3b3c1694 LA |
253 | break; |
254 | case UHI_unlink: | |
18639a28 | 255 | semihost_sys_remove(cs, uhi_cb, gpr[4], 0); |
3b3c1694 LA |
256 | break; |
257 | case UHI_fstat: | |
18639a28 | 258 | semihost_sys_fstat(cs, uhi_fstat_cb, gpr[4], gpr[5]); |
3b3c1694 | 259 | break; |
18639a28 | 260 | |
3b3c1694 LA |
261 | case UHI_argc: |
262 | gpr[2] = semihosting_get_argc(); | |
263 | break; | |
264 | case UHI_argnlen: | |
3bb45bbc RH |
265 | { |
266 | const char *s = semihosting_get_arg(gpr[4]); | |
267 | gpr[2] = s ? strlen(s) : -1; | |
3b3c1694 | 268 | } |
3b3c1694 LA |
269 | break; |
270 | case UHI_argn: | |
3bb45bbc RH |
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; | |
3b3c1694 | 289 | } |
3b3c1694 | 290 | break; |
ea421060 | 291 | |
3b3c1694 | 292 | case UHI_plog: |
ea421060 RH |
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) { | |
b10ccec1 | 309 | unlock_user(p, addr, 0); |
ea421060 RH |
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); | |
b10ccec1 | 316 | unlock_user(p, addr, 0); |
ea421060 RH |
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); | |
8809baf4 PM |
325 | if (!p) { |
326 | report_fault(env); | |
327 | } | |
ea421060 RH |
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); | |
3b3c1694 | 335 | } |
3b3c1694 | 336 | break; |
ea421060 | 337 | |
3b3c1694 | 338 | case UHI_assert: |
412411b3 RH |
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 | ||
3b3c1694 | 356 | default: |
d53a3ed4 | 357 | error_report("Unknown UHI operation %d", op); |
3b3c1694 LA |
358 | abort(); |
359 | } | |
3b3c1694 LA |
360 | return; |
361 | } |