Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] spin-table support #5

Merged
merged 2 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/common/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,11 @@ pub fn invalidate_data_cache(virtual_address: usize) {
unsafe { asm!("DC IVAC, {:x}", in(reg) virtual_address) };
}

#[inline(always)]
pub fn clean_and_invalidate_data_cache(virtual_address: usize) {
unsafe { asm!("DC CIVAC, {:x}", in(reg) virtual_address) };
}

#[inline(always)]
pub fn send_event_all() {
unsafe { asm!("SEV") };
Expand Down
1 change: 1 addition & 0 deletions src/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub struct SystemInformation {
usize, /* base_address */
usize, /* number of pages */
),
pub spin_table_info: Option<(usize /* base_address */, usize /* length */)>,
pub memory_save_list: *mut [MemorySaveListEntry],
pub serial_port: Option<SerialPortInfo>,
pub ecam_info: Option<EcamInfo>,
Expand Down
35 changes: 13 additions & 22 deletions src/hypervisor_bootloader/src/dtb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,30 +465,21 @@ impl DtbNode {
self.address_offset
}

#[allow(dead_code)]
pub fn get_reg(&self, dtb: &DtbAnalyser, index: usize) -> Result<(usize, usize), ()> {
pub fn get_prop_as_u32(
&self,
prop_name: &[u8],
dtb: &DtbAnalyser,
) -> Result<Option<&[u32]>, ()> {
let mut s = self.clone();
if let Some((mut p, property_len)) = s.search_pointer_to_property(PROP_REG, dtb)? {
let offset = index * ((self.address_cells + self.size_cells) as usize) * TOKEN_SIZE;
if offset >= property_len as usize {
return Err(());
}
p += offset;
let mut address = 0usize;
let mut size = 0usize;
for _ in 0..self.address_cells {
address <<= u32::BITS;
address |= u32::from_be(unsafe { *(p as *const u32) }) as usize;
p += TOKEN_SIZE;
}
for _ in 0..self.size_cells {
size <<= u32::BITS;
size |= u32::from_be(unsafe { *(p as *const u32) }) as usize;
p += TOKEN_SIZE;
}
return Ok((address, size));
if let Some((p, len)) = s.search_pointer_to_property(prop_name, dtb)? {
Ok(Some(unsafe {
core::slice::from_raw_parts(
p as *const u32,
len as usize / core::mem::size_of::<u32>(),
)
}))
} else {
Err(())
Ok(None)
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions src/hypervisor_bootloader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ extern "C" fn efi_main(image_handle: EfiHandle, system_table: *mut EfiSystemTabl
None
};

let spin_table_info = if let Some(dtb_address) = unsafe { DTB_ADDRESS } {
detect_spin_table(dtb_address)
} else {
None
};

#[cfg(feature = "smmu")]
let smmu_v3_base_address = if let Some(acpi_address) = unsafe { ACPI_20_TABLE_ADDRESS } {
smmu::detect_smmu(acpi_address)
Expand Down Expand Up @@ -155,6 +161,7 @@ extern "C" fn efi_main(image_handle: EfiHandle, system_table: *mut EfiSystemTabl
acpi_rsdp_address: unsafe { ACPI_20_TABLE_ADDRESS },
vbar_el2: 0,
available_memory_info: unsafe { MEMORY_ALLOCATOR.assume_init_mut().get_all_memory() },
spin_table_info,
memory_save_list,
serial_port: serial,
ecam_info,
Expand Down Expand Up @@ -303,6 +310,41 @@ pub fn free_memory(address: usize, pages: usize) -> Result<(), MemoryAllocationE
}
}

/// Detect spin table
///
/// When device tree is available, this function searches "cpu" node and check "cpu-release-addr".
/// When "cpu-release-addr" exists, secondary processors are enabled by spin-table,
/// This finds area of spin-table(this function assumes "cpu-release-addr" is continued linearly.)
fn detect_spin_table(dtb_address: usize) -> Option<(usize /* Base Address */, usize /* Length */)> {
let dtb_analyzer = dtb::DtbAnalyser::new(dtb_address).unwrap();
let mut search_holder = dtb_analyzer.get_root_node().get_search_holder().unwrap();
let Ok(Some(cpu_node)) = search_holder.search_next_device_by_node_name(b"cpu", &dtb_analyzer)
else {
pr_debug!("Failed to find CPU node");
return None;
};
let Ok(Some(release_addr)) = cpu_node.get_prop_as_u32(b"cpu-release-addr", &dtb_analyzer)
else {
pr_debug!("Faiked to find cpu-release-addr");
return None;
};
let base_address = ((u32::from_be(release_addr[0]) as usize) << u32::BITS)
| (u32::from_be(release_addr[1]) as usize);
let mut length = core::mem::size_of::<u64>();
while let Ok(Some(node)) = search_holder.search_next_device_by_node_name(b"cpu", &dtb_analyzer)
{
let Ok(Some(release_addr)) = node.get_prop_as_u32(b"cpu-release-addr", &dtb_analyzer)
else {
return None;
};
let release_address = ((u32::from_be(release_addr[0]) as usize) << u32::BITS)
| (u32::from_be(release_addr[1]) as usize);
length = release_address + core::mem::size_of::<u64>() - base_address;
pr_debug!("CPU Release Address: {:#X}", release_address);
}
Some((base_address, length))
}

/// Load hypervisor_kernel to [`common::HYPERVISOR_VIRTUAL_BASE_ADDRESS`]
///
/// This function loads hypervisor_kernel according to ELF header.
Expand Down
4 changes: 4 additions & 0 deletions src/hypervisor_kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ fn hypervisor_main(system_information: &mut SystemInformation) {
);
}

if let Some((spin_table_address, length)) = system_information.spin_table_info {
multi_core::setup_spin_table(spin_table_address, length);
}

#[cfg(feature = "acpi_table_protection")]
if let Some(rsdp_address) = unsafe { ACPI_RSDP } {
acpi_protect::init_table_protection(rsdp_address);
Expand Down
Loading