forked from includeos/IncludeOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtio_queue.cpp
More file actions
190 lines (152 loc) · 5.53 KB
/
Copy pathvirtio_queue.cpp
File metadata and controls
190 lines (152 loc) · 5.53 KB
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
190
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//#undef NO_DEBUG
#define DEBUG // Allow debug
#define DEBUG2
#include <os>
#include <virtio/virtio.hpp>
#include <hw/pci.hpp>
#if !defined(__MACH__)
#include <malloc.h>
#else
extern void *memalign(size_t, size_t);
#endif
#include <cstring>
#include <cassert>
/**
Virtio Queue class, nested inside Virtio.
*/
#define ALIGN(x) (((x) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
unsigned Virtio::Queue::virtq_size(unsigned int qsz)
{
return ALIGN(sizeof(virtq_desc)*qsz + sizeof(u16)*(3 + qsz))
+ ALIGN(sizeof(u16)*3 + sizeof(virtq_used_elem)*qsz);
}
void Virtio::Queue::init_queue(int size, char* buf)
{
// The buffer starts with is an array of queue descriptors (i.e. tokens)
_queue.desc = (virtq_desc*) buf;
debug("\t * Queue desc @ 0x%lx \n ",(long)_queue.desc);
// The available buffer starts right after the queue descriptors
_queue.avail = (virtq_avail*) &buf[size * sizeof(virtq_desc)];
debug("\t * Queue avail @ 0x%lx \n ",(long)_queue.avail);
// The used queue starts at the beginning of the next page
_queue.used = (virtq_used*) (((uintptr_t) &_queue.avail->ring[size] + sizeof(uint16_t) + PAGE_SIZE-1) & ~(PAGE_SIZE-1));
debug("\t * Queue used @ 0x%lx \n ",(long)_queue.used);
}
#include <timers>
/** Constructor */
Virtio::Queue::Queue(const std::string& name,
uint16_t size, uint16_t q_index, uint16_t iobase)
: qname(name), _size(size), _iobase(iobase),
_free_head(0), _num_added(0),_last_used_idx(0),_pci_index(q_index)
{
size_t total_bytes = virtq_size(size);
// Allocate page-aligned size and clear it
void* buffer = memalign(PAGE_SIZE, total_bytes);
if (! buffer)
panic("Virtio queue could not allocate aligned queue area");
memset(buffer, 0, total_bytes);
debug(">>> Virtio Queue %s of size %i (%u bytes) initializing \n",
qname.c_str(), _size, total_bytes);
init_queue(size, (char*) buffer);
// Chain buffers
debug("\t * Chaining buffers \n");
for (int i=0; i<size; i++) _queue.desc[i].next = i+1;
_queue.desc[size -1].next = 0;
debug(" >> Virtio Queue %s setup complete. \n", qname.c_str());
}
/** Ported more or less directly from SanOS. */
int Virtio::Queue::enqueue(gsl::span<Token> buffers)
{
debug ("<%s> Enqueuing %i tokens \n", qname.c_str(), buffers.size());
uint16_t last = _free_head;
uint16_t first = _free_head;
// Place each buffer in a token
for( auto buf : buffers ) {
debug ("%s: buf @ %p \n", qname.c_str(), buf.data());
// Set read / write flags
_queue.desc[_free_head].flags =
buf.direction() ? VIRTQ_DESC_F_NEXT : VIRTQ_DESC_F_NEXT | VIRTQ_DESC_F_WRITE;
// Assign raw buffer
_queue.desc[_free_head].addr = (uint64_t) buf.data();
_queue.desc[_free_head].len = buf.size();
last = _free_head;
_free_head = _queue.desc[_free_head].next;
}
_desc_in_flight += buffers.size();
Ensures(_desc_in_flight <= size());
// No continue on last buffer
_queue.desc[last].flags &= ~VIRTQ_DESC_F_NEXT;
// Place the head of this current chain in the avail ring
uint16_t avail_index = (_queue.avail->idx + _num_added) % _size;
// we added a token
_num_added++;
_queue.avail->ring[avail_index] = first;
debug("<%s> avail_index: %u size: %u, free_head %u num free: %u\n",
qname.c_str(), avail_index, size(), _free_head, num_free());
return buffers.size();
}
void Virtio::Queue::release(uint32_t head)
{
// Mark queue element "head" as free (the whole token chain)
uint32_t i = head;
_desc_in_flight --;
while (_queue.desc[i].flags & VIRTQ_DESC_F_NEXT)
{
i = _queue.desc[i].next;
_desc_in_flight --;
}
// Add buffers back to free list
_queue.desc[i].next = _free_head;
_free_head = head;
debug("<%s> Descriptors in flight: %i \n", qname.c_str(), _desc_in_flight);
}
Virtio::Token Virtio::Queue::dequeue()
{
debug("<%s> Dequeueing last_used index %i ", qname.c_str(), _last_used_idx);
// Get next completed buffer
auto& e = _queue.used->ring[_last_used_idx % _size];
debug("<%s> Releasing token @%p, nr. %i Len: %i\n", qname.c_str(), &e, e.id, e.len);
// Release buffer
release(e.id);
_last_used_idx++;
// return token:
return {{(uint8_t*) _queue.desc[e.id].addr, e.len }, Token::IN};
}
void Virtio::Queue::disable_interrupts() {
_queue.avail->flags |= (1 << VIRTQ_AVAIL_F_NO_INTERRUPT);
}
void Virtio::Queue::enable_interrupts() {
_queue.avail->flags &= ~(1 << VIRTQ_AVAIL_F_NO_INTERRUPT);
}
void Virtio::Queue::kick()
{
#if defined(ARCH_x86)
update_avail_idx();
// Std. §3.2.1 pt. 4
__arch_hw_barrier();
if (!(_queue.used->flags & VIRTQ_USED_F_NO_NOTIFY)){
debug("<%s> Kicking virtio. Iobase 0x%x \n", qname.c_str(), _iobase);
hw::outpw(_iobase + VIRTIO_PCI_QUEUE_NOTIFY , _pci_index);
}else{
debug("<%s> Virtio device says we can't kick!\n", qname.c_str());
}
#else
#warning "kick() not implemented for selected arch"
#endif
}