@@ -17,7 +17,7 @@ limitations under the License.
1717use alloc:: format;
1818use alloc:: string:: ToString ;
1919use alloc:: vec:: Vec ;
20- use core:: arch:: global_asm ;
20+ use core:: arch;
2121
2222use hyperlight_common:: flatbuffer_wrappers:: function_call:: { FunctionCall , FunctionCallType } ;
2323use hyperlight_common:: flatbuffer_wrappers:: function_types:: {
@@ -79,21 +79,31 @@ pub fn outb(port: u16, data: &[u8]) {
7979 unsafe {
8080 match RUNNING_MODE {
8181 RunMode :: Hypervisor => {
82- for chunk in data. chunks ( 4 ) {
83- // Process the data in chunks of 4 bytes. If a chunk has fewer than 4 bytes,
84- // pad it with 0x7F to ensure it can be converted into a 4-byte array.
85- // The choice of 0x7F as the padding value is arbitrary and does not carry
86- // any special meaning; it simply ensures consistent chunk size.
87- let val = match chunk {
88- [ a, b, c, d] => u32:: from_le_bytes ( [ * a, * b, * c, * d] ) ,
89- [ a, b, c] => u32:: from_le_bytes ( [ * a, * b, * c, 0x7F ] ) ,
90- [ a, b] => u32:: from_le_bytes ( [ * a, * b, 0x7F , 0x7F ] ) ,
91- [ a] => u32:: from_le_bytes ( [ * a, 0x7F , 0x7F , 0x7F ] ) ,
92- [ ] => break ,
93- _ => unreachable ! ( ) ,
94- } ;
95-
96- hloutd ( val, port) ;
82+ let mut i = 0 ;
83+ while i < data. len ( ) {
84+ let remaining = data. len ( ) - i;
85+ match remaining {
86+ 4 .. => {
87+ let val = u32:: from_le_bytes ( [
88+ data[ i] ,
89+ data[ i + 1 ] ,
90+ data[ i + 2 ] ,
91+ data[ i + 3 ] ,
92+ ] ) ;
93+ out32 ( port, val) ;
94+ i += 4 ;
95+ }
96+ 2 ..=3 => {
97+ let val = u16:: from_le_bytes ( [ data[ i] , data[ i + 1 ] ] ) ;
98+ out16 ( port, val) ;
99+ i += 2 ;
100+ }
101+ 1 => {
102+ out8 ( port, data[ i] ) ;
103+ i += 1 ;
104+ }
105+ _ => break ,
106+ }
97107 }
98108 }
99109 RunMode :: InProcessLinux | RunMode :: InProcessWindows => {
@@ -119,11 +129,33 @@ pub fn outb(port: u16, data: &[u8]) {
119129 }
120130}
121131
122- extern "win64" {
123- fn hloutd ( value : u32 , port : u16 ) ;
132+ pub ( crate ) unsafe fn out8 ( port : u16 , val : u8 ) {
133+ arch:: asm!( "out dx, al" , in( "dx" ) port, in( "al" ) val, options( preserves_flags, nomem, nostack) ) ;
134+ }
135+
136+ pub ( crate ) unsafe fn out16 ( port : u16 , val : u16 ) {
137+ arch:: asm!( "out dx, ax" , in( "dx" ) port, in( "ax" ) val, options( preserves_flags, nomem, nostack) ) ;
138+ }
139+
140+ pub ( crate ) unsafe fn out32 ( port : u16 , val : u32 ) {
141+ arch:: asm!( "out dx, eax" , in( "dx" ) port, in( "eax" ) val, options( preserves_flags, nomem, nostack) ) ;
142+ }
143+
144+ /// Prints a message using `OutBAction::DebugPrint`. It transmits bytes of a message
145+ /// through several VMExists and, with such, it is slower than
146+ /// `print_output_with_host_print`.
147+ ///
148+ /// This function should be used in debug mode only. This function does not
149+ /// require memory to be setup to be used.
150+ pub fn debug_print ( msg : & str ) {
151+ outb ( OutBAction :: DebugPrint as u16 , msg. as_bytes ( ) ) ;
124152}
125153
126- pub fn print_output_as_guest_function ( function_call : & FunctionCall ) -> Result < Vec < u8 > > {
154+ /// Print a message using the host's print function.
155+ ///
156+ /// This function requires memory to be setup to be used. In particular, the
157+ /// existence of the input and output memory regions.
158+ pub fn print_output_with_host_print ( function_call : & FunctionCall ) -> Result < Vec < u8 > > {
127159 if let ParameterValue :: String ( message) = function_call. parameters . clone ( ) . unwrap ( ) [ 0 ] . clone ( ) {
128160 call_host_function (
129161 "HostPrint" ,
@@ -135,20 +167,7 @@ pub fn print_output_as_guest_function(function_call: &FunctionCall) -> Result<Ve
135167 } else {
136168 Err ( HyperlightGuestError :: new (
137169 ErrorCode :: GuestError ,
138- "Wrong Parameters passed to print_output_as_guest_function " . to_string ( ) ,
170+ "Wrong Parameters passed to print_output_with_host_print " . to_string ( ) ,
139171 ) )
140172 }
141173}
142-
143- pub fn debug_print ( msg : & str ) {
144- outb ( OutBAction :: DebugPrint as u16 , msg. as_bytes ( ) ) ;
145- }
146-
147- global_asm ! (
148- ".global hloutd
149- hloutd:
150- mov eax, ecx
151- mov dx, dx
152- out dx, eax
153- ret"
154- ) ;
0 commit comments