From: "Jan Beulich" Subject: [PATCH 3/5] x86: make get_page_from_l1e() return a proper error code Date: Wed, 09 Mar 2011 10:56:21 +0000 Message-ID: <4D776AE50200007800035764@vpn.id2.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartCBE7FCC5.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. --=__PartCBE7FCC5.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline ... so that the guest can actually know the reason for the (hypercall) failure. ptwr_do_page_fault() could propagate the error indicator received from get_page_from_l1e() back to the guest in the high half of the error code (entry_vector), provided we're sure all existing guests can deal with that (or indicate so by means of a to-be-added guest feature flag). Alternatively, a second virtual status register (like CR2) could be introduced. Signed-off-by: Jan Beulich --- 2011-03-09.orig/xen/arch/x86/mm.c +++ 2011-03-09/xen/arch/x86/mm.c @@ -799,12 +799,12 @@ get_page_from_l1e( bool_t write; =20 if ( !(l1f & _PAGE_PRESENT) ) - return 1; + return 0; =20 if ( unlikely(l1f & l1_disallow_mask(l1e_owner)) ) { MEM_LOG("Bad L1 flags %x", l1f & l1_disallow_mask(l1e_owner)); - return 0; + return -EINVAL; } =20 if ( !mfn_valid(mfn) || @@ -821,18 +821,21 @@ get_page_from_l1e( if ( !iomem_access_permitted(pg_owner, mfn, mfn) ) { if ( mfn !=3D (PADDR_MASK >> PAGE_SHIFT) ) /* INVALID_MFN? */ + { MEM_LOG("Non-privileged (%u) attempt to map I/O space = %08lx",=20 pg_owner->domain_id, mfn); - return 0; + return -EPERM; + } + return -EINVAL; } =20 if ( !(l1f & _PAGE_RW) || IS_PRIV(pg_owner) || !rangeset_contains_singleton(mmio_ro_ranges, mfn) ) - return 1; + return 0; dprintk(XENLOG_G_WARNING, "d%d: Forcing read-only access to MFN %lx\n", l1e_owner->domain_id, mfn); - return -1; + return 1; } =20 if ( unlikely(real_pg_owner !=3D pg_owner) ) @@ -863,6 +866,7 @@ get_page_from_l1e( { unsigned long x, nx, y =3D page->count_info; unsigned long cacheattr =3D pte_flags_to_cacheattr(l1f); + int err; =20 if ( is_xen_heap_page(page) ) { @@ -870,7 +874,7 @@ get_page_from_l1e( put_page_type(page); put_page(page); MEM_LOG("Attempt to change cache attributes of Xen heap = page"); - return 0; + return -EACCES; } =20 do { @@ -878,7 +882,8 @@ get_page_from_l1e( nx =3D (x & ~PGC_cacheattr_mask) | (cacheattr << PGC_cacheattr= _base); } while ( (y =3D cmpxchg(&page->count_info, x, nx)) !=3D x ); =20 - if ( unlikely(update_xen_mappings(mfn, cacheattr) !=3D 0) ) + err =3D update_xen_mappings(mfn, cacheattr); + if ( unlikely(err) ) { cacheattr =3D y & PGC_cacheattr_mask; do { @@ -894,11 +899,11 @@ get_page_from_l1e( " from L1 entry %" PRIpte ") for %d", mfn, get_gpfn_from_mfn(mfn), l1e_get_intpte(l1e), l1e_owner->domain_id); - return 0; + return err; } } =20 - return 1; + return 0; =20 could_not_pin: MEM_LOG("Error getting mfn %lx (pfn %lx) from L1 entry %" PRIpte @@ -907,7 +912,7 @@ get_page_from_l1e( l1e_get_intpte(l1e), l1e_owner->domain_id, pg_owner->domain_id= ); if ( real_pg_owner !=3D NULL ) put_page(page); - return 0; + return -EBUSY; } =20 =20 @@ -1197,17 +1202,20 @@ static int alloc_l1_table(struct page_in unsigned long pfn =3D page_to_mfn(page); l1_pgentry_t *pl1e; unsigned int i; + int ret =3D 0; =20 pl1e =3D map_domain_page(pfn); =20 for ( i =3D 0; i < L1_PAGETABLE_ENTRIES; i++ ) { if ( is_guest_l1_slot(i) ) - switch ( get_page_from_l1e(pl1e[i], d, d) ) + switch ( ret =3D get_page_from_l1e(pl1e[i], d, d) ) { - case 0: + default: goto fail; - case -1: + case 0: + break; + case 1: l1e_remove_flags(pl1e[i], _PAGE_RW); break; } @@ -1225,7 +1233,7 @@ static int alloc_l1_table(struct page_in put_page_from_l1e(pl1e[i], d); =20 unmap_domain_page(pl1e); - return -EINVAL; + return ret; } =20 static int create_pae_xen_mappings(struct domain *d, l3_pgentry_t *pl3e) @@ -1794,11 +1802,13 @@ static int mod_l1_entry(l1_pgentry_t *pl return rc; } =20 - switch ( get_page_from_l1e(nl1e, pt_dom, pg_dom) ) + switch ( rc =3D get_page_from_l1e(nl1e, pt_dom, pg_dom) ) { - case 0: + default: return 0; - case -1: + case 0: + break; + case 1: l1e_remove_flags(nl1e, _PAGE_RW); break; } @@ -4948,7 +4958,7 @@ static int ptwr_emulated_update( nl1e =3D l1e_from_intpte(val); switch ( get_page_from_l1e(nl1e, d, d) ) { - case 0: + default: if ( is_pv_32bit_domain(d) && (bytes =3D=3D 4) && (unaligned_addr = & 4) && !do_cmpxchg && (l1e_get_flags(nl1e) & _PAGE_PRESENT) ) { @@ -4968,7 +4978,9 @@ static int ptwr_emulated_update( return X86EMUL_UNHANDLEABLE; } break; - case -1: + case 0: + break; + case 1: l1e_remove_flags(nl1e, _PAGE_RW); break; } --- 2011-03-09.orig/xen/arch/x86/mm/shadow/multi.c +++ 2011-03-09/xen/arch/x86/mm/shadow/multi.c @@ -872,7 +872,7 @@ shadow_get_page_from_l1e(shadow_l1e_t sl // If a privileged domain is attempting to install a map of a page it = does // not own, we let it succeed anyway. // - if ( unlikely(!res) && + if ( unlikely(res < 0) && !shadow_mode_translate(d) && mfn_valid(mfn =3D shadow_l1e_get_mfn(sl1e)) && (owner =3D page_get_owner(mfn_to_page(mfn))) && @@ -883,11 +883,11 @@ shadow_get_page_from_l1e(shadow_l1e_t sl SHADOW_PRINTK("privileged domain %d installs map of mfn %05lx " "which is owned by domain %d: %s\n", d->domain_id, mfn_x(mfn), owner->domain_id, - res ? "success" : "failed"); + res >=3D 0 ? "success" : "failed"); } =20 /* Okay, it might still be a grant mapping PTE. Try it. */ - if ( unlikely(!res) && + if ( unlikely(res < 0) && (type =3D=3D p2m_grant_map_rw || (type =3D=3D p2m_grant_map_ro && !(shadow_l1e_get_flags(sl1e) & _PAGE_RW))) ) @@ -900,7 +900,7 @@ shadow_get_page_from_l1e(shadow_l1e_t sl res =3D get_page_from_l1e(sl1e, d, page_get_owner(mfn_to_page(= mfn))); } =20 - if ( unlikely(!res) ) + if ( unlikely(res < 0) ) { perfc_incr(shadow_get_page_fail); SHADOW_PRINTK("failed: l1e=3D" SH_PRI_pte "\n"); @@ -1229,15 +1229,15 @@ static int shadow_set_l1e(struct vcpu *v TRACE_SHADOW_PATH_FLAG(TRCE_SFLAG_SHADOW_L1_GET_REF); switch ( shadow_get_page_from_l1e(new_sl1e, d, new_type) ) { - case 0: + default: /* Doesn't look like a pagetable. */ flags |=3D SHADOW_SET_ERROR; new_sl1e =3D shadow_l1e_empty(); break; - case -1: + case 1: shadow_l1e_remove_flags(new_sl1e, _PAGE_RW); /* fall through */ - default: + case 0: shadow_vram_get_l1e(new_sl1e, sl1e, sl1mfn, d); break; } --=__PartCBE7FCC5.0__= Content-Type: text/plain; name="x86-get_page_from_l1e-retcode.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-get_page_from_l1e-retcode.patch" ... so that the guest can actually know the reason for the (hypercall)=0Afa= ilure.=0A=0Aptwr_do_page_fault() could propagate the error indicator = received from=0Aget_page_from_l1e() back to the guest in the high half of = the error=0Acode (entry_vector), provided we're sure all existing guests = can deal=0Awith that (or indicate so by means of a to-be-added guest = feature=0Aflag). Alternatively, a second virtual status register (like = CR2) could=0Abe introduced.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- 2011-03-09.orig/xen/arch/x86/mm.c=0A+++ 2011-03-09/xen/arch= /x86/mm.c=0A@@ -799,12 +799,12 @@ get_page_from_l1e(=0A bool_t = write;=0A =0A if ( !(l1f & _PAGE_PRESENT) )=0A- return 1;=0A+ = return 0;=0A =0A if ( unlikely(l1f & l1_disallow_mask(l1e_owner)) = )=0A {=0A MEM_LOG("Bad L1 flags %x", l1f & l1_disallow_mask(l1e= _owner));=0A- return 0;=0A+ return -EINVAL;=0A }=0A =0A = if ( !mfn_valid(mfn) ||=0A@@ -821,18 +821,21 @@ get_page_from_l1e(=0A = if ( !iomem_access_permitted(pg_owner, mfn, mfn) )=0A {=0A = if ( mfn !=3D (PADDR_MASK >> PAGE_SHIFT) ) /* INVALID_MFN? = */=0A+ {=0A MEM_LOG("Non-privileged (%u) = attempt to map I/O space %08lx", =0A pg_owner->doma= in_id, mfn);=0A- return 0;=0A+ return -EPERM;=0A+= }=0A+ return -EINVAL;=0A }=0A =0A = if ( !(l1f & _PAGE_RW) || IS_PRIV(pg_owner) ||=0A !rangeset_co= ntains_singleton(mmio_ro_ranges, mfn) )=0A- return 1;=0A+ = return 0;=0A dprintk(XENLOG_G_WARNING,=0A = "d%d: Forcing read-only access to MFN %lx\n",=0A l1e_owner-= >domain_id, mfn);=0A- return -1;=0A+ return 1;=0A }=0A = =0A if ( unlikely(real_pg_owner !=3D pg_owner) )=0A@@ -863,6 +866,7 @@ = get_page_from_l1e(=0A {=0A unsigned long x, nx, y =3D = page->count_info;=0A unsigned long cacheattr =3D pte_flags_to_cache= attr(l1f);=0A+ int err;=0A =0A if ( is_xen_heap_page(page) = )=0A {=0A@@ -870,7 +874,7 @@ get_page_from_l1e(=0A = put_page_type(page);=0A put_page(page);=0A = MEM_LOG("Attempt to change cache attributes of Xen heap page");=0A- = return 0;=0A+ return -EACCES;=0A }=0A =0A = do {=0A@@ -878,7 +882,8 @@ get_page_from_l1e(=0A nx =3D (x & = ~PGC_cacheattr_mask) | (cacheattr << PGC_cacheattr_base);=0A } = while ( (y =3D cmpxchg(&page->count_info, x, nx)) !=3D x );=0A =0A- = if ( unlikely(update_xen_mappings(mfn, cacheattr) !=3D 0) )=0A+ err = =3D update_xen_mappings(mfn, cacheattr);=0A+ if ( unlikely(err) = )=0A {=0A cacheattr =3D y & PGC_cacheattr_mask;=0A = do {=0A@@ -894,11 +899,11 @@ get_page_from_l1e(=0A = " from L1 entry %" PRIpte ") for %d",=0A mfn, = get_gpfn_from_mfn(mfn),=0A l1e_get_intpte(l1e), = l1e_owner->domain_id);=0A- return 0;=0A+ return = err;=0A }=0A }=0A =0A- return 1;=0A+ return 0;=0A =0A = could_not_pin:=0A MEM_LOG("Error getting mfn %lx (pfn %lx) from L1 = entry %" PRIpte=0A@@ -907,7 +912,7 @@ get_page_from_l1e(=0A = l1e_get_intpte(l1e), l1e_owner->domain_id, pg_owner->domain_id);=0A if = ( real_pg_owner !=3D NULL )=0A put_page(page);=0A- return = 0;=0A+ return -EBUSY;=0A }=0A =0A =0A@@ -1197,17 +1202,20 @@ static int = alloc_l1_table(struct page_in=0A unsigned long pfn =3D page_to_mfn(pag= e);=0A l1_pgentry_t *pl1e;=0A unsigned int i;=0A+ int = ret =3D 0;=0A =0A pl1e =3D map_domain_page(pfn);=0A =0A for ( = i =3D 0; i < L1_PAGETABLE_ENTRIES; i++ )=0A {=0A if ( = is_guest_l1_slot(i) )=0A- switch ( get_page_from_l1e(pl1e[i], = d, d) )=0A+ switch ( ret =3D get_page_from_l1e(pl1e[i], d, d) = )=0A {=0A- case 0:=0A+ default:=0A = goto fail;=0A- case -1:=0A+ case 0:=0A+ = break;=0A+ case 1:=0A l1e_remove_fla= gs(pl1e[i], _PAGE_RW);=0A break;=0A }=0A@@ = -1225,7 +1233,7 @@ static int alloc_l1_table(struct page_in=0A = put_page_from_l1e(pl1e[i], d);=0A =0A unmap_domain_page(pl1e);=0A- = return -EINVAL;=0A+ return ret;=0A }=0A =0A static int create_pae_xen_ma= ppings(struct domain *d, l3_pgentry_t *pl3e)=0A@@ -1794,11 +1802,13 @@ = static int mod_l1_entry(l1_pgentry_t *pl=0A return rc;=0A = }=0A =0A- switch ( get_page_from_l1e(nl1e, pt_dom, pg_dom) )=0A+ = switch ( rc =3D get_page_from_l1e(nl1e, pt_dom, pg_dom) )=0A = {=0A- case 0:=0A+ default:=0A return 0;=0A- = case -1:=0A+ case 0:=0A+ break;=0A+ case 1:=0A = l1e_remove_flags(nl1e, _PAGE_RW);=0A break;=0A = }=0A@@ -4948,7 +4958,7 @@ static int ptwr_emulated_update(=0A nl1e = =3D l1e_from_intpte(val);=0A switch ( get_page_from_l1e(nl1e, d, d) = )=0A {=0A- case 0:=0A+ default:=0A if ( is_pv_32bit_domai= n(d) && (bytes =3D=3D 4) && (unaligned_addr & 4) &&=0A = !do_cmpxchg && (l1e_get_flags(nl1e) & _PAGE_PRESENT) )=0A {=0A@@ = -4968,7 +4978,9 @@ static int ptwr_emulated_update(=0A return = X86EMUL_UNHANDLEABLE;=0A }=0A break;=0A- case -1:=0A+ = case 0:=0A+ break;=0A+ case 1:=0A l1e_remove_flags(nl1e,= _PAGE_RW);=0A break;=0A }=0A--- 2011-03-09.orig/xen/arch/x86/m= m/shadow/multi.c=0A+++ 2011-03-09/xen/arch/x86/mm/shadow/multi.c=0A@@ = -872,7 +872,7 @@ shadow_get_page_from_l1e(shadow_l1e_t sl=0A // If a = privileged domain is attempting to install a map of a page it does=0A = // not own, we let it succeed anyway.=0A //=0A- if ( unlikely(!res) = &&=0A+ if ( unlikely(res < 0) &&=0A !shadow_mode_translate(d) = &&=0A mfn_valid(mfn =3D shadow_l1e_get_mfn(sl1e)) &&=0A = (owner =3D page_get_owner(mfn_to_page(mfn))) &&=0A@@ -883,11 +883,11 @@ = shadow_get_page_from_l1e(shadow_l1e_t sl=0A SHADOW_PRINTK("privileg= ed domain %d installs map of mfn %05lx "=0A "which = is owned by domain %d: %s\n",=0A d->domain_id, = mfn_x(mfn), owner->domain_id,=0A- res ? "success" : = "failed");=0A+ res >=3D 0 ? "success" : "failed");=0A= }=0A =0A /* Okay, it might still be a grant mapping PTE. Try it. = */=0A- if ( unlikely(!res) &&=0A+ if ( unlikely(res < 0) &&=0A = (type =3D=3D p2m_grant_map_rw ||=0A (type =3D=3D p2m_grant_map= _ro &&=0A !(shadow_l1e_get_flags(sl1e) & _PAGE_RW))) )=0A@@ = -900,7 +900,7 @@ shadow_get_page_from_l1e(shadow_l1e_t sl=0A = res =3D get_page_from_l1e(sl1e, d, page_get_owner(mfn_to_page(mfn)));=0A = }=0A =0A- if ( unlikely(!res) )=0A+ if ( unlikely(res < 0) )=0A = {=0A perfc_incr(shadow_get_page_fail);=0A SHADOW_PRINTK("f= ailed: l1e=3D" SH_PRI_pte "\n");=0A@@ -1229,15 +1229,15 @@ static int = shadow_set_l1e(struct vcpu *v=0A TRACE_SHADOW_PATH_FLAG(TRCE_SF= LAG_SHADOW_L1_GET_REF);=0A switch ( shadow_get_page_from_l1e(ne= w_sl1e, d, new_type) )=0A {=0A- case 0:=0A+ = default:=0A /* Doesn't look like a pagetable. */=0A = flags |=3D SHADOW_SET_ERROR;=0A new_sl1e =3D = shadow_l1e_empty();=0A break;=0A- case -1:=0A+ = case 1:=0A shadow_l1e_remove_flags(new_sl1e, = _PAGE_RW);=0A /* fall through */=0A- default:=0A= + case 0:=0A shadow_vram_get_l1e(new_sl1e, = sl1e, sl1mfn, d);=0A break;=0A }=0A --=__PartCBE7FCC5.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 --=__PartCBE7FCC5.0__=--