-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added virtual to physical address mapping code.\nNot yet compiled or …
…tested.
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|