From patchwork Tue Feb 6 07:49:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chao Du X-Patchwork-Id: 13546831 Received: from zg8tmtyylji0my4xnjqumte4.icoremail.net (zg8tmtyylji0my4xnjqumte4.icoremail.net [162.243.164.118]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 43D9B12AAE8 for ; Tue, 6 Feb 2024 07:52:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.243.164.118 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707205953; cv=none; b=gtucvdE84zi1A+Z6gNNXWFfGiRRYdwTXB/RvCZVO7EKOyZyBbO1TRAYKMQg3RrZqxg+icGICeYAAYboRrRkf/zetbjw3YT1oBgfPpmgZSOmyR1u6zaZB/iFfylVS/T26s0fRgybXdsAnmocvNrGSZAFRYByolJVXdq9v0AuAWiM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707205953; c=relaxed/simple; bh=LKCesvnQ8hKwu2pW4oYdaxSBuPICXR1AXOoWtwWSePw=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References; b=paa6Ezvv1Nk30amzmP25sWGUoaZDwVLvVg7gfXKEGhKj7Yh9WjPuyydBUAKn/QHXDRQStgGAeRbKNQ0HwBNM+NMljk7OEaSH5gAgmEszr2TdSBKuGMH2pyo+TR/N5ukVL3nv9DTb3JXZoziJ00TtBmgFqC0IytHBTQYDS/xoo6c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=eswincomputing.com; spf=pass smtp.mailfrom=eswincomputing.com; arc=none smtp.client-ip=162.243.164.118 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=eswincomputing.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=eswincomputing.com Received: from localhost.localdomain (unknown [10.12.130.31]) by app1 (Coremail) with SMTP id TAJkCgD3gv2B5MFlygcKAA--.4463S5; Tue, 06 Feb 2024 15:49:24 +0800 (CST) From: Chao Du To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, anup@brainfault.org, atishp@atishpatra.org, pbonzini@redhat.com, shuah@kernel.org, dbarboza@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, duchao713@qq.com Subject: [PATCH v1 1/3] RISC-V: KVM: Implement kvm_arch_vcpu_ioctl_set_guest_debug() Date: Tue, 6 Feb 2024 07:49:29 +0000 Message-Id: <20240206074931.22930-2-duchao@eswincomputing.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240206074931.22930-1-duchao@eswincomputing.com> References: <20240206074931.22930-1-duchao@eswincomputing.com> X-CM-TRANSID: TAJkCgD3gv2B5MFlygcKAA--.4463S5 X-Coremail-Antispam: 1UD129KBjvJXoW7Cr47Gw4fCryDKw1fCF1rJFb_yoW8tr4xpF 4DCr98Aw4rGr93C34Iya95ur4S939agrnak347uFy3AFWjyrWFyrs5KrZxJry5JrWrWFyx CF1rKF1ru3Z8trUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUP2b7Iv0xC_tr1lb4IE77IF4wAFF20E14v26ryj6rWUM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI 8067AKxVWUGwA2048vs2IY020Ec7CjxVAFwI0_Gr0_Xr1l8cAvFVAK0II2c7xJM28CjxkF 64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcV CY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv 6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c 02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE 4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1lc2 xSY4AK6svPMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8C rVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8Zw CIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x02 67AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr 0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU05D G5UUUUU== X-CM-SenderInfo: xgxfxt3r6h245lqf0zpsxwx03jof0z/ Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: kvm_vm_ioctl_check_extension(): Return 1 if KVM_CAP_SET_GUEST_DEBUG is being checked. kvm_arch_vcpu_ioctl_set_guest_debug(): Update the guest_debug flags from userspace accordingly. Route the breakpoint exceptions to HS mode if the VM is being debugged by userspace, by clearing the corresponding bit in hedeleg CSR. Signed-off-by: Chao Du --- arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/vcpu.c | 15 +++++++++++++-- arch/riscv/kvm/vm.c | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) -- 2.17.1 diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index d6b7a5b95874..8890977836f0 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -17,6 +17,7 @@ #define __KVM_HAVE_IRQ_LINE #define __KVM_HAVE_READONLY_MEM +#define __KVM_HAVE_GUEST_DEBUG #define KVM_COALESCED_MMIO_PAGE_OFFSET 1 diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index b5ca9f2e98ac..6cee974592ac 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -475,8 +475,19 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg) { - /* TODO; To be implemented later. */ - return -EINVAL; + if (dbg->control & KVM_GUESTDBG_ENABLE) { + if (vcpu->guest_debug != dbg->control) { + vcpu->guest_debug = dbg->control; + csr_clear(CSR_HEDELEG, BIT(EXC_BREAKPOINT)); + } + } else { + if (vcpu->guest_debug != 0) { + vcpu->guest_debug = 0; + csr_set(CSR_HEDELEG, BIT(EXC_BREAKPOINT)); + } + } + + return 0; } static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu) diff --git a/arch/riscv/kvm/vm.c b/arch/riscv/kvm/vm.c index ce58bc48e5b8..7396b8654f45 100644 --- a/arch/riscv/kvm/vm.c +++ b/arch/riscv/kvm/vm.c @@ -186,6 +186,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_READONLY_MEM: case KVM_CAP_MP_STATE: case KVM_CAP_IMMEDIATE_EXIT: + case KVM_CAP_SET_GUEST_DEBUG: r = 1; break; case KVM_CAP_NR_VCPUS: From patchwork Tue Feb 6 07:49:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chao Du X-Patchwork-Id: 13546829 Received: from azure-sdnproxy.icoremail.net (azure-sdnproxy.icoremail.net [207.46.229.174]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3657112BF34 for ; Tue, 6 Feb 2024 07:52:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=207.46.229.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707205943; cv=none; b=SzlRAHX4RZilh68bl8KI+yWF9Q7VJpeIHrIEBxa+QlCJ/2+FdRH1jCv2xpwcZU5FscJ8p6imdIX9P01ddUc6Wz+HjXQQRwUmfNHg7q7+hIMTlJqq5QvCOoHgDcGAM4b3Cj8wu4+0ZQ/szCNPr2L1go1pBTWpJP8zyp53TcTi7jA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707205943; c=relaxed/simple; bh=6KMKJpvnLeuBhGIz5BKjsqZww25xAF5Kzg4ckvnw15o=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References; b=f8aW/3Bwfo9spLr93WWVklU/fa7oxNJ5B9fXTkI/jgglRCtHcjMQU52BRUcdbV+DhmI97H88ysTK9tNbzxgW2+JhEnYgOOqHhC6b9viOW3C5xX6BqLaJsLX7uTof09ege1LK6CSKug3tDWirKafUAZ+myfkaT9DB99aUPp0fKXU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=eswincomputing.com; spf=pass smtp.mailfrom=eswincomputing.com; arc=none smtp.client-ip=207.46.229.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=eswincomputing.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=eswincomputing.com Received: from localhost.localdomain (unknown [10.12.130.31]) by app1 (Coremail) with SMTP id TAJkCgD3gv2B5MFlygcKAA--.4463S6; Tue, 06 Feb 2024 15:49:25 +0800 (CST) From: Chao Du To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, anup@brainfault.org, atishp@atishpatra.org, pbonzini@redhat.com, shuah@kernel.org, dbarboza@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, duchao713@qq.com Subject: [PATCH v1 2/3] RISC-V: KVM: Handle breakpoint exits for VCPU Date: Tue, 6 Feb 2024 07:49:30 +0000 Message-Id: <20240206074931.22930-3-duchao@eswincomputing.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240206074931.22930-1-duchao@eswincomputing.com> References: <20240206074931.22930-1-duchao@eswincomputing.com> X-CM-TRANSID: TAJkCgD3gv2B5MFlygcKAA--.4463S6 X-Coremail-Antispam: 1UD129KBjvdXoWruFWxCF4rAF1UZrWUtrWxXrb_yoW3Grg_G3 4xJw1fGFZ8Xw1xtFnrGw4fJrn5Jws5Ja45XryF9r98WF1qvr4xKrZ5X3WUZr18ZrW5ZFsr Jrs5AF43A34jyjkaLaAFLSUrUUUUUb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUIcSsGvfJTRUUUbq8YjsxI4VWxJwAYFVCjjxCrM7AC8VAFwI0_Wr0E3s1l1xkIjI8I 6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l1IIY67AEw4v_Jr0_Jr4l82xGYIkIc2x26280x7 IE14v26r15M28IrcIa0xkI8VCY1x0267AKxVW5JVCq3wA2ocxC64kIII0Yj41l84x0c7CE w4AK67xGY2AK021l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6x kF7I0E14v26r4UJVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIE c7CjxVAFwI0_GcCE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I 8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCF s4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x02628vn2kIc2xKxwCY02 Avz4vE-syl42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG 67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MI IYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E 14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJV W8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxU7iFx UUUUU X-CM-SenderInfo: xgxfxt3r6h245lqf0zpsxwx03jof0z/ Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Exit to userspace for breakpoint traps. Set the exit_reason as KVM_EXIT_DEBUG before exit. Signed-off-by: Chao Du Reviewed-by: Anup Patel --- arch/riscv/kvm/vcpu_exit.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index 2415722c01b8..5761f95abb60 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -204,6 +204,10 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) ret = kvm_riscv_vcpu_sbi_ecall(vcpu, run); break; + case EXC_BREAKPOINT: + run->exit_reason = KVM_EXIT_DEBUG; + ret = 0; + break; default: break; } From patchwork Tue Feb 6 07:49:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chao Du X-Patchwork-Id: 13546830 Received: from zg8tmty3ljk5ljewns4xndka.icoremail.net (zg8tmty3ljk5ljewns4xndka.icoremail.net [167.99.105.149]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ED68E12D172 for ; Tue, 6 Feb 2024 07:52:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=167.99.105.149 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707205949; cv=none; b=cNMCzgCEYDxLNF+2PbRCFQHYSOCaVjLgpFeWMTfA4GJPlQAa9aaf7EwXpZBmRs0XpYT3r8+ziRlveTLsscW3CvCO1A1E9CynkkuiUlLhPHgqIWHmGqWzoUpzvz3a6FfU6+FZyivUpxtDvq5RABTXEnU68FLOnlUE+NzjXNbtFIE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707205949; c=relaxed/simple; bh=Bw6MOYHPl2WWD1z3bWWIzuAF6Sv8FN2jb43KWnjYvWQ=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References; b=BddH+hed0Gr1kBnq8x+vEKHcd3h/a/ey/py/6FsBGvqeNiPFFVz9NFmrxFRHoLc9vDEpvRWCWUJ9tchtS4kiAlNvEAwUrkfwflzNSws7SzSt/fD4ZcF5OEyEVdVgw0qkB1L/rP/mvYh68aOdjK22oGUVk5nINGXf4bZQFz2P/Gg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=eswincomputing.com; spf=pass smtp.mailfrom=eswincomputing.com; arc=none smtp.client-ip=167.99.105.149 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=eswincomputing.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=eswincomputing.com Received: from localhost.localdomain (unknown [10.12.130.31]) by app1 (Coremail) with SMTP id TAJkCgD3gv2B5MFlygcKAA--.4463S7; Tue, 06 Feb 2024 15:49:26 +0800 (CST) From: Chao Du To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, anup@brainfault.org, atishp@atishpatra.org, pbonzini@redhat.com, shuah@kernel.org, dbarboza@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, duchao713@qq.com Subject: [PATCH v1 3/3] RISC-V: KVM: selftests: Add breakpoints test support Date: Tue, 6 Feb 2024 07:49:31 +0000 Message-Id: <20240206074931.22930-4-duchao@eswincomputing.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240206074931.22930-1-duchao@eswincomputing.com> References: <20240206074931.22930-1-duchao@eswincomputing.com> X-CM-TRANSID: TAJkCgD3gv2B5MFlygcKAA--.4463S7 X-Coremail-Antispam: 1UD129KBjvJXoW7KF1rtr1xCFW7Ww15Xw1kKrg_yoW8tw1Up3 WxCw1q9rWvqrW3Gw4xAw4DuF4fKrykXF48Jr1fW34jvrWUJrWrJrnagFy2yr9I93yrXw1f Aa4fW3WS9F4DJw7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUP2b7Iv0xC_Cr1lb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI 8067AKxVWUWwA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF 64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcV CY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv 6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c 02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE 4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1lc2 xSY4AK6svPMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8C rVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8Zw CIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x02 67AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr 0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU8FD 73UUUUU== X-CM-SenderInfo: xgxfxt3r6h245lqf0zpsxwx03jof0z/ Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Initial support for RISC-V KVM breakpoint test. Check the exit reason and the PC when guest debug is enabled. Signed-off-by: Chao Du --- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/riscv/breakpoints.c | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tools/testing/selftests/kvm/riscv/breakpoints.c -- 2.17.1 diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 492e937fab00..5f9048a740b0 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -184,6 +184,7 @@ TEST_GEN_PROGS_s390x += rseq_test TEST_GEN_PROGS_s390x += set_memory_region_test TEST_GEN_PROGS_s390x += kvm_binary_stats_test +TEST_GEN_PROGS_riscv += riscv/breakpoints TEST_GEN_PROGS_riscv += demand_paging_test TEST_GEN_PROGS_riscv += dirty_log_test TEST_GEN_PROGS_riscv += get-reg-list diff --git a/tools/testing/selftests/kvm/riscv/breakpoints.c b/tools/testing/selftests/kvm/riscv/breakpoints.c new file mode 100644 index 000000000000..8ca09a493460 --- /dev/null +++ b/tools/testing/selftests/kvm/riscv/breakpoints.c @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * RISC-V KVM breakpoint tests. + * + * Copyright 2024 Beijing ESWIN Computing Technology Co., Ltd. + * + */ +#include "kvm_util.h" + +#define PC(v) ((uint64_t)&(v)) + +extern unsigned char sw_bp; + +static void guest_code(void) +{ + asm volatile("sw_bp: ebreak"); + asm volatile("nop"); + asm volatile("nop"); + asm volatile("nop"); + + GUEST_DONE(); +} + +int main(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + struct kvm_guest_debug debug; + uint64_t pc; + + TEST_REQUIRE(kvm_has_cap(KVM_CAP_SET_GUEST_DEBUG)); + + vm = vm_create_with_one_vcpu(&vcpu, guest_code); + + memset(&debug, 0, sizeof(debug)); + debug.control = KVM_GUESTDBG_ENABLE; + vcpu_guest_debug_set(vcpu, &debug); + vcpu_run(vcpu); + + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_DEBUG); + + vcpu_get_reg(vcpu, RISCV_CORE_REG(regs.pc), &pc); + + TEST_ASSERT_EQ(pc, PC(sw_bp)); + + kvm_vm_free(vm); + + return 0; +}