2 * Hosted file support for semihosting syscalls.
4 * Copyright (c) 2005, 2007 CodeSourcery.
5 * Copyright (c) 2019 Linaro
6 * Copyright © 2020 by Keith Packard <keithp@keithp.com>
8 * SPDX-License-Identifier: GPL-2.0-or-later
11 #include "qemu/osdep.h"
12 #include "gdbstub/syscalls.h"
13 #include "semihosting/semihost.h"
14 #include "semihosting/guestfd.h"
15 #ifdef CONFIG_USER_ONLY
18 #include "semihosting/uaccess.h"
19 #include CONFIG_DEVICES
22 static GArray
*guestfd_array
;
24 #ifdef CONFIG_ARM_COMPATIBLE_SEMIHOSTING
25 GuestFD console_in_gf
;
26 GuestFD console_out_gf
;
29 void qemu_semihosting_guestfd_init(void)
31 /* New entries zero-initialized, i.e. type GuestFDUnused */
32 guestfd_array
= g_array_new(FALSE
, TRUE
, sizeof(GuestFD
));
34 #ifdef CONFIG_ARM_COMPATIBLE_SEMIHOSTING
35 /* For ARM-compat, the console is in a separate namespace. */
36 if (use_gdb_syscalls()) {
37 console_in_gf
.type
= GuestFDGDB
;
38 console_in_gf
.hostfd
= 0;
39 console_out_gf
.type
= GuestFDGDB
;
40 console_out_gf
.hostfd
= 2;
42 console_in_gf
.type
= GuestFDConsole
;
43 console_out_gf
.type
= GuestFDConsole
;
46 /* Otherwise, the stdio file descriptors apply. */
47 guestfd_array
= g_array_set_size(guestfd_array
, 3);
48 #ifndef CONFIG_USER_ONLY
49 if (!use_gdb_syscalls()) {
50 GuestFD
*gf
= &g_array_index(guestfd_array
, GuestFD
, 0);
51 gf
[0].type
= GuestFDConsole
;
52 gf
[1].type
= GuestFDConsole
;
53 gf
[2].type
= GuestFDConsole
;
57 associate_guestfd(0, 0);
58 associate_guestfd(1, 1);
59 associate_guestfd(2, 2);
64 * Allocate a new guest file descriptor and return it; if we
65 * couldn't allocate a new fd then return -1.
66 * This is a fairly simplistic implementation because we don't
67 * expect that most semihosting guest programs will make very
68 * heavy use of opening and closing fds.
70 int alloc_guestfd(void)
74 /* SYS_OPEN should return nonzero handle on success. Start guestfd from 1 */
75 for (i
= 1; i
< guestfd_array
->len
; i
++) {
76 GuestFD
*gf
= &g_array_index(guestfd_array
, GuestFD
, i
);
78 if (gf
->type
== GuestFDUnused
) {
83 /* All elements already in use: expand the array */
84 g_array_set_size(guestfd_array
, i
+ 1);
88 static void do_dealloc_guestfd(GuestFD
*gf
)
90 gf
->type
= GuestFDUnused
;
94 * Look up the guestfd in the data structure; return NULL
95 * for out of bounds, but don't check whether the slot is unused.
96 * This is used internally by the other guestfd functions.
98 static GuestFD
*do_get_guestfd(int guestfd
)
100 if (guestfd
< 0 || guestfd
>= guestfd_array
->len
) {
104 return &g_array_index(guestfd_array
, GuestFD
, guestfd
);
108 * Given a guest file descriptor, get the associated struct.
109 * If the fd is not valid, return NULL. This is the function
110 * used by the various semihosting calls to validate a handle
112 * Note: calling alloc_guestfd() or dealloc_guestfd() will
113 * invalidate any GuestFD* obtained by calling this function.
115 GuestFD
*get_guestfd(int guestfd
)
117 GuestFD
*gf
= do_get_guestfd(guestfd
);
119 if (!gf
|| gf
->type
== GuestFDUnused
) {
126 * Associate the specified guest fd (which must have been
127 * allocated via alloc_fd() and not previously used) with
128 * the specified host/gdb fd.
130 void associate_guestfd(int guestfd
, int hostfd
)
132 GuestFD
*gf
= do_get_guestfd(guestfd
);
135 gf
->type
= use_gdb_syscalls() ? GuestFDGDB
: GuestFDHost
;
139 void staticfile_guestfd(int guestfd
, const uint8_t *data
, size_t len
)
141 GuestFD
*gf
= do_get_guestfd(guestfd
);
144 gf
->type
= GuestFDStatic
;
145 gf
->staticfile
.data
= data
;
146 gf
->staticfile
.len
= len
;
147 gf
->staticfile
.off
= 0;
151 * Deallocate the specified guest file descriptor. This doesn't
152 * close the host fd, it merely undoes the work of alloc_fd().
154 void dealloc_guestfd(int guestfd
)
156 GuestFD
*gf
= do_get_guestfd(guestfd
);
159 do_dealloc_guestfd(gf
);