Fix sign extension bug in next_table_address (#372)

Fixes #362

Fix an issue where the left shift of the old table address would overwrite the sign extension, making the address non-canonical and leading to #GPs. This calculates the correct sign extension for the new table address.
This commit is contained in:
Isaac Woods
2017-12-05 19:22:29 +00:00
committed by Philipp Oppermann
parent 0a583ca73b
commit cf2c5550aa

View File

@@ -31,6 +31,15 @@ where
}
}
/*
* Addresses are expected to be canonical (bits 48-63 must be the same as bit 47), otherwise the
* CPU will #GP when we ask it to translate it.
*/
fn make_address_canonical(address : usize) -> usize {
let sign_extension = 0o177777_000_000_000_000_0000 * ((address >> 47) & 0b1);
(address & ((1 << 48) - 1)) | sign_extension
}
impl<L> Table<L>
where
L: HierarchicalLevel,
@@ -39,7 +48,7 @@ where
let entry_flags = self[index].flags();
if entry_flags.contains(PRESENT) && !entry_flags.contains(HUGE_PAGE) {
let table_address = self as *const _ as usize;
Some((table_address << 9) | (index << 12))
Some(make_address_canonical((table_address << 9) | (index << 12)))
} else {
None
}