Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.3 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E5DAC54FCB for ; Mon, 27 Apr 2020 15:16:14 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D68562054F for ; Mon, 27 Apr 2020 15:16:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D68562054F Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jT5UJ-0004eU-Ce; Mon, 27 Apr 2020 15:16:03 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jT5UI-0004eO-D5 for xen-devel@lists.xenproject.org; Mon, 27 Apr 2020 15:16:02 +0000 X-Inumbo-ID: 0110367d-889a-11ea-97a9-12813bfff9fa Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 0110367d-889a-11ea-97a9-12813bfff9fa; Mon, 27 Apr 2020 15:16:01 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 0CC60AE2E; Mon, 27 Apr 2020 15:15:59 +0000 (UTC) Subject: Re: [PATCH] x86: refine guest_mode() To: Andrew Cooper References: <7b62d06c-1369-2857-81c0-45e2434357f4@suse.com> <1704f4f6-7e77-971c-2c94-4f6a6719c34a@citrix.com> From: Jan Beulich Message-ID: <5bbe6425-396c-d934-b5af-53b594a4afbc@suse.com> Date: Mon, 27 Apr 2020 17:15:52 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <1704f4f6-7e77-971c-2c94-4f6a6719c34a@citrix.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: "xen-devel@lists.xenproject.org" , Wei Liu , =?UTF-8?Q?Roger_Pau_Monn=c3=a9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" On 27.04.2020 16:35, Andrew Cooper wrote: > On 27/04/2020 09:03, Jan Beulich wrote: >> The 2nd of the assertions as well as the macro's return value have been >> assuming we're on the primary stack. While for most IST exceptions we >> eventually switch back to the main one, > > "we switch to the main one when interrupting user mode". > > "eventually" isn't accurate as it is before we enter C. Right, will change. >> --- a/xen/include/asm-x86/regs.h >> +++ b/xen/include/asm-x86/regs.h >> @@ -10,9 +10,10 @@ >> /* Frame pointer must point into current CPU stack. */ \ >> ASSERT(diff < STACK_SIZE); \ >> /* If not a guest frame, it must be a hypervisor frame. */ \ >> - ASSERT((diff == 0) || (r->cs == __HYPERVISOR_CS)); \ >> + if ( diff < PRIMARY_STACK_SIZE ) \ >> + ASSERT(!diff || ((r)->cs == __HYPERVISOR_CS)); \ >> /* Return TRUE if it's a guest frame. */ \ >> - (diff == 0); \ >> + !diff || ((r)->cs != __HYPERVISOR_CS); \ > > The (diff == 0) already worried me before because it doesn't fail safe, > but this makes things more problematic.  Consider the case back when we > had __HYPERVISOR_CS32. Yes - if __HYPERVISOR_CS32 would ever have been to be used for anything, it would have needed checking for here. > Guest mode is strictly "(r)->cs & 3". As long as CS (a) gets properly saved (it's a "manual" step for SYSCALL/SYSRET as well as #VMEXIT) and (b) didn't get clobbered. I didn't write this code, I don't think, so I can only guess that there were intentions behind this along these lines. > Everything else is expectations about how things ought to be laid out, > but for safety in release builds, the final judgement should not depend > on the expectations evaluating true. Well, I can switch to a purely CS.RPL based approach, as long as we're happy to live with the possible downside mentioned above. Of course this would then end up being a more intrusive change than originally intended ... Jan