From patchwork Tue Apr 16 23:04:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 1924469 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=ventanamicro.com header.i=@ventanamicro.com header.a=rsa-sha256 header.s=google header.b=MhS6s2kN; 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 4VK05H4HJ5z1yZ2 for ; Wed, 17 Apr 2024 09:05:55 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rwrrE-00033f-Fs; Tue, 16 Apr 2024 19:04:56 -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 1rwrr9-00033C-PJ for qemu-devel@nongnu.org; Tue, 16 Apr 2024 19:04:51 -0400 Received: from mail-il1-x12a.google.com ([2607:f8b0:4864:20::12a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rwrr8-0007Pa-4A for qemu-devel@nongnu.org; Tue, 16 Apr 2024 19:04:51 -0400 Received: by mail-il1-x12a.google.com with SMTP id e9e14a558f8ab-36b2ec387a9so4192305ab.0 for ; Tue, 16 Apr 2024 16:04:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1713308689; x=1713913489; 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=ssIuZaHoHfxGkU2ooOzNE6RgtnB9PZD34dZd+Oj8vXc=; b=MhS6s2kNtB6CX0aNK3Otb+65ckHgbxk1tuPkkln0YrDRBGpJeqJw8lXOrBaiKlcWck XD5wALA1FmqlduUhYRXXAovxnWRbBnHlEm/g/mfmQTsSuFcjhQzsCpURVVxtqP1ImjzG YSAuyL3ky6Pp/UrLJP9WvvmVpMIazQYYnrzpDsie1dGN8PtBK6WNOOPzdNszMnVwIAMf 4i4FQi+vuvWm0cg0BBgRXuDUY+tEuhd7L8OrJs6IjVZOMXO1tFH42362G5nR6JRRWcf8 1L5mV6259zoUrkV0CFb+NKRT3s+fpB2Rf5QZwTqVTiFHF6PnRX73trZj16s7gFJU3biO 7X4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713308689; x=1713913489; 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=ssIuZaHoHfxGkU2ooOzNE6RgtnB9PZD34dZd+Oj8vXc=; b=XoQRfYrdxM18ZRhqMUnd+LB0JXGtTkLI9nXsX+PAHZOqG5YIUpxbFvr6lu421Q55cq BkrkW4h8G6EKGdaCEjtUOxnbtpioqbh0QRLQ1aZwFzt9jUaeJdarhYrvnNzUFvm6w5Zm XRq7vGuBo9qXy5b7C6EcRHptkhKXoFnRrnWBCL+PfNh1douEnXhoaJg2KnOhMX4hseJM GPYFh7U/oVsDROS06uUFt6ooNTeJDv/3JC+uhHotr0e7iZhMki3UdhkvMYhQxPAZl3xJ 0HI7sZ1ngwqrdrHXdb5d+htTs7h4Z++sICZeAhWJ49dGy0ZTJn5zU4qmzWURHW6eGIz8 +6bw== X-Gm-Message-State: AOJu0YzikvU9MBx3jICpP2CxvpZTKGc1KEZYmqMAMJfu7aNq8pZ5ZNDu 5P0aIIu3UTnESagygUzK/zgUlCIxKwEQ4DZKFSHjE7U2/7BCGprzvOCdwt6OO3qDBsDwEYyq0QC z X-Google-Smtp-Source: AGHT+IGji79nc52Bll8C4nZtz+BYJYUxlIEdLPtdSIAIgzGDMC8wuSbd+GWtGTKM1t/lMHDDIDhAcQ== X-Received: by 2002:a05:6e02:1c06:b0:36a:686:b3bf with SMTP id l6-20020a056e021c0600b0036a0686b3bfmr18553927ilh.17.1713308688837; Tue, 16 Apr 2024 16:04:48 -0700 (PDT) Received: from grind.. ([177.45.186.202]) by smtp.gmail.com with ESMTPSA id g10-20020a63374a000000b005e4666261besm9399555pgn.50.2024.04.16.16.04.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 16:04:48 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, alistair.francis@wdc.com, bmeng@tinylab.org, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, palmer@rivosinc.com, richard.henderson@linaro.org, Daniel Henrique Barboza Subject: [PATCH for-9.1 v3 1/2] target/riscv/debug: set tval=pc in breakpoint exceptions Date: Tue, 16 Apr 2024 20:04:36 -0300 Message-ID: <20240416230437.1869024-2-dbarboza@ventanamicro.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240416230437.1869024-1-dbarboza@ventanamicro.com> References: <20240416230437.1869024-1-dbarboza@ventanamicro.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::12a; envelope-from=dbarboza@ventanamicro.com; helo=mail-il1-x12a.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 We're not setting (s/m)tval when triggering breakpoints of type 2 (mcontrol) and 6 (mcontrol6). According to the debug spec section 5.7.12, "Match Control Type 6": "The Privileged Spec says that breakpoint exceptions that occur on instruction fetches, loads, or stores update the tval CSR with either zero or the faulting virtual address. The faulting virtual address for an mcontrol6 trigger with action = 0 is the address being accessed and which caused that trigger to fire." A similar text is also found in the Debug spec section 5.7.11 w.r.t. mcontrol. Note that what we're doing ATM is not violating the spec, but it's simple enough to set mtval/stval and it makes life easier for any software that relies on this info. Given that we always use action = 0, save the faulting address for the mcontrol and mcontrol6 trigger breakpoints into env->badaddr, which is used as as scratch area for traps with address information. 'tval' is then set during riscv_cpu_do_interrupt(). Signed-off-by: Daniel Henrique Barboza Reviewed-by: LIU Zhiwei Reviewed-by: Alistair Francis --- target/riscv/cpu_helper.c | 1 + target/riscv/debug.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index fc090d729a..f9c6d7053b 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -1717,6 +1717,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) tval = env->bins; break; case RISCV_EXCP_BREAKPOINT: + tval = env->badaddr; if (cs->watchpoint_hit) { tval = cs->watchpoint_hit->hitaddr; cs->watchpoint_hit = NULL; diff --git a/target/riscv/debug.c b/target/riscv/debug.c index e30d99cc2f..b110370ea6 100644 --- a/target/riscv/debug.c +++ b/target/riscv/debug.c @@ -798,6 +798,7 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs) if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) { /* check U/S/M bit against current privilege level */ if ((ctrl >> 3) & BIT(env->priv)) { + env->badaddr = pc; return true; } } @@ -810,11 +811,13 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs) if (env->virt_enabled) { /* check VU/VS bit against current privilege level */ if ((ctrl >> 23) & BIT(env->priv)) { + env->badaddr = pc; return true; } } else { /* check U/S/M bit against current privilege level */ if ((ctrl >> 3) & BIT(env->priv)) { + env->badaddr = pc; return true; } } From patchwork Tue Apr 16 23:04:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 1924470 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=ventanamicro.com header.i=@ventanamicro.com header.a=rsa-sha256 header.s=google header.b=Kdu0BPjv; 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 4VK05R4qpgz1yZ2 for ; Wed, 17 Apr 2024 09:06:03 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rwrrG-00034S-8X; Tue, 16 Apr 2024 19:04:58 -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 1rwrrE-00033g-57 for qemu-devel@nongnu.org; Tue, 16 Apr 2024 19:04:56 -0400 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rwrrB-0007Py-FI for qemu-devel@nongnu.org; Tue, 16 Apr 2024 19:04:55 -0400 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-6ece8991654so4478492b3a.3 for ; Tue, 16 Apr 2024 16:04:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1713308692; x=1713913492; 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=s7toiwGiMFJ8d0S7eID2dFSemOPb731HeAM6DXT2dsY=; b=Kdu0BPjvbxVgaqrO5RmSpqLrDlzweTnzxk6XQ9mEIofUx+oNBllCfzOBxje4ZU3g2e 5j7rPg83lsy9laaryd7t89BvyxjpUkV4QEnGTkeKq2s++Ht9OzxZHsVDp8V27WNCSoFv iFbbp+/FmtFtq1KF+TWbBPTZYAwCb8OkFKQajsm0NxYQ+6gjf1pXlCX5bo3CXfuErdIn /IrZzqmHB1h7JPb7M/wahsCa6jUdevsKbRalB5bGe4kKW9/3W2UiKWwZ2KgjFwG2DXdJ yYc6pKezfPPjz2Csq5MioIQxg91edlZQq3fZPShx9Hk6vMm9xZHMX5RNSs7iCI4sb98F m5Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713308692; x=1713913492; 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=s7toiwGiMFJ8d0S7eID2dFSemOPb731HeAM6DXT2dsY=; b=WEoSr46tM9kVexv2CRcKxeebgjbEHpNsbdGfzFO/3VieB8rb1RUytukCrpuZeXyuC7 2lRBVg7kiSND5tu25FgEf1mtEIt9D88WyTQAief+qfhslb6AqQZ0X8DscphtveU23B6m W5J/akRo9xr7STpw69jMuIJbfCGRA2gWeAQw7+eCWeAmzT2tJVaTFIDAgPamqjMkLxFd vxHQ9OuHTFdu53gbWsdMkTYqcpY4DyVEVAZE/Yf4Sw8MsKKE+Xbfg5BTW2RB6UMAyJzF Li9J8xoJ6WR8xjusuixnw9Mwnnl/RnqpoN4xZF8BgJB5kEyLODAetC0/ftJhmKdUPWdf uPtQ== X-Gm-Message-State: AOJu0YzEVfnUevWmU0Pps2GiX5+s5FCyzLt7Ju7weDqfY1PlQdrtCYvi Wun8mnDn5kiYeg9pvNjan4+Jou5RO//Xt9+eJM8McbRz+UR+eZksTqr4vnb7bPJFJkCpWhSl9Xu A X-Google-Smtp-Source: AGHT+IHpam/HBLJmBOioxukgZ3F5MlNnSsMX7hH5WAG90QLBd3qr5H26reLoYwhJrK79vYYCnd0gfg== X-Received: by 2002:a05:6a21:328d:b0:1a9:694b:e1f with SMTP id yt13-20020a056a21328d00b001a9694b0e1fmr19706081pzb.2.1713308691833; Tue, 16 Apr 2024 16:04:51 -0700 (PDT) Received: from grind.. ([177.45.186.202]) by smtp.gmail.com with ESMTPSA id g10-20020a63374a000000b005e4666261besm9399555pgn.50.2024.04.16.16.04.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 16:04:51 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, alistair.francis@wdc.com, bmeng@tinylab.org, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, palmer@rivosinc.com, richard.henderson@linaro.org, Daniel Henrique Barboza Subject: [PATCH for-9.1 v3 2/2] trans_privileged.c.inc: set (m|s)tval on ebreak breakpoint Date: Tue, 16 Apr 2024 20:04:37 -0300 Message-ID: <20240416230437.1869024-3-dbarboza@ventanamicro.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240416230437.1869024-1-dbarboza@ventanamicro.com> References: <20240416230437.1869024-1-dbarboza@ventanamicro.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::436; envelope-from=dbarboza@ventanamicro.com; helo=mail-pf1-x436.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 Privileged spec section 4.1.9 mentions: "When a trap is taken into S-mode, stval is written with exception-specific information to assist software in handling the trap. (...) If stval is written with a nonzero value when a breakpoint, address-misaligned, access-fault, or page-fault exception occurs on an instruction fetch, load, or store, then stval will contain the faulting virtual address." A similar text is found for mtval in section 3.1.16. Setting mtval/stval in this scenario is optional, but some softwares read these regs when handling ebreaks. Write 'badaddr' in all ebreak breakpoints to write the appropriate 'tval' during riscv_do_cpu_interrrupt(). Signed-off-by: Daniel Henrique Barboza Reviewed-by: Richard Henderson Reviewed-by: LIU Zhiwei Reviewed-by: Alistair Francis --- target/riscv/insn_trans/trans_privileged.c.inc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/target/riscv/insn_trans/trans_privileged.c.inc b/target/riscv/insn_trans/trans_privileged.c.inc index 620ab54eb0..bc5263a4e0 100644 --- a/target/riscv/insn_trans/trans_privileged.c.inc +++ b/target/riscv/insn_trans/trans_privileged.c.inc @@ -62,6 +62,8 @@ static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a) if (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) { generate_exception(ctx, RISCV_EXCP_SEMIHOST); } else { + tcg_gen_st_tl(tcg_constant_tl(ebreak_addr), tcg_env, + offsetof(CPURISCVState, badaddr)); generate_exception(ctx, RISCV_EXCP_BREAKPOINT); } return true;