From patchwork Fri Nov 22 03:22:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Chang X-Patchwork-Id: 13882740 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DAC7FE65D18 for ; Fri, 22 Nov 2024 03:24:27 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tEKFe-0005VL-Td; Thu, 21 Nov 2024 22:22:34 -0500 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 1tEKFc-0005Tb-IY for qemu-devel@nongnu.org; Thu, 21 Nov 2024 22:22:32 -0500 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tEKFb-0003wN-1S for qemu-devel@nongnu.org; Thu, 21 Nov 2024 22:22:32 -0500 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-212583bd467so16350105ad.3 for ; Thu, 21 Nov 2024 19:22:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1732245749; x=1732850549; 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=ete+ZJB8URbofv6Sb72yOCLqH3KqEk//Slye3l6+Ywk=; b=ReYY0V14yx7limW5OhkEIqMDkrcvla4NKGieLXG5C8GEphe3eTB0naP/L6HUfUuVFK HMPCrxu9vkeJd3QEgqN8Z4MzXrpweGd0EGCPvEHYnBVtCeCoNkwr+tbD3l6OIV7knyu1 8Ua/DT0RYocKCwhbOXKEQ9B5vlgV6Q+ysvpdypxM4FETP+OpEGr+E9mjc15zinI/LQUm PxcfRNxSi9I6pEFy/Cyge7NjfTTD0h15tOUoZ8a3uROuxK01nDWZseyjJoB/+uYk9eIs FTKXlm/NbqsCPe0GMEWHgg4/67dbE4QVrSGR7jIz65w3jK0Ni2Yi9vRpiTF+bavgN/4a +RVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732245749; x=1732850549; 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=ete+ZJB8URbofv6Sb72yOCLqH3KqEk//Slye3l6+Ywk=; b=JKkVs6063lVRDd/sBEqBVWN9WX9J3Xn0pTQNmiZ8/RFxvMH+N3R7KsdO0O4qbhw110 cPq2Zv7QueUtnheNDmEf74nB/2u61ptJ/Oj2tuuPtghF1tMdf6yCBTJjgk6PJYIkpxNp hPCwv3Z1yGHViUApdAJ5LcgTI3himPMY1gj4ItQujNqo4Lv/FggJ3f4QOqn20sp8poA/ 9NpFNegGrPf16w9FRmLRx8ExC36JgXb5yn5gJ6pJnQAkSUvzK8EAr693suFnjuKzI81p t692hX1W0Nf6rUjSfPIf4zB2zovvhjyzNAJLsVVZjSj+bzzVAZvNPQbkUMUi5HYw5c1W lHhQ== X-Gm-Message-State: AOJu0YwjefhUikaLaf+brjcxulhErARNthfKoJf4Ma7T53pwEtiDtqfJ MRAzKpH1K9kq3Asd4bpxtqA4W5614CyYXVSjzupCR3/UfwVTGK6t+qzlnCTSTjT4M7d8imuqjGl HigBPm/X9BasyuA/jS2z0lJvArb6IgpL7zbYsVRbompvrQA6HGhFfXov3Cp3EbI/Ya/LFgsoCnf h+E0fpAac2mnpXviXANQnW8tsoSNVGR/a7lDog1iYBFJpZ X-Gm-Gg: ASbGnctt/vB5Lgm/AkRAxWShfx+1OZPnF0ReR54ATRu6Cap5wq9TpvPq/LLTLaGx8+n dxwt9CQkxFhNHUPBxn4hBCMXntSuvdfs14CAUDVYZlbi1HkEXk0cpAS92jCpx5Jg7/aUPcLtCZJ nqrV4+vGYooWFCAbgCMx3XhBQi5cyhWv4we9HzosVm3fniFFQx8+GJ9HMf8StcerHrt8m9LQcYi Vi2jUnLboqKEOmcF+a4Uug8v82VGEueZO8YFDkYa0jYJAL47vTJ7cjwyzDxp8eNLu4yvKOTyAxv shx7tA== X-Google-Smtp-Source: AGHT+IHBEVnFrbzMXecQHh/nZ+GmGIm98FgEFEm1Vd5Gh6Yy+xQdDvnKgQD8iOmlfoA0OndNvUSyYQ== X-Received: by 2002:a17:902:e742:b0:212:4e37:204b with SMTP id d9443c01a7336-2129fd74184mr13294575ad.51.1732245748842; Thu, 21 Nov 2024 19:22:28 -0800 (PST) Received: from fchang-1826.internal.sifive.com ([136.226.240.187]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2129dc157f3sm5506905ad.203.2024.11.21.19.22.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Nov 2024 19:22:28 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Tommy Wu , Frank Chang , Alistair Francis Subject: [PATCH v9 1/6] target/riscv: Add 'ext_smrnmi' in the RISCVCPUConfig Date: Fri, 22 Nov 2024 11:22:12 +0800 Message-Id: <20241122032217.3816540-2-frank.chang@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241122032217.3816540-1-frank.chang@sifive.com> References: <20241122032217.3816540-1-frank.chang@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=frank.chang@sifive.com; helo=mail-pl1-x62c.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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Tommy Wu The boolean variable 'ext_smrnmi' is used to determine whether the Smrnmi extension exists. Signed-off-by: Frank Chang Signed-off-by: Tommy Wu Reviewed-by: Alistair Francis --- target/riscv/cpu_cfg.h | 1 + 1 file changed, 1 insertion(+) diff --git a/target/riscv/cpu_cfg.h b/target/riscv/cpu_cfg.h index 59d6fc445d..321f64fd72 100644 --- a/target/riscv/cpu_cfg.h +++ b/target/riscv/cpu_cfg.h @@ -128,6 +128,7 @@ struct RISCVCPUConfig { bool ext_ssaia; bool ext_sscofpmf; bool ext_smepmp; + bool ext_smrnmi; bool rvv_ta_all_1s; bool rvv_ma_all_1s; bool rvv_vl_half_avl; From patchwork Fri Nov 22 03:22:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Chang X-Patchwork-Id: 13882738 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C679FE65D18 for ; Fri, 22 Nov 2024 03:23:25 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tEKFi-0005WN-FX; Thu, 21 Nov 2024 22:22:38 -0500 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 1tEKFf-0005Vy-To for qemu-devel@nongnu.org; Thu, 21 Nov 2024 22:22:35 -0500 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tEKFd-0003wm-Sv for qemu-devel@nongnu.org; Thu, 21 Nov 2024 22:22:35 -0500 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-720c286bcd6so1496133b3a.3 for ; Thu, 21 Nov 2024 19:22:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1732245751; x=1732850551; 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=F5dz25xLBEpscPUFsdwI6ZdGbDCjYWaBqQIsk/OC0sI=; b=YCPOTccKG4fCh5DtoSzE3vHMtnSw4cNd1mBioygJBq6QCMlbvQsvCDHo60JAZJABmx kdJwC8UjuAuh2MyU7UKmKy7xUBaNcQetsgGYifmPHrEwXDpQQaObNh/IZtllr7uWh99n MDQW/2/yMOcoB5ZDJesdmb4tIPQNs+9S0D04blu6s0rogSZtu0rtD+RYpKKxqt0oFG9r YeLC3D8rB75SJj1Bb5MirzHdIyKk+EWH3QPfiwgu2Lpez4XXWgrDNGoM0SnRKHDGxEAg YjENB1z+r6y7rWS7cdLzGMrfsMxr63bt4BMPGFhBqRMfLvjXqAsx357FALRzjnm383QJ h/4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732245751; x=1732850551; 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=F5dz25xLBEpscPUFsdwI6ZdGbDCjYWaBqQIsk/OC0sI=; b=X5MEMPAx8FXRzyqpVN/SlP5NdomTWB38S3KXGaEPpYE1qeKHZdgJJxeMjhFiPCN8rY E4kS9KC5el9+WXDXleFx2AjaD8Ls3gUVKnW9pQUR3rka0cr0Hm0XBcVW3K5U++abfQ4y n5MiYbn6Dm5tPoj2HomAFRwvvQu7miYgLj+yRxwNJsxVps7a6IMOCkxXfwTVKCZEo7yi EOjtbaTf+WSlITyTw5KjKF8fZUILX5IB4ej9EsS+eW1UofuYjmlV5NVygFxWxIcYmCSf bVC7gNJgAeahjlfySPlYEFBRVJuokLNO7aOWavPGUBLj3n8zupXUnDVWkNAlvsIOJy1Q Ukhw== X-Gm-Message-State: AOJu0Yzaoq78KHaUCMVykYf7qbJyRc7Pk+6hKpJhg0LlAXTJnDctNjbw dim6FOxfWPUWDCHOVYXdB7YWp8kmQJzb+BQnKJ7twtgTxeTm7AizfsCziEDCwrEbILcrki3Pyct fGCQZN7d9RPcCu2p7mDlk0Oa081pJqU4VvoavNM3i72bUrU36gQizJKNj6iWdNxkw6pfwoDaHdJ iY0/6puhaqMHJF79sK6iRhGluBrL3iJve8q3oUDLDlbIfn X-Gm-Gg: ASbGncu2EEhWhSqbkLMPrTHw2eetzaN/W/GglLNQ+JhWPlcryPyolMrT91DtoaHzkLN /eGJ+EIg/hGfNAJSEh/bToJSzdekdl/EFj+SNQzOqL2kMuiWGOj7BAOrcfGpd4s8X+yjXcXIbms 4l4QSPOxw6re6+nvSXz+QT6qS99b5ACaLcwDa6J8kYB+sV1ykzM+VSke1IeufC6hIIgtFHM+e0F /iX9vaiCwJp3hbn/QGHVk1GXqQhIpLaiUBDInqqmLUpKYBl2KRny6hsl/8W6TQWNTwu27bK7XwB GhTL5w== X-Google-Smtp-Source: AGHT+IFdtxnVWwp1P7K81MudpUg34Yz++DiRVsHh96tjlIQYkHhO9QY3DDf08Er6V00Coz7P8yHRWA== X-Received: by 2002:a17:902:e54f:b0:212:3f36:d985 with SMTP id d9443c01a7336-2129f82ea85mr17392975ad.53.1732245751038; Thu, 21 Nov 2024 19:22:31 -0800 (PST) Received: from fchang-1826.internal.sifive.com ([136.226.240.187]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2129dc157f3sm5506905ad.203.2024.11.21.19.22.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Nov 2024 19:22:30 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Tommy Wu , Frank Chang , Alistair Francis Subject: [PATCH v9 2/6] target/riscv: Add Smrnmi CSRs Date: Fri, 22 Nov 2024 11:22:13 +0800 Message-Id: <20241122032217.3816540-3-frank.chang@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241122032217.3816540-1-frank.chang@sifive.com> References: <20241122032217.3816540-1-frank.chang@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::430; envelope-from=frank.chang@sifive.com; helo=mail-pf1-x430.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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Tommy Wu The Smrnmi extension adds the 'mnscratch', 'mnepc', 'mncause', 'mnstatus' CSRs. Signed-off-by: Frank Chang Signed-off-by: Tommy Wu Reviewed-by: Alistair Francis --- target/riscv/cpu.c | 5 +++ target/riscv/cpu.h | 7 ++++ target/riscv/cpu_bits.h | 11 ++++++ target/riscv/csr.c | 82 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index f219f0c3b5..c404828ca0 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -1029,6 +1029,11 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) riscv_trigger_reset_hold(env); } + if (cpu->cfg.ext_smrnmi) { + env->rnmip = 0; + env->mnstatus = set_field(env->mnstatus, MNSTATUS_NMIE, false); + } + if (kvm_enabled()) { kvm_riscv_reset_vcpu(cpu); } diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 284b112821..a2cb471b3c 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -486,6 +486,13 @@ struct CPUArchState { uint64_t kvm_timer_state; uint64_t kvm_timer_frequency; #endif /* CONFIG_KVM */ + + /* RNMI */ + target_ulong mnscratch; + target_ulong mnepc; + target_ulong mncause; /* mncause without bit XLEN-1 set to 1 */ + target_ulong mnstatus; + target_ulong rnmip; }; /* diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index 385a2c67c2..e69cf4f394 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -353,6 +353,12 @@ #define CSR_PMPADDR14 0x3be #define CSR_PMPADDR15 0x3bf +/* RNMI */ +#define CSR_MNSCRATCH 0x740 +#define CSR_MNEPC 0x741 +#define CSR_MNCAUSE 0x742 +#define CSR_MNSTATUS 0x744 + /* Debug/Trace Registers (shared with Debug Mode) */ #define CSR_TSELECT 0x7a0 #define CSR_TDATA1 0x7a1 @@ -633,6 +639,11 @@ typedef enum { #define SATP64_ASID 0x0FFFF00000000000ULL #define SATP64_PPN 0x00000FFFFFFFFFFFULL +/* RNMI mnstatus CSR mask */ +#define MNSTATUS_NMIE 0x00000008 +#define MNSTATUS_MNPV 0x00000080 +#define MNSTATUS_MNPP 0x00001800 + /* VM modes (satp.mode) privileged ISA 1.10 */ #define VM_1_10_MBARE 0 #define VM_1_10_SV32 1 diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 9846770820..5d8d0d7514 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -597,6 +597,17 @@ static RISCVException debug(CPURISCVState *env, int csrno) return RISCV_EXCP_ILLEGAL_INST; } + +static RISCVException rnmi(CPURISCVState *env, int csrno) +{ + RISCVCPU *cpu = env_archcpu(env); + + if (cpu->cfg.ext_smrnmi) { + return RISCV_EXCP_NONE; + } + + return RISCV_EXCP_ILLEGAL_INST; +} #endif static RISCVException seed(CPURISCVState *env, int csrno) @@ -4647,6 +4658,67 @@ static RISCVException write_upmbase(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } +static RISCVException read_mnscratch(CPURISCVState *env, int csrno, + target_ulong *val) +{ + *val = env->mnscratch; + return RISCV_EXCP_NONE; +} + +static int write_mnscratch(CPURISCVState *env, int csrno, target_ulong val) +{ + env->mnscratch = val; + return RISCV_EXCP_NONE; +} + +static int read_mnepc(CPURISCVState *env, int csrno, target_ulong *val) +{ + *val = env->mnepc; + return RISCV_EXCP_NONE; +} + +static int write_mnepc(CPURISCVState *env, int csrno, target_ulong val) +{ + env->mnepc = val; + return RISCV_EXCP_NONE; +} + +static int read_mncause(CPURISCVState *env, int csrno, target_ulong *val) +{ + *val = env->mncause; + return RISCV_EXCP_NONE; +} + +static int write_mncause(CPURISCVState *env, int csrno, target_ulong val) +{ + env->mncause = val; + return RISCV_EXCP_NONE; +} + +static int read_mnstatus(CPURISCVState *env, int csrno, target_ulong *val) +{ + *val = env->mnstatus; + return RISCV_EXCP_NONE; +} + +static int write_mnstatus(CPURISCVState *env, int csrno, target_ulong val) +{ + target_ulong mask = (MNSTATUS_NMIE | MNSTATUS_MNPP); + + if (riscv_has_ext(env, RVH)) { + /* Flush tlb on mnstatus fields that affect VM. */ + if ((val ^ env->mnstatus) & MNSTATUS_MNPV) { + tlb_flush(env_cpu(env)); + } + + mask |= MNSTATUS_MNPV; + } + + /* mnstatus.mnie can only be cleared by hardware. */ + env->mnstatus = (env->mnstatus & MNSTATUS_NMIE) | (val & mask); + return RISCV_EXCP_NONE; +} + #endif /* Crypto Extension */ @@ -5154,6 +5226,16 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { write_sstateen_1_3, .min_priv_ver = PRIV_VERSION_1_12_0 }, + /* RNMI */ + [CSR_MNSCRATCH] = { "mnscratch", rnmi, read_mnscratch, write_mnscratch, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_MNEPC] = { "mnepc", rnmi, read_mnepc, write_mnepc, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_MNCAUSE] = { "mncause", rnmi, read_mncause, write_mncause, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_MNSTATUS] = { "mnstatus", rnmi, read_mnstatus, write_mnstatus, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + /* Supervisor Trap Setup */ [CSR_SSTATUS] = { "sstatus", smode, read_sstatus, write_sstatus, NULL, read_sstatus_i128 }, From patchwork Fri Nov 22 03:22:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Frank Chang X-Patchwork-Id: 13882737 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9825AE65D18 for ; Fri, 22 Nov 2024 03:23:14 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tEKFm-0005Y4-1S; Thu, 21 Nov 2024 22:22:42 -0500 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 1tEKFh-0005WE-Jm for qemu-devel@nongnu.org; Thu, 21 Nov 2024 22:22:37 -0500 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tEKFf-0003xH-55 for qemu-devel@nongnu.org; Thu, 21 Nov 2024 22:22:37 -0500 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-2124ccf03edso16279275ad.2 for ; Thu, 21 Nov 2024 19:22:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1732245753; x=1732850553; 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=tOE6OxXP+uljt1Ysl2Pdar5s462OP1Rc9jWDEs1d3A0=; b=jpuNdPnGfvmpWgfVM1/LJHC/1n8CoosOgZeT/ZBd/vGGz1XSCrA32ckKWtDpvv2mnZ vH6Ry9c7WpeFIak5Z2qT8q7irJ59PF/Rnw6rotmSnbn3ixc4s06zAiVhERf1Ck7j5wuK 6fpbW78RD0ANYZsM2IefzTDnyBIAhzgnnqS4XgedsilOCq6i16Znqmc251h+XqfhYtZC fiUm02oyRTI1094yOPXIJI6Y9zvlq8Yw3EuoBncKyOKkSclrWHmF+0N/M0tJp+yJuKEC 0k6GG6ErY9Z6jEugf6vo9RI1FMKIlj4jrNdMFMtwFda8a5qXmlVD8Z0nv+54+B+8Gguu efDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732245753; x=1732850553; 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=tOE6OxXP+uljt1Ysl2Pdar5s462OP1Rc9jWDEs1d3A0=; b=c9cfGik8x5htb/11LgGhzhaOiwPSzoUY+1nNrGq9Di0ig4qY83fICXTSDX53VTV5WN EMHl+ASKVPR/Ty4WIgXoQ0hVt6FuSqhUm11CG00mfJq7wIROxfgnm7maojjiGBxp7nl0 ePWxjGIJk4VTFCtmwXocTEBHdQ+V2Z2Slpl8UfgYall1M9eR/jTvKyCaBV+qLfaPiTfl kLCZMKI3VRtcbX0uk2jJwAKC7cLD7qdjmYlyPcPwLPGjwmXDlBZNg4vL6hZ+7gZGOL3S 72b+6jDIl0MOHy2GaVzUYsPgAeqzC2NLN/Rdd3M3TZpzuzfmFFtTavUA99PH6ueYja/N sIpw== X-Gm-Message-State: AOJu0Yy1v8gveF5jwdMTWqUGo1sgozckee8Rqy0EeE3g0S7nHcp0vvM1 jKEO7NQGw6Z03Z0sFC52DSgAl2/oiTTKQFckSM5A5YUUkTOGO7kKqY0v3uyfpC/75iDhgmbZjB/ zcjN8Vb5EHSI7tpLLAOxImr2kLn7ahbMxzWcPJ3Knr4EXWi9q6Nc+REUwkrAe/xl4gZqLFDmSex 4+q/M5Fh0a/03UEN2JHS/1KewetxxqWhZ4H8TZbtfn0SSZ X-Gm-Gg: ASbGnctTg18wuKfJ+wYiQjjyEYPs1ojH6Al6HCXVTT+r3HUDvC8+mqPaep0J/GcU4jO 94ItiLmLqA5PAwpa+qJgdGHdlHO/oXIGtVm49t0ZmszEXwBrVgjLesu8guPGMoM/xC440UTOB/b 9d/Yda53NEBI0iO0rL7mLEcJ2WZu32J1Bu+rOz9CWCHNNtZu/x5iH28An+O3nTYR3JUxxwrc7bV oL0dNnPSJtI3cnzbunhzf2pnm/WC9xoiW1Uxoz6mC9mUagt0h5iXLRoPoj4VzQgrjr5RH+Attj6 CpCsvA== X-Google-Smtp-Source: AGHT+IFYBsKOsuRe2evvWfoW6sHMFAChLDumAzn6n716HuTwXaH3H3NVum9dqdUsKIRf60iHz+voBg== X-Received: by 2002:a17:902:e54f:b0:212:3f36:d985 with SMTP id d9443c01a7336-2129f82ea85mr17394035ad.53.1732245752923; Thu, 21 Nov 2024 19:22:32 -0800 (PST) Received: from fchang-1826.internal.sifive.com ([136.226.240.187]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2129dc157f3sm5506905ad.203.2024.11.21.19.22.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Nov 2024 19:22:32 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Tommy Wu , Frank Chang Subject: [PATCH v9 3/6] target/riscv: Handle Smrnmi interrupt and exception Date: Fri, 22 Nov 2024 11:22:14 +0800 Message-Id: <20241122032217.3816540-4-frank.chang@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241122032217.3816540-1-frank.chang@sifive.com> References: <20241122032217.3816540-1-frank.chang@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62b; envelope-from=frank.chang@sifive.com; helo=mail-pl1-x62b.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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Tommy Wu Because the RNMI interrupt trap handler address is implementation defined. We add the 'rnmi-interrupt-vector' and 'rnmi-exception-vector' as the property of the harts. It’s very easy for users to set the address based on their expectation. This patch also adds the functionality to handle the RNMI signals. Signed-off-by: Frank Chang Signed-off-by: Tommy Wu --- hw/riscv/riscv_hart.c | 40 +++++++++++++++++ include/hw/riscv/riscv_hart.h | 4 ++ target/riscv/cpu.c | 11 +++++ target/riscv/cpu.h | 3 ++ target/riscv/cpu_bits.h | 12 +++++ target/riscv/cpu_helper.c | 85 ++++++++++++++++++++++++++++++++--- 6 files changed, 150 insertions(+), 5 deletions(-) diff --git a/hw/riscv/riscv_hart.c b/hw/riscv/riscv_hart.c index 613ea2aaa0..700e65fc57 100644 --- a/hw/riscv/riscv_hart.c +++ b/hw/riscv/riscv_hart.c @@ -26,6 +26,7 @@ #include "target/riscv/cpu.h" #include "hw/qdev-properties.h" #include "hw/riscv/riscv_hart.h" +#include "qemu/error-report.h" static Property riscv_harts_props[] = { DEFINE_PROP_UINT32("num-harts", RISCVHartArrayState, num_harts, 1), @@ -33,6 +34,22 @@ static Property riscv_harts_props[] = { DEFINE_PROP_STRING("cpu-type", RISCVHartArrayState, cpu_type), DEFINE_PROP_UINT64("resetvec", RISCVHartArrayState, resetvec, DEFAULT_RSTVEC), + /* + * Smrnmi implementation-defined interrupt and exception trap handlers. + * + * When an RNMI interrupt is detected, the hart then enters M-mode and + * jumps to the address defined by "rnmi-interrupt-vector". + * + * When the hart encounters an exception while executing in M-mode with + * the mnstatus.NMIE bit clear, the hart then jumps to the address + * defined by "rnmi-exception-vector". + */ + DEFINE_PROP_ARRAY("rnmi-interrupt-vector", RISCVHartArrayState, + num_rnmi_irqvec, rnmi_irqvec, qdev_prop_uint64, + uint64_t), + DEFINE_PROP_ARRAY("rnmi-exception-vector", RISCVHartArrayState, + num_rnmi_excpvec, rnmi_excpvec, qdev_prop_uint64, + uint64_t), DEFINE_PROP_END_OF_LIST(), }; @@ -47,6 +64,29 @@ static bool riscv_hart_realize(RISCVHartArrayState *s, int idx, { object_initialize_child(OBJECT(s), "harts[*]", &s->harts[idx], cpu_type); qdev_prop_set_uint64(DEVICE(&s->harts[idx]), "resetvec", s->resetvec); + + if (s->harts[idx].cfg.ext_smrnmi) { + if (idx < s->num_rnmi_irqvec) { + qdev_prop_set_uint64(DEVICE(&s->harts[idx]), + "rnmi-interrupt-vector", s->rnmi_irqvec[idx]); + } + + if (idx < s->num_rnmi_excpvec) { + qdev_prop_set_uint64(DEVICE(&s->harts[idx]), + "rnmi-exception-vector", s->rnmi_excpvec[idx]); + } + } else { + if (s->num_rnmi_irqvec > 0) { + warn_report_once("rnmi-interrupt-vector property is ignored " + "because Smrnmi extension is not enabled."); + } + + if (s->num_rnmi_excpvec > 0) { + warn_report_once("rnmi-exception-vector property is ignored " + "because Smrnmi extension is not enabled."); + } + } + s->harts[idx].env.mhartid = s->hartid_base + idx; qemu_register_reset(riscv_harts_cpu_reset, &s->harts[idx]); return qdev_realize(DEVICE(&s->harts[idx]), NULL, errp); diff --git a/include/hw/riscv/riscv_hart.h b/include/hw/riscv/riscv_hart.h index 912b4a2682..a6ed73a195 100644 --- a/include/hw/riscv/riscv_hart.h +++ b/include/hw/riscv/riscv_hart.h @@ -38,6 +38,10 @@ struct RISCVHartArrayState { uint32_t hartid_base; char *cpu_type; uint64_t resetvec; + uint32_t num_rnmi_irqvec; + uint64_t *rnmi_irqvec; + uint32_t num_rnmi_excpvec; + uint64_t *rnmi_excpvec; RISCVCPU *harts; }; diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index c404828ca0..b402d8545b 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -1314,6 +1314,11 @@ static void riscv_cpu_set_irq(void *opaque, int irq, int level) g_assert_not_reached(); } } + +static void riscv_cpu_set_nmi(void *opaque, int irq, int level) +{ + riscv_cpu_set_rnmi(RISCV_CPU(opaque), irq, level); +} #endif /* CONFIG_USER_ONLY */ static bool riscv_cpu_is_dynamic(Object *cpu_obj) @@ -1337,6 +1342,8 @@ static void riscv_cpu_init(Object *obj) #ifndef CONFIG_USER_ONLY qdev_init_gpio_in(DEVICE(obj), riscv_cpu_set_irq, IRQ_LOCAL_MAX + IRQ_LOCAL_GUEST_MAX); + qdev_init_gpio_in_named(DEVICE(cpu), riscv_cpu_set_nmi, + "riscv.cpu.rnmi", RNMI_MAX); #endif /* CONFIG_USER_ONLY */ general_user_opts = g_hash_table_new(g_str_hash, g_str_equal); @@ -2689,6 +2696,10 @@ static Property riscv_cpu_properties[] = { #ifndef CONFIG_USER_ONLY DEFINE_PROP_UINT64("resetvec", RISCVCPU, env.resetvec, DEFAULT_RSTVEC), + DEFINE_PROP_UINT64("rnmi-interrupt-vector", RISCVCPU, env.rnmi_irqvec, + DEFAULT_RNMI_IRQVEC), + DEFINE_PROP_UINT64("rnmi-exception-vector", RISCVCPU, env.rnmi_excpvec, + DEFAULT_RNMI_EXCPVEC), #endif DEFINE_PROP_BOOL("short-isa-string", RISCVCPU, cfg.short_isa_string, false), diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index a2cb471b3c..8dc5b4d002 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -493,6 +493,8 @@ struct CPUArchState { target_ulong mncause; /* mncause without bit XLEN-1 set to 1 */ target_ulong mnstatus; target_ulong rnmip; + uint64_t rnmi_irqvec; + uint64_t rnmi_excpvec; }; /* @@ -591,6 +593,7 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env); int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint64_t interrupts); uint64_t riscv_cpu_update_mip(CPURISCVState *env, uint64_t mask, uint64_t value); +void riscv_cpu_set_rnmi(RISCVCPU *cpu, uint32_t irq, bool level); void riscv_cpu_interrupt(CPURISCVState *env); #define BOOL_TO_MASK(x) (-!!(x)) /* helper for riscv_cpu_update_mip value */ void riscv_cpu_set_rdtime_fn(CPURISCVState *env, uint64_t (*fn)(void *), diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index e69cf4f394..01e279dc25 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -679,6 +679,12 @@ typedef enum { /* Default Reset Vector address */ #define DEFAULT_RSTVEC 0x1000 +/* Default RNMI Interrupt Vector address */ +#define DEFAULT_RNMI_IRQVEC 0x0 + +/* Default RNMI Exception Vector address */ +#define DEFAULT_RNMI_EXCPVEC 0x0 + /* Exception causes */ typedef enum RISCVException { RISCV_EXCP_NONE = -1, /* sentinel value */ @@ -733,6 +739,9 @@ typedef enum RISCVException { /* -1 is due to bit zero of hgeip and hgeie being ROZ. */ #define IRQ_LOCAL_GUEST_MAX (TARGET_LONG_BITS - 1) +/* RNMI causes */ +#define RNMI_MAX 16 + /* mip masks */ #define MIP_USIP (1 << IRQ_U_SOFT) #define MIP_SSIP (1 << IRQ_S_SOFT) @@ -970,6 +979,9 @@ typedef enum RISCVException { #define MHPMEVENT_IDX_MASK 0xFFFFF #define MHPMEVENT_SSCOF_RESVD 16 +/* RISC-V-specific interrupt pending bits. */ +#define CPU_INTERRUPT_RNMI CPU_INTERRUPT_TGT_EXT_0 + /* JVT CSR bits */ #define JVT_MODE 0x3F #define JVT_BASE (~0x3F) diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 0a3ead69ea..828b6a1003 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -505,6 +505,18 @@ static int riscv_cpu_local_irq_pending(CPURISCVState *env) uint64_t vsbits, irq_delegated; int virq; + /* Priority: RNMI > Other interrupt. */ + if (riscv_cpu_cfg(env)->ext_smrnmi) { + /* If mnstatus.NMIE == 0, all interrupts are disabled. */ + if (!get_field(env->mnstatus, MNSTATUS_NMIE)) { + return RISCV_EXCP_NONE; + } + + if (env->rnmip) { + return ctz64(env->rnmip); /* since non-zero */ + } + } + /* Determine interrupt enable state of all privilege modes */ if (env->virt_enabled) { mie = 1; @@ -567,7 +579,9 @@ static int riscv_cpu_local_irq_pending(CPURISCVState *env) bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request) { - if (interrupt_request & CPU_INTERRUPT_HARD) { + uint32_t mask = CPU_INTERRUPT_HARD | CPU_INTERRUPT_RNMI; + + if (interrupt_request & mask) { RISCVCPU *cpu = RISCV_CPU(cs); CPURISCVState *env = &cpu->env; int interruptno = riscv_cpu_local_irq_pending(env); @@ -699,6 +713,30 @@ void riscv_cpu_set_geilen(CPURISCVState *env, target_ulong geilen) env->geilen = geilen; } +void riscv_cpu_set_rnmi(RISCVCPU *cpu, uint32_t irq, bool level) +{ + CPURISCVState *env = &cpu->env; + CPUState *cs = CPU(cpu); + bool release_lock = false; + + if (!bql_locked()) { + release_lock = true; + bql_lock(); + } + + if (level) { + env->rnmip |= 1 << irq; + cpu_interrupt(cs, CPU_INTERRUPT_RNMI); + } else { + env->rnmip &= ~(1 << irq); + cpu_reset_interrupt(cs, CPU_INTERRUPT_RNMI); + } + + if (release_lock) { + bql_unlock(); + } +} + int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint64_t interrupts) { CPURISCVState *env = &cpu->env; @@ -1794,6 +1832,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) bool write_gva = false; bool always_storeamo = (env->excp_uw2 & RISCV_UW2_ALWAYS_STORE_AMO); uint64_t s; + int mode; /* * cs->exception is 32-bits wide unlike mcause which is XLEN-bits wide @@ -1812,6 +1851,23 @@ void riscv_cpu_do_interrupt(CPUState *cs) target_ulong mtval2 = 0; int sxlen = 0; int mxlen = 0; + bool nnmi_excep = false; + + if (cpu->cfg.ext_smrnmi && env->rnmip && async) { + env->mnstatus = set_field(env->mnstatus, MNSTATUS_NMIE, false); + env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPV, + env->virt_enabled); + env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPP, + env->priv); + env->mncause = cause | ((target_ulong)1U << (TARGET_LONG_BITS - 1)); + env->mnepc = env->pc; + env->pc = env->rnmi_irqvec; + + /* Trapping to M mode, virt is disabled */ + riscv_cpu_set_mode(env, PRV_M, false); + + return; + } if (!async) { /* set tval to badaddr for traps with address information */ @@ -1905,8 +1961,10 @@ void riscv_cpu_do_interrupt(CPUState *cs) __func__, env->mhartid, async, cause, env->pc, tval, riscv_cpu_get_trap_name(cause, async)); - if (env->priv <= PRV_S && cause < 64 && - (((deleg >> cause) & 1) || s_injected || vs_injected)) { + mode = env->priv <= PRV_S && cause < 64 && + (((deleg >> cause) & 1) || s_injected || vs_injected) ? PRV_S : PRV_M; + + if (mode == PRV_S) { /* handle the trap in S-mode */ /* save elp status */ if (cpu_get_fcfien(env)) { @@ -1961,6 +2019,14 @@ void riscv_cpu_do_interrupt(CPUState *cs) ((async && (env->stvec & 3) == 1) ? cause * 4 : 0); riscv_cpu_set_mode(env, PRV_S, virt); } else { + /* + * If the hart encounters an exception while executing in M-mode + * with the mnstatus.NMIE bit clear, the exception is an RNMI exception. + */ + nnmi_excep = cpu->cfg.ext_smrnmi && + !get_field(env->mnstatus, MNSTATUS_NMIE) && + !async; + /* handle the trap in M-mode */ /* save elp status */ if (cpu_get_fcfien(env)) { @@ -1994,8 +2060,17 @@ void riscv_cpu_do_interrupt(CPUState *cs) env->mtval = tval; env->mtval2 = mtval2; env->mtinst = tinst; - env->pc = (env->mtvec >> 2 << 2) + - ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0); + + /* + * For RNMI exception, program counter is set to the RNMI exception + * trap handler address. + */ + if (nnmi_excep) { + env->pc = env->rnmi_excpvec; + } else { + env->pc = (env->mtvec >> 2 << 2) + + ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0); + } riscv_cpu_set_mode(env, PRV_M, virt); } From patchwork Fri Nov 22 03:22:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Chang X-Patchwork-Id: 13882741 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8B995E65D18 for ; Fri, 22 Nov 2024 03:25:04 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tEKFq-0005Z3-7k; Thu, 21 Nov 2024 22:22:46 -0500 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 1tEKFj-0005XD-Ce for qemu-devel@nongnu.org; Thu, 21 Nov 2024 22:22:39 -0500 Received: from mail-pl1-x642.google.com ([2607:f8b0:4864:20::642]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tEKFh-0003xk-Il for qemu-devel@nongnu.org; Thu, 21 Nov 2024 22:22:39 -0500 Received: by mail-pl1-x642.google.com with SMTP id d9443c01a7336-2124ccf03edso16279515ad.2 for ; Thu, 21 Nov 2024 19:22:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1732245755; x=1732850555; 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=1YBszf0JEY6lGH98UF5Cbrpj+6ZfpD+LYDcXM9Rwtik=; b=XdEvbXZLO6TelrAqv+NEMhliUGqQodZ+uFAKTqREKZ8tA3TprAp7BVcKiWN9UjsiPX vWbkza+2RRklhc5iKSH/NW9EY+x2nJLn0ODf2J4R+rMH02HG+Z9HD51oFVhqMv5ncv0g AbXFU7hNnedEGTEPHQ8xauGMTlKqeDJJgsDnELvdw7zkNtfyn620hJTKVT+HntKTI4p6 DNBTePgeZmD01cyyvjJ7uvw1XXGiQCJjdHPUhHXeuz6Y4y/NYtsrUArSFqAcGMDOyaqH Ra6Dki10EiQEIuE2Eo61fs+ZMSaw1pTKSiKfynl+mkW7I4h4aB4CcUVU+o++fQC+2KAm sT7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732245755; x=1732850555; 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=1YBszf0JEY6lGH98UF5Cbrpj+6ZfpD+LYDcXM9Rwtik=; b=AFd3QkiEUSWNF/oPdyg3M+wcolj44W/m/B4VkRayQqY+WXqN6nWXzzgrx2feMySRSv QiAsVx9fmrKI9IZBv6nRESt3XWPtAVaYMvAcQPfA6JRdBAgczxoIEWYcQ2ySjm8tL69k fyC4WKwAizFcoWFLJLSFdB/Mw2VGBbBn9Ui4F8esj9Vg1mTS3GjmvNt5Zl1nuYtudJOi GHxXBSuU3dztnbqUqEHt/EcIrjr57TNxPlL6zarGqS6RYAfnWUAdN75JAr+fks5safRC J3KyVx01j5M4zFZZdnLFV2/mQxvB16U0WnGxUxopcl0ubjAe7jbEtvByVxuf66ZrlTIz 8Kyw== X-Gm-Message-State: AOJu0YxTUCzIVx2mHsQRYzC/GpzpskYQkFMRZ/Tvb1fGc2WFoh3B4YzT K13zi8NYQxzfMpWc5hJhlyoHAvh06Vf5cMInZsya+beiKDCzFOPYWzfhG5m6rYN0guI3nehkJm5 NYIGPvsNzRVcOMJdcrSFAiMy7kNlz4akpeE6YWpDBk5C8h7ytXHmpPXnGIEbq/PAS5G5n2G3RU1 0OExvOzRW8beTSQaTAD1EyuQaqNHi1yK4dPq3tbWGj79Cj3/w= X-Gm-Gg: ASbGncvcTegfxlypREzddN8fq12/Pre5HF955wVHXvZTV4utgS6Ku08iJx7CFNzvVpb 12R5ImIcW1czqgOp8pCffrCn9e4YY2Y8V4Ahz9+pZOtdolj6yLhVm8ZUjTuWeixbCFEJMb0GLje sVRgfdeuC0S+vLa6C8nueQODOVp1zu/U/KpsQK0Xv3kM/8gJyjTg3esJZyaMVXfFwwG3J+Iwss6 hcK5/ANtHwXY7uNmajh5LeXQE69+SAvEAdlEf29gv3xR0VGaX36S4NjyKG554xaa6gOtC0DYyyR GCdtMQ== X-Google-Smtp-Source: AGHT+IGz2cAU5Huc2RWPukjPwETpxVCtqdBGLAxmmNrMDf4K6KRuzcSfW1Fi2Z+bCb2O1bfbeMgRkg== X-Received: by 2002:a17:902:d508:b0:212:5bbe:3601 with SMTP id d9443c01a7336-2129f783482mr16779585ad.23.1732245754746; Thu, 21 Nov 2024 19:22:34 -0800 (PST) Received: from fchang-1826.internal.sifive.com ([136.226.240.187]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2129dc157f3sm5506905ad.203.2024.11.21.19.22.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Nov 2024 19:22:34 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Tommy Wu , Frank Chang Subject: [PATCH v9 4/6] target/riscv: Add Smrnmi mnret instruction Date: Fri, 22 Nov 2024 11:22:15 +0800 Message-Id: <20241122032217.3816540-5-frank.chang@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241122032217.3816540-1-frank.chang@sifive.com> References: <20241122032217.3816540-1-frank.chang@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::642; envelope-from=frank.chang@sifive.com; helo=mail-pl1-x642.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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Tommy Wu This patch adds a new instruction 'mnret'. 'mnret' is an M-mode-only instruction that uses the values in `mnepc` and `mnstatus` to return to the program counter, privilege mode, and virtualization mode of the interrupted context. Signed-off-by: Frank Chang Signed-off-by: Tommy Wu --- target/riscv/helper.h | 1 + target/riscv/insn32.decode | 3 ++ .../riscv/insn_trans/trans_privileged.c.inc | 20 +++++++++ target/riscv/op_helper.c | 45 ++++++++++++++++--- 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/target/riscv/helper.h b/target/riscv/helper.h index 451261ce5a..16ea240d26 100644 --- a/target/riscv/helper.h +++ b/target/riscv/helper.h @@ -131,6 +131,7 @@ 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_1(mnret, tl, env) DEF_HELPER_1(wfi, void, env) DEF_HELPER_1(wrs_nto, void, env) DEF_HELPER_1(tlb_flush, void, env) diff --git a/target/riscv/insn32.decode b/target/riscv/insn32.decode index e9139ec1b9..942c434c6e 100644 --- a/target/riscv/insn32.decode +++ b/target/riscv/insn32.decode @@ -121,6 +121,9 @@ wfi 0001000 00101 00000 000 00000 1110011 sfence_vma 0001001 ..... ..... 000 00000 1110011 @sfence_vma sfence_vm 0001000 00100 ..... 000 00000 1110011 @sfence_vm +# *** NMI *** +mnret 0111000 00010 00000 000 00000 1110011 + # *** 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 ecd3b8b2c9..73f940d406 100644 --- a/target/riscv/insn_trans/trans_privileged.c.inc +++ b/target/riscv/insn_trans/trans_privileged.c.inc @@ -18,6 +18,12 @@ * this program. If not, see . */ +#define REQUIRE_SMRNMI(ctx) do { \ + if (!ctx->cfg_ptr->ext_smrnmi) { \ + return false; \ + } \ +} while (0) + static bool trans_ecall(DisasContext *ctx, arg_ecall *a) { /* always generates U-level ECALL, fixed in do_interrupt handler */ @@ -106,6 +112,20 @@ static bool trans_mret(DisasContext *ctx, arg_mret *a) #endif } +static bool trans_mnret(DisasContext *ctx, arg_mnret *a) +{ +#ifndef CONFIG_USER_ONLY + REQUIRE_SMRNMI(ctx); + decode_save_opc(ctx, 0); + gen_helper_mnret(cpu_pc, tcg_env); + tcg_gen_exit_tb(NULL, 0); /* no chaining */ + ctx->base.is_jmp = DISAS_NORETURN; + return true; +#else + return false; +#endif +} + static bool trans_wfi(DisasContext *ctx, arg_wfi *a) { #ifndef CONFIG_USER_ONLY diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c index eddedacf4b..63ec53e992 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -321,24 +321,30 @@ target_ulong helper_sret(CPURISCVState *env) return retpc; } -target_ulong helper_mret(CPURISCVState *env) +static void check_ret_from_m_mode(CPURISCVState *env, target_ulong retpc, + target_ulong prev_priv) { if (!(env->priv >= PRV_M)) { riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); } - target_ulong retpc = env->mepc; if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) { riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC()); } - uint64_t mstatus = env->mstatus; - target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP); - if (riscv_cpu_cfg(env)->pmp && !pmp_get_num_rules(env) && (prev_priv != PRV_M)) { riscv_raise_exception(env, RISCV_EXCP_INST_ACCESS_FAULT, GETPC()); } +} + +target_ulong helper_mret(CPURISCVState *env) +{ + target_ulong retpc = env->mepc; + uint64_t mstatus = env->mstatus; + target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP); + + check_ret_from_m_mode(env, retpc, prev_priv); target_ulong prev_virt = get_field(env->mstatus, MSTATUS_MPV) && (prev_priv != PRV_M); @@ -370,6 +376,35 @@ target_ulong helper_mret(CPURISCVState *env) return retpc; } +target_ulong helper_mnret(CPURISCVState *env) +{ + target_ulong retpc = env->mnepc; + target_ulong prev_priv = get_field(env->mnstatus, MNSTATUS_MNPP); + target_ulong prev_virt; + + check_ret_from_m_mode(env, retpc, prev_priv); + + prev_virt = get_field(env->mnstatus, MNSTATUS_MNPV) && + (prev_priv != PRV_M); + env->mnstatus = set_field(env->mnstatus, MNSTATUS_NMIE, true); + + /* + * If MNRET changes the privilege mode to a mode + * less privileged than M, it also sets mstatus.MPRV to 0. + */ + if (prev_priv < PRV_M) { + env->mstatus = set_field(env->mstatus, MSTATUS_MPRV, false); + } + + if (riscv_has_ext(env, RVH) && prev_virt) { + riscv_cpu_swap_hypervisor_regs(env); + } + + riscv_cpu_set_mode(env, prev_priv, prev_virt); + + return retpc; +} + void helper_wfi(CPURISCVState *env) { CPUState *cs = env_cpu(env); From patchwork Fri Nov 22 03:22:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Chang X-Patchwork-Id: 13882739 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1A8A3E65D1B for ; Fri, 22 Nov 2024 03:24:08 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tEKFo-0005Yj-PJ; Thu, 21 Nov 2024 22:22:46 -0500 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 1tEKFk-0005XX-6Q for qemu-devel@nongnu.org; Thu, 21 Nov 2024 22:22:40 -0500 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tEKFi-0003y3-Na for qemu-devel@nongnu.org; Thu, 21 Nov 2024 22:22:39 -0500 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-21145812538so12799195ad.0 for ; Thu, 21 Nov 2024 19:22:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1732245757; x=1732850557; 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=ac5vpUHfFOddVJFriYu+6Qi8+wWX9FL8DoIUJSWkCVQ=; b=ImnPNExe34RLm4SjPbAiHqykZp/ON8aABTXWBv2qb9kLySrGNFbEDKX4WEYlTGCXgq wsuuc31P6gia8QyECvrlOjqje3gygDbeAnBfHr+QDjsD7cK0VldTLsW0U3TQ7SWkpH+9 8E+QMcujElBy861m48z0GnZqy4PXFvL4RuQf5W0MFSVWqinCSApbSUd575yBeMuUb75X cHBigOPhZ1erchYmtLFWGhpNaWkEYqecMFmL/k4fqjftkH860sVMCVXD7KDb6+krFZES UcnJk/w4YN7RM9FkwWuamclW5IAEBZ63o7kSCAwNetfaUreZOZXGxJqAsgJ/8qlEnbZz hiLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732245757; x=1732850557; 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=ac5vpUHfFOddVJFriYu+6Qi8+wWX9FL8DoIUJSWkCVQ=; b=Y+1/9EyR3p+aP8fOAU6gFJNNd0XDtDRf0Ajo0BZV13Lq5ePUMwlWJdtMxAdL9Rxmyx YuMa7umVBYAyPj3gH2J0GAMR898ly2PpvRqzqKqfq0A97XPeSCBdsYTt4QiSqOr47iVp P/4+7rlafA+qUUdIICOQhiOFUNWb8er/w6Vp+t0NJBjJm2+awCsi61spMVjJIFtbndlO 1fJRFCbG3+9FuE2ukxXU8BQHBIMAnfVrXFzjIP8Wy8tHur6nvfwICNQo5T9R2FSJFoRu mQmEtLMHYohKZqVZhsmKSoRVpr1hhfBMxlARmaSEcblJZxFc5OgujdTD67DicTYx2Ps4 5y8w== X-Gm-Message-State: AOJu0YzWAjgzP69N9+vQ2yKW1ZVknEww91i8WK2XTC1OReBhOOJLKLMB EuaWW8j50wK0XTG+VRPk/F4UIqM3kK+eQDtHHAB4a48HpFlQaQCSOWcS/4nIAvbaUYFnobBIwkP mjAesIZllOjKRicI47pgS5+wzQ5YgzQU0O2/AifLSUQjjSA+7mKpB19PRGU3474MVbWDTnH/wpz uonIDxyz5JA91iCTTz7AmpKY9HFtr9GNjLBT63N5I1Xh7F X-Gm-Gg: ASbGncusj6WrLntjO6ApJPcp+42b8uLWsDiReAT8rzZEHeF7rfdXvbBAM4v1iGBv9Y2 U8fP0xCk0ZpuxrRdNQrAf/fF+5QCptdBJ4/lo6ZpVD4JDYYnIALNZen82dYgS9pigzyS/g74r4s Wma3QKWCZp5YbULz3pdYuLVtCcfMYiWgaPm4EJLU4rSXaxvLg6Sbhnbstw8SEZVMHsRhGw7CPPZ Up/COHoUmBz28zi52Hzo0TIIi2BkeOqedRX9ywUGD+5liAlfvfJCdrQzlEMt/hIIJdiieUtrCXT 5HmzmQ== X-Google-Smtp-Source: AGHT+IGkQEQhtzFsufICB4DTLKiak+NF6QYZWdUlZIyTO2pwITRNoyLVMwo05qfQ7LnD6bOPsPTjQw== X-Received: by 2002:a17:902:f64c:b0:212:6981:7587 with SMTP id d9443c01a7336-2129fd0fc42mr19001305ad.24.1732245756994; Thu, 21 Nov 2024 19:22:36 -0800 (PST) Received: from fchang-1826.internal.sifive.com ([136.226.240.187]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2129dc157f3sm5506905ad.203.2024.11.21.19.22.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Nov 2024 19:22:36 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Tommy Wu , Frank Chang Subject: [PATCH v9 5/6] target/riscv: Add Smrnmi cpu extension Date: Fri, 22 Nov 2024 11:22:16 +0800 Message-Id: <20241122032217.3816540-6-frank.chang@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241122032217.3816540-1-frank.chang@sifive.com> References: <20241122032217.3816540-1-frank.chang@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=frank.chang@sifive.com; helo=mail-pl1-x62c.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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Tommy Wu This adds the properties for ISA extension Smrnmi. Signed-off-by: Frank Chang Signed-off-by: Tommy Wu --- target/riscv/cpu.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index b402d8545b..6c91464a00 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -186,6 +186,7 @@ const RISCVIsaExtData isa_edata_arr[] = { ISA_EXT_DATA_ENTRY(smaia, PRIV_VERSION_1_12_0, ext_smaia), ISA_EXT_DATA_ENTRY(smcntrpmf, PRIV_VERSION_1_12_0, ext_smcntrpmf), ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp), + ISA_EXT_DATA_ENTRY(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi), ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen), ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia), ISA_EXT_DATA_ENTRY(ssccptr, PRIV_VERSION_1_11_0, has_priv_1_11), @@ -1516,6 +1517,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = { MULTI_EXT_CFG_BOOL("smaia", ext_smaia, false), MULTI_EXT_CFG_BOOL("smepmp", ext_smepmp, false), + MULTI_EXT_CFG_BOOL("smrnmi", ext_smrnmi, false), MULTI_EXT_CFG_BOOL("smstateen", ext_smstateen, false), MULTI_EXT_CFG_BOOL("ssaia", ext_ssaia, false), MULTI_EXT_CFG_BOOL("svade", ext_svade, false), From patchwork Fri Nov 22 03:22:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Chang X-Patchwork-Id: 13882742 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5F286E65D18 for ; Fri, 22 Nov 2024 03:25:23 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tEKFt-0005a4-Jb; Thu, 21 Nov 2024 22:22:49 -0500 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 1tEKFm-0005YL-JX for qemu-devel@nongnu.org; Thu, 21 Nov 2024 22:22:42 -0500 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tEKFl-0003yS-4T for qemu-devel@nongnu.org; Thu, 21 Nov 2024 22:22:42 -0500 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-21278f3d547so13415585ad.1 for ; Thu, 21 Nov 2024 19:22:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1732245759; x=1732850559; 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=ziiRgdmp8/djQe/OUBTNWsmMki7f4gTCJYiLTqE+tsI=; b=OHQmRaSVC5NxyDtrUVEueMAfzY5X12tkezD3QPfmlySoC6ssfCo7efn7Ip+qJPm0zc ZP8wnvnNPjIWY7WJpw+oL6Sm9XtXyr+MHcHf3RnkvQQBIvMakvKUmShJwYtURC6+EfXh xXIN30AdmVxuqr7oJCy/PR6k/sgCrNDwohgMbMYXmGnlYTiIZItYozBJku0ffGpRt2DJ 1Grk0v3AWWgbI7LhwqxI1gqGhWcBCXcOZX7MZtmvvGe8wCACOGyqOyuuNrqoimE/8XoH e9qOvgTu3gSC3XlKfD/tgQlF3KOuNiOSErkCP5e8JQIVczca9TYVb8LxvEWisDbteSJd ElDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732245759; x=1732850559; 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=ziiRgdmp8/djQe/OUBTNWsmMki7f4gTCJYiLTqE+tsI=; b=jseywJGpooCdLVmo7B8l9jAaaET0ajIy/IAaumexH46s67+cFH+ayKjXlovq3cKpTi gQb0sFhyoTY5YsttOg1zJZcEc7Izw7KU0+6Suc+mdeGk9/HJxE0L3Na9oj6Bun8f5Cbv YJw0l4Y66fuv8MVhdwz1kcQaIYr/T/cHO2T4FwB0KW+b/ShXMwp8ZLXakQ4cg/TPZWzD Xt/WGQJZVrfSvFtj3vLGRQ0KgK0C5g070o8wgh3HDvybwvFkxBaKycIZbL0V1Xhbj9Hb f5kQgtvX5infNcMZb6Vc5xxCqN6Wrbh0YiEjHlHZj8W5dV6fedUX7fP1K3YlJJsT8YfB mrBQ== X-Gm-Message-State: AOJu0YwFWDIAzL8idcggb20BqaEZU7Oo8ZVcmz3MArQSrL4W9h9Beuhg otO4MNNp1hxxsM/GcxwgrCENwFOUOzHJMdn/ByJAL2PMDEvD7GdL+4lxSTOJKaYyfm4YeTIH9fA dORlTgdUO8SPsUs3G8WHASbtIqA+/sExCTzGao6fbJm4+QQYmlW4JG6yrOs+f26g+NwNDNkvCt0 BVy7A9aMo+sUWJQm4GZDdXo9+v19S6yrYi5I6eoll/o/2d X-Gm-Gg: ASbGncsJlxw5mxbXuLp2Xd8HBiRdziE+ijLM9Y+JX/ZJzIP25/s8LkrrZzoDjKV+MKm sqR623syW5RDzlJg/0N8WjUdkFYYrd4A08mxGedsM1uDYLBfpPZK/dADU9+vL+AAlheqpHx+MO7 Hu/mUz9WlOtDd43Wv9j3566DKSJlRr0oNNGHq6H4lMfLPQAwk/1IMKofEHwnqO+08BV+kg3E2sa aeqoVvPhStB8ZMX6TCGLlkWZWpb6SUK1q97HZEvNbx/4WDdGvGkfkzBTVktxwfITj4KgFTsG9qi 0P3lUQ== X-Google-Smtp-Source: AGHT+IFZYVmDYfhXQLuGHV+w2uZrMzWRR21YsjcvW60NmTvIH/kA5RVeCpSqQwwT0f5aRNMmiMBoeg== X-Received: by 2002:a17:902:e84f:b0:212:514:b30a with SMTP id d9443c01a7336-2129f72fa48mr22064765ad.12.1732245758605; Thu, 21 Nov 2024 19:22:38 -0800 (PST) Received: from fchang-1826.internal.sifive.com ([136.226.240.187]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2129dc157f3sm5506905ad.203.2024.11.21.19.22.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Nov 2024 19:22:38 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Frank Chang Subject: [PATCH v9 6/6] target/riscv: Add Zicfilp support for Smrnmi Date: Fri, 22 Nov 2024 11:22:17 +0800 Message-Id: <20241122032217.3816540-7-frank.chang@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241122032217.3816540-1-frank.chang@sifive.com> References: <20241122032217.3816540-1-frank.chang@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::635; envelope-from=frank.chang@sifive.com; helo=mail-pl1-x635.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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Frank Chang Zicfilp extension introduces the MNPELP (bit 9) in mnstatus. The MNPELP field holds the previous ELP. When a RNMI trap is delivered, the MNPELP is set to ELP and ELP set to NO_LP_EXPECTED. Upon a mnret, if the mnstatus.MNPP holds the value y, then ELP is set to the value of MNPELP if yLPE is 1; otherwise, it is set to NO_LP_EXPECTED. Signed-off-by: Frank Chang --- target/riscv/cpu_bits.h | 1 + target/riscv/cpu_helper.c | 11 ++++++++++- target/riscv/op_helper.c | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index 01e279dc25..f31735d15b 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -642,6 +642,7 @@ typedef enum { /* RNMI mnstatus CSR mask */ #define MNSTATUS_NMIE 0x00000008 #define MNSTATUS_MNPV 0x00000080 +#define MNSTATUS_MNPELP 0x00000200 #define MNSTATUS_MNPP 0x00001800 /* VM modes (satp.mode) privileged ISA 1.10 */ diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 828b6a1003..d53ddc384f 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -1863,6 +1863,10 @@ void riscv_cpu_do_interrupt(CPUState *cs) env->mnepc = env->pc; env->pc = env->rnmi_irqvec; + if (cpu_get_fcfien(env)) { + env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPELP, env->elp); + } + /* Trapping to M mode, virt is disabled */ riscv_cpu_set_mode(env, PRV_M, false); @@ -2030,7 +2034,12 @@ void riscv_cpu_do_interrupt(CPUState *cs) /* handle the trap in M-mode */ /* save elp status */ if (cpu_get_fcfien(env)) { - env->mstatus = set_field(env->mstatus, MSTATUS_MPELP, env->elp); + if (nnmi_excep) { + env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPELP, + env->elp); + } else { + env->mstatus = set_field(env->mstatus, MSTATUS_MPELP, env->elp); + } } if (riscv_has_ext(env, RVH)) { diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c index 63ec53e992..a4b625fcd9 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -402,6 +402,15 @@ target_ulong helper_mnret(CPURISCVState *env) riscv_cpu_set_mode(env, prev_priv, prev_virt); + /* + * If forward cfi enabled for new priv, restore elp status + * and clear mnpelp in mnstatus + */ + if (cpu_get_fcfien(env)) { + env->elp = get_field(env->mnstatus, MNSTATUS_MNPELP); + } + env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPELP, 0); + return retpc; }