Skip to content

Commit

Permalink
td-shim: use fixed-size array for ACPI table installation
Browse files Browse the repository at this point in the history
Replace the vector with a fixed-array to record the table offsets
of ACPI tables. The maximum number of ACPI tables is 128 according to
Linux Kernel implementation.

Signed-off-by: Jiaqi Gao <[email protected]>
  • Loading branch information
gaojiaqi7 committed May 27, 2024
1 parent 2c5c43c commit 2d2ed06
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions td-shim/src/bin/td-shim/acpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@
// SPDX-License-Identifier: BSD-2-Clause-Patent
extern crate alloc;

use alloc::vec::Vec;
use td_shim_interface::acpi::{calculate_checksum, Rsdp, Xsdt};

use super::*;

#[derive(Default)]
const ACPI_MAX_TABLES: usize = 128;

pub struct AcpiTables<'a> {
acpi_memory: &'a mut [u8],
pa: u64,
size: usize,
fadt: Option<(usize, usize)>, // FADT offset in acpi memory
dsdt: Option<usize>, // DSDT offset in acpi memory
table_offset: Vec<usize>,
table_offsets: [usize; ACPI_MAX_TABLES],
num_tables: usize,
}

impl<'a> AcpiTables<'a> {
pub fn new(td_acpi_mem: &'a mut [u8], pa: u64) -> Self {
AcpiTables {
acpi_memory: td_acpi_mem,
pa,
..Default::default()
size: 0,
fadt: None,
dsdt: None,
table_offsets: [0; ACPI_MAX_TABLES],
num_tables: 0,
}
}

Expand Down Expand Up @@ -53,7 +58,7 @@ impl<'a> AcpiTables<'a> {
.expect("Unable to add table into XSDT");
}

for offset in &self.table_offset {
for offset in &self.table_offsets[..self.num_tables] {
xsdt.add_table(self.offset_to_address(*offset))
.expect("Unable to add table into XSDT");
}
Expand Down Expand Up @@ -102,7 +107,7 @@ impl<'a> AcpiTables<'a> {
} else if &header.signature == b"DSDT" {
self.dsdt = Some(self.size);
} else {
for offset in &self.table_offset {
for offset in &self.table_offsets[..self.num_tables] {
// Safe because it's reading data from our own buffer.
let table_header = GenericSdtHeader::read_from(
&self.acpi_memory[*offset..*offset + size_of::<GenericSdtHeader>()],
Expand All @@ -116,7 +121,11 @@ impl<'a> AcpiTables<'a> {
return;
}
}
self.table_offset.push(self.size);
if self.num_tables >= ACPI_MAX_TABLES {
panic!("Number of ACPI table exceeds limit 0x{:X}", ACPI_MAX_TABLES,);
}
self.table_offsets[self.num_tables] = self.size;
self.num_tables += 1;
}

self.acpi_memory[self.size..self.size + table.len()].copy_from_slice(table);
Expand Down

0 comments on commit 2d2ed06

Please sign in to comment.