Skip to content

Commit 02e7edc

Browse files
mingqiangchilijinxia
authored andcommitted
hv: Replace dynamic memory allocation for I/O bitmaps
-- Replace pointer with static memory for io_bitmap inside structure vm_arch. -- Refine allow_guest_pio_access/deny_guest_pio_access Tracked-On: #861 Signed-off-by: Mingqiang Chi <mingqiang.chi@intel.com> Reviewed-by: Anthony Xu <anthony.xu@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent eada04b commit 02e7edc

File tree

5 files changed

+25
-42
lines changed

5 files changed

+25
-42
lines changed

hypervisor/arch/x86/guest/pm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static inline void init_cx_port(struct vm *vm)
9494
if (cx_data->cx_reg.space_id == SPACE_SYSTEM_IO) {
9595
uint16_t port = (uint16_t)cx_data->cx_reg.address;
9696

97-
allow_guest_io_access(vm, port, 1U);
97+
allow_guest_pio_access(vm, port, 1U);
9898
}
9999
}
100100
}

hypervisor/arch/x86/io.c

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -391,44 +391,32 @@ static void empty_io_handler_list(struct vm *vm)
391391
void free_io_emulation_resource(struct vm *vm)
392392
{
393393
empty_io_handler_list(vm);
394-
395-
/* Free I/O emulation bitmaps */
396-
free(vm->arch_vm.iobitmap[0]);
397-
free(vm->arch_vm.iobitmap[1]);
398394
}
399395

400-
void allow_guest_io_access(struct vm *vm, uint32_t address_arg, uint32_t nbytes)
396+
void allow_guest_pio_access(struct vm *vm, uint16_t port_address,
397+
uint32_t nbytes)
401398
{
402-
uint32_t address = address_arg;
399+
uint16_t address = port_address;
403400
uint32_t *b;
404401
uint32_t i;
405-
uint32_t a;
406402

407-
b = vm->arch_vm.iobitmap[0];
403+
b = (uint32_t *)vm->arch_vm.io_bitmap;
408404
for (i = 0U; i < nbytes; i++) {
409-
if ((address & 0x8000U) != 0U) {
410-
b = vm->arch_vm.iobitmap[1];
411-
}
412-
a = address & 0x7fffU;
413-
b[a >> 5U] &= ~(1U << (a & 0x1fU));
405+
b[address >> 5U] &= ~(1U << (address & 0x1fU));
414406
address++;
415407
}
416408
}
417409

418-
static void deny_guest_io_access(struct vm *vm, uint32_t address_arg, uint32_t nbytes)
410+
static void deny_guest_pio_access(struct vm *vm, uint16_t port_address,
411+
uint32_t nbytes)
419412
{
420-
uint32_t address = address_arg;
413+
uint16_t address = port_address;
421414
uint32_t *b;
422415
uint32_t i;
423-
uint32_t a;
424416

425-
b = vm->arch_vm.iobitmap[0];
417+
b = (uint32_t *)vm->arch_vm.io_bitmap;
426418
for (i = 0U; i < nbytes; i++) {
427-
if ((address & 0x8000U) != 0U) {
428-
b = vm->arch_vm.iobitmap[1];
429-
}
430-
a = address & 0x7fffU;
431-
b[a >> 5U] |= (1U << (a & 0x1fU));
419+
b[address >> 5U] |= (1U << (address & 0x1fU));
432420
address++;
433421
}
434422
}
@@ -456,20 +444,11 @@ static struct vm_io_handler *create_io_handler(uint32_t port, uint32_t len,
456444

457445
void setup_io_bitmap(struct vm *vm)
458446
{
459-
/* Allocate VM architecture state and IO bitmaps A and B */
460-
vm->arch_vm.iobitmap[0] = alloc_page();
461-
vm->arch_vm.iobitmap[1] = alloc_page();
462-
463-
ASSERT((vm->arch_vm.iobitmap[0] != NULL) &&
464-
(vm->arch_vm.iobitmap[1] != NULL), "");
465-
466447
if (is_vm0(vm)) {
467-
(void)memset(vm->arch_vm.iobitmap[0], 0x00U, CPU_PAGE_SIZE);
468-
(void)memset(vm->arch_vm.iobitmap[1], 0x00U, CPU_PAGE_SIZE);
448+
(void)memset(vm->arch_vm.io_bitmap, 0x00U, CPU_PAGE_SIZE * 2);
469449
} else {
470450
/* block all IO port access from Guest */
471-
(void)memset(vm->arch_vm.iobitmap[0], 0xFFU, CPU_PAGE_SIZE);
472-
(void)memset(vm->arch_vm.iobitmap[1], 0xFFU, CPU_PAGE_SIZE);
451+
(void)memset(vm->arch_vm.io_bitmap, 0xFFU, CPU_PAGE_SIZE * 2);
473452
}
474453
}
475454

@@ -485,7 +464,7 @@ void register_io_emulation_handler(struct vm *vm, struct vm_io_range *range,
485464
}
486465

487466
if (is_vm0(vm)) {
488-
deny_guest_io_access(vm, range->base, range->len);
467+
deny_guest_pio_access(vm, range->base, range->len);
489468
}
490469

491470
handler = create_io_handler(range->base,

hypervisor/arch/x86/vmx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,10 +1022,10 @@ static void init_exec_ctrl(struct vcpu *vcpu)
10221022
exec_vmwrite32(VMX_CR3_TARGET_COUNT, 0U);
10231023

10241024
/* Set up IO bitmap register A and B - pg 2902 24.6.4 */
1025-
value64 = hva2hpa(vm->arch_vm.iobitmap[0]);
1025+
value64 = hva2hpa(vm->arch_vm.io_bitmap);
10261026
exec_vmwrite64(VMX_IO_BITMAP_A_FULL, value64);
10271027
pr_dbg("VMX_IO_BITMAP_A: 0x%016llx ", value64);
1028-
value64 = hva2hpa(vm->arch_vm.iobitmap[1]);
1028+
value64 = hva2hpa(&(vm->arch_vm.io_bitmap[CPU_PAGE_SIZE]));
10291029
exec_vmwrite64(VMX_IO_BITMAP_B_FULL, value64);
10301030
pr_dbg("VMX_IO_BITMAP_B: 0x%016llx ", value64);
10311031

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ enum vm_state {
8686
};
8787

8888
struct vm_arch {
89+
/* I/O bitmaps A and B for this VM, MUST be 4-Kbyte aligned */
90+
uint8_t io_bitmap[CPU_PAGE_SIZE*2];
91+
8992
uint64_t guest_init_pml4;/* Guest init pml4 */
9093
/* EPT hierarchy for Normal World */
9194
void *nworld_eptp;
@@ -96,7 +99,6 @@ struct vm_arch {
9699
void *sworld_eptp;
97100
void *m2p; /* machine address to guest physical address */
98101
void *tmp_pg_array; /* Page array for tmp guest paging struct */
99-
void *iobitmap[2];/* IO bitmap page array base address for this VM */
100102
void *msr_bitmap; /* MSR bitmap page base address for this VM */
101103
struct acrn_vioapic vioapic; /* Virtual IOAPIC base address */
102104
struct acrn_vpic vpic; /* Virtual PIC */
@@ -111,7 +113,8 @@ struct vm_arch {
111113
struct vm_io_handler *io_handler;
112114

113115
/* reference to virtual platform to come here (as needed) */
114-
};
116+
} __aligned(CPU_PAGE_SIZE);
117+
115118

116119
#define CPUID_CHECK_SUBLEAF (1U << 0)
117120
#define MAX_VM_VCPUID_ENTRIES 64U
@@ -127,11 +130,11 @@ struct vcpuid_entry {
127130
};
128131

129132
struct vm {
133+
struct vm_arch arch_vm; /* Reference to this VM's arch information */
130134
uint16_t vm_id; /* Virtual machine identifier */
131135
struct vm_hw_info hw; /* Reference to this VM's HW information */
132136
struct vm_sw_info sw; /* Reference to SW associated with this VM */
133137
struct vm_pm_info pm; /* Reference to this VM's arch information */
134-
struct vm_arch arch_vm; /* Reference to this VM's arch information */
135138
enum vm_state state; /* VM state */
136139
struct acrn_vuart vuart; /* Virtual UART */
137140
enum vpic_wire_mode wire_mode;
@@ -170,7 +173,7 @@ struct vm {
170173

171174
spinlock_t softirq_dev_lock;
172175
struct list_head softirq_dev_entry_list;
173-
};
176+
} __aligned(CPU_PAGE_SIZE);
174177

175178
#ifdef CONFIG_PARTITION_MODE
176179
struct vpci_vdev_array {

hypervisor/include/arch/x86/ioreq.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ struct mem_io_node {
112112
int32_t pio_instr_vmexit_handler(struct vcpu *vcpu);
113113
void setup_io_bitmap(struct vm *vm);
114114
void free_io_emulation_resource(struct vm *vm);
115-
void allow_guest_io_access(struct vm *vm, uint32_t address_arg, uint32_t nbytes);
115+
void allow_guest_pio_access(struct vm *vm, uint16_t port_address,
116+
uint32_t nbytes);
116117
void register_io_emulation_handler(struct vm *vm, struct vm_io_range *range,
117118
io_read_fn_t io_read_fn_ptr,
118119
io_write_fn_t io_write_fn_ptr);

0 commit comments

Comments
 (0)