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