aboutsummaryrefslogtreecommitdiff
path: root/include/semihosting/guestfd.h
blob: 3d426fedab39060a600b76b4b8a01b4f6b795a8e (plain)
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
/*
 * Hosted file support for semihosting syscalls.
 *
 * Copyright (c) 2005, 2007 CodeSourcery.
 * Copyright (c) 2019 Linaro
 * Copyright © 2020 by Keith Packard <keithp@keithp.com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#ifndef SEMIHOSTING_GUESTFD_H
#define SEMIHOSTING_GUESTFD_H

typedef enum GuestFDType {
    GuestFDUnused = 0,
    GuestFDHost,
    GuestFDGDB,
    GuestFDStatic,
    GuestFDConsole,
} GuestFDType;

/*
 * Guest file descriptors are integer indexes into an array of
 * these structures (we will dynamically resize as necessary).
 */
typedef struct GuestFD {
    GuestFDType type;
    union {
        int hostfd;
        struct {
            const uint8_t *data;
            size_t len;
            size_t off;
        } staticfile;
    };
} GuestFD;

/*
 * For ARM semihosting, we have a separate structure for routing
 * data for the console which is outside the guest fd address space.
 */
extern GuestFD console_in_gf;
extern GuestFD console_out_gf;

/**
 * alloc_guestfd:
 *
 * Allocate an unused GuestFD index.  The associated guestfd index
 * will still be GuestFDUnused until it is initialized.
 */
int alloc_guestfd(void);

/**
 * dealloc_guestfd:
 * @guestfd: GuestFD index
 *
 * Deallocate a GuestFD index.  The associated GuestFD structure
 * will be recycled for a subsequent allocation.
 */
void dealloc_guestfd(int guestfd);

/**
 * get_guestfd:
 * @guestfd: GuestFD index
 *
 * Return the GuestFD structure associated with an initialized @guestfd,
 * or NULL if it has not been allocated, or hasn't been initialized.
 */
GuestFD *get_guestfd(int guestfd);

/**
 * associate_guestfd:
 * @guestfd: GuestFD index
 * @hostfd: host file descriptor
 *
 * Initialize the GuestFD for @guestfd to GuestFDHost using @hostfd.
 */
void associate_guestfd(int guestfd, int hostfd);

/**
 * staticfile_guestfd:
 * @guestfd: GuestFD index
 * @data: data to be read
 * @len: length of @data
 *
 * Initialize the GuestFD for @guestfd to GuestFDStatic.
 * The @len bytes at @data will be returned to the guest on reads.
 */
void staticfile_guestfd(int guestfd, const uint8_t *data, size_t len);

#endif /* SEMIHOSTING_GUESTFD_H */