base.cc revision 10602
1/* 2 * Copyright (c) 2014 ARM Limited 3 * All rights reserved 4 * 5 * The license below extends only to copyright in the software and shall 6 * not be construed as granting a license to any other intellectual 7 * property including but not limited to intellectual property relating 8 * to a hardware implementation of the functionality of the software 9 * licensed hereunder. You may use the software subject to the license 10 * terms below provided that you ensure that this notice is replicated 11 * unmodified and in its entirety in all distributions of the software, 12 * modified or unmodified, in source code or in binary form. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions are 16 * met: redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer; 18 * redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution; 21 * neither the name of the copyright holders nor the names of its 22 * contributors may be used to endorse or promote products derived from 23 * this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * 37 * Authors: Andreas Sandberg 38 */ 39 40#include "debug/VIO.hh" 41#include "dev/virtio/base.hh" 42#include "params/VirtIODeviceBase.hh" 43 44VirtDescriptor::VirtDescriptor(PortProxy &_memProxy, VirtQueue &_queue, 45 Index descIndex) 46 : memProxy(&_memProxy), queue(&_queue), _index(descIndex), 47 desc{0, 0, 0, 0} 48{ 49} 50 51VirtDescriptor::VirtDescriptor(VirtDescriptor &&other) noexcept 52{ 53 *this = std::forward<VirtDescriptor>(other); 54} 55 56VirtDescriptor::~VirtDescriptor() noexcept 57{ 58} 59 60VirtDescriptor & 61VirtDescriptor::operator=(VirtDescriptor &&rhs) noexcept 62{ 63 memProxy = std::move(rhs.memProxy); 64 queue = std::move(rhs.queue); 65 _index = std::move(rhs._index); 66 desc = std::move(rhs.desc); 67 68 return *this; 69} 70 71void 72VirtDescriptor::update() 73{ 74 const Addr vq_addr(queue->getAddress()); 75 // Check if the queue has been initialized yet 76 if (vq_addr == 0) 77 return; 78 79 assert(_index < queue->getSize()); 80 const Addr desc_addr(vq_addr + sizeof(desc) * _index); 81 vring_desc guest_desc; 82 memProxy->readBlob(desc_addr, (uint8_t *)&guest_desc, sizeof(guest_desc)); 83 desc = vtoh_legacy(guest_desc); 84 DPRINTF(VIO, 85 "VirtDescriptor(%i): Addr: 0x%x, Len: %i, Flags: 0x%x, " 86 "Next: 0x%x\n", 87 _index, desc.addr, desc.len, desc.flags, desc.next); 88} 89 90void 91VirtDescriptor::updateChain() 92{ 93 VirtDescriptor *desc(this); 94 do { 95 desc->update(); 96 } while((desc = desc->next()) != NULL && desc != this); 97 98 if (desc == this) 99 panic("Loop in descriptor chain!\n"); 100} 101 102void 103VirtDescriptor::dump() const 104{ 105 if (!DTRACE(VIO)) 106 return; 107 108 DPRINTF(VIO, "Descriptor[%i]: " 109 "Addr: 0x%x, Len: %i, Flags: 0x%x, Next: 0x%x\n", 110 _index, desc.addr, desc.len, desc.flags, desc.next); 111 112 if (isIncoming()) { 113 uint8_t data[desc.len]; 114 read(0, data, desc.len); 115 DDUMP(VIO, data, desc.len); 116 } 117} 118 119void 120VirtDescriptor::dumpChain() const 121{ 122 if (!DTRACE(VIO)) 123 return; 124 125 const VirtDescriptor *desc(this); 126 do { 127 desc->dump(); 128 } while((desc = desc->next()) != NULL); 129} 130 131VirtDescriptor * 132VirtDescriptor::next() const 133{ 134 if (hasNext()) { 135 return queue->getDescriptor(desc.next); 136 } else { 137 return NULL; 138 } 139} 140 141void 142VirtDescriptor::read(size_t offset, uint8_t *dst, size_t size) const 143{ 144 DPRINTF(VIO, "VirtDescriptor(%p, 0x%x, %i)::read: offset: %i, dst: 0x%x, size: %i\n", 145 this, desc.addr, desc.len, offset, (long)dst, size); 146 assert(size <= desc.len - offset); 147 if (!isIncoming()) 148 panic("Trying to read from outgoing buffer\n"); 149 150 memProxy->readBlob(desc.addr + offset, dst, size); 151} 152 153void 154VirtDescriptor::write(size_t offset, const uint8_t *src, size_t size) 155{ 156 DPRINTF(VIO, "VirtDescriptor(%p, 0x%x, %i)::write: offset: %i, src: 0x%x, size: %i\n", 157 this, desc.addr, desc.len, offset, (long)src, size); 158 assert(size <= desc.len - offset); 159 if (!isOutgoing()) 160 panic("Trying to write to incoming buffer\n"); 161 162 memProxy->writeBlob(desc.addr + offset, const_cast<uint8_t *>(src), size); 163} 164 165void 166VirtDescriptor::chainRead(size_t offset, uint8_t *dst, size_t size) const 167{ 168 const VirtDescriptor *desc(this); 169 const size_t full_size(size); 170 do { 171 if (offset < desc->size()) { 172 const size_t chunk_size(std::min(desc->size() - offset, size)); 173 desc->read(offset, dst, chunk_size); 174 dst += chunk_size; 175 size -= chunk_size; 176 offset = 0; 177 } else { 178 offset -= desc->size(); 179 } 180 } while((desc = desc->next()) != NULL && desc->isIncoming() && size > 0); 181 182 if (size != 0) { 183 panic("Failed to read %i bytes from chain of %i bytes @ offset %i\n", 184 full_size, chainSize(), offset); 185 } 186} 187 188void 189VirtDescriptor::chainWrite(size_t offset, const uint8_t *src, size_t size) 190{ 191 VirtDescriptor *desc(this); 192 const size_t full_size(size); 193 do { 194 if (offset < desc->size()) { 195 const size_t chunk_size(std::min(desc->size() - offset, size)); 196 desc->write(offset, src, chunk_size); 197 src += chunk_size; 198 size -= chunk_size; 199 offset = 0; 200 } else { 201 offset -= desc->size(); 202 } 203 } while((desc = desc->next()) != NULL && size > 0); 204 205 if (size != 0) { 206 panic("Failed to write %i bytes into chain of %i bytes @ offset %i\n", 207 full_size, chainSize(), offset); 208 } 209} 210 211size_t 212VirtDescriptor::chainSize() const 213{ 214 size_t size(0); 215 const VirtDescriptor *desc(this); 216 do { 217 size += desc->size(); 218 } while((desc = desc->next()) != NULL); 219 220 return size; 221} 222 223 224 225VirtQueue::VirtQueue(PortProxy &proxy, uint16_t size) 226 : _size(size), _address(0), memProxy(proxy), 227 avail(proxy, size), used(proxy, size), 228 _last_avail(0) 229{ 230 descriptors.reserve(_size); 231 for (int i = 0; i < _size; ++i) 232 descriptors.emplace_back(proxy, *this, i); 233} 234 235void 236VirtQueue::serialize(std::ostream &os) 237{ 238 SERIALIZE_SCALAR(_address); 239 SERIALIZE_SCALAR(_last_avail); 240} 241 242void 243VirtQueue::unserialize(Checkpoint *cp, const std::string §ion) 244{ 245 Addr addr_in; 246 247 paramIn(cp, section, "_address", addr_in); 248 UNSERIALIZE_SCALAR(_last_avail); 249 250 // Use the address setter to ensure that the ring buffer addresses 251 // are updated as well. 252 setAddress(addr_in); 253} 254 255void 256VirtQueue::setAddress(Addr address) 257{ 258 const Addr addr_avail(address + _size * sizeof(struct vring_desc)); 259 const Addr addr_avail_end(addr_avail + sizeof(struct vring_avail) + 260 _size * sizeof(uint16_t)); 261 const Addr addr_used((addr_avail_end + sizeof(uint16_t) + 262 (ALIGN_SIZE - 1)) & ~(ALIGN_SIZE - 1)); 263 _address = address; 264 avail.setAddress(addr_avail); 265 used.setAddress(addr_used); 266} 267 268VirtDescriptor * 269VirtQueue::consumeDescriptor() 270{ 271 avail.read(); 272 DPRINTF(VIO, "consumeDescriptor: _last_avail: %i, avail.idx: %i (->%i)\n", 273 _last_avail, avail.header.index, 274 avail.ring[_last_avail % used.ring.size()]); 275 if (_last_avail == avail.header.index) 276 return NULL; 277 278 VirtDescriptor::Index index(avail.ring[_last_avail % used.ring.size()]); 279 ++_last_avail; 280 281 VirtDescriptor *d(&descriptors[index]); 282 d->updateChain(); 283 284 return d; 285} 286 287void 288VirtQueue::produceDescriptor(VirtDescriptor *desc, uint32_t len) 289{ 290 used.readHeader(); 291 DPRINTF(VIO, "produceDescriptor: dscIdx: %i, len: %i, used.idx: %i\n", 292 desc->index(), len, used.header.index); 293 294 struct vring_used_elem &e(used.ring[used.header.index % used.ring.size()]); 295 e.id = desc->index(); 296 e.len = len; 297 used.header.index += 1; 298 used.write(); 299} 300 301void 302VirtQueue::dump() const 303{ 304 if (!DTRACE(VIO)) 305 return; 306 307 for (const VirtDescriptor &d : descriptors) 308 d.dump(); 309} 310 311void 312VirtQueue::onNotify() 313{ 314 DPRINTF(VIO, "onNotify\n"); 315 316 // Consume all pending descriptors from the input queue. 317 VirtDescriptor *d; 318 while((d = consumeDescriptor()) != NULL) 319 onNotifyDescriptor(d); 320} 321 322 323VirtIODeviceBase::VirtIODeviceBase(Params *params, DeviceId id, 324 size_t config_size, FeatureBits features) 325 : SimObject(params), 326 guestFeatures(0), 327 deviceId(id), configSize(config_size), deviceFeatures(features), 328 _deviceStatus(0), _queueSelect(0), 329 transKick(NULL) 330{ 331} 332 333 334VirtIODeviceBase::~VirtIODeviceBase() 335{ 336} 337 338void 339VirtIODeviceBase::serialize(std::ostream &os) 340{ 341 SERIALIZE_SCALAR(guestFeatures); 342 paramOut(os, "_deviceStatus", (uint8_t)_deviceStatus); 343 SERIALIZE_SCALAR(_queueSelect); 344 for (QueueID i = 0; i < _queues.size(); ++i) { 345 nameOut(os, csprintf("%s._queues.%i", name(), i)); 346 _queues[i]->serialize(os); 347 } 348} 349 350void 351VirtIODeviceBase::unserialize(Checkpoint *cp, const std::string §ion) 352{ 353 UNSERIALIZE_SCALAR(guestFeatures); 354 uint8_t status; 355 paramIn(cp, section, "_deviceStatus", status); 356 _deviceStatus = status; 357 UNSERIALIZE_SCALAR(_queueSelect); 358 for (QueueID i = 0; i < _queues.size(); ++i) 359 _queues[i]->unserialize(cp, csprintf("%s._queues.%i", section, i)); 360} 361 362void 363VirtIODeviceBase::reset() 364{ 365 _queueSelect = 0; 366 guestFeatures = 0; 367 _deviceStatus = 0; 368 369 for (QueueID i = 0; i < _queues.size(); ++i) 370 _queues[i]->setAddress(0); 371} 372 373void 374VirtIODeviceBase::onNotify(QueueID idx) 375{ 376 DPRINTF(VIO, "onNotify: idx: %i\n", idx); 377 if (idx >= _queues.size()) { 378 panic("Guest tried to notify queue (%i), but only %i " 379 "queues registered.\n", 380 idx, _queues.size()); 381 } 382 _queues[idx]->onNotify(); 383} 384 385void 386VirtIODeviceBase::setGuestFeatures(FeatureBits features) 387{ 388 DPRINTF(VIO, "Setting guest features: 0x%x\n", features); 389 if (~deviceFeatures & features) { 390 panic("Guest tried to enable unsupported features:\n" 391 "Device features: 0x%x\n" 392 "Requested features: 0x%x\n", 393 deviceFeatures, features); 394 } 395 guestFeatures = features; 396} 397 398 399void 400VirtIODeviceBase::setDeviceStatus(DeviceStatus status) 401{ 402 _deviceStatus = status; 403 DPRINTF(VIO, "ACK: %i, DRIVER: %i, DRIVER_OK: %i, FAILED: %i\n", 404 status.acknowledge, status.driver, status.driver_ok, status.failed); 405 if (status == 0) 406 reset(); 407} 408 409void 410VirtIODeviceBase::readConfig(PacketPtr pkt, Addr cfgOffset) 411{ 412 panic("Unhandled device config read (offset: 0x%x).\n", cfgOffset); 413} 414 415void 416VirtIODeviceBase::writeConfig(PacketPtr pkt, Addr cfgOffset) 417{ 418 panic("Unhandled device config write (offset: 0x%x).\n", cfgOffset); 419} 420 421void 422VirtIODeviceBase::readConfigBlob(PacketPtr pkt, Addr cfgOffset, const uint8_t *cfg) 423{ 424 const unsigned size(pkt->getSize()); 425 426 if (cfgOffset + size > configSize) 427 panic("Config read out of bounds.\n"); 428 429 pkt->makeResponse(); 430 pkt->setData(const_cast<uint8_t *>(cfg) + cfgOffset); 431} 432 433void 434VirtIODeviceBase::writeConfigBlob(PacketPtr pkt, Addr cfgOffset, uint8_t *cfg) 435{ 436 const unsigned size(pkt->getSize()); 437 438 if (cfgOffset + size > configSize) 439 panic("Config write out of bounds.\n"); 440 441 pkt->makeResponse(); 442 pkt->writeData((uint8_t *)cfg + cfgOffset); 443} 444 445 446const VirtQueue & 447VirtIODeviceBase::getCurrentQueue() const 448{ 449 if (_queueSelect >= _queues.size()) 450 panic("Guest tried to access non-existing VirtQueue (%i).\n", _queueSelect); 451 452 return *_queues[_queueSelect]; 453} 454 455VirtQueue & 456VirtIODeviceBase::getCurrentQueue() 457{ 458 if (_queueSelect >= _queues.size()) 459 panic("Guest tried to access non-existing VirtQueue (%i).\n", _queueSelect); 460 461 return *_queues[_queueSelect]; 462} 463 464void 465VirtIODeviceBase::setQueueAddress(uint32_t address) 466{ 467 getCurrentQueue().setAddress(address * VirtQueue::ALIGN_SIZE); 468} 469 470uint32_t 471VirtIODeviceBase::getQueueAddress() const 472{ 473 Addr address(getCurrentQueue().getAddress()); 474 assert(!(address & ((1 >> VirtQueue::ALIGN_BITS) - 1))); 475 return address >> VirtQueue::ALIGN_BITS; 476} 477 478void 479VirtIODeviceBase::registerQueue(VirtQueue &queue) 480{ 481 _queues.push_back(&queue); 482} 483