Skip to content

Commit 4b6dc02

Browse files
Shawnshhacrnsi
authored andcommitted
HV: fix vmptable misc violations
Fix the violations list below: 1.Function should have one return entry. 2.Do not use -- or ++ operation. 3.For loop should be simple, shall not use comma operations. Tracked-On: #861 Signed-off-by: Huihuang Shi <huihuang.shi@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent 564a601 commit 4b6dc02

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

hypervisor/arch/x86/configs/vmptable.c

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,18 @@ static struct proc_entry proc_entry_template = {
6565
.feature_flags = MPEP_FEATURES
6666
};
6767

68-
static uint8_t mpt_compute_checksum(void *base, size_t len)
68+
static uint8_t mpt_compute_checksum(const void *base, size_t len)
6969
{
70-
uint8_t *bytes;
71-
uint8_t sum;
72-
size_t length = len;
70+
uint8_t sum = 0U;
71+
size_t length;
72+
const uint8_t *bytes = base;
7373

74-
for (bytes = base, sum = 0U; length > 0U; length--) {
74+
for (length = len; length > 0U; length--) {
7575
sum += *bytes;
7676
bytes++;
7777
}
7878

79-
return (256U - sum);
79+
return (~sum) + 1U;
8080
}
8181

8282
/**
@@ -92,6 +92,7 @@ int32_t mptable_build(struct acrn_vm *vm)
9292
size_t mptable_length;
9393
uint16_t i;
9494
uint16_t vcpu_num;
95+
int32_t ret = 0;
9596
uint64_t pcpu_bitmap = 0U;
9697
struct mptable_info *mptable;
9798
struct acrn_vm_config *vm_config;
@@ -110,35 +111,36 @@ int32_t mptable_build(struct acrn_vm *vm)
110111
+ MPEII_NUM_LOCAL_IRQ * sizeof(struct int_entry);
111112

112113
mptable_length = sizeof(struct mpfps) + mptable->mpch.base_table_length;
113-
if (mptable_length > MPTABLE_MAX_LENGTH) {
114-
return -1;
115-
}
116-
117-
for (i = 0U; i < vcpu_num; i++) {
118-
uint16_t pcpu_id = ffs64(pcpu_bitmap);
119-
120-
(void)memcpy_s((void *)(mptable->proc_entry_array + i), sizeof(struct proc_entry),
121-
(const void *)&proc_entry_template, sizeof(struct proc_entry));
122-
mptable->proc_entry_array[i].apic_id = (uint8_t) i;
123-
if (i == 0U) {
124-
mptable->proc_entry_array[i].cpu_flags |= PROCENTRY_FLAG_BP;
114+
if (mptable_length <= MPTABLE_MAX_LENGTH) {
115+
for (i = 0U; i < vcpu_num; i++) {
116+
uint16_t pcpu_id = ffs64(pcpu_bitmap);
117+
118+
(void)memcpy_s((void *)(mptable->proc_entry_array + i), sizeof(struct proc_entry),
119+
(const void *)&proc_entry_template, sizeof(struct proc_entry));
120+
mptable->proc_entry_array[i].apic_id = (uint8_t) i;
121+
if (i == 0U) {
122+
mptable->proc_entry_array[i].cpu_flags |= PROCENTRY_FLAG_BP;
123+
}
124+
bitmap_clear_lock(pcpu_id, &pcpu_bitmap);
125125
}
126-
bitmap_clear_lock(pcpu_id, &pcpu_bitmap);
127-
}
128126

129-
/* Copy mptable info into guest memory */
130-
(void)copy_to_gpa(vm, (void *)mptable, MPTABLE_BASE, mptable_length);
131-
132-
startaddr = (char *)gpa2hva(vm, MPTABLE_BASE);
133-
curraddr = startaddr;
134-
stac();
135-
mpfp = (struct mpfps *)curraddr;
136-
mpfp->checksum = mpt_compute_checksum(mpfp, sizeof(struct mpfps));
137-
curraddr += sizeof(struct mpfps);
138-
139-
mpch = (struct mpcth *)curraddr;
140-
mpch->checksum = mpt_compute_checksum(mpch, mpch->base_table_length);
141-
clac();
127+
/* Copy mptable info into guest memory */
128+
(void)copy_to_gpa(vm, (void *)mptable, MPTABLE_BASE, mptable_length);
129+
130+
startaddr = (char *)gpa2hva(vm, MPTABLE_BASE);
131+
curraddr = startaddr;
132+
stac();
133+
mpfp = (struct mpfps *)curraddr;
134+
mpfp->checksum = mpt_compute_checksum(mpfp, sizeof(struct mpfps));
135+
curraddr += sizeof(struct mpfps);
136+
137+
mpch = (struct mpcth *)curraddr;
138+
mpch->checksum = mpt_compute_checksum(mpch, mpch->base_table_length);
139+
clac();
140+
ret = 0;
141+
} else {
142+
ret = -1;
143+
}
142144

143-
return 0U;
145+
return ret;
144146
}

0 commit comments

Comments
 (0)