perf(engine): keep a fetch's variant and its number while the memory checks, native System features, FP trap, SVCR.SM and timing tier they derive from hold, and give every page a fetch and its refills take the fetch's number in that page's guard unless a page set gives it a variant of its own, where each fetch built its variant anew and found its number by a whole-variant compare, and each page taken, its leaf's other pages and each refilled page compared their variant again (fetch_path_cost with 16 variants: 58 to 43 ns a fetch held by the table, 220 to 206 ns with a TLBI of everything every 840 fetches; tlbi_keep_cost refill 804 to 753 us a TLBI)
This commit is contained in:
1 parent
4d689f4a55
commit
8b970e33a9
5 files changed
+165
-29
No files matched your search
@@ -690,6 +690,19 @@ const VARIANT_MEMO: usize = 4;
|
||||
|
||||
/// [`Machine::variant_memo`]: variants with their numbers in the registry
|
||||
/// at `registry`, the oldest at `next`.
|
||||
/// What a fetch's variant derives from besides the memory checks and the
|
||||
/// native System features, which `version` (`Machine::derived_version`)
|
||||
/// stands for, and the registry its number is in.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
struct FetchInputs {
|
||||
version: u64,
|
||||
registry: usize,
|
||||
fp: bool,
|
||||
single: bool,
|
||||
scalable: Option<bool>,
|
||||
timing: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct VariantMemo {
|
||||
entries: [core::cell::Cell<Option<(Variant, VariantId)>>; VARIANT_MEMO],
|
||||
@@ -1056,6 +1069,12 @@ pub struct Machine<H: Host, D: Devices> {
|
||||
memory_checks: Option<MemoryChecks>,
|
||||
/// [`Variant::system`] for the current EL, while valid.
|
||||
native_system: Option<NativeSystem>,
|
||||
/// How many times `memory_checks` and `native_system` were derived
|
||||
/// anew, which every change of either follows.
|
||||
derived_version: u64,
|
||||
/// A fetch's variant and its number, with what they were derived from
|
||||
/// ([`Machine::fetch_variant`]).
|
||||
fetch_variant: Option<(FetchInputs, Variant, VariantId)>,
|
||||
/// Whether blocks that branch back to their own start run their
|
||||
/// iterations inside themselves (`NativeSystem::self_loops`): on, but
|
||||
/// for tests that compare both forms.
|
||||
@@ -1550,6 +1569,8 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
floating_point: None,
|
||||
memory_checks: None,
|
||||
native_system: None,
|
||||
derived_version: 0,
|
||||
fetch_variant: None,
|
||||
self_loops: true,
|
||||
regions: true,
|
||||
event_sample: None,
|
||||
|
||||
@@ -77,9 +77,13 @@ pub(super) fn grow_table(
|
||||
impl<H: Host, D: Devices> Machine<H, D> {
|
||||
pub(super) fn fetch_block(&mut self, pc: u64, single: bool) -> Result<Fetched, Stop> {
|
||||
self.linked_at_install = None;
|
||||
let context = self.current_variant(single, false);
|
||||
let (context, context_id) = self.fetch_variant(single);
|
||||
// The numbers of the fetch's variant in either page guard, for every
|
||||
// page the fetch takes ([`Self::numbered_page_variant`]).
|
||||
let mut numbers: PageNumbers = [None; 2];
|
||||
numbers[usize::from(context.guarded_page)] = Some(context_id);
|
||||
let el = self.cpu.pstate.el | u8::from(self.cpu.pstate.guarded) << 4;
|
||||
let contexts = self.contexts(el, context);
|
||||
let contexts = self.numbered_contexts(el, context, context_id);
|
||||
self.chain.set_contexts(contexts);
|
||||
if !self.dropped_refills.is_empty() || !self.refills.is_empty() {
|
||||
let refilling = self.host.counter();
|
||||
@@ -87,7 +91,7 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
self.refill_dropped(contexts);
|
||||
}
|
||||
if !self.refills.is_empty() {
|
||||
self.refill_pages(el, context);
|
||||
self.refill_pages(el, context, &mut numbers);
|
||||
}
|
||||
self.statistics.refill_ticks += self.host.counter().wrapping_sub(refilling);
|
||||
}
|
||||
@@ -148,19 +152,19 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
// non-global fetch): the page is recorded, and dispatchers look
|
||||
// up, in their successors.
|
||||
if self.translation_generation != generation {
|
||||
let contexts = self.contexts(el, context);
|
||||
let contexts = self.numbered_contexts(el, context, context_id);
|
||||
self.chain.set_contexts(contexts);
|
||||
}
|
||||
let current = Variant {
|
||||
guarded_page: self.fetch_guarded(pc),
|
||||
..context
|
||||
};
|
||||
let variant = self.page_variant(current, physical);
|
||||
let (variant, variant_id) = self.numbered_page_variant(current, physical, &mut numbers);
|
||||
// The block another context, address space or fetch of the frame
|
||||
// recorded: what a TLBI ALL or a process switch leaves.
|
||||
if !single && let Some(id) = self.known_variant(variant) {
|
||||
if !single && let Some(id) = variant_id {
|
||||
let key = self.block_key(physical, id);
|
||||
self.translate_leaf(pc, key, physical, current);
|
||||
self.numbered_leaf(pc, key, physical, current, &mut numbers);
|
||||
let table = &self.code.table;
|
||||
let target = table.find(table.slot(pc, key), pc, key);
|
||||
// Every release of a block forgets its entries; the block is
|
||||
@@ -191,8 +195,8 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
self.entered(entry.block);
|
||||
return Ok(Fetched::Block(entry.block));
|
||||
}
|
||||
let indexed = self
|
||||
.known_variant(variant)
|
||||
let indexed = variant_id
|
||||
.or_else(|| self.known_variant(variant))
|
||||
.and_then(|id| self.code.find(pc, physical, id));
|
||||
self.statistics.fetches[if indexed.is_some() { 3 } else { 4 }] += 1;
|
||||
let index = match indexed {
|
||||
@@ -227,7 +231,7 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
});
|
||||
if !single {
|
||||
let key = self.block_key(physical, self.code.blocks[index].variant);
|
||||
self.translate_leaf(pc, key, physical, current);
|
||||
self.numbered_leaf(pc, key, physical, current, &mut numbers);
|
||||
}
|
||||
self.remember_or_link(pc, index);
|
||||
self.entered(index);
|
||||
@@ -317,13 +321,55 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
/// `page_variant`), then the other pages of its leaf
|
||||
/// ([`Self::take_leaf_pages`]).
|
||||
pub(super) fn translate_leaf(&mut self, pc: u64, key: u64, physical: u64, current: Variant) {
|
||||
self.numbered_leaf(pc, key, physical, current, &mut [None; 2]);
|
||||
}
|
||||
|
||||
/// [`Self::translate_leaf`] with what the fetch numbered of its variant
|
||||
/// (`numbers`, [`Self::numbered_page_variant`]).
|
||||
fn numbered_leaf(
|
||||
&mut self,
|
||||
pc: u64,
|
||||
key: u64,
|
||||
physical: u64,
|
||||
current: Variant,
|
||||
numbers: &mut PageNumbers,
|
||||
) {
|
||||
self.translate_page(pc, key);
|
||||
if let Some(walk) = self.cached_fetch(pc) {
|
||||
let record = self.fetch_record(pc);
|
||||
self.take_leaf_pages(pc, physical, walk.size, current, walk.global, record);
|
||||
let leaf = Leaf {
|
||||
size: walk.size,
|
||||
global: walk.global,
|
||||
record,
|
||||
};
|
||||
self.take_leaf_pages(pc, physical, leaf, current, numbers);
|
||||
}
|
||||
}
|
||||
|
||||
/// `page_variant(current, physical)` and its number, through `numbers`:
|
||||
/// those of `current` in an unguarded and a guarded page, as far as
|
||||
/// known, which every page the page's sets leave alone shares. A fetch
|
||||
/// numbers its variant once, and each page it and its refills take only
|
||||
/// where a set gives the page a variant of its own. `numbers` hold for
|
||||
/// the one variant `current` is in either guard.
|
||||
pub(super) fn numbered_page_variant(
|
||||
&self,
|
||||
current: Variant,
|
||||
physical: u64,
|
||||
numbers: &mut PageNumbers,
|
||||
) -> (Variant, Option<VariantId>) {
|
||||
let (variant, touched) = self.page_variant_of(current, physical);
|
||||
if touched {
|
||||
return (variant, self.known_variant(variant));
|
||||
}
|
||||
let number = &mut numbers[usize::from(variant.guarded_page)];
|
||||
if number.is_none() {
|
||||
*number = self.known_variant(variant);
|
||||
}
|
||||
debug_assert_eq!(*number, self.known_variant(variant), "numbers of another variant");
|
||||
(variant, *number)
|
||||
}
|
||||
|
||||
/// Give the ITLB the other 4 KiB pages of the [`LEAF_FILL`] granule
|
||||
/// around `pc` that the leaf of `size` bytes holding `pc` maps, whose
|
||||
/// page is in the frame of `physical`, as a fetch of each would take
|
||||
@@ -340,10 +386,13 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
&mut self,
|
||||
pc: u64,
|
||||
physical: u64,
|
||||
size: u64,
|
||||
Leaf {
|
||||
size,
|
||||
global,
|
||||
record,
|
||||
}: Leaf,
|
||||
current: Variant,
|
||||
global: bool,
|
||||
record: Option<tlbi_keep::WalkRecord>,
|
||||
numbers: &mut PageNumbers,
|
||||
) {
|
||||
let span = size.min(LEAF_FILL);
|
||||
if span <= FETCH_PAGE {
|
||||
@@ -360,8 +409,7 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
continue;
|
||||
}
|
||||
let other_frame = frame.wrapping_add(other).wrapping_sub(page);
|
||||
let variant = self.page_variant(current, other_frame);
|
||||
let Some(id) = self.known_variant(variant) else {
|
||||
let (_, Some(id)) = self.numbered_page_variant(current, other_frame, numbers) else {
|
||||
continue;
|
||||
};
|
||||
let key = self.block_key(other_frame, id);
|
||||
@@ -729,7 +777,7 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
/// page, as a walk cache lets hardware walk ([`mmu::WalkMemo`]). Nothing
|
||||
/// in the batch runs guest code or changes the controls, and the walks
|
||||
/// make no descriptor updates; the table is dropped with the batch.
|
||||
pub(super) fn refill_pages(&mut self, el: u8, context: Variant) {
|
||||
pub(super) fn refill_pages(&mut self, el: u8, context: Variant, numbers: &mut PageNumbers) {
|
||||
let generation = self.translation_generation;
|
||||
let mut quiet = QuietWalk::new(&self.cpu);
|
||||
for pc in core::mem::take(&mut self.refills) {
|
||||
@@ -749,8 +797,7 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
guarded_page: walk.guarded,
|
||||
..context
|
||||
};
|
||||
let variant = self.page_variant(current, walk.physical);
|
||||
if let Some(id) = self.known_variant(variant) {
|
||||
if let (_, Some(id)) = self.numbered_page_variant(current, walk.physical, numbers) {
|
||||
let key = self.block_key(walk.physical, id);
|
||||
self.take_page(
|
||||
pc,
|
||||
@@ -761,14 +808,12 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
walk.record,
|
||||
);
|
||||
}
|
||||
self.take_leaf_pages(
|
||||
pc,
|
||||
walk.physical,
|
||||
walk.size,
|
||||
current,
|
||||
walk.global,
|
||||
walk.record,
|
||||
);
|
||||
let leaf = Leaf {
|
||||
size: walk.size,
|
||||
global: walk.global,
|
||||
record: walk.record,
|
||||
};
|
||||
self.take_leaf_pages(pc, walk.physical, leaf, current, numbers);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1172,6 +1217,43 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
}
|
||||
}
|
||||
|
||||
/// [`Self::current_variant`] of a fetch (`single` or not, in no guarded
|
||||
/// page) and its number, kept while what they derive from holds: the
|
||||
/// memory checks and native System features as derived last (a change
|
||||
/// of either derives them anew, which moves `derived_version`), FP's
|
||||
/// trap, SVCR.SM, the timing tier and the registry.
|
||||
pub(super) fn fetch_variant(&mut self, single: bool) -> (Variant, VariantId) {
|
||||
let fp = self.floating_point_enabled();
|
||||
if self.memory_checks.is_none() {
|
||||
self.memory_checks();
|
||||
}
|
||||
if self.native_system.is_none() {
|
||||
self.native_system();
|
||||
}
|
||||
let inputs = FetchInputs {
|
||||
version: self.derived_version,
|
||||
registry: Arc::as_ptr(&self.variants) as usize,
|
||||
fp,
|
||||
single,
|
||||
scalable: self
|
||||
.cpu
|
||||
.scalable
|
||||
.is_some()
|
||||
.then(|| self.cpu.svcr() & 1 != 0),
|
||||
timing: self.cpu.pstate.dit && self.timing_target().is_some(),
|
||||
};
|
||||
if let Some((held, variant, id)) = self.fetch_variant
|
||||
&& held == inputs
|
||||
{
|
||||
debug_assert_eq!(variant, self.current_variant(single, false));
|
||||
return (variant, id);
|
||||
}
|
||||
let variant = self.current_variant(single, false);
|
||||
let id = self.variant_id(variant);
|
||||
self.fetch_variant = Some((inputs, variant, id));
|
||||
(variant, id)
|
||||
}
|
||||
|
||||
/// `variant`'s id, added to [`Self::variants`] the first time by
|
||||
/// whichever machine of the board meets it first.
|
||||
pub(super) fn variant_id(&mut self, variant: Variant) -> VariantId {
|
||||
@@ -2463,6 +2545,18 @@ enum Taken {
|
||||
/// 16 KiB page, the granule XNU maps code with, in 4 KiB ITLB pages.
|
||||
const LEAF_FILL: u64 = 1 << 14;
|
||||
|
||||
/// The numbers of one variant in an unguarded page (0) and a guarded one
|
||||
/// (1), as far as known ([`Machine::numbered_page_variant`]).
|
||||
pub(super) type PageNumbers = [Option<VariantId>; 2];
|
||||
|
||||
/// What [`Machine::take_leaf_pages`] takes of the walk of a leaf: the bytes
|
||||
/// it maps, its global bit and its record.
|
||||
struct Leaf {
|
||||
size: u64,
|
||||
global: bool,
|
||||
record: Option<tlbi_keep::WalkRecord>,
|
||||
}
|
||||
|
||||
/// The most pages one forgotten context leaves to walk again
|
||||
/// ([`Machine::refill_after`]). At 512 the cap bound: after each local TLBI
|
||||
/// of everything a PE refilled about 510 pages and then missed about 600
|
||||
|
||||
@@ -292,6 +292,11 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
/// forgot.
|
||||
pub(super) fn contexts(&mut self, el: u8, variant: Variant) -> [u64; 2] {
|
||||
let id = self.variant_id(variant);
|
||||
self.numbered_contexts(el, variant, id)
|
||||
}
|
||||
|
||||
/// [`Self::contexts`] of `variant`, numbered `id`.
|
||||
pub(super) fn numbered_contexts(&mut self, el: u8, variant: Variant, id: VariantId) -> [u64; 2] {
|
||||
loop {
|
||||
let generation = self.translation_generation;
|
||||
let contexts = [0, 1].map(|half| self.numbered_context(el, variant, id, half));
|
||||
|
||||
@@ -433,20 +433,30 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
/// `variant` for a block of the fetch page holding `physical`: in the
|
||||
/// translated variant when the page needs it, through the host's
|
||||
/// translation helper.
|
||||
pub(super) fn page_variant(&self, mut variant: Variant, physical: u64) -> Variant {
|
||||
pub(super) fn page_variant(&self, variant: Variant, physical: u64) -> Variant {
|
||||
self.page_variant_of(variant, physical).0
|
||||
}
|
||||
|
||||
/// [`Self::page_variant`], and whether the page's sets touched
|
||||
/// `variant` at all: when not, it is `variant` itself.
|
||||
pub(super) fn page_variant_of(&self, mut variant: Variant, physical: u64) -> (Variant, bool) {
|
||||
let mut touched = false;
|
||||
if self.page_translated(physical)
|
||||
&& let Some(helper) = self.host.translation_helper()
|
||||
{
|
||||
variant.translated = true;
|
||||
variant.checks = variant.checks.with_translation(helper);
|
||||
touched = true;
|
||||
}
|
||||
if self.page_tagged(physical) {
|
||||
variant.checks = variant.checks.with_top_byte(self.regime_top_byte());
|
||||
touched = true;
|
||||
}
|
||||
if variant.checks.range.one_sided && self.code.two_sided.contains(physical) {
|
||||
variant.checks.range = variant.checks.range.one_sided(false);
|
||||
touched = true;
|
||||
}
|
||||
variant
|
||||
(variant, touched)
|
||||
}
|
||||
|
||||
/// An EL0 access of the fetch page holding `physical`, by a block of
|
||||
|
||||
@@ -218,6 +218,9 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
/// ID_AA64PFR0_EL1.CSV3 discloses FEAT_CSV3, the guest range check at
|
||||
/// every access (`identification::Speculation::faulting_data`).
|
||||
pub(super) fn memory_checks(&mut self) -> MemoryChecks {
|
||||
if self.memory_checks.is_none() {
|
||||
self.derived_version += 1;
|
||||
}
|
||||
*self.memory_checks.get_or_insert_with(|| {
|
||||
let sctlr = self.cpu.regime_sctlr();
|
||||
let bit = if self.cpu.pstate.el == 0 { 4 } else { 3 };
|
||||
@@ -247,6 +250,9 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
}
|
||||
|
||||
pub(super) fn native_system(&mut self) -> NativeSystem {
|
||||
if self.native_system.is_none() {
|
||||
self.derived_version += 1;
|
||||
}
|
||||
*self.native_system.get_or_insert_with(|| NativeSystem {
|
||||
zero_block: system::native_zero_block(&self.cpu, &self.profile_missing),
|
||||
cache_clean: system::native_cache_clean(&self.cpu),
|
||||
|
||||
Reference in new issue
Block a user