From nobody Mon Feb 9 18:45:29 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=1768624517; cv=none; d=zohomail.com; s=zohoarc; b=iNn51r1B3jzk0Q1pE6hRg3aUZhV5bB1WyosYZY6jrCeoieVyiU1qkxvUQttSxS2UzkbKr8oLzIGxkM7ZD0GFuc7jDy8cLN9Jnq9UoyaW+BoRTO33HHDvcarlG1j2vdTaHY3R2cl3JqTnnr8KtnMu2CprgKXqKc8dsgv3dPNK/do= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1768624517; 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=YhKx6wgfTyYatD8qmCVKg9zN0KuHu56SoyCFvmL9LT4=; b=bBZI1gqErbRyvOOY65MSLfzHDHf0jRbtWfYjw3gf+vboZK+wais5BrP03HPnlJ44CoxCw91jbIh1Wl1EP4S9rbHk6CGgVXZqmWDA4W+ZuojrFUXVqL/LMPYZ0j/pY7zAwi7QVxIf+QcwQMcIZRKGtmIY//DHYIltjIHKes4aXtk= 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 1768624517503624.1729482093604; Fri, 16 Jan 2026 20:35:17 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vgxuk-0000yL-N7; Fri, 16 Jan 2026 23:27:54 -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 1vgxui-0000wu-UI for qemu-devel@nongnu.org; Fri, 16 Jan 2026 23:27:52 -0500 Received: from mail-pg1-x543.google.com ([2607:f8b0:4864:20::543]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vgxug-0001bA-Cx for qemu-devel@nongnu.org; Fri, 16 Jan 2026 23:27:52 -0500 Received: by mail-pg1-x543.google.com with SMTP id 41be03b00d2f7-c0ec27cad8cso1101875a12.1 for ; Fri, 16 Jan 2026 20:27:50 -0800 (PST) Received: from ZEVORN-PC.localdomain ([114.88.97.170]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c5edf395851sm3273056a12.36.2026.01.16.20.27.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Jan 2026 20:27:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768624069; x=1769228869; 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=YhKx6wgfTyYatD8qmCVKg9zN0KuHu56SoyCFvmL9LT4=; b=kFQlfNqet1/rwN1NePJluWTA+dTDK8xjQk4RWAl0zyEObyMbg6qdMPMsQr1D8v4Yay SBdU2T8ebmqYE2HMJ0FmagddtXD6OThH7k/E1UCwTHi8s3YKrj3YNLaW9eZ7HoXfArYQ pGQN5jmzkhRMIeDVRl6co1UDEUkX4/YwJsx7kARdrQwzWNUY5gjsEOqDK5b2Ti/rQ3Ll 5+aZEYIH1IkpMeFyfmlhtjckKsx6YdhUCrfEr8oLToKd5t9jtNZfnuDzDMuDihq/axev kSjbyMIrnvg9E8TXtBUlOczr4ZwVj52BQcTePAHrL0mEjUr3WvMSwWxAFBr2CDDFIWHg K2cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768624069; x=1769228869; 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=YhKx6wgfTyYatD8qmCVKg9zN0KuHu56SoyCFvmL9LT4=; b=aK6iE4D2xpV4qjjj/VBuk1t8UNpIK3D4lSfpG2Z1Z1qEuspJHrDzRQLyvb7s1TBf7p KMPy1TAuSNZc2GUa4nw5p/Zdat6WKcNR9E+OW4hcTGMWLl/iOfXC2Zfb+cXRVYmiOI90 UJ0XYiGPWFEVKq1x4+1RfR0vN6AXo1C+cFOqPSq5BDwmS0ojWVliQk8pbbBdQKNAkFWA IV7AlDuGsCcJ+jXs4hWxcaJ2vQdCgICi1TW1/vqg49GTJwFCS4umZrUGvSqL28Wrbxsr 3lkz5cwkpa3DISovKgMJ+NZ1XrzbFy+x4GYrqB5MJAHB/ikB7sPfo4x5vVR7oeNLDeuY EafQ== X-Gm-Message-State: AOJu0YyKthhYMv48i3+54l/hWzSijOHmhzUBFOfmTjekci491zk+YM4p MH8hSK9nPMKCXXduDgigDlohYQ1Rm2tO74fW8se432lrOx8mP51i6SnT X-Gm-Gg: AY/fxX7RBodkpIt0qM0jIlgwAtElUl7Pw4xlVE8icrdWoVBvvg5hCaSxfETRDP9LnAS 380Rboc8vdCt9XCqOTg16uGSNBE3Sjc7Qu8ggF1Vcg8zf9KLEowVVoVwoVomU8RzQ/Gv8+yoLFX 3hkm7vTN/97bl3GZZu+PTYP/pO4UdqY5y3tRtRkmNtCibiANViWtDFYl6wUz53eNCYTAsPlWUl9 PFxvEM50st6U3MPpk5ZOYo5ib/aMewy2bhv9Do23H4KxfOz57GL7p0pV2N+ipgrBvW89lV3c7Et iLfnl/Qoi16Vt7UY8qOmdQ7aTJO+c846LkZrnIjdKDZ6jBacUsD1krxJhCzmW8eukXs0Dk5WUrI r6fpIQ/fiID8aGpGV4iB+PxKcq/sJs3wiZa6T4Fy/7Yn3HwKtb7HgSXTLLTr05I+DZquPGfSCsW 9jvfAi8Vb+T4G2p8GqjV5IbNEFIZjZCFkpMre276pVaQlqpEzDKgWPrXp+HZHeyQ== X-Received: by 2002:a05:6a21:6d95:b0:33f:df99:11e5 with SMTP id adf61e73a8af0-38dfe64d86emr6082743637.28.1768624068795; Fri, 16 Jan 2026 20:27:48 -0800 (PST) From: Chao Liu To: Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei Cc: qemu-devel@nongnu.org, qemu-riscv@nongnu.org, wangjingwei@iscas.ac.cn, Chao Liu Subject: [RFC PATCH v1 4/8] riscv: add dret instruction Date: Sat, 17 Jan 2026 12:27:25 +0800 Message-ID: <4e95ab6c30b7ca58140533eda21801c1bf6b9243.1768622882.git.chao.liu.zevorn@gmail.com> 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::543; envelope-from=chao.liu.zevorn@gmail.com; helo=mail-pg1-x543.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: 1768624521380158500 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 b785456ee0..6140b6340d 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 cd23b1f3a9..318d330c57 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