/* * Creates a new (reference to a) struct vgic_irq for a given LPI. * If this LPI is already mapped on another ITS, we increase its refcount * and return a pointer to the existing structure. * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq. * This function returns a pointer to the _unlocked_ structure.
*/ staticstruct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid, struct kvm_vcpu *vcpu)
{ struct vgic_dist *dist = &kvm->arch.vgic; struct vgic_irq *irq = vgic_get_irq(kvm, intid), *oldirq; int ret;
/* In this case there is no put, since we keep the reference. */ if (irq) return irq;
irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL_ACCOUNT); if (!irq) return ERR_PTR(-ENOMEM);
ret = xa_reserve(&dist->lpi_xa, intid, GFP_KERNEL_ACCOUNT); if (ret) {
kfree(irq); return ERR_PTR(ret);
}
/* * There could be a race with another vgic_add_lpi(), so we need to * check that we don't add a second list entry with the same LPI.
*/
oldirq = xa_load(&dist->lpi_xa, intid); if (vgic_try_get_irq_ref(oldirq)) { /* Someone was faster with adding this LPI, lets use that. */
kfree(irq);
irq = oldirq;
goto out_unlock;
}
ret = xa_err(__xa_store(&dist->lpi_xa, intid, irq, 0)); if (ret) {
xa_release(&dist->lpi_xa, intid);
kfree(irq);
}
out_unlock:
xa_unlock(&dist->lpi_xa);
if (ret) return ERR_PTR(ret);
/* * We "cache" the configuration table entries in our struct vgic_irq's. * However we only have those structs for mapped IRQs, so we read in * the respective config data from memory here upon mapping the LPI. * * Should any of these fail, behave as if we couldn't create the LPI * by dropping the refcount and returning the error.
*/
ret = update_lpi_config(kvm, irq, NULL, false); if (ret) {
vgic_put_irq(kvm, irq); return ERR_PTR(ret);
}
ret = vgic_v3_lpi_sync_pending_status(kvm, irq); if (ret) {
vgic_put_irq(kvm, irq); return ERR_PTR(ret);
}
return irq;
}
/** * struct vgic_its_abi - ITS abi ops and settings * @cte_esz: collection table entry size * @dte_esz: device table entry size * @ite_esz: interrupt translation table entry size * @save_tables: save the ITS tables into guest RAM * @restore_tables: restore the ITS internal structs from tables * stored in guest RAM * @commit: initialize the registers which expose the ABI settings, * especially the entry sizes
*/ struct vgic_its_abi { int cte_esz; int dte_esz; int ite_esz; int (*save_tables)(struct vgic_its *its); int (*restore_tables)(struct vgic_its *its); int (*commit)(struct vgic_its *its);
};
its->abi_rev = rev;
abi = vgic_its_get_abi(its); return abi->commit(its);
}
/* * Find and returns a device in the device table for an ITS. * Must be called with the its_lock mutex held.
*/ staticstruct its_device *find_its_device(struct vgic_its *its, u32 device_id)
{ struct its_device *device;
list_for_each_entry(device, &its->device_list, dev_list) if (device_id == device->device_id) return device;
return NULL;
}
/* * Find and returns an interrupt translation table entry (ITTE) for a given * Device ID/Event ID pair on an ITS. * Must be called with the its_lock mutex held.
*/ staticstruct its_ite *find_ite(struct vgic_its *its, u32 device_id,
u32 event_id)
{ struct its_device *device; struct its_ite *ite;
device = find_its_device(its, device_id); if (device == NULL) return NULL;
list_for_each_entry(ite, &device->itt_head, ite_list) if (ite->event_id == event_id) return ite;
return NULL;
}
/* To be used as an iterator this macro misses the enclosing parentheses */ #define for_each_lpi_its(dev, ite, its) \
list_for_each_entry(dev, &(its)->device_list, dev_list) \
list_for_each_entry(ite, &(dev)->itt_head, ite_list)
/* * Finds and returns a collection in the ITS collection table. * Must be called with the its_lock mutex held.
*/ staticstruct its_collection *find_collection(struct vgic_its *its, int coll_id)
{ struct its_collection *collection;
/* * Reads the configuration data for a given LPI from guest memory and * updates the fields in struct vgic_irq. * If filter_vcpu is not NULL, applies only if the IRQ is targeting this * VCPU. Unconditionally applies if filter_vcpu is NULL.
*/ staticint update_lpi_config(struct kvm *kvm, struct vgic_irq *irq, struct kvm_vcpu *filter_vcpu, bool needs_inv)
{
u64 propbase = GICR_PROPBASER_ADDRESS(kvm->arch.vgic.propbaser);
u8 prop; int ret; unsignedlong flags;
ret = kvm_read_guest_lock(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
&prop, 1);
/* * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI * is targeting) to the VGIC's view, which deals with target VCPUs. * Needs to be called whenever either the collection for a LPIs has * changed or the collection itself got retargeted.
*/ staticvoid update_affinity_ite(struct kvm *kvm, struct its_ite *ite)
{ struct kvm_vcpu *vcpu;
if (!its_is_collection_mapped(ite->collection)) return;
/* * Updates the target VCPU for every LPI targeting this collection. * Must be called with the its_lock mutex held.
*/ staticvoid update_affinity_collection(struct kvm *kvm, struct vgic_its *its, struct its_collection *coll)
{ struct its_device *device; struct its_ite *ite;
for_each_lpi_its(device, ite, its) { if (ite->collection != coll) continue;
/* * Sync the pending table pending bit of LPIs targeting @vcpu * with our own data structures. This relies on the LPI being * mapped before.
*/ staticint its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
{
gpa_t pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser); struct vgic_dist *dist = &vcpu->kvm->arch.vgic; unsignedlong intid, flags; struct vgic_irq *irq; int last_byte_offset = -1; int ret = 0;
u8 pendmask;
xa_for_each(&dist->lpi_xa, intid, irq) { int byte_offset, bit_nr;
/* * For contiguously allocated LPIs chances are we just read * this very same byte in the last iteration. Reuse that.
*/ if (byte_offset != last_byte_offset) {
ret = kvm_read_guest_lock(vcpu->kvm,
pendbase + byte_offset,
&pendmask, 1); if (ret) return ret;
last_byte_offset = byte_offset;
}
irq = vgic_get_irq(vcpu->kvm, intid); if (!irq) continue;
/* * We use linear CPU numbers for redistributor addressing, * so GITS_TYPER.PTA is 0. * Also we force all PROPBASER registers to be the same, so * CommonLPIAff is 0 as well. * To avoid memory waste in the guest, we keep the number of IDBits and * DevBits low - as least for the time being.
*/
reg |= GIC_ENCODE_SZ(VITS_TYPER_DEVBITS, 5) << GITS_TYPER_DEVBITS_SHIFT;
reg |= GIC_ENCODE_SZ(VITS_TYPER_IDBITS, 5) << GITS_TYPER_IDBITS_SHIFT;
reg |= GIC_ENCODE_SZ(abi->ite_esz, 4) << GITS_TYPER_ITT_ENTRY_SIZE_SHIFT;
if (rev >= NR_ITS_ABIS) return -EINVAL; return vgic_its_set_abi(its, rev);
}
staticunsignedlong vgic_mmio_read_its_idregs(struct kvm *kvm, struct vgic_its *its,
gpa_t addr, unsignedint len)
{ switch (addr & 0xffff) { case GITS_PIDR0: return 0x92; /* part number, bits[7:0] */ case GITS_PIDR1: return 0xb4; /* part number, bits[11:8] */ case GITS_PIDR2: return GIC_PIDR2_ARCH_GICv3 | 0x0b; case GITS_PIDR4: return 0x40; /* This is a 64K software visible page */ /* The following are the ID registers for (any) GIC. */ case GITS_CIDR0: return 0x0d; case GITS_CIDR1: return 0xf0; case GITS_CIDR2: return 0x05; case GITS_CIDR3: return 0xb1;
}
/* Do not cache a directly injected interrupt */ if (irq->hw) return;
/* * The irq refcount is guaranteed to be nonzero while holding the * its_lock, as the ITE (and the reference it holds) cannot be freed.
*/
lockdep_assert_held(&its->its_lock);
vgic_get_irq_ref(irq);
old = xa_store(&its->translation_cache, cache_key, irq, GFP_KERNEL_ACCOUNT);
/* * Put the reference taken on @irq if the store fails. Intentionally do * not return the error as the translation cache is best effort.
*/ if (xa_is_err(old)) {
vgic_put_irq(kvm, irq); return;
}
/* * We could have raced with another CPU caching the same * translation behind our back, ensure we don't leak a * reference if that is the case.
*/ if (old)
vgic_put_irq(kvm, old);
}
/* * Find the target VCPU and the LPI number for a given devid/eventid pair * and make this IRQ pending, possibly injecting it. * Must be called with the its_lock mutex held. * Returns 0 on success, a positive error value for any ITS mapping * related errors and negative error values for generic errors.
*/ staticint vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
u32 devid, u32 eventid)
{ struct vgic_irq *irq = NULL; unsignedlong flags; int err;
/* * Queries the KVM IO bus framework to get the ITS pointer from the given * doorbell address. * We then call vgic_its_trigger_msi() with the decoded data. * According to the KVM_SIGNAL_MSI API description returns 1 on success.
*/ int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi)
{ struct vgic_its *its; int ret;
if (!vgic_its_inject_cached_translation(kvm, msi)) return 1;
its = vgic_msi_to_its(kvm, msi); if (IS_ERR(its)) return PTR_ERR(its);
mutex_lock(&its->its_lock);
ret = vgic_its_trigger_msi(kvm, its, msi->devid, msi->data);
mutex_unlock(&its->its_lock);
if (ret < 0) return ret;
/* * KVM_SIGNAL_MSI demands a return value > 0 for success and 0 * if the guest has blocked the MSI. So we map any LPI mapping * related error to that.
*/ if (ret) return 0; else return 1;
}
/* Requires the its_lock to be held. */ staticvoid its_free_ite(struct kvm *kvm, struct its_ite *ite)
{ struct vgic_irq *irq = ite->irq;
list_del(&ite->ite_list);
/* This put matches the get in vgic_add_lpi. */ if (irq) {
scoped_guard(raw_spinlock_irqsave, &irq->irq_lock) { if (irq->hw)
its_unmap_vlpi(ite->irq->host_irq);
irq->hw = false;
}
vgic_put_irq(kvm, ite->irq);
}
kfree(ite);
}
static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)
{ return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1);
}
/* * The DISCARD command frees an Interrupt Translation Table Entry (ITTE). * Must be called with the its_lock mutex held.
*/ staticint vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,
u64 *its_cmd)
{
u32 device_id = its_cmd_get_deviceid(its_cmd);
u32 event_id = its_cmd_get_id(its_cmd); struct its_ite *ite;
ite = find_ite(its, device_id, event_id); if (ite && its_is_collection_mapped(ite->collection)) { struct its_device *device = find_its_device(its, device_id); int ite_esz = vgic_its_get_abi(its)->ite_esz;
gpa_t gpa = device->itt_addr + ite->event_id * ite_esz; /* * Though the spec talks about removing the pending state, we * don't bother here since we clear the ITTE anyway and the * pending state is a property of the ITTE struct.
*/
vgic_its_invalidate_cache(its);
/* * Check whether an ID can be stored into the corresponding guest table. * For a direct table this is pretty easy, but gets a bit nasty for * indirect tables. We check whether the resulting guest physical address * is actually valid (covered by a memslot and guest accessible). * For this we have to read the respective first level entry.
*/ staticbool vgic_its_check_id(struct vgic_its *its, u64 baser, u32 id,
gpa_t *eaddr)
{ int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
u64 indirect_ptr, type = GITS_BASER_TYPE(baser);
phys_addr_t base = GITS_BASER_ADDR_48_to_52(baser); int esz = GITS_BASER_ENTRY_SIZE(baser); int index;
switch (type) { case GITS_BASER_TYPE_DEVICE: if (id > VITS_MAX_DEVID) returnfalse; break; case GITS_BASER_TYPE_COLLECTION: /* as GITS_TYPER.CIL == 0, ITS supports 16-bit collection ID */ if (id >= BIT_ULL(16)) returnfalse; break; default: returnfalse;
}
if (!(baser & GITS_BASER_INDIRECT)) {
phys_addr_t addr;
if (id >= (l1_tbl_size / esz)) returnfalse;
addr = base + id * esz;
if (eaddr)
*eaddr = addr;
return __is_visible_gfn_locked(its, addr);
}
/* calculate and check the index into the 1st level */
index = id / (SZ_64K / esz); if (index >= (l1_tbl_size / sizeof(u64))) returnfalse;
/* Each 1st level entry is represented by a 64-bit value. */ if (kvm_read_guest_lock(its->dev->kvm,
base + index * sizeof(indirect_ptr),
&indirect_ptr, sizeof(indirect_ptr))) returnfalse;
indirect_ptr = le64_to_cpu(indirect_ptr);
/* check the valid bit of the first level entry */ if (!(indirect_ptr & BIT_ULL(63))) returnfalse;
/* Mask the guest physical address and calculate the frame number. */
indirect_ptr &= GENMASK_ULL(51, 16);
/* Find the address of the actual entry */
index = id % (SZ_64K / esz);
indirect_ptr += index * esz;
/* * Check whether an event ID can be stored in the corresponding Interrupt * Translation Table, which starts at device->itt_addr.
*/ staticbool vgic_its_check_event_id(struct vgic_its *its, struct its_device *device,
u32 event_id)
{ conststruct vgic_its_abi *abi = vgic_its_get_abi(its); int ite_esz = abi->ite_esz;
gpa_t gpa;
/* max table size is: BIT_ULL(device->num_eventid_bits) * ite_esz */ if (event_id >= BIT_ULL(device->num_eventid_bits)) returnfalse;
/* * Add a new collection into the ITS collection table. * Returns 0 on success, and a negative error value for generic errors.
*/ staticint vgic_its_alloc_collection(struct vgic_its *its, struct its_collection **colp,
u32 coll_id)
{ struct its_collection *collection;
collection = kzalloc(sizeof(*collection), GFP_KERNEL_ACCOUNT); if (!collection) return -ENOMEM;
/* * Clearing the mapping for that collection ID removes the * entry from the list. If there wasn't any before, we can * go home early.
*/
collection = find_collection(its, coll_id); if (!collection) return;
/* Must be called with its_lock mutex held */ staticstruct its_ite *vgic_its_alloc_ite(struct its_device *device, struct its_collection *collection,
u32 event_id)
{ struct its_ite *ite;
ite = kzalloc(sizeof(*ite), GFP_KERNEL_ACCOUNT); if (!ite) return ERR_PTR(-ENOMEM);
/* If there is an existing mapping, behavior is UNPREDICTABLE. */ if (find_ite(its, device_id, event_id)) return 0;
collection = find_collection(its, coll_id); if (!collection) { int ret;
if (!vgic_its_check_id(its, its->baser_coll_table, coll_id, NULL)) return E_ITS_MAPC_COLLECTION_OOR;
ret = vgic_its_alloc_collection(its, &collection, coll_id); if (ret) return ret;
new_coll = collection;
}
ite = vgic_its_alloc_ite(device, collection, event_id); if (IS_ERR(ite)) { if (new_coll)
vgic_its_free_collection(its, coll_id); return PTR_ERR(ite);
}
if (its_is_collection_mapped(collection))
vcpu = collection_to_vcpu(kvm, collection);
irq = vgic_add_lpi(kvm, lpi_nr, vcpu); if (IS_ERR(irq)) { if (new_coll)
vgic_its_free_collection(its, coll_id);
its_free_ite(kvm, ite); return PTR_ERR(irq);
}
ite->irq = irq;
return 0;
}
/* Requires the its_lock to be held. */ staticvoid vgic_its_free_device(struct kvm *kvm, struct vgic_its *its, struct its_device *device)
{ struct its_ite *ite, *temp;
/* * The spec says that unmapping a device with still valid * ITTEs associated is UNPREDICTABLE. We remove all ITTEs, * since we cannot leave the memory unreferenced.
*/
list_for_each_entry_safe(ite, temp, &device->itt_head, ite_list)
its_free_ite(kvm, ite);
vgic_its_invalidate_cache(its);
list_del(&device->dev_list);
kfree(device);
}
/* its lock must be held */ staticvoid vgic_its_free_device_list(struct kvm *kvm, struct vgic_its *its)
{ struct its_device *cur, *temp;
/* Must be called with its_lock mutex held */ staticstruct its_device *vgic_its_alloc_device(struct vgic_its *its,
u32 device_id, gpa_t itt_addr,
u8 num_eventid_bits)
{ struct its_device *device;
device = kzalloc(sizeof(*device), GFP_KERNEL_ACCOUNT); if (!device) return ERR_PTR(-ENOMEM);
/* * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs). * Must be called with the its_lock mutex held.
*/ staticint vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
u64 *its_cmd)
{
u32 device_id = its_cmd_get_deviceid(its_cmd); bool valid = its_cmd_get_validbit(its_cmd);
u8 num_eventid_bits = its_cmd_get_size(its_cmd);
gpa_t itt_addr = its_cmd_get_ittaddr(its_cmd); struct its_device *device;
gpa_t gpa;
if (!vgic_its_check_id(its, its->baser_device_table, device_id, &gpa)) return E_ITS_MAPD_DEVICE_OOR;
if (valid && num_eventid_bits > VITS_TYPER_IDBITS) return E_ITS_MAPD_ITTSIZE_OOR;
device = find_its_device(its, device_id);
/* * The spec says that calling MAPD on an already mapped device * invalidates all cached data for this device. We implement this * by removing the mapping and re-establishing it.
*/ if (device)
vgic_its_free_device(kvm, its, device);
/* * The spec does not say whether unmapping a not-mapped device * is an error, so we are done in any case.
*/ if (!valid) return vgic_its_write_entry_lock(its, gpa, 0ULL, dte);
/* * The CLEAR command removes the pending state for a particular LPI. * Must be called with the its_lock mutex held.
*/ staticint vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its,
u64 *its_cmd)
{
u32 device_id = its_cmd_get_deviceid(its_cmd);
u32 event_id = its_cmd_get_id(its_cmd); struct its_ite *ite;
ite = find_ite(its, device_id, event_id); if (!ite) return E_ITS_CLEAR_UNMAPPED_INTERRUPT;
ite->irq->pending_latch = false;
if (ite->irq->hw) return irq_set_irqchip_state(ite->irq->host_irq,
IRQCHIP_STATE_PENDING, false);
/* * The INV command syncs the configuration bits from the memory table. * Must be called with the its_lock mutex held.
*/ staticint vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its,
u64 *its_cmd)
{
u32 device_id = its_cmd_get_deviceid(its_cmd);
u32 event_id = its_cmd_get_id(its_cmd); struct its_ite *ite;
ite = find_ite(its, device_id, event_id); if (!ite) return E_ITS_INV_UNMAPPED_INTERRUPT;
return vgic_its_inv_lpi(kvm, ite->irq);
}
/** * vgic_its_invall - invalidate all LPIs targeting a given vcpu * @vcpu: the vcpu for which the RD is targeted by an invalidation * * Contrary to the INVALL command, this targets a RD instead of a * collection, and we don't need to hold the its_lock, since no ITS is * involved here.
*/ int vgic_its_invall(struct kvm_vcpu *vcpu)
{ struct kvm *kvm = vcpu->kvm; struct vgic_dist *dist = &kvm->arch.vgic; struct vgic_irq *irq; unsignedlong intid;
if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.its_vm)
its_invall_vpe(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe);
return 0;
}
/* * The INVALL command requests flushing of all IRQ data in this collection. * Find the VCPU mapped to that collection, then iterate over the VM's list * of mapped LPIs and update the configuration for each IRQ which targets * the specified vcpu. The configuration will be read from the in-memory * configuration table. * Must be called with the its_lock mutex held.
*/ staticint vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
u64 *its_cmd)
{
u32 coll_id = its_cmd_get_collection(its_cmd); struct its_collection *collection; struct kvm_vcpu *vcpu;
collection = find_collection(its, coll_id); if (!its_is_collection_mapped(collection)) return E_ITS_INVALL_UNMAPPED_COLLECTION;
/* * The MOVALL command moves the pending state of all IRQs targeting one * redistributor to another. We don't hold the pending state in the VCPUs, * but in the IRQs instead, so there is really not much to do for us here. * However the spec says that no IRQ must target the old redistributor * afterwards, so we make sure that no LPI is using the associated target_vcpu. * This command affects all LPIs in the system that target that redistributor.
*/ staticint vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its,
u64 *its_cmd)
{ struct vgic_dist *dist = &kvm->arch.vgic; struct kvm_vcpu *vcpu1, *vcpu2; struct vgic_irq *irq; unsignedlong intid;
/* We advertise GITS_TYPER.PTA==0, making the address the vcpu ID */
vcpu1 = kvm_get_vcpu_by_id(kvm, its_cmd_get_target_addr(its_cmd));
vcpu2 = kvm_get_vcpu_by_id(kvm, its_cmd_mask_field(its_cmd, 3, 16, 32));
if (!vcpu1 || !vcpu2) return E_ITS_MOVALL_PROCNUM_OOR;
/* * The INT command injects the LPI associated with that DevID/EvID pair. * Must be called with the its_lock mutex held.
*/ staticint vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its,
u64 *its_cmd)
{
u32 msi_data = its_cmd_get_id(its_cmd);
u64 msi_devid = its_cmd_get_deviceid(its_cmd);
/* * This function is called with the its_cmd lock held, but the ITS data * structure lock dropped.
*/ staticint vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its,
u64 *its_cmd)
{ int ret = -ENODEV;
mutex_lock(&its->its_lock); switch (its_cmd_get_command(its_cmd)) { case GITS_CMD_MAPD:
ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd); break; case GITS_CMD_MAPC:
ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd); break; case GITS_CMD_MAPI:
ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd); break; case GITS_CMD_MAPTI:
ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd); break; case GITS_CMD_MOVI:
ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd); break; case GITS_CMD_DISCARD:
ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd); break; case GITS_CMD_CLEAR:
ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd); break; case GITS_CMD_MOVALL:
ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd); break; case GITS_CMD_INT:
ret = vgic_its_cmd_handle_int(kvm, its, its_cmd); break; case GITS_CMD_INV:
ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd); break; case GITS_CMD_INVALL:
ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd); break; case GITS_CMD_SYNC: /* we ignore this command: we are in sync all of the time */
ret = 0; break;
}
mutex_unlock(&its->its_lock);
staticvoid vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its,
gpa_t addr, unsignedint len, unsignedlong val)
{ /* When GITS_CTLR.Enable is 1, this register is RO. */ if (its->enabled) return;
mutex_lock(&its->cmd_lock);
its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val);
its->cbaser = vgic_sanitise_its_cbaser(its->cbaser);
its->creadr = 0; /* * CWRITER is architecturally UNKNOWN on reset, but we need to reset * it to CREADR to make sure we start with an empty command buffer.
*/
its->cwriter = its->creadr;
mutex_unlock(&its->cmd_lock);
}
/* Must be called with the cmd_lock held. */ staticvoid vgic_its_process_commands(struct kvm *kvm, struct vgic_its *its)
{
gpa_t cbaser;
u64 cmd_buf[4];
/* Commands are only processed when the ITS is enabled. */ if (!its->enabled) return;
cbaser = GITS_CBASER_ADDRESS(its->cbaser);
while (its->cwriter != its->creadr) { int ret = kvm_read_guest_lock(kvm, cbaser + its->creadr,
cmd_buf, ITS_CMD_SIZE); /* * If kvm_read_guest() fails, this could be due to the guest * programming a bogus value in CBASER or something else going * wrong from which we cannot easily recover. * According to section 6.3.2 in the GICv3 spec we can just * ignore that command then.
*/ if (!ret)
vgic_its_handle_command(kvm, its, cmd_buf);
/* * By writing to CWRITER the guest announces new commands to be processed. * To avoid any races in the first place, we take the its_cmd lock, which * protects our ring buffer variables, so that there is only one user * per ITS handling commands at a given time.
*/ staticvoid vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its,
gpa_t addr, unsignedint len, unsignedlong val)
{
u64 reg;
if (!(reg & GITS_BASER_VALID)) { /* Take the its_lock to prevent a race with a save/restore */
mutex_lock(&its->its_lock); switch (table_type) { case GITS_BASER_TYPE_DEVICE:
vgic_its_free_device_list(kvm, its); break; case GITS_BASER_TYPE_COLLECTION:
vgic_its_free_collection_list(kvm, its); break;
}
mutex_unlock(&its->its_lock);
}
}
/* * It is UNPREDICTABLE to enable the ITS if any of the CBASER or * device/collection BASER are invalid
*/ if (!its->enabled && (val & GITS_CTLR_ENABLE) &&
(!(its->baser_device_table & GITS_BASER_VALID) ||
!(its->baser_coll_table & GITS_BASER_VALID) ||
!(its->cbaser & GITS_CBASER_VALID))) goto out;
its->enabled = !!(val & GITS_CTLR_ENABLE); if (!its->enabled)
vgic_its_invalidate_cache(its);
/* * Try to process any pending commands. This function bails out early * if the ITS is disabled or no commands have been queued.
*/
vgic_its_process_commands(kvm, its);
/* This is called on setting the LPI enable bit in the redistributor. */ void vgic_enable_lpis(struct kvm_vcpu *vcpu)
{ if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ))
its_sync_lpi_pending_table(vcpu);
}
/* * Although the spec supports upper/lower 32-bit accesses to * 64-bit ITS registers, the userspace ABI requires 64-bit * accesses to all 64-bit wide registers. We therefore only * support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID * registers
*/ if ((offset < GITS_TYPER) || (offset >= GITS_PIDR4))
align = 0x3; else
align = 0x7;
if (offset & align) return -EINVAL;
mutex_lock(&dev->kvm->lock);
if (kvm_trylock_all_vcpus(dev->kvm)) {
mutex_unlock(&dev->kvm->lock); return -EBUSY;
}
mutex_lock(&dev->kvm->arch.config_lock);
if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
ret = -ENXIO; goto out;
}
region = vgic_find_mmio_region(its_registers,
ARRAY_SIZE(its_registers),
offset); if (!region) {
ret = -ENXIO; goto out;
}
addr = its->vgic_its_base + offset;
len = region->access_flags & VGIC_ACCESS_64bit ? 8 : 4;
/** * typedef entry_fn_t - Callback called on a table entry restore path * @its: its handle * @id: id of the entry * @entry: pointer to the entry * @opaque: pointer to an opaque data * * Return: < 0 on error, 0 if last element was identified, id offset to next * element otherwise
*/ typedefint (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry, void *opaque);
/** * scan_its_table - Scan a contiguous table in guest RAM and applies a function * to each entry * * @its: its handle * @base: base gpa of the table * @size: size of the table in bytes * @esz: entry size in bytes * @start_id: the ID of the first entry in the table * (non zero for 2d level tables) * @fn: function to apply on each entry * @opaque: pointer to opaque data * * Return: < 0 on error, 0 if last element was identified, 1 otherwise * (the last element may not be found on second level tables)
*/ staticint scan_its_table(struct vgic_its *its, gpa_t base, int size, u32 esz, int start_id, entry_fn_t fn, void *opaque)
{ struct kvm *kvm = its->dev->kvm; unsignedlong len = size; int id = start_id;
gpa_t gpa = base; char entry[ESZ_MAX]; int ret;
memset(entry, 0, esz);
while (true) { int next_offset;
size_t byte_offset;
ret = kvm_read_guest_lock(kvm, gpa, entry, esz); if (ret) return ret;
/* * If an LPI carries the HW bit, this means that this * interrupt is controlled by GICv4, and we do not * have direct access to that state without GICv4.1. * Let's simply fail the save operation...
*/ if (ite->irq->hw && !kvm_vgic_global_state.has_gicv4_1) return -EACCES;
ret = vgic_its_save_ite(its, device, ite, gpa); if (ret) return ret;
} return 0;
}
/** * vgic_its_restore_itt - restore the ITT of a device * * @its: its handle * @dev: device handle * * Return 0 on success, < 0 on error
*/ staticint vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
{ conststruct vgic_its_abi *abi = vgic_its_get_abi(its);
gpa_t base = dev->itt_addr; int ret; int ite_esz = abi->ite_esz;
size_t max_size = BIT_ULL(dev->num_eventid_bits) * ite_esz;
ret = scan_its_table(its, base, max_size, ite_esz, 0,
vgic_its_restore_ite, dev);
/* scan_its_table returns +1 if all ITEs are invalid */ if (ret > 0)
ret = 0;
return ret;
}
/** * vgic_its_save_dte - Save a device table entry at a given GPA * * @its: ITS handle * @dev: ITS device * @ptr: GPA
*/ staticint vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
gpa_t ptr)
{
u64 val, itt_addr_field;
u32 next_offset;
/** * vgic_its_restore_dte - restore a device table entry * * @its: its handle * @id: device id the DTE corresponds to * @ptr: kernel VA where the 8 byte DTE is located * @opaque: unused * * Return: < 0 on error, 0 if the dte is the last one, id offset to the * next dte otherwise
*/ staticint vgic_its_restore_dte(struct vgic_its *its, u32 id, void *ptr, void *opaque)
{ struct its_device *dev;
u64 baser = its->baser_device_table;
gpa_t itt_addr;
u8 num_eventid_bits;
u64 entry = *(u64 *)ptr; bool valid;
u32 offset; int ret;
if (deva->device_id < devb->device_id) return -1; else return 1;
}
/* * vgic_its_save_device_tables - Save the device table and all ITT * into guest RAM * * L1/L2 handling is hidden by vgic_its_check_id() helper which directly * returns the GPA of the device entry
*/ staticint vgic_its_save_device_tables(struct vgic_its *its)
{
u64 baser = its->baser_device_table; struct its_device *dev;
list_for_each_entry(dev, &its->device_list, dev_list) { int ret;
gpa_t eaddr;
if (!vgic_its_check_id(its, baser,
dev->device_id, &eaddr)) return -EINVAL;
ret = vgic_its_save_itt(its, dev); if (ret) return ret;
ret = vgic_its_save_dte(its, dev, eaddr); if (ret) return ret;
} return 0;
}
/** * handle_l1_dte - callback used for L1 device table entries (2 stage case) * * @its: its handle * @id: index of the entry in the L1 table * @addr: kernel VA * @opaque: unused * * L1 table entries are scanned by steps of 1 entry * Return < 0 if error, 0 if last dte was found when scanning the L2 * table, +1 otherwise (meaning next L1 entry must be scanned)
*/ staticint handle_l1_dte(struct vgic_its *its, u32 id, void *addr, void *opaque)
{ conststruct vgic_its_abi *abi = vgic_its_get_abi(its); int l2_start_id = id * (SZ_64K / abi->dte_esz);
u64 entry = *(u64 *)addr; int dte_esz = abi->dte_esz;
gpa_t gpa; int ret;
entry = le64_to_cpu(entry);
if (!(entry & KVM_ITS_L1E_VALID_MASK)) return 1;
gpa = entry & KVM_ITS_L1E_ADDR_MASK;
ret = scan_its_table(its, gpa, SZ_64K, dte_esz,
l2_start_id, vgic_its_restore_dte, NULL);
return ret;
}
/* * vgic_its_restore_device_tables - Restore the device table and all ITT * from guest RAM to internal data structs
*/ staticint vgic_its_restore_device_tables(struct vgic_its *its)
{ conststruct vgic_its_abi *abi = vgic_its_get_abi(its);
u64 baser = its->baser_device_table; int l1_esz, ret; int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
gpa_t l1_gpa;
/* * Restore a collection entry into the ITS collection table. * Return +1 on success, 0 if the entry was invalid (which should be * interpreted as end-of-table), and a negative error value for generic errors.
*/ staticint vgic_its_restore_cte(struct vgic_its *its, gpa_t gpa)
{ struct its_collection *collection; struct kvm *kvm = its->dev->kvm;
u32 target_addr, coll_id;
u64 val; int ret;
ret = vgic_its_read_entry_lock(its, gpa, &val, cte); if (ret) return ret;
val = le64_to_cpu(val); if (!(val & KVM_ITS_CTE_VALID_MASK)) return 0;
target_addr = (u32)(val >> KVM_ITS_CTE_RDBASE_SHIFT);
coll_id = val & KVM_ITS_CTE_ICID_MASK;
if (target_addr != COLLECTION_NOT_MAPPED &&
!kvm_get_vcpu_by_id(kvm, target_addr)) return -EINVAL;
collection = find_collection(its, coll_id); if (collection) return -EEXIST;
if (!vgic_its_check_id(its, its->baser_coll_table, coll_id, NULL)) return -EINVAL;
ret = vgic_its_alloc_collection(its, &collection, coll_id); if (ret) return ret;
collection->target_addr = target_addr; return 1;
}
list_for_each_entry(collection, &its->collection_list, coll_list) {
ret = vgic_its_save_cte(its, collection, gpa); if (ret) return ret;
gpa += cte_esz;
filled += cte_esz;
}
if (filled == max_size) return 0;
/* * table is not fully filled, add a last dummy element * with valid bit unset
*/ return vgic_its_write_entry_lock(its, gpa, 0ULL, cte);
}
/* * vgic_its_restore_collection_table - reads the collection table * in guest memory and restores the ITS internal state. Requires the
--> --------------------
--> maximum size reached
--> --------------------
Messung V0.5
¤ Dauer der Verarbeitung: 0.19 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.