diff options
| author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2026-11-17 17:03:49 +0000 |
|---|---|---|
| committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2026-11-17 17:03:49 +0000 |
| commit | 363fe07ad4cd514d907647ee0eda2310f6efa3c0 (patch) | |
| tree | 31cc927f256747e17887c8bb049b800457fe3208 /src/steamvr_patches/architecture.py | |
| parent | c750b7298bddab8f273365a96ec482538cd39201 (diff) | |
| download | steamvr-patches-363fe07ad4cd514d907647ee0eda2310f6efa3c0.tar.gz steamvr-patches-363fe07ad4cd514d907647ee0eda2310f6efa3c0.tar.xz steamvr-patches-363fe07ad4cd514d907647ee0eda2310f6efa3c0.zip | |
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).
Diffstat (limited to 'src/steamvr_patches/architecture.py')
| -rw-r--r-- | src/steamvr_patches/architecture.py | 92 |
1 files changed, 92 insertions, 0 deletions
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 <tomasz@kramkow.ski> +# 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("<ci", (b"\xe8" if call else b"\xe9"), displacement) + + +def arm64_branch(address: int, target: int, call: bool) -> 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("<I", opcode | ((displacement // 4) & 0x3FFFFFF)) + + +@dataclass(frozen=True) +class Architecture: + compiler: tuple[str, ...] + linker: str + flags: tuple[str, ...] + disassembler: tuple[int, int] + branch: Callable[[int, int, bool], bytes] + hook_size: int + ins_alignment: int + nop: bytes + relative_relocs: frozenset[int] + + def validate_site(self, address: int, length: int) -> 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", + ) + ), + ), +} |
