Skip to content

Commit 9c7c0de

Browse files
lifeixlijinxia
authored andcommitted
hv: mmu: add static paging table allocation for EPT
Add static paging table allocation API for EPT. Note: must configure SOS/UOS_REAM_SIZE exactly as the platform. Tracked-On: #861 Signed-off-by: Li, Fei1 <fei1.li@intel.com>
1 parent dc9d18a commit 9c7c0de

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

hypervisor/arch/x86/Kconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,21 @@ config PLATFORM_RAM_SIZE
235235
A 64-bit integer indicating the size of the physical platform RAM
236236
(not included the MMIO).
237237

238+
config SOS_RAM_SIZE
239+
hex "Size of the vm0 RAM"
240+
default 0x200000000 if SHARING_MODE
241+
default 0x000000000 if PARTITION_MODE
242+
help
243+
A 64-bit integer indicating the size of the vm0 RAM (not included the MMIO).
244+
245+
config UOS_RAM_SIZE
246+
hex "Size of the UOS RAM"
247+
default 0x100000000 if SHARING_MODE
248+
default 0x100000000 if PARTITION_MODE
249+
help
250+
A 64-bit integer indicating the size of the user OS RAM (not included the MMIO).
251+
Now we assume each UOS uses same amount of RAM size.
252+
238253
config CONSTANT_ACPI
239254
bool "The platform ACPI info is constant"
240255
default n

hypervisor/arch/x86/page.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,122 @@ const struct memory_ops ppt_mem_ops = {
6565
.get_pdpt_page = ppt_get_pdpt_page,
6666
.get_pd_page = ppt_get_pd_page,
6767
};
68+
69+
/* The size of the guest physical address space, covered by the EPT page table of a VM */
70+
#define EPT_ADDRESS_SPACE(size) ((size != 0UL) ? (size + PLATFORM_LO_MMIO_SIZE) : 0UL)
71+
DEFINE_PGTABLE_PAGE(vm0_, pml4, PML4, EPT_ADDRESS_SPACE(CONFIG_SOS_RAM_SIZE));
72+
DEFINE_PGTABLE_PAGE(vm0_, pdpt, PDPT, EPT_ADDRESS_SPACE(CONFIG_SOS_RAM_SIZE));
73+
DEFINE_PGTABLE_PAGE(vm0_, pd, PD, EPT_ADDRESS_SPACE(CONFIG_SOS_RAM_SIZE));
74+
DEFINE_PGTABLE_PAGE(vm0_, pt, PT, EPT_ADDRESS_SPACE(CONFIG_SOS_RAM_SIZE));
75+
76+
/* uos_nworld_pml4_pages[i] is ...... of UOS i (whose vm_id = i +1) */
77+
static struct page uos_nworld_pml4_pages[CONFIG_MAX_VM_NUM - 1U][PML4_PAGE_NUM(EPT_ADDRESS_SPACE(CONFIG_UOS_RAM_SIZE))];
78+
static struct page uos_nworld_pdpt_pages[CONFIG_MAX_VM_NUM - 1U][PDPT_PAGE_NUM(EPT_ADDRESS_SPACE(CONFIG_UOS_RAM_SIZE))];
79+
static struct page uos_nworld_pd_pages[CONFIG_MAX_VM_NUM - 1U][PD_PAGE_NUM(EPT_ADDRESS_SPACE(CONFIG_UOS_RAM_SIZE))];
80+
static struct page uos_nworld_pt_pages[CONFIG_MAX_VM_NUM - 1U][PT_PAGE_NUM(EPT_ADDRESS_SPACE(CONFIG_UOS_RAM_SIZE))];
81+
82+
#define TRUSTY_PML4_PAGE_NUM(size) (1UL)
83+
#define TRUSTY_PDPT_PAGE_NUM(size) (1UL)
84+
#define TRUSTY_PD_PAGE_NUM(size) (PD_PAGE_NUM(size))
85+
#define TRUSTY_PT_PAGE_NUM(size) (PT_PAGE_NUM(size))
86+
#define TRUSTY_PGTABLE_PAGE_NUM(size) \
87+
(TRUSTY_PML4_PAGE_NUM(size) + TRUSTY_PDPT_PAGE_NUM(size) + TRUSTY_PD_PAGE_NUM(size) + TRUSTY_PT_PAGE_NUM(size))
88+
89+
static struct page uos_sworld_pgtable_pages[CONFIG_MAX_VM_NUM - 1U][TRUSTY_PGTABLE_PAGE_NUM(TRUSTY_RAM_SIZE)];
90+
91+
/* ept: extended page table*/
92+
static union pgtable_pages_info ept_pages_info[CONFIG_MAX_VM_NUM] = {
93+
{
94+
.ept = {
95+
.top_address_space = EPT_ADDRESS_SPACE(CONFIG_SOS_RAM_SIZE),
96+
.nworld_pml4_base = vm0_pml4_pages,
97+
.nworld_pdpt_base = vm0_pdpt_pages,
98+
.nworld_pd_base = vm0_pd_pages,
99+
.nworld_pt_base = vm0_pt_pages,
100+
},
101+
},
102+
};
103+
104+
static inline uint64_t ept_get_default_access_right(void)
105+
{
106+
return EPT_RWX;
107+
}
108+
109+
static inline uint64_t ept_pgentry_present(uint64_t pte)
110+
{
111+
return pte & EPT_RWX;
112+
}
113+
114+
static inline struct page *ept_get_pml4_page(const union pgtable_pages_info *info, __unused uint64_t gpa)
115+
{
116+
struct page *page;
117+
if (gpa < TRUSTY_EPT_REBASE_GPA) {
118+
page = info->ept.nworld_pml4_base;
119+
} else {
120+
page = info->ept.sworld_pgtable_base;
121+
}
122+
(void)memset(page, 0U, PAGE_SIZE);
123+
return page;
124+
}
125+
126+
static inline struct page *ept_get_pdpt_page(const union pgtable_pages_info *info, uint64_t gpa)
127+
{
128+
struct page *page;
129+
if (gpa < TRUSTY_EPT_REBASE_GPA) {
130+
page = info->ept.nworld_pdpt_base + (gpa >> PML4E_SHIFT);
131+
} else {
132+
page = info->ept.sworld_pgtable_base + TRUSTY_PML4_PAGE_NUM(TRUSTY_EPT_REBASE_GPA) +
133+
((gpa - TRUSTY_EPT_REBASE_GPA) >> PML4E_SHIFT);
134+
}
135+
(void)memset(page, 0U, PAGE_SIZE);
136+
return page;
137+
}
138+
139+
static inline struct page *ept_get_pd_page(const union pgtable_pages_info *info, uint64_t gpa)
140+
{
141+
struct page *page;
142+
if (gpa < TRUSTY_EPT_REBASE_GPA) {
143+
page = info->ept.nworld_pd_base + (gpa >> PDPTE_SHIFT);
144+
} else {
145+
page = info->ept.sworld_pgtable_base + TRUSTY_PML4_PAGE_NUM(TRUSTY_EPT_REBASE_GPA) +
146+
TRUSTY_PDPT_PAGE_NUM(TRUSTY_EPT_REBASE_GPA) + ((gpa - TRUSTY_EPT_REBASE_GPA) >> PDPTE_SHIFT);
147+
}
148+
(void)memset(page, 0U, PAGE_SIZE);
149+
return page;
150+
}
151+
152+
static inline struct page *ept_get_pt_page(const union pgtable_pages_info *info, uint64_t gpa)
153+
{
154+
struct page *page;
155+
if (gpa < TRUSTY_EPT_REBASE_GPA) {
156+
page = info->ept.nworld_pt_base + (gpa >> PDE_SHIFT);
157+
} else {
158+
page = info->ept.sworld_pgtable_base + TRUSTY_PML4_PAGE_NUM(TRUSTY_EPT_REBASE_GPA) +
159+
TRUSTY_PDPT_PAGE_NUM(TRUSTY_EPT_REBASE_GPA) + TRUSTY_PD_PAGE_NUM(TRUSTY_EPT_REBASE_GPA) +
160+
((gpa - TRUSTY_EPT_REBASE_GPA) >> PDE_SHIFT);
161+
}
162+
(void)memset(page, 0U, PAGE_SIZE);
163+
return page;
164+
}
165+
166+
void init_ept_mem_ops(struct vm *vm)
167+
{
168+
uint16_t vm_id = vm->vm_id;
169+
if (vm_id != 0U) {
170+
ept_pages_info[vm_id].ept.top_address_space = EPT_ADDRESS_SPACE(CONFIG_UOS_RAM_SIZE);
171+
ept_pages_info[vm_id].ept.nworld_pml4_base = uos_nworld_pml4_pages[vm_id - 1U];
172+
ept_pages_info[vm_id].ept.nworld_pdpt_base = uos_nworld_pdpt_pages[vm_id - 1U];
173+
ept_pages_info[vm_id].ept.nworld_pd_base = uos_nworld_pd_pages[vm_id - 1U];
174+
ept_pages_info[vm_id].ept.nworld_pt_base = uos_nworld_pt_pages[vm_id - 1U];
175+
ept_pages_info[vm_id].ept.sworld_pgtable_base = uos_sworld_pgtable_pages[vm_id - 1U];
176+
}
177+
vm->arch_vm.ept_mem_ops.info = &ept_pages_info[vm_id];
178+
179+
vm->arch_vm.ept_mem_ops.get_default_access_right = ept_get_default_access_right;
180+
vm->arch_vm.ept_mem_ops.pgentry_present = ept_pgentry_present;
181+
vm->arch_vm.ept_mem_ops.get_pml4_page = ept_get_pml4_page;
182+
vm->arch_vm.ept_mem_ops.get_pdpt_page = ept_get_pdpt_page;
183+
vm->arch_vm.ept_mem_ops.get_pd_page = ept_get_pd_page;
184+
vm->arch_vm.ept_mem_ops.get_pt_page = ept_get_pt_page;
185+
186+
}

hypervisor/include/arch/x86/guest/vm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define VM_H_
99
#include <bsp_extern.h>
1010
#include <vpci.h>
11+
#include <page.h>
1112

1213
#ifdef CONFIG_PARTITION_MODE
1314
#include <mptable.h>
@@ -97,6 +98,8 @@ struct vm_arch {
9798
* but Normal World can not access Secure World's memory.
9899
*/
99100
void *sworld_eptp;
101+
struct memory_ops ept_mem_ops;
102+
100103
void *tmp_pg_array; /* Page array for tmp guest paging struct */
101104
struct acrn_vioapic vioapic; /* Virtual IOAPIC base address */
102105
struct acrn_vpic vpic; /* Virtual PIC */

hypervisor/include/arch/x86/page.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ union pgtable_pages_info {
2525
struct page *pt_base;
2626
} ppt;
2727
struct {
28+
uint64_t top_address_space;
2829
struct page *nworld_pml4_base;
2930
struct page *nworld_pdpt_base;
3031
struct page *nworld_pd_base;
@@ -44,5 +45,6 @@ struct memory_ops {
4445
};
4546

4647
extern const struct memory_ops ppt_mem_ops;
48+
void init_ept_mem_ops(struct vm *vm);
4749

4850
#endif /* PAGE_H */

hypervisor/include/arch/x86/trusty.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#define MMC_PROD_NAME_WITH_PSN_LEN 15U
1313
#define BUP_MKHI_BOOTLOADER_SEED_LEN 64U
1414

15+
#define TRUSTY_RAM_SIZE (16UL * 1024UL * 1024UL) /* 16 MB for now */
16+
1517
/* Trusty EPT rebase gpa: 511G */
1618
#define TRUSTY_EPT_REBASE_GPA (511UL * 1024UL * 1024UL * 1024UL)
1719

0 commit comments

Comments
 (0)