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
|
/*
* Syscall implementations for semihosting.
*
* Copyright (c) 2022 Linaro
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "qemu/osdep.h"
#include "exec/gdbstub.h"
#include "semihosting/guestfd.h"
#include "semihosting/syscalls.h"
#ifdef CONFIG_USER_ONLY
#include "qemu.h"
#else
#include "semihosting/softmmu-uaccess.h"
#endif
/*
* Validate or compute the length of the string (including terminator).
*/
static int validate_strlen(CPUState *cs, target_ulong str, target_ulong tlen)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
char c;
if (tlen == 0) {
ssize_t slen = target_strlen(str);
if (slen < 0) {
return -EFAULT;
}
if (slen >= INT32_MAX) {
return -ENAMETOOLONG;
}
return slen + 1;
}
if (tlen > INT32_MAX) {
return -ENAMETOOLONG;
}
if (get_user_u8(c, str + tlen - 1)) {
return -EFAULT;
}
if (c != 0) {
return -EINVAL;
}
return tlen;
}
static int validate_lock_user_string(char **pstr, CPUState *cs,
target_ulong tstr, target_ulong tlen)
{
int ret = validate_strlen(cs, tstr, tlen);
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
char *str = NULL;
if (ret > 0) {
str = lock_user(VERIFY_READ, tstr, ret, true);
ret = str ? 0 : -EFAULT;
}
*pstr = str;
return ret;
}
/*
* GDB semihosting syscall implementations.
*/
static gdb_syscall_complete_cb gdb_open_complete;
static void gdb_open_cb(CPUState *cs, target_ulong ret, target_ulong err)
{
if (!err) {
int guestfd = alloc_guestfd();
associate_guestfd(guestfd, ret);
ret = guestfd;
}
gdb_open_complete(cs, ret, err);
}
static void gdb_open(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong fname, target_ulong fname_len,
int gdb_flags, int mode)
{
int len = validate_strlen(cs, fname, fname_len);
if (len < 0) {
complete(cs, -1, -len);
return;
}
gdb_open_complete = complete;
gdb_do_syscall(gdb_open_cb, "open,%s,%x,%x",
fname, len, (target_ulong)gdb_flags, (target_ulong)mode);
}
static void gdb_close(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf)
{
gdb_do_syscall(complete, "close,%x", (target_ulong)gf->hostfd);
}
static void gdb_read(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len)
{
gdb_do_syscall(complete, "read,%x,%x,%x",
(target_ulong)gf->hostfd, buf, len);
}
static void gdb_write(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len)
{
gdb_do_syscall(complete, "write,%x,%x,%x",
(target_ulong)gf->hostfd, buf, len);
}
/*
* Host semihosting syscall implementations.
*/
static void host_open(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong fname, target_ulong fname_len,
int gdb_flags, int mode)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
char *p;
int ret, host_flags;
ret = validate_lock_user_string(&p, cs, fname, fname_len);
if (ret < 0) {
complete(cs, -1, -ret);
return;
}
if (gdb_flags & GDB_O_WRONLY) {
host_flags = O_WRONLY;
} else if (gdb_flags & GDB_O_RDWR) {
host_flags = O_RDWR;
} else {
host_flags = O_RDONLY;
}
if (gdb_flags & GDB_O_CREAT) {
host_flags |= O_CREAT;
}
if (gdb_flags & GDB_O_TRUNC) {
host_flags |= O_TRUNC;
}
if (gdb_flags & GDB_O_EXCL) {
host_flags |= O_EXCL;
}
ret = open(p, host_flags, mode);
if (ret < 0) {
complete(cs, -1, errno);
} else {
int guestfd = alloc_guestfd();
associate_guestfd(guestfd, ret);
complete(cs, guestfd, 0);
}
unlock_user(p, fname, 0);
}
static void host_close(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf)
{
/*
* Only close the underlying host fd if it's one we opened on behalf
* of the guest in SYS_OPEN.
*/
if (gf->hostfd != STDIN_FILENO &&
gf->hostfd != STDOUT_FILENO &&
gf->hostfd != STDERR_FILENO &&
close(gf->hostfd) < 0) {
complete(cs, -1, errno);
} else {
complete(cs, 0, 0);
}
}
static void host_read(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
void *ptr = lock_user(VERIFY_WRITE, buf, len, 0);
ssize_t ret;
if (!ptr) {
complete(cs, -1, EFAULT);
return;
}
do {
ret = read(gf->hostfd, ptr, len);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
complete(cs, -1, errno);
unlock_user(ptr, buf, 0);
} else {
complete(cs, ret, 0);
unlock_user(ptr, buf, ret);
}
}
static void host_write(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
void *ptr = lock_user(VERIFY_READ, buf, len, 1);
ssize_t ret;
if (!ptr) {
complete(cs, -1, EFAULT);
return;
}
ret = write(gf->hostfd, ptr, len);
complete(cs, ret, ret == -1 ? errno : 0);
unlock_user(ptr, buf, 0);
}
/*
* Static file semihosting syscall implementations.
*/
static void staticfile_read(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
target_ulong rest = gf->staticfile.len - gf->staticfile.off;
void *ptr;
if (len > rest) {
len = rest;
}
ptr = lock_user(VERIFY_WRITE, buf, len, 0);
if (!ptr) {
complete(cs, -1, EFAULT);
return;
}
memcpy(ptr, gf->staticfile.data + gf->staticfile.off, len);
gf->staticfile.off += len;
complete(cs, len, 0);
unlock_user(ptr, buf, len);
}
/*
* Syscall entry points.
*/
void semihost_sys_open(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong fname, target_ulong fname_len,
int gdb_flags, int mode)
{
if (use_gdb_syscalls()) {
gdb_open(cs, complete, fname, fname_len, gdb_flags, mode);
} else {
host_open(cs, complete, fname, fname_len, gdb_flags, mode);
}
}
void semihost_sys_close(CPUState *cs, gdb_syscall_complete_cb complete, int fd)
{
GuestFD *gf = get_guestfd(fd);
if (!gf) {
complete(cs, -1, EBADF);
return;
}
switch (gf->type) {
case GuestFDGDB:
gdb_close(cs, complete, gf);
break;
case GuestFDHost:
host_close(cs, complete, gf);
break;
case GuestFDStatic:
complete(cs, 0, 0);
break;
default:
g_assert_not_reached();
}
dealloc_guestfd(fd);
}
void semihost_sys_read_gf(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len)
{
switch (gf->type) {
case GuestFDGDB:
gdb_read(cs, complete, gf, buf, len);
break;
case GuestFDHost:
host_read(cs, complete, gf, buf, len);
break;
case GuestFDStatic:
staticfile_read(cs, complete, gf, buf, len);
break;
default:
g_assert_not_reached();
}
}
void semihost_sys_read(CPUState *cs, gdb_syscall_complete_cb complete,
int fd, target_ulong buf, target_ulong len)
{
GuestFD *gf = get_guestfd(fd);
if (gf) {
semihost_sys_read_gf(cs, complete, gf, buf, len);
} else {
complete(cs, -1, EBADF);
}
}
void semihost_sys_write_gf(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len)
{
switch (gf->type) {
case GuestFDGDB:
gdb_write(cs, complete, gf, buf, len);
break;
case GuestFDHost:
host_write(cs, complete, gf, buf, len);
break;
case GuestFDStatic:
/* Static files are never open for writing: EBADF. */
complete(cs, -1, EBADF);
break;
default:
g_assert_not_reached();
}
}
void semihost_sys_write(CPUState *cs, gdb_syscall_complete_cb complete,
int fd, target_ulong buf, target_ulong len)
{
GuestFD *gf = get_guestfd(fd);
if (gf) {
semihost_sys_write_gf(cs, complete, gf, buf, len);
} else {
complete(cs, -1, EBADF);
}
}
|