Received: from list by lists.gnu.org with archive (Exim 4.71) id 1h1xOe-00077Y-Ez for mharc-grub-devel@gnu.org; Thu, 07 Mar 2019 13:05:32 -0500 Received: from eggs.gnu.org ([209.51.188.92]:48248) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h1xOZ-00075K-Ko for grub-devel@gnu.org; Thu, 07 Mar 2019 13:05:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h1xOX-00068h-Cd for grub-devel@gnu.org; Thu, 07 Mar 2019 13:05:27 -0500 Received: from mail-ed1-x541.google.com ([2a00:1450:4864:20::541]:44572) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h1xOX-00066R-1B for grub-devel@gnu.org; Thu, 07 Mar 2019 13:05:25 -0500 Received: by mail-ed1-x541.google.com with SMTP id b20so14228815edw.11 for ; Thu, 07 Mar 2019 10:05:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:openpgp:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=OY+GMNg0qsE7cw32Yv5X2kfgKOUYhWo8pFEWmL8iFPE=; b=QAOGxXmX1XzaaiAeiKRkqmmj+URwojLcAwRLoyv62qV9EpMqVb6fo2Tl2iMBZ9b7h7 eVf8lYCPqULnZkL/qWnyEYMXvCyfsFRen8/bV6LZ/XoVeMZ062Q9JTD4Xn4zHS6qS+ug qjUqR2HmYoAPIgizB7umVLuiFz3cUR0RXTUjM3SBorFh2307s5uV6WYmxLwP8jI63ehM EVfligM2TgODs0Xk9fQGHsWRqqCGhqmNHFSlWpOlXqKndIMpO099Csr7b1/PGStm5vnx 7SIvO4SLUCmUyqq1odbaOLn2vEPWI4V6prk2DwaTeBdQumA3ckaQ7S+gCZdvmtvo1osn 1Gyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:openpgp:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=OY+GMNg0qsE7cw32Yv5X2kfgKOUYhWo8pFEWmL8iFPE=; b=e2jheeWhBbvkBNOaQ1H2otwSEgavotKX88mWLd8rAZyT7BEqYZgKQzcsIZHkuNZOcU 4sPGnLw2r5SXIAa5sGGsENfRS/Xiw2atnoiEWetQ//YnysTkHPODdu3HBXfRI5vTmGhi KIpzjEg4bYs7Gc5CAisAW+Pc9zLrRBN6850BLQ0Rd8UTrNiiJpmcPl6Y4+tmViVV5G/u szTSB0s4G6/xxTXK9EQe7bQADD6v4VGPqQ+Q5x2PbeC866VvGOsSkXPMgADizvJ1V3wR 9yuU+KDfUo2QX5Lhf0A8RIsD/9YcfR3JhCHZb3jwr9qut2vJc6pCc39AfA3GAtfq6m4D 71bw== X-Gm-Message-State: APjAAAWtLX1qgKINX9CnWB59CVjNCPLUwdGc0zULgyxxZdjSHL2jEQxK r+qvHmr8Ak3DRyGsZC5Ukhm3pJHW X-Google-Smtp-Source: APXvYqzw7jKAWvNutthUnQrY5B09HUSl0jsqWGjTacGAfbxSIEz3uuiIsM6FjuFGmYTQURqnz5VT0g== X-Received: by 2002:a17:906:c2d9:: with SMTP id ch25mr8827806ejb.180.1551981922921; Thu, 07 Mar 2019 10:05:22 -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 f40sm1493744eda.25.2019.03.07.10.05.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Mar 2019 10:05:22 -0800 (PST) Subject: Re: [PATCH v2 2/2] Add new MSR modules (rdmsr/wrmsr) To: Daniel Kiper , grub-devel@gnu.org References: <20190301171742.17396-1-jesusdf@gmail.com> <20190301171742.17396-2-jesusdf@gmail.com> <20190305110950.pnyso3klj2tkyrph@tomti.i.net-space.pl> <5d8f9309-5ea6-4cdc-5105-ea3aedbc348b@gmail.com> <20190306103246.pkamttlk5gbsqcxv@tomti.i.net-space.pl> <20190306143057.gkthocy5t6ojbzks@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: Date: Thu, 7 Mar 2019 19:05:20 +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: <20190306143057.gkthocy5t6ojbzks@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::541 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: Thu, 07 Mar 2019 18:05:31 -0000 El 6/3/19 a las 15:30, Daniel Kiper escribió: > On Wed, Mar 06, 2019 at 01:01:07PM +0100, Jesús DF wrote: >> El mié., 6 mar. 2019 a las 11:32, Daniel Kiper () escribió: >>> On Tue, Mar 05, 2019 at 11:17:01PM +0100, Jesús Diéguez Fernández wrote: >>>> 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. :-\ >>> >>> Not big deal... >>> >>>> 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. > > [...] > >>>>>> +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); >>> >>> 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 >> >> Yes, I already did change addr to be 32-bit type in my local branch. > > Great! > >> 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. > >> 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