Commit 92f2cc4a authored by Ricardo Koller's avatar Ricardo Koller Committed by Marc Zyngier
Browse files

KVM: selftests: aarch64: Level-sensitive interrupts tests in vgic_irq



Add a cmdline arg for using level-sensitive interrupts (vs the default
edge-triggered). Then move the handler into a generic handler function
that takes the type of interrupt (level vs. edge) as an arg.  When
handling line-sensitive interrupts it sets the line to low after
acknowledging the IRQ.

Signed-off-by: default avatarRicardo Koller <ricarkol@google.com>
Acked-by: default avatarAndrew Jones <drjones@redhat.com>
Signed-off-by: default avatarMarc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20211109023906.1091208-12-ricarkol@google.com
parent 0ad3ff4a
Loading
Loading
Loading
Loading
+86 −32
Original line number Original line Diff line number Diff line
@@ -30,6 +30,7 @@
struct test_args {
struct test_args {
	uint32_t nr_irqs; /* number of KVM supported IRQs. */
	uint32_t nr_irqs; /* number of KVM supported IRQs. */
	bool eoi_split; /* 1 is eoir+dir, 0 is eoir only */
	bool eoi_split; /* 1 is eoir+dir, 0 is eoir only */
	bool level_sensitive; /* 1 is level, 0 is edge */
};
};


/*
/*
@@ -57,27 +58,31 @@ static void *redist = (void *)GICR_BASE_GPA;


typedef enum {
typedef enum {
	KVM_INJECT_EDGE_IRQ_LINE = 1,
	KVM_INJECT_EDGE_IRQ_LINE = 1,
	KVM_SET_IRQ_LINE,
	KVM_SET_IRQ_LINE_HIGH,
} kvm_inject_cmd;
} kvm_inject_cmd;


struct kvm_inject_args {
struct kvm_inject_args {
	kvm_inject_cmd cmd;
	kvm_inject_cmd cmd;
	uint32_t first_intid;
	uint32_t first_intid;
	uint32_t num;
	uint32_t num;
	int level;
};
};


/* Used on the guest side to perform the hypercall. */
/* Used on the guest side to perform the hypercall. */
static void kvm_inject_call(kvm_inject_cmd cmd, uint32_t first_intid, uint32_t num);
static void kvm_inject_call(kvm_inject_cmd cmd, uint32_t first_intid,

			uint32_t num, int level);
#define KVM_INJECT(cmd, intid)							\
	kvm_inject_call(cmd, intid, 1)

#define KVM_INJECT_MULTI(cmd, intid, num)					\
	kvm_inject_call(cmd, intid, num)


/* Used on the host side to get the hypercall info. */
/* Used on the host side to get the hypercall info. */
static void kvm_inject_get_call(struct kvm_vm *vm, struct ucall *uc,
static void kvm_inject_get_call(struct kvm_vm *vm, struct ucall *uc,
		struct kvm_inject_args *args);
		struct kvm_inject_args *args);


#define KVM_INJECT(cmd, intid)							\
	kvm_inject_call(cmd, intid, 1, -1 /* not used */)

#define KVM_INJECT_MULTI(cmd, intid, num)					\
	kvm_inject_call(cmd, intid, num, -1 /* not used */)

struct kvm_inject_desc {
struct kvm_inject_desc {
	kvm_inject_cmd cmd;
	kvm_inject_cmd cmd;
	/* can inject PPIs, PPIs, and/or SPIs. */
	/* can inject PPIs, PPIs, and/or SPIs. */
@@ -90,6 +95,12 @@ static struct kvm_inject_desc inject_edge_fns[] = {
	{ 0, },
	{ 0, },
};
};


static struct kvm_inject_desc inject_level_fns[] = {
	/*                                      sgi    ppi    spi */
	{ KVM_SET_IRQ_LINE_HIGH,		false, true,  true },
	{ 0, },
};

#define for_each_inject_fn(t, f)						\
#define for_each_inject_fn(t, f)						\
	for ((f) = (t); (f)->cmd; (f)++)
	for ((f) = (t); (f)->cmd; (f)++)


@@ -114,7 +125,9 @@ static uint64_t gic_read_ap1r0(void)
	return reg;
	return reg;
}
}


static void guest_irq_generic_handler(bool eoi_split)
static void guest_set_irq_line(uint32_t intid, uint32_t level);

static void guest_irq_generic_handler(bool eoi_split, bool level_sensitive)
{
{
	uint32_t intid = gic_get_and_ack_irq();
	uint32_t intid = gic_get_and_ack_irq();


@@ -123,8 +136,12 @@ static void guest_irq_generic_handler(bool eoi_split)


	GUEST_ASSERT(gic_irq_get_active(intid));
	GUEST_ASSERT(gic_irq_get_active(intid));


	if (!level_sensitive)
		GUEST_ASSERT(!gic_irq_get_pending(intid));
		GUEST_ASSERT(!gic_irq_get_pending(intid));


	if (level_sensitive)
		guest_set_irq_line(intid, 0);

	GUEST_ASSERT(intid < MAX_SPI);
	GUEST_ASSERT(intid < MAX_SPI);
	irqnr_received[intid] += 1;
	irqnr_received[intid] += 1;
	irq_handled += 1;
	irq_handled += 1;
@@ -138,12 +155,14 @@ static void guest_irq_generic_handler(bool eoi_split)
	GUEST_ASSERT(!gic_irq_get_pending(intid));
	GUEST_ASSERT(!gic_irq_get_pending(intid));
}
}


static void kvm_inject_call(kvm_inject_cmd cmd, uint32_t first_intid, uint32_t num)
static void kvm_inject_call(kvm_inject_cmd cmd, uint32_t first_intid,
			uint32_t num, int level)
{
{
	struct kvm_inject_args args = {
	struct kvm_inject_args args = {
		.cmd = cmd,
		.cmd = cmd,
		.first_intid = first_intid,
		.first_intid = first_intid,
		.num = num,
		.num = num,
		.level = level,
	};
	};
	GUEST_SYNC(&args);
	GUEST_SYNC(&args);
}
}
@@ -158,19 +177,21 @@ do { \
#define CAT_HELPER(a, b) a ## b
#define CAT_HELPER(a, b) a ## b
#define CAT(a, b) CAT_HELPER(a, b)
#define CAT(a, b) CAT_HELPER(a, b)
#define PREFIX guest_irq_handler_
#define PREFIX guest_irq_handler_
#define GUEST_IRQ_HANDLER_NAME(split) CAT(PREFIX, split)
#define GUEST_IRQ_HANDLER_NAME(split, lev) CAT(PREFIX, CAT(split, lev))
#define GENERATE_GUEST_IRQ_HANDLER(split)					\
#define GENERATE_GUEST_IRQ_HANDLER(split, lev)					\
static void CAT(PREFIX, split)(struct ex_regs *regs)				\
static void CAT(PREFIX, CAT(split, lev))(struct ex_regs *regs)			\
{										\
{										\
	guest_irq_generic_handler(split);					\
	guest_irq_generic_handler(split, lev);					\
}
}


GENERATE_GUEST_IRQ_HANDLER(0);
GENERATE_GUEST_IRQ_HANDLER(0, 0);
GENERATE_GUEST_IRQ_HANDLER(1);
GENERATE_GUEST_IRQ_HANDLER(0, 1);
GENERATE_GUEST_IRQ_HANDLER(1, 0);
GENERATE_GUEST_IRQ_HANDLER(1, 1);


static void (*guest_irq_handlers[2])(struct ex_regs *) = {
static void (*guest_irq_handlers[2][2])(struct ex_regs *) = {
	GUEST_IRQ_HANDLER_NAME(0),
	{GUEST_IRQ_HANDLER_NAME(0, 0), GUEST_IRQ_HANDLER_NAME(0, 1),},
	GUEST_IRQ_HANDLER_NAME(1),
	{GUEST_IRQ_HANDLER_NAME(1, 0), GUEST_IRQ_HANDLER_NAME(1, 1),},
};
};


static void reset_priorities(struct test_args *args)
static void reset_priorities(struct test_args *args)
@@ -181,6 +202,11 @@ static void reset_priorities(struct test_args *args)
		gic_set_priority(i, IRQ_DEFAULT_PRIO_REG);
		gic_set_priority(i, IRQ_DEFAULT_PRIO_REG);
}
}


static void guest_set_irq_line(uint32_t intid, uint32_t level)
{
	kvm_inject_call(KVM_SET_IRQ_LINE, intid, 1, level);
}

static void guest_inject(struct test_args *args,
static void guest_inject(struct test_args *args,
		uint32_t first_intid, uint32_t num,
		uint32_t first_intid, uint32_t num,
		kvm_inject_cmd cmd)
		kvm_inject_cmd cmd)
@@ -257,10 +283,12 @@ static void test_inject_preemption(struct test_args *args,
	for (i = 0; i < num; i++) {
	for (i = 0; i < num; i++) {
		uint32_t tmp;
		uint32_t tmp;
		intid = i + first_intid;
		intid = i + first_intid;
		kvm_inject_call(cmd, intid, 1);
		KVM_INJECT(cmd, intid);
		/* Each successive IRQ will preempt the previous one. */
		/* Each successive IRQ will preempt the previous one. */
		tmp = wait_for_and_activate_irq();
		tmp = wait_for_and_activate_irq();
		GUEST_ASSERT_EQ(tmp, intid);
		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. */
	/* finish handling the IRQs starting with the highest priority one. */
@@ -321,22 +349,29 @@ static void test_preemption(struct test_args *args, struct kvm_inject_desc *f)
static void guest_code(struct test_args args)
static void guest_code(struct test_args args)
{
{
	uint32_t i, nr_irqs = args.nr_irqs;
	uint32_t i, nr_irqs = args.nr_irqs;
	struct kvm_inject_desc *f;
	bool level_sensitive = args.level_sensitive;
	struct kvm_inject_desc *f, *inject_fns;


	gic_init(GIC_V3, 1, dist, redist);
	gic_init(GIC_V3, 1, dist, redist);


	for (i = 0; i < nr_irqs; i++)
	for (i = 0; i < nr_irqs; i++)
		gic_irq_enable(i);
		gic_irq_enable(i);


	for (i = MIN_SPI; i < nr_irqs; i++)
		gic_irq_set_config(i, !args.level_sensitive);

	gic_set_eoi_split(args.eoi_split);
	gic_set_eoi_split(args.eoi_split);


	reset_priorities(&args);
	reset_priorities(&args);
	gic_set_priority_mask(CPU_PRIO_MASK);
	gic_set_priority_mask(CPU_PRIO_MASK);


	inject_fns  = level_sensitive ? inject_level_fns
				      : inject_edge_fns;

	local_irq_enable();
	local_irq_enable();


	/* Start the tests. */
	/* Start the tests. */
	for_each_inject_fn(inject_edge_fns, f) {
	for_each_inject_fn(inject_fns, f) {
		test_injection(&args, f);
		test_injection(&args, f);
		test_preemption(&args, f);
		test_preemption(&args, f);
	}
	}
@@ -351,6 +386,7 @@ static void run_guest_cmd(struct kvm_vm *vm, int gic_fd,
	kvm_inject_cmd cmd = inject_args->cmd;
	kvm_inject_cmd cmd = inject_args->cmd;
	uint32_t intid = inject_args->first_intid;
	uint32_t intid = inject_args->first_intid;
	uint32_t num = inject_args->num;
	uint32_t num = inject_args->num;
	int level = inject_args->level;
	uint32_t i;
	uint32_t i;


	assert(intid < UINT_MAX - num);
	assert(intid < UINT_MAX - num);
@@ -362,6 +398,14 @@ static void run_guest_cmd(struct kvm_vm *vm, int gic_fd,
		for (i = intid; i < intid + num; i++)
		for (i = intid; i < intid + num; i++)
			kvm_arm_irq_line(vm, i, 0);
			kvm_arm_irq_line(vm, i, 0);
		break;
		break;
	case KVM_SET_IRQ_LINE:
		for (i = intid; i < intid + num; i++)
			kvm_arm_irq_line(vm, i, level);
		break;
	case KVM_SET_IRQ_LINE_HIGH:
		for (i = intid; i < intid + num; i++)
			kvm_arm_irq_line(vm, i, 1);
		break;
	default:
	default:
		break;
		break;
	}
	}
@@ -380,11 +424,12 @@ static void kvm_inject_get_call(struct kvm_vm *vm, struct ucall *uc,


static void print_args(struct test_args *args)
static void print_args(struct test_args *args)
{
{
	printf("nr-irqs=%d eoi-split=%d\n",
	printf("nr-irqs=%d level-sensitive=%d eoi-split=%d\n",
			args->nr_irqs, args->eoi_split);
			args->nr_irqs, args->level_sensitive,
			args->eoi_split);
}
}


static void test_vgic(uint32_t nr_irqs, bool eoi_split)
static void test_vgic(uint32_t nr_irqs, bool level_sensitive, bool eoi_split)
{
{
	struct ucall uc;
	struct ucall uc;
	int gic_fd;
	int gic_fd;
@@ -393,6 +438,7 @@ static void test_vgic(uint32_t nr_irqs, bool eoi_split)


	struct test_args args = {
	struct test_args args = {
		.nr_irqs = nr_irqs,
		.nr_irqs = nr_irqs,
		.level_sensitive = level_sensitive,
		.eoi_split = eoi_split,
		.eoi_split = eoi_split,
	};
	};


@@ -411,7 +457,7 @@ static void test_vgic(uint32_t nr_irqs, bool eoi_split)
			GICD_BASE_GPA, GICR_BASE_GPA);
			GICD_BASE_GPA, GICR_BASE_GPA);


	vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT,
	vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT,
			guest_irq_handlers[args.eoi_split]);
		guest_irq_handlers[args.eoi_split][args.level_sensitive]);


	while (1) {
	while (1) {
		vcpu_run(vm, VCPU_ID);
		vcpu_run(vm, VCPU_ID);
@@ -442,11 +488,12 @@ static void help(const char *name)
{
{
	printf(
	printf(
	"\n"
	"\n"
	"usage: %s [-n num_irqs] [-e eoi_split]\n", name);
	"usage: %s [-n num_irqs] [-e eoi_split] [-l level_sensitive]\n", name);
	printf(" -n: specify the number of IRQs to configure the vgic with. "
	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");
		"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 "
	printf(" -e: if 1 then EOI is split into a write to DIR on top "
		"of writing EOI.\n");
		"of writing EOI.\n");
	printf(" -l: specify whether the IRQs are level-sensitive (1) or not (0).");
	puts("");
	puts("");
	exit(1);
	exit(1);
}
}
@@ -455,13 +502,14 @@ int main(int argc, char **argv)
{
{
	uint32_t nr_irqs = 64;
	uint32_t nr_irqs = 64;
	bool default_args = true;
	bool default_args = true;
	bool level_sensitive = false;
	int opt;
	int opt;
	bool eoi_split = false;
	bool eoi_split = false;


	/* Tell stdout not to buffer its content */
	/* Tell stdout not to buffer its content */
	setbuf(stdout, NULL);
	setbuf(stdout, NULL);


	while ((opt = getopt(argc, argv, "hn:e:")) != -1) {
	while ((opt = getopt(argc, argv, "hn:e:l:")) != -1) {
		switch (opt) {
		switch (opt) {
		case 'n':
		case 'n':
			nr_irqs = atoi(optarg);
			nr_irqs = atoi(optarg);
@@ -472,6 +520,10 @@ int main(int argc, char **argv)
			eoi_split = (bool)atoi(optarg);
			eoi_split = (bool)atoi(optarg);
			default_args = false;
			default_args = false;
			break;
			break;
		case 'l':
			level_sensitive = (bool)atoi(optarg);
			default_args = false;
			break;
		case 'h':
		case 'h':
		default:
		default:
			help(argv[0]);
			help(argv[0]);
@@ -483,10 +535,12 @@ int main(int argc, char **argv)
	 * combinations.
	 * combinations.
	 */
	 */
	if (default_args) {
	if (default_args) {
		test_vgic(nr_irqs, false /* eoi_split */);
		test_vgic(nr_irqs, false /* level */, false /* eoi_split */);
		test_vgic(nr_irqs, true /* eoi_split */);
		test_vgic(nr_irqs, false /* level */, true /* eoi_split */);
		test_vgic(nr_irqs, true /* level */, false /* eoi_split */);
		test_vgic(nr_irqs, true /* level */, true /* eoi_split */);
	} else {
	} else {
		test_vgic(nr_irqs, eoi_split);
		test_vgic(nr_irqs, level_sensitive, eoi_split);
	}
	}


	return 0;
	return 0;