xref: /openbmc/qemu/include/semihosting/guestfd.h (revision ad4c7f529a279685da84297773b4ec8080153c2d)
11c6ff720SRichard Henderson /*
21c6ff720SRichard Henderson  * Hosted file support for semihosting syscalls.
31c6ff720SRichard Henderson  *
41c6ff720SRichard Henderson  * Copyright (c) 2005, 2007 CodeSourcery.
51c6ff720SRichard Henderson  * Copyright (c) 2019 Linaro
61c6ff720SRichard Henderson  * Copyright © 2020 by Keith Packard <keithp@keithp.com>
71c6ff720SRichard Henderson  *
81c6ff720SRichard Henderson  * SPDX-License-Identifier: GPL-2.0-or-later
91c6ff720SRichard Henderson  */
101c6ff720SRichard Henderson 
111c6ff720SRichard Henderson #ifndef SEMIHOSTING_GUESTFD_H
121c6ff720SRichard Henderson #define SEMIHOSTING_GUESTFD_H
131c6ff720SRichard Henderson 
141c6ff720SRichard Henderson typedef enum GuestFDType {
151c6ff720SRichard Henderson     GuestFDUnused = 0,
16008e1475SRichard Henderson     GuestFDHost,
17008e1475SRichard Henderson     GuestFDGDB,
18008e1475SRichard Henderson     GuestFDStatic,
19008e1475SRichard Henderson     GuestFDConsole,
201c6ff720SRichard Henderson } GuestFDType;
211c6ff720SRichard Henderson 
221c6ff720SRichard Henderson /*
231c6ff720SRichard Henderson  * Guest file descriptors are integer indexes into an array of
241c6ff720SRichard Henderson  * these structures (we will dynamically resize as necessary).
251c6ff720SRichard Henderson  */
261c6ff720SRichard Henderson typedef struct GuestFD {
271c6ff720SRichard Henderson     GuestFDType type;
281c6ff720SRichard Henderson     union {
291c6ff720SRichard Henderson         int hostfd;
301c6ff720SRichard Henderson         struct {
311c6ff720SRichard Henderson             const uint8_t *data;
321c6ff720SRichard Henderson             size_t len;
331c6ff720SRichard Henderson             size_t off;
341c6ff720SRichard Henderson         } staticfile;
351c6ff720SRichard Henderson     };
361c6ff720SRichard Henderson } GuestFD;
371c6ff720SRichard Henderson 
38*e4a4aaa5SRichard Henderson /*
39*e4a4aaa5SRichard Henderson  * For ARM semihosting, we have a separate structure for routing
40*e4a4aaa5SRichard Henderson  * data for the console which is outside the guest fd address space.
41*e4a4aaa5SRichard Henderson  */
42*e4a4aaa5SRichard Henderson extern GuestFD console_in_gf;
43*e4a4aaa5SRichard Henderson extern GuestFD console_out_gf;
44*e4a4aaa5SRichard Henderson 
451c6ff720SRichard Henderson /**
461c6ff720SRichard Henderson  * alloc_guestfd:
471c6ff720SRichard Henderson  *
481c6ff720SRichard Henderson  * Allocate an unused GuestFD index.  The associated guestfd index
491c6ff720SRichard Henderson  * will still be GuestFDUnused until it is initialized.
501c6ff720SRichard Henderson  */
511c6ff720SRichard Henderson int alloc_guestfd(void);
521c6ff720SRichard Henderson 
531c6ff720SRichard Henderson /**
541c6ff720SRichard Henderson  * dealloc_guestfd:
551c6ff720SRichard Henderson  * @guestfd: GuestFD index
561c6ff720SRichard Henderson  *
571c6ff720SRichard Henderson  * Deallocate a GuestFD index.  The associated GuestFD structure
581c6ff720SRichard Henderson  * will be recycled for a subsequent allocation.
591c6ff720SRichard Henderson  */
601c6ff720SRichard Henderson void dealloc_guestfd(int guestfd);
611c6ff720SRichard Henderson 
621c6ff720SRichard Henderson /**
631c6ff720SRichard Henderson  * get_guestfd:
641c6ff720SRichard Henderson  * @guestfd: GuestFD index
651c6ff720SRichard Henderson  *
661c6ff720SRichard Henderson  * Return the GuestFD structure associated with an initialized @guestfd,
671c6ff720SRichard Henderson  * or NULL if it has not been allocated, or hasn't been initialized.
681c6ff720SRichard Henderson  */
691c6ff720SRichard Henderson GuestFD *get_guestfd(int guestfd);
701c6ff720SRichard Henderson 
711c6ff720SRichard Henderson /**
721c6ff720SRichard Henderson  * associate_guestfd:
731c6ff720SRichard Henderson  * @guestfd: GuestFD index
741c6ff720SRichard Henderson  * @hostfd: host file descriptor
751c6ff720SRichard Henderson  *
761c6ff720SRichard Henderson  * Initialize the GuestFD for @guestfd to GuestFDHost using @hostfd.
771c6ff720SRichard Henderson  */
781c6ff720SRichard Henderson void associate_guestfd(int guestfd, int hostfd);
791c6ff720SRichard Henderson 
801c6ff720SRichard Henderson /**
811c6ff720SRichard Henderson  * staticfile_guestfd:
821c6ff720SRichard Henderson  * @guestfd: GuestFD index
831c6ff720SRichard Henderson  * @data: data to be read
841c6ff720SRichard Henderson  * @len: length of @data
851c6ff720SRichard Henderson  *
861c6ff720SRichard Henderson  * Initialize the GuestFD for @guestfd to GuestFDStatic.
871c6ff720SRichard Henderson  * The @len bytes at @data will be returned to the guest on reads.
881c6ff720SRichard Henderson  */
891c6ff720SRichard Henderson void staticfile_guestfd(int guestfd, const uint8_t *data, size_t len);
901c6ff720SRichard Henderson 
911c6ff720SRichard Henderson #endif /* SEMIHOSTING_GUESTFD_H */
92