### Low-Level SMC/HVC Calls in Rust Source: https://context7.com/google/smccc/llms.txt Directly make 32-bit and 64-bit SMC or HVC calls using provided functions. Handles register setup and inline assembly for ARM processors. Ensure correct function ID and argument count for the target architecture. ```rust use smccc::{hvc32, smc32, hvc64, smc64}; // Make a 32-bit SMC call with function ID and 7 arguments // Returns 8 result registers let function_id: u32 = 0x84000000; // Example: PSCI_VERSION let args: [u32; 7] = [0, 0, 0, 0, 0, 0, 0]; let result: [u32; 8] = smc32(function_id, args); let version = result[0]; // Make a 32-bit HVC call (same interface, different instruction) let result: [u32; 8] = hvc32(function_id, args); // Make a 64-bit SMC call with function ID and 17 arguments // Returns 18 result registers (aarch64 only) let args64: [u64; 17] = [0; 17]; let result64: [u64; 18] = smc64(function_id, args64); // Make a 64-bit HVC call (aarch64 only) let result64: [u64; 18] = hvc64(function_id, args64); ``` -------------------------------- ### PSCI Feature Query API Source: https://context7.com/google/smccc/llms.txt Queries whether a specific PSCI function is implemented and gets its feature flags. ```APIDOC ## PSCI Feature Query ### Description Query whether a specific PSCI function is implemented and get its feature flags. ### Method N/A (Function call) ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use smccc::Smc; use smccc::psci::{psci_features, Error, PSCI_CPU_SUSPEND_64, PSCI_SYSTEM_RESET2_64}; // Check if CPU_SUSPEND is supported let result: Result = psci_features::(PSCI_CPU_SUSPEND_64); match result { Ok(features) => { println!("CPU_SUSPEND supported, features: {:#x}", features); // Feature bits are function-specific } Err(Error::NotSupported) => println!("CPU_SUSPEND not implemented"), Err(e) => println!("Query failed: {}", e), } // Check SYSTEM_RESET2 support if psci_features::(PSCI_SYSTEM_RESET2_64).is_ok() { println!("SYSTEM_RESET2 is available"); } ``` ### Response #### Success Response (200) Returns `Ok(u32)` containing feature flags for the requested PSCI function. #### Response Example ```json { "function_id": "PSCI_CPU_SUSPEND_64", "features": "0x1" } ``` ``` -------------------------------- ### CPU Power On via SMC/HVC Source: https://context7.com/google/smccc/llms.txt Power up a specific CPU core using either the 64-bit or 32-bit interface via SMC or HVC. Requires the target CPU's MPIDR, an entry point address, and a context ID. Handles potential errors like `AlreadyOn` or `InvalidParameters`. ```rust use smccc::{Smc, Hvc}; use smccc::psci::{cpu_on, cpu_on_32, Error}; // Power on a CPU using 64-bit interface (aarch64) let target_cpu: u64 = 0x0000_0001; // MPIDR of target CPU (core 1) let entry_point: u64 = 0x4008_0000; // Address where CPU starts execution let context_id: u64 = 0; // Context passed to entry point in x0 let result: Result<(), Error> = cpu_on::(target_cpu, entry_point, context_id); match result { Ok(()) => println!("CPU powered on successfully"), Err(Error::AlreadyOn) => println!("CPU is already powered on"), Err(Error::OnPending) => println!("CPU power-on is already in progress"), Err(Error::InvalidParameters) => println!("Invalid CPU or address"), Err(e) => println!("Failed to power on CPU: {}", e), } // Using 32-bit interface (aarch32 or 32-bit PSCI call) let result = cpu_on_32::(0x01, 0x4008_0000, 0); ``` -------------------------------- ### System Off and Reset Source: https://context7.com/google/smccc/llms.txt Initiate a system-wide shutdown or reset. These functions do not return on success. The `system_reset2` function allows specifying a vendor-defined reset type and a platform-specific cookie for more granular control. ```rust use smccc::Smc; use smccc::psci::{system_off, system_reset, system_reset2, Error}; // Shutdown the system (does not return on success) let result: Result<(), Error> = system_off::(); if let Err(e) = result { println!("System shutdown failed: {}", e); } // Reset the system (does not return on success) let result: Result<(), Error> = system_reset::(); if let Err(e) = result { println!("System reset failed: {}", e); } // Reset with vendor-specific reset type and cookie (64-bit) let reset_type: u32 = 0; // Architectural reset (0) or vendor-defined let cookie: u64 = 0x1234_5678; // Platform-specific cookie let result = system_reset2::(reset_type, cookie); ``` -------------------------------- ### Execute Security Workarounds Source: https://context7.com/google/smccc/llms.txt Applies firmware mitigations for CPU vulnerabilities like Spectre and Meltdown. Different functions exist for specific CVEs, and some allow enabling/disabling the workaround. ```rust use smccc::Smc; use smccc::arch::{arch_workaround_1, arch_workaround_2, arch_workaround_3, Error}; // Execute CVE-2017-5715 (Spectre v2) mitigation let result: Result<(), Error> = arch_workaround_1::(); match result { Ok(()) => println!("Workaround 1 executed"), Err(Error::NotSupported) => println!("Workaround not available"), Err(Error::NotRequired) => println!("Mitigation not needed on this CPU"), Err(e) => println!("Error: {}", e), } // Enable/disable CVE-2018-3639 (Spectre v4) mitigation let enable = true; let result: Result<(), Error> = arch_workaround_2::(enable); match result { Ok(()) => println!("Workaround 2 {}", if enable { "enabled" } else { "disabled" }), Err(e) => println!("Workaround 2 failed: {}", e), } // Execute combined CVE-2017-5715 and CVE-2022-23960 mitigation let result = arch_workaround_3::(); ``` -------------------------------- ### Query CPU Power Statistics Source: https://context7.com/google/smccc/llms.txt Retrieves power state residency time and usage count for a specific CPU. Requires specifying the target CPU and the power state of interest. Calculates average residency if the state was entered more than once. ```rust use smccc::Smc; use smccc::psci::{stat_residency, stat_count}; let target_cpu: u64 = 0x0; // CPU 0 let power_state: u32 = 0x0001_0000; // Power state to query // Get time spent in power state (microseconds since boot) let residency_us: u64 = stat_residency::(target_cpu, power_state); println!("CPU 0 spent {} us in power state", residency_us); // Get number of times power state was entered let count: u64 = stat_count::(target_cpu, power_state); println!("CPU 0 entered power state {} times", count); // Calculate average residency per entry if count > 0 { let avg_residency = residency_us / count; println!("Average: {} us per entry", avg_residency); } ``` -------------------------------- ### System Off and Reset API Source: https://context7.com/google/smccc/llms.txt Shuts down or resets the entire system. ```APIDOC ## System Off and Reset ### Description Shutdown or reset the entire system. ### Method N/A (Function call) ### Endpoint N/A ### Parameters None ### Request Example ```rust use smccc::Smc; use smccc::psci::{system_off, system_reset, system_reset2, Error}; // Shutdown the system (does not return on success) let result: Result<(), Error> = system_off::(); if let Err(e) = result { println!("System shutdown failed: {}", e); } // Reset the system (does not return on success) let result: Result<(), Error> = system_reset::(); if let Err(e) = result { println!("System reset failed: {}", e); } // Reset with vendor-specific reset type and cookie (64-bit) let reset_type: u32 = 0; // Architectural reset (0) or vendor-defined let cookie: u64 = 0x1234_5678; // Platform-specific cookie let result = system_reset2::(reset_type, cookie); ``` ### Response #### Success Response These functions do not return on success; they initiate system shutdown or reset. #### Response Example N/A ``` -------------------------------- ### Query PSCI Version via SMC/HVC Source: https://context7.com/google/smccc/llms.txt Query the implemented PSCI version from firmware using either SMC or HVC. Returns a `Version` struct or an error. The `Version` struct implements `Display` and comparison traits. ```rust use smccc::{Smc, Hvc}; use smccc::psci::{version, Version}; // Query PSCI version via SMC let psci_version: Result = version::(); match psci_version { Ok(v) => { // Version implements Display: "1.1" println!("PSCI version: {}", v); println!("Major: {}, Minor: {}", v.major, v.minor); // Compare versions let min_version = Version { major: 1, minor: 0 }; if v >= min_version { println!("PSCI 1.0+ supported"); } } Err(e) => println!("Failed to get version: {}", e), } // Query via HVC instead let psci_version: Result = version::(); ``` -------------------------------- ### Suspend CPU Execution Source: https://context7.com/google/smccc/llms.txt Suspend the current CPU core or a topology node to a low-power state. The CPU will resume execution at the specified entry point when woken. Supports both 64-bit and 32-bit interfaces. Ensure valid power states and entry point addresses are provided. ```rust use smccc::Smc; use smccc::psci::{cpu_suspend, cpu_suspend_32, Error}; // Suspend with 64-bit interface let power_state: u32 = 0x0001_0000; // Platform-specific power state let entry_point: u64 = 0x4008_0000; // Resume entry point let context_id: u64 = 0x1234; // Context passed on resume let result: Result<(), Error> = cpu_suspend::( power_state, entry_point, context_id, ); match result { Ok(()) => println!("Resumed from suspend"), Err(Error::InvalidParameters) => println!("Invalid power state or address"), Err(e) => println!("Suspend failed: {}", e), } // Using 32-bit interface let result = cpu_suspend_32::(0x0001_0000, 0x4008_0000, 0); ``` -------------------------------- ### Suspend System to RAM Source: https://context7.com/google/smccc/llms.txt Initiates a system suspend to RAM (S3 sleep state). Requires specifying an entry point and context ID for resuming. Handles potential errors like 'NotSupported' or 'InvalidAddress'. ```rust use smccc::Smc; use smccc::psci::{system_suspend, system_suspend_32, Error}; // Entry point and context for resume let entry_point: u64 = 0x4008_0000; let context_id: u64 = 0xABCD; // Suspend system to RAM let result: Result<(), Error> = system_suspend::(entry_point, context_id); match result { Ok(()) => println!("Resumed from system suspend"), Err(Error::NotSupported) => println!("System suspend not supported"), Err(Error::InvalidAddress) => println!("Invalid entry point address"), Err(e) => println!("Suspend failed: {}", e), } // 32-bit version let result = system_suspend_32::(0x4008_0000, 0); ``` -------------------------------- ### Query SMCCC Architecture Version Source: https://context7.com/google/smccc/llms.txt Fetches the implemented version of the SMC Calling Convention. This is distinct from the PSCI version. It allows checking for support of specific SMCCC features based on the version number. ```rust use smccc::Smc; use smccc::arch::{version, Version, Error}; // Get SMCCC version let result: Result = version::(); match result { Ok(v) => { println!("SMCCC version: {}", v); // e.g., "1.4" // Check for minimum SMCCC version let smccc_1_2 = Version { major: 1, minor: 2 }; if v >= smccc_1_2 { println!("SMCCC 1.2+ features available"); } } Err(Error::NotSupported) => println!("SMCCC_VERSION not supported (pre-1.0?)"), Err(e) => println!("Failed: {}", e), } ``` -------------------------------- ### Query Architecture Feature Support Source: https://context7.com/google/smccc/llms.txt Checks if a specific SMCCC architecture function is implemented by the firmware. Returns feature flags if supported, or specific error variants like 'NotSupported' or 'NotRequired'. ```rust use smccc::Smc; use smccc::arch::{features, Error, SMCCC_ARCH_SOC_ID, SMCCC_ARCH_WORKAROUND_1}; // Check if SOC_ID is supported let result: Result = features::(SMCCC_ARCH_SOC_ID); match result { Ok(feature_flags) => println!("SOC_ID supported, flags: {:#x}", feature_flags), Err(Error::NotSupported) => println!("SOC_ID not implemented"), Err(e) => println!("Query failed: {}", e), } // Check if Spectre workaround is available match features::(SMCCC_ARCH_WORKAROUND_1) { Ok(_) => println!("ARCH_WORKAROUND_1 available"), Err(Error::NotSupported) => println!("Workaround not needed or not available"), Err(Error::NotRequired) => println!("Workaround not required on this CPU"), Err(e) => println!("Error: {}", e), } ``` -------------------------------- ### CPU Suspend API Source: https://context7.com/google/smccc/llms.txt Suspends execution of a core or topology node to a low-power state. The CPU will resume at the specified entry point when woken. ```APIDOC ## CPU Suspend ### Description Suspend execution of a core or topology node to a low-power state. The CPU will resume at the specified entry point when woken. ### Method N/A (Function call) ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use smccc::Smc; use smccc::psci::{cpu_suspend, cpu_suspend_32, Error}; // Suspend with 64-bit interface let power_state: u32 = 0x0001_0000; // Platform-specific power state let entry_point: u64 = 0x4008_0000; // Resume entry point let context_id: u64 = 0x1234; // Context passed on resume let result: Result<(), Error> = cpu_suspend::( power_state, entry_point, context_id, ); match result { Ok(()) => println!("Resumed from suspend"), Err(Error::InvalidParameters) => println!("Invalid power state or address"), Err(e) => println!("Suspend failed: {}", e), } // Using 32-bit interface let result = cpu_suspend_32::(0x0001_0000, 0x4008_0000, 0); ``` ### Response #### Success Response (200) Resumes execution. The function returns `Ok(())` upon resuming. #### Response Example N/A (Resumes execution) ``` -------------------------------- ### Affinity Info Query API Source: https://context7.com/google/smccc/llms.txt Queries the power state of a CPU affinity instance (core, cluster, or higher topology level). ```APIDOC ## Affinity Info Query ### Description Query the power state of a CPU affinity instance (core, cluster, or higher topology level). ### Method N/A (Function call) ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use smccc::Smc; use smccc::psci::{affinity_info, affinity_info_32, AffinityState, LowestAffinityLevel, Error}; // Query affinity state of CPU core 1 let target_affinity: u64 = 0x0000_0001; // MPIDR of target let level = LowestAffinityLevel::All; // Check all affinity levels let result: Result = affinity_info::( target_affinity, level, ); match result { Ok(AffinityState::On) => println!("At least one core is ON"), Ok(AffinityState::Off) => println!("All cores are OFF"), Ok(AffinityState::OnPending) => println!("Transitioning to ON"), Err(e) => println!("Query failed: {}", e), } // Check cluster level (ignore Aff0) let cluster_state = affinity_info::( 0x0000_0100, // Cluster 1 LowestAffinityLevel::Aff0Ignored, ); // 32-bit version let state = affinity_info_32::(0x01, LowestAffinityLevel::All); ``` ### Response #### Success Response (200) Returns the `AffinityState` of the queried CPU affinity instance. #### Response Example ```json { "state": "On" } ``` ``` -------------------------------- ### Query SoC Identification Source: https://context7.com/google/smccc/llms.txt Retrieves System-on-Chip version and revision information. Requires specifying the type of ID to query (Version or Revision). Handles 'NotSupported' errors if the feature is unavailable. ```rust use smccc::Smc; use smccc::arch::{soc_id, SocIdType, Error}; // Get SoC version let result: Result = soc_id::(SocIdType::Version); match result { Ok(version) => println!("SoC version: {:#x}", version), Err(Error::NotSupported) => println!("SOC_ID not supported"), Err(e) => println!("Failed: {}", e), } // Get SoC revision let result = soc_id::(SocIdType::Revision); if let Ok(revision) = result { println!("SoC revision: {:#x}", revision); } ``` -------------------------------- ### Rust: Convert SMCCC Return Values to Results Source: https://context7.com/google/smccc/llms.txt Use `success_or_error_32` to convert a 32-bit SMCCC return value to a Rust Result. Use `positive_or_error_32` for calls that should return a positive value or an error. ```rust use smccc::{smc32, Smc, Call}; use smccc::error::{success_or_error_32, success_or_error_64, positive_or_error_32}; use smccc::psci::error::Error as PsciError; // Custom PSCI-like call with error handling fn my_custom_call(function: u32) -> Result<(), PsciError> { let result = C::call32(function, [0; 7])[0]; success_or_error_32(result) } // Call that returns a positive value or error fn query_value(function: u32, arg: u32) -> Result { let result = C::call32(function, [arg, 0, 0, 0, 0, 0, 0])[0]; positive_or_error_32(result) } // Usage match my_custom_call::(0x8400_0008) { Ok(()) => println!("Call succeeded"), Err(PsciError::NotSupported) => println!("Function not supported"), Err(PsciError::InvalidParameters) => println!("Bad parameters"), Err(PsciError::Unknown(code)) => println!("Unknown error: {}", code), Err(e) => println!("Error: {}", e), } ``` -------------------------------- ### Power Off CPU Core Source: https://context7.com/google/smccc/llms.txt Use this function to power down the calling CPU core. It does not return on success, indicating the core has been powered off. Handle errors for cases like attempting to power off the last running CPU. ```rust use smccc::Smc; use smccc::psci::{cpu_off, Error}; // Power off the current CPU // On success, this function does not return let result: Result<(), Error> = cpu_off::(); // Only reaches here on error match result { Err(Error::Denied) => println!("Cannot power off - last running CPU"), Err(e) => println!("Power off failed: {}", e), Ok(()) => unreachable!(), // Never returns Ok on success } ``` -------------------------------- ### Query CPU Affinity State Source: https://context7.com/google/smccc/llms.txt Query the power state of a CPU affinity instance (core, cluster, etc.). This function can check the state of specific cores or entire topology levels. It returns the current state (On, Off, OnPending) or an error if the query fails. Supports 64-bit and 32-bit interfaces. ```rust use smccc::Smc; use smccc::psci::{affinity_info, affinity_info_32, AffinityState, LowestAffinityLevel, Error}; // Query affinity state of CPU core 1 let target_affinity: u64 = 0x0000_0001; // MPIDR of target let level = LowestAffinityLevel::All; // Check all affinity levels let result: Result = affinity_info::( target_affinity, level, ); match result { Ok(AffinityState::On) => println!("At least one core is ON"), Ok(AffinityState::Off) => println!("All cores are OFF"), Ok(AffinityState::OnPending) => println!("Transitioning to ON"), Err(e) => println!("Query failed: {}", e), } // Check cluster level (ignore Aff0) let cluster_state = affinity_info::( 0x0000_0100, // Cluster 1 LowestAffinityLevel::Aff0Ignored, ); // 32-bit version let state = affinity_info_32::(0x01, LowestAffinityLevel::All); ``` -------------------------------- ### CPU Power Off API Source: https://context7.com/google/smccc/llms.txt Powers down the calling CPU core. This function does not return on success. ```APIDOC ## CPU Power Off ### Description Power down the calling CPU core. This function does not return on success. ### Method N/A (Function call) ### Endpoint N/A ### Parameters None ### Request Example ```rust use smccc::Smc; use smccc::psci::{cpu_off, Error}; // Power off the current CPU let result: Result<(), Error> = cpu_off::(); match result { Err(Error::Denied) => println!("Cannot power off - last running CPU"), Err(e) => println!("Power off failed: {}", e), Ok(()) => unreachable!(), // Never returns Ok on success } ``` ### Response #### Success Response Does not return on success. #### Response Example N/A ``` -------------------------------- ### PSCI Feature Query Source: https://context7.com/google/smccc/llms.txt Query whether a specific PSCI function is implemented and retrieve its feature flags. This is useful for determining runtime support for various PSCI capabilities. Use constants like `PSCI_CPU_SUSPEND_64` for querying specific features. ```rust use smccc::Smc; use smccc::psci::{psci_features, Error, PSCI_CPU_SUSPEND_64, PSCI_SYSTEM_RESET2_64}; // Check if CPU_SUSPEND is supported let result: Result = psci_features::(PSCI_CPU_SUSPEND_64); match result { Ok(features) => { println!("CPU_SUSPEND supported, features: {:#x}", features); // Feature bits are function-specific } Err(Error::NotSupported) => println!("CPU_SUSPEND not implemented"), Err(e) => println!("Query failed: {}", e), } // Check SYSTEM_RESET2 support if psci_features::(PSCI_SYSTEM_RESET2_64).is_ok() { println!("SYSTEM_RESET2 is available"); } ``` -------------------------------- ### Memory Protection API Source: https://context7.com/google/smccc/llms.txt Enables or disables and queries memory protection status. ```APIDOC ## Memory Protection ### Description Enable or disable and query memory protection status. ### Method N/A (Function call) ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use smccc::Smc; use smccc::psci::{mem_protect, mem_protect_check_range, Error}; // Enable memory protection (returns previous state) let result: Result = mem_protect::(true); match result { Ok(was_enabled) => println!("Memory protection enabled (was: {})", was_enabled), Err(Error::NotSupported) => println!("Memory protection not supported"), Err(e) => println!("Failed: {}", e), } // Check if a memory range is protected let base: u64 = 0x8000_0000; let length: u64 = 0x1000_0000; // 256 MB let result: Result<(), Error> = mem_protect_check_range::(base, length); match result { Ok(()) => println!("Memory range is protected"), Err(Error::Denied) => println!("Memory range is NOT protected"), Err(e) => println!("Check failed: {}", e), } ``` ### Response #### Success Response (200) For `mem_protect`: Returns `Ok(bool)` where `true` indicates memory protection was previously enabled. For `mem_protect_check_range`: Returns `Ok(())` if the memory range is protected. #### Response Example ```json { "status": "protected" } ``` ``` -------------------------------- ### Memory Protection Control Source: https://context7.com/google/smccc/llms.txt Enable, disable, or query the status of memory protection. `mem_protect` enables or disables protection and returns the previous state. `mem_protect_check_range` verifies if a given memory range is protected. Handle `NotSupported` and `Denied` errors appropriately. ```rust use smccc::Smc; use smccc::psci::{mem_protect, mem_protect_check_range, Error}; // Enable memory protection (returns previous state) let result: Result = mem_protect::(true); match result { Ok(was_enabled) => println!("Memory protection enabled (was: {})", was_enabled), Err(Error::NotSupported) => println!("Memory protection not supported"), Err(e) => println!("Failed: {}", e), } // Check if a memory range is protected let base: u64 = 0x8000_0000; let length: u64 = 0x1000_0000; // 256 MB let result: Result<(), Error> = mem_protect_check_range::(base, length); match result { Ok(()) => println!("Memory range is protected"), Err(Error::Denied) => println!("Memory range is NOT protected"), Err(e) => println!("Check failed: {}", e), } ``` -------------------------------- ### Generic SMC/HVC Calls with Call Trait Source: https://context7.com/google/smccc/llms.txt Utilize the `Call` trait with `Hvc` and `Smc` marker types for generic functions that can perform either hypervisor or secure monitor calls. This allows for code reuse across different calling conventions. ```rust use smccc::{Call, Hvc, Smc}; // Generic function that works with both HVC and SMC fn get_version() -> u32 { let result = C::call32(0x84000000, [0; 7]); result[0] } // Call using HVC (to hypervisor) let hvc_version = get_version::(); // Call using SMC (to secure firmware) let smc_version = get_version::(); // Using 64-bit calling convention fn call_64bit(function: u32, arg: u64) -> u64 { let mut args = [0u64; 17]; args[0] = arg; C::call64(function, args)[0] } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.