From: "Jan Beulich" Subject: [PATCH] x86: synchronize PCI config space access decoding Date: Mon, 09 Mar 2015 16:08:41 +0000 Message-ID: <54FDD3990200007800067A47@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part9DA83999.1__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1YV0EZ-0006zX-3z for xen-devel@lists.xenproject.org; Mon, 09 Mar 2015 16:08:47 +0000 List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Andrew Cooper , Paul Durrant , Keir Fraser 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. --=__Part9DA83999.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Both PV and HVM logic have similar but not similar enough code here. Synchronize the two so that - in the HVM case we don't unconditionally try to access extended config space - in the PV case we pass a correct range to the XSM hook - in the PV case we don't needlessly deny access when the operation isn't really on PCI config space All this along with sharing the macros HVM already had here. Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -2383,11 +2383,6 @@ void hvm_vcpu_down(struct vcpu *v) static struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d, ioreq_t *p) { -#define CF8_BDF(cf8) (((cf8) & 0x00ffff00) >> 8) -#define CF8_ADDR_LO(cf8) ((cf8) & 0x000000fc) -#define CF8_ADDR_HI(cf8) (((cf8) & 0x0f000000) >> 16) -#define CF8_ENABLED(cf8) (!!((cf8) & 0x80000000)) - struct hvm_ioreq_server *s; uint32_t cf8; uint8_t type; @@ -2416,9 +2411,19 @@ static struct hvm_ioreq_server *hvm_sele =20 type =3D IOREQ_TYPE_PCI_CONFIG; addr =3D ((uint64_t)sbdf << 32) | - CF8_ADDR_HI(cf8) | CF8_ADDR_LO(cf8) | (p->addr & 3); + /* AMD extended configuration space access? */ + if ( CF8_ADDR_HI(cf8) && + boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_AMD && + boot_cpu_data.x86 >=3D 0x10 && boot_cpu_data.x86 <=3D 0x17 ) + { + uint64_t msr_val; + + if ( !rdmsr_safe(MSR_AMD64_NB_CFG, msr_val) && + (msr_val & (1ULL << AMD64_NB_CFG_CF8_EXT_ENABLE_BIT)) ) + addr |=3D CF8_ADDR_HI(cf8); + } } else { @@ -2472,11 +2477,6 @@ static struct hvm_ioreq_server *hvm_sele } =20 return d->arch.hvm_domain.default_ioreq_server; - -#undef CF8_ADDR_ENABLED -#undef CF8_ADDR_HI -#undef CF8_ADDR_LO -#undef CF8_BDF } =20 int hvm_buffered_io_send(ioreq_t *p) --- a/xen/arch/x86/traps.c +++ b/xen/arch/x86/traps.c @@ -1770,14 +1770,19 @@ static int admin_io_okay( return ioports_access_permitted(v->domain, port, port + bytes - 1); } =20 -static int pci_cfg_ok(struct domain *d, int write, int size) +static bool_t pci_cfg_ok(struct domain *d, bool_t write, + uint16_t start, uint16_t size) { uint32_t machine_bdf; - uint16_t start, end; - if (!is_hardware_domain(d)) + uint16_t end; + + if ( !is_hardware_domain(d) ) return 0; =20 - machine_bdf =3D (d->arch.pci_cf8 >> 8) & 0xFFFF; + if ( !CF8_ENABLED(d->arch.pci_cf8) ) + return 1; + + machine_bdf =3D CF8_BDF(d->arch.pci_cf8); if ( write ) { const unsigned long *ro_map =3D pci_get_ro_map(0); @@ -1785,9 +1790,9 @@ static int pci_cfg_ok(struct domain *d,=20 if ( ro_map && test_bit(machine_bdf, ro_map) ) return 0; } - start =3D d->arch.pci_cf8 & 0xFF; + start |=3D CF8_ADDR_LO(d->arch.pci_cf8); /* AMD extended configuration space access? */ - if ( (d->arch.pci_cf8 & 0x0F000000) && + if ( CF8_ADDR_HI(d->arch.pci_cf8) && boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_AMD && boot_cpu_data.x86 >=3D 0x10 && boot_cpu_data.x86 <=3D 0x17 ) { @@ -1796,7 +1801,7 @@ static int pci_cfg_ok(struct domain *d,=20 if ( rdmsr_safe(MSR_AMD64_NB_CFG, msr_val) ) return 0; if ( msr_val & (1ULL << AMD64_NB_CFG_CF8_EXT_ENABLE_BIT) ) - start |=3D (d->arch.pci_cf8 >> 16) & 0xF00; + start |=3D CF8_ADDR_HI(d->arch.pci_cf8); } end =3D start + size - 1; if (xsm_pci_config_permission(XSM_HOOK, d, machine_bdf, start, end, = write)) @@ -1855,7 +1860,7 @@ uint32_t guest_io_read( size =3D min(bytes, 4 - (port & 3)); if ( size =3D=3D 3 ) size =3D 2; - if ( pci_cfg_ok(v->domain, 0, size) ) + if ( pci_cfg_ok(v->domain, 0, port & 3, size) ) sub_data =3D pci_conf_read(v->domain->arch.pci_cf8, port = & 3, size); } =20 @@ -1928,7 +1933,7 @@ void guest_io_write( size =3D min(bytes, 4 - (port & 3)); if ( size =3D=3D 3 ) size =3D 2; - if ( pci_cfg_ok(v->domain, 1, size) ) + if ( pci_cfg_ok(v->domain, 1, port & 3, size) ) pci_conf_write(v->domain->arch.pci_cf8, port & 3, size, = data); } =20 --- a/xen/include/asm-x86/pci.h +++ b/xen/include/asm-x86/pci.h @@ -1,6 +1,11 @@ #ifndef __X86_PCI_H__ #define __X86_PCI_H__ =20 +#define CF8_BDF(cf8) (((cf8) & 0x00ffff00) >> 8) +#define CF8_ADDR_LO(cf8) ((cf8) & 0x000000fc) +#define CF8_ADDR_HI(cf8) (((cf8) & 0x0f000000) >> 16) +#define CF8_ENABLED(cf8) (!!((cf8) & 0x80000000)) + #define IS_SNB_GFX(id) (id =3D=3D 0x01068086 || id =3D=3D 0x01168086 \ || id =3D=3D 0x01268086 || id =3D=3D 0x01028086 \ || id =3D=3D 0x01128086 || id =3D=3D 0x01228086 \ --=__Part9DA83999.1__= Content-Type: text/plain; name="x86-CF8-decode.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-CF8-decode.patch" x86: synchronize PCI config space access decoding=0A=0ABoth PV and HVM = logic have similar but not similar enough code here.=0ASynchronize the two = so that=0A- in the HVM case we don't unconditionally try to access = extended=0A config space=0A- in the PV case we pass a correct range to = the XSM hook=0A- in the PV case we don't needlessly deny access when the = operation=0A isn't really on PCI config space=0AAll this along with = sharing the macros HVM already had here.=0A=0ASigned-off-by: Jan Beulich = =0A=0A--- a/xen/arch/x86/hvm/hvm.c=0A+++ b/xen/arch/x86/= hvm/hvm.c=0A@@ -2383,11 +2383,6 @@ void hvm_vcpu_down(struct vcpu *v)=0A = static struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain = *d,=0A ioreq_t = *p)=0A {=0A-#define CF8_BDF(cf8) (((cf8) & 0x00ffff00) >> 8)=0A-#define= CF8_ADDR_LO(cf8) ((cf8) & 0x000000fc)=0A-#define CF8_ADDR_HI(cf8) (((cf8) = & 0x0f000000) >> 16)=0A-#define CF8_ENABLED(cf8) (!!((cf8) & 0x80000000))= =0A-=0A struct hvm_ioreq_server *s;=0A uint32_t cf8;=0A = uint8_t type;=0A@@ -2416,9 +2411,19 @@ static struct hvm_ioreq_server = *hvm_sele=0A =0A type =3D IOREQ_TYPE_PCI_CONFIG;=0A addr = =3D ((uint64_t)sbdf << 32) |=0A- CF8_ADDR_HI(cf8) |=0A = CF8_ADDR_LO(cf8) |=0A (p->addr & 3);=0A+ /* = AMD extended configuration space access? */=0A+ if ( CF8_ADDR_HI(cf8= ) &&=0A+ boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_AMD &&=0A+ = boot_cpu_data.x86 >=3D 0x10 && boot_cpu_data.x86 <=3D 0x17 = )=0A+ {=0A+ uint64_t msr_val;=0A+=0A+ if ( = !rdmsr_safe(MSR_AMD64_NB_CFG, msr_val) &&=0A+ (msr_val & = (1ULL << AMD64_NB_CFG_CF8_EXT_ENABLE_BIT)) )=0A+ addr |=3D = CF8_ADDR_HI(cf8);=0A+ }=0A }=0A else=0A {=0A@@ -2472,11 = +2477,6 @@ static struct hvm_ioreq_server *hvm_sele=0A }=0A =0A = return d->arch.hvm_domain.default_ioreq_server;=0A-=0A-#undef CF8_ADDR_ENAB= LED=0A-#undef CF8_ADDR_HI=0A-#undef CF8_ADDR_LO=0A-#undef CF8_BDF=0A }=0A = =0A int hvm_buffered_io_send(ioreq_t *p)=0A--- a/xen/arch/x86/traps.c=0A+++= b/xen/arch/x86/traps.c=0A@@ -1770,14 +1770,19 @@ static int admin_io_okay(= =0A return ioports_access_permitted(v->domain, port, port + bytes - = 1);=0A }=0A =0A-static int pci_cfg_ok(struct domain *d, int write, int = size)=0A+static bool_t pci_cfg_ok(struct domain *d, bool_t write,=0A+ = uint16_t start, uint16_t size)=0A {=0A uint32_t = machine_bdf;=0A- uint16_t start, end;=0A- if (!is_hardware_domain(d))= =0A+ uint16_t end;=0A+=0A+ if ( !is_hardware_domain(d) )=0A = return 0;=0A =0A- machine_bdf =3D (d->arch.pci_cf8 >> 8) & 0xFFFF;=0A+ = if ( !CF8_ENABLED(d->arch.pci_cf8) )=0A+ return 1;=0A+=0A+ = machine_bdf =3D CF8_BDF(d->arch.pci_cf8);=0A if ( write )=0A {=0A = const unsigned long *ro_map =3D pci_get_ro_map(0);=0A@@ -1785,9 = +1790,9 @@ static int pci_cfg_ok(struct domain *d, =0A if ( ro_map = && test_bit(machine_bdf, ro_map) )=0A return 0;=0A }=0A- = start =3D d->arch.pci_cf8 & 0xFF;=0A+ start |=3D CF8_ADDR_LO(d->arch.pc= i_cf8);=0A /* AMD extended configuration space access? */=0A- if ( = (d->arch.pci_cf8 & 0x0F000000) &&=0A+ if ( CF8_ADDR_HI(d->arch.pci_cf8) = &&=0A boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_AMD &&=0A = boot_cpu_data.x86 >=3D 0x10 && boot_cpu_data.x86 <=3D 0x17 )=0A = {=0A@@ -1796,7 +1801,7 @@ static int pci_cfg_ok(struct domain *d, =0A = if ( rdmsr_safe(MSR_AMD64_NB_CFG, msr_val) )=0A return = 0;=0A if ( msr_val & (1ULL << AMD64_NB_CFG_CF8_EXT_ENABLE_BIT) = )=0A- start |=3D (d->arch.pci_cf8 >> 16) & 0xF00;=0A+ = start |=3D CF8_ADDR_HI(d->arch.pci_cf8);=0A }=0A end =3D start + = size - 1;=0A if (xsm_pci_config_permission(XSM_HOOK, d, machine_bdf, = start, end, write))=0A@@ -1855,7 +1860,7 @@ uint32_t guest_io_read(=0A = size =3D min(bytes, 4 - (port & 3));=0A if ( size = =3D=3D 3 )=0A size =3D 2;=0A- if ( pci_cfg_ok(v-= >domain, 0, size) )=0A+ if ( pci_cfg_ok(v->domain, 0, port & 3, = size) )=0A sub_data =3D pci_conf_read(v->domain->arch.pci_c= f8, port & 3, size);=0A }=0A =0A@@ -1928,7 +1933,7 @@ void = guest_io_write(=0A size =3D min(bytes, 4 - (port & 3));=0A = if ( size =3D=3D 3 )=0A size =3D 2;=0A- = if ( pci_cfg_ok(v->domain, 1, size) )=0A+ if ( pci_cfg_ok(v->dom= ain, 1, port & 3, size) )=0A pci_conf_write(v->domain->arch= .pci_cf8, port & 3, size, data);=0A }=0A =0A--- a/xen/include/asm-x= 86/pci.h=0A+++ b/xen/include/asm-x86/pci.h=0A@@ -1,6 +1,11 @@=0A #ifndef = __X86_PCI_H__=0A #define __X86_PCI_H__=0A =0A+#define CF8_BDF(cf8) = (((cf8) & 0x00ffff00) >> 8)=0A+#define CF8_ADDR_LO(cf8) ((cf8) & 0x000000fc= )=0A+#define CF8_ADDR_HI(cf8) (((cf8) & 0x0f000000) >> 16)=0A+#define = CF8_ENABLED(cf8) (!!((cf8) & 0x80000000))=0A+=0A #define IS_SNB_GFX(id) = (id =3D=3D 0x01068086 || id =3D=3D 0x01168086 \=0A = || id =3D=3D 0x01268086 || id =3D=3D 0x01028086 \=0A = || id =3D=3D 0x01128086 || id =3D=3D 0x01228086 \=0A --=__Part9DA83999.1__= 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.xen.org http://lists.xen.org/xen-devel --=__Part9DA83999.1__=--