Skip to content

Commit

Permalink
Improve handling of custom VM exceptions
Browse files Browse the repository at this point in the history
* Add more error checking (for overlapping etc)
* Do not enforce page alignment for the exception region. Certain
  devices are not page aligned and so if we try to emulate those
  we need to be able to register a region that does not align
  on a page-boundary.

Signed-off-by: Ivan Velickovic <[email protected]>
  • Loading branch information
Ivan-Velickovic committed Sep 20, 2024
1 parent 3bb3481 commit 1da3a55
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/arch/aarch64/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,30 +336,39 @@ bool fault_handle_unknown_syscall(size_t vcpu_id)
return fault_advance_vcpu(vcpu_id, &regs);
}

#define MAX_VM_EXCEPTION_HANDLERS 16

struct vm_exception_handler {
uintptr_t base;
uintptr_t end;
vm_exception_handler_t callback;
void *data;
};
#define MAX_VM_EXCEPTION_HANDLERS 16
struct vm_exception_handler registered_vm_exception_handlers[MAX_VM_EXCEPTION_HANDLERS];
size_t vm_exception_handler_index = 0;

static struct vm_exception_handler registered_vm_exception_handlers[MAX_VM_EXCEPTION_HANDLERS];
static size_t vm_exception_handler_index = 0;

bool fault_register_vm_exception_handler(uintptr_t base, size_t size, vm_exception_handler_t callback, void *data)
{
// @ivanv audit necessary here since this code was written very quickly. Other things to check such
// as the region of memory is not overlapping with other regions, also should have GIC_DIST regions
// use this API.
if (vm_exception_handler_index == MAX_VM_EXCEPTION_HANDLERS - 1) {
LOG_VMM_ERR("maximum number of VM exception handlers registered");
return false;
}

// @ivanv: use a define for page size? preMAture GENeraliZAATION
if (base % 0x1000 != 0) {
if (size == 0) {
LOG_VMM_ERR("registered VM exception handler with size 0\n");
return false;
}

for (int i = 0; i < vm_exception_handler_index; i++) {
struct vm_exception_handler *curr = &registered_vm_exception_handlers[i];
if (!(base >= curr->end || base + size <= curr->base)) {
LOG_VMM_ERR("VM exception handler [0x%lx..0x%lx), overlaps with another handler [0x%lx..0x%lx)\n",
base, base + size, curr->base, curr->end);
return false;
}
}

registered_vm_exception_handlers[vm_exception_handler_index] = (struct vm_exception_handler) {
.base = base,
.end = base + size,
Expand Down

0 comments on commit 1da3a55

Please sign in to comment.