Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2349282rdf; Mon, 6 Nov 2023 11:21:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IEPPSmDMeEF/2r9sYXKL1ZjkhgYdMQRyBE5OZg+vH0ekiW5YZnMY+uNC5PsULhMxJ/7tr8V X-Received: by 2002:a05:6808:1281:b0:3a8:80ea:f0c6 with SMTP id a1-20020a056808128100b003a880eaf0c6mr36690870oiw.29.1699298480615; Mon, 06 Nov 2023 11:21:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699298480; cv=none; d=google.com; s=arc-20160816; b=tw5IxO0Oul+vqp6mmYzVh00niOWdIMNtcvJh4tfkyPL4ICXo7XffhYntQ+l7kI5WKk 6faGsUicKD84FCzgA8NyLnCRKvocP8jIRAh+g6O3BwkFoWJTQkyVIX9npUgKcan1b2E+ H0dGHafx+iW00HgWKkmtB+du+4QSdQnAPuRC+s/m18Wecwd5+4m/mGDrcJvBndU88m4S nT17mQ+5a9zQMZGeBnPyT8o7MfvhkWzyNwQO8WEGhJWF6XKWfu1Wt6J8YghiuEAfgtFP WtMRcdomabY06yLmZi8TcRUremXhpNsYy0EUojaytKViTTHUx5USNV1tf44C+MRStQJa grYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:references:in-reply-to:subject:cc:to:from :dkim-signature; bh=LzCNFvwKhoFrd79Cm58D56ekqVnBIBNuFuzGlHN4QO4=; fh=ob7vmQ3Tc+NAIDXwn1UyERfWahM4nLBkZkM5puezK+4=; b=Y59xZG/wD1eBxPuDooOjVfnqy23YFOv4X70BI3EWm/yEMjJP/LU/R594tU58O/jQCr fe7War7NicZYnQsYogfhu8G+LRgxZ/mtbvgs8mfmoQguUds6qbVWorYXIbVx3uVBmk58 17ekmAqWzYdU3z6GaZkeg6Gz6fTQm7OEbzAQTQ7s6ynZb5etxLrYF9DkIA8p98vkoULo qhispjLOk5k9epGA7V6jLVGQuAq1zVOUXdGubYrXh92RhE3Im7WdKA0PtWe46s8hb0Ah kMS76B+4wdOzwQwrUdStIatl1z/vnX2UXxpJsh1M1bv0ck/oaQt4h/1S2Kyddj5XmjEg GVWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CzC22BIR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id g16-20020aa78750000000b006b28fa70b48si8221013pfo.354.2023.11.06.11.21.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 11:21:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CzC22BIR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 389878039C88; Mon, 6 Nov 2023 11:21:18 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232213AbjKFTVL (ORCPT + 99 others); Mon, 6 Nov 2023 14:21:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232001AbjKFTVJ (ORCPT ); Mon, 6 Nov 2023 14:21:09 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46CB9BF for ; Mon, 6 Nov 2023 11:21:06 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CEE79C433C8; Mon, 6 Nov 2023 19:21:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699298465; bh=DNSe1kWEmQIx9RbnW/5BezE7eNr7BvDl2djBhzSMnZI=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=CzC22BIRJwDgnZhVgMFNrwGQ5hFQ1zOJg1uk/iLLj+M01dpVFAB45brwdB7ujupSF R7/p/7lDbOlboRHRksVysmedjqGCD1rEj15+YNNxEcKbvR0ZcwMgUd15YNSmtAWfSV guY0MJwBtUmQzi//vxXtES0P3GhkIEwIpD6Bu8fdpkAi2FC7brpjThZtcDfG93o4e5 g/sK7RDWpD17ghwsc1iI92Jxz6pi5YwauAUg8efYDQQD0IIAWjka94bIdhbjb81feI nIwR6qQSwjozFZsIPET4cK/SLAYZD9UfZILECpFhS4zLNlFRJ3+QUjJMOCt3em/XSW HkMIi/enxHOpQ== From: =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= To: Palmer Dabbelt Cc: aou@eecs.berkeley.edu, anup@brainfault.org, atishp@atishpatra.org, Atish Patra , Paul Walmsley , Bjorn Topel , Ard Biesheuvel , sunilvl@ventanamicro.com, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: Re: [PATCH] riscv: CONFIG_EFI should not depend on CONFIG_RISCV_ISA_C In-Reply-To: References: Date: Mon, 06 Nov 2023 20:21:04 +0100 Message-ID: <87ttpyu1r3.fsf@all.your.base.are.belong.to.us> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 06 Nov 2023 11:21:18 -0800 (PST) Palmer Dabbelt writes: > On Tue, 24 Oct 2023 12:26:48 PDT (-0700), bjorn@kernel.org wrote: >> From: Bj=C3=B6rn T=C3=B6pel >> >> UEFI/PE mandates that the kernel Image starts with "MZ" ASCII >> (0x5A4D). Convenient enough, "MZ" is a valid compressed RISC-V >> instruction. This means that a non-UEFI loader can simply jump to >> "code0" in the Image header [1] and start executing. >> >> The Image specification [1] says the following about "code0": >> | This header is also reused to support EFI stub for RISC-V. EFI >> | specification needs PE/COFF image header in the beginning of the >> | kernel image in order to load it as an EFI application. In order >> | to support EFI stub, code0 is replaced with "MZ" magic string >> | and res3(at offset 0x3c) points to the rest of the PE/COFF >> | header. >> >> "MZ" is not a valid instruction for implementations without the C >> extension. >> >> A non-UEFI loader, loading a non-C UEFI image have the following >> options: >> 1. Trap and emulate "code0" >> 2. Avoid "code0" if it is "MZ", and have the kernel entry at >> "code1". >> >> Replace the compressed instruction with a hex code variant, that works >> for CONFIG_RISCV_ISA_C=3Dn builds. Further, this change also make sure >> that the "code0" instruction is 32b aligned. > > IMO this breaks the Image format on non-C systems: they'll jump straight= =20 > to the start (as there's no symbols left or anything) and then just fail= =20 > to execute the C instructions. That's right! My idea, which was probably really poor articulated, was to add trap/emulate in OpenSBI/non-C for the MZ instructions. > We could amend the Image format to require bootloaders to handle this=20 > (ie, look at the first instructions and emulate them if there's no C),=20 > that would require some bootloader updates but given this doesn't work=20 > maybe that's fine. > > We could also just stop producing Image+PE binaries on non-C systems=20 > (ie, just produce PE). Yes, or make the Image loader in Qemu et al more robust, by e.g. checking code0/code1. The Image spec does say that code0 is 'MZ' for PE+Image builds, and for non-C capable systems one could argue that it should be checked/skipped by the loader. Does the arguments above make sense for you? I.e.: 1. Merge this patch 2a. Add the trap/emu to OpenSBI (2b. Improve the image loader in Qemu?) Bj=C3=B6rn