From nobody Mon May 6 09:02:51 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=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 1651203447889348.84558542828665; Thu, 28 Apr 2022 20:37:27 -0700 (PDT) Received: from localhost ([::1]:36004 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nkHRe-0004gM-FC for importer2@patchew.org; Thu, 28 Apr 2022 23:37:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47368) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nkHP1-0002Lw-Uu for qemu-devel@nongnu.org; Thu, 28 Apr 2022 23:34:44 -0400 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]:52903) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nkHOz-0002Qj-SU for qemu-devel@nongnu.org; Thu, 28 Apr 2022 23:34:43 -0400 Received: by mail-pj1-x102a.google.com with SMTP id e24so6034631pjt.2 for ; Thu, 28 Apr 2022 20:34:41 -0700 (PDT) Received: from anup-ubuntu64-vm.. ([122.167.97.237]) by smtp.gmail.com with ESMTPSA id l22-20020a17090a071600b001d9781de67fsm8195568pjl.31.2022.04.28.20.34.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 20:34:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7pwjbDN4AJMz1jW5DOU3a5WlAG2G7NEnP2vQW8ryYn4=; b=BzURZO75IkqhAiPc6i7MjsvX5LPlI5wjZ4L88rTy/K7zz8IpuMJU8ETEsEh3FYhDlM AuoM33C/ERmYEuSkxbcDdobyPFkbbKL2ynmETUWNj9aJ8erGAxQFRJPi2DYgOx9KG0ea 8KMmQqOrlkvBVsJpmZEDllGRhBR3PTLNxJAbobmKUXGyWIyQEznlVjvqUEcfiunGMR+T v9JO00kp/PrCmS0HfH+Jui87EVzES6EGT5er/vYkY8dHuHxtx8vnyvA75HG0vZX2ZYV0 e9iUZeOpnYnwdisnF/cBWbWRACsHFjJabps+RxcZiKaMdwvU8SxLuu+kfMo4JqLm8sBH 222Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7pwjbDN4AJMz1jW5DOU3a5WlAG2G7NEnP2vQW8ryYn4=; b=vzO+yyex1V4OA9eF60LBkw5MBRWuTUISqJkRfzfYylEfklYnnrMTzXxZyoEI4yDqbz mnzM8BXvOx/VYEJfs5zktGRJ2BaJopgxbACxf4E5Iicm0DCQT5eM4Wpj9m10WzanQZHH hW9F5Z8Z/PG6zkKSVCE0y9geQWNJ9Jmv4juM0b/H6kvE1tGUgoAFQjZ/EtBmplkQ88Kx xTBKd4HETmysAFUqmOkhxVu/LXtfY4q1iCZ79FnGpBxfxTumJhxVUuFeT3KlM6MZneDk 2bAGE+mFoGYwDfwIRYdrM0BwAGorfCqcmdmyI3k20bHyZ+VMelsqp06nOYiE0lzv5LTP 4mEA== X-Gm-Message-State: AOAM531wDsBDeWtgmuUdN2bwtjVvaUY5Y5RNQe5jCJMqQQH9n2ncty0H xLDKFmQLUZvWaiTg8Ye01RmmBA== X-Google-Smtp-Source: ABdhPJy/tFI8OFHOQYoOBFtyDqisystRs844dW1HV0vUMko348OhZrWttk9aau2w54rITZyAvepQWQ== X-Received: by 2002:a17:902:d484:b0:15e:7aa9:babc with SMTP id c4-20020a170902d48400b0015e7aa9babcmr881111plg.38.1651203280371; Thu, 28 Apr 2022 20:34:40 -0700 (PDT) From: Anup Patel To: Peter Maydell , Palmer Dabbelt , Alistair Francis , Sagar Karandikar Subject: [PATCH 1/4] target/riscv: Fix csr number based privilege checking Date: Fri, 29 Apr 2022 09:04:06 +0530 Message-Id: <20220429033409.258707-2-apatel@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220429033409.258707-1-apatel@ventanamicro.com> References: <20220429033409.258707-1-apatel@ventanamicro.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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102a; envelope-from=apatel@ventanamicro.com; helo=mail-pj1-x102a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, 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: , Cc: Anup Patel , Anup Patel , qemu-riscv@nongnu.org, qemu-devel@nongnu.org, Atish Patra Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1651203449174100001 Content-Type: text/plain; charset="utf-8" When hypervisor and VS CSRs are accessed from VS-mode or VU-mode, the riscv_csrrw_check() function should generate virtual instruction trap instead illegal instruction trap. Fixes: 533c91e8f22c ("target/riscv: Use RISCVException enum for CSR access") Signed-off-by: Anup Patel Reviewed-by: Alistair Francis Reviewed-by: Frank Chang --- target/riscv/csr.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 3500e07f92..2bf0a97196 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -3139,7 +3139,7 @@ static inline RISCVException riscv_csrrw_check(CPURIS= CVState *env, int read_only =3D get_field(csrno, 0xC00) =3D=3D 3; int csr_min_priv =3D csr_ops[csrno].min_priv_ver; #if !defined(CONFIG_USER_ONLY) - int effective_priv =3D env->priv; + int csr_priv, effective_priv =3D env->priv; =20 if (riscv_has_ext(env, RVH) && env->priv =3D=3D PRV_S && @@ -3152,7 +3152,11 @@ static inline RISCVException riscv_csrrw_check(CPURI= SCVState *env, effective_priv++; } =20 - if (!env->debugger && (effective_priv < get_field(csrno, 0x300))) { + csr_priv =3D get_field(csrno, 0x300); + if (!env->debugger && (effective_priv < csr_priv)) { + if (csr_priv =3D=3D (PRV_S + 1) && riscv_cpu_virt_enabled(env)) { + return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; + } return RISCV_EXCP_ILLEGAL_INST; } #endif --=20 2.34.1 From nobody Mon May 6 09:02:51 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=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 16512036372721014.2652121273721; Thu, 28 Apr 2022 20:40:37 -0700 (PDT) Received: from localhost ([::1]:42410 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nkHUh-0000cX-Px for importer2@patchew.org; Thu, 28 Apr 2022 23:40:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47402) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nkHP5-0002QG-3g for qemu-devel@nongnu.org; Thu, 28 Apr 2022 23:34:47 -0400 Received: from mail-pg1-x52d.google.com ([2607:f8b0:4864:20::52d]:37817) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nkHP3-0002RH-Es for qemu-devel@nongnu.org; Thu, 28 Apr 2022 23:34:46 -0400 Received: by mail-pg1-x52d.google.com with SMTP id 15so5560603pgf.4 for ; Thu, 28 Apr 2022 20:34:45 -0700 (PDT) Received: from anup-ubuntu64-vm.. ([122.167.97.237]) by smtp.gmail.com with ESMTPSA id l22-20020a17090a071600b001d9781de67fsm8195568pjl.31.2022.04.28.20.34.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 20:34:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XE6VlfMYlfE/3yLrLBtsEwY5zghvp90cbL5X7mMVwi0=; b=BZ24z1oidTTQHebrwffPilSf23aMhT6iQfb3rCkTVnu0lK6sDLx+0/PYl+KbECO0w3 1t4JNyW6rnbbEF0TIfHCJ/jh4QJ6o7Qg1YAbs4oQnzSFUoCbYziBzf0FgfZOnHPvmvOu fT4OSPJc3lngYwLJHL6NIAFornEk812NzYYNR4blEPLyb2zddqVVk8G2FgQKF5lF+ym8 RiDmjpF9exYz2SY7AByg1w5p+0+lJp++QXwwi9vQnQsA/yW87Xxqe3kXXVs3OZRGCRtS hTwF9UTpMGG0DjfOqrRFrxc8K9rX2b3t8L9Q/Cs0CvSbSZq6ZLuqrDQ2kPxQoY+/7zJB wKXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XE6VlfMYlfE/3yLrLBtsEwY5zghvp90cbL5X7mMVwi0=; b=AAouUFRWYLMcvrkEy0w74jumvxUV6tJtPztJ1YHVGGbGx1XnS//akUgCYaB+0pE+Co utaxPj40429kYAYtzEd5h8ZVY7nRwyRKfWnjxv6K0NRPQqDnfrOeaCfNJ3PsR0MkXoFy U0zMFPv3EFzO70Xc16CgegboqlAjiKZDslt2gsKlb0Tyt14gx6fL1hDTLbK/DzSRhjki mDL21xGbg3hYm01dsyy9JoV3i9RRtfT+odJX1kbTh2dCxaaO1XcHSfoqcpRq0tY/OM4p UHAg0FXsiB+DIAOAS9e5qqddtDY68HTIHts/lIEVT0urefqZo8RG9p9hEXWaCgVom22E nlog== X-Gm-Message-State: AOAM532aYefFXkwUe5qR/jKE2ldsrx5uC22zvOvj2TsPdumnSQKOoXSL NyIkW9LoBA6N5KzW6KH7mrwbNg== X-Google-Smtp-Source: ABdhPJyUvjuh/hixm1ou2mAZ+KmwIBg3usaud5Rq0DktWzzrHoq0/2Xn+QQwQiYy+j1iX0x/yMUC+Q== X-Received: by 2002:a63:6982:0:b0:3aa:a7c9:3913 with SMTP id e124-20020a636982000000b003aaa7c93913mr30442913pgc.295.1651203284127; Thu, 28 Apr 2022 20:34:44 -0700 (PDT) From: Anup Patel To: Peter Maydell , Palmer Dabbelt , Alistair Francis , Sagar Karandikar Subject: [PATCH 2/4] target/riscv: Fix hstatus.GVA bit setting for traps taken from HS-mode Date: Fri, 29 Apr 2022 09:04:07 +0530 Message-Id: <20220429033409.258707-3-apatel@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220429033409.258707-1-apatel@ventanamicro.com> References: <20220429033409.258707-1-apatel@ventanamicro.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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::52d; envelope-from=apatel@ventanamicro.com; helo=mail-pg1-x52d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, 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: , Cc: Anup Patel , Anup Patel , qemu-riscv@nongnu.org, qemu-devel@nongnu.org, Atish Patra Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1651203638042100001 Content-Type: text/plain; charset="utf-8" Currently, QEMU does not set hstatus.GVA bit for traps taken from HS-mode into HS-mode which breaks the Xvisor nested MMU test suite on QEMU. This was working previously. This patch updates riscv_cpu_do_interrupt() to fix the above issue. Fixes: 86d0c457396b ("target/riscv: Fixup setting GVA") Signed-off-by: Anup Patel --- target/riscv/cpu_helper.c | 1 - 1 file changed, 1 deletion(-) diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index e1aa4f2097..d83579accf 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -1434,7 +1434,6 @@ void riscv_cpu_do_interrupt(CPUState *cs) /* Trap into HS mode */ env->hstatus =3D set_field(env->hstatus, HSTATUS_SPV, fals= e); htval =3D env->guest_phys_fault_addr; - write_gva =3D false; } env->hstatus =3D set_field(env->hstatus, HSTATUS_GVA, write_gv= a); } --=20 2.34.1 From nobody Mon May 6 09:02:51 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=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 1651203462953935.4476414911948; Thu, 28 Apr 2022 20:37:42 -0700 (PDT) Received: from localhost ([::1]:36352 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nkHRt-0004uA-0U for importer2@patchew.org; Thu, 28 Apr 2022 23:37:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47438) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nkHP8-0002eF-VC for qemu-devel@nongnu.org; Thu, 28 Apr 2022 23:34:51 -0400 Received: from mail-pg1-x529.google.com ([2607:f8b0:4864:20::529]:42823) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nkHP7-0002Sp-5A for qemu-devel@nongnu.org; Thu, 28 Apr 2022 23:34:50 -0400 Received: by mail-pg1-x529.google.com with SMTP id bg9so5547929pgb.9 for ; Thu, 28 Apr 2022 20:34:48 -0700 (PDT) Received: from anup-ubuntu64-vm.. ([122.167.97.237]) by smtp.gmail.com with ESMTPSA id l22-20020a17090a071600b001d9781de67fsm8195568pjl.31.2022.04.28.20.34.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 20:34:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=v7mY11VfcFj7gRGSs/VoAnL10t6P2FjjSrNp/9c18FU=; b=MGLcQUG908rd/9UqRoiCluQyKTRtVF4rEE1A6kq4qF64j44Dh+JuzQywobSExEib2C 6sYaecWQiznuzKPLMqguJIEPu2VoIa/vHtbUWy3I3WUQTxqk9vr1uAcwPPUq1Q6+vzHF b0pbkt3Y0BuASt0KXtaU3sOPm4l/wL6tnDe667Lz9EjTFTnHFrOIB8Ln9Py7yqBq/Oz1 hzsgay8TR5hCMogl7UfPVhwdU0lsSmxqJkRZOkvFO0MuaWEc4T3o2dkCzUMY8GG/O2Jw /26Bl9jbEyppJwcNGPBuE/taBbHmd8XC1zNAt0bvHB2XnADLXGDDT7zocCuRxxlm5mqL C/3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=v7mY11VfcFj7gRGSs/VoAnL10t6P2FjjSrNp/9c18FU=; b=in3cwMCbNrJBCITd1BiQelPQ2eV1397RhJJHpUYfkI5/bksy1jIBonk0YoKzR7asEg zAVXROnREqkD9o2JKXGcEMjyNKrefMp21I0p3/tdswSwnVVs8dPXLitgpW4Gn/EmMEPT GTJCjzcIY+3a1cy/wdnygEGYkw7dzSgEpSx5in5sWqoz1e3clvI1g2DZpduFsAJ14481 vlXBGENkZrzQQikMnVDrgGxn0OLP97wCWbTdNxCdWG3ftUHxFZcgEUVUAkNa1fxUAaGI VotWgxqorzp0GkZZhDZkLa/U2Xxemati8OMBArwdHFgvvQkmc1UXZz7Bx8e73OIeuU/o YNbw== X-Gm-Message-State: AOAM530Dd6OOnuJs2Rd5Whhikk/Lhql4ndK9FKnms0trtT+lDb2SpQAf 5YMXXBwMh1s0wk6XE1Obi+Gtog== X-Google-Smtp-Source: ABdhPJwpVyPnvZTHPD8xcN/qExPx9nQv4SlF8ks38Cgjau5erhUvCPFaEsVxarObjDL0k9CN+TU4EA== X-Received: by 2002:a63:846:0:b0:39d:9a9d:1178 with SMTP id 67-20020a630846000000b0039d9a9d1178mr30658746pgi.225.1651203287842; Thu, 28 Apr 2022 20:34:47 -0700 (PDT) From: Anup Patel To: Peter Maydell , Palmer Dabbelt , Alistair Francis , Sagar Karandikar Subject: [PATCH 3/4] target/riscv: Set [m|s]tval for both illegal and virtual instruction traps Date: Fri, 29 Apr 2022 09:04:08 +0530 Message-Id: <20220429033409.258707-4-apatel@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220429033409.258707-1-apatel@ventanamicro.com> References: <20220429033409.258707-1-apatel@ventanamicro.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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::529; envelope-from=apatel@ventanamicro.com; helo=mail-pg1-x529.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, 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: , Cc: Anup Patel , Anup Patel , qemu-riscv@nongnu.org, qemu-devel@nongnu.org, Atish Patra Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1651203463264100001 Content-Type: text/plain; charset="utf-8" Currently, the [m|s]tval CSRs are set with trapping instruction encoding only for illegal instruction traps taken at the time of instruction decoding. In RISC-V world, a valid instructions might also trap as illegal or virtual instruction based to trapping bits in various CSRs (such as mstatus.TVM or hstatus.VTVM). We improve setting of [m|s]tval CSRs for all types of illegal and virtual instruction traps. Signed-off-by: Anup Patel Reviewed-by: Alistair Francis Reviewed-by: Frank Chang --- target/riscv/cpu.c | 2 ++ target/riscv/cpu.h | 8 +++++++- target/riscv/cpu_helper.c | 1 + target/riscv/translate.c | 17 +++++++++++++---- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index dff4606585..f0a702fee6 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -406,6 +406,7 @@ void restore_state_to_opc(CPURISCVState *env, Translati= onBlock *tb, } else { env->pc =3D data[0]; } + env->bins =3D data[1]; } =20 static void riscv_cpu_reset(DeviceState *dev) @@ -445,6 +446,7 @@ static void riscv_cpu_reset(DeviceState *dev) env->mcause =3D 0; env->miclaim =3D MIP_SGEIP; env->pc =3D env->resetvec; + env->bins =3D 0; env->two_stage_lookup =3D false; =20 /* Initialized default priorities of local interrupts. */ diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index fe6c9a2c92..a55c918274 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -30,6 +30,12 @@ =20 #define TCG_GUEST_DEFAULT_MO 0 =20 +/* + * RISC-V-specific extra insn start words: + * 1: Original instruction opcode + */ +#define TARGET_INSN_START_EXTRA_WORDS 1 + #define TYPE_RISCV_CPU "riscv-cpu" =20 #define RISCV_CPU_TYPE_SUFFIX "-" TYPE_RISCV_CPU @@ -140,7 +146,7 @@ struct CPUArchState { target_ulong frm; =20 target_ulong badaddr; - uint32_t bins; + target_ulong bins; =20 target_ulong guest_phys_fault_addr; =20 diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index d83579accf..bba4fce777 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -1371,6 +1371,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) tval =3D env->badaddr; break; case RISCV_EXCP_ILLEGAL_INST: + case RISCV_EXCP_VIRT_INSTRUCTION_FAULT: tval =3D env->bins; break; default: diff --git a/target/riscv/translate.c b/target/riscv/translate.c index 0cd1d9ee94..55a4713af2 100644 --- a/target/riscv/translate.c +++ b/target/riscv/translate.c @@ -107,6 +107,8 @@ typedef struct DisasContext { /* PointerMasking extension */ bool pm_mask_enabled; bool pm_base_enabled; + /* TCG of the current insn_start */ + TCGOp *insn_start; } DisasContext; =20 static inline bool has_ext(DisasContext *ctx, uint32_t ext) @@ -236,9 +238,6 @@ static void generate_exception_mtval(DisasContext *ctx,= int excp) =20 static void gen_exception_illegal(DisasContext *ctx) { - tcg_gen_st_i32(tcg_constant_i32(ctx->opcode), cpu_env, - offsetof(CPURISCVState, bins)); - generate_exception(ctx, RISCV_EXCP_ILLEGAL_INST); } =20 @@ -1017,6 +1016,13 @@ static uint32_t opcode_at(DisasContextBase *dcbase, = target_ulong pc) /* Include decoders for factored-out extensions */ #include "decode-XVentanaCondOps.c.inc" =20 +static inline void decode_save_opc(DisasContext *ctx, target_ulong opc) +{ + assert(ctx->insn_start !=3D NULL); + tcg_set_insn_start_param(ctx->insn_start, 1, opc); + ctx->insn_start =3D NULL; +} + static void decode_opc(CPURISCVState *env, DisasContext *ctx, uint16_t opc= ode) { /* @@ -1033,6 +1039,7 @@ static void decode_opc(CPURISCVState *env, DisasConte= xt *ctx, uint16_t opcode) =20 /* Check for compressed insn */ if (extract16(opcode, 0, 2) !=3D 3) { + decode_save_opc(ctx, opcode); if (!has_ext(ctx, RVC)) { gen_exception_illegal(ctx); } else { @@ -1047,6 +1054,7 @@ static void decode_opc(CPURISCVState *env, DisasConte= xt *ctx, uint16_t opcode) opcode32 =3D deposit32(opcode32, 16, 16, translator_lduw(env, &ctx->base, ctx->base.pc_next + 2)); + decode_save_opc(ctx, opcode32); ctx->opcode =3D opcode32; ctx->pc_succ_insn =3D ctx->base.pc_next + 4; =20 @@ -1113,7 +1121,8 @@ static void riscv_tr_insn_start(DisasContextBase *dcb= ase, CPUState *cpu) { DisasContext *ctx =3D container_of(dcbase, DisasContext, base); =20 - tcg_gen_insn_start(ctx->base.pc_next); + tcg_gen_insn_start(ctx->base.pc_next, 0); + ctx->insn_start =3D tcg_last_op(); } =20 static void riscv_tr_translate_insn(DisasContextBase *dcbase, CPUState *cp= u) --=20 2.34.1 From nobody Mon May 6 09:02:51 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=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 1651203632602353.5355581176601; Thu, 28 Apr 2022 20:40:32 -0700 (PDT) Received: from localhost ([::1]:42232 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nkHUd-0000Uv-Iw for importer2@patchew.org; Thu, 28 Apr 2022 23:40:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47480) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nkHPD-0002hw-IM for qemu-devel@nongnu.org; Thu, 28 Apr 2022 23:34:55 -0400 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]:33402) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nkHPA-0002TK-WB for qemu-devel@nongnu.org; Thu, 28 Apr 2022 23:34:55 -0400 Received: by mail-pf1-x435.google.com with SMTP id p12so5922524pfn.0 for ; Thu, 28 Apr 2022 20:34:52 -0700 (PDT) Received: from anup-ubuntu64-vm.. ([122.167.97.237]) by smtp.gmail.com with ESMTPSA id l22-20020a17090a071600b001d9781de67fsm8195568pjl.31.2022.04.28.20.34.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 20:34:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=n4A8g0BH9AOgRoC6O01FAkejCKJwc/Qkn01LZX/qlkc=; b=XBUaaXDv21E/hxTWIHEbLMSVRHy42kCNcqvQBr7tNHyTa2IHYEFzm2D/QZ/ok6+j+8 k//c3qcW8f0OzIz0x+Z0JTAWTqyHB+1SndVQh+fh483ESiJ8uumd92zqbHqXjjebpoEJ BmCtM+S3q01zZag8KcCX+/NxxxBcMAD7HrsND0SQDuGqSyKFQSgLD359IyTzp1boyDWt gPtxiIT3sXP41IyxMYiKYBzXNn+ccwixk0yAUmdgaE3GbbPxugkXa4MhLxOc08oaTIK2 P2bUp74Txwoo7xhcSUt76VDf1n5AySsJq1Tuz11UjDfJfaoOCfMyLopXVdqXvL3Z5JFu YK9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=n4A8g0BH9AOgRoC6O01FAkejCKJwc/Qkn01LZX/qlkc=; b=1FJKwdaeizTrQhLV2cZQMuA1c/PY3WDhriI3exRXsv1nSoSXhhHeMg2PLaIUkNPgZt 5dkWvwqXzPyk+sQn0DNt042C4bPC/MJZB71KiMCqtRdr17jVARM8r5HOzSoK9/iWOBJL rkaiHKiXoY10lCBtzQRHLqtp4vQ7nRYGzOnLchRqvnuppUEIC9DZKBP1qF5M+gYsugal X7pguvvd1lIE63st/60Dx2e9SosMPsCiTiDWwr8Ycc5p4tJAUZiniBSEvEI1K8ugrGh2 UH44Gr+1WSiuuY6+RecrEZfvJX82AmnrdkKnytsikN44ZXgzVlOYNmFv9Thauo672Xjx gtQg== X-Gm-Message-State: AOAM53269snxg7jRPgg46HOSpbTj/gmVYdFNqVpnwrg6+XVFVS8aYos3 m9fOx/tjcMjgQTHNwnkSIptAPw== X-Google-Smtp-Source: ABdhPJxeEUVy5GOgDOGeKwiyUNEEIqXXuM+GvpoWSvgOcr3XLqKKEfM5DD9YaIRfqPCiPJGNVBigRA== X-Received: by 2002:a05:6a00:a8b:b0:4cd:6030:4df3 with SMTP id b11-20020a056a000a8b00b004cd60304df3mr38144037pfl.40.1651203291648; Thu, 28 Apr 2022 20:34:51 -0700 (PDT) From: Anup Patel To: Peter Maydell , Palmer Dabbelt , Alistair Francis , Sagar Karandikar Subject: [PATCH 4/4] target/riscv: Update [m|h]tinst CSR in riscv_cpu_do_interrupt() Date: Fri, 29 Apr 2022 09:04:09 +0530 Message-Id: <20220429033409.258707-5-apatel@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220429033409.258707-1-apatel@ventanamicro.com> References: <20220429033409.258707-1-apatel@ventanamicro.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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::435; envelope-from=apatel@ventanamicro.com; helo=mail-pf1-x435.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, 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: , Cc: Anup Patel , Anup Patel , qemu-riscv@nongnu.org, qemu-devel@nongnu.org, Atish Patra Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1651203634037100001 Content-Type: text/plain; charset="utf-8" We should write transformed instruction encoding of the trapped instruction in [m|h]tinst CSR at time of taking trap as defined by the RISC-V privileged specification v1.12. Signed-off-by: Anup Patel --- target/riscv/cpu_helper.c | 168 +++++++++++++++++++++++++++++++++++++- target/riscv/instmap.h | 41 ++++++++++ 2 files changed, 205 insertions(+), 4 deletions(-) diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index bba4fce777..58f3b2effb 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -22,6 +22,7 @@ #include "qemu/main-loop.h" #include "cpu.h" #include "exec/exec-all.h" +#include "instmap.h" #include "tcg/tcg-op.h" #include "trace.h" #include "semihosting/common-semi.h" @@ -1318,6 +1319,158 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address= , int size, } #endif /* !CONFIG_USER_ONLY */ =20 +static target_ulong riscv_transformed_insn(CPURISCVState *env, + target_ulong insn) +{ + target_ulong xinsn =3D 0; + + /* + * Only Quadrant 0 and Quadrant 2 of RVC instruction space need to + * be uncompressed. The Quadrant 1 of RVC instruction space need + * not be transformed because these instructions won't generate + * any load/store trap. + */ + + if ((insn & 0x3) !=3D 0x3) { + /* Transform 16bit instruction into 32bit instruction */ + switch (GET_C_OP(insn)) { + case OPC_RISC_C_OP_QUAD0: /* Quadrant 0 */ + switch (GET_C_FUNC(insn)) { + case OPC_RISC_C_FUNC_FLD_LQ: + if (riscv_cpu_xlen(env) !=3D 128) { /* C.FLD (RV32/64) */ + xinsn =3D OPC_RISC_FLD; + xinsn =3D SET_RD(xinsn, GET_C_RS2S(insn)); + xinsn =3D SET_RS1(xinsn, GET_C_RS1S(insn)); + xinsn =3D SET_I_IMM(xinsn, GET_C_LD_IMM(insn)); + } + break; + case OPC_RISC_C_FUNC_LW: /* C.LW */ + xinsn =3D OPC_RISC_LW; + xinsn =3D SET_RD(xinsn, GET_C_RS2S(insn)); + xinsn =3D SET_RS1(xinsn, GET_C_RS1S(insn)); + xinsn =3D SET_I_IMM(xinsn, GET_C_LW_IMM(insn)); + break; + case OPC_RISC_C_FUNC_FLW_LD: + if (riscv_cpu_xlen(env) =3D=3D 32) { /* C.FLW (RV32) */ + xinsn =3D OPC_RISC_FLW; + xinsn =3D SET_RD(xinsn, GET_C_RS2S(insn)); + xinsn =3D SET_RS1(xinsn, GET_C_RS1S(insn)); + xinsn =3D SET_I_IMM(xinsn, GET_C_LW_IMM(insn)); + } else { /* C.LD (RV64/RV128) */ + xinsn =3D OPC_RISC_LD; + xinsn =3D SET_RD(xinsn, GET_C_RS2S(insn)); + xinsn =3D SET_RS1(xinsn, GET_C_RS1S(insn)); + xinsn =3D SET_I_IMM(xinsn, GET_C_LD_IMM(insn)); + } + break; + case OPC_RISC_C_FUNC_FSD_SQ: + if (riscv_cpu_xlen(env) !=3D 128) { /* C.FSD (RV32/64) */ + xinsn =3D OPC_RISC_FSD; + xinsn =3D SET_RS2(xinsn, GET_C_RS2S(insn)); + xinsn =3D SET_RS1(xinsn, GET_C_RS1S(insn)); + xinsn =3D SET_S_IMM(xinsn, GET_C_SD_IMM(insn)); + } + break; + case OPC_RISC_C_FUNC_SW: /* C.SW */ + xinsn =3D OPC_RISC_SW; + xinsn =3D SET_RS2(xinsn, GET_C_RS2S(insn)); + xinsn =3D SET_RS1(xinsn, GET_C_RS1S(insn)); + xinsn =3D SET_S_IMM(xinsn, GET_C_SW_IMM(insn)); + break; + case OPC_RISC_C_FUNC_FSW_SD: + if (riscv_cpu_xlen(env) =3D=3D 32) { /* C.FSW (RV32) */ + xinsn =3D OPC_RISC_FSW; + xinsn =3D SET_RS2(xinsn, GET_C_RS2S(insn)); + xinsn =3D SET_RS1(xinsn, GET_C_RS1S(insn)); + xinsn =3D SET_S_IMM(xinsn, GET_C_SW_IMM(insn)); + } else { /* C.SD (RV64/RV128) */ + xinsn =3D OPC_RISC_SD; + xinsn =3D SET_RS2(xinsn, GET_C_RS2S(insn)); + xinsn =3D SET_RS1(xinsn, GET_C_RS1S(insn)); + xinsn =3D SET_S_IMM(xinsn, GET_C_SD_IMM(insn)); + } + break; + default: + break; + } + break; + case OPC_RISC_C_OP_QUAD2: /* Quadrant 2 */ + switch (GET_C_FUNC(insn)) { + case OPC_RISC_C_FUNC_FLDSP_LQSP: + if (riscv_cpu_xlen(env) !=3D 128) { /* C.FLDSP (RV32/64) */ + xinsn =3D OPC_RISC_FLD; + xinsn =3D SET_RD(xinsn, GET_C_RD(insn)); + xinsn =3D SET_RS1(xinsn, 2); + xinsn =3D SET_I_IMM(xinsn, GET_C_LDSP_IMM(insn)); + } + break; + case OPC_RISC_C_FUNC_LWSP: /* C.LWSP */ + xinsn =3D OPC_RISC_LW; + xinsn =3D SET_RD(xinsn, GET_C_RD(insn)); + xinsn =3D SET_RS1(xinsn, 2); + xinsn =3D SET_I_IMM(xinsn, GET_C_LWSP_IMM(insn)); + break; + case OPC_RISC_C_FUNC_FLWSP_LDSP: + if (riscv_cpu_xlen(env) =3D=3D 32) { /* C.FLWSP (RV32) */ + xinsn =3D OPC_RISC_FLW; + xinsn =3D SET_RD(xinsn, GET_C_RD(insn)); + xinsn =3D SET_RS1(xinsn, 2); + xinsn =3D SET_I_IMM(xinsn, GET_C_LWSP_IMM(insn)); + } else { /* C.LDSP (RV64/RV128) */ + xinsn =3D OPC_RISC_LD; + xinsn =3D SET_RD(xinsn, GET_C_RD(insn)); + xinsn =3D SET_RS1(xinsn, 2); + xinsn =3D SET_I_IMM(xinsn, GET_C_LDSP_IMM(insn)); + } + break; + case OPC_RISC_C_FUNC_FSDSP_SQSP: + if (riscv_cpu_xlen(env) !=3D 128) { /* C.FSDSP (RV32/64) */ + xinsn =3D OPC_RISC_FSD; + xinsn =3D SET_RS2(xinsn, GET_C_RS2(insn)); + xinsn =3D SET_RS1(xinsn, 2); + xinsn =3D SET_S_IMM(xinsn, GET_C_SDSP_IMM(insn)); + } + break; + case OPC_RISC_C_FUNC_SWSP: /* C.SWSP */ + xinsn =3D OPC_RISC_SW; + xinsn =3D SET_RS2(xinsn, GET_C_RS2(insn)); + xinsn =3D SET_RS1(xinsn, 2); + xinsn =3D SET_S_IMM(xinsn, GET_C_SWSP_IMM(insn)); + break; + case 7: + if (riscv_cpu_xlen(env) =3D=3D 32) { /* C.FSWSP (RV32) */ + xinsn =3D OPC_RISC_FSW; + xinsn =3D SET_RS2(xinsn, GET_C_RS2(insn)); + xinsn =3D SET_RS1(xinsn, 2); + xinsn =3D SET_S_IMM(xinsn, GET_C_SWSP_IMM(insn)); + } else { /* C.SDSP (RV64/RV128) */ + xinsn =3D OPC_RISC_SD; + xinsn =3D SET_RS2(xinsn, GET_C_RS2(insn)); + xinsn =3D SET_RS1(xinsn, 2); + xinsn =3D SET_S_IMM(xinsn, GET_C_SDSP_IMM(insn)); + } + break; + default: + break; + } + break; + default: + break; + } + + /* + * Clear Bit1 of transformed instruction to indicate that + * original insruction was a 16bit instruction + */ + xinsn &=3D ~((target_ulong)0x2); + } else { + /* No need to transform 32bit (or wider) instructions */ + xinsn =3D insn; + } + + return xinsn; +} + /* * Handle Traps * @@ -1340,6 +1493,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) target_ulong cause =3D cs->exception_index & RISCV_EXCP_INT_MASK; uint64_t deleg =3D async ? env->mideleg : env->medeleg; target_ulong tval =3D 0; + target_ulong tinst =3D 0; target_ulong htval =3D 0; target_ulong mtval2 =3D 0; =20 @@ -1355,18 +1509,22 @@ void riscv_cpu_do_interrupt(CPUState *cs) if (!async) { /* set tval to badaddr for traps with address information */ switch (cause) { - case RISCV_EXCP_INST_GUEST_PAGE_FAULT: case RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT: case RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAULT: - case RISCV_EXCP_INST_ADDR_MIS: - case RISCV_EXCP_INST_ACCESS_FAULT: case RISCV_EXCP_LOAD_ADDR_MIS: case RISCV_EXCP_STORE_AMO_ADDR_MIS: case RISCV_EXCP_LOAD_ACCESS_FAULT: case RISCV_EXCP_STORE_AMO_ACCESS_FAULT: - case RISCV_EXCP_INST_PAGE_FAULT: case RISCV_EXCP_LOAD_PAGE_FAULT: case RISCV_EXCP_STORE_PAGE_FAULT: + write_gva =3D true; + tval =3D env->badaddr; + tinst =3D riscv_transformed_insn(env, env->bins); + break; + case RISCV_EXCP_INST_GUEST_PAGE_FAULT: + case RISCV_EXCP_INST_ADDR_MIS: + case RISCV_EXCP_INST_ACCESS_FAULT: + case RISCV_EXCP_INST_PAGE_FAULT: write_gva =3D true; tval =3D env->badaddr; break; @@ -1448,6 +1606,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) env->sepc =3D env->pc; env->stval =3D tval; env->htval =3D htval; + env->htinst =3D tinst; env->pc =3D (env->stvec >> 2 << 2) + ((async && (env->stvec & 3) =3D=3D 1) ? cause * 4 : 0); riscv_cpu_set_mode(env, PRV_S); @@ -1478,6 +1637,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) env->mepc =3D env->pc; env->mtval =3D tval; env->mtval2 =3D mtval2; + env->mtinst =3D tinst; env->pc =3D (env->mtvec >> 2 << 2) + ((async && (env->mtvec & 3) =3D=3D 1) ? cause * 4 : 0); riscv_cpu_set_mode(env, PRV_M); diff --git a/target/riscv/instmap.h b/target/riscv/instmap.h index 40b6d2b64d..f4ee686c78 100644 --- a/target/riscv/instmap.h +++ b/target/riscv/instmap.h @@ -316,6 +316,12 @@ enum { #define GET_RS2(inst) extract32(inst, 20, 5) #define GET_RD(inst) extract32(inst, 7, 5) #define GET_IMM(inst) sextract64(inst, 20, 12) +#define SET_RS1(inst, val) deposit32(inst, 15, 5, val) +#define SET_RS2(inst, val) deposit32(inst, 20, 5, val) +#define SET_RD(inst, val) deposit32(inst, 7, 5, val) +#define SET_I_IMM(inst, val) deposit32(inst, 20, 12, val) +#define SET_S_IMM(inst, val) \ + deposit32(deposit32(inst, 7, 5, val), 25, 7, (val) >> 5) =20 /* RVC decoding macros */ #define GET_C_IMM(inst) (extract32(inst, 2, 5) \ @@ -346,6 +352,8 @@ enum { | (extract32(inst, 5, 1) << 6)) #define GET_C_LD_IMM(inst) ((extract16(inst, 10, 3) << 3) \ | (extract16(inst, 5, 2) << 6)) +#define GET_C_SW_IMM(inst) GET_C_LW_IMM(inst) +#define GET_C_SD_IMM(inst) GET_C_LD_IMM(inst) #define GET_C_J_IMM(inst) ((extract32(inst, 3, 3) << 1) \ | (extract32(inst, 11, 1) << 4) \ | (extract32(inst, 2, 1) << 5) \ @@ -366,4 +374,37 @@ enum { #define GET_C_RS1S(inst) (8 + extract16(inst, 7, 3)) #define GET_C_RS2S(inst) (8 + extract16(inst, 2, 3)) =20 +#define GET_C_FUNC(inst) extract32(inst, 13, 3) +#define GET_C_OP(inst) extract32(inst, 0, 2) + +enum { + /* RVC Quadrants */ + OPC_RISC_C_OP_QUAD0 =3D 0x0, + OPC_RISC_C_OP_QUAD1 =3D 0x1, + OPC_RISC_C_OP_QUAD2 =3D 0x2 +}; + +enum { + /* RVC Quadrant 0 */ + OPC_RISC_C_FUNC_ADDI4SPN =3D 0x0, + OPC_RISC_C_FUNC_FLD_LQ =3D 0x1, + OPC_RISC_C_FUNC_LW =3D 0x2, + OPC_RISC_C_FUNC_FLW_LD =3D 0x3, + OPC_RISC_C_FUNC_FSD_SQ =3D 0x5, + OPC_RISC_C_FUNC_SW =3D 0x6, + OPC_RISC_C_FUNC_FSW_SD =3D 0x7 +}; + +enum { + /* RVC Quadrant 2 */ + OPC_RISC_C_FUNC_SLLI_SLLI64 =3D 0x0, + OPC_RISC_C_FUNC_FLDSP_LQSP =3D 0x1, + OPC_RISC_C_FUNC_LWSP =3D 0x2, + OPC_RISC_C_FUNC_FLWSP_LDSP =3D 0x3, + OPC_RISC_C_FUNC_JR_MV_EBREAK_JALR_ADD =3D 0x4, + OPC_RISC_C_FUNC_FSDSP_SQSP =3D 0x5, + OPC_RISC_C_FUNC_SWSP =3D 0x6, + OPC_RISC_C_FUNC_FSWSP_SDSP =3D 0x7 +}; + #endif --=20 2.34.1