blob: 3d426fedab39060a600b76b4b8a01b4f6b795a8e [file] [log] [blame]
Richard Henderson1c6ff722022-04-27 21:38:02 -07001/*
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
14typedef enum GuestFDType {
15 GuestFDUnused = 0,
Richard Henderson008e1472022-05-01 13:11:45 -070016 GuestFDHost,
17 GuestFDGDB,
18 GuestFDStatic,
19 GuestFDConsole,
Richard Henderson1c6ff722022-04-27 21:38:02 -070020} GuestFDType;
21
22/*
23 * Guest file descriptors are integer indexes into an array of
24 * these structures (we will dynamically resize as necessary).
25 */
26typedef 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
Richard Hendersone4a4aaa2022-05-01 17:21:00 -070038/*
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 */
42extern GuestFD console_in_gf;
43extern GuestFD console_out_gf;
44
Richard Henderson1c6ff722022-04-27 21:38:02 -070045/**
46 * alloc_guestfd:
47 *
48 * Allocate an unused GuestFD index. The associated guestfd index
49 * will still be GuestFDUnused until it is initialized.
50 */
51int 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 */
60void 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 */
69GuestFD *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 */
78void 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 */
89void staticfile_guestfd(int guestfd, const uint8_t *data, size_t len);
90
91#endif /* SEMIHOSTING_GUESTFD_H */