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