Skip to contentSkip to conversation

x86/shadow: use single (atomic) MOV for emulated writes

PATCH In review Jason Andryuk 2 participants 2 messages Jan 16, 2020 – Jan 17, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/shadow: use single (atomic) MOV for emulated writes

This is the corresponding change to the shadow code as made by bf08a8a08a2e "x86/HVM: use single (atomic) MOV for aligned emulated writes" to the non-shadow HVM code.

The bf08a8a08a2e commit message:
Using memcpy() may result in multiple individual byte accesses (depending how memcpy() is implemented and how the resulting insns, e.g. REP MOVSB, get carried out in hardware), which isn't what we want/need for carrying out guest insns as correctly as possible. Fall back to memcpy() only for accesses not 2, 4, or 8 bytes in size.

---
 xen/arch/x86/mm/shadow/hvm.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
xen/arch/x86/mm/shadow/hvm.c +9 -1
index 48dfad4557..a219266fa2 100644
@@ -215,7 +215,15 @@ hvm_emulate_write(enum x86_segment seg,
         return ~PTR_ERR(ptr);
 
     paging_lock(v->domain);
-    memcpy(ptr, p_data, bytes);
+
+    /* Where possible use single (and hence generally atomic) MOV insns. */
+    switch ( bytes )
+    {
+    case 2: write_u16_atomic(ptr, *(uint16_t *)p_data); break;
+    case 4: write_u32_atomic(ptr, *(uint32_t *)p_data); break;
+    case 8: write_u64_atomic(ptr, *(uint64_t *)p_data); break;
+    default: memcpy(ptr, p_data, bytes);                break;
+    }
 
     if ( tb_init_done )
         v->arch.paging.mode->shadow.trace_emul_write_val(ptr, addr,
Signature · 7 lines
2.24.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Jason Andryuk

This is the corresponding change to the shadow code as made by bf08a8a08a2e "x86/HVM: use single (atomic) MOV for aligned emulated writes" to the non-shadow HVM code.

Quoted 8 lines

The bf08a8a08a2e commit message:
Using memcpy() may result in multiple individual byte accesses (depending how memcpy() is implemented and how the resulting insns, e.g. REP MOVSB, get carried out in hardware), which isn't what we want/need for carrying out guest insns as correctly as possible. Fall back to memcpy() only for accesses not 2, 4, or 8 bytes in size.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel