mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 05:51:06 +00:00
fix(firmware): group a page's MMIO accesses by region kind
A 4 KiB page can now hold SVD-covered addresses classified svd_peripheral and uncovered ones classified by bus convention. Page groups were keyed by address alone and took their region kind and basis from the first access, so a region_kind group filter depended on access order and could hide matching accesses. Page identities now include the region kind, in both the grouping index and the literal-pool pre-allocation measurement, so each kind forms its own group with its own basis. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
This commit is contained in:
1 parent
baafd3535d
commit
24f37becf5
3 files changed
+75
-10
No files matched your search
@@ -245,6 +245,7 @@ const classificationGroup = (
|
||||
? null
|
||||
: BigInt(classified.mmioBaseAddress),
|
||||
architecturalBlock: classified.architecturalBlock,
|
||||
regionKind: classified.regionKind,
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
@@ -1309,4 +1309,51 @@ describe("firmwareLiteralPools domain scanner", () => {
|
||||
]);
|
||||
expect(ambiguous.svd_profile.matched_access_count).toBe(0);
|
||||
});
|
||||
|
||||
it("splits one page into a group per region kind when SVD covers only part of it", () => {
|
||||
// Thumb `ldr r0, [pc, #4]` and `ldr r1, [pc, #8]` load 0x40000004 and 0x40000104.
|
||||
const image = Buffer.alloc(16, 0x00);
|
||||
image.writeUInt16LE(0x4801, 0);
|
||||
image.writeUInt16LE(0x4902, 2);
|
||||
image.writeUInt16LE(0xbf00, 4);
|
||||
image.writeUInt16LE(0xbf00, 6);
|
||||
image.writeUInt32LE(0x4000_0004, 8);
|
||||
image.writeUInt32LE(0x4000_0104, 12);
|
||||
const block = (name: string) =>
|
||||
`<peripheral><name>${name}</name><baseAddress>0x40000000</baseAddress><addressBlock><offset>0x0</offset><size>0x100</size><usage>registers</usage></addressBlock></peripheral>`;
|
||||
const svd = parseSvdPeripheralSpans(
|
||||
`<device><peripherals>${block("TIM2")}${block("TIM3")}</peripherals></device>`,
|
||||
);
|
||||
if (!svd.ok) throw new Error("expected SVD");
|
||||
const res = scanFirmwareLiteralPools({
|
||||
artifact: { path: "/tmp/thumb.bin", sha256: "f".repeat(64), bytes: 16 },
|
||||
fileBytes: image,
|
||||
architecture: "arm-thumb",
|
||||
baseAddressHex: "0x08000000",
|
||||
scanOffset: 0,
|
||||
maxScanBytes: 16,
|
||||
mmioOnly: true,
|
||||
svdSupplied: true,
|
||||
svdProfile: svd.value,
|
||||
});
|
||||
if (!res.ok) throw new Error("expected scan");
|
||||
expect(res.value.accesses.map((row) => row.region_kind)).toEqual([
|
||||
"svd_peripheral",
|
||||
"cortex_m_peripheral",
|
||||
]);
|
||||
expect(res.value.peripheral_groups).toMatchObject([
|
||||
{
|
||||
base_address: "0x40000000",
|
||||
region_kind: "cortex_m_peripheral",
|
||||
basis: "address_map_convention",
|
||||
distinct_addresses: ["0x40000104"],
|
||||
},
|
||||
{
|
||||
base_address: "0x40000000",
|
||||
region_kind: "svd_peripheral",
|
||||
basis: "explicit_profile",
|
||||
distinct_addresses: ["0x40000004"],
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
isMmioRegionKind,
|
||||
mmioAttributionBasis,
|
||||
type FirmwareMmioAccess,
|
||||
type FirmwareMmioRegionKind,
|
||||
type FirmwareMmioPeripheralGroup,
|
||||
} from "./firmwareMmio.js";
|
||||
|
||||
@@ -30,20 +31,29 @@ interface GroupAccumulator {
|
||||
readonly samples: string[];
|
||||
}
|
||||
|
||||
/** Grouping identity of one MMIO access: SVD peripheral, architectural block, or 4 KiB page. */
|
||||
/**
|
||||
* Grouping identity of one MMIO access: SVD peripheral, architectural block,
|
||||
* or 4 KiB page of one region kind, since a page can hold SVD-covered and
|
||||
* convention-classified addresses.
|
||||
*/
|
||||
export type MmioGroupIdentity =
|
||||
| {
|
||||
readonly kind: "svd" | "block";
|
||||
readonly name: string;
|
||||
readonly base: bigint;
|
||||
}
|
||||
| { readonly kind: "page"; readonly base: bigint };
|
||||
| {
|
||||
readonly kind: "page";
|
||||
readonly base: bigint;
|
||||
readonly regionKind: FirmwareMmioRegionKind;
|
||||
};
|
||||
|
||||
export const mmioGroupIdentity = (access: {
|
||||
readonly address: bigint;
|
||||
readonly peripheralName: string | null;
|
||||
readonly peripheralBase: bigint | null;
|
||||
readonly architecturalBlock: string | null;
|
||||
readonly regionKind: FirmwareMmioRegionKind;
|
||||
}): MmioGroupIdentity => {
|
||||
if (access.peripheralName !== null && access.peripheralBase !== null) {
|
||||
return {
|
||||
@@ -54,7 +64,7 @@ export const mmioGroupIdentity = (access: {
|
||||
}
|
||||
const page = access.address & PAGE_MASK;
|
||||
return access.architecturalBlock === null
|
||||
? { kind: "page", base: page }
|
||||
? { kind: "page", base: page, regionKind: access.regionKind }
|
||||
: { kind: "block", name: access.architecturalBlock, base: page };
|
||||
};
|
||||
|
||||
@@ -64,24 +74,30 @@ export const mmioGroupIdentity = (access: {
|
||||
*/
|
||||
export class MmioGroupIndex<T> {
|
||||
readonly #named = { svd: new Map<string, T>(), block: new Map<string, T>() };
|
||||
readonly #pages = new Map<bigint, T>();
|
||||
readonly #pages = new Map<FirmwareMmioRegionKind, Map<bigint, T>>();
|
||||
|
||||
get(identity: MmioGroupIdentity): T | undefined {
|
||||
return identity.kind === "page"
|
||||
? this.#pages.get(identity.base)
|
||||
? this.#pages.get(identity.regionKind)?.get(identity.base)
|
||||
: this.#named[identity.kind].get(identity.name);
|
||||
}
|
||||
|
||||
set(identity: MmioGroupIdentity, value: T): void {
|
||||
if (identity.kind === "page") this.#pages.set(identity.base, value);
|
||||
else this.#named[identity.kind].set(identity.name, value);
|
||||
if (identity.kind === "page") {
|
||||
let pages = this.#pages.get(identity.regionKind);
|
||||
if (pages === undefined) {
|
||||
pages = new Map();
|
||||
this.#pages.set(identity.regionKind, pages);
|
||||
}
|
||||
pages.set(identity.base, value);
|
||||
} else this.#named[identity.kind].set(identity.name, value);
|
||||
}
|
||||
|
||||
values(): T[] {
|
||||
return [
|
||||
...this.#named.svd.values(),
|
||||
...this.#named.block.values(),
|
||||
...this.#pages.values(),
|
||||
...[...this.#pages.values()].flatMap((pages) => [...pages.values()]),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -97,6 +113,7 @@ const groupAccess = (access: FirmwareMmioAccess) =>
|
||||
peripheralBase:
|
||||
access.peripheral_base === null ? null : BigInt(access.peripheral_base),
|
||||
architecturalBlock: access.architectural_block,
|
||||
regionKind: access.region_kind,
|
||||
});
|
||||
|
||||
const groupBytes = (
|
||||
@@ -211,8 +228,8 @@ export const buildMmioPeripheralGroups = (
|
||||
b.access_count - a.access_count ||
|
||||
compareBigints(BigInt(a.base_address), BigInt(b.base_address)) ||
|
||||
compareUnicodeCodePoints(
|
||||
a.peripheral_name ?? a.architectural_block ?? "",
|
||||
b.peripheral_name ?? b.architectural_block ?? "",
|
||||
a.peripheral_name ?? a.architectural_block ?? a.region_kind,
|
||||
b.peripheral_name ?? b.architectural_block ?? b.region_kind,
|
||||
),
|
||||
);
|
||||
return groups;
|
||||
|
||||
Reference in new issue
Block a user