Skip to content

Commit

Permalink
Added virtual to physical address mapping code.\nNot yet compiled or …
Browse files Browse the repository at this point in the history
…tested.
  • Loading branch information
lukego committed Dec 15, 2012
1 parent 865f183 commit 46bb8e3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/physmem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
static int pagemap_fd;

uint64_t phys_page(uint64_t virt_page)
{
if (pagemap_fd == 0) {
if ((pagemap_fd = fopen("/proc/%d/pagemap", O_RD)) <= 0) {
perror("open pagemap");
return 0;
}
}
uint64_t data;
int len;
len = pread(pagemap_fd, &data, sizeof(data), virt_page * sizeof(uint64_t));
if (len != sizeof(data)) {
perror("pread");
return 0;
}
if (data & (1<<63) == 0) {
fprintf(stderr, "page %x not present: %x", virt_page, data);
return 0;
}
return data & ((1 << 55) - 1);
}

23 changes: 23 additions & 0 deletions src/physmem.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module(...,package.seeall)

-- From virtual page to physical page
local cache = {}
local pagesize = 4096

-- Return the 64-bit physical address of virt_addr.
function map (virt_addr)
virt_addr = ffi.cast("int64_t", virt_addr)
local virtpage = tonumber(virt_addr / page_size)
local offset = tonumber(virt_addr % page_size)
local physpage = cache[virtpage] or resolve(virtpage)
return ffi.cast("int64_t", physpage * pagesize + offset)
end

-- Return (and cache) the physical page number of virtpage.
function resolve (virtpage)
local physpage = C.virt_to_phys(page)
if phys_page == 0 then error("Unable to resolve page " .. virt_page) end
cache[virtpage] = physpage
return phys_page
end

0 comments on commit 46bb8e3

Please sign in to comment.