Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 30A80C36008 for ; Tue, 1 Apr 2025 04:54:21 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tzTdJ-0006AT-3P; Tue, 01 Apr 2025 00:53:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tzTdF-0006AE-DO for grub-devel@gnu.org; Tue, 01 Apr 2025 00:53:49 -0400 Received: from mail-io1-xd2e.google.com ([2607:f8b0:4864:20::d2e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tzTdD-0000pY-JS for grub-devel@gnu.org; Tue, 01 Apr 2025 00:53:49 -0400 Received: by mail-io1-xd2e.google.com with SMTP id ca18e2360f4ac-85e14fc7483so18449639f.1 for ; Mon, 31 Mar 2025 21:53:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743483225; x=1744088025; darn=gnu.org; h=mime-version:message-id:subject:to:from:date:from:to:cc:subject :date:message-id:reply-to; bh=IX/fq9JohnJbp63peRuIWlFC8uv9lyc3EYm08YZ/AqQ=; b=V6vVibRztpnyJFruUwnwx2x/9s4GSg81bGdrYb1Xc0ousmsdLFI1kmvzIEgMOobrVH gSSlz+cKHZltQMiDx1S623t4h/uImfD+BIViYlVebepSVsVByksiuQAU1O+9Katjj3H+ Q+ItZgETr1rjrn3Qv10hjbOIjFNEqJgFeH2t/Jod3Fdwymyao0zfwn1vVWpgX2Z3aJX3 RpM7twOjagAsMsPxYl6812Yrf866S4zO5BI04E1q0/HxB9/Q72Y5kJJ6R9sYd/8T7FqB TyECe7nSSSNvr1ucOLXjIgAY9UrB5g8roNwn1p+TxBIqrjyj6lyxuQ95oimKdoznXUIH 7Dag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743483225; x=1744088025; h=mime-version:message-id:subject:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=IX/fq9JohnJbp63peRuIWlFC8uv9lyc3EYm08YZ/AqQ=; b=Zw1usLIMAKIEDspTRRXruJ8X9HhBi0xjsIzl57W1LHfDzFDI9dJcxWZgKbd4oasJ03 k97VI2KGNiDUUU5G2W6ARhEho42uCLJUODqhsfABdhJH/C7xKXJ58/lj/ym2UDieUGRt 8wcKaxSNAdQSuedhtnrTevuTq8V7RSl6mmYpSu4aB1iI7ntAvNxraEU5kiKKGo4MJPhD tXIiDIHTytHm/YF5ocZMaK1CPJDYz4Oc9aWl6+L7HpGp48qUKp0DmvlHIpYOMTOgyB3x wkR5SV7FBt+jjqAaIDBTDS0jUy1FJjbmoObkW/9saUYT9yKNKrBXOhdL7HhtdXp1a7Zn iqwA== X-Gm-Message-State: AOJu0YxKj7pERLuVY/12rp52RuOUY+ldkY0ZIIKtS3AomyfzVVRblZwc wwJgwfdUAy5yehDLuyF4oA5ilL7sdah3iQ81sxjwTe37D2SK9Sf9ORmcvg== X-Gm-Gg: ASbGncvNzd3HL4wZ9QUXXoLt50dWdUwBdY6kUUuVylBALrsbw8/4qzp9Bu4ElMoGrrS WgNyYU7gP53Ss2wCX3gdUCZOJEVU6kxlIE9eT276uAf1QJhelhRkRQtPXlgWtNvgG/q/mt1vLLc DHiCpD2eoZDakHFNiuH7WB5smuE2Z+BK0TEQ1480iOGFpUVvtr7MaRiCrg/yPrtDs9WGX+Juhvu BESihqPt9VjReYLbhmHWDOCc8tur4+KWKA0TFoKwY/blmvSmJLiComAW5HlQ0nxDDtIZHKbUQTU 9y7g3KL9Z9WC3IOUiDpnwBex2zqLuS/0d6HqmfrRWVU= X-Google-Smtp-Source: AGHT+IE6BTGRh03mGys67R4l85TUCKZXq8OWduAFUEeKp7MDjauPgg3uDk3t+FUBDiPqF7guFTvenA== X-Received: by 2002:a05:6e02:3203:b0:3d3:cea6:3cbe with SMTP id e9e14a558f8ab-3d5e09f7931mr28945325ab.3.1743483225397; Mon, 31 Mar 2025 21:53:45 -0700 (PDT) Received: from kf-m2g5 ([2607:fb90:cf8f:c3b4:cb53:5d39:7b96:2f88]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4f4648cc690sm2241439173.140.2025.03.31.21.53.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 21:53:44 -0700 (PDT) Date: Mon, 31 Mar 2025 23:53:32 -0500 From: Aaron Rainbolt To: grub-devel@gnu.org Subject: Determining when paging is and isn't enabled in GRUB Message-ID: <20250331235332.0140c919@kf-m2g5> X-Mailer: Claws Mail 4.3.1 (GTK 3.24.41; x86_64-pc-linux-gnu) MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::d2e; envelope-from=arraybolt3@gmail.com; helo=mail-io1-xd2e.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The development of GNU GRUB Content-Type: multipart/mixed; boundary="===============6214418745234039183==" Errors-To: grub-devel-bounces+grub-devel=archiver.kernel.org@gnu.org Sender: grub-devel-bounces+grub-devel=archiver.kernel.org@gnu.org --===============6214418745234039183== Content-Type: multipart/signed; boundary="Sig_/NICotP3n=CCgKmJhtHrvELH"; protocol="application/pgp-signature"; micalg=pgp-sha512 --Sig_/NICotP3n=CCgKmJhtHrvELH Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable I'm currently writing a patch for GRUB intended for the Qubes OS project (though I may later try to upstream it if it proves to work well downstream). [1] The patch's goal is to allow Xen hypervisor users to pass data to GRUB via command line arguments. This data is exposed as environment variables within GRUB itself, which can then be used in grub.cfg for customizing various parts of the boot process as the user desires. The patch is coming together pretty well (we're still discussing the details downstream), but I have hit one particularly tricky question that I don't know how to get a good answer to. When booting a VM in PV mode, Xen simply passes a start_info structure to the guest, which can then use it directly. This structure includes a cmd_line member, which can then be parsed with minimal additional effort. GRUB's startup code for Xen PV simply takes the start_info struct address and places it in grub_xen_start_page_addr, so it's already loaded with the existing GRUB code. Since Xen PV mode and GRUB work together at all, I assume everything related to memory=20 Unfortunately, this is not the case when using PVH mode - Xen seems to pass an hvm_start_info struct instead, which also contains a pointer to the command line passed by Xen... sorta. What it passes is a physical address in the form of a 64-bit integer. Now I did test this on my machine; in grub-core/kern/i386/xen/pvh.c:grub_xen_setup_pvh, I added code that casts the address to a char * and then grub_strcpy's what it points to into grub_xen_start_page_addr->cmd_line. This appears to work without problems, the command line from Xen ends up accessible to my parsing code elsewhere in GRUB. However, I feel like I'm playing with fire here - I'm assuming that either paging is disabled or the pages I'm working with are identity-mapped when I do this, and I'm assuming that writing into the cmd_line array at this point is safe at all. (I suspect the latter is safe given that grub_xen_start_page_addr is being dereferenced and members of the struct written to in this very function, but I'm really dubious about the former, just because it happens to work for me now doesn't mean it will work anywhere else.) I don't know how to determine for sure what state the page tables are in at any given point in GRUB's codebase. I did spot some other code in GRUB that assumes disabled paging or identity paging (specifically grub-core/commands/i386/pc/drivemap.c:install_int13_handler), but that's specifically for the i386-pc platform and I don't know if that will apply on any other platform GRUB supports. I can't follow the bits of assembly code that handle enabling and disabling of paging well enough to know what exactly they affect (mostly because I don't know x86 assembly, or any assembly language for that matter), I can tell that there are situations where paging is used, and sometimes those situations use identity paging, but that's about it. Is what I'm doing with physical address handling safe? And is there a good way (other than dissecting the assembly code) to determine what is and isn't safe when dealing with these kinds of situations? [1] https://github.com/QubesOS/qubes-linux-pvgrub2/pull/16 -- Aaron --Sig_/NICotP3n=CCgKmJhtHrvELH Content-Type: application/pgp-signature Content-Description: OpenPGP digital signature -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEudh48PFXwyPDa0wGpwkWDXPHkQkFAmfrcUwACgkQpwkWDXPH kQkCbA//aNxmjvnQcjcp0tZ3KsvelILa7H291jIXXBsHaXUt/DChlLkWauT8eraq izImgvS0RjfOo+xD4lCkO/pOD1Zi9a57c1n7WB0avHlZc/9ymwtb6/J9RBa0zWEz juOS6TnMpjVFm2tEqdfUvvWxpjyh64tyQZAzgf3ba4dLr7/nqV4JIe92XviRPjHM AXndTvw4YB0Ykt9jaQU/ZkZBKR3haAxVEMku6YYxWI0H5lz7NBqT+cReQvGG2+81 ctU645wvwfhGXnRlsGoPZxnQGGn+9xl7OKYbayOnvDQNU4gw/iszjZUxJ+pjuKrd WT7CbXkq7G6GI3YleDNcRz2gaIYWSYqS+ah1Q82g7rhmTXxqgOXsGEdDliDSr4hF +DD0DwA+IOKDL9JTHo2OOPcHMQd/svZoqjDHw0v65bWXT9pGsBmCUZterl0DRn51 iYMwDo4MnRhQyEbv0APXmzvXDQ56V+Ei6WTo/8zRgUNd9DXMj4uxm5iXuIRYHCRL iFNwT5sB7Z65jgW/8aPrnQ0S+jhnfiYs7/MYy3lvlGV+eezwhspL2V3UIXz7OKHe YKQL2+4DdwvLzTDp4rrQRo7ehuXsefKd+YOKrbPR2AaNR0qgBNvprbJ6ng/Bje3b DKOYgzdyx5rsmrHQknuGe+2vbT78aKVAeevKbWgA2GHQ+hJ5N3I= =XFvF -----END PGP SIGNATURE----- --Sig_/NICotP3n=CCgKmJhtHrvELH-- --===============6214418745234039183== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KR3J1Yi1kZXZl bCBtYWlsaW5nIGxpc3QKR3J1Yi1kZXZlbEBnbnUub3JnCmh0dHBzOi8vbGlzdHMuZ251Lm9yZy9t YWlsbWFuL2xpc3RpbmZvL2dydWItZGV2ZWwK --===============6214418745234039183==--