Received: from list by lists.gnu.org with archive (Exim 4.71) id 1h1INF-0000Xd-6i for mharc-grub-devel@gnu.org; Tue, 05 Mar 2019 17:17:21 -0500 Received: from eggs.gnu.org ([209.51.188.92]:49830) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h1INC-0000XE-Pc for grub-devel@gnu.org; Tue, 05 Mar 2019 17:17:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h1IN0-0000tQ-5y for grub-devel@gnu.org; Tue, 05 Mar 2019 17:17:08 -0500 Received: from mail-ed1-x543.google.com ([2a00:1450:4864:20::543]:39669) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h1IMz-0000rK-K6 for grub-devel@gnu.org; Tue, 05 Mar 2019 17:17:05 -0500 Received: by mail-ed1-x543.google.com with SMTP id p27so8588733edc.6 for ; Tue, 05 Mar 2019 14:17:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:openpgp:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=lvFNOKT6ZG6CGsVxoccMFACM1bMX2WMMB2VzTqaSAO4=; b=Xj9IFFpx30hfh/V8es9bkL34iLK0O0fzwDSUPR2BleNaufICHbGK++8H7kco5UYXPl rFRlaZV6seCXmIX5yskEo29YrHLEfTXiEln0wuEkiP8TWJHwf4WhEYsUC9JmnLHqGt3t /mRveFv3ytuMZYYgIpGBP6Yi9fB+DKn8DVh4oDjXfVAy6ep/Sxbhbtk/Q8RdXhCk8hQf rUT/Pt9f4KjNQ9vEaovBV3tKguGK5/jrjRqVEgRmtHR1XYJzLdRESekKfxxvcK10iV9v HXH9BP9o73rNB+CZwT3+CitKJOgstQLnIZYJgs6gKwz9wta0sH7juIMe10sFkyyf6D7L Ymbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:openpgp:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=lvFNOKT6ZG6CGsVxoccMFACM1bMX2WMMB2VzTqaSAO4=; b=bYNkhBTlg1RX5pa3lRI929fcTo50/GSdHnsneK20RX6D4UHPQooQae/XJRh+VmAMfh n4mJ8D4OEa6ugS8biPmrmjWN61HP9FMzJrtlTW2axrj8ajibsMwD33kYXXBA9NSRU2Iq ZfK+IwUKkOerV3l61ZPjK9iOSE2B/yHCgRjeEMSyGpxWfYrJ4AQUMcGjpgto4wCaR0lu MJHKv1vO7MGwjfb9oFbmumN+6KEApieI7AmD2qGpFm5YBlO0+35kiU4UyMwECe0eleKU kWnopy4PoaqQbqdtKwOnT4DnU5aJo7iu55VeLc+EnrX6jmqDMmcj7FjQyuwFEQ4U2BxI i/Hg== X-Gm-Message-State: APjAAAV/8nB8Omf2WGCaCWyrMG1+ulqMB5b2tDcboamkNU2irPcAhY+y gUgPxIS/kESpgNyC8VICNm7mt9pm X-Google-Smtp-Source: APXvYqzOaR6wJ1jJzEhB6NABdRvWAzIrFXqxEVMZFVRI1B8J3YVdqwhMP5Q+kECzgC390rhH1U0tyw== X-Received: by 2002:a17:906:95c3:: with SMTP id n3mr1717004ejy.59.1551824223084; Tue, 05 Mar 2019 14:17:03 -0800 (PST) Received: from [192.168.1.11] (112.red-81-47-147.staticip.rima-tde.net. [81.47.147.112]) by smtp.googlemail.com with ESMTPSA id l22sm3313266edc.68.2019.03.05.14.17.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Mar 2019 14:17:02 -0800 (PST) Subject: Re: [PATCH v2 2/2] Add new MSR modules (rdmsr/wrmsr) To: Daniel Kiper Cc: grub-devel@gnu.org References: <20190301171742.17396-1-jesusdf@gmail.com> <20190301171742.17396-2-jesusdf@gmail.com> <20190305110950.pnyso3klj2tkyrph@tomti.i.net-space.pl> From: =?UTF-8?B?SmVzw7pzIERpw6lndWV6IEZlcm7DoW5kZXo=?= Openpgp: id=25B5907521F72F0D2A85805AF2B86990C97D83AE; url=http://pgp.mit.edu:11371/pks/lookup?op=get&search=0xF2B86990C97D83AE Autocrypt: addr=jesusdf@gmail.com; prefer-encrypt=mutual; keydata= mQENBE0EQN4BCADJd9SXqsXllmTx1IIMLUTnSoMAjNzD3JTakEwkDRO2mena5MQMYosSS/lv XFtlY+16IxPOP4rTIIvInqE9D7mwWvXsuqlR8GBUH7y0wssdCmm1STAQicVSfa8hW2Gi4vrk zH3aAWdguLZeuQwtITvpGHcZOcfOsmu5KesH4nHpgUmfrSY+5ZrKLeHIwOxH0K4vAX9Cu5+B RO/2PBMvuB50cCXKMrUtOrn7UitZ8UIlgVWFolRnMoZwoMTsaDR/59nYwCUZcR5U5EwcLTrx nGT23g8kAt+keCkQLyz23LpXoBLFv4umB0+ZUki18Nv5kdBxfAOiDUBSM/jj/8P8jViPABEB AAG0Lkplc8O6cyBEacOpZ3VleiBGZXJuw6FuZGV6IDxqZXN1c2RmQGdtYWlsLmNvbT6JATgE EwECACIFAk0EQN4CGwMGCwkIBwMCBhUIAgkKCwQWAgMBAh4BAheAAAoJEPK4aZDJfYOuAucI ALN5iJnjNvRUsRLmCghisiLPbM8C2X0ctqr+fKYmN6tSZ7YKIe4dmuqnVS6PGPm2wAaW+X9E 3C/cg/ShSe9s55+v018Fq1UUXKPd6MM1CRohr5BR8YtA2uRC5hY0DpANdDop1hAM0DOb1M46 I1c/9P5NLA/mQinFikdPglzID1p2UgzoXeCNaYQuUBcOY8nXsfZM2IMtZPzXj6QT7Hpqh1qe Jw/ujucayQ5uo2ZQH2BfLhDCDG7S9YhKiy9S8CcSmgXb4a+V7r5U4kJIeu16ioKr7wMy1bjE bmN6YAEJcsddaQgH3qLi42tS2c7oIxrCPM22aqkuuOYEWjT4UPHQ3f+5AQ0ETQRA3gEIAMm6 gXWdJN8oQvYB5qxQdzXb+ph5JTzL+URMVuGJyePHIoxpy+saYlx05pPxtWmVtqTzHhahqgDw E5c8XpaY/LjJ/q3LHkUofwF1DhxbkuHYKTbIiVTXKQRiVS596N+CTECjF1WDBAtfO+AGOp98 tpPtHa5oeBuhS8X7va3qOhTIz7NXp9cM9gfZAH1y9gspxZxikVOgOHQQK3t9F34mdmfIQZj0 D3rWwCQW6kJzy0YSsB2w/ewYb1d3fS5QMZsfoVCrcN/Q3CcTR8dRtc41zDUv5zyherrCCWSk c3ghUWt6QEF4gCNJvOf22gBQBYJI/BCrLaVZuhczU2w9qAZnwdkAEQEAAYkBHwQYAQIACQUC TQRA3gIbDAAKCRDyuGmQyX2DrjRzB/0RTqLR6P+J+znCWrG+zTx/voXgUfenB1JF/lt8wDHw 9eSgmh+bvS+I6cGF0FLz9j+zpRggDGSKWuVrDZ8FsfsrMy0V6HfRJlkNtR1DpuRvM8mnTK8o or7yrwkGeGJ7IhhYMPCEPGOrWbQwiSuHUoO1B0GKMT0gqkFoCEnjp9RKwbORMdw3ya6l6SMK xKromJkKLYfwlt0Ju77k0n1CEv6b+VSHjPlctZLqu1eUyDKsewXb/K7/VyAePzg8elWCt6qK RtOfPf+43ULVI5r5uKOxDcH/nmSMJW86Jqtn6Vlyt1/YnKQQHGeed6io8XuLpl864dzTpReX 8XRRy5VFKUa8 Message-ID: <5d8f9309-5ea6-4cdc-5105-ea3aedbc348b@gmail.com> Date: Tue, 5 Mar 2019 23:17:01 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1 MIME-Version: 1.0 In-Reply-To: <20190305110950.pnyso3klj2tkyrph@tomti.i.net-space.pl> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::543 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: Tue, 05 Mar 2019 22:17:20 -0000 Hi, I have some doubts that I comment below. El 5/3/19 a las 12:09, Daniel Kiper escribió: > On Fri, Mar 01, 2019 at 06:17:42PM +0100, Jesús Diéguez Fernández wrote: >> 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. >> >> Signed-off-by: Jesús Diéguez Fernández > > 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... > > [...] 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? > >> diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def >> index 2346bd291..a966a8f28 100644 >> --- a/grub-core/Makefile.core.def >> +++ b/grub-core/Makefile.core.def >> @@ -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? 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. > >> +}; >> +module = { >> + name = wrmsr; >> + common = commands/i386/wrmsr.c; >> + enable = x86; >> + enable = i386_xen_pvh; >> + enable = i386_xen; >> + enable = x86_64_xen; > > Ditto. > [...] >> +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; >> + 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. > >> + 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_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; >> + >> + 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. > >> + 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. > >> + 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. >> + 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); >> +} [...] > > And this #endif should go below too. Like in rdmsr.h. > >> +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 > 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