From 363fe07ad4cd514d907647ee0eda2310f6efa3c0 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Tue, 17 Nov 2026 17:03:49 +0000 Subject: frame-steamos: fix a couple of bugs It's possible to hang after a reset waiting for the first frame if that frame never arrives. This adds calls to the existing decoder watchdog handler to ensure this is recoverable. Sub-optimal but better than nothing. Shouldn't happen normally anyway, and the patch is still worth it as it fixes genuine repeat resets in much more common cases. Additionally, the headset can send an ack even when there is nothing to ack, and it acks frame ID 0 in that case, this breaks the steamvr patch which keeps the ACK cutoff at all times (it can cause things to get stuck in 3.3FPS mode). --- src/steamvr_patches/architecture.py | 92 +++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/steamvr_patches/architecture.py (limited to 'src/steamvr_patches/architecture.py') diff --git a/src/steamvr_patches/architecture.py b/src/steamvr_patches/architecture.py new file mode 100644 index 0000000..2431069 --- /dev/null +++ b/src/steamvr_patches/architecture.py @@ -0,0 +1,92 @@ +# SPDX-FileCopyrightText: 2026 Tomasz Kramkowski +# SPDX-License-Identifier: GPL-3.0-or-later + +"""Architecture-specific tools and encodings for the compiled hooks.""" + +import struct +from collections.abc import Callable +from dataclasses import dataclass + +from capstone import CS_ARCH_ARM64, CS_ARCH_X86, CS_MODE_64, CS_MODE_ARM +from elftools.elf.enums import ENUM_RELOC_TYPE_AARCH64, ENUM_RELOC_TYPE_x64 + + +def x86_branch(address: int, target: int, call: bool) -> bytes: + displacement = target - address - 5 + if not -(1 << 31) <= displacement < 1 << 31: + raise ValueError("hook target is outside relative jump range") + return struct.pack(" bytes: + displacement = target - address + if displacement % 4 or not -(1 << 27) <= displacement < 1 << 27: + raise ValueError("hook target is outside relative jump range") + opcode = 0x94000000 if call else 0x14000000 + return struct.pack(" None: + if length < self.hook_size: + raise ValueError(f"hook requires at least {self.hook_size} bytes") + if address % self.ins_alignment: + raise ValueError( + f"hook address requires {self.ins_alignment}-byte alignment" + ) + + +ARCHITECTURES = { + "EM_X86_64": Architecture( + compiler=("cc",), + linker="ld", + flags=("-mno-red-zone", "-fcf-protection=none"), + disassembler=(CS_ARCH_X86, CS_MODE_64), + branch=x86_branch, + hook_size=5, + ins_alignment=1, + nop=b"\x90", + relative_relocs=frozenset( + ENUM_RELOC_TYPE_x64[name] for name in ("R_X86_64_PC32", "R_X86_64_PLT32") + ), + ), + "EM_AARCH64": Architecture( + compiler=("clang", "--target=aarch64-linux-gnu"), + linker="ld.lld", + flags=("-mbranch-protection=none",), + disassembler=(CS_ARCH_ARM64, CS_MODE_ARM), + branch=arm64_branch, + hook_size=4, + ins_alignment=4, + nop=(0xD503201F).to_bytes(4, "little"), + # PC-relative branches and ADRP + low-12-bit address/load pairs. + relative_relocs=frozenset( + ENUM_RELOC_TYPE_AARCH64[name] + for name in ( + "R_AARCH64_LD_PREL_LO19", + "R_AARCH64_ADR_PREL_LO21", + "R_AARCH64_ADR_PREL_PG_HI21", + "R_AARCH64_ADD_ABS_LO12_NC", + "R_AARCH64_LDST8_ABS_LO12_NC", + "R_AARCH64_TSTBR14", + "R_AARCH64_CONDBR19", + "R_AARCH64_JUMP26", + "R_AARCH64_CALL26", + "R_AARCH64_LDST16_ABS_LO12_NC", + "R_AARCH64_LDST32_ABS_LO12_NC", + "R_AARCH64_LDST64_ABS_LO12_NC", + ) + ), + ), +} -- cgit v1.3.1