Skip to contentSkip to conversation

Various XSA followups

PATCH In review Andrew Cooper 4 participants 28 messages Jun 21, 2017 – Jul 4, 2017 5 messages not in the archive

lore
Share & tools
lore
#0 lore raw

cover Various XSA followups

XSA-219 was discovered while trying to implement the bugfix in patch 6.

Andrew Cooper (6):
x86/hvm: Fixes to hvmemul_insn_fetch()
x86/shadow: Fixes to hvm_emulate_insn_fetch()
x86/shadow: Use ERR_PTR infrastructure for sh_emulate_map_dest() [RFC] x86/hvm: Rename enum hvm_copy_result to hvm_translation_result x86/hvm: Break out __hvm_copy()'s translation logic x86/hvm: Implement hvmemul_write() using real mappings

 xen/arch/x86/hvm/dom0_build.c     |   2 +-
 xen/arch/x86/hvm/emulate.c        | 224 ++++++++++++++++++++++++++++++++------
 xen/arch/x86/hvm/hvm.c            | 181 +++++++++++++++++-------------
 xen/arch/x86/hvm/intercept.c      |  20 ++--
 xen/arch/x86/hvm/svm/nestedsvm.c  |   5 +-
 xen/arch/x86/hvm/svm/svm.c        |   2 +-
 xen/arch/x86/hvm/viridian.c       |   2 +-
 xen/arch/x86/hvm/vmsi.c           |   2 +-
 xen/arch/x86/hvm/vmx/realmode.c   |   2 +-
 xen/arch/x86/hvm/vmx/vvmx.c       |  14 +--
 xen/arch/x86/mm/shadow/common.c   |  22 ++--
 xen/arch/x86/mm/shadow/multi.c    |   8 +-
 xen/arch/x86/mm/shadow/private.h  |   7 +-
 xen/common/libelf/libelf-loader.c |   4 +-
 xen/include/asm-x86/hvm/emulate.h |   7 ++
 xen/include/asm-x86/hvm/support.h |  52 +++++----
 16 files changed, 379 insertions(+), 175 deletions(-)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

PATCH 1/6 x86/hvm: Fixes to hvmemul_insn_fetch()

Force insn_off to a single byte, as offset can wrap around or truncate with respect to sh_ctxt->insn_buf_eip under a number of normal circumstances.

Furthermore, don't use an ASSERT() for bounds checking the write into hvmemul_ctxt->insn_buf[].

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>

For anyone wondering, this is way to explot XSA-186

---
 xen/arch/x86/hvm/emulate.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/emulate.c +13 -2
index 11e4aba..495e312 100644
@@ -939,7 +939,8 @@ int hvmemul_insn_fetch(
 {
     struct hvm_emulate_ctxt *hvmemul_ctxt =
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
-    unsigned int insn_off = offset - hvmemul_ctxt->insn_buf_eip;
+    /* Careful, as offset can wrap or truncate WRT insn_buf_eip. */
+    uint8_t insn_off = offset - hvmemul_ctxt->insn_buf_eip;
 
     /*
      * Fall back if requested bytes are not in the prefetch cache.
@@ -953,7 +954,17 @@ int hvmemul_insn_fetch(
 
         if ( rc == X86EMUL_OKAY && bytes )
         {
-            ASSERT(insn_off + bytes <= sizeof(hvmemul_ctxt->insn_buf));
+            /*
+             * Will we overflow insn_buf[]?  This shouldn't be able to happen,
+             * which means something went wrong with instruction decoding...
+             */
+            if ( insn_off > sizeof(hvmemul_ctxt->insn_buf) ||
+                 (insn_off + bytes) > sizeof(hvmemul_ctxt->insn_buf) )
+            {
+                ASSERT_UNREACHABLE();
+                return X86EMUL_UNHANDLEABLE;
+            }
+
             memcpy(&hvmemul_ctxt->insn_buf[insn_off], p_data, bytes);
             hvmemul_ctxt->insn_buf_bytes = insn_off + bytes;
         }
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#2 lore raw

Re: [PATCH 1/6] x86/hvm: Fixes to hvmemul_insn_fetch()

Quoted 29 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 21 June 2017 16:12
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Jan Beulich <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH 1/6] x86/hvm: Fixes to hvmemul_insn_fetch()

Force insn_off to a single byte, as offset can wrap around or truncate with respect to sh_ctxt->insn_buf_eip under a number of normal circumstances.

Furthermore, don't use an ASSERT() for bounds checking the write into hvmemul_ctxt->insn_buf[].

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>

For anyone wondering, this is way to explot XSA-186

---
 xen/arch/x86/hvm/emulate.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/emulate.c +0 -0
index 11e4aba..495e312 100644

xen/arch/x86/hvm/emulate.c int hvmemul_insn_fetch( · line 943

Quoted 6 lines
xen/arch/x86/hvm/emulate.c +2 -1
@@ -939,7 +939,8 @@ int hvmemul_insn_fetch(
 {
     struct hvm_emulate_ctxt *hvmemul_ctxt =
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
-    unsigned int insn_off = offset - hvmemul_ctxt->insn_buf_eip;
+    /* Careful, as offset can wrap or truncate WRT insn_buf_eip. */
+    uint8_t insn_off = offset - hvmemul_ctxt->insn_buf_eip;

Why the change to a uint8_t?

Paul

Quoted 23 lines
xen/arch/x86/hvm/emulate.c +11 -2
     /*
      * Fall back if requested bytes are not in the prefetch cache.
@@ -953,7 +954,17 @@ int hvmemul_insn_fetch(
 
         if ( rc == X86EMUL_OKAY && bytes )
         {
-            ASSERT(insn_off + bytes <= sizeof(hvmemul_ctxt->insn_buf));
+            /*
+             * Will we overflow insn_buf[]?  This shouldn't be able to happen,
+             * which means something went wrong with instruction decoding...
+             */
+            if ( insn_off > sizeof(hvmemul_ctxt->insn_buf) ||
+                 (insn_off + bytes) > sizeof(hvmemul_ctxt->insn_buf) )
+            {
+                ASSERT_UNREACHABLE();
+                return X86EMUL_UNHANDLEABLE;
+            }
+
             memcpy(&hvmemul_ctxt->insn_buf[insn_off], p_data, bytes);
             hvmemul_ctxt->insn_buf_bytes = insn_off + bytes;
         }
--
Quoted 1 line

2.1.4

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

#3 lore raw

Re: [PATCH 1/6] x86/hvm: Fixes to hvmemul_insn_fetch()

Quoted 24 lines · Paul Durrant
Quoted 24 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 21 June 2017 16:12
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Jan Beulich <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH 1/6] x86/hvm: Fixes to hvmemul_insn_fetch()

Force insn_off to a single byte, as offset can wrap around or truncate with respect to sh_ctxt->insn_buf_eip under a number of normal circumstances.

Furthermore, don't use an ASSERT() for bounds checking the write into hvmemul_ctxt->insn_buf[].

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>

For anyone wondering, this is way to explot XSA-186

---
 xen/arch/x86/hvm/emulate.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
Quoted 12 lines

xen/arch/x86/hvm/emulate.c int hvmemul_insn_fetch( · line 943

Quoted 11 lines
xen/arch/x86/hvm/emulate.c +2 -1
index 11e4aba..495e312 100644
@@ -939,7 +939,8 @@ int hvmemul_insn_fetch(
 {
     struct hvm_emulate_ctxt *hvmemul_ctxt =
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
-    unsigned int insn_off = offset - hvmemul_ctxt->insn_buf_eip;
+    /* Careful, as offset can wrap or truncate WRT insn_buf_eip. */
+    uint8_t insn_off = offset - hvmemul_ctxt->insn_buf_eip;

Why the change to a uint8_t?

XSA-186 caused problems because offset was truncated at 16 bits, but all calculations here are performed at 64 bits wide, then truncated down to 32bits wide. As a result, insn_off could become massively positive.

insn_off needs to be less wide than the minimum truncation width of incoming parameters for it to work correctly.

Code hitting the emulator can legitimately cause offset to truncate at 32bits WRT EIP, and the only reason we aren't still vulnerable is because insn_off is unsigned int. If it were unsigned long, we'd have another privilege escalation vulnerability.

~Andrew

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

Reviewed-by
#4 lore raw

Re: [PATCH 1/6] x86/hvm: Fixes to hvmemul_insn_fetch()

Quoted 51 lines

-----Original Message-----
From: Andrew Cooper
Sent: 21 June 2017 17:15
To: Paul Durrant <[email protected]>; Xen-devel <xen- [email protected]>
Cc: Jan Beulich <[email protected]>
Subject: Re: [PATCH 1/6] x86/hvm: Fixes to hvmemul_insn_fetch()

Quoted 10 lines · Paul Durrant
Quoted 10 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 21 June 2017 16:12
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Jan Beulich <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH 1/6] x86/hvm: Fixes to hvmemul_insn_fetch()

Force insn_off to a single byte, as offset can wrap around or truncate with respect to sh_ctxt->insn_buf_eip under a number of normal

circumstances.

Quoted 14 lines
Quoted 14 lines

Furthermore, don't use an ASSERT() for bounds checking the write into hvmemul_ctxt->insn_buf[].

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>

For anyone wondering, this is way to explot XSA-186

---
 xen/arch/x86/hvm/emulate.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
Quoted 12 lines

xen/arch/x86/hvm/emulate.c int hvmemul_insn_fetch( · line 943

Quoted 11 lines
xen/arch/x86/hvm/emulate.c +2 -1
index 11e4aba..495e312 100644
@@ -939,7 +939,8 @@ int hvmemul_insn_fetch(
 {
     struct hvm_emulate_ctxt *hvmemul_ctxt =
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
-    unsigned int insn_off = offset - hvmemul_ctxt->insn_buf_eip;
+    /* Careful, as offset can wrap or truncate WRT insn_buf_eip. */
+    uint8_t insn_off = offset - hvmemul_ctxt->insn_buf_eip;

Why the change to a uint8_t?

XSA-186 caused problems because offset was truncated at 16 bits, but all calculations here are performed at 64 bits wide, then truncated down to 32bits wide. As a result, insn_off could become massively positive.

Quoted 7 lines

insn_off needs to be less wide than the minimum truncation width of incoming parameters for it to work correctly.

Code hitting the emulator can legitimately cause offset to truncate at 32bits WRT EIP, and the only reason we aren't still vulnerable is because insn_off is unsigned int. If it were unsigned long, we'd have another privilege escalation vulnerability.

Ok.

Quoted 2 lines

~Andrew

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

Reviewed-by on v1 1/6
#5 lore raw

Re: [PATCH 1/6] x86/hvm: Fixes to hvmemul_insn_fetch()

In reply to Andrew Cooper
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 21.06.17 at 17:12, <[email protected]> wrote:

Force insn_off to a single byte, as offset can wrap around or truncate with respect to sh_ctxt->insn_buf_eip under a number of normal circumstances.

Furthermore, don't use an ASSERT() for bounds checking the write into hvmemul_ctxt->insn_buf[].

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

#6 lore raw

PATCH 2/6 x86/shadow: Fixes to hvm_emulate_insn_fetch()

In reply to Andrew Cooper

Zero-legnth reads are jump-target segmentation checks; never serve them from the cache.

Force insn_off to a single byte, as offset can wrap around or truncate with respect to sh_ctxt->insn_buf_eip under a number of normal circumstances.

---
CC: Tim Deegan <[email protected]>
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/mm/shadow/common.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
xen/arch/x86/mm/shadow/common.c +7 -3
index 2e64a77..deea03a 100644
@@ -235,12 +235,16 @@ hvm_emulate_insn_fetch(enum x86_segment seg,
 {
     struct sh_emulate_ctxt *sh_ctxt =
         container_of(ctxt, struct sh_emulate_ctxt, ctxt);
-    unsigned int insn_off = offset - sh_ctxt->insn_buf_eip;
+    /* Careful, as offset can wrap or truncate WRT insn_buf_eip. */
+    uint8_t insn_off = offset - sh_ctxt->insn_buf_eip;
 
     ASSERT(seg == x86_seg_cs);
 
-    /* Fall back if requested bytes are not in the prefetch cache. */
-    if ( unlikely((insn_off + bytes) > sh_ctxt->insn_buf_bytes) )
+    /*
+     * Fall back if requested bytes are not in the prefetch cache, but always
+     * perform the zero-length read for segmentation purposes.
+     */
+    if ( !bytes || unlikely((insn_off + bytes) > sh_ctxt->insn_buf_bytes) )
         return hvm_read(seg, offset, p_data, bytes,
                         hvm_access_insn_fetch, sh_ctxt);
 
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 2/6
#7 lore raw

Re: [PATCH 2/6] x86/shadow: Fixes to hvm_emulate_insn_fetch()

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 21.06.17 at 17:12, <[email protected]> wrote:
xen/arch/x86/mm/shadow/common.c +2 -1
@@ -235,12 +235,16 @@ hvm_emulate_insn_fetch(enum x86_segment seg,
 {
     struct sh_emulate_ctxt *sh_ctxt =
         container_of(ctxt, struct sh_emulate_ctxt, ctxt);
-    unsigned int insn_off = offset - sh_ctxt->insn_buf_eip;
+    /* Careful, as offset can wrap or truncate WRT insn_buf_eip. */
+    uint8_t insn_off = offset - sh_ctxt->insn_buf_eip;
 
     ASSERT(seg == x86_seg_cs);

xen/arch/x86/mm/shadow/common.c hvm_emulate_insn_fetch(enum x86_segment seg, · line 249

Quoted 10 lines
xen/arch/x86/mm/shadow/common.c +5 -2
 
-    /* Fall back if requested bytes are not in the prefetch cache. */
-    if ( unlikely((insn_off + bytes) > sh_ctxt->insn_buf_bytes) )
+    /*
+     * Fall back if requested bytes are not in the prefetch cache, but always
+     * perform the zero-length read for segmentation purposes.
+     */
+    if ( !bytes || unlikely((insn_off + bytes) > sh_ctxt->insn_buf_bytes) )
         return hvm_read(seg, offset, p_data, bytes,
                         hvm_access_insn_fetch, sh_ctxt);

I only note this now - patch one has an (apparently unnecessary) insn_off > sizeof() check ahead of the insn_off + bytes > sizeof() one. If you see a need for it to be there, why don't you put it here too? In any event I'd like to ask for both pieces of code to match up in the end (i.e. I'm fine with the adjustment being done on either side), and then

Jan

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

#8 lore raw

Re: [PATCH 2/6] x86/shadow: Fixes to hvm_emulate_insn_fetch()

In reply to Andrew Cooper

Hi,

Quoted 2 lines · Andrew Cooper

Zero-legnth reads are jump-target segmentation checks; never serve them from the cache.

Why not? If the target is in the cached range, then it has passed the segmentation check. (Or if that's not true then the normal fetch path needs to be fixed too).

Quoted 2 lines

Force insn_off to a single byte, as offset can wrap around or truncate with respect to sh_ctxt->insn_buf_eip under a number of normal circumstances.

Wouldn't it be better to detect that and fall through? Otherwise we might return cached bytes by mistake.

Tim.

Quoted 35 lines

---
CC: Tim Deegan <[email protected]>
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/mm/shadow/common.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
xen/arch/x86/mm/shadow/common.c +7 -3
index 2e64a77..deea03a 100644
@@ -235,12 +235,16 @@ hvm_emulate_insn_fetch(enum x86_segment seg,
 {
     struct sh_emulate_ctxt *sh_ctxt =
         container_of(ctxt, struct sh_emulate_ctxt, ctxt);
-    unsigned int insn_off = offset - sh_ctxt->insn_buf_eip;
+    /* Careful, as offset can wrap or truncate WRT insn_buf_eip. */
+    uint8_t insn_off = offset - sh_ctxt->insn_buf_eip;
 
     ASSERT(seg == x86_seg_cs);
 
-    /* Fall back if requested bytes are not in the prefetch cache. */
-    if ( unlikely((insn_off + bytes) > sh_ctxt->insn_buf_bytes) )
+    /*
+     * Fall back if requested bytes are not in the prefetch cache, but always
+     * perform the zero-length read for segmentation purposes.
+     */
+    if ( !bytes || unlikely((insn_off + bytes) > sh_ctxt->insn_buf_bytes) )
         return hvm_read(seg, offset, p_data, bytes,
                         hvm_access_insn_fetch, sh_ctxt);
 
Signature · 1 line
2.1.4

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

#9 lore raw

PATCH 3/6 x86/shadow: Use ERR_PTR infrastructure for sh_emulate_map_dest()

In reply to Andrew Cooper

sh_emulate_map_dest() predates the introduction of the generic ERR_PTR() infrasturcture, but take the opportunity to avoid opencoding it.

The chosen error constants require need to be negative to work with IS_ERR(), but no other changes.

---
CC: Jan Beulich <[email protected]>
CC: Tim Deegan <[email protected]>

v2:
* Use ~(long)X86EMUL rather than -X86EMUL so MAPPING_SILENT_FAIL is considered an error to IS_ERR()

---
 xen/arch/x86/mm/shadow/multi.c   | 8 ++++----
 xen/arch/x86/mm/shadow/private.h | 7 +++----
 2 files changed, 7 insertions(+), 8 deletions(-)
xen/arch/x86/mm/shadow/multi.c +4 -4
index f65ffc6..74c3641 100644
@@ -4752,8 +4752,8 @@ sh_x86_emulate_write(struct vcpu *v, unsigned long vaddr, void *src,
         return X86EMUL_UNHANDLEABLE;
 
     addr = sh_emulate_map_dest(v, vaddr, bytes, sh_ctxt);
-    if ( sh_emulate_map_dest_failed(addr) )
-        return (long)addr;
+    if ( IS_ERR(addr) )
+        return ~PTR_ERR(addr);
 
     paging_lock(v->domain);
     memcpy(addr, src, bytes);
@@ -4794,8 +4794,8 @@ sh_x86_emulate_cmpxchg(struct vcpu *v, unsigned long vaddr,
         return X86EMUL_UNHANDLEABLE;
 
     addr = sh_emulate_map_dest(v, vaddr, bytes, sh_ctxt);
-    if ( sh_emulate_map_dest_failed(addr) )
-        return (long)addr;
+    if ( IS_ERR(addr) )
+        return ~PTR_ERR(addr);
 
     paging_lock(v->domain);
     switch ( bytes )
xen/arch/x86/mm/shadow/private.h +3 -4
index 472676c..a59ff7a 100644
@@ -395,10 +395,9 @@ void shadow_unhook_mappings(struct domain *d, mfn_t smfn, int user_only);
 
 /* Returns a mapped pointer to write to, or one of the following error
  * indicators. */
-#define MAPPING_UNHANDLEABLE ((void *)(unsigned long)X86EMUL_UNHANDLEABLE)
-#define MAPPING_EXCEPTION    ((void *)(unsigned long)X86EMUL_EXCEPTION)
-#define MAPPING_SILENT_FAIL  ((void *)(unsigned long)X86EMUL_OKAY)
-#define sh_emulate_map_dest_failed(rc) ((unsigned long)(rc) <= 3)
+#define MAPPING_UNHANDLEABLE ERR_PTR(~(long)X86EMUL_UNHANDLEABLE)
+#define MAPPING_EXCEPTION    ERR_PTR(~(long)X86EMUL_EXCEPTION)
+#define MAPPING_SILENT_FAIL  ERR_PTR(~(long)X86EMUL_OKAY)
 void *sh_emulate_map_dest(struct vcpu *v, unsigned long vaddr,
                           unsigned int bytes, struct sh_emulate_ctxt *sh_ctxt);
 void sh_emulate_unmap_dest(struct vcpu *v, void *addr, unsigned int bytes,
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 3/6
#10 lore raw

Re: [PATCH 3/6] x86/shadow: Use ERR_PTR infrastructure for sh_emulate_map_dest()

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 21.06.17 at 17:12, <[email protected]> wrote:

sh_emulate_map_dest() predates the introduction of the generic ERR_PTR() infrasturcture, but take the opportunity to avoid opencoding it.

The chosen error constants require need to be negative to work with IS_ERR(), but no other changes.

Drop one of "require" or "need"?

xen/arch/x86/mm/shadow/private.h void shadow_unhook_mappings(struct domain *d, mfn_t smfn, int user_only); · line 400

Quoted 13 lines
xen/arch/x86/mm/shadow/private.h +3 -4
@@ -395,10 +395,9 @@ void shadow_unhook_mappings(struct domain *d, mfn_t smfn, int user_only);
 
 /* Returns a mapped pointer to write to, or one of the following error
  * indicators. */
-#define MAPPING_UNHANDLEABLE ((void *)(unsigned long)X86EMUL_UNHANDLEABLE)
-#define MAPPING_EXCEPTION    ((void *)(unsigned long)X86EMUL_EXCEPTION)
-#define MAPPING_SILENT_FAIL  ((void *)(unsigned long)X86EMUL_OKAY)
-#define sh_emulate_map_dest_failed(rc) ((unsigned long)(rc) <= 3)
+#define MAPPING_UNHANDLEABLE ERR_PTR(~(long)X86EMUL_UNHANDLEABLE)
+#define MAPPING_EXCEPTION    ERR_PTR(~(long)X86EMUL_EXCEPTION)
+#define MAPPING_SILENT_FAIL  ERR_PTR(~(long)X86EMUL_OKAY)

While this doesn't change with your patch, this last definition has a string dependency on X86EMUL_OKAY to be zero, yet there's no respective BUILD_BUG_ON() anywhere. Would you mind adding one at once? In any event

Jan

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

#11 lore raw

Re: [PATCH 3/6] x86/shadow: Use ERR_PTR infrastructure for sh_emulate_map_dest()

Quoted 25 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 21.06.17 at 17:12, <[email protected]> wrote:

sh_emulate_map_dest() predates the introduction of the generic ERR_PTR() infrasturcture, but take the opportunity to avoid opencoding it.

The chosen error constants require need to be negative to work with IS_ERR(), but no other changes.

Drop one of "require" or "need"?

xen/arch/x86/mm/shadow/private.h void shadow_unhook_mappings(struct domain *d, mfn_t smfn, int user_only); · line 400

Quoted 13 lines
xen/arch/x86/mm/shadow/private.h +3 -4
@@ -395,10 +395,9 @@ void shadow_unhook_mappings(struct domain *d, mfn_t smfn, int user_only);
 
 /* Returns a mapped pointer to write to, or one of the following error
  * indicators. */
-#define MAPPING_UNHANDLEABLE ((void *)(unsigned long)X86EMUL_UNHANDLEABLE)
-#define MAPPING_EXCEPTION    ((void *)(unsigned long)X86EMUL_EXCEPTION)
-#define MAPPING_SILENT_FAIL  ((void *)(unsigned long)X86EMUL_OKAY)
-#define sh_emulate_map_dest_failed(rc) ((unsigned long)(rc) <= 3)
+#define MAPPING_UNHANDLEABLE ERR_PTR(~(long)X86EMUL_UNHANDLEABLE)
+#define MAPPING_EXCEPTION    ERR_PTR(~(long)X86EMUL_EXCEPTION)
+#define MAPPING_SILENT_FAIL  ERR_PTR(~(long)X86EMUL_OKAY)

While this doesn't change with your patch, this last definition has a string dependency on X86EMUL_OKAY to be zero, yet there's no respective BUILD_BUG_ON() anywhere. Would you mind adding one at once? In any event

Quoted 1 line

There is no longer any dependence on X86EMUL_OKAY being 0 (so far as I can see). All this logic would work fine if OKAY had the value 10 for example.

The only requirement (now) is that the X86EMUL constants are all within MAX_ERRNO so they count as errors as far as the ERR_PTR logic is concerned, and I suppose those should gain BUILD_BUG_ON()s

~Andrew

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

#12 lore raw

Re: [PATCH 3/6] x86/shadow: Use ERR_PTR infrastructure for sh_emulate_map_dest()

Quoted 31 lines
Quoted 1 line
Quoted 1 line · On 22.06.17 at 10:21, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 21.06.17 at 17:12, <[email protected]> wrote:

sh_emulate_map_dest() predates the introduction of the generic ERR_PTR() infrasturcture, but take the opportunity to avoid opencoding it.

The chosen error constants require need to be negative to work with

IS_ERR(),

Quoted 6 lines
Quoted 1 line

but no other changes.

Drop one of "require" or "need"?

xen/arch/x86/mm/shadow/private.h void shadow_unhook_mappings(struct domain *d, mfn_t

Quoted 3 lines
xen/arch/x86/mm/shadow/private.h +0 -0
@@ -395,10 +395,9 @@ void shadow_unhook_mappings(struct domain *d, mfn_t 

smfn, int user_only);

Quoted 14 lines

xen/arch/x86/mm/shadow/private.h void shadow_unhook_mappings(struct domain *d, mfn_t smfn, int user_only); · line 400

Quoted 10 lines
xen/arch/x86/mm/shadow/private.h +3 -4
 /* Returns a mapped pointer to write to, or one of the following error
  * indicators. */
-#define MAPPING_UNHANDLEABLE ((void *)(unsigned long)X86EMUL_UNHANDLEABLE)
-#define MAPPING_EXCEPTION    ((void *)(unsigned long)X86EMUL_EXCEPTION)
-#define MAPPING_SILENT_FAIL  ((void *)(unsigned long)X86EMUL_OKAY)
-#define sh_emulate_map_dest_failed(rc) ((unsigned long)(rc) <= 3)
+#define MAPPING_UNHANDLEABLE ERR_PTR(~(long)X86EMUL_UNHANDLEABLE)
+#define MAPPING_EXCEPTION    ERR_PTR(~(long)X86EMUL_EXCEPTION)
+#define MAPPING_SILENT_FAIL  ERR_PTR(~(long)X86EMUL_OKAY)

While this doesn't change with your patch, this last definition has a string dependency on X86EMUL_OKAY to be zero, yet there's no respective BUILD_BUG_ON() anywhere. Would you mind adding one at once? In any event

Quoted 1 line
Quoted 3 lines

There is no longer any dependence on X86EMUL_OKAY being 0 (so far as I can see). All this logic would work fine if OKAY had the value 10 for example.

The mapping failure is "silent" only if X86EMUL_OKAY is zero, afaict.

Quoted 3 lines

The only requirement (now) is that the X86EMUL constants are all within MAX_ERRNO so they count as errors as far as the ERR_PTR logic is concerned, and I suppose those should gain BUILD_BUG_ON()s

That would be helpful too.

Jan

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

#13 lore raw

Re: [PATCH 3/6] x86/shadow: Use ERR_PTR infrastructure for sh_emulate_map_dest()

Quoted 30 lines · Jan Beulich
Quoted 30 lines
Quoted 1 line
Quoted 1 line · On 22.06.17 at 10:21, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 21.06.17 at 17:12, <[email protected]> wrote:

sh_emulate_map_dest() predates the introduction of the generic ERR_PTR() infrasturcture, but take the opportunity to avoid opencoding it.

The chosen error constants require need to be negative to work with

IS_ERR(),

Quoted 6 lines
Quoted 1 line

but no other changes.

Drop one of "require" or "need"?

xen/arch/x86/mm/shadow/private.h void shadow_unhook_mappings(struct domain *d, mfn_t

Quoted 3 lines
xen/arch/x86/mm/shadow/private.h +0 -0
@@ -395,10 +395,9 @@ void shadow_unhook_mappings(struct domain *d, mfn_t 

smfn, int user_only);

Quoted 15 lines

xen/arch/x86/mm/shadow/private.h void shadow_unhook_mappings(struct domain *d, mfn_t smfn, int user_only); · line 400

Quoted 10 lines
xen/arch/x86/mm/shadow/private.h +3 -4
 /* Returns a mapped pointer to write to, or one of the following error
  * indicators. */
-#define MAPPING_UNHANDLEABLE ((void *)(unsigned long)X86EMUL_UNHANDLEABLE)
-#define MAPPING_EXCEPTION    ((void *)(unsigned long)X86EMUL_EXCEPTION)
-#define MAPPING_SILENT_FAIL  ((void *)(unsigned long)X86EMUL_OKAY)
-#define sh_emulate_map_dest_failed(rc) ((unsigned long)(rc) <= 3)
+#define MAPPING_UNHANDLEABLE ERR_PTR(~(long)X86EMUL_UNHANDLEABLE)
+#define MAPPING_EXCEPTION    ERR_PTR(~(long)X86EMUL_EXCEPTION)
+#define MAPPING_SILENT_FAIL  ERR_PTR(~(long)X86EMUL_OKAY)

While this doesn't change with your patch, this last definition has a string dependency on X86EMUL_OKAY to be zero, yet there's no respective BUILD_BUG_ON() anywhere. Would you mind adding one at once? In any event

Quoted 4 lines
Quoted 3 lines

There is no longer any dependence on X86EMUL_OKAY being 0 (so far as I can see). All this logic would work fine if OKAY had the value 10 for example.

The mapping failure is "silent" only if X86EMUL_OKAY is zero, afaict.

It is silent because MAPPING_SILENT_FAIL is considered an error by the caller (sh_x86_emulate_{write,cmpxchg}()), which unpacks the result (turning it into X86EMUL_OKAY) and passes it back into the emulation logic.

This doesn't depend on the absolute value of X86EMUL_OKAY.

Quoted 5 lines
Quoted 4 lines

The only requirement (now) is that the X86EMUL constants are all within MAX_ERRNO so they count as errors as far as the ERR_PTR logic is concerned, and I suppose those should gain BUILD_BUG_ON()s

That would be helpful too.

Its sadly not possible, as ERR_PTR() is a static inline and can't be evaluated in the context of BUILD_BUG_ON().

The best which is possible (without altering the errptr infrastructure) is BUILD_BUG_ON(IS_ERR_VALUE(~(long)X86EMUL_*)), but this won't catch a change which redefines the MAPPING_* constants.

~Andrew

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

#14 lore raw

Re: [PATCH 3/6] x86/shadow: Use ERR_PTR infrastructure for sh_emulate_map_dest()

Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 16:22, <[email protected]> wrote:
Quoted 33 lines · Jan Beulich
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 22.06.17 at 10:21, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 21.06.17 at 17:12, <[email protected]> wrote:

sh_emulate_map_dest() predates the introduction of the generic ERR_PTR() infrasturcture, but take the opportunity to avoid opencoding it.

The chosen error constants require need to be negative to work with

IS_ERR(),

Quoted 6 lines
Quoted 1 line

but no other changes.

Drop one of "require" or "need"?

xen/arch/x86/mm/shadow/private.h void shadow_unhook_mappings(struct domain *d, mfn_t

Quoted 3 lines
xen/arch/x86/mm/shadow/private.h +0 -0
@@ -395,10 +395,9 @@ void shadow_unhook_mappings(struct domain *d, mfn_t 

smfn, int user_only);

Quoted 14 lines

xen/arch/x86/mm/shadow/private.h void shadow_unhook_mappings(struct domain *d, mfn_t smfn, int user_only); · line 400

Quoted 10 lines
xen/arch/x86/mm/shadow/private.h +3 -4
 /* Returns a mapped pointer to write to, or one of the following error
  * indicators. */
-#define MAPPING_UNHANDLEABLE ((void *)(unsigned long)X86EMUL_UNHANDLEABLE)
-#define MAPPING_EXCEPTION    ((void *)(unsigned long)X86EMUL_EXCEPTION)
-#define MAPPING_SILENT_FAIL  ((void *)(unsigned long)X86EMUL_OKAY)
-#define sh_emulate_map_dest_failed(rc) ((unsigned long)(rc) <= 3)
+#define MAPPING_UNHANDLEABLE ERR_PTR(~(long)X86EMUL_UNHANDLEABLE)
+#define MAPPING_EXCEPTION    ERR_PTR(~(long)X86EMUL_EXCEPTION)
+#define MAPPING_SILENT_FAIL  ERR_PTR(~(long)X86EMUL_OKAY)

While this doesn't change with your patch, this last definition has a string dependency on X86EMUL_OKAY to be zero, yet there's no respective BUILD_BUG_ON() anywhere. Would you mind adding one at once? In any event

Quoted 1 line

There is no longer any dependence on X86EMUL_OKAY being 0 (so far as I can see). All this logic would work fine if OKAY had the value 10 for example.

Quoted 7 lines
Quoted 1 line

The mapping failure is "silent" only if X86EMUL_OKAY is zero, afaict.

It is silent because MAPPING_SILENT_FAIL is considered an error by the caller (sh_x86_emulate_{write,cmpxchg}()), which unpacks the result (turning it into X86EMUL_OKAY) and passes it back into the emulation logic.

This doesn't depend on the absolute value of X86EMUL_OKAY.

Oh, indeed. R-b then is unconditional.

Jan

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

Acked-by on v1 3/6
#15 lore raw

Re: [PATCH 3/6] x86/shadow: Use ERR_PTR infrastructure for sh_emulate_map_dest()

In reply to Andrew Cooper

Hi,

Quoted 2 lines · Andrew Cooper

sh_emulate_map_dest() predates the introduction of the generic ERR_PTR() infrasturcture, but take the opportunity to avoid opencoding it.

s/sturct/struct/.

xen/arch/x86/mm/shadow/multi.c sh_x86_emulate_write(struct vcpu *v, unsigned long vaddr, void *src, · line 4756

Quoted 8 lines
xen/arch/x86/mm/shadow/multi.c +2 -2
@@ -4752,8 +4752,8 @@ sh_x86_emulate_write(struct vcpu *v, unsigned long vaddr, void *src,
         return X86EMUL_UNHANDLEABLE;
 
     addr = sh_emulate_map_dest(v, vaddr, bytes, sh_ctxt);
-    if ( sh_emulate_map_dest_failed(addr) )
-        return (long)addr;
+    if ( IS_ERR(addr) )
+        return ~PTR_ERR(addr);

Using "return ~PTR_ERR(addr)" when the usual idiom is "return -PTR_ERR(foo)" is a bit subtle. Still, the code seems to be correct, so if people prefer it,

Cheers,

Tim.

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

#16 lore raw

Re: [PATCH 3/6] x86/shadow: Use ERR_PTR infrastructure for sh_emulate_map_dest()

Quoted 6 lines · Tim Deegan

Hi,

Quoted 2 lines · Andrew Cooper

sh_emulate_map_dest() predates the introduction of the generic ERR_PTR() infrasturcture, but take the opportunity to avoid opencoding it.

s/sturct/struct/.

D'oh - I'm sure you spotted this before. I will try to actually fix it this time.

Quoted 13 lines

xen/arch/x86/mm/shadow/multi.c sh_x86_emulate_write(struct vcpu *v, unsigned long vaddr, void *src, · line 4756

Quoted 9 lines
xen/arch/x86/mm/shadow/multi.c +2 -2
@@ -4752,8 +4752,8 @@ sh_x86_emulate_write(struct vcpu *v, unsigned long vaddr, void *src,
         return X86EMUL_UNHANDLEABLE;
 
     addr = sh_emulate_map_dest(v, vaddr, bytes, sh_ctxt);
-    if ( sh_emulate_map_dest_failed(addr) )
-        return (long)addr;
+    if ( IS_ERR(addr) )
+        return ~PTR_ERR(addr);

Using "return ~PTR_ERR(addr)" when the usual idiom is "return -PTR_ERR(foo)" is a bit subtle. Still, the code seems to be correct, so if people prefer it,

Quoted 1 line

It is necessary to use ~ rather than - because of MAPPING_SILENT_FAIL being X86EMUL_OKAY under the hood.

Short of sliding the X86EMUL_* constants along by 1 (which itself would cause other chaos, as there are plenty of return value checks against 0 rather than X86EMUL_OKAY), I can't see any other way of making this function.

All in all, write-discard p2m types are a pain to work with :(

~Andrew

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

#17 lore raw

RFC PATCH 4/6 x86/hvm: Rename enum hvm_copy_result to hvm_translation_result

In reply to Andrew Cooper

---
CC: Jan Beulich <[email protected]>

Name definitely open to improvement. Perhaps better considered in the context of the following patch.

---
 xen/arch/x86/hvm/dom0_build.c     |  2 +-
 xen/arch/x86/hvm/emulate.c        | 40 ++++++++++++++--------------
 xen/arch/x86/hvm/hvm.c            | 56 +++++++++++++++++++--------------------
 xen/arch/x86/hvm/intercept.c      | 20 +++++++-------
 xen/arch/x86/hvm/svm/nestedsvm.c  |  5 ++--
 xen/arch/x86/hvm/svm/svm.c        |  2 +-
 xen/arch/x86/hvm/viridian.c       |  2 +-
 xen/arch/x86/hvm/vmsi.c           |  2 +-
 xen/arch/x86/hvm/vmx/realmode.c   |  2 +-
 xen/arch/x86/hvm/vmx/vvmx.c       | 14 +++++-----
 xen/arch/x86/mm/shadow/common.c   | 12 ++++-----
 xen/common/libelf/libelf-loader.c |  4 +--
 xen/include/asm-x86/hvm/support.h | 40 ++++++++++++++--------------
 13 files changed, 101 insertions(+), 100 deletions(-)
xen/arch/x86/hvm/dom0_build.c +1 -1
index 020c355..e8f746c 100644
@@ -238,7 +238,7 @@ static int __init pvh_setup_vmx_realmode_helpers(struct domain *d)
     if ( !pvh_steal_ram(d, HVM_VM86_TSS_SIZE, 128, GB(4), &gaddr) )
     {
         if ( hvm_copy_to_guest_phys(gaddr, NULL, HVM_VM86_TSS_SIZE, v) !=
-             HVMCOPY_okay )
+             HVMTRANS_okay )
             printk("Unable to zero VM86 TSS area\n");
         d->arch.hvm_domain.params[HVM_PARAM_VM86_TSS_SIZED] =
             VM86_TSS_UPDATED | ((uint64_t)HVM_VM86_TSS_SIZE << 32) | gaddr;
xen/arch/x86/hvm/emulate.c +20 -20
index 495e312..384ad0b 100644
@@ -100,7 +100,7 @@ static int ioreq_server_read(const struct hvm_io_handler *io_handler,
                     uint32_t size,
                     uint64_t *data)
 {
-    if ( hvm_copy_from_guest_phys(data, addr, size) != HVMCOPY_okay )
+    if ( hvm_copy_from_guest_phys(data, addr, size) != HVMTRANS_okay )
         return X86EMUL_UNHANDLEABLE;
 
     return X86EMUL_OKAY;
@@ -892,18 +892,18 @@ static int __hvmemul_read(
 
     switch ( rc )
     {
-    case HVMCOPY_okay:
+    case HVMTRANS_okay:
         break;
-    case HVMCOPY_bad_gva_to_gfn:
+    case HVMTRANS_bad_linear_to_gfn:
         x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
         return X86EMUL_EXCEPTION;
-    case HVMCOPY_bad_gfn_to_mfn:
+    case HVMTRANS_bad_gfn_to_mfn:
         if ( access_type == hvm_access_insn_fetch )
             return X86EMUL_UNHANDLEABLE;
 
         return hvmemul_linear_mmio_read(addr, bytes, p_data, pfec, hvmemul_ctxt, 0);
-    case HVMCOPY_gfn_paged_out:
-    case HVMCOPY_gfn_shared:
+    case HVMTRANS_gfn_paged_out:
+    case HVMTRANS_gfn_shared:
         return X86EMUL_RETRY;
     default:
         return X86EMUL_UNHANDLEABLE;
@@ -1011,15 +1011,15 @@ static int hvmemul_write(
 
     switch ( rc )
     {
-    case HVMCOPY_okay:
+    case HVMTRANS_okay:
         break;
-    case HVMCOPY_bad_gva_to_gfn:
+    case HVMTRANS_bad_linear_to_gfn:
         x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
         return X86EMUL_EXCEPTION;
-    case HVMCOPY_bad_gfn_to_mfn:
+    case HVMTRANS_bad_gfn_to_mfn:
         return hvmemul_linear_mmio_write(addr, bytes, p_data, pfec, hvmemul_ctxt, 0);
-    case HVMCOPY_gfn_paged_out:
-    case HVMCOPY_gfn_shared:
+    case HVMTRANS_gfn_paged_out:
+    case HVMTRANS_gfn_shared:
         return X86EMUL_RETRY;
     default:
         return X86EMUL_UNHANDLEABLE;
@@ -1383,7 +1383,7 @@ static int hvmemul_rep_movs(
             return rc;
         }
 
-        rc = HVMCOPY_okay;
+        rc = HVMTRANS_okay;
     }
     else
         /*
@@ -1393,16 +1393,16 @@ static int hvmemul_rep_movs(
          */
         rc = hvm_copy_from_guest_phys(buf, sgpa, bytes);
 
-    if ( rc == HVMCOPY_okay )
+    if ( rc == HVMTRANS_okay )
         rc = hvm_copy_to_guest_phys(dgpa, buf, bytes, current);
 
     xfree(buf);
 
-    if ( rc == HVMCOPY_gfn_paged_out )
+    if ( rc == HVMTRANS_gfn_paged_out )
         return X86EMUL_RETRY;
-    if ( rc == HVMCOPY_gfn_shared )
+    if ( rc == HVMTRANS_gfn_shared )
         return X86EMUL_RETRY;
-    if ( rc != HVMCOPY_okay )
+    if ( rc != HVMTRANS_okay )
     {
         gdprintk(XENLOG_WARNING, "Failed memory-to-memory REP MOVS: sgpa=%"
                  PRIpaddr" dgpa=%"PRIpaddr" reps=%lu bytes_per_rep=%u\n",
@@ -1512,10 +1512,10 @@ static int hvmemul_rep_stos(
 
         switch ( rc )
         {
-        case HVMCOPY_gfn_paged_out:
-        case HVMCOPY_gfn_shared:
+        case HVMTRANS_gfn_paged_out:
+        case HVMTRANS_gfn_shared:
             return X86EMUL_RETRY;
-        case HVMCOPY_okay:
+        case HVMTRANS_okay:
             return X86EMUL_OKAY;
         }
 
@@ -2171,7 +2171,7 @@ void hvm_emulate_init_per_insn(
                                         &addr) &&
              hvm_fetch_from_guest_linear(hvmemul_ctxt->insn_buf, addr,
                                          sizeof(hvmemul_ctxt->insn_buf),
-                                         pfec, NULL) == HVMCOPY_okay) ?
+                                         pfec, NULL) == HVMTRANS_okay) ?
             sizeof(hvmemul_ctxt->insn_buf) : 0;
     }
     else
xen/arch/x86/hvm/hvm.c +28 -28

183 lines not on this page · raw message

xen/arch/x86/hvm/intercept.c +10 -10
index e51efd5..ef82419 100644
@@ -136,14 +136,14 @@ int hvm_process_io_intercept(const struct hvm_io_handler *handler,
                 switch ( hvm_copy_to_guest_phys(p->data + step * i,
                                                 &data, p->size, current) )
                 {
-                case HVMCOPY_okay:
+                case HVMTRANS_okay:
                     break;
-                case HVMCOPY_bad_gfn_to_mfn:
+                case HVMTRANS_bad_gfn_to_mfn:
                     /* Drop the write as real hardware would. */
                     continue;
-                case HVMCOPY_bad_gva_to_gfn:
-                case HVMCOPY_gfn_paged_out:
-                case HVMCOPY_gfn_shared:
+                case HVMTRANS_bad_linear_to_gfn:
+                case HVMTRANS_gfn_paged_out:
+                case HVMTRANS_gfn_shared:
                     ASSERT_UNREACHABLE();
                     /* fall through */
                 default:
@@ -164,14 +164,14 @@ int hvm_process_io_intercept(const struct hvm_io_handler *handler,
                 switch ( hvm_copy_from_guest_phys(&data, p->data + step * i,
                                                   p->size) )
                 {
-                case HVMCOPY_okay:
+                case HVMTRANS_okay:
                     break;
-                case HVMCOPY_bad_gfn_to_mfn:
+                case HVMTRANS_bad_gfn_to_mfn:
                     data = ~0;
                     break;
-                case HVMCOPY_bad_gva_to_gfn:
-                case HVMCOPY_gfn_paged_out:
-                case HVMCOPY_gfn_shared:
+                case HVMTRANS_bad_linear_to_gfn:
+                case HVMTRANS_gfn_paged_out:
+                case HVMTRANS_gfn_shared:
                     ASSERT_UNREACHABLE();
                     /* fall through */
                 default:
xen/arch/x86/hvm/svm/nestedsvm.c +3 -2
index 8fd9c23..66a1777 100644
@@ -357,7 +357,7 @@ static int nsvm_vmrun_permissionmap(struct vcpu *v, bool_t viopm)
     struct vmcb_struct *host_vmcb = arch_svm->vmcb;
     unsigned long *ns_msrpm_ptr;
     unsigned int i;
-    enum hvm_copy_result ret;
+    enum hvm_translation_result ret;
     unsigned long *ns_viomap;
     bool_t ioport_80 = 1, ioport_ed = 1;
 
@@ -365,7 +365,8 @@ static int nsvm_vmrun_permissionmap(struct vcpu *v, bool_t viopm)
 
     ret = hvm_copy_from_guest_phys(svm->ns_cached_msrpm,
                                    ns_vmcb->_msrpm_base_pa, MSRPM_SIZE);
-    if (ret != HVMCOPY_okay) {
+    if ( ret != HVMTRANS_okay )
+    {
         gdprintk(XENLOG_ERR, "hvm_copy_from_guest_phys msrpm %u\n", ret);
         return 1;
     }
xen/arch/x86/hvm/svm/svm.c +1 -1
index 402e815..93cfdd7 100644
@@ -1268,7 +1268,7 @@ static void svm_emul_swint_injection(struct x86_event *event)
                                     PFEC_implicit, &pfinfo);
     if ( rc )
     {
-        if ( rc == HVMCOPY_bad_gva_to_gfn )
+        if ( rc == HVMTRANS_bad_linear_to_gfn )
         {
             fault = TRAP_page_fault;
             ec = pfinfo.ec;
xen/arch/x86/hvm/viridian.c +1 -1
index aa9b87c..0c74fa4 100644
@@ -914,7 +914,7 @@ int viridian_hypercall(struct cpu_user_regs *regs)
 
         /* Get input parameters. */
         if ( hvm_copy_from_guest_phys(&input_params, input_params_gpa,
-                                      sizeof(input_params)) != HVMCOPY_okay )
+                                      sizeof(input_params)) != HVMTRANS_okay )
             break;
 
         /*
xen/arch/x86/hvm/vmsi.c +1 -1
index a36692c..7415cdd 100644
@@ -610,7 +610,7 @@ void msix_write_completion(struct vcpu *v)
         if ( desc &&
              hvm_copy_from_guest_phys(&data,
                                       v->arch.hvm_vcpu.hvm_io.msix_snoop_gpa,
-                                      sizeof(data)) == HVMCOPY_okay &&
+                                      sizeof(data)) == HVMTRANS_okay &&
              !(data & PCI_MSIX_VECTOR_BITMASK) )
             ctrl_address = snoop_addr;
     }
xen/arch/x86/hvm/vmx/realmode.c +1 -1
index 1996b1f..bd749f4 100644
@@ -40,7 +40,7 @@ static void realmode_deliver_exception(
     last_byte = (vector * 4) + 3;
     if ( idtr->limit < last_byte ||
          hvm_copy_from_guest_phys(&cs_eip, idtr->base + vector * 4, 4) !=
-         HVMCOPY_okay )
+         HVMTRANS_okay )
     {
         /* Software interrupt? */
         if ( insn_len != 0 )
xen/arch/x86/hvm/vmx/vvmx.c +7 -7
index 3560fae..3feb46c 100644
@@ -479,9 +479,9 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,
             int rc = hvm_copy_from_guest_linear(poperandS, base, size,
                                                 0, &pfinfo);
 
-            if ( rc == HVMCOPY_bad_gva_to_gfn )
+            if ( rc == HVMTRANS_bad_linear_to_gfn )
                 hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
-            if ( rc != HVMCOPY_okay )
+            if ( rc != HVMTRANS_okay )
                 return X86EMUL_EXCEPTION;
         }
         decode->mem = base;
@@ -1453,7 +1453,7 @@ int nvmx_handle_vmxon(struct cpu_user_regs *regs)
     }
 
     rc = hvm_copy_from_guest_phys(&nvmcs_revid, gpa, sizeof(nvmcs_revid));
-    if ( rc != HVMCOPY_okay ||
+    if ( rc != HVMTRANS_okay ||
          (nvmcs_revid & ~VMX_BASIC_REVISION_MASK) ||
          ((nvmcs_revid ^ vmx_basic_msr) & VMX_BASIC_REVISION_MASK) )
     {
@@ -1731,9 +1731,9 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs)
     gpa = nvcpu->nv_vvmcxaddr;
 
     rc = hvm_copy_to_guest_linear(decode.mem, &gpa, decode.len, 0, &pfinfo);
-    if ( rc == HVMCOPY_bad_gva_to_gfn )
+    if ( rc == HVMTRANS_bad_linear_to_gfn )
         hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
-    if ( rc != HVMCOPY_okay )
+    if ( rc != HVMTRANS_okay )
         return X86EMUL_EXCEPTION;
 
     vmsucceed(regs);
@@ -1820,9 +1820,9 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs)
     switch ( decode.type ) {
     case VMX_INST_MEMREG_TYPE_MEMORY:
         rc = hvm_copy_to_guest_linear(decode.mem, &value, decode.len, 0, &pfinfo);
-        if ( rc == HVMCOPY_bad_gva_to_gfn )
+        if ( rc == HVMTRANS_bad_linear_to_gfn )
             hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
-        if ( rc != HVMCOPY_okay )
+        if ( rc != HVMTRANS_okay )
             return X86EMUL_EXCEPTION;
         break;
     case VMX_INST_MEMREG_TYPE_REG:
xen/arch/x86/mm/shadow/common.c +6 -6
index deea03a..fd70147 100644
@@ -196,16 +196,16 @@ hvm_read(enum x86_segment seg,
 
     switch ( rc )
     {
-    case HVMCOPY_okay:
+    case HVMTRANS_okay:
         return X86EMUL_OKAY;
-    case HVMCOPY_bad_gva_to_gfn:
+    case HVMTRANS_bad_linear_to_gfn:
         x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &sh_ctxt->ctxt);
         return X86EMUL_EXCEPTION;
-    case HVMCOPY_bad_gfn_to_mfn:
-    case HVMCOPY_unhandleable:
+    case HVMTRANS_bad_gfn_to_mfn:
+    case HVMTRANS_unhandleable:
         return X86EMUL_UNHANDLEABLE;
-    case HVMCOPY_gfn_paged_out:
-    case HVMCOPY_gfn_shared:
+    case HVMTRANS_gfn_paged_out:
+    case HVMTRANS_gfn_shared:
         return X86EMUL_RETRY;
     }
 
xen/common/libelf/libelf-loader.c +2 -2
index c8b7ec9..0f46872 100644
@@ -154,10 +154,10 @@ static elf_errorstatus elf_memcpy(struct vcpu *v, void *dst, void *src,
 #ifdef CONFIG_X86
     if ( is_hvm_vcpu(v) )
     {
-        enum hvm_copy_result rc;
+        enum hvm_translation_result rc;
 
         rc = hvm_copy_to_guest_phys((paddr_t)dst, src, size, v);
-        return rc != HVMCOPY_okay ? -1 : 0;
+        return rc != HVMTRANS_okay ? -1 : 0;
     }
 #endif
 
xen/include/asm-x86/hvm/support.h +20 -20
index 8a1252b..a1799a3 100644
@@ -53,23 +53,23 @@ extern unsigned int opt_hvm_debug_level;
 
 extern unsigned long hvm_io_bitmap[];
 
-enum hvm_copy_result {
-    HVMCOPY_okay = 0,
-    HVMCOPY_bad_gva_to_gfn,
-    HVMCOPY_bad_gfn_to_mfn,
-    HVMCOPY_unhandleable,
-    HVMCOPY_gfn_paged_out,
-    HVMCOPY_gfn_shared,
+enum hvm_translation_result {
+    HVMTRANS_okay,
+    HVMTRANS_bad_linear_to_gfn,
+    HVMTRANS_bad_gfn_to_mfn,
+    HVMTRANS_unhandleable,
+    HVMTRANS_gfn_paged_out,
+    HVMTRANS_gfn_shared,
 };
 
 /*
  * Copy to/from a guest physical address.
- * Returns HVMCOPY_okay, else HVMCOPY_bad_gfn_to_mfn if the given physical
+ * Returns HVMTRANS_okay, else HVMTRANS_bad_gfn_to_mfn if the given physical
  * address range does not map entirely onto ordinary machine memory.
  */
-enum hvm_copy_result hvm_copy_to_guest_phys(
+enum hvm_translation_result hvm_copy_to_guest_phys(
     paddr_t paddr, void *buf, int size, struct vcpu *v);
-enum hvm_copy_result hvm_copy_from_guest_phys(
+enum hvm_translation_result hvm_copy_from_guest_phys(
     void *buf, paddr_t paddr, int size);
 
 /*
@@ -79,13 +79,13 @@ enum hvm_copy_result hvm_copy_from_guest_phys(
  * to set them.
  * 
  * Returns:
- *  HVMCOPY_okay: Copy was entirely successful.
- *  HVMCOPY_bad_gfn_to_mfn: Some guest physical address did not map to
- *                          ordinary machine memory.
- *  HVMCOPY_bad_gva_to_gfn: Some guest virtual address did not have a valid
- *                          mapping to a guest physical address.  The
- *                          pagefault_info_t structure will be filled in if
- *                          provided.
+ *  HVMTRANS_okay: Copy was entirely successful.
+ *  HVMTRANS_bad_gfn_to_mfn: Some guest physical address did not map to
+ *                           ordinary machine memory.
+ *  HVMTRANS_bad_linear_to_gfn: Some guest linear address did not have a
+ *                              valid mapping to a guest physical address.
+ *                              The pagefault_info_t structure will be filled
+ *                              in if provided.
  */
 typedef struct pagefault_info
 {
@@ -93,13 +93,13 @@ typedef struct pagefault_info
     int ec;
 } pagefault_info_t;
 
-enum hvm_copy_result hvm_copy_to_guest_linear(
+enum hvm_translation_result hvm_copy_to_guest_linear(
     unsigned long addr, void *buf, int size, uint32_t pfec,
     pagefault_info_t *pfinfo);
-enum hvm_copy_result hvm_copy_from_guest_linear(
+enum hvm_translation_result hvm_copy_from_guest_linear(
     void *buf, unsigned long addr, int size, uint32_t pfec,
     pagefault_info_t *pfinfo);
-enum hvm_copy_result hvm_fetch_from_guest_linear(
+enum hvm_translation_result hvm_fetch_from_guest_linear(
     void *buf, unsigned long addr, int size, uint32_t pfec,
     pagefault_info_t *pfinfo);
 
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v1 4/6
#18 lore raw

Re: [PATCH 4/6] [RFC] x86/hvm: Rename enum hvm_copy_result to hvm_translation_result

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 21.06.17 at 17:12, <[email protected]> wrote:

---
CC: Jan Beulich <[email protected]>

Name definitely open to improvement.

I'm fine with it; hvm_translation_result is a little long, but shouldn't be used often.

Jan

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

#19 lore raw

PATCH 5/6 x86/hvm: Break out __hvm_copy()'s translation logic

In reply to Andrew Cooper

It will be reused by later changes.

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/hvm/hvm.c            | 141 ++++++++++++++++++++++----------------
 xen/include/asm-x86/hvm/support.h |  12 ++++
 2 files changed, 95 insertions(+), 58 deletions(-)
xen/arch/x86/hvm/hvm.c +83 -58
index c822d3b..0a5697a 100644
@@ -3072,6 +3072,80 @@ void hvm_task_switch(
     hvm_unmap_entry(nptss_desc);
 }
 
+enum hvm_translation_result hvm_translate_get_page(
+    struct vcpu *v, unsigned long addr, bool linear, uint32_t pfec,
+    pagefault_info_t *pfinfo, struct page_info **_page,
+    gfn_t *__gfn, p2m_type_t *_p2mt)
+{
+    struct page_info *page;
+    p2m_type_t p2mt;
+    gfn_t gfn;
+
+    if ( linear )
+    {
+        gfn = _gfn(paging_gva_to_gfn(v, addr, &pfec));
+
+        if ( gfn_eq(gfn, INVALID_GFN) )
+        {
+            if ( pfec & PFEC_page_paged )
+                return HVMTRANS_gfn_paged_out;
+
+            if ( pfec & PFEC_page_shared )
+                return HVMTRANS_gfn_shared;
+
+            if ( pfinfo )
+            {
+                pfinfo->linear = addr;
+                pfinfo->ec = pfec & ~PFEC_implicit;
+            }
+
+            return HVMTRANS_bad_linear_to_gfn;
+        }
+    }
+    else
+        gfn = _gfn(addr >> PAGE_SHIFT);
+
+    /*
+     * No need to do the P2M lookup for internally handled MMIO, benefiting
+     * - 32-bit WinXP (& older Windows) on AMD CPUs for LAPIC accesses,
+     * - newer Windows (like Server 2012) for HPET accesses.
+     */
+    if ( v == current && is_hvm_vcpu(v)
+         && !nestedhvm_vcpu_in_guestmode(v)
+         && hvm_mmio_internal(gfn_x(gfn) << PAGE_SHIFT) )
+        return HVMTRANS_bad_gfn_to_mfn;
+
+    page = get_page_from_gfn(v->domain, gfn_x(gfn), &p2mt, P2M_UNSHARE);
+
+    if ( !page )
+        return HVMTRANS_bad_gfn_to_mfn;
+
+    if ( p2m_is_paging(p2mt) )
+    {
+        put_page(page);
+        p2m_mem_paging_populate(v->domain, gfn_x(gfn));
+        return HVMTRANS_gfn_paged_out;
+    }
+    if ( p2m_is_shared(p2mt) )
+    {
+        put_page(page);
+        return HVMTRANS_gfn_shared;
+    }
+    if ( p2m_is_grant(p2mt) )
+    {
+        put_page(page);
+        return HVMTRANS_unhandleable;
+    }
+
+    *_page = page;
+    if ( __gfn )
+        *__gfn = gfn;
+    if ( _p2mt )
+        *_p2mt = p2mt;
+
+    return HVMTRANS_okay;
+}
+
 #define HVMCOPY_from_guest (0u<<0)
 #define HVMCOPY_to_guest   (1u<<0)
 #define HVMCOPY_phys       (0u<<2)
@@ -3080,7 +3154,7 @@ static enum hvm_translation_result __hvm_copy(
     void *buf, paddr_t addr, int size, struct vcpu *v, unsigned int flags,
     uint32_t pfec, pagefault_info_t *pfinfo)
 {
-    unsigned long gfn;
+    gfn_t gfn;
     struct page_info *page;
     p2m_type_t p2mt;
     char *p;
@@ -3106,65 +3180,15 @@ static enum hvm_translation_result __hvm_copy(
 
     while ( todo > 0 )
     {
+        enum hvm_translation_result res;
         paddr_t gpa = addr & ~PAGE_MASK;
 
         count = min_t(int, PAGE_SIZE - gpa, todo);
 
-        if ( flags & HVMCOPY_linear )
-        {
-            gfn = paging_gva_to_gfn(v, addr, &pfec);
-            if ( gfn == gfn_x(INVALID_GFN) )
-            {
-                if ( pfec & PFEC_page_paged )
-                    return HVMTRANS_gfn_paged_out;
-                if ( pfec & PFEC_page_shared )
-                    return HVMTRANS_gfn_shared;
-                if ( pfinfo )
-                {
-                    pfinfo->linear = addr;
-                    pfinfo->ec = pfec & ~PFEC_implicit;
-                }
-                return HVMTRANS_bad_linear_to_gfn;
-            }
-            gpa |= (paddr_t)gfn << PAGE_SHIFT;
-        }
-        else
-        {
-            gfn = addr >> PAGE_SHIFT;
-            gpa = addr;
-        }
-
-        /*
-         * No need to do the P2M lookup for internally handled MMIO, benefiting
-         * - 32-bit WinXP (& older Windows) on AMD CPUs for LAPIC accesses,
-         * - newer Windows (like Server 2012) for HPET accesses.
-         */
-        if ( v == current && is_hvm_vcpu(v)
-             && !nestedhvm_vcpu_in_guestmode(v)
-             && hvm_mmio_internal(gpa) )
-            return HVMTRANS_bad_gfn_to_mfn;
-
-        page = get_page_from_gfn(v->domain, gfn, &p2mt, P2M_UNSHARE);
-
-        if ( !page )
-            return HVMTRANS_bad_gfn_to_mfn;
-
-        if ( p2m_is_paging(p2mt) )
-        {
-            put_page(page);
-            p2m_mem_paging_populate(v->domain, gfn);
-            return HVMTRANS_gfn_paged_out;
-        }
-        if ( p2m_is_shared(p2mt) )
-        {
-            put_page(page);
-            return HVMTRANS_gfn_shared;
-        }
-        if ( p2m_is_grant(p2mt) )
-        {
-            put_page(page);
-            return HVMTRANS_unhandleable;
-        }
+        res = hvm_translate_get_page(v, addr, flags & HVMCOPY_linear,
+                                     pfec, pfinfo, &page, &gfn, &p2mt);
+        if ( res != HVMTRANS_okay )
+            return res;
 
         p = (char *)__map_domain_page(page) + (addr & ~PAGE_MASK);
 
@@ -3173,10 +3197,11 @@ static enum hvm_translation_result __hvm_copy(
             if ( p2m_is_discard_write(p2mt) )
             {
                 static unsigned long lastpage;
-                if ( xchg(&lastpage, gfn) != gfn )
+
+                if ( xchg(&lastpage, gfn_x(gfn)) != gfn_x(gfn) )
                     dprintk(XENLOG_G_DEBUG,
                             "%pv attempted write to read-only gfn %#lx (mfn=%#lx)\n",
-                            v, gfn, page_to_mfn(page));
+                            v, gfn_x(gfn), page_to_mfn(page));
             }
             else
             {
xen/include/asm-x86/hvm/support.h +12 -0
index a1799a3..42cccf0 100644
@@ -24,6 +24,7 @@
 #include <xen/sched.h>
 #include <xen/hvm/save.h>
 #include <asm/processor.h>
+#include <asm/p2m.h>
 
 #ifndef NDEBUG
 #define DBG_LEVEL_0                 (1 << 0)
@@ -103,6 +104,17 @@ enum hvm_translation_result hvm_fetch_from_guest_linear(
     void *buf, unsigned long addr, int size, uint32_t pfec,
     pagefault_info_t *pfinfo);
 
+/*
+ * Get a reference on the page under an HVM physical or linear address.  If
+ * linear, a pagewalk is performed using pfec (fault details optionally in
+ * pfinfo).
+ * On success, returns HVMTRANS_okay with a reference taken on **_page.
+ */
+enum hvm_translation_result hvm_translate_get_page(
+    struct vcpu *v, unsigned long addr, bool linear, uint32_t pfec,
+    pagefault_info_t *pfinfo, struct page_info **_page,
+    gfn_t *__gfn, p2m_type_t *_p2mt);
+
 #define HVM_HCALL_completed  0 /* hypercall completed - no further action */
 #define HVM_HCALL_preempted  1 /* hypercall preempted - re-execute VMCALL */
 int hvm_hypercall(struct cpu_user_regs *regs);
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#20 lore raw

Re: [PATCH 5/6] x86/hvm: Break out __hvm_copy()'s translation logic

xen/arch/x86/hvm/hvm.c void hvm_task_switch( · line 3078

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 21.06.17 at 17:12, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +4 -0
@@ -3072,6 +3072,80 @@ void hvm_task_switch(
     hvm_unmap_entry(nptss_desc);
 }
 
+enum hvm_translation_result hvm_translate_get_page(
+    struct vcpu *v, unsigned long addr, bool linear, uint32_t pfec,
+    pagefault_info_t *pfinfo, struct page_info **_page,
+    gfn_t *__gfn, p2m_type_t *_p2mt)

Please avoid leading underscores on parameter names (and even more so double ones). page_p, gfn_p, and p2mt_p perhaps?

Quoted 18 lines
xen/arch/x86/hvm/hvm.c +18 -0
+{
+    struct page_info *page;
+    p2m_type_t p2mt;
+    gfn_t gfn;
+
+    if ( linear )
+    {
+        gfn = _gfn(paging_gva_to_gfn(v, addr, &pfec));
+
+        if ( gfn_eq(gfn, INVALID_GFN) )
+        {
+            if ( pfec & PFEC_page_paged )
+                return HVMTRANS_gfn_paged_out;
+
+            if ( pfec & PFEC_page_shared )
+                return HVMTRANS_gfn_shared;
+
+            if ( pfinfo )

xen/arch/x86/hvm/hvm.c void hvm_task_switch( · line 3106

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+            {
+                pfinfo->linear = addr;
+                pfinfo->ec = pfec & ~PFEC_implicit;
+            }
+
+            return HVMTRANS_bad_linear_to_gfn;
+        }
+    }
+    else
+        gfn = _gfn(addr >> PAGE_SHIFT);

As we risk the caller not recognizing that *pfinfo won't be written to on this "else" path, wouldn't it be a good idea to ASSERT(!pfinfo) here?

xen/arch/x86/hvm/hvm.c void hvm_task_switch( · line 3113

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +6 -0
+    /*
+     * No need to do the P2M lookup for internally handled MMIO, benefiting
+     * - 32-bit WinXP (& older Windows) on AMD CPUs for LAPIC accesses,
+     * - newer Windows (like Server 2012) for HPET accesses.
+     */
+    if ( v == current && is_hvm_vcpu(v)

Isn't the is_hvm_vcpu() a stray leftover from the PVHv1 removal, and hence doesn't need retaining here?

Jan

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

#21 lore raw

PATCH 6/6 x86/hvm: Implement hvmemul_write() using real mappings

In reply to Andrew Cooper

An access which crosses a page boundary is performed atomically by x86 hardware, albeit with a severe performance penalty. An important corner case is when a straddled access hits two pages which differ in whether a translation exists, or in net access rights.

The use of hvm_copy*() in hvmemul_write() is problematic, because it performs a translation then completes the partial write, before moving onto the next translation.

If an individual emulated write straddles two pages, the first of which is writable, and the second of which is not, the first half of the write will complete before #PF is raised from the second half.

This results in guest state corruption as a side effect of emulation, which has been observed to cause windows to crash while under introspection.

Introduce the hvmemul_{,un}map_linear_addr() helpers, which translate an entire contents of a linear access, and vmap() the underlying frames to provide a contiguous virtual mapping for the emulator to use. This is the same mechanism as used by the shadow emulation code.

This will catch any translation issues and abort the emulation before any modifications occur.

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Razvan Cojocaru <[email protected]>
CC: Mihai Donțu <[email protected]>

While the maximum size of linear mapping is capped at 1 page, the logic in the helpers is written to work properly as hvmemul_ctxt->mfn[] gets longer, specifically with XSAVE instruction emulation in mind.

This has only had light testing so far.

---
 xen/arch/x86/hvm/emulate.c        | 179 ++++++++++++++++++++++++++++++++++----
 xen/include/asm-x86/hvm/emulate.h |   7 ++
 2 files changed, 169 insertions(+), 17 deletions(-)
xen/arch/x86/hvm/emulate.c +162 -17
index 384ad0b..804bea5 100644
@@ -498,6 +498,159 @@ static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,
 }
 
 /*
+ * Map the frame(s) covering an individual linear access, for writeable
+ * access.  May return NULL for MMIO, or ERR_PTR(~X86EMUL_*) for other errors
+ * including ERR_PTR(~X86EMUL_OKAY) for write-discard mappings.
+ *
+ * In debug builds, map() checks that each slot in hvmemul_ctxt->mfn[] is
+ * clean before use, and poisions unused slots with INVALID_MFN.
+ */
+static void *hvmemul_map_linear_addr(
+    unsigned long linear, unsigned int bytes, uint32_t pfec,
+    struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    struct vcpu *curr = current;
+    void *err, *mapping;
+
+    /* First and final gfns which need mapping. */
+    unsigned long frame = linear >> PAGE_SHIFT, first = frame;
+    unsigned long final = (linear + bytes - !!bytes) >> PAGE_SHIFT;
+
+    /*
+     * mfn points to the next free slot.  All used slots have a page reference
+     * held on them.
+     */
+    mfn_t *mfn = &hvmemul_ctxt->mfn[0];
+
+    /*
+     * The caller has no legitimate reason for trying a zero-byte write, but
+     * final is calculate to fail safe in release builds.
+     *
+     * The maximum write size depends on the number of adjacent mfns[] which
+     * can be vmap()'d, accouting for possible misalignment within the region.
+     * The higher level emulation callers are responsible for ensuring that
+     * mfns[] is large enough for the requested write size.
+     */
+    if ( bytes == 0 ||
+         final - first > ARRAY_SIZE(hvmemul_ctxt->mfn) - 1 )
+    {
+        ASSERT_UNREACHABLE();
+        goto unhandleable;
+    }
+
+    do {
+        enum hvm_translation_result res;
+        struct page_info *page;
+        pagefault_info_t pfinfo;
+        p2m_type_t p2mt;
+
+        res = hvm_translate_get_page(curr, frame << PAGE_SHIFT, true, pfec,
+                                     &pfinfo, &page, NULL, &p2mt);
+
+        switch ( res )
+        {
+        case HVMTRANS_okay:
+            break;
+
+        case HVMTRANS_bad_linear_to_gfn:
+            x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
+            err = ERR_PTR(~(long)X86EMUL_EXCEPTION);
+            goto out;
+
+        case HVMTRANS_bad_gfn_to_mfn:
+            err = NULL;
+            goto out;
+
+        case HVMTRANS_gfn_paged_out:
+        case HVMTRANS_gfn_shared:
+            err = ERR_PTR(~(long)X86EMUL_RETRY);
+            goto out;
+
+        default:
+            goto unhandleable;
+        }
+
+        /* Error checking.  Confirm that the current slot is clean. */
+        ASSERT(mfn_x(*mfn) == 0);
+
+        *mfn++ = _mfn(page_to_mfn(page));
+        frame++;
+
+        if ( p2m_is_discard_write(p2mt) )
+        {
+            err = ERR_PTR(~(long)X86EMUL_OKAY);
+            goto out;
+        }
+
+    } while ( frame < final );
+
+    /* Entire access within a single frame? */
+    if ( first == final )
+        mapping = map_domain_page(hvmemul_ctxt->mfn[0]) + (linear & ~PAGE_MASK);
+    /* Multiple frames? Need to vmap(). */
+    else if ( (mapping = vmap(hvmemul_ctxt->mfn,
+                              mfn - hvmemul_ctxt->mfn)) == NULL )
+        goto unhandleable;
+
+#ifndef NDEBUG /* Poision unused mfn[]s with INVALID_MFN. */
+    while ( mfn < hvmemul_ctxt->mfn + ARRAY_SIZE(hvmemul_ctxt->mfn) )
+    {
+        ASSERT(mfn_x(*mfn) == 0);
+        *mfn++ = INVALID_MFN;
+    }
+#endif
+
+    return mapping;
+
+ unhandleable:
+    err = ERR_PTR(~(long)X86EMUL_UNHANDLEABLE);
+
+ out:
+    /* Drop all held references. */
+    while ( mfn > hvmemul_ctxt->mfn )
+        put_page(mfn_to_page(mfn_x(*mfn--)));
+
+    return err;
+}
+
+static void hvmemul_unmap_linear_addr(
+    void *mapping, unsigned long linear, unsigned int bytes,
+    struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    struct domain *currd = current->domain;
+    unsigned long frame = linear >> PAGE_SHIFT;
+    unsigned long final = (linear + bytes - !!bytes) >> PAGE_SHIFT;
+    mfn_t *mfn = &hvmemul_ctxt->mfn[0];
+
+    ASSERT(bytes > 0);
+
+    if ( frame == final )
+        unmap_domain_page(mapping);
+    else
+        vunmap(mapping);
+
+    do
+    {
+        ASSERT(mfn_valid(*mfn));
+        paging_mark_dirty(currd, *mfn);
+        put_page(mfn_to_page(mfn_x(*mfn)));
+
+        frame++;
+        *mfn++ = _mfn(0); /* Clean slot for map()'s error checking. */
+
+    } while ( frame < final );
+
+
+#ifndef NDEBUG /* Check (and clean) all unused mfns. */
+    while ( mfn < hvmemul_ctxt->mfn + ARRAY_SIZE(hvmemul_ctxt->mfn) )
+    {
+        ASSERT(mfn_eq(*mfn, INVALID_MFN));
+        *mfn++ = _mfn(0);
+    }
+#endif
+}
+
+/*
  * Convert addr from linear to physical form, valid over the range
  * [addr, addr + *reps * bytes_per_rep]. *reps is adjusted according to
  * the valid computed range. It is always >0 when X86EMUL_OKAY is returned.
@@ -987,11 +1140,11 @@ static int hvmemul_write(
     struct hvm_emulate_ctxt *hvmemul_ctxt =
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
     struct vcpu *curr = current;
-    pagefault_info_t pfinfo;
     unsigned long addr, reps = 1;
     uint32_t pfec = PFEC_page_present | PFEC_write_access;
     struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
     int rc;
+    void *mapping;
 
     if ( is_x86_system_segment(seg) )
         pfec |= PFEC_implicit;
@@ -1007,23 +1160,15 @@ static int hvmemul_write(
          (vio->mmio_gla == (addr & PAGE_MASK)) )
         return hvmemul_linear_mmio_write(addr, bytes, p_data, pfec, hvmemul_ctxt, 1);
 
-    rc = hvm_copy_to_guest_linear(addr, p_data, bytes, pfec, &pfinfo);
-
-    switch ( rc )
-    {
-    case HVMTRANS_okay:
-        break;
-    case HVMTRANS_bad_linear_to_gfn:
-        x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
-        return X86EMUL_EXCEPTION;
-    case HVMTRANS_bad_gfn_to_mfn:
+    mapping = hvmemul_map_linear_addr(addr, bytes, pfec, hvmemul_ctxt);
+    if ( IS_ERR(mapping) )
+        return ~PTR_ERR(mapping);
+    else if ( !mapping )
         return hvmemul_linear_mmio_write(addr, bytes, p_data, pfec, hvmemul_ctxt, 0);
-    case HVMTRANS_gfn_paged_out:
-    case HVMTRANS_gfn_shared:
-        return X86EMUL_RETRY;
-    default:
-        return X86EMUL_UNHANDLEABLE;
-    }
+
+    memcpy(mapping, p_data, bytes);
+
+    hvmemul_unmap_linear_addr(mapping, addr, bytes, hvmemul_ctxt);
 
     return X86EMUL_OKAY;
 }
xen/include/asm-x86/hvm/emulate.h +7 -0
index 8864775..65efd4e 100644
@@ -37,6 +37,13 @@ struct hvm_emulate_ctxt {
     unsigned long seg_reg_accessed;
     unsigned long seg_reg_dirty;
 
+    /*
+     * MFNs behind temporary mappings in the write callback.  The length is
+     * arbitrary, and can be increased if writes longer than PAGE_SIZE are
+     * needed.
+     */
+    mfn_t mfn[2];
+
     uint32_t intr_shadow;
 
     bool_t set_context;
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#22 lore raw

Re: [PATCH 6/6] x86/hvm: Implement hvmemul_write() using real mappings

Quoted 73 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 21 June 2017 16:13
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Jan Beulich <[email protected]>; Paul Durrant <[email protected]>; Razvan Cojocaru <[email protected]>; Mihai Donțu <[email protected]>
Subject: [PATCH 6/6] x86/hvm: Implement hvmemul_write() using real mappings

An access which crosses a page boundary is performed atomically by x86 hardware, albeit with a severe performance penalty. An important corner case
is when a straddled access hits two pages which differ in whether a translation exists, or in net access rights.

The use of hvm_copy*() in hvmemul_write() is problematic, because it performs
a translation then completes the partial write, before moving onto the next translation.

If an individual emulated write straddles two pages, the first of which is writable, and the second of which is not, the first half of the write will complete before #PF is raised from the second half.

This results in guest state corruption as a side effect of emulation, which has been observed to cause windows to crash while under introspection.

Introduce the hvmemul_{,un}map_linear_addr() helpers, which translate an entire contents of a linear access, and vmap() the underlying frames to provide a contiguous virtual mapping for the emulator to use. This is the same mechanism as used by the shadow emulation code.

This will catch any translation issues and abort the emulation before any modifications occur.

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Razvan Cojocaru <[email protected]>
CC: Mihai Donțu <[email protected]>

While the maximum size of linear mapping is capped at 1 page, the logic in the
helpers is written to work properly as hvmemul_ctxt->mfn[] gets longer, specifically with XSAVE instruction emulation in mind.

This has only had light testing so far.

---
 xen/arch/x86/hvm/emulate.c        | 179
++++++++++++++++++++++++++++++++++----
 xen/include/asm-x86/hvm/emulate.h |   7 ++
 2 files changed, 169 insertions(+), 17 deletions(-)
xen/arch/x86/hvm/emulate.c +2 -0
index 384ad0b..804bea5 100644
@@ -498,6 +498,159 @@ static int hvmemul_do_mmio_addr(paddr_t
 mmio_gpa,
 }
 
 /*
+ * Map the frame(s) covering an individual linear access, for writeable
+ * access.  May return NULL for MMIO, or ERR_PTR(~X86EMUL_*) for other

errors
+ * including ERR_PTR(~X86EMUL_OKAY) for write-discard mappings.
+ *
+ * In debug builds, map() checks that each slot in hvmemul_ctxt->mfn[] is
+ * clean before use, and poisions unused slots with INVALID_MFN.
+ */

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 517

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+static void *hvmemul_map_linear_addr(
+    unsigned long linear, unsigned int bytes, uint32_t pfec,
+    struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    struct vcpu *curr = current;
+    void *err, *mapping;
+
+    /* First and final gfns which need mapping. */
+    unsigned long frame = linear >> PAGE_SHIFT, first = frame;
+    unsigned long final = (linear + bytes - !!bytes) >> PAGE_SHIFT;

Do we need to worry about linear + bytes overflowing here?

Also, is this ever legitimately called with bytes == 0?

Quoted 13 lines
xen/arch/x86/hvm/emulate.c +12 -0
+
+    /*
+     * mfn points to the next free slot.  All used slots have a page reference
+     * held on them.
+     */
+    mfn_t *mfn = &hvmemul_ctxt->mfn[0];
+
+    /*
+     * The caller has no legitimate reason for trying a zero-byte write, but
+     * final is calculate to fail safe in release builds.
+     *
+     * The maximum write size depends on the number of adjacent mfns[]

which

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 536

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+     * can be vmap()'d, accouting for possible misalignment within the region.
+     * The higher level emulation callers are responsible for ensuring that
+     * mfns[] is large enough for the requested write size.
+     */
+    if ( bytes == 0 ||
+         final - first > ARRAY_SIZE(hvmemul_ctxt->mfn) - 1 )
+    {

I guess not, so why the weird looking calculation for final? It's value will not be used when bytes == 0.

Quoted 80 lines
xen/arch/x86/hvm/emulate.c +53 -0
+        ASSERT_UNREACHABLE();
+        goto unhandleable;
+    }
+
+    do {
+        enum hvm_translation_result res;
+        struct page_info *page;
+        pagefault_info_t pfinfo;
+        p2m_type_t p2mt;
+
+        res = hvm_translate_get_page(curr, frame << PAGE_SHIFT, true, pfec,
+                                     &pfinfo, &page, NULL, &p2mt);
+
+        switch ( res )
+        {
+        case HVMTRANS_okay:
+            break;
+
+        case HVMTRANS_bad_linear_to_gfn:
+            x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
+            err = ERR_PTR(~(long)X86EMUL_EXCEPTION);
+            goto out;
+
+        case HVMTRANS_bad_gfn_to_mfn:
+            err = NULL;
+            goto out;
+
+        case HVMTRANS_gfn_paged_out:
+        case HVMTRANS_gfn_shared:
+            err = ERR_PTR(~(long)X86EMUL_RETRY);
+            goto out;
+
+        default:
+            goto unhandleable;
+        }
+
+        /* Error checking.  Confirm that the current slot is clean. */
+        ASSERT(mfn_x(*mfn) == 0);
+
+        *mfn++ = _mfn(page_to_mfn(page));
+        frame++;
+
+        if ( p2m_is_discard_write(p2mt) )
+        {
+            err = ERR_PTR(~(long)X86EMUL_OKAY);
+            goto out;
+        }
+
+    } while ( frame < final );
+
+    /* Entire access within a single frame? */
+    if ( first == final )
+        mapping = map_domain_page(hvmemul_ctxt->mfn[0]) + (linear &

~PAGE_MASK);

xen/arch/x86/hvm/emulate.c +26 -0
+    /* Multiple frames? Need to vmap(). */
+    else if ( (mapping = vmap(hvmemul_ctxt->mfn,
+                              mfn - hvmemul_ctxt->mfn)) == NULL )
+        goto unhandleable;
+
+#ifndef NDEBUG /* Poision unused mfn[]s with INVALID_MFN. */
+    while ( mfn < hvmemul_ctxt->mfn + ARRAY_SIZE(hvmemul_ctxt->mfn) )
+    {
+        ASSERT(mfn_x(*mfn) == 0);
+        *mfn++ = INVALID_MFN;
+    }
+#endif
+
+    return mapping;
+
+ unhandleable:
+    err = ERR_PTR(~(long)X86EMUL_UNHANDLEABLE);
+
+ out:
+    /* Drop all held references. */
+    while ( mfn > hvmemul_ctxt->mfn )
+        put_page(mfn_to_page(mfn_x(*mfn--)));
+
+    return err;
+}
+

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 625

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+static void hvmemul_unmap_linear_addr(
+    void *mapping, unsigned long linear, unsigned int bytes,
+    struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    struct domain *currd = current->domain;
+    unsigned long frame = linear >> PAGE_SHIFT;
+    unsigned long final = (linear + bytes - !!bytes) >> PAGE_SHIFT;
+    mfn_t *mfn = &hvmemul_ctxt->mfn[0];
+
+    ASSERT(bytes > 0);

Why not return if bytes == 0? I know it's not a legitimate call but in a non-debug build it would result in unmap_domain_page() being called below.

Paul

Quoted 100 lines
xen/arch/x86/hvm/emulate.c +28 -0
+
+    if ( frame == final )
+        unmap_domain_page(mapping);
+    else
+        vunmap(mapping);
+
+    do
+    {
+        ASSERT(mfn_valid(*mfn));
+        paging_mark_dirty(currd, *mfn);
+        put_page(mfn_to_page(mfn_x(*mfn)));
+
+        frame++;
+        *mfn++ = _mfn(0); /* Clean slot for map()'s error checking. */
+
+    } while ( frame < final );
+
+
+#ifndef NDEBUG /* Check (and clean) all unused mfns. */
+    while ( mfn < hvmemul_ctxt->mfn + ARRAY_SIZE(hvmemul_ctxt->mfn) )
+    {
+        ASSERT(mfn_eq(*mfn, INVALID_MFN));
+        *mfn++ = _mfn(0);
+    }
+#endif
+}
+
+/*
  * Convert addr from linear to physical form, valid over the range
  * [addr, addr + *reps * bytes_per_rep]. *reps is adjusted according to
  * the valid computed range. It is always >0 when X86EMUL_OKAY is

returned.

xen/arch/x86/hvm/emulate.c +9 -17
@@ -987,11 +1140,11 @@ static int hvmemul_write(
     struct hvm_emulate_ctxt *hvmemul_ctxt =
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
     struct vcpu *curr = current;
-    pagefault_info_t pfinfo;
     unsigned long addr, reps = 1;
     uint32_t pfec = PFEC_page_present | PFEC_write_access;
     struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
     int rc;
+    void *mapping;
 
     if ( is_x86_system_segment(seg) )
         pfec |= PFEC_implicit;
@@ -1007,23 +1160,15 @@ static int hvmemul_write(
          (vio->mmio_gla == (addr & PAGE_MASK)) )
         return hvmemul_linear_mmio_write(addr, bytes, p_data, pfec,
 hvmemul_ctxt, 1);
 
-    rc = hvm_copy_to_guest_linear(addr, p_data, bytes, pfec, &pfinfo);
-
-    switch ( rc )
-    {
-    case HVMTRANS_okay:
-        break;
-    case HVMTRANS_bad_linear_to_gfn:
-        x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
-        return X86EMUL_EXCEPTION;
-    case HVMTRANS_bad_gfn_to_mfn:
+    mapping = hvmemul_map_linear_addr(addr, bytes, pfec,
 hvmemul_ctxt);
+    if ( IS_ERR(mapping) )
+        return ~PTR_ERR(mapping);
+    else if ( !mapping )
         return hvmemul_linear_mmio_write(addr, bytes, p_data, pfec,
 hvmemul_ctxt, 0);
-    case HVMTRANS_gfn_paged_out:
-    case HVMTRANS_gfn_shared:
-        return X86EMUL_RETRY;
-    default:
-        return X86EMUL_UNHANDLEABLE;
-    }
+
+    memcpy(mapping, p_data, bytes);
+
+    hvmemul_unmap_linear_addr(mapping, addr, bytes, hvmemul_ctxt);
     return X86EMUL_OKAY;

}

xen/include/asm- renamed from xen/include/asm-x86/hvm/emulate.h +0 -0

x86/hvm/emulate.h
index 8864775..65efd4e 100644

xen/include/asm-x86/hvm/emulate.h +7 -1
@@ -37,6 +37,13 @@ struct hvm_emulate_ctxt {
     unsigned long seg_reg_accessed;
     unsigned long seg_reg_dirty;
 
+    /*
+     * MFNs behind temporary mappings in the write callback.  The length is
+     * arbitrary, and can be increased if writes longer than PAGE_SIZE are
+     * needed.
+     */
+    mfn_t mfn[2];
+
     uint32_t intr_shadow;
 
     bool_t set_context;
--
Quoted 1 line

2.1.4

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

#23 lore raw

Re: [PATCH 6/6] x86/hvm: Implement hvmemul_write() using real mappings

In reply to Andrew Cooper
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 21.06.17 at 17:12, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +7 -0
@@ -498,6 +498,159 @@ static int hvmemul_do_mmio_addr(paddr_t mmio_gpa,
 }
 
 /*
+ * Map the frame(s) covering an individual linear access, for writeable
+ * access.  May return NULL for MMIO, or ERR_PTR(~X86EMUL_*) for other errors
+ * including ERR_PTR(~X86EMUL_OKAY) for write-discard mappings.
+ *
+ * In debug builds, map() checks that each slot in hvmemul_ctxt->mfn[] is
+ * clean before use, and poisions unused slots with INVALID_MFN.
+ */

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 517

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+static void *hvmemul_map_linear_addr(
+    unsigned long linear, unsigned int bytes, uint32_t pfec,
+    struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    struct vcpu *curr = current;
+    void *err, *mapping;
+
+    /* First and final gfns which need mapping. */
+    unsigned long frame = linear >> PAGE_SHIFT, first = frame;
+    unsigned long final = (linear + bytes - !!bytes) >> PAGE_SHIFT;

I second Paul's desire for the zero bytes case to be rejected up the call stack.

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+    /*
+     * mfn points to the next free slot.  All used slots have a page reference
+     * held on them.
+     */
+    mfn_t *mfn = &hvmemul_ctxt->mfn[0];
+
+    /*

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 535

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+     * The caller has no legitimate reason for trying a zero-byte write, but
+     * final is calculate to fail safe in release builds.
+     *
+     * The maximum write size depends on the number of adjacent mfns[] which
+     * can be vmap()'d, accouting for possible misalignment within the region.
+     * The higher level emulation callers are responsible for ensuring that
+     * mfns[] is large enough for the requested write size.
+     */
+    if ( bytes == 0 ||
+         final - first > ARRAY_SIZE(hvmemul_ctxt->mfn) - 1 )

Any reason not to use ">= ARRAY_SIZE(hvmemul_ctxt->mfn)"

Quoted 29 lines
xen/arch/x86/hvm/emulate.c +29 -0
+    {
+        ASSERT_UNREACHABLE();
+        goto unhandleable;
+    }
+
+    do {
+        enum hvm_translation_result res;
+        struct page_info *page;
+        pagefault_info_t pfinfo;
+        p2m_type_t p2mt;
+
+        res = hvm_translate_get_page(curr, frame << PAGE_SHIFT, true, pfec,
+                                     &pfinfo, &page, NULL, &p2mt);
+
+        switch ( res )
+        {
+        case HVMTRANS_okay:
+            break;
+
+        case HVMTRANS_bad_linear_to_gfn:
+            x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
+            err = ERR_PTR(~(long)X86EMUL_EXCEPTION);
+            goto out;
+
+        case HVMTRANS_bad_gfn_to_mfn:
+            err = NULL;
+            goto out;
+
+        case HVMTRANS_gfn_paged_out:

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 574

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+        case HVMTRANS_gfn_shared:
+            err = ERR_PTR(~(long)X86EMUL_RETRY);
+            goto out;
+
+        default:
+            goto unhandleable;
+        }
+
+        /* Error checking.  Confirm that the current slot is clean. */
+        ASSERT(mfn_x(*mfn) == 0);

Wouldn't this better be done first thing in the loop? And wouldn't the value better be INVALID_MFN?

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 582

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+        *mfn++ = _mfn(page_to_mfn(page));
+        frame++;
+
+        if ( p2m_is_discard_write(p2mt) )
+        {
+            err = ERR_PTR(~(long)X86EMUL_OKAY);
+            goto out;

If one page is discard-write and the other isn't, this will end up being wrong.

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 592

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+        }
+
+    } while ( frame < final );
+
+    /* Entire access within a single frame? */
+    if ( first == final )
+        mapping = map_domain_page(hvmemul_ctxt->mfn[0]) + (linear & ~PAGE_MASK);
+    /* Multiple frames? Need to vmap(). */
+    else if ( (mapping = vmap(hvmemul_ctxt->mfn,
+                              mfn - hvmemul_ctxt->mfn)) == NULL )

final - first + 1 would likely yield better code.

Quoted 9 lines
xen/arch/x86/hvm/emulate.c +9 -0
+        goto unhandleable;
+
+#ifndef NDEBUG /* Poision unused mfn[]s with INVALID_MFN. */
+    while ( mfn < hvmemul_ctxt->mfn + ARRAY_SIZE(hvmemul_ctxt->mfn) )
+    {
+        ASSERT(mfn_x(*mfn) == 0);
+        *mfn++ = INVALID_MFN;
+    }
+#endif

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 611

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+
+    return mapping;
+
+ unhandleable:
+    err = ERR_PTR(~(long)X86EMUL_UNHANDLEABLE);
+
+ out:
+    /* Drop all held references. */
+    while ( mfn > hvmemul_ctxt->mfn )
+        put_page(mfn_to_page(mfn_x(*mfn--)));

ITYM

    while ( mfn-- > hvmemul_ctxt->mfn )
        put_page(mfn_to_page(mfn_x(*mfn)));

or

    while ( mfn > hvmemul_ctxt->mfn )
        put_page(mfn_to_page(mfn_x(*--mfn)));

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 617

Quoted 2 lines
xen/arch/x86/hvm/emulate.c +2 -0
+static void hvmemul_unmap_linear_addr(
+    void *mapping, unsigned long linear, unsigned int bytes,

Both vunmap() and unmap_domain_page() take pointers to const, so please use const on the pointer here too.

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 612

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+    struct hvm_emulate_ctxt *hvmemul_ctxt)

There upsides and downsides to requiring the caller to pass in the same values as to map(): You can do more correctness checking here, but you also risk the caller using the wrong values (perhaps because of a meanwhile updated local variable). While I don't outright object to this approach, personally I'd prefer minimal inputs here, and the code deriving everything from hvmemul_ctxt.

Quoted 8 lines
xen/arch/x86/hvm/emulate.c +0 -4
@@ -1007,23 +1160,15 @@ static int hvmemul_write(
          (vio->mmio_gla == (addr & PAGE_MASK)) )
         return hvmemul_linear_mmio_write(addr, bytes, p_data, pfec, hvmemul_ctxt, 1);
 
-    rc = hvm_copy_to_guest_linear(addr, p_data, bytes, pfec, &pfinfo);
-
-    switch ( rc )
-    {

xen/arch/x86/hvm/emulate.c static int hvmemul_write( · line 1166

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +4 -6
-    case HVMTRANS_okay:
-        break;
-    case HVMTRANS_bad_linear_to_gfn:
-        x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
-        return X86EMUL_EXCEPTION;
-    case HVMTRANS_bad_gfn_to_mfn:
+    mapping = hvmemul_map_linear_addr(addr, bytes, pfec, hvmemul_ctxt);
+    if ( IS_ERR(mapping) )
+        return ~PTR_ERR(mapping);
+    else if ( !mapping )

Pointless "else".

xen/arch/x86/hvm/emulate.c static int hvmemul_write( · line 1167

Quoted 1 line
xen/arch/x86/hvm/emulate.c +0 -0
         return hvmemul_linear_mmio_write(addr, bytes, p_data, pfec, hvmemul_ctxt, 0);

Considering the 2nd linear -> guest-phys translation done here, did you consider having hvmemul_map_linear_addr() obtain and provide the GFNs?

xen/include/asm-x86/hvm/emulate.h struct hvm_emulate_ctxt { · line 45

Quoted 12 lines
xen/include/asm-x86/hvm/emulate.h +6 -0
@@ -37,6 +37,13 @@ struct hvm_emulate_ctxt {
     unsigned long seg_reg_accessed;
     unsigned long seg_reg_dirty;
 
+    /*
+     * MFNs behind temporary mappings in the write callback.  The length is
+     * arbitrary, and can be increased if writes longer than PAGE_SIZE are
+     * needed.
+     */
+    mfn_t mfn[2];

Mind being precise in the comment, saying "PAGE_SIZE+1"?

Jan

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

#24 lore raw

Re: [PATCH 6/6] x86/hvm: Implement hvmemul_write() using real mappings

Quoted 18 lines · Jan Beulich
Quoted 8 lines
xen/arch/x86/hvm/emulate.c +7 -0
+    /*
+     * mfn points to the next free slot.  All used slots have a page reference
+     * held on them.
+     */
+    mfn_t *mfn = &hvmemul_ctxt->mfn[0];
+
+    /*

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 535

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+     * The caller has no legitimate reason for trying a zero-byte write, but
+     * final is calculate to fail safe in release builds.
+     *
+     * The maximum write size depends on the number of adjacent mfns[] which
+     * can be vmap()'d, accouting for possible misalignment within the region.
+     * The higher level emulation callers are responsible for ensuring that
+     * mfns[] is large enough for the requested write size.
+     */
+    if ( bytes == 0 ||
+         final - first > ARRAY_SIZE(hvmemul_ctxt->mfn) - 1 )
Quoted 1 line

Any reason not to use ">= ARRAY_SIZE(hvmemul_ctxt->mfn)"

It more obviously identifies the accounting for misalignment.

Quoted 33 lines
Quoted 33 lines
xen/arch/x86/hvm/emulate.c +32 -0
+    {
+        ASSERT_UNREACHABLE();
+        goto unhandleable;
+    }
+
+    do {
+        enum hvm_translation_result res;
+        struct page_info *page;
+        pagefault_info_t pfinfo;
+        p2m_type_t p2mt;
+
+        res = hvm_translate_get_page(curr, frame << PAGE_SHIFT, true, pfec,
+                                     &pfinfo, &page, NULL, &p2mt);
+
+        switch ( res )
+        {
+        case HVMTRANS_okay:
+            break;
+
+        case HVMTRANS_bad_linear_to_gfn:
+            x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
+            err = ERR_PTR(~(long)X86EMUL_EXCEPTION);
+            goto out;
+
+        case HVMTRANS_bad_gfn_to_mfn:
+            err = NULL;
+            goto out;
+
+        case HVMTRANS_gfn_paged_out:
+        case HVMTRANS_gfn_shared:
+            err = ERR_PTR(~(long)X86EMUL_RETRY);
+            goto out;
Quoted 8 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 574

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+
+        default:
+            goto unhandleable;
+        }
+
+        /* Error checking.  Confirm that the current slot is clean. */
+        ASSERT(mfn_x(*mfn) == 0);

Wouldn't this better be done first thing in the loop?

IMO its clearer to keep it next to the assignment, but yes, could in principle move to the top of the loop.

Quoted 1 line

And wouldn't the value better be INVALID_MFN?

The backing array is zeroed by hvm_emulate_init_once(), so relying on 0 here is more convenient.

Furthermore, INVALID_MFN is used lower down to poison unused slots, so initialising the whole array to INVALID_MFN reduces the effectiveness of the checks.

Quoted 10 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 582

Quoted 8 lines
xen/arch/x86/hvm/emulate.c +7 -0
+        *mfn++ = _mfn(page_to_mfn(page));
+        frame++;
+
+        if ( p2m_is_discard_write(p2mt) )
+        {
+            err = ERR_PTR(~(long)X86EMUL_OKAY);
+            goto out;

If one page is discard-write and the other isn't, this will end up being wrong.

Straddled accesses are always a grey area, and discard-write is an extra special case which only exists inside Xen. Discard-write means that the guest knows that it shouldn't write there at all.

Doing nothing (by logically extending the discard-write restriction over the entire region) is the least bad option here, IMO.

Quoted 21 lines
Quoted 9 lines
xen/arch/x86/hvm/emulate.c +9 -0
+        goto unhandleable;
+
+#ifndef NDEBUG /* Poision unused mfn[]s with INVALID_MFN. */
+    while ( mfn < hvmemul_ctxt->mfn + ARRAY_SIZE(hvmemul_ctxt->mfn) )
+    {
+        ASSERT(mfn_x(*mfn) == 0);
+        *mfn++ = INVALID_MFN;
+    }
+#endif

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 611

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+
+    return mapping;
+
+ unhandleable:
+    err = ERR_PTR(~(long)X86EMUL_UNHANDLEABLE);
+
+ out:
+    /* Drop all held references. */
+    while ( mfn > hvmemul_ctxt->mfn )
+        put_page(mfn_to_page(mfn_x(*mfn--)));

ITYM

Quoted 7 lines
    while ( mfn-- > hvmemul_ctxt->mfn )
        put_page(mfn_to_page(mfn_x(*mfn)));

or

    while ( mfn > hvmemul_ctxt->mfn )
        put_page(mfn_to_page(mfn_x(*--mfn)));

Yes, I do.

Quoted 5 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 617

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +2 -0
+static void hvmemul_unmap_linear_addr(
+    void *mapping, unsigned long linear, unsigned int bytes,

Both vunmap() and unmap_domain_page() take pointers to const, so please use const on the pointer here too.

The meaning of const void *p in C is "this function does not modify the content pointed to by p".

Both vunmap() and unmap_domain_page() mutate the content being pointed to, so should not take const pointers.

Quoted 8 lines
Quoted 2 lines

+ struct hvm_emulate_ctxt *hvmemul_ctxt)

There upsides and downsides to requiring the caller to pass in the same values as to map(): You can do more correctness checking here, but you also risk the caller using the wrong values (perhaps because of a meanwhile updated local variable). While I don't outright object to this approach, personally I'd prefer minimal inputs here, and the code deriving everything from hvmemul_ctxt.

I'm not sure exactly how we might wish to extend this logic. Are we ever going to want more than one active mapping at once (perhaps rep movs emulation across two ram regions)?

The other reason is that in the release builds, even if we stored the pointer in hvmemul_ctxt, we still cant determine which unmapping function to use without linear and size.

Quoted 21 lines
Quoted 9 lines
xen/arch/x86/hvm/emulate.c +0 -4
@@ -1007,23 +1160,15 @@ static int hvmemul_write(
          (vio->mmio_gla == (addr & PAGE_MASK)) )
         return hvmemul_linear_mmio_write(addr, bytes, p_data, pfec, hvmemul_ctxt, 1);
 
-    rc = hvm_copy_to_guest_linear(addr, p_data, bytes, pfec, &pfinfo);
-
-    switch ( rc )
-    {

xen/arch/x86/hvm/emulate.c static int hvmemul_write( · line 1166

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +4 -6
-    case HVMTRANS_okay:
-        break;
-    case HVMTRANS_bad_linear_to_gfn:
-        x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
-        return X86EMUL_EXCEPTION;
-    case HVMTRANS_bad_gfn_to_mfn:
+    mapping = hvmemul_map_linear_addr(addr, bytes, pfec, hvmemul_ctxt);
+    if ( IS_ERR(mapping) )
+        return ~PTR_ERR(mapping);
+    else if ( !mapping )

Pointless "else".

Quoted 4 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_write( · line 1167

Quoted 1 line
xen/arch/x86/hvm/emulate.c +0 -0
         return hvmemul_linear_mmio_write(addr, bytes, p_data, pfec, hvmemul_ctxt, 0);

Considering the 2nd linear -> guest-phys translation done here, did you consider having hvmemul_map_linear_addr() obtain and provide the GFNs?

Paul has some plans which should remove this second entry into the MMIO handling code.

Quoted 7 lines
Quoted 7 lines
xen/include/asm-x86/hvm/emulate.h +0 -0
@@ -37,6 +37,13 @@ struct hvm_emulate_ctxt {
     unsigned long seg_reg_accessed;
     unsigned long seg_reg_dirty;
 
Quoted 7 lines

xen/include/asm-x86/hvm/emulate.h struct hvm_emulate_ctxt { · line 45

Quoted 6 lines
xen/include/asm-x86/hvm/emulate.h +6 -0
+    /*
+     * MFNs behind temporary mappings in the write callback.  The length is
+     * arbitrary, and can be increased if writes longer than PAGE_SIZE are
+     * needed.
+     */
+    mfn_t mfn[2];

Mind being precise in the comment, saying "PAGE_SIZE+1"?

While that is strictly true, it is not the behaviour which the map() function takes. I don't think it is worth the overhead of fixing that boundary condition for one extra byte, at which point the documentation should match the implementation.

~Andrew

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

#25 lore raw

Re: [PATCH 6/6] x86/hvm: Implement hvmemul_write() using real mappings

Quoted 46 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 17:07, <[email protected]> wrote:
Quoted 32 lines · Jan Beulich
Quoted 32 lines
xen/arch/x86/hvm/emulate.c +32 -0
+    {
+        ASSERT_UNREACHABLE();
+        goto unhandleable;
+    }
+
+    do {
+        enum hvm_translation_result res;
+        struct page_info *page;
+        pagefault_info_t pfinfo;
+        p2m_type_t p2mt;
+
+        res = hvm_translate_get_page(curr, frame << PAGE_SHIFT, true, pfec,
+                                     &pfinfo, &page, NULL, &p2mt);
+
+        switch ( res )
+        {
+        case HVMTRANS_okay:
+            break;
+
+        case HVMTRANS_bad_linear_to_gfn:
+            x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
+            err = ERR_PTR(~(long)X86EMUL_EXCEPTION);
+            goto out;
+
+        case HVMTRANS_bad_gfn_to_mfn:
+            err = NULL;
+            goto out;
+
+        case HVMTRANS_gfn_paged_out:
+        case HVMTRANS_gfn_shared:
+            err = ERR_PTR(~(long)X86EMUL_RETRY);
+            goto out;
Quoted 8 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 574

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+
+        default:
+            goto unhandleable;
+        }
+
+        /* Error checking.  Confirm that the current slot is clean. */
+        ASSERT(mfn_x(*mfn) == 0);

Wouldn't this better be done first thing in the loop?

IMO its clearer to keep it next to the assignment, but yes, could in principle move to the top of the loop.

Quoted 8 lines
Quoted 1 line

And wouldn't the value better be INVALID_MFN?

The backing array is zeroed by hvm_emulate_init_once(), so relying on 0 here is more convenient.

Furthermore, INVALID_MFN is used lower down to poison unused slots, so initialising the whole array to INVALID_MFN reduces the effectiveness of the checks.

Well, further down I had implicitly asked whether some of the checks wouldn't better go away (as they can't be carried out with some of the redundant unmap inputs dropped).

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+        *mfn++ = _mfn(page_to_mfn(page));
+        frame++;
+
+        if ( p2m_is_discard_write(p2mt) )
+        {
+            err = ERR_PTR(~(long)X86EMUL_OKAY);
+            goto out;
Quoted 6 lines
Quoted 2 lines

If one page is discard-write and the other isn't, this will end up being wrong.

Straddled accesses are always a grey area, and discard-write is an extra special case which only exists inside Xen. Discard-write means that the guest knows that it shouldn't write there at all.

Is it the case that the guest knows? Iirc this type had been introduced for introspection tool use.

Quoted 2 lines

Doing nothing (by logically extending the discard-write restriction over the entire region) is the least bad option here, IMO.

Okay, that's a reasonable argument. I'd suggest saying so explicitly in a comment, though.

Quoted 10 lines
Quoted 4 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 617

Quoted 2 lines
xen/arch/x86/hvm/emulate.c +2 -0
+static void hvmemul_unmap_linear_addr(
+    void *mapping, unsigned long linear, unsigned int bytes,

Both vunmap() and unmap_domain_page() take pointers to const, so please use const on the pointer here too.

The meaning of const void *p in C is "this function does not modify the content pointed to by p".

Both vunmap() and unmap_domain_page() mutate the content being pointed to, so should not take const pointers.

We've had this discussion before, and I continue to take a different position: When you free a memory block, you implicitly declare its contents undefined. An undefined modification to undefined contents still yields undefined. So no actual change. Furthermore it is not a given that a freeing routine actually touches the handed memory block at all - that's an internal implementation detail of the allocator.

Plus - not having the parameters const means you can't allocate and initialize something in one go, and then store or pass around a pointer to it which is const-qualified to make clear the contents of that memory block aren't supposed to be further modified (until de-allocation).

Quoted 11 lines
Quoted 7 lines
Quoted 1 line

+ struct hvm_emulate_ctxt *hvmemul_ctxt)

There upsides and downsides to requiring the caller to pass in the same values as to map(): You can do more correctness checking here, but you also risk the caller using the wrong values (perhaps because of a meanwhile updated local variable). While I don't outright object to this approach, personally I'd prefer minimal inputs here, and the code deriving everything from hvmemul_ctxt.

I'm not sure exactly how we might wish to extend this logic. Are we ever going to want more than one active mapping at once (perhaps rep movs emulation across two ram regions)?

I could imagine even more than two regions - from an abstract perspective it may be possible / helpful to have a mapping of each piece of memory a single instruction accesses, along the lines of minimal number of architecturally guaranteed TLB entries on ia64 in order to execute any possible x86 insn.

Quoted 3 lines

The other reason is that in the release builds, even if we stored the pointer in hvmemul_ctxt, we still cant determine which unmapping function to use without linear and size.

I don't understand - all you pass on is "mapping". And whether to unmap a single or two pages could be told from hvmemul_ctxt->mfn[1] (not) being INVALID_MFN.

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
xen/include/asm-x86/hvm/emulate.h +0 -0
@@ -37,6 +37,13 @@ struct hvm_emulate_ctxt {
     unsigned long seg_reg_accessed;
     unsigned long seg_reg_dirty;
 
Quoted 7 lines

xen/include/asm-x86/hvm/emulate.h struct hvm_emulate_ctxt { · line 45

Quoted 6 lines
xen/include/asm-x86/hvm/emulate.h +6 -0
+    /*
+     * MFNs behind temporary mappings in the write callback.  The length is
+     * arbitrary, and can be increased if writes longer than PAGE_SIZE are
+     * needed.
+     */
+    mfn_t mfn[2];

Mind being precise in the comment, saying "PAGE_SIZE+1"?

Quoted 4 lines

While that is strictly true, it is not the behaviour which the map() function takes. I don't think it is worth the overhead of fixing that boundary condition for one extra byte, at which point the documentation should match the implementation.

I don't understand your reply - with today's map() implementation, any PAGE_SIZE+1 range can be mapped, with the extremes being one mapping just one byte and the other mapping an entire page. But perhaps I'm simply not understanding what map() behavior you're thinking of.

Jan

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

A message that is not in the archive (sent off-list or not yet received): 594BA4A3?= =?UTF-8?Q?0200007800165AA5@prv=ef=bf=bdmh.provo.novell.com

A message that is not in the archive (sent off-list or not yet received): b9e9b637-0755-?= [email protected]

A message that is not in the archive (sent off-list or not yet received): 595A879202000078001680B7@prv-?= =?UTF-8?Q?mh.provo.novell.com

A message that is not in the archive (sent off-list or not yet received): 595A879202000078001680B7@prv-?= =?UTF-8?Q?mh.provo.novell.com

#30 lore raw

Re: [PATCH 6/6] x86/hvm: Implement hvmemul_write() using real mappings

Quoted 63 lines · Jan Beulich
Quoted 45 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 17:07, <[email protected]> wrote:
Quoted 32 lines · Jan Beulich
Quoted 32 lines
xen/arch/x86/hvm/emulate.c +32 -0
+    {
+        ASSERT_UNREACHABLE();
+        goto unhandleable;
+    }
+
+    do {
+        enum hvm_translation_result res;
+        struct page_info *page;
+        pagefault_info_t pfinfo;
+        p2m_type_t p2mt;
+
+        res = hvm_translate_get_page(curr, frame << PAGE_SHIFT, true, pfec,
+                                     &pfinfo, &page, NULL, &p2mt);
+
+        switch ( res )
+        {
+        case HVMTRANS_okay:
+            break;
+
+        case HVMTRANS_bad_linear_to_gfn:
+            x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
+            err = ERR_PTR(~(long)X86EMUL_EXCEPTION);
+            goto out;
+
+        case HVMTRANS_bad_gfn_to_mfn:
+            err = NULL;
+            goto out;
+
+        case HVMTRANS_gfn_paged_out:
+        case HVMTRANS_gfn_shared:
+            err = ERR_PTR(~(long)X86EMUL_RETRY);
+            goto out;
Quoted 8 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 574

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+
+        default:
+            goto unhandleable;
+        }
+
+        /* Error checking.  Confirm that the current slot is clean. */
+        ASSERT(mfn_x(*mfn) == 0);

Wouldn't this better be done first thing in the loop?

IMO its clearer to keep it next to the assignment, but yes, could in principle move to the top of the loop.

Quoted 7 lines
Quoted 1 line

And wouldn't the value better be INVALID_MFN?

The backing array is zeroed by hvm_emulate_init_once(), so relying on 0 here is more convenient.

Furthermore, INVALID_MFN is used lower down to poison unused slots, so initialising the whole array to INVALID_MFN reduces the effectiveness of the checks.

Well, further down I had implicitly asked whether some of the checks wouldn't better go away (as they can't be carried out with some of the redundant unmap inputs dropped).

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+        *mfn++ = _mfn(page_to_mfn(page));
+        frame++;
+
+        if ( p2m_is_discard_write(p2mt) )
+        {
+            err = ERR_PTR(~(long)X86EMUL_OKAY);
+            goto out;
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

If one page is discard-write and the other isn't, this will end up being wrong.

Straddled accesses are always a grey area, and discard-write is an extra special case which only exists inside Xen. Discard-write means that the guest knows that it shouldn't write there at all.

Is it the case that the guest knows? Iirc this type had been introduced for introspection tool use.

This is for Intel GVT-g (c/s 1c02cce0ed).

Introspection would go very wrong if selective writes had no effect.

Quoted 5 lines
Quoted 3 lines

Doing nothing (by logically extending the discard-write restriction over the entire region) is the least bad option here, IMO.

Okay, that's a reasonable argument. I'd suggest saying so explicitly in a comment, though.

I will add a comment.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_do_mmio_addr(paddr_t mmio_gpa, · line 617

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +2 -0
+static void hvmemul_unmap_linear_addr(
+    void *mapping, unsigned long linear, unsigned int bytes,

Both vunmap() and unmap_domain_page() take pointers to const, so please use const on the pointer here too.

Quoted 8 lines
Quoted 5 lines

The meaning of const void *p in C is "this function does not modify the content pointed to by p".

Both vunmap() and unmap_domain_page() mutate the content being pointed to, so should not take const pointers.

We've had this discussion before, and I continue to take a different position: When you free a memory block, you implicitly declare its contents undefined.

In C's view, the object explicitly becomes undefined, as its lifetime has come to an end, but this is a well defined operation.

Quoted 1 line

An undefined modification to undefined contents still yields undefined.

The precondition to this statement is false.

Before the unmap() call, the object and its contents are well defined.

By using a function with a const pointer, the contract of the function says the object doesn't change, and therefore, its content doesn't change.

Therefore, the object and its contents are still well defined after the call.

This final statement is obviously false, because we blew away the pagetables under the mapping. The contradiction exists because of the use of const pointer in the first place.

Quoted 10 lines

So no actual change.
Furthermore it is not a given that a freeing routine actually touches the handed memory block at all - that's an internal implementation detail of the allocator.

Plus - not having the parameters const means you can't allocate and initialize something in one go, and then store or pass around a pointer to it which is const-qualified to make clear the contents of that memory block aren't supposed to be further modified (until de-allocation).

I don't see this as a bad thing. You can't do it with malloc()/free().

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

+ struct hvm_emulate_ctxt *hvmemul_ctxt)

There upsides and downsides to requiring the caller to pass in the same values as to map(): You can do more correctness checking here, but you also risk the caller using the wrong values (perhaps because of a meanwhile updated local variable). While I don't outright object to this approach, personally I'd prefer minimal inputs here, and the code deriving everything from hvmemul_ctxt.

Quoted 8 lines
Quoted 3 lines

I'm not sure exactly how we might wish to extend this logic. Are we ever going to want more than one active mapping at once (perhaps rep movs emulation across two ram regions)?

I could imagine even more than two regions - from an abstract perspective it may be possible / helpful to have a mapping of each piece of memory a single instruction accesses, along the lines of minimal number of architecturally guaranteed TLB entries on ia64 in order to execute any possible x86 insn.

A further problem I've just realised is the use of multiple ->write() calls for a single instruction in x86_emulate(). The preceding writes obviously can't get backed out in the case of the second write taking a fault, which will cause the emulator to exhibit similar non-architectural behaviour as this patch is trying to fix.

Quoted 7 lines
Quoted 4 lines

The other reason is that in the release builds, even if we stored the pointer in hvmemul_ctxt, we still cant determine which unmapping function to use without linear and size.

I don't understand - all you pass on is "mapping". And whether to unmap a single or two pages could be told from hvmemul_ctxt->mfn[1] (not) being INVALID_MFN.

That isn't safe outside of the debug builds.

You could in principle use mfn[1] != 0 in release builds, if it weren't for the fact that hvmemul_ctxt could be used for multiple ->write() calls. In the case of a straddled write followed by a non-straddled write, the second unmap() would evaluate incorrectly.

Quoted 18 lines
Quoted 14 lines
Quoted 7 lines
Quoted 7 lines
xen/include/asm-x86/hvm/emulate.h +0 -0
@@ -37,6 +37,13 @@ struct hvm_emulate_ctxt {
     unsigned long seg_reg_accessed;
     unsigned long seg_reg_dirty;
 
Quoted 7 lines

xen/include/asm-x86/hvm/emulate.h struct hvm_emulate_ctxt { · line 45

Quoted 6 lines
xen/include/asm-x86/hvm/emulate.h +6 -0
+    /*
+     * MFNs behind temporary mappings in the write callback.  The length is
+     * arbitrary, and can be increased if writes longer than PAGE_SIZE are
+     * needed.
+     */
+    mfn_t mfn[2];

Mind being precise in the comment, saying "PAGE_SIZE+1"?

Quoted 4 lines

While that is strictly true, it is not the behaviour which the map() function takes. I don't think it is worth the overhead of fixing that boundary condition for one extra byte, at which point the documentation should match the implementation.

Quoted 5 lines

I don't understand your reply - with today's map() implementation, any PAGE_SIZE+1 range can be mapped, with the extremes being one mapping just one byte and the other mapping an entire page. But perhaps I'm simply not understanding what map() behavior you're thinking of.

Oh so it can. I'd forgotten that I'd changed the implementation.

~Andrew

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

#31 lore raw

Re: [PATCH 6/6] x86/hvm: Implement hvmemul_write() using real mappings

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 19:24, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 17:07, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich

Review

Quoted 7 lines
(quoted diff) +7 -0
+        *mfn++ = _mfn(page_to_mfn(page));
+        frame++;
+
+        if ( p2m_is_discard_write(p2mt) )
+        {
+            err = ERR_PTR(~(long)X86EMUL_OKAY);
+            goto out;

If one page is discard-write and the other isn't, this will end up being wrong.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Straddled accesses are always a grey area, and discard-write is an extra special case which only exists inside Xen. Discard-write means that the guest knows that it shouldn't write there at all.

Is it the case that the guest knows? Iirc this type had been introduced for introspection tool use.

This is for Intel GVT-g (c/s 1c02cce0ed).

Oh, I've mixed this up with the hvmemul_write_discard() & Co, which ...

Quoted 1 line

Introspection would go very wrong if selective writes had no effect.

... are introspection specific, so I'm afraid you're not entirely right with this statement.

Quoted 7 lines
Quoted 5 lines

Plus - not having the parameters const means you can't allocate and initialize something in one go, and then store or pass around a pointer to it which is const-qualified to make clear the contents of that memory block aren't supposed to be further modified (until de-allocation).

I don't see this as a bad thing. You can't do it with malloc()/free().

And wrongly so, imo.

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

+ struct hvm_emulate_ctxt *hvmemul_ctxt)

There upsides and downsides to requiring the caller to pass in the same values as to map(): You can do more correctness checking here, but you also risk the caller using the wrong values (perhaps because of a meanwhile updated local variable). While I don't outright object to this approach, personally I'd prefer minimal inputs here, and the code deriving everything from hvmemul_ctxt.

Quoted 8 lines
Quoted 3 lines

I'm not sure exactly how we might wish to extend this logic. Are we ever going to want more than one active mapping at once (perhaps rep movs emulation across two ram regions)?

I could imagine even more than two regions - from an abstract perspective it may be possible / helpful to have a mapping of each piece of memory a single instruction accesses, along the lines of minimal number of architecturally guaranteed TLB entries on ia64 in order to execute any possible x86 insn.

Quoted 5 lines

A further problem I've just realised is the use of multiple ->write() calls for a single instruction in x86_emulate(). The preceding writes obviously can't get backed out in the case of the second write taking a fault, which will cause the emulator to exhibit similar non-architectural behaviour as this patch is trying to fix.

I'm not convinced hardware guarantees all cases where we use multiple writes to be carried out in one step. I'm relatively sure multiple pushes (for far calls) are being carried out one by one. Just that exception checks likely are being done once up front (albeit I'm less certain about #PF, whereas for all segment related things this would seem natural). Same for SGDT/SIDT, which I think are the only non-push multi-write insns.

And of course the same issue exists for multiple segment register writes.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

The other reason is that in the release builds, even if we stored the pointer in hvmemul_ctxt, we still cant determine which unmapping function to use without linear and size.

I don't understand - all you pass on is "mapping". And whether to unmap a single or two pages could be told from hvmemul_ctxt->mfn[1] (not) being INVALID_MFN.

Quoted 6 lines

That isn't safe outside of the debug builds.

You could in principle use mfn[1] != 0 in release builds, if it weren't for the fact that hvmemul_ctxt could be used for multiple ->write() calls. In the case of a straddled write followed by a non-straddled write, the second unmap() would evaluate incorrectly.

I'd of course imply unmap() to set ->mfn[] to INVALID_MFN.

Jan

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