HV: Make AP trampoline code relocatable

V3->V4: Updated function/variable names for accurancy
V2->V3: Changed a few function/variable names to make it less confusing
V1->V2: removed the unneccesary cache flushing

- For UEFI boot, allocate memory for trampoline code in ACRN EFI,
  and pass the pointer to HV through efi_ctx
- For other boot, scan E820 to allocate memory in HV run time
- update_trampoline_code_refs() updates all the references that need the
  absolute PA with the actual load address

Signed-off-by: Zheng, Gen <gen.zheng@intel.com>
Signed-off-by: Jason Chen CJ <jason.cj.chen@intel.com>
Signed-off-by: Zide Chen <zide.chen@intel.com>
Acked-by: Eddie Dong <eddie.dong>
Acked-by: Xu, Anthony <anthony.xu@intel.com>
This commit is contained in:
Zide Chen
2018-05-02 23:39:57 -07:00
committed by Jack Ren
parent ceb3076b69
commit fe6397d9d8
6 changed files with 79 additions and 18 deletions

View File

@@ -30,6 +30,7 @@
#include <hypervisor.h>
#include <hv_lib.h>
#include <acrn_hv_defs.h>
#include <acrn_common.h>
#include <bsp_extern.h>
#include <hv_arch.h>
@@ -38,9 +39,12 @@
#include <hv_debug.h>
#ifdef CONFIG_EFI_STUB
#include <acrn_efi.h>
extern uint32_t efi_physical_available_ap_bitmap;
#endif
uint64_t trampoline_code_paddr = 0;
spinlock_t cpu_secondary_spinlock = {
.head = 0,
.tail = 0
@@ -88,6 +92,7 @@ int cpu_find_logical_id(uint32_t lapic_id);
#ifndef CONFIG_EFI_STUB
static void start_cpus(void);
#endif
static uint64_t relocate_trampoline_code(void);
static void pcpu_sync_sleep(unsigned long *sync, int mask_bit);
int ibrs_type;
@@ -548,10 +553,7 @@ void bsp_boot_init(void)
/* Trigger event to allow secondary CPUs to continue */
bitmap_set(0, &pcpu_sync);
#else
memcpy_s(_ld_cpu_secondary_reset_start,
(unsigned long)&_ld_cpu_secondary_reset_size,
_ld_cpu_secondary_reset_load,
(unsigned long)&_ld_cpu_secondary_reset_size);
relocate_trampoline_code();
#endif
ASSERT(get_cpu_id() == CPU_BOOT_ID, "");
@@ -660,12 +662,9 @@ static void start_cpus()
{
uint32_t timeout;
uint32_t expected_up;
uint64_t startup_paddr;
/*Copy segment for AP initialization code below 1MB */
memcpy_s(_ld_cpu_secondary_reset_start,
(unsigned long)&_ld_cpu_secondary_reset_size,
_ld_cpu_secondary_reset_load,
(unsigned long)&_ld_cpu_secondary_reset_size);
startup_paddr = relocate_trampoline_code();
/* Set flag showing number of CPUs expected to be up to all
* cpus
@@ -674,7 +673,7 @@ static void start_cpus()
/* Broadcast IPIs to all other CPUs */
send_startup_ipi(INTR_CPU_STARTUP_ALL_EX_SELF,
-1U, ((uint64_t) cpu_secondary_reset));
-1U, startup_paddr);
/* Wait until global count is equal to expected CPU up count or
* configured time-out has expired
@@ -700,6 +699,53 @@ static void start_cpus()
}
#endif
static void update_trampoline_code_refs(uint64_t dest_pa)
{
void *ptr;
int i;
if (dest_pa == 0)
return;
/* Update temporary page tables */
ptr = HPA2HVA(dest_pa + (uint64_t)CPU_Boot_Page_Tables_ptr);
*(uint32_t *)(ptr) += dest_pa;
ptr = HPA2HVA(dest_pa + (uint64_t)CPU_Boot_Page_Tables_Start);
*(uint64_t *)(ptr) += dest_pa;
ptr = HPA2HVA(dest_pa + (uint64_t)cpu_secondary_pdpt_addr);
for (i = 0; i < 4; i++ ) {
*(uint64_t *)(ptr + sizeof(uint64_t) * i) += dest_pa;
}
/* update the gdt base pointer with relocated offset */
ptr = HPA2HVA(dest_pa + (uint64_t)cpu_secondary_gdt_ptr);
*(uint64_t *)(ptr + 2) += dest_pa;
/* update trampoline jump pointer with relocated offset */
ptr = HPA2HVA(dest_pa + (uint64_t)ap_long_mode_jump_ref);
*(uint32_t *)ptr += dest_pa;
}
static uint64_t relocate_trampoline_code(void)
{
uint64_t size, dest_pa;
size = (uint64_t)_ld_cpu_secondary_reset_end - (uint64_t)cpu_secondary_reset;
#ifndef CONFIG_EFI_STUB
dest_pa = e820_alloc_low_memory(CONFIG_LOW_RAM_SIZE);
#else
dest_pa = (uint64_t)get_ap_trampoline_buf();
#endif
/* printf("AP trampoline code: %llx size %x\n", dest_pa, size); */
memcpy_s(HPA2HVA(dest_pa), size, _ld_cpu_secondary_reset_load, size);
update_trampoline_code_refs(dest_pa);
trampoline_code_paddr = dest_pa;
return dest_pa;
}
void cpu_dead(uint32_t logical_id)
{
/* For debug purposes, using a stack variable in the while loop enables