/*
+ * Copyright 2014 Google, Inc.
* Copyright (c) 2012 ARM Limited
* All rights reserved
*
return checkExtension(KVM_CAP_COALESCED_MMIO);
}
+int
+Kvm::capNumMemSlots() const
+{
+#ifdef KVM_CAP_NR_MEMSLOTS
+ return checkExtension(KVM_CAP_NR_MEMSLOTS);
+#else
+ return 0;
+#endif
+}
+
bool
Kvm::capOneReg() const
{
started(false),
nextVCPUID(0)
{
+ maxMemorySlot = kvm.capNumMemSlots();
+ /* If we couldn't determine how memory slots there are, guess 32. */
+ if (!maxMemorySlot)
+ maxMemorySlot = 32;
/* Setup the coalesced MMIO regions */
for (int i = 0; i < params->coalescedMMIO.size(); ++i)
coalesceMMIO(params->coalescedMMIO[i]);
DPRINTF(Kvm, "Mapping region: 0x%p -> 0x%llx [size: 0x%llx]\n",
pmem, range.start(), range.size());
- setUserMemoryRegion(slot, pmem, range, 0 /* flags */);
+ if (range.interleaved()) {
+ panic("Tried to map an interleaved memory range into "
+ "a KVM VM.\n");
+ }
+
+ const MemSlot slot = allocMemSlot(range.size());
+ setupMemSlot(slot, pmem, range.start(), 0/* flags */);
} else {
DPRINTF(Kvm, "Zero-region not mapped: [0x%llx]\n", range.start());
hack("KVM: Zero memory handled as IO\n");
}
}
+const KvmVM::MemSlot
+KvmVM::allocMemSlot(uint64_t size)
+{
+ if (!size)
+ panic("Memory slots must have non-zero size.\n");
+
+ std::vector<MemorySlot>::iterator pos;
+ for (pos = memorySlots.begin(); pos != memorySlots.end(); pos++) {
+ if (!pos->size) {
+ pos->size = size;
+ pos->active = false;
+ return pos->slot;
+ }
+ }
+
+ uint32_t nextSlot = memorySlots.size();
+ if (nextSlot > maxMemorySlot)
+ panic("Out of memory slots.\n");
+
+ MemorySlot slot;
+ slot.size = size;
+ slot.slot = nextSlot;
+ slot.active = false;
+
+ memorySlots.push_back(slot);
+ return MemSlot(slot.slot);
+}
+
void
-KvmVM::setUserMemoryRegion(uint32_t slot,
- void *host_addr, AddrRange guest_range,
- uint32_t flags)
+KvmVM::setupMemSlot(const KvmVM::MemSlot num, void *host_addr, Addr guest,
+ uint32_t flags)
+{
+ MemorySlot &slot = memorySlots.at(num.num);
+ slot.active = true;
+ setUserMemoryRegion(num.num, host_addr, guest, slot.size, flags);
+}
+
+void
+KvmVM::disableMemSlot(const KvmVM::MemSlot num)
{
- if (guest_range.interleaved())
- panic("Tried to map an interleaved memory range into a KVM VM.\n");
+ MemorySlot &slot = memorySlots.at(num.num);
+ if (slot.active)
+ setUserMemoryRegion(num.num, NULL, 0, 0, 0);
+ slot.active = false;
+}
- setUserMemoryRegion(slot, host_addr,
- guest_range.start(), guest_range.size(),
- flags);
+void
+KvmVM::freeMemSlot(const KvmVM::MemSlot num)
+{
+ disableMemSlot(num.num);
+ MemorySlot &slot = memorySlots.at(num.num);
+ slot.size = 0;
}
void
/*
+ * Copyright 2014 Google, Inc.
* Copyright (c) 2012 ARM Limited
* All rights reserved
*
*/
int capCoalescedMMIO() const;
+ /**
+ * Attempt to determine how many memory slots are available. If it can't
+ * be determined, this function returns 0.
+ */
+ int capNumMemSlots() const;
+
/**
* Support for reading and writing single registers.
*
bool hasKernelIRQChip() const { return _hasKernelIRQChip; }
/** @} */
+ struct MemSlot
+ {
+ MemSlot(uint32_t _num) : num(_num)
+ {}
+ MemSlot() : num(-1)
+ {}
+
+ int32_t num;
+ };
+
+ /**
+ * Allocate a memory slot within the VM.
+ */
+ const MemSlot allocMemSlot(uint64_t size);
+
+ /**
+ * Setup a region of physical memory in the guest
+ *
+ * @param slot KVM memory slot ID returned by allocMemSlot
+ * @param host_addr Memory allocation backing the memory
+ * @param guest_addr Address in the guest
+ * @param flags Flags (see the KVM API documentation)
+ */
+ void setupMemSlot(const MemSlot slot, void *host_addr, Addr guest_addr,
+ uint32_t flags);
+
+ /**
+ * Disable a memory slot.
+ */
+ void disableMemSlot(const MemSlot slot);
+
+ /**
+ * Free a previously allocated memory slot.
+ */
+ void freeMemSlot(const MemSlot slot);
+
/** Global KVM interface */
Kvm kvm;
* @param slot KVM memory slot ID (must be unique)
* @param host_addr Memory allocation backing the memory
* @param guest_addr Address in the guest
- * @param guest_range Address range used by guest.
* @param len Size of the allocation in bytes
* @param flags Flags (see the KVM API documentation)
*/
void setUserMemoryRegion(uint32_t slot,
void *host_addr, Addr guest_addr,
uint64_t len, uint32_t flags);
- void setUserMemoryRegion(uint32_t slot,
- void *host_addr, AddrRange guest_range,
- uint32_t flags);
/** @} */
/**
/** Next unallocated vCPU ID */
long nextVCPUID;
+
+ /**
+ * Structures tracking memory slots.
+ */
+ class MemorySlot
+ {
+ public:
+ uint64_t size;
+ uint32_t slot;
+ bool active;
+ };
+ std::vector<MemorySlot> memorySlots;
+ uint32_t maxMemorySlot;
};
#endif