Blame 0009-vga-add-sr_vbe-register-set.patch

f0208c
From: Gerd Hoffmann <kraxel@redhat.com>
f0208c
Date: Tue, 17 May 2016 10:54:54 +0200
f0208c
Subject: [PATCH] vga: add sr_vbe register set
f0208c
f0208c
Commit "fd3c136 vga: make sure vga register setup for vbe stays intact
f0208c
(CVE-2016-3712)." causes a regression.  The win7 installer is unhappy
f0208c
because it can't freely modify vga registers any more while in vbe mode.
f0208c
f0208c
This patch introduces a new sr_vbe register set.  The vbe_update_vgaregs
f0208c
will fill sr_vbe[] instead of sr[].  Normal vga register reads and
f0208c
writes go to sr[].  Any sr register read access happens through a new
f0208c
sr() helper function which will read from sr_vbe[] with vbe active and
f0208c
from sr[] otherwise.
f0208c
f0208c
This way we can allow guests update sr[] registers as they want, without
f0208c
allowing them disrupt vbe video modes that way.
f0208c
f0208c
Cc: qemu-stable@nongnu.org
f0208c
Reported-by: Thomas Lamprecht <thomas@lamprecht.org>
f0208c
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
f0208c
Message-id: 1463475294-14119-1-git-send-email-kraxel@redhat.com
f0208c
(cherry picked from commit 94ef4f337fb614f18b765a8e0e878a4c23cdedcd)
f0208c
---
f0208c
 hw/display/vga.c     | 50 ++++++++++++++++++++++++++++----------------------
f0208c
 hw/display/vga_int.h |  1 +
f0208c
 2 files changed, 29 insertions(+), 22 deletions(-)
f0208c
f0208c
diff --git a/hw/display/vga.c b/hw/display/vga.c
f0208c
index 4a55ec6..9ebc54f 100644
f0208c
--- a/hw/display/vga.c
f0208c
+++ b/hw/display/vga.c
f0208c
@@ -149,6 +149,11 @@ static inline bool vbe_enabled(VGACommonState *s)
f0208c
     return s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED;
f0208c
 }
f0208c
 
f0208c
+static inline uint8_t sr(VGACommonState *s, int idx)
f0208c
+{
f0208c
+    return vbe_enabled(s) ? s->sr_vbe[idx] : s->sr[idx];
f0208c
+}
f0208c
+
f0208c
 static void vga_update_memory_access(VGACommonState *s)
f0208c
 {
f0208c
     hwaddr base, offset, size;
f0208c
@@ -163,8 +168,8 @@ static void vga_update_memory_access(VGACommonState *s)
f0208c
         s->has_chain4_alias = false;
f0208c
         s->plane_updated = 0xf;
f0208c
     }
f0208c
-    if ((s->sr[VGA_SEQ_PLANE_WRITE] & VGA_SR02_ALL_PLANES) ==
f0208c
-        VGA_SR02_ALL_PLANES && s->sr[VGA_SEQ_MEMORY_MODE] & VGA_SR04_CHN_4M) {
f0208c
+    if ((sr(s, VGA_SEQ_PLANE_WRITE) & VGA_SR02_ALL_PLANES) ==
f0208c
+        VGA_SR02_ALL_PLANES && sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
f0208c
         offset = 0;
f0208c
         switch ((s->gr[VGA_GFX_MISC] >> 2) & 3) {
f0208c
         case 0:
f0208c
@@ -234,7 +239,7 @@ static void vga_precise_update_retrace_info(VGACommonState *s)
f0208c
           ((s->cr[VGA_CRTC_OVERFLOW] >> 6) & 2)) << 8);
f0208c
     vretr_end_line = s->cr[VGA_CRTC_V_SYNC_END] & 0xf;
f0208c
 
f0208c
-    clocking_mode = (s->sr[VGA_SEQ_CLOCK_MODE] >> 3) & 1;
f0208c
+    clocking_mode = (sr(s, VGA_SEQ_CLOCK_MODE) >> 3) & 1;
f0208c
     clock_sel = (s->msr >> 2) & 3;
f0208c
     dots = (s->msr & 1) ? 8 : 9;
f0208c
 
f0208c
@@ -486,7 +491,6 @@ void vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
f0208c
         printf("vga: write SR%x = 0x%02x\n", s->sr_index, val);
f0208c
 #endif
f0208c
         s->sr[s->sr_index] = val & sr_mask[s->sr_index];
f0208c
-        vbe_update_vgaregs(s);
f0208c
         if (s->sr_index == VGA_SEQ_CLOCK_MODE) {
f0208c
             s->update_retrace_info(s);
f0208c
         }
f0208c
@@ -680,13 +684,13 @@ static void vbe_update_vgaregs(VGACommonState *s)
f0208c
 
f0208c
     if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4) {
f0208c
         shift_control = 0;
f0208c
-        s->sr[VGA_SEQ_CLOCK_MODE] &= ~8; /* no double line */
f0208c
+        s->sr_vbe[VGA_SEQ_CLOCK_MODE] &= ~8; /* no double line */
f0208c
     } else {
f0208c
         shift_control = 2;
f0208c
         /* set chain 4 mode */
f0208c
-        s->sr[VGA_SEQ_MEMORY_MODE] |= VGA_SR04_CHN_4M;
f0208c
+        s->sr_vbe[VGA_SEQ_MEMORY_MODE] |= VGA_SR04_CHN_4M;
f0208c
         /* activate all planes */
f0208c
-        s->sr[VGA_SEQ_PLANE_WRITE] |= VGA_SR02_ALL_PLANES;
f0208c
+        s->sr_vbe[VGA_SEQ_PLANE_WRITE] |= VGA_SR02_ALL_PLANES;
f0208c
     }
f0208c
     s->gr[VGA_GFX_MODE] = (s->gr[VGA_GFX_MODE] & ~0x60) |
f0208c
         (shift_control << 5);
f0208c
@@ -836,7 +840,7 @@ uint32_t vga_mem_readb(VGACommonState *s, hwaddr addr)
f0208c
         break;
f0208c
     }
f0208c
 
f0208c
-    if (s->sr[VGA_SEQ_MEMORY_MODE] & VGA_SR04_CHN_4M) {
f0208c
+    if (sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
f0208c
         /* chain 4 mode : simplest access */
f0208c
         assert(addr < s->vram_size);
f0208c
         ret = s->vram_ptr[addr];
f0208c
@@ -904,11 +908,11 @@ void vga_mem_writeb(VGACommonState *s, hwaddr addr, uint32_t val)
f0208c
         break;
f0208c
     }
f0208c
 
f0208c
-    if (s->sr[VGA_SEQ_MEMORY_MODE] & VGA_SR04_CHN_4M) {
f0208c
+    if (sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
f0208c
         /* chain 4 mode : simplest access */
f0208c
         plane = addr & 3;
f0208c
         mask = (1 << plane);
f0208c
-        if (s->sr[VGA_SEQ_PLANE_WRITE] & mask) {
f0208c
+        if (sr(s, VGA_SEQ_PLANE_WRITE) & mask) {
f0208c
             assert(addr < s->vram_size);
f0208c
             s->vram_ptr[addr] = val;
f0208c
 #ifdef DEBUG_VGA_MEM
f0208c
@@ -921,7 +925,7 @@ void vga_mem_writeb(VGACommonState *s, hwaddr addr, uint32_t val)
f0208c
         /* odd/even mode (aka text mode mapping) */
f0208c
         plane = (s->gr[VGA_GFX_PLANE_READ] & 2) | (addr & 1);
f0208c
         mask = (1 << plane);
f0208c
-        if (s->sr[VGA_SEQ_PLANE_WRITE] & mask) {
f0208c
+        if (sr(s, VGA_SEQ_PLANE_WRITE) & mask) {
f0208c
             addr = ((addr & ~1) << 1) | plane;
f0208c
             if (addr >= s->vram_size) {
f0208c
                 return;
f0208c
@@ -996,7 +1000,7 @@ void vga_mem_writeb(VGACommonState *s, hwaddr addr, uint32_t val)
f0208c
 
f0208c
     do_write:
f0208c
         /* mask data according to sr[2] */
f0208c
-        mask = s->sr[VGA_SEQ_PLANE_WRITE];
f0208c
+        mask = sr(s, VGA_SEQ_PLANE_WRITE);
f0208c
         s->plane_updated |= mask; /* only used to detect font change */
f0208c
         write_mask = mask16[mask];
f0208c
         if (addr * sizeof(uint32_t) >= s->vram_size) {
f0208c
@@ -1152,10 +1156,10 @@ static void vga_get_text_resolution(VGACommonState *s, int *pwidth, int *pheight
f0208c
     /* total width & height */
f0208c
     cheight = (s->cr[VGA_CRTC_MAX_SCAN] & 0x1f) + 1;
f0208c
     cwidth = 8;
f0208c
-    if (!(s->sr[VGA_SEQ_CLOCK_MODE] & VGA_SR01_CHAR_CLK_8DOTS)) {
f0208c
+    if (!(sr(s, VGA_SEQ_CLOCK_MODE) & VGA_SR01_CHAR_CLK_8DOTS)) {
f0208c
         cwidth = 9;
f0208c
     }
f0208c
-    if (s->sr[VGA_SEQ_CLOCK_MODE] & 0x08) {
f0208c
+    if (sr(s, VGA_SEQ_CLOCK_MODE) & 0x08) {
f0208c
         cwidth = 16; /* NOTE: no 18 pixel wide */
f0208c
     }
f0208c
     width = (s->cr[VGA_CRTC_H_DISP] + 1);
f0208c
@@ -1197,7 +1201,7 @@ static void vga_draw_text(VGACommonState *s, int full_update)
f0208c
     int64_t now = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
f0208c
 
f0208c
     /* compute font data address (in plane 2) */
f0208c
-    v = s->sr[VGA_SEQ_CHARACTER_MAP];
f0208c
+    v = sr(s, VGA_SEQ_CHARACTER_MAP);
f0208c
     offset = (((v >> 4) & 1) | ((v << 1) & 6)) * 8192 * 4 + 2;
f0208c
     if (offset != s->font_offsets[0]) {
f0208c
         s->font_offsets[0] = offset;
f0208c
@@ -1506,11 +1510,11 @@ static void vga_draw_graphic(VGACommonState *s, int full_update)
f0208c
     }
f0208c
 
f0208c
     if (shift_control == 0) {
f0208c
-        if (s->sr[VGA_SEQ_CLOCK_MODE] & 8) {
f0208c
+        if (sr(s, VGA_SEQ_CLOCK_MODE) & 8) {
f0208c
             disp_width <<= 1;
f0208c
         }
f0208c
     } else if (shift_control == 1) {
f0208c
-        if (s->sr[VGA_SEQ_CLOCK_MODE] & 8) {
f0208c
+        if (sr(s, VGA_SEQ_CLOCK_MODE) & 8) {
f0208c
             disp_width <<= 1;
f0208c
         }
f0208c
     }
f0208c
@@ -1574,7 +1578,7 @@ static void vga_draw_graphic(VGACommonState *s, int full_update)
f0208c
 
f0208c
     if (shift_control == 0) {
f0208c
         full_update |= update_palette16(s);
f0208c
-        if (s->sr[VGA_SEQ_CLOCK_MODE] & 8) {
f0208c
+        if (sr(s, VGA_SEQ_CLOCK_MODE) & 8) {
f0208c
             v = VGA_DRAW_LINE4D2;
f0208c
         } else {
f0208c
             v = VGA_DRAW_LINE4;
f0208c
@@ -1582,7 +1586,7 @@ static void vga_draw_graphic(VGACommonState *s, int full_update)
f0208c
         bits = 4;
f0208c
     } else if (shift_control == 1) {
f0208c
         full_update |= update_palette16(s);
f0208c
-        if (s->sr[VGA_SEQ_CLOCK_MODE] & 8) {
f0208c
+        if (sr(s, VGA_SEQ_CLOCK_MODE) & 8) {
f0208c
             v = VGA_DRAW_LINE2D2;
f0208c
         } else {
f0208c
             v = VGA_DRAW_LINE2;
f0208c
@@ -1629,7 +1633,7 @@ static void vga_draw_graphic(VGACommonState *s, int full_update)
f0208c
 #if 0
f0208c
     printf("w=%d h=%d v=%d line_offset=%d cr[0x09]=0x%02x cr[0x17]=0x%02x linecmp=%d sr[0x01]=0x%02x\n",
f0208c
            width, height, v, line_offset, s->cr[9], s->cr[VGA_CRTC_MODE],
f0208c
-           s->line_compare, s->sr[VGA_SEQ_CLOCK_MODE]);
f0208c
+           s->line_compare, sr(s, VGA_SEQ_CLOCK_MODE));
f0208c
 #endif
f0208c
     addr1 = (s->start_addr * 4);
f0208c
     bwidth = (width * bits + 7) / 8;
f0208c
@@ -1781,6 +1785,7 @@ void vga_common_reset(VGACommonState *s)
f0208c
 {
f0208c
     s->sr_index = 0;
f0208c
     memset(s->sr, '\0', sizeof(s->sr));
f0208c
+    memset(s->sr_vbe, '\0', sizeof(s->sr_vbe));
f0208c
     s->gr_index = 0;
f0208c
     memset(s->gr, '\0', sizeof(s->gr));
f0208c
     s->ar_index = 0;
f0208c
@@ -1883,10 +1888,10 @@ static void vga_update_text(void *opaque, console_ch_t *chardata)
f0208c
         /* total width & height */
f0208c
         cheight = (s->cr[VGA_CRTC_MAX_SCAN] & 0x1f) + 1;
f0208c
         cw = 8;
f0208c
-        if (!(s->sr[VGA_SEQ_CLOCK_MODE] & VGA_SR01_CHAR_CLK_8DOTS)) {
f0208c
+        if (!(sr(s, VGA_SEQ_CLOCK_MODE) & VGA_SR01_CHAR_CLK_8DOTS)) {
f0208c
             cw = 9;
f0208c
         }
f0208c
-        if (s->sr[VGA_SEQ_CLOCK_MODE] & 0x08) {
f0208c
+        if (sr(s, VGA_SEQ_CLOCK_MODE) & 0x08) {
f0208c
             cw = 16; /* NOTE: no 18 pixel wide */
f0208c
         }
f0208c
         width = (s->cr[VGA_CRTC_H_DISP] + 1);
f0208c
@@ -2053,6 +2058,7 @@ static int vga_common_post_load(void *opaque, int version_id)
f0208c
 
f0208c
     /* force refresh */
f0208c
     s->graphic_mode = -1;
f0208c
+    vbe_update_vgaregs(s);
f0208c
     return 0;
f0208c
 }
f0208c
 
f0208c
diff --git a/hw/display/vga_int.h b/hw/display/vga_int.h
f0208c
index bdb43a5..3ce5544 100644
f0208c
--- a/hw/display/vga_int.h
f0208c
+++ b/hw/display/vga_int.h
f0208c
@@ -98,6 +98,7 @@ typedef struct VGACommonState {
f0208c
     MemoryRegion chain4_alias;
f0208c
     uint8_t sr_index;
f0208c
     uint8_t sr[256];
f0208c
+    uint8_t sr_vbe[256];
f0208c
     uint8_t gr_index;
f0208c
     uint8_t gr[256];
f0208c
     uint8_t ar_index;