@@ -17,6 +17,8 @@ limitations under the License.
1717use std:: cmp:: max;
1818use std:: time:: Duration ;
1919
20+ #[ cfg( target_os = "linux" ) ]
21+ use libc:: c_int;
2022use tracing:: { instrument, Span } ;
2123
2224use crate :: mem:: exe:: ExeInfo ;
@@ -63,6 +65,14 @@ pub struct SandboxConfiguration {
6365 /// signal can be delivered to the thread, but the thread may not yet
6466 /// have entered kernel space.
6567 interrupt_retry_delay : Duration ,
68+ /// Offset from `SIGRTMIN` used to determine the signal number for interrupting
69+ /// the VCPU thread. The actual signal sent is `SIGRTMIN + interrupt_vcpu_sigrtmin_offset`.
70+ ///
71+ /// This signal must fall within the valid real-time signal range supported by the host.
72+ ///
73+ /// Note: Since real-time signals can vary across platforms, ensure that the offset
74+ /// results in a signal number that is not already in use by other components of the system.
75+ interrupt_vcpu_sigrtmin_offset : u8 ,
6676}
6777
6878impl SandboxConfiguration {
@@ -76,6 +86,8 @@ impl SandboxConfiguration {
7686 pub const MIN_OUTPUT_SIZE : usize = 0x2000 ;
7787 /// The default interrupt retry delay
7888 pub const DEFAULT_INTERRUPT_RETRY_DELAY : Duration = Duration :: from_micros ( 500 ) ;
89+ /// The default signal offset from `SIGRTMIN` used to determine the signal number for interrupting
90+ pub const INTERRUPT_VCPU_SIGRTMIN_OFFSET : u8 = 0 ;
7991
8092 #[ allow( clippy:: too_many_arguments) ]
8193 /// Create a new configuration for a sandbox with the given sizes.
@@ -86,6 +98,7 @@ impl SandboxConfiguration {
8698 stack_size_override : Option < u64 > ,
8799 heap_size_override : Option < u64 > ,
88100 interrupt_retry_delay : Duration ,
101+ interrupt_vcpu_sigrtmin_offset : u8 ,
89102 #[ cfg( gdb) ] guest_debug_info : Option < DebugInfo > ,
90103 ) -> Self {
91104 Self {
@@ -94,7 +107,7 @@ impl SandboxConfiguration {
94107 stack_size_override : stack_size_override. unwrap_or ( 0 ) ,
95108 heap_size_override : heap_size_override. unwrap_or ( 0 ) ,
96109 interrupt_retry_delay,
97-
110+ interrupt_vcpu_sigrtmin_offset ,
98111 #[ cfg( gdb) ]
99112 guest_debug_info,
100113 }
@@ -136,6 +149,31 @@ impl SandboxConfiguration {
136149 self . interrupt_retry_delay
137150 }
138151
152+ /// Get the signal offset from `SIGRTMIN` used to determine the signal number for interrupting the VCPU thread
153+ #[ cfg( target_os = "linux" ) ]
154+ pub fn get_interrupt_vcpu_sigrtmin_offset ( & self ) -> u8 {
155+ self . interrupt_vcpu_sigrtmin_offset
156+ }
157+
158+ /// Sets the offset from `SIGRTMIN` to determine the real-time signal used for
159+ /// interrupting the VCPU thread.
160+ ///
161+ /// The final signal number is computed as `SIGRTMIN + offset`, and it must fall within
162+ /// the valid range of real-time signals supported by the host system.
163+ ///
164+ /// Returns Ok(()) if the offset is valid, or an error if it exceeds the maximum real-time signal number.
165+ #[ cfg( target_os = "linux" ) ]
166+ pub fn set_interrupt_vcpu_sigrtmin_offset ( & mut self , offset : u8 ) -> crate :: Result < ( ) > {
167+ if libc:: SIGRTMIN ( ) + offset as c_int > libc:: SIGRTMAX ( ) {
168+ return Err ( crate :: new_error!(
169+ "Invalid SIGRTMIN offset: {}. It exceeds the maximum real-time signal number." ,
170+ offset
171+ ) ) ;
172+ }
173+ self . interrupt_vcpu_sigrtmin_offset = offset;
174+ Ok ( ( ) )
175+ }
176+
139177 /// Sets the configuration for the guest debug
140178 #[ cfg( gdb) ]
141179 #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
@@ -195,6 +233,7 @@ impl Default for SandboxConfiguration {
195233 None ,
196234 None ,
197235 Self :: DEFAULT_INTERRUPT_RETRY_DELAY ,
236+ Self :: INTERRUPT_VCPU_SIGRTMIN_OFFSET ,
198237 #[ cfg( gdb) ]
199238 None ,
200239 )
@@ -218,6 +257,7 @@ mod tests {
218257 Some ( STACK_SIZE_OVERRIDE ) ,
219258 Some ( HEAP_SIZE_OVERRIDE ) ,
220259 SandboxConfiguration :: DEFAULT_INTERRUPT_RETRY_DELAY ,
260+ SandboxConfiguration :: INTERRUPT_VCPU_SIGRTMIN_OFFSET ,
221261 #[ cfg( gdb) ]
222262 None ,
223263 ) ;
@@ -244,6 +284,7 @@ mod tests {
244284 None ,
245285 None ,
246286 SandboxConfiguration :: DEFAULT_INTERRUPT_RETRY_DELAY ,
287+ SandboxConfiguration :: INTERRUPT_VCPU_SIGRTMIN_OFFSET ,
247288 #[ cfg( gdb) ]
248289 None ,
249290 ) ;
0 commit comments