Skip to content

Commit a9151ff

Browse files
binbinwu1lijinxia
authored andcommitted
hv: add compile time assert for static checks
Add two files to do compile time assert. One is arch specific, and put in hypervisor/arch/x86/. The other one is common, and put in hypervisor/common/. If the statement is not true, there will be error during compile time. The file will not increase the size of HV binary. Signed-off-by: Binbin Wu <binbin.wu@intel.com> Reviewed-by: Junjie Mao <junjie.mao@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent 69522dc commit a9151ff

File tree

5 files changed

+75
-76
lines changed

5 files changed

+75
-76
lines changed

hypervisor/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ C_SRCS += arch/x86/cpu_state_tbl.c
140140
C_SRCS += arch/x86/mtrr.c
141141
C_SRCS += arch/x86/pm.c
142142
S_SRCS += arch/x86/wakeup.S
143+
C_SRCS += arch/x86/static_checks.c
143144
C_SRCS += arch/x86/guest/vcpu.c
144145
C_SRCS += arch/x86/guest/vm.c
145146
C_SRCS += arch/x86/guest/vlapic.c
@@ -165,6 +166,7 @@ C_SRCS += common/schedule.c
165166
C_SRCS += common/vm_load.c
166167
C_SRCS += common/io_request.c
167168
C_SRCS += common/ptdev.c
169+
C_SRCS += common/static_checks.c
168170

169171
ifdef STACK_PROTECTOR
170172
C_SRCS += common/stack_protector.c

hypervisor/arch/x86/cpu.c

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -386,78 +386,6 @@ void bsp_boot_init(void)
386386
(void)memset(&_ld_bss_start, 0U,
387387
(size_t)(&_ld_bss_end - &_ld_bss_start));
388388

389-
/* Build time sanity checks to make sure hard-coded offset
390-
* is matching the actual offset!
391-
*/
392-
ASSERT((sizeof(struct trusty_startup_param)
393-
+ sizeof(struct trusty_key_info)) < 0x1000U,
394-
"trusty_startup_param + key_info > 1Page size(4KB)!");
395-
396-
ASSERT(NR_WORLD == 2, "Only 2 Worlds supported!");
397-
ASSERT(offsetof(struct cpu_gp_regs, rax) ==
398-
CPU_CONTEXT_OFFSET_RAX,
399-
"cpu_gp_regs rax offset not match");
400-
ASSERT(offsetof(struct cpu_gp_regs, rbx) ==
401-
CPU_CONTEXT_OFFSET_RBX,
402-
"cpu_gp_regs rbx offset not match");
403-
ASSERT(offsetof(struct cpu_gp_regs, rcx) ==
404-
CPU_CONTEXT_OFFSET_RCX,
405-
"cpu_gp_regs rcx offset not match");
406-
ASSERT(offsetof(struct cpu_gp_regs, rdx) ==
407-
CPU_CONTEXT_OFFSET_RDX,
408-
"cpu_gp_regs rdx offset not match");
409-
ASSERT(offsetof(struct cpu_gp_regs, rbp) ==
410-
CPU_CONTEXT_OFFSET_RBP,
411-
"cpu_gp_regs rbp offset not match");
412-
ASSERT(offsetof(struct cpu_gp_regs, rsi) ==
413-
CPU_CONTEXT_OFFSET_RSI,
414-
"cpu_gp_regs rsi offset not match");
415-
ASSERT(offsetof(struct cpu_gp_regs, rdi) ==
416-
CPU_CONTEXT_OFFSET_RDI,
417-
"cpu_gp_regs rdi offset not match");
418-
ASSERT(offsetof(struct cpu_gp_regs, r8) ==
419-
CPU_CONTEXT_OFFSET_R8,
420-
"cpu_gp_regs r8 offset not match");
421-
ASSERT(offsetof(struct cpu_gp_regs, r9) ==
422-
CPU_CONTEXT_OFFSET_R9,
423-
"cpu_gp_regs r9 offset not match");
424-
ASSERT(offsetof(struct cpu_gp_regs, r10) ==
425-
CPU_CONTEXT_OFFSET_R10,
426-
"cpu_gp_regs r10 offset not match");
427-
ASSERT(offsetof(struct cpu_gp_regs, r11) ==
428-
CPU_CONTEXT_OFFSET_R11,
429-
"cpu_gp_regs r11 offset not match");
430-
ASSERT(offsetof(struct cpu_gp_regs, r12) ==
431-
CPU_CONTEXT_OFFSET_R12,
432-
"cpu_gp_regs r12 offset not match");
433-
ASSERT(offsetof(struct cpu_gp_regs, r13) ==
434-
CPU_CONTEXT_OFFSET_R13,
435-
"cpu_gp_regs r13 offset not match");
436-
ASSERT(offsetof(struct cpu_gp_regs, r14) ==
437-
CPU_CONTEXT_OFFSET_R14,
438-
"cpu_gp_regs r14 offset not match");
439-
ASSERT(offsetof(struct cpu_gp_regs, r15) ==
440-
CPU_CONTEXT_OFFSET_R15,
441-
"cpu_gp_regs r15 offset not match");
442-
ASSERT(offsetof(struct run_context, cr2) ==
443-
CPU_CONTEXT_OFFSET_CR2,
444-
"run_context cr2 offset not match");
445-
ASSERT(offsetof(struct run_context, ia32_spec_ctrl) ==
446-
CPU_CONTEXT_OFFSET_IA32_SPEC_CTRL,
447-
"run_context ia32_spec_ctrl offset not match");
448-
ASSERT(offsetof(struct run_context, rflags) ==
449-
CPU_CONTEXT_OFFSET_RFLAGS,
450-
"run_context rflags offset not match");
451-
ASSERT(offsetof(struct ext_context, cr3) ==
452-
CPU_CONTEXT_OFFSET_CR3 - CPU_CONTEXT_OFFSET_EXTCTX_START,
453-
"ext_context cr3 offset not match");
454-
ASSERT(offsetof(struct ext_context, idtr) ==
455-
CPU_CONTEXT_OFFSET_IDTR - CPU_CONTEXT_OFFSET_EXTCTX_START,
456-
"ext_context idtr offset not match");
457-
ASSERT(offsetof(struct ext_context, ldtr) ==
458-
CPU_CONTEXT_OFFSET_LDTR - CPU_CONTEXT_OFFSET_EXTCTX_START,
459-
"ext_context ldtr offset not match");
460-
461389
bitmap_set_nolock(BOOT_CPU_ID, &pcpu_active_bitmap);
462390

463391
misc_en = msr_read(MSR_IA32_MISC_ENABLE);

hypervisor/arch/x86/static_checks.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (C) 2018 Intel Corporation. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
#include <hv_lib.h>
7+
#include <hv_arch.h>
8+
#include <vm0_boot.h>
9+
10+
#define CAT_(A,B) A ## B
11+
#define CTASSERT(expr) \
12+
typedef int CAT_(CTA_DummyType,__LINE__)[(expr) ? 1 : -1]
13+
14+
/* Build time sanity checks to make sure hard-coded offset
15+
* is matching the actual offset!
16+
*/
17+
CTASSERT(BOOT_CTX_CR0_OFFSET == offsetof(struct boot_ctx, cr0));
18+
CTASSERT(BOOT_CTX_CR3_OFFSET == offsetof(struct boot_ctx, cr3));
19+
CTASSERT(BOOT_CTX_CR4_OFFSET == offsetof(struct boot_ctx, cr4));
20+
CTASSERT(BOOT_CTX_IDT_OFFSET == offsetof(struct boot_ctx, idt));
21+
CTASSERT(BOOT_CTX_GDT_OFFSET == offsetof(struct boot_ctx, gdt));
22+
CTASSERT(BOOT_CTX_LDT_SEL_OFFSET == offsetof(struct boot_ctx, ldt_sel));
23+
CTASSERT(BOOT_CTX_TR_SEL_OFFSET == offsetof(struct boot_ctx, tr_sel));
24+
CTASSERT(BOOT_CTX_CS_SEL_OFFSET == offsetof(struct boot_ctx, cs_sel));
25+
CTASSERT(BOOT_CTX_SS_SEL_OFFSET == offsetof(struct boot_ctx, ss_sel));
26+
CTASSERT(BOOT_CTX_DS_SEL_OFFSET == offsetof(struct boot_ctx, ds_sel));
27+
CTASSERT(BOOT_CTX_ES_SEL_OFFSET == offsetof(struct boot_ctx, es_sel));
28+
CTASSERT(BOOT_CTX_FS_SEL_OFFSET == offsetof(struct boot_ctx, fs_sel));
29+
CTASSERT(BOOT_CTX_GS_SEL_OFFSET == offsetof(struct boot_ctx, gs_sel));
30+
CTASSERT(BOOT_CTX_CS_AR_OFFSET == offsetof(struct boot_ctx, cs_ar));
31+
CTASSERT(BOOT_CTX_EFER_LOW_OFFSET == offsetof(struct boot_ctx, ia32_efer));
32+
CTASSERT(BOOT_CTX_EFER_HIGH_OFFSET == offsetof(struct boot_ctx, ia32_efer) + 4);
33+
34+
CTASSERT(CPU_CONTEXT_OFFSET_RAX == offsetof(struct cpu_gp_regs, rax));
35+
CTASSERT(CPU_CONTEXT_OFFSET_RBX == offsetof(struct cpu_gp_regs, rbx));
36+
CTASSERT(CPU_CONTEXT_OFFSET_RCX == offsetof(struct cpu_gp_regs, rcx));
37+
CTASSERT(CPU_CONTEXT_OFFSET_RDX == offsetof(struct cpu_gp_regs, rdx));
38+
CTASSERT(CPU_CONTEXT_OFFSET_RBP == offsetof(struct cpu_gp_regs, rbp));
39+
CTASSERT(CPU_CONTEXT_OFFSET_RSI == offsetof(struct cpu_gp_regs, rsi));
40+
CTASSERT(CPU_CONTEXT_OFFSET_RDI == offsetof(struct cpu_gp_regs, rdi));
41+
CTASSERT(CPU_CONTEXT_OFFSET_R8 == offsetof(struct cpu_gp_regs, r8));
42+
CTASSERT(CPU_CONTEXT_OFFSET_R9 == offsetof(struct cpu_gp_regs, r9));
43+
CTASSERT(CPU_CONTEXT_OFFSET_R10 == offsetof(struct cpu_gp_regs, r10));
44+
CTASSERT(CPU_CONTEXT_OFFSET_R11 == offsetof(struct cpu_gp_regs, r11));
45+
CTASSERT(CPU_CONTEXT_OFFSET_R12 == offsetof(struct cpu_gp_regs, r12));
46+
CTASSERT(CPU_CONTEXT_OFFSET_R13 == offsetof(struct cpu_gp_regs, r13));
47+
CTASSERT(CPU_CONTEXT_OFFSET_R14 == offsetof(struct cpu_gp_regs, r14));
48+
CTASSERT(CPU_CONTEXT_OFFSET_R15 == offsetof(struct cpu_gp_regs, r15));
49+
CTASSERT(CPU_CONTEXT_OFFSET_CR2 == offsetof(struct run_context, cr2));
50+
CTASSERT(CPU_CONTEXT_OFFSET_IA32_SPEC_CTRL
51+
== offsetof(struct run_context, ia32_spec_ctrl));
52+
CTASSERT(CPU_CONTEXT_OFFSET_RFLAGS == offsetof(struct run_context, rflags));
53+
CTASSERT(CPU_CONTEXT_OFFSET_CR3 - CPU_CONTEXT_OFFSET_EXTCTX_START
54+
== offsetof(struct ext_context, cr3));
55+
CTASSERT(CPU_CONTEXT_OFFSET_IDTR - CPU_CONTEXT_OFFSET_EXTCTX_START
56+
== offsetof(struct ext_context, idtr));
57+
CTASSERT(CPU_CONTEXT_OFFSET_LDTR - CPU_CONTEXT_OFFSET_EXTCTX_START
58+
== offsetof(struct ext_context, ldtr));
59+
CTASSERT((sizeof(struct trusty_startup_param)
60+
+ sizeof(struct trusty_key_info)) < 0x1000U);
61+
CTASSERT(NR_WORLD == 2);

hypervisor/common/io_request.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ acrn_insert_request_wait(struct vcpu *vcpu, struct io_request *io_req)
6262
struct vhm_request *vhm_req;
6363
uint16_t cur;
6464

65-
ASSERT(sizeof(struct vhm_request) == (4096U/VHM_REQUEST_MAX),
66-
"vhm_request page broken!");
67-
68-
6965
if ((vcpu == NULL) || (io_req == NULL) ||
7066
(vcpu->vm->sw.io_shared_page == NULL)) {
7167
return -EINVAL;

hypervisor/common/static_checks.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright (C) 2018 Intel Corporation. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
#include <acrn_common.h>
7+
8+
#define CAT_(A,B) A ## B
9+
#define CTASSERT(expr) \
10+
typedef int CAT_(CTA_DummyType,__LINE__)[(expr) ? 1 : -1]
11+
12+
CTASSERT(sizeof(struct vhm_request) == (4096U/VHM_REQUEST_MAX));

0 commit comments

Comments
 (0)