These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / seabios / src / fw / shadow.c
1 // Support for enabling/disabling BIOS ram shadowing.
2 //
3 // Copyright (C) 2008-2010  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "config.h" // CONFIG_*
9 #include "dev-q35.h" // PCI_VENDOR_ID_INTEL
10 #include "dev-piix.h" // I440FX_PAM0
11 #include "hw/pci.h" // pci_config_writeb
12 #include "hw/pci_ids.h" // PCI_VENDOR_ID_INTEL
13 #include "hw/pci_regs.h" // PCI_VENDOR_ID
14 #include "malloc.h" // rom_get_last
15 #include "output.h" // dprintf
16 #include "paravirt.h" // runningOnXen
17 #include "string.h" // memset
18 #include "util.h" // make_bios_writable
19 #include "x86.h" // wbinvd
20
21 // On the emulators, the bios at 0xf0000 is also at 0xffff0000
22 #define BIOS_SRC_OFFSET 0xfff00000
23
24 // Enable shadowing and copy bios.
25 static void
26 __make_bios_writable_intel(u16 bdf, u32 pam0)
27 {
28     // Make ram from 0xc0000-0xf0000 writable
29     int clear = 0;
30     int i;
31     for (i=0; i<6; i++) {
32         u32 pam = pam0 + 1 + i;
33         int reg = pci_config_readb(bdf, pam);
34         if (CONFIG_OPTIONROMS_DEPLOYED && (reg & 0x11) != 0x11) {
35             // Need to copy optionroms to work around qemu implementation
36             void *mem = (void*)(BUILD_ROM_START + i * 32*1024);
37             memcpy((void*)BUILD_BIOS_TMP_ADDR, mem, 32*1024);
38             pci_config_writeb(bdf, pam, 0x33);
39             memcpy(mem, (void*)BUILD_BIOS_TMP_ADDR, 32*1024);
40             clear = 1;
41         } else {
42             pci_config_writeb(bdf, pam, 0x33);
43         }
44     }
45     if (clear)
46         memset((void*)BUILD_BIOS_TMP_ADDR, 0, 32*1024);
47
48     // Make ram from 0xf0000-0x100000 writable
49     int reg = pci_config_readb(bdf, pam0);
50     pci_config_writeb(bdf, pam0, 0x30);
51     if (reg & 0x10)
52         // Ram already present.
53         return;
54
55     // Copy bios.
56     memcpy(VSYMBOL(code32flat_start), VSYMBOL(code32flat_start) + BIOS_SRC_OFFSET
57            , SYMBOL(code32flat_end) - SYMBOL(code32flat_start));
58 }
59
60 static void
61 make_bios_writable_intel(u16 bdf, u32 pam0)
62 {
63     int reg = pci_config_readb(bdf, pam0);
64     if (!(reg & 0x10)) {
65         // QEMU doesn't fully implement the piix shadow capabilities -
66         // if ram isn't backing the bios segment when shadowing is
67         // disabled, the code itself won't be in memory.  So, run the
68         // code from the high-memory flash location.
69         u32 pos = (u32)__make_bios_writable_intel + BIOS_SRC_OFFSET;
70         void (*func)(u16 bdf, u32 pam0) = (void*)pos;
71         func(bdf, pam0);
72         return;
73     }
74     // Ram already present - just enable writes
75     __make_bios_writable_intel(bdf, pam0);
76 }
77
78 static void
79 make_bios_readonly_intel(u16 bdf, u32 pam0)
80 {
81     // Flush any pending writes before locking memory.
82     wbinvd();
83
84     // Write protect roms from 0xc0000-0xf0000
85     u32 romlast = BUILD_BIOS_ADDR, rommax = BUILD_BIOS_ADDR;
86     if (CONFIG_WRITABLE_UPPERMEMORY)
87         romlast = rom_get_last();
88     if (CONFIG_MALLOC_UPPERMEMORY)
89         rommax = rom_get_max();
90     int i;
91     for (i=0; i<6; i++) {
92         u32 mem = BUILD_ROM_START + i * 32*1024;
93         u32 pam = pam0 + 1 + i;
94         if (romlast < mem + 16*1024 || rommax < mem + 32*1024) {
95             if (romlast >= mem && rommax >= mem + 16*1024)
96                 pci_config_writeb(bdf, pam, 0x31);
97             break;
98         }
99         pci_config_writeb(bdf, pam, 0x11);
100     }
101
102     // Write protect 0xf0000-0x100000
103     pci_config_writeb(bdf, pam0, 0x10);
104 }
105
106 static int ShadowBDF = -1;
107
108 // Make the 0xc0000-0x100000 area read/writable.
109 void
110 make_bios_writable(void)
111 {
112     if (!CONFIG_QEMU || runningOnXen())
113         return;
114
115     dprintf(3, "enabling shadow ram\n");
116
117     // At this point, statically allocated variables can't be written,
118     // so do this search manually.
119     int bdf;
120     foreachbdf(bdf, 0) {
121         u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
122         u16 vendor = vendev & 0xffff, device = vendev >> 16;
123         if (vendor == PCI_VENDOR_ID_INTEL
124             && device == PCI_DEVICE_ID_INTEL_82441) {
125             make_bios_writable_intel(bdf, I440FX_PAM0);
126             ShadowBDF = bdf;
127             return;
128         }
129         if (vendor == PCI_VENDOR_ID_INTEL
130             && device == PCI_DEVICE_ID_INTEL_Q35_MCH) {
131             make_bios_writable_intel(bdf, Q35_HOST_BRIDGE_PAM0);
132             ShadowBDF = bdf;
133             return;
134         }
135     }
136     dprintf(1, "Unable to unlock ram - bridge not found\n");
137 }
138
139 // Make the BIOS code segment area (0xf0000) read-only.
140 void
141 make_bios_readonly(void)
142 {
143     if (!CONFIG_QEMU || runningOnXen())
144         return;
145     dprintf(3, "locking shadow ram\n");
146
147     if (ShadowBDF < 0) {
148         dprintf(1, "Unable to lock ram - bridge not found\n");
149         return;
150     }
151
152     u16 device = pci_config_readw(ShadowBDF, PCI_DEVICE_ID);
153     if (device == PCI_DEVICE_ID_INTEL_82441)
154         make_bios_readonly_intel(ShadowBDF, I440FX_PAM0);
155     else
156         make_bios_readonly_intel(ShadowBDF, Q35_HOST_BRIDGE_PAM0);
157 }
158
159 void
160 qemu_prep_reset(void)
161 {
162     if (!CONFIG_QEMU || runningOnXen())
163         return;
164     // QEMU doesn't map 0xc0000-0xfffff back to the original rom on a
165     // reset, so do that manually before invoking a hard reset.
166     make_bios_writable();
167     memcpy(VSYMBOL(code32flat_start), VSYMBOL(code32flat_start) + BIOS_SRC_OFFSET
168            , SYMBOL(code32flat_end) - SYMBOL(code32flat_start));
169 }