panda/api/
misc.rs

1use crate::prelude::*;
2use std::os::raw::c_int;
3use std::ffi::CStr;
4
5/// Determine if guest is currently executing in kernel mode
6pub fn in_kernel_mode(cpu: &mut CPUState) -> bool {
7    unsafe {
8        panda_sys::panda_in_kernel_mode_external(cpu)
9    }
10}
11
12/// Determine if guest is currently executing kernel code
13pub fn in_kernel_code_linux(cpu: &mut CPUState) -> bool {
14    unsafe {
15        panda_sys::panda_in_kernel_code_linux_external(cpu)
16    }
17}
18
19/// Get current architecture independent Address-Space ID (ASID)
20pub fn current_asid(cpu: &mut CPUState) -> target_ulong {
21    unsafe {
22        panda_sys::panda_current_asid(cpu)
23    }
24}
25
26/// Get current guest program counter
27pub fn current_pc(cpu: &mut CPUState) -> target_ulong {
28    unsafe {
29        panda_sys::panda_current_pc(cpu)
30    }
31}
32
33/// Get current guest userspace stack pointer
34pub fn current_sp(cpu: &mut CPUState) -> target_ulong {
35    unsafe {
36        panda_sys::panda_current_sp_external(cpu)
37    }
38}
39
40/// Get current guest userspace stack pointer, masking of page size MSBs
41pub fn current_sp_masked_pagesize(cpu: &mut CPUState, page_size: target_ulong) -> target_ulong {
42    unsafe {
43        panda_sys::panda_current_sp_masked_pagesize_external(cpu, page_size)
44    }
45}
46
47/// Get current guest kernelspace stack pointer
48pub fn current_ksp(cpu: &mut CPUState) -> target_ulong {
49    unsafe {
50        panda_sys::panda_current_ksp_external(cpu)
51    }
52}
53
54/// Get current guest function return value
55pub fn get_ret_val(cpu: &mut CPUState) -> target_ulong {
56    unsafe {
57        panda_sys::panda_get_retval_external(cpu)
58    }
59}
60
61/// If required for the target architecture, enter into a high-privilege mode in order to conduct some memory access.
62/// Returns true if a switch into high-privilege mode has been made.
63/// A NO-OP on systems where such changes are unnecessary.
64pub fn enter_priv(cpu: &mut CPUState) -> bool {
65    unsafe {
66        panda_sys::enter_priv(cpu)
67    }
68}
69
70/// Revert the guest to the privilege mode it was in prior to the last call to enter_priv().
71/// A NO-OP for architectures where enter_priv() is a NO-OP.
72pub fn exit_priv(cpu: &mut CPUState) {
73    unsafe {
74        panda_sys::exit_priv(cpu)
75    }
76}
77
78/// Get count of commandline arguments
79pub fn argc() -> c_int {
80    unsafe {
81        panda_sys::panda_argc
82    }
83}
84
85/// Get commandline arguments
86pub fn argv() -> Vec<String> {
87    let mut rs_argv = Vec::new();
88
89    for char_ptr in unsafe { panda_sys::panda_argv }.iter() {
90        if let Ok(str_slice) = unsafe { CStr::from_ptr(*char_ptr) }.to_str() {
91            rs_argv.push(str_slice.to_owned());
92        }
93    }
94
95    rs_argv
96}