Skip to contentSkip to conversation

x86/HVM: drop stdvga caching mode

PATCH Superseded Jan Beulich 2 participants 21 messages Sep 10, 2024 – Sep 11, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86/HVM: drop stdvga caching mode

It's been unused for nearly 9 years. By the end of the series stdvga.c's sole purpose will be to make sure VRAM writes use the bufio ioreq path.

1: drop stdvga's "cache" struct member
2: drop stdvga's "stdvga" struct member
3: remove unused MMIO handling code
4: drop stdvga's "gr[]" struct member
5: drop stdvga's "sr[]" struct member
6: drop stdvga's "{g,s}r_index" struct members
7: drop stdvga's "vram_page[]" struct member

Of course all of these could be folded into a single patch, but I think going piecemeal makes more clear that the individual parts are indeed unused.

Jan

#1 lore raw

PATCH 1/7 x86/HVM: drop stdvga's "cache" struct member

As of 68e1183411be ("libxc: introduce a xc_dom_arch for hvm-3.0-x86_32 guests") caching mode is disabled for HVM domains from start-of-day, due the disabling being unconditional in hvm/save.c:arch_hvm_load(). With that the field is useless, and can be dropped. Drop the helper functions manipulating / checking as well right away, but leave the use sites of stdvga_cache_is_enabled() with the hard-coded result the function would have produced, to aid validation of subsequent dropping of further code.

xen/arch/x86/hvm/save.c +0 -3
@@ -69,9 +69,6 @@ static void arch_hvm_load(struct domain
 
     /* Time when restore started  */
     d->arch.hvm.sync_tsc = rdtsc();
-
-    /* VGA state is not saved/restored, so we nobble the cache. */
-    d->arch.hvm.stdvga.cache = STDVGA_CACHE_DISABLED;
 }
 
 /* List of handlers for various HVM save and restore types */
xen/arch/x86/hvm/stdvga.c +3 -41

72 lines not on this page · raw message

xen/arch/x86/include/asm/hvm/io.h +0 -7
@@ -110,19 +110,12 @@ struct vpci_arch_msix_entry {
     int pirq;
 };
 
-enum stdvga_cache_state {
-    STDVGA_CACHE_UNINITIALIZED,
-    STDVGA_CACHE_ENABLED,
-    STDVGA_CACHE_DISABLED
-};
-
 struct hvm_hw_stdvga {
     uint8_t sr_index;
     uint8_t sr[8];
     uint8_t gr_index;
     uint8_t gr[9];
     bool stdvga;
-    enum stdvga_cache_state cache;
     uint32_t latch;
     struct page_info *vram_page[64];  /* shadow of 0xa0000-0xaffff */
     spinlock_t lock;
Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] x86/HVM: drop stdvga's "cache" struct member

Quoted 9 lines · Jan Beulich

As of 68e1183411be ("libxc: introduce a xc_dom_arch for hvm-3.0-x86_32 guests") caching mode is disabled for HVM domains from start-of-day, due the disabling being unconditional in hvm/save.c:arch_hvm_load(). With that the field is useless, and can be dropped. Drop the helper functions manipulating / checking as well right away, but leave the use sites of stdvga_cache_is_enabled() with the hard-coded result the function would have produced, to aid validation of subsequent dropping of further code.

This only applies to VMs constructed with libxenguest, which isn't all VMs.  But, it's probably close enough to ~100% of VMs to count.

Personally I think it would be clearer to say "Since 68e1183411be, HVM guests are built using XEN_DOMCTL_sethvmcontext, which intentionally disables stdvga caching in arch_hvm_load()".

As a minor tangent, this is yet another casualty of nothing being wired into the migration stream.  Rightly or wrongly, that mindset has caused an immense amount of problems in Xen.

How does this interact with X86_EMU_VGA?

stdvga_init() is called unconditionally for HVM domains, exiting early for !EMU_VGA (skipping the pointless re-zero of the structure). Nevertheless, the cache is UNINITIALISED for all configurations at this point.

And we won't hit the stdvga_try_cache_enable() calls in any of stdvga_{outb,mem_{write,accept}}() prior to XEN_DOMCTL_sethvmcontext, which will unconditionally set DISABLED.

So yes, I think this is for-all-intents-and-purposes dead logic.

#3 lore raw

Re: [PATCH 1/7] x86/HVM: drop stdvga's "cache" struct member

Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Jan Beulich

As of 68e1183411be ("libxc: introduce a xc_dom_arch for hvm-3.0-x86_32 guests") caching mode is disabled for HVM domains from start-of-day, due the disabling being unconditional in hvm/save.c:arch_hvm_load(). With that the field is useless, and can be dropped. Drop the helper functions manipulating / checking as well right away, but leave the use sites of stdvga_cache_is_enabled() with the hard-coded result the function would have produced, to aid validation of subsequent dropping of further code.

Quoted 8 lines
Quoted 1 line

This only applies to VMs constructed with libxenguest, which isn't all VMs.  But, it's probably close enough to ~100% of VMs to count.

Personally I think it would be clearer to say "Since 68e1183411be, HVM guests are built using XEN_DOMCTL_sethvmcontext, which intentionally disables stdvga caching in arch_hvm_load()".

I'll derive something intermediate. I don't think it was intentional that caching got disabled this way. It's just that no-one noticed or paid attention.

Quoted 3 lines

As a minor tangent, this is yet another casualty of nothing being wired into the migration stream.  Rightly or wrongly, that mindset has caused an immense amount of problems in Xen.

Indeed.

Quoted 7 lines

How does this interact with X86_EMU_VGA?

stdvga_init() is called unconditionally for HVM domains, exiting early for !EMU_VGA (skipping the pointless re-zero of the structure). Nevertheless, the cache is UNINITIALISED for all configurations at this point.

Quoted 7 lines

And we won't hit the stdvga_try_cache_enable() calls in any of stdvga_{outb,mem_{write,accept}}() prior to XEN_DOMCTL_sethvmcontext, which will unconditionally set DISABLED.

So yes, I think this is for-all-intents-and-purposes dead logic.

Thanks.

Jan

#4 lore raw

PATCH 2/7 x86/HVM: drop stdvga's "stdvga" struct member

In reply to Jan Beulich

Two of its consumers are dead (in compile-time constant conditionals) and the only remaining ones are merely controlling (debugging) log messages. Hence the field is now pointless to set, which in particular allows to get rid of the questionable conditional from which the field's value was established (afaict 551ceee97513 ["x86, hvm: stdvga cache always on"] had dropped too much of the earlier extra check that was there, and quite likely further checks were missing).

xen/arch/x86/hvm/stdvga.c +5 -22
@@ -103,7 +103,7 @@ static void vram_put(struct hvm_hw_stdvg
 static int stdvga_outb(uint64_t addr, uint8_t val)
 {
     struct hvm_hw_stdvga *s = &current->domain->arch.hvm.stdvga;
-    int rc = 1, prev_stdvga = s->stdvga;
+    int rc = 1;
 
     switch ( addr )
     {
@@ -132,19 +132,6 @@ static int stdvga_outb(uint64_t addr, ui
         break;
     }
 
-    /* When in standard vga mode, emulate here all writes to the vram buffer
-     * so we can immediately satisfy reads without waiting for qemu. */
-    s->stdvga = (s->sr[7] == 0x00);
-
-    if ( !prev_stdvga && s->stdvga )
-    {
-        gdprintk(XENLOG_INFO, "entering stdvga mode\n");
-    }
-    else if ( prev_stdvga && !s->stdvga )
-    {
-        gdprintk(XENLOG_INFO, "leaving stdvga mode\n");
-    }
-
     return rc;
 }
 
@@ -425,7 +412,6 @@ static int cf_check stdvga_mem_write(
     const struct hvm_io_handler *handler, uint64_t addr, uint32_t size,
     uint64_t data)
 {
-    struct hvm_hw_stdvga *s = &current->domain->arch.hvm.stdvga;
     ioreq_t p = {
         .type = IOREQ_TYPE_COPY,
         .addr = addr,
@@ -436,8 +422,7 @@ static int cf_check stdvga_mem_write(
     };
     struct ioreq_server *srv;
 
-    if ( true || !s->stdvga )
-        goto done;
+    goto done;
 
     /* Intercept mmio write */
     switch ( size )
@@ -498,19 +483,17 @@ static bool cf_check stdvga_mem_accept(
 
     spin_lock(&s->lock);
 
-    if ( p->dir == IOREQ_WRITE && p->count > 1 )
+    if ( p->dir != IOREQ_WRITE || p->count > 1 )
     {
         /*
          * We cannot return X86EMUL_UNHANDLEABLE on anything other then the
          * first cycle of an I/O. So, since we cannot guarantee to always be
          * able to send buffered writes, we have to reject any multi-cycle
-         * I/O.
+         * I/O. And of course we have to reject all reads, for not being
+         * able to service them.
          */
         goto reject;
     }
-    else if ( p->dir == IOREQ_READ &&
-              (true || !s->stdvga) )
-        goto reject;
 
     /* s->lock intentionally held */
     return 1;
xen/arch/x86/include/asm/hvm/io.h +0 -1
@@ -115,7 +115,6 @@ struct hvm_hw_stdvga {
     uint8_t sr[8];
     uint8_t gr_index;
     uint8_t gr[9];
-    bool stdvga;
     uint32_t latch;
     struct page_info *vram_page[64];  /* shadow of 0xa0000-0xaffff */
     spinlock_t lock;
#5 lore raw

Re: [PATCH 2/7] x86/HVM: drop stdvga's "stdvga" struct member

Quoted 3 lines · Jan Beulich

Two of its consumers are dead (in compile-time constant conditionals) and the only remaining ones are merely controlling (debugging) log messages.

Minor, but I'd phrase this as "merely controlling debug logging."

Quoted 11 lines

Hence the field is now pointless to set, which in particular
allows to get rid of the questionable conditional from which the field's value was established (afaict 551ceee97513 ["x86, hvm: stdvga cache always on"] had dropped too much of the earlier extra check that was there, and quite likely further checks were missing).

xen/arch/x86/hvm/stdvga.c static void vram_put(struct hvm_hw_stdvg · line 106

Quoted 5 lines
xen/arch/x86/hvm/stdvga.c +1 -1
@@ -103,7 +103,7 @@ static void vram_put(struct hvm_hw_stdvg
 static int stdvga_outb(uint64_t addr, uint8_t val)
 {
     struct hvm_hw_stdvga *s = &current->domain->arch.hvm.stdvga;
-    int rc = 1, prev_stdvga = s->stdvga;
+    int rc = 1;

This looks to also drop a MISRA essential-type complaint.

Quoted 51 lines
xen/arch/x86/hvm/stdvga.c +2 -17
     switch ( addr )
     {
@@ -132,19 +132,6 @@ static int stdvga_outb(uint64_t addr, ui
         break;
     }
 
-    /* When in standard vga mode, emulate here all writes to the vram buffer
-     * so we can immediately satisfy reads without waiting for qemu. */
-    s->stdvga = (s->sr[7] == 0x00);
-
-    if ( !prev_stdvga && s->stdvga )
-    {
-        gdprintk(XENLOG_INFO, "entering stdvga mode\n");
-    }
-    else if ( prev_stdvga && !s->stdvga )
-    {
-        gdprintk(XENLOG_INFO, "leaving stdvga mode\n");
-    }
-
     return rc;
 }
 
@@ -425,7 +412,6 @@ static int cf_check stdvga_mem_write(
     const struct hvm_io_handler *handler, uint64_t addr, uint32_t size,
     uint64_t data)
 {
-    struct hvm_hw_stdvga *s = &current->domain->arch.hvm.stdvga;
     ioreq_t p = {
         .type = IOREQ_TYPE_COPY,
         .addr = addr,
@@ -436,8 +422,7 @@ static int cf_check stdvga_mem_write(
     };
     struct ioreq_server *srv;
 
-    if ( true || !s->stdvga )
-        goto done;
+    goto done;
 
     /* Intercept mmio write */
     switch ( size )
@@ -498,19 +483,17 @@ static bool cf_check stdvga_mem_accept(
 
     spin_lock(&s->lock);
 
-    if ( p->dir == IOREQ_WRITE && p->count > 1 )
+    if ( p->dir != IOREQ_WRITE || p->count > 1 )
     {
         /*
          * We cannot return X86EMUL_UNHANDLEABLE on anything other then the
          * first cycle of an I/O. So, since we cannot guarantee to always be

xen/arch/x86/hvm/stdvga.c static bool cf_check stdvga_mem_accept( · line 513

Quoted 10 lines
xen/arch/x86/hvm/stdvga.c +2 -4
          * able to send buffered writes, we have to reject any multi-cycle
-         * I/O.
+         * I/O. And of course we have to reject all reads, for not being
+         * able to service them.
          */
         goto reject;
     }
-    else if ( p->dir == IOREQ_READ &&
-              (true || !s->stdvga) )
-        goto reject;

Before, we rejected on (WRITE && count) or READ, and I think we still do afterwards given the boolean-ness of read/write.  However, the comment is distinctly less relevant.

I think we now just end up with /* Only accept single writes. */ which matches with with shuffling these into the bufioreq ring.

~Andrew

#6 lore raw

Re: [PATCH 2/7] x86/HVM: drop stdvga's "stdvga" struct member

Quoted 26 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich
xen/arch/x86/hvm/stdvga.c +1 -1
@@ -498,19 +483,17 @@ static bool cf_check stdvga_mem_accept(
 
     spin_lock(&s->lock);
 
-    if ( p->dir == IOREQ_WRITE && p->count > 1 )
+    if ( p->dir != IOREQ_WRITE || p->count > 1 )
     {
         /*
          * We cannot return X86EMUL_UNHANDLEABLE on anything other then the
          * first cycle of an I/O. So, since we cannot guarantee to always be

xen/arch/x86/hvm/stdvga.c static bool cf_check stdvga_mem_accept( · line 513

Quoted 10 lines
xen/arch/x86/hvm/stdvga.c +2 -4
          * able to send buffered writes, we have to reject any multi-cycle
-         * I/O.
+         * I/O. And of course we have to reject all reads, for not being
+         * able to service them.
          */
         goto reject;
     }
-    else if ( p->dir == IOREQ_READ &&
-              (true || !s->stdvga) )
-        goto reject;

Before, we rejected on (WRITE && count) or READ, and I think we still do afterwards given the boolean-ness of read/write.  However, the comment is distinctly less relevant.

Quoted 2 lines

I think we now just end up with /* Only accept single writes. */ which matches with with shuffling these into the bufioreq ring.

Fine with me. As usually I'm commenting rather too little, I was fearing I'd remove too much if I shrunk the comment like this.

Jan

#7 lore raw

Re: [PATCH 2/7] x86/HVM: drop stdvga's "stdvga" struct member

In reply to Andrew Cooper
Quoted 26 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich
xen/arch/x86/hvm/stdvga.c +1 -1
@@ -498,19 +483,17 @@ static bool cf_check stdvga_mem_accept(
 
     spin_lock(&s->lock);
 
-    if ( p->dir == IOREQ_WRITE && p->count > 1 )
+    if ( p->dir != IOREQ_WRITE || p->count > 1 )
     {
         /*
          * We cannot return X86EMUL_UNHANDLEABLE on anything other then the
          * first cycle of an I/O. So, since we cannot guarantee to always be

xen/arch/x86/hvm/stdvga.c static bool cf_check stdvga_mem_accept( · line 513

Quoted 10 lines
xen/arch/x86/hvm/stdvga.c +2 -4
          * able to send buffered writes, we have to reject any multi-cycle
-         * I/O.
+         * I/O. And of course we have to reject all reads, for not being
+         * able to service them.
          */
         goto reject;
     }
-    else if ( p->dir == IOREQ_READ &&
-              (true || !s->stdvga) )
-        goto reject;

Before, we rejected on (WRITE && count) or READ, and I think we still do afterwards given the boolean-ness of read/write.  However, the comment is distinctly less relevant.

Quoted 2 lines

I think we now just end up with /* Only accept single writes. */ which matches with with shuffling these into the bufioreq ring.

I'd like to keep a little more, if you don't mind:

/*
* Only accept single writes, as that's the only thing we accelerate
* using buffered ioreq handling. */
*/

Not the least because writing this I noticed another flaw (or even two, depending on how one looks at it): ioreq_send_buffered() further refuses data_is_ptr requests. (Checking ->data_is_ptr is actually more important than ->count, as ->count will be unequal to 1 only if ->data_is_ptr is set, whereas ->count can also be 1 in that case.) Not the least because that "refusing" is actually returning "success" (0 == X86EMUL_OKAY == IOREQ_STATUS_HANDLED) on x86, and IO_ABORT on Arm. I.e. such requests would simply be lost on x86, and whether IO_ABORT is okay(ish) on Arm I simply don't know (I'll see if digging through history reveals intentions).

And then - how can a buffered ioreq be a read, when there's nothing being returned? (I.e. I consider this an omission in what ioreq_send_buffered() refuses to process.)

Jan

#8 lore raw

PATCH 3/7 x86/HVM: remove unused MMIO handling code

In reply to Jan Beulich

All read accesses are rejected by the ->accept handler, while writes bypass the bulk of the function body. Drop the dead code, leaving an assertion in the read handler.

A number of other static items (and a macro) are then unreferenced and hence also need (want) dropping. The same applies to the "latch" field of the state structure.

xen/arch/x86/hvm/stdvga.c +4 -313
@@ -37,26 +37,6 @@
 #define VGA_MEM_BASE 0xa0000
 #define VGA_MEM_SIZE 0x20000
 
-#define PAT(x) (x)
-static const uint32_t mask16[16] = {
-    PAT(0x00000000U),
-    PAT(0x000000ffU),
-    PAT(0x0000ff00U),
-    PAT(0x0000ffffU),
-    PAT(0x00ff0000U),
-    PAT(0x00ff00ffU),
-    PAT(0x00ffff00U),
-    PAT(0x00ffffffU),
-    PAT(0xff000000U),
-    PAT(0xff0000ffU),
-    PAT(0xff00ff00U),
-    PAT(0xff00ffffU),
-    PAT(0xffff0000U),
-    PAT(0xffff00ffU),
-    PAT(0xffffff00U),
-    PAT(0xffffffffU),
-};
-
 /* force some bits to zero */
 static const uint8_t sr_mask[8] = {
     (uint8_t)~0xfc,
@@ -81,25 +61,6 @@ static const uint8_t gr_mask[9] = {
     (uint8_t)~0x00, /* 0x08 */
 };
 
-static uint8_t *vram_getb(struct hvm_hw_stdvga *s, unsigned int a)
-{
-    struct page_info *pg = s->vram_page[(a >> 12) & 0x3f];
-    uint8_t *p = __map_domain_page(pg);
-    return &p[a & 0xfff];
-}
-
-static uint32_t *vram_getl(struct hvm_hw_stdvga *s, unsigned int a)
-{
-    struct page_info *pg = s->vram_page[(a >> 10) & 0x3f];
-    uint32_t *p = __map_domain_page(pg);
-    return &p[a & 0x3ff];
-}
-
-static void vram_put(struct hvm_hw_stdvga *s, void *p)
-{
-    unmap_domain_page(p);
-}
-
 static int stdvga_outb(uint64_t addr, uint8_t val)
 {
     struct hvm_hw_stdvga *s = &current->domain->arch.hvm.stdvga;
@@ -168,244 +129,13 @@ static int cf_check stdvga_intercept_pio
     return X86EMUL_UNHANDLEABLE; /* propagate to external ioemu */
 }
 
-static unsigned int stdvga_mem_offset(
-    struct hvm_hw_stdvga *s, unsigned int mmio_addr)
-{
-    unsigned int memory_map_mode = (s->gr[6] >> 2) & 3;
-    unsigned int offset = mmio_addr & 0x1ffff;
-
-    switch ( memory_map_mode )
-    {
-    case 0:
-        break;
-    case 1:
-        if ( offset >= 0x10000 )
-            goto fail;
-        offset += 0; /* assume bank_offset == 0; */
-        break;
-    case 2:
-        offset -= 0x10000;
-        if ( offset >= 0x8000 )
-            goto fail;
-        break;
-    default:
-    case 3:
-        offset -= 0x18000;
-        if ( offset >= 0x8000 )
-            goto fail;
-        break;
-    }
-
-    return offset;
-
- fail:
-    return ~0u;
-}
-
-#define GET_PLANE(data, p) (((data) >> ((p) * 8)) & 0xff)
-
-static uint8_t stdvga_mem_readb(uint64_t addr)
-{
-    struct hvm_hw_stdvga *s = &current->domain->arch.hvm.stdvga;
-    int plane;
-    uint32_t ret, *vram_l;
-    uint8_t *vram_b;
-
-    addr = stdvga_mem_offset(s, addr);
-    if ( addr == ~0u )
-        return 0xff;
-
-    if ( s->sr[4] & 0x08 )
-    {
-        /* chain 4 mode : simplest access */
-        vram_b = vram_getb(s, addr);
-        ret = *vram_b;
-        vram_put(s, vram_b);
-    }
-    else if ( s->gr[5] & 0x10 )
-    {
-        /* odd/even mode (aka text mode mapping) */
-        plane = (s->gr[4] & 2) | (addr & 1);
-        vram_b = vram_getb(s, ((addr & ~1) << 1) | plane);
-        ret = *vram_b;
-        vram_put(s, vram_b);
-    }
-    else
-    {
-        /* standard VGA latched access */
-        vram_l = vram_getl(s, addr);
-        s->latch = *vram_l;
-        vram_put(s, vram_l);
-
-        if ( !(s->gr[5] & 0x08) )
-        {
-            /* read mode 0 */
-            plane = s->gr[4];
-            ret = GET_PLANE(s->latch, plane);
-        }
-        else
-        {
-            /* read mode 1 */
-            ret = (s->latch ^ mask16[s->gr[2]]) & mask16[s->gr[7]];
-            ret |= ret >> 16;
-            ret |= ret >> 8;
-            ret = (~ret) & 0xff;
-        }
-    }
-
-    return ret;
-}
-
 static int cf_check stdvga_mem_read(
     const struct hvm_io_handler *handler, uint64_t addr, uint32_t size,
     uint64_t *p_data)
 {
-    uint64_t data = ~0UL;
-
-    switch ( size )
-    {
-    case 1:
-        data = stdvga_mem_readb(addr);
-        break;
-
-    case 2:
-        data = stdvga_mem_readb(addr);
-        data |= stdvga_mem_readb(addr + 1) << 8;
-        break;
-
-    case 4:
-        data = stdvga_mem_readb(addr);
-        data |= stdvga_mem_readb(addr + 1) << 8;
-        data |= stdvga_mem_readb(addr + 2) << 16;
-        data |= (uint32_t)stdvga_mem_readb(addr + 3) << 24;
-        break;
-
-    case 8:
-        data =  (uint64_t)(stdvga_mem_readb(addr));
-        data |= (uint64_t)(stdvga_mem_readb(addr + 1)) << 8;
-        data |= (uint64_t)(stdvga_mem_readb(addr + 2)) << 16;
-        data |= (uint64_t)(stdvga_mem_readb(addr + 3)) << 24;
-        data |= (uint64_t)(stdvga_mem_readb(addr + 4)) << 32;
-        data |= (uint64_t)(stdvga_mem_readb(addr + 5)) << 40;
-        data |= (uint64_t)(stdvga_mem_readb(addr + 6)) << 48;
-        data |= (uint64_t)(stdvga_mem_readb(addr + 7)) << 56;
-        break;
-
-    default:
-        gdprintk(XENLOG_WARNING, "invalid io size: %u\n", size);
-        break;
-    }
-
-    *p_data = data;
-    return X86EMUL_OKAY;
-}
-
-static void stdvga_mem_writeb(uint64_t addr, uint32_t val)
-{
-    struct hvm_hw_stdvga *s = &current->domain->arch.hvm.stdvga;
-    int plane, write_mode, b, func_select, mask;
-    uint32_t write_mask, bit_mask, set_mask, *vram_l;
-    uint8_t *vram_b;
-
-    addr = stdvga_mem_offset(s, addr);
-    if ( addr == ~0u )
-        return;
-
-    if ( s->sr[4] & 0x08 )
-    {
-        /* chain 4 mode : simplest access */
-        plane = addr & 3;
-        mask = (1 << plane);
-        if ( s->sr[2] & mask )
-        {
-            vram_b = vram_getb(s, addr);
-            *vram_b = val;
-            vram_put(s, vram_b);
-        }
-    }
-    else if ( s->gr[5] & 0x10 )
-    {
-        /* odd/even mode (aka text mode mapping) */
-        plane = (s->gr[4] & 2) | (addr & 1);
-        mask = (1 << plane);
-        if ( s->sr[2] & mask )
-        {
-            addr = ((addr & ~1) << 1) | plane;
-            vram_b = vram_getb(s, addr);
-            *vram_b = val;
-            vram_put(s, vram_b);
-        }
-    }
-    else
-    {
-        write_mode = s->gr[5] & 3;
-        switch ( write_mode )
-        {
-        default:
-        case 0:
-            /* rotate */
-            b = s->gr[3] & 7;
-            val = ((val >> b) | (val << (8 - b))) & 0xff;
-            val |= val << 8;
-            val |= val << 16;
-
-            /* apply set/reset mask */
-            set_mask = mask16[s->gr[1]];
-            val = (val & ~set_mask) | (mask16[s->gr[0]] & set_mask);
-            bit_mask = s->gr[8];
-            break;
-        case 1:
-            val = s->latch;
-            goto do_write;
-        case 2:
-            val = mask16[val & 0x0f];
-            bit_mask = s->gr[8];
-            break;
-        case 3:
-            /* rotate */
-            b = s->gr[3] & 7;
-            val = (val >> b) | (val << (8 - b));
-
-            bit_mask = s->gr[8] & val;
-            val = mask16[s->gr[0]];
-            break;
-        }
-
-        /* apply logical operation */
-        func_select = s->gr[3] >> 3;
-        switch ( func_select )
-        {
-        case 0:
-        default:
-            /* nothing to do */
-            break;
-        case 1:
-            /* and */
-            val &= s->latch;
-            break;
-        case 2:
-            /* or */
-            val |= s->latch;
-            break;
-        case 3:
-            /* xor */
-            val ^= s->latch;
-            break;
-        }
-
-        /* apply bit mask */
-        bit_mask |= bit_mask << 8;
-        bit_mask |= bit_mask << 16;
-        val = (val & bit_mask) | (s->latch & ~bit_mask);
-
-    do_write:
-        /* mask data according to sr[2] */
-        mask = s->sr[2];
-        write_mask = mask16[mask];
-        vram_l = vram_getl(s, addr);
-        *vram_l = (*vram_l & ~write_mask) | (val & write_mask);
-        vram_put(s, vram_l);
-    }
+    ASSERT_UNREACHABLE();
+    *p_data = ~0;
+    return X86EMUL_UNHANDLEABLE;
 }
 
 static int cf_check stdvga_mem_write(
@@ -420,47 +150,8 @@ static int cf_check stdvga_mem_write(
         .dir = IOREQ_WRITE,
         .data = data,
     };
-    struct ioreq_server *srv;
-
-    goto done;
-
-    /* Intercept mmio write */
-    switch ( size )
-    {
-    case 1:
-        stdvga_mem_writeb(addr, (data >>  0) & 0xff);
-        break;
-
-    case 2:
-        stdvga_mem_writeb(addr+0, (data >>  0) & 0xff);
-        stdvga_mem_writeb(addr+1, (data >>  8) & 0xff);
-        break;
-
-    case 4:
-        stdvga_mem_writeb(addr+0, (data >>  0) & 0xff);
-        stdvga_mem_writeb(addr+1, (data >>  8) & 0xff);
-        stdvga_mem_writeb(addr+2, (data >> 16) & 0xff);
-        stdvga_mem_writeb(addr+3, (data >> 24) & 0xff);
-        break;
-
-    case 8:
-        stdvga_mem_writeb(addr+0, (data >>  0) & 0xff);
-        stdvga_mem_writeb(addr+1, (data >>  8) & 0xff);
-        stdvga_mem_writeb(addr+2, (data >> 16) & 0xff);
-        stdvga_mem_writeb(addr+3, (data >> 24) & 0xff);
-        stdvga_mem_writeb(addr+4, (data >> 32) & 0xff);
-        stdvga_mem_writeb(addr+5, (data >> 40) & 0xff);
-        stdvga_mem_writeb(addr+6, (data >> 48) & 0xff);
-        stdvga_mem_writeb(addr+7, (data >> 56) & 0xff);
-        break;
-
-    default:
-        gdprintk(XENLOG_WARNING, "invalid io size: %u\n", size);
-        break;
-    }
+    struct ioreq_server *srv = ioreq_server_select(current->domain, &p);
 
- done:
-    srv = ioreq_server_select(current->domain, &p);
     if ( !srv )
         return X86EMUL_UNHANDLEABLE;
 
xen/arch/x86/include/asm/hvm/io.h +0 -1
@@ -115,7 +115,6 @@ struct hvm_hw_stdvga {
     uint8_t sr[8];
     uint8_t gr_index;
     uint8_t gr[9];
-    uint32_t latch;
     struct page_info *vram_page[64];  /* shadow of 0xa0000-0xaffff */
     spinlock_t lock;
 };
Reviewed-by on v1 3/7
#9 lore raw

Re: [PATCH 3/7] x86/HVM: remove unused MMIO handling code

Quoted 9 lines · Jan Beulich

All read accesses are rejected by the ->accept handler, while writes bypass the bulk of the function body. Drop the dead code, leaving an assertion in the read handler.

A number of other static items (and a macro) are then unreferenced and hence also need (want) dropping. The same applies to the "latch" field of the state structure.

In the read handler, I don't think we need to fill in ~0.  A while back, we had the base layer fill this in, to fix stack rubble leaks.  Either way,

I'm sad that we called map & unmap for every byte in multi-byte accesses.  Good riddance.

#10 lore raw

Re: [PATCH 3/7] x86/HVM: remove unused MMIO handling code

Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

All read accesses are rejected by the ->accept handler, while writes bypass the bulk of the function body. Drop the dead code, leaving an assertion in the read handler.

Quoted 8 lines
Quoted 5 lines

A number of other static items (and a macro) are then unreferenced and hence also need (want) dropping. The same applies to the "latch" field of the state structure.

In the read handler, I don't think we need to fill in ~0.  A while back, we had the base layer fill this in, to fix stack rubble leaks.

It fills something, yes, but 0, not ~0. Plus I was trying to make obvious by just looking at the function that nothing unexpected can happen even in release builds, if the function was really called in error.

Quoted 3 lines

  Either way,

Thanks.

Jan

#11 lore raw

PATCH 4/7 x86/HVM: drop stdvga's "gr[]" struct member

In reply to Jan Beulich

No consumers are left, hence the producer and the array itself can also go away. The static gr_mask[] is then orphaned and hence needs dropping, too.

xen/arch/x86/hvm/stdvga.c +0 -18
@@ -49,18 +49,6 @@ static const uint8_t sr_mask[8] = {
     (uint8_t)~0x00,
 };
 
-static const uint8_t gr_mask[9] = {
-    (uint8_t)~0xf0, /* 0x00 */
-    (uint8_t)~0xf0, /* 0x01 */
-    (uint8_t)~0xf0, /* 0x02 */
-    (uint8_t)~0xe0, /* 0x03 */
-    (uint8_t)~0xfc, /* 0x04 */
-    (uint8_t)~0x84, /* 0x05 */
-    (uint8_t)~0xf0, /* 0x06 */
-    (uint8_t)~0xf0, /* 0x07 */
-    (uint8_t)~0x00, /* 0x08 */
-};
-
 static int stdvga_outb(uint64_t addr, uint8_t val)
 {
     struct hvm_hw_stdvga *s = &current->domain->arch.hvm.stdvga;
@@ -82,12 +70,6 @@ static int stdvga_outb(uint64_t addr, ui
         s->gr_index = val;
         break;
 
-    case 0x3cf:                 /* graphics data register */
-        rc = (s->gr_index < sizeof(s->gr));
-        if ( rc )
-            s->gr[s->gr_index] = val & gr_mask[s->gr_index];
-        break;
-
     default:
         rc = 0;
         break;
xen/arch/x86/include/asm/hvm/io.h +0 -1
@@ -114,7 +114,6 @@ struct hvm_hw_stdvga {
     uint8_t sr_index;
     uint8_t sr[8];
     uint8_t gr_index;
-    uint8_t gr[9];
     struct page_info *vram_page[64];  /* shadow of 0xa0000-0xaffff */
     spinlock_t lock;
 };
#13 lore raw

PATCH 5/7 x86/HVM: drop stdvga's "sr[]" struct member

In reply to Jan Beulich

No consumers are left, hence the producer and the array itself can also go away. The static sr_mask[] is then orphaned and hence needs dropping, too.

xen/arch/x86/hvm/stdvga.c +0 -18
@@ -37,18 +37,6 @@
 #define VGA_MEM_BASE 0xa0000
 #define VGA_MEM_SIZE 0x20000
 
-/* force some bits to zero */
-static const uint8_t sr_mask[8] = {
-    (uint8_t)~0xfc,
-    (uint8_t)~0xc2,
-    (uint8_t)~0xf0,
-    (uint8_t)~0xc0,
-    (uint8_t)~0xf1,
-    (uint8_t)~0xff,
-    (uint8_t)~0xff,
-    (uint8_t)~0x00,
-};
-
 static int stdvga_outb(uint64_t addr, uint8_t val)
 {
     struct hvm_hw_stdvga *s = &current->domain->arch.hvm.stdvga;
@@ -60,12 +48,6 @@ static int stdvga_outb(uint64_t addr, ui
         s->sr_index = val;
         break;
 
-    case 0x3c5:                 /* sequencer data register */
-        rc = (s->sr_index < sizeof(s->sr));
-        if ( rc )
-            s->sr[s->sr_index] = val & sr_mask[s->sr_index] ;
-        break;
-
     case 0x3ce:                 /* graphics address register */
         s->gr_index = val;
         break;
xen/arch/x86/include/asm/hvm/io.h +0 -1
@@ -112,7 +112,6 @@ struct vpci_arch_msix_entry {
 
 struct hvm_hw_stdvga {
     uint8_t sr_index;
-    uint8_t sr[8];
     uint8_t gr_index;
     struct page_info *vram_page[64];  /* shadow of 0xa0000-0xaffff */
     spinlock_t lock;
#15 lore raw

PATCH 6/7 x86/HVM: drop stdvga's "{g,s}r_index" struct members

In reply to Jan Beulich

No consumers are left, hence the producer and the fields themselves can also go away. stdvga_outb() is then useless, rendering stdvga_out() useless as well. Hence the entire I/O port intercept can go away.

xen/arch/x86/hvm/stdvga.c +0 -61
@@ -37,62 +37,6 @@
 #define VGA_MEM_BASE 0xa0000
 #define VGA_MEM_SIZE 0x20000
 
-static int stdvga_outb(uint64_t addr, uint8_t val)
-{
-    struct hvm_hw_stdvga *s = &current->domain->arch.hvm.stdvga;
-    int rc = 1;
-
-    switch ( addr )
-    {
-    case 0x3c4:                 /* sequencer address register */
-        s->sr_index = val;
-        break;
-
-    case 0x3ce:                 /* graphics address register */
-        s->gr_index = val;
-        break;
-
-    default:
-        rc = 0;
-        break;
-    }
-
-    return rc;
-}
-
-static void stdvga_out(uint32_t port, uint32_t bytes, uint32_t val)
-{
-    switch ( bytes )
-    {
-    case 1:
-        stdvga_outb(port, val);
-        break;
-
-    case 2:
-        stdvga_outb(port + 0, val >> 0);
-        stdvga_outb(port + 1, val >> 8);
-        break;
-
-    default:
-        break;
-    }
-}
-
-static int cf_check stdvga_intercept_pio(
-    int dir, unsigned int port, unsigned int bytes, uint32_t *val)
-{
-    struct hvm_hw_stdvga *s = &current->domain->arch.hvm.stdvga;
-
-    if ( dir == IOREQ_WRITE )
-    {
-        spin_lock(&s->lock);
-        stdvga_out(port, bytes, *val);
-        spin_unlock(&s->lock);
-    }
-
-    return X86EMUL_UNHANDLEABLE; /* propagate to external ioemu */
-}
-
 static int cf_check stdvga_mem_read(
     const struct hvm_io_handler *handler, uint64_t addr, uint32_t size,
     uint64_t *p_data)
@@ -197,11 +141,6 @@ void stdvga_init(struct domain *d)
     {
         struct hvm_io_handler *handler;
 
-        /* Sequencer registers. */
-        register_portio_handler(d, 0x3c4, 2, stdvga_intercept_pio);
-        /* Graphics registers. */
-        register_portio_handler(d, 0x3ce, 2, stdvga_intercept_pio);
-
         /* VGA memory */
         handler = hvm_next_io_handler(d);
 
xen/arch/x86/include/asm/hvm/io.h +0 -2
@@ -111,8 +111,6 @@ struct vpci_arch_msix_entry {
 };
 
 struct hvm_hw_stdvga {
-    uint8_t sr_index;
-    uint8_t gr_index;
     struct page_info *vram_page[64];  /* shadow of 0xa0000-0xaffff */
     spinlock_t lock;
 };
Reviewed-by on v1 6/7
#16 lore raw

Re: [PATCH 6/7] x86/HVM: drop stdvga's "{g,s}r_index" struct members

Quoted 5 lines · Jan Beulich

No consumers are left, hence the producer and the fields themselves can also go away. stdvga_outb() is then useless, rendering stdvga_out() useless as well. Hence the entire I/O port intercept can go away.

#17 lore raw

PATCH 7/7 x86/HVM: drop stdvga's "vram_page[]" struct member

In reply to Jan Beulich

No uses are left, hence its setup, teardown, and the field itself can also go away. stdvga_deinit() is then empty and can be dropped as well.

---
I have no idea whether in the tool stack some memory calculations would want adjusting, to account for the 256k less that a HVM domain now needs.

xen/arch/x86/hvm/hvm.c +0 -2
@@ -700,7 +700,6 @@ int hvm_domain_initialise(struct domain
     return 0;
 
  fail2:
-    stdvga_deinit(d);
     vioapic_deinit(d);
  fail1:
     if ( is_hardware_domain(d) )
@@ -763,7 +762,6 @@ void hvm_domain_destroy(struct domain *d
     if ( hvm_funcs.domain_destroy )
         alternative_vcall(hvm_funcs.domain_destroy, d);
 
-    stdvga_deinit(d);
     vioapic_deinit(d);
 
     XFREE(d->arch.hvm.pl_time);
xen/arch/x86/hvm/stdvga.c +4 -37

59 lines not on this page · raw message

xen/arch/x86/include/asm/hvm/io.h +0 -2
@@ -111,12 +111,10 @@ struct vpci_arch_msix_entry {
 };
 
 struct hvm_hw_stdvga {
-    struct page_info *vram_page[64];  /* shadow of 0xa0000-0xaffff */
     spinlock_t lock;
 };
 
 void stdvga_init(struct domain *d);
-void stdvga_deinit(struct domain *d);
 
 extern void hvm_dpci_msi_eoi(struct domain *d, int vector);
 
Reviewed-by on v1 7/7
#18 lore raw

Re: [PATCH 7/7] x86/HVM: drop stdvga's "vram_page[]" struct member

Quoted 4 lines · Jan Beulich

No uses are left, hence its setup, teardown, and the field itself can also go away. stdvga_deinit() is then empty and can be dropped as well.

there's more to do.

Quoted 4 lines

---
I have no idea whether in the tool stack some memory calculations would want adjusting, to account for the 256k less that a HVM domain now needs.

I honestly don't know.  We need dalloc() and friends, simply to figure out whether it's a significant number or not.

xen/arch/x86/include/asm/hvm/io.h struct vpci_arch_msix_entry { · line 115

Quoted 9 lines
xen/arch/x86/include/asm/hvm/io.h +0 -1
@@ -111,12 +111,10 @@ struct vpci_arch_msix_entry {
 };
 
 struct hvm_hw_stdvga {
-    struct page_info *vram_page[64];  /* shadow of 0xa0000-0xaffff */
     spinlock_t lock;
 };

I'm pretty sure you can drop the lock too.  It's taken in accept(), and dropped in complete(), but there's no state at all to be protected.

stdvga_mem_accept()'s return value is a simple expression of p.

With that dropped, the complete() handler disappears, and it's the only hvm_io_ops->complete() handler in Xen so the whole field can go.

So I'm pretty sure there are 2 more patches that ought to be part of this series, which go in a further negative direction.

~Andrew

#19 lore raw

Re: [PATCH 7/7] x86/HVM: drop stdvga's "vram_page[]" struct member

Quoted 7 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

No uses are left, hence its setup, teardown, and the field itself can also go away. stdvga_deinit() is then empty and can be dropped as well.

Thanks.

Quoted 15 lines

although I think there's more to do.

xen/arch/x86/include/asm/hvm/io.h struct vpci_arch_msix_entry { · line 115

Quoted 9 lines
xen/arch/x86/include/asm/hvm/io.h +0 -1
@@ -111,12 +111,10 @@ struct vpci_arch_msix_entry {
 };
 
 struct hvm_hw_stdvga {
-    struct page_info *vram_page[64];  /* shadow of 0xa0000-0xaffff */
     spinlock_t lock;
 };

I'm pretty sure you can drop the lock too.  It's taken in accept(), and dropped in complete(), but there's no state at all to be protected.

Quoted 1 line

stdvga_mem_accept()'s return value is a simple expression of p.

I think you're right. Previously I was assuming the lock was (also) about serializing the bufioreq sending, yet ioreq_send_buffered() has its own serialization. And hence yes, with all other state gone, the lock can go too, as can ...

Quoted 2 lines

With that dropped, the complete() handler disappears, and it's the only hvm_io_ops->complete() handler in Xen so the whole field can go.

... this hook.

Quoted 2 lines

So I'm pretty sure there are 2 more patches that ought to be part of this series, which go in a further negative direction.

Will do.

Jan

#20 lore raw
In reply to Jan Beulich
Quoted 11 lines · Jan Beulich

It's been unused for nearly 9 years. By the end of the series stdvga.c's sole purpose will be to make sure VRAM writes use the bufio ioreq path.

1: drop stdvga's "cache" struct member
2: drop stdvga's "stdvga" struct member
3: remove unused MMIO handling code
4: drop stdvga's "gr[]" struct member
5: drop stdvga's "sr[]" struct member
6: drop stdvga's "{g,s}r_index" struct members
7: drop stdvga's "vram_page[]" struct member

Quoted 3 lines

Of course all of these could be folded into a single patch, but I think going piecemeal makes more clear that the individual parts are indeed unused.

I far prefer a series split like this, than a single patch with 7 moving parts.

Also, this is certainly in the right direction.

$ ../scripts/bloat-o-meter -c xen-syms-before xen-syms-after
add/remove: 0/6 grow/shrink: 0/7 up/down: 0/-2640 (-2640)
Function                                     old     new   delta
hvm_domain_destroy                           250     242      -8
hvm_load                                     548     532     -16
hvm_domain_initialise                       1050    1034     -16
stdvga_deinit                                 67       -     -67
stdvga_mem_accept                            236     166     -70
stdvga_mem_offset.isra                       103       -    -103
stdvga_intercept_pio                         158       -    -158
stdvga_init                                  223      52    -171
stdvga_outb.isra                             240       -    -240
stdvga_mem_write                             430     105    -325
stdvga_mem_readb                             396       -    -396
stdvga_mem_read                              423      17    -406
stdvga_mem_writeb                            664       -    -664
add/remove: 0/3 grow/shrink: 0/0 up/down: 0/-81 (-81)
Data                                         old     new   delta
sr_mask                                        8       -      -8
gr_mask                                        9       -      -9
mask16                                        64       -     -64

Interestingly nm identifies these as 'd' (initialised data) rather than 'n' or 'r' (read-only data), but they are between _{s,e}rodata.

~Andrew