From 56018166fd0c3eb1e71ad001e5083bcbb16b214d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E4=B8=8A=E9=93=9C=2010332738?= Date: Fri, 6 Jun 2025 15:38:12 +0800 Subject: [PATCH] add support for riscv64 --- add_support_for_riscv64_architecture.patch | 496 +++++++++++++++++++++ kata-containers.spec | 9 +- 2 files changed, 504 insertions(+), 1 deletion(-) create mode 100644 add_support_for_riscv64_architecture.patch diff --git a/add_support_for_riscv64_architecture.patch b/add_support_for_riscv64_architecture.patch new file mode 100644 index 0000000..b89bb33 --- /dev/null +++ b/add_support_for_riscv64_architecture.patch @@ -0,0 +1,496 @@ +diff -ruN kata-containers-3.2.0/src/agent/src/linux_abi.rs kata-containers-3.2.0-bak/src/agent/src/linux_abi.rs +--- kata-containers-3.2.0/src/agent/src/linux_abi.rs 2025-06-06 10:57:55.279629296 -0400 ++++ kata-containers-3.2.0-bak/src/agent/src/linux_abi.rs 2025-06-06 10:59:47.080150652 -0400 +@@ -16,7 +16,8 @@ + target_arch = "powerpc64", + target_arch = "s390x", + target_arch = "x86_64", +- target_arch = "x86" ++ target_arch = "x86", ++ target_arch = "riscv64" + ))] + pub fn create_pci_root_bus_path() -> String { + String::from("/devices/pci0000:00") +diff -ruN kata-containers-3.2.0/src/runtime/arch/riscv64-options.mk kata-containers-3.2.0-bak/src/runtime/arch/riscv64-options.mk +--- kata-containers-3.2.0/src/runtime/arch/riscv64-options.mk 1969-12-31 19:00:00.000000000 -0500 ++++ kata-containers-3.2.0-bak/src/runtime/arch/riscv64-options.mk 2025-06-06 11:01:50.180724705 -0400 +@@ -0,0 +1,13 @@ ++# Copyright (c) 2024 Institute of Software, CAS ++# ++# SPDX-License-Identifier: Apache-2.0 ++# ++ ++# riscv64 settings ++ ++MACHINETYPE := virt ++KERNELPARAMS := ++MACHINEACCELERATORS := ++CPUFEATURES := ++ ++QEMUCMD := qemu-system-riscv64 +diff -ruN kata-containers-3.2.0/src/runtime/cmd/kata-runtime/kata-check_data_riscv64_test.go kata-containers-3.2.0-bak/src/runtime/cmd/kata-runtime/kata-check_data_riscv64_test.go +--- kata-containers-3.2.0/src/runtime/cmd/kata-runtime/kata-check_data_riscv64_test.go 1969-12-31 19:00:00.000000000 -0500 ++++ kata-containers-3.2.0-bak/src/runtime/cmd/kata-runtime/kata-check_data_riscv64_test.go 2025-06-06 11:03:23.271158811 -0400 +@@ -0,0 +1,25 @@ ++// Copyright (c) 2025 Institute of Software, CAS. ++// ++// SPDX-License-Identifier: Apache-2.0 ++// ++ ++package main ++ ++const testCPUInfoTemplate = ` ++processor : 0 ++hart : 1 ++isa : rv64imafdch_zicbom_zicboz_zicntr_zicsr_zifencei_zihintntl_zihintpause_zihpm_zawrs_zfa_zca_zcd_zba_zbb_zbc_zbs_smaia_ssaia_sstc ++mmu : sv48 ++mvendorid : 0x0 ++marchid : 0x0 ++mimpid : 0x0 ++hart isa : rv64imafdch_zicbom_zicboz_zicntr_zicsr_zifencei_zihintntl_zihintpause_zihpm_zawrs_zfa_zca_zcd_zba_zbb_zbc_zbs_smaia_ssaia_sstc ++processor : 1 ++hart : 0 ++isa : rv64imafdch_zicbom_zicboz_zicntr_zicsr_zifencei_zihintntl_zihintpause_zihpm_zawrs_zfa_zca_zcd_zba_zbb_zbc_zbs_smaia_ssaia_sstc ++mmu : sv48 ++mvendorid : 0x0 ++marchid : 0x0 ++mimpid : 0x0 ++hart isa : rv64imafdch_zicbom_zicboz_zicntr_zicsr_zifencei_zihintntl_zihintpause_zihpm_zawrs_zfa_zca_zcd_zba_zbb_zbc_zbs_smaia_ssaia_sstc ++` +diff -ruN kata-containers-3.2.0/src/runtime/cmd/kata-runtime/kata-check_generic_test.go kata-containers-3.2.0-bak/src/runtime/cmd/kata-runtime/kata-check_generic_test.go +--- kata-containers-3.2.0/src/runtime/cmd/kata-runtime/kata-check_generic_test.go 2025-06-06 10:57:55.279629296 -0400 ++++ kata-containers-3.2.0-bak/src/runtime/cmd/kata-runtime/kata-check_generic_test.go 2025-06-06 11:04:36.351499605 -0400 +@@ -3,7 +3,7 @@ + // SPDX-License-Identifier: Apache-2.0 + // + +-//go:build arm64 || ppc64le || loong64 ++//go:build arm64 || ppc64le || loong64 || riscv64 + + package main + +diff -ruN kata-containers-3.2.0/src/runtime/cmd/kata-runtime/kata-check_riscv64.go kata-containers-3.2.0-bak/src/runtime/cmd/kata-runtime/kata-check_riscv64.go +--- kata-containers-3.2.0/src/runtime/cmd/kata-runtime/kata-check_riscv64.go 1969-12-31 19:00:00.000000000 -0500 ++++ kata-containers-3.2.0-bak/src/runtime/cmd/kata-runtime/kata-check_riscv64.go 2025-06-06 11:08:31.122594408 -0400 +@@ -0,0 +1,133 @@ ++// Copyright (c) 2018 Intel Corporation ++// ++// SPDX-License-Identifier: Apache-2.0 ++// ++ ++package main ++ ++import ( ++ "fmt" ++ "strings" ++ ++ vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers" ++ "github.com/sirupsen/logrus" ++) ++ ++const ( ++ cpuFlagsTag = genericCPUFlagsTag ++ archCPUVendorField = "uarch" ++ archCPUModelField = "isa" ++) ++ ++// archRequiredCPUFlags maps a CPU flag value to search for and a ++// human-readable description of that value. ++var archRequiredCPUFlags = map[string]string{} ++ ++// archRequiredCPUAttribs maps a CPU (non-CPU flag) attribute value to search for ++// and a human-readable description of that value. ++var archRequiredCPUAttribs = map[string]string{} ++ ++// archRequiredKernelModules maps a required module name to a human-readable ++// description of the modules functionality and an optional list of ++// required module parameters. ++var archRequiredKernelModules = map[string]kernelModule{ ++ "kvm": { ++ desc: "Kernel-based Virtual Machine", ++ required: true, ++ }, ++ "vhost_vsock": { ++ desc: "Host Support for Linux VM Sockets", ++ required: false, ++ }, ++} ++ ++func setCPUtype(hypervisorType vc.HypervisorType) error { ++ return nil ++} ++ ++// kvmIsUsable determines if it will be possible to create a full virtual machine ++// by creating a minimal VM and then deleting it. ++func kvmIsUsable() error { ++ return genericKvmIsUsable() ++} ++ ++func archHostCanCreateVMContainer(hypervisorType vc.HypervisorType) error { ++ if hypervisorType == "remote" { ++ return nil ++ } ++ return kvmIsUsable() ++} ++ ++// hostIsVMContainerCapable checks to see if the host is theoretically capable ++// of creating a VM container. ++func hostIsVMContainerCapable(details vmContainerCapableDetails) error { ++ ++ _, err := getCPUInfo(details.cpuInfoFile) ++ if err != nil { ++ return err ++ } ++ ++ count, err := checkKernelModules(details.requiredKernelModules, archKernelParamHandler) ++ if err != nil { ++ return err ++ } ++ ++ if count == 0 { ++ return nil ++ } ++ ++ return fmt.Errorf("ERROR: %s", failMessage) ++ ++} ++ ++func archKernelParamHandler(onVMM bool, fields logrus.Fields, msg string) bool { ++ return genericArchKernelParamHandler(onVMM, fields, msg) ++} ++ ++func getRiscv64CPUDetails() (vendor, model string, err error) { ++ prefixModel := "processor" ++ cpuinfo, err := getCPUInfo(procCPUInfo) ++ if err != nil { ++ return "", "", err ++ } ++ ++ lines := strings.Split(cpuinfo, "\n") ++ ++ for _, line := range lines { ++ if archCPUVendorField != "" { ++ if strings.HasPrefix(line, archCPUVendorField) { ++ fields := strings.Split(line, ":") ++ if len(fields) > 1 { ++ vendor = strings.TrimSpace(fields[1]) ++ } ++ } ++ } else { ++ vendor = "Unknown" ++ } ++ if archCPUModelField != "" { ++ if strings.HasPrefix(line, prefixModel) { ++ fields := strings.Split(line, ":") ++ if len(fields) > 1 { ++ model = strings.TrimSpace(fields[1]) ++ } ++ } ++ } ++ } ++ ++ if vendor == "" { ++ return "", "", fmt.Errorf("cannot find vendor field in file %v", procCPUInfo) ++ } ++ ++ if model == "" { ++ return "", "", fmt.Errorf("Error in parsing cpu model from %v", procCPUInfo) ++ } ++ ++ return vendor, model, nil ++} ++ ++func getCPUDetails() (string, string, error) { ++ if vendor, model, err := genericGetCPUDetails(); err == nil { ++ return vendor, model, nil ++ } ++ return getRiscv64CPUDetails() ++} +diff -ruN kata-containers-3.2.0/src/runtime/cmd/kata-runtime/kata-check_riscv64_test.go kata-containers-3.2.0-bak/src/runtime/cmd/kata-runtime/kata-check_riscv64_test.go +--- kata-containers-3.2.0/src/runtime/cmd/kata-runtime/kata-check_riscv64_test.go 1969-12-31 19:00:00.000000000 -0500 ++++ kata-containers-3.2.0-bak/src/runtime/cmd/kata-runtime/kata-check_riscv64_test.go 2025-06-06 11:11:58.953563582 -0400 +@@ -0,0 +1,106 @@ ++// Copyright (c) 2025 Institute of Software, CAS. ++// Copyright (c) 2018 ARM Limited ++// ++// SPDX-License-Identifier: Apache-2.0 ++// ++ ++package main ++ ++import ( ++ "fmt" ++ "os" ++ "path/filepath" ++ "testing" ++ ++ "github.com/stretchr/testify/assert" ++) ++ ++func setupCheckHostIsVMContainerCapable(assert *assert.Assertions, cpuInfoFile string, cpuData []testCPUData, moduleData []testModuleData) { ++ createModules(assert, cpuInfoFile, moduleData) ++ err := makeCPUInfoFile(cpuInfoFile, "", "") ++ assert.NoError(err) ++} ++ ++func TestCCCheckCLIFunction(t *testing.T) { ++ if os.Getenv("GITHUB_RUNNER_CI_NON_VIRT") == "true" { ++ t.Skip("Skipping the test as the GitHub self hosted runners for RISC-V do not support Virtualization") ++ } ++ ++ var cpuData []testCPUData ++ moduleData := []testModuleData{ ++ {filepath.Join(sysModuleDir, "kvm"), "", true}, ++ {filepath.Join(sysModuleDir, "vhost"), "", true}, ++ {filepath.Join(sysModuleDir, "vhost_net"), "", true}, ++ } ++ ++ genericCheckCLIFunction(t, cpuData, moduleData) ++} ++ ++func TestGetCPUDetails(t *testing.T) { ++ type testData struct { ++ contents string ++ expectedNormalizeVendor string ++ expectedNormalizeModel string ++ expectError bool ++ } ++ ++ validVendorName := "0x0" ++ validModelName := "0x0" ++ validVendor := fmt.Sprintf(`%s : %s`, archCPUVendorField, validVendorName) ++ validModel := fmt.Sprintf(`%s : %s`, archCPUModelField, validModelName) ++ ++ validContents := fmt.Sprintf(` ++a : b ++%s ++foo : bar ++%s ++`, validVendor, validModel) ++ ++ data := []testData{ ++ {"", "", "", true}, ++ {"invalid", "", "", true}, ++ {archCPUVendorField, "", "", true}, ++ {archCPUModelField, "", "", true}, ++ {"", validVendorName, "", true}, ++ {"", "", validModelName, true}, ++ {validContents, validVendorName, validModelName, false}, ++ } ++ ++ tmpdir := t.TempDir() ++ ++ savedProcCPUInfo := procCPUInfo ++ ++ testProcCPUInfo := filepath.Join(tmpdir, "cpuinfo") ++ ++ // override ++ procCPUInfo = testProcCPUInfo ++ ++ defer func() { ++ procCPUInfo = savedProcCPUInfo ++ }() ++ ++ _, _, err := getCPUDetails() ++ // ENOENT ++ assert.Error(t, err) ++ assert.True(t, os.IsNotExist(err)) ++ ++ for _, d := range data { ++ err := createFile(procCPUInfo, d.contents) ++ assert.NoError(t, err) ++ ++ vendor, model, err := getCPUDetails() ++ ++ if d.expectError { ++ assert.Error(t, err, fmt.Sprintf("%+v", d)) ++ continue ++ } else { ++ assert.NoError(t, err, fmt.Sprintf("%+v", d)) ++ assert.Equal(t, d.expectedNormalizeVendor, vendor) ++ assert.Equal(t, d.expectedNormalizeModel, model) ++ } ++ } ++} ++ ++func TestSetCPUtype(t *testing.T) { ++ testSetCPUTypeGeneric(t) ++} +diff -ruN kata-containers-3.2.0/src/runtime/cmd/kata-runtime/kata-env_riscv64_test.go kata-containers-3.2.0-bak/src/runtime/cmd/kata-runtime/kata-env_riscv64_test.go +--- kata-containers-3.2.0/src/runtime/cmd/kata-runtime/kata-env_riscv64_test.go 1969-12-31 19:00:00.000000000 -0500 ++++ kata-containers-3.2.0-bak/src/runtime/cmd/kata-runtime/kata-env_riscv64_test.go 2025-06-06 11:12:46.023783083 -0400 +@@ -0,0 +1,14 @@ ++ ++// Copyright (c) 2025 Institute of Software, CAS. ++// ++// SPDX-License-Identifier: Apache-2.0 ++// ++ ++package main ++ ++func getExpectedHostDetails(tmpdir string) (HostInfo, error) { ++ expectedVendor := "0x0" ++ expectedModel := "0x0" ++ expectedVMContainerCapable := true ++ return genericGetExpectedHostDetails(tmpdir, expectedVendor, expectedModel, expectedVMContainerCapable) ++} +diff -ruN kata-containers-3.2.0/src/runtime/go-test.sh kata-containers-3.2.0-bak/src/runtime/go-test.sh +--- kata-containers-3.2.0/src/runtime/go-test.sh 2025-06-06 10:57:55.289629342 -0400 ++++ kata-containers-3.2.0-bak/src/runtime/go-test.sh 2025-06-06 11:15:15.664480899 -0400 +@@ -22,8 +22,8 @@ + # "go test -timeout X" + go_test_flags="-timeout ${KATA_GO_TEST_TIMEOUT:-30s}" + +- # -race flag is not supported on s390x +- [ "$(go env GOARCH)" != "s390x" ] && [ "$(go env GOARCH)" != "loong64" ] && go_test_flags+=" -race" ++ # -race flag is not supported on s390x and riscv64 ++ [ "$(go env GOARCH)" != "s390x" ] && [ "$(go env GOARCH)" != "loong64" ] && [ "$(go env GOARCH)" != "riscv64" ] && go_test_flags+=" -race" + + # s390x requires special linker flags + [ "$(go env GOARCH)" = s390x ] && go_test_flags+=" -ldflags '-extldflags -Wl,--s390-pgste'" +diff -ruN kata-containers-3.2.0/src/runtime/Makefile kata-containers-3.2.0-bak/src/runtime/Makefile +--- kata-containers-3.2.0/src/runtime/Makefile 2025-06-06 10:57:55.279629296 -0400 ++++ kata-containers-3.2.0-bak/src/runtime/Makefile 2025-06-06 11:15:52.984654934 -0400 +@@ -25,6 +25,9 @@ + ifeq ($(ARCH),loongarch64) + override ARCH = loong64 + endif ++ifeq ($(ARCH), riscv64gc) ++ override ARCH = riscv64 ++endif + + ARCH_DIR = arch + ARCH_FILE_SUFFIX = -options.mk +diff -ruN kata-containers-3.2.0/src/runtime/pkg/govmm/vmm_riscv64.go kata-containers-3.2.0-bak/src/runtime/pkg/govmm/vmm_riscv64.go +--- kata-containers-3.2.0/src/runtime/pkg/govmm/vmm_riscv64.go 1969-12-31 19:00:00.000000000 -0500 ++++ kata-containers-3.2.0-bak/src/runtime/pkg/govmm/vmm_riscv64.go 2025-06-06 11:16:49.284917477 -0400 +@@ -0,0 +1,12 @@ ++// ++// Copyright (c) 2018 Intel Corporation ++// ++// SPDX-License-Identifier: Apache-2.0 ++// ++ ++package govmm ++ ++// MaxVCPUs returns the maximum number of vCPUs supported ++func MaxVCPUs() uint32 { ++ return uint32(512) ++} +diff -ruN kata-containers-3.2.0/src/runtime/virtcontainers/factory/template/template_riscv64.go kata-containers-3.2.0-bak/src/runtime/virtcontainers/factory/template/template_riscv64.go +--- kata-containers-3.2.0/src/runtime/virtcontainers/factory/template/template_riscv64.go 1969-12-31 19:00:00.000000000 -0500 ++++ kata-containers-3.2.0-bak/src/runtime/virtcontainers/factory/template/template_riscv64.go 2025-06-06 11:18:01.905256126 -0400 +@@ -0,0 +1,14 @@ ++// Copyright (c) 2024 Institute of Software, CAS ++// ++// SPDX-License-Identifier: Apache-2.0 ++// ++// template implements base vm factory with vm templating. ++ ++package template ++ ++// templateDeviceStateSize denotes device state size when ++// mount tmpfs. ++// when bypass-shared-memory is not support like arm64, ++// creating template will occupy more space. That's why we ++// put it here. ++const templateDeviceStateSize = 8 +diff -ruN kata-containers-3.2.0/src/runtime/virtcontainers/hypervisor_linux_riscv64.go kata-containers-3.2.0-bak/src/runtime/virtcontainers/hypervisor_linux_riscv64.go +--- kata-containers-3.2.0/src/runtime/virtcontainers/hypervisor_linux_riscv64.go 1969-12-31 19:00:00.000000000 -0500 ++++ kata-containers-3.2.0-bak/src/runtime/virtcontainers/hypervisor_linux_riscv64.go 2025-06-06 11:18:42.595445876 -0400 +@@ -0,0 +1,10 @@ ++// Copyright (c) 2024 Institute of Software, CAS. ++// ++// SPDX-License-Identifier: Apache-2.0 ++ ++package virtcontainers ++ ++// Guest protection is not available on RISC-V. ++func availableGuestProtection() (guestProtection, error) { ++ return noneProtection, nil ++} +diff -ruN kata-containers-3.2.0/src/runtime/virtcontainers/qemu_riscv64.go kata-containers-3.2.0-bak/src/runtime/virtcontainers/qemu_riscv64.go +--- kata-containers-3.2.0/src/runtime/virtcontainers/qemu_riscv64.go 1969-12-31 19:00:00.000000000 -0500 ++++ kata-containers-3.2.0-bak/src/runtime/virtcontainers/qemu_riscv64.go 2025-06-06 11:20:46.136021980 -0400 +@@ -0,0 +1,68 @@ ++// Copyright (c) 2024 Institute of Software, CAS ++// ++// SPDX-License-Identifier: Apache-2.0 ++// ++ ++package virtcontainers ++ ++import ( ++ "fmt" ++ "time" ++ ++ govmmQemu "github.com/kata-containers/kata-containers/src/runtime/pkg/govmm/qemu" ++) ++ ++type qemuRiscv64 struct { ++ // inherit from qemuArchBase, overwrite methods if needed ++ qemuArchBase ++} ++ ++const defaultQemuPath = "/usr/bin/qemu-system-riscv64" ++ ++const defaultQemuMachineType = QemuVirt ++ ++const qmpMigrationWaitTimeout = 10 * time.Second ++ ++const defaultQemuMachineOptions = "" ++ ++var defaultGICVersion = uint32(3) ++ ++var kernelParams = []Param{ ++ {"numa", "off"}, ++} ++ ++var supportedQemuMachine = govmmQemu.Machine{ ++ Type: QemuVirt, ++ Options: defaultQemuMachineOptions, ++} ++ ++// MaxQemuVCPUs returns the maximum number of vCPUs supported ++func MaxQemuVCPUs() uint32 { ++ return uint32(512) ++} ++ ++func newQemuArch(config HypervisorConfig) (qemuArch, error) { ++ machineType := config.HypervisorMachineType ++ if machineType == "" { ++ machineType = defaultQemuMachineType ++ } ++ ++ if machineType != defaultQemuMachineType { ++ return nil, fmt.Errorf("unrecognised machinetype: %v", machineType) ++ } ++ ++ q := &qemuRiscv64{ ++ qemuArchBase{ ++ qemuMachine: supportedQemuMachine, ++ qemuExePath: defaultQemuPath, ++ memoryOffset: config.MemOffset, ++ kernelParamsNonDebug: kernelParamsNonDebug, ++ kernelParamsDebug: kernelParamsDebug, ++ kernelParams: kernelParams, ++ }, ++ } ++ ++ q.handleImagePath(config) ++ ++ return q, nil ++} +diff -ruN kata-containers-3.2.0/utils.mk kata-containers-3.2.0-bak/utils.mk +--- kata-containers-3.2.0/utils.mk 2023-10-23 13:48:26.000000000 -0400 ++++ kata-containers-3.2.0-bak/utils.mk 2025-06-06 11:31:43.539087633 -0400 +@@ -135,6 +135,9 @@ + ##VAR ARCH=arch target to build (format: uname -m) + HOST_ARCH = $(shell uname -m) + ARCH ?= $(HOST_ARCH) ++ifeq ($(ARCH), riscv64) ++ ARCH = riscv64gc ++endif + ##VAR LIBC=musl|gnu + LIBC ?= musl + ifneq ($(LIBC),musl) diff --git a/kata-containers.spec b/kata-containers.spec index f20a69e..74770fe 100644 --- a/kata-containers.spec +++ b/kata-containers.spec @@ -1,4 +1,4 @@ -%define anolis_release 2 +%define anolis_release 3 # go-rpm-macros are not available on RHEL. %global have_go_rpm_macros 0 @@ -73,6 +73,7 @@ Source8: update-sys-and-net-to-support-loongarch64.patch Patch0999: 0999-osbuilder-Adjust-agent_version-for-our-builds.patch Patch1000: 1000-Remove-shebang-in-non-executable-completion-script.patch Patch1001: 1001-backport-to-support-loong64.patch +Patch1002: add_support_for_riscv64_architecture.patch %if 0%{?have_go_rpm_macros} @@ -138,6 +139,9 @@ BuildRequires: crate(ttrpc/default) >= 0.0.0 %ifarch loongarch64 BuildRequires: golang-vendored-golang.org %endif +%ifarch riscv64 +BuildRequires: protobuf-compiler dwarves +%endif Requires: busybox Requires: dracut @@ -388,6 +392,9 @@ fi %changelog +* Fri Jun 06 2025 Shangtong Guo - 3.2.0-3 +- add support for riscv64 + * Mon Dec 18 2023 Wenlong Zhang - 3.2.0-2 - add loongarch64 support for kata-containers -- Gitee