From mboxrd@z Thu Jan 1 00:00:00 1970 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5BB69C433F5 for ; Wed, 11 May 2022 13:59:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243059AbiEKN7W (ORCPT ); Wed, 11 May 2022 09:59:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244257AbiEKN7U (ORCPT ); Wed, 11 May 2022 09:59:20 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 722D85B892 for ; Wed, 11 May 2022 06:59:19 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id s14so1959342plk.8 for ; Wed, 11 May 2022 06:59:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=ZjjX/D7+e1CU0soxhtpqFh9Eg4y/Y1Z0qCVDda3ER7I=; b=l5vtfPE5w/vFTxxW2PLFbwnIjQyYCOhrLMonKHJNyILwanFVcK4dgjiTQfB99410KT PMTMR+97aIOmOxzle4CgWx4i0bmNMOznOyBYCPsEad95f+NteMTLG+dLQIRREizPz27i 4cMUlNqrx8gIKpQqc4hHP1oNQ0/X/msdz6/gWlq8uVb15HWVNyLG3xknkX2pD/mp1fRx femwFw68cRHyk7gDWrEkEOslsYFNTiGZWqk9ddydfaKSRnYrkQezDx6FFmdR5gQRNkbw B/wEN+oA/9mVGjExXLOYAYx2LbwAz1NLR+a4QmA5Fwt5/EcCUPn4TduCtoxrgZtA55vD 4y7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=ZjjX/D7+e1CU0soxhtpqFh9Eg4y/Y1Z0qCVDda3ER7I=; b=wrnq/hVqCl3VbnwWUI5nnvtU3IktSz1xocolI8oZM/C9EIXX63XhIr0G8anWELA7ly eCDWsglYGfnGNOI89Jp4EEp9msJJ5t3VZjEVJPo3UG2tXCUQcUP5V6+Alj1Ek4Jhm9+m GJCujCdMnL0lkw8WiofhfJZ9lp0/zSNQazpRJayBQ/s33nmheMO+i7yBaq6jpgToJEiX xT4vT97N91FUiKeYhMagRlCfTQmwZkZ61RAVjccyK0UVF1bsq8SuKlAwhW9xP5ycGH91 d6hOTjVlk/cXoLJIy02yccoTf8zHALyxGKYYOzGUyQ1HLpYDFxHEhexhdR5Xrzsd/iWP yGLg== X-Gm-Message-State: AOAM530T3Mfk7rmYnBUEtL0NV7rO3TxpJnPHi9kqlBqijaEXSzEEPDx6 hK30smLpCwiRgMjXlzsB9Mgzl4A0zchXug== X-Google-Smtp-Source: ABdhPJz8n4abpGdrwPWzRKWsDr5A7bKb5JLCXUOL06OrQeQgDMFGxKlOM3jspnHzcsZCvMyXtm6p6A== X-Received: by 2002:a17:902:f710:b0:15f:165f:b50b with SMTP id h16-20020a170902f71000b0015f165fb50bmr13252701plo.158.1652277558633; Wed, 11 May 2022 06:59:18 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id lb1-20020a17090b4a4100b001cd4989ff64sm1880064pjb.43.2022.05.11.06.59.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 06:59:17 -0700 (PDT) Date: Wed, 11 May 2022 13:59:14 +0000 From: Sean Christopherson To: Jim Mattson Cc: Arnabjyoti Kalita , kvm@vger.kernel.org Subject: Re: Causing VMEXITs when kprobes are hit in the guest VM Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org On Wed, May 11, 2022, Jim Mattson wrote: > On Fri, May 6, 2022 at 11:31 PM Arnabjyoti Kalita > wrote: > > > > Dear Sean and all, > > > > When a VMEXIT happens of type "KVM_EXIT_DEBUG" because a hardware > > breakpoint was triggered when an instruction was about to be executed, > > does the instruction where the breakpoint was placed actually execute > > before the VMEXIT happens? > > > > I am attempting to record the occurrence of the debug exception in > > userspace. I do not want to do anything extra with the debug > > exception. I have modified the kernel code (handle_exception_nmi) to > > do something like this - > > > > case BP_VECTOR: > > /* > > * Update instruction length as we may reinject #BP from > > * user space while in guest debugging mode. Reading it for > > * #DB as well causes no harm, it is not used in that case. > > */ > > vmx->vcpu.arch.event_exit_inst_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); > > kvm_run->exit_reason = KVM_EXIT_DEBUG; > > ...... > > kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; > > kvm_run->debug.arch.exception = ex_no; > > kvm_rip_write(vcpu, rip + vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); > > <---Change : update RIP here > > break; > > > > This allows the guest to proceed after the hardware breakpoint > > exception was triggered. However, the guest kernel keeps running into > > page fault at arbitrary points in time. So, I'm not sure if I need to > > handle something else too. > > > > I have modified the userspace code to not trigger any exception, it > > just records the occurence of this VMEXIT and lets the guest continue. > > > > Is this the right approach? > > Probably not. I'm not sure how kprobes work, but the tracepoint hooks > at function entry are multi-byte nopl instructions. The int3 > instruction that raises a #BP fault is only one byte. If you advance > past that byte, you will try to execute the remaining bytes of the > original nopl. You want to skip past the entire nopl. And kprobes aren't the only thing that will generate #BP, e.g. the kernel uses INT3 for patching, userspace debuggers in the guest can insert INT3, etc... The correct thing to do is to re-inject the #BP back into the guest without touching RIP.