x86_64 icon indicating copy to clipboard operation
x86_64 copied to clipboard

Provide convenience methods to create PageTables

Open vinaychandra opened this issue 4 years ago • 9 comments

The current implementations of offset and recursive page table require that new to be called with an already existing page table. Provide convenience methods to create more page tables from an existing page table or by taking limits as a parameter to the new function.

vinaychandra avatar Mar 13 '20 02:03 vinaychandra

Could you clarify what you mean with "creating more page tables from an existing page table"? Do you mean to create new independent 4-level page table hierarchies for creating other processes?

by taking limits as a parameter to the new function

What kind of limits do you mean?

phil-opp avatar Mar 13 '20 09:03 phil-opp

Yes, for creating other processes. The limits in sense, for offset page table, the offset and max address may be for example.

vinaychandra avatar Mar 13 '20 14:03 vinaychandra

Ok, thanks for clarifying! I'm still not sure how such a function should look though. The problem is that the OffsetPageTable and the other types only store a &mut PageTable reference internally, and thus do not have ownership of the PageTable instance. We designed it this way to support both static page tables and heap allocated page tables.

One possible way for creating a new page table hierarchy could be:

let level_4_page_table = Box::leak(Box::new(PageTable::new()));
let mapper = OffsetPageTable::new(level_4_page_table);

This approach works, but it does not handle deallocation of the page table. So you need to do Box::from_raw with the level_4_page_table pointer before dropping the process. (You will also need to free all page table frames. We don't have a good solution for that yet.)

phil-opp avatar Mar 13 '20 15:03 phil-opp

Calling OffsetPageTable::new doesn't update the page table passed to it. So, we need to update the page table as well (Please correct me if i am wrong).

vinaychandra avatar Mar 14 '20 01:03 vinaychandra

What do you mean with "update"?

phil-opp avatar Mar 14 '20 08:03 phil-opp

Creating entries in the page table. Does calling new automatically create the entries? AFAIK it doesn't.

vinaychandra avatar Mar 14 '20 14:03 vinaychandra

Ah, I think I understand what you mean now. You basically propose to add methods like the following:

impl OffsetPageTable {
    /// Creates a new level 4 page table that includes the physical memory mapping.
    ///
    /// Returns a tuple of the new `PageTable` and the offset of the physical memory
    /// mapping. These values can then be passed to the `OffsetPageTable::new`
    /// function.
    pub fn new_level_4_table(&self, frame_allocator: A) -> (PageTable, usize) {…}
}

impl RecursivePageTable {
    /// Creates a new level 4 page table with a single recursive entry.
    ///
    /// Returns a tuple of the new `PageTable` and the index of the recursive entry.
    /// These values can then be passed to the `RecursivePageTable::new` function.
    pub fn new_level_4_table(&self) -> (PageTable, PageTableIndex) {…}
}

Did I get understand that right?

Such methods seem like a useful addition and I'd be happy to merge a PR for that!

phil-opp avatar Mar 16 '20 16:03 phil-opp

That is correct. It could also be done so that we pass in the offset instead of it auto deciding an offset but that's optional.

vinaychandra avatar Mar 16 '20 16:03 vinaychandra

Oh yeah, that makes sense!

phil-opp avatar Mar 16 '20 17:03 phil-opp