Pu region sizing/bounds fix (#2024)

* fix the pu region's end point overflowing

According to gericom it cannot overflow at all

* set a minimum and a better maximum for the pu region size

* fix pu logging

* PU regions with a size of 31 always take up the entire address space

also tweak some logging a little more

* start is actually force aligned by size, oops

* small tweaks

* hopefully more clear code

* math is for nerds
This commit is contained in:
Jakly 2024-05-02 11:44:59 -04:00 committed by GitHub
parent ba8d547dfa
commit 6112aa120a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 7 deletions

View File

@ -186,10 +186,14 @@ void ARMv5::UpdatePURegion(u32 n)
return; return;
} }
u32 start = rgn >> 12; // notes:
u32 sz = 2 << ((rgn >> 1) & 0x1F); // * min size of a pu region is 4KiB (12 bits)
u32 end = start + (sz >> 12); // * size is calculated as size + 1, but the 12 lsb of address space are ignored, therefore we need it as size + 1 - 12, or size - 11
// TODO: check alignment of start // * pu regions are aligned based on their size
u32 size = std::max((int)((rgn>>1) & 0x1F) - 11, 0); // obtain the size, subtract 11 and clamp to a min of 0.
u32 start = ((rgn >> 12) >> size) << size; // determine the start offset, and use shifts to force alignment with a multiple of the size.
u32 end = start + (1<<size); // add 1 left shifted by size to start to determine end point
// dont need to bounds check the end point because the force alignment inherently prevents it from breaking
u8 usermask = 0; u8 usermask = 0;
u8 privmask = 0; u8 privmask = 0;
@ -239,7 +243,7 @@ void ARMv5::UpdatePURegion(u32 n)
"PU region %d: %08X-%08X, user=%02X priv=%02X, %08X/%08X\n", "PU region %d: %08X-%08X, user=%02X priv=%02X, %08X/%08X\n",
n, n,
start << 12, start << 12,
end << 12, (end << 12) - 1,
usermask, usermask,
privmask, privmask,
PU_DataRW, PU_DataRW,
@ -579,12 +583,12 @@ void ARMv5::CP15Write(u32 id, u32 val)
std::snprintf(log_output, std::snprintf(log_output,
sizeof(log_output), sizeof(log_output),
"PU: region %d = %08X : %s, %08X-%08X\n", "PU: region %d = %08X : %s, start: %08X size: %02X\n",
(id >> 4) & 0xF, (id >> 4) & 0xF,
val, val,
val & 1 ? "enabled" : "disabled", val & 1 ? "enabled" : "disabled",
val & 0xFFFFF000, val & 0xFFFFF000,
(val & 0xFFFFF000) + (2 << ((val & 0x3E) >> 1)) (val & 0x3E) >> 1
); );
Log(LogLevel::Debug, "%s", log_output); Log(LogLevel::Debug, "%s", log_output);
// Some implementations of Log imply a newline, so we build up the line before printing it // Some implementations of Log imply a newline, so we build up the line before printing it