From nobody Mon Nov 25 05:52:40 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 ARC-Seal: i=1; a=rsa-sha256; t=1716999192; cv=none; d=zohomail.com; s=zohoarc; b=dXkksfYk0cYWZD5WjTyBy4S0dU2UHIXzoSX7uyGFvhgBhwRq/XaWaw0zBuZTG0ysefxb5JKB6dintqOVJx6k+cfH4UdHUhGIhHeldfcqYDQ8WgMWX49E8sN4Y6dWktodNhuNKpzJnrJnnjHdvdy/4u+RJ3ArueEGRW3rV38rCPU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716999192; 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=0rgCgyNe1gFuETnV2hFHHcZAE4WID1bHJfoxPojDtt4=; b=N3OC5+ufWBmBb3QNAnDtQ9Dd4qAKoNhG/WhrWAvFb/84anhPyjqql+E9eYJ7TM2TnQGzn3iARXrrKUPWLmbgBl+OxFPBYhvXmbp+MvFn7bOXDquevs4zJDJgPwDCOxZSWUR3YrjQtmHFp+4igj4qrUoiihnxxzes4UaOe5yJDhg= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 171699919285331.003045874251484; Wed, 29 May 2024 09:13:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sCLsN-0000qn-Fb; Wed, 29 May 2024 12:10:07 -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 1sCLsL-0000pB-QU for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:05 -0400 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sCLsC-0003M4-7t for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:05 -0400 Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-354e22bc14bso1907141f8f.1 for ; Wed, 29 May 2024 09:09:55 -0700 (PDT) Received: from rkanwal-XPS-15-9520.Home ([2a02:c7c:7527:ee00:7446:71c1:a41a:da9b]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3557dcf0740sm15228213f8f.107.2024.05.29.09.09.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 09:09:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716998994; x=1717603794; 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=0rgCgyNe1gFuETnV2hFHHcZAE4WID1bHJfoxPojDtt4=; b=l57/93W3aU8wUHKYJUsEQP4pfG3dJncK5FBvAZWqlEWhRXwpsrGYDmZbGt/HuF8BIr 4bE0V+KtDTSrsBMV4pnaP9XDFRasaH3C/LlVFNQuMP2UgL5rNBrVq6iPhxtzq9rtAn/U Vf3F2NCbzyjF4r4tW2oJTU2SuMoKE+suaW7gGhZB/UHkm4NZK01+57sT5Ymd4AwudToP Wl7NyBkwtWbSjV/bsjyilMObZI8cu9NF0KZDBn9qUGtz5zwVj/aUQzrRnmM1Mwni5QrB Ehs/3sCbFqcSyah/J76qflp4CZEdjgOM5GmeLFQlRKuZ46ZL9vz+/RKmKqVpiCOLy7Th 6YXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716998994; x=1717603794; 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=0rgCgyNe1gFuETnV2hFHHcZAE4WID1bHJfoxPojDtt4=; b=wtMfuiLk7qoF+tdI/lrNW+QErJOSp5/vazzQrPB9DmhFe3J5RAemzdYiiue7/jysnL +6OjLwuA817f01yyTh3jPeTgVyX4oTE1p/qMlA0ohbcVUF+WPUe1X79WzwnIctg5W5EW EqoXsuKTE0FZPi2g29bS4RTV3cmJObLlN26JVvnvabo/UiC9CQYv9aqFGkKjaVGsrU+z DSl03fcQei/nsHDPla1rIXzrDfpUaIKlNw1HNLJSYgQQaIQTgtCMdxX2JVYJJTeEh3Nd M+aBKTvrouuK67mvDeFoOhHNFw8YoAGxuSJJym2aV8Z9psuk5AxRD9LygZW0tqndYRDK szfw== X-Forwarded-Encrypted: i=1; AJvYcCVqkta28MlC+QywrHGgrExDZa11MYWWe5DgIoZO7jMxcEq/QyeO0S1NsgJlpE+yb32yYfSeaRVU0aEvPOpdtmqmUpgftLs= X-Gm-Message-State: AOJu0Ywjec9jPJfZYgR/te8K9trL4hvs0Bu0gaZOyJ6BLwzLUxqqGh0g fREr/rnPa4ziR/+Ty+WgZ+n9hXjG2sARA46/tgA+17+2ThL2bUw7HR/C4yzfbwk= X-Google-Smtp-Source: AGHT+IEKq36zS/jtHLiHLHUVYEeDvsl0Uzo3f0YRmPIvRFRorXUoIqB3NPmjFMN7nTsL/7MmEcMb6w== X-Received: by 2002:adf:f0d0:0:b0:354:fb97:1365 with SMTP id ffacd0b85a97d-3552fe19723mr10083659f8f.52.1716998994367; Wed, 29 May 2024 09:09:54 -0700 (PDT) From: Rajnesh Kanwal To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, bin.meng@windriver.com, liweiwei@iscas.ac.cn, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com, atishp@rivosinc.com, apatel@ventanamicro.com, rkanwal@rivosinc.com, beeman@rivosinc.com, tech-control-transfer-records@lists.riscv.org Subject: [PATCH 1/6] target/riscv: Remove obsolete sfence.vm instruction Date: Wed, 29 May 2024 17:09:45 +0100 Message-Id: <20240529160950.132754-2-rkanwal@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240529160950.132754-1-rkanwal@rivosinc.com> References: <20240529160950.132754-1-rkanwal@rivosinc.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=2a00:1450:4864:20::42f; envelope-from=rkanwal@rivosinc.com; helo=mail-wr1-x42f.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, 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 @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1716999194362100001 Content-Type: text/plain; charset="utf-8" Signed-off-by: Rajnesh Kanwal Reviewed-by: Alistair Francis --- target/riscv/insn32.decode | 1 - target/riscv/insn_trans/trans_privileged.c.inc | 5 ----- 2 files changed, 6 deletions(-) diff --git a/target/riscv/insn32.decode b/target/riscv/insn32.decode index f22df04cfd..9cb1a1b4ec 100644 --- a/target/riscv/insn32.decode +++ b/target/riscv/insn32.decode @@ -112,7 +112,6 @@ sret 0001000 00010 00000 000 00000 1110011 mret 0011000 00010 00000 000 00000 1110011 wfi 0001000 00101 00000 000 00000 1110011 sfence_vma 0001001 ..... ..... 000 00000 1110011 @sfence_vma -sfence_vm 0001000 00100 ..... 000 00000 1110011 @sfence_vm =20 # *** RV32I Base Instruction Set *** lui .................... ..... 0110111 @u diff --git a/target/riscv/insn_trans/trans_privileged.c.inc b/target/riscv/= insn_trans/trans_privileged.c.inc index bc5263a4e0..4eccdddeaa 100644 --- a/target/riscv/insn_trans/trans_privileged.c.inc +++ b/target/riscv/insn_trans/trans_privileged.c.inc @@ -127,8 +127,3 @@ static bool trans_sfence_vma(DisasContext *ctx, arg_sfe= nce_vma *a) #endif return false; } - -static bool trans_sfence_vm(DisasContext *ctx, arg_sfence_vm *a) -{ - return false; -} --=20 2.34.1 From nobody Mon Nov 25 05:52:41 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 ARC-Seal: i=1; a=rsa-sha256; t=1716999142; cv=none; d=zohomail.com; s=zohoarc; b=AMpywoSYDYJ5pVuLaGFyXM32A35ZlFSVxEGkIZPY5ZksT+z3JiSzW1WBIPiLeENZfe3im9d9Ia6eT3Vh0SFnqB20kGRKhN2WVxIh7kJMzGXpbCFYBiPd75X55s3Xnef7BWMxCk59D2aQAxJyonNu0XYR5HcC1EyJAqrOvFK20XQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716999142; 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=a/Haw6h0NMogk0m9baqrbppzlik8qxWnPEs/BFAYo5E=; b=DA3fyviPQQkr/OWYL430B8ZtX/u7z2bpyi89OveMc5QSaSLJNC2+drcZ2vtx2hGkntyfKRO6ckkv0ez4EDPG7fH7UL4ZCHCnzUdVyUodALnI8IFCs2GjxPlt3J4vdOV2aK6xAufiCRWh1nflFJ0DMS2Vz1p1KN8CAzBRmKwyHaA= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716999142968252.0156769640904; Wed, 29 May 2024 09:12:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sCLsQ-0000st-OL; Wed, 29 May 2024 12:10:11 -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 1sCLsP-0000rq-2W for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:09 -0400 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sCLsC-0003MH-Lk for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:08 -0400 Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-351da5838fcso2160086f8f.1 for ; Wed, 29 May 2024 09:09:56 -0700 (PDT) Received: from rkanwal-XPS-15-9520.Home ([2a02:c7c:7527:ee00:7446:71c1:a41a:da9b]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3557dcf0740sm15228213f8f.107.2024.05.29.09.09.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 09:09:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716998995; x=1717603795; 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=a/Haw6h0NMogk0m9baqrbppzlik8qxWnPEs/BFAYo5E=; b=N8SpopgT7WpkDru55GIVV2ZwcwtIaIDErH45NKpJHOUBO+9r8aCvqDtxTQ+wlBNH51 xbYpXRP0zW/x9lfvSSAjRCTYZ07PnruM6QY2enD8hGhYabJ7H7OLxqR9WN+9swff2GBY K7vcWJid3VOWYo5nT8KSoBpQ/I6VMcax357ckdkr8pwL4d9/bcLIY8IIyYzPVMhYpQvT uloUP6V8EqGGKSYIjg9o6QJPlusSgnIU0fhbk+r18t1naHmka+0ipq8e2yCWxJxblnc+ R1ZXKhVEVr7rMUVAVSXBofpc9xgBdh/FROkRKs73m5T7XQVjS6Rwz2MtR4J6PzGnlPAv UkeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716998995; x=1717603795; 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=a/Haw6h0NMogk0m9baqrbppzlik8qxWnPEs/BFAYo5E=; b=tR7j/tfIwJnCDlha24fAWl6/MufsjgJc8tq2sGmeQYdpphOvmVQoYz1rm1m6B+PUaF TFepXBEqGq6pu6BYb7Xc2A4xigySo79aUyBC02YkziPSkIhRlQ1eaM0sCca76pRZkwPM 9uvoIfO4cT1pZcQq6iEEsN7C1bDR11Yp0P4tvbb/tKvNgrIY1Y+87R9RIiiMgK6oEqZU +6X47bCGQCrr46jlpdEYoNUsVQBEeUccO2aWlw5rBOHvTokoeZCqototfOC4zKmbKL95 YsYkSIrIH/YsS/QAu44Cfj1QT44/bkRq5jR6nY5mKwUbWgKVy+ti46+L7WMkRi8yqnkF jqzQ== X-Forwarded-Encrypted: i=1; AJvYcCVOdORcDIk+25Clu6bpchVTxYBSd6ZHQF66dOMG+/DlGozta1FEt6AUeg1Q37f+o7WQE92yj8lleLrVvJSwnO1YXlw9t4E= X-Gm-Message-State: AOJu0YyDweR37v4uvCqmHHctp5z518qPUcYa224oafsgZIlVy1bqB6wD JxvQu2893/oStzhFkcOlHtBS8TsMAdTwG6+jv0ULDoUX9v8Epp2QRhHGGdQmvto= X-Google-Smtp-Source: AGHT+IEiufWSbAkjmpuVHVrPKRcweyO9rRDNrtTrCFPME8UQyAO/o+aFp45yLwNpbvIqoSInw4dyGw== X-Received: by 2002:a5d:6aca:0:b0:354:fa6e:7ae2 with SMTP id ffacd0b85a97d-3552219cfa0mr10202094f8f.36.1716998995162; Wed, 29 May 2024 09:09:55 -0700 (PDT) From: Rajnesh Kanwal To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, bin.meng@windriver.com, liweiwei@iscas.ac.cn, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com, atishp@rivosinc.com, apatel@ventanamicro.com, rkanwal@rivosinc.com, beeman@rivosinc.com, tech-control-transfer-records@lists.riscv.org Subject: [PATCH 2/6] target/riscv: Add Control Transfer Records CSR definitions. Date: Wed, 29 May 2024 17:09:46 +0100 Message-Id: <20240529160950.132754-3-rkanwal@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240529160950.132754-1-rkanwal@rivosinc.com> References: <20240529160950.132754-1-rkanwal@rivosinc.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=2a00:1450:4864:20::435; envelope-from=rkanwal@rivosinc.com; helo=mail-wr1-x435.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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01, UPPERCASE_50_75=0.008 autolearn=no 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 @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1716999144244100004 Content-Type: text/plain; charset="utf-8" The Control Transfer Records (CTR) extension provides a method to record a limited branch history in register-accessible internal chip storage. This extension is similar to Arch LBR in x86 and BRBE in ARM. The Extension has been stable and the latest release can be found here https://github.com/riscv/riscv-control-transfer-records/release Signed-off-by: Rajnesh Kanwal --- target/riscv/cpu_bits.h | 154 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index 86e15381c8..71ddccaf1a 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -242,6 +242,17 @@ #define CSR_SIEH 0x114 #define CSR_SIPH 0x154 =20 +/* Machine-Level Control transfer records CSRs */ +#define CSR_MCTRCTL 0x34e + +/* Supervisor-Level Control transfer records CSRs */ +#define CSR_SCTRCTL 0x14e +#define CSR_SCTRSTATUS 0x14f +#define CSR_SCTRDEPTH 0x15f + +/* VS-Level Control transfer records CSRs */ +#define CSR_VSCTRCTL 0x24e + /* Hpervisor CSRs */ #define CSR_HSTATUS 0x600 #define CSR_HEDELEG 0x602 @@ -339,6 +350,7 @@ #define SMSTATEEN0_CS (1ULL << 0) #define SMSTATEEN0_FCSR (1ULL << 1) #define SMSTATEEN0_JVT (1ULL << 2) +#define SMSTATEEN0_CTR (1ULL << 54) #define SMSTATEEN0_HSCONTXT (1ULL << 57) #define SMSTATEEN0_IMSIC (1ULL << 58) #define SMSTATEEN0_AIA (1ULL << 59) @@ -854,6 +866,148 @@ typedef enum RISCVException { #define UMTE_U_PM_INSN U_PM_INSN #define UMTE_MASK (UMTE_U_PM_ENABLE | MMTE_U_PM_CURRENT | UMTE_U_PM_IN= SN) =20 +/* mctrctl CSR bits. */ +#define MCTRCTL_U_ENABLE BIT(0) +#define MCTRCTL_S_ENABLE BIT(1) +#define MCTRCTL_M_ENABLE BIT(2) +#define MCTRCTL_RASEMU BIT(7) +#define MCTRCTL_STE BIT(8) +#define MCTRCTL_MTE BIT(9) +#define MCTRCTL_BPFRZ BIT(11) +#define MCTRCTL_LCOFIFRZ BIT(12) +#define MCTRCTL_EXCINH BIT(33) +#define MCTRCTL_INTRINH BIT(34) +#define MCTRCTL_TRETINH BIT(35) +#define MCTRCTL_NTBREN BIT(36) +#define MCTRCTL_TKBRINH BIT(37) +#define MCTRCTL_INDCALL_INH BIT(40) +#define MCTRCTL_DIRCALL_INH BIT(41) +#define MCTRCTL_INDJUMP_INH BIT(42) +#define MCTRCTL_DIRJUMP_INH BIT(43) +#define MCTRCTL_CORSWAP_INH BIT(44) +#define MCTRCTL_RET_INH BIT(45) +#define MCTRCTL_INDOJUMP_INH BIT(46) +#define MCTRCTL_DIROJUMP_INH BIT(47) + +#define MCTRCTL_INH_START 32U + +#define MCTRCTL_MASK (MCTRCTL_M_ENABLE | MCTRCTL_S_ENABLE | \ + MCTRCTL_U_ENABLE | MCTRCTL_RASEMU | \ + MCTRCTL_MTE | MCTRCTL_STE | \ + MCTRCTL_BPFRZ | MCTRCTL_LCOFIFRZ | \ + MCTRCTL_EXCINH | MCTRCTL_INTRINH | \ + MCTRCTL_TRETINH | MCTRCTL_NTBREN | \ + MCTRCTL_TKBRINH | MCTRCTL_INDCALL_INH | \ + MCTRCTL_DIRCALL_INH | MCTRCTL_INDJUMP_INH | \ + MCTRCTL_DIRJUMP_INH | MCTRCTL_CORSWAP_INH | \ + MCTRCTL_RET_INH | MCTRCTL_INDOJUMP_INH | \ + MCTRCTL_DIROJUMP_INH) + +/* sctrctl CSR bits. */ +#define SCTRCTL_U_ENABLE MCTRCTL_U_ENABLE +#define SCTRCTL_S_ENABLE MCTRCTL_S_ENABLE +#define SCTRCTL_RASEMU MCTRCTL_RASEMU +#define SCTRCTL_STE MCTRCTL_STE +#define SCTRCTL_BPFRZ MCTRCTL_BPFRZ +#define SCTRCTL_LCOFIFRZ MCTRCTL_LCOFIFRZ +#define SCTRCTL_EXCINH MCTRCTL_EXCINH +#define SCTRCTL_INTRINH MCTRCTL_INTRINH +#define SCTRCTL_TRETINH MCTRCTL_TRETINH +#define SCTRCTL_NTBREN MCTRCTL_NTBREN +#define SCTRCTL_TKBRINH MCTRCTL_TKBRINH +#define SCTRCTL_INDCALL_INH MCTRCTL_INDCALL_INH +#define SCTRCTL_DIRCALL_INH MCTRCTL_DIRCALL_INH +#define SCTRCTL_INDJUMP_INH MCTRCTL_INDJUMP_INH +#define SCTRCTL_DIRJUMP_INH MCTRCTL_DIRJUMP_INH +#define SCTRCTL_CORSWAP_INH MCTRCTL_CORSWAP_INH +#define SCTRCTL_RET_INH MCTRCTL_RET_INH +#define SCTRCTL_INDOJUMP_INH MCTRCTL_INDOJUMP_INH +#define SCTRCTL_DIROJUMP_INH MCTRCTL_DIROJUMP_INH + +#define SCTRCTL_MASK (SCTRCTL_S_ENABLE | SCTRCTL_U_ENABLE | \ + SCTRCTL_RASEMU | SCTRCTL_STE | \ + SCTRCTL_BPFRZ | SCTRCTL_LCOFIFRZ | \ + SCTRCTL_EXCINH | SCTRCTL_INTRINH | \ + SCTRCTL_TRETINH | SCTRCTL_NTBREN | \ + SCTRCTL_TKBRINH | SCTRCTL_INDCALL_INH | \ + SCTRCTL_DIRCALL_INH | SCTRCTL_INDJUMP_INH | \ + SCTRCTL_DIRJUMP_INH | SCTRCTL_CORSWAP_INH | \ + SCTRCTL_RET_INH | SCTRCTL_INDOJUMP_INH | \ + SCTRCTL_DIROJUMP_INH) + +/* sctrstatus CSR bits. */ +#define SCTRSTATUS_WRPTR_MASK 0xFF +#define SCTRSTATUS_FROZEN BIT(31) +#define SCTRSTATUS_MASK (SCTRSTATUS_WRPTR_MASK | SCTRSTATUS_FR= OZEN) + +/* sctrdepth CSR bits. */ +#define SCTRDEPTH_MASK 0x7 +#define SCTRDEPTH_MIN 0U /* 16 Entries. */ +#define SCTRDEPTH_MAX 4U /* 256 Entries. */ + +/* vsctrctl CSR bits. */ +#define VSCTRCTL_VU_ENABLE MCTRCTL_U_ENABLE +#define VSCTRCTL_VS_ENABLE MCTRCTL_S_ENABLE +#define VSCTRCTL_RASEMU MCTRCTL_RASEMU +#define VSCTRCTL_VSTE MCTRCTL_STE +#define VSCTRCTL_BPFRZ MCTRCTL_BPFRZ +#define VSCTRCTL_LCOFIFRZ MCTRCTL_LCOFIFRZ +#define VSCTRCTL_EXCINH MCTRCTL_EXCINH +#define VSCTRCTL_INTRINH MCTRCTL_INTRINH +#define VSCTRCTL_TRETINH MCTRCTL_TRETINH +#define VSCTRCTL_NTBREN MCTRCTL_NTBREN +#define VSCTRCTL_TKBRINH MCTRCTL_TKBRINH +#define VSCTRCTL_INDCALL_INH MCTRCTL_INDCALL_INH +#define VSCTRCTL_DIRCALL_INH MCTRCTL_DIRCALL_INH +#define VSCTRCTL_INDJUMP_INH MCTRCTL_INDJUMP_INH +#define VSCTRCTL_DIRJUMP_INH MCTRCTL_DIRJUMP_INH +#define VSCTRCTL_CORSWAP_INH MCTRCTL_CORSWAP_INH +#define VSCTRCTL_RET_INH MCTRCTL_RET_INH +#define VSCTRCTL_INDOJUMP_INH MCTRCTL_INDOJUMP_INH +#define VSCTRCTL_DIROJUMP_INH MCTRCTL_DIROJUMP_INH + +#define VSCTRCTL_MASK (VSCTRCTL_VS_ENABLE | VSCTRCTL_VU_ENABLE | \ + VSCTRCTL_RASEMU | VSCTRCTL_VSTE | \ + VSCTRCTL_BPFRZ | VSCTRCTL_LCOFIFRZ | \ + VSCTRCTL_EXCINH | VSCTRCTL_INTRINH | \ + VSCTRCTL_TRETINH | VSCTRCTL_NTBREN | \ + VSCTRCTL_TKBRINH | VSCTRCTL_INDCALL_INH | \ + VSCTRCTL_DIRCALL_INH | VSCTRCTL_INDJUMP_INH | \ + VSCTRCTL_DIRJUMP_INH | VSCTRCTL_CORSWAP_INH | \ + VSCTRCTL_RET_INH | VSCTRCTL_INDOJUMP_INH | \ + VSCTRCTL_DIROJUMP_INH) + +#define CTR_ENTRIES_FIRST 0x200 +#define CTR_ENTRIES_LAST 0x2ff + +#define CTRSOURCE_VALID BIT(0) +#define CTRTARGET_MISP BIT(0) + +#define CTRDATA_TYPE_MASK 0xF +#define CTRDATA_CCV BIT(15) +#define CTRDATA_CCM_MASK 0xFFF0000 +#define CTRDATA_CCE_MASK 0xF0000000 + +#define CTRDATA_MASK (CTRDATA_TYPE_MASK | CTRDATA_CCV | \ + CTRDATA_CCM_MASK | CTRDATA_CCE_MASK) + +#define CTRDATA_TYPE_NONE 0 +#define CTRDATA_TYPE_EXCEPTION 1 +#define CTRDATA_TYPE_INTERRUPT 2 +#define CTRDATA_TYPE_EXCEP_INT_RET 3 +#define CTRDATA_TYPE_NONTAKEN_BRANCH 4 +#define CTRDATA_TYPE_TAKEN_BRANCH 5 +#define CTRDATA_TYPE_RESERVED_0 6 +#define CTRDATA_TYPE_RESERVED_1 7 +#define CTRDATA_TYPE_INDIRECT_CALL 8 +#define CTRDATA_TYPE_DIRECT_CALL 9 +#define CTRDATA_TYPE_INDIRECT_JUMP 10 +#define CTRDATA_TYPE_DIRECT_JUMP 11 +#define CTRDATA_TYPE_CO_ROUTINE_SWAP 12 +#define CTRDATA_TYPE_RETURN 13 +#define CTRDATA_TYPE_OTHER_INDIRECT_JUMP 14 +#define CTRDATA_TYPE_OTHER_DIRECT_JUMP 15 + /* MISELECT, SISELECT, and VSISELECT bits (AIA) */ #define ISELECT_IPRIO0 0x30 #define ISELECT_IPRIO15 0x3f --=20 2.34.1 From nobody Mon Nov 25 05:52:41 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 ARC-Seal: i=1; a=rsa-sha256; t=1716999117; cv=none; d=zohomail.com; s=zohoarc; b=fx+ynNfuPHcewRP+xfQdtzv4DibduzctFCEZcQJqcMbTJiBlYri4kO7vCafNQgBQ9vAwc3vPMj/Iut6GC7lQlvEEQ0CjB0uvu0b1KZIwfG5yFINzU/zAcD+/CheA3AE5CbA1LmE5b/I+McnU2yMoE9XQz9GGkwXj3lfT4YdsZl0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716999117; 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=StS3+7nlVIf5oE8cBUohIBpboOuZOuETS8f794xWjaU=; b=bPnM/J9zFi5rVBisxqE/kH0e1PhZN3k6vAzn4YeWMI5HfbCE8r4CWad7RpTqD1eDneeieWB/DOLovZOOFXE83PDQp0dCnSxJ2M6ZaC/sq2JzKv5EOlyT+eYYFj6jZDHpYxEyfxsajk5sdP2mAV8Tf0/Egq5OGig6+O8jUEVB5/g= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716999117130599.4954000508425; Wed, 29 May 2024 09:11:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sCLsV-0000w6-00; Wed, 29 May 2024 12:10:16 -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 1sCLsR-0000sr-H9 for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:11 -0400 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sCLsE-0003MR-1t for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:10 -0400 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-42121d28664so11275515e9.2 for ; Wed, 29 May 2024 09:09:57 -0700 (PDT) Received: from rkanwal-XPS-15-9520.Home ([2a02:c7c:7527:ee00:7446:71c1:a41a:da9b]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3557dcf0740sm15228213f8f.107.2024.05.29.09.09.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 09:09:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716998996; x=1717603796; 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=StS3+7nlVIf5oE8cBUohIBpboOuZOuETS8f794xWjaU=; b=NBgFyaiYJ0W823dafgsxAAgnFZaXKxe7T9kp7huvafDYpiY9t9YKdOMLhv+Woo9omj jmYKDW7zAk4TLzT7FbBCmc9IjQNdDN2si/Kkfl56aaIF7gihMT7SgDcSbX/JhxToIIiC yuhggB1Cv0Va9IqqBTMZav2j7ZJQRV8h+f84WpbYvR9TNqS7eXjkIBVohgieQvC1WWIC n/mUjm3IsTErqNRGs53nNgJuE4STbeuhmPthcTBSlanQWkfTJKSurGWf/Gu6FJQUQOne EodYkTznGiMXA6Gt6XlcUkITCNdnRV2xUctVJXOhKlFQ70hGeqG+WwTa/ucdqCggznWV Q83Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716998996; x=1717603796; 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=StS3+7nlVIf5oE8cBUohIBpboOuZOuETS8f794xWjaU=; b=l1qHQXscat8v0/UlhYnFYy0/thlMucnQENXvYY/6xe+6q/V7bALsbxu6v54RAXjt9C eF2R/LujM1EaoKVWKEsiPNWvsNUxrq8PvrMu9SyTCCT8vPmTMsBy+ERqar5UxNY+9Azf j4E5unweo6oPjlbrl+H7bjlvz9UMox6rlpjPlrWi2QqZMsLo2SmACqEv8kpmyFqQFWRM IlTs5ILJlm9se9aN9vBW4SHWoI9fyCy5DYPZJJW33bnK8OshuW3DWbOCRDKTBYRk5vCq VNSRkB1EULahIu/ML8WgT7kRRDElYTKi8RefWxveJ7twQIwN+e4QPdvCT/aXItfMVIQC 1j9Q== X-Forwarded-Encrypted: i=1; AJvYcCUIY5+3pXf+ZoxOGgD3jcGU2Um5ZCmyDgpewDm95yHPdghYRvsPb+JKtn/uG0mlVngZCiLwbvcRTnJ1AgtPKnS6MCmmXdE= X-Gm-Message-State: AOJu0YzW0aWnbYN0IpLtjZ5DcFGxszsJA9sqCDsp8VYLiw6G6b1+7sap 42H5K+1TPxuV0D026gSoF4EkAkN7YgrgkMo0QJ0EnXhX4eGaM9Nnci6kav1GmI4= X-Google-Smtp-Source: AGHT+IGCJ/NYOxMvS1YDuREfbFCQfN3xOX+rh0kdeKF2fE/hy7YqfCJIFYcwrThYPSPNJql3gkGoRw== X-Received: by 2002:a05:600c:364f:b0:420:1094:65d with SMTP id 5b1f17b1804b1-421089f4dd2mr120738055e9.12.1716998996240; Wed, 29 May 2024 09:09:56 -0700 (PDT) From: Rajnesh Kanwal To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, bin.meng@windriver.com, liweiwei@iscas.ac.cn, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com, atishp@rivosinc.com, apatel@ventanamicro.com, rkanwal@rivosinc.com, beeman@rivosinc.com, tech-control-transfer-records@lists.riscv.org Subject: [PATCH 3/6] target/riscv: Add support for Control Transfer Records extension CSRs. Date: Wed, 29 May 2024 17:09:47 +0100 Message-Id: <20240529160950.132754-4-rkanwal@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240529160950.132754-1-rkanwal@rivosinc.com> References: <20240529160950.132754-1-rkanwal@rivosinc.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=2a00:1450:4864:20::335; envelope-from=rkanwal@rivosinc.com; helo=mail-wm1-x335.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, 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 @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1716999118655100029 Content-Type: text/plain; charset="utf-8" This commit adds support for [m|s|vs]ctrcontrol, sctrstatus and sctrdepth CSRs handling. Signed-off-by: Rajnesh Kanwal --- target/riscv/cpu.h | 5 ++ target/riscv/cpu_cfg.h | 2 + target/riscv/csr.c | 159 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index a185e2d494..3d4d5172b8 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -263,6 +263,11 @@ struct CPUArchState { target_ulong mcause; target_ulong mtval; /* since: priv-1.10.0 */ =20 + uint64_t mctrctl; + uint32_t sctrdepth; + uint32_t sctrstatus; + uint64_t vsctrctl; + /* Machine and Supervisor interrupt priorities */ uint8_t miprio[64]; uint8_t siprio[64]; diff --git a/target/riscv/cpu_cfg.h b/target/riscv/cpu_cfg.h index d9354dc80a..d329a65811 100644 --- a/target/riscv/cpu_cfg.h +++ b/target/riscv/cpu_cfg.h @@ -123,6 +123,8 @@ struct RISCVCPUConfig { bool ext_zvfhmin; bool ext_smaia; bool ext_ssaia; + bool ext_smctr; + bool ext_ssctr; bool ext_sscofpmf; bool ext_smepmp; bool rvv_ta_all_1s; diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 2f92e4b717..888084d8e5 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -621,6 +621,61 @@ static RISCVException pointer_masking(CPURISCVState *e= nv, int csrno) return RISCV_EXCP_ILLEGAL_INST; } =20 +/* + * M-mode: + * Without ext_smctr raise illegal inst excep. + * Otherwise everything is accessible to m-mode. + * + * S-mode: + * Without ext_ssctr or mstateen.ctr raise illegal inst excep. + * Otherwise everything other than mctrctl is accessible. + * + * VS-mode: + * Without ext_ssctr or mstateen.ctr raise illegal inst excep. + * Without hstateen.ctr raise virtual illegal inst excep. + * Otherwise allow vsctrctl, sctrstatus, 0x200-0x2ff entry range. + * Always raise illegal instruction exception for sctrdepth. + */ +static RISCVException ctr_mmode(CPURISCVState *env, int csrno) +{ + /* Check if smctr-ext is present */ + if (riscv_cpu_cfg(env)->ext_smctr) { + return RISCV_EXCP_NONE; + } + + return RISCV_EXCP_ILLEGAL_INST; +} + +static RISCVException ctr_smode(CPURISCVState *env, int csrno) +{ + if ((env->priv =3D=3D PRV_M && riscv_cpu_cfg(env)->ext_smctr) || + (env->priv =3D=3D PRV_S && !env->virt_enabled && + riscv_cpu_cfg(env)->ext_ssctr)) { + return smstateen_acc_ok(env, 0, SMSTATEEN0_CTR); + } + + if (env->priv =3D=3D PRV_S && env->virt_enabled && + riscv_cpu_cfg(env)->ext_ssctr) { + if (csrno =3D=3D CSR_SCTRSTATUS) { + return smstateen_acc_ok(env, 0, SMSTATEEN0_CTR); + } + + return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; + } + + return RISCV_EXCP_ILLEGAL_INST; +} + +static RISCVException ctr_vsmode(CPURISCVState *env, int csrno) +{ + if (env->priv =3D=3D PRV_S && env->virt_enabled && + riscv_cpu_cfg(env)->ext_ssctr) { + return smstateen_acc_ok(env, 0, SMSTATEEN0_CTR); + } + + return ctr_smode(env, csrno); +} + static RISCVException aia_hmode(CPURISCVState *env, int csrno) { int ret; @@ -3835,6 +3890,100 @@ static RISCVException write_satp(CPURISCVState *env= , int csrno, return RISCV_EXCP_NONE; } =20 +static RISCVException rmw_sctrdepth(CPURISCVState *env, int csrno, + target_ulong *ret_val, + target_ulong new_val, target_ulong wr_= mask) +{ + uint64_t mask =3D wr_mask & SCTRDEPTH_MASK; + + if (ret_val) { + *ret_val =3D env->sctrdepth & SCTRDEPTH_MASK; + } + + env->sctrdepth =3D (env->sctrdepth & ~mask) | (new_val & mask); + + /* Correct depth. */ + if (wr_mask & SCTRDEPTH_MASK) { + uint64_t depth =3D get_field(env->sctrdepth, SCTRDEPTH_MASK); + + if (depth > SCTRDEPTH_MAX) { + env->sctrdepth =3D + set_field(env->sctrdepth, SCTRDEPTH_MASK, SCTRDEPTH_MAX); + } + + /* Update sctrstatus.WRPTR with a legal value */ + depth =3D 16 << depth; + env->sctrstatus =3D + env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth - 1)); + } + + return RISCV_EXCP_NONE; +} + +static RISCVException rmw_mctrctl(CPURISCVState *env, int csrno, + target_ulong *ret_val, + target_ulong new_val, target_ulong wr_= mask) +{ + uint64_t mask =3D wr_mask & MCTRCTL_MASK; + + if (ret_val) { + *ret_val =3D env->mctrctl & MCTRCTL_MASK; + } + + env->mctrctl =3D (env->mctrctl & ~mask) | (new_val & mask); + + return RISCV_EXCP_NONE; +} + +static RISCVException rmw_sctrctl(CPURISCVState *env, int csrno, + target_ulong *ret_val, + target_ulong new_val, target_ulong wr_= mask) +{ + uint64_t mask =3D wr_mask & SCTRCTL_MASK; + RISCVException ret; + + ret =3D rmw_mctrctl(env, csrno, ret_val, new_val, mask); + if (ret_val) { + *ret_val &=3D SCTRCTL_MASK; + } + + return ret; +} + +static RISCVException rmw_sctrstatus(CPURISCVState *env, int csrno, + target_ulong *ret_val, + target_ulong new_val, target_ulong wr= _mask) +{ + uint32_t depth =3D 16 << get_field(env->sctrdepth, SCTRDEPTH_MASK); + uint32_t mask =3D wr_mask & SCTRSTATUS_MASK; + + if (ret_val) { + *ret_val =3D env->sctrstatus & SCTRSTATUS_MASK; + } + + env->sctrstatus =3D (env->sctrstatus & ~mask) | (new_val & mask); + + /* Update sctrstatus.WRPTR with a legal value */ + env->sctrstatus =3D env->sctrstatus & (~SCTRSTATUS_WRPTR_MASK | (depth= - 1)); + + return RISCV_EXCP_NONE; +} + +static RISCVException rmw_vsctrctl(CPURISCVState *env, int csrno, + target_ulong *ret_val, + target_ulong new_val, target_ulong wr_= mask) +{ + uint64_t mask =3D wr_mask & VSCTRCTL_MASK; + + if (ret_val) { + *ret_val =3D env->vsctrctl & VSCTRCTL_MASK; + } + + env->vsctrctl =3D (env->vsctrctl & ~mask) | (new_val & mask); + + return RISCV_EXCP_NONE; +} + static RISCVException read_vstopi(CPURISCVState *env, int csrno, target_ulong *val) { @@ -5771,6 +5920,16 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { [CSR_SPMBASE] =3D { "spmbase", pointer_masking, read_spmbase, write_spmbase = }, =20 + [CSR_MCTRCTL] =3D { "mctrctl", ctr_mmode, NULL, NULL, + rmw_mctrctl }, + [CSR_SCTRCTL] =3D { "sctrctl", ctr_smode, NULL, NULL, + rmw_sctrctl }, + [CSR_SCTRDEPTH] =3D { "sctrdepth", ctr_smode, NULL, NULL, + rmw_sctrdepth }, + [CSR_SCTRSTATUS] =3D { "sctrstatus", ctr_smode, NULL, NULL, + rmw_sctrstatus }, + [CSR_VSCTRCTL] =3D { "vsctrctl", ctr_vsmode, NULL, NULL, + rmw_vsctrctl }, /* Performance Counters */ [CSR_HPMCOUNTER3] =3D { "hpmcounter3", ctr, read_hpmcounter }, [CSR_HPMCOUNTER4] =3D { "hpmcounter4", ctr, read_hpmcounter }, --=20 2.34.1 From nobody Mon Nov 25 05:52:41 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 ARC-Seal: i=1; a=rsa-sha256; t=1716999117; cv=none; d=zohomail.com; s=zohoarc; b=fTvQEUjyZvczFKR0VdmgPvvsh2Y/5zLKQQPSjhD9MHjF3SE5bevXsRSOLE9VYOKvIM9bM/NF3/61NP09GaGZqwcIc6YiODeYVv416dSSB5wHUaSu2aq0g2tugkbeGrL/fmFY8Kc9wKvJE+ySWnEbJ1U95lmYdqfXKwYSOAWD9tQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716999117; h=Content-Type: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=osjOjWRu9rt+8nh61bUbxnuyPV8jhA5IYjsXZIh5+NY=; b=AWAx3r23Rmf6b6Y/lzw2lkvpesBUwdU08o/b/OZsHjOzUykrrUv+S4IiTjCEgzzhTEO8/ZulkpaSYL1Dg9GebM6FAuGwrGRO+qSGCYC2fIPnpgCeChNEICJHgcFjNH1i7gGHRvv/BZnamMoiMzBMsT1AbXm6yZIXiMSHMcue+TQ= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716999117153336.0657061213376; Wed, 29 May 2024 09:11:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sCLsZ-0000yN-8T; Wed, 29 May 2024 12:10:19 -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 1sCLsT-0000vB-5E for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:13 -0400 Received: from mail-lj1-x229.google.com ([2a00:1450:4864:20::229]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sCLsE-0003Mp-W1 for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:12 -0400 Received: by mail-lj1-x229.google.com with SMTP id 38308e7fff4ca-2e95a1f9c53so27748571fa.0 for ; Wed, 29 May 2024 09:09:58 -0700 (PDT) Received: from rkanwal-XPS-15-9520.Home ([2a02:c7c:7527:ee00:7446:71c1:a41a:da9b]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3557dcf0740sm15228213f8f.107.2024.05.29.09.09.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 09:09:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716998997; x=1717603797; 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=osjOjWRu9rt+8nh61bUbxnuyPV8jhA5IYjsXZIh5+NY=; b=BfBBp3SM3tMrDLefPHxmkXK+TsptfHAmIdS0gFnQ/QAOVyg1frSYDTdlpgoGLGmp1b sXYbMzSwBTKNCKdIlOAgX1zmdlKEg0/xWs33Q4Yn+Jj22mILnKAg5iejlm4ZeVbMemlK LdQSSsZBte8XxPxF39HTeTqu612G5praZ8tKDbyOqXB0yRjD0znA6Cc3PpElb3p74gTk 8Dl65VLHPsdcXJzYc1pkQO1k7pnut3Mk6MCo2hOKIXUixHAFZ43EPdIOvrib+CFLd/cd IpKzn6NQORj3kXczBqfq/deJ2JjeSf5O/p3NdPRj0GvlkaTzB43hO718E+bDfryxuBmy CltA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716998997; x=1717603797; 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=osjOjWRu9rt+8nh61bUbxnuyPV8jhA5IYjsXZIh5+NY=; b=d+FbEpSbKtjLVEvnyoSXXE11RCw+pjPaR5+UpV8A/b/ZVAI5U+3moa0O0QKVVte/s0 +GmeprGlwDCrzI5/ZFlDIYeHPGu3PhpJhyni5y+fvLv1E0sIKfqErrY9ShZDN6iLxnNg iPTpwI4nG90Fr5cdoiF4Rj0nAPaO+8XYSwcq8VVw2XD9CHV2slwvkhqars/kJ+dxwkUK saycpyR7A3XcQayXZJYjnaeEVdyBf/LG30qFafzBCLUiFpRV4U5JRkriq4V4qDkj3m9G kx1Ud5Ep0s7UnpXvVYfNbH8PGrlNGHqUBSqhWqCVPhCBvoioaVeeqFdRAlSnSTe4xR0R WaKQ== X-Forwarded-Encrypted: i=1; AJvYcCUFPxycZl2/r3sCgwmzYLoI/b0RiCCDje2t+B6a00p5kUmbmv0+KNBLm+7F+AQGpUVmlUn9oGsOQ4hJzAojrMIZVn/Up00= X-Gm-Message-State: AOJu0YxfJfON7MhMNNjhUs1lZFuHDE6pXU5p2t9ewdqD/PeO1sYJASLt /A0xaThCmQZsx04IXcVhTsWH3AbbEiVI0dVQ9EYPzzOonbgB9z3sUevV+hJkzw4= X-Google-Smtp-Source: AGHT+IESivAIuB9YUvE2L2+4fRxB2qtupF0Iz8QieJ8U7scKzHK9F7bj/U+zUdIbwtERSQJ9EhoGNQ== X-Received: by 2002:a2e:b169:0:b0:2e9:5d26:a56e with SMTP id 38308e7fff4ca-2e95d26a675mr94727121fa.16.1716998997116; Wed, 29 May 2024 09:09:57 -0700 (PDT) From: Rajnesh Kanwal To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, bin.meng@windriver.com, liweiwei@iscas.ac.cn, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com, atishp@rivosinc.com, apatel@ventanamicro.com, rkanwal@rivosinc.com, beeman@rivosinc.com, tech-control-transfer-records@lists.riscv.org Subject: [PATCH 4/6] target/riscv: Add support to record CTR entries. Date: Wed, 29 May 2024 17:09:48 +0100 Message-Id: <20240529160950.132754-5-rkanwal@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240529160950.132754-1-rkanwal@rivosinc.com> References: <20240529160950.132754-1-rkanwal@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=2a00:1450:4864:20::229; envelope-from=rkanwal@rivosinc.com; helo=mail-lj1-x229.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, 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 @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1716999118674100030 This commit adds logic to records CTR entries of different types and adds required hooks in TCG and interrupt/Exception logic to record events. This commit also adds support to invoke freeze CTR logic for breakpoint exceptions and counter overflow interrupts. Signed-off-by: Rajnesh Kanwal --- target/riscv/cpu.h | 8 + target/riscv/cpu_helper.c | 206 ++++++++++++++++++ target/riscv/helper.h | 8 +- .../riscv/insn_trans/trans_privileged.c.inc | 6 +- target/riscv/insn_trans/trans_rvi.c.inc | 27 +++ target/riscv/op_helper.c | 112 +++++++++- target/riscv/translate.c | 9 + 7 files changed, 370 insertions(+), 6 deletions(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 3d4d5172b8..a294a5372a 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -268,6 +268,10 @@ struct CPUArchState { uint32_t sctrstatus; uint64_t vsctrctl; =20 + uint64_t ctr_src[16 << SCTRDEPTH_MAX]; + uint64_t ctr_dst[16 << SCTRDEPTH_MAX]; + uint64_t ctr_data[16 << SCTRDEPTH_MAX]; + /* Machine and Supervisor interrupt priorities */ uint8_t miprio[64]; uint8_t siprio[64]; @@ -565,6 +569,10 @@ RISCVException smstateen_acc_ok(CPURISCVState *env, in= t index, uint64_t bit); #endif void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv, bool vir= t_en); =20 +void riscv_ctr_freeze(CPURISCVState *env, uint64_t freeze_mask); +void riscv_ctr_add_entry(CPURISCVState *env, target_long src, target_long = dst, + uint64_t type, target_ulong prev_priv, bool prev_= virt); + void riscv_translate_init(void); G_NORETURN void riscv_raise_exception(CPURISCVState *env, uint32_t exception, uintptr_t pc); diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index a441a03ef4..e064a7306e 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -663,6 +663,10 @@ uint64_t riscv_cpu_update_mip(CPURISCVState *env, uint= 64_t mask, uint64_t value) =20 BQL_LOCK_GUARD(); =20 + if (MIP_LCOFIP & value & mask) { + riscv_ctr_freeze(env, MCTRCTL_LCOFIFRZ); + } + env->mip =3D (env->mip & ~mask) | (value & mask); =20 riscv_cpu_interrupt(env); @@ -691,6 +695,197 @@ void riscv_cpu_set_aia_ireg_rmw_fn(CPURISCVState *env= , uint32_t priv, } } =20 +void riscv_ctr_freeze(CPURISCVState *env, uint64_t freeze_mask) +{ + assert((freeze_mask & (~(MCTRCTL_BPFRZ | MCTRCTL_LCOFIFRZ))) =3D=3D 0); + + if (env->mctrctl & freeze_mask) { + env->sctrstatus |=3D SCTRSTATUS_FROZEN; + } +} + +static uint64_t riscv_ctr_priv_to_mask(target_ulong priv, bool virt) +{ + switch (priv) { + case PRV_M: + return MCTRCTL_M_ENABLE; + case PRV_S: + if (virt) { + return VSCTRCTL_VS_ENABLE; + } + return MCTRCTL_S_ENABLE; + case PRV_U: + if (virt) { + return VSCTRCTL_VU_ENABLE; + } + return MCTRCTL_U_ENABLE; + } + + g_assert_not_reached(); +} + +static uint64_t riscv_ctr_get_control(CPURISCVState *env, target_long priv, + bool virt) +{ + switch (priv) { + case PRV_M: + return env->mctrctl; + case PRV_S: + case PRV_U: + if (virt) { + return env->vsctrctl; + } + return env->mctrctl; + } + + g_assert_not_reached(); +} + +/* + * Special cases for traps and trap returns: + * + * 1- Traps, and trap returns, between enabled modes are recorded as norma= l. + * 2- Traps from an inhibited mode to an enabled mode, and trap returns fr= om an + * enabled mode back to an inhibited mode, are partially recorded. In such + * cases, the PC from the inhibited mode (source PC for traps, and target = PC + * for trap returns) is 0. + * + * 3- Trap returns from an inhibited mode to an enabled mode are not recor= ded. + * Traps from an enabled mode to an inhibited mode, known as external trap= s, + * receive special handling. + * By default external traps are not recorded, but a handshake mechanism e= xists + * to allow partial recording. Software running in the target mode of the= trap + * can opt-in to allowing CTR to record traps into that mode even when the= mode + * is inhibited. The MTE, STE, and VSTE bits allow M-mode, S-mode, and VS= -mode, + * respectively, to opt-in. When an External Trap occurs, and xTE=3D1, suc= h that + * x is the target privilege mode of the trap, will CTR record the trap. I= n such + * cases, the target PC is 0. + */ +/* + * CTR arrays are implemented as circular buffers and new entry is stored = at + * sctrstatus.WRPTR, but they are presented to software as moving circular + * buffers. Which means, software get's the illusion that whenever a new e= ntry + * is added the whole buffer is moved by one place and the new entry is ad= ded at + * the start keeping new entry at idx 0 and older ones follow. + * + * Depth =3D 16. + * + * buffer [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] [F] + * WRPTR W + * entry 7 6 5 4 3 2 1 0 F E D C B A 9 8 + * + * When a new entry is added: + * buffer [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] [F] + * WRPTR W + * entry 8 7 6 5 4 3 2 1 0 F E D C B A 9 + * + * entry here denotes the logical entry number that software can access + * using ctrsource, ctrtarget and ctrdata registers. So xiselect 0x200 + * will return entry 0 i-e buffer[8] and 0x201 will return entry 1 i-e + * buffer[7]. Here is how we convert entry to buffer idx. + * + * entry =3D isel - CTR_ENTRIES_FIRST; + * idx =3D (sctrstatus.WRPTR - entry - 1) & (depth - 1); + */ +void riscv_ctr_add_entry(CPURISCVState *env, target_long src, target_long = dst, + uint64_t type, target_ulong src_priv, bool src_vi= rt) +{ + bool tgt_virt =3D env->virt_enabled; + uint64_t src_mask =3D riscv_ctr_priv_to_mask(src_priv, src_virt); + uint64_t tgt_mask =3D riscv_ctr_priv_to_mask(env->priv, tgt_virt); + uint64_t src_ctrl =3D riscv_ctr_get_control(env, src_priv, src_virt); + uint64_t tgt_ctrl =3D riscv_ctr_get_control(env, env->priv, tgt_virt); + uint64_t depth, head; + bool ext_trap =3D false; + + if (env->sctrstatus & SCTRSTATUS_FROZEN) { + return; + } + + /* + * With RAS Emul enabled, only allow Indirect, drirect calls, Function + * returns and Co-routine swap types. + */ + if (env->mctrctl & MCTRCTL_RASEMU && + type !=3D CTRDATA_TYPE_INDIRECT_CALL && + type !=3D CTRDATA_TYPE_DIRECT_CALL && + type !=3D CTRDATA_TYPE_RETURN && + type !=3D CTRDATA_TYPE_CO_ROUTINE_SWAP) { + return; + } + + if (type =3D=3D CTRDATA_TYPE_EXCEPTION || type =3D=3D CTRDATA_TYPE_INT= ERRUPT) { + /* Case 2 for traps. */ + if (!(src_ctrl & src_mask) && (tgt_ctrl & tgt_mask)) { + src =3D 0; + } else if ((src_ctrl & src_mask) && !(tgt_ctrl & tgt_mask)) { + /* Check if target priv-mode has allowed external trap recordi= ng. */ + if ((env->priv =3D=3D PRV_M && !(tgt_ctrl & MCTRCTL_MTE)) || + (env->priv =3D=3D PRV_S && !(tgt_ctrl & MCTRCTL_STE))) { + return; + } + + ext_trap =3D true; + dst =3D 0; + } else if (!(src_ctrl & src_mask) && !(tgt_ctrl & tgt_mask)) { + return; + } + } else if (type =3D=3D CTRDATA_TYPE_EXCEP_INT_RET) { + /* + * Case 3 for trap returns. Trap returns from inhibited mode are = not + * recorded. + */ + if (!(src_ctrl & src_mask)) { + return; + } + + /* Case 2 for trap returns. */ + if (!(tgt_ctrl & tgt_mask)) { + dst =3D 0; + } + } else if (!(tgt_ctrl & tgt_mask)) { + return; + } + + /* Ignore filters in case of RASEMU mode or External trap. */ + if (!(tgt_ctrl & MCTRCTL_RASEMU) && !ext_trap) { + /* + * Check if the specific type is inhibited. Not taken branch filte= r is + * an enable bit and needs to be checked separatly. + */ + bool check =3D tgt_ctrl & BIT_ULL(type + MCTRCTL_INH_START); + if ((type =3D=3D CTRDATA_TYPE_NONTAKEN_BRANCH && !check) || + (type !=3D CTRDATA_TYPE_NONTAKEN_BRANCH && check)) { + return; + } + } + + head =3D get_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK); + + depth =3D 16 << get_field(env->sctrdepth, SCTRDEPTH_MASK); + if (tgt_ctrl & MCTRCTL_RASEMU && type =3D=3D CTRDATA_TYPE_RETURN) { + head =3D (head - 1) & (depth - 1); + + env->ctr_src[head] &=3D ~CTRSOURCE_VALID; + env->sctrstatus =3D + set_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK, head); + return; + } + + /* In case of Co-routine SWAP we overwrite latest entry. */ + if (tgt_ctrl & MCTRCTL_RASEMU && type =3D=3D CTRDATA_TYPE_CO_ROUTINE_S= WAP) { + head =3D (head - 1) & (depth - 1); + } + + env->ctr_src[head] =3D src | CTRSOURCE_VALID; + env->ctr_dst[head] =3D dst & ~CTRTARGET_MISP; + env->ctr_data[head] =3D set_field(0, CTRDATA_TYPE_MASK, type); + + head =3D (head + 1) & (depth - 1); + + env->sctrstatus =3D set_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK, = head); +} + void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv, bool vir= t_en) { g_assert(newpriv <=3D PRV_M && newpriv !=3D PRV_RESERVED); @@ -1669,10 +1864,13 @@ void riscv_cpu_do_interrupt(CPUState *cs) !(env->mip & (1 << cause)); bool vs_injected =3D env->hvip & (1 << cause) & env->hvien && !(env->mip & (1 << cause)); + const bool prev_virt =3D env->virt_enabled; + const target_ulong prev_priv =3D env->priv; target_ulong tval =3D 0; target_ulong tinst =3D 0; target_ulong htval =3D 0; target_ulong mtval2 =3D 0; + target_ulong src; =20 if (!async) { /* set tval to badaddr for traps with address information */ @@ -1729,6 +1927,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) tval =3D cs->watchpoint_hit->hitaddr; cs->watchpoint_hit =3D NULL; } + riscv_ctr_freeze(env, MCTRCTL_BPFRZ); break; default: break; @@ -1807,6 +2006,8 @@ void riscv_cpu_do_interrupt(CPUState *cs) env->pc =3D (env->stvec >> 2 << 2) + ((async && (env->stvec & 3) =3D=3D 1) ? cause * 4 : 0); riscv_cpu_set_mode(env, PRV_S, virt); + + src =3D env->sepc; } else { /* handle the trap in M-mode */ if (riscv_has_ext(env, RVH)) { @@ -1838,8 +2039,13 @@ void riscv_cpu_do_interrupt(CPUState *cs) env->pc =3D (env->mtvec >> 2 << 2) + ((async && (env->mtvec & 3) =3D=3D 1) ? cause * 4 : 0); riscv_cpu_set_mode(env, PRV_M, virt); + src =3D env->mepc; } =20 + riscv_ctr_add_entry(env, src, env->pc, + async ? CTRDATA_TYPE_INTERRUPT : CTRDATA_TYPE_EXCE= PTION, + prev_priv, prev_virt); + /* * NOTE: it is not necessary to yield load reservations here. It is on= ly * necessary for an SC from "another hart" to cause a load reservation diff --git a/target/riscv/helper.h b/target/riscv/helper.h index 451261ce5a..0a9a545d87 100644 --- a/target/riscv/helper.h +++ b/target/riscv/helper.h @@ -129,12 +129,16 @@ DEF_HELPER_2(csrr_i128, tl, env, int) DEF_HELPER_4(csrw_i128, void, env, int, tl, tl) DEF_HELPER_6(csrrw_i128, tl, env, int, tl, tl, tl, tl) #ifndef CONFIG_USER_ONLY -DEF_HELPER_1(sret, tl, env) -DEF_HELPER_1(mret, tl, env) +DEF_HELPER_2(sret, tl, env, tl) +DEF_HELPER_2(mret, tl, env, tl) +DEF_HELPER_1(ctr_clear, void, env) DEF_HELPER_1(wfi, void, env) DEF_HELPER_1(wrs_nto, void, env) DEF_HELPER_1(tlb_flush, void, env) DEF_HELPER_1(tlb_flush_all, void, env) +DEF_HELPER_4(ctr_branch, void, env, tl, tl, tl) +DEF_HELPER_4(ctr_jal, void, env, tl, tl, tl) +DEF_HELPER_5(ctr_jalr, void, env, tl, tl, tl, tl) /* Native Debug */ DEF_HELPER_1(itrigger_match, void, env) #endif diff --git a/target/riscv/insn_trans/trans_privileged.c.inc b/target/riscv/= insn_trans/trans_privileged.c.inc index 4eccdddeaa..339d659151 100644 --- a/target/riscv/insn_trans/trans_privileged.c.inc +++ b/target/riscv/insn_trans/trans_privileged.c.inc @@ -78,9 +78,10 @@ static bool trans_sret(DisasContext *ctx, arg_sret *a) { #ifndef CONFIG_USER_ONLY if (has_ext(ctx, RVS)) { + TCGv src =3D tcg_constant_tl(ctx->base.pc_next); decode_save_opc(ctx); translator_io_start(&ctx->base); - gen_helper_sret(cpu_pc, tcg_env); + gen_helper_sret(cpu_pc, tcg_env, src); exit_tb(ctx); /* no chaining */ ctx->base.is_jmp =3D DISAS_NORETURN; } else { @@ -95,9 +96,10 @@ static bool trans_sret(DisasContext *ctx, arg_sret *a) static bool trans_mret(DisasContext *ctx, arg_mret *a) { #ifndef CONFIG_USER_ONLY + TCGv src =3D tcg_constant_tl(ctx->base.pc_next); decode_save_opc(ctx); translator_io_start(&ctx->base); - gen_helper_mret(cpu_pc, tcg_env); + gen_helper_mret(cpu_pc, tcg_env, src); exit_tb(ctx); /* no chaining */ ctx->base.is_jmp =3D DISAS_NORETURN; return true; diff --git a/target/riscv/insn_trans/trans_rvi.c.inc b/target/riscv/insn_tr= ans/trans_rvi.c.inc index ad40d3e87f..7f95362b66 100644 --- a/target/riscv/insn_trans/trans_rvi.c.inc +++ b/target/riscv/insn_trans/trans_rvi.c.inc @@ -55,6 +55,11 @@ static bool trans_jalr(DisasContext *ctx, arg_jalr *a) TCGLabel *misaligned =3D NULL; TCGv target_pc =3D tcg_temp_new(); TCGv succ_pc =3D dest_gpr(ctx, a->rd); +#ifndef CONFIG_USER_ONLY + TCGv rd =3D tcg_constant_tl(a->rd); + TCGv rs1 =3D tcg_constant_tl(a->rs1); + TCGv src =3D tcg_constant_tl(ctx->base.pc_next); +#endif =20 tcg_gen_addi_tl(target_pc, get_gpr(ctx, a->rs1, EXT_NONE), a->imm); tcg_gen_andi_tl(target_pc, target_pc, (target_ulong)-2); @@ -75,6 +80,9 @@ static bool trans_jalr(DisasContext *ctx, arg_jalr *a) gen_set_gpr(ctx, a->rd, succ_pc); =20 tcg_gen_mov_tl(cpu_pc, target_pc); +#ifndef CONFIG_USER_ONLY + gen_helper_ctr_jalr(tcg_env, src, cpu_pc, rd, rs1); +#endif lookup_and_goto_ptr(ctx); =20 if (misaligned) { @@ -164,6 +172,11 @@ static bool gen_branch(DisasContext *ctx, arg_b *a, TC= GCond cond) TCGv src1 =3D get_gpr(ctx, a->rs1, EXT_SIGN); TCGv src2 =3D get_gpr(ctx, a->rs2, EXT_SIGN); target_ulong orig_pc_save =3D ctx->pc_save; +#ifndef CONFIG_USER_ONLY + TCGv src =3D tcg_constant_tl(ctx->base.pc_next); + TCGv taken; + TCGv dest; +#endif =20 if (get_xl(ctx) =3D=3D MXL_RV128) { TCGv src1h =3D get_gprh(ctx, a->rs1); @@ -176,6 +189,14 @@ static bool gen_branch(DisasContext *ctx, arg_b *a, TC= GCond cond) } else { tcg_gen_brcond_tl(cond, src1, src2, l); } + +#ifndef CONFIG_USER_ONLY + dest =3D tcg_constant_tl(ctx->base.pc_next + ctx->cur_insn_len); + taken =3D tcg_constant_tl(0); + + gen_helper_ctr_branch(tcg_env, src, dest, taken); +#endif + gen_goto_tb(ctx, 1, ctx->cur_insn_len); ctx->pc_save =3D orig_pc_save; =20 @@ -188,6 +209,12 @@ static bool gen_branch(DisasContext *ctx, arg_b *a, TC= GCond cond) gen_pc_plus_diff(target_pc, ctx, a->imm); gen_exception_inst_addr_mis(ctx, target_pc); } else { +#ifndef CONFIG_USER_ONLY + dest =3D tcg_constant_tl(ctx->base.pc_next + a->imm); + taken =3D tcg_constant_tl(1); + + gen_helper_ctr_branch(tcg_env, src, dest, taken); +#endif gen_goto_tb(ctx, 0, a->imm); } ctx->pc_save =3D -1; diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c index 25a5263573..c8053d9c2f 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -259,10 +259,12 @@ void helper_cbo_inval(CPURISCVState *env, target_ulon= g address) =20 #ifndef CONFIG_USER_ONLY =20 -target_ulong helper_sret(CPURISCVState *env) +target_ulong helper_sret(CPURISCVState *env, target_ulong curr_pc) { uint64_t mstatus; target_ulong prev_priv, prev_virt =3D env->virt_enabled; + const target_ulong src_priv =3D env->priv; + const bool src_virt =3D env->virt_enabled; =20 if (!(env->priv >=3D PRV_S)) { riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); @@ -309,11 +311,17 @@ target_ulong helper_sret(CPURISCVState *env) =20 riscv_cpu_set_mode(env, prev_priv, prev_virt); =20 + riscv_ctr_add_entry(env, curr_pc, retpc, CTRDATA_TYPE_EXCEP_INT_RET, + src_priv, src_virt); + return retpc; } =20 -target_ulong helper_mret(CPURISCVState *env) +target_ulong helper_mret(CPURISCVState *env, target_ulong curr_pc) { + const target_ulong src_priv =3D env->priv; + const bool src_virt =3D env->virt_enabled; + if (!(env->priv >=3D PRV_M)) { riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); } @@ -350,9 +358,109 @@ target_ulong helper_mret(CPURISCVState *env) =20 riscv_cpu_set_mode(env, prev_priv, prev_virt); =20 + riscv_ctr_add_entry(env, curr_pc, retpc, CTRDATA_TYPE_EXCEP_INT_RET, + src_priv, src_virt); + return retpc; } =20 +/* + * Indirect calls + * =E2=80=93 jalr x1, rs where rs !=3D x5; + * =E2=80=93 jalr x5, rs where rs !=3D x1; + * =E2=80=93 c.jalr rs1 where rs1 !=3D x5; + * + * Indirect jumps + * =E2=80=93 jalr x0, rs where rs !=3D x1 and rs !=3D x5; + * =E2=80=93 c.jr rs1 where rs1 !=3D x1 and rs1 !=3D x5. + * + * Returns + * =E2=80=93 jalr rd, rs where (rs =3D=3D x1 or rs =3D=3D x5) and rd !=3D = x1 and rd !=3D x5; + * =E2=80=93 c.jr rs1 where rs1 =3D=3D x1 or rs1 =3D=3D x5. + * + * Co-routine swap + * =E2=80=93 jalr x1, x5; + * =E2=80=93 jalr x5, x1; + * =E2=80=93 c.jalr x5. + * + * Other indirect jumps + * =E2=80=93 jalr rd, rs where rs !=3D x1, rs !=3D x5, rd !=3D x0, rd !=3D= x1 and rd !=3D x5. + */ +void helper_ctr_jalr(CPURISCVState *env, target_ulong src, target_ulong de= st, + target_ulong rd, target_ulong rs1) +{ + target_ulong curr_priv =3D env->priv; + bool curr_virt =3D env->virt_enabled; + + if ((rd =3D=3D 1 && rs1 !=3D 5) || (rd =3D=3D 5 && rs1 !=3D 1)) { + riscv_ctr_add_entry(env, src, dest, CTRDATA_TYPE_INDIRECT_CALL, + curr_priv, curr_virt); + } else if (rd =3D=3D 0 && rs1 !=3D 1 && rs1 !=3D 5) { + riscv_ctr_add_entry(env, src, dest, CTRDATA_TYPE_INDIRECT_JUMP, + curr_priv, curr_virt); + } else if ((rs1 =3D=3D 1 || rs1 =3D=3D 5) && (rd !=3D 1 && rd !=3D 5))= { + riscv_ctr_add_entry(env, src, dest, CTRDATA_TYPE_RETURN, + curr_priv, curr_virt); + } else if ((rs1 =3D=3D 1 && rd =3D=3D 5) || (rs1 =3D=3D 5 && rd =3D=3D= 1)) { + riscv_ctr_add_entry(env, src, dest, CTRDATA_TYPE_CO_ROUTINE_SWAP, + curr_priv, curr_virt); + } else { + riscv_ctr_add_entry(env, src, dest, + CTRDATA_TYPE_OTHER_INDIRECT_JUMP, curr_priv, + curr_virt); + } +} + +/* + * Direct calls + * =E2=80=93 jal x1; + * =E2=80=93 jal x5; + * =E2=80=93 c.jal. + * + * Direct jumps + * =E2=80=93 jal x0; + * =E2=80=93 c.j; + * + * Other direct jumps + * =E2=80=93 jal rd where rd !=3D x1 and rd !=3D x5 and rd !=3D x0; + */ +void helper_ctr_jal(CPURISCVState *env, target_ulong src, target_ulong des= t, + target_ulong rd) +{ + target_ulong priv =3D env->priv; + bool virt =3D env->virt_enabled; + + /* + * If rd is x1 or x5 link registers, treat this as direct call otherwi= se + * its a direct jump. + */ + if (rd =3D=3D 1 || rd =3D=3D 5) { + riscv_ctr_add_entry(env, src, dest, CTRDATA_TYPE_DIRECT_CALL, priv, + virt); + } else if (rd =3D=3D 0) { + riscv_ctr_add_entry(env, src, dest, CTRDATA_TYPE_DIRECT_JUMP, priv, + virt); + } else { + riscv_ctr_add_entry(env, src, dest, CTRDATA_TYPE_OTHER_DIRECT_JUMP, + priv, virt); + } +} + +void helper_ctr_branch(CPURISCVState *env, target_ulong src, target_ulong = dest, + target_ulong branch_taken) +{ + target_ulong curr_priv =3D env->priv; + bool curr_virt =3D env->virt_enabled; + + if (branch_taken) { + riscv_ctr_add_entry(env, src, dest, CTRDATA_TYPE_TAKEN_BRANCH, + curr_priv, curr_virt); + } else { + riscv_ctr_add_entry(env, src, dest, CTRDATA_TYPE_NONTAKEN_BRANCH, + curr_priv, curr_virt); + } +} + void helper_wfi(CPURISCVState *env) { CPUState *cs =3D env_cpu(env); diff --git a/target/riscv/translate.c b/target/riscv/translate.c index 15e7123a68..8b0492991d 100644 --- a/target/riscv/translate.c +++ b/target/riscv/translate.c @@ -561,6 +561,11 @@ static void gen_set_fpr_d(DisasContext *ctx, int reg_n= um, TCGv_i64 t) static void gen_jal(DisasContext *ctx, int rd, target_ulong imm) { TCGv succ_pc =3D dest_gpr(ctx, rd); +#ifndef CONFIG_USER_ONLY + TCGv dest =3D tcg_constant_tl(ctx->base.pc_next + imm); + TCGv src =3D tcg_constant_tl(ctx->base.pc_next); + TCGv tcg_rd =3D tcg_constant_tl((target_ulong)rd); +#endif =20 /* check misaligned: */ if (!has_ext(ctx, RVC) && !ctx->cfg_ptr->ext_zca) { @@ -572,6 +577,10 @@ static void gen_jal(DisasContext *ctx, int rd, target_= ulong imm) } } =20 +#ifndef CONFIG_USER_ONLY + gen_helper_ctr_jal(tcg_env, src, dest, tcg_rd); +#endif + gen_pc_plus_diff(succ_pc, ctx, ctx->cur_insn_len); gen_set_gpr(ctx, rd, succ_pc); =20 --=20 2.34.1 From nobody Mon Nov 25 05:52:41 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 ARC-Seal: i=1; a=rsa-sha256; t=1716999116; cv=none; d=zohomail.com; s=zohoarc; b=NMktkFZDLkrBO/7M2CqRldEfiotBjctL8GAzxP5uVHJ/no8Y3tMTi1KzIt1aUSTwcSDjkedizXGszkfCOV6bWeTk6Kia/ehMnkBad6bcqVRyIGxyfamEEE7v1XDp53pk8b0s0GC+tH7KmcQutSznF5S3Exn82aTN0oPx6In5tGg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716999116; 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=iI6lEcod+kV6vGmbuxWzdf/Up/aCvnaIBFk1W2Klj5Y=; b=f8gQdUJ15LNoV52bNqzmBlJRFh46ve8k+/n92Zlj175aznJAXRIaMjYnN+Ur+zt1Hggfoov1nL+NcA73CLJBiRc8WZ+sLbhNqTbKSyJN4/+2YDTH6Q+ELpaQa52jfi/RlzKFKZWxAxp/hnbXHZ/FZaKVyFjmCcXnSxxg0H1kuFE= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716999116244745.239315095891; Wed, 29 May 2024 09:11:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sCLsc-00013O-PX; Wed, 29 May 2024 12:10:22 -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 1sCLsa-0000yz-DW for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:20 -0400 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sCLsF-0003Mx-CT for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:19 -0400 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-420180b59b7so19662115e9.0 for ; Wed, 29 May 2024 09:09:58 -0700 (PDT) Received: from rkanwal-XPS-15-9520.Home ([2a02:c7c:7527:ee00:7446:71c1:a41a:da9b]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3557dcf0740sm15228213f8f.107.2024.05.29.09.09.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 09:09:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716998998; x=1717603798; 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=iI6lEcod+kV6vGmbuxWzdf/Up/aCvnaIBFk1W2Klj5Y=; b=3NdSmr1YeY4zkUZX/DTdnpFiMdGIU/bQ51OQeY8ovFixbhDGJvdI40OycVyjm18nb0 brEAFM3/CipaBwlsffIt2iMgFgAsbS67k02+PxWAWuNptkM3Hhge+aV51oWiTzF0BjWY N9S2n7bzUf9BymdOIdbzLmetoqr50z4Glz1Mb3vi4pH2SmWyvW6qYbxYkWXVFmrx5LcE bmugpYhVvMhWRjVbevEeHiNY3SPM9XVpsvBmWg50WYFRkhEXD4hGGafheAWEuYEQDol9 6L1LPffn585/pt5E4ltituR28UH+jEG+ANsXjF9nnSil1n7IAxFYC3QfXHxfIeLR3JFn ibgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716998998; x=1717603798; 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=iI6lEcod+kV6vGmbuxWzdf/Up/aCvnaIBFk1W2Klj5Y=; b=XbvlKH59lbE4isYyYI+fUtt1ICkUJxmKQIQ7mQo8XO3RGseNLr9x1k0mQeGcuPF69J 5UwahO7PZEWPeSlrhMmPVqX7mGpw2M1SHGTzB3dNeV/NA0dtWa8hqCQ56myQjpb1JhdO zoaReEQfTKCUgJ/Rag2iWZGysHkNyZPxTRpakbcXu2DJhHGoyN/ZuxK/33SfeTCV9NAj IaHDEWtGHSV9yeqeO38p2+BtuwN0f6b9Kj2+qP3dZDyVFf6OT2cFCx6vyIGM8FpEO5RY suskH4/sKxaWkBqwpXMpxGNp+vsfQBJD3VUXS/9+vF1aeKQf8BNNgW/wB67e9e+vARDF IhNQ== X-Forwarded-Encrypted: i=1; AJvYcCWaa20aa8zuKABqH5OHCvp6EUDuT5knZPMALFyU/D94pHtTE+QYxKbwxKd3CBXNX4mttv2mFdEmAk+CTqZX0FSpTN1JSp0= X-Gm-Message-State: AOJu0YyQgC17C8WaqrpGCeDsBTfJtD9bj/wQ25u6UGPTNh+X2wQz7idm LT360OOd7YZzl5j7Wyp7Wor6ImVnodMWlS50f/6ioGpvCLZ4TYvFs9B1LGrxxks= X-Google-Smtp-Source: AGHT+IEKSXVpvPWdRNTTnkZUuQVzuld8coqRKSWudhLmkIpv62J6mN57sHkahY14egGZJ3Mcm6UJpA== X-Received: by 2002:a7b:c305:0:b0:41c:2313:d966 with SMTP id 5b1f17b1804b1-421089d3943mr132846665e9.1.1716998997854; Wed, 29 May 2024 09:09:57 -0700 (PDT) From: Rajnesh Kanwal To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, bin.meng@windriver.com, liweiwei@iscas.ac.cn, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com, atishp@rivosinc.com, apatel@ventanamicro.com, rkanwal@rivosinc.com, beeman@rivosinc.com, tech-control-transfer-records@lists.riscv.org Subject: [PATCH 5/6] target/riscv: Add CTR sctrclr instruction. Date: Wed, 29 May 2024 17:09:49 +0100 Message-Id: <20240529160950.132754-6-rkanwal@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240529160950.132754-1-rkanwal@rivosinc.com> References: <20240529160950.132754-1-rkanwal@rivosinc.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=2a00:1450:4864:20::330; envelope-from=rkanwal@rivosinc.com; helo=mail-wm1-x330.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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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 @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1716999118561100027 Content-Type: text/plain; charset="utf-8" CTR extension adds a new instruction sctrclr to quickly clear the recorded entries buffer. Signed-off-by: Rajnesh Kanwal --- target/riscv/cpu.h | 1 + target/riscv/cpu_helper.c | 7 +++++++ target/riscv/insn32.decode | 1 + target/riscv/insn_trans/trans_privileged.c.inc | 10 ++++++++++ target/riscv/op_helper.c | 5 +++++ 5 files changed, 24 insertions(+) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index a294a5372a..fade71aa09 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -572,6 +572,7 @@ void riscv_cpu_set_mode(CPURISCVState *env, target_ulon= g newpriv, bool virt_en); void riscv_ctr_freeze(CPURISCVState *env, uint64_t freeze_mask); void riscv_ctr_add_entry(CPURISCVState *env, target_long src, target_long = dst, uint64_t type, target_ulong prev_priv, bool prev_= virt); +void riscv_ctr_clear(CPURISCVState *env); =20 void riscv_translate_init(void); G_NORETURN void riscv_raise_exception(CPURISCVState *env, diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index e064a7306e..45502f50a7 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -704,6 +704,13 @@ void riscv_ctr_freeze(CPURISCVState *env, uint64_t fre= eze_mask) } } =20 +void riscv_ctr_clear(CPURISCVState *env) +{ + memset(env->ctr_src, 0x0, sizeof(env->ctr_src)); + memset(env->ctr_dst, 0x0, sizeof(env->ctr_dst)); + memset(env->ctr_data, 0x0, sizeof(env->ctr_data)); +} + static uint64_t riscv_ctr_priv_to_mask(target_ulong priv, bool virt) { switch (priv) { diff --git a/target/riscv/insn32.decode b/target/riscv/insn32.decode index 9cb1a1b4ec..d3d38c7c68 100644 --- a/target/riscv/insn32.decode +++ b/target/riscv/insn32.decode @@ -107,6 +107,7 @@ # *** Privileged Instructions *** ecall 000000000000 00000 000 00000 1110011 ebreak 000000000001 00000 000 00000 1110011 +sctrclr 000100000100 00000 000 00000 1110011 uret 0000000 00010 00000 000 00000 1110011 sret 0001000 00010 00000 000 00000 1110011 mret 0011000 00010 00000 000 00000 1110011 diff --git a/target/riscv/insn_trans/trans_privileged.c.inc b/target/riscv/= insn_trans/trans_privileged.c.inc index 339d659151..dd9da8651f 100644 --- a/target/riscv/insn_trans/trans_privileged.c.inc +++ b/target/riscv/insn_trans/trans_privileged.c.inc @@ -69,6 +69,16 @@ static bool trans_ebreak(DisasContext *ctx, arg_ebreak *= a) return true; } =20 +static bool trans_sctrclr(DisasContext *ctx, arg_sctrclr *a) +{ +#ifndef CONFIG_USER_ONLY + gen_helper_ctr_clear(tcg_env); + return true; +#else + return false; +#endif +} + static bool trans_uret(DisasContext *ctx, arg_uret *a) { return false; diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c index c8053d9c2f..89423c04b3 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -461,6 +461,11 @@ void helper_ctr_branch(CPURISCVState *env, target_ulon= g src, target_ulong dest, } } =20 +void helper_ctr_clear(CPURISCVState *env) +{ + riscv_ctr_clear(env); +} + void helper_wfi(CPURISCVState *env) { CPUState *cs =3D env_cpu(env); --=20 2.34.1 From nobody Mon Nov 25 05:52:41 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 ARC-Seal: i=1; a=rsa-sha256; t=1716999179; cv=none; d=zohomail.com; s=zohoarc; b=AHD5WILuWlPJbcLvbPGQH5bzVBz9wHhwUpW0DZIK0PMUCaphb0pHf7QIYlbrczkP+JkQ2tdYiAg2PEYURDJakLN/AYSw5bA381t8h+W13ZQ17qhCeSblO9FgPWkuukMpGNsU1K+xELWW4QXG15+M9dkCc7PYtw5klVosBvMV30U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716999179; 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=8dhVgnvdfsSEiK3cdIQuIqMe5XSQpUI6COG5vQN5dTE=; b=DYlcVdDwUvQo1WbVpkrU9/Ky26D1qWhNmn8H2IollAIhK7T2YOqvCZHA5mbzUGHGc1T/sWHQcCJTE9aHdEsJl9fOkbwBQQHXXOEdCfvDBnBHDPdSgbD2J8cAqcXtK6pnQwXf1GJ6B/JZko36nnffZK9joj66qLb6kfQltDsOC8o= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716999179075724.5990900853592; Wed, 29 May 2024 09:12:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sCLsm-00018u-Fl; Wed, 29 May 2024 12:10:32 -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 1sCLsk-00018B-Fp for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:30 -0400 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sCLsI-0003NK-BV for qemu-devel@nongnu.org; Wed, 29 May 2024 12:10:30 -0400 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-42122ac2f38so719345e9.1 for ; Wed, 29 May 2024 09:10:00 -0700 (PDT) Received: from rkanwal-XPS-15-9520.Home ([2a02:c7c:7527:ee00:7446:71c1:a41a:da9b]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3557dcf0740sm15228213f8f.107.2024.05.29.09.09.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 09:09:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716998999; x=1717603799; 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=8dhVgnvdfsSEiK3cdIQuIqMe5XSQpUI6COG5vQN5dTE=; b=AaSVMRaO3u6oDbYJjv9j07vKC79+KLZxPCNBem1rtoQEMggZU+pyB0w8jN2p2mp7fQ 4bSNQ/E9ARB705nnXU2X+kND7Ce66uwm1qnE62vmewLLy0x7r/e8qUk16gKgxu5KAjxg fsVp2f0hf21MY0aufCUy4v1gTh7SfWftQVnqcdG7g6svwQodY6tB2zWLcC/skMGoJ9tV IYoVxdmgszj1ny022SwejEo8LSdeETWQ5yOgjnJVpGz457qRIl6zwfDKs5RPnelFM4ak 0foWeFK46IckVWI5nIFNrvIO/JE/ZBWkJGKKDrMM7FIP3IMbkyfxzU+ZzFBMKA7Wn1kW L52A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716998999; x=1717603799; 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=8dhVgnvdfsSEiK3cdIQuIqMe5XSQpUI6COG5vQN5dTE=; b=SGqoxvSviX2u6u7P6OEnVOZMdeTcg5j6JfBcMOfcg+T5dZA20PwLQepSSbPLKt3F3B l2J1dIhNNizjGQKE7je7o3oaKnTbzl3zIV1XTYS00Is3ti7NPKEju9XsAOSukUWGX6gg yHur/2IvQnY7mzxHBEmAnHCxtsBPXtciOa5YJdzRtCQyZZgdOeokuz2tsHGUan6uC+oH BD9pvNbkaje2VSGdMGtOXpxr0ZmuxydyKEw8mmbnKEiuePfkKihMffF/KrG5wRU3msB0 yJQBOQ14C/keBLup1Eo17i4rvyHHFC2X3pf2POZ92S0Z6wjqWXVzsrkp35+XojnZvB+d SuFw== X-Forwarded-Encrypted: i=1; AJvYcCWN26dRXcHWigRtYRRmcs5jbv7aZ1YGdVVrTTwLY5wUqVM5w8sqz1nuIj5SyWRzYEMMeQTr8AJu+xolT71xwcHZBNrGyvI= X-Gm-Message-State: AOJu0YwQmbQ1Aa1c0V/EVQbmuosvz+wiuich45VycS4h7qY/I3FVILeg PI7VcKATWuok9+xwmHrXwDqAkzcBF8inYIq3QyfwYizqbdvM1ChcTtYSy2Hzxkk= X-Google-Smtp-Source: AGHT+IEzPb+Nvi0RwEK3w0dHknFO7/g6S7ljMR3QWhfsVPoCeYtFQTE1kNo5a+c35gQACukvYdVjBw== X-Received: by 2002:a05:600c:468a:b0:41b:4caa:554c with SMTP id 5b1f17b1804b1-42122adb0eamr26331835e9.2.1716998998743; Wed, 29 May 2024 09:09:58 -0700 (PDT) From: Rajnesh Kanwal To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, bin.meng@windriver.com, liweiwei@iscas.ac.cn, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com, atishp@rivosinc.com, apatel@ventanamicro.com, rkanwal@rivosinc.com, beeman@rivosinc.com, tech-control-transfer-records@lists.riscv.org Subject: [PATCH 6/6] target/riscv: Add support to access ctrsource, ctrtarget, ctrdata regs. Date: Wed, 29 May 2024 17:09:50 +0100 Message-Id: <20240529160950.132754-7-rkanwal@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240529160950.132754-1-rkanwal@rivosinc.com> References: <20240529160950.132754-1-rkanwal@rivosinc.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=2a00:1450:4864:20::335; envelope-from=rkanwal@rivosinc.com; helo=mail-wm1-x335.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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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 @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1716999180378100006 Content-Type: text/plain; charset="utf-8" CTR entries are accessed using ctrsource, ctrtarget and ctrdata registers using smcsrind/sscsrind extension. This commits extends the csrind extension to support CTR registers. ctrsource is accessible through xireg CSR, ctrtarget is accessible through xireg1 and ctrdata is accessible through xireg2 CSR. CTR supports maximum depth of 256 entries which are accessed using xiselect range 0x200 to 0x2ff. This commits also adds properties to enable CTR extension. CTR can be enabled using smctr=3Dtrue and ssctr=3Dtrue now. Signed-off-by: Rajnesh Kanwal --- target/riscv/cpu.c | 4 ++ target/riscv/csr.c | 153 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 156 insertions(+), 1 deletion(-) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 30bdfc22ae..a77b1d5caf 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -193,6 +193,8 @@ const RISCVIsaExtData isa_edata_arr[] =3D { ISA_EXT_DATA_ENTRY(sstvala, PRIV_VERSION_1_12_0, has_priv_1_12), ISA_EXT_DATA_ENTRY(sstvecd, PRIV_VERSION_1_12_0, has_priv_1_12), ISA_EXT_DATA_ENTRY(svade, PRIV_VERSION_1_11_0, ext_svade), + ISA_EXT_DATA_ENTRY(smctr, PRIV_VERSION_1_12_0, ext_smctr), + ISA_EXT_DATA_ENTRY(ssctr, PRIV_VERSION_1_12_0, ext_ssctr), ISA_EXT_DATA_ENTRY(svadu, PRIV_VERSION_1_12_0, ext_svadu), ISA_EXT_DATA_ENTRY(svinval, PRIV_VERSION_1_12_0, ext_svinval), ISA_EXT_DATA_ENTRY(svnapot, PRIV_VERSION_1_12_0, ext_svnapot), @@ -1473,6 +1475,8 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = =3D { MULTI_EXT_CFG_BOOL("sscsrind", ext_sscsrind, false), MULTI_EXT_CFG_BOOL("smcdeleg", ext_smcdeleg, false), MULTI_EXT_CFG_BOOL("ssccfg", ext_ssccfg, false), + MULTI_EXT_CFG_BOOL("smctr", ext_smctr, false), + MULTI_EXT_CFG_BOOL("ssctr", ext_ssctr, false), MULTI_EXT_CFG_BOOL("zifencei", ext_zifencei, true), MULTI_EXT_CFG_BOOL("zicsr", ext_zicsr, true), MULTI_EXT_CFG_BOOL("zihintntl", ext_zihintntl, true), diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 888084d8e5..15b953f268 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -2291,6 +2291,11 @@ static bool xiselect_cd_range(target_ulong isel) return (ISELECT_CD_FIRST <=3D isel && isel <=3D ISELECT_CD_LAST); } =20 +static bool xiselect_ctr_range(target_ulong isel) +{ + return (CTR_ENTRIES_FIRST <=3D isel && isel <=3D CTR_ENTRIES_LAST); +} + static int rmw_iprio(target_ulong xlen, target_ulong iselect, uint8_t *iprio, target_ulong *val, target_ulong new_val, @@ -2336,6 +2341,118 @@ static int rmw_iprio(target_ulong xlen, return 0; } =20 +static int rmw_xctrsource(CPURISCVState *env, int isel, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) +{ + /* + * CTR arrays are treated as circular buffers and TOS always points to= next + * empty slot, keeping TOS - 1 always pointing to latest entry. Given = entry + * 0 is always the latest one, traversal is a bit different here. See = the + * below example. + * + * Depth =3D 16. + * + * idx [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] = [F] + * TOS H + * entry 6 5 4 3 2 1 0 F E D C B A 9 8 = 7 + */ + const uint64_t entry =3D isel - CTR_ENTRIES_FIRST; + const uint64_t depth =3D 16 << get_field(env->sctrdepth, SCTRDEPTH_MAS= K); + uint64_t idx; + + /* Entry greater than depth-1 is read-only zero */ + if (entry >=3D depth) { + *val =3D 0; + return 0; + } + + idx =3D get_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK); + idx =3D (idx - entry - 1) & (depth - 1); + + if (val) { + *val =3D env->ctr_src[idx]; + } + + env->ctr_src[idx] =3D (env->ctr_src[idx] & ~wr_mask) | (new_val & wr_m= ask); + + return 0; +} + +static int rmw_xctrtarget(CPURISCVState *env, int isel, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) +{ + /* + * CTR arrays are treated as circular buffers and TOS always points to= next + * empty slot, keeping TOS - 1 always pointing to latest entry. Given = entry + * 0 is always the latest one, traversal is a bit different here. See = the + * below example. + * + * Depth =3D 16. + * + * idx [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] = [F] + * head H + * entry 6 5 4 3 2 1 0 F E D C B A 9 8 = 7 + */ + const uint64_t entry =3D isel - CTR_ENTRIES_FIRST; + const uint64_t depth =3D 16 << get_field(env->sctrdepth, SCTRDEPTH_MAS= K); + uint64_t idx; + + /* Entry greater than depth-1 is read-only zero */ + if (entry >=3D depth) { + *val =3D 0; + return 0; + } + + idx =3D get_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK); + idx =3D (idx - entry - 1) & (depth - 1); + + if (val) { + *val =3D env->ctr_dst[idx]; + } + + env->ctr_dst[idx] =3D (env->ctr_dst[idx] & ~wr_mask) | (new_val & wr_m= ask); + + return 0; +} + +static int rmw_xctrdata(CPURISCVState *env, int isel, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) +{ + /* + * CTR arrays are treated as circular buffers and TOS always points to= next + * empty slot, keeping TOS - 1 always pointing to latest entry. Given = entry + * 0 is always the latest one, traversal is a bit different here. See = the + * below example. + * + * Depth =3D 16. + * + * idx [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] = [F] + * head H + * entry 6 5 4 3 2 1 0 F E D C B A 9 8 = 7 + */ + const uint64_t entry =3D isel - CTR_ENTRIES_FIRST; + const uint64_t mask =3D wr_mask & CTRDATA_MASK; + const uint64_t depth =3D 16 << get_field(env->sctrdepth, SCTRDEPTH_MAS= K); + uint64_t idx; + + /* Entry greater than depth-1 is read-only zero */ + if (entry >=3D depth) { + *val =3D 0; + return 0; + } + + idx =3D get_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK); + idx =3D (idx - entry - 1) & (depth - 1); + + if (val) { + *val =3D env->ctr_data[idx]; + } + + env->ctr_data[idx] =3D (env->ctr_data[idx] & ~mask) | (new_val & mask); + + return 0; +} + static RISCVException rmw_xireg_aia(CPURISCVState *env, int csrno, target_ulong isel, target_ulong *val, target_ulong new_val, target_ulong wr_mask) @@ -2486,6 +2603,38 @@ done: return ret; } =20 +static int rmw_xireg_ctr(CPURISCVState *env, int csrno, + target_ulong isel, target_ulong *val, + target_ulong new_val, target_ulong wr_mask) +{ + bool ext_sxctr =3D false; + int ret =3D -EINVAL; + + if (CSR_MIREG <=3D csrno && csrno <=3D CSR_MIREG3) { + ext_sxctr =3D riscv_cpu_cfg(env)->ext_smctr; + } else if (CSR_SIREG <=3D csrno && csrno <=3D CSR_SIREG3) { + ext_sxctr =3D riscv_cpu_cfg(env)->ext_ssctr; + } else if (CSR_VSIREG <=3D csrno && csrno <=3D CSR_VSIREG3) { + ext_sxctr =3D riscv_cpu_cfg(env)->ext_ssctr; + } + + if (!ext_sxctr) { + return -EINVAL; + } + + if (csrno =3D=3D CSR_MIREG || csrno =3D=3D CSR_SIREG || csrno =3D=3D C= SR_VSIREG) { + ret =3D rmw_xctrsource(env, isel, val, new_val, wr_mask); + } else if (csrno =3D=3D CSR_MIREG2 || csrno =3D=3D CSR_SIREG2 || + csrno =3D=3D CSR_VSIREG2) { + ret =3D rmw_xctrtarget(env, isel, val, new_val, wr_mask); + } else if (csrno =3D=3D CSR_MIREG3 || csrno =3D=3D CSR_SIREG3 || + csrno =3D=3D CSR_VSIREG3) { + ret =3D rmw_xctrdata(env, isel, val, new_val, wr_mask); + } + + return ret; +} + /* * rmw_xireg_sxcsrind: Perform indirect access to xireg and xireg2-xireg6 * @@ -2497,11 +2646,13 @@ static int rmw_xireg_sxcsrind(CPURISCVState *env, i= nt csrno, target_ulong isel, target_ulong *val, target_ulong new_val, target_ulong wr_mask) { - int ret =3D -EINVAL; bool virt =3D csrno =3D=3D CSR_VSIREG ? true : false; + int ret =3D -EINVAL; =20 if (xiselect_cd_range(isel)) { ret =3D rmw_xireg_cd(env, csrno, isel, val, new_val, wr_mask); + } else if (xiselect_ctr_range(isel)) { + ret =3D rmw_xireg_ctr(env, csrno, isel, val, new_val, wr_mask); } else { /* * As per the specification, access to unimplented region is undef= ined --=20 2.34.1