#!/usr/bin/env python3 """Check that a patched kernel tree keeps the arm32 vDSO clock off the virtual counter when the kernel times on the physical one (bfe-core1106-sdk#35). On the RV1106 the boot firmware does not enter the kernel in HYP mode and leaves CNTVOFF non-zero. The DT timer node lists no virtual-timer PPI, so the kernel keeps time on the physical counter, while the arm32 vDSO always reads the virtual counter: clock_gettime() then runs about 739 s ahead of the kernel clock and every absolute deadline taken from it expires late (flare-edge#2319). Two guards must both be present in the patched tree: 1. Every armv7/armv8 timer node in the RV1106 device trees that has no virtual-timer PPI (GIC_PPI 11) carries `arm,cpu-registers-not-fw-configured`, which makes arch/arm/kernel/vdso.c drop the vDSO clock symbols. 2. drivers/clocksource/arm_arch_timer.c sets the counter's vdso_clock_mode to VDSO_CLOCKMODE_NONE on arm32 when the kernel uses the physical counter without HYP mode (the safety net for a device tree without the property). Usage: check-vdso-counter.py Exit 0 when both hold, 1 with one FAIL line per problem, 2 on bad usage. """ import pathlib import re import sys DTS_GLOB = "arch/arm/boot/dts/rockchip/rv1106*.dts*" TIMER_C = "drivers/clocksource/arm_arch_timer.c" PROPERTY = "arm,cpu-registers-not-fw-configured" def strip_comments(text): text = re.sub(r"/\*.*?\*/", "", text, flags=re.S) return re.sub(r"//[^\n]*", "", text) def timer_nodes(text): """Yield the body of every node whose compatible is an arch timer.""" for m in re.finditer(r"\b[\w@,.-]+\s*\{", text): depth, i = 1, m.end() while depth and i < len(text): depth += {"{": 1, "}": -1}.get(text[i], 0) i += 1 body = text[m.end():i - 1] # Only the node's own properties, not its children's. own = re.sub(r"\{.*\}", "", body, flags=re.S) if re.search(r'compatible\s*=\s*"arm,armv[78]-timer"', own): yield own def check_dts(root): problems, seen = [], 0 for path in sorted(root.glob(DTS_GLOB)): text = strip_comments(path.read_text(errors="replace")) for own in timer_nodes(text): seen += 1 has_virt = re.search(r"GIC_PPI\s+11\b", own) is not None has_prop = re.search(r"(^|[\s;])" + re.escape(PROPERTY) + r"\s*;", own) is not None if not has_virt and not has_prop: problems.append(f"{path.relative_to(root)}: timer node has no virtual-timer " f"PPI and no {PROPERTY}") if not seen: problems.append(f"no arm,armv7-timer node found under {DTS_GLOB}") return problems def check_timer_c(root): path = root / TIMER_C if not path.is_file(): return [f"{TIMER_C} missing"] text = strip_comments(path.read_text(errors="replace")) m = re.search(r"static void __init arch_counter_register\(void\)\s*\{", text) if not m: return [f"{TIMER_C}: arch_counter_register() not found"] depth, i = 1, m.end() while depth and i < len(text): depth += {"{": 1, "}": -1}.get(text[i], 0) i += 1 body = text[m.end():i - 1] guard = re.search( r"if\s*\(\s*IS_ENABLED\(CONFIG_ARM\)\s*&&\s*" r"arch_timer_uses_ppi\s*!=\s*ARCH_TIMER_VIRT_PPI\s*&&\s*" r"!\s*is_hyp_mode_available\(\)[^{]*\{([^}]*)\}", body) if not guard or not re.search( r"clocksource_counter\.vdso_clock_mode\s*=\s*VDSO_CLOCKMODE_NONE\s*;", guard.group(1)): return [f"{TIMER_C}: arch_counter_register() lacks the arm32 physical-counter " "vDSO fallback (vdso_clock_mode = VDSO_CLOCKMODE_NONE)"] default_at = body.find("clocksource_counter.vdso_clock_mode = vdso_default;") if default_at < 0 or default_at > guard.start(): return [f"{TIMER_C}: the vDSO fallback must come after the vdso_default assignment"] return [] def main(argv): if len(argv) != 2 or not pathlib.Path(argv[1]).is_dir(): print("usage: check-vdso-counter.py ", file=sys.stderr) return 2 root = pathlib.Path(argv[1]) problems = check_dts(root) + check_timer_c(root) for p in problems: print(f"FAIL: {p}") if problems: return 1 print("vdso-counter: ok (DT property and arm32 physical-counter fallback present)") return 0 if __name__ == "__main__": sys.exit(main(sys.argv))