-
Notifications
You must be signed in to change notification settings - Fork 62
/
virtio.h
146 lines (116 loc) · 4.59 KB
/
virtio.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
/*
* VIRTIO driver
*
* Copyright (c) 2016 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef VIRTIO_H
#define VIRTIO_H
#include <sys/select.h>
#include "iomem.h"
#include "pci.h"
#define VIRTIO_PAGE_SIZE 4096
#if defined(EMSCRIPTEN)
#define VIRTIO_ADDR_BITS 32
#else
#define VIRTIO_ADDR_BITS 64
#endif
#if VIRTIO_ADDR_BITS == 64
typedef uint64_t virtio_phys_addr_t;
#else
typedef uint32_t virtio_phys_addr_t;
#endif
typedef struct {
/* PCI only: */
PCIBus *pci_bus;
/* MMIO only: */
PhysMemoryMap *mem_map;
uint64_t addr;
IRQSignal *irq;
} VIRTIOBusDef;
typedef struct VIRTIODevice VIRTIODevice;
#define VIRTIO_DEBUG_IO (1 << 0)
#define VIRTIO_DEBUG_9P (1 << 1)
void virtio_set_debug(VIRTIODevice *s, int debug_flags);
/* block device */
typedef void BlockDeviceCompletionFunc(void *opaque, int ret);
typedef struct BlockDevice BlockDevice;
struct BlockDevice {
int64_t (*get_sector_count)(BlockDevice *bs);
int (*read_async)(BlockDevice *bs,
uint64_t sector_num, uint8_t *buf, int n,
BlockDeviceCompletionFunc *cb, void *opaque);
int (*write_async)(BlockDevice *bs,
uint64_t sector_num, const uint8_t *buf, int n,
BlockDeviceCompletionFunc *cb, void *opaque);
void *opaque;
};
VIRTIODevice *virtio_block_init(VIRTIOBusDef *bus, BlockDevice *bs);
/* network device */
typedef struct EthernetDevice EthernetDevice;
struct EthernetDevice {
uint8_t mac_addr[6]; /* mac address of the interface */
void (*write_packet)(EthernetDevice *net,
const uint8_t *buf, int len);
void *opaque;
#if !defined(EMSCRIPTEN)
void (*select_fill)(EthernetDevice *net, int *pfd_max,
fd_set *rfds, fd_set *wfds, fd_set *efds,
int *pdelay);
void (*select_poll)(EthernetDevice *net,
fd_set *rfds, fd_set *wfds, fd_set *efds,
int select_ret);
#endif
/* the following is set by the device */
void *device_opaque;
BOOL (*device_can_write_packet)(EthernetDevice *net);
void (*device_write_packet)(EthernetDevice *net,
const uint8_t *buf, int len);
void (*device_set_carrier)(EthernetDevice *net, BOOL carrier_state);
};
VIRTIODevice *virtio_net_init(VIRTIOBusDef *bus, EthernetDevice *es);
/* console device */
typedef struct {
void *opaque;
void (*write_data)(void *opaque, const uint8_t *buf, int len);
int (*read_data)(void *opaque, uint8_t *buf, int len);
} CharacterDevice;
VIRTIODevice *virtio_console_init(VIRTIOBusDef *bus, CharacterDevice *cs);
BOOL virtio_console_can_write_data(VIRTIODevice *s);
int virtio_console_get_write_len(VIRTIODevice *s);
int virtio_console_write_data(VIRTIODevice *s, const uint8_t *buf, int buf_len);
void virtio_console_resize_event(VIRTIODevice *s, int width, int height);
/* input device */
typedef enum {
VIRTIO_INPUT_TYPE_KEYBOARD,
VIRTIO_INPUT_TYPE_MOUSE,
VIRTIO_INPUT_TYPE_TABLET,
} VirtioInputTypeEnum;
#define VIRTIO_INPUT_ABS_SCALE 32768
int virtio_input_send_key_event(VIRTIODevice *s, BOOL is_down,
uint16_t key_code);
int virtio_input_send_mouse_event(VIRTIODevice *s, int dx, int dy, int dz,
unsigned int buttons);
VIRTIODevice *virtio_input_init(VIRTIOBusDef *bus, VirtioInputTypeEnum type);
/* 9p filesystem device */
#include "fs.h"
VIRTIODevice *virtio_9p_init(VIRTIOBusDef *bus, FSDevice *fs,
const char *mount_tag);
#endif /* VIRTIO_H */