Skip to content

Commit 7eb076a

Browse files
JBYoshiJonathanWoollett-Light
authored andcommitted
ARM64: Decrease the sizes of some values
These values can be changed to smaller types without causing problems. I've done this in a separate commit because my dev setup doesn't use ARM. In case something ARM-specific breaks in CI, I'd like to be able to keep those changes in their own commit so I can debug more easily. Signed-off-by: Jonathan Browne <[email protected]>
1 parent 2212c9f commit 7eb076a

File tree

5 files changed

+28
-28
lines changed

5 files changed

+28
-28
lines changed

src/vmm/src/arch/aarch64/cache_info.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub(crate) struct CacheEntry {
3737
pub level: u8,
3838
// Type of cache: Unified, Data, Instruction.
3939
pub type_: CacheType,
40-
pub size_: Option<usize>,
40+
pub size_: Option<u32>,
4141
pub number_of_sets: Option<u16>,
4242
pub line_size: Option<u16>,
4343
// How many CPUS share this cache.
@@ -211,12 +211,12 @@ fn readln_special<T: AsRef<Path>>(file_path: &T) -> Result<String, CacheInfoErro
211211
Ok(line.trim_end().to_string())
212212
}
213213

214-
fn to_bytes(cache_size_pretty: &mut String) -> Result<usize, CacheInfoError> {
214+
fn to_bytes(cache_size_pretty: &mut String) -> Result<u32, CacheInfoError> {
215215
match cache_size_pretty.pop() {
216-
Some('K') => Ok(cache_size_pretty.parse::<usize>().map_err(|err| {
216+
Some('K') => Ok(cache_size_pretty.parse::<u32>().map_err(|err| {
217217
CacheInfoError::InvalidCacheAttr("size".to_string(), err.to_string())
218218
})? * 1024),
219-
Some('M') => Ok(cache_size_pretty.parse::<usize>().map_err(|err| {
219+
Some('M') => Ok(cache_size_pretty.parse::<u32>().map_err(|err| {
220220
CacheInfoError::InvalidCacheAttr("size".to_string(), err.to_string())
221221
})? * 1024
222222
* 1024),

src/vmm/src/arch/aarch64/fdt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn create_cpu_nodes(fdt: &mut FdtWriter, vcpu_mpidr: &[u64]) -> Result<(), FdtEr
142142
// https://github.com/devicetree-org/devicetree-specification/releases/download/v0.3/devicetree-specification-v0.3.pdf,
143143
// section 3.8.
144144
if let Some(size) = cache.size_ {
145-
fdt.property_u32(cache.type_.of_cache_size(), size as u32)?;
145+
fdt.property_u32(cache.type_.of_cache_size(), size)?;
146146
}
147147
if let Some(line_size) = cache.line_size {
148148
fdt.property_u32(cache.type_.of_cache_line_size(), u32::from(line_size))?;
@@ -189,7 +189,7 @@ fn create_cpu_nodes(fdt: &mut FdtWriter, vcpu_mpidr: &[u64]) -> Result<(), FdtEr
189189
fdt.property_string("compatible", "cache")?;
190190
fdt.property_u32("cache-level", u32::from(cache.level))?;
191191
if let Some(size) = cache.size_ {
192-
fdt.property_u32(cache.type_.of_cache_size(), size as u32)?;
192+
fdt.property_u32(cache.type_.of_cache_size(), size)?;
193193
}
194194
if let Some(line_size) = cache.line_size {
195195
fdt.property_u32(cache.type_.of_cache_line_size(), u32::from(line_size))?;

src/vmm/src/arch/aarch64/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
/// Start of RAM on 64 bit ARM.
5353
pub const DRAM_MEM_START: u64 = 0x8000_0000; // 2 GB.
5454
/// The maximum RAM size.
55-
pub const DRAM_MEM_MAX_SIZE: u64 = 0x00FF_8000_0000; // 1024 - 2 = 1022G.
55+
pub const DRAM_MEM_MAX_SIZE: usize = 0x00FF_8000_0000; // 1024 - 2 = 1022G.
5656

5757
/// Kernel command line maximum size.
5858
/// As per `arch/arm64/include/uapi/asm/setup.h`.

src/vmm/src/arch/aarch64/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub const MMIO_MEM_SIZE: u64 = layout::DRAM_MEM_START - layout::MAPPED_IO_START;
4040
/// Returns a Vec of the valid memory addresses for aarch64.
4141
/// See [`layout`](layout) module for a drawing of the specific memory model for this platform.
4242
pub fn arch_memory_regions(size: usize) -> Vec<(GuestAddress, usize)> {
43-
let dram_size = min(size as u64, layout::DRAM_MEM_MAX_SIZE) as usize;
43+
let dram_size = min(size, layout::DRAM_MEM_MAX_SIZE);
4444
vec![(GuestAddress(layout::DRAM_MEM_START), dram_size)]
4545
}
4646

src/vmm/src/arch/aarch64/regs.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,27 +136,27 @@ pub enum RegSize {
136136

137137
impl RegSize {
138138
/// Size of u8 register in bytes
139-
pub const U8_SIZE: u64 = 1;
139+
pub const U8_SIZE: usize = 1;
140140
/// Size of u16 register in bytes
141-
pub const U16_SIZE: u64 = 2;
141+
pub const U16_SIZE: usize = 2;
142142
/// Size of u32 register in bytes
143-
pub const U32_SIZE: u64 = 4;
143+
pub const U32_SIZE: usize = 4;
144144
/// Size of u64 register in bytes
145-
pub const U64_SIZE: u64 = 8;
145+
pub const U64_SIZE: usize = 8;
146146
/// Size of u128 register in bytes
147-
pub const U128_SIZE: u64 = 16;
147+
pub const U128_SIZE: usize = 16;
148148
/// Size of u256 register in bytes
149-
pub const U256_SIZE: u64 = 32;
149+
pub const U256_SIZE: usize = 32;
150150
/// Size of u512 register in bytes
151-
pub const U512_SIZE: u64 = 64;
151+
pub const U512_SIZE: usize = 64;
152152
/// Size of u1024 register in bytes
153-
pub const U1024_SIZE: u64 = 128;
153+
pub const U1024_SIZE: usize = 128;
154154
/// Size of u2048 register in bytes
155-
pub const U2048_SIZE: u64 = 256;
155+
pub const U2048_SIZE: usize = 256;
156156
}
157157

158-
impl From<u64> for RegSize {
159-
fn from(value: u64) -> Self {
158+
impl From<usize> for RegSize {
159+
fn from(value: usize) -> Self {
160160
match value {
161161
RegSize::U8_SIZE => RegSize::U8,
162162
RegSize::U16_SIZE => RegSize::U16,
@@ -172,7 +172,7 @@ impl From<u64> for RegSize {
172172
}
173173
}
174174

175-
impl From<RegSize> for u64 {
175+
impl From<RegSize> for usize {
176176
fn from(value: RegSize) -> Self {
177177
match value {
178178
RegSize::U8 => RegSize::U8_SIZE,
@@ -189,8 +189,8 @@ impl From<RegSize> for u64 {
189189
}
190190

191191
/// Returns register size in bytes
192-
pub fn reg_size(reg_id: u64) -> u64 {
193-
2_u64.pow(((reg_id & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT) as u32)
192+
pub fn reg_size(reg_id: u64) -> usize {
193+
2_usize.pow(((reg_id & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT) as u32)
194194
}
195195

196196
/// Storage for aarch64 registers with different sizes.
@@ -294,7 +294,7 @@ impl Versionize for Aarch64RegisterVec {
294294
Self: Sized,
295295
{
296296
let inner = Aarch64RegisterVecInner::deserialize(reader, version_map, source_version)?;
297-
let mut total_size: u64 = 0;
297+
let mut total_size: usize = 0;
298298
for id in inner.ids.iter() {
299299
let reg_size = reg_size(*id);
300300
if RegSize::U2048_SIZE < reg_size {
@@ -306,7 +306,7 @@ impl Versionize for Aarch64RegisterVec {
306306
}
307307
total_size += reg_size;
308308
}
309-
if total_size as usize != inner.data.len() {
309+
if total_size != inner.data.len() {
310310
Err(VersionizeError::Deserialize(
311311
"Failed to deserialize aarch64 registers. Sum of registers sizes is not equal to \
312312
registers data length"
@@ -337,7 +337,7 @@ impl<'a> Iterator for Aarch64RegisterVecIterator<'a> {
337337
fn next(&mut self) -> Option<Self::Item> {
338338
if self.index < self.ids.len() {
339339
let id = self.ids[self.index];
340-
let reg_size = reg_size(id) as usize;
340+
let reg_size = reg_size(id);
341341
let reg_ref = Aarch64RegisterRef {
342342
id,
343343
data: &self.data[self.offset..self.offset + reg_size],
@@ -366,7 +366,7 @@ impl<'a> Iterator for Aarch64RegisterVecIteratorMut<'a> {
366366
fn next(&mut self) -> Option<Self::Item> {
367367
if self.index < self.ids.len() {
368368
let id = self.ids[self.index];
369-
let reg_size = reg_size(id) as usize;
369+
let reg_size = reg_size(id);
370370

371371
let data = std::mem::take(&mut self.data);
372372
let (head, tail) = data.split_at_mut(reg_size);
@@ -396,7 +396,7 @@ impl<'a> Aarch64RegisterRef<'a> {
396396
/// will panic.
397397
pub fn new(id: u64, data: &'a [u8]) -> Self {
398398
assert_eq!(
399-
reg_size(id) as usize,
399+
reg_size(id),
400400
data.len(),
401401
"Attempt to create a register reference with incompatible id and data length"
402402
);
@@ -438,7 +438,7 @@ impl<'a> Aarch64RegisterRefMut<'a> {
438438
/// will panic.
439439
pub fn new(id: u64, data: &'a mut [u8]) -> Self {
440440
assert_eq!(
441-
reg_size(id) as usize,
441+
reg_size(id),
442442
data.len(),
443443
"Attempt to create a register reference with incompatible id and data length"
444444
);

0 commit comments

Comments
 (0)