Skip to content

Commit 5aa1ad3

Browse files
Xiangyang Wulijinxia
authored andcommitted
HV:treewide:fix value outside range of underlying type
There are potential value outside range of underlying type in some assignment expressions. This violates Rule 10.3 or Rule 10.4 of MISRA C:2012. BTW, all operations shall be conducted in exactly the same arithmetic (underlying) type, otherwise, there is a value outside range violation. Update related assignment expressions. V1-->V2: * Fix potential overflow in "pit_calibrate_tsc"; * Move PTDEV_INVALID_PIN definition before get_entry_info since this MACRO is only used by debug function. Signed-off-by: Xiangyang Wu <xiangyang.wu@linux.intel.com> Reviewed-by: Junjie Mao <junjie.mao@intel.com>
1 parent c663267 commit 5aa1ad3

File tree

5 files changed

+26
-23
lines changed

5 files changed

+26
-23
lines changed

hypervisor/arch/x86/assign.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,10 @@ void ptdev_remove_msix_remapping(struct vm *vm, uint16_t virt_bdf,
951951
}
952952

953953
#ifdef HV_DEBUG
954+
#define PTDEV_INVALID_PIN 0xffU
954955
static void get_entry_info(struct ptdev_remapping_info *entry, char *type,
955956
uint32_t *irq, uint32_t *vector, uint64_t *dest, bool *lvl_tm,
956-
int *pin, int *vpin, uint32_t *bdf, uint32_t *vbdf)
957+
uint8_t *pin, uint8_t *vpin, uint32_t *bdf, uint32_t *vbdf)
957958
{
958959
struct ptdev_intx_info *intx = &entry->ptdev_intr_info.intx;
959960
if (is_entry_active(entry)) {
@@ -967,8 +968,8 @@ static void get_entry_info(struct ptdev_remapping_info *entry, char *type,
967968
} else {
968969
*lvl_tm = false;
969970
}
970-
*pin = IRQ_INVALID;
971-
*vpin = -1;
971+
*pin = PTDEV_INVALID_PIN;
972+
*vpin = PTDEV_INVALID_PIN;
972973
*bdf = entry->phys_bdf;
973974
*vbdf = entry->virt_bdf;
974975
} else {
@@ -1018,7 +1019,7 @@ void get_ptdev_info(char *str_arg, int str_max)
10181019
char type[16];
10191020
uint64_t dest;
10201021
bool lvl_tm;
1021-
int32_t pin, vpin;
1022+
uint8_t pin, vpin;
10221023
uint32_t bdf, vbdf;
10231024
struct list_head *pos;
10241025

@@ -1043,14 +1044,14 @@ void get_ptdev_info(char *str_arg, int str_max)
10431044
str += len;
10441045

10451046
len = snprintf(str, size,
1046-
"\t%s\t%d\t%d\t%x:%x.%x\t%x:%x.%x",
1047+
"\t%s\t%hhu\t%hhu\t%x:%x.%x\t%x:%x.%x",
10471048
is_entry_active(entry) ?
10481049
(lvl_tm ? "level" : "edge") : "none",
10491050
pin, vpin,
1050-
(bdf & 0xff00U) >> 8,
1051-
(bdf & 0xf8U) >> 3, bdf & 0x7U,
1052-
(vbdf & 0xff00U) >> 8,
1053-
(vbdf & 0xf8U) >> 3, vbdf & 0x7U);
1051+
(bdf & 0xff00U) >> 8U,
1052+
(bdf & 0xf8U) >> 3U, bdf & 0x7U,
1053+
(vbdf & 0xff00U) >> 8U,
1054+
(vbdf & 0xf8U) >> 3U, vbdf & 0x7U);
10541055
size -= len;
10551056
str += len;
10561057
}

hypervisor/arch/x86/gdt.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
#include <hypervisor.h>
88

99
static void set_tss_desc(union tss_64_descriptor *desc,
10-
uint64_t tss, size_t tss_limit, int type)
10+
uint64_t tss, size_t tss_limit, uint32_t type)
1111
{
1212
uint32_t u1, u2, u3;
13+
uint32_t tss_hi_32 = (uint32_t)(tss >> 32U);
14+
uint32_t tss_lo_32 = (uint32_t)tss;
1315

14-
u1 = (uint32_t)((tss << 16U) & 0xFFFFFFFFUL);
15-
u2 = (uint32_t)(tss & 0xFF000000UL);
16-
u3 = (uint32_t)((tss & 0x00FF0000UL) >> 16U);
16+
u1 = tss_lo_32 << 16U;
17+
u2 = tss_lo_32 & 0xFF000000U;
18+
u3 = (tss_lo_32 & 0x00FF0000U) >> 16U;
1719

1820

1921
desc->fields.low32.value = u1 | (tss_limit & 0xFFFFU);
20-
desc->fields.base_addr_63_32 = (uint32_t)(tss >> 32U);
21-
desc->fields.high32.value = (u2 | ((uint32_t)type << 8U) | 0x8000U | u3);
22+
desc->fields.base_addr_63_32 = tss_hi_32;
23+
desc->fields.high32.value = u2 | (type << 8U) | 0x8000U | u3;
2224
}
2325

2426
void load_gdtr_and_tr(void)

hypervisor/arch/x86/timer.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,16 @@ void check_tsc(void)
227227
CPU_CR_WRITE(cr4, (temp64 & ~CR4_TSD));
228228
}
229229

230-
static uint64_t pit_calibrate_tsc(uint16_t cal_ms_arg)
230+
static uint64_t pit_calibrate_tsc(uint32_t cal_ms_arg)
231231
{
232232
#define PIT_TICK_RATE 1193182U
233233
#define PIT_TARGET 0x3FFFU
234234
#define PIT_MAX_COUNT 0xFFFFU
235235

236-
uint16_t cal_ms = cal_ms_arg;
236+
uint32_t cal_ms = cal_ms_arg;
237237
uint32_t initial_pit;
238238
uint16_t current_pit;
239-
uint16_t max_cal_ms;
239+
uint32_t max_cal_ms;
240240
uint64_t current_tsc;
241241
uint8_t initial_pit_high, initial_pit_low;
242242

@@ -246,7 +246,7 @@ static uint64_t pit_calibrate_tsc(uint16_t cal_ms_arg)
246246
/* Assume the 8254 delivers 18.2 ticks per second when 16 bits fully
247247
* wrap. This is about 1.193MHz or a clock period of 0.8384uSec
248248
*/
249-
initial_pit = ((uint32_t)cal_ms * PIT_TICK_RATE) / 1000U;
249+
initial_pit = (cal_ms * PIT_TICK_RATE) / 1000U;
250250
initial_pit += PIT_TARGET;
251251
initial_pit_high = (uint8_t)(initial_pit >> 8U);
252252
initial_pit_low = (uint8_t)initial_pit;
@@ -267,8 +267,8 @@ static uint64_t pit_calibrate_tsc(uint16_t cal_ms_arg)
267267
*/
268268
pio_write8(0x00U, 0x43U);
269269

270-
current_pit = pio_read8(0x40U); /* Read LSB */
271-
current_pit |= pio_read8(0x40U) << 8U; /* Read MSB */
270+
current_pit = (uint16_t)pio_read8(0x40U); /* Read LSB */
271+
current_pit |= (uint16_t)pio_read8(0x40U) << 8U; /* Read MSB */
272272
/* Let the counter count down to PIT_TARGET */
273273
} while (current_pit > PIT_TARGET);
274274

hypervisor/arch/x86/vmx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ static void init_host_state(__unused struct vcpu *vcpu)
11731173
exec_vmwrite(field, idtb.base);
11741174
pr_dbg("VMX_HOST_IDTR_BASE: 0x%x ", idtb.base);
11751175

1176-
value32 = (uint32_t)(msr_read(MSR_IA32_SYSENTER_CS) & 0xFFFFFFFFUL);
1176+
value32 = (uint32_t)msr_read(MSR_IA32_SYSENTER_CS);
11771177
field = VMX_HOST_IA32_SYSENTER_CS;
11781178
exec_vmwrite32(field, value32);
11791179
pr_dbg("VMX_HOST_IA32_SYSENTER_CS: 0x%x ",

hypervisor/include/arch/x86/gdt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
#include <types.h>
7777
#include <cpu.h>
7878

79-
#define TSS_AVAIL (9)
79+
#define TSS_AVAIL (9U)
8080

8181
/*
8282
* Definition of an 8 byte code segment descriptor.

0 commit comments

Comments
 (0)