From mboxrd@z Thu Jan 1 00:00:00 1970 From: Heinrich Schuchardt Date: Thu, 30 Jul 2020 13:04:15 +0200 Subject: efi_loader/RISC-V: misaligned load when running grubriscv64.efi In-Reply-To: References: <7f999242-4c12-620d-1198-20a197d4c801@gmx.de> Message-ID: <941aa434-47fe-0456-54d0-4a2d70c02d08@gmx.de> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de 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? @Ard How about the EFI part of the Linux kernel? Best regards Heinrich From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1k16Mh-0003Sb-QB for mharc-grub-devel@gnu.org; Thu, 30 Jul 2020 07:04:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57578) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k16Mc-0003Rz-VC for grub-devel@gnu.org; Thu, 30 Jul 2020 07:04:44 -0400 Received: from mout.gmx.net ([212.227.15.15]:50795) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k16Ma-0001sH-DC for grub-devel@gnu.org; Thu, 30 Jul 2020 07:04:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1596107058; bh=slGMtQyTWUKw/pN+V/v+tnYLEObkA36PfNrwnYKHRYA=; h=X-UI-Sender-Class:Subject:To:Cc:References:From:Date:In-Reply-To; b=CehSTteXg/DOBVHB12sOfb/t1sG1+uknJlECLo0GKcPK3UCiy2aTDT/3KjAPV+h/Q TJkGEucyIdBa1rNI+ZtybX48rk36LNc8eETdM0UMyxpKNaiql8RWhmgRMTCz/79zel eQqDTH+x99/d52I9VXPmOvSbyV1ARktMS0yMjLa8= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.123.70] ([178.202.41.107]) by mail.gmx.com (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MVNAr-1kAlXh0ytb-00SRD7; Thu, 30 Jul 2020 13:04:18 +0200 Subject: Re: efi_loader/RISC-V: misaligned load when running grubriscv64.efi To: Sean Anderson , Leif Lindholm Cc: Alexander Graf , U-Boot Mailing List , Rick Chen , Daniel Kiper , GRUB mailing list , Ard Biesheuvel References: <7f999242-4c12-620d-1198-20a197d4c801@gmx.de> From: Heinrich Schuchardt Autocrypt: addr=xypron.glpk@gmx.de; prefer-encrypt=mutual; keydata= mQINBE2g3goBEACaikqtClH8OarLlauqv9d9CPndgghjEmi3vvPZJi4jvgrhmIUKwl7q79wG IATxJ1UOXIGgriwoBwoHdooOK33QNy4hkjiNFNrtcaNT7uig+BG0g40AxSwVZ/OLmSFyEioO BmRqz1Zdo+AQ5RzHpu49ULlppgdSUYMYote8VPsRcE4Z8My/LLKmd7lvCn1kvcTGcOS1hyUC 4tMvfuloIehHX3tbcbw5UcQkg4IDh4l8XUc7lt2mdiyJwJoouyqezO3TJpkmkayS3L7o7dB5 AkUwntyY82tE6BU4quRVF6WJ8GH5gNn4y5m3TMDl135w27IIDd9Hv4Y5ycK5sEL3N+mjaWlk 2Sf6j1AOy3KNMHusXLgivPO8YKcL9GqtKRENpy7n+qWrvyHA9xV2QQiUDF13z85Sgy4Xi307 ex0GGrIo54EJXZBvwIDkufRyN9y0Ql7AdPyefOTDsGq5U4XTxh6xfsEXLESMDKQMiVMI74Ec cPYL8blzdkQc1MZJccU+zAr6yERkUwo1or14GC2WPGJh0y/Ym9L0FhXVkq9e1gnXjpF3QIJh wq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qSZ DDw0aNy1mlCv6KmJBRENfOIZBFUqXCtODPvO5TcduJV/5XuxbTR/33Zj7ez2uZkOEuTs/pPN oKMATC28qfg0qM59YjDrrkdXi/+iDe7qCX93XxdIxpA5YM/ZiqgwziJX8ZOKV7UDV+Ph5KwF lTPJMPdQZYXDOt5DjG5l5j0cQWqE05QtYR/V6g8un6V2PqOs9WzaT/RB12YFcaeWlusa8Iqs Eg== Message-ID: <941aa434-47fe-0456-54d0-4a2d70c02d08@gmx.de> Date: Thu, 30 Jul 2020 13:04:15 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:WWVZ1JF+ABbCSE/cRFClYaaJW7sSvp0DtALC2/n1ilyAXLbOHUV kJCB7uv+gqggjd+fwvzu6+vuSdH83uqiZKNUSQi1xs7FO7I//9qYBQhhhLnXpXDsw97xHUF HTkaIVNRVR3kV5LR0Sta+jzPNX9n/XtPxVIORDwaVAq2Kxgj2wqVbBWpS3Zdp7kT84HEsxW 11XpfyDNKhHUPCzutHupw== X-UI-Out-Filterresults: notjunk:1;V03:K0:/yEra5+aHTE=:4l8p9850LZ3F5TaRLIqkIQ Br5V+x3I4USNc8DP7dyeN7su8V1WDXNEohDdLXOhXu9dd91H64yjUqDH4xL9Ms9AZS2cKlk5v R+wimpCqKeJeVAvpH9id2QCaaP7BSmx1Ff5YHQUGWKfJTY7hS28CGPk8w7Ib1g5IJNNLwIDoK 8abCJ16gCxoRHJxtZiJMaS7j+Del1XWueQXQlGOAWMjGbSaXDpM5XwNjBVeUtI6y5wT+xpck7 NBMA9whszNjG/LIOe88GRXdxiY09lGslseJakUDGfeRaogp0x8Z8+3FPJxe9NAdaxZ+HuIboo 0N460u7zyQvKWs5gkzGIVzo1bx/FoR3mWGoAmM7RPvbjmgu/mRKtLh/99aYhIqNTmbz+2F9/I Sy9lcXvoDO+rWCZEk5GsmEAVdPBHnaiZyO87xDNUukPVrm5ytNlTJE+sJi/3a0feN02A8iL8m XarsLzAn5sFJtfwZbl6sB7+Vssx3cgfyCCxMzqIIp/YoFrq0NRMPrroojkvRXGb3gAbG/2Abo Jq7yPSJTpWHNAAAygGuVe+02z6v6eo23iXmtS6t6ASXV+Je7WW+UY/LwoX0Cn3GJH2m3ijS6/ 7pwfKADlV+fTgdJp7dxv+G55Gz7L7STNcZyv0u+oeuZWJF2pp0Hg2NofichB4vyb+Buue8dw7 AAtN8CvnFhi7wHdTEkYFIhwgJoyYSl5Z0J3L633sb6/o8v7VL0z6NzMdCTSBb6o14Tn514SVC Mmcw5UdIY6p7b51GxbC/rEOoVlHrO3Dm16ZjJUpaQZXbCVhBzYctlm6JIIcdghgKKb+iKOzk2 F6nfgfmPxmZq0g1IOnxeMC9+fw4WWO/r12NqWDvL9mD++KmS53sUrpjUqsYZ3IBGq+1NmZyI1 0hQTnpSwcFLuuhN2suYWb2+WP+8xfpAbx62ASH2PjQxY0aK7Xk30fTZZHBzfHja7SI3gRVqCi Y2wYLZ37+FvRJI4FwFyAK/aZQ/AYm+q0LudbdgCWL/XGbzkUqMFX3cD8lOy4uD81AHSAEhoyx GIYbsHXbyV1MfvAKPWu1gHXUgtxd1WdB+bTyE77UfMX3tG8JBdSNdPHMBjNBI16LDRw0b58+N 7aOVZ6yG6qzFW0p3mRw6Oul7vX++NnxaoOqHVS0/kyc3KkfE2oPVCpdS4h7ANDeryIOHlALv3 0nLReSupBpKd4ANM9yZwhCD0pM9z1i1vkoRPhAAe4dKI3xBZn8MFi0Bosb302L5FAlzS3Oqt8 3W3xqUtsGMEUNyL60njIm+XquAOIS/eER5wBK3w== Received-SPF: pass client-ip=212.227.15.15; envelope-from=xypron.glpk@gmx.de; helo=mout.gmx.net X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/30 05:19:48 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-1, 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 11:04:44 -0000 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 =3D efi_reloc + offset; >> *x64 +=3D (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/b95= 9661626b38a738673a9efb2f398b2fabd5c77/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=3D0x1004 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? @Ard How about the EFI part of the Linux kernel? Best regards Heinrich