From patchwork Mon Sep 2 07:13:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tommy Wu X-Patchwork-Id: 13786847 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E1444CA0ED3 for ; Mon, 2 Sep 2024 07:15:38 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sl1GX-0008Gs-I2; Mon, 02 Sep 2024 03:14:21 -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 1sl1GU-00089z-Sj for qemu-devel@nongnu.org; Mon, 02 Sep 2024 03:14:18 -0400 Received: from mail-io1-xd2f.google.com ([2607:f8b0:4864:20::d2f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sl1GS-0003hk-UM for qemu-devel@nongnu.org; Mon, 02 Sep 2024 03:14:18 -0400 Received: by mail-io1-xd2f.google.com with SMTP id ca18e2360f4ac-82a20593e53so151605739f.2 for ; Mon, 02 Sep 2024 00:14:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1725261255; x=1725866055; 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=CdpxHGAujFiCeLIq9jDNKBjdMMvfgjRI0rVz+c1fYDY=; b=AEcoJz6qTfnzN1LisVaShOlKxTkO+3Yimqi24G3ftebmMyNakNugOVCBzKuP2qeRBy 1tPJuWNZXodbYeUU6hQ7iaMi5WNk57M5hCwRPRTh7o+E38bSY7GZ2M77oFOGkxydxEVe sIRAxUdhJl5NAITkzF0a25QsnFnS1Zn+E17z6A7CbB0lebIf/8BziMi+DtTJy47Tfoce nRXOvBVas8GyRJDmnr29l7imQimFJlkxJb4v1+iSTKa7Ghc1G0MtnpEFUdeYju5XquhV 1XNbZ6sQ6lN6ZhpjfgPlJUv9LrZkDoOdeTac5AWXui41azghv8PXXrrEF6+IhzPhs7Jd J/WQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725261255; x=1725866055; 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=CdpxHGAujFiCeLIq9jDNKBjdMMvfgjRI0rVz+c1fYDY=; b=bKF/+jk9RSb8IlhkmrCKOO+R+j4CFA7igoIvlYTMzUET5rCRg1z8Vg45G5rOmGzwY0 Xe8SEiJAXlICJ6BoBOkaq+RP6fT90/O0ArMLMXgTKpma+OEr3Qikj3WgBveq1Xa8sse4 dZUeC3k1jpwodHkKutmkcU+BHu1+U6mC6o/VRbKIe6oMlwd/vETsIflil8U9m9IQpEGY +f3gUezyVQ/rQaKyWMW/nbiJCIYVyu9dijIygyswbLktVOWgyyIiiEgpiZo9+bTTVbmy 8178Th8C42E0qI2rdSnIh+y9NBnHjmroyQx3vnsUaPInwh17hf/4cpGZD15vWBXAu9CB faVQ== X-Gm-Message-State: AOJu0YxVVUHUOuvwLHurJhZibQk38paixB9MbNkYGxvbUtG7f1R9eNDK Rbhg2iCI4x5k3ZO5dOSuaG3acNDbL+4MK6fCxJnHckrFcd89wzrtMYvBRtLD0wypIyr+naCcTjq yc9MXEJaZtN4NWz5YHFDOWULyVW+yrAG6RijWuksYqnK7cbtNTqn7LOQIxF0rhkBfbuCgK4AZuZ qqlcQwFqoS13ssTQRomjLjmfP84m9SQn4wqgwncg== X-Google-Smtp-Source: AGHT+IEIVFy3tOcTRngNnmHu/lk63dW1PlYnAmjR9tHhfs6muJKuhhw4a2rH12Ma47kEY518P9IcUg== X-Received: by 2002:a05:6e02:1d17:b0:39f:5521:2cd4 with SMTP id e9e14a558f8ab-39f55213075mr46579255ab.26.1725261254939; Mon, 02 Sep 2024 00:14:14 -0700 (PDT) Received: from sw07.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7d22e743252sm6793331a12.15.2024.09.02.00.14.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2024 00:14:14 -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, ajones@ventanamicro.com, Tommy Wu Subject: [PATCH v6 1/5] target/riscv: Add `ext_smrnmi` in the RISCVCPUConfig. Date: Mon, 2 Sep 2024 00:13:54 -0700 Message-Id: <20240902071358.1061693-2-tommy.wu@sifive.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240902071358.1061693-1-tommy.wu@sifive.com> References: <20240902071358.1061693-1-tommy.wu@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::d2f; envelope-from=tommy.wu@sifive.com; helo=mail-io1-xd2f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.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 Mon Sep 2 07:13:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tommy Wu X-Patchwork-Id: 13786845 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3248CCA0ED3 for ; Mon, 2 Sep 2024 07:15:16 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sl1Ga-0008T7-Es; Mon, 02 Sep 2024 03:14:25 -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 1sl1GX-0008Ij-RM for qemu-devel@nongnu.org; Mon, 02 Sep 2024 03:14:21 -0400 Received: from mail-il1-x135.google.com ([2607:f8b0:4864:20::135]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sl1GV-0003iS-M5 for qemu-devel@nongnu.org; Mon, 02 Sep 2024 03:14:21 -0400 Received: by mail-il1-x135.google.com with SMTP id e9e14a558f8ab-39d30f0f831so14095185ab.0 for ; Mon, 02 Sep 2024 00:14:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1725261258; x=1725866058; 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=+H4+/Iz5PzT+lklA4TibKNEKH7JqTpy04zdQSsVSUtM=; b=ftTPVbAj01K1YUNlJaYTXWhDCIvXBZ5mUzdGNA/6MBUAbUJ1Zc5jDqWfI23jhSN9Dx LV6VKH6fR5E+EQj1Zg4PTFcdXoxCLIdAkUjeH9fW1rCxqo5g+13Uf98Lhoiy0YmRXhUn elvOuIcmZqUn43vHwNGNTrCictrqCWqDVF8CTltSVe0lr9qSaTwJeT8RA4spNlhML0md nYrCAzNDbjZ19pQnnWcFpEzXEX0t4n3uH6LF1RRbhpJUEpTmKa8oTUGLrga5na0QDH5w 76B/bZJ8MEsI0tEk6YBaT9fHFeiivRw7/pvbbI2jbtSfUv2yKAwoN9T/hJkj7F7y78e/ 2YWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725261258; x=1725866058; 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=+H4+/Iz5PzT+lklA4TibKNEKH7JqTpy04zdQSsVSUtM=; b=uC8rhuTUfm2baXgNk+5x2dEG0wmRu/H6OtGhIAf3frrzDiX6pj/qUTqh7/rpiC4FaX C03qa2txSK+EPbz3kBvJVpTnShiMSB94rkuo3BSteUlRSdlDgc0FzDkX0OOjFTlFxEFV BHOkrlwx8kT2+xLDWX73dcWdu7j/yvO4hygtltvVA+ljSNx8lSBSM1Ze/sg46j5zT4Kf hB6eZZcWxDRSlVQiPg0I5raWiAQqd3KN98nHZ8f9212f748tinxh2a/4YWiuOFeMTmL3 KGosKYlt4/l5DbbfPRBHoDvrQZIojfGz8eEIprOnnkOJDygVmmUXoKLogGtsyecbKNtX G0Gg== X-Gm-Message-State: AOJu0Yw35WHNmzJpmpzXaVz5T3VeQj/GwDNP08iDX+asuuGQRc+Xi1Dz Adp//M8APA3RTRVLuW6C26L64QvHLLTP7Ff/iqF69jXC7rqQlADS6k8BZtpkMEtp1n7RKNAzBzN F7GUhhbJSeqNJJcB4mjrjdDiL7Wh5CbW/BwPjuy9VzBOzhV+AdcKV1I0368db0Xg0YBeXfqkcRR 1R37LP7r+MJ6zuSQWTkMfCNzY498Ft46XY5svMlQ== X-Google-Smtp-Source: AGHT+IE8g006CuyDf/J/w0u2GEkGYSVwVE4UFQU60S5qVc9v5GryUHnnEBJVrvx58sqKaB5lWXpERQ== X-Received: by 2002:a05:6e02:12cf:b0:39b:32f6:5e90 with SMTP id e9e14a558f8ab-39f4e0d41e1mr79453825ab.15.1725261257322; Mon, 02 Sep 2024 00:14:17 -0700 (PDT) Received: from sw07.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7d22e743252sm6793331a12.15.2024.09.02.00.14.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2024 00:14:16 -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, ajones@ventanamicro.com, Tommy Wu Subject: [PATCH v6 2/5] target/riscv: Handle Smrnmi interrupt and exception. Date: Mon, 2 Sep 2024 00:13:55 -0700 Message-Id: <20240902071358.1061693-3-tommy.wu@sifive.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240902071358.1061693-1-tommy.wu@sifive.com> References: <20240902071358.1061693-1-tommy.wu@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::135; envelope-from=tommy.wu@sifive.com; helo=mail-il1-x135.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.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 | 80 ++++++++++++++++++++++++++++++++--- 6 files changed, 126 insertions(+), 5 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..9d0400035f 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; @@ -1832,8 +1897,13 @@ void riscv_cpu_do_interrupt(CPUState *cs) env->mtval = tval; env->mtval2 = mtval2; env->mtinst = tinst; - env->pc = (env->mtvec >> 2 << 2) + - ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0); + + 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 Mon Sep 2 07:13:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tommy Wu X-Patchwork-Id: 13786846 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id ACCA9CD13CF for ; Mon, 2 Sep 2024 07:15:22 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sl1Gb-00005e-W0; Mon, 02 Sep 2024 03:14:26 -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 1sl1GZ-0008Ne-2t for qemu-devel@nongnu.org; Mon, 02 Sep 2024 03:14:23 -0400 Received: from mail-oi1-x231.google.com ([2607:f8b0:4864:20::231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sl1GX-0003il-7I for qemu-devel@nongnu.org; Mon, 02 Sep 2024 03:14:22 -0400 Received: by mail-oi1-x231.google.com with SMTP id 5614622812f47-3df13906bcdso1554487b6e.0 for ; Mon, 02 Sep 2024 00:14:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1725261260; x=1725866060; 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=m1nHOFLTYiSfN+EKbsGMK0zHek6c+isX39zy1shDluu20WycvFieoNgZGBuA7RigQB K9fwo3SGOzP269NcI3+eLQUUW20Klg+Rj+I4iAizl+xJPZ++xPP6G27FLnuVIk/pPsWL 5D+PBzFqI3Ulu89ccO7R27MScUPTt/PCHBEfPuGePzB5VeembHLlyZ/gyZyZ6fWuycSi cWs0UgOn88YtG8Jf4dotEZpbd6w9CCUBTyTOoTr2Y9l2n9VQtCEjKxjtvLQclbQU7HBk kA9vrHvWmCQZbKGVubayR7bDtl2iVlOlm/zZGFhfXgiKJSZHnWexpjhZ9S33LBON4LGZ /NJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725261260; x=1725866060; 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=aKdNg8ZSRgaTTtMxsvm/uoKDXr/tmPhczb8NTiGiKbKkZLnrWJsBFT8N7OErxiYQzI WpeNYao7JqC1dHZx+He7Qhc73oArcfYFxWRbK1/vd6CUkNO8NoC42DwW2HtPTapFLLvf 4majppzQNLUH1rtKkPQIsc/cs+97oSwo/pkSP/i3Q9mIOgHHAwnC2DfTZDCUWAtyweWD 36ryPjvr/wu7zsQf9d6dMxM8A7srFlvaSkBiGMPbBLT/3hyWmptYnyHQYbiPxstGhBNU LcgCNrjiTas3WvcuG+VfjyZ4+blREEumfH3oFUcx39rqUHwxBk3KCphgpwXHhjSUb9QS oGCQ== X-Gm-Message-State: AOJu0YwTNgjEk37M3bK3/JzHutdPej8fAU/CPP3LpouJADj/rJ/yhdMi v7UwZYQis847N2PbFsZ0tUsp9BxXLMjugiZgLEJibjH5zN0BycCgkH2gs2RLd2HkAIS03OpGM8K 5yLiZf2BBPkqAoC2RflD+OIJCR1x8UHfDn5XLXVEtHgGHcxZyNJuqz+lfMWMVWOYD+2ParVlgAz /yB8Ud4lzh9EOmJBp2Kf+vKCLdzcDN6W1e11nnpw== X-Google-Smtp-Source: AGHT+IHbLe+Tyxmoyf/TOS2s+p/qYkWCK8X9IEp27gwNP5h5aq8imhVpkPM99N3C8hj8mY5g7WgiuQ== X-Received: by 2002:a05:6808:2287:b0:3dc:4223:898a with SMTP id 5614622812f47-3df1b72c270mr7419478b6e.14.1725261259450; Mon, 02 Sep 2024 00:14:19 -0700 (PDT) Received: from sw07.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7d22e743252sm6793331a12.15.2024.09.02.00.14.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2024 00:14:19 -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, ajones@ventanamicro.com, Tommy Wu Subject: [PATCH v6 3/5] target/riscv: Add Smrnmi CSRs. Date: Mon, 2 Sep 2024 00:13:56 -0700 Message-Id: <20240902071358.1061693-4-tommy.wu@sifive.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240902071358.1061693-1-tommy.wu@sifive.com> References: <20240902071358.1061693-1-tommy.wu@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::231; envelope-from=tommy.wu@sifive.com; helo=mail-oi1-x231.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.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 Mon Sep 2 07:13:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tommy Wu X-Patchwork-Id: 13786848 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D06FACD13CF for ; Mon, 2 Sep 2024 07:15:56 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sl1Gn-0000sc-1b; Mon, 02 Sep 2024 03:14:37 -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 1sl1Gl-0000mX-Br for qemu-devel@nongnu.org; Mon, 02 Sep 2024 03:14:35 -0400 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sl1Ga-0003jR-4V for qemu-devel@nongnu.org; Mon, 02 Sep 2024 03:14:35 -0400 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-714114be925so3175924b3a.2 for ; Mon, 02 Sep 2024 00:14:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1725261262; x=1725866062; 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=C9Rj5XH4OpfmEHaIVToFPnkTlpcGbOtFPfKlICf0VvA=; b=GCC0NfGgir39+GR2eYwV98iGfYLn4eg6DB+81EUf0Dbpm5sevzob8tzN8Gvm/h5Vll z7Trtbj2FrZ9hJwDiG+NpsYNuepHG0eETRGC0epVaH6anrhgBh/aS2yqcJ+viPmp1Da/ j8arUrg7Q6n5HFdMxtL1IFWLA0VOdQ3sOcoF4Q8f2Mplfc9LgKGgWxUuN02ZpJo3LsPQ yJ63GzYA4VKJjHJqOV4gvtKv15n5maqc0btv4okARA00xN3RbY+uvpUtKWZqbJuDBURO xtRcg4vdLQR5MHgpolAwaJbfx0gwekLNZlvca3iperoZ06UI6Eyjb38x9wnVxxwIUcJd kp1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725261262; x=1725866062; 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=C9Rj5XH4OpfmEHaIVToFPnkTlpcGbOtFPfKlICf0VvA=; b=IZ3zUG5s2xmWx8egArnMdwpOYlQYFLkyKBUWKhWb/VhVHLp8p7Kaag3SdZv4Ieqlat 4MH7qDjT/HZEwlA6dKBr9KV7qZzYH3Cz0EqBQ1EetNsXmSfwOmkR6/Kw8Kv8VEkcsqKJ ezMM33bxlmCITYb8/Wb96h+oho3KcHhjIDEpm6fakYSeIc1+9ycZhXkXedY16QFDvbGN TzOEgvdQqjVqbq4KdFCpNuaQE1OFuBRJ36/ujec4NHU5NqfDRL8YK+EKz+ZDu2xmLwqX eljX+kfu+NAUF9Sb9eu20AuzgOLonVYpPPPW/Xr17LhOkUYxlTOFvnm1Je+Im41VG4cY Dz/A== X-Gm-Message-State: AOJu0YxeAxLF13N3G/jdG+1sYfybaPRl22rH14CWDohZDgcfTTBaQYHt EKtnDaS6ur8SP0kBixqpgQj8ps+5H9o6FB6+aMCxF/e9MqR2cUWdUdzI1j4aM+pZNER5I7p5kJH t/D+eqbH8fxlAaGZlqlNgr6D2+r/JVkMbqLizVsNqRzgCTPeUVXWCXucEuPD4JJmGNgHCELAUO3 AmbbYLt6w5NRatqEX5Hc9ce6jc2g7+K1JQu2xywg== X-Google-Smtp-Source: AGHT+IFIrAwRhLB/vHV5Jqxb2nI6RY5kmzyM2JAgR4Y0JPLllrw8AYGlVF8CpU2YyUbOu21kCdcVUQ== X-Received: by 2002:a05:6a21:3489:b0:1c4:231e:8ee5 with SMTP id adf61e73a8af0-1cecf516945mr6729999637.29.1725261261513; Mon, 02 Sep 2024 00:14:21 -0700 (PDT) Received: from sw07.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7d22e743252sm6793331a12.15.2024.09.02.00.14.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2024 00:14:21 -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, ajones@ventanamicro.com, Tommy Wu Subject: [PATCH v6 4/5] target/riscv: Add Smrnmi mnret instruction. Date: Mon, 2 Sep 2024 00:13:57 -0700 Message-Id: <20240902071358.1061693-5-tommy.wu@sifive.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240902071358.1061693-1-tommy.wu@sifive.com> References: <20240902071358.1061693-1-tommy.wu@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::432; envelope-from=tommy.wu@sifive.com; helo=mail-pf1-x432.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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.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 | 49 +++++++++++++++++-- 4 files changed, 60 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 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..6895c7596b 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -312,24 +312,30 @@ target_ulong helper_sret(CPURISCVState *env) return retpc; } -target_ulong helper_mret(CPURISCVState *env) +static void check_ret_from_m_mode(CPURISCVState *env, target_ulong retpc, + target_ulong prev_priv) { if (!(env->priv >= PRV_M)) { riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); } - target_ulong retpc = env->mepc; if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) { riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC()); } - uint64_t mstatus = env->mstatus; - target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP); - if (riscv_cpu_cfg(env)->pmp && !pmp_get_num_rules(env) && (prev_priv != PRV_M)) { riscv_raise_exception(env, RISCV_EXCP_INST_ACCESS_FAULT, GETPC()); } +} + +target_ulong helper_mret(CPURISCVState *env) +{ + target_ulong retpc = env->mepc; + uint64_t mstatus = env->mstatus; + target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP); + + check_ret_from_m_mode(env, retpc, prev_priv); target_ulong prev_virt = get_field(env->mstatus, MSTATUS_MPV) && (prev_priv != PRV_M); @@ -353,6 +359,39 @@ 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()); + } + + target_ulong retpc = env->mnepc; + target_ulong prev_priv = get_field(env->mnstatus, MNSTATUS_MNPP); + + check_ret_from_m_mode(env, retpc, prev_priv); + + 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 Mon Sep 2 07:13:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tommy Wu X-Patchwork-Id: 13786849 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 79454CA0ED3 for ; Mon, 2 Sep 2024 07:16:06 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sl1Gf-0000My-4u; Mon, 02 Sep 2024 03:14:29 -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 1sl1Gd-0000FA-E1 for qemu-devel@nongnu.org; Mon, 02 Sep 2024 03:14:27 -0400 Received: from mail-ot1-x32e.google.com ([2607:f8b0:4864:20::32e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sl1Gb-0003jn-Nb for qemu-devel@nongnu.org; Mon, 02 Sep 2024 03:14:27 -0400 Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-70f794cd0e3so904884a34.2 for ; Mon, 02 Sep 2024 00:14:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1725261264; x=1725866064; 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=anjql5FnY67JEJxldqez59aBrRrUtw/FPrUkIDAq73qOdqVfTCfmx/2A2bpmgLBZas cX2XO9k45RPpqjsDfeBwCyDH7gKJamqVPTuRRYF3TeAqRvVeUqXF/vQQ6e7X4gxdxW7A KOIWbyUICPZxP+XfHp2WYU7CSnfgg1GMKXUUCqB6cHu7eYfdadKCaAV8fOkxkslK3lGE HMKe9mJVnDGw5/Af7peSVZzeMipXMw6pN1DHlnrgGJPlm46xKDtDMXl1jT+f0btrr1X7 quHBp8W8MW2B09CxSVP6omjcwbp1WoTCW5wiM7TsKv8kzRNqKZfyZk/M06hAE2rW3F3z mxjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725261264; x=1725866064; 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=oQIVWQwGnk2c1u8oc4jBSYU3Cx1LzxjdVkG86CFWU2G9vUHlL9tw/J/1T1665Ls+W+ BZCZw+sJDdXeGhGkLY6d+IFOCQlT2ehhgFSNhJ9jcYrfon0M9yGMEKsbAB5CZRwfhl+4 TQ0I3fy9B1R6lOCQTBmw87hMf2CymSWY1ZN9e5xtMmicjffxMzNheFmgI/5joy9ALLtz 1bCwyY9tkXiruD5KtH7p4zc7uQIILY+NqwZ+7V2Ka+MIU7ZY8ULDi0JaDDp7Tr/dAH5I I6IxVN8aDn0unhO9UCU2Oogro2Y96NFBvvpAiSe0vYCjIczY8pg7xtL2Nh+wCvAlz1lo UTMQ== X-Gm-Message-State: AOJu0Yyv6iotfn5Sxk0+1NkTLqyAzMaiglj6imfzjdHHsQKi0MfOJtPw v7Ylfrikn8Xmg0NtS0R2uqcPY60F//qdfqp1e3yZtwLMsdzVr8WyjV9LTSiypM6gzDkD6nIDR4X OJ28FZoa6F4adM5I93pH1ieX7osacnOYvp7DPCF6CGd84EOIsKROU6ULo0mbdL65RQolBROAuWT k9qk9zLuCn30Gf8PVI9dMYsA5vAxFWO29UPUw7HA== X-Google-Smtp-Source: AGHT+IH4tkb5Oexhex8sbALS925W99zRTLH9z06Uw6RGuEFR9ffyy37i4nulFobaPOnCbVvlKieyNQ== X-Received: by 2002:a05:6358:88d:b0:1b5:a034:4713 with SMTP id e5c5f4694b2df-1b7ef352898mr935257555d.0.1725261263712; Mon, 02 Sep 2024 00:14:23 -0700 (PDT) Received: from sw07.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7d22e743252sm6793331a12.15.2024.09.02.00.14.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2024 00:14:23 -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, ajones@ventanamicro.com, Tommy Wu Subject: [PATCH v6 5/5] target/riscv: Add Smrnmi cpu extension. Date: Mon, 2 Sep 2024 00:13:58 -0700 Message-Id: <20240902071358.1061693-6-tommy.wu@sifive.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240902071358.1061693-1-tommy.wu@sifive.com> References: <20240902071358.1061693-1-tommy.wu@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::32e; envelope-from=tommy.wu@sifive.com; helo=mail-ot1-x32e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.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),