From nobody Mon Feb 9 06:50:26 2026 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=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1768897509; cv=none; d=zohomail.com; s=zohoarc; b=IneuBlUDRg/4nxwW7fIeavAmW1emhEyEilUhoy3U0b1iagcUgFWiSrbTtA4BFDvxNjgqpkm2DwQ4IlYhkjzUWaRneF8o1GOU48hDyJ2qrlQ4z9ZMldcY7zsuzoHOaunR3CW8FPLYGMFeSCS0i7nL9ZnU3xpmBwSetmW1LE95tgU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1768897509; 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=i1r38n5/smozxAk3MfAzmduskm1/QEC2/wzUy2JGan4=; b=cv3hB81H5dMTK2g4km+3RJaVLtMqpluHc7l7dulFGRenGtnAQ9f+Nad2/x3b5jRRmydchj5EPu+5PTFjo6g7QvSvj+WXt2/ZqhLPie0bt7m5GLKeREhF8FXI9XH+MuJTruUMlnEi0lzEgKfP0Gp88Repu+niJM0GX2fgSyy3JvA= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1768897509137211.03353553306363; Tue, 20 Jan 2026 00:25:09 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vi72Q-0005VT-SX; Tue, 20 Jan 2026 03:24: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 1vi72O-0005QY-ME for qemu-devel@nongnu.org; Tue, 20 Jan 2026 03:24:32 -0500 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vi72M-00034V-Vh for qemu-devel@nongnu.org; Tue, 20 Jan 2026 03:24:32 -0500 Received: by mail-pg1-x541.google.com with SMTP id 41be03b00d2f7-c551edc745eso2158816a12.2 for ; Tue, 20 Jan 2026 00:24:30 -0800 (PST) Received: from ZEVORN-PC.bbrouter ([183.195.21.37]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-35274573a7esm4273592a91.11.2026.01.20.00.24.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jan 2026 00:24:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768897469; x=1769502269; 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=i1r38n5/smozxAk3MfAzmduskm1/QEC2/wzUy2JGan4=; b=QGR3kgRYmQZh7nGWbs5zjaxM3qBSv1e+c6ZSoEtiwvp3lUMqPO7Mp/TbpyMrH0mYtj vdC6b6hQa6JevoJ7MAcP7Paa9JImS4e+p/7gfJHk2DktrA+2P9EX4T85sbLBXrYrH2qt 61oQVkoT6B8gq05YqIc0TU5stYVRU+L29gdKrWlb8Ycd+n0GVZHeAP0BxxeewbUHNeIu GoaGwSHd2PrPvMXZJ8+H6tTtwb5cbExqM5sR0QBxs2brlOpG2z4i47d9JtddA0LoqTHL 6DxjbWwVJ0VmIY3vJpfY4JrZjiJBJa5UK8RH+mTb5BPDbCqokp3CZrH8dGB4NFhVn4fK ekGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768897469; x=1769502269; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=i1r38n5/smozxAk3MfAzmduskm1/QEC2/wzUy2JGan4=; b=wFtRXtY0/Yeq/ERbWacliRa3V113SR5CD3b8tJTCeIY5fR/Ylv1HMgCM4iPt9Eu8wU 6iqgFXVwDVK3Y9NTRA39MXOOO7y1tlcBhvSODSIHUzzAfzwg0/7WQCmQGb4pvsdPAzLh drdhWvTFrsZNPuuuGUJvmlBcwz2lMXwP7L/t1i2TOnANMtTK1sHTJo12+bnfZxksmdXv SS1PHF2dmg9e400jo2LR3HxBpu8xlYB5mFjzX2x0X/8jyvmd6RcQlzaxFfftCJhDIxyT ajVwg20mhlHt0MhE+mUO9eAQ0lijcDaQAPdQMGJfRbtFEiPFKSlB8LTFyajQIPbY8HJo T5yA== X-Gm-Message-State: AOJu0YxZ5VYYe+JoLRvSaPkLUyEyR4pXwpxJqWFtWCo8hmJ7Di6QwXUu sSnKo6lgapvM+sShbaVkOGMuOJ78YTcokMktj4wTnQy5MdEv7QzxVp9w X-Gm-Gg: AZuq6aKtK/Nht+I1pTRmRLcXU9MP4m9jDh3Ef6OoK/Ih6SgRY8A92a05e+y1Cg1g//C 5wAWGh4obXu2Lf9DdjWop+nx7FKWp/J/MNZqK2hwlLjpoMHdyfre+S5+Pr0lKTlbnlKmKUJhe5e Ce89CK3fSwXl0reF7+3DJi6SZvuc7fKfNRwxO8MsfpOzIUwwNt5EoIo888VJfJeeQDK+jUW8my5 bZMDSUHywQGoMx6h+FbMT/oJL0Xu1vpRZY5gKiK0grGhL3Dv2hSknHhJAVHy9RDV6R+p2WppGaA F/lGm99Cd5fAt2Iipxv6fUFcwbN3HTXifpFFJmVmbv8PpaWsKDgwhKUOTr0xpaYVU80yRCcW+Zt TdWTJnFCWguZTiZIbH5eHA2PibdY4Tbe2VNDVRknKXrB8ASH5GwaArdjdg7Na9l6FFebwvzWeyv EOGt/IIzXKgenMUOEOXIncoKWsJAUuZrCxdSrTGUyRDllANfmAJ1qQI4ZDhA== X-Received: by 2002:a17:90a:d88b:b0:32e:a8b7:e9c with SMTP id 98e67ed59e1d1-352c40533cbmr1053201a91.29.1768897469525; Tue, 20 Jan 2026 00:24:29 -0800 (PST) From: Chao Liu To: Alistair Francis , Daniel Henrique Barboza , Palmer Dabbelt , Weiwei Li , Liu Zhiwei Cc: qemu-devel@nongnu.org, qemu-riscv@nongnu.org, wangjingwei@iscas.ac.cn, Chao Liu Subject: [RFC PATCH v2 4/7] target/riscv: add dret instruction Date: Tue, 20 Jan 2026 16:24:00 +0800 Message-ID: X-Mailer: git-send-email 2.52.0 In-Reply-To: References: 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 (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::541; envelope-from=chao.liu.zevorn@gmail.com; helo=mail-pg1-x541.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, FREEMAIL_FROM=0.001, 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: qemu development 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 @gmail.com) X-ZM-MESSAGEID: 1768897518209154100 Content-Type: text/plain; charset="utf-8" RISC-V Debug Specification: https://github.com/riscv/riscv-debug-spec/releases/tag/1.0 Add DRET decode/translate and a helper to leave Debug Mode and return to dpc. Executing DRET outside Debug Mode raises illegal instruction. Signed-off-by: Chao Liu --- target/riscv/helper.h | 1 + target/riscv/insn32.decode | 1 + target/riscv/insn_trans/trans_privileged.c.inc | 18 ++++++++++++++++++ target/riscv/op_helper.c | 16 ++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/target/riscv/helper.h b/target/riscv/helper.h index 162303fb6c..af6f3f4fd5 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(dret, tl, env) DEF_HELPER_1(mnret, tl, env) DEF_HELPER_1(ctr_clear, void, env) DEF_HELPER_1(wfi, void, env) diff --git a/target/riscv/insn32.decode b/target/riscv/insn32.decode index c58223ebd8..d04b192ad2 100644 --- a/target/riscv/insn32.decode +++ b/target/riscv/insn32.decode @@ -118,6 +118,7 @@ sctrclr 000100000100 00000 000 00000 1110011 uret 0000000 00010 00000 000 00000 1110011 sret 0001000 00010 00000 000 00000 1110011 mret 0011000 00010 00000 000 00000 1110011 +dret 0111101 10010 00000 000 00000 1110011 wfi 0001000 00101 00000 000 00000 1110011 sfence_vma 0001001 ..... ..... 000 00000 1110011 @sfence_vma =20 diff --git a/target/riscv/insn_trans/trans_privileged.c.inc b/target/riscv/= insn_trans/trans_privileged.c.inc index 8a62b4cfcd..f8641b1977 100644 --- a/target/riscv/insn_trans/trans_privileged.c.inc +++ b/target/riscv/insn_trans/trans_privileged.c.inc @@ -125,6 +125,24 @@ static bool trans_mret(DisasContext *ctx, arg_mret *a) #endif } =20 +static bool trans_dret(DisasContext *ctx, arg_dret *a) +{ +#ifndef CONFIG_USER_ONLY + if (!ctx->cfg_ptr->ext_sdext) { + return false; + } + decode_save_opc(ctx, 0); + translator_io_start(&ctx->base); + gen_update_pc(ctx, 0); + gen_helper_dret(cpu_pc, tcg_env); + exit_tb(ctx); /* no chaining */ + ctx->base.is_jmp =3D DISAS_NORETURN; + return true; +#else + return false; +#endif +} + static bool trans_mnret(DisasContext *ctx, arg_mnret *a) { #ifndef CONFIG_USER_ONLY diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c index 6ccc127c30..99736bbebb 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -454,6 +454,22 @@ target_ulong helper_mret(CPURISCVState *env) return retpc; } =20 +target_ulong helper_dret(CPURISCVState *env) +{ + uintptr_t ra =3D GETPC(); +#ifdef CONFIG_USER_ONLY + riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra); + return 0; +#else + if (!riscv_cpu_cfg(env)->ext_sdext || !env->debug_mode) { + riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra); + } + target_ulong retpc =3D env->dpc & get_xepc_mask(env); + riscv_cpu_leave_debug_mode(env); + return retpc; +#endif +} + target_ulong helper_mnret(CPURISCVState *env) { target_ulong retpc =3D env->mnepc; --=20 2.52.0