9ae3a8
From 40d4a0ec487abcde65d4175dde0c9dda45b570f9 Mon Sep 17 00:00:00 2001
9ae3a8
From: Gerd Hoffmann <kraxel@redhat.com>
9ae3a8
Date: Thu, 16 Jun 2016 15:30:11 +0200
9ae3a8
Subject: [PATCH 2/2] vga: add sr_vbe register set
9ae3a8
9ae3a8
RH-Author: Gerd Hoffmann <kraxel@redhat.com>
9ae3a8
Message-id: <1466091011-8095-2-git-send-email-kraxel@redhat.com>
9ae3a8
Patchwork-id: 70639
9ae3a8
O-Subject: [RHEL-7.3 qemu-kvm PATCH 1/1] vga: add sr_vbe register set
9ae3a8
Bugzilla: 1346982
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
9ae3a8
9ae3a8
Commit "fd3c136 vga: make sure vga register setup for vbe stays intact
9ae3a8
(CVE-2016-3712)." causes a regression.  The win7 installer is unhappy
9ae3a8
because it can't freely modify vga registers any more while in vbe mode.
9ae3a8
9ae3a8
This patch introduces a new sr_vbe register set.  The vbe_update_vgaregs
9ae3a8
will fill sr_vbe[] instead of sr[].  Normal vga register reads and
9ae3a8
writes go to sr[].  Any sr register read access happens through a new
9ae3a8
sr() helper function which will read from sr_vbe[] with vbe active and
9ae3a8
from sr[] otherwise.
9ae3a8
9ae3a8
This way we can allow guests update sr[] registers as they want, without
9ae3a8
allowing them disrupt vbe video modes that way.
9ae3a8
9ae3a8
Cc: qemu-stable@nongnu.org
9ae3a8
Reported-by: Thomas Lamprecht <thomas@lamprecht.org>
9ae3a8
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
9ae3a8
Message-id: 1463475294-14119-1-git-send-email-kraxel@redhat.com
9ae3a8
(cherry picked from commit 94ef4f337fb614f18b765a8e0e878a4c23cdedcd)
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	hw/display/vga.c
9ae3a8
9ae3a8
[ RHEL-7 note: context differences in vga_update_memory_access ]
9ae3a8
---
9ae3a8
 hw/display/vga.c     | 50 ++++++++++++++++++++++++++++----------------------
9ae3a8
 hw/display/vga_int.h |  1 +
9ae3a8
 2 files changed, 29 insertions(+), 22 deletions(-)
9ae3a8
9ae3a8
diff --git a/hw/display/vga.c b/hw/display/vga.c
9ae3a8
index f049b26..4e3c3f3 100644
9ae3a8
--- a/hw/display/vga.c
9ae3a8
+++ b/hw/display/vga.c
9ae3a8
@@ -173,6 +173,11 @@ static inline bool vbe_enabled(VGACommonState *s)
9ae3a8
     return s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED;
9ae3a8
 }
9ae3a8
 
9ae3a8
+static inline uint8_t sr(VGACommonState *s, int idx)
9ae3a8
+{
9ae3a8
+    return vbe_enabled(s) ? s->sr_vbe[idx] : s->sr[idx];
9ae3a8
+}
9ae3a8
+
9ae3a8
 static void vga_update_memory_access(VGACommonState *s)
9ae3a8
 {
9ae3a8
     MemoryRegion *region, *old_region = s->chain4_alias;
9ae3a8
@@ -180,8 +185,8 @@ static void vga_update_memory_access(VGACommonState *s)
9ae3a8
 
9ae3a8
     s->chain4_alias = NULL;
9ae3a8
 
9ae3a8
-    if ((s->sr[VGA_SEQ_PLANE_WRITE] & VGA_SR02_ALL_PLANES) ==
9ae3a8
-        VGA_SR02_ALL_PLANES && s->sr[VGA_SEQ_MEMORY_MODE] & VGA_SR04_CHN_4M) {
9ae3a8
+    if ((sr(s, VGA_SEQ_PLANE_WRITE) & VGA_SR02_ALL_PLANES) ==
9ae3a8
+        VGA_SR02_ALL_PLANES && sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
9ae3a8
         offset = 0;
9ae3a8
         switch ((s->gr[VGA_GFX_MISC] >> 2) & 3) {
9ae3a8
         case 0:
9ae3a8
@@ -257,7 +262,7 @@ static void vga_precise_update_retrace_info(VGACommonState *s)
9ae3a8
           ((s->cr[VGA_CRTC_OVERFLOW] >> 6) & 2)) << 8);
9ae3a8
     vretr_end_line = s->cr[VGA_CRTC_V_SYNC_END] & 0xf;
9ae3a8
 
9ae3a8
-    clocking_mode = (s->sr[VGA_SEQ_CLOCK_MODE] >> 3) & 1;
9ae3a8
+    clocking_mode = (sr(s, VGA_SEQ_CLOCK_MODE) >> 3) & 1;
9ae3a8
     clock_sel = (s->msr >> 2) & 3;
9ae3a8
     dots = (s->msr & 1) ? 8 : 9;
9ae3a8
 
9ae3a8
@@ -513,7 +518,6 @@ void vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
9ae3a8
         printf("vga: write SR%x = 0x%02x\n", s->sr_index, val);
9ae3a8
 #endif
9ae3a8
         s->sr[s->sr_index] = val & sr_mask[s->sr_index];
9ae3a8
-        vbe_update_vgaregs(s);
9ae3a8
         if (s->sr_index == VGA_SEQ_CLOCK_MODE) {
9ae3a8
             s->update_retrace_info(s);
9ae3a8
         }
9ae3a8
@@ -707,13 +711,13 @@ static void vbe_update_vgaregs(VGACommonState *s)
9ae3a8
 
9ae3a8
     if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4) {
9ae3a8
         shift_control = 0;
9ae3a8
-        s->sr[VGA_SEQ_CLOCK_MODE] &= ~8; /* no double line */
9ae3a8
+        s->sr_vbe[VGA_SEQ_CLOCK_MODE] &= ~8; /* no double line */
9ae3a8
     } else {
9ae3a8
         shift_control = 2;
9ae3a8
         /* set chain 4 mode */
9ae3a8
-        s->sr[VGA_SEQ_MEMORY_MODE] |= VGA_SR04_CHN_4M;
9ae3a8
+        s->sr_vbe[VGA_SEQ_MEMORY_MODE] |= VGA_SR04_CHN_4M;
9ae3a8
         /* activate all planes */
9ae3a8
-        s->sr[VGA_SEQ_PLANE_WRITE] |= VGA_SR02_ALL_PLANES;
9ae3a8
+        s->sr_vbe[VGA_SEQ_PLANE_WRITE] |= VGA_SR02_ALL_PLANES;
9ae3a8
     }
9ae3a8
     s->gr[VGA_GFX_MODE] = (s->gr[VGA_GFX_MODE] & ~0x60) |
9ae3a8
         (shift_control << 5);
9ae3a8
@@ -863,7 +867,7 @@ uint32_t vga_mem_readb(VGACommonState *s, hwaddr addr)
9ae3a8
         break;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    if (s->sr[VGA_SEQ_MEMORY_MODE] & VGA_SR04_CHN_4M) {
9ae3a8
+    if (sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
9ae3a8
         /* chain 4 mode : simplest access */
9ae3a8
         assert(addr < s->vram_size);
9ae3a8
         ret = s->vram_ptr[addr];
9ae3a8
@@ -931,11 +935,11 @@ void vga_mem_writeb(VGACommonState *s, hwaddr addr, uint32_t val)
9ae3a8
         break;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    if (s->sr[VGA_SEQ_MEMORY_MODE] & VGA_SR04_CHN_4M) {
9ae3a8
+    if (sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
9ae3a8
         /* chain 4 mode : simplest access */
9ae3a8
         plane = addr & 3;
9ae3a8
         mask = (1 << plane);
9ae3a8
-        if (s->sr[VGA_SEQ_PLANE_WRITE] & mask) {
9ae3a8
+        if (sr(s, VGA_SEQ_PLANE_WRITE) & mask) {
9ae3a8
             assert(addr < s->vram_size);
9ae3a8
             s->vram_ptr[addr] = val;
9ae3a8
 #ifdef DEBUG_VGA_MEM
9ae3a8
@@ -948,7 +952,7 @@ void vga_mem_writeb(VGACommonState *s, hwaddr addr, uint32_t val)
9ae3a8
         /* odd/even mode (aka text mode mapping) */
9ae3a8
         plane = (s->gr[VGA_GFX_PLANE_READ] & 2) | (addr & 1);
9ae3a8
         mask = (1 << plane);
9ae3a8
-        if (s->sr[VGA_SEQ_PLANE_WRITE] & mask) {
9ae3a8
+        if (sr(s, VGA_SEQ_PLANE_WRITE) & mask) {
9ae3a8
             addr = ((addr & ~1) << 1) | plane;
9ae3a8
             if (addr >= s->vram_size) {
9ae3a8
                 return;
9ae3a8
@@ -1023,7 +1027,7 @@ void vga_mem_writeb(VGACommonState *s, hwaddr addr, uint32_t val)
9ae3a8
 
9ae3a8
     do_write:
9ae3a8
         /* mask data according to sr[2] */
9ae3a8
-        mask = s->sr[VGA_SEQ_PLANE_WRITE];
9ae3a8
+        mask = sr(s, VGA_SEQ_PLANE_WRITE);
9ae3a8
         s->plane_updated |= mask; /* only used to detect font change */
9ae3a8
         write_mask = mask16[mask];
9ae3a8
         if (addr * sizeof(uint32_t) >= s->vram_size) {
9ae3a8
@@ -1314,10 +1318,10 @@ static void vga_get_text_resolution(VGACommonState *s, int *pwidth, int *pheight
9ae3a8
     /* total width & height */
9ae3a8
     cheight = (s->cr[VGA_CRTC_MAX_SCAN] & 0x1f) + 1;
9ae3a8
     cwidth = 8;
9ae3a8
-    if (!(s->sr[VGA_SEQ_CLOCK_MODE] & VGA_SR01_CHAR_CLK_8DOTS)) {
9ae3a8
+    if (!(sr(s, VGA_SEQ_CLOCK_MODE) & VGA_SR01_CHAR_CLK_8DOTS)) {
9ae3a8
         cwidth = 9;
9ae3a8
     }
9ae3a8
-    if (s->sr[VGA_SEQ_CLOCK_MODE] & 0x08) {
9ae3a8
+    if (sr(s, VGA_SEQ_CLOCK_MODE) & 0x08) {
9ae3a8
         cwidth = 16; /* NOTE: no 18 pixel wide */
9ae3a8
     }
9ae3a8
     width = (s->cr[VGA_CRTC_H_DISP] + 1);
9ae3a8
@@ -1373,7 +1377,7 @@ static void vga_draw_text(VGACommonState *s, int full_update)
9ae3a8
     int64_t now = qemu_get_clock_ms(vm_clock);
9ae3a8
 
9ae3a8
     /* compute font data address (in plane 2) */
9ae3a8
-    v = s->sr[VGA_SEQ_CHARACTER_MAP];
9ae3a8
+    v = sr(s, VGA_SEQ_CHARACTER_MAP);
9ae3a8
     offset = (((v >> 4) & 1) | ((v << 1) & 6)) * 8192 * 4 + 2;
9ae3a8
     if (offset != s->font_offsets[0]) {
9ae3a8
         s->font_offsets[0] = offset;
9ae3a8
@@ -1744,11 +1748,11 @@ static void vga_draw_graphic(VGACommonState *s, int full_update)
9ae3a8
     }
9ae3a8
 
9ae3a8
     if (shift_control == 0) {
9ae3a8
-        if (s->sr[VGA_SEQ_CLOCK_MODE] & 8) {
9ae3a8
+        if (sr(s, VGA_SEQ_CLOCK_MODE) & 8) {
9ae3a8
             disp_width <<= 1;
9ae3a8
         }
9ae3a8
     } else if (shift_control == 1) {
9ae3a8
-        if (s->sr[VGA_SEQ_CLOCK_MODE] & 8) {
9ae3a8
+        if (sr(s, VGA_SEQ_CLOCK_MODE) & 8) {
9ae3a8
             disp_width <<= 1;
9ae3a8
         }
9ae3a8
     }
9ae3a8
@@ -1788,7 +1792,7 @@ static void vga_draw_graphic(VGACommonState *s, int full_update)
9ae3a8
 
9ae3a8
     if (shift_control == 0) {
9ae3a8
         full_update |= update_palette16(s);
9ae3a8
-        if (s->sr[VGA_SEQ_CLOCK_MODE] & 8) {
9ae3a8
+        if (sr(s, VGA_SEQ_CLOCK_MODE) & 8) {
9ae3a8
             v = VGA_DRAW_LINE4D2;
9ae3a8
         } else {
9ae3a8
             v = VGA_DRAW_LINE4;
9ae3a8
@@ -1796,7 +1800,7 @@ static void vga_draw_graphic(VGACommonState *s, int full_update)
9ae3a8
         bits = 4;
9ae3a8
     } else if (shift_control == 1) {
9ae3a8
         full_update |= update_palette16(s);
9ae3a8
-        if (s->sr[VGA_SEQ_CLOCK_MODE] & 8) {
9ae3a8
+        if (sr(s, VGA_SEQ_CLOCK_MODE) & 8) {
9ae3a8
             v = VGA_DRAW_LINE2D2;
9ae3a8
         } else {
9ae3a8
             v = VGA_DRAW_LINE2;
9ae3a8
@@ -1844,7 +1848,7 @@ static void vga_draw_graphic(VGACommonState *s, int full_update)
9ae3a8
 #if 0
9ae3a8
     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",
9ae3a8
            width, height, v, line_offset, s->cr[9], s->cr[VGA_CRTC_MODE],
9ae3a8
-           s->line_compare, s->sr[VGA_SEQ_CLOCK_MODE]);
9ae3a8
+           s->line_compare, sr(s, VGA_SEQ_CLOCK_MODE));
9ae3a8
 #endif
9ae3a8
     addr1 = (s->start_addr * 4);
9ae3a8
     bwidth = (width * bits + 7) / 8;
9ae3a8
@@ -2003,6 +2007,7 @@ void vga_common_reset(VGACommonState *s)
9ae3a8
 {
9ae3a8
     s->sr_index = 0;
9ae3a8
     memset(s->sr, '\0', sizeof(s->sr));
9ae3a8
+    memset(s->sr_vbe, '\0', sizeof(s->sr_vbe));
9ae3a8
     s->gr_index = 0;
9ae3a8
     memset(s->gr, '\0', sizeof(s->gr));
9ae3a8
     s->ar_index = 0;
9ae3a8
@@ -2104,10 +2109,10 @@ static void vga_update_text(void *opaque, console_ch_t *chardata)
9ae3a8
         /* total width & height */
9ae3a8
         cheight = (s->cr[VGA_CRTC_MAX_SCAN] & 0x1f) + 1;
9ae3a8
         cw = 8;
9ae3a8
-        if (!(s->sr[VGA_SEQ_CLOCK_MODE] & VGA_SR01_CHAR_CLK_8DOTS)) {
9ae3a8
+        if (!(sr(s, VGA_SEQ_CLOCK_MODE) & VGA_SR01_CHAR_CLK_8DOTS)) {
9ae3a8
             cw = 9;
9ae3a8
         }
9ae3a8
-        if (s->sr[VGA_SEQ_CLOCK_MODE] & 0x08) {
9ae3a8
+        if (sr(s, VGA_SEQ_CLOCK_MODE) & 0x08) {
9ae3a8
             cw = 16; /* NOTE: no 18 pixel wide */
9ae3a8
         }
9ae3a8
         width = (s->cr[VGA_CRTC_H_DISP] + 1);
9ae3a8
@@ -2273,6 +2278,7 @@ static int vga_common_post_load(void *opaque, int version_id)
9ae3a8
 
9ae3a8
     /* force refresh */
9ae3a8
     s->graphic_mode = -1;
9ae3a8
+    vbe_update_vgaregs(s);
9ae3a8
     return 0;
9ae3a8
 }
9ae3a8
 
9ae3a8
diff --git a/hw/display/vga_int.h b/hw/display/vga_int.h
9ae3a8
index 5a2f466..7c758ac 100644
9ae3a8
--- a/hw/display/vga_int.h
9ae3a8
+++ b/hw/display/vga_int.h
9ae3a8
@@ -98,6 +98,7 @@ typedef struct VGACommonState {
9ae3a8
     MemoryRegion *chain4_alias;
9ae3a8
     uint8_t sr_index;
9ae3a8
     uint8_t sr[256];
9ae3a8
+    uint8_t sr_vbe[256];
9ae3a8
     uint8_t gr_index;
9ae3a8
     uint8_t gr[256];
9ae3a8
     uint8_t ar_index;
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8