Skip to content

Commit ea4cb64

Browse files
Copilotsangho2
andcommitted
Update bitfield usage and remove wrapper methods per review feedback
Co-authored-by: sangho2 <3938640+sangho2@users.noreply.github.com>
1 parent e9bc9ae commit ea4cb64

File tree

5 files changed

+56
-140
lines changed

5 files changed

+56
-140
lines changed

litebox_platform_lvbs/src/mshv/hvcall.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ pub fn init() -> Result<(), HypervError> {
129129
debug_serial_println!("HV_X64_MSR_SIMP: {:#x}", rdmsr(HV_X64_MSR_SIMP));
130130

131131
let mut sint = HvSynicSint::new();
132-
sint.set_vector_u64(u64::from(HYPERVISOR_CALLBACK_VECTOR));
133-
sint.set_auto_eoi_flag();
132+
sint.set_vector((HYPERVISOR_CALLBACK_VECTOR as u8).into());
133+
sint.set_auto_eoi(true);
134134

135135
wrmsr(HV_X64_MSR_SINT0, sint.as_uint64());
136136
if get_core_id() == 0 {

litebox_platform_lvbs/src/mshv/hvcall_vp.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,33 +142,33 @@ fn hv_vtl_populate_vp_context(input: &mut HvEnableVpVtl, tss: u64, rip: u64, rsp
142142
// We only support 64-bit long mode for now, so most of the segment register fields are ignored.
143143
input.vp_context.cs.selector = SegmentSelector::new(1, PrivilegeLevel::Ring0).0;
144144
input.vp_context.cs.set_attributes(
145-
SegmentRegisterAttributeFlags::ACCESSED.bits()
146-
| SegmentRegisterAttributeFlags::WRITABLE.bits()
147-
| SegmentRegisterAttributeFlags::EXECUTABLE.bits()
148-
| SegmentRegisterAttributeFlags::USER_SEGMENT.bits()
149-
| SegmentRegisterAttributeFlags::PRESENT.bits()
150-
| SegmentRegisterAttributeFlags::AVAILABLE.bits()
151-
| SegmentRegisterAttributeFlags::LONG_MODE.bits(),
145+
SegmentRegisterAttributeFlags::ACCESSED
146+
| SegmentRegisterAttributeFlags::WRITABLE
147+
| SegmentRegisterAttributeFlags::EXECUTABLE
148+
| SegmentRegisterAttributeFlags::USER_SEGMENT
149+
| SegmentRegisterAttributeFlags::PRESENT
150+
| SegmentRegisterAttributeFlags::AVAILABLE
151+
| SegmentRegisterAttributeFlags::LONG_MODE,
152152
);
153153

154154
input.vp_context.ss.selector = SegmentSelector::new(2, PrivilegeLevel::Ring0).0;
155155
input.vp_context.ss.set_attributes(
156-
SegmentRegisterAttributeFlags::ACCESSED.bits()
157-
| SegmentRegisterAttributeFlags::WRITABLE.bits()
158-
| SegmentRegisterAttributeFlags::USER_SEGMENT.bits()
159-
| SegmentRegisterAttributeFlags::PRESENT.bits()
160-
| SegmentRegisterAttributeFlags::AVAILABLE.bits(),
156+
SegmentRegisterAttributeFlags::ACCESSED
157+
| SegmentRegisterAttributeFlags::WRITABLE
158+
| SegmentRegisterAttributeFlags::USER_SEGMENT
159+
| SegmentRegisterAttributeFlags::PRESENT
160+
| SegmentRegisterAttributeFlags::AVAILABLE,
161161
);
162162

163163
input.vp_context.tr.selector = SegmentSelector::new(3, PrivilegeLevel::Ring0).0;
164164
input.vp_context.tr.base = tss;
165165
input.vp_context.tr.limit =
166166
u32::try_from(core::mem::size_of::<TaskStateSegment>()).unwrap() - 1;
167167
input.vp_context.tr.set_attributes(
168-
SegmentRegisterAttributeFlags::ACCESSED.bits()
169-
| SegmentRegisterAttributeFlags::WRITABLE.bits()
170-
| SegmentRegisterAttributeFlags::EXECUTABLE.bits()
171-
| SegmentRegisterAttributeFlags::PRESENT.bits(),
168+
SegmentRegisterAttributeFlags::ACCESSED
169+
| SegmentRegisterAttributeFlags::WRITABLE
170+
| SegmentRegisterAttributeFlags::EXECUTABLE
171+
| SegmentRegisterAttributeFlags::PRESENT,
172172
);
173173
}
174174

litebox_platform_lvbs/src/mshv/mod.rs

Lines changed: 30 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ bitflags::bitflags! {
142142
}
143143

144144
bitflags::bitflags! {
145-
#[derive(Debug, PartialEq)]
145+
#[derive(Debug, PartialEq, Clone, Copy, Default)]
146146
pub struct SegmentRegisterAttributeFlags: u16 {
147147
const ACCESSED = 1 << 0;
148148
const WRITABLE = 1 << 1;
@@ -166,13 +166,7 @@ pub struct HvX64SegmentRegister {
166166
pub base: u64,
167167
pub limit: u32,
168168
pub selector: u16,
169-
170-
_attributes: u16,
171-
// union of
172-
// segment_type: 4, non_system_segment: 1,
173-
// descriptor_privilege_level: 2, present: 1,
174-
// reserved: 4, available: 1, _long: 1,
175-
// _default: 1, granularity: 1
169+
pub attributes: SegmentRegisterAttributeFlags,
176170
}
177171

178172
impl HvX64SegmentRegister {
@@ -183,9 +177,12 @@ impl HvX64SegmentRegister {
183177
}
184178
}
185179

186-
#[expect(clippy::used_underscore_binding)]
187-
pub fn set_attributes(&mut self, attrs: u16) {
188-
self._attributes = attrs;
180+
pub fn set_attributes(&mut self, attrs: SegmentRegisterAttributeFlags) {
181+
self.attributes = attrs;
182+
}
183+
184+
pub fn get_attributes(&self) -> SegmentRegisterAttributeFlags {
185+
self.attributes
189186
}
190187
}
191188

@@ -259,11 +256,6 @@ impl HvInputVtl {
259256
pub fn target_vtl_value(&self) -> u8 {
260257
self.target_vtl() as u8
261258
}
262-
263-
/// Sets the target VTL from a u8 value
264-
pub fn set_target_vtl_value(&mut self, target_vtl: u8) {
265-
self.set_target_vtl(target_vtl.into());
266-
}
267259
}
268260

269261
#[derive(Default, Clone, Copy)]
@@ -390,13 +382,6 @@ pub struct HvNestedEnlightenmentsControlFeatures {
390382
__: B31,
391383
}
392384

393-
impl HvNestedEnlightenmentsControlFeatures {
394-
/// Set direct hypercall flag from u32 (for compatibility)
395-
pub fn set_direct_hypercall_u32(&mut self, direct_hypercall: u32) {
396-
self.set_direct_hypercall(direct_hypercall != 0);
397-
}
398-
}
399-
400385
#[bitfield]
401386
#[derive(Clone, Copy, Default)]
402387
#[repr(C)]
@@ -406,13 +391,6 @@ pub struct HvNestedEnlightenmentsControlHypercallControls {
406391
__: B31,
407392
}
408393

409-
impl HvNestedEnlightenmentsControlHypercallControls {
410-
/// Set inter partition comm flag from u32 (for compatibility)
411-
pub fn set_inter_partition_comm_u32(&mut self, inter_partition_comm: u32) {
412-
self.set_inter_partition_comm(inter_partition_comm != 0);
413-
}
414-
}
415-
416394
#[expect(non_snake_case)]
417395
#[derive(Default, Clone, Copy)]
418396
#[repr(C, packed)]
@@ -523,16 +501,6 @@ impl HvRegisterVsmVpSecureVtlConfig {
523501
pub fn as_u64(&self) -> u64 {
524502
u64::from_le_bytes(self.into_bytes())
525503
}
526-
527-
/// Enable MBEC (for compatibility)
528-
pub fn set_mbec_enabled_flag(&mut self) {
529-
self.set_mbec_enabled(true);
530-
}
531-
532-
/// Enable TLB locked (for compatibility)
533-
pub fn set_tlb_locked_flag(&mut self) {
534-
self.set_tlb_locked(true);
535-
}
536504
}
537505

538506
#[bitfield]
@@ -565,11 +533,6 @@ impl HvRegisterVsmPartitionConfig {
565533
Self::from_bytes(value.to_le_bytes())
566534
}
567535

568-
/// Set the default VTL protection mask from a u64 value
569-
pub fn set_default_vtl_protection_mask_value(&mut self, mask: u64) {
570-
self.set_default_vtl_protection_mask((mask as u8).into());
571-
}
572-
573536
/// Get the default VTL protection mask as a u64 value
574537
pub fn default_vtl_protection_mask_value(&self) -> u64 {
575538
self.default_vtl_protection_mask() as u64
@@ -756,26 +719,6 @@ impl HvSynicSint {
756719
pub fn as_uint64(&self) -> u64 {
757720
u64::from_le_bytes(self.into_bytes())
758721
}
759-
760-
/// Set vector from u64 (for compatibility)
761-
pub fn set_vector_u64(&mut self, vector: u64) {
762-
self.set_vector((vector as u8).into());
763-
}
764-
765-
/// Set masked flag (for compatibility)
766-
pub fn set_masked_flag(&mut self) {
767-
self.set_masked(true);
768-
}
769-
770-
/// Set auto_eoi flag (for compatibility)
771-
pub fn set_auto_eoi_flag(&mut self) {
772-
self.set_auto_eoi(true);
773-
}
774-
775-
/// Set polling flag (for compatibility)
776-
pub fn set_polling_flag(&mut self) {
777-
self.set_polling(true);
778-
}
779722
}
780723

781724
#[derive(Default, Clone, Copy)]
@@ -868,31 +811,6 @@ impl HvPendingExceptionEvent {
868811
pub fn as_u64(&self) -> u64 {
869812
u64::from_le_bytes(self.into_bytes())
870813
}
871-
872-
/// Set event pending flag (for compatibility)
873-
pub fn set_event_pending_flag(&mut self) {
874-
self.set_event_pending(true);
875-
}
876-
877-
/// Set event type from u64 (for compatibility)
878-
pub fn set_event_type_u64(&mut self, event_type: u64) {
879-
self.set_event_type((event_type as u8).into());
880-
}
881-
882-
/// Set deliver error code flag (for compatibility)
883-
pub fn set_deliver_error_code_flag(&mut self) {
884-
self.set_deliver_error_code(true);
885-
}
886-
887-
/// Set vector from u64 (for compatibility)
888-
pub fn set_vector_u64(&mut self, vector: u64) {
889-
self.set_vector((vector as u16).into());
890-
}
891-
892-
/// Set error code from u64 (for compatibility)
893-
pub fn set_error_code_u64(&mut self, error_code: u64) {
894-
self.set_error_code((error_code as u32).into());
895-
}
896814
}
897815

898816
#[cfg(test)]
@@ -914,7 +832,7 @@ mod tests {
914832

915833
// Test individual field manipulation
916834
let mut vtl = HvInputVtl::new();
917-
vtl.set_target_vtl_value(10);
835+
vtl.set_target_vtl(10_u8.into());
918836
vtl.set_use_target_vtl(true);
919837
assert_eq!(vtl.target_vtl_value(), 10);
920838
assert_eq!(vtl.use_target_vtl(), true);
@@ -949,7 +867,7 @@ mod tests {
949867
assert_eq!(config.intercept_page(), true);
950868

951869
// Test the 4-bit protection mask field
952-
config.set_default_vtl_protection_mask_value(0b1010);
870+
config.set_default_vtl_protection_mask((0b1010_u8).into());
953871
assert_eq!(config.default_vtl_protection_mask_value(), 0b1010);
954872

955873
// Test size - should be 8 bytes (64 bits)
@@ -991,12 +909,12 @@ mod tests {
991909
features.set_direct_hypercall(true);
992910
assert_eq!(features.direct_hypercall(), true);
993911

994-
// Test compatibility method
912+
// Test direct method
995913
let mut features2 = HvNestedEnlightenmentsControlFeatures::new();
996-
features2.set_direct_hypercall_u32(1);
914+
features2.set_direct_hypercall(true);
997915
assert_eq!(features2.direct_hypercall(), true);
998916

999-
features2.set_direct_hypercall_u32(0);
917+
features2.set_direct_hypercall(false);
1000918
assert_eq!(features2.direct_hypercall(), false);
1001919

1002920
// Test size - should be 4 bytes (32 bits)
@@ -1017,12 +935,12 @@ mod tests {
1017935
controls.set_inter_partition_comm(true);
1018936
assert_eq!(controls.inter_partition_comm(), true);
1019937

1020-
// Test compatibility method
938+
// Test direct method
1021939
let mut controls2 = HvNestedEnlightenmentsControlHypercallControls::new();
1022-
controls2.set_inter_partition_comm_u32(1);
940+
controls2.set_inter_partition_comm(true);
1023941
assert_eq!(controls2.inter_partition_comm(), true);
1024942

1025-
controls2.set_inter_partition_comm_u32(0);
943+
controls2.set_inter_partition_comm(false);
1026944
assert_eq!(controls2.inter_partition_comm(), false);
1027945

1028946
// Test size - should be 4 bytes (32 bits)
@@ -1046,12 +964,12 @@ mod tests {
1046964
config.set_tlb_locked(true);
1047965
assert_eq!(config.tlb_locked(), true);
1048966

1049-
// Test compatibility methods
967+
// Test direct methods
1050968
let mut config2 = HvRegisterVsmVpSecureVtlConfig::new();
1051-
config2.set_mbec_enabled_flag();
969+
config2.set_mbec_enabled(true);
1052970
assert_eq!(config2.mbec_enabled(), true);
1053971

1054-
config2.set_tlb_locked_flag();
972+
config2.set_tlb_locked(true);
1055973
assert_eq!(config2.tlb_locked(), true);
1056974

1057975
// Test size - should be 8 bytes (64 bits)
@@ -1088,18 +1006,18 @@ mod tests {
10881006
sint.set_polling(true);
10891007
assert_eq!(sint.polling(), true);
10901008

1091-
// Test compatibility methods
1009+
// Test direct methods
10921010
let mut sint2 = HvSynicSint::new();
1093-
sint2.set_vector_u64(0xf3);
1011+
sint2.set_vector((0xf3_u8).into());
10941012
assert_eq!(sint2.vector() as u8, 0xf3);
10951013

1096-
sint2.set_masked_flag();
1014+
sint2.set_masked(true);
10971015
assert_eq!(sint2.masked(), true);
10981016

1099-
sint2.set_auto_eoi_flag();
1017+
sint2.set_auto_eoi(true);
11001018
assert_eq!(sint2.auto_eoi(), true);
11011019

1102-
sint2.set_polling_flag();
1020+
sint2.set_polling(true);
11031021
assert_eq!(sint2.polling(), true);
11041022

11051023
// Test size - should be 8 bytes (64 bits)
@@ -1140,21 +1058,21 @@ mod tests {
11401058
exception.set_error_code(0x87654321_u32.into()); // 32 bits
11411059
assert_eq!(exception.error_code() as u32, 0x87654321);
11421060

1143-
// Test compatibility methods
1061+
// Test direct methods
11441062
let mut exception2 = HvPendingExceptionEvent::new();
1145-
exception2.set_event_pending_flag();
1063+
exception2.set_event_pending(true);
11461064
assert_eq!(exception2.event_pending(), true);
11471065

1148-
exception2.set_deliver_error_code_flag();
1066+
exception2.set_deliver_error_code(true);
11491067
assert_eq!(exception2.deliver_error_code(), true);
11501068

1151-
exception2.set_event_type_u64(7);
1069+
exception2.set_event_type((7_u8).into());
11521070
assert_eq!(exception2.event_type() as u8, 7);
11531071

1154-
exception2.set_vector_u64(0xabcd);
1072+
exception2.set_vector((0xabcd_u16).into());
11551073
assert_eq!(exception2.vector() as u16, 0xabcd);
11561074

1157-
exception2.set_error_code_u64(0x12345678);
1075+
exception2.set_error_code((0x12345678_u32).into());
11581076
assert_eq!(exception2.error_code() as u32, 0x12345678);
11591077

11601078
// Test size - should be 8 bytes (64 bits)

litebox_platform_lvbs/src/mshv/vsm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ pub fn mshv_vsm_secure_config_vtl0() -> Result<i64, Errno> {
167167
debug_serial_println!("VSM: Secure VTL0 configuration");
168168

169169
let mut config = HvRegisterVsmVpSecureVtlConfig::new();
170-
config.set_mbec_enabled_flag();
171-
config.set_tlb_locked_flag();
170+
config.set_mbec_enabled(true);
171+
config.set_tlb_locked(true);
172172

173173
hvcall_set_vp_registers(HV_REGISTER_VSM_VP_SECURE_CONFIG_VTL0, config.as_u64())
174174
.map_err(|_| Errno::EFAULT)?;
@@ -181,7 +181,7 @@ pub fn mshv_vsm_configure_partition() -> Result<i64, Errno> {
181181
debug_serial_println!("VSM: Configure partition");
182182

183183
let mut config = HvRegisterVsmPartitionConfig::new();
184-
config.set_default_vtl_protection_mask_value(HvPageProtFlags::HV_PAGE_FULL_ACCESS.bits().into());
184+
config.set_default_vtl_protection_mask((HvPageProtFlags::HV_PAGE_FULL_ACCESS.bits() as u8).into());
185185
config.set_enable_vtl_protection(true);
186186

187187
hvcall_set_vp_registers(HV_REGISTER_VSM_PARTITION_CONFIG, config.as_u64())

litebox_platform_lvbs/src/mshv/vsm_intercept.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,11 @@ fn advance_vtl0_rip(int_msg_hdr: &HvInterceptMessageHeader) -> Result<u64, Hyper
147147
#[inline]
148148
fn raise_vtl0_gp_fault() -> Result<u64, HypervCallError> {
149149
let mut exception = HvPendingExceptionEvent::new();
150-
exception.set_event_pending_flag();
151-
exception.set_event_type_u64(0);
152-
exception.set_deliver_error_code_flag();
153-
exception.set_vector_u64(u64::from(
154-
x86_64::structures::idt::ExceptionVector::GeneralProtection as u8,
155-
));
156-
exception.set_error_code_u64(0);
150+
exception.set_event_pending(true);
151+
exception.set_event_type(0_u8.into());
152+
exception.set_deliver_error_code(true);
153+
exception.set_vector((x86_64::structures::idt::ExceptionVector::GeneralProtection as u8 as u16).into());
154+
exception.set_error_code(0_u32.into());
157155

158156
hvcall_set_vp_vtl0_registers(HV_REGISTER_PENDING_EVENT0, exception.as_u64())
159157
}

0 commit comments

Comments
 (0)