Skip to content

Commit 8454376

Browse files
junjiemao1lijinxia
authored andcommitted
HV: instr_emul: keep using enum vm_reg_name for registers
The vm_reg_name is a good example of a collection of discrete values. This patch replaces signed integers with this type whenever applicable to avoid dependence on the underlying value of such enumeration constants. Signed-off-by: Junjie Mao <junjie.mao@intel.com>
1 parent edc7931 commit 8454376

File tree

3 files changed

+74
-69
lines changed

3 files changed

+74
-69
lines changed

hypervisor/arch/x86/guest/instr_emul.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,9 @@ emulate_movs(struct vcpu *vcpu, __unused uint64_t gpa, struct vie *vie,
671671
{
672672
uint64_t dstaddr, srcaddr;
673673
uint64_t rcx, rdi, rsi, rflags;
674-
int error, fault, seg, repeat;
674+
int error, fault, repeat;
675675
uint8_t opsize;
676+
enum vm_reg_name seg;
676677

677678
opsize = (vie->op.op_byte == 0xA4U) ? 1U : vie->opsize;
678679
error = 0;
@@ -700,7 +701,7 @@ emulate_movs(struct vcpu *vcpu, __unused uint64_t gpa, struct vie *vie,
700701
}
701702
}
702703

703-
seg = (vie->segment_override != 0U) ? vie->segment_register : VM_REG_GUEST_DS;
704+
seg = (vie->segment_override != 0U) ? (vie->segment_register) : VM_REG_GUEST_DS;
704705
error = get_gla(vcpu, vie, paging, opsize, vie->addrsize,
705706
PROT_READ, seg, VM_REG_GUEST_RSI, &srcaddr, &fault);
706707
if ((error != 0) || (fault != 0))
@@ -1735,7 +1736,7 @@ vie_advance(struct vie *vie)
17351736
}
17361737

17371738
static bool
1738-
segment_override(uint8_t x, int *seg)
1739+
segment_override(uint8_t x, enum vm_reg_name *seg)
17391740
{
17401741

17411742
switch (x) {

hypervisor/arch/x86/guest/instr_emul_wrapper.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010
#include "instr_emul.h"
1111

1212
static int
13-
encode_vmcs_seg_desc(int seg, uint32_t *base, uint32_t *lim, uint32_t *acc);
13+
encode_vmcs_seg_desc(enum vm_reg_name seg,
14+
uint32_t *base, uint32_t *lim, uint32_t *acc);
1415

1516
static int32_t
16-
get_vmcs_field(int ident);
17+
get_vmcs_field(enum vm_reg_name ident);
1718

1819
static bool
19-
is_segment_register(int reg);
20+
is_segment_register(enum vm_reg_name reg);
2021

2122
static bool
22-
is_descriptor_table(int reg);
23+
is_descriptor_table(enum vm_reg_name reg);
2324

24-
int vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval)
25+
int vm_get_register(struct vcpu *vcpu, enum vm_reg_name reg, uint64_t *retval)
2526
{
2627
struct run_context *cur_context;
2728

@@ -46,7 +47,7 @@ int vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval)
4647
return 0;
4748
}
4849

49-
int vm_set_register(struct vcpu *vcpu, int reg, uint64_t val)
50+
int vm_set_register(struct vcpu *vcpu, enum vm_reg_name reg, uint64_t val)
5051
{
5152
struct run_context *cur_context;
5253

@@ -71,7 +72,8 @@ int vm_set_register(struct vcpu *vcpu, int reg, uint64_t val)
7172
return 0;
7273
}
7374

74-
int vm_set_seg_desc(struct vcpu *vcpu, int seg, struct seg_desc *ret_desc)
75+
int vm_set_seg_desc(struct vcpu *vcpu, enum vm_reg_name seg,
76+
struct seg_desc *ret_desc)
7577
{
7678
int error;
7779
uint32_t base, limit, access;
@@ -93,7 +95,8 @@ int vm_set_seg_desc(struct vcpu *vcpu, int seg, struct seg_desc *ret_desc)
9395
return 0;
9496
}
9597

96-
int vm_get_seg_desc(struct vcpu *vcpu, int seg, struct seg_desc *desc)
98+
int vm_get_seg_desc(struct vcpu *vcpu, enum vm_reg_name seg,
99+
struct seg_desc *desc)
97100
{
98101
int error;
99102
uint32_t base, limit, access;
@@ -115,7 +118,7 @@ int vm_get_seg_desc(struct vcpu *vcpu, int seg, struct seg_desc *desc)
115118
return 0;
116119
}
117120

118-
static bool is_descriptor_table(int reg)
121+
static bool is_descriptor_table(enum vm_reg_name reg)
119122
{
120123
switch (reg) {
121124
case VM_REG_GUEST_IDTR:
@@ -126,7 +129,7 @@ static bool is_descriptor_table(int reg)
126129
}
127130
}
128131

129-
static bool is_segment_register(int reg)
132+
static bool is_segment_register(enum vm_reg_name reg)
130133
{
131134
switch (reg) {
132135
case VM_REG_GUEST_ES:
@@ -143,8 +146,9 @@ static bool is_segment_register(int reg)
143146
}
144147
}
145148

146-
static int encode_vmcs_seg_desc(int seg, uint32_t *base, uint32_t *lim,
147-
uint32_t *acc)
149+
static int
150+
encode_vmcs_seg_desc(enum vm_reg_name seg,
151+
uint32_t *base, uint32_t *lim, uint32_t *acc)
148152
{
149153
switch (seg) {
150154
case VM_REG_GUEST_ES:
@@ -204,7 +208,7 @@ static int encode_vmcs_seg_desc(int seg, uint32_t *base, uint32_t *lim,
204208
return 0;
205209
}
206210

207-
static int32_t get_vmcs_field(int ident)
211+
static int32_t get_vmcs_field(enum vm_reg_name ident)
208212
{
209213
switch (ident) {
210214
case VM_REG_GUEST_CR0:

hypervisor/arch/x86/guest/instr_emul_wrapper.h

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,52 @@
3131
#define INSTR_EMUL_WRAPPER_H
3232
#include <cpu.h>
3333

34+
/*
35+
* Identifiers for architecturally defined registers.
36+
*/
37+
enum vm_reg_name {
38+
VM_REG_GUEST_RAX,
39+
VM_REG_GUEST_RBX,
40+
VM_REG_GUEST_RCX,
41+
VM_REG_GUEST_RDX,
42+
VM_REG_GUEST_RBP,
43+
VM_REG_GUEST_RSI,
44+
VM_REG_GUEST_R8,
45+
VM_REG_GUEST_R9,
46+
VM_REG_GUEST_R10,
47+
VM_REG_GUEST_R11,
48+
VM_REG_GUEST_R12,
49+
VM_REG_GUEST_R13,
50+
VM_REG_GUEST_R14,
51+
VM_REG_GUEST_R15,
52+
VM_REG_GUEST_RDI,
53+
VM_REG_GUEST_CR0,
54+
VM_REG_GUEST_CR3,
55+
VM_REG_GUEST_CR4,
56+
VM_REG_GUEST_DR7,
57+
VM_REG_GUEST_RSP,
58+
VM_REG_GUEST_RIP,
59+
VM_REG_GUEST_RFLAGS,
60+
VM_REG_GUEST_ES,
61+
VM_REG_GUEST_CS,
62+
VM_REG_GUEST_SS,
63+
VM_REG_GUEST_DS,
64+
VM_REG_GUEST_FS,
65+
VM_REG_GUEST_GS,
66+
VM_REG_GUEST_LDTR,
67+
VM_REG_GUEST_TR,
68+
VM_REG_GUEST_IDTR,
69+
VM_REG_GUEST_GDTR,
70+
VM_REG_GUEST_EFER,
71+
VM_REG_GUEST_CR2,
72+
VM_REG_GUEST_PDPTE0,
73+
VM_REG_GUEST_PDPTE1,
74+
VM_REG_GUEST_PDPTE2,
75+
VM_REG_GUEST_PDPTE3,
76+
VM_REG_GUEST_INTR_SHADOW,
77+
VM_REG_LAST
78+
};
79+
3480
struct vie_op {
3581
uint8_t op_byte; /* actual opcode byte */
3682
uint8_t op_type; /* type of operation (e.g. MOV) */
@@ -67,9 +113,9 @@ struct vie {
67113
uint8_t imm_bytes;
68114

69115
uint8_t scale;
70-
int base_register; /* VM_REG_GUEST_xyz */
71-
int index_register; /* VM_REG_GUEST_xyz */
72-
int segment_register; /* VM_REG_GUEST_xyz */
116+
enum vm_reg_name base_register; /* VM_REG_GUEST_xyz */
117+
enum vm_reg_name index_register; /* VM_REG_GUEST_xyz */
118+
enum vm_reg_name segment_register; /* VM_REG_GUEST_xyz */
73119

74120
int64_t displacement; /* optional addr displacement */
75121
int64_t immediate; /* optional immediate operand */
@@ -139,56 +185,10 @@ struct emul_cnx {
139185
struct vcpu *vcpu;
140186
};
141187

142-
/*
143-
* Identifiers for architecturally defined registers.
144-
*/
145-
enum vm_reg_name {
146-
VM_REG_GUEST_RAX,
147-
VM_REG_GUEST_RBX,
148-
VM_REG_GUEST_RCX,
149-
VM_REG_GUEST_RDX,
150-
VM_REG_GUEST_RBP,
151-
VM_REG_GUEST_RSI,
152-
VM_REG_GUEST_R8,
153-
VM_REG_GUEST_R9,
154-
VM_REG_GUEST_R10,
155-
VM_REG_GUEST_R11,
156-
VM_REG_GUEST_R12,
157-
VM_REG_GUEST_R13,
158-
VM_REG_GUEST_R14,
159-
VM_REG_GUEST_R15,
160-
VM_REG_GUEST_RDI,
161-
VM_REG_GUEST_CR0,
162-
VM_REG_GUEST_CR3,
163-
VM_REG_GUEST_CR4,
164-
VM_REG_GUEST_DR7,
165-
VM_REG_GUEST_RSP,
166-
VM_REG_GUEST_RIP,
167-
VM_REG_GUEST_RFLAGS,
168-
VM_REG_GUEST_ES,
169-
VM_REG_GUEST_CS,
170-
VM_REG_GUEST_SS,
171-
VM_REG_GUEST_DS,
172-
VM_REG_GUEST_FS,
173-
VM_REG_GUEST_GS,
174-
VM_REG_GUEST_LDTR,
175-
VM_REG_GUEST_TR,
176-
VM_REG_GUEST_IDTR,
177-
VM_REG_GUEST_GDTR,
178-
VM_REG_GUEST_EFER,
179-
VM_REG_GUEST_CR2,
180-
VM_REG_GUEST_PDPTE0,
181-
VM_REG_GUEST_PDPTE1,
182-
VM_REG_GUEST_PDPTE2,
183-
VM_REG_GUEST_PDPTE3,
184-
VM_REG_GUEST_INTR_SHADOW,
185-
VM_REG_LAST
186-
};
187-
188-
int vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval);
189-
int vm_set_register(struct vcpu *vcpu, int reg, uint64_t val);
190-
int vm_get_seg_desc(struct vcpu *vcpu, int reg,
188+
int vm_get_register(struct vcpu *vcpu, enum vm_reg_name reg, uint64_t *retval);
189+
int vm_set_register(struct vcpu *vcpu, enum vm_reg_name reg, uint64_t val);
190+
int vm_get_seg_desc(struct vcpu *vcpu, enum vm_reg_name reg,
191191
struct seg_desc *ret_desc);
192-
int vm_set_seg_desc(struct vcpu *vcpu, int reg,
192+
int vm_set_seg_desc(struct vcpu *vcpu, enum vm_reg_name reg,
193193
struct seg_desc *desc);
194194
#endif

0 commit comments

Comments
 (0)