title: U-Boot with OpenSBI on HiFive Unleashed in QEMU: Store/AMO access fault
body: <p>I've been trying to get U-Boot to work in QEMU for the <code>sifive_u</code> machine. I've tried using both the built in OpenSBI 'BIOS' and building my own OpenSBI, but I keep running into the same issue.</p>
  
  <p>I'm building U-Boot using the riscv64 toolchain from the Arch repos:</p>
  
  <pre><code>➜  u-boot git:(0b0c6af387) riscv64-linux-gnu-gcc --version
  riscv64-linux-gnu-gcc (GCC) 9.2.0
  Copyright (C) 2019 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  ➜  u-boot git:(0b0c6af387) export CROSS_COMPILE=riscv64-linux-gnu-
  ➜  u-boot git:(0b0c6af387) export ARCH=riscv
  ➜  u-boot git:(0b0c6af387) make sifive_fu540_defconfig
  ...
  ➜  u-boot git:(0b0c6af387) make
  </code></pre>
  
  <p>I've tried to run this as follows:</p>
  
  <pre><code>➜  opensbi git:(master) ✗ qemu-system-riscv64 -M sifive_u -m 256M -bios default -display none -serial stdio -device loader,addr=0x80200000,file=../u-boot/u-boot.bin
  
  OpenSBI v0.5 (Oct  9 2019 12:03:04)
     ____                    _____ ____ _____
    / __ \                  / ____|  _ \_   _|
   | |  | |_ __   ___ _ __ | (___ | |_) || |
   | |  | | '_ \ / _ \ '_ \ \___ \|  _ &lt; | |
   | |__| | |_) |  __/ | | |____) | |_) || |_
    \____/| .__/ \___|_| |_|_____/|____/_____|
          | |
          |_|
  
  Platform Name          : SiFive Freedom U540
  Platform HART Features : RV64ACDFIMSU
  Platform Max HARTs     : 5
  Current Hart           : 1
  Firmware Base          : 0x80000000
  Firmware Size          : 96 KB
  Runtime SBI Version    : 0.2
  
  PMP0: 0x0000000080000000-0x000000008001ffff (A)
  PMP1: 0x0000000000000000-0xffffffffffffffff (A,R,W,X)
  
  
  U-Boot 2020.01 (Jan 20 2020 - 18:14:27 +0000)
  
  CPU:   rv64imafdc
  Model: SiFive HiFive Unleashed A00
  DRAM:  exception code: 7 , Store/AMO access fault , epc 8023cbdc , ra 8020c670
  ### ERROR ### Please RESET the board ###
  </code></pre>
  
  <p>I've also tried bundling U-Boot as a payload into my own build of OpenSBI:</p>
  
  <pre><code>➜  opensbi git:(master) ✗ export CROSS_COMPILE=riscv64-unknown-elf-
  ➜  opensbi git:(master) ✗ make PLATFORM=sifive/fu540 FW_PAYLOAD_PATH=../u-boot/u-boot.bin
  
  ➜  opensbi git:(master) ✗ qemu-system-riscv64 -M sifive_u -m 256M -bios none -display none -serial stdio -device loader,addr=0x80000000,file=./build/platform/sifive/fu540/firmware/fw_payload.bin
  
  
  OpenSBI v0.5-32-gc0849cd
     ____                    _____ ____ _____
    / __ \                  / ____|  _ \_   _|
   | |  | |_ __   ___ _ __ | (___ | |_) || |
   | |  | | '_ \ / _ \ '_ \ \___ \|  _ &lt; | |
   | |__| | |_) |  __/ | | |____) | |_) || |_
    \____/| .__/ \___|_| |_|_____/|____/_____|
          | |
          |_|
  
  Platform Name          : SiFive Freedom U540
  Platform HART Features : RV64ACDFIMSU
  Platform Max HARTs     : 5
  Current Hart           : 1
  Firmware Base          : 0x80000000
  Firmware Size          : 100 KB
  Runtime SBI Version    : 0.2
  
  PMP0: 0x0000000080000000-0x000000008001ffff (A)
  PMP1: 0x0000000000000000-0xffffffffffffffff (A,R,W,X)
  
  
  U-Boot 2020.01 (Jan 20 2020 - 18:14:27 +0000)
  
  CPU:   rv64imafdc
  Model: SiFive HiFive Unleashed A00
  DRAM:  exception code: 7 , Store/AMO access fault , epc 8023cbdc , ra 8020c670
  ### ERROR ### Please RESET the board ###
  </code></pre>
  
  <p>I've tried looking at the offending instruction (which I presume is at <code>0x8023cbdc</code>) based on the error message:</p>
  
  <pre><code>➜  u-boot git:(0b0c6af387) riscv64-unknown-elf-objdump --start-address=0x8023cbdc -d ./u-boot | head -8
  
  ./u-boot:     file format elf64-littleriscv
  
  
  Disassembly of section .text_rest:
  
  000000008023cbdc &lt;memset+0x40&gt;:
      8023cbdc:   fee7bc23                sd      a4,-8(a5)
  </code></pre>
  
  <p>It's a store, so it's plausible that this is indeed accessing an invalid location; I'm trying to get GDB working so I can examine what's actually happening in more detail, but that's causing issues of its own...</p>
  
  <p>Has anyone had any success with this?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've managed to fix this specific error by changing the amount of memory with <code>-M 2G</code>; I presume the address it's trying to access lies outside the 256M range, but I'm not sure what it's doing that requires this much RAM.</p>
  
  <p>Now I'm getting the following error:</p>
  
  <pre><code>➜  opensbi git:(master) ✗ qemu-system-riscv64 -M sifive_u -m 8G -bios default -display none -serial stdio -device loader,addr=0x80200000,file=../u-boot/u-boot-dtb.bin
  
  OpenSBI v0.5 (Oct  9 2019 12:03:04)
     ____                    _____ ____ _____
    / __ \                  / ____|  _ \_   _|
   | |  | |_ __   ___ _ __ | (___ | |_) || |
   | |  | | '_ \ / _ \ '_ \ \___ \|  _ &lt; | |
   | |__| | |_) |  __/ | | |____) | |_) || |_
    \____/| .__/ \___|_| |_|_____/|____/_____|
          | |
          |_|
  
  Platform Name          : SiFive Freedom U540
  Platform HART Features : RV64ACDFIMSU
  Platform Max HARTs     : 5
  Current Hart           : 1
  Firmware Base          : 0x80000000
  Firmware Size          : 96 KB
  Runtime SBI Version    : 0.2
  
  PMP0: 0x0000000080000000-0x000000008001ffff (A)
  PMP1: 0x0000000000000000-0xffffffffffffffff (A,R,W,X)
  
  
  U-Boot 2020.01 (Jan 20 2020 - 21:40:02 +0000)
  
  CPU:   rv64imafdc
  Model: SiFive HiFive Unleashed A00
  DRAM:  8 GiB
  MMC:   exception code: 5 , Load access fault , epc fffadbd6 , ra fffadbd2
  ### ERROR ### Please RESET the board ###
  qemu-system-riscv64: terminating on signal 2
  </code></pre>
  
  <p>Again it's similar (although a load this time); interestingly, the address <code>fffadbd6</code> isn't part of U-Boot so I've no idea what's happening here; I very much doubt it's running off the end, because then I would expect undefined behaviour as opposed to consistently getting the same bad load.</p>