Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions pyplugins/compat/qemu_compat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import shlex
from contextlib import contextmanager
from pathlib import Path
from typing import Callable, List, Optional

Expand Down Expand Up @@ -481,6 +482,26 @@ def _lib_symbol(self, name: str):
except AttributeError:
return None

@contextmanager
def bql_held(self, callsite: bytes = b"pyplugins/qemu_compat.py"):
"""Acquire the BQL on entry if the current thread doesn't already hold it.

Safe to call from any thread. KVM vCPU threads, the remotectrl asyncio
thread, and other non-main-loop threads can use this to invoke QEMU
APIs that assert(bql_locked()) — e.g. memory_region_transaction_commit,
cpu_memory_rw_debug, plugin/listener registration.
"""
bql_locked = self._lib_symbol("bql_locked")
if bql_locked is None or bql_locked():
yield
return
self.lib.bql_lock_impl(callsite, 0)
try:
yield
finally:
if bql_locked():
self.lib.bql_unlock()

def set_hypercall_callback(self, cb: Callable):
if self.mode == "kvm":
ctype = (
Expand Down Expand Up @@ -511,10 +532,14 @@ def _dispatch_hypercall(self, cs, nr, a0, a1, a2, a3, a4, a5, ret_ptr, opaque=No
self._current_ret_ptr = ret_ptr
self._current_retval = 0

# KVM vCPU threads drop BQL across KVM_RUN and most exit handlers don't
# reacquire it, but our pyplugin handlers call BQL-protected APIs
# (cpu_memory_rw_debug, memory_region_*, etc). Re-acquire here.
try:
plugin = self.hypercall_plugin
if plugin is not None:
return plugin.dispatch(cs, nr, ret_ptr)
with self.bql_held(b"pyplugins/qemu_compat.py:_dispatch_hypercall"):
plugin = self.hypercall_plugin
if plugin is not None:
return plugin.dispatch(cs, nr, ret_ptr)
finally:
self._current_ret_ptr = self.ffi.NULL

Expand Down
13 changes: 9 additions & 4 deletions pyplugins/interventions/remotectrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,16 @@ def _process_message(self, data):
cmd_type = cmd.get('type')
handler = self.handlers.get(cmd_type)

if handler:
result = handler(cmd)
return {"status": "success", **(result if isinstance(result, dict) else {})}
else:
if not handler:
return {"status": "error", "message": f"Unknown command: {cmd_type}"}

# Handlers run on the asyncio thread, but loading pyplugins,
# registering callbacks, or reading guest memory require the BQL.
# Hold it across the whole dispatch so any cascading PANDA/QEMU
# API hits assert(bql_locked()) cleanly.
with self.panda.bql_held(b"pyplugins/remotectrl.py:_process_message"):
result = handler(cmd)
return {"status": "success", **(result if isinstance(result, dict) else {})}
except Exception as e:
self.logger.error(traceback.format_exc())
return {"status": "error", "message": str(e)}
Expand Down
Loading