From nobody Mon Nov 25 07:31:50 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1717413539; cv=none; d=zohomail.com; s=zohoarc; b=UKaijQoGQDYguIImJCLVNZgdTeGolsXxS0AI4w7mHVhBKKIY7S/IiZeq/xOmc6GhO1RPtOfscMtX/QuIYr+pndgi+n4E9CDsNk+aSmsTIi3FtFLfMPWj5PW+tGHQEgDAFpb8B9goZRh2fg6NPFXGRyEtObAhUxWyGx3Etq0i3cE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1717413539; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=D8UK8uAVTnW82lBqc0PHDyOOwY4BUuAnWYWQniDrcpo=; b=fHICNVRwCuJv5/JDM2/v4eEvmgMHmJlxNvmhwRCzpd3I7EPovKzIJc92QCtsOWI9o8crMdzczUHjytlWCPcM/tXOtVoItt53cjKx76NbthkzddLUQ6qQC1ZlPrcAc8QVrFktMRomPnVuWaw0Ph8swQyxC0z2pq8SfbAmB2SAlq8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1717413539880280.8602832915551; Mon, 3 Jun 2024 04:18:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sE5gl-0003V3-Ta; Mon, 03 Jun 2024 07:17:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sE5gY-0003Fq-Ag; Mon, 03 Jun 2024 07:17:11 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sE5gU-0006A4-7S; Mon, 03 Jun 2024 07:17:05 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1f661450af5so11825095ad.3; Mon, 03 Jun 2024 04:17:01 -0700 (PDT) Received: from toolbox.alistair23.me (2403-580b-97e8-0-82ce-f179-8a79-69f4.ip6.aussiebb.net. [2403:580b:97e8:0:82ce:f179:8a79:69f4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6323e18c7sm62375435ad.177.2024.06.03.04.16.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 04:16:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717413420; x=1718018220; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=D8UK8uAVTnW82lBqc0PHDyOOwY4BUuAnWYWQniDrcpo=; b=dIONXo/MPyygUVBBdUZc0jGe2FSl4xWBTRyoe+8kzaqDmGc9W+rINqWwn5e/MdRNvW ygmy9Kl6gODcwLYW6jPNOCc9pgqWD8zms2VuPQ4PD5eWBUvvAF8GsgCvb0pN2blm0Dy5 6N+2ZelwgwgNLicxWC9HVPCdHJ3yOWtUtPW61jxQJtx46jCoevI4E5LNG0LwJep8MDQR fwTtJnXUsDODuuDtKVh3Icoo5OzZeSIINFy81xLW0oxhbnwZnp2k4ju3j/qwUUCEwM1p yx4LT7InADs3mqy/BK6ILBCarPrvwITyPR18O7fVzTqxSUkps9k4ENn6grPeQGlBaen4 8kOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717413420; x=1718018220; 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=D8UK8uAVTnW82lBqc0PHDyOOwY4BUuAnWYWQniDrcpo=; b=VEMsa/XHQlTuCMOX9JE3AYtNpppdoVjsXKOpcuIhKneRSZcZfYPHnprlAQIAfnFZTP djktL/5eAobliaN7mzvwak73G3Tf/GvvEvlB3lJruQHz+b2oIRe8WleD1ZJwA37/cpga WU/N34oUt2NVbDuD0vK3YzF6RlcBMDj+cjjfy4bU0Dvd7LLANbI3QniDuwm8psosD86W kHWR4iA3/rVJl31cgHmBSmepYjsl9uXqibHOn0oDksNi6+ZVmerU3RRsrFgKQ1/nOlTl VX2aEdC8hxt01NCgwjYzSBswgKiHVFKoVj3UOl55I+Rl0vEYa78s9J2B6+6PQ1wGjEUM K4yw== X-Forwarded-Encrypted: i=1; AJvYcCV5hW3oVahghoQ2iN4pKA0gptxQOVOcSVg1hEJ35R5M+GsZD9J4RRX6TeTnOpAHqegl07kbiBUBKGTnznwzugjVg6AqNAH6 X-Gm-Message-State: AOJu0Yzx2Es30fc1Bcl3IvOMNjgZk7evpx6j5g10G1cvN+TmmuirJNm/ ekX/9jg5v7C8+XemHQetcv8vp66nnjs+aJr2O8u7OJWaHgvL1AK3nWE82Q== X-Google-Smtp-Source: AGHT+IEMqijZiWaLRZNjSfdKxl7igKi7G+sP2Bl2QPAekDn36KTtqEhJSbB9MuPlHmq6up2dIxWQeQ== X-Received: by 2002:a17:903:41d1:b0:1f6:6426:8da8 with SMTP id d9443c01a7336-1f664269316mr61143105ad.9.1717413420026; Mon, 03 Jun 2024 04:17:00 -0700 (PDT) From: Alistair Francis X-Google-Original-From: Alistair Francis To: qemu-devel@nongnu.org Cc: alistair23@gmail.com, Andrew Jones , Daniel Henrique Barboza , qemu-stable , Alistair Francis Subject: [PULL v2 02/27] target/riscv/kvm: Fix exposure of Zkr Date: Mon, 3 Jun 2024 21:16:18 +1000 Message-ID: <20240603111643.258712-3-alistair.francis@wdc.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240603111643.258712-1-alistair.francis@wdc.com> References: <20240603111643.258712-1-alistair.francis@wdc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=alistair23@gmail.com; helo=mail-pl1-x634.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1717413540233100003 Content-Type: text/plain; charset="utf-8" From: Andrew Jones The Zkr extension may only be exposed to KVM guests if the VMM implements the SEED CSR. Use the same implementation as TCG. Without this patch, running with a KVM which does not forward the SEED CSR access to QEMU will result in an ILL exception being injected into the guest (this results in Linux guests crashing on boot). And, when running with a KVM which does forward the access, QEMU will crash, since QEMU doesn't know what to do with the exit. Fixes: 3108e2f1c69d ("target/riscv/kvm: update KVM exts to Linux 6.8") Signed-off-by: Andrew Jones Reviewed-by: Daniel Henrique Barboza Cc: qemu-stable Message-ID: <20240422134605.534207-2-ajones@ventanamicro.com> Signed-off-by: Alistair Francis --- target/riscv/cpu.h | 3 +++ target/riscv/csr.c | 18 ++++++++++++++---- target/riscv/kvm/kvm-cpu.c | 25 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 2d0c02c35b..746efd099a 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -819,6 +819,9 @@ void riscv_set_csr_ops(int csrno, riscv_csr_operations = *ops); =20 void riscv_cpu_register_gdb_regs_for_features(CPUState *cs); =20 +target_ulong riscv_new_csr_seed(target_ulong new_value, + target_ulong write_mask); + uint8_t satp_mode_max_from_map(uint32_t map); const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit); =20 diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 726096444f..829d8346ed 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -4267,10 +4267,8 @@ static RISCVException write_upmbase(CPURISCVState *e= nv, int csrno, #endif =20 /* Crypto Extension */ -static RISCVException rmw_seed(CPURISCVState *env, int csrno, - target_ulong *ret_value, - target_ulong new_value, - target_ulong write_mask) +target_ulong riscv_new_csr_seed(target_ulong new_value, + target_ulong write_mask) { uint16_t random_v; Error *random_e =3D NULL; @@ -4294,6 +4292,18 @@ static RISCVException rmw_seed(CPURISCVState *env, i= nt csrno, rval =3D random_v | SEED_OPST_ES16; } =20 + return rval; +} + +static RISCVException rmw_seed(CPURISCVState *env, int csrno, + target_ulong *ret_value, + target_ulong new_value, + target_ulong write_mask) +{ + target_ulong rval; + + rval =3D riscv_new_csr_seed(new_value, write_mask); + if (ret_value) { *ret_value =3D rval; } diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c index eaa36121c7..b8136c7ef8 100644 --- a/target/riscv/kvm/kvm-cpu.c +++ b/target/riscv/kvm/kvm-cpu.c @@ -1418,6 +1418,28 @@ static int kvm_riscv_handle_sbi(CPUState *cs, struct= kvm_run *run) return ret; } =20 +static int kvm_riscv_handle_csr(CPUState *cs, struct kvm_run *run) +{ + target_ulong csr_num =3D run->riscv_csr.csr_num; + target_ulong new_value =3D run->riscv_csr.new_value; + target_ulong write_mask =3D run->riscv_csr.write_mask; + int ret =3D 0; + + switch (csr_num) { + case CSR_SEED: + run->riscv_csr.ret_value =3D riscv_new_csr_seed(new_value, write_m= ask); + break; + default: + qemu_log_mask(LOG_UNIMP, + "%s: un-handled CSR EXIT for CSR %lx\n", + __func__, csr_num); + ret =3D -1; + break; + } + + return ret; +} + int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) { int ret =3D 0; @@ -1425,6 +1447,9 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run= *run) case KVM_EXIT_RISCV_SBI: ret =3D kvm_riscv_handle_sbi(cs, run); break; + case KVM_EXIT_RISCV_CSR: + ret =3D kvm_riscv_handle_csr(cs, run); + break; default: qemu_log_mask(LOG_UNIMP, "%s: un-handled exit reason %d\n", __func__, run->exit_reason); --=20 2.45.1