vm: address-space: add function to translate virtual addresses to physical

This commit is contained in:
2026-03-15 22:22:25 +00:00
parent a2e918c428
commit 8a38d940cc
2 changed files with 46 additions and 3 deletions

View File

@@ -110,9 +110,9 @@ static struct vm_area *get_entry(
enum get_entry_flags flags)
{
/* `x` must be to the left of `y` */
#define LEFT_DIFF(x, y) ((y) ? ((y)->vma_base - (x)) : ((size_t)-1))
#define LEFT_DIFF(x, y) ((y) ? ((y)->vma_base - (x)) : ((size_t) - 1))
/* `x` must be to the right of `y` */
#define RIGHT_DIFF(x, y) ((y) ? ((y)->vma_limit - (x)) : ((size_t)-1))
#define RIGHT_DIFF(x, y) ((y) ? ((y)->vma_limit - (x)) : ((size_t) - 1))
struct btree_node *cur = region->s_mappings.b_root;
if (!cur) {
@@ -1413,6 +1413,42 @@ extern kern_status_t address_space_memmove_v(
return KERN_OK;
}
kern_status_t address_space_translate(
struct address_space *space,
virt_addr_t in,
phys_addr_t *out,
unsigned long *irq_flags)
{
if (in >= VM_KERNEL_BASE) {
return vm_virt_to_phys((const void *)in);
}
struct vm_area *area = get_entry(space, in, GET_ENTRY_EXACT);
if (!area || !area->vma_object) {
return KERN_NO_ENTRY;
}
off_t offset = in - area->vma_base + area->vma_object_offset;
struct vm_object *vmo = area->vma_object;
vm_object_lock(vmo);
address_space_unlock(space);
struct vm_page *pg = vm_object_get_page(
vmo,
offset,
VMO_ALLOCATE_MISSING_PAGE | VMO_REQUEST_MISSING_PAGE,
irq_flags);
if (!pg) {
return KERN_NO_ENTRY;
}
phys_addr_t paddr = vm_page_get_paddr(pg);
paddr += (in & VM_PAGE_MASK);
vm_object_unlock(vmo);
address_space_lock(space);
return paddr;
}
#ifdef TRACE
void address_space_dump(struct address_space *region)
{