From d2b47661ef706d5cd91922751673164d4c6dd10b Mon Sep 17 00:00:00 2001 From: novafacing Date: Sun, 2 Jun 2024 15:20:06 -0700 Subject: [PATCH 1/7] Update to 9.0.0, add plugin version 2 and 3, update tracer and tiny examples with register output --- .vscode/settings.json | 2 +- Cargo.toml | 8 +- README.md | 2 +- plugins/tiny/src/lib.rs | 20 +- plugins/tracer/Cargo.toml | 13 +- plugins/tracer/src/bin/tracer.rs | 69 +- plugins/tracer/src/bin/tracer.rs~ | 240 ------- plugins/tracer/src/lib.rs | 199 ++++-- qemu-plugin-sys/Cargo.toml | 13 +- qemu-plugin-sys/build.rs | 10 +- qemu-plugin-sys/generate-bindings.rs | 151 +++-- .../src/{bindings.rs => bindings_v1.rs} | 183 +----- qemu-plugin-sys/src/bindings_v2.rs | 504 +++++++++++++++ qemu-plugin-sys/src/bindings_v3.rs | 508 +++++++++++++++ qemu-plugin-sys/src/lib.rs | 11 +- ..._plugin_api.def => qemu_plugin_api_v1.def} | 0 qemu-plugin-sys/src/qemu_plugin_api_v2.def | 54 ++ qemu-plugin-sys/src/qemu_plugin_api_v3.def | 54 ++ qemu-plugin/Cargo.toml | 19 +- qemu-plugin/README.md | 2 +- qemu-plugin/src/error/mod.rs | 14 + qemu-plugin/src/lib.rs | 606 +++++++++++++++++- qemu-plugin/src/unix_weak_link/mod.rs | 125 ++++ qemu-plugin/src/version/mod.rs | 7 + qemu/Cargo.toml | 8 +- qemu/README.md | 6 +- qemu/build.rs | 2 +- qemu/src/lib.rs | 2 +- 28 files changed, 2301 insertions(+), 531 deletions(-) delete mode 100644 plugins/tracer/src/bin/tracer.rs~ rename qemu-plugin-sys/src/{bindings.rs => bindings_v1.rs} (83%) create mode 100644 qemu-plugin-sys/src/bindings_v2.rs create mode 100644 qemu-plugin-sys/src/bindings_v3.rs rename qemu-plugin-sys/src/{qemu_plugin_api.def => qemu_plugin_api_v1.def} (100%) create mode 100644 qemu-plugin-sys/src/qemu_plugin_api_v2.def create mode 100644 qemu-plugin-sys/src/qemu_plugin_api_v3.def create mode 100644 qemu-plugin/src/version/mod.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index a5b4d17..3550f6e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "rust-analyzer.cargo.buildScripts.enable": false + "rust-analyzer.cargo.buildScripts.enable": false, } diff --git a/Cargo.toml b/Cargo.toml index a0ead70..f9b2e2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ license = "GPL-2.0-only" publish = true readme = "README.md" repository = "https://github.com/novafacing/qemu-rs" -version = "8.2.2-v0" +version = "9.0.0-v0" [workspace] resolver = "2" @@ -23,6 +23,6 @@ members = [ default-members = ["qemu-plugin", "qemu-plugin-sys"] [workspace.dependencies] -qemu-plugin-sys = { version = "8.2.2-v0", path = "qemu-plugin-sys" } -qemu-plugin = { version = "8.2.2-v0", path = "qemu-plugin" } -qemu = { version = "8.2.2-v0", path = "qemu" } +qemu-plugin-sys = { version = "9.0.0-v0", path = "qemu-plugin-sys", default-features = false } +qemu-plugin = { version = "9.0.0-v0", path = "qemu-plugin", default-features = false } +qemu = { version = "9.0.0-v0", path = "qemu" } diff --git a/README.md b/README.md index 1e6c5da..f77f316 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ installs Rust wrappers for QEMU as binaries. You can install QEMU with (add any additional features you need, e.g. `plugins`): ```sh -cargo install qemu@8.2.2-v0 --features=binaries +cargo install qemu@9.0.0-v0 --features=binaries ``` On some systems, particularly BTRFS systems, `/tmp` may not be large enough for the diff --git a/plugins/tiny/src/lib.rs b/plugins/tiny/src/lib.rs index 91b199d..2f9307d 100644 --- a/plugins/tiny/src/lib.rs +++ b/plugins/tiny/src/lib.rs @@ -2,23 +2,37 @@ use anyhow::{anyhow, Result}; use ctor::ctor; use qemu_plugin::{ plugin::{HasCallbacks, Plugin, Register, PLUGIN}, - PluginId, TranslationBlock, + PluginId, TranslationBlock, VCPUIndex, qemu_plugin_get_registers, + RegisterDescriptor, }; use std::sync::Mutex; -struct TinyTrace {} +#[derive(Default)] +struct TinyTrace { + registers: Vec>, +} impl Plugin for TinyTrace {} impl Register for TinyTrace {} impl HasCallbacks for TinyTrace { + fn on_vcpu_init(&mut self, _id: PluginId, _vcpu_id: VCPUIndex) -> Result<()> { + self.registers = qemu_plugin_get_registers()?; + Ok(()) + } fn on_translation_block_translate( &mut self, _id: PluginId, tb: TranslationBlock, ) -> Result<()> { + let registers = self.registers.clone(); tb.instructions().try_for_each(|insn| { println!("{:08x}: {}", insn.vaddr(), insn.disas()?); + for register in ®isters { + let value = register.read()?; + println!(" {}: {:?}", register.name, value); + } + Ok(()) }) } @@ -28,7 +42,7 @@ impl HasCallbacks for TinyTrace { #[ctor] fn init() { PLUGIN - .set(Mutex::new(Box::new(TinyTrace {}))) + .set(Mutex::new(Box::new(TinyTrace::default()))) .map_err(|_| anyhow!("Failed to set plugin")) .expect("Failed to set plugin"); } diff --git a/plugins/tracer/Cargo.toml b/plugins/tracer/Cargo.toml index 34bfedf..1889dc0 100644 --- a/plugins/tracer/Cargo.toml +++ b/plugins/tracer/Cargo.toml @@ -10,7 +10,9 @@ crate-type = ["cdylib", "lib"] [dependencies] anyhow = "1.0.75" ctor = "0.2.6" -qemu-plugin = { workspace = true, features = ["unix-weak-link"] } +qemu-plugin = { workspace = true, features = [ + "unix-weak-link", +], default-features = false } serde = { version = "1.0.193", features = ["derive"] } serde_cbor = "0.11.2" tokio = { version = "1.35.0", features = ["full"] } @@ -22,13 +24,14 @@ yaxpeax-x86 = "1.2.2" clap = { version = "4.4.11", features = ["derive", "string"] } # Enable the `qemu` feature to build and install QEMU with the `qemu` crate instead # of trying to use the system QEMU. -qemu = { workspace = true, features = [ - "plugins", -], optional = true } +qemu = { workspace = true, features = ["plugins"], optional = true } memfd-exec = { version = "0.2.1", optional = true } rand = "0.8.5" serde_json = "1.0.108" [features] qemu = ["dep:memfd-exec", "dep:qemu"] -default = [] +default = ["plugin-api-v2"] +plugin-api-v1 = ["qemu-plugin/plugin-api-v1"] +plugin-api-v2 = ["qemu-plugin/plugin-api-v2"] +plugin-api-v3 = ["qemu-plugin/plugin-api-v3"] diff --git a/plugins/tracer/src/bin/tracer.rs b/plugins/tracer/src/bin/tracer.rs index a653c4c..30b47c6 100644 --- a/plugins/tracer/src/bin/tracer.rs +++ b/plugins/tracer/src/bin/tracer.rs @@ -1,6 +1,6 @@ use anyhow::{anyhow, Error, Result}; use clap::Parser; -#[cfg(feature = "memfd-exec")] +#[cfg(feature = "qemu")] use memfd_exec::{MemFdExecutable, Stdio}; #[cfg(feature = "qemu")] use qemu::QEMU_X86_64_LINUX_USER; @@ -47,6 +47,38 @@ fn tmp(prefix: &str, suffix: &str) -> PathBuf { )) } +#[cfg(feature = "plugin-api-v1")] +#[derive(Parser, Debug, Clone)] +/// Run QEMU with a plugin that logs events. To pass arguments to QEMU, use the QEMU environment +/// variables. +struct Args { + #[clap(short = 'i', long)] + /// Whether instructions should be logged + pub log_insns: bool, + #[clap(short = 'm', long)] + /// Whether memory accesses should be logged + pub log_mem: bool, + #[clap(short = 's', long)] + /// Whether syscalls should be logged + pub log_syscalls: bool, + #[clap(short = 'a', long)] + /// Whether all events should be logged + pub log_all: bool, + #[clap(short = 'I', long)] + /// An input file to use as the program's stdin, otherwise the driver's stdin is used + pub input_file: Option, + #[clap(short = 'O', long)] + /// An output file to write the trace to, otherwise stdout is used + pub output_file: Option, + /// The program to run + #[clap()] + pub program: PathBuf, + /// The arguments to the program + #[clap(num_args = 1.., last = true)] + pub args: Vec, +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] #[derive(Parser, Debug, Clone)] /// Run QEMU with a plugin that logs events. To pass arguments to QEMU, use the QEMU environment /// variables. @@ -60,6 +92,9 @@ struct Args { #[clap(short = 's', long)] /// Whether syscalls should be logged pub log_syscalls: bool, + #[clap(short = 'r', long)] + /// Whether registers should be logged + pub log_registers: bool, #[clap(short = 'a', long)] /// Whether all events should be logged pub log_all: bool, @@ -79,12 +114,25 @@ struct Args { impl Args { fn to_plugin_args(&self) -> String { - format!( - "log_insns={},log_mem={},log_syscalls={}", - self.log_insns | self.log_all, - self.log_mem | self.log_all, - self.log_syscalls | self.log_all - ) + #[cfg(feature = "plugin-api-v1")] + { + format!( + "log_insns={},log_mem={},log_syscalls={}", + self.log_insns | self.log_all, + self.log_mem | self.log_all, + self.log_syscalls | self.log_all, + ) + } + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] + { + format!( + "log_insns={},log_mem={},log_syscalls={},log_registers={}", + self.log_insns | self.log_all, + self.log_mem | self.log_all, + self.log_syscalls | self.log_all, + self.log_registers | self.log_all, + ) + } } fn to_qemu_args(&self, socket_path: &Path, plugin_path: &Path) -> Result> { @@ -302,17 +350,14 @@ async fn main() -> Result<()> { let listen_sock = UnixListener::bind(&socket_path)?; let qemu_args = args.to_qemu_args(&socket_path, &plugin_path)?; - let qemu_task = spawn(async move { run(input, qemu_args).await }); - let socket_task = spawn_blocking(move || listen(listen_sock, args.output_file.as_ref())); - - let (qemu_res, socket_res) = join!(qemu_task, socket_task); + let qemu_task = spawn(async move { run(input, qemu_args).await }); + let (qemu_res, socket_res) = join!(socket_task, qemu_task); remove_file(&plugin_path).await?; remove_file(&socket_path).await?; qemu_res??; - socket_res??; Ok(()) diff --git a/plugins/tracer/src/bin/tracer.rs~ b/plugins/tracer/src/bin/tracer.rs~ deleted file mode 100644 index ead681e..0000000 --- a/plugins/tracer/src/bin/tracer.rs~ +++ /dev/null @@ -1,240 +0,0 @@ -use anyhow::{anyhow, Error, Result}; -use clap::Parser; -use memfd_exec::{MemFdExecutable, Stdio}; -#[cfg(feature = "qemu")] -use qemu::QEMU_X86_64_LINUX_USER; -use rand::{distributions::Alphanumeric, thread_rng, Rng}; -use serde_cbor::Deserializer; -use serde_json::to_string; -use std::{ - fs::OpenOptions, - io::{stdout, BufRead, BufReader, Write}, - os::unix::net::UnixListener, - path::{Path, PathBuf}, -}; -use tokio::{ - fs::{read, remove_file, write}, - join, main, spawn, - task::spawn_blocking, -}; -use tracer::Event; - -#[cfg(debug_assertions)] -const PLUGIN: &[u8] = include_bytes!(concat!( - env!("CARGO_MANIFEST_DIR"), - "/../../../target/debug/libtracer.so" -)); - -#[cfg(not(debug_assertions))] -const PLUGIN: &[u8] = include_bytes!(concat!( - env!("CARGO_MANIFEST_DIR"), - "/../../../target/release/libtracer.so" -)); - -fn tmp(prefix: &str, suffix: &str) -> PathBuf { - PathBuf::from(format!( - "{}{}{}", - prefix, - thread_rng() - .sample_iter(&Alphanumeric) - .take(8) - .map(char::from) - .collect::(), - suffix - )) -} - -#[derive(Parser, Debug, Clone)] -/// Run QEMU with a plugin that logs events. To pass arguments to QEMU, use the QEMU environment -/// variables. -struct Args { - #[clap(short = 'i', long)] - /// Whether instructions should be logged - pub log_insns: bool, - #[clap(short = 'm', long)] - /// Whether memory accesses should be logged - pub log_mem: bool, - #[clap(short = 's', long)] - /// Whether syscalls should be logged - pub log_syscalls: bool, - #[clap(short = 'a', long)] - /// Whether all events should be logged - pub log_all: bool, - #[clap(short = 'I', long)] - /// An input file to use as the program's stdin, otherwise the driver's stdin is used - pub input_file: Option, - #[clap(short = 'O', long)] - /// An output file to write the trace to, otherwise stdout is used - pub output_file: Option, - /// The program to run - #[clap()] - pub program: PathBuf, - /// The arguments to the program - #[clap(num_args = 1.., last = true)] - pub args: Vec, -} - -impl Args { - fn to_plugin_args(&self) -> String { - format!( - "log_insns={},log_mem={},log_syscalls={}", - self.log_insns | self.log_all, - self.log_mem | self.log_all, - self.log_syscalls | self.log_all - ) - } - - fn to_qemu_args(&self, socket_path: &Path, plugin_path: &Path) -> Result> { - let mut qemu_args = vec![ - "-plugin".to_string(), - format!( - "{},{},socket_path={}", - plugin_path.display(), - self.to_plugin_args(), - socket_path.display() - ), - "--".to_string(), - self.program - .to_str() - .ok_or_else(|| anyhow!("Failed to convert program path to string"))? - .to_string(), - ]; - - qemu_args.extend(self.args.clone()); - - Ok(qemu_args) - } -} - -async fn run(input: Option>, args: Vec) -> Result<()> { - let mut exe = MemFdExecutable::new("qemu", QEMU_X86_64_LINUX_USER) - .args(args) - .stdin(if input.is_some() { - Stdio::piped() - } else { - Stdio::inherit() - }) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn()?; - - if let Some(input) = input { - let mut stdin = exe.stdin.take().ok_or_else(|| anyhow!("No stdin"))?; - spawn_blocking(move || stdin.write_all(&input)); - } - - let stdout = exe.stdout.take().ok_or_else(|| anyhow!("No stdout"))?; - - let out_reader = spawn_blocking(move || { - let mut line = String::new(); - let mut out_reader = BufReader::new(stdout); - - loop { - line.clear(); - - if let 0 = out_reader.read_line(&mut line)? { - break; - } - - let line = line.trim(); - - if !line.is_empty() { - println!("{line}"); - } - } - - Ok::<(), Error>(()) - }); - - let stderr = exe.stderr.take().ok_or_else(|| anyhow!("No stderr"))?; - - let err_reader = spawn_blocking(move || { - let mut line = String::new(); - let mut err_reader = BufReader::new(stderr); - - loop { - line.clear(); - - if let 0 = err_reader.read_line(&mut line)? { - break; - } - - let line = line.trim(); - - if !line.is_empty() { - eprintln!("{line}"); - } - } - - Ok::<(), Error>(()) - }); - - let waiter = spawn_blocking(move || exe.wait()); - - let (out_res, err_res, waiter_res) = join!(out_reader, err_reader, waiter); - - out_res??; - err_res??; - waiter_res??; - - Ok(()) -} - -fn listen

(listen_sock: UnixListener, outfile: Option

) -> Result<()> -where - P: AsRef, -{ - let mut outfile_stream = if let Some(outfile) = outfile.as_ref() { - Box::new(OpenOptions::new().create(true).append(true).open(outfile)?) as Box - } else { - Box::new(stdout()) as Box - }; - - let (mut stream, _) = listen_sock.accept()?; - let it = Deserializer::from_reader(&mut stream).into_iter::(); - - for event in it { - outfile_stream.write(to_string(&event?)?.as_bytes())?; - outfile_stream.write(b"\n")?; - } - - Ok(()) -} - -#[main] -async fn main() -> Result<()> { - let args = Args::parse(); - - let socket_path = tmp("/tmp/qemu-", ".sock"); - let plugin_path = tmp("/tmp/qemu-", ".so"); - - write(&plugin_path, PLUGIN).await?; - - let input = if let Some(input_file) = args.input_file.as_ref() { - let Ok(input_file) = input_file.canonicalize() else { - return Err(anyhow!("Failed to canonicalize input file")); - }; - - Some(read(input_file).await?) - } else { - None - }; - - let listen_sock = UnixListener::bind(&socket_path)?; - - let qemu_args = args.to_qemu_args(&socket_path, &plugin_path)?; - let qemu_task = spawn(async move { run(input, qemu_args).await }); - - let socket_task = spawn_blocking(move || listen(listen_sock, args.output_file.as_ref())); - - let (qemu_res, socket_res) = join!(qemu_task, socket_task); - - remove_file(&plugin_path).await?; - remove_file(&socket_path).await?; - - qemu_res??; - - socket_res??; - - Ok(()) -} diff --git a/plugins/tracer/src/lib.rs b/plugins/tracer/src/lib.rs index 8f36208..6405578 100644 --- a/plugins/tracer/src/lib.rs +++ b/plugins/tracer/src/lib.rs @@ -5,6 +5,8 @@ use qemu_plugin::{ plugin::{HasCallbacks, Plugin, Register, PLUGIN}, Instruction, MemRW, MemoryInfo, PluginId, TranslationBlock, VCPUIndex, }; +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +use qemu_plugin::{qemu_plugin_get_registers, RegisterDescriptor}; use serde::{Deserialize, Serialize}; use serde_cbor::to_writer; use std::{ @@ -95,9 +97,17 @@ pub struct SyscallEvent { pub args: [u64; 8], } +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct Registers(pub HashMap>); + #[derive(Clone, Debug, Deserialize, Serialize)] pub enum Event { - Instruction(InstructionEvent), + Instruction { + event: InstructionEvent, + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] + registers: Registers, + }, Memory(MemoryEvent), Syscall(SyscallEvent), } @@ -105,6 +115,8 @@ pub enum Event { #[derive(TypedBuilder, Clone, Debug)] struct Tracer { pub syscalls: Arc>>, + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] + pub registers: Arc>>>, #[builder(default)] pub tx: Arc>>, #[builder(default)] @@ -113,17 +125,46 @@ struct Tracer { pub log_mem: bool, #[builder(default)] pub log_syscalls: bool, + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] + #[builder(default)] + pub log_registers: bool, } impl Tracer { pub fn new() -> Self { - Self::builder() - .syscalls(Arc::new(Mutex::new(HashMap::new()))) - .build() + #[cfg(feature = "plugin-api-v1")] + { + Self::builder() + .syscalls(Arc::new(Mutex::new(HashMap::new()))) + .build() + } + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] + { + Self::builder() + .syscalls(Arc::new(Mutex::new(HashMap::new()))) + .registers(Arc::new(Mutex::new(Vec::new()))) + .build() + } } } impl HasCallbacks for Tracer { + fn on_vcpu_init( + &mut self, + _id: PluginId, + _vcpu_id: VCPUIndex, + ) -> std::prelude::v1::Result<(), anyhow::Error> { + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] + { + *self + .registers + .lock() + .map_err(|e| anyhow!("Failed to lock registers: {}", e))? = + qemu_plugin_get_registers()?; + } + Ok(()) + } + fn on_translation_block_translate( &mut self, _id: PluginId, @@ -134,6 +175,11 @@ impl HasCallbacks for Tracer { if self.log_insns { let tx = self.tx.clone(); + let registers = self + .registers + .lock() + .map_err(|e| anyhow!("Failed to lock registers: {}", e))? + .clone(); insn.register_execute_callback(move |_| { tx.lock() @@ -141,7 +187,22 @@ impl HasCallbacks for Tracer { .and_then(|tx| { to_writer( tx.as_ref().ok_or_else(|| anyhow!("No tx"))?, - &Event::Instruction(event.clone()), + &Event::Instruction { + event: event.clone(), + #[cfg(any( + feature = "plugin-api-v2", + feature = "plugin-api-v3" + ))] + registers: Registers( + registers + .iter() + .map(|r| { + let value = r.read().unwrap_or_else(|_| vec![]); + (r.name.clone(), value) + }) + .collect(), + ), + }, ) .map_err(|e| anyhow!(e)) }) @@ -262,6 +323,8 @@ pub struct PluginArgs { pub log_insns: bool, pub log_mem: bool, pub log_syscalls: bool, + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] + pub log_registers: bool, pub socket_path: PathBuf, } @@ -269,42 +332,91 @@ impl TryFrom<&Args> for PluginArgs { type Error = Error; fn try_from(value: &Args) -> Result { - Ok(Self::builder() - .log_insns( - value - .parsed - .get("log_insns") - .map(|li| if let Value::Bool(v) = li { *v } else { false }) - .unwrap_or_default(), - ) - .log_mem( - value - .parsed - .get("log_mem") - .map(|lm| if let Value::Bool(v) = lm { *v } else { false }) - .unwrap_or_default(), - ) - .log_syscalls( - value - .parsed - .get("log_syscalls") - .map(|ls| if let Value::Bool(v) = ls { *v } else { false }) - .unwrap_or_default(), - ) - .socket_path( - value - .parsed - .get("socket_path") - .and_then(|sp| { - if let Value::String(v) = sp { - Some(PathBuf::from(v)) - } else { - None - } - }) - .ok_or_else(|| anyhow!("No socket path provided"))?, - ) - .build()) + #[cfg(feature = "plugin-api-v1")] + { + Ok(Self::builder() + .log_insns( + value + .parsed + .get("log_insns") + .map(|li| if let Value::Bool(v) = li { *v } else { false }) + .unwrap_or_default(), + ) + .log_mem( + value + .parsed + .get("log_mem") + .map(|lm| if let Value::Bool(v) = lm { *v } else { false }) + .unwrap_or_default(), + ) + .log_syscalls( + value + .parsed + .get("log_syscalls") + .map(|ls| if let Value::Bool(v) = ls { *v } else { false }) + .unwrap_or_default(), + ) + .socket_path( + value + .parsed + .get("socket_path") + .and_then(|sp| { + if let Value::String(v) = sp { + Some(PathBuf::from(v)) + } else { + None + } + }) + .ok_or_else(|| anyhow!("No socket path provided"))?, + ) + .build()) + } + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] + { + Ok(Self::builder() + .log_insns( + value + .parsed + .get("log_insns") + .map(|li| if let Value::Bool(v) = li { *v } else { false }) + .unwrap_or_default(), + ) + .log_mem( + value + .parsed + .get("log_mem") + .map(|lm| if let Value::Bool(v) = lm { *v } else { false }) + .unwrap_or_default(), + ) + .log_syscalls( + value + .parsed + .get("log_syscalls") + .map(|ls| if let Value::Bool(v) = ls { *v } else { false }) + .unwrap_or_default(), + ) + .log_registers( + value + .parsed + .get("log_registers") + .map(|lr| if let Value::Bool(v) = lr { *v } else { false }) + .unwrap_or_default(), + ) + .socket_path( + value + .parsed + .get("socket_path") + .and_then(|sp| { + if let Value::String(v) = sp { + Some(PathBuf::from(v)) + } else { + None + } + }) + .ok_or_else(|| anyhow!("No socket path provided"))?, + ) + .build()) + } } } @@ -320,6 +432,11 @@ impl Register for Tracer { self.log_mem = plugin_args.log_mem; self.log_syscalls = plugin_args.log_syscalls; + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] + { + self.log_registers = plugin_args.log_registers; + } + Ok(()) } } diff --git a/qemu-plugin-sys/Cargo.toml b/qemu-plugin-sys/Cargo.toml index 3a2058a..1868666 100644 --- a/qemu-plugin-sys/Cargo.toml +++ b/qemu-plugin-sys/Cargo.toml @@ -12,7 +12,16 @@ repository.workspace = true version.workspace = true [build-dependencies] -anyhow = "1.0.75" +anyhow = "1.0.86" [lints.rust] -non_snake_case = "allow" \ No newline at end of file +non_snake_case = "allow" + +[features] +default = ["plugin-api-v2"] +# Use the V1 plugin API, which is defined for versions below 9.0.0 +plugin-api-v1 = [] +# Use the V2 plugin API, which is defined for version 9.0.0 +plugin-api-v2 = [] +# Use the V3 plugin API, which is defined for versions above 9.0.0 +plugin-api-v3 = [] diff --git a/qemu-plugin-sys/build.rs b/qemu-plugin-sys/build.rs index 36e8b6f..09c0b4e 100644 --- a/qemu-plugin-sys/build.rs +++ b/qemu-plugin-sys/build.rs @@ -4,6 +4,13 @@ use anyhow::Result; #[cfg(windows)] use std::{env::var, path::PathBuf, process::Command, str::FromStr}; +#[cfg(feature = "plugin-api-v1")] +pub const PLUGIN_API_DEF_FILE_NAME: &str = "qemu_plugin_api_v1.def"; +#[cfg(feature = "plugin-api-v2")] +pub const PLUGIN_API_DEF_FILE_NAME: &str = "qemu_plugin_api_v2.def"; +#[cfg(feature = "plugin-api-v3")] +pub const PLUGIN_API_DEF_FILE_NAME: &str = "qemu_plugin_api_v3.def"; + #[cfg(windows)] fn out_dir() -> Result { Ok(PathBuf::from( @@ -15,7 +22,7 @@ fn main() -> Result<()> { #[cfg(windows)] { let out_dir = out_dir()?; - let def_file = PathBuf::from_str("src/qemu_plugin_api.def")?; + let def_file = PathBuf::from_str(&format!("src/{PLUGIN_API_DEF_FILE_NAME}"))?; let def_file_str = def_file.to_string_lossy(); let lib_file = out_dir.join("qemu_plugin_api.lib"); let lib_file_str = lib_file.to_string_lossy(); @@ -36,5 +43,6 @@ fn main() -> Result<()> { println!("cargo:rustc-link-search={}", out_dir.display()); println!("cargo:rustc-link-lib=qemu_plugin_api"); } + Ok(()) } diff --git a/qemu-plugin-sys/generate-bindings.rs b/qemu-plugin-sys/generate-bindings.rs index 2e3b388..5584680 100755 --- a/qemu-plugin-sys/generate-bindings.rs +++ b/qemu-plugin-sys/generate-bindings.rs @@ -1,15 +1,19 @@ #!/usr/bin/env -S cargo +nightly-gnu -Z script -## [package] -## edition = "2021" -## [dependencies] -## anyhow = "*" -## bindgen = "*" -## cargo_metadata = "*" -## reqwest = { version = "*", features = ["blocking"] } -## tar = "*" -## xz2 = "*" -## [lints.rust] -## non_snake_case = "allow" +--- +[package] +edition = "2021" +[dependencies] +anyhow = "*" +bindgen = "*" +cargo_metadata = "*" +# pkg-config = "*" +reqwest = { version = "*", features = ["blocking"] } +tar = "*" +xz2 = "*" +zip = "*" +[lints.rust] +non_snake_case = "allow" +--- use anyhow::{anyhow, Result}; use bindgen::{ @@ -18,20 +22,33 @@ use bindgen::{ }; use cargo_metadata::MetadataCommand; use reqwest::blocking::get; -#[cfg(windows)] -use std::fs::{read_to_string, write}; use std::{ - fs::{create_dir_all, File, OpenOptions}, + fs::{create_dir_all, read_to_string, write, File, OpenOptions}, path::Path, }; use tar::Archive; use xz2::read::XzDecoder; +use zip::ZipArchive; +const QEMU_GITHUB_URL_BASE: &str = "https://github.com/qemu/qemu/"; const QEMU_SRC_URL_BASE: &str = "https://download.qemu.org/"; -const QEMU_VERSION: &str = "8.2.0"; +// Plugin V1 is from introduction to 8.2.4 +const QEMU_VERSION_V1: &str = "8.2.4"; +// Plugin V2 is from 9.0.0 +const QEMU_VERSION_V2: &str = "9.0.0"; +/// Plugin V3 is from 4abc892362f8282450f18c4e45c5b0534461d01e +const QEMU_VERSION_V3: &str = "4abc892362f8282450f18c4e45c5b0534461d01e"; -fn qemu_src_url() -> String { - format!("{}qemu-{}.tar.xz", QEMU_SRC_URL_BASE, QEMU_VERSION) +fn qemu_src_url_v1() -> String { + format!("{}qemu-{}.tar.xz", QEMU_SRC_URL_BASE, QEMU_VERSION_V1) +} + +fn qemu_src_url_v2() -> String { + format!("{}qemu-{}.tar.xz", QEMU_SRC_URL_BASE, QEMU_VERSION_V2) +} + +fn qemu_src_url_v3() -> String { + format!("{}/archive/4abc892362f8282450f18c4e45c5b0534461d01e.zip", QEMU_GITHUB_URL_BASE) } /// Download a URL to a destination, using a blocking request @@ -74,21 +91,40 @@ fn extract_txz(archive: &Path, destination: &Path) -> Result<()> { Ok(()) } -#[cfg(windows)] -fn generate_windows_delaylink_library(qemu_plugin_symbols: &Path, out_dir: &Path) -> Result<()> { - let def_file = out_dir.join("qemu_plugin_api.def"); +/// Extract a zip frile at a path to a destination +fn extract_zip(archive: &Path, destination: &Path) -> Result<()> { + let archive = File::open(archive)?; + let mut archive = ZipArchive::new(archive)?; + archive.extract(destination)?; + Ok(()) +} + +fn generate_windows_delaylink_library(qemu_plugin_symbols: &Path, destination: &Path) -> Result<()> { let all_commands = read_to_string(qemu_plugin_symbols)?; let all_commands = all_commands.replace(|x| "{};".contains(x), ""); - write(&def_file, format!("EXPORTS\n{all_commands}"))?; + write(destination, format!("EXPORTS\n{all_commands}"))?; Ok(()) } fn generate_bindings(qemu_plugin_header: &Path, destination: &Path) -> Result<()> { + // let glib_20 = probe_library("glib-2.0").map_err(|e| anyhow!(e))?; + let header_contents = read_to_string(qemu_plugin_header)?; + let header_file_name = qemu_plugin_header.file_name().ok_or_else(|| anyhow!("Failed to get file name"))?.to_str().ok_or_else(|| anyhow!("Failed to convert file name to string"))?; + let header_contents = header_contents.replace("#include ", ""); + // Append `typedef GArray void;` and `typedef GByteArray void;` to the header. Otherwise, we + // need to use pkg_config to find the glib-2.0 include paths and our bindings will be + // massive. + let header_contents = format!( + "{}\n{}\n{}\n", + "typedef struct GArray { char *data; unsigned int len; } GArray;", + "typedef struct GByteArray { unsigned char *data; unsigned int len; } GByteArray;", + header_contents, + ); + let rust_bindings = builder() .clang_arg("-fretain-comments-from-system-headers") .clang_arg("-fparse-all-comments") - // We don't care at all what warnings QEMU generates .clang_arg("-Wno-everything") .default_visibility(FieldVisibilityKind::Public) .default_alias_style(AliasVariation::TypeAlias) @@ -104,7 +140,8 @@ fn generate_bindings(qemu_plugin_header: &Path, destination: &Path) -> Result<() .derive_eq(true) .derive_partialeq(true) .generate_comments(true) - .header(qemu_plugin_header.to_str().unwrap()) + .layout_tests(false) + .header_contents(header_file_name, &header_contents) // Blocklist because we will define these items .blocklist_function("qemu_plugin_install") .blocklist_item("qemu_plugin_version") @@ -477,34 +514,76 @@ fn main() -> Result<()> { .join("src") .into_std_path_buf(); - println!("out_dir: {:?}", out_dir); - let tmp_dir = metadata.target_directory.join("tmp").into_std_path_buf(); if !tmp_dir.exists() { create_dir_all(&tmp_dir)?; } - let src_archive = tmp_dir.join(format!("qemu-{}.tar.xz", QEMU_VERSION)); - let src_dir = tmp_dir.join(format!("qemu-{}", QEMU_VERSION)); + let src_archive_v1 = tmp_dir.join(format!("qemu-{}.tar.xz", QEMU_VERSION_V1)); + let src_dir_v1 = tmp_dir.join(format!("qemu-{}", QEMU_VERSION_V1)); + + if !src_archive_v1.exists() { + download(&qemu_src_url_v1(), &src_archive_v1)?; + } + + if !src_dir_v1.exists() { + extract_txz(&src_archive_v1, &src_dir_v1)?; + } + + let src_archive_v2 = tmp_dir.join(format!("qemu-{}.tar.xz", QEMU_VERSION_V2)); + let src_dir_v2 = tmp_dir.join(format!("qemu-{}", QEMU_VERSION_V2)); + + if !src_archive_v2.exists() { + download(&qemu_src_url_v2(), &src_archive_v2)?; + } + + if !src_dir_v2.exists() { + extract_txz(&src_archive_v2, &src_dir_v2)?; + } + + let src_archive_v3 = tmp_dir.join(format!("qemu-{}.zip", QEMU_VERSION_V3)); + let src_dir_v3 = tmp_dir.join(format!("qemu-{}", QEMU_VERSION_V3)); - if !src_archive.exists() { - download(&qemu_src_url(), &src_archive)?; + if !src_archive_v3.exists() { + download(&qemu_src_url_v3(), &src_archive_v3)?; } - if !src_dir.exists() { - extract_txz(&src_archive, &src_dir)?; + if !src_dir_v3.exists() { + // NOTE: ZipArchive::extract extracts into the directory given a directory named + // the same as the ZIP file. + extract_zip(&src_archive_v3, &tmp_dir)?; } - #[cfg(windows)] generate_windows_delaylink_library( - &src_dir.join("plugins").join("qemu-plugins.symbols"), - &out_dir, + &src_dir_v1.join("plugins").join("qemu-plugins.symbols"), + &out_dir.join("qemu_plugin_api_v1.def"), + )?; + + generate_windows_delaylink_library( + &src_dir_v2.join("plugins").join("qemu-plugins.symbols"), + &out_dir.join("qemu_plugin_api_v2.def"), + )?; + + generate_windows_delaylink_library( + &src_dir_v3.join("plugins").join("qemu-plugins.symbols"), + &out_dir.join("qemu_plugin_api_v3.def"), + )?; + + + generate_bindings( + &src_dir_v1.join("include").join("qemu").join("qemu-plugin.h"), + &out_dir.join("bindings_v1.rs"), + )?; + + generate_bindings( + &src_dir_v2.join("include").join("qemu").join("qemu-plugin.h"), + &out_dir.join("bindings_v2.rs"), )?; generate_bindings( - &src_dir.join("include").join("qemu").join("qemu-plugin.h"), - &out_dir.join("bindings.rs"), + &src_dir_v3.join("include").join("qemu").join("qemu-plugin.h"), + &out_dir.join("bindings_v3.rs"), )?; Ok(()) diff --git a/qemu-plugin-sys/src/bindings.rs b/qemu-plugin-sys/src/bindings_v1.rs similarity index 83% rename from qemu-plugin-sys/src/bindings.rs rename to qemu-plugin-sys/src/bindings_v1.rs index 139b51e..c136161 100644 --- a/qemu-plugin-sys/src/bindings.rs +++ b/qemu-plugin-sys/src/bindings_v1.rs @@ -1,6 +1,38 @@ -/* automatically generated by rust-bindgen 0.69.1 */ +/* automatically generated by rust-bindgen 0.69.4 */ +pub const __SYSCALL_WORDSIZE: u32 = 64; +pub const __KERNEL_OLD_TIMEVAL_MATCHES_TIMEVAL64: u32 = 1; pub const QEMU_PLUGIN_VERSION: u32 = 1; +#[repr(C)] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct GArray { + pub data: *mut ::std::os::raw::c_char, + pub len: ::std::os::raw::c_uint, +} +impl Default for GArray { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[repr(C)] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct GByteArray { + pub data: *mut ::std::os::raw::c_uchar, + pub len: ::std::os::raw::c_uint, +} +impl Default for GByteArray { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} #[doc = " typedef qemu_plugin_id_t - Unique plugin ID"] pub type qemu_plugin_id_t = u64; #[doc = " struct qemu_info_t - system information for plugins\n\n This structure provides for some limited information about the\n system to allow the plugin to make decisions on how to proceed. For\n example it might only be suitable for running on some guest\n architectures or when under full system emulation."] @@ -21,42 +53,6 @@ pub struct qemu_info_t__bindgen_ty_1 { pub min: ::std::os::raw::c_int, pub cur: ::std::os::raw::c_int, } -#[test] -fn bindgen_test_layout_qemu_info_t__bindgen_ty_1() { - const UNINIT: ::std::mem::MaybeUninit = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::(), - 8usize, - concat!("Size of: ", stringify!(qemu_info_t__bindgen_ty_1)) - ); - assert_eq!( - ::std::mem::align_of::(), - 4usize, - concat!("Alignment of ", stringify!(qemu_info_t__bindgen_ty_1)) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).min) as usize - ptr as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(qemu_info_t__bindgen_ty_1), - "::", - stringify!(min) - ) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).cur) as usize - ptr as usize }, - 4usize, - concat!( - "Offset of field: ", - stringify!(qemu_info_t__bindgen_ty_1), - "::", - stringify!(cur) - ) - ); -} #[repr(C)] #[derive(Copy, Clone)] pub union qemu_info_t__bindgen_ty_2 { @@ -71,74 +67,6 @@ pub struct qemu_info_t__bindgen_ty_2__bindgen_ty_1 { #[doc = " @system.max_vcpus: maximum possible number of vCPUs"] pub max_vcpus: ::std::os::raw::c_int, } -#[test] -fn bindgen_test_layout_qemu_info_t__bindgen_ty_2__bindgen_ty_1() { - const UNINIT: ::std::mem::MaybeUninit = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::(), - 8usize, - concat!( - "Size of: ", - stringify!(qemu_info_t__bindgen_ty_2__bindgen_ty_1) - ) - ); - assert_eq!( - ::std::mem::align_of::(), - 4usize, - concat!( - "Alignment of ", - stringify!(qemu_info_t__bindgen_ty_2__bindgen_ty_1) - ) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).smp_vcpus) as usize - ptr as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(qemu_info_t__bindgen_ty_2__bindgen_ty_1), - "::", - stringify!(smp_vcpus) - ) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).max_vcpus) as usize - ptr as usize }, - 4usize, - concat!( - "Offset of field: ", - stringify!(qemu_info_t__bindgen_ty_2__bindgen_ty_1), - "::", - stringify!(max_vcpus) - ) - ); -} -#[test] -fn bindgen_test_layout_qemu_info_t__bindgen_ty_2() { - const UNINIT: ::std::mem::MaybeUninit = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::(), - 8usize, - concat!("Size of: ", stringify!(qemu_info_t__bindgen_ty_2)) - ); - assert_eq!( - ::std::mem::align_of::(), - 4usize, - concat!("Alignment of ", stringify!(qemu_info_t__bindgen_ty_2)) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).system) as usize - ptr as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(qemu_info_t__bindgen_ty_2), - "::", - stringify!(system) - ) - ); -} impl Default for qemu_info_t__bindgen_ty_2 { fn default() -> Self { let mut s = ::std::mem::MaybeUninit::::uninit(); @@ -148,51 +76,6 @@ impl Default for qemu_info_t__bindgen_ty_2 { } } } -#[test] -fn bindgen_test_layout_qemu_info_t() { - const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::(), - 32usize, - concat!("Size of: ", stringify!(qemu_info_t)) - ); - assert_eq!( - ::std::mem::align_of::(), - 8usize, - concat!("Alignment of ", stringify!(qemu_info_t)) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).target_name) as usize - ptr as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(qemu_info_t), - "::", - stringify!(target_name) - ) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).version) as usize - ptr as usize }, - 8usize, - concat!( - "Offset of field: ", - stringify!(qemu_info_t), - "::", - stringify!(version) - ) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).system_emulation) as usize - ptr as usize }, - 16usize, - concat!( - "Offset of field: ", - stringify!(qemu_info_t), - "::", - stringify!(system_emulation) - ) - ); -} impl Default for qemu_info_t { fn default() -> Self { let mut s = ::std::mem::MaybeUninit::::uninit(); diff --git a/qemu-plugin-sys/src/bindings_v2.rs b/qemu-plugin-sys/src/bindings_v2.rs new file mode 100644 index 0000000..44e5517 --- /dev/null +++ b/qemu-plugin-sys/src/bindings_v2.rs @@ -0,0 +1,504 @@ +/* automatically generated by rust-bindgen 0.69.4 */ + +pub const __SYSCALL_WORDSIZE: u32 = 64; +pub const __KERNEL_OLD_TIMEVAL_MATCHES_TIMEVAL64: u32 = 1; +pub const QEMU_PLUGIN_VERSION: u32 = 2; +#[repr(C)] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct GArray { + pub data: *mut ::std::os::raw::c_char, + pub len: ::std::os::raw::c_uint, +} +impl Default for GArray { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[repr(C)] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct GByteArray { + pub data: *mut ::std::os::raw::c_uchar, + pub len: ::std::os::raw::c_uint, +} +impl Default for GByteArray { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[doc = " typedef qemu_plugin_id_t - Unique plugin ID"] +pub type qemu_plugin_id_t = u64; +#[doc = " struct qemu_info_t - system information for plugins\n\n This structure provides for some limited information about the\n system to allow the plugin to make decisions on how to proceed. For\n example it might only be suitable for running on some guest\n architectures or when under full system emulation."] +#[repr(C)] +#[derive(Copy, Clone)] +pub struct qemu_info_t { + #[doc = " @target_name: string describing architecture"] + pub target_name: *const ::std::os::raw::c_char, + pub version: qemu_info_t__bindgen_ty_1, + #[doc = " @system_emulation: is this a full system emulation?"] + pub system_emulation: bool, + pub __bindgen_anon_1: qemu_info_t__bindgen_ty_2, +} +#[doc = " @version: minimum and current plugin API level"] +#[repr(C)] +#[derive(Debug, Default, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct qemu_info_t__bindgen_ty_1 { + pub min: ::std::os::raw::c_int, + pub cur: ::std::os::raw::c_int, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union qemu_info_t__bindgen_ty_2 { + pub system: qemu_info_t__bindgen_ty_2__bindgen_ty_1, +} +#[doc = " @system: information relevant to system emulation"] +#[repr(C)] +#[derive(Debug, Default, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct qemu_info_t__bindgen_ty_2__bindgen_ty_1 { + #[doc = " @system.smp_vcpus: initial number of vCPUs"] + pub smp_vcpus: ::std::os::raw::c_int, + #[doc = " @system.max_vcpus: maximum possible number of vCPUs"] + pub max_vcpus: ::std::os::raw::c_int, +} +impl Default for qemu_info_t__bindgen_ty_2 { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +impl Default for qemu_info_t { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[doc = " typedef qemu_plugin_simple_cb_t - simple callback\n @id: the unique qemu_plugin_id_t\n\n This callback passes no information aside from the unique @id."] +pub type qemu_plugin_simple_cb_t = + ::std::option::Option; +#[doc = " typedef qemu_plugin_udata_cb_t - callback with user data\n @id: the unique qemu_plugin_id_t\n @userdata: a pointer to some user data supplied when the callback\n was registered."] +pub type qemu_plugin_udata_cb_t = ::std::option::Option< + unsafe extern "C" fn(id: qemu_plugin_id_t, userdata: *mut ::std::os::raw::c_void), +>; +#[doc = " typedef qemu_plugin_vcpu_simple_cb_t - vcpu callback\n @id: the unique qemu_plugin_id_t\n @vcpu_index: the current vcpu context"] +pub type qemu_plugin_vcpu_simple_cb_t = ::std::option::Option< + unsafe extern "C" fn(id: qemu_plugin_id_t, vcpu_index: ::std::os::raw::c_uint), +>; +#[doc = " typedef qemu_plugin_vcpu_udata_cb_t - vcpu callback\n @vcpu_index: the current vcpu context\n @userdata: a pointer to some user data supplied when the callback\n was registered."] +pub type qemu_plugin_vcpu_udata_cb_t = ::std::option::Option< + unsafe extern "C" fn(vcpu_index: ::std::os::raw::c_uint, userdata: *mut ::std::os::raw::c_void), +>; +extern "C" { + #[doc = " qemu_plugin_uninstall() - Uninstall a plugin\n @id: this plugin's opaque ID\n @cb: callback to be called once the plugin has been removed\n\n Do NOT assume that the plugin has been uninstalled once this function\n returns. Plugins are uninstalled asynchronously, and therefore the given\n plugin receives callbacks until @cb is called.\n\n Note: Calling this function from qemu_plugin_install() is a bug."] + pub fn qemu_plugin_uninstall(id: qemu_plugin_id_t, cb: qemu_plugin_simple_cb_t); +} +extern "C" { + #[doc = " qemu_plugin_reset() - Reset a plugin\n @id: this plugin's opaque ID\n @cb: callback to be called once the plugin has been reset\n\n Unregisters all callbacks for the plugin given by @id.\n\n Do NOT assume that the plugin has been reset once this function returns.\n Plugins are reset asynchronously, and therefore the given plugin receives\n callbacks until @cb is called."] + pub fn qemu_plugin_reset(id: qemu_plugin_id_t, cb: qemu_plugin_simple_cb_t); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_init_cb() - register a vCPU initialization callback\n @id: plugin ID\n @cb: callback function\n\n The @cb function is called every time a vCPU is initialized.\n\n See also: qemu_plugin_register_vcpu_exit_cb()"] + pub fn qemu_plugin_register_vcpu_init_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_simple_cb_t, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_exit_cb() - register a vCPU exit callback\n @id: plugin ID\n @cb: callback function\n\n The @cb function is called every time a vCPU exits.\n\n See also: qemu_plugin_register_vcpu_init_cb()"] + pub fn qemu_plugin_register_vcpu_exit_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_simple_cb_t, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_idle_cb() - register a vCPU idle callback\n @id: plugin ID\n @cb: callback function\n\n The @cb function is called every time a vCPU idles."] + pub fn qemu_plugin_register_vcpu_idle_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_simple_cb_t, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_resume_cb() - register a vCPU resume callback\n @id: plugin ID\n @cb: callback function\n\n The @cb function is called every time a vCPU resumes execution."] + pub fn qemu_plugin_register_vcpu_resume_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_simple_cb_t, + ); +} +#[doc = " struct qemu_plugin_tb - Opaque handle for a translation block"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct qemu_plugin_tb { + _unused: [u8; 0], +} +#[doc = " struct qemu_plugin_insn - Opaque handle for a translated instruction"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct qemu_plugin_insn { + _unused: [u8; 0], +} +#[doc = " struct qemu_plugin_scoreboard - Opaque handle for a scoreboard"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct qemu_plugin_scoreboard { + _unused: [u8; 0], +} +#[doc = " typedef qemu_plugin_u64 - uint64_t member of an entry in a scoreboard\n\n This field allows to access a specific uint64_t member in one given entry,\n located at a specified offset. Inline operations expect this as entry."] +#[repr(C)] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct qemu_plugin_u64 { + pub score: *mut qemu_plugin_scoreboard, + pub offset: usize, +} +impl Default for qemu_plugin_u64 { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[repr(u32)] +#[doc = " enum qemu_plugin_cb_flags - type of callback\n\n @QEMU_PLUGIN_CB_NO_REGS: callback does not access the CPU's regs\n @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs\n @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs\n\n Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change\n system register state."] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub enum qemu_plugin_cb_flags { + QEMU_PLUGIN_CB_NO_REGS = 0, + QEMU_PLUGIN_CB_R_REGS = 1, + QEMU_PLUGIN_CB_RW_REGS = 2, +} +#[repr(u32)] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub enum qemu_plugin_mem_rw { + QEMU_PLUGIN_MEM_R = 1, + QEMU_PLUGIN_MEM_W = 2, + QEMU_PLUGIN_MEM_RW = 3, +} +#[doc = " typedef qemu_plugin_vcpu_tb_trans_cb_t - translation callback\n @id: unique plugin id\n @tb: opaque handle used for querying and instrumenting a block."] +pub type qemu_plugin_vcpu_tb_trans_cb_t = + ::std::option::Option; +extern "C" { + #[doc = " qemu_plugin_register_vcpu_tb_trans_cb() - register a translate cb\n @id: plugin ID\n @cb: callback function\n\n The @cb function is called every time a translation occurs. The @cb\n function is passed an opaque qemu_plugin_type which it can query\n for additional information including the list of translated\n instructions. At this point the plugin can register further\n callbacks to be triggered when the block or individual instruction\n executes."] + pub fn qemu_plugin_register_vcpu_tb_trans_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_tb_trans_cb_t, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_tb_exec_cb() - register execution callback\n @tb: the opaque qemu_plugin_tb handle for the translation\n @cb: callback function\n @flags: does the plugin read or write the CPU's registers?\n @userdata: any plugin data to pass to the @cb?\n\n The @cb function is called every time a translated unit executes."] + pub fn qemu_plugin_register_vcpu_tb_exec_cb( + tb: *mut qemu_plugin_tb, + cb: qemu_plugin_vcpu_udata_cb_t, + flags: qemu_plugin_cb_flags, + userdata: *mut ::std::os::raw::c_void, + ); +} +#[repr(u32)] +#[doc = " enum qemu_plugin_op - describes an inline op\n\n @QEMU_PLUGIN_INLINE_ADD_U64: add an immediate value uint64_t\n\n Note: currently only a single inline op is supported."] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub enum qemu_plugin_op { + QEMU_PLUGIN_INLINE_ADD_U64 = 0, +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu() - execution inline op\n @tb: the opaque qemu_plugin_tb handle for the translation\n @op: the type of qemu_plugin_op (e.g. ADD_U64)\n @entry: entry to run op\n @imm: the op data (e.g. 1)\n\n Insert an inline op on a given scoreboard entry."] + pub fn qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu( + tb: *mut qemu_plugin_tb, + op: qemu_plugin_op, + entry: qemu_plugin_u64, + imm: u64, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_insn_exec_cb() - register insn execution cb\n @insn: the opaque qemu_plugin_insn handle for an instruction\n @cb: callback function\n @flags: does the plugin read or write the CPU's registers?\n @userdata: any plugin data to pass to the @cb?\n\n The @cb function is called every time an instruction is executed"] + pub fn qemu_plugin_register_vcpu_insn_exec_cb( + insn: *mut qemu_plugin_insn, + cb: qemu_plugin_vcpu_udata_cb_t, + flags: qemu_plugin_cb_flags, + userdata: *mut ::std::os::raw::c_void, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu() - insn exec inline op\n @insn: the opaque qemu_plugin_insn handle for an instruction\n @op: the type of qemu_plugin_op (e.g. ADD_U64)\n @entry: entry to run op\n @imm: the op data (e.g. 1)\n\n Insert an inline op to every time an instruction executes."] + pub fn qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu( + insn: *mut qemu_plugin_insn, + op: qemu_plugin_op, + entry: qemu_plugin_u64, + imm: u64, + ); +} +extern "C" { + #[doc = " qemu_plugin_tb_n_insns() - query helper for number of insns in TB\n @tb: opaque handle to TB passed to callback\n\n Returns: number of instructions in this block"] + pub fn qemu_plugin_tb_n_insns(tb: *const qemu_plugin_tb) -> usize; +} +extern "C" { + #[doc = " qemu_plugin_tb_vaddr() - query helper for vaddr of TB start\n @tb: opaque handle to TB passed to callback\n\n Returns: virtual address of block start"] + pub fn qemu_plugin_tb_vaddr(tb: *const qemu_plugin_tb) -> u64; +} +extern "C" { + #[doc = " qemu_plugin_tb_get_insn() - retrieve handle for instruction\n @tb: opaque handle to TB passed to callback\n @idx: instruction number, 0 indexed\n\n The returned handle can be used in follow up helper queries as well\n as when instrumenting an instruction. It is only valid for the\n lifetime of the callback.\n\n Returns: opaque handle to instruction"] + pub fn qemu_plugin_tb_get_insn(tb: *const qemu_plugin_tb, idx: usize) -> *mut qemu_plugin_insn; +} +extern "C" { + #[doc = " qemu_plugin_insn_data() - return ptr to instruction data\n @insn: opaque instruction handle from qemu_plugin_tb_get_insn()\n\n Note: data is only valid for duration of callback. See\n qemu_plugin_insn_size() to calculate size of stream.\n\n Returns: pointer to a stream of bytes containing the value of this\n instructions opcode."] + pub fn qemu_plugin_insn_data(insn: *const qemu_plugin_insn) -> *const ::std::os::raw::c_void; +} +extern "C" { + #[doc = " qemu_plugin_insn_size() - return size of instruction\n @insn: opaque instruction handle from qemu_plugin_tb_get_insn()\n\n Returns: size of instruction in bytes"] + pub fn qemu_plugin_insn_size(insn: *const qemu_plugin_insn) -> usize; +} +extern "C" { + #[doc = " qemu_plugin_insn_vaddr() - return vaddr of instruction\n @insn: opaque instruction handle from qemu_plugin_tb_get_insn()\n\n Returns: virtual address of instruction"] + pub fn qemu_plugin_insn_vaddr(insn: *const qemu_plugin_insn) -> u64; +} +extern "C" { + #[doc = " qemu_plugin_insn_haddr() - return hardware addr of instruction\n @insn: opaque instruction handle from qemu_plugin_tb_get_insn()\n\n Returns: hardware (physical) target address of instruction"] + pub fn qemu_plugin_insn_haddr(insn: *const qemu_plugin_insn) -> *mut ::std::os::raw::c_void; +} +#[doc = " typedef qemu_plugin_meminfo_t - opaque memory transaction handle\n\n This can be further queried using the qemu_plugin_mem_* query\n functions."] +pub type qemu_plugin_meminfo_t = u32; +#[doc = " struct qemu_plugin_hwaddr - opaque hw address handle"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct qemu_plugin_hwaddr { + _unused: [u8; 0], +} +extern "C" { + #[doc = " qemu_plugin_mem_size_shift() - get size of access\n @info: opaque memory transaction handle\n\n Returns: size of access in ^2 (0=byte, 1=16bit, 2=32bit etc...)"] + pub fn qemu_plugin_mem_size_shift(info: qemu_plugin_meminfo_t) -> ::std::os::raw::c_uint; +} +extern "C" { + #[doc = " qemu_plugin_mem_is_sign_extended() - was the access sign extended\n @info: opaque memory transaction handle\n\n Returns: true if it was, otherwise false"] + pub fn qemu_plugin_mem_is_sign_extended(info: qemu_plugin_meminfo_t) -> bool; +} +extern "C" { + #[doc = " qemu_plugin_mem_is_big_endian() - was the access big endian\n @info: opaque memory transaction handle\n\n Returns: true if it was, otherwise false"] + pub fn qemu_plugin_mem_is_big_endian(info: qemu_plugin_meminfo_t) -> bool; +} +extern "C" { + #[doc = " qemu_plugin_mem_is_store() - was the access a store\n @info: opaque memory transaction handle\n\n Returns: true if it was, otherwise false"] + pub fn qemu_plugin_mem_is_store(info: qemu_plugin_meminfo_t) -> bool; +} +extern "C" { + #[doc = " qemu_plugin_get_hwaddr() - return handle for memory operation\n @info: opaque memory info structure\n @vaddr: the virtual address of the memory operation\n\n For system emulation returns a qemu_plugin_hwaddr handle to query\n details about the actual physical address backing the virtual\n address. For linux-user guests it just returns NULL.\n\n This handle is *only* valid for the duration of the callback. Any\n information about the handle should be recovered before the\n callback returns."] + pub fn qemu_plugin_get_hwaddr( + info: qemu_plugin_meminfo_t, + vaddr: u64, + ) -> *mut qemu_plugin_hwaddr; +} +extern "C" { + #[doc = " qemu_plugin_hwaddr_is_io() - query whether memory operation is IO\n @haddr: address handle from qemu_plugin_get_hwaddr()\n\n Returns true if the handle's memory operation is to memory-mapped IO, or\n false if it is to RAM"] + pub fn qemu_plugin_hwaddr_is_io(haddr: *const qemu_plugin_hwaddr) -> bool; +} +extern "C" { + #[doc = " qemu_plugin_hwaddr_phys_addr() - query physical address for memory operation\n @haddr: address handle from qemu_plugin_get_hwaddr()\n\n Returns the physical address associated with the memory operation\n\n Note that the returned physical address may not be unique if you are dealing\n with multiple address spaces."] + pub fn qemu_plugin_hwaddr_phys_addr(haddr: *const qemu_plugin_hwaddr) -> u64; +} +extern "C" { + #[doc = " Returns a string representing the device. The string is valid for\n the lifetime of the plugin."] + pub fn qemu_plugin_hwaddr_device_name( + h: *const qemu_plugin_hwaddr, + ) -> *const ::std::os::raw::c_char; +} +#[doc = " typedef qemu_plugin_vcpu_mem_cb_t - memory callback function type\n @vcpu_index: the executing vCPU\n @info: an opaque handle for further queries about the memory\n @vaddr: the virtual address of the transaction\n @userdata: any user data attached to the callback"] +pub type qemu_plugin_vcpu_mem_cb_t = ::std::option::Option< + unsafe extern "C" fn( + vcpu_index: ::std::os::raw::c_uint, + info: qemu_plugin_meminfo_t, + vaddr: u64, + userdata: *mut ::std::os::raw::c_void, + ), +>; +extern "C" { + #[doc = " qemu_plugin_register_vcpu_mem_cb() - register memory access callback\n @insn: handle for instruction to instrument\n @cb: callback of type qemu_plugin_vcpu_mem_cb_t\n @flags: (currently unused) callback flags\n @rw: monitor reads, writes or both\n @userdata: opaque pointer for userdata\n\n This registers a full callback for every memory access generated by\n an instruction. If the instruction doesn't access memory no\n callback will be made.\n\n The callback reports the vCPU the access took place on, the virtual\n address of the access and a handle for further queries. The user\n can attach some userdata to the callback for additional purposes.\n\n Other execution threads will continue to execute during the\n callback so the plugin is responsible for ensuring it doesn't get\n confused by making appropriate use of locking if required."] + pub fn qemu_plugin_register_vcpu_mem_cb( + insn: *mut qemu_plugin_insn, + cb: qemu_plugin_vcpu_mem_cb_t, + flags: qemu_plugin_cb_flags, + rw: qemu_plugin_mem_rw, + userdata: *mut ::std::os::raw::c_void, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_mem_inline_per_vcpu() - inline op for mem access\n @insn: handle for instruction to instrument\n @rw: apply to reads, writes or both\n @op: the op, of type qemu_plugin_op\n @entry: entry to run op\n @imm: immediate data for @op\n\n This registers a inline op every memory access generated by the\n instruction."] + pub fn qemu_plugin_register_vcpu_mem_inline_per_vcpu( + insn: *mut qemu_plugin_insn, + rw: qemu_plugin_mem_rw, + op: qemu_plugin_op, + entry: qemu_plugin_u64, + imm: u64, + ); +} +pub type qemu_plugin_vcpu_syscall_cb_t = ::std::option::Option< + unsafe extern "C" fn( + id: qemu_plugin_id_t, + vcpu_index: ::std::os::raw::c_uint, + num: i64, + a1: u64, + a2: u64, + a3: u64, + a4: u64, + a5: u64, + a6: u64, + a7: u64, + a8: u64, + ), +>; +extern "C" { + pub fn qemu_plugin_register_vcpu_syscall_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_syscall_cb_t, + ); +} +pub type qemu_plugin_vcpu_syscall_ret_cb_t = ::std::option::Option< + unsafe extern "C" fn( + id: qemu_plugin_id_t, + vcpu_idx: ::std::os::raw::c_uint, + num: i64, + ret: i64, + ), +>; +extern "C" { + pub fn qemu_plugin_register_vcpu_syscall_ret_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_syscall_ret_cb_t, + ); +} +extern "C" { + #[doc = " qemu_plugin_insn_disas() - return disassembly string for instruction\n @insn: instruction reference\n\n Returns an allocated string containing the disassembly"] + pub fn qemu_plugin_insn_disas(insn: *const qemu_plugin_insn) -> *mut ::std::os::raw::c_char; +} +extern "C" { + #[doc = " qemu_plugin_insn_symbol() - best effort symbol lookup\n @insn: instruction reference\n\n Return a static string referring to the symbol. This is dependent\n on the binary QEMU is running having provided a symbol table."] + pub fn qemu_plugin_insn_symbol(insn: *const qemu_plugin_insn) -> *const ::std::os::raw::c_char; +} +extern "C" { + #[doc = " qemu_plugin_vcpu_for_each() - iterate over the existing vCPU\n @id: plugin ID\n @cb: callback function\n\n The @cb function is called once for each existing vCPU.\n\n See also: qemu_plugin_register_vcpu_init_cb()"] + pub fn qemu_plugin_vcpu_for_each(id: qemu_plugin_id_t, cb: qemu_plugin_vcpu_simple_cb_t); +} +extern "C" { + pub fn qemu_plugin_register_flush_cb(id: qemu_plugin_id_t, cb: qemu_plugin_simple_cb_t); +} +extern "C" { + #[doc = " qemu_plugin_register_atexit_cb() - register exit callback\n @id: plugin ID\n @cb: callback\n @userdata: user data for callback\n\n The @cb function is called once execution has finished. Plugins\n should be able to free all their resources at this point much like\n after a reset/uninstall callback is called.\n\n In user-mode it is possible a few un-instrumented instructions from\n child threads may run before the host kernel reaps the threads."] + pub fn qemu_plugin_register_atexit_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_udata_cb_t, + userdata: *mut ::std::os::raw::c_void, + ); +} +extern "C" { + #[doc = " returns how many vcpus were started at this point"] + pub fn qemu_plugin_num_vcpus() -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " qemu_plugin_outs() - output string via QEMU's logging system\n @string: a string"] + pub fn qemu_plugin_outs(string: *const ::std::os::raw::c_char); +} +extern "C" { + #[doc = " qemu_plugin_bool_parse() - parses a boolean argument in the form of\n \"=[on|yes|true|off|no|false]\"\n\n @name: argument name, the part before the equals sign\n @val: argument value, what's after the equals sign\n @ret: output return value\n\n returns true if the combination @name=@val parses correctly to a boolean\n argument, and false otherwise"] + pub fn qemu_plugin_bool_parse( + name: *const ::std::os::raw::c_char, + val: *const ::std::os::raw::c_char, + ret: *mut bool, + ) -> bool; +} +extern "C" { + #[doc = " qemu_plugin_path_to_binary() - path to binary file being executed\n\n Return a string representing the path to the binary. For user-mode\n this is the main executable. For system emulation we currently\n return NULL. The user should g_free() the string once no longer\n needed."] + pub fn qemu_plugin_path_to_binary() -> *const ::std::os::raw::c_char; +} +extern "C" { + #[doc = " qemu_plugin_start_code() - returns start of text segment\n\n Returns the nominal start address of the main text segment in\n user-mode. Currently returns 0 for system emulation."] + pub fn qemu_plugin_start_code() -> u64; +} +extern "C" { + #[doc = " qemu_plugin_end_code() - returns end of text segment\n\n Returns the nominal end address of the main text segment in\n user-mode. Currently returns 0 for system emulation."] + pub fn qemu_plugin_end_code() -> u64; +} +extern "C" { + #[doc = " qemu_plugin_entry_code() - returns start address for module\n\n Returns the nominal entry address of the main text segment in\n user-mode. Currently returns 0 for system emulation."] + pub fn qemu_plugin_entry_code() -> u64; +} +#[doc = " struct qemu_plugin_register - Opaque handle for register access"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct qemu_plugin_register { + _unused: [u8; 0], +} +#[doc = " typedef qemu_plugin_reg_descriptor - register descriptions\n\n @handle: opaque handle for retrieving value with qemu_plugin_read_register\n @name: register name\n @feature: optional feature descriptor, can be NULL"] +#[repr(C)] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct qemu_plugin_reg_descriptor { + pub handle: *mut qemu_plugin_register, + pub name: *const ::std::os::raw::c_char, + pub feature: *const ::std::os::raw::c_char, +} +impl Default for qemu_plugin_reg_descriptor { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +extern "C" { + #[doc = " qemu_plugin_get_registers() - return register list for current vCPU\n\n Returns a potentially empty GArray of qemu_plugin_reg_descriptor.\n Caller frees the array (but not the const strings).\n\n Should be used from a qemu_plugin_register_vcpu_init_cb() callback\n after the vCPU is initialised, i.e. in the vCPU context."] + pub fn qemu_plugin_get_registers() -> *mut GArray; +} +extern "C" { + #[doc = " qemu_plugin_read_register() - read register for current vCPU\n\n @handle: a @qemu_plugin_reg_handle handle\n @buf: A GByteArray for the data owned by the plugin\n\n This function is only available in a context that register read access is\n explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag.\n\n Returns the size of the read register. The content of @buf is in target byte\n order. On failure returns -1."] + pub fn qemu_plugin_read_register( + handle: *mut qemu_plugin_register, + buf: *mut GByteArray, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " qemu_plugin_scoreboard_new() - alloc a new scoreboard\n\n @element_size: size (in bytes) for one entry\n\n Returns a pointer to a new scoreboard. It must be freed using\n qemu_plugin_scoreboard_free."] + pub fn qemu_plugin_scoreboard_new(element_size: usize) -> *mut qemu_plugin_scoreboard; +} +extern "C" { + #[doc = " qemu_plugin_scoreboard_free() - free a scoreboard\n @score: scoreboard to free"] + pub fn qemu_plugin_scoreboard_free(score: *mut qemu_plugin_scoreboard); +} +extern "C" { + #[doc = " qemu_plugin_scoreboard_find() - get pointer to an entry of a scoreboard\n @score: scoreboard to query\n @vcpu_index: entry index\n\n Returns address of entry of a scoreboard matching a given vcpu_index. This\n address can be modified later if scoreboard is resized."] + pub fn qemu_plugin_scoreboard_find( + score: *mut qemu_plugin_scoreboard, + vcpu_index: ::std::os::raw::c_uint, + ) -> *mut ::std::os::raw::c_void; +} +extern "C" { + #[doc = " qemu_plugin_u64_add() - add a value to a qemu_plugin_u64 for a given vcpu\n @entry: entry to query\n @vcpu_index: entry index\n @added: value to add"] + pub fn qemu_plugin_u64_add( + entry: qemu_plugin_u64, + vcpu_index: ::std::os::raw::c_uint, + added: u64, + ); +} +extern "C" { + #[doc = " qemu_plugin_u64_get() - get value of a qemu_plugin_u64 for a given vcpu\n @entry: entry to query\n @vcpu_index: entry index"] + pub fn qemu_plugin_u64_get(entry: qemu_plugin_u64, vcpu_index: ::std::os::raw::c_uint) -> u64; +} +extern "C" { + #[doc = " qemu_plugin_u64_set() - set value of a qemu_plugin_u64 for a given vcpu\n @entry: entry to query\n @vcpu_index: entry index\n @val: new value"] + pub fn qemu_plugin_u64_set( + entry: qemu_plugin_u64, + vcpu_index: ::std::os::raw::c_uint, + val: u64, + ); +} +extern "C" { + #[doc = " qemu_plugin_u64_sum() - return sum of all vcpu entries in a scoreboard\n @entry: entry to sum"] + pub fn qemu_plugin_u64_sum(entry: qemu_plugin_u64) -> u64; +} diff --git a/qemu-plugin-sys/src/bindings_v3.rs b/qemu-plugin-sys/src/bindings_v3.rs new file mode 100644 index 0000000..747a67e --- /dev/null +++ b/qemu-plugin-sys/src/bindings_v3.rs @@ -0,0 +1,508 @@ +/* automatically generated by rust-bindgen 0.69.4 */ + +pub const __SYSCALL_WORDSIZE: u32 = 64; +pub const __KERNEL_OLD_TIMEVAL_MATCHES_TIMEVAL64: u32 = 1; +pub const QEMU_PLUGIN_VERSION: u32 = 3; +#[repr(C)] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct GArray { + pub data: *mut ::std::os::raw::c_char, + pub len: ::std::os::raw::c_uint, +} +impl Default for GArray { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[repr(C)] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct GByteArray { + pub data: *mut ::std::os::raw::c_uchar, + pub len: ::std::os::raw::c_uint, +} +impl Default for GByteArray { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[doc = " typedef qemu_plugin_id_t - Unique plugin ID"] +pub type qemu_plugin_id_t = u64; +#[doc = " struct qemu_info_t - system information for plugins\n\n This structure provides for some limited information about the\n system to allow the plugin to make decisions on how to proceed. For\n example it might only be suitable for running on some guest\n architectures or when under full system emulation."] +#[repr(C)] +#[derive(Copy, Clone)] +pub struct qemu_info_t { + #[doc = " @target_name: string describing architecture"] + pub target_name: *const ::std::os::raw::c_char, + pub version: qemu_info_t__bindgen_ty_1, + #[doc = " @system_emulation: is this a full system emulation?"] + pub system_emulation: bool, + pub __bindgen_anon_1: qemu_info_t__bindgen_ty_2, +} +#[doc = " @version: minimum and current plugin API level"] +#[repr(C)] +#[derive(Debug, Default, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct qemu_info_t__bindgen_ty_1 { + pub min: ::std::os::raw::c_int, + pub cur: ::std::os::raw::c_int, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union qemu_info_t__bindgen_ty_2 { + pub system: qemu_info_t__bindgen_ty_2__bindgen_ty_1, +} +#[doc = " @system: information relevant to system emulation"] +#[repr(C)] +#[derive(Debug, Default, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct qemu_info_t__bindgen_ty_2__bindgen_ty_1 { + #[doc = " @system.smp_vcpus: initial number of vCPUs"] + pub smp_vcpus: ::std::os::raw::c_int, + #[doc = " @system.max_vcpus: maximum possible number of vCPUs"] + pub max_vcpus: ::std::os::raw::c_int, +} +impl Default for qemu_info_t__bindgen_ty_2 { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +impl Default for qemu_info_t { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[doc = " typedef qemu_plugin_simple_cb_t - simple callback\n @id: the unique qemu_plugin_id_t\n\n This callback passes no information aside from the unique @id."] +pub type qemu_plugin_simple_cb_t = + ::std::option::Option; +#[doc = " typedef qemu_plugin_udata_cb_t - callback with user data\n @id: the unique qemu_plugin_id_t\n @userdata: a pointer to some user data supplied when the callback\n was registered."] +pub type qemu_plugin_udata_cb_t = ::std::option::Option< + unsafe extern "C" fn(id: qemu_plugin_id_t, userdata: *mut ::std::os::raw::c_void), +>; +#[doc = " typedef qemu_plugin_vcpu_simple_cb_t - vcpu callback\n @id: the unique qemu_plugin_id_t\n @vcpu_index: the current vcpu context"] +pub type qemu_plugin_vcpu_simple_cb_t = ::std::option::Option< + unsafe extern "C" fn(id: qemu_plugin_id_t, vcpu_index: ::std::os::raw::c_uint), +>; +#[doc = " typedef qemu_plugin_vcpu_udata_cb_t - vcpu callback\n @vcpu_index: the current vcpu context\n @userdata: a pointer to some user data supplied when the callback\n was registered."] +pub type qemu_plugin_vcpu_udata_cb_t = ::std::option::Option< + unsafe extern "C" fn(vcpu_index: ::std::os::raw::c_uint, userdata: *mut ::std::os::raw::c_void), +>; +extern "C" { + #[doc = " qemu_plugin_uninstall() - Uninstall a plugin\n @id: this plugin's opaque ID\n @cb: callback to be called once the plugin has been removed\n\n Do NOT assume that the plugin has been uninstalled once this function\n returns. Plugins are uninstalled asynchronously, and therefore the given\n plugin receives callbacks until @cb is called.\n\n Note: Calling this function from qemu_plugin_install() is a bug."] + pub fn qemu_plugin_uninstall(id: qemu_plugin_id_t, cb: qemu_plugin_simple_cb_t); +} +extern "C" { + #[doc = " qemu_plugin_reset() - Reset a plugin\n @id: this plugin's opaque ID\n @cb: callback to be called once the plugin has been reset\n\n Unregisters all callbacks for the plugin given by @id.\n\n Do NOT assume that the plugin has been reset once this function returns.\n Plugins are reset asynchronously, and therefore the given plugin receives\n callbacks until @cb is called."] + pub fn qemu_plugin_reset(id: qemu_plugin_id_t, cb: qemu_plugin_simple_cb_t); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_init_cb() - register a vCPU initialization callback\n @id: plugin ID\n @cb: callback function\n\n The @cb function is called every time a vCPU is initialized.\n\n See also: qemu_plugin_register_vcpu_exit_cb()"] + pub fn qemu_plugin_register_vcpu_init_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_simple_cb_t, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_exit_cb() - register a vCPU exit callback\n @id: plugin ID\n @cb: callback function\n\n The @cb function is called every time a vCPU exits.\n\n See also: qemu_plugin_register_vcpu_init_cb()"] + pub fn qemu_plugin_register_vcpu_exit_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_simple_cb_t, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_idle_cb() - register a vCPU idle callback\n @id: plugin ID\n @cb: callback function\n\n The @cb function is called every time a vCPU idles."] + pub fn qemu_plugin_register_vcpu_idle_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_simple_cb_t, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_resume_cb() - register a vCPU resume callback\n @id: plugin ID\n @cb: callback function\n\n The @cb function is called every time a vCPU resumes execution."] + pub fn qemu_plugin_register_vcpu_resume_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_simple_cb_t, + ); +} +#[doc = " struct qemu_plugin_tb - Opaque handle for a translation block"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct qemu_plugin_tb { + _unused: [u8; 0], +} +#[doc = " struct qemu_plugin_insn - Opaque handle for a translated instruction"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct qemu_plugin_insn { + _unused: [u8; 0], +} +#[doc = " struct qemu_plugin_scoreboard - Opaque handle for a scoreboard"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct qemu_plugin_scoreboard { + _unused: [u8; 0], +} +#[doc = " typedef qemu_plugin_u64 - uint64_t member of an entry in a scoreboard\n\n This field allows to access a specific uint64_t member in one given entry,\n located at a specified offset. Inline operations expect this as entry."] +#[repr(C)] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct qemu_plugin_u64 { + pub score: *mut qemu_plugin_scoreboard, + pub offset: usize, +} +impl Default for qemu_plugin_u64 { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[repr(u32)] +#[doc = " enum qemu_plugin_cb_flags - type of callback\n\n @QEMU_PLUGIN_CB_NO_REGS: callback does not access the CPU's regs\n @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs\n @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs\n\n Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change\n system register state."] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub enum qemu_plugin_cb_flags { + QEMU_PLUGIN_CB_NO_REGS = 0, + QEMU_PLUGIN_CB_R_REGS = 1, + QEMU_PLUGIN_CB_RW_REGS = 2, +} +#[repr(u32)] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub enum qemu_plugin_mem_rw { + QEMU_PLUGIN_MEM_R = 1, + QEMU_PLUGIN_MEM_W = 2, + QEMU_PLUGIN_MEM_RW = 3, +} +#[doc = " typedef qemu_plugin_vcpu_tb_trans_cb_t - translation callback\n @id: unique plugin id\n @tb: opaque handle used for querying and instrumenting a block."] +pub type qemu_plugin_vcpu_tb_trans_cb_t = + ::std::option::Option; +extern "C" { + #[doc = " qemu_plugin_register_vcpu_tb_trans_cb() - register a translate cb\n @id: plugin ID\n @cb: callback function\n\n The @cb function is called every time a translation occurs. The @cb\n function is passed an opaque qemu_plugin_type which it can query\n for additional information including the list of translated\n instructions. At this point the plugin can register further\n callbacks to be triggered when the block or individual instruction\n executes."] + pub fn qemu_plugin_register_vcpu_tb_trans_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_tb_trans_cb_t, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_tb_exec_cb() - register execution callback\n @tb: the opaque qemu_plugin_tb handle for the translation\n @cb: callback function\n @flags: does the plugin read or write the CPU's registers?\n @userdata: any plugin data to pass to the @cb?\n\n The @cb function is called every time a translated unit executes."] + pub fn qemu_plugin_register_vcpu_tb_exec_cb( + tb: *mut qemu_plugin_tb, + cb: qemu_plugin_vcpu_udata_cb_t, + flags: qemu_plugin_cb_flags, + userdata: *mut ::std::os::raw::c_void, + ); +} +#[repr(u32)] +#[doc = " enum qemu_plugin_op - describes an inline op\n\n @QEMU_PLUGIN_INLINE_ADD_U64: add an immediate value uint64_t\n\n Note: currently only a single inline op is supported."] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub enum qemu_plugin_op { + QEMU_PLUGIN_INLINE_ADD_U64 = 0, +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu() - execution inline op\n @tb: the opaque qemu_plugin_tb handle for the translation\n @op: the type of qemu_plugin_op (e.g. ADD_U64)\n @entry: entry to run op\n @imm: the op data (e.g. 1)\n\n Insert an inline op on a given scoreboard entry."] + pub fn qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu( + tb: *mut qemu_plugin_tb, + op: qemu_plugin_op, + entry: qemu_plugin_u64, + imm: u64, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_insn_exec_cb() - register insn execution cb\n @insn: the opaque qemu_plugin_insn handle for an instruction\n @cb: callback function\n @flags: does the plugin read or write the CPU's registers?\n @userdata: any plugin data to pass to the @cb?\n\n The @cb function is called every time an instruction is executed"] + pub fn qemu_plugin_register_vcpu_insn_exec_cb( + insn: *mut qemu_plugin_insn, + cb: qemu_plugin_vcpu_udata_cb_t, + flags: qemu_plugin_cb_flags, + userdata: *mut ::std::os::raw::c_void, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu() - insn exec inline op\n @insn: the opaque qemu_plugin_insn handle for an instruction\n @op: the type of qemu_plugin_op (e.g. ADD_U64)\n @entry: entry to run op\n @imm: the op data (e.g. 1)\n\n Insert an inline op to every time an instruction executes."] + pub fn qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu( + insn: *mut qemu_plugin_insn, + op: qemu_plugin_op, + entry: qemu_plugin_u64, + imm: u64, + ); +} +extern "C" { + #[doc = " qemu_plugin_tb_n_insns() - query helper for number of insns in TB\n @tb: opaque handle to TB passed to callback\n\n Returns: number of instructions in this block"] + pub fn qemu_plugin_tb_n_insns(tb: *const qemu_plugin_tb) -> usize; +} +extern "C" { + #[doc = " qemu_plugin_tb_vaddr() - query helper for vaddr of TB start\n @tb: opaque handle to TB passed to callback\n\n Returns: virtual address of block start"] + pub fn qemu_plugin_tb_vaddr(tb: *const qemu_plugin_tb) -> u64; +} +extern "C" { + #[doc = " qemu_plugin_tb_get_insn() - retrieve handle for instruction\n @tb: opaque handle to TB passed to callback\n @idx: instruction number, 0 indexed\n\n The returned handle can be used in follow up helper queries as well\n as when instrumenting an instruction. It is only valid for the\n lifetime of the callback.\n\n Returns: opaque handle to instruction"] + pub fn qemu_plugin_tb_get_insn(tb: *const qemu_plugin_tb, idx: usize) -> *mut qemu_plugin_insn; +} +extern "C" { + #[doc = " qemu_plugin_insn_data() - copy instruction data\n @insn: opaque instruction handle from qemu_plugin_tb_get_insn()\n @dest: destination into which data is copied\n @len: length of dest\n\n Returns the number of bytes copied, minimum of @len and insn size."] + pub fn qemu_plugin_insn_data( + insn: *const qemu_plugin_insn, + dest: *mut ::std::os::raw::c_void, + len: usize, + ) -> usize; +} +extern "C" { + #[doc = " qemu_plugin_insn_size() - return size of instruction\n @insn: opaque instruction handle from qemu_plugin_tb_get_insn()\n\n Returns: size of instruction in bytes"] + pub fn qemu_plugin_insn_size(insn: *const qemu_plugin_insn) -> usize; +} +extern "C" { + #[doc = " qemu_plugin_insn_vaddr() - return vaddr of instruction\n @insn: opaque instruction handle from qemu_plugin_tb_get_insn()\n\n Returns: virtual address of instruction"] + pub fn qemu_plugin_insn_vaddr(insn: *const qemu_plugin_insn) -> u64; +} +extern "C" { + #[doc = " qemu_plugin_insn_haddr() - return hardware addr of instruction\n @insn: opaque instruction handle from qemu_plugin_tb_get_insn()\n\n Returns: hardware (physical) target address of instruction"] + pub fn qemu_plugin_insn_haddr(insn: *const qemu_plugin_insn) -> *mut ::std::os::raw::c_void; +} +#[doc = " typedef qemu_plugin_meminfo_t - opaque memory transaction handle\n\n This can be further queried using the qemu_plugin_mem_* query\n functions."] +pub type qemu_plugin_meminfo_t = u32; +#[doc = " struct qemu_plugin_hwaddr - opaque hw address handle"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct qemu_plugin_hwaddr { + _unused: [u8; 0], +} +extern "C" { + #[doc = " qemu_plugin_mem_size_shift() - get size of access\n @info: opaque memory transaction handle\n\n Returns: size of access in ^2 (0=byte, 1=16bit, 2=32bit etc...)"] + pub fn qemu_plugin_mem_size_shift(info: qemu_plugin_meminfo_t) -> ::std::os::raw::c_uint; +} +extern "C" { + #[doc = " qemu_plugin_mem_is_sign_extended() - was the access sign extended\n @info: opaque memory transaction handle\n\n Returns: true if it was, otherwise false"] + pub fn qemu_plugin_mem_is_sign_extended(info: qemu_plugin_meminfo_t) -> bool; +} +extern "C" { + #[doc = " qemu_plugin_mem_is_big_endian() - was the access big endian\n @info: opaque memory transaction handle\n\n Returns: true if it was, otherwise false"] + pub fn qemu_plugin_mem_is_big_endian(info: qemu_plugin_meminfo_t) -> bool; +} +extern "C" { + #[doc = " qemu_plugin_mem_is_store() - was the access a store\n @info: opaque memory transaction handle\n\n Returns: true if it was, otherwise false"] + pub fn qemu_plugin_mem_is_store(info: qemu_plugin_meminfo_t) -> bool; +} +extern "C" { + #[doc = " qemu_plugin_get_hwaddr() - return handle for memory operation\n @info: opaque memory info structure\n @vaddr: the virtual address of the memory operation\n\n For system emulation returns a qemu_plugin_hwaddr handle to query\n details about the actual physical address backing the virtual\n address. For linux-user guests it just returns NULL.\n\n This handle is *only* valid for the duration of the callback. Any\n information about the handle should be recovered before the\n callback returns."] + pub fn qemu_plugin_get_hwaddr( + info: qemu_plugin_meminfo_t, + vaddr: u64, + ) -> *mut qemu_plugin_hwaddr; +} +extern "C" { + #[doc = " qemu_plugin_hwaddr_is_io() - query whether memory operation is IO\n @haddr: address handle from qemu_plugin_get_hwaddr()\n\n Returns true if the handle's memory operation is to memory-mapped IO, or\n false if it is to RAM"] + pub fn qemu_plugin_hwaddr_is_io(haddr: *const qemu_plugin_hwaddr) -> bool; +} +extern "C" { + #[doc = " qemu_plugin_hwaddr_phys_addr() - query physical address for memory operation\n @haddr: address handle from qemu_plugin_get_hwaddr()\n\n Returns the physical address associated with the memory operation\n\n Note that the returned physical address may not be unique if you are dealing\n with multiple address spaces."] + pub fn qemu_plugin_hwaddr_phys_addr(haddr: *const qemu_plugin_hwaddr) -> u64; +} +extern "C" { + #[doc = " Returns a string representing the device. The string is valid for\n the lifetime of the plugin."] + pub fn qemu_plugin_hwaddr_device_name( + h: *const qemu_plugin_hwaddr, + ) -> *const ::std::os::raw::c_char; +} +#[doc = " typedef qemu_plugin_vcpu_mem_cb_t - memory callback function type\n @vcpu_index: the executing vCPU\n @info: an opaque handle for further queries about the memory\n @vaddr: the virtual address of the transaction\n @userdata: any user data attached to the callback"] +pub type qemu_plugin_vcpu_mem_cb_t = ::std::option::Option< + unsafe extern "C" fn( + vcpu_index: ::std::os::raw::c_uint, + info: qemu_plugin_meminfo_t, + vaddr: u64, + userdata: *mut ::std::os::raw::c_void, + ), +>; +extern "C" { + #[doc = " qemu_plugin_register_vcpu_mem_cb() - register memory access callback\n @insn: handle for instruction to instrument\n @cb: callback of type qemu_plugin_vcpu_mem_cb_t\n @flags: (currently unused) callback flags\n @rw: monitor reads, writes or both\n @userdata: opaque pointer for userdata\n\n This registers a full callback for every memory access generated by\n an instruction. If the instruction doesn't access memory no\n callback will be made.\n\n The callback reports the vCPU the access took place on, the virtual\n address of the access and a handle for further queries. The user\n can attach some userdata to the callback for additional purposes.\n\n Other execution threads will continue to execute during the\n callback so the plugin is responsible for ensuring it doesn't get\n confused by making appropriate use of locking if required."] + pub fn qemu_plugin_register_vcpu_mem_cb( + insn: *mut qemu_plugin_insn, + cb: qemu_plugin_vcpu_mem_cb_t, + flags: qemu_plugin_cb_flags, + rw: qemu_plugin_mem_rw, + userdata: *mut ::std::os::raw::c_void, + ); +} +extern "C" { + #[doc = " qemu_plugin_register_vcpu_mem_inline_per_vcpu() - inline op for mem access\n @insn: handle for instruction to instrument\n @rw: apply to reads, writes or both\n @op: the op, of type qemu_plugin_op\n @entry: entry to run op\n @imm: immediate data for @op\n\n This registers a inline op every memory access generated by the\n instruction."] + pub fn qemu_plugin_register_vcpu_mem_inline_per_vcpu( + insn: *mut qemu_plugin_insn, + rw: qemu_plugin_mem_rw, + op: qemu_plugin_op, + entry: qemu_plugin_u64, + imm: u64, + ); +} +pub type qemu_plugin_vcpu_syscall_cb_t = ::std::option::Option< + unsafe extern "C" fn( + id: qemu_plugin_id_t, + vcpu_index: ::std::os::raw::c_uint, + num: i64, + a1: u64, + a2: u64, + a3: u64, + a4: u64, + a5: u64, + a6: u64, + a7: u64, + a8: u64, + ), +>; +extern "C" { + pub fn qemu_plugin_register_vcpu_syscall_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_syscall_cb_t, + ); +} +pub type qemu_plugin_vcpu_syscall_ret_cb_t = ::std::option::Option< + unsafe extern "C" fn( + id: qemu_plugin_id_t, + vcpu_idx: ::std::os::raw::c_uint, + num: i64, + ret: i64, + ), +>; +extern "C" { + pub fn qemu_plugin_register_vcpu_syscall_ret_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_vcpu_syscall_ret_cb_t, + ); +} +extern "C" { + #[doc = " qemu_plugin_insn_disas() - return disassembly string for instruction\n @insn: instruction reference\n\n Returns an allocated string containing the disassembly"] + pub fn qemu_plugin_insn_disas(insn: *const qemu_plugin_insn) -> *mut ::std::os::raw::c_char; +} +extern "C" { + #[doc = " qemu_plugin_insn_symbol() - best effort symbol lookup\n @insn: instruction reference\n\n Return a static string referring to the symbol. This is dependent\n on the binary QEMU is running having provided a symbol table."] + pub fn qemu_plugin_insn_symbol(insn: *const qemu_plugin_insn) -> *const ::std::os::raw::c_char; +} +extern "C" { + #[doc = " qemu_plugin_vcpu_for_each() - iterate over the existing vCPU\n @id: plugin ID\n @cb: callback function\n\n The @cb function is called once for each existing vCPU.\n\n See also: qemu_plugin_register_vcpu_init_cb()"] + pub fn qemu_plugin_vcpu_for_each(id: qemu_plugin_id_t, cb: qemu_plugin_vcpu_simple_cb_t); +} +extern "C" { + pub fn qemu_plugin_register_flush_cb(id: qemu_plugin_id_t, cb: qemu_plugin_simple_cb_t); +} +extern "C" { + #[doc = " qemu_plugin_register_atexit_cb() - register exit callback\n @id: plugin ID\n @cb: callback\n @userdata: user data for callback\n\n The @cb function is called once execution has finished. Plugins\n should be able to free all their resources at this point much like\n after a reset/uninstall callback is called.\n\n In user-mode it is possible a few un-instrumented instructions from\n child threads may run before the host kernel reaps the threads."] + pub fn qemu_plugin_register_atexit_cb( + id: qemu_plugin_id_t, + cb: qemu_plugin_udata_cb_t, + userdata: *mut ::std::os::raw::c_void, + ); +} +extern "C" { + #[doc = " returns how many vcpus were started at this point"] + pub fn qemu_plugin_num_vcpus() -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " qemu_plugin_outs() - output string via QEMU's logging system\n @string: a string"] + pub fn qemu_plugin_outs(string: *const ::std::os::raw::c_char); +} +extern "C" { + #[doc = " qemu_plugin_bool_parse() - parses a boolean argument in the form of\n \"=[on|yes|true|off|no|false]\"\n\n @name: argument name, the part before the equals sign\n @val: argument value, what's after the equals sign\n @ret: output return value\n\n returns true if the combination @name=@val parses correctly to a boolean\n argument, and false otherwise"] + pub fn qemu_plugin_bool_parse( + name: *const ::std::os::raw::c_char, + val: *const ::std::os::raw::c_char, + ret: *mut bool, + ) -> bool; +} +extern "C" { + #[doc = " qemu_plugin_path_to_binary() - path to binary file being executed\n\n Return a string representing the path to the binary. For user-mode\n this is the main executable. For system emulation we currently\n return NULL. The user should g_free() the string once no longer\n needed."] + pub fn qemu_plugin_path_to_binary() -> *const ::std::os::raw::c_char; +} +extern "C" { + #[doc = " qemu_plugin_start_code() - returns start of text segment\n\n Returns the nominal start address of the main text segment in\n user-mode. Currently returns 0 for system emulation."] + pub fn qemu_plugin_start_code() -> u64; +} +extern "C" { + #[doc = " qemu_plugin_end_code() - returns end of text segment\n\n Returns the nominal end address of the main text segment in\n user-mode. Currently returns 0 for system emulation."] + pub fn qemu_plugin_end_code() -> u64; +} +extern "C" { + #[doc = " qemu_plugin_entry_code() - returns start address for module\n\n Returns the nominal entry address of the main text segment in\n user-mode. Currently returns 0 for system emulation."] + pub fn qemu_plugin_entry_code() -> u64; +} +#[doc = " struct qemu_plugin_register - Opaque handle for register access"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct qemu_plugin_register { + _unused: [u8; 0], +} +#[doc = " typedef qemu_plugin_reg_descriptor - register descriptions\n\n @handle: opaque handle for retrieving value with qemu_plugin_read_register\n @name: register name\n @feature: optional feature descriptor, can be NULL"] +#[repr(C)] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub struct qemu_plugin_reg_descriptor { + pub handle: *mut qemu_plugin_register, + pub name: *const ::std::os::raw::c_char, + pub feature: *const ::std::os::raw::c_char, +} +impl Default for qemu_plugin_reg_descriptor { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +extern "C" { + #[doc = " qemu_plugin_get_registers() - return register list for current vCPU\n\n Returns a potentially empty GArray of qemu_plugin_reg_descriptor.\n Caller frees the array (but not the const strings).\n\n Should be used from a qemu_plugin_register_vcpu_init_cb() callback\n after the vCPU is initialised, i.e. in the vCPU context."] + pub fn qemu_plugin_get_registers() -> *mut GArray; +} +extern "C" { + #[doc = " qemu_plugin_read_register() - read register for current vCPU\n\n @handle: a @qemu_plugin_reg_handle handle\n @buf: A GByteArray for the data owned by the plugin\n\n This function is only available in a context that register read access is\n explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag.\n\n Returns the size of the read register. The content of @buf is in target byte\n order. On failure returns -1."] + pub fn qemu_plugin_read_register( + handle: *mut qemu_plugin_register, + buf: *mut GByteArray, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " qemu_plugin_scoreboard_new() - alloc a new scoreboard\n\n @element_size: size (in bytes) for one entry\n\n Returns a pointer to a new scoreboard. It must be freed using\n qemu_plugin_scoreboard_free."] + pub fn qemu_plugin_scoreboard_new(element_size: usize) -> *mut qemu_plugin_scoreboard; +} +extern "C" { + #[doc = " qemu_plugin_scoreboard_free() - free a scoreboard\n @score: scoreboard to free"] + pub fn qemu_plugin_scoreboard_free(score: *mut qemu_plugin_scoreboard); +} +extern "C" { + #[doc = " qemu_plugin_scoreboard_find() - get pointer to an entry of a scoreboard\n @score: scoreboard to query\n @vcpu_index: entry index\n\n Returns address of entry of a scoreboard matching a given vcpu_index. This\n address can be modified later if scoreboard is resized."] + pub fn qemu_plugin_scoreboard_find( + score: *mut qemu_plugin_scoreboard, + vcpu_index: ::std::os::raw::c_uint, + ) -> *mut ::std::os::raw::c_void; +} +extern "C" { + #[doc = " qemu_plugin_u64_add() - add a value to a qemu_plugin_u64 for a given vcpu\n @entry: entry to query\n @vcpu_index: entry index\n @added: value to add"] + pub fn qemu_plugin_u64_add( + entry: qemu_plugin_u64, + vcpu_index: ::std::os::raw::c_uint, + added: u64, + ); +} +extern "C" { + #[doc = " qemu_plugin_u64_get() - get value of a qemu_plugin_u64 for a given vcpu\n @entry: entry to query\n @vcpu_index: entry index"] + pub fn qemu_plugin_u64_get(entry: qemu_plugin_u64, vcpu_index: ::std::os::raw::c_uint) -> u64; +} +extern "C" { + #[doc = " qemu_plugin_u64_set() - set value of a qemu_plugin_u64 for a given vcpu\n @entry: entry to query\n @vcpu_index: entry index\n @val: new value"] + pub fn qemu_plugin_u64_set( + entry: qemu_plugin_u64, + vcpu_index: ::std::os::raw::c_uint, + val: u64, + ); +} +extern "C" { + #[doc = " qemu_plugin_u64_sum() - return sum of all vcpu entries in a scoreboard\n @entry: entry to sum"] + pub fn qemu_plugin_u64_sum(entry: qemu_plugin_u64) -> u64; +} diff --git a/qemu-plugin-sys/src/lib.rs b/qemu-plugin-sys/src/lib.rs index 1b320b4..dc2464c 100644 --- a/qemu-plugin-sys/src/lib.rs +++ b/qemu-plugin-sys/src/lib.rs @@ -1,9 +1,16 @@ //! Low level bindings to the QEMU Plugin API -//! +//! //! These bindings are generated from the QEMU source code, and should not be used directly. //! Instead, use the `qeu-plugin` crate. #![allow(non_upper_case_globals)] #![allow(non_camel_case_types)] -include!("bindings.rs"); +#[cfg(feature = "plugin-api-v1")] +include!("bindings_v1.rs"); + +#[cfg(feature = "plugin-api-v2")] +include!("bindings_v2.rs"); + +#[cfg(feature = "plugin-api-v3")] +include!("bindings_v3.rs"); diff --git a/qemu-plugin-sys/src/qemu_plugin_api.def b/qemu-plugin-sys/src/qemu_plugin_api_v1.def similarity index 100% rename from qemu-plugin-sys/src/qemu_plugin_api.def rename to qemu-plugin-sys/src/qemu_plugin_api_v1.def diff --git a/qemu-plugin-sys/src/qemu_plugin_api_v2.def b/qemu-plugin-sys/src/qemu_plugin_api_v2.def new file mode 100644 index 0000000..a11358a --- /dev/null +++ b/qemu-plugin-sys/src/qemu_plugin_api_v2.def @@ -0,0 +1,54 @@ +EXPORTS + + qemu_plugin_bool_parse + qemu_plugin_end_code + qemu_plugin_entry_code + qemu_plugin_get_hwaddr + qemu_plugin_get_registers + qemu_plugin_hwaddr_device_name + qemu_plugin_hwaddr_is_io + qemu_plugin_hwaddr_phys_addr + qemu_plugin_insn_data + qemu_plugin_insn_disas + qemu_plugin_insn_haddr + qemu_plugin_insn_size + qemu_plugin_insn_symbol + qemu_plugin_insn_vaddr + qemu_plugin_mem_is_big_endian + qemu_plugin_mem_is_sign_extended + qemu_plugin_mem_is_store + qemu_plugin_mem_size_shift + qemu_plugin_num_vcpus + qemu_plugin_outs + qemu_plugin_path_to_binary + qemu_plugin_read_register + qemu_plugin_register_atexit_cb + qemu_plugin_register_flush_cb + qemu_plugin_register_vcpu_exit_cb + qemu_plugin_register_vcpu_idle_cb + qemu_plugin_register_vcpu_init_cb + qemu_plugin_register_vcpu_insn_exec_cb + qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu + qemu_plugin_register_vcpu_mem_cb + qemu_plugin_register_vcpu_mem_inline_per_vcpu + qemu_plugin_register_vcpu_resume_cb + qemu_plugin_register_vcpu_syscall_cb + qemu_plugin_register_vcpu_syscall_ret_cb + qemu_plugin_register_vcpu_tb_exec_cb + qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu + qemu_plugin_register_vcpu_tb_trans_cb + qemu_plugin_reset + qemu_plugin_scoreboard_free + qemu_plugin_scoreboard_find + qemu_plugin_scoreboard_new + qemu_plugin_start_code + qemu_plugin_tb_get_insn + qemu_plugin_tb_n_insns + qemu_plugin_tb_vaddr + qemu_plugin_u64_add + qemu_plugin_u64_get + qemu_plugin_u64_set + qemu_plugin_u64_sum + qemu_plugin_uninstall + qemu_plugin_vcpu_for_each + diff --git a/qemu-plugin-sys/src/qemu_plugin_api_v3.def b/qemu-plugin-sys/src/qemu_plugin_api_v3.def new file mode 100644 index 0000000..a11358a --- /dev/null +++ b/qemu-plugin-sys/src/qemu_plugin_api_v3.def @@ -0,0 +1,54 @@ +EXPORTS + + qemu_plugin_bool_parse + qemu_plugin_end_code + qemu_plugin_entry_code + qemu_plugin_get_hwaddr + qemu_plugin_get_registers + qemu_plugin_hwaddr_device_name + qemu_plugin_hwaddr_is_io + qemu_plugin_hwaddr_phys_addr + qemu_plugin_insn_data + qemu_plugin_insn_disas + qemu_plugin_insn_haddr + qemu_plugin_insn_size + qemu_plugin_insn_symbol + qemu_plugin_insn_vaddr + qemu_plugin_mem_is_big_endian + qemu_plugin_mem_is_sign_extended + qemu_plugin_mem_is_store + qemu_plugin_mem_size_shift + qemu_plugin_num_vcpus + qemu_plugin_outs + qemu_plugin_path_to_binary + qemu_plugin_read_register + qemu_plugin_register_atexit_cb + qemu_plugin_register_flush_cb + qemu_plugin_register_vcpu_exit_cb + qemu_plugin_register_vcpu_idle_cb + qemu_plugin_register_vcpu_init_cb + qemu_plugin_register_vcpu_insn_exec_cb + qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu + qemu_plugin_register_vcpu_mem_cb + qemu_plugin_register_vcpu_mem_inline_per_vcpu + qemu_plugin_register_vcpu_resume_cb + qemu_plugin_register_vcpu_syscall_cb + qemu_plugin_register_vcpu_syscall_ret_cb + qemu_plugin_register_vcpu_tb_exec_cb + qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu + qemu_plugin_register_vcpu_tb_trans_cb + qemu_plugin_reset + qemu_plugin_scoreboard_free + qemu_plugin_scoreboard_find + qemu_plugin_scoreboard_new + qemu_plugin_start_code + qemu_plugin_tb_get_insn + qemu_plugin_tb_n_insns + qemu_plugin_tb_vaddr + qemu_plugin_u64_add + qemu_plugin_u64_get + qemu_plugin_u64_set + qemu_plugin_u64_sum + qemu_plugin_uninstall + qemu_plugin_vcpu_for_each + diff --git a/qemu-plugin/Cargo.toml b/qemu-plugin/Cargo.toml index e41b1a5..1366bdc 100644 --- a/qemu-plugin/Cargo.toml +++ b/qemu-plugin/Cargo.toml @@ -12,10 +12,11 @@ repository.workspace = true version.workspace = true [dependencies] -anyhow = "1.0.75" +anyhow = "1.0.86" once_cell = "1.19.0" -qemu-plugin-sys = { version = "8.2.2-v0", workspace = true } -thiserror = "1.0.51" +qemu-plugin-sys = { version = "9.0.0-v0", workspace = true, default-features = false } +thiserror = "1.0.61" +num-traits = { version = "0.2.19", optional = true } [target.'cfg(windows)'.dependencies.libloading] version = "0.8.3" @@ -23,7 +24,7 @@ version = "0.8.3" version = "1.4" [target.'cfg(windows)'.dependencies.windows] -version = "0.52" +version = "0.56.0" features = [ "Win32_System_WindowsProgramming", "Win32_System_LibraryLoader", @@ -31,8 +32,16 @@ features = [ ] [target.'cfg(windows)'.dependencies.libc] -version = "0.2.152" +version = "0.2.155" [features] +default = ["plugin-api-v2"] # Define external symbols with weak definition unix-weak-link = [] +# Use the V1 plugin API, which is defined for versions below 9.0.0 +plugin-api-v1 = ["qemu-plugin-sys/plugin-api-v1"] +# Use the V2 plugin API, which is defined for version 9.0.0 +plugin-api-v2 = ["qemu-plugin-sys/plugin-api-v2"] +# Use the V2 plugin API, which is defined for versions above 9.0.0 +plugin-api-v3 = ["qemu-plugin-sys/plugin-api-v3"] +num-traits = ["dep:num-traits"] diff --git a/qemu-plugin/README.md b/qemu-plugin/README.md index e49d771..44cd25a 100644 --- a/qemu-plugin/README.md +++ b/qemu-plugin/README.md @@ -65,7 +65,7 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] -qemu-plugin = "8.2.2-v0" +qemu-plugin = "9.0.0-v0" anyhow = "1.0.75" ffi = "0.1.0" ctor = "0.2.6" diff --git a/qemu-plugin/src/error/mod.rs b/qemu-plugin/src/error/mod.rs index 4082e83..26f16b5 100644 --- a/qemu-plugin/src/error/mod.rs +++ b/qemu-plugin/src/error/mod.rs @@ -43,6 +43,20 @@ pub enum Error { #[error("No disassembly string available for instruction")] /// Error when no disassembly string is available for an instruction (i.e. NULL string NoDisassemblyString, + #[error("Invalid size {size} for read of register {name}")] + /// Error when the size of a register read is invalid + InvalidRegisterReadSize { + /// The register name + name: String, + /// The size of the attempted read + size: usize, + }, + #[error("Error while reading register {name}")] + /// Error when reading a register fails + RegisterReadError { + /// The register name + name: String, + }, #[error(transparent)] /// A transparently wrapped `std::str::Utf8Error` Utf8Error(#[from] std::str::Utf8Error), diff --git a/qemu-plugin/src/lib.rs b/qemu-plugin/src/lib.rs index 177491a..0430925 100644 --- a/qemu-plugin/src/lib.rs +++ b/qemu-plugin/src/lib.rs @@ -64,7 +64,7 @@ //! crate-type = ["cdylib"] //! //! [dependencies] -//! qemu-plugin = "8.2.2-v0" +//! qemu-plugin = "9.0.0-v0" //! anyhow = "1.0.75" //! ffi = "0.1.0" //! ctor = "0.2.6" @@ -72,6 +72,7 @@ #![deny(missing_docs)] #![cfg_attr(all(unix, feature = "unix-weak-link"), feature(linkage))] +#![cfg_attr(feature = "num-traits", feature(generic_const_exprs))] #[cfg(all(unix, feature = "unix-weak-link"))] mod unix_weak_link; @@ -80,11 +81,18 @@ mod unix_weak_link; mod win_link_hook; use crate::error::{Error, Result}; +#[cfg(feature = "num-traits")] +use num_traits::{FromBytes, PrimInt}; use qemu_plugin_sys::{ qemu_plugin_cb_flags, qemu_plugin_hwaddr, qemu_plugin_id_t, qemu_plugin_insn, - qemu_plugin_mem_rw, qemu_plugin_meminfo_t, qemu_plugin_simple_cb_t, qemu_plugin_tb, - qemu_plugin_vcpu_simple_cb_t, qemu_plugin_vcpu_syscall_cb_t, qemu_plugin_vcpu_syscall_ret_cb_t, - qemu_plugin_vcpu_tb_trans_cb_t, + qemu_plugin_mem_rw, qemu_plugin_meminfo_t, qemu_plugin_op, qemu_plugin_simple_cb_t, + qemu_plugin_tb, qemu_plugin_vcpu_simple_cb_t, qemu_plugin_vcpu_syscall_cb_t, + qemu_plugin_vcpu_syscall_ret_cb_t, qemu_plugin_vcpu_tb_trans_cb_t, +}; +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +use qemu_plugin_sys::{ + qemu_plugin_read_register, qemu_plugin_reg_descriptor, qemu_plugin_register, + qemu_plugin_scoreboard, qemu_plugin_u64, GArray, GByteArray, }; use std::{ ffi::{c_uint, c_void, CStr, CString}, @@ -92,11 +100,17 @@ use std::{ path::PathBuf, sync::{Mutex, OnceLock}, }; +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +use std::{ + fmt::{Debug, Formatter}, + mem::MaybeUninit, +}; pub mod error; pub mod install; pub mod plugin; pub mod sys; +pub mod version; #[cfg(not(windows))] extern "C" { @@ -104,6 +118,19 @@ extern "C" { fn g_free(mem: *mut c_void); } +#[cfg(all( + not(windows), + any(feature = "plugin-api-v2", feature = "plugin-api-v3") +))] +extern "C" { + /// glib g_byte_array_new is provided by the QEMU program we are being linked into + fn g_byte_array_new() -> *mut GByteArray; + /// glib g_byte_array_free is provided by the QEMU program we are being linked into + fn g_byte_array_free(array: *mut GByteArray, free_segment: bool) -> *mut u8; + /// glib g_array_free is provided byt he QEMU program we are being linked into + fn g_array_free(array: *mut GArray, free_segment: bool) -> *mut u8; +} + #[cfg(windows)] lazy_static::lazy_static! { static ref G_FREE : libloading::os::windows::Symbol = { @@ -117,6 +144,45 @@ lazy_static::lazy_static! { }; } +#[cfg(all(windows, any(feature = "plugin-api-v2", feature = "plugin-api-v3")))] +lazy_static::lazy_static! { + static ref G_BYTE_ARRAY_NEW: libloading::os::windows::Symbol *mut GByteArray> = { + let lib = + libloading::os::windows::Library::open_already_loaded("libglib-2.0-0.dll") + .expect("libglib-2.0-0.dll should already be loaded"); + // SAFETY + // "Users of `Library::get` should specify the correct type of the function loaded". + // We are specifying the correct type of g_free above (`void g_free(void*)`) + unsafe{lib.get(b"g_byte_array_new").expect("find g_byte_array_new")} + }; +} + +#[cfg(all(windows, any(feature = "plugin-api-v2", feature = "plugin-api-v3")))] +lazy_static::lazy_static! { + static ref G_BYTE_ARRAY_FREE: libloading::os::windows::Symbol *mut u8> = { + let lib = + libloading::os::windows::Library::open_already_loaded("libglib-2.0-0.dll") + .expect("libglib-2.0-0.dll should already be loaded"); + // SAFETY + // "Users of `Library::get` should specify the correct type of the function loaded". + // We are specifying the correct type of g_free above (`void g_free(void*)`) + unsafe{lib.get(b"g_byte_array_free").expect("find g_byte_array_free")} + }; +} + +#[cfg(all(windows, any(feature = "plugin-api-v2", feature = "plugin-api-v3")))] +lazy_static::lazy_static! { + static ref G_ARRAY_FREE: libloading::os::windows::Symbol *mut u8> = { + let lib = + libloading::os::windows::Library::open_already_loaded("libglib-2.0-0.dll") + .expect("libglib-2.0-0.dll should already be loaded"); + // SAFETY + // "Users of `Library::get` should specify the correct type of the function loaded". + // We are specifying the correct type of g_free above (`void g_free(void*)`) + unsafe{lib.get(b"g_array_free").expect("find g_array_free")} + }; +} + #[cfg(windows)] /// Define g_free, because on Windows we cannot delay link it /// @@ -130,12 +196,56 @@ unsafe fn g_free(mem: *mut c_void) { unsafe { G_FREE(mem) } } +#[cfg(all(windows, any(feature = "plugin-api-v2", feature = "plugin-api-v3")))] +/// Define g_byte_array_new, because on Windows we cannot delay link it +/// +/// # Safety +/// +/// `g_byte_array_new` must be used to allocate a new `GByteArray` which can be used to store +/// arbitrary data. The returned `GByteArray` must be freed with `g_byte_array_free` when it is +/// no longer needed. +unsafe fn g_byte_array_new() -> *mut GByteArray { + unsafe { G_BYTE_ARRAY_NEW() } +} + +#[cfg(all(windows, any(feature = "plugin-api-v2", feature = "plugin-api-v3")))] +/// Define g_byte_array_free, because on Windows we cannot delay link it +/// +/// # Safety +/// +/// `g_byte_array_free` must be used to free a `GByteArray` allocated with `g_byte_array_new`. +/// The `free_segment` argument should be `true` if the data stored in the `GByteArray` should +/// also be freed. If `false`, the data will not be freed, and the caller is responsible for +/// freeing it with `g_free`. +unsafe fn g_byte_array_free(array: *mut GByteArray, free_segment: bool) -> *mut u8 { + unsafe { G_BYTE_ARRAY_FREE(array as *mut c_void, free_segment) } +} + +#[cfg(all(windows, any(feature = "plugin-api-v2", feature = "plugin-api-v3")))] +/// Define g_array_free, because on Windows we cannot delay link it +/// +/// # Safety +/// +/// `g_array_free` must be used to free a `GArray` allocated with `g_array_new`. The `free_segment` +/// argument should be `true` if the data stored in the `GArray` should also be freed. If `false`, +/// the data will not be freed, and the caller is responsible for freeing it with `g_free`. +unsafe fn g_array_free(array: *mut GArray, free_segment: bool) -> *mut u8 { + unsafe { G_ARRAY_FREE(array as *mut c_void, free_segment) } +} + /// The index of a vCPU pub type VCPUIndex = c_uint; +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +/// u64 member of an entry in a scoreboard, allows access to a specific u64 member in +/// one given entry, located at a specified offset. Inline operations expect this as an +/// entry. +pub type QemuPluginU64 = qemu_plugin_u64; /// Flags for callbacks pub type CallbackFlags = qemu_plugin_cb_flags; /// Memory read/write flags pub type MemRW = qemu_plugin_mem_rw; +/// Plugin operations for inline operations +pub type QemuPluginOp = qemu_plugin_op; /// A plugin ID pub type PluginId = qemu_plugin_id_t; @@ -276,6 +386,14 @@ impl<'a> TranslationBlock<'a> { /// Register a callback to be run on execution of this translation block pub fn register_execute_callback(&self, cb: F) + where + F: FnMut(VCPUIndex) + Send + Sync + 'static, + { + self.register_execute_callback_flags(cb, CallbackFlags::QEMU_PLUGIN_CB_NO_REGS); + } + + /// Register a callback to be run on execution of this translation block + pub fn register_execute_callback_flags(&self, cb: F, flags: CallbackFlags) where F: FnMut(VCPUIndex) + Send + Sync + 'static, { @@ -287,8 +405,7 @@ impl<'a> TranslationBlock<'a> { crate::sys::qemu_plugin_register_vcpu_tb_exec_cb( self.translation_block as *mut qemu_plugin_tb, Some(handle_qemu_plugin_register_vcpu_tb_exec_cb::), - // NOTE: Not checked, and is an error to specify any other value - CallbackFlags::QEMU_PLUGIN_CB_NO_REGS, + flags, userdata, ) }; @@ -342,9 +459,11 @@ impl<'a> Instruction<'a> { } impl<'a> Instruction<'a> { + #[cfg(any(feature = "plugin-api-v1", feature = "plugin-api-v2"))] /// Returns the data for this instruction. This method may only be called inside the /// callback in which the instruction is obtained, but the resulting data is owned. pub fn data(&self) -> Vec { + println!("data 1"); let size = self.size(); let mut data = Vec::with_capacity(size); @@ -360,6 +479,26 @@ impl<'a> Instruction<'a> { data } + #[cfg(feature = "plugin-api-v3")] + /// Returns the data for this instruction. This method may only be called inside the + /// callback in which the instruction is obtained, but the resulting data is owned. + pub fn data(&self) -> Vec { + let size = self.size(); + let mut data = vec![0; size]; + + let size = unsafe { + crate::sys::qemu_plugin_insn_data( + self.instruction as *mut qemu_plugin_insn, + data.as_mut_ptr() as *mut _, + data.len(), + ) + }; + + data.truncate(size); + + data + } + /// Returns the size of the data for this instruction pub fn size(&self) -> usize { unsafe { crate::sys::qemu_plugin_insn_size(self.instruction as *mut qemu_plugin_insn) } @@ -410,6 +549,14 @@ impl<'a> Instruction<'a> { /// Register a callback to be run on execution of this instruction pub fn register_execute_callback(&self, cb: F) + where + F: FnMut(VCPUIndex) + Send + Sync + 'static, + { + self.register_execute_callback_flags(cb, CallbackFlags::QEMU_PLUGIN_CB_NO_REGS) + } + + /// Register a callback to be run on execution of this instruction + pub fn register_execute_callback_flags(&self, cb: F, flags: CallbackFlags) where F: FnMut(VCPUIndex) + Send + Sync + 'static, { @@ -421,8 +568,7 @@ impl<'a> Instruction<'a> { crate::sys::qemu_plugin_register_vcpu_insn_exec_cb( self.instruction as *mut qemu_plugin_insn, Some(handle_qemu_plugin_register_vcpu_insn_exec_cb::), - // NOTE: Not checked, and is an error to specify any other value - CallbackFlags::QEMU_PLUGIN_CB_NO_REGS, + flags, userdata, ) }; @@ -435,6 +581,19 @@ impl<'a> Instruction<'a> { /// - `cb`: The callback to be run /// - `rw`: The type of memory access to trigger the callback on pub fn register_memory_access_callback(&self, cb: F, rw: MemRW) + where + F: FnMut(VCPUIndex, MemoryInfo, u64) + Send + Sync + 'static, + { + self.register_memory_access_callback_flags(cb, rw, CallbackFlags::QEMU_PLUGIN_CB_NO_REGS) + } + + /// Register a callback to be run on memory access of this instruction + /// + /// # Arguments + /// + /// - `cb`: The callback to be run + /// - `rw`: The type of memory access to trigger the callback on + pub fn register_memory_access_callback_flags(&self, cb: F, rw: MemRW, flags: CallbackFlags) where F: FnMut(VCPUIndex, MemoryInfo, u64) + Send + Sync + 'static, { @@ -446,7 +605,7 @@ impl<'a> Instruction<'a> { crate::sys::qemu_plugin_register_vcpu_mem_cb( self.instruction as *mut qemu_plugin_insn, Some(handle_qemu_plugin_register_vcpu_mem_cb::), - CallbackFlags::QEMU_PLUGIN_CB_NO_REGS, + flags, rw, userdata, ) @@ -508,6 +667,132 @@ impl<'a> MemoryInfo<'a> { } } +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +#[derive(Clone)] +/// Wrapper structure for a `qemu_plugin_register_descriptor` +/// +/// # Safety +/// +/// This structure is safe to use as long as the pointer is valid. The pointer is +/// always opaque, and therefore may not be dereferenced. +pub struct RegisterDescriptor<'a> { + /// Opaque handle to the register for retrieving the value with + /// qemu_plugin_read_register + handle: usize, + /// The register name + pub name: String, + /// Optional feature descriptor + pub feature: Option, + marker: PhantomData<&'a ()>, +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +impl<'a> From for RegisterDescriptor<'a> { + fn from(descriptor: qemu_plugin_reg_descriptor) -> Self { + let name = unsafe { CStr::from_ptr(descriptor.name) } + .to_str() + .expect("Register name is not valid UTF-8") + .to_string(); + + let feature = if descriptor.feature.is_null() { + None + } else { + Some( + unsafe { CStr::from_ptr(descriptor.feature) } + .to_str() + .expect("Register feature is not valid UTF-8") + .to_string(), + ) + }; + + Self { + handle: descriptor.handle as usize, + name, + feature, + marker: PhantomData, + } + } +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +impl<'a> Debug for RegisterDescriptor<'a> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.debug_struct("RegisterDescriptor") + .field("name", &self.name) + .field("feature", &self.feature) + .finish() + } +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +impl<'a> RegisterDescriptor<'a> { + /// Read a register value + /// + /// This must only be called in a callback which has been registered with + /// `CallbackFlags::QEMU_PLUGIN_CB_R_REGS` or + /// `CallbackFlags::QEMU_PLUGIN_CB_RW_REGS`. + pub fn read(&self) -> Result> { + let byte_array = unsafe { g_byte_array_new() }; + + let result = unsafe { + qemu_plugin_read_register(self.handle as *mut qemu_plugin_register, byte_array) + }; + + if result == -1 { + return Err(Error::RegisterReadError { + name: self.name.clone(), + }); + } + + let mut data = Vec::new(); + data.extend_from_slice(unsafe { + std::slice::from_raw_parts((*byte_array).data, (*byte_array).len as usize) + }); + + assert_eq!( + unsafe { g_byte_array_free(byte_array, true) }, + std::ptr::null_mut(), + "g_byte_array_free must return NULL" + ); + + Ok(data) + } + + #[cfg(feature = "num-traits")] + /// Read a register value into a numeric type in big-endian byte order + /// + /// This must only be called in a callback which has been registered with + /// `CallbackFlags::QEMU_PLUGIN_CB_R_REGS` or + /// `CallbackFlags::QEMU_PLUGIN_CB_RW_REGS`. + pub fn read_be(&self) -> Result + where + T: PrimInt + FromBytes + Sized, + T: FromBytes()]>, + { + let data = self.read()?; + let mut bytes = [0; std::mem::size_of::()]; + bytes.copy_from_slice(&data); + Ok(T::from_be_bytes(&bytes)) + } + + #[cfg(feature = "num-traits")] + /// Read a register value into a numeric type in little-endian byte order + /// + /// This must only be called in a callback which has been registered with + /// `CallbackFlags::QEMU_PLUGIN_CB_R_REGS` or + /// `CallbackFlags::QEMU_PLUGIN_CB_RW_REGS`. + pub fn read_le(&self) -> Result + where + T: PrimInt + FromBytes + Sized, + T: FromBytes()]>, + { + let data = self.read()?; + let mut bytes = [0; std::mem::size_of::()]; + bytes.copy_from_slice(&data); + Ok(T::from_le_bytes(&bytes)) + } +} + /// Wrapper structure for a `qemu_plugin_hwaddr *` /// /// # Safety @@ -556,6 +841,62 @@ impl<'a> HwAddr<'a> { } } +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +/// A wrapper structure for a `qemu_plugin_scoreboard *`. This is a way of having one +/// entry per VCPU, the count of which is managed automatically by QEMU. Keep in mind +/// that additional entries *and* existing entries will be allocated and reallocated by +/// *qemu*, not by the plugin, so every use of a `T` should include a check for whether +/// it is initialized. +pub struct Scoreboard<'a, T> +where + T: Sized, +{ + handle: usize, + marker: PhantomData<&'a T>, +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +impl<'a, T> Scoreboard<'a, T> { + /// Allocate a new scoreboard object. This must be freed by calling + /// `qemu_plugin_scoreboard_free` (or by being dropped). + pub fn new() -> Self { + let handle = + unsafe { crate::sys::qemu_plugin_scoreboard_new(std::mem::size_of::()) as usize }; + + Self { + handle, + marker: PhantomData, + } + } + + /// Returns a reference to entry of a scoreboard matching a given vcpu index. This address + /// is only valid until the next call to `get` or `set`. + pub fn find<'b>(&mut self, vcpu_index: VCPUIndex) -> &'b mut MaybeUninit { + unsafe { + &mut *(crate::sys::qemu_plugin_scoreboard_find( + self.handle as *mut qemu_plugin_scoreboard, + vcpu_index, + ) as *mut MaybeUninit) + } + } +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +impl<'a, T> Default for Scoreboard<'a, T> { + fn default() -> Self { + Self::new() + } +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +impl<'a, T> Drop for Scoreboard<'a, T> { + fn drop(&mut self) { + unsafe { + crate::sys::qemu_plugin_scoreboard_free(self.handle as *mut qemu_plugin_scoreboard) + } + } +} + // NOTE: Box( vcpu_index: VCPUIndex, userdata: *mut c_void, @@ -782,6 +1175,58 @@ where insn.register_execute_callback(cb); } +#[cfg(feature = "plugin-api-v1")] +#[allow(clippy::not_unsafe_ptr_arg_deref)] +/// Register an inline callback to be called when an instruction is executed. +/// +/// # Arguments +/// +/// - `insn`: The instruction handle to register the callback for +/// - `op`: The operation to be performed +/// - `ptr`: The pointer to the data to be passed to the operation +/// - `imm`: The immediate value to be passed to the operation +pub fn qemu_plugin_register_vcpu_insn_exec_inline( + insn: Instruction, + op: QemuPluginOp, + ptr: *mut c_void, + imm: u64, +) { + unsafe { + crate::sys::qemu_plugin_register_vcpu_insn_exec_inline( + insn.instruction as *mut qemu_plugin_insn, + op, + ptr, + imm, + ); + } +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +#[allow(clippy::not_unsafe_ptr_arg_deref)] +/// Register an inline callback to be called when an instruction is executed. +/// +/// # Arguments +/// +/// - `insn`: The instruction handle to register the callback for +/// - `op`: The operation to be performed +/// - `entry`: The entry to be passed to the operation +/// - `imm`: The immediate value to be passed to the operation +pub fn qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu( + insn: Instruction, + op: QemuPluginOp, + entry: QemuPluginU64, + imm: u64, +) { + unsafe { + crate::sys::qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu( + insn.instruction as *mut qemu_plugin_insn, + op, + entry, + imm, + ); + } +} + extern "C" fn handle_qemu_plugin_register_vcpu_mem_cb( vcpu_index: VCPUIndex, meminfo: qemu_plugin_meminfo_t, @@ -812,6 +1257,64 @@ where insn.register_memory_access_callback(cb, rw); } +#[cfg(feature = "plugin-api-v1")] +#[allow(clippy::not_unsafe_ptr_arg_deref)] +/// Register an inline callback for every memory transaction of a particular instruction. +/// +/// # Arguments +/// +/// - `insn`: The instruction handle to register the callback for +/// - `rw`: Whether the callback should be called for reads, writes, or both +/// - `op`: The operation to be performed +/// - `ptr`: The pointer to the data to be passed to the operation +/// - `imm`: The immediate value to be passed to the operation +pub fn qemu_plugin_register_vcpu_mem_inline( + insn: Instruction, + rw: MemRW, + op: QemuPluginOp, + ptr: *mut c_void, + imm: u64, +) { + unsafe { + crate::sys::qemu_plugin_register_vcpu_mem_inline( + insn.instruction as *mut qemu_plugin_insn, + rw, + op, + ptr, + imm, + ); + } +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +#[allow(clippy::not_unsafe_ptr_arg_deref)] +/// Register an inline callback for every memory transaction of a particular instruction. +/// +/// # Arguments +/// +/// - `insn`: The instruction handle to register the callback for +/// - `rw`: Whether the callback should be called for reads, writes, or both +/// - `op`: The operation to be performed +/// - `entry`: The entry to be passed to the operation +/// - `imm`: The immediate value to be passed to the operation +pub fn qemu_plugin_register_vcpu_mem_inline_per_vcpu( + insn: Instruction, + rw: MemRW, + op: QemuPluginOp, + entry: QemuPluginU64, + imm: u64, +) { + unsafe { + crate::sys::qemu_plugin_register_vcpu_mem_inline_per_vcpu( + insn.instruction as *mut qemu_plugin_insn, + rw, + op, + entry, + imm, + ); + } +} + extern "C" fn handle_qemu_plugin_register_atexit_cb(id: qemu_plugin_id_t, userdata: *mut c_void) where F: FnOnce(qemu_plugin_id_t) + Send + Sync + 'static, @@ -894,7 +1397,7 @@ where /// # Arguments /// /// - `name`: argument name, the part before the equals sign @val: argument value, what’s -/// after the equals sign @ret: output return value +/// after the equals sign @ret: output return value /// - `val`: Argument value, what’s after the equals sign /// pub fn qemu_plugin_bool_parse(name: S, val: S) -> Result @@ -974,20 +1477,22 @@ pub fn qemu_plugin_entry_code() -> Option { } } -/// Return the maximum number of vCPUs, if running in system mode -pub fn qemu_plugin_n_max_cpus() -> Option { - let max_cpus = unsafe { crate::sys::qemu_plugin_n_max_vcpus() }; +#[cfg(feature = "plugin-api-v1")] +/// Return the number of vCPUs, if running in system mode +pub fn qemu_plugin_n_vcpus() -> Option { + let vcpus = unsafe { crate::sys::qemu_plugin_n_vcpus() }; - if max_cpus == -1 { + if vcpus == -1 { None } else { - Some(max_cpus) + Some(vcpus) } } +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] /// Return the number of vCPUs, if running in system mode -pub fn qemu_plugin_n_vcpus() -> Option { - let vcpus = unsafe { crate::sys::qemu_plugin_n_vcpus() }; +pub fn qemu_plugin_num_vcpus() -> Option { + let vcpus = unsafe { crate::sys::qemu_plugin_num_vcpus() }; if vcpus == -1 { None @@ -995,3 +1500,68 @@ pub fn qemu_plugin_n_vcpus() -> Option { Some(vcpus) } } + +#[cfg(feature = "plugin-api-v1")] +/// Return the maximum number of vCPUs, if running in system mode +pub fn qemu_plugin_n_max_vcpus() -> Option { + let max_cpus = unsafe { crate::sys::qemu_plugin_n_max_vcpus() }; + + if max_cpus == -1 { + None + } else { + Some(max_cpus) + } +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +/// Returns a potentially empty list of registers. This should be used from a +/// qemu_plugin_register_vcpu_init_cb callback after the vcpu has been initialized. +pub fn qemu_plugin_get_registers<'a>() -> Result>> { + use std::slice::from_raw_parts; + + let array = unsafe { crate::sys::qemu_plugin_get_registers() }; + + let registers = unsafe { + from_raw_parts( + (*array).data as *mut qemu_plugin_reg_descriptor, + (*array).len as usize, + ) + } + .iter() + .map(|desc| RegisterDescriptor::from(*desc)) + .collect::>(); + + // Function notes say caller frees the array but not the strings in each entry + assert_eq!( + unsafe { g_array_free(array, true) }, + std::ptr::null_mut(), + "g_array_free return value must be NULL" + ); + + Ok(registers) +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +/// Add a value to a `QemuPluginU64` for a given VCPU +pub fn qemu_plugin_u64_add(entry: QemuPluginU64, vcpu_index: VCPUIndex, added: u64) -> Result<()> { + unsafe { crate::sys::qemu_plugin_u64_add(entry, vcpu_index, added) }; + Ok(()) +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +/// Get the value of a `QemuPluginU64` for a given VCPU +pub fn qemu_plugin_u64_get(entry: QemuPluginU64, vcpu_index: VCPUIndex) -> u64 { + unsafe { crate::sys::qemu_plugin_u64_get(entry, vcpu_index) } +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +/// Set the value of a `QemuPluginU64` for a given VCPU +pub fn qemu_plugin_u64_set(entry: QemuPluginU64, vcpu_index: VCPUIndex, value: u64) { + unsafe { crate::sys::qemu_plugin_u64_set(entry, vcpu_index, value) } +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +/// Get the sum of all VCPU entries in a scoreboard +pub fn qemu_plugin_scoreboard_sum(entry: QemuPluginU64) -> u64 { + unsafe { crate::sys::qemu_plugin_u64_sum(entry) } +} diff --git a/qemu-plugin/src/unix_weak_link/mod.rs b/qemu-plugin/src/unix_weak_link/mod.rs index a40a2ff..b206997 100644 --- a/qemu-plugin/src/unix_weak_link/mod.rs +++ b/qemu-plugin/src/unix_weak_link/mod.rs @@ -62,6 +62,7 @@ pub extern "C" fn qemu_plugin_register_vcpu_tb_exec_cb( ) { } +#[cfg(feature = "plugin-api-v1")] #[no_mangle] #[linkage = "weak"] pub extern "C" fn qemu_plugin_register_vcpu_tb_exec_inline( @@ -72,6 +73,17 @@ pub extern "C" fn qemu_plugin_register_vcpu_tb_exec_inline( ) { } +#[cfg(feature = "plugin-api-v2")] +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu( + _: *mut qemu_plugin_tb, + _: qemu_plugin_op, + _: *mut ::std::os::raw::c_void, + _: u64, +) { +} + #[no_mangle] #[linkage = "weak"] pub extern "C" fn qemu_plugin_register_vcpu_insn_exec_cb( @@ -82,6 +94,7 @@ pub extern "C" fn qemu_plugin_register_vcpu_insn_exec_cb( ) { } +#[cfg(feature = "plugin-api-v1")] #[no_mangle] #[linkage = "weak"] pub extern "C" fn qemu_plugin_register_vcpu_insn_exec_inline( @@ -92,6 +105,17 @@ pub extern "C" fn qemu_plugin_register_vcpu_insn_exec_inline( ) { } +#[cfg(feature = "plugin-api-v2")] +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu( + _: *mut qemu_plugin_insn, + _: qemu_plugin_op, + _: *mut ::std::os::raw::c_void, + _: u64, +) { +} + #[no_mangle] #[linkage = "weak"] pub extern "C" fn qemu_plugin_tb_n_insns(_: *const qemu_plugin_tb) -> usize { @@ -205,6 +229,7 @@ pub extern "C" fn qemu_plugin_register_vcpu_mem_cb( ) { } +#[cfg(feature = "plugin-api-v1")] #[no_mangle] #[linkage = "weak"] pub extern "C" fn qemu_plugin_register_vcpu_mem_inline( @@ -216,6 +241,18 @@ pub extern "C" fn qemu_plugin_register_vcpu_mem_inline( ) { } +#[cfg(feature = "plugin-api-v2")] +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn qemu_plugin_register_vcpu_mem_inline_per_vcpu( + _: *mut qemu_plugin_insn, + _: qemu_plugin_mem_rw, + _: qemu_plugin_op, + _: *mut ::std::os::raw::c_void, + _: u64, +) { +} + #[no_mangle] #[linkage = "weak"] pub extern "C" fn qemu_plugin_register_vcpu_syscall_cb( @@ -265,12 +302,21 @@ pub extern "C" fn qemu_plugin_register_atexit_cb( ) { } +#[cfg(feature = "plugin-api-v1")] #[no_mangle] #[linkage = "weak"] pub extern "C" fn qemu_plugin_n_vcpus() -> ::std::os::raw::c_int { 0 } +#[cfg(feature = "plugin-api-v2")] +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn qemu_plugin_num_vcpus() -> ::std::os::raw::c_int { + 0 +} + +#[cfg(feature = "plugin-api-v1")] #[no_mangle] #[linkage = "weak"] pub extern "C" fn qemu_plugin_n_max_vcpus() -> ::std::os::raw::c_int { @@ -315,6 +361,85 @@ pub extern "C" fn qemu_plugin_entry_code() -> u64 { 0 } +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn qemu_plugin_get_registers() -> *mut GArray { + null_mut() +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn qemu_plugin_read_register( + _: *mut qemu_plugin_register, + _: *mut GByteArray, +) -> ::std::os::raw::c_int { + 0 +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn qemu_plugin_scoreboard_new(_: usize) -> *mut qemu_plugin_scoreboard { + null_mut() +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn qemu_plugin_scoreboard_free(_: *mut qemu_plugin_scoreboard) {} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn qemu_plugin_scoreboard_find( + _: *mut qemu_plugin_scoreboard, + _: ::std::os::raw::c_uint, +) -> *mut ::std::os::raw::c_void { + null_mut() +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn qemu_plugin_u64_add(_: qemu_plugin_u64, _: ::std::os::raw::c_uint, _: u64) {} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn qemu_plugin_u64_get(_: qemu_plugin_u64, _: ::std::os::raw::c_uint) -> u64 { + 0 +} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn qemu_plugin_u64_set(_: qemu_plugin_u64, _: ::std::os::raw::c_uint, _: u64) {} + +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn qemu_plugin_u64_sum(_: qemu_plugin_u64) {} + #[no_mangle] #[linkage = "weak"] pub extern "C" fn g_free(_: *mut ::std::ffi::c_void) {} + +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn g_byte_array_new() -> *mut GByteArray { + null_mut() +} + +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn g_byte_array_free(_: *mut GByteArray, _: bool) -> *mut u8 { + null_mut() +} + +#[no_mangle] +#[linkage = "weak"] +pub extern "C" fn g_array_free(_: *mut GArray, _: bool) -> *mut u8 { + null_mut() +} diff --git a/qemu-plugin/src/version/mod.rs b/qemu-plugin/src/version/mod.rs new file mode 100644 index 0000000..a967360 --- /dev/null +++ b/qemu-plugin/src/version/mod.rs @@ -0,0 +1,7 @@ +//! Tools for checking whether the plugin version and the +//! QEMU version are compatible + +/// A mapping of the QEMU plugin version (given in sys::QEMU_PLUGIN_VERSION) to the last +/// QEMU version which supports that plugin version ( or "latest" if it is supported by +/// the latest version of QEMU) +pub const COMPABILITY_MAP: [(u8, &str); 3] = [(1, "8.2.3"), (2, "9.0.0"), (3, "latest")]; diff --git a/qemu/Cargo.toml b/qemu/Cargo.toml index 02366a4..502d4d9 100644 --- a/qemu/Cargo.toml +++ b/qemu/Cargo.toml @@ -279,10 +279,10 @@ required-features = ["binaries", "xtensaeb-softmmu"] memfd-exec = { version = "0.2.1", optional = true } [build-dependencies] -anyhow = "1.0.75" -command-ext = "0.1.1" -num_cpus = "1.13.0" -reqwest = { version = "0.11.23", default-features = false, features = [ +anyhow = "1.0.86" +command-ext = "0.1.2" +num_cpus = "1.16.0" +reqwest = { version = "0.12.4", default-features = false, features = [ "rustls-tls", "blocking", ] } diff --git a/qemu/README.md b/qemu/README.md index c8c880e..1d6177f 100644 --- a/qemu/README.md +++ b/qemu/README.md @@ -78,7 +78,7 @@ license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] memfd-exec = "2.1.0" -qemu = { version = "8.2.2", features = ["qemu-aarch64"] } +qemu = { version = "9.0.0", features = ["qemu-aarch64"] } ``` ```rust @@ -115,7 +115,7 @@ to configure this crate as a dependency: This will make the `qemu-x86_64` binary available. ```toml -qemu = { version = "8.2.2", features = ["qemu-x86_64"] } +qemu = { version = "9.0.0", features = ["qemu-x86_64"] } ``` ### Install an optimized qemu-x86_64 usermode emulator @@ -124,7 +124,7 @@ This will also make the `qemu-x86_64` binary available, but will strip and optim with `lto`. ```toml -qemu = { version = "8.2.2", features = ["qemu-x86_64", "lto", "strip"] +qemu = { version = "9.0.0", features = ["qemu-x86_64", "lto", "strip"] ``` ### Install qemu-system-arm emulator with customized options diff --git a/qemu/build.rs b/qemu/build.rs index 3bba494..419b859 100644 --- a/qemu/build.rs +++ b/qemu/build.rs @@ -14,7 +14,7 @@ use tar::Archive; use xz2::read::XzDecoder; const QEMU_SRC_URL_BASE: &str = "https://download.qemu.org/"; -const QEMU_VERSION: &str = "8.2.2"; +const QEMU_VERSION: &str = "9.0.0"; pub struct ConfigureArgs(Vec); diff --git a/qemu/src/lib.rs b/qemu/src/lib.rs index c81df7d..92e1d36 100644 --- a/qemu/src/lib.rs +++ b/qemu/src/lib.rs @@ -23,7 +23,7 @@ //! //! To install with binaries, `cargo install qemu --features=binaries,plugins,lto` -pub const QEMU_VERSION: &str = "8.2.2"; +pub const QEMU_VERSION: &str = "9.0.0"; #[cfg(all(feature = "aarch64-linux-user", not(docs_rs)))] /// QEMU binary for qemu-aarch64 From edb83c56523727eb34c27b6e229bf42ebef54209 Mon Sep 17 00:00:00 2001 From: novafacing Date: Sun, 2 Jun 2024 16:55:55 -0700 Subject: [PATCH 2/7] Fixup plugin versions for each test platform --- .github/workflows/ci.yml | 87 +++++++++++++++++++++++++++++++--- plugins/tiny-system/Cargo.toml | 10 +++- plugins/tiny/Cargo.toml | 10 +++- 3 files changed, 98 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 30da6ca..f246d68 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: test_plugins_linux: name: Build and Test Plugins (Linux) runs-on: ubuntu-latest - container: ubuntu:22.04 + container: ubuntu:24.04 steps: - name: Install QEMU User run: | @@ -27,14 +27,87 @@ jobs: - name: Build and Test Tracer run: | cd plugins/tracer - cargo build -r || exit 0 - cargo build -r - cargo run -r --bin tracer -- -a /bin/ls -- -lah + cargo build -r --features=plugin-api-v1 --no-default-features || exit 0 + cargo build -r --features=plugin-api-v1 --no-default-features + cargo run --features=plugin-api-v1 --no-default-features -r --bin tracer -- -a /bin/ls -- -lah cd ../.. - name: Build and Test Tiny run: | cd plugins/tiny - cargo build -r + cargo build -r --features=plugin-api-v1 --no-default-features + qemu-x86_64 -plugin ../../target/release/libtiny.so /bin/ls -lah + cd ../.. + + test_plugins_linux_bleeding: + name: Build and Test Plugins (Linux, Bleeding) + runs-on: ubuntu-latest + container: ubuntu:24.04 + steps: + - name: Set up Sources List + run: | + cat < /etc/apt/sources.list.d/ubuntu.sources + Types: deb + URIs: http://archive.ubuntu.com/ubuntu/ + Suites: noble noble-updates noble-backports + Components: main universe restricted multiverse + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + + Types: deb + URIs: http://security.ubuntu.com/ubuntu/ + Suites: noble-security + Components: main universe restricted multiverse + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + + Types: deb-src + URIs: http://archive.ubuntu.com/ubuntu/ + Suites: noble noble-updates noble-backports + Components: main universe restricted multiverse + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + + Types: deb + URIs: http://security.ubuntu.com/ubuntu/ + Suites: noble-security + Components: main universe restricted multiverse + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + EOF + - name: Install QEMU Build Dependencies + run: | + apt -y update && \ + apt -y install git curl build-essential && \ + apt -y source qemu && \ + apt -y build-dep qemu + + # Clone without history + - name: Clone QEMU + run: | + git clone --depth 1 https://github.com/qemu/qemu qemu-upstream + + - name: Build QEMU + run: | + pushd qemu-upstream + ./configure --enable-plugins + pushd build + make -j$(nproc) + make install + popd + popd + + - uses: dtolnay/rust-toolchain@nightly + - uses: actions/checkout@v4 + - name: Test QEMU Install + run: | + qemu-x86_64 --help + - name: Build and Test Tracer + run: | + cd plugins/tracer + cargo build -r --features=plugin-api-v3 --no-default-features || exit 0 + cargo build -r --features=plugin-api-v3 --no-default-features + cargo run --features=plugin-api-v3 --no-default-features -r --bin tracer -- -a /bin/ls -- -lah + cd ../.. + - name: Build and Test Tiny + run: | + cd plugins/tiny + cargo build -r --features=plugin-api-v3 --no-default-features --no-default-features qemu-x86_64 -plugin ../../target/release/libtiny.so /bin/ls -lah cd ../.. @@ -42,7 +115,7 @@ jobs: name: Build and Test Plugins (Windows) runs-on: windows-latest env: - # QEMU 8.2.0 + # QEMU 9.0.0 # NOTE: This installer does not work headless # QEMU_URL: "https://qemu.weilnetz.de/w64/2023/qemu-w64-setup-20231224.exe" RUSTUP_URL: "https://win.rustup.rs/x86_64" @@ -83,7 +156,7 @@ jobs: - name: Build and Test Tiny run: | cd plugins/tiny-system - cargo build -r + cargo build -r --features=plugin-api-v3 --no-default-features cd ../.. $process = Start-Process PowerShell.exe -NoNewWindow -RedirectStandardOutput out.txt -RedirectStandardError err.txt -PassThru -ArgumentList "-Command", "C:\msys-custom\msys64\ucrt64\bin\qemu-system-x86_64.exe -machine type=q35 -m 2G -nographic -device virtio-net-pci,netdev=net0 -netdev user,id=net0,hostfwd=tcp::2222-:22 -drive if=virtio,format=qcow2,file=Fedora-Cloud-Base-39-1.5.x86_64.qcow2 -drive if=virtio,format=raw,file=.github/rsrc/seed.img -plugin target/release/tiny_system.dll" echo "Sleeping 180.0 seconds until booted (boot process took 118s first time)" diff --git a/plugins/tiny-system/Cargo.toml b/plugins/tiny-system/Cargo.toml index 47d3942..6cd466c 100644 --- a/plugins/tiny-system/Cargo.toml +++ b/plugins/tiny-system/Cargo.toml @@ -7,7 +7,15 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] -qemu-plugin.workspace = true +qemu-plugin = { workspace = true, features = [ + "unix-weak-link", +], default-features = false } anyhow = "1.0.75" ffi = "0.1.0" ctor = "0.2.6" + +[features] +default = ["plugin-api-v2"] +plugin-api-v1 = ["qemu-plugin/plugin-api-v1"] +plugin-api-v2 = ["qemu-plugin/plugin-api-v2"] +plugin-api-v3 = ["qemu-plugin/plugin-api-v3"] diff --git a/plugins/tiny/Cargo.toml b/plugins/tiny/Cargo.toml index ed3a4f4..d9d92e8 100644 --- a/plugins/tiny/Cargo.toml +++ b/plugins/tiny/Cargo.toml @@ -7,7 +7,15 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] -qemu-plugin.workspace = true +qemu-plugin = { workspace = true, features = [ + "unix-weak-link", +], default-features = false } anyhow = "1.0.75" ffi = "0.1.0" ctor = "0.2.6" + +[features] +default = ["plugin-api-v2"] +plugin-api-v1 = ["qemu-plugin/plugin-api-v1"] +plugin-api-v2 = ["qemu-plugin/plugin-api-v2"] +plugin-api-v3 = ["qemu-plugin/plugin-api-v3"] From f6e4c5a49bcd81f4ae96663cfd7660e81a72b236 Mon Sep 17 00:00:00 2001 From: novafacing Date: Sun, 2 Jun 2024 18:11:51 -0700 Subject: [PATCH 3/7] Do not use pushd and fix versioning for tiny plugin --- .github/workflows/ci.yml | 7 +++---- plugins/tiny/src/lib.rs | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f246d68..aff9d68 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -84,13 +84,12 @@ jobs: - name: Build QEMU run: | - pushd qemu-upstream + cd qemu-upstream ./configure --enable-plugins - pushd build + cd build make -j$(nproc) make install - popd - popd + cd ../.. - uses: dtolnay/rust-toolchain@nightly - uses: actions/checkout@v4 diff --git a/plugins/tiny/src/lib.rs b/plugins/tiny/src/lib.rs index 2f9307d..5d01dce 100644 --- a/plugins/tiny/src/lib.rs +++ b/plugins/tiny/src/lib.rs @@ -2,13 +2,15 @@ use anyhow::{anyhow, Result}; use ctor::ctor; use qemu_plugin::{ plugin::{HasCallbacks, Plugin, Register, PLUGIN}, - PluginId, TranslationBlock, VCPUIndex, qemu_plugin_get_registers, - RegisterDescriptor, + PluginId, TranslationBlock, }; +#[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] +use qemu_plugin::{qemu_plugin_get_registers, RegisterDescriptor, VCPUIndex}; use std::sync::Mutex; #[derive(Default)] struct TinyTrace { + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] registers: Vec>, } @@ -16,6 +18,7 @@ impl Plugin for TinyTrace {} impl Register for TinyTrace {} impl HasCallbacks for TinyTrace { + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] fn on_vcpu_init(&mut self, _id: PluginId, _vcpu_id: VCPUIndex) -> Result<()> { self.registers = qemu_plugin_get_registers()?; Ok(()) @@ -25,12 +28,18 @@ impl HasCallbacks for TinyTrace { _id: PluginId, tb: TranslationBlock, ) -> Result<()> { + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] let registers = self.registers.clone(); + tb.instructions().try_for_each(|insn| { println!("{:08x}: {}", insn.vaddr(), insn.disas()?); - for register in ®isters { - let value = register.read()?; - println!(" {}: {:?}", register.name, value); + + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] + { + for register in ®isters { + let value = register.read()?; + println!(" {}: {:?}", register.name, value); + } } Ok(()) @@ -38,7 +47,6 @@ impl HasCallbacks for TinyTrace { } } - #[ctor] fn init() { PLUGIN From 06645b9de25812b5241f890eda98396ef9fa9ee4 Mon Sep 17 00:00:00 2001 From: novafacing Date: Sun, 2 Jun 2024 21:01:47 -0700 Subject: [PATCH 4/7] Remove test for default qemu because of missing plugin option, fix typo --- .github/workflows/ci.yml | 30 +----------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aff9d68..21f346a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,34 +14,6 @@ jobs: name: Build and Test Plugins (Linux) runs-on: ubuntu-latest container: ubuntu:24.04 - steps: - - name: Install QEMU User - run: | - apt-get -y update && \ - apt-get -y install git curl qemu-user build-essential - - uses: dtolnay/rust-toolchain@nightly - - uses: actions/checkout@v4 - - name: Test QEMU Install - run: | - qemu-x86_64 --help - - name: Build and Test Tracer - run: | - cd plugins/tracer - cargo build -r --features=plugin-api-v1 --no-default-features || exit 0 - cargo build -r --features=plugin-api-v1 --no-default-features - cargo run --features=plugin-api-v1 --no-default-features -r --bin tracer -- -a /bin/ls -- -lah - cd ../.. - - name: Build and Test Tiny - run: | - cd plugins/tiny - cargo build -r --features=plugin-api-v1 --no-default-features - qemu-x86_64 -plugin ../../target/release/libtiny.so /bin/ls -lah - cd ../.. - - test_plugins_linux_bleeding: - name: Build and Test Plugins (Linux, Bleeding) - runs-on: ubuntu-latest - container: ubuntu:24.04 steps: - name: Set up Sources List run: | @@ -106,7 +78,7 @@ jobs: - name: Build and Test Tiny run: | cd plugins/tiny - cargo build -r --features=plugin-api-v3 --no-default-features --no-default-features + cargo build -r --features=plugin-api-v3 --no-default-features qemu-x86_64 -plugin ../../target/release/libtiny.so /bin/ls -lah cd ../.. From a2496bcd1cc3ac3093db4d39db625364f6b6164b Mon Sep 17 00:00:00 2001 From: novafacing Date: Sun, 2 Jun 2024 21:17:25 -0700 Subject: [PATCH 5/7] Add plugin tests for all three plugin versions --- .github/workflows/ci.yml | 165 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 162 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 21f346a..13c7895 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,10 +10,90 @@ env: CARGO_TERM_COLOR: always jobs: - test_plugins_linux: - name: Build and Test Plugins (Linux) + test_plugins_linux_v1: + name: Build and Test Plugins API v1 (Linux) runs-on: ubuntu-latest container: ubuntu:24.04 + env: + # Hash of v1 + QEMU_COMMIT_HASH: fb691b8cbabf5bde7d25a7f720d5ec7d5b1341e1 + steps: + - name: Set up Sources List + run: | + cat < /etc/apt/sources.list.d/ubuntu.sources + Types: deb + URIs: http://archive.ubuntu.com/ubuntu/ + Suites: noble noble-updates noble-backports + Components: main universe restricted multiverse + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + + Types: deb + URIs: http://security.ubuntu.com/ubuntu/ + Suites: noble-security + Components: main universe restricted multiverse + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + + Types: deb-src + URIs: http://archive.ubuntu.com/ubuntu/ + Suites: noble noble-updates noble-backports + Components: main universe restricted multiverse + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + + Types: deb + URIs: http://security.ubuntu.com/ubuntu/ + Suites: noble-security + Components: main universe restricted multiverse + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + EOF + - name: Install QEMU Build Dependencies + run: | + apt -y update && \ + apt -y install git curl build-essential && \ + apt -y source qemu && \ + apt -y build-dep qemu + + # Clone without history + - name: Clone QEMU + run: | + git clone https://github.com/qemu/qemu qemu-upstream + cd qemu-upstream + git checkout "${QEMU_COMMIT_HASH}" + + - name: Build QEMU + run: | + cd qemu-upstream + ./configure --enable-plugins + cd build + make -j$(nproc) + make install + cd ../.. + + - uses: dtolnay/rust-toolchain@nightly + - uses: actions/checkout@v4 + - name: Test QEMU Install + run: | + qemu-x86_64 --help + - name: Build and Test Tracer + run: | + cd plugins/tracer + cargo build -r --features=plugin-api-v1 --no-default-features || exit 0 + cargo build -r --features=plugin-api-v1 --no-default-features + cargo run --features=plugin-api-v1 --no-default-features -r --bin tracer -- -a /bin/ls -- -lah + cd ../.. + - name: Build and Test Tiny + run: | + cd plugins/tiny + cargo build -r --features=plugin-api-v1 --no-default-features + qemu-x86_64 -plugin ../../target/release/libtiny.so /bin/ls -lah + cd ../.. + + test_plugins_linux_v2: + name: Build and Test Plugins API v2 (Linux) + runs-on: ubuntu-latest + container: ubuntu:24.04 + env: + # Hash of v2 + QEMU_COMMIT_HASH: fba3b490a26cb278dfa183d7fcc375746e312980 steps: - name: Set up Sources List run: | @@ -52,7 +132,86 @@ jobs: # Clone without history - name: Clone QEMU run: | - git clone --depth 1 https://github.com/qemu/qemu qemu-upstream + git clone https://github.com/qemu/qemu qemu-upstream + cd qemu-upstream + git checkout "${QEMU_COMMIT_HASH}" + + - name: Build QEMU + run: | + cd qemu-upstream + ./configure --enable-plugins + cd build + make -j$(nproc) + make install + cd ../.. + + - uses: dtolnay/rust-toolchain@nightly + - uses: actions/checkout@v4 + - name: Test QEMU Install + run: | + qemu-x86_64 --help + - name: Build and Test Tracer + run: | + cd plugins/tracer + cargo build -r --features=plugin-api-v2 --no-default-features || exit 0 + cargo build -r --features=plugin-api-v2 --no-default-features + cargo run --features=plugin-api-v2 --no-default-features -r --bin tracer -- -a /bin/ls -- -lah + cd ../.. + - name: Build and Test Tiny + run: | + cd plugins/tiny + cargo build -r --features=plugin-api-v2 --no-default-features + qemu-x86_64 -plugin ../../target/release/libtiny.so /bin/ls -lah + cd ../.. + + test_plugins_linux_v3: + name: Build and Test Plugins API V3 (Linux) + runs-on: ubuntu-latest + container: ubuntu:24.04 + env: + # Hash of v3 + QEMU_COMMIT_HASH: 4abc892362f8282450f18c4e45c5b0534461d01e + steps: + - name: Set up Sources List + run: | + cat < /etc/apt/sources.list.d/ubuntu.sources + Types: deb + URIs: http://archive.ubuntu.com/ubuntu/ + Suites: noble noble-updates noble-backports + Components: main universe restricted multiverse + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + + Types: deb + URIs: http://security.ubuntu.com/ubuntu/ + Suites: noble-security + Components: main universe restricted multiverse + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + + Types: deb-src + URIs: http://archive.ubuntu.com/ubuntu/ + Suites: noble noble-updates noble-backports + Components: main universe restricted multiverse + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + + Types: deb + URIs: http://security.ubuntu.com/ubuntu/ + Suites: noble-security + Components: main universe restricted multiverse + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + EOF + - name: Install QEMU Build Dependencies + run: | + apt -y update && \ + apt -y install git curl build-essential && \ + apt -y source qemu && \ + apt -y build-dep qemu + + # Clone without history + - name: Clone QEMU + run: | + git clone https://github.com/qemu/qemu qemu-upstream + cd qemu-upstream + git checkout "${QEMU_COMMIT_HASH}" - name: Build QEMU run: | From ae16a6d75f8c953b839af755512dc06fc6090025 Mon Sep 17 00:00:00 2001 From: novafacing Date: Mon, 3 Jun 2024 10:28:08 -0700 Subject: [PATCH 6/7] Fix some typos and add conditional API for V3 --- .github/workflows/ci.yml | 2 +- plugins/tracer/src/lib.rs | 39 ++-- qemu-plugin-sys/generate-bindings.rs | 9 +- qemu-plugin-sys/src/bindings_v3.rs | 40 +++- qemu-plugin-sys/src/qemu_plugin_api_v3.def | 2 + qemu-plugin/src/lib.rs | 214 ++++++++++++++++++--- 6 files changed, 261 insertions(+), 45 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 13c7895..8b8e800 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -170,7 +170,7 @@ jobs: container: ubuntu:24.04 env: # Hash of v3 - QEMU_COMMIT_HASH: 4abc892362f8282450f18c4e45c5b0534461d01e + QEMU_COMMIT_HASH: 7de77d37880d7267a491cb32a1b2232017d1e545 steps: - name: Set up Sources List run: | diff --git a/plugins/tracer/src/lib.rs b/plugins/tracer/src/lib.rs index 6405578..b0d8ada 100644 --- a/plugins/tracer/src/lib.rs +++ b/plugins/tracer/src/lib.rs @@ -149,19 +149,17 @@ impl Tracer { } impl HasCallbacks for Tracer { + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] fn on_vcpu_init( &mut self, _id: PluginId, _vcpu_id: VCPUIndex, ) -> std::prelude::v1::Result<(), anyhow::Error> { - #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] - { - *self - .registers - .lock() - .map_err(|e| anyhow!("Failed to lock registers: {}", e))? = - qemu_plugin_get_registers()?; - } + *self + .registers + .lock() + .map_err(|e| anyhow!("Failed to lock registers: {}", e))? = + qemu_plugin_get_registers()?; Ok(()) } @@ -173,6 +171,27 @@ impl HasCallbacks for Tracer { tb.instructions().try_for_each(|insn| { let event = InstructionEvent::try_from(&insn)?; + #[cfg(feature = "plugin-api-v1")] + if self.log_insns { + let tx = self.tx.clone(); + + insn.register_execute_callback(move |_| { + tx.lock() + .map_err(|e| anyhow!("Failed to lock tx: {}", e)) + .and_then(|tx| { + to_writer( + tx.as_ref().ok_or_else(|| anyhow!("No tx"))?, + &Event::Instruction { + event: event.clone(), + }, + ) + .map_err(|e| anyhow!(e)) + }) + .expect("Failed to send instruction event"); + }); + } + + #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] if self.log_insns { let tx = self.tx.clone(); let registers = self @@ -189,10 +208,6 @@ impl HasCallbacks for Tracer { tx.as_ref().ok_or_else(|| anyhow!("No tx"))?, &Event::Instruction { event: event.clone(), - #[cfg(any( - feature = "plugin-api-v2", - feature = "plugin-api-v3" - ))] registers: Registers( registers .iter() diff --git a/qemu-plugin-sys/generate-bindings.rs b/qemu-plugin-sys/generate-bindings.rs index 5584680..335e44c 100755 --- a/qemu-plugin-sys/generate-bindings.rs +++ b/qemu-plugin-sys/generate-bindings.rs @@ -36,8 +36,8 @@ const QEMU_SRC_URL_BASE: &str = "https://download.qemu.org/"; const QEMU_VERSION_V1: &str = "8.2.4"; // Plugin V2 is from 9.0.0 const QEMU_VERSION_V2: &str = "9.0.0"; -/// Plugin V3 is from 4abc892362f8282450f18c4e45c5b0534461d01e -const QEMU_VERSION_V3: &str = "4abc892362f8282450f18c4e45c5b0534461d01e"; +/// Plugin V3 is from 7de77d37880d7267a491cb32a1b2232017d1e545 +const QEMU_VERSION_V3: &str = "7de77d37880d7267a491cb32a1b2232017d1e545"; fn qemu_src_url_v1() -> String { format!("{}qemu-{}.tar.xz", QEMU_SRC_URL_BASE, QEMU_VERSION_V1) @@ -48,7 +48,7 @@ fn qemu_src_url_v2() -> String { } fn qemu_src_url_v3() -> String { - format!("{}/archive/4abc892362f8282450f18c4e45c5b0534461d01e.zip", QEMU_GITHUB_URL_BASE) + format!("{}/archive/{}.zip", QEMU_GITHUB_URL_BASE, QEMU_VERSION_V3) } /// Download a URL to a destination, using a blocking request @@ -91,7 +91,7 @@ fn extract_txz(archive: &Path, destination: &Path) -> Result<()> { Ok(()) } -/// Extract a zip frile at a path to a destination +/// Extract a zip file at a path to a destination fn extract_zip(archive: &Path, destination: &Path) -> Result<()> { let archive = File::open(archive)?; let mut archive = ZipArchive::new(archive)?; @@ -108,7 +108,6 @@ fn generate_windows_delaylink_library(qemu_plugin_symbols: &Path, destination: & } fn generate_bindings(qemu_plugin_header: &Path, destination: &Path) -> Result<()> { - // let glib_20 = probe_library("glib-2.0").map_err(|e| anyhow!(e))?; let header_contents = read_to_string(qemu_plugin_header)?; let header_file_name = qemu_plugin_header.file_name().ok_or_else(|| anyhow!("Failed to get file name"))?.to_str().ok_or_else(|| anyhow!("Failed to convert file name to string"))?; let header_contents = header_contents.replace("#include ", ""); diff --git a/qemu-plugin-sys/src/bindings_v3.rs b/qemu-plugin-sys/src/bindings_v3.rs index 747a67e..3d09443 100644 --- a/qemu-plugin-sys/src/bindings_v3.rs +++ b/qemu-plugin-sys/src/bindings_v3.rs @@ -185,6 +185,19 @@ pub enum qemu_plugin_mem_rw { QEMU_PLUGIN_MEM_W = 2, QEMU_PLUGIN_MEM_RW = 3, } +#[repr(u32)] +#[doc = " enum qemu_plugin_cond - condition to enable callback\n\n @QEMU_PLUGIN_COND_NEVER: false\n @QEMU_PLUGIN_COND_ALWAYS: true\n @QEMU_PLUGIN_COND_EQ: is equal?\n @QEMU_PLUGIN_COND_NE: is not equal?\n @QEMU_PLUGIN_COND_LT: is less than?\n @QEMU_PLUGIN_COND_LE: is less than or equal?\n @QEMU_PLUGIN_COND_GT: is greater than?\n @QEMU_PLUGIN_COND_GE: is greater than or equal?"] +#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] +pub enum qemu_plugin_cond { + QEMU_PLUGIN_COND_NEVER = 0, + QEMU_PLUGIN_COND_ALWAYS = 1, + QEMU_PLUGIN_COND_EQ = 2, + QEMU_PLUGIN_COND_NE = 3, + QEMU_PLUGIN_COND_LT = 4, + QEMU_PLUGIN_COND_LE = 5, + QEMU_PLUGIN_COND_GT = 6, + QEMU_PLUGIN_COND_GE = 7, +} #[doc = " typedef qemu_plugin_vcpu_tb_trans_cb_t - translation callback\n @id: unique plugin id\n @tb: opaque handle used for querying and instrumenting a block."] pub type qemu_plugin_vcpu_tb_trans_cb_t = ::std::option::Option; @@ -204,11 +217,24 @@ extern "C" { userdata: *mut ::std::os::raw::c_void, ); } +extern "C" { + #[doc = " qemu_plugin_register_vcpu_tb_exec_cond_cb() - register conditional callback\n @tb: the opaque qemu_plugin_tb handle for the translation\n @cb: callback function\n @cond: condition to enable callback\n @entry: first operand for condition\n @imm: second operand for condition\n @flags: does the plugin read or write the CPU's registers?\n @userdata: any plugin data to pass to the @cb?\n\n The @cb function is called when a translated unit executes if\n entry @cond imm is true.\n If condition is QEMU_PLUGIN_COND_ALWAYS, condition is never interpreted and\n this function is equivalent to qemu_plugin_register_vcpu_tb_exec_cb.\n If condition QEMU_PLUGIN_COND_NEVER, condition is never interpreted and\n callback is never installed."] + pub fn qemu_plugin_register_vcpu_tb_exec_cond_cb( + tb: *mut qemu_plugin_tb, + cb: qemu_plugin_vcpu_udata_cb_t, + flags: qemu_plugin_cb_flags, + cond: qemu_plugin_cond, + entry: qemu_plugin_u64, + imm: u64, + userdata: *mut ::std::os::raw::c_void, + ); +} #[repr(u32)] -#[doc = " enum qemu_plugin_op - describes an inline op\n\n @QEMU_PLUGIN_INLINE_ADD_U64: add an immediate value uint64_t\n\n Note: currently only a single inline op is supported."] +#[doc = " enum qemu_plugin_op - describes an inline op\n\n @QEMU_PLUGIN_INLINE_ADD_U64: add an immediate value uint64_t\n @QEMU_PLUGIN_INLINE_STORE_U64: store an immediate value uint64_t"] #[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] pub enum qemu_plugin_op { QEMU_PLUGIN_INLINE_ADD_U64 = 0, + QEMU_PLUGIN_INLINE_STORE_U64 = 1, } extern "C" { #[doc = " qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu() - execution inline op\n @tb: the opaque qemu_plugin_tb handle for the translation\n @op: the type of qemu_plugin_op (e.g. ADD_U64)\n @entry: entry to run op\n @imm: the op data (e.g. 1)\n\n Insert an inline op on a given scoreboard entry."] @@ -228,6 +254,18 @@ extern "C" { userdata: *mut ::std::os::raw::c_void, ); } +extern "C" { + #[doc = " qemu_plugin_register_vcpu_insn_exec_cond_cb() - conditional insn execution cb\n @insn: the opaque qemu_plugin_insn handle for an instruction\n @cb: callback function\n @flags: does the plugin read or write the CPU's registers?\n @cond: condition to enable callback\n @entry: first operand for condition\n @imm: second operand for condition\n @userdata: any plugin data to pass to the @cb?\n\n The @cb function is called when an instruction executes if\n entry @cond imm is true.\n If condition is QEMU_PLUGIN_COND_ALWAYS, condition is never interpreted and\n this function is equivalent to qemu_plugin_register_vcpu_insn_exec_cb.\n If condition QEMU_PLUGIN_COND_NEVER, condition is never interpreted and\n callback is never installed."] + pub fn qemu_plugin_register_vcpu_insn_exec_cond_cb( + insn: *mut qemu_plugin_insn, + cb: qemu_plugin_vcpu_udata_cb_t, + flags: qemu_plugin_cb_flags, + cond: qemu_plugin_cond, + entry: qemu_plugin_u64, + imm: u64, + userdata: *mut ::std::os::raw::c_void, + ); +} extern "C" { #[doc = " qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu() - insn exec inline op\n @insn: the opaque qemu_plugin_insn handle for an instruction\n @op: the type of qemu_plugin_op (e.g. ADD_U64)\n @entry: entry to run op\n @imm: the op data (e.g. 1)\n\n Insert an inline op to every time an instruction executes."] pub fn qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu( diff --git a/qemu-plugin-sys/src/qemu_plugin_api_v3.def b/qemu-plugin-sys/src/qemu_plugin_api_v3.def index a11358a..ece395c 100644 --- a/qemu-plugin-sys/src/qemu_plugin_api_v3.def +++ b/qemu-plugin-sys/src/qemu_plugin_api_v3.def @@ -28,6 +28,7 @@ EXPORTS qemu_plugin_register_vcpu_idle_cb qemu_plugin_register_vcpu_init_cb qemu_plugin_register_vcpu_insn_exec_cb + qemu_plugin_register_vcpu_insn_exec_cond_cb qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu qemu_plugin_register_vcpu_mem_cb qemu_plugin_register_vcpu_mem_inline_per_vcpu @@ -35,6 +36,7 @@ EXPORTS qemu_plugin_register_vcpu_syscall_cb qemu_plugin_register_vcpu_syscall_ret_cb qemu_plugin_register_vcpu_tb_exec_cb + qemu_plugin_register_vcpu_tb_exec_cond_cb qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu qemu_plugin_register_vcpu_tb_trans_cb qemu_plugin_reset diff --git a/qemu-plugin/src/lib.rs b/qemu-plugin/src/lib.rs index 0430925..59bd49f 100644 --- a/qemu-plugin/src/lib.rs +++ b/qemu-plugin/src/lib.rs @@ -83,6 +83,8 @@ mod win_link_hook; use crate::error::{Error, Result}; #[cfg(feature = "num-traits")] use num_traits::{FromBytes, PrimInt}; +#[cfg(feature = "plugin-api-v3")] +use qemu_plugin_sys::qemu_plugin_cond; use qemu_plugin_sys::{ qemu_plugin_cb_flags, qemu_plugin_hwaddr, qemu_plugin_id_t, qemu_plugin_insn, qemu_plugin_mem_rw, qemu_plugin_meminfo_t, qemu_plugin_op, qemu_plugin_simple_cb_t, @@ -239,13 +241,16 @@ pub type VCPUIndex = c_uint; /// u64 member of an entry in a scoreboard, allows access to a specific u64 member in /// one given entry, located at a specified offset. Inline operations expect this as an /// entry. -pub type QemuPluginU64 = qemu_plugin_u64; +pub type PluginU64 = qemu_plugin_u64; /// Flags for callbacks pub type CallbackFlags = qemu_plugin_cb_flags; /// Memory read/write flags pub type MemRW = qemu_plugin_mem_rw; +#[cfg(any(feature = "plugin-api-v3"))] +/// A condition for a callback to be run +pub type PluginCondition = qemu_plugin_cond; /// Plugin operations for inline operations -pub type QemuPluginOp = qemu_plugin_op; +pub type PluginOp = qemu_plugin_op; /// A plugin ID pub type PluginId = qemu_plugin_id_t; @@ -410,6 +415,57 @@ impl<'a> TranslationBlock<'a> { ) }; } + + #[cfg(feature = "plugin-api-v3")] + /// Register a callback to be conditionally run on execution of this translation + /// block + pub fn register_conditional_execute_callback( + &self, + cb: F, + cond: PluginCondition, + entry: PluginU64, + immediate: u64, + ) where + F: FnMut(VCPUIndex) + Send + Sync + 'static, + { + self.register_conditional_execute_callback_flags( + cb, + CallbackFlags::QEMU_PLUGIN_CB_NO_REGS, + cond, + entry, + immediate, + ) + } + + #[cfg(feature = "plugin-api-v3")] + /// Register a callback to be conditionally run on execution of this translation + /// block + pub fn register_conditional_execute_callback_flags( + &self, + cb: F, + flags: CallbackFlags, + cond: PluginCondition, + entry: PluginU64, + immediate: u64, + ) where + F: FnMut(VCPUIndex) + Send + Sync + 'static, + { + let callback = Box::new(cb); + let callback_box = Box::new(callback); + let userdata = Box::into_raw(callback_box) as *mut c_void; + + unsafe { + crate::sys::qemu_plugin_register_vcpu_tb_exec_cond_cb( + self.translation_block as *mut qemu_plugin_tb, + Some(handle_qemu_plugin_register_vcpu_tb_exec_cb::), + flags, + cond, + entry, + immediate, + userdata, + ) + }; + } } /// An iterator over the instructions of a translation block @@ -463,10 +519,10 @@ impl<'a> Instruction<'a> { /// Returns the data for this instruction. This method may only be called inside the /// callback in which the instruction is obtained, but the resulting data is owned. pub fn data(&self) -> Vec { - println!("data 1"); let size = self.size(); let mut data = Vec::with_capacity(size); + // NOTE: The name of this API doesn't change, but its parameters and return value *do* let insn_data = unsafe { crate::sys::qemu_plugin_insn_data(self.instruction as *mut qemu_plugin_insn) } as *mut u8; @@ -486,6 +542,7 @@ impl<'a> Instruction<'a> { let size = self.size(); let mut data = vec![0; size]; + // NOTE: The name of this API doesn't change, but its parameters and return value *do* let size = unsafe { crate::sys::qemu_plugin_insn_data( self.instruction as *mut qemu_plugin_insn, @@ -574,6 +631,55 @@ impl<'a> Instruction<'a> { }; } + /// Register a callback to be conditionally run on execution of this instruction + #[cfg(feature = "plugin-api-v3")] + pub fn register_conditional_execute_callback( + &self, + cb: F, + cond: PluginCondition, + entry: PluginU64, + immediate: u64, + ) where + F: FnMut(VCPUIndex) + Send + Sync + 'static, + { + self.register_conditional_execute_callback_flags( + cb, + CallbackFlags::QEMU_PLUGIN_CB_NO_REGS, + cond, + entry, + immediate, + ) + } + + /// Register a callback to be conditionally run on execution of this instruction + #[cfg(feature = "plugin-api-v3")] + pub fn register_conditional_execute_callback_flags( + &self, + cb: F, + flags: CallbackFlags, + cond: PluginCondition, + entry: PluginU64, + immediate: u64, + ) where + F: FnMut(VCPUIndex) + Send + Sync + 'static, + { + let callback = Box::new(cb); + let callback_box = Box::new(callback); + let userdata = Box::into_raw(callback_box) as *mut c_void; + + unsafe { + crate::sys::qemu_plugin_register_vcpu_insn_exec_cond_cb( + self.instruction as *mut qemu_plugin_insn, + Some(handle_qemu_plugin_register_vcpu_insn_exec_cb::), + flags, + cond, + entry, + immediate, + userdata, + ) + }; + } + /// Register a callback to be run on memory access of this instruction /// /// # Arguments @@ -1090,11 +1196,39 @@ extern "C" fn handle_qemu_plugin_register_vcpu_tb_exec_cb( /// /// This function is safe when the pointer `tb` is a valid pointer to a `qemu_plugin_tb` /// structure, which is always opaque. -pub fn qemu_plugin_register_vcpu_tb_exec_cb(tb: TranslationBlock, cb: F) +pub fn qemu_plugin_register_vcpu_tb_exec_cb(tb: TranslationBlock, cb: F, flags: CallbackFlags) where F: FnMut(VCPUIndex) + Send + Sync + 'static, { - tb.register_execute_callback(cb); + tb.register_execute_callback_flags(cb, flags); +} + +#[cfg(feature = "plugin-api-v3")] +/// Register a callback to be conditionally called when a translation block is executed. +/// +/// # Arguments +/// +/// - `tb`: The translated block to register the execution callback for +/// - `cb`: The callback to be called when the block `tb` is executed +/// - `cond`: The condition to be met for the callback to be called +/// - `entry`: The entry to be passed to the callback +/// - `immediate`: The immediate value to be passed to the callback +/// +/// # Safety +/// +/// This function is safe when the pointer `tb` is a valid pointer to a `qemu_plugin_tb` +/// structure, which is always opaque. +pub fn qemu_plugin_register_vcpu_tb_exec_cond_cb( + tb: TranslationBlock, + cb: F, + flags: CallbackFlags, + cond: PluginCondition, + entry: PluginU64, + immediate: u64, +) where + F: FnMut(VCPUIndex) + Send + Sync + 'static, +{ + tb.register_conditional_execute_callback_flags(cb, flags, cond, entry, immediate); } #[cfg(feature = "plugin-api-v1")] @@ -1109,7 +1243,7 @@ where /// - `imm`: The immediate value to be passed to the operation pub fn qemu_plugin_register_vcpu_tb_exec_inline( tb: TranslationBlock, - op: QemuPluginOp, + op: PluginOp, ptr: *mut c_void, imm: u64, ) { @@ -1135,8 +1269,8 @@ pub fn qemu_plugin_register_vcpu_tb_exec_inline( /// - `imm`: The immediate value to be passed to the operation pub fn qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu( tb: TranslationBlock, - op: QemuPluginOp, - entry: QemuPluginU64, + op: PluginOp, + entry: PluginU64, imm: u64, ) { unsafe { @@ -1168,11 +1302,35 @@ extern "C" fn handle_qemu_plugin_register_vcpu_insn_exec_cb( /// /// - `insn`: The instruction handle to register the callback for /// - `cb`: The callback to be called -pub fn qemu_plugin_register_vcpu_insn_exec_cb(insn: Instruction, cb: F) +pub fn qemu_plugin_register_vcpu_insn_exec_cb(insn: Instruction, cb: F, flags: CallbackFlags) where F: FnMut(VCPUIndex) + Send + Sync + 'static, { - insn.register_execute_callback(cb); + insn.register_execute_callback_flags(cb, flags); +} + +#[cfg(feature = "plugin-api-v3")] +#[allow(clippy::not_unsafe_ptr_arg_deref)] +/// Register a callback to be conditionally called when an instruction is executed. +/// +/// # Arguments +/// +/// - `insn`: The instruction handle to register the callback for +/// - `cb`: The callback to be called +/// - `cond`: The condition to be met for the callback to be called +/// - `entry`: The entry to be passed to the callback +/// - `immediate`: The immediate value to be passed to the callback +pub fn qemu_plugin_register_vcpu_insn_exec_cond_cb( + insn: Instruction, + cb: F, + flags: CallbackFlags, + cond: PluginCondition, + entry: PluginU64, + immediate: u64, +) where + F: FnMut(VCPUIndex) + Send + Sync + 'static, +{ + insn.register_conditional_execute_callback_flags(cb, flags, cond, entry, immediate); } #[cfg(feature = "plugin-api-v1")] @@ -1187,7 +1345,7 @@ where /// - `imm`: The immediate value to be passed to the operation pub fn qemu_plugin_register_vcpu_insn_exec_inline( insn: Instruction, - op: QemuPluginOp, + op: PluginOp, ptr: *mut c_void, imm: u64, ) { @@ -1213,8 +1371,8 @@ pub fn qemu_plugin_register_vcpu_insn_exec_inline( /// - `imm`: The immediate value to be passed to the operation pub fn qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu( insn: Instruction, - op: QemuPluginOp, - entry: QemuPluginU64, + op: PluginOp, + entry: PluginU64, imm: u64, ) { unsafe { @@ -1250,11 +1408,15 @@ extern "C" fn handle_qemu_plugin_register_vcpu_mem_cb( /// - `insn`: The instruction handle to register the callback for /// - `cb`: The callback to be called /// - `rw`: Whether the callback should be called for reads, writes, or both -pub fn qemu_plugin_register_vcpu_mem_cb(insn: Instruction, cb: F, rw: MemRW) -where +pub fn qemu_plugin_register_vcpu_mem_cb( + insn: Instruction, + cb: F, + flags: CallbackFlags, + rw: MemRW, +) where F: FnMut(VCPUIndex, MemoryInfo, u64) + Send + Sync + 'static, { - insn.register_memory_access_callback(cb, rw); + insn.register_memory_access_callback_flags(cb, rw, flags); } #[cfg(feature = "plugin-api-v1")] @@ -1271,7 +1433,7 @@ where pub fn qemu_plugin_register_vcpu_mem_inline( insn: Instruction, rw: MemRW, - op: QemuPluginOp, + op: PluginOp, ptr: *mut c_void, imm: u64, ) { @@ -1300,8 +1462,8 @@ pub fn qemu_plugin_register_vcpu_mem_inline( pub fn qemu_plugin_register_vcpu_mem_inline_per_vcpu( insn: Instruction, rw: MemRW, - op: QemuPluginOp, - entry: QemuPluginU64, + op: PluginOp, + entry: PluginU64, imm: u64, ) { unsafe { @@ -1542,26 +1704,26 @@ pub fn qemu_plugin_get_registers<'a>() -> Result>> { } #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] -/// Add a value to a `QemuPluginU64` for a given VCPU -pub fn qemu_plugin_u64_add(entry: QemuPluginU64, vcpu_index: VCPUIndex, added: u64) -> Result<()> { +/// Add a value to a `PluginU64` for a given VCPU +pub fn qemu_plugin_u64_add(entry: PluginU64, vcpu_index: VCPUIndex, added: u64) -> Result<()> { unsafe { crate::sys::qemu_plugin_u64_add(entry, vcpu_index, added) }; Ok(()) } #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] -/// Get the value of a `QemuPluginU64` for a given VCPU -pub fn qemu_plugin_u64_get(entry: QemuPluginU64, vcpu_index: VCPUIndex) -> u64 { +/// Get the value of a `PluginU64` for a given VCPU +pub fn qemu_plugin_u64_get(entry: PluginU64, vcpu_index: VCPUIndex) -> u64 { unsafe { crate::sys::qemu_plugin_u64_get(entry, vcpu_index) } } #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] -/// Set the value of a `QemuPluginU64` for a given VCPU -pub fn qemu_plugin_u64_set(entry: QemuPluginU64, vcpu_index: VCPUIndex, value: u64) { +/// Set the value of a `PluginU64` for a given VCPU +pub fn qemu_plugin_u64_set(entry: PluginU64, vcpu_index: VCPUIndex, value: u64) { unsafe { crate::sys::qemu_plugin_u64_set(entry, vcpu_index, value) } } #[cfg(any(feature = "plugin-api-v2", feature = "plugin-api-v3"))] /// Get the sum of all VCPU entries in a scoreboard -pub fn qemu_plugin_scoreboard_sum(entry: QemuPluginU64) -> u64 { +pub fn qemu_plugin_scoreboard_sum(entry: PluginU64) -> u64 { unsafe { crate::sys::qemu_plugin_u64_sum(entry) } } From fa36f978763887ed59ef4fd6e9bec9650705afc5 Mon Sep 17 00:00:00 2001 From: novafacing Date: Mon, 3 Jun 2024 10:39:03 -0700 Subject: [PATCH 7/7] Add versioning information to readme --- README.md | 2 +- qemu-plugin-sys/README.md | 18 +++++++++++++++++- qemu-plugin/README.md | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f77f316..ffdef46 100644 --- a/README.md +++ b/README.md @@ -37,4 +37,4 @@ cargo install qemu@9.0.0-v0 --features=binaries On some systems, particularly BTRFS systems, `/tmp` may not be large enough for the temporary build directory (QEMU is quite large to build). In this case, create a directory on your root filesystem (e.g. `$HOME/.cargo/tmp`) and set -`CARGO_TARGET_DIR=$HOME/.cargo/tmp` when running the install command. +`CARGO_TARGET_DIR=$HOME/.cargo/tmp` when running the install command. \ No newline at end of file diff --git a/qemu-plugin-sys/README.md b/qemu-plugin-sys/README.md index 1936e66..24a9ac6 100644 --- a/qemu-plugin-sys/README.md +++ b/qemu-plugin-sys/README.md @@ -2,4 +2,20 @@ Low level auto-generated FFI bindings to the QEMU Plugin API (`qemu-plugin.h`). This crate should not be used directly, check out the `qemu-plugin` crate for the idiomatic -high-level bindings. \ No newline at end of file +high-level bindings. + +## Versioning + +As of QEMU 8.2.4, the QEMU plugin API has more than a single version. This enables some +great features like register inspection and conditional callbacks. Versioning is +implemented in the `qemu-plugin-sys` crate via compile-time features, because a dynamic +library can only be compatible with one version at a time. To choose a version, set a +listing like: + +```toml +qemu-plugin-sys = { version = "9.0.0-v0", features = ["plugin-api-v2"], default-features = false } +``` + +The `qemu-plugin-sys` crate's default plugin version is set to the latest version that +is officially released in QEMU. Currently, this is V2, released in 8.2.4 and 9.0.0. If +you need a different version, you *must* set `default-features = false`. \ No newline at end of file diff --git a/qemu-plugin/README.md b/qemu-plugin/README.md index 44cd25a..4aef260 100644 --- a/qemu-plugin/README.md +++ b/qemu-plugin/README.md @@ -70,3 +70,19 @@ anyhow = "1.0.75" ffi = "0.1.0" ctor = "0.2.6" ``` + +## Versioning + +As of QEMU 8.2.4, the QEMU plugin API has more than a single version. This enables some +great features like register inspection and conditional callbacks. Versioning is +implemented in the `qemu-plugin` crate via compile-time features, because a dynamic +library can only be compatible with one version at a time. To choose a version, set a +listing like: + +```toml +qemu-plugin = { version = "9.0.0-v0", features = ["plugin-api-v2"], default-features = false } +``` + +The `qemu-plugin` crate's default plugin version is set to the latest version that is +officially released in QEMU. Currently, this is V2, released in 8.2.4 and 9.0.0. If you +need a different version, you *must* set `default-features = false`. \ No newline at end of file