Skip to contentSkip to conversation

Permit fault-less access to non-emulated MSRs

PATCH In review Boris Ostrovsky 5 participants 53 messages Jan 20, 2021 – Feb 23, 2021

lore
Share & tools
lore
#0 lore raw

v2 cover Permit fault-less access to non-emulated MSRs

Disallowing accesses to MSRs that are not explicitly handled (done by commit 84e848fd7a16 ("x86/hvm: disallow access to unknown MSRs")) caused regression for Solaris guests who access MSR_RAPL_POWER_UNIT assuming it's always there.

We can add special handling for this and other RAPL registers (and that's what should happen for pre-4.14 releases) but a more general solution is to provide an option to allow accesses to unhandled registers to proceed.

The new option is "ignore_msrs" and it can take three values
never: Issue a warning to the log and #GP to the guest. This is default.
silent: MSR reads return 0, MSR writes are ignored. No warnings to the log.
verbose: Similar to silent but a warning is written.

v2:
* pass ignore_msrs in msr_policy's value filed, not flags
* use 0x400002ff as unhandled MSR
* make sure MSR policy is only consulted for guest's MSR acceses

Boris Ostrovsky (4):
xl: Add support for ignore_msrs option
x86: Introduce MSR_UNHANDLED
x86: Allow non-faulting accesses to non-emulated MSRs if policy
permits this
tools/libs: Apply MSR policy to a guest

 docs/man/xl.cfg.5.pod.in               |  20 +++++-
 tools/include/xenctrl.h                |   2 +
 tools/libs/guest/Makefile              |   1 +
 tools/libs/guest/xg_msrs_x86.c         | 110 +++++++++++++++++++++++++++++++++
 tools/libs/light/libxl_dom.c           |   5 +-
 tools/libs/light/libxl_internal.h      |   2 +
 tools/libs/light/libxl_types.idl       |   7 +++
 tools/libs/light/libxl_x86.c           |   7 +++
 tools/xl/xl_parse.c                    |   7 +++
 xen/arch/x86/hvm/svm/svm.c             |  10 ++-
 xen/arch/x86/hvm/vmx/vmx.c             |  10 ++-
 xen/arch/x86/msr.c                     |  32 +++++++++-
 xen/arch/x86/pv/emul-priv-op.c         |  10 +--
 xen/arch/x86/x86_emulate/x86_emulate.h |   6 ++
 xen/include/asm-x86/msr.h              |   3 +
 xen/include/xen/lib/x86/msr.h          |  17 ++++-
 xen/lib/x86/msr.c                      |   2 +
 17 files changed, 229 insertions(+), 22 deletions(-)
 create mode 100644 tools/libs/guest/xg_msrs_x86.c
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/4 xl: Add support for ignore_msrs option

This option allows guest administrator specify what should happen when guest accesses an MSR which is not explicitly emulated by the hypervisor.

---
 docs/man/xl.cfg.5.pod.in         | 20 +++++++++++++++++++-
 tools/libs/light/libxl_types.idl |  7 +++++++
 tools/xl/xl_parse.c              |  7 +++++++
 3 files changed, 33 insertions(+), 1 deletion(-)
docs/man/xl.cfg.5.pod.in +19 -1
index c8e017f950de..96ce97c42cab 100644
@@ -2044,7 +2044,25 @@ Do not provide a VM generation ID.
 See also "Virtual Machine Generation ID" by Microsoft:
 L<https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/virtual-machine-generation-identifier>
 
-=back 
+=over
+
+=item B<ignore_msrs="STRING">
+
+Determine hypervisor behavior on accesses to MSRs that are not emulated by the hypervisor.
+
+=over 4
+
+=item B<never>
+
+Issue a warning to the log and #GP to the guest. This is default.
+
+=item B<silent>
+
+MSR reads return 0, MSR writes are ignored. No warnings to the log.
+
+=item B<verbose>
+
+Similar to B<silent> but a warning is written.
 
 =head3 Guest Virtual Time Controls
 
tools/libs/light/libxl_types.idl +7 -0
index 05324736b744..7b5fef771ee8 100644
@@ -477,6 +477,12 @@ libxl_tee_type = Enumeration("tee_type", [
     (1, "optee")
     ], init_val = "LIBXL_TEE_TYPE_NONE")
 
+libxl_ignore_msrs = Enumeration("ignore_msrs", [
+    (0, "never"),
+    (1, "silent"),
+    (2, "verbose"),
+    ], init_val = "LIBXL_IGNORE_MSRS_NEVER")
+
 libxl_rdm_reserve = Struct("rdm_reserve", [
     ("strategy",    libxl_rdm_reserve_strategy),
     ("policy",      libxl_rdm_reserve_policy),
@@ -559,6 +565,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
     ("apic",             libxl_defbool),
     ("dm_restrict",      libxl_defbool),
     ("tee",              libxl_tee_type),
+    ("ignore_msrs",      libxl_ignore_msrs),
     ("u", KeyedUnion(None, libxl_domain_type, "type",
                 [("hvm", Struct(None, [("firmware",         string),
                                        ("bios",             libxl_bios_type),
tools/xl/xl_parse.c +7 -0
index 4ebf39620ae7..942086c3f41d 100644
@@ -2732,6 +2732,13 @@ skip_usbdev:
         }
     }
 
+    if (!xlu_cfg_get_string(config, "ignore_msrs", &buf, 0)) {
+        if (libxl_ignore_msrs_from_string(buf, &b_info->ignore_msrs)) {
+           fprintf(stderr, "ERROR: invalid value \"%s\" for \"ignore_msrs\"\n", buf);
+           exit(1);
+        }
+    }
+
     parse_vkb_list(config, d_config);
 
     xlu_cfg_get_defbool(config, "xend_suspend_evtchn_compat",
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

Quoted 9 lines · Boris Ostrovsky

This option allows guest administrator specify what should happen when guest accesses an MSR which is not explicitly emulated by the hypervisor.

---
 docs/man/xl.cfg.5.pod.in         | 20 +++++++++++++++++++-
 tools/libs/light/libxl_types.idl |  7 +++++++
 tools/xl/xl_parse.c              |  7 +++++++
 3 files changed, 33 insertions(+), 1 deletion(-)

It is mainly missing a #define LIBXL_HAVE_XXX in libxl.h.

Other than that, this patch looks good to me. If you end up resending this series, please fix that issue.

If other patches are all reviewed, you can provide some text to be merged into this patch.

Wei.

#3 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

Quoted 13 lines · Wei Liu
Quoted 9 lines · Boris Ostrovsky

This option allows guest administrator specify what should happen when guest accesses an MSR which is not explicitly emulated by the hypervisor.

---
 docs/man/xl.cfg.5.pod.in         | 20 +++++++++++++++++++-
 tools/libs/light/libxl_types.idl |  7 +++++++
 tools/xl/xl_parse.c              |  7 +++++++
 3 files changed, 33 insertions(+), 1 deletion(-)

It is mainly missing a #define LIBXL_HAVE_XXX in libxl.h.

Quoted 8 lines

Other than that, this patch looks good to me. If you end up resending this series, please fix that issue.

If other patches are all reviewed, you can provide some text to be merged into this patch.

Wei.

Ah yes, I forgot about this.

tools/include/libxl.h +7 -0
index 3433c950f9aa..f249740daf3f 100644
@@ -1310,6 +1310,13 @@ void libxl_mac_copy(libxl_ctx *ctx, libxl_mac *dst, const libxl_mac *src);
  */
 #define LIBXL_HAVE_CREATEINFO_XEND_SUSPEND_EVTCHN_COMPAT
 
+/*
+ *  LIBXL_HAVE_IGNORE_MSRS indicates that the libxl_ignore_msrs field is
+ *  present in libxl_domain_build_info. This field describes hypervisor
+ *  behavior on guest accesses to unimplemented MSRs.
+ */
+#define LIBXL_HAVE_IGNORE_MSRS 1
+
 typedef char **libxl_string_list;
 void libxl_string_list_dispose(libxl_string_list *sl);
 int libxl_string_list_length(const libxl_string_list *sl);
#4 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

In reply to Boris Ostrovsky

Hi Boris,

Quoted 15 lines · Boris Ostrovsky

This option allows guest administrator specify what should happen when guest accesses an MSR which is not explicitly emulated by the hypervisor.

---
  docs/man/xl.cfg.5.pod.in         | 20 +++++++++++++++++++-
  tools/libs/light/libxl_types.idl |  7 +++++++
  tools/xl/xl_parse.c              |  7 +++++++
  3 files changed, 33 insertions(+), 1 deletion(-)
docs/man/xl.cfg.5.pod.in +0 -0
index c8e017f950de..96ce97c42cab 100644

docs/man/xl.cfg.5.pod.in Do not provide a VM generation ID. · line 2051

Quoted 9 lines
docs/man/xl.cfg.5.pod.in +5 -1
@@ -2044,7 +2044,25 @@ Do not provide a VM generation ID.
  See also "Virtual Machine Generation ID" by Microsoft:
  L<https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/virtual-machine-generation-identifier>
 
-=back
+=over
+
+=item B<ignore_msrs="STRING">
+
+Determine hypervisor behavior on accesses to MSRs that are not emulated by the hypervisor.

The description of the feature looks very x86 focus. Yet, it seems to be defined as a generic one.

Could you clarify whether this is intended to be re-usable by other architectures?

Quoted 51 lines
docs/man/xl.cfg.5.pod.in +14 -0
+
+=over 4
+
+=item B<never>
+
+Issue a warning to the log and #GP to the guest. This is default.
+
+=item B<silent>
+
+MSR reads return 0, MSR writes are ignored. No warnings to the log.
+
+=item B<verbose>
+
+Similar to B<silent> but a warning is written.
 
  =head3 Guest Virtual Time Controls
 
tools/libs/light/libxl_types.idl +7 -0
index 05324736b744..7b5fef771ee8 100644
@@ -477,6 +477,12 @@ libxl_tee_type = Enumeration("tee_type", [
      (1, "optee")
      ], init_val = "LIBXL_TEE_TYPE_NONE")
 
+libxl_ignore_msrs = Enumeration("ignore_msrs", [
+    (0, "never"),
+    (1, "silent"),
+    (2, "verbose"),
+    ], init_val = "LIBXL_IGNORE_MSRS_NEVER")
+
  libxl_rdm_reserve = Struct("rdm_reserve", [
      ("strategy",    libxl_rdm_reserve_strategy),
      ("policy",      libxl_rdm_reserve_policy),
@@ -559,6 +565,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
      ("apic",             libxl_defbool),
      ("dm_restrict",      libxl_defbool),
      ("tee",              libxl_tee_type),
+    ("ignore_msrs",      libxl_ignore_msrs),
      ("u", KeyedUnion(None, libxl_domain_type, "type",
                  [("hvm", Struct(None, [("firmware",         string),
                                         ("bios",             libxl_bios_type),
tools/xl/xl_parse.c +0 -0
index 4ebf39620ae7..942086c3f41d 100644
@@ -2732,6 +2732,13 @@ skip_usbdev:
          }
      }
 

tools/xl/xl_parse.c skip_usbdev: · line 2744

Quoted 10 lines
tools/xl/xl_parse.c +7 -0
+    if (!xlu_cfg_get_string(config, "ignore_msrs", &buf, 0)) {
+        if (libxl_ignore_msrs_from_string(buf, &b_info->ignore_msrs)) {
+           fprintf(stderr, "ERROR: invalid value \"%s\" for \"ignore_msrs\"\n", buf);
+           exit(1);
+        }
+    }
+
      parse_vkb_list(config, d_config);
 
      xlu_cfg_get_defbool(config, "xend_suspend_evtchn_compat",

Cheers,

Signature · 1 line
Julien Grall
#5 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

Quoted 29 lines · Julien Grall

Hi Boris,

Quoted 19 lines · Boris Ostrovsky

This option allows guest administrator specify what should happen when guest accesses an MSR which is not explicitly emulated by the hypervisor.

---
  docs/man/xl.cfg.5.pod.in         | 20 +++++++++++++++++++- tools/libs/light/libxl_types.idl |  7 +++++++ tools/xl/xl_parse.c              |  7 +++++++

  3 files changed, 33 insertions(+), 1 deletion(-)
docs/man/xl.cfg.5.pod.in +0 -0
index c8e017f950de..96ce97c42cab 100644
@@ -2044,7 +2044,25 @@ Do not provide a VM generation ID.

  See also "Virtual Machine Generation ID" by Microsoft:
  L<https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/virtual-machine-generation-identifier> -=back
+=over

docs/man/xl.cfg.5.pod.in Do not provide a VM generation ID. · line 2051

Quoted 4 lines
docs/man/xl.cfg.5.pod.in +4 -0
+
+=item B<ignore_msrs="STRING">
+
+Determine hypervisor behavior on accesses to MSRs that are not emulated by the hypervisor.

The description of the feature looks very x86 focus. Yet, it seems to be defined as a generic one.

Quoted 1 line

Could you clarify whether this is intended to be re-usable by other architectures?

x86 only. I'll add appropriate note.

-boris

#6 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

Quoted 32 lines · Boris Ostrovsky
Quoted 29 lines · Julien Grall

Hi Boris,

Quoted 15 lines · Boris Ostrovsky

This option allows guest administrator specify what should happen when guest accesses an MSR which is not explicitly emulated by the hypervisor.

---
   docs/man/xl.cfg.5.pod.in         | 20 +++++++++++++++++++-
   tools/libs/light/libxl_types.idl |  7 +++++++
   tools/xl/xl_parse.c              |  7 +++++++
   3 files changed, 33 insertions(+), 1 deletion(-)
docs/man/xl.cfg.5.pod.in +0 -0
index c8e017f950de..96ce97c42cab 100644

docs/man/xl.cfg.5.pod.in Do not provide a VM generation ID. · line 2051

Quoted 8 lines
docs/man/xl.cfg.5.pod.in +5 -0
@@ -2044,7 +2044,25 @@ Do not provide a VM generation ID.
   See also "Virtual Machine Generation ID" by Microsoft:
   L<https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/virtual-machine-generation-identifier>
   -=back
+=over
+
+=item B<ignore_msrs="STRING">
+
+Determine hypervisor behavior on accesses to MSRs that are not emulated by the hypervisor.

The description of the feature looks very x86 focus. Yet, it seems to be defined as a generic one.

Quoted 4 lines
Quoted 1 line

Could you clarify whether this is intended to be re-usable by other architectures?

x86 only. I'll add appropriate note.

Thanks. In which case, I would suggest to move the definition of ignore_msrs in the idle to an x86 specific structure. Maybe "arch_x86"?

I noticed that none exists today, but we could duplicate that "arch_arm" one.

Wei, Ian, what do you think?

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

Quoted 2 lines · Julien Grall

Thanks. In which case, I would suggest to move the definition of ignore_msrs in the idle to an x86 specific structure. Maybe "arch_x86"?

I did consider this but left it in common area since there are already a bunch of fields there that are x86-specific.

If there is desire to create an x86 sub-struct then we should move all of them there.

-boris

Quoted 7 lines

I noticed that none exists today, but we could duplicate that "arch_arm" one.

Wei, Ian, what do you think?

Cheers,

#8 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

Quoted 9 lines · Boris Ostrovsky
Quoted 2 lines · Julien Grall

Thanks. In which case, I would suggest to move the definition of ignore_msrs in the idle to an x86 specific structure. Maybe "arch_x86"?

I did consider this but left it in common area since there are already a bunch of fields there that are x86-specific.

Most of them were introduced before Arm existed :).

Quoted 2 lines

If there is desire to create an x86 sub-struct then we should move all of them there.

The libxl interface is meant to be stable at the source level but not ABI stable. So we wouldn't be able to move the existing field in a new x86 without adding a compat layer (see for instance hvm.altp2m vs altp2m). I don't think this is worth it for them.

That said, we should try to treat each architecture equally. So I think it is not a good idea to continue to pursue adding an field in common area if the field is only meant to be used for a single arch.

If Ian or Wei thinks it is fine to define in common code, then so it be.

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

In reply to Boris Ostrovsky
Quoted 28 lines · Boris Ostrovsky

This option allows guest administrator specify what should happen when guest accesses an MSR which is not explicitly emulated by the hypervisor.

---
 docs/man/xl.cfg.5.pod.in         | 20 +++++++++++++++++++-
 tools/libs/light/libxl_types.idl |  7 +++++++
 tools/xl/xl_parse.c              |  7 +++++++
 3 files changed, 33 insertions(+), 1 deletion(-)
docs/man/xl.cfg.5.pod.in +9 -1
index c8e017f950de..96ce97c42cab 100644
@@ -2044,7 +2044,25 @@ Do not provide a VM generation ID.
 See also "Virtual Machine Generation ID" by Microsoft:
 L<https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/virtual-machine-generation-identifier>
 
-=back 
+=over
+
+=item B<ignore_msrs="STRING">
+
+Determine hypervisor behavior on accesses to MSRs that are not emulated by the hypervisor.
+
+=over 4
+
+=item B<never>

docs/man/xl.cfg.5.pod.in Do not provide a VM generation ID. · line 2065

Quoted 10 lines
docs/man/xl.cfg.5.pod.in +10 -0
+
+Issue a warning to the log and #GP to the guest. This is default.
+
+=item B<silent>
+
+MSR reads return 0, MSR writes are ignored. No warnings to the log.
+
+=item B<verbose>
+
+Similar to B<silent> but a warning is written.

Would it make sense to allow for this option to be more fine-grained in the future?

Not that you need to implement the full thing now, but maybe we could have something like:

"
=item B<ignore_msrs=[ "MSR_RANGE, "MSR_RANGE", ..]>

Specify a list of MSR ranges that will be ignored by the hypervisor:
reads will return zeros and writes will be discarded without raising a #GP.

Each MSR_RANGE is given in hexadecimal format and may be a range, e.g. c00102f0-c00102f1 (inclusive), or a single MSR, e.g. c00102f1. "

Then you can print the messages in the hypervisor using a guest log level and modify it on demand in order to get more verbose output?

I don't think selecting whether the messages are printed or not from xl is that helpful as the same could be achieved using guest_loglvl.

Also I think it will be fine to only implement:

ignore_msrs=[ "0-ffffffff" ]

Right now and return an error for any other combination, so that we can get something in soon and expand it later.

Thanks, Roger.

#10 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

Quoted 34 lines · Roger Pau Monné
Quoted 33 lines · Boris Ostrovsky

This option allows guest administrator specify what should happen when guest accesses an MSR which is not explicitly emulated by the hypervisor.

---
 docs/man/xl.cfg.5.pod.in         | 20 +++++++++++++++++++-
 tools/libs/light/libxl_types.idl |  7 +++++++
 tools/xl/xl_parse.c              |  7 +++++++
 3 files changed, 33 insertions(+), 1 deletion(-)
docs/man/xl.cfg.5.pod.in +14 -1
index c8e017f950de..96ce97c42cab 100644
@@ -2044,7 +2044,25 @@ Do not provide a VM generation ID.
 See also "Virtual Machine Generation ID" by Microsoft:
 L<https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/virtual-machine-generation-identifier>
 
-=back 
+=over
+
+=item B<ignore_msrs="STRING">
+
+Determine hypervisor behavior on accesses to MSRs that are not emulated by the hypervisor.
+
+=over 4
+
+=item B<never>
+
+Issue a warning to the log and #GP to the guest. This is default.
+
+=item B<silent>
+
Quoted 8 lines

docs/man/xl.cfg.5.pod.in Do not provide a VM generation ID. · line 2065

Quoted 5 lines
docs/man/xl.cfg.5.pod.in +5 -0
+MSR reads return 0, MSR writes are ignored. No warnings to the log.
+
+=item B<verbose>
+
+Similar to B<silent> but a warning is written.

Would it make sense to allow for this option to be more fine-grained in the future?

From an abstract perspective - maybe. But remember that this information will need to be migrated with the guest. It would seem to me that Boris'es approach is easier migration-wise.

Quoted 10 lines

Not that you need to implement the full thing now, but maybe we could have something like:

"
=item B<ignore_msrs=[ "MSR_RANGE, "MSR_RANGE", ..]>

Specify a list of MSR ranges that will be ignored by the hypervisor:
reads will return zeros and writes will be discarded without raising a #GP.

Quoted 6 lines

Each MSR_RANGE is given in hexadecimal format and may be a range, e.g. c00102f0-c00102f1 (inclusive), or a single MSR, e.g. c00102f1. "

Then you can print the messages in the hypervisor using a guest log level and modify it on demand in order to get more verbose output?

"Modify on demand"? Irrespective of what you mean with this, ...

Quoted 2 lines

I don't think selecting whether the messages are printed or not from xl is that helpful as the same could be achieved using guest_loglvl.

... controlling this via guest_loglvl would affect various other log messages' visibility.

Jan

#11 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

Quoted 44 lines · Jan Beulich
Quoted 34 lines · Roger Pau Monné
Quoted 33 lines · Boris Ostrovsky

This option allows guest administrator specify what should happen when guest accesses an MSR which is not explicitly emulated by the hypervisor.

---
 docs/man/xl.cfg.5.pod.in         | 20 +++++++++++++++++++-
 tools/libs/light/libxl_types.idl |  7 +++++++
 tools/xl/xl_parse.c              |  7 +++++++
 3 files changed, 33 insertions(+), 1 deletion(-)
docs/man/xl.cfg.5.pod.in +14 -1
index c8e017f950de..96ce97c42cab 100644
@@ -2044,7 +2044,25 @@ Do not provide a VM generation ID.
 See also "Virtual Machine Generation ID" by Microsoft:
 L<https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/virtual-machine-generation-identifier>
 
-=back 
+=over
+
+=item B<ignore_msrs="STRING">
+
+Determine hypervisor behavior on accesses to MSRs that are not emulated by the hypervisor.
+
+=over 4
+
+=item B<never>
+
+Issue a warning to the log and #GP to the guest. This is default.
+
+=item B<silent>
+
Quoted 8 lines

docs/man/xl.cfg.5.pod.in Do not provide a VM generation ID. · line 2065

Quoted 5 lines
docs/man/xl.cfg.5.pod.in +5 -0
+MSR reads return 0, MSR writes are ignored. No warnings to the log.
+
+=item B<verbose>
+
+Similar to B<silent> but a warning is written.

Would it make sense to allow for this option to be more fine-grained in the future?

Quoted 3 lines

From an abstract perspective - maybe. But remember that this information will need to be migrated with the guest. It would seem to me that Boris'es approach is easier migration-wise.

I'm not an expert on migration, but I seem to recall there's already a libxl blob that gets migrated that contains the domain configuration, so having the MSR configuration there seems like a sensible thing to do.

Quoted 17 lines
Quoted 10 lines

Not that you need to implement the full thing now, but maybe we could have something like:

"
=item B<ignore_msrs=[ "MSR_RANGE, "MSR_RANGE", ..]>

Specify a list of MSR ranges that will be ignored by the hypervisor:
reads will return zeros and writes will be discarded without raising a #GP.

Quoted 6 lines

Each MSR_RANGE is given in hexadecimal format and may be a range, e.g. c00102f0-c00102f1 (inclusive), or a single MSR, e.g. c00102f1. "

Then you can print the messages in the hypervisor using a guest log level and modify it on demand in order to get more verbose output?

Quoted 7 lines

"Modify on demand"? Irrespective of what you mean with this, ...

Quoted 2 lines

I don't think selecting whether the messages are printed or not from xl is that helpful as the same could be achieved using guest_loglvl.

... controlling this via guest_loglvl would affect various other log messages' visibility.

Right, but do we really need this level of per-guest log control, implemented in this way exclusively for MSRs?

We don't have a way for other parts of the code to have such fine-grained control about what messages should be printed, and I don't think MSR should be an exception. I assume this would be used to detect which MSRs a guest is trying to access, and I would be fine just using guest_loglvl to that end, the more that it can be modified at run time now.

In any case I'm more worried about having a big switch to ignore all unhandled MSRs rather than whether accesses should print a message or not. Worse come to worse we could always add a new option afterwards to selectively ignore MSR access, but that would be confusing for users IMO.

Thanks, Roger.

#12 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

Quoted 30 lines · Roger Pau Monné
Quoted 18 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné

Not that you need to implement the full thing now, but maybe we could have something like:

"
=item B<ignore_msrs=[ "MSR_RANGE, "MSR_RANGE", ..]>

Specify a list of MSR ranges that will be ignored by the hypervisor:
reads will return zeros and writes will be discarded without raising a #GP.

Quoted 6 lines

Each MSR_RANGE is given in hexadecimal format and may be a range, e.g. c00102f0-c00102f1 (inclusive), or a single MSR, e.g. c00102f1. "

Then you can print the messages in the hypervisor using a guest log level and modify it on demand in order to get more verbose output?

Quoted 7 lines

"Modify on demand"? Irrespective of what you mean with this, ...

Quoted 2 lines

I don't think selecting whether the messages are printed or not from xl is that helpful as the same could be achieved using guest_loglvl.

... controlling this via guest_loglvl would affect various other log messages' visibility.

Right, but do we really need this level of per-guest log control, implemented in this way exclusively for MSRs?

Quoted 6 lines

We don't have a way for other parts of the code to have such fine-grained control about what messages should be printed, and I don't think MSR should be an exception. I assume this would be used to detect which MSRs a guest is trying to access, and I would be fine just using guest_loglvl to that end, the more that it can be modified at run time now.

I can certainly see your point. The problem is that for guests heavily accessing such MSRs, all other messages may disappear due to rate limiting. That's not going to be helpful.

Jan

#13 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

Quoted 19 lines · Jan Beulich
Quoted 18 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Roger Pau Monné

Not that you need to implement the full thing now, but maybe we could have something like:

"
=item B<ignore_msrs=[ "MSR_RANGE, "MSR_RANGE", ..]>

Specify a list of MSR ranges that will be ignored by the hypervisor:
reads will return zeros and writes will be discarded without raising a #GP.

Each MSR_RANGE is given in hexadecimal format and may be a range, e.g. c00102f0-c00102f1 (inclusive), or a single MSR, e.g. c00102f1. "

Then you can print the messages in the hypervisor using a guest log level and modify it on demand in order to get more verbose output?

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

"Modify on demand"? Irrespective of what you mean with this, ...

Quoted 2 lines

I don't think selecting whether the messages are printed or not from xl is that helpful as the same could be achieved using guest_loglvl.

... controlling this via guest_loglvl would affect various other log messages' visibility.

Right, but do we really need this level of per-guest log control, implemented in this way exclusively for MSRs?

In a multi-tenant environment we may need to figure out why a particular guest is failing to boot, without affecting behavior of other guests.

If we had per-guest log level in general then what you are suggesting would be the right thing to do IMO. Maybe that's what we should add?

-boris

Quoted 10 lines
Quoted 7 lines

We don't have a way for other parts of the code to have such fine-grained control about what messages should be printed, and I don't think MSR should be an exception. I assume this would be used to detect which MSRs a guest is trying to access, and I would be fine just using guest_loglvl to that end, the more that it can be modified at run time now.

I can certainly see your point. The problem is that for guests heavily accessing such MSRs, all other messages may disappear due to rate limiting. That's not going to be helpful.

#14 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

Quoted 27 lines · Boris Ostrovsky
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné

Not that you need to implement the full thing now, but maybe we could have something like:

"
=item B<ignore_msrs=[ "MSR_RANGE, "MSR_RANGE", ..]>

Specify a list of MSR ranges that will be ignored by the hypervisor:
reads will return zeros and writes will be discarded without raising a #GP.

Quoted 6 lines

Each MSR_RANGE is given in hexadecimal format and may be a range, e.g. c00102f0-c00102f1 (inclusive), or a single MSR, e.g. c00102f1. "

Then you can print the messages in the hypervisor using a guest log level and modify it on demand in order to get more verbose output?

"Modify on demand"? Irrespective of what you mean with this, ...

Quoted 2 lines

I don't think selecting whether the messages are printed or not from xl is that helpful as the same could be achieved using guest_loglvl.

... controlling this via guest_loglvl would affect various other log messages' visibility.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

Right, but do we really need this level of per-guest log control, implemented in this way exclusively for MSRs?

In a multi-tenant environment we may need to figure out why a particular guest is failing to boot, without affecting behavior of other guests.

If we had per-guest log level in general then what you are suggesting would be the right thing to do IMO. Maybe that's what we should add?

Yes, that would seem better IMO, but I don't think it's fair to ask you to do that work.

Do you think it would be acceptable to untangle both, and try to get the MSR stuff without any logging changes?

I know we would be addressing only one part of what the series originally tried to achieve, but I would rather prefer to have a generic way to set a per-guest log level rather than something specific to MSR accesses.

Thanks, Roger.

#15 lore raw

Re: [PATCH v2 1/4] xl: Add support for ignore_msrs option

Quoted 40 lines · Roger Pau Monné
Quoted 27 lines · Boris Ostrovsky
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné

Not that you need to implement the full thing now, but maybe we could have something like:

"
=item B<ignore_msrs=[ "MSR_RANGE, "MSR_RANGE", ..]>

Specify a list of MSR ranges that will be ignored by the hypervisor:
reads will return zeros and writes will be discarded without raising a #GP.

Quoted 6 lines

Each MSR_RANGE is given in hexadecimal format and may be a range, e.g. c00102f0-c00102f1 (inclusive), or a single MSR, e.g. c00102f1. "

Then you can print the messages in the hypervisor using a guest log level and modify it on demand in order to get more verbose output?

"Modify on demand"? Irrespective of what you mean with this, ...

Quoted 2 lines

I don't think selecting whether the messages are printed or not from xl is that helpful as the same could be achieved using guest_loglvl.

... controlling this via guest_loglvl would affect various other log messages' visibility.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

Right, but do we really need this level of per-guest log control, implemented in this way exclusively for MSRs?

In a multi-tenant environment we may need to figure out why a particular guest is failing to boot, without affecting behavior of other guests.

If we had per-guest log level in general then what you are suggesting would be the right thing to do IMO. Maybe that's what we should add?

Yes, that would seem better IMO, but I don't think it's fair to ask you to do that work.

Quoted 7 lines

Do you think it would be acceptable to untangle both, and try to get the MSR stuff without any logging changes?

I know we would be addressing only one part of what the series originally tried to achieve, but I would rather prefer to have a generic way to set a per-guest log level rather than something specific to MSR accesses.

TBH I'd see us go the other route: Follow Boris'es approach for 4.15, and switch the logging control to per-guest once that ability is there, _and_ if we're really convinced we don't want to have this extra level of control. The latter because I think a domain could end up pretty chatty just because of MSR accesses, and it might therefore be undesirable to also hide all other potentially relevant output. Perhaps the per-domain log level control needs to be finer grained than what "guest_loglvl=" currently permits, more like what "hvm_debug=" has.

Jan

#16 lore raw

PATCH v2 2/4 x86: Introduce MSR_UNHANDLED

In reply to Boris Ostrovsky

When toolstack updates MSR policy, this MSR offset (which is the last index in the hypervisor MSR range) is used to indicate hypervisor behavior when guest accesses an MSR which is not explicitly emulated.

---
Changes in v2:
* Use 0x400002ff for MSR_UNHANDLED
* Pass ignore_msrs in msr_policy.value

 xen/arch/x86/msr.c            |  4 ++--
 xen/include/xen/lib/x86/msr.h | 17 ++++++++++++++++-
 xen/lib/x86/msr.c             |  2 ++
 3 files changed, 20 insertions(+), 3 deletions(-)
xen/arch/x86/msr.c +2 -2
index be8e36386250..433d16c80728 100644
@@ -295,7 +295,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val)
         }
 
         /* Fallthrough. */
-    case 0x40000200 ... 0x400002ff:
+    case 0x40000200 ... 0x400002fe:
         ret = guest_rdmsr_xen(v, msr, val);
         break;
 
@@ -514,7 +514,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)
         }
 
         /* Fallthrough. */
-    case 0x40000200 ... 0x400002ff:
+    case 0x40000200 ... 0x400002fe:
         ret = guest_wrmsr_xen(v, msr, val);
         break;
 
xen/include/xen/lib/x86/msr.h +16 -1
index 48ba4a59c036..fbbb3b7ba870 100644
@@ -2,8 +2,21 @@
 #ifndef XEN_LIB_X86_MSR_H
 #define XEN_LIB_X86_MSR_H
 
+/*
+ * Behavior on accesses to MSRs that are not handled by emulation:
+ *  0 = return #GP, warning emitted
+ *  1 = read as 0, writes are dropped, no warning
+ *  2 = read as 0, writes are dropped, warning emitted
+ */
+#define MSR_UNHANDLED_NEVER     0
+#define MSR_UNHANDLED_SILENT    1
+#define MSR_UNHANDLED_VERBOSE   2
+
+/* MSR that is not explicitly processed by emulation */
+#define MSR_UNHANDLED           0x400002ff
+
 /* Maximum number of MSRs written when serialising msr_policy. */
-#define MSR_MAX_SERIALISED_ENTRIES 2
+#define MSR_MAX_SERIALISED_ENTRIES 3
 
 /* MSR policy object for shared per-domain MSRs */
 struct msr_policy
@@ -45,6 +58,8 @@ struct msr_policy
             bool taa_no:1;
         };
     } arch_caps;
+
+    uint8_t ignore_msrs;
 };
 
 #ifdef __XEN__
xen/lib/x86/msr.c +2 -0
index 7d71e92a380a..178203803946 100644
@@ -40,6 +40,7 @@ int x86_msr_copy_to_buffer(const struct msr_policy *p,
 
     COPY_MSR(MSR_INTEL_PLATFORM_INFO, p->platform_info.raw);
     COPY_MSR(MSR_ARCH_CAPABILITIES,   p->arch_caps.raw);
+    COPY_MSR(MSR_UNHANDLED,           p->ignore_msrs);
 
 #undef COPY_MSR
 
@@ -101,6 +102,7 @@ int x86_msr_copy_from_buffer(struct msr_policy *p,
 
         case MSR_INTEL_PLATFORM_INFO: ASSIGN(platform_info.raw); break;
         case MSR_ARCH_CAPABILITIES:   ASSIGN(arch_caps.raw);     break;
+        case MSR_UNHANDLED:           ASSIGN(ignore_msrs);       break;
 
 #undef ASSIGN
 
Signature · 1 line
1.8.3.1
#17 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 12 lines · Boris Ostrovsky
xen/arch/x86/msr.c +1 -1
@@ -295,7 +295,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val)
         }
 
         /* Fallthrough. */
-    case 0x40000200 ... 0x400002ff:
+    case 0x40000200 ... 0x400002fe:
         ret = guest_rdmsr_xen(v, msr, val);
         break;
 

xen/arch/x86/msr.c int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val) · line 519

Quoted 7 lines
xen/arch/x86/msr.c +1 -1
@@ -514,7 +514,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)
         }
 
         /* Fallthrough. */
-    case 0x40000200 ... 0x400002ff:
+    case 0x40000200 ... 0x400002fe:
         ret = guest_wrmsr_xen(v, msr, val);
         break;

For both of these, we need some kind of connection to MSR_UNHANDLED. Could be a BUILD_BUG_ON() or an explicit "case MSR_UNHANDLED:" (preventing someone "correcting" the apparent mistake) or yet something else.

Quoted 8 lines
xen/include/xen/lib/x86/msr.h +2 -0
@@ -2,8 +2,21 @@
 #ifndef XEN_LIB_X86_MSR_H
 #define XEN_LIB_X86_MSR_H
 
+/*
+ * Behavior on accesses to MSRs that are not handled by emulation:

xen/include/xen/lib/x86/msr.h line 16

Quoted 10 lines
xen/include/xen/lib/x86/msr.h +10 -0
+ *  0 = return #GP, warning emitted
+ *  1 = read as 0, writes are dropped, no warning
+ *  2 = read as 0, writes are dropped, warning emitted
+ */
+#define MSR_UNHANDLED_NEVER     0
+#define MSR_UNHANDLED_SILENT    1
+#define MSR_UNHANDLED_VERBOSE   2
+
+/* MSR that is not explicitly processed by emulation */
+#define MSR_UNHANDLED           0x400002ff

MSR indexes as well as definitions for MSR contents generally live in asm-x86/msr-index.h. I think it would be better for the above to also go there.

Additionally the comment on MSR_UNHANDLED should not only say what will not happen for this index, but also what its intended purpose is.

xen/include/xen/lib/x86/msr.h struct msr_policy · line 62

Quoted 6 lines
xen/include/xen/lib/x86/msr.h +2 -0
@@ -45,6 +58,8 @@ struct msr_policy
             bool taa_no:1;
         };
     } arch_caps;
+
+    uint8_t ignore_msrs;

Add a brief comment along the lines of /* MSR_UNHANDLED_* */ here to make the connection to intended values?

Also, Andrew, (I think I did say so before) - I definitely would want your general consent with this model, as what gets altered here is almost all relatively recent contributions by you. Nor would I exclude the approach being controversial.

Jan

#18 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 15 lines · Jan Beulich
Quoted 14 lines · Boris Ostrovsky
xen/arch/x86/msr.c +1 -1
@@ -295,7 +295,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val)
         }
 
         /* Fallthrough. */
-    case 0x40000200 ... 0x400002ff:
+    case 0x40000200 ... 0x400002fe:
         ret = guest_rdmsr_xen(v, msr, val);
         break;
 
@@ -514,7 +514,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)
         }
 
Quoted 10 lines

xen/arch/x86/msr.c int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val) · line 519

Quoted 5 lines
xen/arch/x86/msr.c +1 -1
         /* Fallthrough. */
-    case 0x40000200 ... 0x400002ff:
+    case 0x40000200 ... 0x400002fe:
         ret = guest_wrmsr_xen(v, msr, val);
         break;

For both of these, we need some kind of connection to MSR_UNHANDLED. Could be a BUILD_BUG_ON() or an explicit "case MSR_UNHANDLED:" (preventing someone "correcting" the apparent mistake) or yet something else.

I actually meant to add a comment there but forgot.

I'll add an explicit 'case'.

Quoted 7 lines
Quoted 7 lines
xen/include/xen/lib/x86/msr.h +0 -0
@@ -2,8 +2,21 @@
 #ifndef XEN_LIB_X86_MSR_H
 #define XEN_LIB_X86_MSR_H
 
Quoted 17 lines

xen/include/xen/lib/x86/msr.h line 16

Quoted 12 lines
xen/include/xen/lib/x86/msr.h +12 -0
+/*
+ * Behavior on accesses to MSRs that are not handled by emulation:
+ *  0 = return #GP, warning emitted
+ *  1 = read as 0, writes are dropped, no warning
+ *  2 = read as 0, writes are dropped, warning emitted
+ */
+#define MSR_UNHANDLED_NEVER     0
+#define MSR_UNHANDLED_SILENT    1
+#define MSR_UNHANDLED_VERBOSE   2
+
+/* MSR that is not explicitly processed by emulation */
+#define MSR_UNHANDLED           0x400002ff

MSR indexes as well as definitions for MSR contents generally live in asm-x86/msr-index.h. I think it would be better for the above to also go there.

I didn't put it there because I felt that file is for "real" (including hypervisor range) MSRs. But I can move it to msr-index.h

-boris

#19 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

In reply to Jan Beulich
Quoted 5 lines · Jan Beulich

Also, Andrew, (I think I did say so before) - I definitely would want your general consent with this model, as what gets altered here is almost all relatively recent contributions by you. Nor would I exclude the approach being controversial.

Andrew, ping?

-boris

#20 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

In reply to Boris Ostrovsky
Quoted 3 lines · Boris Ostrovsky

When toolstack updates MSR policy, this MSR offset (which is the last index in the hypervisor MSR range) is used to indicate hypervisor behavior when guest accesses an MSR which is not explicitly emulated.

It's kind of weird to use an MSR to store this. I assume this is done for migration reasons?

Isn't is possible to convey this data in the xl migration stream instead of having to pack it with MSRs?

Thanks, Roger.

#21 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 6 lines · Roger Pau Monné
Quoted 3 lines · Boris Ostrovsky

When toolstack updates MSR policy, this MSR offset (which is the last index in the hypervisor MSR range) is used to indicate hypervisor behavior when guest accesses an MSR which is not explicitly emulated.

It's kind of weird to use an MSR to store this. I assume this is done for migration reasons?

Not really. It just seemed to me that MSR policy is the logical place to do that. Because it *is* a policy of how to deal with such accesses, isn't it?

Quoted 2 lines

Isn't is possible to convey this data in the xl migration stream instead of having to pack it with MSRs?

I haven't looked at this but again --- the feature itself has nothing to do with migration. The fact that folding it into policy makes migration of this information "just work" is just a nice side benefit of this implementation.

-boris

#22 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 11 lines · Boris Ostrovsky
Quoted 6 lines · Roger Pau Monné
Quoted 3 lines · Boris Ostrovsky

When toolstack updates MSR policy, this MSR offset (which is the last index in the hypervisor MSR range) is used to indicate hypervisor behavior when guest accesses an MSR which is not explicitly emulated.

It's kind of weird to use an MSR to store this. I assume this is done for migration reasons?

Not really. It just seemed to me that MSR policy is the logical place to do that. Because it *is* a policy of how to deal with such accesses, isn't it?

I agree that using the msr_policy seems like the most suitable place to convey this information between the toolstack and Xen. I wonder if it would be fine to have fields in msr_policy that don't directly translate into an MSR value?

But having such a list of ignored MSRs in msr_policy makes the whole get/set logic a bit weird, as the user would have to provide a buffer in order to get the list of ignored MSRs.

Quoted 6 lines
Quoted 3 lines

Isn't is possible to convey this data in the xl migration stream instead of having to pack it with MSRs?

I haven't looked at this but again --- the feature itself has nothing to do with migration. The fact that folding it into policy makes migration of this information "just work" is just a nice side benefit of this implementation.

IMO it feels slightly weird that we have to use a MSR (MSR_UNHANDLED) to store this option, seems like wasting an MSR index when there's really no need for it to be stored in an MSR, as we don't expose it to guests.

It would seem more natural for such option to live in arch_domain as a rangeset for example.

Maybe introduce a new DOMCTL to set it?

#define XEN_DOMCTL_msr_set_ignore ...

struct xen_domctl_msr_set_ignore {
    uint32_t index;
    uint32_t size;
};

Thanks, Roger.

#23 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 6 lines · Roger Pau Monné
Quoted 5 lines · Boris Ostrovsky
Quoted 4 lines · Roger Pau Monné
Quoted 3 lines · Boris Ostrovsky

When toolstack updates MSR policy, this MSR offset (which is the last index in the hypervisor MSR range) is used to indicate hypervisor behavior when guest accesses an MSR which is not explicitly emulated.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

It's kind of weird to use an MSR to store this. I assume this is done for migration reasons?

Not really. It just seemed to me that MSR policy is the logical place to do that. Because it *is* a policy of how to deal with such accesses, isn't it?

I agree that using the msr_policy seems like the most suitable place to convey this information between the toolstack and Xen. I wonder if it would be fine to have fields in msr_policy that don't directly translate into an MSR value?

We have xen_msr_entry_t.flags that we can use when passing policy array back and forth. Then we can ignore xen_msr_entry_t.idx for that entry (although in earlier version of this series Jan preferred to use idx and leave flags alone).

Quoted 4 lines

But having such a list of ignored MSRs in msr_policy makes the whole get/set logic a bit weird, as the user would have to provide a buffer in order to get the list of ignored MSRs.

If we go with ranges/lists of ignored MSRs then we will need to have ignore_msrs as a rangeset in msr_policy, not as (current) uint8. And xen_msr_entry_t will need to have a range as opposed to single index. Or maybe I don't understand what you are referring to as get/set logic.

But I would like to make sure we really want to support such ranges, I am a little concerned about over-engineering this (especially wrt migration, I think it will need marshalling/unmarshalling)

Quoted 12 lines
Quoted 4 lines
Quoted 2 lines

Isn't is possible to convey this data in the xl migration stream instead of having to pack it with MSRs?

I haven't looked at this but again --- the feature itself has nothing to do with migration. The fact that folding it into policy makes migration of this information "just work" is just a nice side benefit of this implementation.

IMO it feels slightly weird that we have to use a MSR (MSR_UNHANDLED) to store this option, seems like wasting an MSR index when there's really no need for it to be stored in an MSR, as we don't expose it to guests.

It would seem more natural for such option to live in arch_domain as a rangeset for example.

Quoted 7 lines

Maybe introduce a new DOMCTL to set it?

#define XEN_DOMCTL_msr_set_ignore ...

struct xen_domctl_msr_set_ignore {
    uint32_t index;
    uint32_t size;
};

That will work too but this is adding 2 new domctls (I think we will need a "get" too) whereas with policy we use existing interface.

-boris

#24 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 11 lines · Boris Ostrovsky
Quoted 9 lines · Roger Pau Monné
Quoted 8 lines · Boris Ostrovsky
Quoted 6 lines · Roger Pau Monné
Quoted 3 lines · Boris Ostrovsky

When toolstack updates MSR policy, this MSR offset (which is the last index in the hypervisor MSR range) is used to indicate hypervisor behavior when guest accesses an MSR which is not explicitly emulated.

It's kind of weird to use an MSR to store this. I assume this is done for migration reasons?

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Not really. It just seemed to me that MSR policy is the logical place to do that. Because it *is* a policy of how to deal with such accesses, isn't it?

I agree that using the msr_policy seems like the most suitable place to convey this information between the toolstack and Xen. I wonder if it would be fine to have fields in msr_policy that don't directly translate into an MSR value?

We have xen_msr_entry_t.flags that we can use when passing policy array back and forth. Then we can ignore xen_msr_entry_t.idx for that entry (although in earlier version of this series Jan preferred to use idx and leave flags alone).

Which, just to clarify, was not the least because of the flags field being per-entry, i.e. per MSR, while here we want a global indicator.

Jan

#25 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 19 lines · Jan Beulich
Quoted 18 lines · Boris Ostrovsky
Quoted 14 lines · Roger Pau Monné
Quoted 9 lines · Boris Ostrovsky
Quoted 6 lines · Roger Pau Monné
Quoted 3 lines · Boris Ostrovsky

When toolstack updates MSR policy, this MSR offset (which is the last index in the hypervisor MSR range) is used to indicate hypervisor behavior when guest accesses an MSR which is not explicitly emulated.

It's kind of weird to use an MSR to store this. I assume this is done for migration reasons?

Not really. It just seemed to me that MSR policy is the logical place to do that. Because it *is* a policy of how to deal with such accesses, isn't it?

I agree that using the msr_policy seems like the most suitable place to convey this information between the toolstack and Xen. I wonder if it would be fine to have fields in msr_policy that don't directly translate into an MSR value?

Quoted 5 lines
Quoted 1 line

We have xen_msr_entry_t.flags that we can use when passing policy array back and forth. Then we can ignore xen_msr_entry_t.idx for that entry (although in earlier version of this series Jan preferred to use idx and leave flags alone).

Which, just to clarify, was not the least because of the flags field being per-entry, i.e. per MSR, while here we want a global indicator.

We could exploit a bit the xen_msr_entry_t structure and use it like:

typedef struct xen_msr_entry {
    uint32_t idx;
#define XEN_MSR_IGNORE (1u << 0)
    uint32_t flags;
    uint64_t val;
} xen_msr_entry_t;

Then use the idx and val fields to signal the start and size of the range to ignore accesses to when XEN_MSR_IGNORE is set in the flags field?

xen_msr_entry_t = {
    .idx = 0,
    .val = 0xffffffff,
    .flags = XEN_MSR_IGNORE,
};

Would be equivalent to ignoring accesses to the whole MSR range.

This would allow selectively selecting which MSRs to ignore, while not wasting a MSR itself to convey the information?

It would still need to be stored somewhere in the Xen internal domain structure using a rangeset I think, which could be translated back and forth into this xen_msr_entry_t format.

Roger.

#26 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 46 lines · Roger Pau Monné
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Boris Ostrovsky
Quoted 14 lines · Roger Pau Monné
Quoted 9 lines · Boris Ostrovsky
Quoted 6 lines · Roger Pau Monné
Quoted 3 lines · Boris Ostrovsky

When toolstack updates MSR policy, this MSR offset (which is the last index in the hypervisor MSR range) is used to indicate hypervisor behavior when guest accesses an MSR which is not explicitly emulated.

It's kind of weird to use an MSR to store this. I assume this is done for migration reasons?

Not really. It just seemed to me that MSR policy is the logical place to do that. Because it *is* a policy of how to deal with such accesses, isn't it?

I agree that using the msr_policy seems like the most suitable place to convey this information between the toolstack and Xen. I wonder if it would be fine to have fields in msr_policy that don't directly translate into an MSR value?

Quoted 5 lines
Quoted 1 line

We have xen_msr_entry_t.flags that we can use when passing policy array back and forth. Then we can ignore xen_msr_entry_t.idx for that entry (although in earlier version of this series Jan preferred to use idx and leave flags alone).

Which, just to clarify, was not the least because of the flags field being per-entry, i.e. per MSR, while here we want a global indicator.

We could exploit a bit the xen_msr_entry_t structure and use it like:

typedef struct xen_msr_entry {
    uint32_t idx;
#define XEN_MSR_IGNORE (1u << 0)
    uint32_t flags;
    uint64_t val;
} xen_msr_entry_t;

Then use the idx and val fields to signal the start and size of the range to ignore accesses to when XEN_MSR_IGNORE is set in the flags field?

xen_msr_entry_t = {
    .idx = 0,
    .val = 0xffffffff,
    .flags = XEN_MSR_IGNORE,
};
Quoted 4 lines

Would be equivalent to ignoring accesses to the whole MSR range.

This would allow selectively selecting which MSRs to ignore, while not wasting a MSR itself to convey the information?

Hmm, yes, the added flexibility would be nice from an abstract pov (not sure how relevant it would be to Solaris'es issue). But my dislike of using a flag which is meaningless in ordinary entries remains, as was voiced against Boris'es original version.

Andrew - afaict you've been completely silent on this thread so far. What's your view?

Jan

#27 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 49 lines · Jan Beulich
Quoted 48 lines · Roger Pau Monné
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Boris Ostrovsky
Quoted 14 lines · Roger Pau Monné
Quoted 9 lines · Boris Ostrovsky
Quoted 6 lines · Roger Pau Monné
Quoted 3 lines · Boris Ostrovsky

When toolstack updates MSR policy, this MSR offset (which is the last index in the hypervisor MSR range) is used to indicate hypervisor behavior when guest accesses an MSR which is not explicitly emulated.

It's kind of weird to use an MSR to store this. I assume this is done for migration reasons?

Not really. It just seemed to me that MSR policy is the logical place to do that. Because it *is* a policy of how to deal with such accesses, isn't it?

I agree that using the msr_policy seems like the most suitable place to convey this information between the toolstack and Xen. I wonder if it would be fine to have fields in msr_policy that don't directly translate into an MSR value?

Quoted 5 lines
Quoted 1 line

We have xen_msr_entry_t.flags that we can use when passing policy array back and forth. Then we can ignore xen_msr_entry_t.idx for that entry (although in earlier version of this series Jan preferred to use idx and leave flags alone).

Which, just to clarify, was not the least because of the flags field being per-entry, i.e. per MSR, while here we want a global indicator.

We could exploit a bit the xen_msr_entry_t structure and use it like:

typedef struct xen_msr_entry {
    uint32_t idx;
#define XEN_MSR_IGNORE (1u << 0)
    uint32_t flags;
    uint64_t val;
} xen_msr_entry_t;

Then use the idx and val fields to signal the start and size of the range to ignore accesses to when XEN_MSR_IGNORE is set in the flags field?

xen_msr_entry_t = {
    .idx = 0,
    .val = 0xffffffff,
    .flags = XEN_MSR_IGNORE,
};

Would be equivalent to ignoring accesses to the whole MSR range.

Quoted 7 lines
Quoted 2 lines

This would allow selectively selecting which MSRs to ignore, while not wasting a MSR itself to convey the information?

Hmm, yes, the added flexibility would be nice from an abstract pov (not sure how relevant it would be to Solaris'es issue). But my dislike of using a flag which is meaningless in ordinary entries remains, as was voiced against Boris'es original version.

I understand the flags field is meaningless for regular MSRs, but I don't see why it would be an issue to start using it for this specific case of registering ranges of ignored MSRs. It certainly seems better than hijacking an MSR index (MSR_UNHANDLED).

Thanks, Roger.

#28 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 53 lines · Roger Pau Monné
Quoted 52 lines · Jan Beulich
Quoted 50 lines · Roger Pau Monné
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Boris Ostrovsky
Quoted 14 lines · Roger Pau Monné
Quoted 9 lines · Boris Ostrovsky
Quoted 6 lines · Roger Pau Monné
Quoted 3 lines · Boris Ostrovsky

When toolstack updates MSR policy, this MSR offset (which is the last index in the hypervisor MSR range) is used to indicate hypervisor behavior when guest accesses an MSR which is not explicitly emulated.

It's kind of weird to use an MSR to store this. I assume this is done for migration reasons?

Not really. It just seemed to me that MSR policy is the logical place to do that. Because it *is* a policy of how to deal with such accesses, isn't it?

I agree that using the msr_policy seems like the most suitable place to convey this information between the toolstack and Xen. I wonder if it would be fine to have fields in msr_policy that don't directly translate into an MSR value?

Quoted 5 lines
Quoted 1 line

We have xen_msr_entry_t.flags that we can use when passing policy array back and forth. Then we can ignore xen_msr_entry_t.idx for that entry (although in earlier version of this series Jan preferred to use idx and leave flags alone).

Which, just to clarify, was not the least because of the flags field being per-entry, i.e. per MSR, while here we want a global indicator.

We could exploit a bit the xen_msr_entry_t structure and use it like:

typedef struct xen_msr_entry {
    uint32_t idx;
#define XEN_MSR_IGNORE (1u << 0)
    uint32_t flags;
    uint64_t val;
} xen_msr_entry_t;

Then use the idx and val fields to signal the start and size of the range to ignore accesses to when XEN_MSR_IGNORE is set in the flags field?

xen_msr_entry_t = {
    .idx = 0,
    .val = 0xffffffff,
    .flags = XEN_MSR_IGNORE,
};

Would be equivalent to ignoring accesses to the whole MSR range.

This would allow selectively selecting which MSRs to ignore, while not wasting a MSR itself to convey the information?

Quoted 8 lines
Quoted 4 lines

Hmm, yes, the added flexibility would be nice from an abstract pov (not sure how relevant it would be to Solaris'es issue). But my dislike of using a flag which is meaningless in ordinary entries remains, as was voiced against Boris'es original version.

I understand the flags field is meaningless for regular MSRs, but I don't see why it would be an issue to start using it for this specific case of registering ranges of ignored MSRs.

It's not an "issue", it is - as said - my dislike. However, the way it is in your proposal it is perhaps indeed not as bad as in Boris'es original one: The flag now designates the purpose of the entry, and the other two fields still have a meaning. Hence I was wrong to state that it's "meaningless" - it now is required to be clear for "ordinary" entries.

In principle there could then also be multiple such entries / ranges.

Quoted 1 line

It certainly seems better than hijacking an MSR index (MSR_UNHANDLED).

Not sure.

Jan

#29 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 55 lines · Jan Beulich
Quoted 47 lines · Roger Pau Monné
Quoted 46 lines · Jan Beulich
Quoted 45 lines · Roger Pau Monné
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Boris Ostrovsky
Quoted 13 lines · Roger Pau Monné
Quoted 8 lines · Boris Ostrovsky
Quoted 6 lines · Roger Pau Monné
Quoted 3 lines · Boris Ostrovsky

When toolstack updates MSR policy, this MSR offset (which is the last index in the hypervisor MSR range) is used to indicate hypervisor behavior when guest accesses an MSR which is not explicitly emulated.

It's kind of weird to use an MSR to store this. I assume this is done for migration reasons?

Not really. It just seemed to me that MSR policy is the logical place to do that. Because it *is* a policy of how to deal with such accesses, isn't it?

I agree that using the msr_policy seems like the most suitable place to convey this information between the toolstack and Xen. I wonder if it would be fine to have fields in msr_policy that don't directly translate into an MSR value?

Quoted 4 lines
Quoted 1 line

We have xen_msr_entry_t.flags that we can use when passing policy array back and forth. Then we can ignore xen_msr_entry_t.idx for that entry (although in earlier version of this series Jan preferred to use idx and leave flags alone).

Which, just to clarify, was not the least because of the flags field being per-entry, i.e. per MSR, while here we want a global indicator.

We could exploit a bit the xen_msr_entry_t structure and use it like:

typedef struct xen_msr_entry {
    uint32_t idx;
#define XEN_MSR_IGNORE (1u << 0)
    uint32_t flags;
    uint64_t val;
} xen_msr_entry_t;

Then use the idx and val fields to signal the start and size of the range to ignore accesses to when XEN_MSR_IGNORE is set in the flags field?

xen_msr_entry_t = {
    .idx = 0,
    .val = 0xffffffff,
    .flags = XEN_MSR_IGNORE,
};

Would be equivalent to ignoring accesses to the whole MSR range.

This would allow selectively selecting which MSRs to ignore, while not wasting a MSR itself to convey the information?

Quoted 7 lines
Quoted 4 lines

Hmm, yes, the added flexibility would be nice from an abstract pov (not sure how relevant it would be to Solaris'es issue). But my dislike of using a flag which is meaningless in ordinary entries remains, as was voiced against Boris'es original version.

I understand the flags field is meaningless for regular MSRs, but I don't see why it would be an issue to start using it for this specific case of registering ranges of ignored MSRs.

Quoted 8 lines

It's not an "issue", it is - as said - my dislike. However, the way it is in your proposal it is perhaps indeed not as bad as in Boris'es original one: The flag now designates the purpose of the entry, and the other two fields still have a meaning. Hence I was wrong to state that it's "meaningless" - it now is required to be clear for "ordinary" entries.

In principle there could then also be multiple such entries / ranges.

TBH I am not sold on usefulness of multiple ranges but if both of you feel it can be handy I'll do that, using Roger's proposal above. (Would it make sense to make val a union with, say, size or should the context be clear from flag's value?)

Before I do that though --- what was the conclusion on verbosity control?

-boris

#30 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 64 lines · Boris Ostrovsky
Quoted 62 lines · Jan Beulich
Quoted 47 lines · Roger Pau Monné
Quoted 46 lines · Jan Beulich
Quoted 45 lines · Roger Pau Monné
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Boris Ostrovsky
Quoted 13 lines · Roger Pau Monné
Quoted 8 lines · Boris Ostrovsky
Quoted 6 lines · Roger Pau Monné
Quoted 3 lines · Boris Ostrovsky

When toolstack updates MSR policy, this MSR offset (which is the last index in the hypervisor MSR range) is used to indicate hypervisor behavior when guest accesses an MSR which is not explicitly emulated.

It's kind of weird to use an MSR to store this. I assume this is done for migration reasons?

Not really. It just seemed to me that MSR policy is the logical place to do that. Because it *is* a policy of how to deal with such accesses, isn't it?

I agree that using the msr_policy seems like the most suitable place to convey this information between the toolstack and Xen. I wonder if it would be fine to have fields in msr_policy that don't directly translate into an MSR value?

Quoted 4 lines
Quoted 1 line

We have xen_msr_entry_t.flags that we can use when passing policy array back and forth. Then we can ignore xen_msr_entry_t.idx for that entry (although in earlier version of this series Jan preferred to use idx and leave flags alone).

Which, just to clarify, was not the least because of the flags field being per-entry, i.e. per MSR, while here we want a global indicator.

We could exploit a bit the xen_msr_entry_t structure and use it like:

typedef struct xen_msr_entry {
    uint32_t idx;
#define XEN_MSR_IGNORE (1u << 0)
    uint32_t flags;
    uint64_t val;
} xen_msr_entry_t;

Then use the idx and val fields to signal the start and size of the range to ignore accesses to when XEN_MSR_IGNORE is set in the flags field?

xen_msr_entry_t = {
    .idx = 0,
    .val = 0xffffffff,
    .flags = XEN_MSR_IGNORE,
};

Would be equivalent to ignoring accesses to the whole MSR range.

This would allow selectively selecting which MSRs to ignore, while not wasting a MSR itself to convey the information?

Quoted 7 lines
Quoted 4 lines

Hmm, yes, the added flexibility would be nice from an abstract pov (not sure how relevant it would be to Solaris'es issue). But my dislike of using a flag which is meaningless in ordinary entries remains, as was voiced against Boris'es original version.

I understand the flags field is meaningless for regular MSRs, but I don't see why it would be an issue to start using it for this specific case of registering ranges of ignored MSRs.

It's not an "issue", it is - as said - my dislike. However, the way it is in your proposal it is perhaps indeed not as bad as in Boris'es original one: The flag now designates the purpose of the entry, and the other two fields still have a meaning. Hence I was wrong to state that it's "meaningless" - it now is required to be clear for "ordinary" entries.

Quoted 5 lines
Quoted 1 line

In principle there could then also be multiple such entries / ranges.

TBH I am not sold on usefulness of multiple ranges but if both of you feel it can be handy I'll do that, using Roger's proposal above.

As indicated I really think an opinion from Andrew would be helpful.

Quoted 2 lines

(Would it make sense to make val a union with, say, size or should the context be clear from flag's value?)

I'd recommend against this, unless you were to also name the upper half of the 64-bit field so you can check that part to be zero. Plus unions are generally not nice to be introduced into public headers for already existing types.

Quoted 2 lines

Before I do that though --- what was the conclusion on verbosity control?

Not sure - afaict the conclusion was that we still don't really agree. Roger?

Jan

#31 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 6 lines · Jan Beulich
Quoted 2 lines · Boris Ostrovsky

Before I do that though --- what was the conclusion on verbosity control?

Not sure - afaict the conclusion was that we still don't really agree. Roger?

As I said on my reply to Boris, I would really like to have a more generic way of doing this kind of per-domain verbosity level, but I don't think it would be fair to ask Boris to implement this. Xen likely needs a way to print issues with MSRs accesses in 4.15 on a per-domain basis, so going for a MSR specific solution would be acceptable given the time frame. I think Boris made a proposal for a solution on another email, let me go look at that.

Thanks, Roger.

#32 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

In reply to Boris Ostrovsky
Quoted 64 lines · Boris Ostrovsky
Quoted 62 lines · Jan Beulich
Quoted 47 lines · Roger Pau Monné
Quoted 46 lines · Jan Beulich
Quoted 45 lines · Roger Pau Monné
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Boris Ostrovsky
Quoted 13 lines · Roger Pau Monné
Quoted 8 lines · Boris Ostrovsky
Quoted 6 lines · Roger Pau Monné
Quoted 3 lines · Boris Ostrovsky

When toolstack updates MSR policy, this MSR offset (which is the last index in the hypervisor MSR range) is used to indicate hypervisor behavior when guest accesses an MSR which is not explicitly emulated.

It's kind of weird to use an MSR to store this. I assume this is done for migration reasons?

Not really. It just seemed to me that MSR policy is the logical place to do that. Because it *is* a policy of how to deal with such accesses, isn't it?

I agree that using the msr_policy seems like the most suitable place to convey this information between the toolstack and Xen. I wonder if it would be fine to have fields in msr_policy that don't directly translate into an MSR value?

Quoted 4 lines
Quoted 1 line

We have xen_msr_entry_t.flags that we can use when passing policy array back and forth. Then we can ignore xen_msr_entry_t.idx for that entry (although in earlier version of this series Jan preferred to use idx and leave flags alone).

Which, just to clarify, was not the least because of the flags field being per-entry, i.e. per MSR, while here we want a global indicator.

We could exploit a bit the xen_msr_entry_t structure and use it like:

typedef struct xen_msr_entry {
    uint32_t idx;
#define XEN_MSR_IGNORE (1u << 0)
    uint32_t flags;
    uint64_t val;
} xen_msr_entry_t;

Then use the idx and val fields to signal the start and size of the range to ignore accesses to when XEN_MSR_IGNORE is set in the flags field?

xen_msr_entry_t = {
    .idx = 0,
    .val = 0xffffffff,
    .flags = XEN_MSR_IGNORE,
};

Would be equivalent to ignoring accesses to the whole MSR range.

This would allow selectively selecting which MSRs to ignore, while not wasting a MSR itself to convey the information?

Quoted 7 lines
Quoted 4 lines

Hmm, yes, the added flexibility would be nice from an abstract pov (not sure how relevant it would be to Solaris'es issue). But my dislike of using a flag which is meaningless in ordinary entries remains, as was voiced against Boris'es original version.

I understand the flags field is meaningless for regular MSRs, but I don't see why it would be an issue to start using it for this specific case of registering ranges of ignored MSRs.

It's not an "issue", it is - as said - my dislike. However, the way it is in your proposal it is perhaps indeed not as bad as in Boris'es original one: The flag now designates the purpose of the entry, and the other two fields still have a meaning. Hence I was wrong to state that it's "meaningless" - it now is required to be clear for "ordinary" entries.

Quoted 4 lines
Quoted 1 line

In principle there could then also be multiple such entries / ranges.

TBH I am not sold on usefulness of multiple ranges but if both of you feel it can be handy I'll do that, using Roger's proposal above. (Would it make sense to make val a union with, say, size or should the context be clear from flag's value?)

Doing an union with { val, size } would be fine for me, I'm also fine with not doing it though.

Quoted 2 lines

Before I do that though --- what was the conclusion on verbosity control?

Ideally I would like to find a way to have a more generic interface to change verbosity level on a per-guest basis, but I haven't looked at all about how to do that, neither I think it would be acceptable to put that burden on you.

Maybe we could introduce another flag to set whether ignored MSRs should be logged, as that would be easier to implement?

I think in that case we could enforce that either all ranges have the flag set or not, in order to prevent ending up tracking two different ranges of ignored MSRs, one that triggers a message and another one that doesn't.

Thanks, Roger.

#33 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 10 lines · Roger Pau Monné
Quoted 1 line · Boris Ostrovsky

Before I do that though --- what was the conclusion on verbosity control?

Ideally I would like to find a way to have a more generic interface to change verbosity level on a per-guest basis, but I haven't looked at all about how to do that, neither I think it would be acceptable to put that burden on you.

Maybe we could introduce another flag to set whether ignored MSRs should be logged, as that would be easier to implement?

Probably.

    msr_ignore=["verbose=<bool>", "<range>", "range>", ...]

-boris

Quoted 7 lines

I think in that case we could enforce that either all ranges have the flag set or not, in order to prevent ending up tracking two different ranges of ignored MSRs, one that triggers a message and another one that doesn't.

Thanks, Roger.

#34 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 10 lines · Boris Ostrovsky
Quoted 8 lines · Roger Pau Monné
Quoted 1 line · Boris Ostrovsky

Before I do that though --- what was the conclusion on verbosity control?

Ideally I would like to find a way to have a more generic interface to change verbosity level on a per-guest basis, but I haven't looked at all about how to do that, neither I think it would be acceptable to put that burden on you.

Quoted 8 lines
Quoted 2 lines

Maybe we could introduce another flag to set whether ignored MSRs should be logged, as that would be easier to implement?

Probably.

    msr_ignore=["verbose=<bool>", "<range>", "range>", ...]

I think just adding a new option will be easier to parse in xl rather than placing it in the list of MSR ranges:

msr_ignore=["<range>", "range>", ...]
msr_ignore_verbose=<boolean>

Seems easier to implement IMO.

Thanks, Roger.

#35 lore raw

Re: [PATCH v2 2/4] x86: Introduce MSR_UNHANDLED

Quoted 16 lines · Roger Pau Monné
Quoted 15 lines · Boris Ostrovsky
Quoted 10 lines · Roger Pau Monné
Quoted 1 line · Boris Ostrovsky

Before I do that though --- what was the conclusion on verbosity control?

Ideally I would like to find a way to have a more generic interface to change verbosity level on a per-guest basis, but I haven't looked at all about how to do that, neither I think it would be acceptable to put that burden on you.

Maybe we could introduce another flag to set whether ignored MSRs should be logged, as that would be easier to implement?

Probably.

Quoted 8 lines
Quoted 1 line

    msr_ignore=["verbose=<bool>", "<range>", "range>", ...]

I think just adding a new option will be easier to parse in xl rather than placing it in the list of MSR ranges:

msr_ignore=["<range>", "range>", ...]
msr_ignore_verbose=<boolean>

Seems easier to implement IMO.

I haven't looked at what parsing support is available in xl. If I don't find anything useful (and I give up quickly ;-) ) I can use separate options.

-boris

#36 lore raw

PATCH v2 3/4 x86: Allow non-faulting accesses to non-emulated MSRs if policy permits this

In reply to Boris Ostrovsky

Starting with commit 84e848fd7a16 ("x86/hvm: disallow access to unknown MSRs") accesses to unhandled MSRs result in #GP sent to the guest. This caused a regression for Solaris who tries to acccess MSR_RAPL_POWER_UNIT and (unlike, for example, Linux) does not catch exceptions when accessing MSRs that potentially may not be present.

Instead of special-casing RAPL registers we decide what to do when any non-emulated MSR is accessed based on ignore_msrs field of msr_policy.

---
Changes in v2:
* define x86_emul_guest_msr_access() and use it to determine whether emulated instruction is rd/wrmsr.
* Don't use ignore_msrs for MSR accesses that are not guest's rd/wrmsr.
* Clear @val for writes too in guest_unhandled_msr()

 xen/arch/x86/hvm/svm/svm.c             | 10 ++++------
 xen/arch/x86/hvm/vmx/vmx.c             | 10 ++++------
 xen/arch/x86/msr.c                     | 28 ++++++++++++++++++++++++++++
 xen/arch/x86/pv/emul-priv-op.c         | 10 ++++++----
 xen/arch/x86/x86_emulate/x86_emulate.h |  6 ++++++
 xen/include/asm-x86/msr.h              |  3 +++
 6 files changed, 51 insertions(+), 16 deletions(-)
xen/arch/x86/hvm/svm/svm.c +4 -6
index b819897a4a9f..7b59885b2619 100644
@@ -1965,8 +1965,8 @@ static int svm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
         break;
 
     default:
-        gdprintk(XENLOG_WARNING, "RDMSR 0x%08x unimplemented\n", msr);
-        goto gpf;
+        if ( guest_unhandled_msr(v, msr, msr_content, false, true) )
+            goto gpf;
     }
 
     HVM_DBG_LOG(DBG_LEVEL_MSR, "returns: ecx=%x, msr_value=%"PRIx64,
@@ -2151,10 +2151,8 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
         break;
 
     default:
-        gdprintk(XENLOG_WARNING,
-                 "WRMSR 0x%08x val 0x%016"PRIx64" unimplemented\n",
-                 msr, msr_content);
-        goto gpf;
+        if ( guest_unhandled_msr(v, msr, &msr_content, true, true) )
+            goto gpf;
     }
 
     return X86EMUL_OKAY;
xen/arch/x86/hvm/vmx/vmx.c +4 -6
index 2d4475ee3de2..87baca57d33f 100644
@@ -3017,8 +3017,8 @@ static int vmx_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
             break;
         }
 
-        gdprintk(XENLOG_WARNING, "RDMSR 0x%08x unimplemented\n", msr);
-        goto gp_fault;
+        if ( guest_unhandled_msr(curr, msr, msr_content, false, true) )
+            goto gp_fault;
     }
 
 done:
@@ -3319,10 +3319,8 @@ static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content)
              is_last_branch_msr(msr) )
             break;
 
-        gdprintk(XENLOG_WARNING,
-                 "WRMSR 0x%08x val 0x%016"PRIx64" unimplemented\n",
-                 msr, msr_content);
-        goto gp_fault;
+        if ( guest_unhandled_msr(v, msr, &msr_content, true, true) )
+            goto gp_fault;
     }
 
     return X86EMUL_OKAY;
xen/arch/x86/msr.c +28 -0
index 433d16c80728..a57d838f642b 100644
@@ -164,6 +164,34 @@ int init_vcpu_msr_policy(struct vcpu *v)
     return 0;
 }
 
+/* Returns true if policy requires #GP to the guest. */
+bool guest_unhandled_msr(const struct vcpu *v, uint32_t msr, uint64_t *val,
+                         bool is_write, bool is_guest_msr_access)
+{
+    u8 ignore_msrs = v->domain->arch.msr->ignore_msrs;
+
+    /*
+     * Accesses to unimplemented MSRs as part of emulation of instructions
+     * other than guest's RDMSR/WRMSR should never succeed.
+     */
+    if ( !is_guest_msr_access )
+        ignore_msrs = MSR_UNHANDLED_NEVER;
+
+    if ( unlikely(ignore_msrs != MSR_UNHANDLED_NEVER) )
+        *val = 0;
+
+    if ( likely(ignore_msrs != MSR_UNHANDLED_SILENT) )
+    {
+        if ( is_write )
+            gdprintk(XENLOG_WARNING, "WRMSR 0x%08x val 0x%016"PRIx64
+                    " unimplemented\n", msr, *val);
+        else
+            gdprintk(XENLOG_WARNING, "RDMSR 0x%08x unimplemented\n", msr);
+    }
+
+    return (ignore_msrs == MSR_UNHANDLED_NEVER);
+}
+
 int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val)
 {
     const struct vcpu *curr = current;
xen/arch/x86/pv/emul-priv-op.c +6 -4
index dbceed8a05fd..6b378dbe2239 100644
@@ -984,7 +984,9 @@ static int read_msr(unsigned int reg, uint64_t *val,
         }
         /* fall through */
     default:
-        gdprintk(XENLOG_WARNING, "RDMSR 0x%08x unimplemented\n", reg);
+        if ( !guest_unhandled_msr(curr, reg, val, false,
+                                  x86_emul_guest_msr_access(ctxt)) )
+            return X86EMUL_OKAY;
         break;
 
     normal:
@@ -1146,9 +1148,9 @@ static int write_msr(unsigned int reg, uint64_t val,
         }
         /* fall through */
     default:
-        gdprintk(XENLOG_WARNING,
-                 "WRMSR 0x%08x val 0x%016"PRIx64" unimplemented\n",
-                 reg, val);
+        if ( !guest_unhandled_msr(curr, reg, &val, true,
+                                  x86_emul_guest_msr_access(ctxt)) )
+            return X86EMUL_OKAY;
         break;
 
     invalid:
xen/arch/x86/x86_emulate/x86_emulate.h +6 -0
index d8fb3a990933..06e6b7479f37 100644
@@ -850,4 +850,10 @@ static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt)
     ctxt->event = (struct x86_event){};
 }
 
+static inline bool x86_emul_guest_msr_access(struct x86_emulate_ctxt *ctxt)
+{
+    return ctxt->opcode == X86EMUL_OPC(0x0f, 0x32) ||  /* RDMSR */
+           ctxt->opcode == X86EMUL_OPC(0x0f, 0x30);    /* WRMSR */
+}
+
 #endif /* __X86_EMULATE_H__ */
xen/include/asm-x86/msr.h +3 -0
index 16f95e734428..e7d69ad5bf29 100644
@@ -345,5 +345,8 @@ int init_vcpu_msr_policy(struct vcpu *v);
  */
 int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val);
 int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val);
+bool guest_unhandled_msr(const struct vcpu *v, uint32_t msr,
+                         uint64_t *val, bool is_write,
+                         bool is_guest_msr_access);
 
 #endif /* __ASM_MSR_H */
Signature · 1 line
1.8.3.1
#37 lore raw

Re: [PATCH v2 3/4] x86: Allow non-faulting accesses to non-emulated MSRs if policy permits this

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 169

Quoted 9 lines · Boris Ostrovsky
xen/arch/x86/msr.c +3 -0
@@ -164,6 +164,34 @@ int init_vcpu_msr_policy(struct vcpu *v)
     return 0;
 }
 
+/* Returns true if policy requires #GP to the guest. */
+bool guest_unhandled_msr(const struct vcpu *v, uint32_t msr, uint64_t *val,
+                         bool is_write, bool is_guest_msr_access)

Would you mind dropping the "_msr" infix from the last parameter's name? We're anyway aware we're talking about MSR accesses here, from the function name.

As a nit - while I'm okay with the uint64_t *, I think the uint32_t would better be unsigned int - see ./CODING_STYLE.

Finally, this being a policy function and policy being per- domain here, I think the first parameter wants to be const struct domain *.

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 171

Quoted 2 lines
xen/arch/x86/msr.c +2 -0
+{
+    u8 ignore_msrs = v->domain->arch.msr->ignore_msrs;

uint8_t please or, as per above, even better unsigned int.

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 178

Quoted 7 lines
xen/arch/x86/msr.c +7 -0
+
+    /*
+     * Accesses to unimplemented MSRs as part of emulation of instructions
+     * other than guest's RDMSR/WRMSR should never succeed.
+     */
+    if ( !is_guest_msr_access )
+        ignore_msrs = MSR_UNHANDLED_NEVER;

Wouldn't you better "return true" here? Such accesses also shouldn't be logged imo (albeit I agree that's a change from current behavior).

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 181

Quoted 2 lines
xen/arch/x86/msr.c +2 -0
+    if ( unlikely(ignore_msrs != MSR_UNHANDLED_NEVER) )
+        *val = 0;

I don't understand the conditional here, even more so with the respective changelog entry. In any event you don't want to clobber the value ahead of ...

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 187

Quoted 5 lines
xen/arch/x86/msr.c +5 -0
+    if ( likely(ignore_msrs != MSR_UNHANDLED_SILENT) )
+    {
+        if ( is_write )
+            gdprintk(XENLOG_WARNING, "WRMSR 0x%08x val 0x%016"PRIx64
+                    " unimplemented\n", msr, *val);

... logging it.

xen/arch/x86/x86_emulate/x86_emulate.h static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt) · line 853

Quoted 7 lines
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
@@ -850,4 +850,10 @@ static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt)
     ctxt->event = (struct x86_event){};
 }
 
+static inline bool x86_emul_guest_msr_access(struct x86_emulate_ctxt *ctxt)

The parameter wants to be pointer-to-const. In addition I wonder whether this wouldn't better be a sibling to x86_insn_is_cr_access() (without a "state" parameter, which would be unused and unavailable to the callers), which may end up finding further uses down the road.

xen/arch/x86/x86_emulate/x86_emulate.h static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt) · line 857

Quoted 4 lines
xen/arch/x86/x86_emulate/x86_emulate.h +4 -0
+{
+    return ctxt->opcode == X86EMUL_OPC(0x0f, 0x32) ||  /* RDMSR */
+           ctxt->opcode == X86EMUL_OPC(0x0f, 0x30);    /* WRMSR */
+}

Personally I'd prefer if this was a single comparison:

    return (ctxt->opcode | 2) == X86EMUL_OPC(0x0f, 0x32);

But maybe nowadays' compilers are capable of this
transformation?

I notice you use this function only from PV priv-op emulation. What about the call paths through hvmemul_{read,write}_msr()? (It's also questionable whether the write paths need this - the only MSR written outside of WRMSR emulation is MSR_SHADOW_GS_BASE, which can't possibly reach the "unhandled" logic anywhere. But maybe better to be future proof here in case new MSR writes appear in the emulator, down the road.)

Jan

#38 lore raw

Re: [PATCH v2 3/4] x86: Allow non-faulting accesses to non-emulated MSRs if policy permits this

Quoted 12 lines · Jan Beulich

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 178

Quoted 7 lines · Boris Ostrovsky
xen/arch/x86/msr.c +7 -0
+
+    /*
+     * Accesses to unimplemented MSRs as part of emulation of instructions
+     * other than guest's RDMSR/WRMSR should never succeed.
+     */
+    if ( !is_guest_msr_access )
+        ignore_msrs = MSR_UNHANDLED_NEVER;
Quoted 3 lines

Wouldn't you better "return true" here? Such accesses also shouldn't be logged imo (albeit I agree that's a change from current behavior).

Yes, that's why I didn't return here. We will be here in !is_guest_msr_access case most likely due to a bug in the emulator so I think we do want to see the error logged.

Quoted 8 lines

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 181

Quoted 3 lines
xen/arch/x86/msr.c +2 -0
+    if ( unlikely(ignore_msrs != MSR_UNHANDLED_NEVER) )
+        *val = 0;

I don't understand the conditional here, even more so with the respective changelog entry. In any event you don't want to clobber the value ahead of ...

Quoted 7 lines

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 187

Quoted 5 lines
xen/arch/x86/msr.c +5 -0
+    if ( likely(ignore_msrs != MSR_UNHANDLED_SILENT) )
+    {
+        if ( is_write )
+            gdprintk(XENLOG_WARNING, "WRMSR 0x%08x val 0x%016"PRIx64
+                    " unimplemented\n", msr, *val);

... logging it.

True. I dropped !is_write from v1 without considering this.

As far as the conditional --- dropping it too would be a behavior change.

Quoted 7 lines
Quoted 7 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -0
@@ -850,4 +850,10 @@ static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt)
     ctxt->event = (struct x86_event){};
 }
 
Quoted 7 lines

xen/arch/x86/x86_emulate/x86_emulate.h static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt) · line 853

Quoted 1 line
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
+static inline bool x86_emul_guest_msr_access(struct x86_emulate_ctxt *ctxt)

The parameter wants to be pointer-to-const. In addition I wonder whether this wouldn't better be a sibling to x86_insn_is_cr_access() (without a "state" parameter, which would be unused and unavailable to the callers), which may end up finding further uses down the road.

"Sibling" in terms of name (yes, it would be) or something else?

Quoted 8 lines

xen/arch/x86/x86_emulate/x86_emulate.h static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt) · line 857

Quoted 5 lines
xen/arch/x86/x86_emulate/x86_emulate.h +4 -0
+{
+    return ctxt->opcode == X86EMUL_OPC(0x0f, 0x32) ||  /* RDMSR */
+           ctxt->opcode == X86EMUL_OPC(0x0f, 0x30);    /* WRMSR */
+}

Personally I'd prefer if this was a single comparison:

Quoted 4 lines
    return (ctxt->opcode | 2) == X86EMUL_OPC(0x0f, 0x32);

But maybe nowadays' compilers are capable of this
transformation?

Here is what I've got (not an inline but shouldn't make much difference I'd think)

ffff82d040385960 <x86_emul_guest_msr_access_2>: # your code

ffff82d040385960:       8b 47 2c                mov    0x2c(%rdi),%eax
ffff82d040385963:       83 e0 fd                and    $0xfffffffd,%eax
ffff82d040385966:       3d 30 00 0f 00          cmp    $0xf0030,%eax
ffff82d04038596b:       0f 94 c0                sete   %al
ffff82d04038596e:       c3                      retq

ffff82d04038596f <x86_emul_guest_msr_access_1>: # my code

ffff82d04038596f:       8b 47 2c                mov    0x2c(%rdi),%eax
ffff82d040385972:       83 c8 02                or     $0x2,%eax
ffff82d040385975:       3d 32 00 0f 00          cmp    $0xf0032,%eax
ffff82d04038597a:       0f 94 c0                sete   %al
ffff82d04038597d:       c3                      retq

So it's a wash in terms of generated code.

Quoted 8 lines

I notice you use this function only from PV priv-op emulation. What about the call paths through hvmemul_{read,write}_msr()? (It's also questionable whether the write paths need this - the only MSR written outside of WRMSR emulation is MSR_SHADOW_GS_BASE, which can't possibly reach the "unhandled" logic anywhere. But maybe better to be future proof here in case new MSR writes appear in the emulator, down the road.)

Won't we end up in hvm_funcs.msr_write_intercept ops which do call it?

-boris

#39 lore raw

Re: [PATCH v2 3/4] x86: Allow non-faulting accesses to non-emulated MSRs if policy permits this

Quoted 10 lines · Boris Ostrovsky
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Boris Ostrovsky
xen/arch/x86/msr.c +7 -0
+
+    /*
+     * Accesses to unimplemented MSRs as part of emulation of instructions
+     * other than guest's RDMSR/WRMSR should never succeed.
+     */
+    if ( !is_guest_msr_access )
+        ignore_msrs = MSR_UNHANDLED_NEVER;
Quoted 6 lines
Quoted 3 lines

Wouldn't you better "return true" here? Such accesses also shouldn't be logged imo (albeit I agree that's a change from current behavior).

Yes, that's why I didn't return here. We will be here in !is_guest_msr_access case most likely due to a bug in the emulator so I think we do want to see the error logged.

Why "most likely"?

Quoted 13 lines
Quoted 13 lines

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 181

Quoted 2 lines
xen/arch/x86/msr.c +2 -0
+    if ( unlikely(ignore_msrs != MSR_UNHANDLED_NEVER) )
+        *val = 0;

I don't understand the conditional here, even more so with the respective changelog entry. In any event you don't want to clobber the value ahead of ...

Quoted 5 lines
xen/arch/x86/msr.c +5 -0
+    if ( likely(ignore_msrs != MSR_UNHANDLED_SILENT) )
+    {
+        if ( is_write )
+            gdprintk(XENLOG_WARNING, "WRMSR 0x%08x val 0x%016"PRIx64
+                    " unimplemented\n", msr, *val);
Quoted 6 lines
Quoted 1 line

... logging it.

True. I dropped !is_write from v1 without considering this.

As far as the conditional --- dropping it too would be a behavior change.

Albeit an intentional one then? Plus I think I have trouble seeing what behavior it would be that would change.

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -0
@@ -850,4 +850,10 @@ static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt)
     ctxt->event = (struct x86_event){};
 }
 
Quoted 7 lines

xen/arch/x86/x86_emulate/x86_emulate.h static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt) · line 853

Quoted 1 line
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
+static inline bool x86_emul_guest_msr_access(struct x86_emulate_ctxt *ctxt)

The parameter wants to be pointer-to-const. In addition I wonder whether this wouldn't better be a sibling to x86_insn_is_cr_access() (without a "state" parameter, which would be unused and unavailable to the callers), which may end up finding further uses down the road.

Quoted 1 line

"Sibling" in terms of name (yes, it would be) or something else?

Name and (possible) purpose - a validate hook could want to make use of this, for example.

Quoted 22 lines
Quoted 11 lines

xen/arch/x86/x86_emulate/x86_emulate.h static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt) · line 857

Quoted 4 lines
xen/arch/x86/x86_emulate/x86_emulate.h +4 -0
+{
+    return ctxt->opcode == X86EMUL_OPC(0x0f, 0x32) ||  /* RDMSR */
+           ctxt->opcode == X86EMUL_OPC(0x0f, 0x30);    /* WRMSR */
+}

Personally I'd prefer if this was a single comparison:

    return (ctxt->opcode | 2) == X86EMUL_OPC(0x0f, 0x32);

But maybe nowadays' compilers are capable of this
transformation?

Here is what I've got (not an inline but shouldn't make much difference I'd think)

ffff82d040385960 <x86_emul_guest_msr_access_2>: # your code

ffff82d040385960:       8b 47 2c                mov    0x2c(%rdi),%eax
ffff82d040385963:       83 e0 fd                and    $0xfffffffd,%eax
ffff82d040385966:       3d 30 00 0f 00          cmp    $0xf0030,%eax
ffff82d04038596b:       0f 94 c0                sete   %al
ffff82d04038596e:       c3                      retq

ffff82d04038596f <x86_emul_guest_msr_access_1>: # my code

Quoted 8 lines
ffff82d04038596f:       8b 47 2c                mov    0x2c(%rdi),%eax
ffff82d040385972:       83 c8 02                or     $0x2,%eax
ffff82d040385975:       3d 32 00 0f 00          cmp    $0xf0032,%eax
ffff82d04038597a:       0f 94 c0                sete   %al
ffff82d04038597d:       c3                      retq

So it's a wash in terms of generated code.

True, albeit I guess you got "your code" and "my code" the wrong way round, as I don't expect the compiler to translate | into "and".

Quoted 10 lines
Quoted 7 lines

I notice you use this function only from PV priv-op emulation. What about the call paths through hvmemul_{read,write}_msr()? (It's also questionable whether the write paths need this - the only MSR written outside of WRMSR emulation is MSR_SHADOW_GS_BASE, which can't possibly reach the "unhandled" logic anywhere. But maybe better to be future proof here in case new MSR writes appear in the emulator, down the road.)

Won't we end up in hvm_funcs.msr_write_intercept ops which do call it?

Of course we will - the boolean will very likely need propagating (a possible alternative being a per-vCPU flag indicating "in emulator").

Jan

#40 lore raw

Re: [PATCH v2 3/4] x86: Allow non-faulting accesses to non-emulated MSRs if policy permits this

Quoted 11 lines · Jan Beulich
Quoted 10 lines · Boris Ostrovsky
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Boris Ostrovsky
xen/arch/x86/msr.c +7 -0
+
+    /*
+     * Accesses to unimplemented MSRs as part of emulation of instructions
+     * other than guest's RDMSR/WRMSR should never succeed.
+     */
+    if ( !is_guest_msr_access )
+        ignore_msrs = MSR_UNHANDLED_NEVER;
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Wouldn't you better "return true" here? Such accesses also shouldn't be logged imo (albeit I agree that's a change from current behavior).

Yes, that's why I didn't return here. We will be here in !is_guest_msr_access case most likely due to a bug in the emulator so I think we do want to see the error logged.

Why "most likely"?

OK, definitely ;-) But I still think logging these accesses would be helpful.

Quoted 17 lines
Quoted 17 lines
Quoted 15 lines

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 181

Quoted 3 lines
xen/arch/x86/msr.c +2 -0
+    if ( unlikely(ignore_msrs != MSR_UNHANDLED_NEVER) )
+        *val = 0;

I don't understand the conditional here, even more so with the respective changelog entry. In any event you don't want to clobber the value ahead of ...

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 187

Quoted 5 lines
xen/arch/x86/msr.c +5 -0
+    if ( likely(ignore_msrs != MSR_UNHANDLED_SILENT) )
+    {
+        if ( is_write )
+            gdprintk(XENLOG_WARNING, "WRMSR 0x%08x val 0x%016"PRIx64
+                    " unimplemented\n", msr, *val);

... logging it.

Quoted 6 lines
Quoted 3 lines

True. I dropped !is_write from v1 without considering this.

As far as the conditional --- dropping it too would be a behavior change.

Albeit an intentional one then? Plus I think I have trouble seeing what behavior it would be that would change.

Currently callers of, say, read_msr() don't expect the argument that they pass in to change. Granted, they shouldn't (and AFAICS don't) look at it but it's a change nonetheless.

Quoted 16 lines
Quoted 16 lines
Quoted 14 lines

xen/arch/x86/x86_emulate/x86_emulate.h static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt) · line 853

Quoted 8 lines
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
@@ -850,4 +850,10 @@ static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt)
     ctxt->event = (struct x86_event){};
 }
 
+static inline bool x86_emul_guest_msr_access(struct x86_emulate_ctxt *ctxt)

The parameter wants to be pointer-to-const. In addition I wonder whether this wouldn't better be a sibling to x86_insn_is_cr_access() (without a "state" parameter, which would be unused and unavailable to the callers), which may end up finding further uses down the road.

Quoted 4 lines
Quoted 1 line

"Sibling" in terms of name (yes, it would be) or something else?

Name and (possible) purpose - a validate hook could want to make use of this, for example.

A validate hook?

Quoted 30 lines
Quoted 30 lines
Quoted 8 lines

xen/arch/x86/x86_emulate/x86_emulate.h static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt) · line 857

Quoted 5 lines
xen/arch/x86/x86_emulate/x86_emulate.h +4 -0
+{
+    return ctxt->opcode == X86EMUL_OPC(0x0f, 0x32) ||  /* RDMSR */
+           ctxt->opcode == X86EMUL_OPC(0x0f, 0x30);    /* WRMSR */
+}

Personally I'd prefer if this was a single comparison:

Quoted 4 lines
    return (ctxt->opcode | 2) == X86EMUL_OPC(0x0f, 0x32);

But maybe nowadays' compilers are capable of this
transformation?

Here is what I've got (not an inline but shouldn't make much difference I'd think)

ffff82d040385960 <x86_emul_guest_msr_access_2>: # your code

ffff82d040385960:       8b 47 2c                mov    0x2c(%rdi),%eax
ffff82d040385963:       83 e0 fd                and    $0xfffffffd,%eax
ffff82d040385966:       3d 30 00 0f 00          cmp    $0xf0030,%eax
ffff82d04038596b:       0f 94 c0                sete   %al
ffff82d04038596e:       c3                      retq

ffff82d04038596f <x86_emul_guest_msr_access_1>: # my code

ffff82d04038596f:       8b 47 2c                mov    0x2c(%rdi),%eax
ffff82d040385972:       83 c8 02                or     $0x2,%eax
ffff82d040385975:       3d 32 00 0f 00          cmp    $0xf0032,%eax
ffff82d04038597a:       0f 94 c0                sete   %al
ffff82d04038597d:       c3                      retq
Quoted 5 lines
Quoted 1 line

So it's a wash in terms of generated code.

True, albeit I guess you got "your code" and "my code" the wrong way round, as I don't expect the compiler to translate | into "and".

Yes, looks like I did switch them.

Quoted 10 lines
Quoted 10 lines
Quoted 8 lines

I notice you use this function only from PV priv-op emulation. What about the call paths through hvmemul_{read,write}_msr()? (It's also questionable whether the write paths need this - the only MSR written outside of WRMSR emulation is MSR_SHADOW_GS_BASE, which can't possibly reach the "unhandled" logic anywhere. But maybe better to be future proof here in case new MSR writes appear in the emulator, down the road.)

Quoted 5 lines
Quoted 1 line

Won't we end up in hvm_funcs.msr_write_intercept ops which do call it?

Of course we will - the boolean will very likely need propagating (a possible alternative being a per-vCPU flag indicating "in emulator").

Oh, I see what you mean. By per-vcpu flag you mean arch_vcpu field I assume?

-boris

#41 lore raw

Re: [PATCH v2 3/4] x86: Allow non-faulting accesses to non-emulated MSRs if policy permits this

Quoted 17 lines · Boris Ostrovsky
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Boris Ostrovsky
Quoted 12 lines · Jan Beulich

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 178

Quoted 7 lines · Boris Ostrovsky
xen/arch/x86/msr.c +7 -0
+
+    /*
+     * Accesses to unimplemented MSRs as part of emulation of instructions
+     * other than guest's RDMSR/WRMSR should never succeed.
+     */
+    if ( !is_guest_msr_access )
+        ignore_msrs = MSR_UNHANDLED_NEVER;

Wouldn't you better "return true" here? Such accesses also shouldn't be logged imo (albeit I agree that's a change from current behavior).

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Yes, that's why I didn't return here. We will be here in !is_guest_msr_access case most likely due to a bug in the emulator so I think we do want to see the error logged.

Why "most likely"?

OK, definitely ;-)

Oops - I was thinking the other way around, considering such to possibly be legitimate. It just so happens that curently we have no such path.

Quoted 1 line

But I still think logging these accesses would be helpful.

Because of the above I continue to question this.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 7 lines

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 181

Quoted 2 lines
xen/arch/x86/msr.c +2 -0
+    if ( unlikely(ignore_msrs != MSR_UNHANDLED_NEVER) )
+        *val = 0;

I don't understand the conditional here, even more so with the respective changelog entry. In any event you don't want to clobber the value ahead of ...

Quoted 7 lines

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 187

Quoted 5 lines
xen/arch/x86/msr.c +5 -0
+    if ( likely(ignore_msrs != MSR_UNHANDLED_SILENT) )
+    {
+        if ( is_write )
+            gdprintk(XENLOG_WARNING, "WRMSR 0x%08x val 0x%016"PRIx64
+                    " unimplemented\n", msr, *val);

... logging it.

True. I dropped !is_write from v1 without considering this.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

As far as the conditional --- dropping it too would be a behavior change.

Albeit an intentional one then? Plus I think I have trouble seeing what behavior it would be that would change.

Currently callers of, say, read_msr() don't expect the argument that they pass in to change. Granted, they shouldn't (and AFAICS don't) look at it but it's a change nonetheless.

Hmm, I'm confused: The purpose of read_msr() is to change the value pointed at by the passed in argument. And for write_msr() the users of the hook pass the argument by value, i.e. wouldn't observe the changed value (it would only possibly be intermediate layers which might observe the change, but those ought to not care).

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 13 lines

xen/arch/x86/x86_emulate/x86_emulate.h static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt) · line 853

Quoted 7 lines
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
@@ -850,4 +850,10 @@ static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt)
     ctxt->event = (struct x86_event){};
 }
 
+static inline bool x86_emul_guest_msr_access(struct x86_emulate_ctxt *ctxt)

The parameter wants to be pointer-to-const. In addition I wonder whether this wouldn't better be a sibling to x86_insn_is_cr_access() (without a "state" parameter, which would be unused and unavailable to the callers), which may end up finding further uses down the road.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

"Sibling" in terms of name (yes, it would be) or something else?

Name and (possible) purpose - a validate hook could want to make use of this, for example.

A validate hook?

Quoting from struct x86_emulate_ops:

    /*
     * validate: Post-decode, pre-emulate hook to allow caller controlled
     * filtering.
     */
    int (*validate)(
        const struct x86_emulate_state *state,
        struct x86_emulate_ctxt *ctxt);

Granted to be directly usable the function would need to have a "state" parameter. As that's unused, having it have one and passing NULL in your case might be acceptable. But I also could see arguments towards this not being a good idea.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 7 lines

I notice you use this function only from PV priv-op emulation. What about the call paths through hvmemul_{read,write}_msr()? (It's also questionable whether the write paths need this - the only MSR written outside of WRMSR emulation is MSR_SHADOW_GS_BASE, which can't possibly reach the "unhandled" logic anywhere. But maybe better to be future proof here in case new MSR writes appear in the emulator, down the road.)

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Won't we end up in hvm_funcs.msr_write_intercept ops which do call it?

Of course we will - the boolean will very likely need propagating (a possible alternative being a per-vCPU flag indicating "in emulator").

Oh, I see what you mean. By per-vcpu flag you mean arch_vcpu field I assume?

Yes, a boolean in one of the arch-specific per-vCPU structs. Whether that's arch_vcpu or perhaps something HVM specific is another question.

Jan

#42 lore raw

Re: [PATCH v2 3/4] x86: Allow non-faulting accesses to non-emulated MSRs if policy permits this

Quoted 20 lines · Jan Beulich
Quoted 19 lines · Boris Ostrovsky
Quoted 18 lines · Jan Beulich
Quoted 16 lines · Boris Ostrovsky
Quoted 9 lines · Jan Beulich

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 178

Quoted 7 lines · Boris Ostrovsky
xen/arch/x86/msr.c +7 -0
+
+    /*
+     * Accesses to unimplemented MSRs as part of emulation of instructions
+     * other than guest's RDMSR/WRMSR should never succeed.
+     */
+    if ( !is_guest_msr_access )
+        ignore_msrs = MSR_UNHANDLED_NEVER;
Quoted 3 lines

Wouldn't you better "return true" here? Such accesses also shouldn't be logged imo (albeit I agree that's a change from current behavior).

Yes, that's why I didn't return here. We will be here in !is_guest_msr_access case most likely due to a bug in the emulator so I think we do want to see the error logged.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Why "most likely"?

OK, definitely ;-)

Oops - I was thinking the other way around, considering such to possibly be legitimate. It just so happens that curently we have no such path.

I was imagining a case where we'd add have another "ops->read_msr(_regs.ecx, ...)" in the emulator and some values of ecx may not be tested. (Or even passing an explicit index that is not handled, although that is highly unlikely to pass code review).

Yes, these cases do not currently exist.

Quoted 25 lines
Quoted 2 lines

But I still think logging these accesses would be helpful.

Because of the above I continue to question this.

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 7 lines

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 181

Quoted 2 lines
xen/arch/x86/msr.c +2 -0
+    if ( unlikely(ignore_msrs != MSR_UNHANDLED_NEVER) )
+        *val = 0;

I don't understand the conditional here, even more so with the respective changelog entry. In any event you don't want to clobber the value ahead of ...

Quoted 7 lines

xen/arch/x86/msr.c int init_vcpu_msr_policy(struct vcpu *v) · line 187

Quoted 5 lines
xen/arch/x86/msr.c +5 -0
+    if ( likely(ignore_msrs != MSR_UNHANDLED_SILENT) )
+    {
+        if ( is_write )
+            gdprintk(XENLOG_WARNING, "WRMSR 0x%08x val 0x%016"PRIx64
+                    " unimplemented\n", msr, *val);

... logging it.

True. I dropped !is_write from v1 without considering this.

As far as the conditional --- dropping it too would be a behavior change.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Albeit an intentional one then? Plus I think I have trouble seeing what behavior it would be that would change.

Currently callers of, say, read_msr() don't expect the argument that they pass in to change. Granted, they shouldn't (and AFAICS don't) look at it but it's a change nonetheless.

Hmm, I'm confused: The purpose of read_msr() is to change the value pointed at by the passed in argument.

Only if MSR exists/handled. We add parameters that allow it to be set to zero for unhandled case but default (which is existing behavior, i.e. MSR_UNHANDLED_NEVER) would leave the (pointed to) argument unchanged.

Quoted 5 lines

And for write_msr()
the users of the hook pass the argument by value, i.e. wouldn't observe the changed value (it would only possibly be intermediate layers which might observe the change, but those ought to not care).

Yes, this would only be relevant to reads but since I dropped is_write check the conditional covers both reads and writes.

In any case, this (and the one above) are minor issues and I don't mind making the change.

Quoted 33 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 14 lines

xen/arch/x86/x86_emulate/x86_emulate.h static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt) · line 853

Quoted 8 lines
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
@@ -850,4 +850,10 @@ static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt)
     ctxt->event = (struct x86_event){};
 }
 
+static inline bool x86_emul_guest_msr_access(struct x86_emulate_ctxt *ctxt)

The parameter wants to be pointer-to-const. In addition I wonder whether this wouldn't better be a sibling to x86_insn_is_cr_access() (without a "state" parameter, which would be unused and unavailable to the callers), which may end up finding further uses down the road.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

"Sibling" in terms of name (yes, it would be) or something else?

Name and (possible) purpose - a validate hook could want to make use of this, for example.

A validate hook?

Quoting from struct x86_emulate_ops:

    /*
     * validate: Post-decode, pre-emulate hook to allow caller controlled
     * filtering.
     */
    int (*validate)(
        const struct x86_emulate_state *state,
        struct x86_emulate_ctxt *ctxt);
Quoted 4 lines

Granted to be directly usable the function would need to have a "state" parameter. As that's unused, having it have one and passing NULL in your case might be acceptable. But I also could see arguments towards this not being a good idea.

I see. Where would we need to call this hook though? We are never directly emulating MSR access (compared to, for example, CR accesses where x86_insn_is_cr_access is used). And for PV we already validate it in emul-priv-op.c():validate().

Quoted 17 lines
Quoted 17 lines
Quoted 15 lines
Quoted 11 lines
Quoted 8 lines

I notice you use this function only from PV priv-op emulation. What about the call paths through hvmemul_{read,write}_msr()? (It's also questionable whether the write paths need this - the only MSR written outside of WRMSR emulation is MSR_SHADOW_GS_BASE, which can't possibly reach the "unhandled" logic anywhere. But maybe better to be future proof here in case new MSR writes appear in the emulator, down the road.)

Won't we end up in hvm_funcs.msr_write_intercept ops which do call it?

Of course we will - the boolean will very likely need propagating (a possible alternative being a per-vCPU flag indicating "in emulator").

Quoted 5 lines
Quoted 1 line

Oh, I see what you mean. By per-vcpu flag you mean arch_vcpu field I assume?

Yes, a boolean in one of the arch-specific per-vCPU structs. Whether that's arch_vcpu or perhaps something HVM specific is another question.

Currently we only need this for HVM but using it for both will avoid the need to check guest type.

-boris

#43 lore raw

Re: [PATCH v2 3/4] x86: Allow non-faulting accesses to non-emulated MSRs if policy permits this

Quoted 26 lines · Boris Ostrovsky
Quoted 20 lines · Jan Beulich
Quoted 8 lines · Boris Ostrovsky
Quoted 5 lines · Jan Beulich
Quoted 1 line · Boris Ostrovsky

"Sibling" in terms of name (yes, it would be) or something else?

Name and (possible) purpose - a validate hook could want to make use of this, for example.

A validate hook?

Quoting from struct x86_emulate_ops:

    /*
     * validate: Post-decode, pre-emulate hook to allow caller controlled
     * filtering.
     */
    int (*validate)(
        const struct x86_emulate_state *state,
        struct x86_emulate_ctxt *ctxt);
Quoted 4 lines

Granted to be directly usable the function would need to have a "state" parameter. As that's unused, having it have one and passing NULL in your case might be acceptable. But I also could see arguments towards this not being a good idea.

Quoted 4 lines

I see. Where would we need to call this hook though? We are never directly emulating MSR access (compared to, for example, CR accesses where x86_insn_is_cr_access is used). And for PV we already validate it in emul-priv-op.c():validate().

If you look at some of the functions used for this hook, you may realize that what your function does could also fit a hypothetical future hook. Hence I was suggesting to make the new function suitable for such use right away (and in particular fit their naming scheme). But it looks like this has ended up more confusing than anything else, so never mind.

Jan

#44 lore raw

Re: [PATCH v2 3/4] x86: Allow non-faulting accesses to non-emulated MSRs if policy permits this

In reply to Boris Ostrovsky
Quoted 69 lines · Boris Ostrovsky

Starting with commit 84e848fd7a16 ("x86/hvm: disallow access to unknown MSRs") accesses to unhandled MSRs result in #GP sent to the guest. This caused a regression for Solaris who tries to acccess MSR_RAPL_POWER_UNIT and (unlike, for example, Linux) does not catch exceptions when accessing MSRs that potentially may not be present.

Instead of special-casing RAPL registers we decide what to do when any non-emulated MSR is accessed based on ignore_msrs field of msr_policy.

---
Changes in v2:
* define x86_emul_guest_msr_access() and use it to determine whether emulated instruction is rd/wrmsr.
* Don't use ignore_msrs for MSR accesses that are not guest's rd/wrmsr.
* Clear @val for writes too in guest_unhandled_msr()

 xen/arch/x86/hvm/svm/svm.c             | 10 ++++------
 xen/arch/x86/hvm/vmx/vmx.c             | 10 ++++------
 xen/arch/x86/msr.c                     | 28 ++++++++++++++++++++++++++++
 xen/arch/x86/pv/emul-priv-op.c         | 10 ++++++----
 xen/arch/x86/x86_emulate/x86_emulate.h |  6 ++++++
 xen/include/asm-x86/msr.h              |  3 +++
 6 files changed, 51 insertions(+), 16 deletions(-)
xen/arch/x86/hvm/svm/svm.c +4 -6
index b819897a4a9f..7b59885b2619 100644
@@ -1965,8 +1965,8 @@ static int svm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
         break;
 
     default:
-        gdprintk(XENLOG_WARNING, "RDMSR 0x%08x unimplemented\n", msr);
-        goto gpf;
+        if ( guest_unhandled_msr(v, msr, msr_content, false, true) )
+            goto gpf;
     }
 
     HVM_DBG_LOG(DBG_LEVEL_MSR, "returns: ecx=%x, msr_value=%"PRIx64,
@@ -2151,10 +2151,8 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
         break;
 
     default:
-        gdprintk(XENLOG_WARNING,
-                 "WRMSR 0x%08x val 0x%016"PRIx64" unimplemented\n",
-                 msr, msr_content);
-        goto gpf;
+        if ( guest_unhandled_msr(v, msr, &msr_content, true, true) )
+            goto gpf;
     }
 
     return X86EMUL_OKAY;
xen/arch/x86/hvm/vmx/vmx.c +2 -2
index 2d4475ee3de2..87baca57d33f 100644
@@ -3017,8 +3017,8 @@ static int vmx_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
             break;
         }
 
-        gdprintk(XENLOG_WARNING, "RDMSR 0x%08x unimplemented\n", msr);
-        goto gp_fault;
+        if ( guest_unhandled_msr(curr, msr, msr_content, false, true) )
+            goto gp_fault;
     }
 
 done:

xen/arch/x86/hvm/vmx/vmx.c static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 3324

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -4
@@ -3319,10 +3319,8 @@ static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content)
              is_last_branch_msr(msr) )
             break;
 
-        gdprintk(XENLOG_WARNING,
-                 "WRMSR 0x%08x val 0x%016"PRIx64" unimplemented\n",
-                 msr, msr_content);
-        goto gp_fault;
+        if ( guest_unhandled_msr(v, msr, &msr_content, true, true) )
+            goto gp_fault;
     }

I think this could be done in hvm_msr_read_intercept instead of having to call guest_unhandled_msr from each vendor specific handler?

Oh, I see, that's likely done to differentiate between guest MSR accesses and emulator ones? I'm not sure we really need to make a difference between guests MSR accesses and emulator ones, surely in the past they would be treated equally?

Thanks, Roger.

#45 lore raw

Re: [PATCH v2 3/4] x86: Allow non-faulting accesses to non-emulated MSRs if policy permits this

Quoted 29 lines · Roger Pau Monné
Quoted 14 lines · Boris Ostrovsky
xen/arch/x86/hvm/vmx/vmx.c +2 -2
@@ -3017,8 +3017,8 @@ static int vmx_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
             break;
         }
 
-        gdprintk(XENLOG_WARNING, "RDMSR 0x%08x unimplemented\n", msr);
-        goto gp_fault;
+        if ( guest_unhandled_msr(curr, msr, msr_content, false, true) )
+            goto gp_fault;
     }
 
 done:

xen/arch/x86/hvm/vmx/vmx.c static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 3324

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -4
@@ -3319,10 +3319,8 @@ static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content)
              is_last_branch_msr(msr) )
             break;
 
-        gdprintk(XENLOG_WARNING,
-                 "WRMSR 0x%08x val 0x%016"PRIx64" unimplemented\n",
-                 msr, msr_content);
-        goto gp_fault;
+        if ( guest_unhandled_msr(v, msr, &msr_content, true, true) )
+            goto gp_fault;
     }

I think this could be done in hvm_msr_read_intercept instead of having to call guest_unhandled_msr from each vendor specific handler?

Quoted 4 lines

Oh, I see, that's likely done to differentiate between guest MSR accesses and emulator ones? I'm not sure we really need to make a difference between guests MSR accesses and emulator ones, surely in the past they would be treated equally?

We did discuss this before. Even if they were treated the same in the past, that's not correct, and hence we shouldn't suppress the distinction going forward. A guest explicitly asking to access an MSR (via RDMSR/WRMSR) is entirely different from the emulator perhaps just probing an MSR, falling back to some default behavior if it's unavailable.

Jan

#46 lore raw

Re: [PATCH v2 3/4] x86: Allow non-faulting accesses to non-emulated MSRs if policy permits this

Quoted 35 lines · Jan Beulich
Quoted 29 lines · Roger Pau Monné
Quoted 14 lines · Boris Ostrovsky
xen/arch/x86/hvm/vmx/vmx.c +2 -2
@@ -3017,8 +3017,8 @@ static int vmx_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
             break;
         }
 
-        gdprintk(XENLOG_WARNING, "RDMSR 0x%08x unimplemented\n", msr);
-        goto gp_fault;
+        if ( guest_unhandled_msr(curr, msr, msr_content, false, true) )
+            goto gp_fault;
     }
 
 done:

xen/arch/x86/hvm/vmx/vmx.c static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 3324

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -4
@@ -3319,10 +3319,8 @@ static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content)
              is_last_branch_msr(msr) )
             break;
 
-        gdprintk(XENLOG_WARNING,
-                 "WRMSR 0x%08x val 0x%016"PRIx64" unimplemented\n",
-                 msr, msr_content);
-        goto gp_fault;
+        if ( guest_unhandled_msr(v, msr, &msr_content, true, true) )
+            goto gp_fault;
     }

I think this could be done in hvm_msr_read_intercept instead of having to call guest_unhandled_msr from each vendor specific handler?

Quoted 4 lines

Oh, I see, that's likely done to differentiate between guest MSR accesses and emulator ones? I'm not sure we really need to make a difference between guests MSR accesses and emulator ones, surely in the past they would be treated equally?

Quoted 6 lines

We did discuss this before. Even if they were treated the same in the past, that's not correct, and hence we shouldn't suppress the distinction going forward. A guest explicitly asking to access an MSR (via RDMSR/WRMSR) is entirely different from the emulator perhaps just probing an MSR, falling back to some default behavior if it's unavailable.

Ack, then placing the calls to guest_unhandled_msr in vendor code seems like the best option.

Thanks, Roger.

#47 lore raw

PATCH v2 4/4 tools/libs: Apply MSR policy to a guest

In reply to Boris Ostrovsky

When creating a guest, if ignore_msrs option has been specified, apply it to guest's MSR policy.

---
 tools/include/xenctrl.h           |   2 +
 tools/libs/guest/Makefile         |   1 +
 tools/libs/guest/xg_msrs_x86.c    | 110 ++++++++++++++++++++++++++++++++++++++
 tools/libs/light/libxl_dom.c      |   5 +-
 tools/libs/light/libxl_internal.h |   2 +
 tools/libs/light/libxl_x86.c      |   7 +++
 6 files changed, 125 insertions(+), 2 deletions(-)
 create mode 100644 tools/libs/guest/xg_msrs_x86.c
tools/include/xenctrl.h +2 -0
index 3796425e1eca..1d6a38e73dcf 100644
@@ -1835,6 +1835,8 @@ int xc_cpuid_apply_policy(xc_interface *xch,
                           const uint32_t *featureset,
                           unsigned int nr_features, bool pae, bool itsc,
                           bool nested_virt, const struct xc_xend_cpuid *xend);
+int xc_msr_apply_policy(xc_interface *xch, uint32_t domid,
+                        unsigned int ignore_msr);
 int xc_mca_op(xc_interface *xch, struct xen_mc *mc);
 int xc_mca_op_inject_v2(xc_interface *xch, unsigned int flags,
                         xc_cpumap_t cpumap, unsigned int nr_cpus);
tools/libs/guest/Makefile +1 -0
index 1c729040b337..452155ea0385 100644
@@ -56,6 +56,7 @@ SRCS-y                 += xg_dom_compat_linux.c
 
 SRCS-$(CONFIG_X86)     += xg_dom_x86.c
 SRCS-$(CONFIG_X86)     += xg_cpuid_x86.c
+SRCS-$(CONFIG_X86)     += xg_msrs_x86.c
 SRCS-$(CONFIG_ARM)     += xg_dom_arm.c
 
 ifeq ($(CONFIG_LIBXC_MINIOS),y)
tools/libs/guest/xg_msrs_x86.c new file +110 -0

110 lines not on this page · raw message

tools/libs/light/libxl_dom.c +3 -2
index 19168572fd3e..1f2abf6679d7 100644
@@ -383,9 +383,10 @@ int libxl__build_pre(libxl__gc *gc, uint32_t domid,
     /* Construct a CPUID policy, but only for brand new domains.  Domains
      * being migrated-in/restored have CPUID handled during the
      * static_data_done() callback. */
-    if (!state->restore)
+    if (!state->restore) {
         libxl__cpuid_legacy(ctx, domid, false, info);
-
+        libxl__msr_policy(ctx, domid, info);
+    }
     return rc;
 }
 
tools/libs/light/libxl_internal.h +2 -0
index c79523ba9248..4f369e6a6f14 100644
@@ -2054,6 +2054,8 @@ _hidden char *libxl__object_to_json(libxl_ctx *ctx, const char *type,
 
 _hidden void libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool retore,
                                  libxl_domain_build_info *info);
+_hidden void libxl__msr_policy(libxl_ctx *ctx, uint32_t domid,
+                               libxl_domain_build_info *info);
 
 /* Calls poll() again - useful to check whether a signaled condition
  * is still true.  Cannot fail.  Returns currently-true revents. */
tools/libs/light/libxl_x86.c +7 -0
index 86d272999d67..92ec1da77139 100644
@@ -1,5 +1,6 @@
 #include "libxl_internal.h"
 #include "libxl_arch.h"
+#include "xen/lib/x86/msr.h"
 
 int libxl__arch_domain_prepare_config(libxl__gc *gc,
                                       libxl_domain_config *d_config,
@@ -838,6 +839,12 @@ int libxl__arch_passthrough_mode_setdefault(libxl__gc *gc,
     return rc;
 }
 
+void libxl__msr_policy(libxl_ctx *ctx, uint32_t domid,
+                       libxl_domain_build_info *info)
+{
+    if (info->ignore_msrs != LIBXL_IGNORE_MSRS_NEVER)
+        xc_msr_apply_policy(ctx->xch, domid, info->ignore_msrs);
+}
 
 /*
  * Local variables:
Signature · 1 line
1.8.3.1
#49 lore raw

Re: [PATCH v2 4/4] tools/libs: Apply MSR policy to a guest

In reply to Boris Ostrovsky

Hi Boris,

tools/libs/light/libxl_dom.c int libxl__build_pre(libxl__gc *gc, uint32_t domid, · line 388

Quoted 13 lines · Boris Ostrovsky
tools/libs/light/libxl_dom.c +2 -2
index 19168572fd3e..1f2abf6679d7 100644
@@ -383,9 +383,10 @@ int libxl__build_pre(libxl__gc *gc, uint32_t domid,
      /* Construct a CPUID policy, but only for brand new domains.  Domains
       * being migrated-in/restored have CPUID handled during the
       * static_data_done() callback. */
-    if (!state->restore)
+    if (!state->restore) {
          libxl__cpuid_legacy(ctx, domid, false, info);
-
+        libxl__msr_policy(ctx, domid, info);

AFAICT, this is going to break compilation of the toolst on Arm because libxl__msr_policy().

However, I am a bit unsure whether we should define a stub for this on Arm. It feels to me it would be better to pass an extra boolean (restore) to libxl__arch_domain_create() and directly implement it there.

Cheers,

Signature · 1 line
Julien Grall
#50 lore raw

Re: [PATCH v2 4/4] tools/libs: Apply MSR policy to a guest

Quoted 18 lines · Julien Grall

Hi Boris,

tools/libs/light/libxl_dom.c int libxl__build_pre(libxl__gc *gc, uint32_t domid, · line 388

Quoted 13 lines · Boris Ostrovsky
tools/libs/light/libxl_dom.c +2 -2
index 19168572fd3e..1f2abf6679d7 100644
@@ -383,9 +383,10 @@ int libxl__build_pre(libxl__gc *gc, uint32_t domid,
       /* Construct a CPUID policy, but only for brand new domains.  Domains
        * being migrated-in/restored have CPUID handled during the
        * static_data_done() callback. */
-    if (!state->restore)
+    if (!state->restore) {
           libxl__cpuid_legacy(ctx, domid, false, info);
-
+        libxl__msr_policy(ctx, domid, info);

AFAICT, this is going to break compilation of the toolst on Arm because libxl__msr_policy().

Yes, it will ;-(

Quoted 2 lines

However, I am a bit unsure whether we should define a stub for this on Arm. It feels to me it would be better to pass an extra boolean (restore) to libxl__arch_domain_create() and directly implement it there.

Yes. And move libxl__cpuid_legacy call there too then.

-boris

#51 lore raw

Re: [PATCH v2 4/4] tools/libs: Apply MSR policy to a guest

In reply to Boris Ostrovsky
Quoted 102 lines · Boris Ostrovsky

When creating a guest, if ignore_msrs option has been specified, apply it to guest's MSR policy.

---
 tools/include/xenctrl.h           |   2 +
 tools/libs/guest/Makefile         |   1 +
 tools/libs/guest/xg_msrs_x86.c    | 110 ++++++++++++++++++++++++++++++++++++++
 tools/libs/light/libxl_dom.c      |   5 +-
 tools/libs/light/libxl_internal.h |   2 +
 tools/libs/light/libxl_x86.c      |   7 +++
 6 files changed, 125 insertions(+), 2 deletions(-)
 create mode 100644 tools/libs/guest/xg_msrs_x86.c
tools/include/xenctrl.h +2 -0
index 3796425e1eca..1d6a38e73dcf 100644
@@ -1835,6 +1835,8 @@ int xc_cpuid_apply_policy(xc_interface *xch,
                           const uint32_t *featureset,
                           unsigned int nr_features, bool pae, bool itsc,
                           bool nested_virt, const struct xc_xend_cpuid *xend);
+int xc_msr_apply_policy(xc_interface *xch, uint32_t domid,
+                        unsigned int ignore_msr);
 int xc_mca_op(xc_interface *xch, struct xen_mc *mc);
 int xc_mca_op_inject_v2(xc_interface *xch, unsigned int flags,
                         xc_cpumap_t cpumap, unsigned int nr_cpus);
tools/libs/guest/Makefile +1 -0
index 1c729040b337..452155ea0385 100644
@@ -56,6 +56,7 @@ SRCS-y                 += xg_dom_compat_linux.c
 
 SRCS-$(CONFIG_X86)     += xg_dom_x86.c
 SRCS-$(CONFIG_X86)     += xg_cpuid_x86.c
+SRCS-$(CONFIG_X86)     += xg_msrs_x86.c
 SRCS-$(CONFIG_ARM)     += xg_dom_arm.c
 
 ifeq ($(CONFIG_LIBXC_MINIOS),y)
tools/libs/guest/xg_msrs_x86.c new file +57 -0
new file mode 100644
index 000000000000..464ce9292ad8
@@ -0,0 +1,110 @@
+/******************************************************************************
+ * xc_msrs_x86.c
+ *
+ * Update MSR policy of a domain.
+ *
+ * Copyright (c) 2021, Oracle and/or its affiliates.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "xc_private.h"
+#include "xen/lib/x86/msr.h"
+
+
+
+int xc_msr_apply_policy(xc_interface *xch, uint32_t domid, unsigned int ignore_msr)
+{
+    int rc;
+    unsigned int nr_leaves, nr_msrs;
+    xen_msr_entry_t *msrs = NULL;
+    struct msr_policy *p = NULL;
+    xc_dominfo_t di;
+    unsigned int err_leaf, err_subleaf, err_msr;
+
+    if ( xc_domain_getinfo(xch, domid, 1, &di) != 1 ||
+         di.domid != domid )
+    {
+        ERROR("Failed to obtain d%d info", domid);
+        rc = -ESRCH;
+        goto out;
+    }
+
+    rc = xc_get_cpu_policy_size(xch, &nr_leaves, &nr_msrs);
+    if ( rc )
+    {
+        PERROR("Failed to obtain policy info size");
+        rc = -errno;
+        goto out;
+    }
+
+    rc = -ENOMEM;
+    if ( (msrs = calloc(nr_msrs, sizeof(*msrs))) == NULL ||
+         (p = calloc(1, sizeof(*p))) == NULL )
+        goto out;
+
+    /* Get the domain's default policy. */

tools/libs/guest/xg_msrs_x86.c line 67

Quoted 10 lines
tools/libs/guest/xg_msrs_x86.c new file +10 -0
+    nr_leaves = 0;
+    rc = xc_get_system_cpu_policy(xch, di.hvm ? XEN_SYSCTL_cpu_policy_hvm_default
+                                              : XEN_SYSCTL_cpu_policy_pv_default,
+                                  &nr_leaves, NULL, &nr_msrs, msrs);
+    if ( rc )
+    {
+        PERROR("Failed to obtain %s default policy", di.hvm ? "hvm" : "pv");
+        rc = -errno;
+        goto out;
+    }

Why not use xc_get_domain_cpu_policy instead so that you can avoid the call to xc_domain_getinfo?

It would also seem safer, as you won't be discarding any adjustments made to the default policy by the hypervisor for this specific domain.

Thanks, Roger.

#52 lore raw

Re: [PATCH v2 4/4] tools/libs: Apply MSR policy to a guest

Quoted 12 lines · Roger Pau Monné

tools/libs/guest/xg_msrs_x86.c line 67

Quoted 12 lines
tools/libs/guest/xg_msrs_x86.c new file +11 -0
+    /* Get the domain's default policy. */
+    nr_leaves = 0;
+    rc = xc_get_system_cpu_policy(xch, di.hvm ? XEN_SYSCTL_cpu_policy_hvm_default
+                                              : XEN_SYSCTL_cpu_policy_pv_default,
+                                  &nr_leaves, NULL, &nr_msrs, msrs);
+    if ( rc )
+    {
+        PERROR("Failed to obtain %s default policy", di.hvm ? "hvm" : "pv");
+        rc = -errno;
+        goto out;
+    }
Quoted 2 lines

Why not use xc_get_domain_cpu_policy instead so that you can avoid the call to xc_domain_getinfo?

Yes, indeed.

-boris

Quoted 5 lines

It would also seem safer, as you won't be discarding any adjustments made to the default policy by the hypervisor for this specific domain.

Thanks, Roger.