const ATC_ENTRIES: usize = 64;
#[derive(Debug, Clone, Copy, Default)]
struct AtcEntry {
valid: bool,
tag: u32,
phys_page: u32,
write_protected: bool,
supervisor_only: bool,
modified: bool,
}
#[derive(Debug, Clone)]
pub struct Atc {
entries: [AtcEntry; ATC_ENTRIES],
}
impl Default for Atc {
fn default() -> Self {
Self {
entries: [AtcEntry::default(); ATC_ENTRIES],
}
}
}
impl Atc {
#[inline]
fn tag(page_frame: u32, supervisor: bool) -> u32 {
(page_frame << 1) | supervisor as u32
}
#[inline]
fn index(page_frame: u32) -> usize {
(page_frame as usize) & (ATC_ENTRIES - 1)
}
#[inline]
pub fn lookup(&self, page_frame: u32, supervisor: bool, write: bool) -> Option<u32> {
let e = &self.entries[Self::index(page_frame)];
if !e.valid || e.tag != Self::tag(page_frame, supervisor) {
return None;
}
if (write && (e.write_protected || !e.modified)) || (!supervisor && e.supervisor_only) {
return None;
}
Some(e.phys_page)
}
#[inline]
pub fn insert(
&mut self,
page_frame: u32,
supervisor: bool,
phys_page: u32,
write_protected: bool,
supervisor_only: bool,
) {
self.insert_entry(
page_frame,
supervisor,
phys_page,
write_protected,
supervisor_only,
true,
);
}
#[inline]
pub fn insert_entry(
&mut self,
page_frame: u32,
supervisor: bool,
phys_page: u32,
write_protected: bool,
supervisor_only: bool,
modified: bool,
) {
self.entries[Self::index(page_frame)] = AtcEntry {
valid: true,
tag: Self::tag(page_frame, supervisor),
phys_page,
write_protected,
supervisor_only,
modified,
};
}
pub fn flush_all(&mut self) {
for e in &mut self.entries {
e.valid = false;
}
}
pub fn flush_page(&mut self, page_frame: u32) {
let e = &mut self.entries[Self::index(page_frame)];
if e.tag >> 1 == page_frame {
e.valid = false;
}
}
}
pub const ATC_030_ENTRIES: usize = 22;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
struct Atc030Entry {
valid: bool,
fc: u8,
page: u32,
phys_page: u32,
write_protected: bool,
stamp: u64,
}
const ATC_030_M: u32 = 1;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Atc030 {
entries: [Atc030Entry; ATC_030_ENTRIES],
mru: u8,
clock: u64,
shift: u8,
#[cfg_attr(feature = "serde", serde(skip))]
hints: [u8; ATC_030_HINTS],
}
const ATC_030_HINTS: usize = 32;
impl PartialEq for Atc030 {
fn eq(&self, other: &Self) -> bool {
self.entries == other.entries
&& self.mru == other.mru
&& self.clock == other.clock
&& self.shift == other.shift
}
}
impl Eq for Atc030 {}
impl Default for Atc030 {
fn default() -> Self {
Self {
entries: [Atc030Entry::default(); ATC_030_ENTRIES],
mru: 0,
clock: 0,
shift: 0,
hints: [0; ATC_030_HINTS],
}
}
}
impl Atc030 {
#[inline]
fn hint_slot(fc: u8, page: u32) -> usize {
(page as usize ^ (usize::from(fc) << 2)) & (ATC_030_HINTS - 1)
}
#[inline]
pub fn lookup(&mut self, fc: u8, page: u32, shift: u32, write: bool) -> Option<u32> {
if u32::from(self.shift) != shift {
return None;
}
let hit = |e: &Atc030Entry| e.valid && e.page == page && e.fc == fc;
let mru = usize::from(self.mru);
let index = if hit(&self.entries[mru]) {
mru
} else {
let slot = Self::hint_slot(fc, page);
let hinted = usize::from(self.hints[slot]);
let index = if hinted < ATC_030_ENTRIES && hit(&self.entries[hinted]) {
hinted
} else {
let index = self.entries.iter().position(hit)?;
self.hints[slot] = index as u8;
index
};
self.clock += 1;
self.entries[index].stamp = self.clock;
self.mru = index as u8;
index
};
let e = &self.entries[index];
if write && (e.write_protected || e.phys_page & ATC_030_M == 0) {
return None;
}
Some(e.phys_page & !ATC_030_M)
}
#[inline]
pub fn probe(&self, fc: u8, page: u32, shift: u32, write: bool) -> Option<u32> {
if u32::from(self.shift) != shift {
return None;
}
let hit = |e: &Atc030Entry| e.valid && e.page == page && e.fc == fc;
let hinted = usize::from(self.hints[Self::hint_slot(fc, page)]);
let e = if hit(&self.entries[usize::from(self.mru)]) {
&self.entries[usize::from(self.mru)]
} else if hinted < ATC_030_ENTRIES && hit(&self.entries[hinted]) {
&self.entries[hinted]
} else {
self.entries.iter().find(|e| hit(e))?
};
if write && (e.write_protected || e.phys_page & ATC_030_M == 0) {
return None;
}
Some(e.phys_page & !ATC_030_M)
}
pub fn status(&self, fc: u8, page: u32, shift: u32) -> Option<(bool, bool)> {
if u32::from(self.shift) != shift {
return None;
}
self.entries
.iter()
.find(|e| e.valid && e.page == page && e.fc == fc)
.map(|e| (e.write_protected, e.phys_page & ATC_030_M != 0))
}
pub fn insert(&mut self, fc: u8, page: u32, shift: u32, phys_page: u32, write_protected: bool) {
self.insert_entry(fc, page, shift, phys_page, write_protected, true);
}
pub fn insert_entry(
&mut self,
fc: u8,
page: u32,
shift: u32,
phys_page: u32,
write_protected: bool,
modified: bool,
) {
if u32::from(self.shift) != shift {
self.flush_all();
self.shift = shift as u8;
}
let index = match self
.entries
.iter()
.position(|e| e.valid && e.page == page && e.fc == fc)
{
Some(index) => index,
None => match self.entries.iter().position(|e| !e.valid) {
Some(index) => index,
None => self
.entries
.iter()
.enumerate()
.min_by_key(|(_, e)| e.stamp)
.map_or(0, |(i, _)| i),
},
};
self.clock += 1;
self.entries[index] = Atc030Entry {
valid: true,
fc,
page,
phys_page: (phys_page & !ATC_030_M) | if modified { ATC_030_M } else { 0 },
write_protected,
stamp: self.clock,
};
self.mru = index as u8;
self.hints[Self::hint_slot(fc, page)] = index as u8;
}
pub fn flush_all(&mut self) {
for e in &mut self.entries {
e.valid = false;
}
}
pub fn flush_fc(&mut self, fc: u8, mask: u8) {
for e in &mut self.entries {
if (e.fc ^ fc) & mask == 0 {
e.valid = false;
}
}
}
pub fn flush_fc_page(&mut self, fc: u8, mask: u8, logical: u32) {
if self.shift == 0 {
return;
}
let page = logical >> self.shift;
for e in &mut self.entries {
if (e.fc ^ fc) & mask == 0 && e.page == page {
e.valid = false;
}
}
}
pub fn valid_entries(&self) -> usize {
self.entries.iter().filter(|e| e.valid).count()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn write_misses_an_entry_whose_page_is_not_modified() {
let mut atc = Atc::default();
atc.insert_entry(0x10, true, 0x8000, false, false, false);
assert_eq!(atc.lookup(0x10, true, false), Some(0x8000));
assert_eq!(atc.lookup(0x10, true, true), None);
atc.insert_entry(0x10, true, 0x8000, false, false, true);
assert_eq!(atc.lookup(0x10, true, true), Some(0x8000));
}
#[test]
fn hit_after_insert_miss_after_flush() {
let mut atc = Atc::default();
assert_eq!(atc.lookup(0x10, false, false), None);
atc.insert(0x10, false, 0x8000, false, false);
assert_eq!(atc.lookup(0x10, false, false), Some(0x8000));
assert_eq!(atc.lookup(0x10, true, false), None);
atc.flush_all();
assert_eq!(atc.lookup(0x10, false, false), None);
}
#[test]
fn flush_page_drops_only_that_frame() {
let mut atc = Atc::default();
atc.insert(0x10, false, 0x1000, false, false);
atc.flush_page(0x11); assert_eq!(atc.lookup(0x10, false, false), Some(0x1000));
atc.flush_page(0x10);
assert_eq!(atc.lookup(0x10, false, false), None);
}
#[test]
fn permission_violation_does_not_hit() {
let mut atc = Atc::default();
atc.insert(0x10, true, 0x5000, true, true);
assert_eq!(atc.lookup(0x10, true, false), Some(0x5000));
assert_eq!(atc.lookup(0x10, true, true), None);
}
const SHIFT: u32 = 12;
#[test]
fn atc_030_entries_are_tagged_by_function_code() {
let mut atc = Atc030::default();
assert_eq!(atc.lookup(5, 0x10, SHIFT, false), None);
atc.insert(5, 0x10, SHIFT, 0x0008_0000, false);
assert_eq!(atc.lookup(5, 0x10, SHIFT, false), Some(0x0008_0000));
assert_eq!(atc.lookup(6, 0x10, SHIFT, false), None);
assert_eq!(atc.lookup(1, 0x10, SHIFT, false), None);
assert_eq!(atc.lookup(5, 0x10, SHIFT + 1, false), None);
}
#[test]
fn atc_030_write_protected_entry_misses_only_for_writes() {
let mut atc = Atc030::default();
atc.insert(1, 0x20, SHIFT, 0x0010_0000, true);
assert_eq!(atc.lookup(1, 0x20, SHIFT, false), Some(0x0010_0000));
assert_eq!(atc.lookup(1, 0x20, SHIFT, true), None);
}
#[test]
fn atc_030_holds_22_entries_and_replaces_the_least_recently_used() {
let mut atc = Atc030::default();
for page in 0..ATC_030_ENTRIES as u32 {
atc.insert(1, page, SHIFT, page << SHIFT, false);
}
assert_eq!(atc.valid_entries(), ATC_030_ENTRIES);
for page in (0..ATC_030_ENTRIES as u32).filter(|&p| p != 3) {
assert!(atc.lookup(1, page, SHIFT, false).is_some());
}
atc.insert(1, 100, SHIFT, 0x0064_0000, false);
assert_eq!(atc.valid_entries(), ATC_030_ENTRIES);
assert_eq!(atc.lookup(1, 3, SHIFT, false), None);
assert_eq!(atc.lookup(1, 100, SHIFT, false), Some(0x0064_0000));
assert!(atc.lookup(1, 4, SHIFT, false).is_some());
atc.insert(1, 4, SHIFT, 0x0099_0000, true);
assert_eq!(atc.valid_entries(), ATC_030_ENTRIES);
assert_eq!(atc.lookup(1, 4, SHIFT, false), Some(0x0099_0000));
}
#[test]
fn atc_030_probe_answers_like_lookup_without_recording_a_use() {
let mut atc = Atc030::default();
for page in 0..ATC_030_ENTRIES as u32 {
atc.insert(1, page, SHIFT, page << SHIFT, page == 7);
}
let before = atc.clone();
for page in 0..ATC_030_ENTRIES as u32 + 2 {
for write in [false, true] {
assert_eq!(
atc.probe(1, page, SHIFT, write),
atc.clone().lookup(1, page, SHIFT, write),
"page {page}, write {write}"
);
}
}
assert_eq!(atc.probe(2, 3, SHIFT, false), None, "other function code");
assert_eq!(atc.probe(1, 3, SHIFT + 1, false), None, "other page size");
assert_eq!(atc, before, "a probe changes nothing");
atc.insert(1, 100, SHIFT, 0x0064_0000, false);
assert_eq!(atc.probe(1, 0, SHIFT, false), None);
}
#[test]
fn atc_030_hint_never_changes_what_a_lookup_finds() {
let mut atc = Atc030::default();
atc.insert(5, 0, SHIFT, 0x0010_0000, false);
atc.insert(5, 32, SHIFT, 0x0020_0000, false);
atc.insert(6, 1, SHIFT, 0x0030_0000, false);
for _ in 0..3 {
assert_eq!(atc.lookup(5, 0, SHIFT, false), Some(0x0010_0000));
assert_eq!(atc.lookup(6, 1, SHIFT, false), Some(0x0030_0000));
assert_eq!(atc.lookup(5, 32, SHIFT, false), Some(0x0020_0000));
assert_eq!(atc.probe(5, 0, SHIFT, false), Some(0x0010_0000));
}
atc.flush_fc_page(5, 7, 32 << SHIFT);
assert_eq!(atc.lookup(5, 32, SHIFT, false), None);
assert_eq!(atc.probe(5, 32, SHIFT, false), None);
}
#[test]
fn atc_030_pflush_honours_the_function_code_mask_and_page() {
let mut atc = Atc030::default();
for fc in [1u8, 2, 5, 6] {
atc.insert(fc, 0x30, SHIFT, 0x0030_0000, false);
atc.insert(fc, 0x31, SHIFT, 0x0031_0000, false);
}
atc.flush_fc_page(5, 7, 0x30 << SHIFT | 0x123);
assert_eq!(atc.lookup(5, 0x30, SHIFT, false), None);
assert!(atc.lookup(5, 0x31, SHIFT, false).is_some());
assert!(atc.lookup(1, 0x30, SHIFT, false).is_some());
atc.flush_fc(4, 4);
assert_eq!(atc.lookup(5, 0x31, SHIFT, false), None);
assert_eq!(atc.lookup(6, 0x30, SHIFT, false), None);
assert!(atc.lookup(1, 0x31, SHIFT, false).is_some());
assert!(atc.lookup(2, 0x30, SHIFT, false).is_some());
atc.flush_fc(0, 0);
assert_eq!(atc.valid_entries(), 0);
}
#[test]
fn atc_030_write_misses_an_entry_whose_page_is_not_modified() {
let mut atc = Atc030::default();
atc.insert_entry(5, 0x10, SHIFT, 0x0008_0000, false, false);
assert_eq!(atc.lookup(5, 0x10, SHIFT, false), Some(0x0008_0000));
assert_eq!(atc.lookup(5, 0x10, SHIFT, true), None);
assert_eq!(atc.probe(5, 0x10, SHIFT, true), None);
assert_eq!(atc.status(5, 0x10, SHIFT), Some((false, false)));
atc.insert_entry(5, 0x10, SHIFT, 0x0008_0000, false, true);
assert_eq!(atc.lookup(5, 0x10, SHIFT, true), Some(0x0008_0000));
assert_eq!(atc.probe(5, 0x10, SHIFT, true), Some(0x0008_0000));
assert_eq!(atc.status(5, 0x10, SHIFT), Some((false, true)));
assert_eq!(atc.status(1, 0x10, SHIFT), None);
}
}