Skip to content

Instantly share code, notes, and snippets.

@aghosn
Created September 18, 2025 10:57
Show Gist options
  • Select an option

  • Save aghosn/b7203dae58aefdd5b58d6078f9be6c1a to your computer and use it in GitHub Desktop.

Select an option

Save aghosn/b7203dae58aefdd5b58d6078f9be6c1a to your computer and use it in GitHub Desktop.
Small discovery script
import struct
import os
import sys
GREEN = "\033[32m"
RED = "\033[31m"
RESET = "\033[0m"
def _use_color() -> bool:
if os.environ.get("NO_COLOR") is not None:
return False
return sys.stdout.isatty()
USE_COLOR = _use_color()
def color_bool(flag: bool) -> str:
text = "True" if flag else "False"
if not USE_COLOR:
return text
return f"{GREEN if flag else RED}{text}{RESET}"
def print_table(rows):
if not rows:
return
width = max(len(label) for label, _ in rows)
for (idx,(label, val)) in enumerate(rows):
if idx !=0 and idx % 2 == 0:
print("")
print(f"{label.ljust(width)} {color_bool(val)}")
def read_msr(cpu: int, msr: int) -> int:
"""
Read a 64-bit MSR value from /dev/cpu/<cpu>/msr.
cpu: logical CPU number
msr: MSR index (integer, e.g. 0x10 for IA32_TSC)
returns: 64-bit unsigned integer
"""
path = f"/dev/cpu/{cpu}/msr"
with open(path, "rb") as f:
# Seek to offset = msr index
f.seek(msr)
data = f.read(8)
if len(data) != 8:
raise OSError("Short read from MSR device")
return struct.unpack("<Q", data)[0] # little-endian unsigned long long
# Example: read IA32_TSC (0x10) on CPU 0
if __name__ == "__main__":
# A.3.3 in Intel Manual
SECONDARY_VM_EXECUTION_CONTROLS = 0x48b
# A.3.1 in Intel Manual
PIN_BASED_VM_EXECUTION_CONTROLS = 0x481
secondary = read_msr(0, SECONDARY_VM_EXECUTION_CONTROLS)
pin = read_msr(0, PIN_BASED_VM_EXECUTION_CONTROLS)
print("Raw value for secondary is", hex(secondary))
print("Raw value value pin is", hex(pin))
rows = [
("APIC-register virtualization may be enabled?", bool(secondary & (1 << (8 + 32)))),
("APIC-register virtualization must be enabled?", (secondary & (1 << 8)) == 1),
("Virtual-interrupt delivery may be enabled?", bool(secondary & (1 << (9 + 32)))),
("Virtual-interrupt delivery must be enabled?", (secondary & (1 << 8)) == 1),
("Process posted interrupts may be enabled?", bool(pin & (1 << (7 + 32)))),
("Process posted interrupts must be enabled?", (pin & (1 << 7)) == 1),
]
print_table(rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment