From patchwork Fri Aug 9 08:12:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tommy Wu X-Patchwork-Id: 1970844 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=KInpY9NA; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WgGrr3sdWz1yfC for ; Fri, 9 Aug 2024 18:13:47 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1scKjo-0005Z8-Hn; Fri, 09 Aug 2024 04:12:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1scKjm-0005Ry-J4 for qemu-devel@nongnu.org; Fri, 09 Aug 2024 04:12:38 -0400 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1scKjk-0006mV-9i for qemu-devel@nongnu.org; Fri, 09 Aug 2024 04:12:38 -0400 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1fc491f9b55so15236225ad.3 for ; Fri, 09 Aug 2024 01:12:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1723191154; x=1723795954; 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=q0akk9tyfAh4jdZDvZ/G+t9QiEID+7sYMlv0pGeXDRA=; b=KInpY9NAAfrlISj/Go8FGbF6N/8fvLUUt5Z/iBGHesNN0o81DIzMtUxpwL/gw/rCX6 tqZc/qPfCQHJdIJv/SJWodmaeLmDQRAa6Zs3S9QwmEBgZJKNwPN0k1PrrYmuivcEPILt G/EwJ6cH5QDZ/WLsOYFF8IF37LigPPZ+DjdaXCPs8A+Tv+Bqr/lQNh01yhwwuiXZ12Ei bJXvIqmPcGbQYQBuIyxBTy1H3493mqjR9YOnvZAKk8P8bcJT1+tMjgKlxgHs1fMGM4Xm KKoFE5bq+XJ/lODL2IAtCFOAWtwPjSJ94U19aRuIeRoVykMWMP3BdB11m5QsYIPcgi6V c4FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723191154; x=1723795954; 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=q0akk9tyfAh4jdZDvZ/G+t9QiEID+7sYMlv0pGeXDRA=; b=bEc/6E5tEoj4NDJks44KCUoIMD4FghU1gmLXGULi1APusAuEKdRieNjGE6ddXoj+MH 43DTT4z8u5dUTmHF95cO/oTw02wi/WU5csK73/w+j3e7sgkDQNPjfCZRrv2gNzXbW1yR R7YBYT3Gtqa32JZeYbpOBZxnVvkD1AaMwpJHMS3ppDuuAf51Lkrjali1wzBi8osidt88 tZdLKqV62M/LuHQPbo+4tRXAHxNVXY9KxpYGBoZB3xCJDd8eDKLP9DQvQhbgMIoenb+p jpwXnnIIEKeHC+k/7uKET4dxFZ320+gFNwtGE3UxAa3bQSGKDxldPQx6VrES4O7HIi3z UpmQ== X-Gm-Message-State: AOJu0YwuFZb/geyUjvUeCz1dDIblTh8CxLzWAe6GR5gFA75Z2mxm2nAI 4ANddfCK2L9JlgVHyebMcNXQEs15oRNWDSH6xOpbcQAQDJr+UgX7Rf1fG6sLVhEw/2KrmTkPEEZ Z1hbLyUzyoDoCEwy+dyOEoVpbt+C1g0N2fUl20FQ8KVJAoy+Poye+kvIblzBk+Z3dGjX08OqZhL V6z3E3bQ5TKqzm7kqy27W0b4RH+OUcnoHRhPcefg== X-Google-Smtp-Source: AGHT+IFNx0RIqB+bWlhV26lfOXNTWY3R7BP6Yqg3janFpP+XZj/yfkm0e3MT+WqlaBQEtZIoDEZmcg== X-Received: by 2002:a17:903:32c6:b0:1fd:a412:5e05 with SMTP id d9443c01a7336-200ae5de153mr6428595ad.52.1723191154053; Fri, 09 Aug 2024 01:12:34 -0700 (PDT) Received: from sw07.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff592b6cfbsm136391185ad.306.2024.08.09.01.12.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 01:12:33 -0700 (PDT) From: Tommy Wu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: frank.chang@sifive.com, palmer@dabbelt.com, alistair.francis@wdc.com, alistair23@gmail.com, bin.meng@windriver.com, liweiwei@iscas.ac.cn, dbarboza@ventanamicro.com, Tommy Wu Subject: [PATCH v5 1/5] target/riscv: Add `ext_smrnmi` in the RISCVCPUConfig. Date: Fri, 9 Aug 2024 01:12:23 -0700 Message-Id: <20240809081227.1588508-2-tommy.wu@sifive.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240809081227.1588508-1-tommy.wu@sifive.com> References: <20240809081227.1588508-1-tommy.wu@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=tommy.wu@sifive.com; helo=mail-pl1-x636.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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 8b272fb826..ae2a945b5f 100644 --- a/target/riscv/cpu_cfg.h +++ b/target/riscv/cpu_cfg.h @@ -125,6 +125,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; From patchwork Fri Aug 9 08:12:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tommy Wu X-Patchwork-Id: 1970847 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=nsFJK3Q4; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WgGsJ3vwZz1ybS for ; Fri, 9 Aug 2024 18:14:12 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1scKjq-0005fK-7w; Fri, 09 Aug 2024 04:12:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1scKjp-0005bX-46 for qemu-devel@nongnu.org; Fri, 09 Aug 2024 04:12:41 -0400 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 1scKjm-0006mv-SO for qemu-devel@nongnu.org; Fri, 09 Aug 2024 04:12:40 -0400 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1fd66cddd07so14602685ad.2 for ; Fri, 09 Aug 2024 01:12:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1723191156; x=1723795956; 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=2CJ7nkkJAHhtFWGqFm7LmVNcwlwW3z2wjacsiudUdSo=; b=nsFJK3Q4J7SFFHyVkcmnkGNVvQGCdbk2sfk0S4Nb9ymIFXPPCAZ+LZ7+r1qeCV2rLx M2w+A7bdVEWhLR8RfB0XdQyQpiNob9/E49668l2qjV/weJupB170HDhn7DohiD98zzZI dk8ztUggOTacc3nrwRs/ZpbE/GCmQb8yUCy71UX8TLnCM7jLTKtqz8Pus7jBnSQeMK05 PaEI9XMsRSWquNzxcPnE/mUH7UKSD+gsPtM84RsnfA2M3VXTUyLIjfxtswk4ejB7+qDN iCbZ6h+mKAwDuSZtrqZ43BXQCf3ZPwMQBOIgfkGX6soF+Exn+aCfh5xhT7GNtFcWL/BQ mumA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723191156; x=1723795956; 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=2CJ7nkkJAHhtFWGqFm7LmVNcwlwW3z2wjacsiudUdSo=; b=Ed+cWDLZOq5LqKK5dYkAhQ583El6/Km9U85RWKBKAP/msQtsKZqJMhFlEbyT/GyR3j 9V7OudlC++wWm1nsWVh1kxX2g38hfTE44WJmdOSjo7/9JyiyFbSK9ztv2uLl5qOAQ0Nr mQvWqGn0g0n99HRkxaRVmSRv2g2Rp3UpMxhQcHrfXQDAFGPOp71ys1kkStXDNp9yIb8d ac0PuqXIj8IMlVTs4KvEdoY+bvlBvkCKvQlQFRmPcByGIze/AZ8RBHL42akyEvjkVYhM 5ugsaT34/zcI3qxr+lxR0Rg+AqoM9QBDXEpZ9E/gWotIpudxS55/JqDsvkEvcjHhPYa6 Rgwg== X-Gm-Message-State: AOJu0Ywz5tf4OJ5+WSEOAE7tZEo/cV/9/lGDB5NYeDvK4giMi9MVizRD GMflz6WdNfPaKRNTwNAAsvHl6KFvmaisTmZIS8DSXIQekmait7eB/aUzy66bai2r+VDeLYHmf/+ Ah8MbtZbnaVIcyLtONxmk1Wo0HisQduLvgTbrQ9PQRE9lwHzU8pshvwKncLdLyJH/zA2biTUiFF WgTCYBs/+DlVvjXh1JopjvqBHeAXskulDUht1jqA== X-Google-Smtp-Source: AGHT+IHvBc2mYOzWaVnAIfrPrRNEP0GGagedQ+/D+izHmUwY4IeSMc6aB0FtFdv7wm4Qh5sN9wjKzA== X-Received: by 2002:a17:902:e74a:b0:1fb:8c35:6022 with SMTP id d9443c01a7336-200ae4bad13mr6534465ad.4.1723191156184; Fri, 09 Aug 2024 01:12:36 -0700 (PDT) Received: from sw07.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff592b6cfbsm136391185ad.306.2024.08.09.01.12.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 01:12:35 -0700 (PDT) From: Tommy Wu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: frank.chang@sifive.com, palmer@dabbelt.com, alistair.francis@wdc.com, alistair23@gmail.com, bin.meng@windriver.com, liweiwei@iscas.ac.cn, dbarboza@ventanamicro.com, Tommy Wu Subject: [PATCH v5 2/5] target/riscv: Handle Smrnmi interrupt and exception. Date: Fri, 9 Aug 2024 01:12:24 -0700 Message-Id: <20240809081227.1588508-3-tommy.wu@sifive.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240809081227.1588508-1-tommy.wu@sifive.com> References: <20240809081227.1588508-1-tommy.wu@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::635; envelope-from=tommy.wu@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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 | 18 ++++++++ include/hw/riscv/riscv_hart.h | 4 ++ target/riscv/cpu.c | 11 +++++ target/riscv/cpu.h | 6 +++ target/riscv/cpu_bits.h | 12 ++++++ target/riscv/cpu_helper.c | 77 +++++++++++++++++++++++++++++++++-- 6 files changed, 125 insertions(+), 3 deletions(-) diff --git a/hw/riscv/riscv_hart.c b/hw/riscv/riscv_hart.c index 613ea2aaa0..b7d73f7a82 100644 --- a/hw/riscv/riscv_hart.c +++ b/hw/riscv/riscv_hart.c @@ -33,6 +33,12 @@ static Property riscv_harts_props[] = { DEFINE_PROP_STRING("cpu-type", RISCVHartArrayState, cpu_type), DEFINE_PROP_UINT64("resetvec", RISCVHartArrayState, resetvec, DEFAULT_RSTVEC), + 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 +53,18 @@ 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 (s->rnmi_irqvec) { + qdev_prop_set_uint64(DEVICE(&s->harts[idx]), + "rnmi-interrupt-vector", + s->rnmi_irqvec[idx]); + } + if (s->rnmi_excpvec) { + qdev_prop_set_uint64(DEVICE(&s->harts[idx]), + "rnmi-exception-vector", + s->rnmi_excpvec[idx]); + } + } 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 a90808a3ba..2f64b3df22 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -1309,6 +1309,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) @@ -1332,6 +1337,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); @@ -2681,6 +2688,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 1619c3acb6..d8ad04ec31 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -472,6 +472,11 @@ struct CPUArchState { uint64_t kvm_timer_state; uint64_t kvm_timer_frequency; #endif /* CONFIG_KVM */ + + /* RNMI */ + target_ulong rnmip; + uint64_t rnmi_irqvec; + uint64_t rnmi_excpvec; }; /* @@ -568,6 +573,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 32b068f18a..e14b654c35 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -662,6 +662,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 */ @@ -711,6 +717,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) @@ -942,6 +951,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 395a1d9140..f1d464a554 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -434,6 +434,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; @@ -496,7 +508,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); @@ -619,6 +633,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; @@ -1654,6 +1692,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) bool virt = env->virt_enabled; bool write_gva = false; uint64_t s; + int mode; /* * cs->exception is 32-bits wide unlike mcause which is XLEN-bits wide @@ -1670,6 +1709,20 @@ void riscv_cpu_do_interrupt(CPUState *cs) target_ulong tinst = 0; target_ulong htval = 0; target_ulong mtval2 = 0; + bool nmi_execp = 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; + riscv_cpu_set_mode(env, PRV_M, virt); + return; + } if (!async) { /* set tval to badaddr for traps with address information */ @@ -1755,8 +1808,20 @@ 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 the hart encounters an exception while executing in M-mode, + * with the mnstatus.NMIE bit clear, the program counter is set to + * the RNMI exception trap handler address. + */ + nmi_execp = cpu->cfg.ext_smrnmi && + !get_field(env->mnstatus, MNSTATUS_NMIE) && + !async && + mode == PRV_M; + + if (mode == PRV_S) { /* handle the trap in S-mode */ if (riscv_has_ext(env, RVH)) { uint64_t hdeleg = async ? env->hideleg : env->hedeleg; @@ -1834,6 +1899,12 @@ void riscv_cpu_do_interrupt(CPUState *cs) env->mtinst = tinst; env->pc = (env->mtvec >> 2 << 2) + ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0); + if (cpu->cfg.ext_smrnmi && nmi_execp) { + 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 Aug 9 08:12:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tommy Wu X-Patchwork-Id: 1970843 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=DzAJPKYU; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WgGrq6scnz1ybS for ; Fri, 9 Aug 2024 18:13:46 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1scKjr-0005lf-Qf; Fri, 09 Aug 2024 04:12:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1scKjq-0005gp-Kc for qemu-devel@nongnu.org; Fri, 09 Aug 2024 04:12:42 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1scKjo-0006nH-9G for qemu-devel@nongnu.org; Fri, 09 Aug 2024 04:12:42 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1fd9e6189d5so17014615ad.3 for ; Fri, 09 Aug 2024 01:12:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1723191158; x=1723795958; 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=1oI6nGeEdG0xhvDYynLlNJXm9hUsd9zbhDLS15UCJSI=; b=DzAJPKYUZUrwBHHrtHwQhPMR4u8FhUturkqRmPNlXqQJGnR+jc6EKpW5N0Ei1WavaH 593Hu44UiTKwjfTshIafK1nlIbqMXjLVOdsWIduyLzVQjaLX10FKLP/azOHW81FsTMjo B7QtByV0pe2O8SAbQbdvip+h/U7vdGaddeS10EtuJIzFQZeTB2bcnGTuc7WTQF9z+oii +/gUea9bdLbs8Vody3rMs+JJAWZTOSpXdazPcPqaX1zrfHOUbfBe08uHuQhtzZ4QuuxL i3uN5pRWPap/nK7U57XBGM8u2bez4ZlYcqJyGCzTFzBaeiXd62HsLenv5B1fyRRueKNp 3oNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723191158; x=1723795958; 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=1oI6nGeEdG0xhvDYynLlNJXm9hUsd9zbhDLS15UCJSI=; b=NyAYRGQg5yNVbhjnWDuEhKyFI1pscmMKGGbkiRPt+jh0X+Sd7HDRoxQVxKMRnIWY+u U3mg6BKpnviDedRMxbFPdl/FNuTNY8VjODtLgO2i9b5dTKDeEpdQsmQryIAWBD59KO6+ 7W5jCxuRFuLrT6hGkWAUKFBsMW8naTUxB5moEmKmGSbv4UEoGLmCzt0GXCnffiwX0Zz3 kZr9YvMmh4nHc5o2IpbMsAZvWjgjU1IRrZKgqqPNQAc/5YpdpMWPUsqyCIQO/WKVimRF dI5yPadKcSae5E03AiHaWgzH7+LoqgOmu4Lk8wY3IXvuchwu4j5vVAfWJHliC9qedE1h bo9Q== X-Gm-Message-State: AOJu0YzNjBXPfCSjquJvcMVCEqG8NGtznCwtp7mf/Gkwsc5+p93g7pZ1 Yd/Os237XmWcR423HhV2mlfkq+iUP8jdR99Io4K8yntLAnw4ifwV1FqnaKC5OwBr0q38SQzE03m 3zibP5Gxi2ICzfO6DTzvt7lzp/aPbe/iRc66LdysNjhDxA61dlNGAkYPnWllmKwBMZfdxw+7i72 GKGNcJjvBTJPor3bC40qsG7rEa+CVYb7gn0JKcKQ== X-Google-Smtp-Source: AGHT+IEvWikipzIgptNoqiWFRjklKZRsP52KR1OSpqHYTmgAx82bX/n29/BzbehKdbeTWqtLuHiLkQ== X-Received: by 2002:a17:902:e845:b0:1fd:9e88:e4b7 with SMTP id d9443c01a7336-200ae4dbc2bmr6875895ad.12.1723191158135; Fri, 09 Aug 2024 01:12:38 -0700 (PDT) Received: from sw07.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff592b6cfbsm136391185ad.306.2024.08.09.01.12.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 01:12:37 -0700 (PDT) From: Tommy Wu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: frank.chang@sifive.com, palmer@dabbelt.com, alistair.francis@wdc.com, alistair23@gmail.com, bin.meng@windriver.com, liweiwei@iscas.ac.cn, dbarboza@ventanamicro.com, Tommy Wu Subject: [PATCH v5 3/5] target/riscv: Add Smrnmi CSRs. Date: Fri, 9 Aug 2024 01:12:25 -0700 Message-Id: <20240809081227.1588508-4-tommy.wu@sifive.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240809081227.1588508-1-tommy.wu@sifive.com> References: <20240809081227.1588508-1-tommy.wu@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=tommy.wu@sifive.com; helo=mail-pl1-x632.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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 | 4 ++ target/riscv/cpu_bits.h | 11 ++++++ target/riscv/csr.c | 82 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 2f64b3df22..98e6940e93 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -1024,6 +1024,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 d8ad04ec31..a84e719d3f 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -474,6 +474,10 @@ struct CPUArchState { #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; uint64_t rnmi_irqvec; uint64_t rnmi_excpvec; diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index e14b654c35..da1723496c 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -350,6 +350,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 @@ -627,6 +633,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 ea3560342c..e5de72453c 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -578,6 +578,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) @@ -4566,6 +4577,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 */ @@ -5070,6 +5142,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 Aug 9 08:12:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tommy Wu X-Patchwork-Id: 1970848 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=nlrU6lLU; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WgGsd0WLpz1ybS for ; Fri, 9 Aug 2024 18:14:29 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1scKjs-0005p9-NU; Fri, 09 Aug 2024 04:12:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1scKjr-0005mQ-VS for qemu-devel@nongnu.org; Fri, 09 Aug 2024 04:12:43 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1scKjq-0006o8-6N for qemu-devel@nongnu.org; Fri, 09 Aug 2024 04:12:43 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1fc52394c92so17668745ad.1 for ; Fri, 09 Aug 2024 01:12:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1723191160; x=1723795960; 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=Lnyy3iG1ppUhes/NXnsprwtoY4OtwR9FbGvONHrEws4=; b=nlrU6lLUynn10G5MHcr8kOnc/xCk6hqT67bgo3/FIrJhRYWxRPtIq3YAnQn3Mi7OAy X3aZsMEfwYxzvB2qaHSSyfYZhPC3TJnjWaujqX0Y8y9OfL3VX+CjnS3qS5drO9mIb6PD 5wc3yRDibWUnIfO2hWpVt0+uJv4pFrZwyyHdyxAuloslkiriR7TCwHpEVB13PrPUqX/r P8ZILJKmRBBUMNkbxVdQU6YpV8BIKFqqhMa8BP7rjHpc2rWbQ0sdmEslRc2agRiKGIpX b8rD3KVE6RRP9PrbJOJNOscks5qqHNlKDhEg/idEwfM9JeRtBiEtarQ8btD8n72Fvg8h fZFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723191160; x=1723795960; 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=Lnyy3iG1ppUhes/NXnsprwtoY4OtwR9FbGvONHrEws4=; b=JtFvds4AbAvZi3xMNeQ/Gu91cjqXObgYoQnm1UakvJ6QVI4r6Jqkuz8joQxSLWtIQb RjNyAi9wCP1YbgLqeF6HSXWeWAJRcZ2Z7Nh01srxZeujt9w9bgwfx1WB6aoav+hk1gDt GlJ+RLa4EN5hYldjZIFB5aNy1r5E9OG5Fr5pZKUaSw3ubJDI5BP+egsEbbcU1CRo1u6H 3+WU1CmH/gcm8bWvc8i0OlMFliDSUVqarXljTssIcrsZ7NDBse21yNgvIYjuFrQKQZD7 y2o2tpoT5V5ilwNlScfv1uj8o9aKj7IntCCyXDsGvqFzkmznH82ilcvHoLqxVQJXnf7F Qyhg== X-Gm-Message-State: AOJu0Yw+IReHab9ZRpPT8FPGTkdjQdyKnBBqHWKE/zhDA5hFqvvO96vP QLL271t4HOBN/oDNvRcvByR4FlOdJl1p1sN35Z31F3q72PGI3fZtj2XKZF6NuOjLsA5kFNclsU/ Tdj9xR1oMDTFPRJZNoPow9KhRRUJAyYSMwGB9gpDdo2iC2tyUC7b1iV5C2hEnmC9GouwazF+S7H OfUYRcX6Vq0StyMHhwYMPrB9N3C698PfNN/K5aBw== X-Google-Smtp-Source: AGHT+IFBgG0clyz7afN9hi4gW03bwRKThMZ1JXg/HKqNLrxWgOUMocZsinWjmK5J27VcOwe2nM9XSQ== X-Received: by 2002:a17:902:d4c8:b0:1fb:9280:c970 with SMTP id d9443c01a7336-200ae4db87fmr6802285ad.7.1723191160068; Fri, 09 Aug 2024 01:12:40 -0700 (PDT) Received: from sw07.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff592b6cfbsm136391185ad.306.2024.08.09.01.12.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 01:12:39 -0700 (PDT) From: Tommy Wu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: frank.chang@sifive.com, palmer@dabbelt.com, alistair.francis@wdc.com, alistair23@gmail.com, bin.meng@windriver.com, liweiwei@iscas.ac.cn, dbarboza@ventanamicro.com, Tommy Wu Subject: [PATCH v5 4/5] target/riscv: Add Smrnmi mnret instruction. Date: Fri, 9 Aug 2024 01:12:26 -0700 Message-Id: <20240809081227.1588508-5-tommy.wu@sifive.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240809081227.1588508-1-tommy.wu@sifive.com> References: <20240809081227.1588508-1-tommy.wu@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=tommy.wu@sifive.com; helo=mail-pl1-x634.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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 | 12 +++++ target/riscv/op_helper.c | 46 +++++++++++++++++++ 4 files changed, 62 insertions(+) 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 c45b8fa1d8..d320631e8c 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 auipc .................... ..... 0010111 @u diff --git a/target/riscv/insn_trans/trans_privileged.c.inc b/target/riscv/insn_trans/trans_privileged.c.inc index bc5263a4e0..06bc20dda4 100644 --- a/target/riscv/insn_trans/trans_privileged.c.inc +++ b/target/riscv/insn_trans/trans_privileged.c.inc @@ -106,6 +106,18 @@ static bool trans_mret(DisasContext *ctx, arg_mret *a) #endif } +static bool trans_mnret(DisasContext *ctx, arg_mnret *a) +{ +#ifndef CONFIG_USER_ONLY + 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 25a5263573..3e26392e65 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -353,6 +353,52 @@ target_ulong helper_mret(CPURISCVState *env) return retpc; } +target_ulong helper_mnret(CPURISCVState *env) +{ + if (!riscv_cpu_cfg(env)->ext_smrnmi) { + /* RNMI feature is not presented. */ + riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); + } + + if (!(env->priv >= PRV_M)) { + riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); + } + + /* Get return PC from mnepc CSR. */ + target_ulong retpc = env->mnepc; + if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) { + riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC()); + } + + /* Get previous privilege level from mnstatus CSR. */ + target_ulong prev_priv = get_field(env->mnstatus, MNSTATUS_MNPP); + + if (riscv_cpu_cfg(env)->pmp && + !pmp_get_num_rules(env) && (prev_priv != PRV_M)) { + riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); + } + + target_ulong 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 Aug 9 08:12:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tommy Wu X-Patchwork-Id: 1970846 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=BBGlJkyI; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WgGs83Ghtz1ybS for ; Fri, 9 Aug 2024 18:14:04 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1scKjx-00061r-Fb; Fri, 09 Aug 2024 04:12:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1scKju-0005vo-BG for qemu-devel@nongnu.org; Fri, 09 Aug 2024 04:12:46 -0400 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1scKjs-0006oz-Op for qemu-devel@nongnu.org; Fri, 09 Aug 2024 04:12:46 -0400 Received: by mail-pg1-x52c.google.com with SMTP id 41be03b00d2f7-7a115c427f1so1307903a12.0 for ; Fri, 09 Aug 2024 01:12:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1723191162; x=1723795962; 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=riPnYasURW77Dz2ZwVlvtuzpt5ZqhwUi415BFr5uQMs=; b=BBGlJkyIvFc7cnkwsxndlrhqvwzFDnenCliYCb+Ypt3H8ZX5oEy8j8OhfwsGFSGPiT rUo3NCl9bYEm+E+9RaenZOF24Lak+yCiMGnz+a2GuWgy/ceGr5WX1z4nXDP9pBABCDIS Ep9dCU0Eev00YAx6zECSa86yact/noFKIbE7Pos7saOrpT8yZ7bLkie83wMWYPC+/gjW Nn9paJkRtTbvYcAU+8bd6ZY77IXpTZrqS605SgUGdTt5EMmlyw/WhzuYzPHT8JURn375 5/GQ/l6o8aF+0J6MqBy/wVFcvQ5D2lNbj58BDgog3XWk5F6IPAuJxt77UTKPb+ap7OIz Q0lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723191162; x=1723795962; 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=riPnYasURW77Dz2ZwVlvtuzpt5ZqhwUi415BFr5uQMs=; b=fCaI7y9yhX84fUiqj1sJ08p941fXFA19HCPKGUBQ2CmRE+C26u/CA3QOyUb63mDMWj 6l+QglMoERGxPUMtOqsS4Qhc/FYh2YtVRCfGEkv1m4hlY+krhu2tUs2QcamYIl/LJTxP yLBiFXa2oiNBGiG4ST+euF2ry+IhPegbQr2GLPSDdREp5FpKuE6sKaUpgn5JW4fsdHP2 zvm1LQIWRUpdFMDJlbpX5fyr2519N91qIFYMmXWtUwsl7b2Jkh+s9pF/vt3fI4np7PFo YuVC1xsYMN3Unka1SHF7vvx6uE/QxQcfmSkNO8aSl41+44IB7fzXRD6g7cgTAgoJWWZT 7WXQ== X-Gm-Message-State: AOJu0YwdLlfv4R0uGNsIJ0gz/rsSQkkobUh7JOEiChXnE9fMI4sFfR18 xIQYrEqQY1pHCTkXrThP+a7qyXB1oSM6Sk+eeR7KtzC4xtSOARfFmuqCBel5kfu8QwAf8NbX3BE ourFEOKU6PQmVbz4fpsJrMZW+FXtpoSNCU5pnVzFAKOv88oahhU9cht0CsG/8gSf89iwyKAHWoW NCQLIMcAfd7P2SwU0UfpER949Pl7JEbYsf0pNi4Q== X-Google-Smtp-Source: AGHT+IFKKPAYoETCMuKCeXr2CwdLER/abdxO7bgP/hZ5dtBM9pHST60eB95q8yv0siAjyk4XZPhwYA== X-Received: by 2002:a17:902:fb10:b0:1fd:96e1:802a with SMTP id d9443c01a7336-200ae60cf79mr6505065ad.59.1723191162520; Fri, 09 Aug 2024 01:12:42 -0700 (PDT) Received: from sw07.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff592b6cfbsm136391185ad.306.2024.08.09.01.12.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 01:12:42 -0700 (PDT) From: Tommy Wu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: frank.chang@sifive.com, palmer@dabbelt.com, alistair.francis@wdc.com, alistair23@gmail.com, bin.meng@windriver.com, liweiwei@iscas.ac.cn, dbarboza@ventanamicro.com, Tommy Wu Subject: [PATCH v5 5/5] target/riscv: Add Smrnmi cpu extension. Date: Fri, 9 Aug 2024 01:12:27 -0700 Message-Id: <20240809081227.1588508-6-tommy.wu@sifive.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240809081227.1588508-1-tommy.wu@sifive.com> References: <20240809081227.1588508-1-tommy.wu@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52c; envelope-from=tommy.wu@sifive.com; helo=mail-pg1-x52c.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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 98e6940e93..7ee7b9c4ee 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -184,6 +184,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), @@ -1514,6 +1515,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),