forked from sysprog21/semu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.h
189 lines (162 loc) · 4.68 KB
/
device.h
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#pragma once
#include "riscv.h"
#include "virtio.h"
/* RAM */
#define RAM_SIZE (512 * 1024 * 1024)
#define DTB_SIZE (1 * 1024 * 1024)
#define INITRD_SIZE (8 * 1024 * 1024)
void ram_read(vm_t *core,
uint32_t *mem,
const uint32_t addr,
const uint8_t width,
uint32_t *value);
void ram_write(vm_t *core,
uint32_t *mem,
const uint32_t addr,
const uint8_t width,
const uint32_t value);
/* PLIC */
typedef struct {
uint32_t masked;
uint32_t ip;
uint32_t ie;
/* state of input interrupt lines (level-triggered), set by environment */
uint32_t active;
} plic_state_t;
void plic_update_interrupts(vm_t *core, plic_state_t *plic);
void plic_read(vm_t *core,
plic_state_t *plic,
uint32_t addr,
uint8_t width,
uint32_t *value);
void plic_write(vm_t *core,
plic_state_t *plic,
uint32_t addr,
uint8_t width,
uint32_t value);
/* UART */
#define IRQ_UART 1
#define IRQ_UART_BIT (1 << IRQ_UART)
typedef struct {
uint8_t dll, dlh; /**< divisor (ignored) */
uint8_t lcr; /**< UART config */
uint8_t ier; /**< interrupt config */
uint8_t current_int, pending_ints; /**< interrupt status */
/* other output signals, loopback mode (ignored) */
uint8_t mcr;
/* I/O handling */
int in_fd, out_fd;
bool in_ready;
} u8250_state_t;
void u8250_update_interrupts(u8250_state_t *uart);
void u8250_read(vm_t *core,
u8250_state_t *uart,
uint32_t addr,
uint8_t width,
uint32_t *value);
void u8250_write(vm_t *core,
u8250_state_t *uart,
uint32_t addr,
uint8_t width,
uint32_t value);
void u8250_check_ready(u8250_state_t *uart);
void capture_keyboard_input();
/* virtio-net */
#if SEMU_HAS(VIRTIONET)
#define IRQ_VNET 2
#define IRQ_VNET_BIT (1 << IRQ_VNET)
typedef struct {
uint32_t QueueNum;
uint32_t QueueDesc;
uint32_t QueueAvail;
uint32_t QueueUsed;
uint16_t last_avail;
bool ready;
bool fd_ready;
} virtio_net_queue_t;
typedef struct {
/* feature negotiation */
uint32_t DeviceFeaturesSel;
uint32_t DriverFeatures;
uint32_t DriverFeaturesSel;
/* queue config */
uint32_t QueueSel;
virtio_net_queue_t queues[2];
/* status */
uint32_t Status;
uint32_t InterruptStatus;
/* supplied by environment */
int tap_fd;
uint32_t *ram;
/* implementation-specific */
void *priv;
} virtio_net_state_t;
void virtio_net_read(vm_t *core,
virtio_net_state_t *vnet,
uint32_t addr,
uint8_t width,
uint32_t *value);
void virtio_net_write(vm_t *core,
virtio_net_state_t *vnet,
uint32_t addr,
uint8_t width,
uint32_t value);
void virtio_net_refresh_queue(virtio_net_state_t *vnet);
bool virtio_net_init(virtio_net_state_t *vnet);
#endif /* SEMU_HAS(VIRTIONET) */
/* VirtIO-Block */
#if SEMU_HAS(VIRTIOBLK)
#define IRQ_VBLK 3
#define IRQ_VBLK_BIT (1 << IRQ_VBLK)
typedef struct {
uint32_t QueueNum;
uint32_t QueueDesc;
uint32_t QueueAvail;
uint32_t QueueUsed;
uint16_t last_avail;
bool ready;
} virtio_blk_queue_t;
typedef struct {
/* feature negotiation */
uint32_t DeviceFeaturesSel;
uint32_t DriverFeatures;
uint32_t DriverFeaturesSel;
/* queue config */
uint32_t QueueSel;
virtio_blk_queue_t queues[2];
/* status */
uint32_t Status;
uint32_t InterruptStatus;
/* supplied by environment */
uint32_t *ram;
uint32_t *disk;
/* implementation-specific */
void *priv;
} virtio_blk_state_t;
void virtio_blk_read(vm_t *vm,
virtio_blk_state_t *vblk,
uint32_t addr,
uint8_t width,
uint32_t *value);
void virtio_blk_write(vm_t *vm,
virtio_blk_state_t *vblk,
uint32_t addr,
uint8_t width,
uint32_t value);
uint32_t *virtio_blk_init(virtio_blk_state_t *vblk, char *disk_file);
#endif /* SEMU_HAS(VIRTIOBLK) */
/* memory mapping */
typedef struct {
bool stopped;
uint32_t *ram;
uint32_t *disk;
plic_state_t plic;
u8250_state_t uart;
#if SEMU_HAS(VIRTIONET)
virtio_net_state_t vnet;
#endif
#if SEMU_HAS(VIRTIOBLK)
virtio_blk_state_t vblk;
#endif
uint32_t timer_lo, timer_hi;
} emu_state_t;