Skip to content

Commit d80e4ce

Browse files
committed
Replace float point with integers
1 parent 62e5500 commit d80e4ce

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4753,6 +4753,7 @@ dependencies = [
47534753
"sha2",
47544754
"sidecar_defs",
47554755
"tdcall",
4756+
"test_with_tracing",
47564757
"underhill_confidentiality",
47574758
"x86defs",
47584759
"zerocopy 0.8.23",

openhcl/openhcl_boot/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ minimal_rt_build.workspace = true
3737

3838
[lints]
3939
workspace = true
40+
41+
[dev-dependencies]
42+
test_with_tracing.workspace = true

openhcl/openhcl_boot/src/dt.rs

-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::ReservedMemoryType;
99
use crate::host_params::COMMAND_LINE_SIZE;
1010
use crate::host_params::PartitionInfo;
1111
use crate::host_params::shim_params::IsolationType;
12-
use crate::log; // YSP
1312
use crate::sidecar::SidecarConfig;
1413
use crate::single_threaded::off_stack;
1514
use arrayvec::ArrayString;
@@ -212,7 +211,6 @@ pub fn write_dt(
212211
let p_enable_method = builder.add_string("enable-method")?;
213212

214213
let num_cpus = partition_info.cpus.len();
215-
log!("YSP: num_cpus2 = {}", num_cpus);
216214

217215
let mut root_builder = builder
218216
.start_node("")?

openhcl/openhcl_boot/src/host_params/dma_hint.rs

+39-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
//! Calculate DMA hint value if not provided by host.
55
6-
use crate::log;
76
use igvm_defs::{MemoryMapEntryType, PAGE_SIZE_4K};
87
use super::PartitionInfo;
98

@@ -60,10 +59,14 @@ const LOOKUP_TABLE: &'static [DmaLookupStruct] = &[
6059
},
6160
];
6261

62+
/// Round up to next 2MiB.
63+
fn round_up_to_2mb(pages_4k: u64) -> u64 {
64+
(pages_4k + 511) & !(511)
65+
}
66+
6367
/// Returns calculated DMA hint value, in 4k pages.
6468
pub fn vtl2_calculate_dma_hint(vp_count: usize, storage: &PartitionInfo) -> u64 {
6569
let mut dma_hint_4k = 0;
66-
log!("YSP: vp_count = {}", vp_count);
6770
let mem_size = storage
6871
.vtl2_ram
6972
.iter()
@@ -73,16 +76,16 @@ pub fn vtl2_calculate_dma_hint(vp_count: usize, storage: &PartitionInfo) -> u64
7376
// Sanity check for the calculated memory size.
7477
if mem_size > 0 && mem_size < 0xFFFFFFFF00000 {
7578
let mem_size_mb = (mem_size / 1048576) as u32;
76-
log!("YSP: mem_size_mb = {}", mem_size_mb);
7779

7880
let mut min_vtl2_memory_mb = 1000000;
7981
let mut max_vtl2_memory_mb = 0;
80-
// TODO: If we won't allow floats in boot-shim, replace with scaled integers
81-
let mut min_ratio: f32 = 0.1;
82-
let mut max_ratio: f32 = 0.01;
82+
83+
// To avoid using floats, scale ratios to 1:1000.
84+
let mut min_ratio_1000th = 100000;
85+
let mut max_ratio_1000th = 1000;
8386

8487
let mut min_vp_count = 1;
85-
let mut max_vp_count = 4096;
88+
let mut max_vp_count = vp_count as u32;
8689

8790
for f in LOOKUP_TABLE {
8891
if f.vp_count == vp_count as u32 {
@@ -94,8 +97,8 @@ pub fn vtl2_calculate_dma_hint(vp_count: usize, storage: &PartitionInfo) -> u64
9497
// Prepare for possible extrapolation.
9598
min_vtl2_memory_mb = min_vtl2_memory_mb.min(f.vtl2_memory_mb);
9699
max_vtl2_memory_mb = max_vtl2_memory_mb.max(f.vtl2_memory_mb);
97-
min_ratio = min_ratio.min(f.dma_hint_mb as f32 / f.vtl2_memory_mb as f32);
98-
max_ratio = max_ratio.max(f.dma_hint_mb as f32 / f.vtl2_memory_mb as f32);
100+
min_ratio_1000th = min_ratio_1000th.min(f.vtl2_memory_mb as u32 * 1000 / f.dma_hint_mb as u32);
101+
max_ratio_1000th = max_ratio_1000th.max(f.vtl2_memory_mb as u32 * 1000 / f.dma_hint_mb as u32);
99102
}
100103
} else if f.vp_count < vp_count as u32 {
101104
// Find the nearest VP counts if exact match is not in the table.
@@ -112,19 +115,20 @@ pub fn vtl2_calculate_dma_hint(vp_count: usize, storage: &PartitionInfo) -> u64
112115
.iter()
113116
.filter(|e| e.vp_count == min_vp_count || e.vp_count == max_vp_count)
114117
.for_each(|f| {
115-
// Prepare for possible extrapolation.
116118
min_vtl2_memory_mb = min_vtl2_memory_mb.min(f.vtl2_memory_mb);
117119
max_vtl2_memory_mb = max_vtl2_memory_mb.max(f.vtl2_memory_mb);
118-
min_ratio = min_ratio.min(f.dma_hint_mb as f32 / f.vtl2_memory_mb as f32);
119-
max_ratio = max_ratio.max(f.dma_hint_mb as f32 / f.vtl2_memory_mb as f32);
120+
min_ratio_1000th = min_ratio_1000th.min(f.vtl2_memory_mb as u32 * 1000 / f.dma_hint_mb as u32);
121+
max_ratio_1000th = max_ratio_1000th.max(f.vtl2_memory_mb as u32 * 1000 / f.dma_hint_mb as u32);
120122
});
121123
}
122-
124+
123125
if dma_hint_4k == 0 {
124126
// If we didn't find an exact match for our vp_count, try to extrapolate.
125-
dma_hint_4k = (mem_size_mb as f32 * ((min_ratio + max_ratio) / 2.0)) as u64 *
126-
1048576 /
127-
PAGE_SIZE_4K;
127+
dma_hint_4k = (mem_size_mb as u64 * 1000u64 * (1048576u64 / PAGE_SIZE_4K)) /
128+
((min_ratio_1000th + max_ratio_1000th) as u64 / 2u64);
129+
130+
// And then round up to 2MiB.
131+
dma_hint_4k = round_up_to_2mb(dma_hint_4k);
128132
}
129133
}
130134

@@ -136,6 +140,7 @@ mod test {
136140
use super::*;
137141
use crate::host_params::MemoryEntry;
138142
use crate::MemoryRange;
143+
use test_with_tracing::test;
139144

140145
#[test]
141146
fn test_vtl2_calculate_dma_hint() {
@@ -164,7 +169,23 @@ mod test {
164169
mem_type: MemoryMapEntryType::VTL2_PROTECTABLE,
165170
vnode: 0,
166171
});
167-
// TODO: Unfinished, maybe the return value is incorrect.
168-
assert_eq!(vtl2_calculate_dma_hint(112, &storage), 2048);
172+
assert_eq!(vtl2_calculate_dma_hint(112, &storage), 2560);
173+
174+
// Test unusual VP count.
175+
storage.vtl2_ram.clear();
176+
storage.vtl2_ram.push(MemoryEntry {
177+
range: MemoryRange::new(0x0..0x6000000),
178+
mem_type: MemoryMapEntryType::VTL2_PROTECTABLE,
179+
vnode: 0,
180+
});
181+
assert_eq!(vtl2_calculate_dma_hint(52, &storage), 2048);
182+
183+
storage.vtl2_ram.clear();
184+
storage.vtl2_ram.push(MemoryEntry {
185+
range: MemoryRange::new(0x0..0x8000000),
186+
mem_type: MemoryMapEntryType::VTL2_PROTECTABLE,
187+
vnode: 0,
188+
});
189+
assert_eq!(vtl2_calculate_dma_hint(52, &storage), 2560);
169190
}
170191
}

openhcl/openhcl_boot/src/host_params/dt.rs

-4
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ impl PartitionInfo {
338338
let parsed = ParsedDeviceTree::parse(dt, &mut *dt_storage).map_err(DtError::DeviceTree)?;
339339

340340
let command_line = params.command_line();
341-
log!("YSP: device_dma_page_count {}", parsed.device_dma_page_count.unwrap_or(0));
342341

343342
// Always write the measured command line.
344343
write!(
@@ -360,7 +359,6 @@ impl PartitionInfo {
360359

361360
match parsed.memory_allocation_mode {
362361
MemoryAllocationMode::Host => {
363-
log!("YSP: MemoryAllocationMode::Host");
364362
storage.vtl2_ram.clear();
365363
storage
366364
.vtl2_ram
@@ -372,7 +370,6 @@ impl PartitionInfo {
372370
memory_size,
373371
mmio_size,
374372
} => {
375-
log!("YSP: MemoryAllocationMode::Vtl2");
376373
storage.vtl2_ram.clear();
377374
storage
378375
.vtl2_ram
@@ -515,7 +512,6 @@ impl PartitionInfo {
515512

516513
storage.vtl2_pool_memory = pool;
517514
}
518-
log!("YSP: vtl2_gpa_pool_size {}", vtl2_gpa_pool_size);
519515

520516
// If we can trust the host, use the provided alias map
521517
if can_trust_host {

0 commit comments

Comments
 (0)