Skip to content

Commit

Permalink
add unit tests for address translation
Browse files Browse the repository at this point in the history
Signed-off-by: smallkirby <ssmallkirby@gmail.com>
  • Loading branch information
smallkirby committed Aug 15, 2024
1 parent c5f9e3b commit f267ef0
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions ymir/mem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,30 @@ pub fn virt2phys(addr: Virt) Phys {
/// This function just use simple calculation and does not walk page tables.
/// To do page table walk, use arch-specific functions.
pub fn phys2virt(addr: Phys) Virt {
return if (addr < ymir.kernel_base) b: {
// Direct mapping region.
break :b addr + ymir.direct_map_base;
} else b: {
// Kernel image mapping region.
break :b addr + ymir.kernel_base;
};
return addr + ymir.direct_map_base;
}

// ========================================

const testing = std.testing;

test {
testing.refAllDeclsRecursive(@This());
}

test "address translation" {
const direct_map_base = ymir.direct_map_base;
const kernel_base = ymir.kernel_base;

// virt -> phys
try testing.expectEqual(0x0, virt2phys(direct_map_base));
try testing.expectEqual(0x100, virt2phys(direct_map_base + 0x100));
try testing.expectEqual(ymir.arch.page_size * 0x100, virt2phys(direct_map_base + ymir.arch.page_size * 0x100));
try testing.expectEqual(kernel_base - direct_map_base - 1, virt2phys(kernel_base - 1));
try testing.expectEqual(0, virt2phys(kernel_base));
try testing.expectEqual(0x100000, virt2phys(kernel_base + 0x100000));

// phys -> virt
try testing.expectEqual(direct_map_base, phys2virt(0x0));
try testing.expectEqual(direct_map_base + 0x100, phys2virt(0x100));
}

0 comments on commit f267ef0

Please sign in to comment.