xref: /openbmc/qemu/include/semihosting/guestfd.h (revision f0984d40)
1 /*
2  * Hosted file support for semihosting syscalls.
3  *
4  * Copyright (c) 2005, 2007 CodeSourcery.
5  * Copyright (c) 2019 Linaro
6  * Copyright © 2020 by Keith Packard <keithp@keithp.com>
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10 
11 #ifndef SEMIHOSTING_GUESTFD_H
12 #define SEMIHOSTING_GUESTFD_H
13 
14 typedef enum GuestFDType {
15     GuestFDUnused = 0,
16     GuestFDHost,
17     GuestFDGDB,
18     GuestFDStatic,
19     GuestFDConsole,
20 } GuestFDType;
21 
22 /*
23  * Guest file descriptors are integer indexes into an array of
24  * these structures (we will dynamically resize as necessary).
25  */
26 typedef struct GuestFD {
27     GuestFDType type;
28     union {
29         int hostfd;
30         struct {
31             const uint8_t *data;
32             size_t len;
33             size_t off;
34         } staticfile;
35     };
36 } GuestFD;
37 
38 /*
39  * For ARM semihosting, we have a separate structure for routing
40  * data for the console which is outside the guest fd address space.
41  */
42 extern GuestFD console_in_gf;
43 extern GuestFD console_out_gf;
44 
45 /**
46  * alloc_guestfd:
47  *
48  * Allocate an unused GuestFD index.  The associated guestfd index
49  * will still be GuestFDUnused until it is initialized.
50  */
51 int alloc_guestfd(void);
52 
53 /**
54  * dealloc_guestfd:
55  * @guestfd: GuestFD index
56  *
57  * Deallocate a GuestFD index.  The associated GuestFD structure
58  * will be recycled for a subsequent allocation.
59  */
60 void dealloc_guestfd(int guestfd);
61 
62 /**
63  * get_guestfd:
64  * @guestfd: GuestFD index
65  *
66  * Return the GuestFD structure associated with an initialized @guestfd,
67  * or NULL if it has not been allocated, or hasn't been initialized.
68  */
69 GuestFD *get_guestfd(int guestfd);
70 
71 /**
72  * associate_guestfd:
73  * @guestfd: GuestFD index
74  * @hostfd: host file descriptor
75  *
76  * Initialize the GuestFD for @guestfd to GuestFDHost using @hostfd.
77  */
78 void associate_guestfd(int guestfd, int hostfd);
79 
80 /**
81  * staticfile_guestfd:
82  * @guestfd: GuestFD index
83  * @data: data to be read
84  * @len: length of @data
85  *
86  * Initialize the GuestFD for @guestfd to GuestFDStatic.
87  * The @len bytes at @data will be returned to the guest on reads.
88  */
89 void staticfile_guestfd(int guestfd, const uint8_t *data, size_t len);
90 
91 #endif /* SEMIHOSTING_GUESTFD_H */
92