x86: detect PIT aliasing on ports other than 0x4[0-3]
v3 cover x86: detect PIT aliasing on ports other than 0x4[0-3]
1: PIT: supply and use #define-s
2: detect PIT aliasing on ports other than 0x4[0-3]
No functional change from v2, just the introduction of the new prereq patch to help overall readability.
Jan
PATCH v3 1/2 x86/PIT: supply and use #define-s
Help reading of code programming the PIT by introducing constants for control word, read back and latch commands, as well as status.
---
v3: New.
xen/arch/x86/apic.c +1 -1
@@ -983,7 +983,7 @@ static unsigned int __init get_8254_time/*spin_lock_irqsave(&i8253_lock, flags);*/ -outb_p (0x00 ,PIT_MODE ); +outb_p (PIT_LTCH_CH (0 ),PIT_MODE ); count =inb_p (PIT_CH0 ); count |=inb_p (PIT_CH0 ) <<8 ;
xen/arch/x86/include/asm/time.h +34 -0
@@ -58,4 +58,38 @@ struct time_scale;void set_time_scale (struct time_scale *ts,u64 ticks_per_sec);u64 scale_delta (u64 delta,const struct time_scale *scale); +/* Programmable Interval Timer (8254) */ + +/* Timer Control Word */ +#define PIT_TCW_CH (n) ((n) << 6) +/* Lower bits also Timer Status. */ +#define PIT_RW_MSB (1 << 5) +#define PIT_RW_LSB (1 << 4) +#define PIT_RW_LSB_MSB (PIT_RW_LSB | PIT_RW_MSB) +#define PIT_MODE_EOC (0 << 1) +#define PIT_MODE_ONESHOT (1 << 1) +#define PIT_MODE_RATE_GEN (2 << 1) +#define PIT_MODE_SQUARE_WAVE (3 << 1) +#define PIT_MODE_SW_STROBE (4 << 1) +#define PIT_MODE_HW_STROBE (5 << 1) +#define PIT_BINARY (0 << 0) +#define PIT_BCD (1 << 0) + +/* Read Back Command */ +#define PIT_RDB PIT_TCW_CH(3) +#define PIT_RDB_NO_COUNT (1 << 5) +#define PIT_RDB_NO_STATUS (1 << 4) +#define PIT_RDB_CH2 (1 << 3) +#define PIT_RDB_CH1 (1 << 2) +#define PIT_RDB_CH0 (1 << 1) +#define PIT_RDB_RSVD (1 << 0) + +/* Counter Latch Command */ +#define PIT_LTCH_CH (n) PIT_TCW_CH(n) + +/* Timer Status */ +#define PIT_STATUS_OUT_PIN (1 << 7) +#define PIT_STATUS_NULL_COUNT (1 << 6) +/* Lower bits match Timer Control Word. */ +#endif /* __X86_TIME_H__ */
xen/arch/x86/time.c +18 -13
@@ -222,7 +222,7 @@ static void cf_check timer_interrupt(intspin_lock_irq (&pit_lock); -outb (0x80 ,PIT_MODE ); +outb (PIT_LTCH_CH (2 ),PIT_MODE ); count =inb (PIT_CH2 ); count |=inb (PIT_CH2 ) <<8 ;
@@ -245,7 +245,8 @@ static void preinit_pit(void) {/* Set PIT channel 0 to HZ Hz. */ #define LATCH (((CLOCK_TICK_RATE)+(HZ/2))/HZ) -outb_p (0x34 ,PIT_MODE );/* binary, mode 2, LSB/MSB, ch 0 */ +outb_p (PIT_TCW_CH (0 ) |PIT_RW_LSB_MSB |PIT_MODE_RATE_GEN |PIT_BINARY , +PIT_MODE );outb_p (LATCH &0xff ,PIT_CH0 );/* LSB */ outb (LATCH >>8 ,PIT_CH0 );/* MSB */ #undef LATCH
@@ -356,7 +357,7 @@ static u64 cf_check read_pit_count(void)spin_lock_irqsave (&pit_lock, flags); -outb (0x80 ,PIT_MODE ); +outb (PIT_LTCH_CH (2 ),PIT_MODE ); count16 =inb (PIT_CH2 ); count16 |=inb (PIT_CH2 ) <<8 ;
@@ -383,7 +384,8 @@ static s64 __init cf_check init_pit(stru*/ #define CALIBRATE_LATCH CALIBRATE_VALUE(CLOCK_TICK_RATE)BUILD_BUG_ON (CALIBRATE_LATCH >>16 ); -outb (0xb0 ,PIT_MODE );/* binary, mode 0, LSB/MSB, Ch 2 */ +outb (PIT_TCW_CH (2 ) |PIT_RW_LSB_MSB |PIT_MODE_EOC |PIT_BINARY , +PIT_MODE );outb (CALIBRATE_LATCH &0xff ,PIT_CH2 );/* LSB of count */ outb (CALIBRATE_LATCH >>8 ,PIT_CH2 );/* MSB of count */ #undef CALIBRATE_LATCH
@@ -408,7 +410,8 @@ static s64 __init cf_check init_pit(stru static void cf_check resume_pit(struct platform_timesource *pts) {/* Set CTC channel 2 to mode 0 again; initial value does not matter. */ -outb (0xb0 ,PIT_MODE );/* binary, mode 0, LSB/MSB, Ch 2 */ +outb (PIT_TCW_CH (2 ) |PIT_RW_LSB_MSB |PIT_MODE_EOC |PIT_BINARY , +PIT_MODE );outb (0 ,PIT_CH2 );/* LSB of count */ outb (0 ,PIT_CH2 );/* MSB of count */ }
@@ -2456,7 +2459,8 @@ static int _disable_pit_irq(bool init) }/* Disable PIT CH0 timer interrupt. */ -outb_p (0x30 ,PIT_MODE ); +outb_p (PIT_TCW_CH (0 ) |PIT_RW_LSB_MSB |PIT_MODE_EOC |PIT_BINARY , +PIT_MODE );outb_p (0 ,PIT_CH0 );outb_p (0 ,PIT_CH0 );
@@ -2562,17 +2566,18 @@ int hwdom_pit_access(struct ioreq *ioreqcase PIT_MODE :if ( ioreq->dir ==IOREQ_READ )return 0 ;/* urk! */ -switch ( ioreq->data &0xc0 ) +switch ( ioreq->data &PIT_TCW_CH (3 ) ) { -case 0xc0 :/* Read Back */ -if ( ioreq->data &0x08 )/* Select Channel 2? */ -outb (ioreq->data &0xf8 ,PIT_MODE ); -if ( !(ioreq->data &0x06 ) )/* Select Channel 0/1? */ +case PIT_RDB :/* Read Back */ +if ( ioreq->data &PIT_RDB_CH2 ) +outb (ioreq->data & ~(PIT_RDB_CH1 |PIT_RDB_CH0 |PIT_RDB_RSVD ), +PIT_MODE ); +if ( !(ioreq->data & (PIT_RDB_CH0 |PIT_RDB_CH1 )) )return 1 ;/* no - we're done */ /* Filter Channel 2 and reserved bit 0. */ - ioreq->data &= ~0x09 ; + ioreq->data &= ~(PIT_RDB_CH2 |PIT_RDB_RSVD );return 0 ;/* emulate ch0/1 readback */ -case 0x80 :/* Select Counter 2 */ +case PIT_TCW_CH (2 ):outb (ioreq->data,PIT_MODE );return 1 ; }
Re: [PATCH v3 1/2] x86/PIT: supply and use #define-s
Quoted 5 lines · Jan Beulich
Help reading of code programming the PIT by introducing constants for control word, read back and latch commands, as well as status.
Requested-by Jason Andryuk <[email protected]>Signed-off-by Jan Beulich <[email protected]>
Thanks for making the switch.
Regards,
Jason
PATCH v3 2/2 x86: detect PIT aliasing on ports other than 0x4[0-3]
In reply to Jan Beulich... in order to also deny Dom0 access through the alias ports (commonly observed on Intel chipsets). Without this it is only giving the impression of denying access to PIT. Unlike for CMOS/RTC, do detection pretty early, to avoid disturbing normal operation later on (even if typically we won't use much of the PIT).
Like for CMOS/RTC a fundamental assumption of the probing is that reads from the probed alias port won't have side effects (beyond such that PIT reads have anyway) in case it does not alias the PIT's.
As to the port 0x61 accesses: Unlike other accesses we do, this masks off the top four bits (in addition to the bottom two ones), following Intel chipset documentation saying that these (read-only) bits should only be written with zero.
---
If Xen was running on top of another instance of itself (in HVM mode, not PVH, i.e. not as a shim), prior to 14f42af3f52d ('x86/vPIT: account for "counter stopped" time') I'm afraid our vPIT logic would not have allowed the "Try to further make sure ..." check to pass in the Xen running on top: We don't respect the gate bit being clear when handling counter reads. (There are more unhandled [and unmentioned as being so] aspects of PIT behavior though, yet it's unclear in how far addressing at least some of them would be useful.)
---
v3: Use PIT_* in dom0_setup_permissions(). Use #define-s introduced by
new earlier patch.
v2: Use new command line option. Re-base over changes to earlier
patches. Use ISOLATE_LSB().
xen/arch/x86/dom0_build.c +6 -1
@@ -20,6 +20,7 @@#include <asm/p2m.h> #include <asm/setup.h> #include <asm/spec_ctrl.h> +#include <io_ports.h> struct memsize {long nr_pages;
@@ -495,7 +496,11 @@ int __init dom0_setup_permissions(struct rc |=ioports_deny_access (d,0x4D0 ,0x4D1 );/* Interval Timer (PIT). */ - rc |=ioports_deny_access (d,0x40 ,0x43 ); +for ( offs =0 , i =ISOLATE_LSB (pit_alias_mask) ?:4 ; + offs <= pit_alias_mask; offs += i ) +if ( !(offs & ~pit_alias_mask) ) + rc |=ioports_deny_access (d,PIT_CH0 + offs,PIT_MODE + offs); +/* PIT Channel 2 / PC Speaker Control. */ rc |=ioports_deny_access (d,0x61 ,0x61 );
xen/arch/x86/include/asm/setup.h +1 -0
@@ -49,6 +49,7 @@ extern unsigned long highmem_start;#endif extern unsigned int i8259A_alias_mask; +extern unsigned int pit_alias_mask;extern int8_t opt_smt;extern int8_t opt_probe_port_aliases;
xen/arch/x86/time.c +70 -0
82 lines not on this page · raw message
Quoted 54 lines · Jan Beulich
... in order to also deny Dom0 access through the alias ports (commonly observed on Intel chipsets). Without this it is only giving the impression of denying access to PIT. Unlike for CMOS/RTC, do detection pretty early, to avoid disturbing normal operation later on (even if typically we won't use much of the PIT).
Like for CMOS/RTC a fundamental assumption of the probing is that reads from the probed alias port won't have side effects (beyond such that PIT reads have anyway) in case it does not alias the PIT's.
As to the port
0x61accesses: Unlike other accesses we do, this masks off the top four bits (in addition to the bottom two ones), following Intel chipset documentation saying that these (read-only) bits should only be written with zero.Signed-off-by Jan Beulich <[email protected]>xen/arch/x86/time.c +31
-0@@ -427,6 +427,74 @@ static struct platform_timesource __init .resume = resume_pit, }; +unsigned int __initdata pit_alias_mask; + +static void __init probe_pit_alias (void ) +{ +unsigned int mask =0x1c ; +uint8_t val =0 ; + +if ( !opt_probe_port_aliases ) +return ; + +/* +* Use channel 2 in mode 0 for probing. In this mode even a non-initial +* count is loaded independent of counting being / becoming enabled. Thus +* we have a 16-bit value fully under our control, to write and then check +* whether we can also read it back unaltered. +*/ + +/* Turn off speaker output and disable channel 2 counting. */ +outb (inb (0x61 ) &0x0c ,0x61 ); + +outb (PIT_LTCH_CH (2 ) |PIT_RW_LSB_MSB |PIT_MODE_EOC |PIT_BINARY , +PIT_MODE ); + + do { + uint8_t val2; + unsigned int offs; + + outb(val, PIT_CH2); + outb(val ^ 0xff, PIT_CH2); + + /* Wait for the Null Count bit to clear. */
xen/arch/x86/time.c static struct platform_timesource __init · line 470
Quoted 10 lines
xen/arch/x86/time.c +10
-0+do { +/* Latch status. */ +outb (PIT_RDB |PIT_RDB_NO_COUNT |PIT_RDB_CH2 ,PIT_MODE ); + +/* Try to make sure we're actually having a PIT here. */ + val2 =inb (PIT_CH2 ); +if ( (val2 & ~(PIT_STATUS_OUT_PIN |PIT_STATUS_NULL_COUNT )) != + (PIT_RW_LSB_MSB |PIT_MODE_EOC |PIT_BINARY ) ) +return ; + }while ( val2 & (1 <<6 ) );
You can use PIT_STATUS_NULL_COUNT here.
With that:
Thanks,
Jason
Quoted 42 lines · Jason Andryuk
Quoted 41 lines · Jan Beulich
xen/arch/x86/time.c +35
-0@@ -427,6 +427,74 @@ static struct platform_timesource __init .resume = resume_pit, }; +unsigned int __initdata pit_alias_mask; + +static void __init probe_pit_alias(void) +{ + unsigned int mask = 0x1c; + uint8_t val = 0; + + if ( !opt_probe_port_aliases ) + return; + + /* + * Use channel 2 in mode 0 for probing. In this mode even a non-initial + * count is loaded independent of counting being / becoming enabled. Thus + * we have a 16-bit value fully under our control, to write and then check + * whether we can also read it back unaltered. + */ + + /* Turn off speaker output and disable channel 2 counting. */ + outb(inb(0x61) & 0x0c, 0x61); + + outb(PIT_LTCH_CH(2) | PIT_RW_LSB_MSB | PIT_MODE_EOC | PIT_BINARY, + PIT_MODE); + + do { + uint8_t val2; + unsigned int offs; + + outb(val, PIT_CH2); + outb(val ^ 0xff, PIT_CH2); + + /* Wait for the Null Count bit to clear. */ + do { + /* Latch status. */ + outb(PIT_RDB | PIT_RDB_NO_COUNT | PIT_RDB_CH2, PIT_MODE); +
Quoted 8 lines
xen/arch/x86/time.c static struct platform_timesource __init · line 470
Quoted 6 lines
xen/arch/x86/time.c +6
-0+/* Try to make sure we're actually having a PIT here. */ + val2 =inb (PIT_CH2 ); +if ( (val2 & ~(PIT_STATUS_OUT_PIN |PIT_STATUS_NULL_COUNT )) != + (PIT_RW_LSB_MSB |PIT_MODE_EOC |PIT_BINARY ) ) +return ; + }while ( val2 & (1 <<6 ) );You can use
PIT_STATUS_NULL_COUNThere.
Indeed, and I meant to but then forgot. Thanks for noticing.
Quoted 2 lines
With that:
Reviewed-by Jason Andryuk <[email protected]>
Thanks.
Jan