Skip to content

Commit 4cab8b9

Browse files
mgcaojren1
authored andcommitted
HV: code cleanup as MISRA-C report for guest/vmsr
like: constant of unsigned int need add U/UL as surfix. enum value can't use to give or compare with int directlly. unsigned and signed mis-matched Signed-off-by: Minggui Cao <minggui.cao@intel.com> Reviewed-by: Junjie Mao <junjie.mao@intel.com> Acked-by: Anthony Xu <anthony.xu@intel.com>
1 parent 8c43ad5 commit 4cab8b9

File tree

5 files changed

+78
-78
lines changed

5 files changed

+78
-78
lines changed

hypervisor/arch/x86/guest/guest.c

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <multiboot.h>
1010
#include <reloc.h>
1111

12-
#define ACRN_DBG_GUEST 6
12+
#define ACRN_DBG_GUEST 6U
1313

1414
/* for VM0 e820 */
1515
uint32_t e820_entries;
@@ -18,7 +18,7 @@ struct e820_mem_params e820_mem;
1818

1919
struct page_walk_info {
2020
uint64_t top_entry; /* Top level paging structure entry */
21-
int level;
21+
uint32_t level;
2222
uint32_t width;
2323
bool is_user_mode;
2424
bool is_write_access;
@@ -32,7 +32,7 @@ struct page_walk_info {
3232
inline bool
3333
is_vm0(struct vm *vm)
3434
{
35-
return (vm->attr.boot_idx & 0x7FU) == 0;
35+
return (vm->attr.boot_idx & 0x7FU) == 0U;
3636
}
3737

3838
inline struct vcpu *vcpu_from_vid(struct vm *vm, uint16_t vcpu_id)
@@ -77,7 +77,7 @@ inline struct vcpu *get_primary_vcpu(struct vm *vm)
7777
inline uint64_t vcpumask2pcpumask(struct vm *vm, uint64_t vdmask)
7878
{
7979
uint16_t vcpu_id;
80-
uint64_t dmask = 0;
80+
uint64_t dmask = 0UL;
8181
struct vcpu *vcpu;
8282

8383
for (vcpu_id = ffs64(vdmask); vcpu_id != INVALID_BIT_INDEX;
@@ -129,8 +129,9 @@ enum vm_paging_mode get_vcpu_paging_mode(struct vcpu *vcpu)
129129
static int _gva2gpa_common(struct vcpu *vcpu, struct page_walk_info *pw_info,
130130
uint64_t gva, uint64_t *gpa, uint32_t *err_code)
131131
{
132-
int i;
133-
uint32_t index, shift;
132+
uint32_t i;
133+
uint64_t index;
134+
uint32_t shift;
134135
uint8_t *base;
135136
uint64_t entry;
136137
uint64_t addr, page_size;
@@ -141,15 +142,18 @@ static int _gva2gpa_common(struct vcpu *vcpu, struct page_walk_info *pw_info,
141142
return -EINVAL;
142143

143144
addr = pw_info->top_entry;
144-
for (i = pw_info->level - 1; i >= 0; i--) {
145+
i = pw_info->level;
146+
while (i != 0U) {
147+
i--;
148+
145149
addr = addr & IA32E_REF_MASK;
146150
base = GPA2HVA(vcpu->vm, addr);
147151
if (base == NULL) {
148152
ret = -EFAULT;
149153
goto out;
150154
}
151155

152-
shift = (uint32_t) i * pw_info->width + 12U;
156+
shift = i * pw_info->width + 12U;
153157
index = (gva >> shift) & ((1UL << pw_info->width) - 1UL);
154158
page_size = 1UL << shift;
155159

@@ -188,9 +192,9 @@ static int _gva2gpa_common(struct vcpu *vcpu, struct page_walk_info *pw_info,
188192

189193
entry >>= shift;
190194
/* shift left 12bit more and back to clear XD/Prot Key/Ignored bits */
191-
entry <<= (shift + 12);
192-
entry >>= 12;
193-
*gpa = entry | (gva & (page_size - 1));
195+
entry <<= (shift + 12U);
196+
entry >>= 12U;
197+
*gpa = entry | (gva & (page_size - 1UL));
194198
out:
195199

196200
if (fault != 0) {
@@ -224,7 +228,7 @@ static int _gva2gpa_pae(struct vcpu *vcpu, struct page_walk_info *pw_info,
224228
goto out;
225229
}
226230

227-
pw_info->level = 2;
231+
pw_info->level = 2U;
228232
pw_info->top_entry = entry;
229233
ret = _gva2gpa_common(vcpu, pw_info, gva, gpa, err_code);
230234

@@ -265,24 +269,25 @@ int gva2gpa(struct vcpu *vcpu, uint64_t gva, uint64_t *gpa,
265269

266270
pw_info.top_entry = cur_context->cr3;
267271
pw_info.level = pm;
268-
pw_info.is_write_access = !!(*err_code & PAGE_FAULT_WR_FLAG);
269-
pw_info.is_inst_fetch = !!(*err_code & PAGE_FAULT_ID_FLAG);
272+
pw_info.is_write_access = ((*err_code & PAGE_FAULT_WR_FLAG) != 0U);
273+
pw_info.is_inst_fetch = ((*err_code & PAGE_FAULT_ID_FLAG) != 0U);
270274
pw_info.is_user_mode = ((exec_vmread(VMX_GUEST_CS_SEL) & 0x3UL) == 3UL);
271275
pw_info.pse = true;
272-
pw_info.nxe = cur_context->ia32_efer & MSR_IA32_EFER_NXE_BIT;
273-
pw_info.wp = !!(cur_context->cr0 & CR0_WP);
276+
pw_info.nxe =
277+
((cur_context->ia32_efer & MSR_IA32_EFER_NXE_BIT) != 0UL);
278+
pw_info.wp = ((cur_context->cr0 & CR0_WP) != 0UL);
274279

275280
*err_code &= ~PAGE_FAULT_P_FLAG;
276281

277282
if (pm == PAGING_MODE_4_LEVEL) {
278-
pw_info.width = 9;
283+
pw_info.width = 9U;
279284
ret = _gva2gpa_common(vcpu, &pw_info, gva, gpa, err_code);
280285
} else if(pm == PAGING_MODE_3_LEVEL) {
281-
pw_info.width = 9;
286+
pw_info.width = 9U;
282287
ret = _gva2gpa_pae(vcpu, &pw_info, gva, gpa, err_code);
283288
} else if (pm == PAGING_MODE_2_LEVEL) {
284-
pw_info.width = 10;
285-
pw_info.pse = !!(cur_context->cr4 & CR4_PSE);
289+
pw_info.width = 10U;
290+
pw_info.pse = ((cur_context->cr4 & CR4_PSE) != 0UL);
286291
pw_info.nxe = false;
287292
ret = _gva2gpa_common(vcpu, &pw_info, gva, gpa, err_code);
288293
} else
@@ -296,25 +301,25 @@ int gva2gpa(struct vcpu *vcpu, uint64_t gva, uint64_t *gpa,
296301
return ret;
297302
}
298303

299-
static inline int32_t _copy_gpa(struct vm *vm, void *h_ptr, uint64_t gpa,
304+
static inline uint32_t _copy_gpa(struct vm *vm, void *h_ptr, uint64_t gpa,
300305
uint32_t size, uint32_t fix_pg_size, bool cp_from_vm)
301306
{
302307
uint64_t hpa;
303-
uint32_t off_in_pg, len, pg_size;
308+
uint32_t offset_in_pg, len, pg_size;
304309
void *g_ptr;
305310

306311
hpa = _gpa2hpa(vm, gpa, &pg_size);
307312
if (pg_size == 0U) {
308313
pr_err("GPA2HPA not found");
309-
return -EINVAL;
314+
return 0;
310315
}
311316

312317
if (fix_pg_size != 0U)
313318
pg_size = fix_pg_size;
314319

315-
off_in_pg = gpa & (pg_size - 1);
316-
len = (size > pg_size - off_in_pg) ?
317-
(pg_size - off_in_pg) : size;
320+
offset_in_pg = (uint32_t)gpa & (pg_size - 1U);
321+
len = (size > (pg_size - offset_in_pg)) ?
322+
(pg_size - offset_in_pg) : size;
318323

319324
g_ptr = HPA2HVA(hpa);
320325

@@ -329,32 +334,30 @@ static inline int32_t _copy_gpa(struct vm *vm, void *h_ptr, uint64_t gpa,
329334
static inline int copy_gpa(struct vm *vm, void *h_ptr, uint64_t gpa,
330335
uint32_t size, bool cp_from_vm)
331336
{
332-
int32_t ret;
333337
uint32_t len;
334338

335339
if (vm == NULL) {
336340
pr_err("guest phy addr copy need vm param");
337341
return -EINVAL;
338342
}
339343

340-
do {
341-
ret = _copy_gpa(vm, h_ptr, gpa, size, 0, cp_from_vm);
342-
if (ret < 0)
343-
return ret;
344+
while (size > 0U) {
345+
len = _copy_gpa(vm, h_ptr, gpa, size, 0U, cp_from_vm);
346+
if (len == 0U)
347+
return -EINVAL;
344348

345-
len = (uint32_t) ret;
346349
gpa += len;
347350
h_ptr += len;
348351
size -= len;
349-
} while (size > 0U);
352+
}
350353

351354
return 0;
352355
}
353356

354357
static inline int copy_gva(struct vcpu *vcpu, void *h_ptr, uint64_t gva,
355358
uint32_t size, uint32_t *err_code, bool cp_from_vm)
356359
{
357-
uint64_t gpa = 0;
360+
uint64_t gpa = 0UL;
358361
int32_t ret;
359362
uint32_t len;
360363

@@ -367,24 +370,23 @@ static inline int copy_gva(struct vcpu *vcpu, void *h_ptr, uint64_t gva,
367370
return -EINVAL;
368371
}
369372

370-
do {
373+
while (size > 0U) {
371374
ret = gva2gpa(vcpu, gva, &gpa, err_code);
372375
if (ret < 0) {
373376
pr_err("error[%d] in GVA2GPA, err_code=0x%x",
374377
ret, *err_code);
375378
return ret;
376379
}
377380

378-
ret = _copy_gpa(vcpu->vm, h_ptr, gpa, size,
381+
len = _copy_gpa(vcpu->vm, h_ptr, gpa, size,
379382
PAGE_SIZE_4K, cp_from_vm);
380-
if (ret < 0)
381-
return ret;
383+
if (len == 0U)
384+
return -EINVAL;
382385

383-
len = (uint32_t) ret;
384386
gva += len;
385387
h_ptr += len;
386388
size -= len;
387-
} while (size > 0U);
389+
}
388390

389391
return 0;
390392
}
@@ -569,11 +571,11 @@ static void rebuild_vm0_e820(void)
569571
int prepare_vm0_memmap_and_e820(struct vm *vm)
570572
{
571573
uint32_t i;
572-
uint32_t attr_wb = (IA32E_EPT_R_BIT |
574+
uint64_t attr_wb = (IA32E_EPT_R_BIT |
573575
IA32E_EPT_W_BIT |
574576
IA32E_EPT_X_BIT |
575577
IA32E_EPT_WB);
576-
uint32_t attr_uc = (IA32E_EPT_R_BIT |
578+
uint64_t attr_uc = (IA32E_EPT_R_BIT |
577579
IA32E_EPT_W_BIT |
578580
IA32E_EPT_X_BIT |
579581
IA32E_EPT_UNCACHED);
@@ -598,7 +600,6 @@ int prepare_vm0_memmap_and_e820(struct vm *vm)
598600
entry->length, MAP_MEM, attr_wb);
599601
}
600602

601-
602603
dev_dbg(ACRN_DBG_GUEST, "VM0 e820 layout:\n");
603604
for (i = 0U; i < e820_entries; i++) {
604605
entry = &e820[i];
@@ -613,7 +614,7 @@ int prepare_vm0_memmap_and_e820(struct vm *vm)
613614
* will cause EPT violation if sos accesses hv memory
614615
*/
615616
hv_hpa = get_hv_image_base();
616-
ept_mmap(vm, hv_hpa, hv_hpa, CONFIG_RAM_SIZE, MAP_UNMAP, 0);
617+
ept_mmap(vm, hv_hpa, hv_hpa, CONFIG_RAM_SIZE, MAP_UNMAP, 0U);
617618
return 0;
618619
}
619620

@@ -623,7 +624,8 @@ uint64_t e820_alloc_low_memory(uint32_t size)
623624
struct e820_entry *entry, *new_entry;
624625

625626
/* We want memory in page boundary and integral multiple of pages */
626-
size = ROUND_PAGE_UP(size);
627+
size = ((size + CPU_PAGE_SIZE - 1U) >> CPU_PAGE_SHIFT)
628+
<< CPU_PAGE_SHIFT;
627629

628630
for (i = 0U; i < e820_entries; i++) {
629631
entry = &e820[i];
@@ -823,12 +825,12 @@ static const uint64_t guest_init_gdt[] = {
823825
GUEST_INIT_GDT_DESC_3,
824826
};
825827

826-
uint32_t create_guest_init_gdt(struct vm *vm, uint32_t *limit)
828+
uint64_t create_guest_init_gdt(struct vm *vm, uint32_t *limit)
827829
{
828830
void *gtd_addr = GPA2HVA(vm, GUEST_INIT_GDT_START);
829831

830-
*limit = sizeof(guest_init_gdt) - 1;
831-
(void)memcpy_s(gtd_addr, 64, guest_init_gdt, sizeof(guest_init_gdt));
832+
*limit = sizeof(guest_init_gdt) - 1U;
833+
(void)memcpy_s(gtd_addr, 64U, guest_init_gdt, sizeof(guest_init_gdt));
832834

833835
return GUEST_INIT_GDT_START;
834836
};

hypervisor/arch/x86/guest/vmsr.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ static void enable_msr_interception(uint8_t *bitmap, uint32_t msr)
4343
}
4444

4545
msr &= 0x1FFFU;
46-
value = read_map[(msr>>3)];
47-
value |= 1U<<(msr%8);
46+
value = read_map[(msr>>3U)];
47+
value |= 1U<<(msr%8U);
4848
/* right now we trap for both r/w */
49-
read_map[(msr>>3)] = value;
50-
write_map[(msr>>3)] = value;
49+
read_map[(msr>>3U)] = value;
50+
write_map[(msr>>3U)] = value;
5151
}
5252

5353
/* not used now just leave it for some cases it may be used as API*/
@@ -69,16 +69,16 @@ void disable_msr_interception(uint8_t *bitmap, uint32_t msr)
6969
}
7070

7171
msr &= 0x1FFFU;
72-
value = read_map[(msr>>3)];
73-
value &= ~(1U<<(msr%8));
72+
value = read_map[(msr>>3U)];
73+
value &= ~(1U<<(msr%8U));
7474
/* right now we trap for both r/w */
75-
read_map[(msr>>3)] = value;
76-
write_map[(msr>>3)] = value;
75+
read_map[(msr>>3U)] = value;
76+
write_map[(msr>>3U)] = value;
7777
}
7878

7979
void init_msr_emulation(struct vcpu *vcpu)
8080
{
81-
uint32_t i = 0;
81+
uint32_t i;
8282
uint32_t msrs_count = ARRAY_SIZE(emulated_msrs);
8383
void *msr_bitmap;
8484
uint64_t value64;
@@ -92,7 +92,7 @@ void init_msr_emulation(struct vcpu *vcpu)
9292
/* Allocate and initialize memory for MSR bitmap region*/
9393
vcpu->vm->arch_vm.msr_bitmap = alloc_page();
9494
ASSERT(vcpu->vm->arch_vm.msr_bitmap != NULL, "");
95-
(void)memset(vcpu->vm->arch_vm.msr_bitmap, 0x0, CPU_PAGE_SIZE);
95+
(void)memset(vcpu->vm->arch_vm.msr_bitmap, 0x0U, CPU_PAGE_SIZE);
9696

9797
msr_bitmap = vcpu->vm->arch_vm.msr_bitmap;
9898

@@ -136,7 +136,7 @@ void init_msr_emulation(struct vcpu *vcpu)
136136
(uint64_t *)calloc(msrs_count, sizeof(uint64_t));
137137

138138
ASSERT(vcpu->guest_msrs != NULL, "");
139-
(void)memset(vcpu->guest_msrs, 0, msrs_count * sizeof(uint64_t));
139+
(void)memset(vcpu->guest_msrs, 0U, msrs_count * sizeof(uint64_t));
140140
}
141141

142142
int rdmsr_vmexit_handler(struct vcpu *vcpu)
@@ -242,8 +242,9 @@ int rdmsr_vmexit_handler(struct vcpu *vcpu)
242242

243243
/* Store the MSR contents in RAX and RDX */
244244
vcpu->arch_vcpu.contexts[cur_context].guest_cpu_regs.regs.rax =
245-
v & 0xffffffff;
246-
vcpu->arch_vcpu.contexts[cur_context].guest_cpu_regs.regs.rdx = v >> 32;
245+
v & 0xffffffffU;
246+
vcpu->arch_vcpu.contexts[cur_context].guest_cpu_regs.regs.rdx =
247+
v >> 32U;
247248

248249
TRACE_2L(TRACE_VMEXIT_RDMSR, msr, v);
249250

@@ -259,11 +260,11 @@ int wrmsr_vmexit_handler(struct vcpu *vcpu)
259260
&vcpu->arch_vcpu.contexts[vcpu->arch_vcpu.cur_context];
260261

261262
/* Read the MSR ID */
262-
msr = cur_context->guest_cpu_regs.regs.rcx;
263+
msr = (uint32_t)cur_context->guest_cpu_regs.regs.rcx;
263264

264265
/* Get the MSR contents */
265-
v = (((uint64_t) cur_context->guest_cpu_regs.regs.rdx) << 32) |
266-
((uint64_t) cur_context->guest_cpu_regs.regs.rax);
266+
v = (cur_context->guest_cpu_regs.regs.rdx << 32U) |
267+
cur_context->guest_cpu_regs.regs.rax;
267268

268269
/* Do the required processing for each msr case */
269270
switch (msr) {

hypervisor/bsp/include/bsp_extern.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#ifndef BSP_EXTERN_H
1919
#define BSP_EXTERN_H
2020

21-
#define UOS_DEFAULT_START_ADDR (0x100000000)
21+
#define UOS_DEFAULT_START_ADDR (0x100000000UL)
2222
/**********************************/
2323
/* EXTERNAL VARIABLES */
2424
/**********************************/

0 commit comments

Comments
 (0)