From: "Jan Beulich" Subject: [RFC] x86: cacheability page attributes Date: Tue, 03 Apr 2007 15:00:25 +0100 Message-ID: <46127A18.76E4.0078.0@novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part88AFD1E9.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part88AFD1E9.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline Attached draft patch is supposed to help dealing with tracking cacheability attributes on x86, specifically to prevent page aliases using different cacheability attributes. The problem with the approach of extending the set of page types (by splitting PGT_writable_page) is that a simple update of a pte which changes nothing but _PAGE_PWT or _PAGE_PCD doesn't work, as the scheme requires the old type (e.g. simple writeable page) to first drop its type reference count to zero, before the new type can be established. While in all previous scenarios this doesn't present a problem, here it simply seems wrong: If the only use is through the pte being updated, this one reference shouldn't be considered. As I understand it, racing requests to bump the type reference count could be avoided by clearing PGT_validated while retaining the single reference, but what I don't have a reasonable idea for is how (and where) to handle the transition while retaining the type reference. Thanks, Jan --=__Part88AFD1E9.0__= Content-Type: text/plain; name="page-cacheability.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="page-cacheability.patch" Index: 2007-03-19/xen/arch/x86/mm.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 2007-03-19.orig/xen/arch/x86/mm.c 2007-03-27 10:32:56.000000000 = +0200 +++ 2007-03-19/xen/arch/x86/mm.c 2007-04-03 14:33:46.000000000 = +0200 @@ -146,6 +146,12 @@ struct page_info *frame_table; unsigned long max_page; unsigned long total_pages; =20 +#define l1_disallow_mask(d) (!(d)->iomem_caps || \ + !rangeset_is_empty((d)->iomem_caps) || \ + !rangeset_is_empty((d)->arch.ioport_caps) ? = \ + L1_DISALLOW_MASK : \ + L1_DISALLOW_MASK|_PAGE_PAT|_PAGE_PCD|_PAGE_PW= T) + #ifdef CONFIG_COMPAT l2_pgentry_t *compat_idle_pg_table_l2 =3D NULL; #define l3_disallow_mask(d) (!IS_COMPAT(d) ? \ @@ -256,9 +262,10 @@ void share_xen_page_with_guest( =20 spin_lock(&d->page_alloc_lock); =20 - /* The incremented type count pins as writable or read-only. */ page->u.inuse.type_info =3D (readonly ? PGT_none : PGT_writable_page)= ; - page->u.inuse.type_info |=3D PGT_validated | 1; + if ( readonly || d !=3D dom_io ) + /* The incremented type count pins as writable or read-only. */ + page->u.inuse.type_info |=3D PGT_validated | 1; =20 page_set_owner(page, d); wmb(); /* install valid domain ptr before updating refcnt. */ @@ -577,15 +584,16 @@ get_page_from_l1e( l1_pgentry_t l1e, struct domain *d) { unsigned long mfn =3D l1e_get_pfn(l1e); + unsigned int flags =3D l1e_get_flags(l1e); struct page_info *page =3D mfn_to_page(mfn); int okay; =20 - if ( !(l1e_get_flags(l1e) & _PAGE_PRESENT) ) + if ( !(flags & _PAGE_PRESENT) ) return 1; =20 - if ( unlikely(l1e_get_flags(l1e) & L1_DISALLOW_MASK) ) + if ( unlikely(flags & l1_disallow_mask(d)) ) { - MEM_LOG("Bad L1 flags %x", l1e_get_flags(l1e) & L1_DISALLOW_MASK);= + MEM_LOG("Bad L1 flags %x", flags & l1_disallow_mask(d)); return 0; } =20 @@ -615,10 +623,23 @@ get_page_from_l1e( * contribute to writeable mapping refcounts. (This allows the * qemu-dm helper process in dom0 to map the domain's memory without * messing up the count of "real" writable mappings.) */ - okay =3D (((l1e_get_flags(l1e) & _PAGE_RW) &&=20 - !(unlikely(paging_mode_external(d) && (d !=3D current->domain= )))) - ? get_page_and_type(page, d, PGT_writable_page) - : get_page(page, d)); + if ( !(flags & (_PAGE_RW|_PAGE_PWT|_PAGE_PCD|_PAGE_PAT)) || + (unlikely(paging_mode_external(d) && (d !=3D current->domain))) = ) + okay =3D get_page(page, d); + else + { + unsigned long type =3D PGT_writable_page; + + if ( flags & _PAGE_PWT ) + type |=3D PGT_pwt_mask; + if ( flags & _PAGE_PCD ) + type |=3D PGT_pcd_mask; +#ifdef CONFIG_PAT + if ( flags & _PAGE_PAT ) + type |=3D PGT_pat_mask; +#endif + okay =3D get_page_and_type(page, d, type); + } if ( !okay ) { MEM_LOG("Error getting mfn %lx (pfn %lx) from L1 entry %" PRIpte @@ -1331,10 +1352,10 @@ static int mod_l1_entry(l1_pgentry_t *pl nl1e =3D l1e_from_pfn(gmfn_to_mfn(FOREIGNDOM, l1e_get_pfn(nl1e)), l1e_get_flags(nl1e)); =20 - if ( unlikely(l1e_get_flags(nl1e) & L1_DISALLOW_MASK) ) + if ( unlikely(l1e_get_flags(nl1e) & l1_disallow_mask(d)) ) { MEM_LOG("Bad L1 flags %x", - l1e_get_flags(nl1e) & L1_DISALLOW_MASK); + l1e_get_flags(nl1e) & l1_disallow_mask(d)); return 0; } =20 @@ -1539,10 +1560,41 @@ static int mod_l4_entry(struct domain *d =20 #endif =20 -int alloc_page_type(struct page_info *page, unsigned long type) +static int alloc_page_type(struct page_info *page, unsigned long type) { struct domain *owner =3D page_get_owner(page); =20 + if ( type & PGT_writable_page ) + { + unsigned long mfn =3D page_to_mfn(page); + unsigned long flags =3D 0; + int ret; + + if ( owner =3D=3D dom_io ) + return 1; +#ifdef __i386__ + if ( mfn >=3D ((DIRECTMAP_VIRT_END - DIRECTMAP_VIRT_START) >> = PAGE_SHIFT) ) + return 1; +#endif + + if ( type & PGT_pwt_mask ) + flags |=3D _PAGE_PWT; + if ( type & PGT_pcd_mask ) + flags |=3D _PAGE_PCD; +#ifdef CONFIG_PAT + if ( type & PGT_pat_mask ) + flags |=3D _PAGE_PAT; +#endif + ASSERT(flags); + ret =3D map_pages_to_xen((unsigned long)mfn_to_virt(mfn), mfn, 1, + PAGE_HYPERVISOR | flags); + if ( ret =3D=3D 0 ) + return 1; + + MEM_LOG("Error %d changing cacheability of mfn %lx", ret, mfn); + return 0; + } + /* A page table is dirtied when its type count becomes non-zero. */ if ( likely(owner !=3D NULL) ) mark_dirty(owner, page_to_mfn(page)); @@ -1576,6 +1628,27 @@ void free_page_type(struct page_info *pa struct domain *owner =3D page_get_owner(page); unsigned long gmfn; =20 + if ( type & PGT_writable_page ) + { + unsigned long mfn =3D page_to_mfn(page); + + if ( owner =3D=3D dom_io ) + return; +#ifdef __i386__ + if ( mfn >=3D ((DIRECTMAP_VIRT_END - DIRECTMAP_VIRT_START) >> = PAGE_SHIFT) ) + return; +#endif + + if ( map_pages_to_xen((unsigned long)mfn_to_virt(mfn), mfn, 1, + PAGE_HYPERVISOR) ) + { + printk("Reverting cacheability for %lx failed\n", mfn); + BUG(); + } + + return; + } + if ( likely(owner !=3D NULL) ) { /* @@ -1645,11 +1718,13 @@ void put_page_type(struct page_info *pag =20 if ( unlikely((nx & PGT_count_mask) =3D=3D 0) ) { - if ( unlikely((nx & PGT_type_mask) <=3D PGT_l4_page_table) && + if ( (unlikely((nx & PGT_type_mask) <=3D PGT_l4_page_table) = || + unlikely((nx & PGT_type_mask) > PGT_writable_page)) && likely(nx & PGT_validated) ) { /* - * Page-table pages must be unvalidated when count is = zero. The + * Page-table pages and writable pages with non-default + * cacheability must be unvalidated when count is zero. = The * 'free' is safe because the refcnt is non-zero and = validated * bit is clear =3D> other ops will spin or fail. */ @@ -1718,7 +1793,7 @@ int get_page_type(struct page_info *page if ( unlikely(!cpus_empty(mask)) && /* Shadow mode: track only writable pages. */ (!shadow_mode_enabled(page_get_owner(page)) || - ((nx & PGT_type_mask) =3D=3D PGT_writable_page)) ) + (nx & PGT_writable_page)) ) { perfc_incrc(need_flush_tlb_flush); flush_tlb_mask(mask); @@ -3435,6 +3510,8 @@ int map_pages_to_xen( if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ) { pl1e =3D alloc_xen_pagetable(); + if ( pl1e =3D=3D NULL ) + return -ENOMEM; clear_page(pl1e); l2e_write(pl2e, l2e_from_pfn(virt_to_mfn(pl1e), __PAGE_HYPERVISOR)); @@ -3442,6 +3519,8 @@ int map_pages_to_xen( else if ( l2e_get_flags(*pl2e) & _PAGE_PSE ) { pl1e =3D alloc_xen_pagetable(); + if ( pl1e =3D=3D NULL ) + return -ENOMEM; for ( i =3D 0; i < L1_PAGETABLE_ENTRIES; i++ ) l1e_write(&pl1e[i], l1e_from_pfn(l2e_get_pfn(*pl2e) + i, @@ -3460,6 +3539,28 @@ int map_pages_to_xen( virt +=3D 1UL << L1_PAGETABLE_SHIFT; mfn +=3D 1UL; nr_mfns -=3D 1UL; + + if ( !map_small_pages && + flags =3D=3D PAGE_HYPERVISOR && + ( nr_mfns =3D=3D 0 || + ((((virt>>PAGE_SHIFT) | mfn) & ((1<shadow_flags & (1<type)) ); /* Bad type count on guest page? */ - if ( (gpg->u.inuse.type_info & PGT_type_mask) =3D=3D = PGT_writable_page=20 + if ( (gpg->u.inuse.type_info & PGT_writable_page) && (gpg->u.inuse.type_info & PGT_count_mask) !=3D 0 ) { SHADOW_ERROR("MFN %#lx shadowed (by %#"PRI_mfn")" Index: 2007-03-19/xen/arch/x86/mm/shadow/multi.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 2007-03-19.orig/xen/arch/x86/mm/shadow/multi.c 2007-03-19 = 13:24:00.000000000 +0100 +++ 2007-03-19/xen/arch/x86/mm/shadow/multi.c 2007-04-03 11:26:31.0000000= 00 +0200 @@ -4135,8 +4135,7 @@ audit_gfn_to_mfn(struct vcpu *v, gfn_t g if ( !shadow_mode_translate(v->domain) ) return _mfn(gfn_x(gfn)); =20 - if ( (mfn_to_page(gmfn)->u.inuse.type_info & PGT_type_mask) - !=3D PGT_writable_page )=20 + if ( !(mfn_to_page(gmfn)->u.inuse.type_info & PGT_writable_page) ) return _mfn(gfn_x(gfn)); /* This is a paging-disabled shadow */ else=20 return gfn_to_mfn(v->domain, gfn_x(gfn)); Index: 2007-03-19/xen/include/asm-x86/mm.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 2007-03-19.orig/xen/include/asm-x86/mm.h 2007-03-19 13:17:45.0000000= 00 +0100 +++ 2007-03-19/xen/include/asm-x86/mm.h 2007-04-03 10:04:07.000000000 = +0200 @@ -64,24 +64,35 @@ struct page_info }; =20 /* The following page types are MUTUALLY EXCLUSIVE. */ -#define PGT_none (0U<<29) /* no special uses of this page */ -#define PGT_l1_page_table (1U<<29) /* using this page as an L1 page = table? */ -#define PGT_l2_page_table (2U<<29) /* using this page as an L2 page = table? */ -#define PGT_l3_page_table (3U<<29) /* using this page as an L3 page = table? */ -#define PGT_l4_page_table (4U<<29) /* using this page as an L4 page = table? */ -#define PGT_gdt_page (5U<<29) /* using this page in a GDT? */ -#define PGT_ldt_page (6U<<29) /* using this page in an LDT? */ -#define PGT_writable_page (7U<<29) /* has writable mappings of this = page? */ -#define PGT_type_mask (7U<<29) /* Bits 29-31. */ +#define PGT_none (0U<<28) /* no special uses of this page */ +#define PGT_l1_page_table (1U<<28) /* using this page as an L1 page = table? */ +#define PGT_l2_page_table (2U<<28) /* using this page as an L2 page = table? */ +#define PGT_l3_page_table (3U<<28) /* using this page as an L3 page = table? */ +#define PGT_l4_page_table (4U<<28) /* using this page as an L4 page = table? */ +#define PGT_gdt_page (5U<<28) /* using this page in a GDT? */ +#define PGT_ldt_page (6U<<28) /* using this page in an LDT? */ +#define PGT_writable_page (0x8U<<28) /* has writable mappings of this = page? */ +#define PGT_pwt_mask (0x1U<<28) /* (l1e & _PAGE_PWT) mirror */ +#define PGT_pcd_mask (0x2U<<28) /* (l1e & _PAGE_PCD) mirror */ +#define PGT_wb_page (0x8U<<28) /* WB cached writable page? */ +#define PGT_wt_page (0x9U<<28) /* WT cached writable page? */ +#define PGT_ucm_page (0xAU<<28) /* UC- cached writable page? */ +#define PGT_uc_page (0xBU<<28) /* UC cached writable page? */ +#ifdef CONFIG_PAT +#define PGT_pat_mask (0x4U<<28) /* (l1e & _PAGE_PAT) mirror */ +#define PGT_wc_page (0xCU<<28) /* WC cached writable page? */ +#define PGT_wp_page (0xDU<<28) /* WP cached writable page? */ +#endif +#define PGT_type_mask (0xFU<<28) /* Bits 28-31. */ =20 /* Owning guest has pinned this page to its current type? */ -#define _PGT_pinned 28 +#define _PGT_pinned 22 #define PGT_pinned (1U<<_PGT_pinned) /* Has this page been validated for use as its current type? */ -#define _PGT_validated 27 +#define _PGT_validated 21 #define PGT_validated (1U<<_PGT_validated) /* PAE only: is this an L2 page directory containing Xen-private = mappings? */ -#define _PGT_pae_xen_l2 26 +#define _PGT_pae_xen_l2 20 #define PGT_pae_xen_l2 (1U<<_PGT_pae_xen_l2) =20 /* 16-bit count of uses of this frame as its current type. */ @@ -141,7 +152,6 @@ extern unsigned long max_page; extern unsigned long total_pages; void init_frametable(void); =20 -int alloc_page_type(struct page_info *page, unsigned long type); void free_page_type(struct page_info *page, unsigned long type); int _shadow_mode_refcounts(struct domain *d); =20 Index: 2007-03-19/xen/include/asm-x86/x86_32/page-3level.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 2007-03-19.orig/xen/include/asm-x86/x86_32/page-3level.h 2007-01-08 = 14:15:32.000000000 +0100 +++ 2007-03-19/xen/include/asm-x86/x86_32/page-3level.h 2007-04-03 = 09:10:24.000000000 +0200 @@ -85,6 +85,6 @@ typedef l3_pgentry_t root_pgentry_t; #define get_pte_flags(x) (((int)((x) >> 32) & ~0xFFF) | ((int)(x) & = 0xFFF)) #define put_pte_flags(x) (((intpte_t)((x) & ~0xFFF) << 32) | ((x) & = 0xFFF)) =20 -#define L3_DISALLOW_MASK 0xFFFFF1E6U /* must-be-zero */ +#define L3_DISALLOW_MASK 0xFFFFF1FEU /* must-be-zero */ =20 #endif /* __X86_32_PAGE_3LEVEL_H__ */ Index: 2007-03-19/xen/include/asm-x86/x86_32/page.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 2007-03-19.orig/xen/include/asm-x86/x86_32/page.h 2006-12-04 = 08:49:58.000000000 +0100 +++ 2007-03-19/xen/include/asm-x86/x86_32/page.h 2007-04-03 = 09:30:46.000000000 +0200 @@ -29,13 +29,13 @@ extern unsigned int PAGE_HYPERVISOR_NOCA (_PAGE_PRESENT|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_GNTTAB) =20 /* - * Disallow unused flag bits plus PAT, PSE and GLOBAL. + * Disallow unused flag bits plus PAT/PSE and GLOBAL. * Permit the NX bit if the hardware supports it. */ #define BASE_DISALLOW_MASK (0xFFFFF180U & ~_PAGE_NX) =20 #define L1_DISALLOW_MASK (BASE_DISALLOW_MASK | _PAGE_GNTTAB) -#define L2_DISALLOW_MASK (BASE_DISALLOW_MASK) +#define L2_DISALLOW_MASK (BASE_DISALLOW_MASK | _PAGE_PCD | _PAGE_PWT) =20 #endif /* __X86_32_PAGE_H__ */ =20 Index: 2007-03-19/xen/include/asm-x86/x86_64/page.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 2007-03-19.orig/xen/include/asm-x86/x86_64/page.h 2007-02-26 = 14:59:09.000000000 +0100 +++ 2007-03-19/xen/include/asm-x86/x86_64/page.h 2007-04-03 = 09:29:31.000000000 +0200 @@ -87,18 +87,18 @@ typedef l4_pgentry_t root_pgentry_t; #define _PAGE_NX (cpu_has_nx ? _PAGE_NX_BIT : 0U) =20 /* - * Disallow unused flag bits plus PAT, PSE and GLOBAL. + * Disallow unused flag bits plus PAT/PSE and GLOBAL. * Permit the NX bit if the hardware supports it. * Note that range [62:52] is available for software use on x86/64. */ #define BASE_DISALLOW_MASK (0xFF800180U & ~_PAGE_NX) =20 #define L1_DISALLOW_MASK (BASE_DISALLOW_MASK | _PAGE_GNTTAB) -#define L2_DISALLOW_MASK (BASE_DISALLOW_MASK) -#define L3_DISALLOW_MASK (BASE_DISALLOW_MASK) -#define L4_DISALLOW_MASK (BASE_DISALLOW_MASK) +#define L2_DISALLOW_MASK (BASE_DISALLOW_MASK | _PAGE_PCD | _PAGE_PWT) +#define L3_DISALLOW_MASK (BASE_DISALLOW_MASK | _PAGE_PCD | _PAGE_PWT) +#define L4_DISALLOW_MASK (BASE_DISALLOW_MASK | _PAGE_PCD | _PAGE_PWT) =20 -#define COMPAT_L3_DISALLOW_MASK 0xFFFFF1E6U +#define COMPAT_L3_DISALLOW_MASK 0xFFFFF1FEU =20 #define PAGE_HYPERVISOR (__PAGE_HYPERVISOR | _PAGE_GLOBAL)= #define PAGE_HYPERVISOR_NOCACHE (__PAGE_HYPERVISOR_NOCACHE | _PAGE_GLOBAL)= --=__Part88AFD1E9.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=__Part88AFD1E9.0__=--