// SPDX-License-Identifier: GPL-2.0 /* * vgic_irq.c - Test userspace injection of IRQs * * This test validates the injection of IRQs from userspace using various * methods (e.g., KVM_IRQ_LINE) and modes (e.g., EOI). The guest "asks" the * host to inject a specific intid via a GUEST_SYNC call, and then checks that * it received it.
*/ #include <asm/kvm.h> #include <asm/kvm_para.h> #include <sys/eventfd.h> #include <linux/sizes.h>
/* * Stores the user specified args; it's passed to the guest and to every test * function.
*/ struct test_args {
uint32_t nr_irqs; /* number of KVM supported IRQs. */ bool eoi_split; /* 1 is eoir+dir, 0 is eoir only */ bool level_sensitive; /* 1 is level, 0 is edge */ int kvm_max_routes; /* output of KVM_CAP_IRQ_ROUTING */ bool kvm_supports_irqfd; /* output of KVM_CAP_IRQFD */
};
/* * KVM implements 32 priority levels: * 0x00 (highest priority) - 0xF8 (lowest priority), in steps of 8 * * Note that these macros will still be correct in the case that KVM implements * more priority levels. Also note that 32 is the minimum for GICv3 and GICv2.
*/ #define KVM_NUM_PRIOS 32 #define KVM_PRIO_SHIFT 3 /* steps of 8 = 1 << 3 */ #define KVM_PRIO_STEPS (1 << KVM_PRIO_SHIFT) /* 8 */ #define LOWEST_PRIO (KVM_NUM_PRIOS - 1) #define CPU_PRIO_MASK (LOWEST_PRIO << KVM_PRIO_SHIFT) /* 0xf8 */ #define IRQ_DEFAULT_PRIO (LOWEST_PRIO - 1) #define IRQ_DEFAULT_PRIO_REG (IRQ_DEFAULT_PRIO << KVM_PRIO_SHIFT) /* 0xf0 */
/* * The kvm_inject_* utilities are used by the guest to ask the host to inject * interrupts (e.g., using the KVM_IRQ_LINE ioctl).
*/
/* Used on the guest side to perform the hypercall. */ staticvoid kvm_inject_call(kvm_inject_cmd cmd, uint32_t first_intid,
uint32_t num, int level, bool expect_failure);
/* Used on the host side to get the hypercall info. */ staticvoid kvm_inject_get_call(struct kvm_vm *vm, struct ucall *uc, struct kvm_inject_args *args);
#define _KVM_INJECT_MULTI(cmd, intid, num, expect_failure) \
kvm_inject_call(cmd, intid, num, -1 /* not used */, expect_failure)
/* Cycle over all priorities to make things more interesting. */ for (i = first_intid; i < num + first_intid; i++)
gic_set_priority(i, (i % (KVM_NUM_PRIOS - 1)) << 3);
GUEST_ASSERT_EQ(irq_handled, num); for (i = first_intid; i < num + first_intid; i++)
GUEST_ASSERT_EQ(irqnr_received[i], 1);
GUEST_ASSERT_IAR_EMPTY();
reset_priorities(args);
}
/* * Restore the active state of multiple concurrent IRQs (given by * concurrent_irqs). This does what a live-migration would do on the * destination side assuming there are some active IRQs that were not * deactivated yet.
*/ staticvoid guest_restore_active(struct test_args *args,
uint32_t first_intid, uint32_t num,
kvm_inject_cmd cmd)
{
uint32_t prio, intid, ap1r; int i;
/* * Set the priorities of the first (KVM_NUM_PRIOS - 1) IRQs * in descending order, so intid+1 can preempt intid.
*/ for (i = 0, prio = (num - 1) * 8; i < num; i++, prio -= 8) {
GUEST_ASSERT(prio >= 0);
intid = i + first_intid;
gic_set_priority(intid, prio);
}
/* * In a real migration, KVM would restore all GIC state before running * guest code.
*/ for (i = 0; i < num; i++) {
intid = i + first_intid;
KVM_ACTIVATE(cmd, intid);
ap1r = gic_read_ap1r0();
ap1r |= 1U << i;
gic_write_ap1r0(ap1r);
}
/* This is where the "migration" would occur. */
/* finish handling the IRQs starting with the highest priority one. */ for (i = 0; i < num; i++) {
intid = num - i - 1 + first_intid;
gic_set_eoi(intid); if (args->eoi_split)
gic_set_dir(intid);
}
for (i = 0; i < num; i++)
GUEST_ASSERT(!gic_irq_get_active(i + first_intid));
GUEST_ASSERT_EQ(gic_read_ap1r0(), 0);
GUEST_ASSERT_IAR_EMPTY();
}
/* * Polls the IAR until it's not a spurious interrupt. * * This function should only be used in test_inject_preemption (with IRQs * masked).
*/ static uint32_t wait_for_and_activate_irq(void)
{
uint32_t intid;
do { asmvolatile("wfi" : : : "memory");
intid = gic_get_and_ack_irq();
} while (intid == IAR_SPURIOUS);
return intid;
}
/* * Inject multiple concurrent IRQs (num IRQs starting at first_intid) and * handle them without handling the actual exceptions. This is done by masking * interrupts for the whole test.
*/ staticvoid test_inject_preemption(struct test_args *args,
uint32_t first_intid, int num,
kvm_inject_cmd cmd)
{
uint32_t intid, prio, step = KVM_PRIO_STEPS; int i;
/* Set the priorities of the first (KVM_NUM_PRIOS - 1) IRQs * in descending order, so intid+1 can preempt intid.
*/ for (i = 0, prio = (num - 1) * step; i < num; i++, prio -= step) {
GUEST_ASSERT(prio >= 0);
intid = i + first_intid;
gic_set_priority(intid, prio);
}
local_irq_disable();
for (i = 0; i < num; i++) {
uint32_t tmp;
intid = i + first_intid;
KVM_INJECT(cmd, intid); /* Each successive IRQ will preempt the previous one. */
tmp = wait_for_and_activate_irq();
GUEST_ASSERT_EQ(tmp, intid); if (args->level_sensitive)
guest_set_irq_line(intid, 0);
}
/* finish handling the IRQs starting with the highest priority one. */ for (i = 0; i < num; i++) {
intid = num - i - 1 + first_intid;
gic_set_eoi(intid); if (args->eoi_split)
gic_set_dir(intid);
}
local_irq_enable();
for (i = 0; i < num; i++)
GUEST_ASSERT(!gic_irq_get_active(i + first_intid));
GUEST_ASSERT_EQ(gic_read_ap1r0(), 0);
GUEST_ASSERT_IAR_EMPTY();
for (i = 0; i < ARRAY_SIZE(bad_intid); i++)
test_inject_fail(args, bad_intid[i], f->cmd);
}
staticvoid test_preemption(struct test_args *args, struct kvm_inject_desc *f)
{ /* * Test up to 4 levels of preemption. The reason is that KVM doesn't * currently implement the ability to have more than the number-of-LRs * number of concurrently active IRQs. The number of LRs implemented is * IMPLEMENTATION DEFINED, however, it seems that most implement 4.
*/ if (f->sgi)
test_inject_preemption(args, MIN_SGI, 4, f->cmd);
if (f->ppi)
test_inject_preemption(args, MIN_PPI, 4, f->cmd);
if (f->spi)
test_inject_preemption(args, MIN_SPI, 4, f->cmd);
}
staticvoid test_restore_active(struct test_args *args, struct kvm_inject_desc *f)
{ /* Test up to 4 active IRQs. Same reason as in test_preemption. */ if (f->sgi)
guest_restore_active(args, MIN_SGI, 4, f->cmd);
if (f->ppi)
guest_restore_active(args, MIN_PPI, 4, f->cmd);
if (f->spi)
guest_restore_active(args, MIN_SPI, 4, f->cmd);
}
/* * Restore the active state of IRQs. This would happen when live * migrating IRQs in the middle of being handled.
*/
for_each_supported_activate_fn(args, set_active_fns, f)
test_restore_active(args, f);
GUEST_DONE();
}
staticvoid kvm_irq_line_check(struct kvm_vm *vm, uint32_t intid, int level, struct test_args *test_args, bool expect_failure)
{ int ret;
if (!expect_failure) {
kvm_arm_irq_line(vm, intid, level);
} else { /* The interface doesn't allow larger intid's. */ if (intid > KVM_ARM_IRQ_NUM_MASK) return;
ret = _kvm_arm_irq_line(vm, intid, level);
TEST_ASSERT(ret != 0 && errno == EINVAL, "Bad intid %i did not cause KVM_IRQ_LINE " "error: rc: %i errno: %i", intid, ret, errno);
}
}
void kvm_irq_set_level_info_check(int gic_fd, uint32_t intid, int level, bool expect_failure)
{ if (!expect_failure) {
kvm_irq_set_level_info(gic_fd, intid, level);
} else { int ret = _kvm_irq_set_level_info(gic_fd, intid, level); /* * The kernel silently fails for invalid SPIs and SGIs (which * are not level-sensitive). It only checks for intid to not * spill over 1U << 10 (the max reserved SPI). Also, callers * are supposed to mask the intid with 0x3ff (1023).
*/ if (intid > VGIC_MAX_RESERVED)
TEST_ASSERT(ret != 0 && errno == EINVAL, "Bad intid %i did not cause VGIC_GRP_LEVEL_INFO " "error: rc: %i errno: %i", intid, ret, errno); else
TEST_ASSERT(!ret, "KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO " "for intid %i failed, rc: %i errno: %i",
intid, ret, errno);
}
}
routing = kvm_gsi_routing_create(); for (i = intid; i < (uint64_t)intid + num; i++)
kvm_gsi_routing_irqchip_add(routing, i - MIN_SPI, i - MIN_SPI);
if (!expect_failure) {
kvm_gsi_routing_write(vm, routing);
} else {
ret = _kvm_gsi_routing_write(vm, routing); /* The kernel only checks e->irqchip.pin >= KVM_IRQCHIP_NUM_PINS */ if (((uint64_t)intid + num - 1 - MIN_SPI) >= KVM_IRQCHIP_NUM_PINS)
TEST_ASSERT(ret != 0 && errno == EINVAL, "Bad intid %u did not cause KVM_SET_GSI_ROUTING " "error: rc: %i errno: %i", intid, ret, errno); else
TEST_ASSERT(ret == 0, "KVM_SET_GSI_ROUTING " "for intid %i failed, rc: %i errno: %i",
intid, ret, errno);
}
}
staticvoid kvm_irq_write_ispendr_check(int gic_fd, uint32_t intid, struct kvm_vcpu *vcpu, bool expect_failure)
{ /* * Ignore this when expecting failure as invalid intids will lead to * either trying to inject SGIs when we configured the test to be * level_sensitive (or the reverse), or inject large intids which * will lead to writing above the ISPENDR register space (and we * don't want to do that either).
*/ if (!expect_failure)
kvm_irq_write_ispendr(gic_fd, intid, vcpu);
}
/* * There is no way to try injecting an SGI or PPI as the interface * starts counting from the first SPI (above the private ones), so just * exit.
*/ if (INTID_IS_SGI(intid) || INTID_IS_PPI(intid)) return;
/* * If expect_failure, then just to inject anyway. These * will silently fail. And in any case, the guest will check * that no actual interrupt was injected for those cases.
*/
for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++)
fd[f] = kvm_new_eventfd();
for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++) {
assert(i <= (uint64_t)UINT_MAX);
kvm_assign_irqfd(vm, i - MIN_SPI, fd[f]);
}
for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++) {
val = 1;
ret = write(fd[f], &val, sizeof(uint64_t));
TEST_ASSERT(ret == sizeof(uint64_t),
__KVM_SYSCALL_ERROR("write()", ret));
}
for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++)
close(fd[f]);
}
switch (get_ucall(vcpu, &uc)) { case UCALL_SYNC:
kvm_inject_get_call(vm, &uc, &inject_args);
run_guest_cmd(vcpu, gic_fd, &inject_args, &args); break; case UCALL_ABORT:
REPORT_GUEST_ASSERT(uc); break; case UCALL_DONE: goto done; default:
TEST_FAIL("Unknown ucall %lu", uc.cmd);
}
}
done:
close(gic_fd);
kvm_vm_free(vm);
}
staticvoid help(constchar *name)
{
printf( "\n" "usage: %s [-n num_irqs] [-e eoi_split] [-l level_sensitive]\n", name);
printf(" -n: specify number of IRQs to setup the vgic with. " "It has to be a multiple of 32 and between 64 and 1024.\n");
printf(" -e: if 1 then EOI is split into a write to DIR on top " "of writing EOI.\n");
printf(" -l: specify whether the IRQs are level-sensitive (1) or not (0).");
puts(""); exit(1);
}
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.