From mboxrd@z Thu Jan 1 00:00:00 1970 From: Atish Patra Date: Thu, 30 Jul 2020 11:31:47 -0700 Subject: efi_loader/RISC-V: misaligned load when running grubriscv64.efi In-Reply-To: <941aa434-47fe-0456-54d0-4a2d70c02d08@gmx.de> References: <7f999242-4c12-620d-1198-20a197d4c801@gmx.de> <941aa434-47fe-0456-54d0-4a2d70c02d08@gmx.de> Message-ID: List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de On Thu, Jul 30, 2020 at 4:04 AM Heinrich Schuchardt wrote: > > On 30.07.20 12:16, Sean Anderson wrote: > > On 7/30/20 6:03 AM, Heinrich Schuchardt wrote: > >> Dear Sean, > >> > >> when trying to run grubriscv64.efi from the > >> trini/u-boot-gitlab-ci-runner:bionic-20200526-18Jun2020 Docker image on > >> a MAIXDUINO the relocations are not naturally aligned: > >> > >> lib/efi_loader/efi_image_loader.c(133) efi_loader_relocate(): > >> > >> efi_reloc 000000008030a000, offset 0x101e, type 10 > >> > >> Here we are trying to change an u64 at 0x8030B01E: > >> > >> uint64_t *x64 = efi_reloc + offset; > >> *x64 += (uint64_t)delta; > >> > >> This leads to an exception in function efi_loader_relocate(): > >> > >> Unhandled exception: Load address misaligned > >> EPC: 00000000805a95ac RA: 00000000805a953a TVAL: 000000008030b01e > >> EPC: 000000008001c5ac RA: 000000008001c53a reloc > >> > >> The GRUB image is available here: > >> > >> https://gist.github.com/xypron/522a91962248e9c3888d8554cb61ad2c/raw/b959661626b38a738673a9efb2f398b2fabd5c77/grubriscv64.efi > >> > >> On QEMU the GRUB image is executed without problems: > >> > >> https://gitlab.denx.de/u-boot/custodians/u-boot-efi/-/jobs/132919 > >> > >> The UEFI specification requires for the ARM platform that unaligned > >> support is enabled. This is why we have implemented function > >> allow_unaligned(). > >> > >> On RISC-V we have not yet implemented allow_unaligned() yet. Is there a > >> way to switch RISCV64 CPUs especially the Kendryte K210 into a mode > >> supporting unaligned access? > > > > AFAIK RISC-V has no requirement that un-aligned loads/stores complete. I > > believe the recommended solution is to install a trap handler which > > completes the un-aligned load through a series of aligned loads and then > > returns back to the application. For an example of such an > > implementation, check out arch/riscv/kernel/traps_misaligned.c in Linux. > > This may be too complex for U-Boot, so perhaps you can simply disallow > > unaligned accesses? > > > > --Sean > > > > Working around the problem inside U-Boot is easy (just some memcpy() > calls) but the GRUB image itself also makes unaligned accesses: > > Unhandled exception: Load address misaligned > EPC: 000000008030b004 RA: 00000000805a4eca TVAL: 000000008030b02e > EPC: 000000007fd7e004 RA: 0000000080017eca reloc > > UEFI image [0x000000008030a000:0x0000000080433fff] pc=0x1004 > > This is what I found in "RISC-V Unprivileged ISA V20191213" > > "Loads and stores where the effective address is not naturally > aligned to the referenced datatype (i.e., on a four-byte boundary for > 32-bit accesses, and a two-byte boundary for 16-bit accesses) have > behavior dependent on the EEI. An EEI may guarantee that misaligned > loads and stores are fully supported, and so the software running inside > the execution environment will never experience a contained or fatal > address-misaligned trap." > > @Leif > Should GRUB be built with -mstrict-align for RISC-V? > That shouldn't be necessary. Any real board with an MMU that can boot Linux needs a SBI provider such as OpenSBI. OpenSBI already implements a misaligned handler. Are we planning to support EFI booting for NoMMU platforms ? As per my understanding runtime services need to be mapped through kernel page tables. > @Ard > How about the EFI part of the Linux kernel? > > Best regards > > Heinrich -- Regards, Atish From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1k1DLe-00065E-9M for mharc-grub-devel@gnu.org; Thu, 30 Jul 2020 14:32:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39202) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k1DLb-00064t-Ci for grub-devel@gnu.org; Thu, 30 Jul 2020 14:32:07 -0400 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]:35783) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k1DLW-0004A3-SC for grub-devel@gnu.org; Thu, 30 Jul 2020 14:32:06 -0400 Received: by mail-wm1-x342.google.com with SMTP id 184so7118571wmb.0 for ; Thu, 30 Jul 2020 11:32:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=atishpatra.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=+xHS/0DVK5ySpAHiN8IEgWo+UwfOYdhzou9FxjvQgFY=; b=uL3Z1FFzUYBsdNDW7X7eyq83FQHf+lXJNiRBCpbb7QbFTsmhzyTsmtlrneKEZgdXAd CsBbaXwBKRwaAAS8zaLDycmOpN5NL2K0qdwGDHTgdqJv5Nk7/AlELf3B0CwlKb3OMAuZ i6bpe3SHsARbudiHtubxHXFIfRvw015S0/iJY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=+xHS/0DVK5ySpAHiN8IEgWo+UwfOYdhzou9FxjvQgFY=; b=DI/TJsmxsHGt7l0/cbrywg1fq0R3FfU7My1IIVlLAsR3XXUW7HHfDVYkA7qb7+XTuJ CoEDiq/ieDoc2jvlA1KfFZ3gHGJJyVFKrUfLEmALbEJaNpMQFPQ3VSc4apZQT3VXJUQJ pSmcNGi76yJ3WnCutr1WO2rUYKIPLkJ+VyZzPgRkx3Nt/0iupaanpE74QZ2L/05amwvr AKbyErgRko0zvJS/S4ZeqMns/9Jt02yXQYmxeY5icCCVs1aIQIBrt8hGSpd/xup8xxz8 WaEmonkGHmt1FBmmixSqkdt+75GPJ1Vx2g4svnQ9g75rGRszHX6IzMv/QceM+pcrIKcq y+dA== X-Gm-Message-State: AOAM5306y9qGTBFygGmjXvnlQus9p77zwyI4fl7ryqdY4Am3zBvOkzNr m9t3gdqKaYLEKX2/mueMFSRhm+L53N64CH+oRY/G X-Google-Smtp-Source: ABdhPJyn0+mqllAIr6R5saor4WnewWrB2elC7ThbcnNvWt33X4GCLhDP4c1JJ1+iHXp+CbC3/UtOi+qH2TWA/XB/RQw= X-Received: by 2002:a05:600c:2154:: with SMTP id v20mr479862wml.186.1596133919004; Thu, 30 Jul 2020 11:31:59 -0700 (PDT) MIME-Version: 1.0 References: <7f999242-4c12-620d-1198-20a197d4c801@gmx.de> <941aa434-47fe-0456-54d0-4a2d70c02d08@gmx.de> In-Reply-To: <941aa434-47fe-0456-54d0-4a2d70c02d08@gmx.de> From: Atish Patra Date: Thu, 30 Jul 2020 11:31:47 -0700 Message-ID: Subject: Re: efi_loader/RISC-V: misaligned load when running grubriscv64.efi To: Heinrich Schuchardt Cc: Sean Anderson , Leif Lindholm , Alexander Graf , U-Boot Mailing List , Rick Chen , Daniel Kiper , GRUB mailing list , Ard Biesheuvel Content-Type: text/plain; charset="UTF-8" Received-SPF: pass client-ip=2a00:1450:4864:20::342; envelope-from=atishp@atishpatra.org; helo=mail-wm1-x342.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 30 Jul 2020 18:32:07 -0000 On Thu, Jul 30, 2020 at 4:04 AM Heinrich Schuchardt wrote: > > On 30.07.20 12:16, Sean Anderson wrote: > > On 7/30/20 6:03 AM, Heinrich Schuchardt wrote: > >> Dear Sean, > >> > >> when trying to run grubriscv64.efi from the > >> trini/u-boot-gitlab-ci-runner:bionic-20200526-18Jun2020 Docker image on > >> a MAIXDUINO the relocations are not naturally aligned: > >> > >> lib/efi_loader/efi_image_loader.c(133) efi_loader_relocate(): > >> > >> efi_reloc 000000008030a000, offset 0x101e, type 10 > >> > >> Here we are trying to change an u64 at 0x8030B01E: > >> > >> uint64_t *x64 = efi_reloc + offset; > >> *x64 += (uint64_t)delta; > >> > >> This leads to an exception in function efi_loader_relocate(): > >> > >> Unhandled exception: Load address misaligned > >> EPC: 00000000805a95ac RA: 00000000805a953a TVAL: 000000008030b01e > >> EPC: 000000008001c5ac RA: 000000008001c53a reloc > >> > >> The GRUB image is available here: > >> > >> https://gist.github.com/xypron/522a91962248e9c3888d8554cb61ad2c/raw/b959661626b38a738673a9efb2f398b2fabd5c77/grubriscv64.efi > >> > >> On QEMU the GRUB image is executed without problems: > >> > >> https://gitlab.denx.de/u-boot/custodians/u-boot-efi/-/jobs/132919 > >> > >> The UEFI specification requires for the ARM platform that unaligned > >> support is enabled. This is why we have implemented function > >> allow_unaligned(). > >> > >> On RISC-V we have not yet implemented allow_unaligned() yet. Is there a > >> way to switch RISCV64 CPUs especially the Kendryte K210 into a mode > >> supporting unaligned access? > > > > AFAIK RISC-V has no requirement that un-aligned loads/stores complete. I > > believe the recommended solution is to install a trap handler which > > completes the un-aligned load through a series of aligned loads and then > > returns back to the application. For an example of such an > > implementation, check out arch/riscv/kernel/traps_misaligned.c in Linux. > > This may be too complex for U-Boot, so perhaps you can simply disallow > > unaligned accesses? > > > > --Sean > > > > Working around the problem inside U-Boot is easy (just some memcpy() > calls) but the GRUB image itself also makes unaligned accesses: > > Unhandled exception: Load address misaligned > EPC: 000000008030b004 RA: 00000000805a4eca TVAL: 000000008030b02e > EPC: 000000007fd7e004 RA: 0000000080017eca reloc > > UEFI image [0x000000008030a000:0x0000000080433fff] pc=0x1004 > > This is what I found in "RISC-V Unprivileged ISA V20191213" > > "Loads and stores where the effective address is not naturally > aligned to the referenced datatype (i.e., on a four-byte boundary for > 32-bit accesses, and a two-byte boundary for 16-bit accesses) have > behavior dependent on the EEI. An EEI may guarantee that misaligned > loads and stores are fully supported, and so the software running inside > the execution environment will never experience a contained or fatal > address-misaligned trap." > > @Leif > Should GRUB be built with -mstrict-align for RISC-V? > That shouldn't be necessary. Any real board with an MMU that can boot Linux needs a SBI provider such as OpenSBI. OpenSBI already implements a misaligned handler. Are we planning to support EFI booting for NoMMU platforms ? As per my understanding runtime services need to be mapped through kernel page tables. > @Ard > How about the EFI part of the Linux kernel? > > Best regards > > Heinrich -- Regards, Atish