From nobody Sat Nov 23 16:19:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1732245868; cv=none; d=zohomail.com; s=zohoarc; b=YQ8t1ggXq4J9fAuC8v8VgshueFERzaS39OldTW2nMaTrS+pLLWYPeiD0Y/nr+Rrw4CbWow464bQAVNohI7j3IITIK0wcPesQVdQIAit8SkvjC0gl71NN82CVxQ5um+eOtXwC7GqHBHatBETo7qvR6XsBlqyLEFMsTrRgpLLIVuQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732245868; 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=ete+ZJB8URbofv6Sb72yOCLqH3KqEk//Slye3l6+Ywk=; b=IxdtAKMVsmZUe4Bn6L5TbT2aDl35MMjMlDACztRBlieqKz4+kALaIaOCxMrAWAMZWXVRcq6+tB4c+G4GJT9uZPS+McH04V+pCbpAstM2nQlGAhLBFqL2u3Hh1+f2DoHUOH8ikKuFQF+n4MLCdu8g6pmoQTN/aKmjA29mBS2yEfY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732245868105561.6110285825845; Thu, 21 Nov 2024 19:24:28 -0800 (PST) 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) 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) 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) 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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @sifive.com) X-ZM-MESSAGEID: 1732245868504116600 Content-Type: text/plain; charset="utf-8" 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; --=20 2.34.1 From nobody Sat Nov 23 16:19:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1732245806; cv=none; d=zohomail.com; s=zohoarc; b=Rw0+C1SVAUxstGO+izsUa70j4R7ECkRRAyDxP2qfqH5gQze56lc1xuxGAvzVcIpnthpnsu67th3S3Vq2G774WvqeRp2ZLHsJOuZs0B2YxCcXLyHta61hzbsGj5NuZhz885T5ia+M1u2klRaWD71YfeBG8Dghi7s7OZtuDUoXED8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732245806; 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=F5dz25xLBEpscPUFsdwI6ZdGbDCjYWaBqQIsk/OC0sI=; b=U4+97Uvcia47qU5X0d6EvrvrQNm+J3kNS4dJTgllRuF/i7TAosYX4ig5sU7sxLVCmxFRbQG33GNHRHVi6zjst7cJBuTuhddFHZmTWjWBB3nV6z4tmn/7diULRCYqGFkL4U7m6FuJIOsb8Ncb2NmWomVIfJkWlJcFrsYts0xcZ/M= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732245806140113.6575822551946; Thu, 21 Nov 2024 19:23:26 -0800 (PST) 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) 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) 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) 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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @sifive.com) X-ZM-MESSAGEID: 1732245808416116600 Content-Type: text/plain; charset="utf-8" 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, ResetT= ype type) riscv_trigger_reset_hold(env); } =20 + if (cpu->cfg.ext_smrnmi) { + env->rnmip =3D 0; + env->mnstatus =3D 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; }; =20 /* 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 =20 +/* 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 =20 +/* 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 cs= rno) =20 return RISCV_EXCP_ILLEGAL_INST; } + +static RISCVException rnmi(CPURISCVState *env, int csrno) +{ + RISCVCPU *cpu =3D env_archcpu(env); + + if (cpu->cfg.ext_smrnmi) { + return RISCV_EXCP_NONE; + } + + return RISCV_EXCP_ILLEGAL_INST; +} #endif =20 static RISCVException seed(CPURISCVState *env, int csrno) @@ -4647,6 +4658,67 @@ static RISCVException write_upmbase(CPURISCVState *e= nv, int csrno, return RISCV_EXCP_NONE; } =20 +static RISCVException read_mnscratch(CPURISCVState *env, int csrno, + target_ulong *val) +{ + *val =3D env->mnscratch; + return RISCV_EXCP_NONE; +} + +static int write_mnscratch(CPURISCVState *env, int csrno, target_ulong val) +{ + env->mnscratch =3D val; + return RISCV_EXCP_NONE; +} + +static int read_mnepc(CPURISCVState *env, int csrno, target_ulong *val) +{ + *val =3D env->mnepc; + return RISCV_EXCP_NONE; +} + +static int write_mnepc(CPURISCVState *env, int csrno, target_ulong val) +{ + env->mnepc =3D val; + return RISCV_EXCP_NONE; +} + +static int read_mncause(CPURISCVState *env, int csrno, target_ulong *val) +{ + *val =3D env->mncause; + return RISCV_EXCP_NONE; +} + +static int write_mncause(CPURISCVState *env, int csrno, target_ulong val) +{ + env->mncause =3D val; + return RISCV_EXCP_NONE; +} + +static int read_mnstatus(CPURISCVState *env, int csrno, target_ulong *val) +{ + *val =3D env->mnstatus; + return RISCV_EXCP_NONE; +} + +static int write_mnstatus(CPURISCVState *env, int csrno, target_ulong val) +{ + target_ulong mask =3D (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 |=3D MNSTATUS_MNPV; + } + + /* mnstatus.mnie can only be cleared by hardware. */ + env->mnstatus =3D (env->mnstatus & MNSTATUS_NMIE) | (val & mask); + return RISCV_EXCP_NONE; +} + #endif =20 /* Crypto Extension */ @@ -5154,6 +5226,16 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { write_sstateen_1_3, .min_priv_ver =3D PRIV_VERSION_1_12_0 }, =20 + /* RNMI */ + [CSR_MNSCRATCH] =3D { "mnscratch", rnmi, read_mnscratch, write_mnscrat= ch, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + [CSR_MNEPC] =3D { "mnepc", rnmi, read_mnepc, write_mnepc, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + [CSR_MNCAUSE] =3D { "mncause", rnmi, read_mncause, write_mncause, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + [CSR_MNSTATUS] =3D { "mnstatus", rnmi, read_mnstatus, write_mnstatu= s, + .min_priv_ver =3D PRIV_VERSION_1_12_0 = }, + /* Supervisor Trap Setup */ [CSR_SSTATUS] =3D { "sstatus", smode, read_sstatus, write_sst= atus, NULL, read_sstatus_i128 = }, --=20 2.34.1 From nobody Sat Nov 23 16:19:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1732245795; cv=none; d=zohomail.com; s=zohoarc; b=QuDyMCqK9zkIgcejf8ep6wTobPRVIScxwVfHYgLkj5pgYx+HuYTG0DU02sJguGEgpozDest/YoyaUSJbPiUAdTV3eredbia6sNXo4fNBlEN1UvMbuZxTw2EbYL9ozZNPF9d9FR7Vr2PXZKHaXEc2Y/rlbAlhkQkA1dN2X+7CgUE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732245795; 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=tOE6OxXP+uljt1Ysl2Pdar5s462OP1Rc9jWDEs1d3A0=; b=muVJHHR4ZsWDChIt3kIBR997VjXMdcwXO/NCycj9u/7U3WhCtP5mk7BVYkO8/i8CamqkGW20MbQrAfaLfzyYZ5tiGImZnn+28AnWmbi9oN28ISfO/VCRVl1DhURxV3GfYWFgk2Si8V23kw8PnWFgASc0pYAHK/TEvVUKRPo9vfQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732245795024972.510294382517; Thu, 21 Nov 2024 19:23:15 -0800 (PST) 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) 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) 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) 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 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=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @sifive.com) X-ZM-MESSAGEID: 1732245798010116600 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 prope= rty of the harts. It=E2=80=99s very easy for users to set the address based on = their expectation. This patch also adds the functionality to handle the RNMI sign= als. 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" =20 static Property riscv_harts_props[] =3D { DEFINE_PROP_UINT32("num-harts", RISCVHartArrayState, num_harts, 1), @@ -33,6 +34,22 @@ static Property riscv_harts_props[] =3D { 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(), }; =20 @@ -47,6 +64,29 @@ static bool riscv_hart_realize(RISCVHartArrayState *s, i= nt idx, { object_initialize_child(OBJECT(s), "harts[*]", &s->harts[idx], cpu_typ= e); 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[i= dx]); + } + + 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 =3D 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; }; =20 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 */ =20 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 */ =20 general_user_opts =3D g_hash_table_new(g_str_hash, g_str_equal); @@ -2689,6 +2696,10 @@ static Property riscv_cpu_properties[] =3D { =20 #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 =20 DEFINE_PROP_BOOL("short-isa-string", RISCVCPU, cfg.short_isa_string, f= alse), 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; }; =20 /* @@ -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 =20 +/* 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 =3D -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) =20 +/* 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 =20 +/* 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; =20 + /* Priority: RNMI > Other interrupt. */ + if (riscv_cpu_cfg(env)->ext_smrnmi) { + /* If mnstatus.NMIE =3D=3D 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 =3D 1; @@ -567,7 +579,9 @@ static int riscv_cpu_local_irq_pending(CPURISCVState *e= nv) =20 bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request) { - if (interrupt_request & CPU_INTERRUPT_HARD) { + uint32_t mask =3D CPU_INTERRUPT_HARD | CPU_INTERRUPT_RNMI; + + if (interrupt_request & mask) { RISCVCPU *cpu =3D RISCV_CPU(cs); CPURISCVState *env =3D &cpu->env; int interruptno =3D riscv_cpu_local_irq_pending(env); @@ -699,6 +713,30 @@ void riscv_cpu_set_geilen(CPURISCVState *env, target_u= long geilen) env->geilen =3D geilen; } =20 +void riscv_cpu_set_rnmi(RISCVCPU *cpu, uint32_t irq, bool level) +{ + CPURISCVState *env =3D &cpu->env; + CPUState *cs =3D CPU(cpu); + bool release_lock =3D false; + + if (!bql_locked()) { + release_lock =3D true; + bql_lock(); + } + + if (level) { + env->rnmip |=3D 1 << irq; + cpu_interrupt(cs, CPU_INTERRUPT_RNMI); + } else { + env->rnmip &=3D ~(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 =3D &cpu->env; @@ -1794,6 +1832,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) bool write_gva =3D false; bool always_storeamo =3D (env->excp_uw2 & RISCV_UW2_ALWAYS_STORE_AMO); uint64_t s; + int mode; =20 /* * 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 =3D 0; int sxlen =3D 0; int mxlen =3D 0; + bool nnmi_excep =3D false; + + if (cpu->cfg.ext_smrnmi && env->rnmip && async) { + env->mnstatus =3D set_field(env->mnstatus, MNSTATUS_NMIE, false); + env->mnstatus =3D set_field(env->mnstatus, MNSTATUS_MNPV, + env->virt_enabled); + env->mnstatus =3D set_field(env->mnstatus, MNSTATUS_MNPP, + env->priv); + env->mncause =3D cause | ((target_ulong)1U << (TARGET_LONG_BITS - = 1)); + env->mnepc =3D env->pc; + env->pc =3D env->rnmi_irqvec; + + /* Trapping to M mode, virt is disabled */ + riscv_cpu_set_mode(env, PRV_M, false); + + return; + } =20 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)); =20 - if (env->priv <=3D PRV_S && cause < 64 && - (((deleg >> cause) & 1) || s_injected || vs_injected)) { + mode =3D env->priv <=3D PRV_S && cause < 64 && + (((deleg >> cause) & 1) || s_injected || vs_injected) ? PRV_S : PR= V_M; + + if (mode =3D=3D 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) =3D=3D 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 exce= ption. + */ + nnmi_excep =3D 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 =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); + + /* + * For RNMI exception, program counter is set to the RNMI exception + * trap handler address. + */ + if (nnmi_excep) { + env->pc =3D env->rnmi_excpvec; + } else { + 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); } =20 --=20 2.34.1 From nobody Sat Nov 23 16:19:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1732245904; cv=none; d=zohomail.com; s=zohoarc; b=hawdO4Ym8pOtzZY31oklySxh4OfJjSa4g0NK5VHLLVALLD+Qijsy12aO3l/oeYD3QVlFenmLfGc9e0mRko68Qty9vcOZuBzuUDUw74b01jCMOaAJgHlzAI4K5/GVwi4R82DIMaj/lfhzqhD1e0OWzb4ZxPEqbWW847YMC3fBZpg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732245904; 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=1YBszf0JEY6lGH98UF5Cbrpj+6ZfpD+LYDcXM9Rwtik=; b=Y3LSQ+8PhkeiIC5/7VthYmZ4RSHdvK7CijXCT/DvPCXdO/ZL0xqcqi39w3U4ZmMrX3xZSIXowZ2ixHSNPzr/kQFVbpGktRAwkVgt1ezCQJwXSkA7Z48Bjl5PXFYT8KUMqIklkOe91WnSZ9wB25KbTQbncvGwgjkRnpQIjSPv5o0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732245904753431.68190676477695; Thu, 21 Nov 2024 19:25:04 -0800 (PST) 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) 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) 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) 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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @sifive.com) X-ZM-MESSAGEID: 1732245906567116600 Content-Type: text/plain; charset="utf-8" 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 =20 +# *** 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 . */ =20 +#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 } =20 +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 =3D 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; } =20 -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 >=3D PRV_M)) { riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); } =20 - target_ulong retpc =3D env->mepc; if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) { riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC()); } =20 - uint64_t mstatus =3D env->mstatus; - target_ulong prev_priv =3D get_field(mstatus, MSTATUS_MPP); - if (riscv_cpu_cfg(env)->pmp && !pmp_get_num_rules(env) && (prev_priv !=3D PRV_M)) { riscv_raise_exception(env, RISCV_EXCP_INST_ACCESS_FAULT, GETPC()); } +} + +target_ulong helper_mret(CPURISCVState *env) +{ + target_ulong retpc =3D env->mepc; + uint64_t mstatus =3D env->mstatus; + target_ulong prev_priv =3D get_field(mstatus, MSTATUS_MPP); + + check_ret_from_m_mode(env, retpc, prev_priv); =20 target_ulong prev_virt =3D get_field(env->mstatus, MSTATUS_MPV) && (prev_priv !=3D PRV_M); @@ -370,6 +376,35 @@ target_ulong helper_mret(CPURISCVState *env) return retpc; } =20 +target_ulong helper_mnret(CPURISCVState *env) +{ + target_ulong retpc =3D env->mnepc; + target_ulong prev_priv =3D get_field(env->mnstatus, MNSTATUS_MNPP); + target_ulong prev_virt; + + check_ret_from_m_mode(env, retpc, prev_priv); + + prev_virt =3D get_field(env->mnstatus, MNSTATUS_MNPV) && + (prev_priv !=3D PRV_M); + env->mnstatus =3D 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 =3D 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 =3D env_cpu(env); --=20 2.34.1 From nobody Sat Nov 23 16:19:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1732245848; cv=none; d=zohomail.com; s=zohoarc; b=I+aU237+j5QJmtnq753+iksZgCR0Mg8j6LKy+lnSuuV9dVrfawHdtlTMCXAgaWfhj9DEu7FTSSgcAKPp8xs7M3WGyf38fSoCO5WMvEBFFyvf9OZoeMbJzXC8CEMmPVKkD21xWJpYyNyl6ipSjQXrNnu4QD04REv6mET+veule6c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732245848; 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=ac5vpUHfFOddVJFriYu+6Qi8+wWX9FL8DoIUJSWkCVQ=; b=WtSWLoY0OXjEIzFQNe0h+sq6FnlOhl98ea1AhJTFV4wn8UJzvrjHAm+8eBZoIJEnTPFsT3PH8RdhfSIzfupgbXR4rfF0KaKjl+yIW6iUaQVFNcPv8FApFa7bqZP7liG8SpJCLfGmiQ4NxuGUyJga9cXaioUJyy1e7jto2bgTA+I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732245848405470.86449854927423; Thu, 21 Nov 2024 19:24:08 -0800 (PST) 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) 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) 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) 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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @sifive.com) X-ZM-MESSAGEID: 1732245850414116600 Content-Type: text/plain; charset="utf-8" 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[] =3D { 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[] = =3D { =20 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), --=20 2.34.1 From nobody Sat Nov 23 16:19:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1732245923; cv=none; d=zohomail.com; s=zohoarc; b=cebGtqDbU/5QyMi1w45SAbzog7lJZx9YogTeQ4aXN88RF0+i3JfT/v2s3UREOTKVLLoSjVYEyPBdchiENeWqQMIXHlPOmyInygKxDBBphaV0neU9+akTSj7LnxNmAuYr2W3p2vAUIaJ/7xuZ36sFR9rIYaQQXIFLuC5jxruMkWU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732245923; 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=ziiRgdmp8/djQe/OUBTNWsmMki7f4gTCJYiLTqE+tsI=; b=GZaWkfubO7SRKn1hXQzjjKXYW6OwCx5h4Qg0bOMeVK5MnQCEo1tx3k0+fczP9kQ6Pm0+siCldVEmGh+hvL5or/4sfeP7jS+79iZj0IHuxDs98Iptwh8Efc0Awh56VTzFmnO9eQp1H9w+i/CbLOIskPSFPIypUKjHDJ6TPQvgSmU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732245923489218.1492648869522; Thu, 21 Nov 2024 19:25:23 -0800 (PST) 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) 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) 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) 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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @sifive.com) X-ZM-MESSAGEID: 1732245924679116600 Content-Type: text/plain; charset="utf-8" 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 =20 /* 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 =3D env->pc; env->pc =3D env->rnmi_irqvec; =20 + if (cpu_get_fcfien(env)) { + env->mnstatus =3D set_field(env->mnstatus, MNSTATUS_MNPELP, en= v->elp); + } + /* Trapping to M mode, virt is disabled */ riscv_cpu_set_mode(env, PRV_M, false); =20 @@ -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 =3D set_field(env->mstatus, MSTATUS_MPELP, env->e= lp); + if (nnmi_excep) { + env->mnstatus =3D set_field(env->mnstatus, MNSTATUS_MNPELP, + env->elp); + } else { + env->mstatus =3D set_field(env->mstatus, MSTATUS_MPELP, en= v->elp); + } } =20 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) =20 riscv_cpu_set_mode(env, prev_priv, prev_virt); =20 + /* + * If forward cfi enabled for new priv, restore elp status + * and clear mnpelp in mnstatus + */ + if (cpu_get_fcfien(env)) { + env->elp =3D get_field(env->mnstatus, MNSTATUS_MNPELP); + } + env->mnstatus =3D set_field(env->mnstatus, MNSTATUS_MNPELP, 0); + return retpc; } =20 --=20 2.34.1