Update RISCV-QEMU emulator to 8.2.1 (#15320)

Supports hwcap

Update the RISCV CI docker and run_riscv_test.sh QEMU flags

For RISCV bare-metal smoke tests, the docker still uses the QEMU v5.2.0,
for QEMU v8.1.2 RISC-V has more restricted memory protection by default.
diff --git a/.github/workflows/build_benchmark_tools.yml b/.github/workflows/build_benchmark_tools.yml
index c31a176..0748ca0 100644
--- a/.github/workflows/build_benchmark_tools.yml
+++ b/.github/workflows/build_benchmark_tools.yml
@@ -69,7 +69,7 @@
             tracy_capture: "gs://iree-shared-files/tracy-capture-linux-x86_64-52b6af88"
           - platform: "linux"
             arch: "riscv_64"
-            docker_image: "gcr.io/iree-oss/riscv@sha256:b827812336aeee4689b342ddba49b06325df6e89cadfd2ec1ad965cf5ae77ac1"
+            docker_image: "gcr.io/iree-oss/riscv@sha256:61af5fee01800033826d851cbcf946a548c76f09306578f8cedb48438328f464"
             build_script: "./build_tools/cmake/build_riscv.sh"
             tracy_capture: "gs://iree-shared-files/tracy-capture-linux-x86_64-52b6af88"
           - platform: "android"
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 2f3de9f..61fc1c1 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -922,19 +922,19 @@
           - platform: linux
             arch: riscv_64
             abi: lp64d
-            docker_image: "gcr.io/iree-oss/riscv@sha256:b827812336aeee4689b342ddba49b06325df6e89cadfd2ec1ad965cf5ae77ac1"
+            docker_image: "gcr.io/iree-oss/riscv@sha256:61af5fee01800033826d851cbcf946a548c76f09306578f8cedb48438328f464"
             build_script: "./build_tools/cmake/build_riscv.sh"
             test_script: "./build_tools/cmake/test_riscv.sh"
           - platform: linux
             arch: riscv_32
             abi: ilp32d
-            docker_image: "gcr.io/iree-oss/riscv@sha256:b827812336aeee4689b342ddba49b06325df6e89cadfd2ec1ad965cf5ae77ac1"
+            docker_image: "gcr.io/iree-oss/riscv@sha256:61af5fee01800033826d851cbcf946a548c76f09306578f8cedb48438328f464"
             build_script: "./build_tools/cmake/build_riscv.sh"
             test_script: "./build_tools/cmake/test_riscv.sh"
           - platform: generic
             arch: riscv_32
             abi: ilp32
-            docker_image: "gcr.io/iree-oss/riscv@sha256:b827812336aeee4689b342ddba49b06325df6e89cadfd2ec1ad965cf5ae77ac1"
+            docker_image: "gcr.io/iree-oss/riscv@sha256:61af5fee01800033826d851cbcf946a548c76f09306578f8cedb48438328f464"
             build_script: "./build_tools/cmake/build_riscv.sh"
             test_script: "./tests/riscv32/smoke.sh"
           - platform: emscripten
@@ -1023,11 +1023,11 @@
         target:
           - platform: linux
             arch: riscv_64
-            docker_image: "gcr.io/iree-oss/riscv@sha256:b827812336aeee4689b342ddba49b06325df6e89cadfd2ec1ad965cf5ae77ac1"
+            docker_image: "gcr.io/iree-oss/riscv@sha256:61af5fee01800033826d851cbcf946a548c76f09306578f8cedb48438328f464"
             run_scripts: "./build_tools/cmake/build_riscv.sh && ./build_tools/cmake/test_riscv.sh"
           - platform: linux
             arch: riscv_32
-            docker_image: "gcr.io/iree-oss/riscv@sha256:b827812336aeee4689b342ddba49b06325df6e89cadfd2ec1ad965cf5ae77ac1"
+            docker_image: "gcr.io/iree-oss/riscv@sha256:61af5fee01800033826d851cbcf946a548c76f09306578f8cedb48438328f464"
             run_scripts: "./build_tools/cmake/build_riscv.sh && ./build_tools/cmake/test_riscv.sh"
           - platform: linux
             arch: x86_64
diff --git a/build_tools/cmake/run_riscv_test.sh b/build_tools/cmake/run_riscv_test.sh
index e4431c9..af6759d 100755
--- a/build_tools/cmake/run_riscv_test.sh
+++ b/build_tools/cmake/run_riscv_test.sh
@@ -22,10 +22,10 @@
 # `QEMU_RV64_BIN` or `QEMU_RV32_BIN` environment variable to run the artifacts
 # under the emulator.
 if [[ "${RISCV_PLATFORM_ARCH}" == "linux-riscv_64" ]] && [[ ! -z "${QEMU_RV64_BIN}" ]]; then
-  "${QEMU_RV64_BIN}" "-cpu" "rv64,x-v=true,x-k=true,vlen=512,elen=64,vext_spec=v1.0" \
+  "${QEMU_RV64_BIN}" "-cpu" "rv64,Zve64d=true,vlen=512,elen=64,vext_spec=v1.0" \
   "-L" "${RISCV_RV64_LINUX_TOOLCHAIN_ROOT}/sysroot" "$@"
 elif [[ "${RISCV_PLATFORM_ARCH}" == "linux-riscv_32" ]] && [[ ! -z "${QEMU_RV32_BIN}" ]]; then
-  "${QEMU_RV32_BIN}" "-cpu" "rv32,x-v=true,x-k=true,vlen=512,elen=32,vext_spec=v1.0" \
+  "${QEMU_RV32_BIN}" "-cpu" "rv32,Zve32f=true,vlen=512,elen=32,vext_spec=v1.0" \
   "-L" "${RISCV_RV64_LINUX_TOOLCHAIN_ROOT}/sysroot" "$@"
 else
 # TODO(dcaballe): Add on-device run commands.
diff --git a/build_tools/docker/dockerfiles/riscv.Dockerfile b/build_tools/docker/dockerfiles/riscv.Dockerfile
index 26e064b..ddad1bb 100644
--- a/build_tools/docker/dockerfiles/riscv.Dockerfile
+++ b/build_tools/docker/dockerfiles/riscv.Dockerfile
@@ -12,14 +12,20 @@
 RUN tar -xf "toolchain_iree_manylinux_2_28_20231012.tar.gz" -C /usr/src/
 RUN wget --no-verbose "https://storage.googleapis.com/iree-shared-files/toolchain_iree_rv32imf_manylinux_2_28_20231012.tar.gz"
 RUN tar -xf "toolchain_iree_rv32imf_manylinux_2_28_20231012.tar.gz" -C /usr/src/
+RUN wget --no-verbose "https://storage.googleapis.com/iree-shared-files/qemu-riscv_8.1.2_manylinux_2.28_20231026.tar.gz"
+RUN tar -xf "qemu-riscv_8.1.2_manylinux_2.28_20231026.tar.gz" -C /usr/src/
+# Old qemu-v5.2.0 to support embedded elf (without memory protection)
+RUN mkdir -p /usr/src/qemu-v5.2.0
 RUN wget --no-verbose "https://storage.googleapis.com/iree-shared-files/qemu-riscv.tar.gz"
-RUN tar -xf "qemu-riscv.tar.gz" -C /usr/src/
+RUN tar -xf "qemu-riscv.tar.gz" -C /usr/src/qemu-v5.2.0
 
 FROM gcr.io/iree-oss/base@sha256:796fb81a11ff7e7d057c93de468b74e48b6a9641aa19b7f7673c2772e8ea3b33 AS final
 COPY --from=install-riscv "/usr/src/toolchain_iree" "/usr/src/toolchain_iree"
 COPY --from=install-riscv "/usr/src/toolchain_iree_rv32imf" "/usr/src/toolchain_iree_rv32imf"
 COPY --from=install-riscv "/usr/src/qemu-riscv" "/usr/src/qemu-riscv"
+COPY --from=install-riscv "/usr/src/qemu-v5.2.0/qemu-riscv/qemu-riscv32" "/usr/src/qemu-riscv/qemu-riscv32-v5.2.0"
 ENV RISCV_RV64_LINUX_TOOLCHAIN_ROOT="/usr/src/toolchain_iree"
 ENV RISCV_RV32_NEWLIB_TOOLCHAIN_ROOT="/usr/src/toolchain_iree_rv32imf"
 ENV QEMU_RV64_BIN="/usr/src/qemu-riscv/qemu-riscv64"
 ENV QEMU_RV32_BIN="/usr/src/qemu-riscv/qemu-riscv32"
+ENV QEMU_RV32_V5_2_BIN="/usr/src/qemu-riscv/qemu-riscv32-v5.2.0"
diff --git a/build_tools/docker/prod_digests.txt b/build_tools/docker/prod_digests.txt
index deafaa6..90674cf 100644
--- a/build_tools/docker/prod_digests.txt
+++ b/build_tools/docker/prod_digests.txt
@@ -5,7 +5,7 @@
 gcr.io/iree-oss/frontends-nvidia@sha256:c3d590c6f1a6369cd34ccf0fc6f9ca2fbf8ee06abdc58a2827fc847718c308b8
 gcr.io/iree-oss/frontends-swiftshader@sha256:1d2424dc512545a32b68e3f6b839541832fa24b5fce78cb253b3a4cd4592d9b2
 gcr.io/iree-oss/gradle-android@sha256:cf7bf0392d5125f2babb4b9de4b43b583220506ecebd6b6201b23b2575f671c0
-gcr.io/iree-oss/riscv@sha256:b827812336aeee4689b342ddba49b06325df6e89cadfd2ec1ad965cf5ae77ac1
+gcr.io/iree-oss/riscv@sha256:61af5fee01800033826d851cbcf946a548c76f09306578f8cedb48438328f464
 gcr.io/iree-oss/nvidia@sha256:4bc8f74e6f8dece34184eedfafede9c28ba3af1674e6774f5cd867802beffc9b
 gcr.io/iree-oss/emscripten@sha256:6e412f7ca51439ffce051a8a2e8fcbd7398743f461930a5f51c089441ffc3588
 gcr.io/iree-oss/android@sha256:d349a3d137d0ed0f7360af16096db682da0233679cdbb00a326150149e3c4fa4
diff --git a/build_tools/riscv/riscv_bootstrap.sh b/build_tools/riscv/riscv_bootstrap.sh
index ee71b46..fe7ca0f 100755
--- a/build_tools/riscv/riscv_bootstrap.sh
+++ b/build_tools/riscv/riscv_bootstrap.sh
@@ -26,8 +26,8 @@
   RISCV_CLANG_TOOLCHAIN_FILE_NAME="toolchain_iree_manylinux_2_28_20231012.tar.gz"
   RISCV_CLANG_TOOLCHAIN_FILE_SHA="3af56a58551ed5ae7441214822461a5368fee9403d7c883762fa902489bfbff0"
 
-  QEMU_FILE_NAME="qemu-riscv.tar.gz"
-  QEMU_FILE_SHA="6e0bca77408e606add8577d6f1b6709f6ef3165b0e241ed2ba191183dfc931ec"
+  QEMU_FILE_NAME="qemu-riscv_8.1.2_manylinux_2.28_20231026.tar.gz"
+  QEMU_FILE_SHA="dd77b39820d45b80bafab9155581578b4c625cb92fd6db9e9adbb9798fde3597"
 
   TOOLCHAIN_PATH_PREFIX=${PREBUILT_DIR}/toolchain/clang/linux/RISCV
   QEMU_PATH_PREFIX=${PREBUILT_DIR}/qemu/linux/RISCV
diff --git a/docs/website/docs/building-from-source/riscv.md b/docs/website/docs/building-from-source/riscv.md
index 092146d..a9a9d6e 100644
--- a/docs/website/docs/building-from-source/riscv.md
+++ b/docs/website/docs/building-from-source/riscv.md
@@ -34,7 +34,7 @@
 
 * [Clang getting started](https://clang.llvm.org/get_started.html)
 * [RISC-V GNU toolchain](https://github.com/riscv/riscv-gnu-toolchain)
-* [QEMU](https://github.com/qemu/qemu)
+* [QEMU](https://gitlab.com/qemu-project/qemu)
 * [RISC-V Linux QEMU](https://risc-v-getting-started-guide.readthedocs.io/en/latest/linux-qemu.html)
 
 !!! note
@@ -151,7 +151,7 @@
       (master branch). Switch the "riscv-binutils" submodule to
       `git://sourceware.org/git/binutils-gdb.git` (master branch) manually.
 * RISC-V QEMU is built from
-[https://github.com/sifive/qemu/tree/v5.2.0-rvv-rvb-zfh](https://github.com/sifive/qemu/tree/v5.2.0-rvv-rvb-zfh).
+<https://gitlab.com/qemu-project/qemu/tree/v8.1.2>.
 
 The SIMD code can be generated following the
 [IREE CPU flow](../guides/deployment-configurations/cpu.md)
@@ -172,7 +172,7 @@
 
 ```shell hl_lines="2 5"
 ${QEMU_BIN} \
-  -cpu rv64,x-v=true,x-k=true,vlen=512,elen=64,vext_spec=v1.0 \
+  -cpu rv64,Zve64d=true,vlen=512,elen=64,vext_spec=v1.0 \
   -L ${RISCV_TOOLCHAIN_ROOT}/sysroot/ \
   ../iree-build-riscv/tools/iree-run-module \
   --device=local-task \
diff --git a/tests/riscv32/smoke.sh b/tests/riscv32/smoke.sh
index 686afe5..5f0f2b4 100755
--- a/tests/riscv32/smoke.sh
+++ b/tests/riscv32/smoke.sh
@@ -19,14 +19,15 @@
 # Run the embedded_library module loader and simple_embedding under QEMU.
 echo "Test elf_module_test_binary"
 pushd "${BUILD_DIR}/runtime/src/iree/hal/local/elf" > /dev/null
-"${QEMU_RV32_BIN}" -cpu rv32 elf_module_test_binary
+# Need to run qemu without memory protection for embedded elf (code in .data)
+"${QEMU_RV32_V5_2_BIN}" -cpu rv32 elf_module_test_binary
 popd > /dev/null
 
 echo "Test simple_embedding binaries"
 pushd "${BUILD_DIR}/samples/simple_embedding" > /dev/null
 
-"${QEMU_RV32_BIN}" -cpu rv32 simple_embedding_embedded_sync
+"${QEMU_RV32_V5_2_BIN}" -cpu rv32 simple_embedding_embedded_sync
 
-"${QEMU_RV32_BIN}" -cpu rv32 simple_embedding_vmvx_sync
+"${QEMU_RV32_V5_2_BIN}" -cpu rv32 simple_embedding_vmvx_sync
 
 popd > /dev/null