Add qemu 2.4.0
[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     extern u8 code32flat_start[], code32flat_end[];
57     memcpy(code32flat_start, code32flat_start + BIOS_SRC_OFFSET
58            , code32flat_end - code32flat_start);
59 }
60
61 static void
62 make_bios_writable_intel(u16 bdf, u32 pam0)
63 {
64     int reg = pci_config_readb(bdf, pam0);
65     if (!(reg & 0x10)) {
66         // QEMU doesn't fully implement the piix shadow capabilities -
67         // if ram isn't backing the bios segment when shadowing is
68         // disabled, the code itself wont be in memory.  So, run the
69         // code from the high-memory flash location.
70         u32 pos = (u32)__make_bios_writable_intel + BIOS_SRC_OFFSET;
71         void (*func)(u16 bdf, u32 pam0) = (void*)pos;
72         func(bdf, pam0);
73         return;
74     }
75     // Ram already present - just enable writes
76     __make_bios_writable_intel(bdf, pam0);
77 }
78
79 static void
80 make_bios_readonly_intel(u16 bdf, u32 pam0)
81 {
82     // Flush any pending writes before locking memory.
83     wbinvd();
84
85     // Write protect roms from 0xc0000-0xf0000
86     u32 romlast = BUILD_BIOS_ADDR, rommax = BUILD_BIOS_ADDR;
87     if (CONFIG_WRITABLE_UPPERMEMORY)
88         romlast = rom_get_last();
89     if (CONFIG_MALLOC_UPPERMEMORY)
90         rommax = rom_get_max();
91     int i;
92     for (i=0; i<6; i++) {
93         u32 mem = BUILD_ROM_START + i * 32*1024;
94         u32 pam = pam0 + 1 + i;
95         if (romlast < mem + 16*1024 || rommax < mem + 32*1024) {
96             if (romlast >= mem && rommax >= mem + 16*1024)
97                 pci_config_writeb(bdf, pam, 0x31);
98             break;
99         }
100         pci_config_writeb(bdf, pam, 0x11);
101     }
102
103     // Write protect 0xf0000-0x100000
104     pci_config_writeb(bdf, pam0, 0x10);
105 }
106
107 static int ShadowBDF = -1;
108
109 // Make the 0xc0000-0x100000 area read/writable.
110 void
111 make_bios_writable(void)
112 {
113     if (!CONFIG_QEMU || runningOnXen())
114         return;
115
116     dprintf(3, "enabling shadow ram\n");
117
118     // At this point, statically allocated variables can't be written,
119     // so do this search manually.
120     int bdf;
121     foreachbdf(bdf, 0) {
122         u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
123         u16 vendor = vendev & 0xffff, device = vendev >> 16;
124         if (vendor == PCI_VENDOR_ID_INTEL
125             && device == PCI_DEVICE_ID_INTEL_82441) {
126             make_bios_writable_intel(bdf, I440FX_PAM0);
127             ShadowBDF = bdf;
128             return;
129         }
130         if (vendor == PCI_VENDOR_ID_INTEL
131             && device == PCI_DEVICE_ID_INTEL_Q35_MCH) {
132             make_bios_writable_intel(bdf, Q35_HOST_BRIDGE_PAM0);
133             ShadowBDF = bdf;
134             return;
135         }
136     }
137     dprintf(1, "Unable to unlock ram - bridge not found\n");
138 }
139
140 // Make the BIOS code segment area (0xf0000) read-only.
141 void
142 make_bios_readonly(void)
143 {
144     if (!CONFIG_QEMU || runningOnXen())
145         return;
146     dprintf(3, "locking shadow ram\n");
147
148     if (ShadowBDF < 0) {
149         dprintf(1, "Unable to lock ram - bridge not found\n");
150         return;
151     }
152
153     u16 device = pci_config_readw(ShadowBDF, PCI_DEVICE_ID);
154     if (device == PCI_DEVICE_ID_INTEL_82441)
155         make_bios_readonly_intel(ShadowBDF, I440FX_PAM0);
156     else
157         make_bios_readonly_intel(ShadowBDF, Q35_HOST_BRIDGE_PAM0);
158 }
159
160 void
161 qemu_prep_reset(void)
162 {
163     if (!CONFIG_QEMU || runningOnXen())
164         return;
165     // QEMU doesn't map 0xc0000-0xfffff back to the original rom on a
166     // reset, so do that manually before invoking a hard reset.
167     make_bios_writable();
168     extern u8 code32flat_start[], code32flat_end[];
169     memcpy(code32flat_start, code32flat_start + BIOS_SRC_OFFSET
170            , code32flat_end - code32flat_start);
171 }