From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1gv3mF-0002sO-Q6 for mharc-grub-devel@gnu.org; Sat, 16 Feb 2019 12:29:23 -0500 Received: from eggs.gnu.org ([209.51.188.92]:34983) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gv3mB-0002qj-Vh for grub-devel@gnu.org; Sat, 16 Feb 2019 12:29:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gv3mA-00030W-IB for grub-devel@gnu.org; Sat, 16 Feb 2019 12:29:19 -0500 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]:38486) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gv3mA-000307-Bl for grub-devel@gnu.org; Sat, 16 Feb 2019 12:29:18 -0500 Received: by mail-wr1-x434.google.com with SMTP id v13so13568983wrw.5 for ; Sat, 16 Feb 2019 09:29:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=AFlWYGp3Tn1WvrbfpjI/2uhdlctUfR+Da4u+TlR/ziA=; b=fdOXUTyIQTzsIo3VQVkkCPTeQefuyzHLLT1gGbiC1VaVr8hX84iDLPHJUlWw7JzZ60 K+rGV+RpxOet532hIVv7t9DULTXr8hxQE5HrJ+vRaKZ62mAJW5qKpWvyG7JD3R+eM4eg aYdfyS/9NEjRlSfd7WgQEScLFrKxsSWzc1dJYiBLY3sHGe+lf2THcea+c41ARtsy0Lps HS73mpjtZ9sGjsmCVq0EZGKeiQUJ/LJdqDBQaTgZzrgYWcmNSUBGRs/uEuhLA/qsqM6a jiapWb3jL6p+vdIzASIeVUHJz7GIbtqBigaCWzxPQtuH1WPrXm5J0B6gI8U1bf3axlc6 8xug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=AFlWYGp3Tn1WvrbfpjI/2uhdlctUfR+Da4u+TlR/ziA=; b=njcDoQoAVksSP05BEACkl7YrG7Ss3xgW4gYxmcw16QonLLETp/3bmPodO/M+TSHbhr lnwLnpzhBfqsfIVDrX2dL4YuXKom+lNrAFUdct7AqGEuiXakLkdQlTLytB2j2QOuAUSY xgbbR6ozyST2Fxca7oeOD5t2SnBaOuLzDbwHXnsv3ZMpSXrQCmJElctnBuHQeNR7rHPY AlEJz/jr8U9qdmekj3Ms3kNYi02nMaJd8Ku8gTipCmIk5ldmf+IQIf2xIC/NuC2Piufx pLpgO8KDL5oqhav79wrgBO0Qa2YgM1GjU8cBbr0WwieGtqCo4eVIqrSglWPJBZxOnOCA zTBg== X-Gm-Message-State: AHQUAuZgxbFebF3im72ew1qHppbxb4fG64Uq0CW+yfF4kA+rRZBTVl4o Rp45wEMdRdVoNAPe7FjlQ9lYRHW2Qg4= X-Google-Smtp-Source: AHgI3Iam7Ak1lKiOImv6aWxk6bnutNBs0RbrvJvAfmmF2loZn8Ij49IOePNWkEsI3Zua+MvQ8UMmoA== X-Received: by 2002:a5d:4e50:: with SMTP id r16mr6406078wrt.8.1550338157088; Sat, 16 Feb 2019 09:29:17 -0800 (PST) Received: from izanagi.home.vikt0ry.com (57.red-88-19-226.staticip.rima-tde.net. [88.19.226.57]) by smtp.gmail.com with ESMTPSA id a144sm14425078wmd.30.2019.02.16.09.29.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 16 Feb 2019 09:29:16 -0800 (PST) From: JesusDF To: grub-devel@gnu.org Cc: JesusDF Subject: [PATCH 1/1] Add a new grub module called msr that registers two commands (rdmsr and wrmsr) to be able to read and write to the model-specific registers. It is i386 specific, as the cpuid module. The name of the module and commands match the linux kernel module and intel commands to interact with it. Date: Sat, 16 Feb 2019 18:29:01 +0100 Message-Id: <20190216172901.20108-2-jesusdf@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190216172901.20108-1-jesusdf@gmail.com> References: <20190216172901.20108-1-jesusdf@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::434 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 16 Feb 2019 17:29:21 -0000 --- grub-core/Makefile.core.def | 10 ++++ grub-core/commands/i386/msr.c | 106 ++++++++++++++++++++++++++++++++++ include/grub/i386/msr.h | 67 +++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 grub-core/commands/i386/msr.c create mode 100644 include/grub/i386/msr.h diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index e16fb06ba..836a353d0 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -2455,3 +2455,13 @@ module = { common = loader/i386/xen_file64.c; extra_dist = loader/i386/xen_fileXX.c; }; + + +module = { + name = msr; + common = commands/i386/msr.c; + enable = x86; + enable = i386_xen_pvh; + enable = i386_xen; + enable = x86_64_xen; +}; diff --git a/grub-core/commands/i386/msr.c b/grub-core/commands/i386/msr.c new file mode 100644 index 000000000..ea63adf97 --- /dev/null +++ b/grub-core/commands/i386/msr.c @@ -0,0 +1,106 @@ +/* msr.c - Read or write CPU model specific registers */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2006, 2007, 2009 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 . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +GRUB_MOD_LICENSE("GPLv3+"); + +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; + grub_uint64_t value; + char buf[sizeof("XXXXXXXX")]; + + if (argc != 1) + { + return grub_error(GRUB_ERR_BAD_ARGUMENT, N_("one argument expected")); + } + + addr = grub_strtoul(argv[0], 0, 0); + + 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; +} + +static grub_err_t +grub_cmd_msr_write(grub_command_t cmd, int argc, char **argv) +{ + grub_addr_t addr; + grub_uint32_t value; + + if (argc != 2) + { + return grub_error(GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected")); + } + + addr = grub_strtoul(argv[0], 0, 0); + value = grub_strtoul(argv[1], 0, 0); + + grub_msr_write(addr, value); + + return GRUB_ERR_NONE; +} + +static grub_extcmd_t cmd_read; +static grub_extcmd_t cmd_write; + +GRUB_MOD_INIT(msr) +{ + cmd_read = grub_register_extcmd("rdmsr", grub_cmd_msr_read, 0, + N_("ADDR"), + N_("Read a CPU model specific register."), + options); + + 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(msr) +{ + grub_unregister_extcmd(cmd_read); + grub_unregister_command(cmd_write); +} diff --git a/include/grub/i386/msr.h b/include/grub/i386/msr.h new file mode 100644 index 000000000..a14b3dcfb --- /dev/null +++ b/include/grub/i386/msr.h @@ -0,0 +1,67 @@ +/* + * 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 . + */ + +#ifndef GRUB_CPU_MSR_HEADER +#define GRUB_CPU_MSR_HEADER 1 +#endif + +#ifdef __x86_64__ + +extern __inline grub_uint64_t grub_msr_read (grub_uint64_t msr_id) +{ + grub_uint32_t low; + grub_uint32_t high; + __asm__ __volatile__ ( "rdmsr" + : "=a"(low), "=d"(high) + : "c"(msr) + ); + return ((grub_uint64_t)high << 32) | low; +} + +extern __inline void grub_msr_write(grub_uint64_t msr, grub_uint64_t value) +{ + grub_uint32_t low = value & 0xFFFFFFFF; + grub_uint32_t high = value >> 32; + __asm__ __volatile__ ( + "wrmsr" + : + : "c"(msr), "a"(low), "d"(high) + ); +} + +#else + +extern __inline grub_uint64_t grub_msr_read (grub_uint64_t msr_id) +{ + /* We use uint64 in msr_id just to keep the same function signature */ + grub_uint32_t low_id = msr_id & 0xFFFFFFFF; + grub_uint64_t msr_value; + __asm__ __volatile__ ( "rdmsr" : "=A" (msr_value) : "c" (low_id) ); + return msr_value; +} + +extern __inline void grub_msr_write(grub_uint64_t msr_id, grub_uint64_t msr_value) +{ + /* We use uint64 in msr_id just to keep the same function signature */ + grub_uint32_t low_id = msr_id & 0xFFFFFFFF; + grub_uint32_t low_value = msr_value & 0xFFFFFFFF; + __asm__ __volatile__ ( "wrmsr" : : "c" (low_id), "A" (low_value) ); +} + +#endif + -- 2.17.1