acrn-hypervisor/hypervisor/arch/x86/trampoline.c

127 lines
3.4 KiB
C
Raw Normal View History

/*
* Copyright (C) 2018 Intel Corporation. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <types.h>
#include <mmu.h>
#include <per_cpu.h>
#include <trampoline.h>
#include <reloc.h>
restruct boot and bsp dir for firmware stuff currently, ACRN hypervisor can either boot from sbl/abl or uefi, that's why we have different firmware method under bsp & boot dirs. but the fact is that we actually have two different operations based on different guest boot mode: 1. de-privilege-boot: ACRN hypervisor will boot VM0 in the same context as native(before entering hypervisor) - it means hypervisor will co-work with ACRN UEFI bootloader, restore the context env and de-privilege this env to VM0 guest. 2. direct-boot: ACRN hypervisor will directly boot different pre-launched VM(including SOS), it will setup guest env by pre-defined configuration, and prepare guest kernel image, ramdisk which fetch from multiboot modules. this patch is trying to: - rename files related with firmware, change them to guest vboot related - restruct all guest boot stuff in boot & bsp dirs into a new boot/guest dir - use de-privilege & direct boot to distinguish two different boot operations this patch is pure file movement, the rename of functions based on old assumption will be in the following patch. Changes to be committed: modified: ../efi-stub/Makefile modified: ../efi-stub/boot.c modified: Makefile modified: arch/x86/cpu.c modified: arch/x86/guest/vm.c modified: arch/x86/init.c modified: arch/x86/irq.c modified: arch/x86/trampoline.c modified: boot/acpi.c renamed: bsp/cmdline.c -> boot/cmdline.c renamed: bsp/firmware_uefi.c -> boot/guest/deprivilege_boot.c renamed: boot/uefi/uefi_boot.c -> boot/guest/deprivilege_boot_info.c renamed: bsp/firmware_sbl.c -> boot/guest/direct_boot.c renamed: boot/sbl/multiboot.c -> boot/guest/direct_boot_info.c renamed: bsp/firmware_wrapper.c -> boot/guest/vboot_wrapper.c modified: boot/include/acpi.h renamed: bsp/include/firmware_uefi.h -> boot/include/guest/deprivilege_boot.h renamed: bsp/include/firmware_sbl.h -> boot/include/guest/direct_boot.h renamed: bsp/include/firmware.h -> boot/include/guest/vboot.h modified: include/arch/x86/multiboot.h Tracked-On: #1842 Signed-off-by: Jason Chen CJ <jason.cj.chen@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
2019-04-30 13:56:32 +08:00
#include <vboot.h>
#include <ld_sym.h>
static uint64_t trampoline_start16_paddr;
/*
* Because trampoline code is relocated in different way, if HV code
* accesses trampoline using relative addressing, it needs to take
* out the HV relocation delta
*
* This function is valid if:
* - The hpa of HV code is always higher than trampoline code
* - The HV code is always relocated to higher address, compared
* with CONFIG_HV_RAM_START
*/
static uint64_t trampoline_relo_addr(const void *addr)
{
return (uint64_t)addr - get_hv_image_delta();
}
uint64_t read_trampoline_sym(const void *sym)
{
uint64_t *hva = (uint64_t *)(hpa2hva(trampoline_start16_paddr) + trampoline_relo_addr(sym));
return *hva;
}
void write_trampoline_sym(const void *sym, uint64_t val)
{
uint64_t *hva = (uint64_t *)(hpa2hva(trampoline_start16_paddr) + trampoline_relo_addr(sym));
*hva = val;
clflush(hva);
}
void write_trampoline_stack_sym(uint16_t pcpu_id)
{
uint64_t *hva, stack_sym_addr;
hva = (uint64_t *)(hpa2hva(trampoline_start16_paddr) + trampoline_relo_addr(secondary_cpu_stack));
stack_sym_addr = (uint64_t)&per_cpu(stack, pcpu_id)[CONFIG_STACK_SIZE - 1];
stack_sym_addr &= ~(CPU_STACK_ALIGN - 1UL);
*hva = stack_sym_addr;
clflush(hva);
}
uint64_t get_trampoline_start16_paddr(void)
{
return trampoline_start16_paddr;
}
/*
* @pre pcpu_has_cap(X86_FEATURE_PAGE1GB) == true
*/
static void update_trampoline_code_refs(uint64_t dest_pa)
{
void *ptr;
uint64_t val;
/*
* calculate the fixup CS:IP according to fixup target address
* dynamically.
*
* trampoline code starts in real mode,
* so the target addres is HPA
*/
val = dest_pa + trampoline_relo_addr(&trampoline_fixup_target);
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_fixup_cs));
*(uint16_t *)(ptr) = (uint16_t)((val >> 4U) & 0xFFFFU);
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_fixup_ip));
*(uint16_t *)(ptr) = (uint16_t)(val & 0xfU);
/* Update temporary page tables */
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&cpu_boot_page_tables_ptr));
*(uint32_t *)(ptr) += (uint32_t)dest_pa;
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&cpu_boot_page_tables_start));
*(uint64_t *)(ptr) += dest_pa;
/* update the gdt base pointer with relocated offset */
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_gdt_ptr));
*(uint64_t *)(ptr + 2) += dest_pa;
/* update trampoline jump pointer with relocated offset */
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_start64_fixup));
*(uint32_t *)ptr += (uint32_t)dest_pa;
/* update trampoline's main entry pointer */
ptr = hpa2hva(dest_pa + trampoline_relo_addr(main_entry));
*(uint64_t *)ptr += get_hv_image_delta();
}
uint64_t prepare_trampoline(void)
{
uint64_t size, dest_pa, i;
size = (uint64_t)(&ld_trampoline_end - &ld_trampoline_start);
dest_pa = get_ap_trampoline_buf();
pr_dbg("trampoline code: %lx size %x", dest_pa, size);
/* Copy segment for AP initialization code below 1MB */
(void)memcpy_s(hpa2hva(dest_pa), (size_t)size, &ld_trampoline_load,
(size_t)size);
update_trampoline_code_refs(dest_pa);
for (i = 0UL; i < size; i = i + CACHE_LINE_SIZE) {
clflush(hpa2hva(dest_pa + i));
}
trampoline_start16_paddr = dest_pa;
return dest_pa;
}