From: "Jan Beulich" Subject: Re: [PATCH RFC v2 1/4] x86/mm: Shadow and p2m changes for PV mem_access Date: Fri, 25 Jul 2014 08:19:35 +0100 Message-ID: <53D221270200007800025CEA@mail.emea.novell.com> References: <1404787805-4540-1-git-send-email-aravindp@cisco.com> <1404787805-4540-2-git-send-email-aravindp@cisco.com> <53D13457020000780002599B@mail.emea.novell.com> <97A500D504438F4ABC02EBA81613CC633188B795@xmb-aln-x02.cisco.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1XAZn0-0003er-QS for xen-devel@lists.xenproject.org; Fri, 25 Jul 2014 07:19:39 +0000 In-Reply-To: <97A500D504438F4ABC02EBA81613CC633188B795@xmb-aln-x02.cisco.com> Content-Disposition: inline List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: "Aravindh Puthiyaparambil (aravindp)" Cc: "xen-devel@lists.xenproject.org" , Keir Fraser , Ian Jackson , Ian Campbell , Tim Deegan List-Id: xen-devel@lists.xenproject.org >>> On 25.07.14 at 01:34, wrote: >>> + >>> + /* If the access is against the permissions, then send to mem_event >>*/ >>> + switch ( p2ma ) >>> + { >>> + case p2m_access_r: >>> + violation = access_w || access_x; >>> + break; >>> + case p2m_access_rx: >>> + case p2m_access_rx2rw: >>> + violation = access_w; >>> + break; >>> + case p2m_access_rw: >>> + violation = access_x; >>> + break; >>> + case p2m_access_rwx: >>> + default: >>> + break; >>> + } >>> + >>> + /* >>> + * Do not police writes to guest memory from the Xen hypervisor. >>> + * This keeps PV mem_access on par with HVM. Turn off CR0.WP >>here to >>> + * allow the write to go through if the guest has marked the page as >>> + * writable. Turn it back on in the guest access functions >>> + * __copy_to_user / __put_user_size() after the write is completed. >>> + */ >>> + if ( violation && access_w && >>> + regs->eip >= XEN_VIRT_START && regs->eip <= XEN_VIRT_END ) >> >>Definitely < instead of <= on the right side. But - is this safe, the more >>that this again doesn't appear to be sitting in a guest kind specific block? >>I'd at least expect this to be qualified by a regs->cs and/or >>guest_mode() check. > > I will add the guest kind check. Should I do it for the whole block i.e add > is_pv_domain() in addition to mem_event_check_ring()? Since mem_event_check_ring() isn't PV specific, you'd need to go through and amend any such checks with a PV one where needed. And yes, doing it once for the whole code block seems like the right thing to do here. > That will also address > next comment below. I will add the guest_mode() in addition to the above > check for policing Xen writes to guest memory. > >>> + { >>> + unsigned long cr0 = read_cr0(); >>> + >>> + violation = 0; >>> + if ( cr0 & X86_CR0_WP && >>> + guest_l1e_get_flags(gw.l1e) & _PAGE_RW ) >>> + { >>> + cr0 &= ~X86_CR0_WP; >>> + write_cr0(cr0); >>> + v->arch.pv_vcpu.need_cr0_wp_set = 1; >> >>PV field access within a non-PV-only code block? >> >>> + } >> >>I wonder how well the window where you're running with CR0.WP clear >>is bounded: The flag serves as a kind of security measure, and hence >>shouldn't be left off for extended periods of time. > > I agree. Is there a way I can bound this? That's what you need to figure out. The simplistic solution (single stepping just the critical instruction(s)) is probably not going to be acceptable due to its fragility. I have no good other suggestions, but I'm not eager to allow code in that weakens protection. >>> --- a/xen/common/page_alloc.c >>> +++ b/xen/common/page_alloc.c >>> @@ -43,6 +43,7 @@ >>> #include >>> #include >>> #include >>> +#include >> >>Breaking ARM? >>> #ifdef CONFIG_X86 >>> #include >>> #include /* for highmem_start only */ >>> @@ -1660,6 +1661,8 @@ int assign_pages( >>> page_set_owner(&pg[i], d); >>> smp_wmb(); /* Domain pointer must be visible before updating refcnt. >>*/ >>> pg[i].count_info = PGC_allocated | 1; >>> + if ( is_pv_domain(d) ) >>> + shadow_set_access(&pg[i], p2m_get_hostp2m(d)->default_access); >> >>I don't think you should call shadow code from here. > > Should I add a p2m wrapper for this, so that it is valid only for x86 and a > no-op for ARM? That's not necessarily enough, but at least presumably the right route: You also need to avoid fiddling with struct page_info fields that may be used (now or in the future) for other purposes, i.e. you need to gate the setting of the flags by more than just is_pv_domain(). >>> @@ -65,6 +67,11 @@ do { >> \ >>> case 8: __put_user_asm(x,ptr,retval,"q","","ir",errret);break; \ >>> default: __put_user_bad(); \ >>> } \ >>> + if ( unlikely(current->arch.pv_vcpu.need_cr0_wp_set) ) \ >>> + { \ >>> + write_cr0(read_cr0() | X86_CR0_WP); \ >>> + current->arch.pv_vcpu.need_cr0_wp_set = 0; \ >>> + } \ >>> } while (0) >> >>Please obey to the original indentation method. > > I thought tab characters were not allowed for indentation. But I guess you > do not want tabs and spaces for indentation purposes to be mixed within a > macro? Correct - ./CODING_STYLE explicitly says that you should match existing coding style when an entire file (or a significant code portion) was imported from elsewhere without style adjustment. Jan