Skip to contentSkip to conversation

Improve "Emulation failed" error message

PATCH Superseded Andrew Cooper 4 participants 21 messages Sep 26, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Improve "Emulation failed" error message

After wanting to improve this error for a long time, I have encountered two cases in the past 2 days where I have needed more information than it provided, so have finally gotten around to fixing it.

Patch 1 introduces a "print hex buffer" custom %p format (compatible with the Linux equivelent), while Patch 2 improves the error message.

Konrad: I am requesting a release ack for this as it is a non-feature change for the purpose of improving error messages. Neither of the patches are complicated (low risk of bugs/regressions), and if the worse comes to the worst, they can safely be reverted.

Andrew Cooper (2):
xen/vsprintf: Introduce %*ph extended format specifier for hex
buffers
x86/hvm: Improve "Emulation failed @" error messages

 docs/misc/printk-formats.txt      |    6 ++++++
 xen/arch/x86/hvm/emulate.c        |   41 +++++++++++++++++++++++++++++--------
 xen/arch/x86/hvm/io.c             |   11 +---------
 xen/arch/x86/hvm/vmx/realmode.c   |    9 +-------
 xen/common/vsprintf.c             |   25 ++++++++++++++++++++++
 xen/include/asm-x86/hvm/emulate.h |    3 +++
 6 files changed, 68 insertions(+), 27 deletions(-)
#1 lore raw

PATCH 1/2 xen/vsprintf: Introduce %*ph extended format specifier for hex buffers

This behaves in the same way as Linux. The 64 byte limit is arbitrary but long enough for practical purposes.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Tim Deegan <[email protected]>
CC Konrad Rzeszutek Wilk <[email protected]>
---
 docs/misc/printk-formats.txt |    6 ++++++
 xen/common/vsprintf.c        |   25 +++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
docs/misc/printk-formats.txt +6 -0
index 3b4323a..81b3651 100644
@@ -18,3 +18,9 @@ Symbol/Function pointers:
 
        %pv     Domain and vCPU ID from a 'struct vcpu *' (printed as
                "d<domid>v<vcpuid>")
+
+
+Raw buffer as hex string:
+
+       %*ph    Up to 64 characters, printed as "00 01 02 ... ff".  Buffer length
+               expected via the field_width paramter. i.e. printk("%*ph", 8, buffer);
xen/common/vsprintf.c +25 -0
index e68886a..4a7da1c 100644
@@ -272,6 +272,31 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
     /* Custom %p suffixes. See XEN_ROOT/docs/misc/printk-formats.txt */
     switch ( fmt[1] )
     {
+    case 'h': /* Raw buffer as hex string. */
+    {
+        /*
+         * User expected to provide an explicit count using %*.  Bound between
+         * 0 and 64 bytes, defaulting to 0.
+         */
+        unsigned i, nr_bytes =
+            ((field_width < 1) || (field_width > 64)) ? 0 : field_width;
+        const uint8_t *hex_buffer = arg;
+
+        /* Consumed 'h' from the format string. */
+        ++*fmt_ptr;
+
+        for ( i = 0; i < nr_bytes; ++i )
+        {
+            /* Each byte: 2 chars, 0-padded, base 16, no hex prefix. */
+            str = number(str, end, hex_buffer[i], 16, 2, -1, ZEROPAD);
+            if ( str < end )
+                *str = ' ';
+            ++str;
+        }
+
+        return str;
+    }
+
     case 's': /* Symbol name with offset and size (iff offset != 0) */
     case 'S': /* Symbol name unconditionally with offset and size */
     {
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH for-4.5 1/2] xen/vsprintf: Introduce %*ph extended format specifier for hex buffers

docs/misc/printk-formats.txt Symbol/Function pointers: · line 26

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 12:10, <[email protected]> wrote:
docs/misc/printk-formats.txt +6 -0
@@ -18,3 +18,9 @@ Symbol/Function pointers:
 
        %pv     Domain and vCPU ID from a 'struct vcpu *' (printed as
                "d<domid>v<vcpuid>")
+
+
+Raw buffer as hex string:
+
+       %*ph    Up to 64 characters, printed as "00 01 02 ... ff".  Buffer length
+               expected via the field_width paramter. i.e. printk("%*ph", 8, buffer);

Let's keep this list sorted alphabetically please.

Also Linux supports an optional suffix (any of C, D, or N).

Quoted 4 lines
xen/common/vsprintf.c +0 -0
@@ -272,6 +272,31 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
     /* Custom %p suffixes. See XEN_ROOT/docs/misc/printk-formats.txt */

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 282

Quoted 10 lines
xen/common/vsprintf.c +8 -0
     switch ( fmt[1] )
     {
+    case 'h': /* Raw buffer as hex string. */
+    {
+        /*
+         * User expected to provide an explicit count using %*.  Bound between
+         * 0 and 64 bytes, defaulting to 0.
+         */
+        unsigned i, nr_bytes =
+            ((field_width < 1) || (field_width > 64)) ? 0 : field_width;

Producing no output for too small a field width makes sense, but why not print 64 bytes if more were requested?

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 294

Quoted 12 lines
xen/common/vsprintf.c +12 -0
+        const uint8_t *hex_buffer = arg;
+
+        /* Consumed 'h' from the format string. */
+        ++*fmt_ptr;
+
+        for ( i = 0; i < nr_bytes; ++i )
+        {
+            /* Each byte: 2 chars, 0-padded, base 16, no hex prefix. */
+            str = number(str, end, hex_buffer[i], 16, 2, -1, ZEROPAD);
+            if ( str < end )
+                *str = ' ';
+            ++str;

Could you suppress the trailing blank this produces, just like Linux does?

Jan

#3 lore raw

Re: [PATCH for-4.5 1/2] xen/vsprintf: Introduce %*ph extended format specifier for hex buffers

Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 12:10, <[email protected]> wrote:
docs/misc/printk-formats.txt +0 -0
@@ -18,3 +18,9 @@ Symbol/Function pointers:
 
Quoted 9 lines

docs/misc/printk-formats.txt Symbol/Function pointers: · line 26

Quoted 8 lines
docs/misc/printk-formats.txt +6 -0
        %pv     Domain and vCPU ID from a 'struct vcpu *' (printed as
                "d<domid>v<vcpuid>")
+
+
+Raw buffer as hex string:
+
+       %*ph    Up to 64 characters, printed as "00 01 02 ... ff".  Buffer length
+               expected via the field_width paramter. i.e. printk("%*ph", 8, buffer);

Let's keep this list sorted alphabetically please.

Ok, but then the "Symbol/Function pointers:" paragraph marker should be dropped.

I am happy with doing either.

Quoted 2 lines

Also Linux supports an optional suffix (any of C, D, or N).

None of those looked particularly useful IMO. They would be trivial to add should a need arise.

Quoted 17 lines
Quoted 5 lines
xen/common/vsprintf.c +0 -0
@@ -272,6 +272,31 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
     /* Custom %p suffixes. See XEN_ROOT/docs/misc/printk-formats.txt */

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 282

Quoted 10 lines
xen/common/vsprintf.c +8 -0
     switch ( fmt[1] )
     {
+    case 'h': /* Raw buffer as hex string. */
+    {
+        /*
+         * User expected to provide an explicit count using %*.  Bound between
+         * 0 and 64 bytes, defaulting to 0.
+         */
+        unsigned i, nr_bytes =
+            ((field_width < 1) || (field_width > 64)) ? 0 : field_width;

Producing no output for too small a field width makes sense, but why not print 64 bytes if more were requested?

64 is arbitrary (taken from the Linux statement to the same effect). Even with an upper bound of 64, the caller should be using something shorter and putting in newlines.

Quoted 15 lines

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 294

Quoted 13 lines
xen/common/vsprintf.c +12 -0
+        const uint8_t *hex_buffer = arg;
+
+        /* Consumed 'h' from the format string. */
+        ++*fmt_ptr;
+
+        for ( i = 0; i < nr_bytes; ++i )
+        {
+            /* Each byte: 2 chars, 0-padded, base 16, no hex prefix. */
+            str = number(str, end, hex_buffer[i], 16, 2, -1, ZEROPAD);
+            if ( str < end )
+                *str = ' ';
+            ++str;

Could you suppress the trailing blank this produces, just like Linux does?

I will see what I can do.

~Andrew

#4 lore raw

Re: [PATCH for-4.5 1/2] xen/vsprintf: Introduce %*ph extended format specifier for hex buffers

Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 14:16, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 12:10, <[email protected]> wrote:
docs/misc/printk-formats.txt +0 -0
@@ -18,3 +18,9 @@ Symbol/Function pointers:
 
Quoted 7 lines

docs/misc/printk-formats.txt Symbol/Function pointers:

Quoted 7 lines
docs/misc/printk-formats.txt +5 -0
        %pv     Domain and vCPU ID from a 'struct vcpu *' (printed as
                "d<domid>v<vcpuid>")
+
+
+Raw buffer as hex string:
+
+       %*ph    Up to 64 characters, printed as "00 01 02 ... ff".  Buffer 

length

Quoted 8 lines
Quoted 1 line

docs/misc/printk-formats.txt Symbol/Function pointers: · line 26

Quoted 1 line
docs/misc/printk-formats.txt +1 -0
+               expected via the field_width paramter. i.e. printk("%*ph", 

8, buffer);

Quoted 1 line

Let's keep this list sorted alphabetically please.

Ok, but then the "Symbol/Function pointers:" paragraph marker should be dropped.

I am happy with doing either.

Actually it looks like I should have added a header when adding %pv, so maybe that's what wants to be corrected? Sorting by formatting character still would see desirable to me, as would keeping the headings.

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
xen/common/vsprintf.c +2 -0
@@ -272,6 +272,31 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
     /* Custom %p suffixes. See XEN_ROOT/docs/misc/printk-formats.txt */
     switch ( fmt[1] )
     {
+    case 'h': /* Raw buffer as hex string. */
+    {
Quoted 8 lines

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 282

Quoted 6 lines
xen/common/vsprintf.c +6 -0
+        /*
+         * User expected to provide an explicit count using %*.  Bound between
+         * 0 and 64 bytes, defaulting to 0.
+         */
+        unsigned i, nr_bytes =
+            ((field_width < 1) || (field_width > 64)) ? 0 : field_width;

Producing no output for too small a field width makes sense, but why not print 64 bytes if more were requested?

Quoted 3 lines

64 is arbitrary (taken from the Linux statement to the same effect). Even with an upper bound of 64, the caller should be using something shorter and putting in newlines.

I'd be fine with you limiting it to a lower value; I just find it odd to zap a value exceeding the boundary to zero rather than to the upper bound.

Jan

#5 lore raw

Re: [PATCH for-4.5 1/2] xen/vsprintf: Introduce %*ph extended format specifier for hex buffers

Quoted 18 lines · Jan Beulich
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 14:16, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 12:10, <[email protected]> wrote:
docs/misc/printk-formats.txt +0 -0
@@ -18,3 +18,9 @@ Symbol/Function pointers:
 
Quoted 7 lines

docs/misc/printk-formats.txt Symbol/Function pointers:

Quoted 7 lines
docs/misc/printk-formats.txt +5 -0
        %pv     Domain and vCPU ID from a 'struct vcpu *' (printed as
                "d<domid>v<vcpuid>")
+
+
+Raw buffer as hex string:
+
+       %*ph    Up to 64 characters, printed as "00 01 02 ... ff".  Buffer 

length

Quoted 1 line

docs/misc/printk-formats.txt Symbol/Function pointers: · line 26

Quoted 1 line
docs/misc/printk-formats.txt +1 -0
+               expected via the field_width paramter. i.e. printk("%*ph", 

8, buffer);

Quoted 1 line

Let's keep this list sorted alphabetically please.

Quoted 8 lines
Quoted 4 lines

Ok, but then the "Symbol/Function pointers:" paragraph marker should be dropped.

I am happy with doing either.

Actually it looks like I should have added a header when adding %pv, so maybe that's what wants to be corrected? Sorting by formatting character still would see desirable to me, as would keeping the headings.

I will introduce the heading for %pv

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 5 lines
xen/common/vsprintf.c +0 -0
@@ -272,6 +272,31 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
     /* Custom %p suffixes. See XEN_ROOT/docs/misc/printk-formats.txt */

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 282

Quoted 10 lines
xen/common/vsprintf.c +8 -0
     switch ( fmt[1] )
     {
+    case 'h': /* Raw buffer as hex string. */
+    {
+        /*
+         * User expected to provide an explicit count using %*.  Bound between
+         * 0 and 64 bytes, defaulting to 0.
+         */
+        unsigned i, nr_bytes =
+            ((field_width < 1) || (field_width > 64)) ? 0 : field_width;

Producing no output for too small a field width makes sense, but why not print 64 bytes if more were requested?

Quoted 9 lines
Quoted 3 lines

64 is arbitrary (taken from the Linux statement to the same effect). Even with an upper bound of 64, the caller should be using something shorter and putting in newlines.

I'd be fine with you limiting it to a lower value; I just find it odd to zap a value exceeding the boundary to zero rather than to the upper bound.

Jan

Ok

~Andrew

#6 lore raw

Re: [PATCH for-4.5 1/2] xen/vsprintf: Introduce %*ph extended format specifier for hex buffers

In reply to Andrew Cooper
Quoted 43 lines · Andrew Cooper

This behaves in the same way as Linux. The 64 byte limit is arbitrary but long enough for practical purposes.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Tim Deegan <[email protected]>
CC Konrad Rzeszutek Wilk <[email protected]>
---
 docs/misc/printk-formats.txt |    6 ++++++
 xen/common/vsprintf.c        |   25 +++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
docs/misc/printk-formats.txt +6 -0
index 3b4323a..81b3651 100644
@@ -18,3 +18,9 @@ Symbol/Function pointers:
 
        %pv     Domain and vCPU ID from a 'struct vcpu *' (printed as
                "d<domid>v<vcpuid>")
+
+
+Raw buffer as hex string:
+
+       %*ph    Up to 64 characters, printed as "00 01 02 ... ff".  Buffer length
+               expected via the field_width paramter. i.e. printk("%*ph", 8, buffer);
xen/common/vsprintf.c +8 -0
index e68886a..4a7da1c 100644
@@ -272,6 +272,31 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
     /* Custom %p suffixes. See XEN_ROOT/docs/misc/printk-formats.txt */
     switch ( fmt[1] )
     {
+    case 'h': /* Raw buffer as hex string. */
+    {
+        /*
+         * User expected to provide an explicit count using %*.  Bound between
+         * 0 and 64 bytes, defaulting to 0.
+         */
+        unsigned i, nr_bytes =
+            ((field_width < 1) || (field_width > 64)) ? 0 : field_width;

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 292

Quoted 10 lines
xen/common/vsprintf.c +10 -0
+        const uint8_t *hex_buffer = arg;
+
+        /* Consumed 'h' from the format string. */
+        ++*fmt_ptr;
+
+        for ( i = 0; i < nr_bytes; ++i )
+        {
+            /* Each byte: 2 chars, 0-padded, base 16, no hex prefix. */
+            str = number(str, end, hex_buffer[i], 16, 2, -1, ZEROPAD);
+            if ( str < end )

(str < end && i + 1 < nr_bytes) to avoid a trailing space?

Otherwise, Reviewed-by: Tim Deegan <[email protected]>

Tim.

#7 lore raw

Re: [PATCH for-4.5 1/2] xen/vsprintf: Introduce %*ph extended format specifier for hex buffers

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 13:49, <[email protected]> wrote:

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 292

Quoted 5 lines · Andrew Cooper
xen/common/vsprintf.c +5 -0
+        for ( i = 0; i < nr_bytes; ++i )
+        {
+            /* Each byte: 2 chars, 0-padded, base 16, no hex prefix. */
+            str = number(str, end, hex_buffer[i], 16, 2, -1, ZEROPAD);
+            if ( str < end )

(str < end && i + 1 < nr_bytes) to avoid a trailing space?

Not exactly, as the increment of str must then also be suppressed.

Jan

#8 lore raw

PATCH 2/2 x86/hvm: Improve "Emulation failed @" error messages

In reply to Andrew Cooper

* Introduce hvm_dump_emulation_state() to be a common implementation rather than having the printk() open-coded slightly differently in 3 separate places.
* Identify the vcpu operating mode to allow for unambiguous decoding of the instruction bytes.
* Identify the actual number of bytes in the instruction buffer, and print exactly the number of bytes fetched. A valid instruction can be up to 15 bytes long, but may also be shorter than the current arbitrary 10 bytes.

A sample new error message looks like:

(d1) MMIO emulation failed: d1v0 64bit mode, 7 bytes @ 0008:ffff82d080102fea: 48 63 8d 40 ff ff ff

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Tim Deegan <[email protected]>
CC Konrad Rzeszutek Wilk <[email protected]>
CC Jun Nakajima <[email protected]>
CC Eddie Dong <[email protected]>
CC Kevin Tian <[email protected]>

---

Ideally, hvm_dump_emulation_state() would have a const context pointer, but hvmemul_get_seg_reg() currently needs to set an accessed bit. We should probably differencitate between get_seg_reg() for the purposes of emulating, and get_seg_reg() for the purpose of debug printing.

---
 xen/arch/x86/hvm/emulate.c        |   41 +++++++++++++++++++++++++++++--------
 xen/arch/x86/hvm/io.c             |   11 +---------
 xen/arch/x86/hvm/vmx/realmode.c   |    9 +-------
 xen/include/asm-x86/hvm/emulate.h |    3 +++
 4 files changed, 37 insertions(+), 27 deletions(-)
xen/arch/x86/hvm/emulate.c +32 -9
index 6ab06e0..d42d251 100644
@@ -1390,15 +1390,7 @@ void hvm_mem_event_emulate_one(bool_t nowrite, unsigned int trapnr,
          */
         return;
     case X86EMUL_UNHANDLEABLE:
-        gdprintk(XENLOG_DEBUG, "Emulation failed @ %04x:%lx: "
-               "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
-               hvmemul_get_seg_reg(x86_seg_cs, &ctx)->sel,
-               ctx.insn_buf_eip,
-               ctx.insn_buf[0], ctx.insn_buf[1],
-               ctx.insn_buf[2], ctx.insn_buf[3],
-               ctx.insn_buf[4], ctx.insn_buf[5],
-               ctx.insn_buf[6], ctx.insn_buf[7],
-               ctx.insn_buf[8], ctx.insn_buf[9]);
+        hvm_dump_emulation_state(XENLOG_G_DEBUG "Mem event", &ctx);
         hvm_inject_hw_exception(trapnr, errcode);
         break;
     case X86EMUL_EXCEPTION:
@@ -1449,6 +1441,37 @@ struct segment_register *hvmemul_get_seg_reg(
     return &hvmemul_ctxt->seg_reg[seg];
 }
 
+static const char *guest_x86_mode_to_str(int mode)
+{
+    switch ( mode )
+    {
+    case 0:
+        return "Real";
+    case 1:
+        return "v8086";
+    case 2:
+    case 4:
+        return "32bit";
+    case 8:
+        return "64bit";
+    default:
+        return "Unknown";
+    }
+}
+
+void hvm_dump_emulation_state(const char *prefix,
+                              struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    struct vcpu *curr = current;
+    const char *mode_str = guest_x86_mode_to_str(hvm_guest_x86_mode(curr));
+    struct segment_register *cs = hvmemul_get_seg_reg(x86_seg_cs, hvmemul_ctxt);
+
+    printk("%s emulation failed: %pv %s mode, %u bytes @ %04x:%lx: %*ph\n",
+           prefix, curr, mode_str, hvmemul_ctxt->insn_buf_bytes,
+           cs->sel, hvmemul_ctxt->insn_buf_eip,
+           hvmemul_ctxt->insn_buf_bytes, hvmemul_ctxt->insn_buf);
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/x86/hvm/io.c +1 -10
index 9f565d6..acbb69b 100644
@@ -100,16 +100,7 @@ int handle_mmio(void)
     switch ( rc )
     {
     case X86EMUL_UNHANDLEABLE:
-        gdprintk(XENLOG_WARNING,
-                 "MMIO emulation failed @ %04x:%lx: "
-                 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
-                 hvmemul_get_seg_reg(x86_seg_cs, &ctxt)->sel,
-                 ctxt.insn_buf_eip,
-                 ctxt.insn_buf[0], ctxt.insn_buf[1],
-                 ctxt.insn_buf[2], ctxt.insn_buf[3],
-                 ctxt.insn_buf[4], ctxt.insn_buf[5],
-                 ctxt.insn_buf[6], ctxt.insn_buf[7],
-                 ctxt.insn_buf[8], ctxt.insn_buf[9]);
+        hvm_dump_emulation_state(XENLOG_G_WARNING "MMIO", &ctxt);
         return 0;
     case X86EMUL_EXCEPTION:
         if ( ctxt.exn_pending )
xen/arch/x86/hvm/vmx/realmode.c +1 -8
index 45066b2..80d1543 100644
@@ -157,14 +157,7 @@ static void realmode_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt)
     return;
 
  fail:
-    gdprintk(XENLOG_ERR,
-             "Real-mode emulation failed @ %04x:%08lx: "
-             "%02x %02x %02x %02x %02x %02x\n",
-             hvmemul_get_seg_reg(x86_seg_cs, hvmemul_ctxt)->sel,
-             hvmemul_ctxt->insn_buf_eip,
-             hvmemul_ctxt->insn_buf[0], hvmemul_ctxt->insn_buf[1],
-             hvmemul_ctxt->insn_buf[2], hvmemul_ctxt->insn_buf[3],
-             hvmemul_ctxt->insn_buf[4], hvmemul_ctxt->insn_buf[5]);
+    hvm_dump_emulation_state(XENLOG_G_ERR "Real-mode", hvmemul_ctxt);
     domain_crash(curr->domain);
 }
 
xen/include/asm-x86/hvm/emulate.h +3 -0
index efff97e..a510123 100644
@@ -55,4 +55,7 @@ int hvmemul_do_pio(
     unsigned long port, unsigned long *reps, int size,
     paddr_t ram_gpa, int dir, int df, void *p_data);
 
+void hvm_dump_emulation_state(const char *situation,
+                              struct hvm_emulate_ctxt *hvmemul_ctxt);
+
 #endif /* __ASM_X86_HVM_EMULATE_H__ */
Signature · 1 line
1.7.10.4
#9 lore raw

Re: [PATCH for-4.5 2/2] x86/hvm: Improve "Emulation failed @" error messages

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 12:10, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +0 -0
@@ -1449,6 +1441,37 @@ struct segment_register *hvmemul_get_seg_reg(
     return &hvmemul_ctxt->seg_reg[seg];
 }

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1452

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +9 -0
 
+static const char *guest_x86_mode_to_str(int mode)
+{
+    switch ( mode )
+    {
+    case 0:
+        return "Real";
+    case 1:
+        return "v8086";
+    case 2:

return "16bit";

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +5 -0
+    case 4:
+        return "32bit";
+    case 8:
+        return "64bit";
+    default:

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1467

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+        return "Unknown";
+    }
+}
+
+void hvm_dump_emulation_state(const char *prefix,
+                              struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    struct vcpu *curr = current;
+    const char *mode_str = guest_x86_mode_to_str(hvm_guest_x86_mode(curr));
+    struct segment_register *cs = hvmemul_get_seg_reg(x86_seg_cs, hvmemul_ctxt);

Long line. And perhaps add "const"?

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1470

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +3 -0
+
+    printk("%s emulation failed: %pv %s mode, %u bytes @ %04x:%lx: %*ph\n",
+           prefix, curr, mode_str, hvmemul_ctxt->insn_buf_bytes,

Do you really need to print the byte count as a number when the new formatting will suitably limit output anyway?

Jan

#10 lore raw

Re: [PATCH for-4.5 2/2] x86/hvm: Improve "Emulation failed @" error messages

Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 12:10, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +0 -0
@@ -1449,6 +1441,37 @@ struct segment_register *hvmemul_get_seg_reg(
     return &hvmemul_ctxt->seg_reg[seg];
 }
 
Quoted 10 lines

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1452

Quoted 9 lines
xen/arch/x86/hvm/emulate.c +9 -0
+static const char *guest_x86_mode_to_str(int mode)
+{
+    switch ( mode )
+    {
+    case 0:
+        return "Real";
+    case 1:
+        return "v8086";
+    case 2:

return "16bit";

case 2 is 32bit mode code in a 16bit segment. Therefore, 32bit is still the correct text when aiding decode of the instruction.

What I want to avoid is the confusing statement of "16bit mode" which is easily confused as "Real mode" and a set of bytes which should be decoded as 32bit instructions.

Quoted 17 lines
Quoted 6 lines
xen/arch/x86/hvm/emulate.c +5 -0
+    case 4:
+        return "32bit";
+    case 8:
+        return "64bit";
+    default:

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1467

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+        return "Unknown";
+    }
+}
+
+void hvm_dump_emulation_state(const char *prefix,
+                              struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    struct vcpu *curr = current;
+    const char *mode_str = guest_x86_mode_to_str(hvm_guest_x86_mode(curr));
+    struct segment_register *cs = hvmemul_get_seg_reg(x86_seg_cs, hvmemul_ctxt);

Long line. And perhaps add "const"?

Ok

Quoted 6 lines

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1470

Quoted 4 lines
xen/arch/x86/hvm/emulate.c +3 -0
+
+    printk("%s emulation failed: %pv %s mode, %u bytes @ %04x:%lx: %*ph\n",
+           prefix, curr, mode_str, hvmemul_ctxt->insn_buf_bytes,

Do you really need to print the byte count as a number when the new formatting will suitably limit output anyway?

I considered that, but thought that "@ xxxx:xxxx:\n" might be a little obscure. On the other hand, it might be ok. I am happy dropping the "%u bytes" if that is considered ok.

~Andrew

#11 lore raw

Re: [PATCH for-4.5 2/2] x86/hvm: Improve "Emulation failed @" error messages

Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 14:04, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 12:10, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +0 -0
@@ -1449,6 +1441,37 @@ struct segment_register *hvmemul_get_seg_reg(
     return &hvmemul_ctxt->seg_reg[seg];
 }
 
Quoted 10 lines

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1452

Quoted 9 lines
xen/arch/x86/hvm/emulate.c +9 -0
+static const char *guest_x86_mode_to_str(int mode)
+{
+    switch ( mode )
+    {
+    case 0:
+        return "Real";
+    case 1:
+        return "v8086";
+    case 2:

return "16bit";

Quoted 2 lines

case 2 is 32bit mode code in a 16bit segment. Therefore, 32bit is still the correct text when aiding decode of the instruction.

It's specifically not: Operand and address size (and respective prefixes) have different meaning. You really don't care about the mode the CPU as a whole is in, but the kind of instructions it executes.

Quoted 3 lines

What I want to avoid is the confusing statement of "16bit mode" which is easily confused as "Real mode" and a set of bytes which should be decoded as 32bit instructions.

But instructions in a 16-bit segment should be decoded as 16-bit instructions, not 32-bit ones. Yes, OS/2 and 16-bit Windows are long gone, but this hasn't changed.

Quoted 8 lines
Quoted 4 lines

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1470

Quoted 2 lines
xen/arch/x86/hvm/emulate.c +2 -0
+    printk("%s emulation failed: %pv %s mode, %u bytes @ %04x:%lx: %*ph\n",
+           prefix, curr, mode_str, hvmemul_ctxt->insn_buf_bytes,

Do you really need to print the byte count as a number when the new formatting will suitably limit output anyway?

I considered that, but thought that "@ xxxx:xxxx:\n" might be a little obscure. On the other hand, it might be ok. I am happy dropping the "%u bytes" if that is considered ok.

Just make it "%04x:%08lx -> %*ph"? (Intentionally not using %lx as you did - I'd really dislike seeing addresses like 0000:12, while I'd be much less concerned for digit counts between 8 and 16 to vary.)

Jan

#12 lore raw

Re: [PATCH for-4.5 2/2] x86/hvm: Improve "Emulation failed @" error messages

Quoted 24 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 14:04, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 12:10, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +0 -0
@@ -1449,6 +1441,37 @@ struct segment_register *hvmemul_get_seg_reg(
     return &hvmemul_ctxt->seg_reg[seg];
 }
 
Quoted 10 lines

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1452

Quoted 9 lines
xen/arch/x86/hvm/emulate.c +9 -0
+static const char *guest_x86_mode_to_str(int mode)
+{
+    switch ( mode )
+    {
+    case 0:
+        return "Real";
+    case 1:
+        return "v8086";
+    case 2:

return "16bit";

Quoted 2 lines

case 2 is 32bit mode code in a 16bit segment. Therefore, 32bit is still the correct text when aiding decode of the instruction.

It's specifically not: Operand and address size (and respective prefixes) have different meaning. You really don't care about the mode the CPU as a whole is in, but the kind of instructions it executes.

Quoted 6 lines
Quoted 3 lines

What I want to avoid is the confusing statement of "16bit mode" which is easily confused as "Real mode" and a set of bytes which should be decoded as 32bit instructions.

But instructions in a 16-bit segment should be decoded as 16-bit instructions, not 32-bit ones. Yes, OS/2 and 16-bit Windows are long gone, but this hasn't changed.

I am indeed getting confused.

Quoted 11 lines
Quoted 8 lines
Quoted 5 lines

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1470

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +2 -0
+    printk("%s emulation failed: %pv %s mode, %u bytes @ %04x:%lx: %*ph\n",
+           prefix, curr, mode_str, hvmemul_ctxt->insn_buf_bytes,

Do you really need to print the byte count as a number when the new formatting will suitably limit output anyway?

I considered that, but thought that "@ xxxx:xxxx:\n" might be a little obscure. On the other hand, it might be ok. I am happy dropping the "%u bytes" if that is considered ok.

Just make it "%04x:%08lx -> %*ph"? (Intentionally not using %lx as you did - I'd really dislike seeing addresses like 0000:12, while I'd be much less concerned for digit counts between 8 and 16 to vary.)

Looks better - I shall go with that.

~Andrew

#13 lore raw

Re: [PATCH for-4.5 2/2] x86/hvm: Improve "Emulation failed @" error messages

In reply to Andrew Cooper

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1452

Quoted 9 lines · Andrew Cooper
xen/arch/x86/hvm/emulate.c +9 -0
+static const char *guest_x86_mode_to_str(int mode)
+{
+    switch ( mode )
+    {
+    case 0:
+        return "Real";
+    case 1:
+        return "v8086";
+    case 2:

return "16bit"?

Otherwise, Reviewed-by: Tim Deegan <[email protected]>

While you're respinning, could you also shorten all of these strings (e.g. to real/vm86/16b/32b/64b), and trim the rest of the line, something like:

(d1) MMIO emulation failed: d1v0 64b @ 0008:ffff82d080102fea: 48 63 8d 40 ff ff ff

You can keep the reviewed-by regardless of whatever cosmetic changes.

Cheers,

Tim.

Quoted 6 lines
xen/arch/x86/hvm/emulate.c +6 -0
+    case 4:
+        return "32bit";
+    case 8:
+        return "64bit";
+    default:
+        return "Unknown";
#14 lore raw

Re: [PATCH for-4.5 2/2] x86/hvm: Improve "Emulation failed @" error messages

Quoted 18 lines · Tim Deegan

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1452

Quoted 9 lines · Andrew Cooper
xen/arch/x86/hvm/emulate.c +9 -0
+static const char *guest_x86_mode_to_str(int mode)
+{
+    switch ( mode )
+    {
+    case 0:
+        return "Real";
+    case 1:
+        return "v8086";
+    case 2:

return "16bit"?

Otherwise, Reviewed-by: Tim Deegan <[email protected]>

While you're respinning, could you also shorten all of these strings (e.g. to real/vm86/16b/32b/64b), and trim the rest of the line, something like:

Quoted 7 lines

(d1) MMIO emulation failed: d1v0 64b @ 0008:ffff82d080102fea: 48 63 8d 40 ff ff ff

You can keep the reviewed-by regardless of whatever cosmetic changes.

Cheers,

Tim.

As identified in the other thread, "16bit" is misleading as the instruction bytes are actually 32bit code in a 16bit segment.

I am not sure what the best solution here is. Perhaps we can trust anyone capable of interpreting this error to know that "16b" != "Real" or "v86" when it comes to decoding the instruction.

~Andrew

#15 lore raw

Re: [PATCH for-4.5 2/2] x86/hvm: Improve "Emulation failed @" error messages

Quoted 25 lines · Andrew Cooper
Quoted 24 lines · Tim Deegan

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1452

Quoted 9 lines · Andrew Cooper
xen/arch/x86/hvm/emulate.c +9 -0
+static const char *guest_x86_mode_to_str(int mode)
+{
+    switch ( mode )
+    {
+    case 0:
+        return "Real";
+    case 1:
+        return "v8086";
+    case 2:

return "16bit"?

Otherwise, Reviewed-by: Tim Deegan <[email protected]>

While you're respinning, could you also shorten all of these strings (e.g. to real/vm86/16b/32b/64b), and trim the rest of the line, something like:

(d1) MMIO emulation failed: d1v0 64b @ 0008:ffff82d080102fea: 48 63 8d 40 ff ff ff

You can keep the reviewed-by regardless of whatever cosmetic changes.

Cheers,

Quoted 8 lines
Quoted 1 line

Tim.

As identified in the other thread, "16bit" is misleading as the instruction bytes are actually 32bit code in a 16bit segment.

I am not sure what the best solution here is. Perhaps we can trust anyone capable of interpreting this error to know that "16b" != "Real" or "v86" when it comes to decoding the instruction.

Hmm. I can see that 16bit is a bit misleading if you don't know/remember that vm86 and real mode would be reported as such. OTOH that is infomration that's needed for decoding -- the instruction will have 16bit operands and addresses even though it uses 32bit registers and protected segments.

Maybe we should report it as '16bit protected' or similar?

Tim.

#16 lore raw

Re: [PATCH for-4.5 2/2] x86/hvm: Improve "Emulation failed @" error messages

Quoted 33 lines · Tim Deegan
Quoted 25 lines · Andrew Cooper
Quoted 24 lines · Tim Deegan

xen/arch/x86/hvm/emulate.c struct segment_register *hvmemul_get_seg_reg( · line 1452

Quoted 9 lines · Andrew Cooper
xen/arch/x86/hvm/emulate.c +9 -0
+static const char *guest_x86_mode_to_str(int mode)
+{
+    switch ( mode )
+    {
+    case 0:
+        return "Real";
+    case 1:
+        return "v8086";
+    case 2:

return "16bit"?

Otherwise, Reviewed-by: Tim Deegan <[email protected]>

While you're respinning, could you also shorten all of these strings (e.g. to real/vm86/16b/32b/64b), and trim the rest of the line, something like:

(d1) MMIO emulation failed: d1v0 64b @ 0008:ffff82d080102fea: 48 63 8d 40 ff ff ff

You can keep the reviewed-by regardless of whatever cosmetic changes.

Cheers,

Quoted 7 lines
Quoted 1 line

Tim.

As identified in the other thread, "16bit" is misleading as the instruction bytes are actually 32bit code in a 16bit segment.

I am not sure what the best solution here is. Perhaps we can trust anyone capable of interpreting this error to know that "16b" != "Real" or "v86" when it comes to decoding the instruction.

Quoted 7 lines

Hmm. I can see that 16bit is a bit misleading if you don't know/remember that vm86 and real mode would be reported as such. OTOH that is infomration that's needed for decoding -- the instruction will have 16bit operands and addresses even though it uses 32bit registers and protected segments.

Maybe we should report it as '16bit protected' or similar?

How about following the convention at http://sandpile.org/x86/mode.htm ?

Currently, we can distinguish between RM16, VM16, (P/C)M{16,32} and PM64, which is good enough for decoding the bytes correctly.

Alternatively, we could extend {vmx,svm}_guest_x86_mode() to provide a rather more complete enum of processor modes and cover the other cases?

~Andrew

#17 lore raw

Re: [PATCH for-4.5 2/2] x86/hvm: Improve "Emulation failed @" error messages

Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 14:57, <[email protected]> wrote:
Quoted 7 lines · Tim Deegan
Quoted 6 lines · Andrew Cooper

As identified in the other thread, "16bit" is misleading as the instruction bytes are actually 32bit code in a 16bit segment.

I am not sure what the best solution here is. Perhaps we can trust anyone capable of interpreting this error to know that "16b" != "Real" or "v86" when it comes to decoding the instruction.

Quoted 7 lines

Hmm. I can see that 16bit is a bit misleading if you don't know/remember that vm86 and real mode would be reported as such. OTOH that is infomration that's needed for decoding -- the instruction will have 16bit operands and addresses even though it uses 32bit registers and protected segments.

Maybe we should report it as '16bit protected' or similar?

Quoted 7 lines

How about following the convention at http://sandpile.org/x86/mode.htm ?

Currently, we can distinguish between RM16, VM16, (P/C)M{16,32} and PM64, which is good enough for decoding the bytes correctly.

Alternatively, we could extend {vmx,svm}_guest_x86_mode() to provide a rather more complete enum of processor modes and cover the other cases?

None of this is relevant for instruction decoding. Even the 16-bit protected / real / vm86 mode distinction is relevant there, that's only useful as additional context.

Jan

#18 lore raw

Re: [PATCH for-4.5 2/2] x86/hvm: Improve "Emulation failed @" error messages

Quoted 18 lines · Jan Beulich
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 14:57, <[email protected]> wrote:
Quoted 7 lines · Tim Deegan
Quoted 6 lines · Andrew Cooper

As identified in the other thread, "16bit" is misleading as the instruction bytes are actually 32bit code in a 16bit segment.

I am not sure what the best solution here is. Perhaps we can trust anyone capable of interpreting this error to know that "16b" != "Real" or "v86" when it comes to decoding the instruction.

Quoted 7 lines

Hmm. I can see that 16bit is a bit misleading if you don't know/remember that vm86 and real mode would be reported as such. OTOH that is infomration that's needed for decoding -- the instruction will have 16bit operands and addresses even though it uses 32bit registers and protected segments.

Maybe we should report it as '16bit protected' or similar?

How about following the convention at http://sandpile.org/x86/mode.htm ?

Quoted 8 lines
Quoted 5 lines

Currently, we can distinguish between RM16, VM16, (P/C)M{16,32} and PM64, which is good enough for decoding the bytes correctly.

Alternatively, we could extend {vmx,svm}_guest_x86_mode() to provide a rather more complete enum of processor modes and cover the other cases?

None of this is relevant for instruction decoding. Even the 16-bit protected / real / vm86 mode distinction is relevant there, that's only useful as additional context.

I presume you mean "is irrelevant there" ?

The question is whether we want to provide more or less context.

For now, I will move to just 16/32/64bit as it is the simpler course of action. More context can easily be added in the future, as extending ???_guest_x86_mode() will be quite invasive and we are currently in a code freeze.

~Andrew

#19 lore raw

Re: [PATCH for-4.5 2/2] x86/hvm: Improve "Emulation failed @" error messages

Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 15:16, <[email protected]> wrote:
Quoted 21 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 26.09.14 at 14:57, <[email protected]> wrote:
Quoted 7 lines · Tim Deegan
Quoted 6 lines · Andrew Cooper

As identified in the other thread, "16bit" is misleading as the instruction bytes are actually 32bit code in a 16bit segment.

I am not sure what the best solution here is. Perhaps we can trust anyone capable of interpreting this error to know that "16b" != "Real" or "v86" when it comes to decoding the instruction.

Quoted 7 lines

Hmm. I can see that 16bit is a bit misleading if you don't know/remember that vm86 and real mode would be reported as such. OTOH that is infomration that's needed for decoding -- the instruction will have 16bit operands and addresses even though it uses 32bit registers and protected segments.

Maybe we should report it as '16bit protected' or similar?

How about following the convention at http://sandpile.org/x86/mode.htm ?

Currently, we can distinguish between RM16, VM16, (P/C)M{16,32} and PM64, which is good enough for decoding the bytes correctly.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Alternatively, we could extend {vmx,svm}_guest_x86_mode() to provide a rather more complete enum of processor modes and cover the other cases?

None of this is relevant for instruction decoding. Even the 16-bit protected / real / vm86 mode distinction is relevant there, that's only useful as additional context.

I presume you mean "is irrelevant there" ?

Oops, yes, of course.

Jan

#20 lore raw
In reply to Andrew Cooper
Quoted 11 lines · Andrew Cooper

After wanting to improve this error for a long time, I have encountered two cases in the past 2 days where I have needed more information than it provided, so have finally gotten around to fixing it.

Patch 1 introduces a "print hex buffer" custom %p format (compatible with the Linux equivelent), while Patch 2 improves the error message.

Konrad: I am requesting a release ack for this as it is a non-feature change for the purpose of improving error messages. Neither of the patches are complicated (low risk of bugs/regressions), and if the worse comes to the worst, they can safely be reverted.

Yes. I think these can go in, even if they are not considered bug-fixes but rather features to help developers/users in the field.

And conveniently I have an excellent test-case so I can test this and make sure it does not cause regressions.

Quoted 18 lines

Andrew Cooper (2):
xen/vsprintf: Introduce %*ph extended format specifier for hex
buffers
x86/hvm: Improve "Emulation failed @" error messages

 docs/misc/printk-formats.txt      |    6 ++++++
 xen/arch/x86/hvm/emulate.c        |   41 +++++++++++++++++++++++++++++--------
 xen/arch/x86/hvm/io.c             |   11 +---------
 xen/arch/x86/hvm/vmx/realmode.c   |    9 +-------
 xen/common/vsprintf.c             |   25 ++++++++++++++++++++++
 xen/include/asm-x86/hvm/emulate.h |    3 +++
 6 files changed, 68 insertions(+), 27 deletions(-)

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