Skip to contentSkip to conversation

x86/hvm: Allow hvm_copy_to_guest_*() to come from const sources

PATCH In review Andrew Cooper 2 participants 2 messages Jul 19, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hvm: Allow hvm_copy_to_guest_*() to come from const sources

The work to fix MISRA rule 7.4 (using mutable pointers to string literals) identifies that string literals do indeed get passed into hvm_copy_to_guest_linear() by way of the PVH dom0 command line.

This higlights that the copy_to_* helpers really ought to take a const source. Update the function types to match, and cast away constness in the wrappers around __hvm_copy() where HVMCOPY_to_guest is used.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/hvm/hvm.c                 | 11 ++++++-----
 xen/arch/x86/include/asm/hvm/support.h |  4 ++--
 2 files changed, 8 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/hvm.c +6 -5
index 57363c2ae124..32dcd34f678c 100644
@@ -3401,9 +3401,10 @@ static enum hvm_translation_result __hvm_copy(
 }
 
 enum hvm_translation_result hvm_copy_to_guest_phys(
-    paddr_t paddr, void *buf, unsigned int size, struct vcpu *v)
+    paddr_t paddr, const void *buf, unsigned int size, struct vcpu *v)
 {
-    return __hvm_copy(buf, paddr, size, v,
+    return __hvm_copy((void *)buf /* HVMCOPY_to_guest doesn't modify */,
+                      paddr, size, v,
                       HVMCOPY_to_guest | HVMCOPY_phys, 0, NULL);
 }
 
@@ -3415,11 +3416,11 @@ enum hvm_translation_result hvm_copy_from_guest_phys(
 }
 
 enum hvm_translation_result hvm_copy_to_guest_linear(
-    unsigned long addr, void *buf, unsigned int size, uint32_t pfec,
+    unsigned long addr, const void *buf, unsigned int size, uint32_t pfec,
     pagefault_info_t *pfinfo)
 {
-    return __hvm_copy(buf, addr, size, current,
-                      HVMCOPY_to_guest | HVMCOPY_linear,
+    return __hvm_copy((void *)buf /* HVMCOPY_to_guest doesn't modify */,
+                      addr, size, current, HVMCOPY_to_guest | HVMCOPY_linear,
                       PFEC_page_present | PFEC_write_access | pfec, pfinfo);
 }
 
xen/arch/x86/include/asm/hvm/support.h +2 -2
index 8d4707e58c9c..142e5985642d 100644
@@ -59,7 +59,7 @@ enum hvm_translation_result {
  * address range does not map entirely onto ordinary machine memory.
  */
 enum hvm_translation_result hvm_copy_to_guest_phys(
-    paddr_t paddr, void *buf, unsigned int size, struct vcpu *v);
+    paddr_t paddr, const void *buf, unsigned int size, struct vcpu *v);
 enum hvm_translation_result hvm_copy_from_guest_phys(
     void *buf, paddr_t paddr, unsigned int size);
 
@@ -85,7 +85,7 @@ typedef struct pagefault_info
 } pagefault_info_t;
 
 enum hvm_translation_result hvm_copy_to_guest_linear(
-    unsigned long addr, void *buf, unsigned int size, uint32_t pfec,
+    unsigned long addr, const void *buf, unsigned int size, uint32_t pfec,
     pagefault_info_t *pfinfo);
 enum hvm_translation_result hvm_copy_from_guest_linear(
     void *buf, unsigned long addr, unsigned int size, uint32_t pfec,
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Andrew Cooper

The work to fix MISRA rule 7.4 (using mutable pointers to string literals) identifies that string literals do indeed get passed into hvm_copy_to_guest_linear() by way of the PVH dom0 command line.

This higlights that the copy_to_* helpers really ought to take a const source. Update the function types to match, and cast away constness in the wrappers around __hvm_copy() where HVMCOPY_to_guest is used.

No functional change.

Well, yes, what do you do.

Jan