From nobody Sun Feb 8 02:25:54 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1669697714; cv=none; d=zohomail.com; s=zohoarc; b=MR81Ayhbbp8FjR+7NGXLSuva9iO+XTkhqCmRl6wP1A4oNG5YkkwSgYg5wffHEHP4/LKfnCJ2acovjDQSH7MPWGZuG5HmRNwdBIavN5qAXpgXzBICxoqdN/QdRvjM514mj0FjM3TkFAKGcxt6K7Ln7AWMJwVG18Zt/azduFo6saQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669697714; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=kGtxQ1/A3mVbaOg2hw4KzmJUfy36cVTJSceV7I2PGM4=; b=FitdAVKC8HoFiUdHdbNbSybIyT81KnekWqfOOXFxBCCPSR1kNsG1oFZ1ZxFyzNKqWKspqwAM2C0qYPQaXEMTG8sOiSezN2mkVq5SXf09iy7n2T4fZ78kUiSmWktnInFfmZQ5n398D4beCAuDUq2MzOzVbP1qgitdO6fDiDAjKGc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1669697714357506.11954972469744; Mon, 28 Nov 2022 20:55:14 -0800 (PST) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-674-MteiDb4zM8y2xAA2tZ-wcw-1; Mon, 28 Nov 2022 23:55:04 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3D35E380671C; Tue, 29 Nov 2022 04:55:02 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 60DF7C15BA4; Tue, 29 Nov 2022 04:54:59 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 0F93B1946597; Tue, 29 Nov 2022 04:54:58 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id E55CC1946594 for ; Tue, 29 Nov 2022 04:54:53 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id AD376C15BB1; Tue, 29 Nov 2022 04:54:53 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast09.extmail.prod.ext.rdu2.redhat.com [10.11.55.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A55E8C15BA4 for ; Tue, 29 Nov 2022 04:54:53 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 86287299E745 for ; Tue, 29 Nov 2022 04:54:53 +0000 (UTC) Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-110-fJi40rsJP7GyElya63cVbA-1; Mon, 28 Nov 2022 23:54:51 -0500 Received: by mail-pf1-f182.google.com with SMTP id 9so12573671pfx.11 for ; Mon, 28 Nov 2022 20:54:51 -0800 (PST) Received: from localhost.localdomain (42-3-27-244.ptr.netvigator.com. [42.3.27.244]) by smtp.gmail.com with ESMTPSA id s13-20020a170902b18d00b00174f7d10a03sm9716171plr.86.2022.11.28.20.54.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Nov 2022 20:54:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669697713; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=kGtxQ1/A3mVbaOg2hw4KzmJUfy36cVTJSceV7I2PGM4=; b=HHSPHq4MPDO67y28IQaIkSVZZtGutqnrERhEZm3eiGi9VcJy+Nm+C7UwIEYWpg62QahStq 7IBCcmEIfgGmQ6yyQ/Ff+ylDJPFW3XXgsaDMJoi7RaR0nxMtABwkXupSAP1Sa5OjUg1fR1 zQ5GH02ahwDzNyFasTIp7Fxp9n0HXtY= X-MC-Unique: MteiDb4zM8y2xAA2tZ-wcw-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: fJi40rsJP7GyElya63cVbA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kGtxQ1/A3mVbaOg2hw4KzmJUfy36cVTJSceV7I2PGM4=; b=Jpfl6o0O9G60tWQB4UYAfFD/4sfkNaJUb5ZxXRMVF42GizMR0icI4d2uJjRPfqxGAb KmfYqTPY0bE1R8xfGGwAwLpPSXcXSOH86I2sfUWYAYDnJjt/190hgTJYeLTbyIJhC3El 3JlZn3nKBX/yJnPD4NyczcEaGl3mvQP+FtOc7QBLZ8SKJmziblejyx4P4lsFIwUREHNz Sx7qHQybVuWqiAKgIfoAhUrLUtcYuUVxzVLbF4/tMA/ih1uVpUZkm76Os7h0r8d32CPF 3o9zgmxo5VGaA/6aXrcLUGC72JQa2X2Dc7a3rL5FoT4RmTdvmpErDEbGWIFcEe+AGUGN 3L9w== X-Gm-Message-State: ANoB5pke4t7WHeCottjemXkfPPi5w/9Jnmj/Z99OnzZoEbhGR9eZUNQu hFxsJOOhsDnidM+WCDIMMNXs6vowM+w= X-Google-Smtp-Source: AA0mqf6LqzSPXbD+XTzbkiC8lRetu02u6PiqB1qmnYW5T5IDJQqLOQFHCKdLWUZ+ueuIjKqXmLV59Q== X-Received: by 2002:a63:e607:0:b0:477:e302:ee5d with SMTP id g7-20020a63e607000000b00477e302ee5dmr16889350pgh.335.1669697689560; Mon, 28 Nov 2022 20:54:49 -0800 (PST) From: Yu Gu To: libvir-list@redhat.com Subject: [libvirt PATCH v3] Add RV64 support Date: Tue, 29 Nov 2022 12:52:41 +0800 Message-Id: <20221129045241.738848-1-guyu2876@gmail.com> In-Reply-To: <90eb47eb-b805-a681-d5cd-4a0f8c402d98@redhat.com> References: <90eb47eb-b805-a681-d5cd-4a0f8c402d98@redhat.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Yu Gu Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669697714876100001 Content-Type: text/plain; charset="utf-8"; x-default="true" This patch provides RV64 support for the RISC-V architecture. Signed-off-by: Yu Gu --- po/POTFILES | 1 + src/cpu/cpu.c | 2 + src/cpu/cpu.h | 2 + src/cpu/cpu_riscv64.c | 116 ++++++++++++++++++++++++ src/cpu/cpu_riscv64.h | 25 ++++++ src/cpu/cpu_riscv64_data.h | 37 ++++++++ src/cpu/meson.build | 1 + src/cpu_map/index.xml | 4 + src/cpu_map/meson.build | 1 + src/cpu_map/riscv64_vendors.xml | 3 + src/util/virarch.c | 2 + src/util/virhostcpu.c | 2 +- src/util/virsysinfo.c | 150 ++++++++++++++++++++++++++++++++ src/util/virsysinfopriv.h | 3 + 14 files changed, 348 insertions(+), 1 deletion(-) create mode 100644 src/cpu/cpu_riscv64.c create mode 100644 src/cpu/cpu_riscv64.h create mode 100644 src/cpu/cpu_riscv64_data.h create mode 100644 src/cpu_map/riscv64_vendors.xml diff --git a/po/POTFILES b/po/POTFILES index 169e2a41dc..a52795e7c1 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -72,6 +72,7 @@ src/cpu/cpu_map.c src/cpu/cpu_ppc64.c src/cpu/cpu_s390.c src/cpu/cpu_x86.c +src/cpu/cpu_riscv64.c src/datatypes.c src/driver.c src/esx/esx_driver.c diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c index d97ef5e873..8fdc42e719 100644 --- a/src/cpu/cpu.c +++ b/src/cpu/cpu.c @@ -27,6 +27,7 @@ #include "cpu_ppc64.h" #include "cpu_s390.h" #include "cpu_arm.h" +#include "cpu_riscv64.h" #include "capabilities.h" =20 =20 @@ -39,6 +40,7 @@ static struct cpuArchDriver *drivers[] =3D { &cpuDriverPPC64, &cpuDriverS390, &cpuDriverArm, + &cpuDriverRISCV64, }; =20 =20 diff --git a/src/cpu/cpu.h b/src/cpu/cpu.h index 41a62ce486..6e0a06fce4 100644 --- a/src/cpu/cpu.h +++ b/src/cpu/cpu.h @@ -27,6 +27,7 @@ #include "cpu_x86_data.h" #include "cpu_ppc64_data.h" #include "cpu_arm_data.h" +#include "cpu_riscv64_data.h" =20 =20 typedef struct _virCPUData virCPUData; @@ -36,6 +37,7 @@ struct _virCPUData { virCPUx86Data x86; virCPUppc64Data ppc64; virCPUarmData arm; + virCPUriscv64Data riscv64; /* generic driver needs no data */ } data; }; diff --git a/src/cpu/cpu_riscv64.c b/src/cpu/cpu_riscv64.c new file mode 100644 index 0000000000..9b90ae87d5 --- /dev/null +++ b/src/cpu/cpu_riscv64.c @@ -0,0 +1,116 @@ +/* + * cpu_riscv64.c: CPU driver for riscv64(x) CPUs + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include "cpu.h" + +#define VIR_FROM_THIS VIR_FROM_CPU + +static const virArch archs[] =3D { VIR_ARCH_RISCV64 }; + +static virCPUCompareResult +virCPUriscv64Compare(virCPUDef *host G_GNUC_UNUSED, + virCPUDef *cpu G_GNUC_UNUSED, + bool failMessages G_GNUC_UNUSED) +{ + /* riscv64 relies on QEMU to perform all runability checking. Return + * VIR_CPU_COMPARE_IDENTICAL to bypass Libvirt checking. + */ + return VIR_CPU_COMPARE_IDENTICAL; +} + +static int +virCPUriscv64Update(virCPUDef *guest, + const virCPUDef *host, + bool relative) +{ + g_autoptr(virCPUDef) updated =3D NULL; + size_t i; + + if (!relative) + return 0; + + if (guest->mode =3D=3D VIR_CPU_MODE_CUSTOM) { + if (guest->match =3D=3D VIR_CPU_MATCH_MINIMUM) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("match mode %s not supported"), + virCPUMatchTypeToString(guest->match)); + } else { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("optional CPU features are not supported")); + } + return -1; + } + + if (!host) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("unknown host CPU model")); + return -1; + } + + if (!(updated =3D virCPUDefCopyWithoutModel(guest))) + return -1; + + updated->mode =3D VIR_CPU_MODE_CUSTOM; + if (virCPUDefCopyModel(updated, host, true) < 0) + return -1; + + for (i =3D 0; i < guest->nfeatures; i++) { + if (virCPUDefUpdateFeature(updated, + guest->features[i].name, + guest->features[i].policy) < 0) + return -1; + } + + virCPUDefStealModel(guest, updated, false); + guest->mode =3D VIR_CPU_MODE_CUSTOM; + guest->match =3D VIR_CPU_MATCH_EXACT; + + return 0; +} + + +static int +virCPUriscv64ValidateFeatures(virCPUDef *cpu) +{ + size_t i; + + for (i =3D 0; i < cpu->nfeatures; i++) { + if (cpu->features[i].policy =3D=3D VIR_CPU_FEATURE_OPTIONAL) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("only cpu feature policies 'require' and 'dis= able' are supported for %s"), + cpu->features[i].name); + return -1; + } + } + + return 0; +} + +struct cpuArchDriver cpuDriverRISCV64 =3D { + .name =3D "riscv64", + .arch =3D archs, + .narch =3D G_N_ELEMENTS(archs), + .compare =3D virCPUriscv64Compare, + .decode =3D NULL, + .encode =3D NULL, + .baseline =3D NULL, + .update =3D virCPUriscv64Update, + .validateFeatures =3D virCPUriscv64ValidateFeatures, +}; diff --git a/src/cpu/cpu_riscv64.h b/src/cpu/cpu_riscv64.h new file mode 100644 index 0000000000..2ab43a9f95 --- /dev/null +++ b/src/cpu/cpu_riscv64.h @@ -0,0 +1,25 @@ +/* + * cpu_riscv64.h: CPU driver for 64-bit RISC-V CPUs + * + * Copyright (C) Copyright (C) IBM Corporation, 2010 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#pragma once + +#include "cpu.h" + +extern struct cpuArchDriver cpuDriverRISCV64; diff --git a/src/cpu/cpu_riscv64_data.h b/src/cpu/cpu_riscv64_data.h new file mode 100644 index 0000000000..71c9dbf832 --- /dev/null +++ b/src/cpu/cpu_riscv64_data.h @@ -0,0 +1,37 @@ +/* + * cpu_riscv64_data.h: 64-bit riscv64 CPU specific data + * + * Copyright (C) 2012 IBM Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; If not, see + * . + */ + +#pragma once + +#include + +typedef struct _virCPUriscv64Prid virCPUriscv64Prid; +struct _virCPUriscv64Prid { + uint32_t value; + uint32_t mask; +}; + +# define VIR_CPU_riscv64_DATA_INIT { 0 } + +typedef struct _virCPUriscv64Data virCPUriscv64Data; +struct _virCPUriscv64Data { + size_t len; + virCPUriscv64Prid *prid; +}; diff --git a/src/cpu/meson.build b/src/cpu/meson.build index b4ad95e46d..eba1d45743 100644 --- a/src/cpu/meson.build +++ b/src/cpu/meson.build @@ -5,6 +5,7 @@ cpu_sources =3D [ 'cpu_ppc64.c', 'cpu_s390.c', 'cpu_x86.c', + 'cpu_riscv64.c', ] =20 cpu_lib =3D static_library( diff --git a/src/cpu_map/index.xml b/src/cpu_map/index.xml index d533a28865..f2c4b1c62a 100644 --- a/src/cpu_map/index.xml +++ b/src/cpu_map/index.xml @@ -89,6 +89,10 @@ =20 + + + + diff --git a/src/cpu_map/meson.build b/src/cpu_map/meson.build index 99264289e2..1a02df8268 100644 --- a/src/cpu_map/meson.build +++ b/src/cpu_map/meson.build @@ -19,6 +19,7 @@ cpumap_data =3D [ 'ppc64_POWERPC_e5500.xml', 'ppc64_POWERPC_e6500.xml', 'ppc64_vendors.xml', + 'riscv64_vendors.xml', 'x86_486.xml', 'x86_athlon.xml', 'x86_Broadwell-IBRS.xml', diff --git a/src/cpu_map/riscv64_vendors.xml b/src/cpu_map/riscv64_vendors.= xml new file mode 100644 index 0000000000..24476bd20b --- /dev/null +++ b/src/cpu_map/riscv64_vendors.xml @@ -0,0 +1,3 @@ + + + diff --git a/src/util/virarch.c b/src/util/virarch.c index 2134dd6a9d..3d14ecd193 100644 --- a/src/util/virarch.c +++ b/src/util/virarch.c @@ -190,6 +190,8 @@ virArch virArchFromHost(void) return VIR_ARCH_ALPHA; case PROCESSOR_ARCHITECTURE_PPC: return VIR_ARCH_PPC; + case PROCESSOR_ARCHITECTURE_RISCV: + return VIR_ARCH_RISCV64; case PROCESSOR_ARCHITECTURE_SHX: return VIR_ARCH_SH4; case PROCESSOR_ARCHITECTURE_ARM: diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c index c1e8dc8078..08c2290f00 100644 --- a/src/util/virhostcpu.c +++ b/src/util/virhostcpu.c @@ -544,7 +544,7 @@ virHostCPUParseFrequency(FILE *cpuinfo, char line[1024]; =20 /* No sensible way to retrieve CPU frequency */ - if (ARCH_IS_ARM(arch)) + if (ARCH_IS_ARM(arch) || ARCH_IS_RISCV(arch)) return 0; =20 if (ARCH_IS_X86(arch)) diff --git a/src/util/virsysinfo.c b/src/util/virsysinfo.c index 376d5d4816..afa480ead4 100644 --- a/src/util/virsysinfo.c +++ b/src/util/virsysinfo.c @@ -624,6 +624,154 @@ virSysinfoReadS390(void) } =20 =20 +static const char * +virSysinfoParseRISCVDelimited(const char *base, const char *name, char **v= alue, + char delim1, char delim2) +{ + const char *start; + const char *end; + + if (delim1 !=3D delim2 && + (start =3D strstr(base, name)) && + (start =3D strchr(start, delim1))) { + start +=3D 1; + end =3D strchr(start, delim2); + if (!end) + end =3D start + strlen(start); + virSkipSpaces(&start); + *value =3D g_strndup(start, end - start); + virTrimSpaces(*value, NULL); + return end; + } + return NULL; +} + +static const char * +virSysinfoParseRISCVLine(const char *base, const char *name, char **value) +{ + return virSysinfoParseRISCVDelimited(base, name, value, ':', '\n'); +} + +static int +virSysinfoParseRISCVSystem(const char *base, virSysinfoSystemDef **sysdef) +{ + int ret =3D -1; + virSysinfoSystemDef *def; + + def =3D g_new0(virSysinfoSystemDef, 1); + + if (!virSysinfoParseRISCVLine(base, "Manufacturer", &def->manufacturer= )) + goto cleanup; + + if (!virSysinfoParseRISCVLine(base, "Type", &def->family)) + goto cleanup; + def->manufacturer =3D g_strndup("Virt-RISC-V", sizeof("Virt RISC-V")); + + if (!def->manufacturer && !def->product && !def->version && + !def->serial && !def->uuid && !def->sku && !def->family) { + g_clear_pointer(&def, virSysinfoSystemDefFree); + } + + *sysdef =3D g_steal_pointer(&def); + ret =3D 0; + cleanup: + virSysinfoSystemDefFree(def); + return ret; +} + +static int +virSysinfoParseRISCVProcessor(const char *base, virSysinfoDef *ret) +{ + const char *tmp_base; + char *manufacturer =3D NULL; + char *procline =3D NULL; + char *ncpu =3D NULL; + int result =3D -1; + virSysinfoProcessorDef *processor; + + if (!(tmp_base =3D virSysinfoParseRISCVLine(base, "uarch", &manufactur= er))) + goto error; + + /* Find processor N: line and gather the processor manufacturer, + version, serial number, and family */ + while ((tmp_base =3D strstr(tmp_base, "processor ")) + && (tmp_base =3D virSysinfoParseRISCVLine(tmp_base, "processor = ", + &procline))) { + VIR_EXPAND_N(ret->processor, ret->nprocessor, 1); + processor =3D &ret->processor[ret->nprocessor - 1]; + processor->processor_manufacturer =3D g_strdup(manufacturer); + + VIR_FREE(procline); + } + + /* now, for each processor found, extract the frequency information */ + tmp_base =3D base; + + while ((tmp_base =3D strstr(tmp_base, "cpu number")) && + (tmp_base =3D virSysinfoParseRISCVLine(tmp_base, "cpu number", = &ncpu))) { + unsigned int n; + char *mhz =3D NULL; + + if (virStrToLong_uip(ncpu, NULL, 10, &n) < 0) + goto error; + + if (n >=3D ret->nprocessor) { + VIR_DEBUG("CPU number '%u' out of range", n); + goto cleanup; + } + + if (!(tmp_base =3D strstr(tmp_base, "cpu MHz static")) || + !virSysinfoParseRISCVLine(tmp_base, "cpu MHz static", &mhz)) + goto cleanup; + + ret->processor[n].processor_max_speed =3D mhz; + + VIR_FREE(ncpu); + } + + cleanup: + result =3D 0; + + error: + VIR_FREE(manufacturer); + VIR_FREE(procline); + VIR_FREE(ncpu); + return result; +} + +virSysinfoDef * +virSysinfoReadRISCV(void) +{ + g_autoptr(virSysinfoDef) ret =3D NULL; + g_autofree char *outbuf =3D NULL; + + ret =3D g_new0(virSysinfoDef, 1); + + /* Gather info from /proc/cpuinfo */ + if (virFileReadAll(CPUINFO, CPUINFO_FILE_LEN, &outbuf) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to open %s"), CPUINFO); + return NULL; + } + + if (virSysinfoParseRISCVProcessor(outbuf, ret) < 0) + return NULL; + + /* Free buffer before reading next file */ + VIR_FREE(outbuf); + + /* Gather info from /proc/sysinfo */ + if (virFileReadAll(SYSINFO, 8192, &outbuf) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to open %s"), SYSINFO); + return NULL; + } + + if (virSysinfoParseRISCVSystem(outbuf, &ret->system) < 0) + return NULL; + return g_steal_pointer(&ret); +} + static int virSysinfoParseBIOS(const char *base, virSysinfoBIOSDef **bios) { @@ -1243,6 +1391,8 @@ virSysinfoRead(void) return virSysinfoReadPPC(); #elif defined(__arm__) || defined(__aarch64__) return virSysinfoReadARM(); +#elif defined(__riscv) && __riscv_xlen =3D=3D 64 + return virSysinfoReadRISCV(); #elif defined(__s390__) || defined(__s390x__) return virSysinfoReadS390(); #elif !defined(WIN32) && \ diff --git a/src/util/virsysinfopriv.h b/src/util/virsysinfopriv.h index e47bd3c361..33c8ceeddb 100644 --- a/src/util/virsysinfopriv.h +++ b/src/util/virsysinfopriv.h @@ -36,5 +36,8 @@ virSysinfoReadARM(void); virSysinfoDef * virSysinfoReadS390(void); =20 +virSysinfoDef * +virSysinfoReadRISCV(void); + virSysinfoDef * virSysinfoReadDMI(void); --=20 2.38.1