Skip to content

Commit e13c852

Browse files
wuxyintellijinxia
authored andcommitted
HV:INSTR_EMUL: Clean up CPU_reg_name
In the current hypervisor, there are many members of CPU_reg_name used to check range and useless register names. Define some CPU_REG_XX_FIRST and CPU_REG_XX_LAST MACROs to make range checking clear; Remove useless register names CPU_REG_XX_LAST in CPU_reg_name; Update the related caller. V1-->V2: Update a mistake, replace second CPU_REG_SEG_FIRST with CPU_REG_SEG_LAST in ASSERT. V2-->V3: Add '()' for bool expression in ASSERT. Signed-off-by: Xiangyang Wu <xiangyang.wu@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent f4ca3cc commit e13c852

File tree

3 files changed

+69
-34
lines changed

3 files changed

+69
-34
lines changed

hypervisor/arch/x86/guest/instr_emul.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@ vie_calculate_gla(enum vm_cpu_mode cpu_mode, enum cpu_reg_name seg,
15981598
uint8_t glasize;
15991599
uint32_t type;
16001600

1601-
ASSERT(seg >= CPU_REG_ES && seg <= CPU_REG_GS,
1601+
ASSERT((seg >= CPU_REG_SEG_FIRST) && (seg <= CPU_REG_SEG_LAST),
16021602
"%s: invalid segment %d", __func__, seg);
16031603
ASSERT(length == 1U || length == 2U || length == 4U || length == 8U,
16041604
"%s: invalid operand size %hhu", __func__, length);
@@ -1725,10 +1725,6 @@ vie_init(struct vie *vie, struct vcpu *vcpu)
17251725

17261726
(void)memset(vie, 0U, sizeof(struct vie));
17271727

1728-
vie->base_register = CPU_REG_LAST;
1729-
vie->index_register = CPU_REG_LAST;
1730-
vie->segment_register = CPU_REG_LAST;
1731-
17321728
err_code = PAGE_FAULT_ID_FLAG;
17331729
ret = copy_from_gva(vcpu, vie->inst, guest_rip_gva,
17341730
inst_len, &err_code);
@@ -2021,7 +2017,7 @@ decode_sib(struct vie *vie)
20212017
}
20222018

20232019
/* 'scale' makes sense only in the context of an index register */
2024-
if (vie->index_register < CPU_REG_LAST) {
2020+
if (vie->index_register <= CPU_REG_LAST) {
20252021
vie->scale = 1U << vie->ss;
20262022
}
20272023

hypervisor/arch/x86/guest/instr_emul_wrapper.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ int vm_get_register(struct vcpu *vcpu, enum cpu_reg_name reg, uint64_t *retval)
3232
return -EINVAL;
3333
}
3434

35-
if ((reg >= CPU_REG_LAST) || (reg < CPU_REG_RAX)) {
35+
if ((reg > CPU_REG_LAST) || (reg < CPU_REG_FIRST)) {
3636
return -EINVAL;
3737
}
3838

39-
if ((reg >= CPU_REG_RAX) && (reg <= CPU_REG_RDI)) {
39+
if ((reg >= CPU_REG_GENERAL_FIRST) && (reg <= CPU_REG_GENERAL_LAST)) {
4040
cur_context =
4141
&vcpu->arch_vcpu.contexts[vcpu->arch_vcpu.cur_context];
4242
*retval = cur_context->guest_cpu_regs.longs[reg];
43-
} else if ((reg > CPU_REG_RDI) && (reg < CPU_REG_LAST)) {
43+
} else if ((reg >= CPU_REG_NONGENERAL_FIRST) && (reg <= CPU_REG_NONGENERAL_LAST)) {
4444
uint32_t field = get_vmcs_field(reg);
4545

4646
if (field != VMX_INVALID_VMCS_FIELD) {
47-
if (reg < CPU_REG_NATURAL_LAST) {
47+
if (reg <= CPU_REG_NATURAL_LAST) {
4848
*retval = exec_vmread(field);
49-
} else if (reg < CPU_REG_64BIT_LAST) {
49+
} else if (reg <= CPU_REG_64BIT_LAST) {
5050
*retval = exec_vmread64(field);
5151
} else {
5252
*retval = (uint64_t)exec_vmread16(field);
@@ -67,19 +67,19 @@ int vm_set_register(struct vcpu *vcpu, enum cpu_reg_name reg, uint64_t val)
6767
return -EINVAL;
6868
}
6969

70-
if ((reg >= CPU_REG_LAST) || (reg < CPU_REG_RAX)) {
70+
if ((reg > CPU_REG_LAST) || (reg < CPU_REG_FIRST)) {
7171
return -EINVAL;
7272
}
7373

74-
if ((reg >= CPU_REG_RAX) && (reg <= CPU_REG_RDI)) {
74+
if ((reg >= CPU_REG_GENERAL_FIRST) && (reg <= CPU_REG_GENERAL_LAST)) {
7575
cur_context =
7676
&vcpu->arch_vcpu.contexts[vcpu->arch_vcpu.cur_context];
7777
cur_context->guest_cpu_regs.longs[reg] = val;
78-
} else if ((reg > CPU_REG_RDI) && (reg < CPU_REG_LAST)) {
78+
} else if ((reg >= CPU_REG_NONGENERAL_FIRST) && (reg <= CPU_REG_NONGENERAL_LAST)) {
7979
uint32_t field = get_vmcs_field(reg);
8080

8181
if (field != VMX_INVALID_VMCS_FIELD) {
82-
if (reg < CPU_REG_NATURAL_LAST) {
82+
if (reg <= CPU_REG_NATURAL_LAST) {
8383
exec_vmwrite(field, val);
8484
} else if (reg <= CPU_REG_64BIT_LAST) {
8585
exec_vmwrite64(field, val);
@@ -242,9 +242,8 @@ encode_vmcs_seg_desc(enum cpu_reg_name seg,
242242
*the corresponding field index MACROs in VMCS.
243243
*
244244
*Post Condition:
245-
*In the non-general register names group (CPU_REG_CR0~CPU_REG_LAST),
246-
*for register names CPU_REG_CR2, CPU_REG_IDTR, CPU_REG_GDTR,
247-
*CPU_REG_NATURAL_LAST, CPU_REG_64BIT_LAST and CPU_REG_LAST,
245+
*In the non-general register names group (CPU_REG_CR0~CPU_REG_GDTR),
246+
*for register names CPU_REG_CR2, CPU_REG_IDTR and CPU_REG_GDTR,
248247
*this function returns VMX_INVALID_VMCS_FIELD;
249248
*for other register names, it returns correspoding field index MACROs
250249
*in VMCS.

hypervisor/arch/x86/guest/instr_emul_wrapper.h

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,8 @@
3939
* Within the following groups,register name need to be
4040
* kept in order:
4141
* General register names group (CPU_REG_RAX~CPU_REG_RDI);
42-
* Non general register names group (CPU_REG_CR0~CPU_REG_LAST);
42+
* Non general register names group (CPU_REG_CR0~CPU_REG_GDTR);
4343
* Segement register names group (CPU_REG_ES~CPU_REG_GS).
44-
*
45-
* CPU_REG_NATURAL_LAST indicates in the non general register names
46-
* group the register name (less than CPU_REG_NATURAL_last) is
47-
* corresponds to the natural width field in VMCS;
48-
*
49-
* CPU_REG_64BIT_LAST indicates in the non general register names
50-
* group the register name (less than CPU_REG_64BIT_LAST and more than
51-
* CPU_REG_NATURAL_last) corresponds to the 64-bit field in VMCS.
52-
*
53-
* CPU_REG_LAST indicates the last register name.
54-
*
5544
*/
5645
enum cpu_reg_name {
5746
CPU_REG_RAX,
@@ -77,13 +66,13 @@ enum cpu_reg_name {
7766
CPU_REG_RSP,
7867
CPU_REG_RIP,
7968
CPU_REG_RFLAGS,
80-
CPU_REG_NATURAL_LAST,
69+
/*CPU_REG_NATURAL_LAST*/
8170
CPU_REG_EFER,
8271
CPU_REG_PDPTE0,
8372
CPU_REG_PDPTE1,
8473
CPU_REG_PDPTE2,
8574
CPU_REG_PDPTE3,
86-
CPU_REG_64BIT_LAST,
75+
/*CPU_REG_64BIT_LAST,*/
8776
CPU_REG_ES,
8877
CPU_REG_CS,
8978
CPU_REG_SS,
@@ -93,10 +82,61 @@ enum cpu_reg_name {
9382
CPU_REG_LDTR,
9483
CPU_REG_TR,
9584
CPU_REG_IDTR,
96-
CPU_REG_GDTR,
97-
CPU_REG_LAST
85+
CPU_REG_GDTR
86+
/*CPU_REG_LAST*/
9887
};
9988

89+
/**
90+
* Define the following MACRO to make range checking clear.
91+
*
92+
* CPU_REG_FIRST indicates the first register name, its value
93+
* is the same as CPU_REG_RAX;
94+
* CPU_REG_LAST indicates the last register name, its value is
95+
* the same as CPU_REG_GDTR;
96+
*
97+
* CPU_REG_GENERAL_FIRST indicates the first general register name,
98+
* its value is the same as CPU_REG_RAX;
99+
* CPU_REG_GENERAL_LAST indicates the last general register name,
100+
* its value is the same as CPU_REG_RDI;
101+
*
102+
* CPU_REG_NONGENERAL_FIRST indicates the first non general register
103+
* name, its value is the same as CPU_REG_CR0;
104+
* CPU_REG_NONGENERAL_LAST indicates the last non general register
105+
* name, its value is the same as CPU_REG_GDTR;
106+
*
107+
* CPU_REG_NATURAL_FIRST indicates the first register name that
108+
* is corresponds to the natural width field in VMCS, its value
109+
* is the same as CPU_REG_CR0;
110+
* CPU_REG_NATURAL_LAST indicates the last register name that
111+
* is corresponds to the natural width field in VMCS, its value
112+
* is the same as CPU_REG_RFLAGS;
113+
*
114+
* CPU_REG_64BIT_FIRST indicates the first register name that
115+
* is corresponds to the 64 bit field in VMCS, its value
116+
* is the same as CPU_REG_EFER;
117+
* CPU_REG_64BIT_LAST indicates the last register name that
118+
* is corresponds to the 64 bit field in VMCS, its value
119+
* is the same as CPU_REG_PDPTE3;
120+
*
121+
* CPU_REG_SEG_FIRST indicates the first segement register name,
122+
* its value is the same as CPU_REG_ES;
123+
* CPU_REG_SEG_FIRST indicates the last segement register name,
124+
* its value is the same as CPU_REG_GS
125+
*
126+
*/
127+
#define CPU_REG_FIRST CPU_REG_RAX
128+
#define CPU_REG_LAST CPU_REG_GDTR
129+
#define CPU_REG_GENERAL_FIRST CPU_REG_RAX
130+
#define CPU_REG_GENERAL_LAST CPU_REG_RDI
131+
#define CPU_REG_NONGENERAL_FIRST CPU_REG_CR0
132+
#define CPU_REG_NONGENERAL_LAST CPU_REG_GDTR
133+
#define CPU_REG_NATURAL_FIRST CPU_REG_CR0
134+
#define CPU_REG_NATURAL_LAST CPU_REG_RFLAGS
135+
#define CPU_REG_64BIT_FIRST CPU_REG_EFER
136+
#define CPU_REG_64BIT_LAST CPU_REG_PDPTE3
137+
#define CPU_REG_SEG_FIRST CPU_REG_ES
138+
#define CPU_REG_SEG_LAST CPU_REG_GS
139+
100140
struct vie_op {
101141
uint8_t op_byte; /* actual opcode byte */
102142
uint8_t op_type; /* type of operation (e.g. MOV) */

0 commit comments

Comments
 (0)