Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(irq): prevent interrupt reservation and improve error handling #97

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/arch/armv8/gic.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ void gicd_init()

/* No need to setup gicd->NSACR as all interrupts are setup to group 1 */

interrupts_reserve(platform.arch.gic.maintenance_id,
gic_maintenance_handler);
if(!interrupts_reserve(platform.arch.gic.maintenance_id,
gic_maintenance_handler)) {
ERROR("Failed to reserve GIC maintenance interrupt");
}
}

void gic_map_mmio();
Expand Down
5 changes: 4 additions & 1 deletion src/arch/riscv/iommu.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,10 @@ void rv_iommu_init(void)
rv_iommu.hw.reg_ptr->fqh = 0;

// Allocate IRQ for FQ
interrupts_reserve(platform.arch.iommu.fq_irq_id, rv_iommu_fq_irq_handler);
if(!interrupts_reserve(platform.arch.iommu.fq_irq_id, rv_iommu_fq_irq_handler)) {
ERROR("Failed to reserve IOMMU FQ interrupt");
}

interrupts_cpu_enable(platform.arch.iommu.fq_irq_id, true);

// Enable FQ (fqcsr)
Expand Down
4 changes: 3 additions & 1 deletion src/arch/riscv/sbi.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,5 +476,7 @@ void sbi_init()
}
}

interrupts_reserve(TIMR_INT_ID, sbi_timer_irq_handler);
if(!interrupts_reserve(TIMR_INT_ID, sbi_timer_irq_handler)) {
ERROR("Failed to reserve SBI TIMR_INT_ID interrupt");
}
}
4 changes: 2 additions & 2 deletions src/core/inc/interrupts.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct vm;
typedef void (*irq_handler_t)(irqid_t int_id);

void interrupts_init();
void interrupts_reserve(irqid_t int_id, irq_handler_t handler);
bool interrupts_reserve(irqid_t int_id, irq_handler_t handler);

void interrupts_cpu_sendipi(cpuid_t target_cpu, irqid_t ipi_id);
void interrupts_cpu_enable(irqid_t int_id, bool en);
Expand All @@ -27,7 +27,7 @@ void interrupts_clear(irqid_t int_id);
enum irq_res { HANDLED_BY_HYP, FORWARD_TO_VM };
enum irq_res interrupts_handle(irqid_t int_id);

void interrupts_vm_assign(struct vm *vm, irqid_t id);
bool interrupts_vm_assign(struct vm *vm, irqid_t id);

/* Must be implemented by architecture */

Expand Down
33 changes: 24 additions & 9 deletions src/core/interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

BITMAP_ALLOC(hyp_interrupt_bitmap, MAX_INTERRUPTS);
BITMAP_ALLOC(global_interrupt_bitmap, MAX_INTERRUPTS);
spinlock_t irq_reserve_lock = SPINLOCK_INITVAL;

irq_handler_t interrupt_handlers[MAX_INTERRUPTS];

Expand Down Expand Up @@ -40,7 +41,9 @@ inline void interrupts_init()
interrupts_arch_init();

if (cpu()->id == CPU_MASTER) {
interrupts_reserve(IPI_CPU_MSG, cpu_msg_handler);
if(!interrupts_reserve(IPI_CPU_MSG, cpu_msg_handler)) {
ERROR("Failed to reserve IPI_CPU_MSG interrupt");
}
}

interrupts_cpu_enable(IPI_CPU_MSG, true);
Expand Down Expand Up @@ -81,23 +84,35 @@ enum irq_res interrupts_handle(irqid_t int_id)
}
}

void interrupts_vm_assign(struct vm *vm, irqid_t id)
bool interrupts_vm_assign(struct vm *vm, irqid_t id)
{
if (interrupts_arch_conflict(global_interrupt_bitmap, id)) {
ERROR("Interrupts conflict, id = %d\n", id);
}
bool ret = false;

spin_lock(&irq_reserve_lock);
if (!interrupts_arch_conflict(global_interrupt_bitmap, id)) {
ret = true;
interrupts_arch_vm_assign(vm, id);

interrupts_arch_vm_assign(vm, id);
bitmap_set(vm->interrupt_bitmap, id);
bitmap_set(global_interrupt_bitmap, id);
}
spin_unlock(&irq_reserve_lock);

bitmap_set(vm->interrupt_bitmap, id);
bitmap_set(global_interrupt_bitmap, id);
return ret;
}

void interrupts_reserve(irqid_t int_id, irq_handler_t handler)
bool interrupts_reserve(irqid_t int_id, irq_handler_t handler)
{
bool ret = false;

spin_lock(&irq_reserve_lock);
if ((int_id < MAX_INTERRUPTS) && !interrupt_assigned(int_id)) {
ret = true;
interrupt_handlers[int_id] = handler;
bitmap_set(hyp_interrupt_bitmap, int_id);
bitmap_set(global_interrupt_bitmap, int_id);
}
spin_unlock(&irq_reserve_lock);

return ret;
}
4 changes: 3 additions & 1 deletion src/core/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ static void vm_init_dev(struct vm* vm, const struct vm_config* config)
}

for (size_t j = 0; j < dev->interrupt_num; j++) {
interrupts_vm_assign(vm, dev->interrupts[j]);
if(!interrupts_vm_assign(vm, dev->interrupts[j])) {
ERROR("Failed to assign interrupt id %d", dev->interrupts[j]);
}
}
}

Expand Down