acrn-hypervisor/hypervisor/arch/x86/guest/ucode.c
Junjie Mao f691cab994 HV: treewide: terminate 'if .. else if' constructs with 'else'
MISRA C requires that a 'if' statement followed by one or more 'else if'
statement shall be terminated by an 'else' statement which contains either
side-effect or a comment, to ensure that conditions are considered
exhaustively.

Note that a simple 'if' statement is not required to be terminated by 'else'.

This patch fixes such violations by either refactoring the code or add the
'else' statement with either a comment (describing why this case can be skipped)
or logging the event. It may not be satisfactory for the release version where
logging is no-op, but properly handling these non-trivial cases is out of the
scope of this patch.

v1 -> v2:

    * Fix unintended semantic changes in add_(msix|intx)_remapping and
      io_instr_vmexit_handler.
    * Simplify boolean checks in vpic_ocw2.
    * Rephrase the comment in strtol_deci.

Signed-off-by: Junjie Mao <junjie.mao@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
2018-07-19 14:09:36 +08:00

73 lines
1.5 KiB
C

/*
* Copyright (C) 2018 Intel Corporation. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <hypervisor.h>
#include <ucode.h>
uint64_t get_microcode_version(void)
{
uint64_t val;
uint32_t eax = 0U, ebx = 0U, ecx = 0U, edx = 0U;
msr_write(MSR_IA32_BIOS_SIGN_ID, 0U);
cpuid(CPUID_FEATURES, &eax, &ebx, &ecx, &edx);
val = msr_read(MSR_IA32_BIOS_SIGN_ID);
return val;
}
/*
* According to SDM vol 3 Table 9-7. If data_size field of uCode
* header is zero, the ucode length is 2000
*/
#define UCODE_GET_DATA_SIZE(uhdr) \
((uhdr.data_size != 0U) ? uhdr.data_size : 2000U)
void acrn_update_ucode(struct vcpu *vcpu, uint64_t v)
{
uint64_t gva;
struct ucode_header uhdr;
uint32_t data_page_num;
size_t data_size;
uint8_t *ucode_ptr;
int err;
uint32_t err_code;
gva = v - sizeof(struct ucode_header);
err_code = 0U;
err = copy_from_gva(vcpu, &uhdr, gva, sizeof(uhdr), &err_code);
if (err < 0) {
if (err == -EFAULT) {
vcpu_inject_pf(vcpu, gva, err_code);
}
return;
}
data_size = UCODE_GET_DATA_SIZE(uhdr) + sizeof(struct ucode_header);
data_page_num =
(data_size + CPU_PAGE_SIZE - 1U) >> CPU_PAGE_SHIFT;
ucode_ptr = alloc_pages(data_page_num);
if (ucode_ptr == NULL) {
return;
}
err_code = 0U;
err = copy_from_gva(vcpu, ucode_ptr, gva, data_size, &err_code);
if (err < 0) {
if (err == -EFAULT) {
vcpu_inject_pf(vcpu, gva, err_code);
}
return;
}
msr_write(MSR_IA32_BIOS_UPDT_TRIG,
(uint64_t)ucode_ptr + sizeof(struct ucode_header));
get_microcode_version();
free(ucode_ptr);
}