Skip to content

Commit 27fbf9b

Browse files
wuxyintellijinxia
authored andcommitted
HV:treewide:Fixing pointer castings
In the hypervisor, there are many casts from an void pointer to integer pointer, then from integer pointer to structure pointer. These pointer castings are detected by static analysis tool. All pointer casts are violations, There are some duplicated pointer cast. This will make deviation analysis complex. BTW, there are one useless pointer casting and one wrong pointer casting in the hypervisor. Remvoe duplicated pointer casts to make deviation analysis simple; Remove one useless pointer casting; Update one wrong pointer casting. Note: There are many void type pointer casts, non-void type pointer is casted to void type pointer, char type pointer casts, non-char type pointer is casted to char type pointer. These pointer casting is need by the memory management module, IO moudle etc. Deviation analysis will be made and recoded in the analysis report. V1-->V2: Fix mixing pointer and array voilation. V2-->V3: Remvoe pointer casting from integer pointer into non-void/non-char pointer directly; Remove redundant type conversion. Signed-off-by: Xiangyang Wu <xiangyang.wu@intel.com> Reviewed-by: Junjie Mao <junjie.mao@intel.com>
1 parent a368b57 commit 27fbf9b

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

hypervisor/arch/x86/guest/guest.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static int _gva2gpa_common(struct vcpu *vcpu, struct page_walk_info *pw_info,
140140
uint32_t i;
141141
uint64_t index;
142142
uint32_t shift;
143-
uint8_t *base;
143+
void *base;
144144
uint64_t entry;
145145
uint64_t addr, page_size;
146146
int ret = 0;
@@ -167,10 +167,12 @@ static int _gva2gpa_common(struct vcpu *vcpu, struct page_walk_info *pw_info,
167167
page_size = 1UL << shift;
168168

169169
if (pw_info->width == 10U) {
170+
uint32_t *base32 = (uint32_t *)base;
170171
/* 32bit entry */
171-
entry = *((uint32_t *)(base + (4U * index)));
172+
entry = (uint64_t)(*(base32 + index));
172173
} else {
173-
entry = *((uint64_t *)(base + (8U * index)));
174+
uint64_t *base64 = (uint64_t *)base;
175+
entry = *(base64 + index);
174176
}
175177

176178
/* check if the entry present */

hypervisor/arch/x86/guest/instr_emul.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ emulate_movs(struct vcpu *vcpu, __unused uint64_t gpa, struct vie *vie,
746746
goto done;
747747
}
748748

749-
(void)memcpy_s((char *)dstaddr, 16U, (char *)srcaddr, opsize);
749+
(void)memcpy_s((void *)dstaddr, 16U, (void *)srcaddr, opsize);
750750

751751
error = vie_read_register(vcpu, CPU_REG_RSI, &rsi);
752752
error = vie_read_register(vcpu, CPU_REG_RDI, &rdi);

hypervisor/arch/x86/vmx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ static void init_exec_ctrl(struct vcpu *vcpu)
12051205
{
12061206
uint32_t value32;
12071207
uint64_t value64;
1208-
struct vm *vm = (struct vm *) vcpu->vm;
1208+
struct vm *vm = vcpu->vm;
12091209

12101210
/* Log messages to show initializing VMX execution controls */
12111211
pr_dbg("*****************************");

hypervisor/arch/x86/vtd.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -933,9 +933,9 @@ static int add_iommu_device(struct iommu_domain *domain, uint16_t segment,
933933
uint8_t bus, uint8_t devfun)
934934
{
935935
struct dmar_drhd_rt *dmar_uint;
936-
uint64_t *root_table;
936+
struct dmar_root_entry *root_table;
937937
uint64_t context_table_addr;
938-
uint64_t *context_table;
938+
struct dmar_context_entry *context_table;
939939
struct dmar_root_entry *root_entry;
940940
struct dmar_context_entry *context_entry;
941941
uint64_t upper = 0UL;
@@ -975,9 +975,9 @@ static int add_iommu_device(struct iommu_domain *domain, uint16_t segment,
975975
}
976976
}
977977

978-
root_table = (uint64_t *)HPA2HVA(dmar_uint->root_table_addr);
978+
root_table = (struct dmar_root_entry *)HPA2HVA(dmar_uint->root_table_addr);
979979

980-
root_entry = (struct dmar_root_entry *)&root_table[bus * 2];
980+
root_entry = root_table + bus;
981981

982982
if (dmar_get_bitslice(root_entry->lower,
983983
ROOT_ENTRY_LOWER_PRESENT_MASK,
@@ -1014,8 +1014,8 @@ static int add_iommu_device(struct iommu_domain *domain, uint16_t segment,
10141014

10151015
context_table_addr = context_table_addr << 12;
10161016

1017-
context_table = (uint64_t *)HPA2HVA(context_table_addr);
1018-
context_entry = (struct dmar_context_entry *)&context_table[devfun * 2];
1017+
context_table = (struct dmar_context_entry *)HPA2HVA(context_table_addr);
1018+
context_entry = context_table + devfun;
10191019

10201020
/* the context entry should not be present */
10211021
if (dmar_get_bitslice(context_entry->lower,
@@ -1088,9 +1088,9 @@ remove_iommu_device(struct iommu_domain *domain, uint16_t segment,
10881088
uint8_t bus, uint8_t devfun)
10891089
{
10901090
struct dmar_drhd_rt *dmar_uint;
1091-
uint64_t *root_table;
1091+
struct dmar_root_entry *root_table;
10921092
uint64_t context_table_addr;
1093-
uint64_t *context_table;
1093+
struct dmar_context_entry *context_table;
10941094
struct dmar_root_entry *root_entry;
10951095
struct dmar_context_entry *context_entry;
10961096
uint16_t dom_id;
@@ -1106,16 +1106,16 @@ remove_iommu_device(struct iommu_domain *domain, uint16_t segment,
11061106
return 1;
11071107
}
11081108

1109-
root_table = (uint64_t *)HPA2HVA(dmar_uint->root_table_addr);
1110-
root_entry = (struct dmar_root_entry *)&root_table[bus * 2];
1109+
root_table = (struct dmar_root_entry *)HPA2HVA(dmar_uint->root_table_addr);
1110+
root_entry = root_table + bus;
11111111

11121112
context_table_addr = dmar_get_bitslice(root_entry->lower,
11131113
ROOT_ENTRY_LOWER_CTP_MASK,
11141114
ROOT_ENTRY_LOWER_CTP_POS);
11151115
context_table_addr = context_table_addr << 12;
1116-
context_table = (uint64_t *)HPA2HVA(context_table_addr);
1116+
context_table = (struct dmar_context_entry *)HPA2HVA(context_table_addr);
11171117

1118-
context_entry = (struct dmar_context_entry *)&context_table[devfun * 2];
1118+
context_entry = context_table + devfun;
11191119

11201120
dom_id = (uint16_t)dmar_get_bitslice(context_entry->upper,
11211121
CTX_ENTRY_UPPER_DID_MASK, CTX_ENTRY_UPPER_DID_POS);

0 commit comments

Comments
 (0)