Skip to content

Commit 79463fd

Browse files
lifeixwenlingz
authored andcommitted
hv: avoid using of mixed mode arithmetic
Avoid using of mixed mode arithmetic by using explicit casts Tracked-On: #861 Signed-off-by: Li, Fei1 <fei1.li@intel.com>
1 parent 9c133c7 commit 79463fd

File tree

5 files changed

+42
-46
lines changed

5 files changed

+42
-46
lines changed

hypervisor/arch/x86/cpu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ void start_cpus(void)
636636
/* Wait until global count is equal to expected CPU up count or
637637
* configured time-out has expired
638638
*/
639-
timeout = CONFIG_CPU_UP_TIMEOUT * 1000U;
639+
timeout = (uint32_t)CONFIG_CPU_UP_TIMEOUT * 1000U;
640640
while ((atomic_load16(&up_count) != expected_up) && (timeout != 0U)) {
641641
/* Delay 10us */
642642
udelay(10U);
@@ -664,7 +664,7 @@ void stop_cpus(void)
664664
uint16_t pcpu_id, expected_up;
665665
uint32_t timeout;
666666

667-
timeout = CONFIG_CPU_UP_TIMEOUT * 1000U;
667+
timeout = (uint32_t)CONFIG_CPU_UP_TIMEOUT * 1000U;
668668
for (pcpu_id = 0U; pcpu_id < phys_cpu_num; pcpu_id++) {
669669
if (get_cpu_id() == pcpu_id) { /* avoid offline itself */
670670
continue;

hypervisor/arch/x86/guest/instr_emul.c

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ static void vie_calc_bytereg(const struct instr_emul_vie *vie,
512512
enum cpu_reg_name *reg, int *lhbr)
513513
{
514514
*lhbr = 0;
515-
*reg = vie->reg;
515+
*reg = (enum cpu_reg_name)(vie->reg);
516516

517517
/*
518518
* 64-bit mode imposes limitations on accessing legacy high byte
@@ -529,7 +529,7 @@ static void vie_calc_bytereg(const struct instr_emul_vie *vie,
529529
if (vie->rex_present == 0U) {
530530
if ((vie->reg & 0x4U) != 0U) {
531531
*lhbr = 1;
532-
*reg = vie->reg & 0x3U;
532+
*reg = (enum cpu_reg_name)(vie->reg & 0x3U);
533533
}
534534
}
535535
}
@@ -689,7 +689,7 @@ static int emulate_mov(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie)
689689
* REX.W + 89/r mov r/m64, r64
690690
*/
691691

692-
reg = vie->reg;
692+
reg = (enum cpu_reg_name)(vie->reg);
693693
val = vm_get_register(vcpu, reg);
694694
val &= size2mask[size];
695695
vie_mmio_write(vcpu, val);
@@ -712,7 +712,7 @@ static int emulate_mov(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie)
712712
* REX.W 8B/r: mov r64, r/m64
713713
*/
714714
vie_mmio_read(vcpu, &val);
715-
reg = vie->reg;
715+
reg = (enum cpu_reg_name)(vie->reg);
716716
vie_update_register(vcpu, reg, val, size);
717717
break;
718718
case 0xA1U:
@@ -796,7 +796,7 @@ static int emulate_movx(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie
796796
vie_mmio_read(vcpu, &val);
797797

798798
/* get the second operand */
799-
reg = vie->reg;
799+
reg = (enum cpu_reg_name)(vie->reg);
800800

801801
/* zero-extend byte */
802802
val = (uint8_t)val;
@@ -814,7 +814,7 @@ static int emulate_movx(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie
814814
*/
815815
vie_mmio_read(vcpu, &val);
816816

817-
reg = vie->reg;
817+
reg = (enum cpu_reg_name)(vie->reg);
818818

819819
/* zero-extend word */
820820
val = (uint16_t)val;
@@ -835,7 +835,7 @@ static int emulate_movx(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie
835835
vie_mmio_read(vcpu, &val);
836836

837837
/* get the second operand */
838-
reg = vie->reg;
838+
reg = (enum cpu_reg_name)(vie->reg);
839839

840840
/* sign extend byte */
841841
val = (int8_t)val;
@@ -969,8 +969,8 @@ static int emulate_movs(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie
969969
uint64_t rcx, rdi, rsi, rflags;
970970
uint32_t err_code;
971971
enum cpu_reg_name seg;
972-
int error, repeat;
973-
uint8_t opsize = vie->opsize;
972+
int error;
973+
uint8_t repeat, opsize = vie->opsize;
974974
bool is_mmio_write;
975975

976976
error = 0;
@@ -985,7 +985,7 @@ static int emulate_movs(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie
985985
*/
986986
repeat = vie->repz_present | vie->repnz_present;
987987

988-
if (repeat != 0) {
988+
if (repeat != 0U) {
989989
rcx = vm_get_register(vcpu, CPU_REG_RCX);
990990

991991
/*
@@ -1034,7 +1034,7 @@ static int emulate_movs(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie
10341034
vie_update_register(vcpu, CPU_REG_RSI, rsi, vie->addrsize);
10351035
vie_update_register(vcpu, CPU_REG_RDI, rdi, vie->addrsize);
10361036

1037-
if (repeat != 0) {
1037+
if (repeat != 0U) {
10381038
rcx = rcx - 1;
10391039
vie_update_register(vcpu, CPU_REG_RCX, rcx, vie->addrsize);
10401040

@@ -1051,14 +1051,13 @@ static int emulate_movs(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie
10511051

10521052
static int emulate_stos(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie)
10531053
{
1054-
int repeat;
1055-
uint8_t opsize = vie->opsize;
1054+
uint8_t repeat, opsize = vie->opsize;
10561055
uint64_t val;
10571056
uint64_t rcx, rdi, rflags;
10581057

10591058
repeat = vie->repz_present | vie->repnz_present;
10601059

1061-
if (repeat != 0) {
1060+
if (repeat != 0U) {
10621061
rcx = vm_get_register(vcpu, CPU_REG_RCX);
10631062

10641063
/*
@@ -1085,7 +1084,7 @@ static int emulate_stos(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie
10851084

10861085
vie_update_register(vcpu, CPU_REG_RDI, rdi, vie->addrsize);
10871086

1088-
if (repeat != 0) {
1087+
if (repeat != 0U) {
10891088
rcx = rcx - 1;
10901089
vie_update_register(vcpu, CPU_REG_RCX, rcx, vie->addrsize);
10911090

@@ -1129,7 +1128,7 @@ static int emulate_test(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie
11291128
*/
11301129

11311130
/* get the first operand */
1132-
reg = vie->reg;
1131+
reg = (enum cpu_reg_name)(vie->reg);
11331132
val1 = vm_get_register(vcpu, reg);
11341133

11351134
/* get the second operand */
@@ -1186,7 +1185,7 @@ static int emulate_and(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie)
11861185
*/
11871186

11881187
/* get the first operand */
1189-
reg = vie->reg;
1188+
reg = (enum cpu_reg_name)(vie->reg);
11901189
val1 = vm_get_register(vcpu, reg);
11911190

11921191
/* get the second operand */
@@ -1299,7 +1298,7 @@ static int emulate_or(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie)
12991298
vie_mmio_read(vcpu, &val1);
13001299

13011300
/* get the second operand */
1302-
reg = vie->reg;
1301+
reg = (enum cpu_reg_name)(vie->reg);
13031302
val2 = vm_get_register(vcpu, reg);
13041303

13051304
/* perform the operation and write the result */
@@ -1360,7 +1359,7 @@ static int emulate_cmp(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie)
13601359
*/
13611360

13621361
/* Get the register operand */
1363-
reg = vie->reg;
1362+
reg = (enum cpu_reg_name)(vie->reg);
13641363
regop = vm_get_register(vcpu, reg);
13651364

13661365
/* Get the memory operand */
@@ -1440,7 +1439,7 @@ static int emulate_sub(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie)
14401439
*/
14411440

14421441
/* get the first operand */
1443-
reg = vie->reg;
1442+
reg = (enum cpu_reg_name)(vie->reg);
14441443
val1 = vm_get_register(vcpu, reg);
14451444

14461445
/* get the second operand */
@@ -1847,7 +1846,7 @@ static int decode_modrm(struct instr_emul_vie *vie, enum vm_cpu_mode cpu_mode)
18471846
goto done;
18481847
}
18491848

1850-
vie->base_register = vie->rm;
1849+
vie->base_register = (enum cpu_reg_name)vie->rm;
18511850

18521851
switch (vie->mod) {
18531852
case VIE_MOD_INDIRECT_DISP8:
@@ -1942,7 +1941,7 @@ static int decode_sib(struct instr_emul_vie *vie)
19421941
*/
19431942
vie->disp_bytes = 4U;
19441943
} else {
1945-
vie->base_register = vie->base;
1944+
vie->base_register = (enum cpu_reg_name)vie->base;
19461945
}
19471946

19481947
/*
@@ -1953,7 +1952,7 @@ static int decode_sib(struct instr_emul_vie *vie)
19531952
* Table 2-5: Special Cases of REX Encodings
19541953
*/
19551954
if (vie->index != 4U) {
1956-
vie->index_register = vie->index;
1955+
vie->index_register = (enum cpu_reg_name)vie->index;
19571956
}
19581957

19591958
/* 'scale' makes sense only in the context of an index register */
@@ -1968,8 +1967,7 @@ static int decode_sib(struct instr_emul_vie *vie)
19681967

19691968
static int decode_displacement(struct instr_emul_vie *vie)
19701969
{
1971-
int n, i;
1972-
uint8_t x;
1970+
uint8_t n, i, x;
19731971

19741972
union {
19751973
uint8_t buf[4];
@@ -1978,17 +1976,17 @@ static int decode_displacement(struct instr_emul_vie *vie)
19781976
} u;
19791977

19801978
n = vie->disp_bytes;
1981-
if (n == 0) {
1979+
if (n == 0U) {
19821980
return 0;
19831981
}
19841982

1985-
if ((n != 1) && (n != 4)) {
1983+
if ((n != 1U) && (n != 4U)) {
19861984
pr_err("%s: decode_displacement: invalid disp_bytes %d",
19871985
__func__, n);
19881986
return -EINVAL;
19891987
}
19901988

1991-
for (i = 0; i < n; i++) {
1989+
for (i = 0U; i < n; i++) {
19921990
if (vie_peek(vie, &x) != 0) {
19931991
return -1;
19941992
}
@@ -1997,7 +1995,7 @@ static int decode_displacement(struct instr_emul_vie *vie)
19971995
vie_advance(vie);
19981996
}
19991997

2000-
if (n == 1) {
1998+
if (n == 1U) {
20011999
vie->displacement = u.signed8; /* sign-extended */
20022000
} else {
20032001
vie->displacement = u.signed32; /* sign-extended */
@@ -2008,8 +2006,7 @@ static int decode_displacement(struct instr_emul_vie *vie)
20082006

20092007
static int decode_immediate(struct instr_emul_vie *vie)
20102008
{
2011-
int i, n;
2012-
uint8_t x;
2009+
uint8_t i, n, x;
20132010
union {
20142011
uint8_t buf[4];
20152012
int8_t signed8;
@@ -2039,17 +2036,17 @@ static int decode_immediate(struct instr_emul_vie *vie)
20392036
}
20402037

20412038
n = vie->imm_bytes;
2042-
if (n == 0) {
2039+
if (n == 0U) {
20432040
return 0;
20442041
}
20452042

2046-
if ((n != 1) && (n != 2) && (n != 4)) {
2043+
if ((n != 1U) && (n != 2U) && (n != 4U)) {
20472044
pr_err("%s: invalid number of immediate bytes: %d",
20482045
__func__, n);
20492046
return -EINVAL;
20502047
}
20512048

2052-
for (i = 0; i < n; i++) {
2049+
for (i = 0U; i < n; i++) {
20532050
if (vie_peek(vie, &x) != 0) {
20542051
return -1;
20552052
}
@@ -2059,9 +2056,9 @@ static int decode_immediate(struct instr_emul_vie *vie)
20592056
}
20602057

20612058
/* sign-extend the immediate value before use */
2062-
if (n == 1) {
2059+
if (n == 1U) {
20632060
vie->immediate = u.signed8;
2064-
} else if (n == 2) {
2061+
} else if (n == 2U) {
20652062
vie->immediate = u.signed16;
20662063
} else {
20672064
vie->immediate = u.signed32;
@@ -2317,7 +2314,7 @@ int32_t emulate_instruction(const struct acrn_vcpu *vcpu)
23172314
pr_err("%s: Failed to get instr_emul_ctxt", __func__);
23182315
ret = -1;
23192316
} else {
2320-
ret = vmm_emulate_instruction(ctxt);
2317+
ret = vmm_emulate_instruction(ctxt);
23212318
}
23222319

23232320
return ret;

hypervisor/arch/x86/guest/pm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
int validate_pstate(const struct acrn_vm *vm, uint64_t perf_ctl)
1010
{
1111
const struct cpu_px_data *px_data;
12-
int i, px_cnt;
12+
uint8_t i, px_cnt;
1313

1414
if (is_vm0(vm)) {
1515
return 0;
@@ -18,11 +18,11 @@ int validate_pstate(const struct acrn_vm *vm, uint64_t perf_ctl)
1818
px_cnt = vm->pm.px_cnt;
1919
px_data = vm->pm.px_data;
2020

21-
if ((px_cnt == 0) || (px_data == NULL)) {
21+
if ((px_cnt == 0U) || (px_data == NULL)) {
2222
return -1;
2323
}
2424

25-
for (i = 0; i < px_cnt; i++) {
25+
for (i = 0U; i < px_cnt; i++) {
2626
if ((px_data + i)->control == (perf_ctl & 0xffffUL)) {
2727
return 0;
2828
}

hypervisor/arch/x86/guest/vlapic.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ vlapic_lvtt_tsc_deadline(const struct acrn_vlapic *vlapic)
267267
static inline bool
268268
vlapic_lvtt_masked(const struct acrn_vlapic *vlapic)
269269
{
270-
return ((vlapic->apic_page.lvt[APIC_LVT_TIMER].v) & APIC_LVTT_M)
271-
!= 0U;
270+
return (((vlapic->apic_page.lvt[APIC_LVT_TIMER].v) & APIC_LVTT_M) != 0U);
272271
}
273272

274273
/**

hypervisor/common/hypercall.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ int32_t hcall_get_cpu_pm_state(struct acrn_vm *vm, uint64_t cmd, uint64_t param)
973973
return 0;
974974
}
975975
case PMCMD_GET_PX_DATA: {
976-
int32_t pn;
976+
uint8_t pn;
977977
struct cpu_px_data *px_data;
978978

979979
/* For now we put px data as per-vm,
@@ -984,7 +984,7 @@ int32_t hcall_get_cpu_pm_state(struct acrn_vm *vm, uint64_t cmd, uint64_t param)
984984
return -1;
985985
}
986986

987-
pn = (cmd & PMCMD_STATE_NUM_MASK) >> PMCMD_STATE_NUM_SHIFT;
987+
pn = (uint8_t)((cmd & PMCMD_STATE_NUM_MASK) >> PMCMD_STATE_NUM_SHIFT);
988988
if (pn >= target_vm->pm.px_cnt) {
989989
return -1;
990990
}

0 commit comments

Comments
 (0)