@@ -33,7 +33,11 @@ use crate::{
33
33
memory_region:: MemoryMapping ,
34
34
program:: BuiltinFunction ,
35
35
vm:: { get_runtime_environment_key, Config , ContextObject , EbpfVm } ,
36
- x86:: * ,
36
+ x86:: {
37
+ FenceType , X86IndirectAccess , X86Instruction ,
38
+ X86Register :: { self , * } ,
39
+ ARGUMENT_REGISTERS , CALLEE_SAVED_REGISTERS , CALLER_SAVED_REGISTERS ,
40
+ } ,
37
41
} ;
38
42
39
43
const MAX_EMPTY_PROGRAM_MACHINE_CODE_LENGTH : usize = 4096 ;
@@ -202,7 +206,7 @@ const ANCHOR_CALL_REG_UNSUPPORTED_INSTRUCTION: usize = 14;
202
206
const ANCHOR_TRANSLATE_MEMORY_ADDRESS : usize = 21 ;
203
207
const ANCHOR_COUNT : usize = 34 ; // Update me when adding or removing anchors
204
208
205
- const REGISTER_MAP : [ u8 ; 11 ] = [
209
+ const REGISTER_MAP : [ X86Register ; 11 ] = [
206
210
CALLER_SAVED_REGISTERS [ 0 ] , // RAX
207
211
ARGUMENT_REGISTERS [ 1 ] , // RSI
208
212
ARGUMENT_REGISTERS [ 2 ] , // RDX
@@ -217,11 +221,11 @@ const REGISTER_MAP: [u8; 11] = [
217
221
] ;
218
222
219
223
/// RDI: Used together with slot_in_vm()
220
- const REGISTER_PTR_TO_VM : u8 = ARGUMENT_REGISTERS [ 0 ] ;
224
+ const REGISTER_PTR_TO_VM : X86Register = ARGUMENT_REGISTERS [ 0 ] ;
221
225
/// R10: Program counter limit
222
- const REGISTER_INSTRUCTION_METER : u8 = CALLER_SAVED_REGISTERS [ 7 ] ;
226
+ const REGISTER_INSTRUCTION_METER : X86Register = CALLER_SAVED_REGISTERS [ 7 ] ;
223
227
/// R11: Scratch register
224
- const REGISTER_SCRATCH : u8 = CALLER_SAVED_REGISTERS [ 8 ] ;
228
+ const REGISTER_SCRATCH : X86Register = CALLER_SAVED_REGISTERS [ 8 ] ;
225
229
226
230
#[ derive( Copy , Clone , Debug ) ]
227
231
pub enum OperandSize {
@@ -233,10 +237,10 @@ pub enum OperandSize {
233
237
}
234
238
235
239
enum Value {
236
- Register ( u8 ) ,
237
- RegisterIndirect ( u8 , i32 , bool ) ,
238
- RegisterPlusConstant32 ( u8 , i32 , bool ) ,
239
- RegisterPlusConstant64 ( u8 , i64 , bool ) ,
240
+ Register ( X86Register ) ,
241
+ RegisterIndirect ( X86Register , i32 , bool ) ,
242
+ RegisterPlusConstant32 ( X86Register , i32 , bool ) ,
243
+ RegisterPlusConstant64 ( X86Register , i64 , bool ) ,
240
244
Constant64 ( i64 , bool ) ,
241
245
}
242
246
@@ -875,7 +879,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
875
879
}
876
880
877
881
#[ inline]
878
- fn emit_sanitized_load_immediate ( & mut self , destination : u8 , value : i64 ) {
882
+ fn emit_sanitized_load_immediate ( & mut self , destination : X86Register , value : i64 ) {
879
883
let lower_key = self . immediate_value_key as i32 as i64 ;
880
884
if value >= i32:: MIN as i64 && value <= i32:: MAX as i64 {
881
885
self . emit_ins ( X86Instruction :: load_immediate ( destination, value. wrapping_sub ( lower_key) ) ) ;
@@ -898,7 +902,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
898
902
}
899
903
900
904
#[ inline]
901
- fn emit_sanitized_alu ( & mut self , size : OperandSize , opcode : u8 , opcode_extension : u8 , destination : u8 , immediate : i64 ) {
905
+ fn emit_sanitized_alu ( & mut self , size : OperandSize , opcode : u8 , opcode_extension : u8 , destination : X86Register , immediate : i64 ) {
902
906
if self . should_sanitize_constant ( immediate) {
903
907
self . emit_sanitized_load_immediate ( REGISTER_SCRATCH , immediate) ;
904
908
self . emit_ins ( X86Instruction :: alu ( size, opcode, REGISTER_SCRATCH , destination, None ) ) ;
@@ -985,7 +989,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
985
989
self . emit_profile_instruction_count ( target_pc) ;
986
990
}
987
991
988
- fn emit_rust_call ( & mut self , target : Value , arguments : & [ Argument ] , result_reg : Option < u8 > ) {
992
+ fn emit_rust_call ( & mut self , target : Value , arguments : & [ Argument ] , result_reg : Option < X86Register > ) {
989
993
let mut saved_registers = CALLER_SAVED_REGISTERS . to_vec ( ) ;
990
994
if let Some ( reg) = result_reg {
991
995
if let Some ( dst) = saved_registers. iter ( ) . position ( |x| * x == reg) {
@@ -1014,7 +1018,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1014
1018
for argument in arguments {
1015
1019
let is_stack_argument = argument. index >= ARGUMENT_REGISTERS . len ( ) ;
1016
1020
let dst = if is_stack_argument {
1017
- u8 :: MAX // Never used
1021
+ RSP // Never used
1018
1022
} else {
1019
1023
ARGUMENT_REGISTERS [ argument. index ]
1020
1024
} ;
@@ -1029,7 +1033,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1029
1033
Value :: RegisterIndirect ( reg, offset, user_provided) => {
1030
1034
debug_assert ! ( !user_provided) ;
1031
1035
if is_stack_argument {
1032
- debug_assert_ne ! ( reg, RSP ) ;
1036
+ debug_assert ! ( reg != RSP ) ;
1033
1037
self . emit_ins ( X86Instruction :: push ( reg, Some ( X86IndirectAccess :: Offset ( offset) ) ) ) ;
1034
1038
} else if reg == RSP {
1035
1039
self . emit_ins ( X86Instruction :: load ( OperandSize :: S64 , RSP , dst, X86IndirectAccess :: OffsetIndexShift ( offset, RSP , 0 ) ) ) ;
@@ -1146,7 +1150,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1146
1150
}
1147
1151
1148
1152
#[ inline]
1149
- fn emit_address_translation ( & mut self , dst : Option < u8 > , vm_addr : Value , len : u64 , value : Option < Value > ) {
1153
+ fn emit_address_translation ( & mut self , dst : Option < X86Register > , vm_addr : Value , len : u64 , value : Option < Value > ) {
1150
1154
debug_assert_ne ! ( dst. is_some( ) , value. is_some( ) ) ;
1151
1155
1152
1156
let stack_slot_of_value_to_store = X86IndirectAccess :: OffsetIndexShift ( -112 , RSP , 0 ) ;
@@ -1217,7 +1221,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1217
1221
}
1218
1222
1219
1223
#[ inline]
1220
- fn emit_conditional_branch_reg ( & mut self , op : u8 , bitwise : bool , first_operand : u8 , second_operand : u8 , target_pc : usize ) {
1224
+ fn emit_conditional_branch_reg ( & mut self , op : u8 , bitwise : bool , first_operand : X86Register , second_operand : X86Register , target_pc : usize ) {
1221
1225
self . emit_validate_and_profile_instruction_count ( Some ( target_pc) ) ;
1222
1226
if bitwise { // Logical
1223
1227
self . emit_ins ( X86Instruction :: test ( OperandSize :: S64 , first_operand, second_operand, None ) ) ;
@@ -1231,7 +1235,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1231
1235
}
1232
1236
1233
1237
#[ inline]
1234
- fn emit_conditional_branch_imm ( & mut self , op : u8 , bitwise : bool , immediate : i64 , second_operand : u8 , target_pc : usize ) {
1238
+ fn emit_conditional_branch_imm ( & mut self , op : u8 , bitwise : bool , immediate : i64 , second_operand : X86Register , target_pc : usize ) {
1235
1239
self . emit_validate_and_profile_instruction_count ( Some ( target_pc) ) ;
1236
1240
if self . should_sanitize_constant ( immediate) {
1237
1241
self . emit_sanitized_load_immediate ( REGISTER_SCRATCH , immediate) ;
@@ -1251,7 +1255,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1251
1255
self . emit_undo_profile_instruction_count ( Value :: Constant64 ( target_pc as i64 , true ) ) ;
1252
1256
}
1253
1257
1254
- fn emit_shift ( & mut self , size : OperandSize , opcode_extension : u8 , source : u8 , destination : u8 , immediate : Option < i64 > ) {
1258
+ fn emit_shift ( & mut self , size : OperandSize , opcode_extension : u8 , source : X86Register , destination : X86Register , immediate : Option < i64 > ) {
1255
1259
if let Some ( immediate) = immediate {
1256
1260
if self . should_sanitize_constant ( immediate) {
1257
1261
self . emit_sanitized_load_immediate ( source, immediate) ;
@@ -1296,8 +1300,8 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1296
1300
alt_dst : bool ,
1297
1301
division : bool ,
1298
1302
signed : bool ,
1299
- src : u8 ,
1300
- dst : u8 ,
1303
+ src : X86Register ,
1304
+ dst : X86Register ,
1301
1305
imm : Option < i64 > ,
1302
1306
) {
1303
1307
// LMUL UHMUL SHMUL UDIV SDIV UREM SREM
@@ -1390,7 +1394,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
1390
1394
self . emit_ins ( X86Instruction :: store_immediate ( OperandSize :: S64 , REGISTER_MAP [ 0 ] , X86IndirectAccess :: Offset ( std:: mem:: size_of :: < u64 > ( ) as i32 ) , err_kind as i64 ) ) ; // err.kind = err_kind;
1391
1395
}
1392
1396
1393
- fn emit_result_is_err ( & mut self , destination : u8 ) {
1397
+ fn emit_result_is_err ( & mut self , destination : X86Register ) {
1394
1398
let ok = ProgramResult :: Ok ( 0 ) ;
1395
1399
let ok_discriminant = ok. discriminant ( ) ;
1396
1400
self . emit_ins ( X86Instruction :: lea ( OperandSize :: S64 , REGISTER_PTR_TO_VM , destination, Some ( X86IndirectAccess :: Offset ( self . slot_in_vm ( RuntimeEnvironmentSlot :: ProgramResult ) ) ) ) ) ;
0 commit comments