Skip to contentSkip to conversation

Replace __asm__ __volatile__ with asm volatile.

PATCH Superseded Jesús Diéguez Fernández 2 participants 10 messages Mar 1, 2019 – Mar 8, 2019 2 messages not in the archive

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 Replace __asm__ __volatile__ with asm volatile.

In order to maintain the coding style consistency, it was requested to replace the methods that use __asm__ __volatile__ with asm volatile.

---
 grub-core/efiemu/runtime/efiemu.c | 10 +++++-----
 grub-core/lib/i386/halt.c         |  2 +-
 include/grub/arm/time.h           |  2 +-
 include/grub/arm64/time.h         |  2 +-
 include/grub/i386/io.h            | 12 ++++++------
 include/grub/i386/time.h          |  2 +-
 include/grub/i386/tsc.h           |  2 +-
 include/grub/x86_64/time.h        |  2 +-
 8 files changed, 17 insertions(+), 17 deletions(-)
grub-core/efiemu/runtime/efiemu.c +5 -5
index d923e409f..5db1f347b 100644
@@ -174,17 +174,17 @@ efiemu_memset (grub_uint8_t *a, grub_uint8_t b, grub_size_t n)
 static inline void
 write_cmos (grub_uint8_t addr, grub_uint8_t val)
 {
-  __asm__ __volatile__ ("outb %%al,$0x70\n"
-			"mov %%cl, %%al\n"
-			"outb %%al,$0x71": :"a" (addr), "c" (val));
+  asm volatile ("outb %%al,$0x70\n"
+		"mov %%cl, %%al\n"
+		"outb %%al,$0x71": :"a" (addr), "c" (val));
 }
 
 static inline grub_uint8_t
 read_cmos (grub_uint8_t addr)
 {
   grub_uint8_t ret;
-  __asm__ __volatile__ ("outb %%al, $0x70\n"
-			"inb $0x71, %%al": "=a"(ret) :"a" (addr));
+  asm volatile ("outb %%al, $0x70\n"
+		"inb $0x71, %%al": "=a"(ret) :"a" (addr));
   return ret;
 }
 
grub-core/lib/i386/halt.c +1 -1
index 9f8405494..2364fe4d7 100644
@@ -66,7 +66,7 @@ grub_halt (void)
 #endif
 
   /* Disable interrupts.  */
-  __asm__ __volatile__ ("cli");
+  asm volatile ("cli");
 
   /* Bochs, QEMU, etc. Removed in newer QEMU releases.  */
   for (i = 0; i < sizeof (bochs_shutdown) - 1; i++)
include/grub/arm/time.h +1 -1
index 4128506cb..c5a685225 100644
@@ -23,7 +23,7 @@ static __inline void
 grub_cpu_idle (void)
 {
   /* FIXME: this can't work until we handle interrupts.  */
-/*  __asm__ __volatile__ ("wfi"); */
+/*  asm volatile ("wfi"); */
 }
 
 #endif /* ! KERNEL_CPU_TIME_HEADER */
include/grub/arm64/time.h +1 -1
index 4128506cb..c5a685225 100644
@@ -23,7 +23,7 @@ static __inline void
 grub_cpu_idle (void)
 {
   /* FIXME: this can't work until we handle interrupts.  */
-/*  __asm__ __volatile__ ("wfi"); */
+/*  asm volatile ("wfi"); */
 }
 
 #endif /* ! KERNEL_CPU_TIME_HEADER */
include/grub/i386/io.h +6 -6
index ae12a3e3d..e9cd80957 100644
@@ -28,7 +28,7 @@ grub_inb (unsigned short int port)
 {
   unsigned char _v;
 
-  __asm__ __volatile__ ("inb %w1,%0":"=a" (_v):"Nd" (port));
+  asm volatile ("inb %w1,%0":"=a" (_v):"Nd" (port));
   return _v;
 }
 
@@ -37,7 +37,7 @@ grub_inw (unsigned short int port)
 {
   unsigned short _v;
 
-  __asm__ __volatile__ ("inw %w1,%0":"=a" (_v):"Nd" (port));
+  asm volatile ("inw %w1,%0":"=a" (_v):"Nd" (port));
   return _v;
 }
 
@@ -46,27 +46,27 @@ grub_inl (unsigned short int port)
 {
   unsigned int _v;
 
-  __asm__ __volatile__ ("inl %w1,%0":"=a" (_v):"Nd" (port));
+  asm volatile ("inl %w1,%0":"=a" (_v):"Nd" (port));
   return _v;
 }
 
 static __inline void
 grub_outb (unsigned char value, unsigned short int port)
 {
-  __asm__ __volatile__ ("outb %b0,%w1": :"a" (value), "Nd" (port));
+  asm volatile ("outb %b0,%w1": :"a" (value), "Nd" (port));
 }
 
 static __inline void
 grub_outw (unsigned short int value, unsigned short int port)
 {
-  __asm__ __volatile__ ("outw %w0,%w1": :"a" (value), "Nd" (port));
+  asm volatile ("outw %w0,%w1": :"a" (value), "Nd" (port));
 
 }
 
 static __inline void
 grub_outl (unsigned int value, unsigned short int port)
 {
-  __asm__ __volatile__ ("outl %0,%w1": :"a" (value), "Nd" (port));
+  asm volatile ("outl %0,%w1": :"a" (value), "Nd" (port));
 }
 
 #endif /* _SYS_IO_H */
include/grub/i386/time.h +1 -1
index 842882cf2..4da5ae93c 100644
@@ -23,7 +23,7 @@ static __inline void
 grub_cpu_idle (void)
 {
   /* FIXME: this can't work until we handle interrupts.  */
-/*  __asm__ __volatile__ ("hlt"); */
+/*  asm volatile ("hlt"); */
 }
 
 #endif /* ! KERNEL_CPU_TIME_HEADER */
include/grub/i386/tsc.h +1 -1
index 324174ded..947e62fa1 100644
@@ -46,7 +46,7 @@ grub_get_tsc (void)
   grub_cpuid (0,a,b,c,d);
   /* Read TSC value.  We cannot use "=A", since this would use
      %rax on x86_64. */
-  __asm__ __volatile__ ("rdtsc":"=a" (lo), "=d" (hi));
+  asm volatile ("rdtsc":"=a" (lo), "=d" (hi));
 
   return (((grub_uint64_t) hi) << 32) | lo;
 }
include/grub/x86_64/time.h +1 -1
index 842882cf2..4da5ae93c 100644
@@ -23,7 +23,7 @@ static __inline void
 grub_cpu_idle (void)
 {
   /* FIXME: this can't work until we handle interrupts.  */
-/*  __asm__ __volatile__ ("hlt"); */
+/*  asm volatile ("hlt"); */
 }
 
 #endif /* ! KERNEL_CPU_TIME_HEADER */
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 2/2 Add new MSR modules (rdmsr/wrmsr)

In order to be able to read and write from/to model-specific registers, two new modules are added. They are i386 specific, as the cpuid module.

rdmsr module registers the command rdmsr that allows reading from a MSR. wrmsr module registers the command wrmsr that allows writing to a MSR.

wrmsr module is disabled if UEFI secure boot is enabled.

Please note that on SMP systems, interacting with a MSR that has a scope per hardware thread, implies that the value only applies to the particular cpu/core/thread that ran the command.

Changelog v1 -> v2:

- Patch all source code files with s/__asm__ __volatile__/asm volatile/g.
- Split the module in two (rdmsr/wrmsr).
- Include the wrmsr module in the forbidden modules efi list.
- Code indentation and cleanup.
- Copyright year update.
- Implicit casting mask removed.
- Use the same assembly code for x86 and x86_64.
- Add missing documentation.
- Patch submited with Signed-off-by.

---
 docs/grub.texi                     | 45 ++++++++++++++--
 grub-core/Makefile.core.def        | 16 ++++++
 grub-core/commands/efi/shim_lock.c |  2 +-
 grub-core/commands/i386/rdmsr.c    | 84 ++++++++++++++++++++++++++++++
 grub-core/commands/i386/wrmsr.c    | 75 ++++++++++++++++++++++++++
 include/grub/i386/rdmsr.h          | 32 ++++++++++++
 include/grub/i386/wrmsr.h          | 29 +++++++++++
 7 files changed, 277 insertions(+), 6 deletions(-)
 create mode 100644 grub-core/commands/i386/rdmsr.c
 create mode 100644 grub-core/commands/i386/wrmsr.c
 create mode 100644 include/grub/i386/rdmsr.h
 create mode 100644 include/grub/i386/wrmsr.h
docs/grub.texi +40 -5
index ecaba9d5c..1d792b636 100644
@@ -3931,6 +3931,7 @@ you forget a command, you can run the command @command{help}
 * play::                        Play a tune
 * probe::                       Retrieve device info
 * pxe_unload::                  Unload the PXE environment
+* rdmsr::                       Read values from model-specific registers
 * read::                        Read user input
 * reboot::                      Reboot your computer
 * regexp::                      Test if regular expression matches string
@@ -3953,6 +3954,7 @@ you forget a command, you can run the command @command{help}
 * verify_detached::             Verify detached digital signature
 * videoinfo::                   List available video modes
 @comment * xen_*::              Xen boot commands for AArch64
+* wrmsr::                       Write values to model-specific registers
 * xen_hypervisor::              Load xen hypervisor binary (only on AArch64)
 * xen_module::                  Load xen modules for xen hypervisor (only on AArch64)
 @end menu
@@ -4785,6 +4787,20 @@ This command is only available on PC BIOS systems.
 @end deffn
 
 
+@node rdmsr
+@subsection rdmsr
+
+@deffn Command: rdmsr 0xADDR [-v VARNAME]
+Read a model-specific register at address 0xADDR. If the parameter
+@option{-v} is used and an environment variable @var{VARNAME} is 
+given, set that environment variable to the value that was read.
+
+Please note that on SMP systems, reading from a MSR that has a
+scope per hardware thread, implies that the value that is returned
+only applies to the particular cpu/core/thread that runs the command.
+@end deffn
+
+
 @node read
 @subsection read
 
@@ -5223,6 +5239,17 @@ successfully.  If validation fails, it is set to a non-zero value.
 List available video modes. If resolution is given, show only matching modes.
 @end deffn
 
+@node wrmsr
+@subsection wrmsr
+
+@deffn Command: wrmsr 0xADDR 0xVALUE
+Write a 0xVALUE to a model-specific register at address 0xADDR.
+
+Please note that on SMP systems, writing to a MSR that has a scope 
+per hardware thread, implies that the value that is written
+only applies to the particular cpu/core/thread that runs the command.
+@end deffn
+
 @node xen_hypervisor
 @subsection xen_hypervisor
 
@@ -5716,11 +5743,11 @@ boot and the shim. This functionality is provided by the shim_lock module. It
 is recommend to build in this and other required modules into the @file{core.img}.
 All modules not stored in the @file{core.img} and the ACPI tables for the
 @command{acpi} command have to be signed, e.g. using PGP. Additionally, the
-@command{iorw} and the @command{memrw} commands are prohibited if the UEFI
-secure boot is enabled. This is done due to security reasons. All above
-mentioned requirements are enforced by the shim_lock module. And itself it
-is a persistent module which means that it cannot be unloaded if it was
-loaded into the memory.
+@command{iorw}, the @command{memrw} and the @command{wrmsr} commands are 
+prohibited if the UEFI secure boot is enabled.  This is done due to 
+security reasons.  All above mentioned requirements are enforced by the 
+shim_lock module. And itself it is a persistent module which means that 
+it cannot be unloaded if it was loaded into the memory.
 
 @node Measured Boot
 @section Measuring boot components
@@ -5831,6 +5858,8 @@ to install to is specified, UUID is used instead as well.
 @item USB                @tab yes     @tab yes      @tab yes          @tab yes
 @item chainloader        @tab local   @tab yes      @tab yes          @tab no
 @item cpuid              @tab partial @tab partial  @tab partial      @tab partial
+@item rdmsr              @tab partial @tab partial  @tab partial      @tab partial
+@item wrmsr              @tab partial @tab partial  @tab partial      @tab partial
 @item hints              @tab guess   @tab guess    @tab guess        @tab guess
 @item PCI                @tab yes     @tab yes      @tab yes          @tab yes
 @item badram             @tab yes     @tab yes      @tab yes          @tab yes
@@ -5850,6 +5879,8 @@ to install to is specified, UUID is used instead as well.
 @item USB                @tab yes         @tab yes       @tab yes           @tab no
 @item chainloader        @tab local       @tab local     @tab no            @tab local
 @item cpuid              @tab partial     @tab partial   @tab partial       @tab no
+@item rdmsr              @tab partial     @tab partial   @tab partial       @tab no
+@item wrmsr              @tab partial     @tab partial   @tab partial       @tab no
 @item hints              @tab guess       @tab guess     @tab good          @tab guess
 @item PCI                @tab yes         @tab yes       @tab yes           @tab no
 @item badram             @tab yes         @tab yes       @tab no            @tab yes
@@ -5869,6 +5900,8 @@ to install to is specified, UUID is used instead as well.
 @item USB                @tab yes         @tab no      @tab no      @tab no
 @item chainloader        @tab yes         @tab no      @tab no      @tab no
 @item cpuid              @tab no          @tab no      @tab no      @tab no
+@item rdmsr              @tab no          @tab no      @tab no      @tab no
+@item wrmsr              @tab no          @tab no      @tab no      @tab no
 @item hints              @tab good        @tab good    @tab good    @tab no
 @item PCI                @tab yes         @tab no      @tab no      @tab no
 @item badram             @tab yes (*)     @tab no      @tab no      @tab no
@@ -5888,6 +5921,8 @@ to install to is specified, UUID is used instead as well.
 @item USB                @tab N/A       @tab yes         @tab no
 @item chainloader        @tab yes       @tab no          @tab yes
 @item cpuid              @tab no        @tab no          @tab yes
+@item rdmsr              @tab no        @tab no          @tab yes
+@item wrmsr              @tab no        @tab no          @tab yes
 @item hints              @tab guess     @tab no          @tab no
 @item PCI                @tab no        @tab no          @tab no
 @item badram             @tab yes (*)   @tab no          @tab no
grub-core/Makefile.core.def +16 -0
index 2346bd291..a966a8f28 100644
@@ -2484,3 +2484,19 @@ module = {
   common = loader/i386/xen_file64.c;
   extra_dist = loader/i386/xen_fileXX.c;
 };
+module = {
+  name = rdmsr;
+  common = commands/i386/rdmsr.c;
+  enable = x86;
+  enable = i386_xen_pvh;
+  enable = i386_xen;
+  enable = x86_64_xen;
+};
+module = {
+  name = wrmsr;
+  common = commands/i386/wrmsr.c;
+  enable = x86;
+  enable = i386_xen_pvh;
+  enable = i386_xen;
+  enable = x86_64_xen;
+};
grub-core/commands/efi/shim_lock.c +1 -1
index 83568cb2b..764098cfc 100644
@@ -43,7 +43,7 @@ static grub_efi_guid_t shim_lock_guid = GRUB_EFI_SHIM_LOCK_GUID;
 static grub_efi_shim_lock_protocol_t *sl;
 
 /* List of modules which cannot be loaded if UEFI secure boot mode is enabled. */
-static const char * const disabled_mods[] = {"iorw", "memrw", NULL};
+static const char * const disabled_mods[] = {"iorw", "memrw", "wrmsr", NULL};
 
 static grub_err_t
 shim_lock_init (grub_file_t io, enum grub_file_type type,
grub-core/commands/i386/rdmsr.c new file +84 -0

84 lines not on this page · raw message

grub-core/commands/i386/wrmsr.c new file +75 -0

75 lines not on this page · raw message

include/grub/i386/rdmsr.h new file +32 -0
new file mode 100644
index 000000000..e907f1052
@@ -0,0 +1,32 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2019  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_CPU_MSR_READ_HEADER
+#define GRUB_CPU_MSR_READ_HEADER 1
+#endif
+
+extern __inline grub_uint64_t grub_msr_read (grub_uint64_t msr_id)
+{
+    grub_uint32_t low_id = msr_id, low, high;
+
+    asm volatile ( "rdmsr"  : "=a"(low), "=d"(high) : "c"(low_id) );
+
+    return ((grub_uint64_t)high << 32) | low;
+}
+
+
include/grub/i386/wrmsr.h new file +29 -0
new file mode 100644
index 000000000..2e535b8fe
@@ -0,0 +1,29 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2019  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_CPU_MSR_WRITE_HEADER
+#define GRUB_CPU_MSR_WRITE_HEADER 1
+#endif
+
+extern __inline void grub_msr_write(grub_uint64_t msr_id, grub_uint64_t msr_value)
+{
+    grub_uint32_t low_id = msr_id, low = msr_value, high = msr_value >> 32;
+
+    asm volatile ( "wrmsr" : : "c"(low_id), "a"(low), "d"(high) );
+}
+
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v2 2/2] Add new MSR modules (rdmsr/wrmsr)

Quoted 24 lines · Jesús Diéguez Fernández

In order to be able to read and write from/to model-specific registers, two new modules are added. They are i386 specific, as the cpuid module.

rdmsr module registers the command rdmsr that allows reading from a MSR. wrmsr module registers the command wrmsr that allows writing to a MSR.

wrmsr module is disabled if UEFI secure boot is enabled.

Please note that on SMP systems, interacting with a MSR that has a scope per hardware thread, implies that the value only applies to the particular cpu/core/thread that ran the command.

Changelog v1 -> v2:

- Patch all source code files with s/__asm__ __volatile__/asm volatile/g.
- Split the module in two (rdmsr/wrmsr).
- Include the wrmsr module in the forbidden modules efi list.
- Code indentation and cleanup.
- Copyright year update.
- Implicit casting mask removed.
- Use the same assembly code for x86 and x86_64.
- Add missing documentation.
- Patch submited with Signed-off-by.

Quoted 1 line

In general it looks much better but I still have some concerns...

First of all, two patches and more should have mail introducing them. Good example you can find here
http://lists.gnu.org/archive/html/grub-devel/2018-10/msg00201.html
or here
http://lists.gnu.org/archive/html/grub-devel/2018-11/msg00282.html

git send-email --compose ... is your friend...

[...]

Quoted 5 lines
grub-core/Makefile.core.def +0 -0
index 2346bd291..a966a8f28 100644

grub-core/Makefile.core.def module = { · line 2493

Quoted 10 lines
grub-core/Makefile.core.def +7 -0
@@ -2484,3 +2484,19 @@ module = {
   common = loader/i386/xen_file64.c;
   extra_dist = loader/i386/xen_fileXX.c;
 };
+module = {
+  name = rdmsr;
+  common = commands/i386/rdmsr.c;
+  enable = x86;
+  enable = i386_xen_pvh;
+  enable = i386_xen;
+  enable = x86_64_xen;

I think that x86 should suffice? Does not it?

grub-core/Makefile.core.def module = { · line 2501

Quoted 8 lines
grub-core/Makefile.core.def +8 -0
+};
+module = {
+  name = wrmsr;
+  common = commands/i386/wrmsr.c;
+  enable = x86;
+  enable = i386_xen_pvh;
+  enable = i386_xen;
+  enable = x86_64_xen;

Ditto.

Quoted 66 lines
grub-core/Makefile.core.def +1 -0
+};
grub-core/commands/efi/shim_lock.c +1 -1
index 83568cb2b..764098cfc 100644
@@ -43,7 +43,7 @@ static grub_efi_guid_t shim_lock_guid = GRUB_EFI_SHIM_LOCK_GUID;
 static grub_efi_shim_lock_protocol_t *sl;
 
 /* List of modules which cannot be loaded if UEFI secure boot mode is enabled. */
-static const char * const disabled_mods[] = {"iorw", "memrw", NULL};
+static const char * const disabled_mods[] = {"iorw", "memrw", "wrmsr", NULL};
 
 static grub_err_t
 shim_lock_init (grub_file_t io, enum grub_file_type type,
grub-core/commands/i386/rdmsr.c new file +46 -0
new file mode 100644
index 000000000..08e5aee0b
@@ -0,0 +1,84 @@
+/* rdmsr.c - Read CPU model-specific registers */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2019  Free Software Foundation, Inc.
+ *  Based on gcc/gcc/config/i386/driver-i386.c
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/env.h>
+#include <grub/command.h>
+#include <grub/extcmd.h>
+#include <grub/i386/rdmsr.h>
+#include <grub/i18n.h>
+
+GRUB_MOD_LICENSE("GPLv3+");
+
+static grub_extcmd_t cmd_read;
+
+static const struct grub_arg_option options[] =
+{
+    {0, 'v', 0, N_("Save read value into variable VARNAME."),
+    N_("VARNAME"), ARG_TYPE_STRING},
+    {0, 0, 0, 0, 0, 0}
+};
+
+static grub_err_t
+grub_cmd_msr_read (grub_extcmd_context_t ctxt, int argc, char **argv)
+{
+    grub_uint64_t addr, value;
+    char *ptr;
+    char buf[sizeof("XXXXXXXX")];

grub-core/commands/i386/rdmsr.c line 56

Quoted 10 lines
grub-core/commands/i386/rdmsr.c new file +10 -0
+
+    if (argc != 1)
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
+
+    grub_errno = GRUB_ERR_NONE;
+    ptr = argv[0];
+    addr = grub_strtoul (ptr, &ptr, 0);
+
+    if (grub_errno != GRUB_ERR_NONE)
+        return grub_errno;

Should not you display a blurb to the user what happened here? Like you do below...

Quoted 72 lines
grub-core/commands/i386/rdmsr.c new file +28 -0
+    if (*ptr != '\0')
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
+
+    value = grub_msr_read (addr);
+
+    if (ctxt->state[0].set)
+    {
+        grub_snprintf (buf, sizeof(buf), "%llx", value);
+        grub_env_set (ctxt->state[0].arg, buf);
+    }
+    else
+        grub_printf ("0x%llx\n", value);
+
+    return GRUB_ERR_NONE;
+}
+
+GRUB_MOD_INIT(rdmsr)
+{
+    cmd_read = grub_register_extcmd ("rdmsr", grub_cmd_msr_read, 0,
+                                    N_("ADDR"),
+                                    N_("Read a CPU model specific register."),
+                                    options);
+}
+
+GRUB_MOD_FINI(rdmsr)
+{
+    grub_unregister_extcmd (cmd_read);
+}
grub-core/commands/i386/wrmsr.c new file +38 -0
new file mode 100644
index 000000000..351b93f93
@@ -0,0 +1,75 @@
+/* wrmsr.c - Write CPU model-specific registers */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2019  Free Software Foundation, Inc.
+ *  Based on gcc/gcc/config/i386/driver-i386.c
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/env.h>
+#include <grub/command.h>
+#include <grub/extcmd.h>
+#include <grub/i386/wrmsr.h>
+#include <grub/i18n.h>
+
+GRUB_MOD_LICENSE("GPLv3+");
+
+static grub_command_t cmd_write;
+
+static grub_err_t
+grub_cmd_msr_write (grub_command_t cmd, int argc, char **argv)
+{
+    grub_uint64_t addr, value;
+    char *ptr;

grub-core/commands/i386/wrmsr.c line 48

Quoted 10 lines
grub-core/commands/i386/wrmsr.c new file +10 -0
+
+    if (argc != 2)
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
+
+    grub_errno = GRUB_ERR_NONE;
+    ptr = argv[0];
+    addr = grub_strtoul (ptr, &ptr, 0);
+
+    if (grub_errno != GRUB_ERR_NONE)
+        return grub_errno;

Ditto.

grub-core/commands/i386/wrmsr.c line 56

Quoted 8 lines
grub-core/commands/i386/wrmsr.c new file +8 -0
+    if (*ptr != '\0')
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
+
+    ptr = argv[1];
+    value = grub_strtoul (ptr, &ptr, 0);
+
+    if (grub_errno != GRUB_ERR_NONE)
+        return grub_errno;

Ditto.

Quoted 35 lines
grub-core/commands/i386/wrmsr.c new file +19 -0
+    if (*ptr != '\0')
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
+
+    grub_msr_write (addr, value);
+
+    return GRUB_ERR_NONE;
+}
+
+GRUB_MOD_INIT(wrmsr)
+{
+    cmd_write = grub_register_command ("wrmsr", grub_cmd_msr_write,
+                                      N_("ADDR VALUE"),
+                                      N_("Write a value to a CPU model specific register."));
+}
+
+GRUB_MOD_FINI(wrmsr)
+{
+    grub_unregister_command (cmd_write);
+}
include/grub/i386/rdmsr.h new file +10 -0
new file mode 100644
index 000000000..e907f1052
@@ -0,0 +1,32 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2019  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,

include/grub/i386/rdmsr.h line 20

Quoted 10 lines
include/grub/i386/rdmsr.h new file +10 -0
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_CPU_MSR_READ_HEADER
+#define GRUB_CPU_MSR_READ_HEADER 1

s/GRUB_CPU_MSR_READ_HEADER/GRUB_RDMSR_H/

include/grub/i386/rdmsr.h line 21

Quoted 1 line
include/grub/i386/rdmsr.h new file +1 -0
+#endif

This #endif should go...

include/grub/i386/rdmsr.h line 31

Quoted 10 lines
include/grub/i386/rdmsr.h new file +10 -0
+
+extern __inline grub_uint64_t grub_msr_read (grub_uint64_t msr_id)
+{
+    grub_uint32_t low_id = msr_id, low, high;
+
+    asm volatile ( "rdmsr"  : "=a"(low), "=d"(high) : "c"(low_id) );
+
+    return ((grub_uint64_t)high << 32) | low;
+}
+

...here. Like this

#endif /* GRUB_RDMSR_H */

include/grub/i386/rdmsr.h line 32

Quoted 1 line
include/grub/i386/rdmsr.h new file +1 -0
+

Please drop this empty line.

Quoted 16 lines
include/grub/i386/wrmsr.h new file +10 -0
new file mode 100644
index 000000000..2e535b8fe
@@ -0,0 +1,29 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2019  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,

include/grub/i386/wrmsr.h line 20

Quoted 10 lines
include/grub/i386/wrmsr.h new file +10 -0
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_CPU_MSR_WRITE_HEADER
+#define GRUB_CPU_MSR_WRITE_HEADER 1

s/GRUB_CPU_MSR_READ_HEADER/GRUB_WRMSR_H/

include/grub/i386/wrmsr.h line 21

Quoted 1 line
include/grub/i386/wrmsr.h new file +1 -0
+#endif

And this #endif should go below too. Like in rdmsr.h.

include/grub/i386/wrmsr.h line 28

Quoted 6 lines
include/grub/i386/wrmsr.h new file +6 -0
+extern __inline void grub_msr_write(grub_uint64_t msr_id, grub_uint64_t msr_value)
+{
+    grub_uint32_t low_id = msr_id, low = msr_value, high = msr_value >> 32;
+
+    asm volatile ( "wrmsr" : : "c"(low_id), "a"(low), "d"(high) );
+}

Anyway, thank you for doing the work. Looking forward for next version of patches. Please post them quickly because freeze date is nearing.

Daniel

#3 lore raw

Re: [PATCH v2 2/2] Add new MSR modules (rdmsr/wrmsr)

Hi,
I have some doubts that I comment below.

Quoted 35 lines · Daniel Kiper
Quoted 24 lines · Jesús Diéguez Fernández

In order to be able to read and write from/to model-specific registers, two new modules are added. They are i386 specific, as the cpuid module.

rdmsr module registers the command rdmsr that allows reading from a MSR. wrmsr module registers the command wrmsr that allows writing to a MSR.

wrmsr module is disabled if UEFI secure boot is enabled.

Please note that on SMP systems, interacting with a MSR that has a scope per hardware thread, implies that the value only applies to the particular cpu/core/thread that ran the command.

Changelog v1 -> v2:

- Patch all source code files with s/__asm__ __volatile__/asm volatile/g.
- Split the module in two (rdmsr/wrmsr).
- Include the wrmsr module in the forbidden modules efi list.
- Code indentation and cleanup.
- Copyright year update.
- Implicit casting mask removed.
- Use the same assembly code for x86 and x86_64.
- Add missing documentation.
- Patch submited with Signed-off-by.

Quoted 1 line

In general it looks much better but I still have some concerns...

First of all, two patches and more should have mail introducing them. Good example you can find here
http://lists.gnu.org/archive/html/grub-devel/2018-10/msg00201.html
or here
http://lists.gnu.org/archive/html/grub-devel/2018-11/msg00282.html

Quoted 3 lines

git send-email --compose ... is your friend...

[...]

I thought about it before sending the e-mail, but I chose the wrong option. :-\

For the v3, now that the patch [1/2] has already been reviewed, should I send it again, or should I skip it?

Quoted 10 lines
Quoted 10 lines
grub-core/Makefile.core.def +1 -0
index 2346bd291..a966a8f28 100644
@@ -2484,3 +2484,19 @@ module = {
   common = loader/i386/xen_file64.c;
   extra_dist = loader/i386/xen_fileXX.c;
 };
+module = {
Quoted 8 lines

grub-core/Makefile.core.def module = { · line 2493

Quoted 6 lines
grub-core/Makefile.core.def +6 -0
+  name = rdmsr;
+  common = commands/i386/rdmsr.c;
+  enable = x86;
+  enable = i386_xen_pvh;
+  enable = i386_xen;
+  enable = x86_64_xen;

I think that x86 should suffice? Does not it?

I used the cpuid module as an example since it has many similarities. Even so, I thought that accessing MSR from a Xen guest could be interesting:

http://www.brendangregg.com/blog/2014-09-15/the-msrs-of-ec2.html

For my use case, leaving alone x86 is enough. I'll modify it as you tell me to.

Quoted 12 lines

grub-core/Makefile.core.def module = { · line 2501

Quoted 9 lines
grub-core/Makefile.core.def +8 -0
+};
+module = {
+  name = wrmsr;
+  common = commands/i386/wrmsr.c;
+  enable = x86;
+  enable = i386_xen_pvh;
+  enable = i386_xen;
+  enable = x86_64_xen;

Ditto.

[...]

Quoted 11 lines
Quoted 11 lines
grub-core/Makefile.core.def +11 -0
+static grub_err_t
+grub_cmd_msr_read (grub_extcmd_context_t ctxt, int argc, char **argv)
+{
+    grub_uint64_t addr, value;
+    char *ptr;
+    char buf[sizeof("XXXXXXXX")];
+
+    if (argc != 1)
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
+
+    grub_errno = GRUB_ERR_NONE;
Quoted 8 lines

grub-core/Makefile.core.def

Quoted 5 lines
grub-core/Makefile.core.def +5 -0
+    ptr = argv[0];
+    addr = grub_strtoul (ptr, &ptr, 0);
+
+    if (grub_errno != GRUB_ERR_NONE)
+        return grub_errno;

Should not you display a blurb to the user what happened here? Like you do below...

I used the same method that is used in grub-core/term/terminfo.c

static grub_err_t
grub_cmd_terminfo (grub_extcmd_context_t ctxt, int argc, char **args) {
...

      char *ptr = state[OPTION_GEOMETRY].arg;
      w = grub_strtoul (ptr, &ptr, 0);
      if (grub_errno)
        return grub_errno;
      if (*ptr != 'x')
        return grub_error (GRUB_ERR_BAD_ARGUMENT,
                           N_("incorrect terminal dimensions

specification"));

I tested this with:

rdmsr z => shows "unrecognized number"
rdmsr 0x1122334455667788991100 => shows "overflow detected"

I can change it to:

if (grub_errno != GRUB_ERR_NONE)

        return grub_error (grub_errno, N_("invalid argument"));

If that is any better.

Quoted 16 lines
Quoted 16 lines
grub-core/Makefile.core.def +15 -0
+    if (*ptr != '\0')
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
+
+    value = grub_msr_read (addr);
+
+    if (ctxt->state[0].set)
+    {
+        grub_snprintf (buf, sizeof(buf), "%llx", value);
+        grub_env_set (ctxt->state[0].arg, buf);
+    }
+    else
+        grub_printf ("0x%llx\n", value);
+
+    return GRUB_ERR_NONE;
+}

[...]

Quoted 23 lines
Quoted 10 lines
grub-core/Makefile.core.def +10 -0
+
+GRUB_MOD_LICENSE("GPLv3+");
+
+static grub_command_t cmd_write;
+
+static grub_err_t
+grub_cmd_msr_write (grub_command_t cmd, int argc, char **argv)
+{
+    grub_uint64_t addr, value;
+    char *ptr;

grub-core/Makefile.core.def

Quoted 10 lines
grub-core/Makefile.core.def +10 -0
+
+    if (argc != 2)
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
+
+    grub_errno = GRUB_ERR_NONE;
+    ptr = argv[0];
+    addr = grub_strtoul (ptr, &ptr, 0);
+
+    if (grub_errno != GRUB_ERR_NONE)
+        return grub_errno;

Ditto.

Quoted 14 lines

grub-core/Makefile.core.def

Quoted 8 lines
grub-core/Makefile.core.def +8 -0
+    if (*ptr != '\0')
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
+
+    ptr = argv[1];
+    value = grub_strtoul (ptr, &ptr, 0);
+
+    if (grub_errno != GRUB_ERR_NONE)
+        return grub_errno;

Ditto.

Quoted 3 lines
grub-core/Makefile.core.def +3 -0
+    if (*ptr != '\0')
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
+

I found a bug in this function, it is possible to write 64 bit values to the MSR, so I should have used grub_strtoull when reading the value. It will be fixed in the v3.

Quoted 16 lines
Quoted 16 lines
grub-core/Makefile.core.def +16 -0
+    grub_msr_write (addr, value);
+
+    return GRUB_ERR_NONE;
+}
+
+GRUB_MOD_INIT(wrmsr)
+{
+    cmd_write = grub_register_command ("wrmsr", grub_cmd_msr_write,
+                                      N_("ADDR VALUE"),
+                                      N_("Write a value to a CPU model specific register."));
+}
+
+GRUB_MOD_FINI(wrmsr)
+{
+    grub_unregister_command (cmd_write);
+}

[...]

Quoted 13 lines

And this #endif should go below too. Like in rdmsr.h.

grub-core/Makefile.core.def

Quoted 6 lines
grub-core/Makefile.core.def +6 -0
+extern __inline void grub_msr_write(grub_uint64_t msr_id, grub_uint64_t msr_value)
+{
+    grub_uint32_t low_id = msr_id, low = msr_value, high = msr_value >> 32;
+
+    asm volatile ( "wrmsr" : : "c"(low_id), "a"(low), "d"(high) );
+}

Anyway, thank you for doing the work. Looking forward for next version of patches. Please post them quickly because freeze date is nearing.

Quoted 2 lines

Daniel

I'm also interested in getting this patch into the next version. I'll send you the v3 as soon as this doubts are cleared.

Jesus

#4 lore raw

Re: [PATCH v2 2/2] Add new MSR modules (rdmsr/wrmsr)

Quoted 39 lines · Jesús Diéguez Fernández

Hi,
I have some doubts that I comment below.

Quoted 35 lines · Daniel Kiper
Quoted 24 lines · Jesús Diéguez Fernández

In order to be able to read and write from/to model-specific registers, two new modules are added. They are i386 specific, as the cpuid module.

rdmsr module registers the command rdmsr that allows reading from a MSR. wrmsr module registers the command wrmsr that allows writing to a MSR.

wrmsr module is disabled if UEFI secure boot is enabled.

Please note that on SMP systems, interacting with a MSR that has a scope per hardware thread, implies that the value only applies to the particular cpu/core/thread that ran the command.

Changelog v1 -> v2:

- Patch all source code files with s/__asm__ __volatile__/asm volatile/g.
- Split the module in two (rdmsr/wrmsr).
- Include the wrmsr module in the forbidden modules efi list.
- Code indentation and cleanup.
- Copyright year update.
- Implicit casting mask removed.
- Use the same assembly code for x86 and x86_64.
- Add missing documentation.
- Patch submited with Signed-off-by.

Quoted 1 line

In general it looks much better but I still have some concerns...

First of all, two patches and more should have mail introducing them. Good example you can find here
http://lists.gnu.org/archive/html/grub-devel/2018-10/msg00201.html
or here
http://lists.gnu.org/archive/html/grub-devel/2018-11/msg00282.html

Quoted 6 lines
Quoted 3 lines

git send-email --compose ... is your friend...

[...]

I thought about it before sending the e-mail, but I chose the wrong option. :-\

Not big deal...

Quoted 2 lines

For the v3, now that the patch [1/2] has already been reviewed, should I send it again, or should I skip it?

Please add my Reviewed-by under your Signed-off-by (SOB) and resend it.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
grub-core/Makefile.core.def +1 -0
index 2346bd291..a966a8f28 100644
@@ -2484,3 +2484,19 @@ module = {
   common = loader/i386/xen_file64.c;
   extra_dist = loader/i386/xen_fileXX.c;
 };
+module = {
Quoted 8 lines

grub-core/Makefile.core.def module = { · line 2493

Quoted 6 lines
grub-core/Makefile.core.def +6 -0
+  name = rdmsr;
+  common = commands/i386/rdmsr.c;
+  enable = x86;
+  enable = i386_xen_pvh;
+  enable = i386_xen;
+  enable = x86_64_xen;

I think that x86 should suffice? Does not it?

Quoted 7 lines

I used the cpuid module as an example since it has many similarities. Even so, I thought that accessing MSR from a Xen guest could be interesting:

http://www.brendangregg.com/blog/2014-09-15/the-msrs-of-ec2.html

For my use case, leaving alone x86 is enough. I'll modify it as you tell me to.

I am not saying that MSR access is not interesting on Xen. I am saying that I have a feeling that all "enable ..." except "enable = x86;" are redundant. So, I think that "enable = x86;" will cover Xen too. Please double check it. If it does not cover leave xen stuff as is.

Quoted 55 lines
Quoted 11 lines

grub-core/Makefile.core.def module = { · line 2501

Quoted 8 lines
grub-core/Makefile.core.def +8 -0
+};
+module = {
+  name = wrmsr;
+  common = commands/i386/wrmsr.c;
+  enable = x86;
+  enable = i386_xen_pvh;
+  enable = i386_xen;
+  enable = x86_64_xen;

Ditto.

[...]

Quoted 11 lines
Quoted 11 lines
grub-core/Makefile.core.def +11 -0
+static grub_err_t
+grub_cmd_msr_read (grub_extcmd_context_t ctxt, int argc, char **argv)
+{
+    grub_uint64_t addr, value;
+    char *ptr;
+    char buf[sizeof("XXXXXXXX")];
+
+    if (argc != 1)
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
+
+    grub_errno = GRUB_ERR_NONE;
Quoted 8 lines

grub-core/Makefile.core.def

Quoted 5 lines
grub-core/Makefile.core.def +5 -0
+    ptr = argv[0];
+    addr = grub_strtoul (ptr, &ptr, 0);
+
+    if (grub_errno != GRUB_ERR_NONE)
+        return grub_errno;

Should not you display a blurb to the user what happened here? Like you do below...

I used the same method that is used in grub-core/term/terminfo.c

static grub_err_t
grub_cmd_terminfo (grub_extcmd_context_t ctxt, int argc, char **args) {
...

      char *ptr = state[OPTION_GEOMETRY].arg;
      w = grub_strtoul (ptr, &ptr, 0);
      if (grub_errno)
        return grub_errno;
      if (*ptr != 'x')
        return grub_error (GRUB_ERR_BAD_ARGUMENT,
                           N_("incorrect terminal dimensions

specification"));

I tested this with:

rdmsr z => shows "unrecognized number"
rdmsr 0x1122334455667788991100 => shows "overflow detected"

Quoted 6 lines

I can change it to:

if (grub_errno != GRUB_ERR_NONE)

        return grub_error (grub_errno, N_("invalid argument"));

If that is any better.

If your/terminfo version works correctly then I am fine with it. You do not need to change it.

Quoted 52 lines
Quoted 15 lines
Quoted 15 lines
grub-core/Makefile.core.def +15 -0
+    if (*ptr != '\0')
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
+
+    value = grub_msr_read (addr);
+
+    if (ctxt->state[0].set)
+    {
+        grub_snprintf (buf, sizeof(buf), "%llx", value);
+        grub_env_set (ctxt->state[0].arg, buf);
+    }
+    else
+        grub_printf ("0x%llx\n", value);
+
+    return GRUB_ERR_NONE;
+}

[...]

Quoted 34 lines
Quoted 10 lines
grub-core/Makefile.core.def +10 -0
+
+GRUB_MOD_LICENSE("GPLv3+");
+
+static grub_command_t cmd_write;
+
+static grub_err_t
+grub_cmd_msr_write (grub_command_t cmd, int argc, char **argv)
+{
+    grub_uint64_t addr, value;
+    char *ptr;

grub-core/Makefile.core.def

Quoted 10 lines
grub-core/Makefile.core.def +10 -0
+
+    if (argc != 2)
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
+
+    grub_errno = GRUB_ERR_NONE;
+    ptr = argv[0];
+    addr = grub_strtoul (ptr, &ptr, 0);
+
+    if (grub_errno != GRUB_ERR_NONE)
+        return grub_errno;

Ditto.

grub-core/Makefile.core.def

Quoted 8 lines
grub-core/Makefile.core.def +8 -0
+    if (*ptr != '\0')
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
+
+    ptr = argv[1];
+    value = grub_strtoul (ptr, &ptr, 0);
+
+    if (grub_errno != GRUB_ERR_NONE)
+        return grub_errno;

Ditto.

Quoted 9 lines
Quoted 3 lines

grub-core/Makefile.core.def

Quoted 3 lines
grub-core/Makefile.core.def +3 -0
+    if (*ptr != '\0')
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
+

I found a bug in this function, it is possible to write 64 bit values to the MSR, so I should have used grub_strtoull when reading the value. It will be fixed in the v3.

Quoted 1 line
Quoted 1 line

+ grub_msr_write (addr, value);

There are other issues. IMO addr should be 32-bit type instead of 64-bit.

And Intel spec says:
The CPUID instruction should be used to determine whether MSRs are supported (CPUID.01H:EDX[5] = 1) before using this instruction.

Specifying a reserved or unimplemented MSR address in ECX will also cause a general protection exception.

What will happen if somebody specifies invalid MSR? Does GRUB crashes?

Daniel

A message that is not in the archive (sent off-list or not yet received): CAPo7o_5d2rsb13+iVopwtcZJGYWK1uHzJn21_ece747Jw56Qzw@mail.gmail.com

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

#7 lore raw

Re: [PATCH v2 2/2] Add new MSR modules (rdmsr/wrmsr)

Quoted 126 lines · Daniel Kiper
Quoted 57 lines · Jesús DF
Quoted 47 lines · Daniel Kiper
Quoted 39 lines · Jesús Diéguez Fernández

Hi,
I have some doubts that I comment below.

Quoted 35 lines · Daniel Kiper
Quoted 24 lines · Jesús Diéguez Fernández

In order to be able to read and write from/to model-specific registers, two new modules are added. They are i386 specific, as the cpuid module.

rdmsr module registers the command rdmsr that allows reading from a MSR. wrmsr module registers the command wrmsr that allows writing to a MSR.

wrmsr module is disabled if UEFI secure boot is enabled.

Please note that on SMP systems, interacting with a MSR that has a scope per hardware thread, implies that the value only applies to the particular cpu/core/thread that ran the command.

Changelog v1 -> v2:

- Patch all source code files with s/__asm__ __volatile__/asm volatile/g.
- Split the module in two (rdmsr/wrmsr).
- Include the wrmsr module in the forbidden modules efi list.
- Code indentation and cleanup.
- Copyright year update.
- Implicit casting mask removed.
- Use the same assembly code for x86 and x86_64.
- Add missing documentation.
- Patch submited with Signed-off-by.

Quoted 1 line

In general it looks much better but I still have some concerns...

First of all, two patches and more should have mail introducing them. Good example you can find here
http://lists.gnu.org/archive/html/grub-devel/2018-10/msg00201.html
or here
http://lists.gnu.org/archive/html/grub-devel/2018-11/msg00282.html

Quoted 6 lines
Quoted 3 lines

git send-email --compose ... is your friend...

[...]

I thought about it before sending the e-mail, but I chose the wrong option. :-\

Quoted 7 lines

Not big deal...

Quoted 2 lines

For the v3, now that the patch [1/2] has already been reviewed, should I send it again, or should I skip it?

Please add my Reviewed-by under your Signed-off-by (SOB) and resend it.

Ok.

[...]

Quoted 45 lines
Quoted 45 lines
Quoted 29 lines
Quoted 29 lines
Quoted 5 lines
grub-core/commands/i386/wrmsr.c new file +5 -0
+static grub_err_t
+grub_cmd_msr_write (grub_command_t cmd, int argc, char **argv)
+{
+    grub_uint64_t addr, value;
+    char *ptr;

grub-core/commands/i386/wrmsr.c line 48

Quoted 10 lines
grub-core/commands/i386/wrmsr.c new file +10 -0
+
+    if (argc != 2)
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
+
+    grub_errno = GRUB_ERR_NONE;
+    ptr = argv[0];
+    addr = grub_strtoul (ptr, &ptr, 0);
+
+    if (grub_errno != GRUB_ERR_NONE)
+        return grub_errno;

Ditto.

grub-core/commands/i386/wrmsr.c line 56

Quoted 8 lines
grub-core/commands/i386/wrmsr.c new file +8 -0
+    if (*ptr != '\0')
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
+
+    ptr = argv[1];
+    value = grub_strtoul (ptr, &ptr, 0);
+
+    if (grub_errno != GRUB_ERR_NONE)
+        return grub_errno;

Ditto.

Quoted 9 lines
Quoted 3 lines

grub-core/commands/i386/rdmsr.c line 59

Quoted 3 lines
grub-core/commands/i386/rdmsr.c new file +3 -0
+    if (*ptr != '\0')
+        return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
+

I found a bug in this function, it is possible to write 64 bit values to the MSR, so I should have used grub_strtoull when reading the value. It will be fixed in the v3.

Quoted 1 line
Quoted 1 line

+ grub_msr_write (addr, value);

There are other issues. IMO addr should be 32-bit type instead of 64-bit.

And Intel spec says:
The CPUID instruction should be used to determine whether MSRs are supported (CPUID.01H:EDX[5] = 1) before using this instruction.

Quoted 8 lines
Quoted 6 lines

Specifying a reserved or unimplemented MSR address in ECX will also cause a general protection exception.

What will happen if somebody specifies invalid MSR? Does GRUB crashes?

Daniel

Yes, I already did change addr to be 32-bit type in my local branch.

Great!

Quoted 4 lines

All Intel CPUs since the first Pentium support that instructions, but to be safe I should check it using CPUID.
I'll add that functionality to the cpuid module and use it to verify if its availiable before calling the instruction.

I am not sure that it pays to pull in cpuid module here. Maybe it will be easier to just use asm with cpuid before grub_msr_read()/grub_msr_write() calls. Anyway, whichever is simpler.

Quoted 9 lines
Quoted 4 lines

Currently, when you try to read or write from an invalid MSR, the system is rebooted.
I'm not familiar with the way to handle a general protection exceptions, do you know where should I look at?

Intel SDM, Linux kernel or Xen.

Daniel

I've sorted out the MSR support check, but I'm struggling a bit with handling the exception.

I've read the Intel SDM and other docs, and I think I understand the reason why the system reboots: when the rdmsr or wrmsr access a restricted area, a general protection exception is raised; and because there's no interrupt 13 (GP#) handler installed, it double faults and then triple faults, resulting in a cpu halt and motherboard reboot.

The linux kernel uses the following function to read and write to the msr:

https://elixir.bootlin.com/linux/latest/source/arch/x86/include/asm/msr.h#L137

If I understood it correctly, it uses some gcc syntax to setup the handler of an exception in a different section, but I think that it relies on the IDT that the linux kernel has already installed, so I can't use that code.

https://github.com/torvalds/linux/blob/master/Documentation/x86/exception-tables.txt

Apart from setting up the IDT, I've found the opcodes CLI and STI, but I'm quite sure that trying to use them to ignore the interrupt is a dead end.

I've looked around in the grub code and there are only a few files that reference CLI, STI, LIDT or IRET, mainly the code that makes a stage change.
I also found that the file grub-core/commands/i386/pc/drivemap.c sets up a int13h trap to handle the disk mappings.

Since I'm not very familiar with this area (and maybe I'm overcomplicating it), it seems that the approach of handling the exception could take a lot more effort than the whole module itself.

Now, the questions I have are about what direction I should take:

- Do I really need to make a custom handler (something like drivemap.c does), or is there any simpler way?

- I assume that I'm not the first one to need to handle a general protection exception in grub, but I couldn't find any example in other modules/commands. Any reference would be appreciated.

- And from a practical point of view, in case that I need to setup a custom handler for this, is it mandatory to ship the patch with it or could it be added later (maybe adding a TODO now)?

Jesus

#8 lore raw

Re: [PATCH v2 2/2] Add new MSR modules (rdmsr/wrmsr)

On Thu, Mar 07, 2019 at 07:05:20PM +0100, Jesús Diéguez Fernández wrote:

[...]

Quoted 1 line

I've sorted out the MSR support check, but I'm struggling a bit with

Great!

Quoted 1 line

handling the exception.

Yeah, this can be difficult.

Quoted 31 lines

I've read the Intel SDM and other docs, and I think I understand the reason why the system reboots: when the rdmsr or wrmsr access a restricted area, a general protection exception is raised; and because there's no interrupt 13 (GP#) handler installed, it double faults and then triple faults, resulting in a cpu halt and motherboard reboot.

The linux kernel uses the following function to read and write to the msr:

https://elixir.bootlin.com/linux/latest/source/arch/x86/include/asm/msr.h#L137

If I understood it correctly, it uses some gcc syntax to setup the handler of an exception in a different section, but I think that it relies on the IDT that the linux kernel has already installed, so I can't use that code.

https://github.com/torvalds/linux/blob/master/Documentation/x86/exception-tables.txt

Apart from setting up the IDT, I've found the opcodes CLI and STI, but I'm quite sure that trying to use them to ignore the interrupt is a dead end.

I've looked around in the grub code and there are only a few files that reference CLI, STI, LIDT or IRET, mainly the code that makes a stage change.
I also found that the file grub-core/commands/i386/pc/drivemap.c sets up a int13h trap to handle the disk mappings.

Since I'm not very familiar with this area (and maybe I'm overcomplicating it), it seems that the approach of handling the exception could take a lot more effort than the whole module itself.

Quoted 8 lines

Now, the questions I have are about what direction I should take:

- Do I really need to make a custom handler (something like drivemap.c does), or is there any simpler way?

- I assume that I'm not the first one to need to handle a general protection exception in grub, but I couldn't find any example in other modules/commands. Any reference would be appreciated.

I think that we should have generic #GP handler which catches general protection faults.

Quoted 3 lines

- And from a practical point of view, in case that I need to setup a custom handler for this, is it mandatory to ship the patch with it or could it be added later (maybe adding a TODO now)?

I can accept this patch series without #GP handler if:
- you add warning to the GRUB doc that #GP for rdmsr/wrmsr are not handled and system reboots; so, potential users have to be careful using these modules,
- you add TODO notice to the code saying that generic #GP handling should be added to the GRUB code,
- you promise me that at some point, let's say after GRUB release, you will try to implement generic #GP handler.

Is this acceptable for you?

Daniel

#9 lore raw

Re: [PATCH v2 2/2] Add new MSR modules (rdmsr/wrmsr)

Quoted 4 lines · Daniel Kiper

On Thu, Mar 07, 2019 at 07:05:20PM +0100, Jesús Diéguez Fernández wrote:

[...]

[...]

Quoted 10 lines
Quoted 3 lines

- And from a practical point of view, in case that I need to setup a custom handler for this, is it mandatory to ship the patch with it or could it be added later (maybe adding a TODO now)?

I can accept this patch series without #GP handler if:
- you add warning to the GRUB doc that #GP for rdmsr/wrmsr are not handled and system reboots; so, potential users have to be careful using these modules,
- you add TODO notice to the code saying that generic #GP handling should be added to the GRUB code,

That's what I had in mind.

Quoted 6 lines

- you promise me that at some point, let's say after GRUB release, you will try to implement generic #GP handler.

Quoted 1 line

Is this acceptable for you?

Daniel

I think that having a generic #GP handler is a nice improvement. As I said before, I don't know exactly the size of the rabbit hole I'm entering, so I can't guarantee a delivery date, but I do guarantee that I'll do my best.

GRUB is becoming very interesting to me, if you don't mind having a newbie around here, maybe making one question or two sometimes, then we have a deal. :)

Jesus

#10 lore raw

Re: [PATCH v2 2/2] Add new MSR modules (rdmsr/wrmsr)

Quoted 12 lines · Jesús Diéguez Fernández
Quoted 4 lines · Daniel Kiper

On Thu, Mar 07, 2019 at 07:05:20PM +0100, Jesús Diéguez Fernández wrote:

[...]

[...]

Quoted 4 lines
Quoted 3 lines

- And from a practical point of view, in case that I need to setup a custom handler for this, is it mandatory to ship the patch with it or could it be added later (maybe adding a TODO now)?

Quoted 8 lines
Quoted 6 lines

I can accept this patch series without #GP handler if:
- you add warning to the GRUB doc that #GP for rdmsr/wrmsr are not handled and system reboots; so, potential users have to be careful using these modules,
- you add TODO notice to the code saying that generic #GP handling should be added to the GRUB code,

That's what I had in mind.

Perfect!

Quoted 11 lines
Quoted 6 lines

- you promise me that at some point, let's say after GRUB release, you will try to implement generic #GP handler.

Quoted 1 line

Is this acceptable for you?

Daniel

I think that having a generic #GP handler is a nice improvement. As I said before, I don't know exactly the size of the rabbit hole I'm entering, so I can't guarantee a delivery date, but I do guarantee that I'll do my best.

I do not expect delivery date from you. I am asking you to work on this issue. If you encounter any problems send us questions. We will try to help. However, please do not drop the project in the middle without informing us why. This is the worst think which can be done.

Quoted 1 line

GRUB is becoming very interesting to me, if you don't mind having a

Nice to hear that!

Quoted 1 line

newbie around here, maybe making one question or two sometimes, then we

Sure thing!

Quoted 1 line

have a deal. :)

We have the deal!

Daniel