Skip to content

Commit 4d13ad9

Browse files
yonghuahwenlingz
authored andcommitted
hv: enable NX in hypervisor
- enable NX feature in hypervisor: 1. Set 'XD' bit for all pages, including pages for guests when initialize MMU tables in hypervisor. 2. remove 'XD' bit for pages that contain hypervisor instructions. 3. enable MSR EFER.NXE,which will enable page access restriction by preventing instruction fetches form pages with XD bit set. - remove "-Wl -z noexecstack" GCC flag option in hypervisor Makefile as it would not affect stack attribute in hyervisor, which setup stack itself, instead of by loader. Tracked-On: #1122 Signed-off-by: Yonghua Huang <yonghua.huang@intel.com> Acked-by: Anthony Xu <anthony.xu@intel.com>
1 parent 405d133 commit 4d13ad9

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

hypervisor/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ ASFLAGS += -m64 -nostdinc -nostdlib
7676

7777
LDFLAGS += -Wl,--gc-sections -nostartfiles -nostdlib
7878
LDFLAGS += -Wl,-n,-z,max-page-size=0x1000
79-
LDFLAGS += -Wl,-z,noexecstack
8079

8180
ifeq (y, $(CONFIG_RELOC))
8281
# on X86_64, when build with "-pie", GCC fails on linking R_X86_64_32

hypervisor/arch/x86/boot/trampoline.S

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ trampoline_fixup_target:
107107
orl $0x00000100, %eax
108108
wrmsr
109109

110+
/* 0xc0000080 = MSR_IA32_EFER */
111+
movl $0xc0000080, %ecx
112+
rdmsr
113+
/* 0x00000800 = MSR_IA32_EFER_NXE_BIT */
114+
orl $0x00000800, %eax
115+
wrmsr
116+
110117
/* Enable paging, protection, numeric error and co-processor
111118
monitoring in CR0 to enter long mode */
112119

hypervisor/arch/x86/mmu.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,20 @@ void enable_paging(void)
209209
{
210210
uint64_t tmp64 = 0UL;
211211

212+
/*
213+
* Enable MSR IA32_EFER.NXE bit,to prevent
214+
* instruction fetching from pages with XD bit set.
215+
*/
216+
tmp64 = msr_read(MSR_IA32_EFER);
217+
tmp64 |= MSR_IA32_EFER_NXE_BIT;
218+
msr_write(MSR_IA32_EFER, tmp64);
219+
212220
/* Enable Write Protect, inhibiting writing to read-only pages */
213221
CPU_CR_READ(cr0, &tmp64);
214222
CPU_CR_WRITE(cr0, tmp64 | CR0_WP);
215223

216224
CPU_CR_WRITE(cr3, hva2hpa(ppt_mmu_pml4_addr));
225+
217226
}
218227

219228
void enable_smep(void)
@@ -225,14 +234,13 @@ void enable_smep(void)
225234
CPU_CR_WRITE(cr4, val64 | CR4_SMEP);
226235
}
227236

228-
229237
void init_paging(void)
230238
{
231-
uint64_t hv_hpa;
239+
uint64_t hv_hpa, text_end, size;
232240
uint32_t i;
233241
uint64_t low32_max_ram = 0UL;
234242
uint64_t high64_max_ram;
235-
uint64_t attr_uc = (PAGE_PRESENT | PAGE_RW | PAGE_USER | PAGE_CACHE_UC);
243+
uint64_t attr_uc = (PAGE_PRESENT | PAGE_RW | PAGE_USER | PAGE_CACHE_UC | PAGE_NX);
236244

237245
const struct e820_entry *entry;
238246
uint32_t entries_count = get_e820_entries_count();
@@ -282,6 +290,17 @@ void init_paging(void)
282290
CONFIG_HV_RAM_SIZE + (((hv_hpa & (PDE_SIZE - 1UL)) != 0UL) ? PDE_SIZE : 0UL),
283291
PAGE_CACHE_WB, PAGE_CACHE_MASK | PAGE_USER, &ppt_mem_ops, MR_MODIFY);
284292

293+
size = ((uint64_t)&ld_text_end - CONFIG_HV_RAM_START);
294+
text_end = hv_hpa + size;
295+
/*round up 'text_end' to 2MB aligned.*/
296+
text_end = (text_end + PDE_SIZE - 1UL) & PDE_MASK;
297+
/*
298+
* remove 'NX' bit for pages that contain hv code section, as by default XD bit is set for
299+
* all pages, including pages for guests.
300+
*/
301+
mmu_modify_or_del((uint64_t *)ppt_mmu_pml4_addr, hv_hpa & PDE_MASK,
302+
text_end - (hv_hpa & PDE_MASK), 0UL, PAGE_NX, &ppt_mem_ops, MR_MODIFY);
303+
285304
mmu_modify_or_del((uint64_t *)ppt_mmu_pml4_addr, (uint64_t)get_reserve_sworld_memory_base(),
286305
TRUSTY_RAM_SIZE * (CONFIG_MAX_VM_NUM - 1U), PAGE_USER, 0UL, &ppt_mem_ops, MR_MODIFY);
287306

hypervisor/bsp/ld/link_ram.ld.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ SECTIONS
3131
*(.retpoline_thunk)
3232
} > ram
3333

34+
/*Align text top boundary to 2MBytes.*/
35+
. = ALIGN(0x200000);
36+
ld_text_end = . ;
37+
3438
.rodata :
3539
{
3640
*(.rodata*) ;

hypervisor/include/arch/x86/mmu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
#define IA32E_REF_MASK \
5454
(boot_cpu_data.physical_address_mask)
5555

56+
extern uint8_t ld_text_end;
57+
5658
static inline uint64_t round_page_up(uint64_t addr)
5759
{
5860
return (((addr + (uint64_t)PAGE_SIZE) - 1UL) & PAGE_MASK);

0 commit comments

Comments
 (0)