From: "Jan Beulich" Subject: [PATCH] x86: detect CMOS aliasing on ports other then 0x70/0x71 Date: Tue, 22 Sep 2015 07:10:51 -0600 Message-ID: <56016F7B02000078000A45DA@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part27107D4B.1__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1ZeNLY-0001no-Sm for xen-devel@lists.xenproject.org; Tue, 22 Sep 2015 13:11:01 +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 , 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. --=__Part27107D4B.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline ... in order to also intercept accesses through the alias ports. Also stop intercepting accesses to the CMOS ports if we won't ourselves use the CMOS RTC. Signed-off-by: Jan Beulich --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -1544,42 +1544,6 @@ int __hwdom_init xen_in_range(unsigned l return 0; } =20 -static int __hwdom_init io_bitmap_cb(unsigned long s, unsigned long e, - void *ctx) -{ - struct domain *d =3D ctx; - unsigned int i; - - ASSERT(e <=3D INT_MAX); - for ( i =3D s; i <=3D e; i++ ) - __clear_bit(i, d->arch.hvm_domain.io_bitmap); - - return 0; -} - -void __hwdom_init setup_io_bitmap(struct domain *d) -{ - int rc; - - if ( has_hvm_container_domain(d) ) - { - bitmap_fill(d->arch.hvm_domain.io_bitmap, 0x10000); - rc =3D rangeset_report_ranges(d->arch.ioport_caps, 0, 0x10000, - io_bitmap_cb, d); - BUG_ON(rc); - /* - * NB: we need to trap accesses to 0xcf8 in order to intercept - * 4 byte accesses, that need to be handled by Xen in order to - * keep consistency. - * Access to 1 byte RTC ports also needs to be trapped in order - * to keep consistency with PV. - */ - __set_bit(0xcf8, d->arch.hvm_domain.io_bitmap); - __set_bit(RTC_PORT(0), d->arch.hvm_domain.io_bitmap); - __set_bit(RTC_PORT(1), d->arch.hvm_domain.io_bitmap); - } -} - /* * Local variables: * mode: C --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -736,7 +736,10 @@ static unsigned long get_cmos_time(void) if ( seconds < 60 ) { if ( rtc.sec !=3D seconds ) + { cmos_rtc_probe =3D 0; + acpi_gbl_FADT.boot_flags &=3D ~ACPI_FADT_NO_CMOS_RTC; + } break; } =20 --- a/xen/arch/x86/traps.c +++ b/xen/arch/x86/traps.c @@ -62,6 +62,7 @@ #include #include #include +#include #include #include #include @@ -105,6 +106,8 @@ idt_entry_t *idt_tables[NR_CPUS] __read_ void (*ioemul_handle_quirk)( u8 opcode, char *io_emul_stub, struct cpu_user_regs *regs); =20 +static unsigned int __read_mostly cmos_alias_mask; + static int debug_stack_lines =3D 20; integer_param("debug_stack_lines", debug_stack_lines); =20 @@ -1769,8 +1772,12 @@ static bool_t admin_io_okay(unsigned int if ( (port =3D=3D 0xcf8) && (bytes =3D=3D 4) ) return 0; =20 - /* We also never permit direct access to the RTC/CMOS registers. */ - if ( ((port & ~1) =3D=3D RTC_PORT(0)) ) + /* + * We also never permit direct access to the RTC/CMOS registers + * if we may be accessing the RTC ones ourselves. + */ + if ( !(acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_CMOS_RTC) && + ((port & ~(cmos_alias_mask | 1)) =3D=3D RTC_PORT(0)) ) return 0; =20 return ioports_access_permitted(d, port, port + bytes - 1); @@ -1842,18 +1849,19 @@ uint32_t guest_io_read(unsigned int port { sub_data =3D pv_pit_handler(port, 0, 0); } - else if ( (port =3D=3D RTC_PORT(0)) ) + else if ( ((port & ~cmos_alias_mask) =3D=3D RTC_PORT(0)) ) { sub_data =3D currd->arch.cmos_idx; } - else if ( (port =3D=3D RTC_PORT(1)) && - ioports_access_permitted(currd, RTC_PORT(0), RTC_PORT(1)= ) ) + else if ( ((port & ~cmos_alias_mask) =3D=3D RTC_PORT(1)) && + ioports_access_permitted(currd, port - 1, port) ) { unsigned long flags; =20 spin_lock_irqsave(&rtc_lock, flags); - outb(currd->arch.cmos_idx & 0x7f, RTC_PORT(0)); - sub_data =3D inb(RTC_PORT(1)); + outb(currd->arch.cmos_idx & (0xff >> (port =3D=3D RTC_PORT(1))= ), + port - 1); + sub_data =3D inb(port); spin_unlock_irqrestore(&rtc_lock, flags); } else if ( (port =3D=3D 0xcf8) && (bytes =3D=3D 4) ) @@ -1911,20 +1919,22 @@ void guest_io_write(unsigned int port, u { pv_pit_handler(port, (uint8_t)data, 1); } - else if ( (port =3D=3D RTC_PORT(0)) ) + else if ( ((port & ~cmos_alias_mask) =3D=3D RTC_PORT(0)) ) { currd->arch.cmos_idx =3D data; } - else if ( (port =3D=3D RTC_PORT(1)) && - ioports_access_permitted(currd, RTC_PORT(0), RTC_PORT(1)= ) ) + else if ( ((port & ~cmos_alias_mask) =3D=3D RTC_PORT(1)) && + ioports_access_permitted(currd, port - 1, port) ) { + unsigned int idx =3D currd->arch.cmos_idx & + (0xff >> (port =3D=3D RTC_PORT(1))); unsigned long flags; =20 if ( pv_rtc_handler ) - pv_rtc_handler(currd->arch.cmos_idx & 0x7f, data); + pv_rtc_handler(idx, data); spin_lock_irqsave(&rtc_lock, flags); - outb(currd->arch.cmos_idx & 0x7f, RTC_PORT(0)); - outb(data, RTC_PORT(1)); + outb(idx, port - 1); + outb(data, port); spin_unlock_irqrestore(&rtc_lock, flags); } else if ( (port =3D=3D 0xcf8) && (bytes =3D=3D 4) ) @@ -3727,6 +3737,84 @@ void __init trap_init(void) open_softirq(PCI_SERR_SOFTIRQ, pci_serr_softirq); } =20 +static int __init probe_cmos_alias(void) +{ + unsigned int i, offs; + + if ( acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_CMOS_RTC ) + return 0; + + for ( offs =3D 2; offs < 8; offs <<=3D 1 ) + { + bool_t read =3D 1; + + for ( i =3D RTC_REG_D + 1; i < 0x80; ++i ) + { + uint8_t normal, alt; + unsigned long flags; + + if ( i =3D=3D acpi_gbl_FADT.century ) + continue; + + spin_lock_irqsave(&rtc_lock, flags); + + normal =3D CMOS_READ(i); + if ( inb(RTC_PORT(offs)) !=3D i ) + read =3D 0; + + alt =3D inb(RTC_PORT(offs + 1)); + + spin_unlock_irqrestore(&rtc_lock, flags); + + if ( normal !=3D alt ) + break; + + process_pending_softirqs(); + } + if ( i =3D=3D 0x80 ) + { + cmos_alias_mask |=3D offs; + printk(XENLOG_INFO "CMOS aliased at %02x, index %s\n", + RTC_PORT(offs), read ? "r/w" : "w/o"); + } + } + + return 0; +} +__initcall(probe_cmos_alias); + +static int __hwdom_init io_bitmap_cb(unsigned long s, unsigned long e, + void *ctx) +{ + const struct domain *d =3D ctx; + unsigned int i; + + ASSERT(e <=3D INT_MAX); + for ( i =3D s; i <=3D e; i++ ) + if ( admin_io_okay(i, 1, d) ) + __clear_bit(i, d->arch.hvm_domain.io_bitmap); + + return 0; +} + +void __hwdom_init setup_io_bitmap(struct domain *d) +{ + if ( !has_hvm_container_domain(d) ) + return; + + bitmap_fill(d->arch.hvm_domain.io_bitmap, 0x10000); + if ( rangeset_report_ranges(d->arch.ioport_caps, 0, 0x10000, + io_bitmap_cb, d) ) + BUG(); + + /* + * We need to trap 4-byte accesses to 0xcf8 (see admin_io_okay(), + * guest_io_read(), and guest_io_write()), which isn't covered by + * the admin_io_okay() check in io_bitmap_cb(). + */ + __set_bit(0xcf8, d->arch.hvm_domain.io_bitmap); +} + long register_guest_nmi_callback(unsigned long address) { struct vcpu *v =3D current; --=__Part27107D4B.1__= Content-Type: text/plain; name="x86-CMOS-aliasing.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-CMOS-aliasing.patch" x86: detect CMOS aliasing on ports other then 0x70/0x71=0A=0A... in order = to also intercept accesses through the alias ports.=0A=0AAlso stop = intercepting accesses to the CMOS ports if we won't ourselves=0Ause the = CMOS RTC.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/xen/arch/x86/setup.c=0A+++ b/xen/arch/x86/setup.c=0A@@ -1544,42 +1544,6 = @@ int __hwdom_init xen_in_range(unsigned l=0A return 0;=0A }=0A = =0A-static int __hwdom_init io_bitmap_cb(unsigned long s, unsigned long = e,=0A- void *ctx)=0A-{=0A- struct = domain *d =3D ctx;=0A- unsigned int i;=0A-=0A- ASSERT(e <=3D = INT_MAX);=0A- for ( i =3D s; i <=3D e; i++ )=0A- __clear_bit(i, = d->arch.hvm_domain.io_bitmap);=0A-=0A- return 0;=0A-}=0A-=0A-void = __hwdom_init setup_io_bitmap(struct domain *d)=0A-{=0A- int rc;=0A-=0A- = if ( has_hvm_container_domain(d) )=0A- {=0A- bitmap_fill(d->ar= ch.hvm_domain.io_bitmap, 0x10000);=0A- rc =3D rangeset_report_ranges= (d->arch.ioport_caps, 0, 0x10000,=0A- = io_bitmap_cb, d);=0A- BUG_ON(rc);=0A- /*=0A- * NB: = we need to trap accesses to 0xcf8 in order to intercept=0A- * 4 = byte accesses, that need to be handled by Xen in order to=0A- * = keep consistency.=0A- * Access to 1 byte RTC ports also needs to = be trapped in order=0A- * to keep consistency with PV.=0A- = */=0A- __set_bit(0xcf8, d->arch.hvm_domain.io_bitmap);=0A- = __set_bit(RTC_PORT(0), d->arch.hvm_domain.io_bitmap);=0A- __set_bit(= RTC_PORT(1), d->arch.hvm_domain.io_bitmap);=0A- }=0A-}=0A-=0A /*=0A * = Local variables:=0A * mode: C=0A--- a/xen/arch/x86/time.c=0A+++ b/xen/arch= /x86/time.c=0A@@ -736,7 +736,10 @@ static unsigned long get_cmos_time(void)= =0A if ( seconds < 60 )=0A {=0A if ( rtc.sec = !=3D seconds )=0A+ {=0A cmos_rtc_probe =3D = 0;=0A+ acpi_gbl_FADT.boot_flags &=3D ~ACPI_FADT_NO_CMOS_RTC;= =0A+ }=0A break;=0A }=0A =0A--- a/xen/arch/x= 86/traps.c=0A+++ b/xen/arch/x86/traps.c=0A@@ -62,6 +62,7 @@=0A #include = =0A #include =0A #include =0A+#inc= lude =0A #include =0A #include =0A #include =0A@@ -105,6 +106,8 @@ idt_entry_t *idt_tables[= NR_CPUS] __read_=0A void (*ioemul_handle_quirk)(=0A u8 opcode, char = *io_emul_stub, struct cpu_user_regs *regs);=0A =0A+static unsigned int = __read_mostly cmos_alias_mask;=0A+=0A static int debug_stack_lines =3D = 20;=0A integer_param("debug_stack_lines", debug_stack_lines);=0A =0A@@ = -1769,8 +1772,12 @@ static bool_t admin_io_okay(unsigned int=0A if ( = (port =3D=3D 0xcf8) && (bytes =3D=3D 4) )=0A return 0;=0A =0A- = /* We also never permit direct access to the RTC/CMOS registers. */=0A- = if ( ((port & ~1) =3D=3D RTC_PORT(0)) )=0A+ /*=0A+ * We also never = permit direct access to the RTC/CMOS registers=0A+ * if we may be = accessing the RTC ones ourselves.=0A+ */=0A+ if ( !(acpi_gbl_FADT.bo= ot_flags & ACPI_FADT_NO_CMOS_RTC) &&=0A+ ((port & ~(cmos_alias_mask= | 1)) =3D=3D RTC_PORT(0)) )=0A return 0;=0A =0A return = ioports_access_permitted(d, port, port + bytes - 1);=0A@@ -1842,18 = +1849,19 @@ uint32_t guest_io_read(unsigned int port=0A {=0A = sub_data =3D pv_pit_handler(port, 0, 0);=0A }=0A- = else if ( (port =3D=3D RTC_PORT(0)) )=0A+ else if ( ((port & = ~cmos_alias_mask) =3D=3D RTC_PORT(0)) )=0A {=0A = sub_data =3D currd->arch.cmos_idx;=0A }=0A- else if ( (port = =3D=3D RTC_PORT(1)) &&=0A- ioports_access_permitted(currd,= RTC_PORT(0), RTC_PORT(1)) )=0A+ else if ( ((port & ~cmos_alias_mask= ) =3D=3D RTC_PORT(1)) &&=0A+ ioports_access_permitted(curr= d, port - 1, port) )=0A {=0A unsigned long flags;=0A = =0A spin_lock_irqsave(&rtc_lock, flags);=0A- = outb(currd->arch.cmos_idx & 0x7f, RTC_PORT(0));=0A- sub_data = =3D inb(RTC_PORT(1));=0A+ outb(currd->arch.cmos_idx & (0xff >> = (port =3D=3D RTC_PORT(1))),=0A+ port - 1);=0A+ = sub_data =3D inb(port);=0A spin_unlock_irqrestore(&rtc_lock, = flags);=0A }=0A else if ( (port =3D=3D 0xcf8) && (bytes = =3D=3D 4) )=0A@@ -1911,20 +1919,22 @@ void guest_io_write(unsigned int = port, u=0A {=0A pv_pit_handler(port, (uint8_t)data, = 1);=0A }=0A- else if ( (port =3D=3D RTC_PORT(0)) )=0A+ = else if ( ((port & ~cmos_alias_mask) =3D=3D RTC_PORT(0)) )=0A = {=0A currd->arch.cmos_idx =3D data;=0A }=0A- = else if ( (port =3D=3D RTC_PORT(1)) &&=0A- ioports_access_= permitted(currd, RTC_PORT(0), RTC_PORT(1)) )=0A+ else if ( ((port & = ~cmos_alias_mask) =3D=3D RTC_PORT(1)) &&=0A+ ioports_acces= s_permitted(currd, port - 1, port) )=0A {=0A+ unsigned = int idx =3D currd->arch.cmos_idx &=0A+ (0xff = >> (port =3D=3D RTC_PORT(1)));=0A unsigned long flags;=0A =0A = if ( pv_rtc_handler )=0A- pv_rtc_handler(currd->a= rch.cmos_idx & 0x7f, data);=0A+ pv_rtc_handler(idx, = data);=0A spin_lock_irqsave(&rtc_lock, flags);=0A- = outb(currd->arch.cmos_idx & 0x7f, RTC_PORT(0));=0A- outb(data, = RTC_PORT(1));=0A+ outb(idx, port - 1);=0A+ outb(data,= port);=0A spin_unlock_irqrestore(&rtc_lock, flags);=0A = }=0A else if ( (port =3D=3D 0xcf8) && (bytes =3D=3D 4) )=0A@@ = -3727,6 +3737,84 @@ void __init trap_init(void)=0A open_softirq(PCI_SER= R_SOFTIRQ, pci_serr_softirq);=0A }=0A =0A+static int __init probe_cmos_alia= s(void)=0A+{=0A+ unsigned int i, offs;=0A+=0A+ if ( acpi_gbl_FADT.boo= t_flags & ACPI_FADT_NO_CMOS_RTC )=0A+ return 0;=0A+=0A+ for ( = offs =3D 2; offs < 8; offs <<=3D 1 )=0A+ {=0A+ bool_t read =3D = 1;=0A+=0A+ for ( i =3D RTC_REG_D + 1; i < 0x80; ++i )=0A+ = {=0A+ uint8_t normal, alt;=0A+ unsigned long = flags;=0A+=0A+ if ( i =3D=3D acpi_gbl_FADT.century )=0A+ = continue;=0A+=0A+ spin_lock_irqsave(&rtc_lock, = flags);=0A+=0A+ normal =3D CMOS_READ(i);=0A+ if ( = inb(RTC_PORT(offs)) !=3D i )=0A+ read =3D 0;=0A+=0A+ = alt =3D inb(RTC_PORT(offs + 1));=0A+=0A+ spin_unlock_irqrest= ore(&rtc_lock, flags);=0A+=0A+ if ( normal !=3D alt )=0A+ = break;=0A+=0A+ process_pending_softirqs();=0A+ = }=0A+ if ( i =3D=3D 0x80 )=0A+ {=0A+ cmos_alias_ma= sk |=3D offs;=0A+ printk(XENLOG_INFO "CMOS aliased at %02x, = index %s\n",=0A+ RTC_PORT(offs), read ? "r/w" : = "w/o");=0A+ }=0A+ }=0A+=0A+ return 0;=0A+}=0A+__initcall(probe= _cmos_alias);=0A+=0A+static int __hwdom_init io_bitmap_cb(unsigned long s, = unsigned long e,=0A+ void *ctx)=0A+{=0A= + const struct domain *d =3D ctx;=0A+ unsigned int i;=0A+=0A+ = ASSERT(e <=3D INT_MAX);=0A+ for ( i =3D s; i <=3D e; i++ )=0A+ = if ( admin_io_okay(i, 1, d) )=0A+ __clear_bit(i, d->arch.hvm_dom= ain.io_bitmap);=0A+=0A+ return 0;=0A+}=0A+=0A+void __hwdom_init = setup_io_bitmap(struct domain *d)=0A+{=0A+ if ( !has_hvm_container_domai= n(d) )=0A+ return;=0A+=0A+ bitmap_fill(d->arch.hvm_domain.io_bitm= ap, 0x10000);=0A+ if ( rangeset_report_ranges(d->arch.ioport_caps, 0, = 0x10000,=0A+ io_bitmap_cb, d) )=0A+ = BUG();=0A+=0A+ /*=0A+ * We need to trap 4-byte accesses to 0xcf8 = (see admin_io_okay(),=0A+ * guest_io_read(), and guest_io_write()), = which isn't covered by=0A+ * the admin_io_okay() check in io_bitmap_cb(= ).=0A+ */=0A+ __set_bit(0xcf8, d->arch.hvm_domain.io_bitmap);=0A+}= =0A+=0A long register_guest_nmi_callback(unsigned long address)=0A {=0A = struct vcpu *v =3D current;=0A --=__Part27107D4B.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 --=__Part27107D4B.1__=--