root / hw / pc.c @ bf0cb498
History | View | Annotate | Download (44.6 kB)
1 | 80cabfad | bellard | /*
|
---|---|---|---|
2 | 80cabfad | bellard | * QEMU PC System Emulator
|
3 | 5fafdf24 | ths | *
|
4 | 80cabfad | bellard | * Copyright (c) 2003-2004 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 80cabfad | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 80cabfad | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 80cabfad | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 80cabfad | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 80cabfad | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 80cabfad | bellard | * furnished to do so, subject to the following conditions:
|
12 | 80cabfad | bellard | *
|
13 | 80cabfad | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 80cabfad | bellard | * all copies or substantial portions of the Software.
|
15 | 80cabfad | bellard | *
|
16 | 80cabfad | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 80cabfad | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 80cabfad | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 80cabfad | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 80cabfad | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 80cabfad | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 80cabfad | bellard | * THE SOFTWARE.
|
23 | 80cabfad | bellard | */
|
24 | 87ecb68b | pbrook | #include "hw.h" |
25 | 87ecb68b | pbrook | #include "pc.h" |
26 | 87ecb68b | pbrook | #include "fdc.h" |
27 | 87ecb68b | pbrook | #include "pci.h" |
28 | 87ecb68b | pbrook | #include "block.h" |
29 | 87ecb68b | pbrook | #include "sysemu.h" |
30 | 87ecb68b | pbrook | #include "audio/audio.h" |
31 | 87ecb68b | pbrook | #include "net.h" |
32 | 87ecb68b | pbrook | #include "smbus.h" |
33 | 87ecb68b | pbrook | #include "boards.h" |
34 | 376253ec | aliguori | #include "monitor.h" |
35 | 3cce6243 | blueswir1 | #include "fw_cfg.h" |
36 | 16b29ae1 | aliguori | #include "hpet_emul.h" |
37 | 9dd986cc | Richard W.M. Jones | #include "watchdog.h" |
38 | b6f6e3d3 | aliguori | #include "smbios.h" |
39 | 80cabfad | bellard | |
40 | b41a2cd1 | bellard | /* output Bochs bios info messages */
|
41 | b41a2cd1 | bellard | //#define DEBUG_BIOS
|
42 | b41a2cd1 | bellard | |
43 | f16408df | Alexander Graf | /* Show multiboot debug output */
|
44 | f16408df | Alexander Graf | //#define DEBUG_MULTIBOOT
|
45 | f16408df | Alexander Graf | |
46 | 80cabfad | bellard | #define BIOS_FILENAME "bios.bin" |
47 | 80cabfad | bellard | #define VGABIOS_FILENAME "vgabios.bin" |
48 | de9258a8 | bellard | #define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin" |
49 | 80cabfad | bellard | |
50 | 7fb4fdcf | balrog | #define PC_MAX_BIOS_SIZE (4 * 1024 * 1024) |
51 | 7fb4fdcf | balrog | |
52 | a80274c3 | pbrook | /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables. */
|
53 | a80274c3 | pbrook | #define ACPI_DATA_SIZE 0x10000 |
54 | 3cce6243 | blueswir1 | #define BIOS_CFG_IOPORT 0x510 |
55 | 8a92ea2f | aliguori | #define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0) |
56 | b6f6e3d3 | aliguori | #define FW_CFG_SMBIOS_ENTRIES (FW_CFG_ARCH_LOCAL + 1) |
57 | 6b35e7bf | Jes Sorensen | #define FW_CFG_IRQ0_OVERRIDE (FW_CFG_ARCH_LOCAL + 2) |
58 | 80cabfad | bellard | |
59 | e4bcb14c | ths | #define MAX_IDE_BUS 2 |
60 | e4bcb14c | ths | |
61 | baca51fa | bellard | static fdctrl_t *floppy_controller;
|
62 | b0a21b53 | bellard | static RTCState *rtc_state;
|
63 | ec844b96 | bellard | static PITState *pit;
|
64 | a5954d5c | bellard | static PCIDevice *i440fx_state;
|
65 | 80cabfad | bellard | |
66 | e28f9884 | Glauber Costa | typedef struct rom_reset_data { |
67 | e28f9884 | Glauber Costa | uint8_t *data; |
68 | e28f9884 | Glauber Costa | target_phys_addr_t addr; |
69 | e28f9884 | Glauber Costa | unsigned size;
|
70 | e28f9884 | Glauber Costa | } RomResetData; |
71 | e28f9884 | Glauber Costa | |
72 | e28f9884 | Glauber Costa | static void option_rom_reset(void *_rrd) |
73 | e28f9884 | Glauber Costa | { |
74 | e28f9884 | Glauber Costa | RomResetData *rrd = _rrd; |
75 | e28f9884 | Glauber Costa | |
76 | e28f9884 | Glauber Costa | cpu_physical_memory_write_rom(rrd->addr, rrd->data, rrd->size); |
77 | e28f9884 | Glauber Costa | } |
78 | e28f9884 | Glauber Costa | |
79 | e28f9884 | Glauber Costa | static void option_rom_setup_reset(target_phys_addr_t addr, unsigned size) |
80 | e28f9884 | Glauber Costa | { |
81 | e28f9884 | Glauber Costa | RomResetData *rrd = qemu_malloc(sizeof *rrd);
|
82 | e28f9884 | Glauber Costa | |
83 | e28f9884 | Glauber Costa | rrd->data = qemu_malloc(size); |
84 | e28f9884 | Glauber Costa | cpu_physical_memory_read(addr, rrd->data, size); |
85 | e28f9884 | Glauber Costa | rrd->addr = addr; |
86 | e28f9884 | Glauber Costa | rrd->size = size; |
87 | a08d4367 | Jan Kiszka | qemu_register_reset(option_rom_reset, rrd); |
88 | e28f9884 | Glauber Costa | } |
89 | e28f9884 | Glauber Costa | |
90 | 1452411b | Avi Kivity | typedef struct isa_irq_state { |
91 | 1452411b | Avi Kivity | qemu_irq *i8259; |
92 | 1632dc6a | Avi Kivity | qemu_irq *ioapic; |
93 | 1452411b | Avi Kivity | } IsaIrqState; |
94 | 1452411b | Avi Kivity | |
95 | 1452411b | Avi Kivity | static void isa_irq_handler(void *opaque, int n, int level) |
96 | 1452411b | Avi Kivity | { |
97 | 1452411b | Avi Kivity | IsaIrqState *isa = (IsaIrqState *)opaque; |
98 | 1452411b | Avi Kivity | |
99 | 1632dc6a | Avi Kivity | if (n < 16) { |
100 | 1632dc6a | Avi Kivity | qemu_set_irq(isa->i8259[n], level); |
101 | 1632dc6a | Avi Kivity | } |
102 | 1632dc6a | Avi Kivity | qemu_set_irq(isa->ioapic[n], level); |
103 | 1632dc6a | Avi Kivity | }; |
104 | 1452411b | Avi Kivity | |
105 | b41a2cd1 | bellard | static void ioport80_write(void *opaque, uint32_t addr, uint32_t data) |
106 | 80cabfad | bellard | { |
107 | 80cabfad | bellard | } |
108 | 80cabfad | bellard | |
109 | f929aad6 | bellard | /* MSDOS compatibility mode FPU exception support */
|
110 | d537cf6c | pbrook | static qemu_irq ferr_irq;
|
111 | f929aad6 | bellard | /* XXX: add IGNNE support */
|
112 | f929aad6 | bellard | void cpu_set_ferr(CPUX86State *s)
|
113 | f929aad6 | bellard | { |
114 | d537cf6c | pbrook | qemu_irq_raise(ferr_irq); |
115 | f929aad6 | bellard | } |
116 | f929aad6 | bellard | |
117 | f929aad6 | bellard | static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data) |
118 | f929aad6 | bellard | { |
119 | d537cf6c | pbrook | qemu_irq_lower(ferr_irq); |
120 | f929aad6 | bellard | } |
121 | f929aad6 | bellard | |
122 | 28ab0e2e | bellard | /* TSC handling */
|
123 | 28ab0e2e | bellard | uint64_t cpu_get_tsc(CPUX86State *env) |
124 | 28ab0e2e | bellard | { |
125 | 4a1418e0 | Anthony Liguori | return cpu_get_ticks();
|
126 | 28ab0e2e | bellard | } |
127 | 28ab0e2e | bellard | |
128 | a5954d5c | bellard | /* SMM support */
|
129 | a5954d5c | bellard | void cpu_smm_update(CPUState *env)
|
130 | a5954d5c | bellard | { |
131 | a5954d5c | bellard | if (i440fx_state && env == first_cpu)
|
132 | a5954d5c | bellard | i440fx_set_smm(i440fx_state, (env->hflags >> HF_SMM_SHIFT) & 1);
|
133 | a5954d5c | bellard | } |
134 | a5954d5c | bellard | |
135 | a5954d5c | bellard | |
136 | 3de388f6 | bellard | /* IRQ handling */
|
137 | 3de388f6 | bellard | int cpu_get_pic_interrupt(CPUState *env)
|
138 | 3de388f6 | bellard | { |
139 | 3de388f6 | bellard | int intno;
|
140 | 3de388f6 | bellard | |
141 | 3de388f6 | bellard | intno = apic_get_interrupt(env); |
142 | 3de388f6 | bellard | if (intno >= 0) { |
143 | 3de388f6 | bellard | /* set irq request if a PIC irq is still pending */
|
144 | 3de388f6 | bellard | /* XXX: improve that */
|
145 | 5fafdf24 | ths | pic_update_irq(isa_pic); |
146 | 3de388f6 | bellard | return intno;
|
147 | 3de388f6 | bellard | } |
148 | 3de388f6 | bellard | /* read the irq from the PIC */
|
149 | 0e21e12b | ths | if (!apic_accept_pic_intr(env))
|
150 | 0e21e12b | ths | return -1; |
151 | 0e21e12b | ths | |
152 | 3de388f6 | bellard | intno = pic_read_irq(isa_pic); |
153 | 3de388f6 | bellard | return intno;
|
154 | 3de388f6 | bellard | } |
155 | 3de388f6 | bellard | |
156 | d537cf6c | pbrook | static void pic_irq_request(void *opaque, int irq, int level) |
157 | 3de388f6 | bellard | { |
158 | a5b38b51 | aurel32 | CPUState *env = first_cpu; |
159 | a5b38b51 | aurel32 | |
160 | d5529471 | aurel32 | if (env->apic_state) {
|
161 | d5529471 | aurel32 | while (env) {
|
162 | d5529471 | aurel32 | if (apic_accept_pic_intr(env))
|
163 | 1a7de94a | aurel32 | apic_deliver_pic_intr(env, level); |
164 | d5529471 | aurel32 | env = env->next_cpu; |
165 | d5529471 | aurel32 | } |
166 | d5529471 | aurel32 | } else {
|
167 | b614106a | aurel32 | if (level)
|
168 | b614106a | aurel32 | cpu_interrupt(env, CPU_INTERRUPT_HARD); |
169 | b614106a | aurel32 | else
|
170 | b614106a | aurel32 | cpu_reset_interrupt(env, CPU_INTERRUPT_HARD); |
171 | a5b38b51 | aurel32 | } |
172 | 3de388f6 | bellard | } |
173 | 3de388f6 | bellard | |
174 | b0a21b53 | bellard | /* PC cmos mappings */
|
175 | b0a21b53 | bellard | |
176 | 80cabfad | bellard | #define REG_EQUIPMENT_BYTE 0x14 |
177 | 80cabfad | bellard | |
178 | 777428f2 | bellard | static int cmos_get_fd_drive_type(int fd0) |
179 | 777428f2 | bellard | { |
180 | 777428f2 | bellard | int val;
|
181 | 777428f2 | bellard | |
182 | 777428f2 | bellard | switch (fd0) {
|
183 | 777428f2 | bellard | case 0: |
184 | 777428f2 | bellard | /* 1.44 Mb 3"5 drive */
|
185 | 777428f2 | bellard | val = 4;
|
186 | 777428f2 | bellard | break;
|
187 | 777428f2 | bellard | case 1: |
188 | 777428f2 | bellard | /* 2.88 Mb 3"5 drive */
|
189 | 777428f2 | bellard | val = 5;
|
190 | 777428f2 | bellard | break;
|
191 | 777428f2 | bellard | case 2: |
192 | 777428f2 | bellard | /* 1.2 Mb 5"5 drive */
|
193 | 777428f2 | bellard | val = 2;
|
194 | 777428f2 | bellard | break;
|
195 | 777428f2 | bellard | default:
|
196 | 777428f2 | bellard | val = 0;
|
197 | 777428f2 | bellard | break;
|
198 | 777428f2 | bellard | } |
199 | 777428f2 | bellard | return val;
|
200 | 777428f2 | bellard | } |
201 | 777428f2 | bellard | |
202 | 5fafdf24 | ths | static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd) |
203 | ba6c2377 | bellard | { |
204 | ba6c2377 | bellard | RTCState *s = rtc_state; |
205 | ba6c2377 | bellard | int cylinders, heads, sectors;
|
206 | ba6c2377 | bellard | bdrv_get_geometry_hint(hd, &cylinders, &heads, §ors); |
207 | ba6c2377 | bellard | rtc_set_memory(s, type_ofs, 47);
|
208 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs, cylinders); |
209 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 1, cylinders >> 8); |
210 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 2, heads);
|
211 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 3, 0xff); |
212 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 4, 0xff); |
213 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3)); |
214 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 6, cylinders);
|
215 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 7, cylinders >> 8); |
216 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 8, sectors);
|
217 | ba6c2377 | bellard | } |
218 | ba6c2377 | bellard | |
219 | 6ac0e82d | balrog | /* convert boot_device letter to something recognizable by the bios */
|
220 | 6ac0e82d | balrog | static int boot_device2nibble(char boot_device) |
221 | 6ac0e82d | balrog | { |
222 | 6ac0e82d | balrog | switch(boot_device) {
|
223 | 6ac0e82d | balrog | case 'a': |
224 | 6ac0e82d | balrog | case 'b': |
225 | 6ac0e82d | balrog | return 0x01; /* floppy boot */ |
226 | 6ac0e82d | balrog | case 'c': |
227 | 6ac0e82d | balrog | return 0x02; /* hard drive boot */ |
228 | 6ac0e82d | balrog | case 'd': |
229 | 6ac0e82d | balrog | return 0x03; /* CD-ROM boot */ |
230 | 6ac0e82d | balrog | case 'n': |
231 | 6ac0e82d | balrog | return 0x04; /* Network boot */ |
232 | 6ac0e82d | balrog | } |
233 | 6ac0e82d | balrog | return 0; |
234 | 6ac0e82d | balrog | } |
235 | 6ac0e82d | balrog | |
236 | 0ecdffbb | aurel32 | /* copy/pasted from cmos_init, should be made a general function
|
237 | 0ecdffbb | aurel32 | and used there as well */
|
238 | 3b4366de | blueswir1 | static int pc_boot_set(void *opaque, const char *boot_device) |
239 | 0ecdffbb | aurel32 | { |
240 | 376253ec | aliguori | Monitor *mon = cur_mon; |
241 | 0ecdffbb | aurel32 | #define PC_MAX_BOOT_DEVICES 3 |
242 | 3b4366de | blueswir1 | RTCState *s = (RTCState *)opaque; |
243 | 0ecdffbb | aurel32 | int nbds, bds[3] = { 0, }; |
244 | 0ecdffbb | aurel32 | int i;
|
245 | 0ecdffbb | aurel32 | |
246 | 0ecdffbb | aurel32 | nbds = strlen(boot_device); |
247 | 0ecdffbb | aurel32 | if (nbds > PC_MAX_BOOT_DEVICES) {
|
248 | 376253ec | aliguori | monitor_printf(mon, "Too many boot devices for PC\n");
|
249 | 0ecdffbb | aurel32 | return(1); |
250 | 0ecdffbb | aurel32 | } |
251 | 0ecdffbb | aurel32 | for (i = 0; i < nbds; i++) { |
252 | 0ecdffbb | aurel32 | bds[i] = boot_device2nibble(boot_device[i]); |
253 | 0ecdffbb | aurel32 | if (bds[i] == 0) { |
254 | 376253ec | aliguori | monitor_printf(mon, "Invalid boot device for PC: '%c'\n",
|
255 | 376253ec | aliguori | boot_device[i]); |
256 | 0ecdffbb | aurel32 | return(1); |
257 | 0ecdffbb | aurel32 | } |
258 | 0ecdffbb | aurel32 | } |
259 | 0ecdffbb | aurel32 | rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]); |
260 | 0ecdffbb | aurel32 | rtc_set_memory(s, 0x38, (bds[2] << 4)); |
261 | 0ecdffbb | aurel32 | return(0); |
262 | 0ecdffbb | aurel32 | } |
263 | 0ecdffbb | aurel32 | |
264 | ba6c2377 | bellard | /* hd_table must contain 4 block drivers */
|
265 | 00f82b8a | aurel32 | static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size, |
266 | 00f82b8a | aurel32 | const char *boot_device, BlockDriverState **hd_table) |
267 | 80cabfad | bellard | { |
268 | b0a21b53 | bellard | RTCState *s = rtc_state; |
269 | 28c5af54 | j_mayer | int nbds, bds[3] = { 0, }; |
270 | 80cabfad | bellard | int val;
|
271 | b41a2cd1 | bellard | int fd0, fd1, nb;
|
272 | ba6c2377 | bellard | int i;
|
273 | b0a21b53 | bellard | |
274 | b0a21b53 | bellard | /* various important CMOS locations needed by PC/Bochs bios */
|
275 | 80cabfad | bellard | |
276 | 80cabfad | bellard | /* memory size */
|
277 | 333190eb | bellard | val = 640; /* base memory in K */ |
278 | 333190eb | bellard | rtc_set_memory(s, 0x15, val);
|
279 | 333190eb | bellard | rtc_set_memory(s, 0x16, val >> 8); |
280 | 333190eb | bellard | |
281 | 80cabfad | bellard | val = (ram_size / 1024) - 1024; |
282 | 80cabfad | bellard | if (val > 65535) |
283 | 80cabfad | bellard | val = 65535;
|
284 | b0a21b53 | bellard | rtc_set_memory(s, 0x17, val);
|
285 | b0a21b53 | bellard | rtc_set_memory(s, 0x18, val >> 8); |
286 | b0a21b53 | bellard | rtc_set_memory(s, 0x30, val);
|
287 | b0a21b53 | bellard | rtc_set_memory(s, 0x31, val >> 8); |
288 | 80cabfad | bellard | |
289 | 00f82b8a | aurel32 | if (above_4g_mem_size) {
|
290 | 00f82b8a | aurel32 | rtc_set_memory(s, 0x5b, (unsigned int)above_4g_mem_size >> 16); |
291 | 00f82b8a | aurel32 | rtc_set_memory(s, 0x5c, (unsigned int)above_4g_mem_size >> 24); |
292 | 00f82b8a | aurel32 | rtc_set_memory(s, 0x5d, (uint64_t)above_4g_mem_size >> 32); |
293 | 00f82b8a | aurel32 | } |
294 | 00f82b8a | aurel32 | |
295 | 9da98861 | bellard | if (ram_size > (16 * 1024 * 1024)) |
296 | 9da98861 | bellard | val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536); |
297 | 9da98861 | bellard | else
|
298 | 9da98861 | bellard | val = 0;
|
299 | 80cabfad | bellard | if (val > 65535) |
300 | 80cabfad | bellard | val = 65535;
|
301 | b0a21b53 | bellard | rtc_set_memory(s, 0x34, val);
|
302 | b0a21b53 | bellard | rtc_set_memory(s, 0x35, val >> 8); |
303 | 3b46e624 | ths | |
304 | 298e01b6 | aurel32 | /* set the number of CPU */
|
305 | 298e01b6 | aurel32 | rtc_set_memory(s, 0x5f, smp_cpus - 1); |
306 | 298e01b6 | aurel32 | |
307 | 6ac0e82d | balrog | /* set boot devices, and disable floppy signature check if requested */
|
308 | 28c5af54 | j_mayer | #define PC_MAX_BOOT_DEVICES 3 |
309 | 28c5af54 | j_mayer | nbds = strlen(boot_device); |
310 | 28c5af54 | j_mayer | if (nbds > PC_MAX_BOOT_DEVICES) {
|
311 | 28c5af54 | j_mayer | fprintf(stderr, "Too many boot devices for PC\n");
|
312 | 28c5af54 | j_mayer | exit(1);
|
313 | 28c5af54 | j_mayer | } |
314 | 28c5af54 | j_mayer | for (i = 0; i < nbds; i++) { |
315 | 28c5af54 | j_mayer | bds[i] = boot_device2nibble(boot_device[i]); |
316 | 28c5af54 | j_mayer | if (bds[i] == 0) { |
317 | 28c5af54 | j_mayer | fprintf(stderr, "Invalid boot device for PC: '%c'\n",
|
318 | 28c5af54 | j_mayer | boot_device[i]); |
319 | 28c5af54 | j_mayer | exit(1);
|
320 | 28c5af54 | j_mayer | } |
321 | 28c5af54 | j_mayer | } |
322 | 28c5af54 | j_mayer | rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]); |
323 | 28c5af54 | j_mayer | rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1)); |
324 | 80cabfad | bellard | |
325 | b41a2cd1 | bellard | /* floppy type */
|
326 | b41a2cd1 | bellard | |
327 | baca51fa | bellard | fd0 = fdctrl_get_drive_type(floppy_controller, 0);
|
328 | baca51fa | bellard | fd1 = fdctrl_get_drive_type(floppy_controller, 1);
|
329 | 80cabfad | bellard | |
330 | 777428f2 | bellard | val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
|
331 | b0a21b53 | bellard | rtc_set_memory(s, 0x10, val);
|
332 | 3b46e624 | ths | |
333 | b0a21b53 | bellard | val = 0;
|
334 | b41a2cd1 | bellard | nb = 0;
|
335 | 80cabfad | bellard | if (fd0 < 3) |
336 | 80cabfad | bellard | nb++; |
337 | 80cabfad | bellard | if (fd1 < 3) |
338 | 80cabfad | bellard | nb++; |
339 | 80cabfad | bellard | switch (nb) {
|
340 | 80cabfad | bellard | case 0: |
341 | 80cabfad | bellard | break;
|
342 | 80cabfad | bellard | case 1: |
343 | b0a21b53 | bellard | val |= 0x01; /* 1 drive, ready for boot */ |
344 | 80cabfad | bellard | break;
|
345 | 80cabfad | bellard | case 2: |
346 | b0a21b53 | bellard | val |= 0x41; /* 2 drives, ready for boot */ |
347 | 80cabfad | bellard | break;
|
348 | 80cabfad | bellard | } |
349 | b0a21b53 | bellard | val |= 0x02; /* FPU is there */ |
350 | b0a21b53 | bellard | val |= 0x04; /* PS/2 mouse installed */ |
351 | b0a21b53 | bellard | rtc_set_memory(s, REG_EQUIPMENT_BYTE, val); |
352 | b0a21b53 | bellard | |
353 | ba6c2377 | bellard | /* hard drives */
|
354 | ba6c2377 | bellard | |
355 | ba6c2377 | bellard | rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0)); |
356 | ba6c2377 | bellard | if (hd_table[0]) |
357 | ba6c2377 | bellard | cmos_init_hd(0x19, 0x1b, hd_table[0]); |
358 | 5fafdf24 | ths | if (hd_table[1]) |
359 | ba6c2377 | bellard | cmos_init_hd(0x1a, 0x24, hd_table[1]); |
360 | ba6c2377 | bellard | |
361 | ba6c2377 | bellard | val = 0;
|
362 | 40b6ecc6 | bellard | for (i = 0; i < 4; i++) { |
363 | ba6c2377 | bellard | if (hd_table[i]) {
|
364 | 46d4767d | bellard | int cylinders, heads, sectors, translation;
|
365 | 46d4767d | bellard | /* NOTE: bdrv_get_geometry_hint() returns the physical
|
366 | 46d4767d | bellard | geometry. It is always such that: 1 <= sects <= 63, 1
|
367 | 46d4767d | bellard | <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
|
368 | 46d4767d | bellard | geometry can be different if a translation is done. */
|
369 | 46d4767d | bellard | translation = bdrv_get_translation_hint(hd_table[i]); |
370 | 46d4767d | bellard | if (translation == BIOS_ATA_TRANSLATION_AUTO) {
|
371 | 46d4767d | bellard | bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, §ors); |
372 | 46d4767d | bellard | if (cylinders <= 1024 && heads <= 16 && sectors <= 63) { |
373 | 46d4767d | bellard | /* No translation. */
|
374 | 46d4767d | bellard | translation = 0;
|
375 | 46d4767d | bellard | } else {
|
376 | 46d4767d | bellard | /* LBA translation. */
|
377 | 46d4767d | bellard | translation = 1;
|
378 | 46d4767d | bellard | } |
379 | 40b6ecc6 | bellard | } else {
|
380 | 46d4767d | bellard | translation--; |
381 | ba6c2377 | bellard | } |
382 | ba6c2377 | bellard | val |= translation << (i * 2);
|
383 | ba6c2377 | bellard | } |
384 | 40b6ecc6 | bellard | } |
385 | ba6c2377 | bellard | rtc_set_memory(s, 0x39, val);
|
386 | 80cabfad | bellard | } |
387 | 80cabfad | bellard | |
388 | 59b8ad81 | bellard | void ioport_set_a20(int enable) |
389 | 59b8ad81 | bellard | { |
390 | 59b8ad81 | bellard | /* XXX: send to all CPUs ? */
|
391 | 59b8ad81 | bellard | cpu_x86_set_a20(first_cpu, enable); |
392 | 59b8ad81 | bellard | } |
393 | 59b8ad81 | bellard | |
394 | 59b8ad81 | bellard | int ioport_get_a20(void) |
395 | 59b8ad81 | bellard | { |
396 | 59b8ad81 | bellard | return ((first_cpu->a20_mask >> 20) & 1); |
397 | 59b8ad81 | bellard | } |
398 | 59b8ad81 | bellard | |
399 | e1a23744 | bellard | static void ioport92_write(void *opaque, uint32_t addr, uint32_t val) |
400 | e1a23744 | bellard | { |
401 | 59b8ad81 | bellard | ioport_set_a20((val >> 1) & 1); |
402 | e1a23744 | bellard | /* XXX: bit 0 is fast reset */
|
403 | e1a23744 | bellard | } |
404 | e1a23744 | bellard | |
405 | e1a23744 | bellard | static uint32_t ioport92_read(void *opaque, uint32_t addr) |
406 | e1a23744 | bellard | { |
407 | 59b8ad81 | bellard | return ioport_get_a20() << 1; |
408 | e1a23744 | bellard | } |
409 | e1a23744 | bellard | |
410 | 80cabfad | bellard | /***********************************************************/
|
411 | 80cabfad | bellard | /* Bochs BIOS debug ports */
|
412 | 80cabfad | bellard | |
413 | 9596ebb7 | pbrook | static void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val) |
414 | 80cabfad | bellard | { |
415 | a2f659ee | bellard | static const char shutdown_str[8] = "Shutdown"; |
416 | a2f659ee | bellard | static int shutdown_index = 0; |
417 | 3b46e624 | ths | |
418 | 80cabfad | bellard | switch(addr) {
|
419 | 80cabfad | bellard | /* Bochs BIOS messages */
|
420 | 80cabfad | bellard | case 0x400: |
421 | 80cabfad | bellard | case 0x401: |
422 | 80cabfad | bellard | fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
|
423 | 80cabfad | bellard | exit(1);
|
424 | 80cabfad | bellard | case 0x402: |
425 | 80cabfad | bellard | case 0x403: |
426 | 80cabfad | bellard | #ifdef DEBUG_BIOS
|
427 | 80cabfad | bellard | fprintf(stderr, "%c", val);
|
428 | 80cabfad | bellard | #endif
|
429 | 80cabfad | bellard | break;
|
430 | a2f659ee | bellard | case 0x8900: |
431 | a2f659ee | bellard | /* same as Bochs power off */
|
432 | a2f659ee | bellard | if (val == shutdown_str[shutdown_index]) {
|
433 | a2f659ee | bellard | shutdown_index++; |
434 | a2f659ee | bellard | if (shutdown_index == 8) { |
435 | a2f659ee | bellard | shutdown_index = 0;
|
436 | a2f659ee | bellard | qemu_system_shutdown_request(); |
437 | a2f659ee | bellard | } |
438 | a2f659ee | bellard | } else {
|
439 | a2f659ee | bellard | shutdown_index = 0;
|
440 | a2f659ee | bellard | } |
441 | a2f659ee | bellard | break;
|
442 | 80cabfad | bellard | |
443 | 80cabfad | bellard | /* LGPL'ed VGA BIOS messages */
|
444 | 80cabfad | bellard | case 0x501: |
445 | 80cabfad | bellard | case 0x502: |
446 | 80cabfad | bellard | fprintf(stderr, "VGA BIOS panic, line %d\n", val);
|
447 | 80cabfad | bellard | exit(1);
|
448 | 80cabfad | bellard | case 0x500: |
449 | 80cabfad | bellard | case 0x503: |
450 | 80cabfad | bellard | #ifdef DEBUG_BIOS
|
451 | 80cabfad | bellard | fprintf(stderr, "%c", val);
|
452 | 80cabfad | bellard | #endif
|
453 | 80cabfad | bellard | break;
|
454 | 80cabfad | bellard | } |
455 | 80cabfad | bellard | } |
456 | 80cabfad | bellard | |
457 | 11c2fd3e | aliguori | extern uint64_t node_cpumask[MAX_NODES];
|
458 | 11c2fd3e | aliguori | |
459 | bf483392 | Alexander Graf | static void *bochs_bios_init(void) |
460 | 80cabfad | bellard | { |
461 | 3cce6243 | blueswir1 | void *fw_cfg;
|
462 | b6f6e3d3 | aliguori | uint8_t *smbios_table; |
463 | b6f6e3d3 | aliguori | size_t smbios_len; |
464 | 11c2fd3e | aliguori | uint64_t *numa_fw_cfg; |
465 | 11c2fd3e | aliguori | int i, j;
|
466 | 3cce6243 | blueswir1 | |
467 | b41a2cd1 | bellard | register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL); |
468 | b41a2cd1 | bellard | register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL); |
469 | b41a2cd1 | bellard | register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL); |
470 | b41a2cd1 | bellard | register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL); |
471 | a2f659ee | bellard | register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL); |
472 | b41a2cd1 | bellard | |
473 | b41a2cd1 | bellard | register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL); |
474 | b41a2cd1 | bellard | register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL); |
475 | b41a2cd1 | bellard | register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL); |
476 | b41a2cd1 | bellard | register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL); |
477 | 3cce6243 | blueswir1 | |
478 | 3cce6243 | blueswir1 | fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0); |
479 | bf483392 | Alexander Graf | |
480 | 3cce6243 | blueswir1 | fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
|
481 | 905fdcb5 | blueswir1 | fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size); |
482 | 80deece2 | blueswir1 | fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES, (uint8_t *)acpi_tables, |
483 | 80deece2 | blueswir1 | acpi_tables_len); |
484 | 6b35e7bf | Jes Sorensen | fw_cfg_add_bytes(fw_cfg, FW_CFG_IRQ0_OVERRIDE, &irq0override, 1);
|
485 | b6f6e3d3 | aliguori | |
486 | b6f6e3d3 | aliguori | smbios_table = smbios_get_table(&smbios_len); |
487 | b6f6e3d3 | aliguori | if (smbios_table)
|
488 | b6f6e3d3 | aliguori | fw_cfg_add_bytes(fw_cfg, FW_CFG_SMBIOS_ENTRIES, |
489 | b6f6e3d3 | aliguori | smbios_table, smbios_len); |
490 | 11c2fd3e | aliguori | |
491 | 11c2fd3e | aliguori | /* allocate memory for the NUMA channel: one (64bit) word for the number
|
492 | 11c2fd3e | aliguori | * of nodes, one word for each VCPU->node and one word for each node to
|
493 | 11c2fd3e | aliguori | * hold the amount of memory.
|
494 | 11c2fd3e | aliguori | */
|
495 | 11c2fd3e | aliguori | numa_fw_cfg = qemu_mallocz((1 + smp_cpus + nb_numa_nodes) * 8); |
496 | 11c2fd3e | aliguori | numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes);
|
497 | 11c2fd3e | aliguori | for (i = 0; i < smp_cpus; i++) { |
498 | 11c2fd3e | aliguori | for (j = 0; j < nb_numa_nodes; j++) { |
499 | 11c2fd3e | aliguori | if (node_cpumask[j] & (1 << i)) { |
500 | 11c2fd3e | aliguori | numa_fw_cfg[i + 1] = cpu_to_le64(j);
|
501 | 11c2fd3e | aliguori | break;
|
502 | 11c2fd3e | aliguori | } |
503 | 11c2fd3e | aliguori | } |
504 | 11c2fd3e | aliguori | } |
505 | 11c2fd3e | aliguori | for (i = 0; i < nb_numa_nodes; i++) { |
506 | 11c2fd3e | aliguori | numa_fw_cfg[smp_cpus + 1 + i] = cpu_to_le64(node_mem[i]);
|
507 | 11c2fd3e | aliguori | } |
508 | 11c2fd3e | aliguori | fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, (uint8_t *)numa_fw_cfg, |
509 | 11c2fd3e | aliguori | (1 + smp_cpus + nb_numa_nodes) * 8); |
510 | bf483392 | Alexander Graf | |
511 | bf483392 | Alexander Graf | return fw_cfg;
|
512 | 80cabfad | bellard | } |
513 | 80cabfad | bellard | |
514 | 642a4f96 | ths | /* Generate an initial boot sector which sets state and jump to
|
515 | 642a4f96 | ths | a specified vector */
|
516 | 7ffa4767 | pbrook | static void generate_bootsect(target_phys_addr_t option_rom, |
517 | 4fc9af53 | aliguori | uint32_t gpr[8], uint16_t segs[6], uint16_t ip) |
518 | 642a4f96 | ths | { |
519 | 4fc9af53 | aliguori | uint8_t rom[512], *p, *reloc;
|
520 | 4fc9af53 | aliguori | uint8_t sum; |
521 | 642a4f96 | ths | int i;
|
522 | 642a4f96 | ths | |
523 | 4fc9af53 | aliguori | memset(rom, 0, sizeof(rom)); |
524 | 4fc9af53 | aliguori | |
525 | 4fc9af53 | aliguori | p = rom; |
526 | 4fc9af53 | aliguori | /* Make sure we have an option rom signature */
|
527 | 4fc9af53 | aliguori | *p++ = 0x55;
|
528 | 4fc9af53 | aliguori | *p++ = 0xaa;
|
529 | 642a4f96 | ths | |
530 | 4fc9af53 | aliguori | /* ROM size in sectors*/
|
531 | 4fc9af53 | aliguori | *p++ = 1;
|
532 | 642a4f96 | ths | |
533 | 4fc9af53 | aliguori | /* Hook int19 */
|
534 | 642a4f96 | ths | |
535 | 4fc9af53 | aliguori | *p++ = 0x50; /* push ax */ |
536 | 4fc9af53 | aliguori | *p++ = 0x1e; /* push ds */ |
537 | 4fc9af53 | aliguori | *p++ = 0x31; *p++ = 0xc0; /* xor ax, ax */ |
538 | 4fc9af53 | aliguori | *p++ = 0x8e; *p++ = 0xd8; /* mov ax, ds */ |
539 | 642a4f96 | ths | |
540 | 4fc9af53 | aliguori | *p++ = 0xc7; *p++ = 0x06; /* movvw _start,0x64 */ |
541 | 4fc9af53 | aliguori | *p++ = 0x64; *p++ = 0x00; |
542 | 4fc9af53 | aliguori | reloc = p; |
543 | 4fc9af53 | aliguori | *p++ = 0x00; *p++ = 0x00; |
544 | 4fc9af53 | aliguori | |
545 | 4fc9af53 | aliguori | *p++ = 0x8c; *p++ = 0x0e; /* mov cs,0x66 */ |
546 | 4fc9af53 | aliguori | *p++ = 0x66; *p++ = 0x00; |
547 | 4fc9af53 | aliguori | |
548 | 4fc9af53 | aliguori | *p++ = 0x1f; /* pop ds */ |
549 | 4fc9af53 | aliguori | *p++ = 0x58; /* pop ax */ |
550 | 4fc9af53 | aliguori | *p++ = 0xcb; /* lret */ |
551 | 4fc9af53 | aliguori | |
552 | 642a4f96 | ths | /* Actual code */
|
553 | 4fc9af53 | aliguori | *reloc = (p - rom); |
554 | 4fc9af53 | aliguori | |
555 | 642a4f96 | ths | *p++ = 0xfa; /* CLI */ |
556 | 642a4f96 | ths | *p++ = 0xfc; /* CLD */ |
557 | 642a4f96 | ths | |
558 | 642a4f96 | ths | for (i = 0; i < 6; i++) { |
559 | 642a4f96 | ths | if (i == 1) /* Skip CS */ |
560 | 642a4f96 | ths | continue;
|
561 | 642a4f96 | ths | |
562 | 642a4f96 | ths | *p++ = 0xb8; /* MOV AX,imm16 */ |
563 | 642a4f96 | ths | *p++ = segs[i]; |
564 | 642a4f96 | ths | *p++ = segs[i] >> 8;
|
565 | 642a4f96 | ths | *p++ = 0x8e; /* MOV <seg>,AX */ |
566 | 642a4f96 | ths | *p++ = 0xc0 + (i << 3); |
567 | 642a4f96 | ths | } |
568 | 642a4f96 | ths | |
569 | 642a4f96 | ths | for (i = 0; i < 8; i++) { |
570 | 642a4f96 | ths | *p++ = 0x66; /* 32-bit operand size */ |
571 | 642a4f96 | ths | *p++ = 0xb8 + i; /* MOV <reg>,imm32 */ |
572 | 642a4f96 | ths | *p++ = gpr[i]; |
573 | 642a4f96 | ths | *p++ = gpr[i] >> 8;
|
574 | 642a4f96 | ths | *p++ = gpr[i] >> 16;
|
575 | 642a4f96 | ths | *p++ = gpr[i] >> 24;
|
576 | 642a4f96 | ths | } |
577 | 642a4f96 | ths | |
578 | 642a4f96 | ths | *p++ = 0xea; /* JMP FAR */ |
579 | 642a4f96 | ths | *p++ = ip; /* IP */
|
580 | 642a4f96 | ths | *p++ = ip >> 8;
|
581 | 642a4f96 | ths | *p++ = segs[1]; /* CS */ |
582 | 642a4f96 | ths | *p++ = segs[1] >> 8; |
583 | 642a4f96 | ths | |
584 | 4fc9af53 | aliguori | /* sign rom */
|
585 | 4fc9af53 | aliguori | sum = 0;
|
586 | 4fc9af53 | aliguori | for (i = 0; i < (sizeof(rom) - 1); i++) |
587 | 4fc9af53 | aliguori | sum += rom[i]; |
588 | 4fc9af53 | aliguori | rom[sizeof(rom) - 1] = -sum; |
589 | 4fc9af53 | aliguori | |
590 | 7ffa4767 | pbrook | cpu_physical_memory_write_rom(option_rom, rom, sizeof(rom));
|
591 | d6ecb036 | Glauber Costa | option_rom_setup_reset(option_rom, sizeof (rom));
|
592 | 642a4f96 | ths | } |
593 | 80cabfad | bellard | |
594 | 642a4f96 | ths | static long get_file_size(FILE *f) |
595 | 642a4f96 | ths | { |
596 | 642a4f96 | ths | long where, size;
|
597 | 642a4f96 | ths | |
598 | 642a4f96 | ths | /* XXX: on Unix systems, using fstat() probably makes more sense */
|
599 | 642a4f96 | ths | |
600 | 642a4f96 | ths | where = ftell(f); |
601 | 642a4f96 | ths | fseek(f, 0, SEEK_END);
|
602 | 642a4f96 | ths | size = ftell(f); |
603 | 642a4f96 | ths | fseek(f, where, SEEK_SET); |
604 | 642a4f96 | ths | |
605 | 642a4f96 | ths | return size;
|
606 | 642a4f96 | ths | } |
607 | 642a4f96 | ths | |
608 | f16408df | Alexander Graf | #define MULTIBOOT_STRUCT_ADDR 0x9000 |
609 | f16408df | Alexander Graf | |
610 | f16408df | Alexander Graf | #if MULTIBOOT_STRUCT_ADDR > 0xf0000 |
611 | f16408df | Alexander Graf | #error multiboot struct needs to fit in 16 bit real mode |
612 | f16408df | Alexander Graf | #endif
|
613 | f16408df | Alexander Graf | |
614 | f16408df | Alexander Graf | static int load_multiboot(void *fw_cfg, |
615 | f16408df | Alexander Graf | FILE *f, |
616 | f16408df | Alexander Graf | const char *kernel_filename, |
617 | f16408df | Alexander Graf | const char *initrd_filename, |
618 | f16408df | Alexander Graf | const char *kernel_cmdline, |
619 | f16408df | Alexander Graf | uint8_t *header) |
620 | f16408df | Alexander Graf | { |
621 | f16408df | Alexander Graf | int i, t, is_multiboot = 0; |
622 | f16408df | Alexander Graf | uint32_t flags = 0;
|
623 | f16408df | Alexander Graf | uint32_t mh_entry_addr; |
624 | f16408df | Alexander Graf | uint32_t mh_load_addr; |
625 | f16408df | Alexander Graf | uint32_t mb_kernel_size; |
626 | f16408df | Alexander Graf | uint32_t mmap_addr = MULTIBOOT_STRUCT_ADDR; |
627 | f16408df | Alexander Graf | uint32_t mb_bootinfo = MULTIBOOT_STRUCT_ADDR + 0x500;
|
628 | f16408df | Alexander Graf | uint32_t mb_cmdline = mb_bootinfo + 0x200;
|
629 | f16408df | Alexander Graf | uint32_t mb_mod_end; |
630 | f16408df | Alexander Graf | |
631 | f16408df | Alexander Graf | /* Ok, let's see if it is a multiboot image.
|
632 | f16408df | Alexander Graf | The header is 12x32bit long, so the latest entry may be 8192 - 48. */
|
633 | f16408df | Alexander Graf | for (i = 0; i < (8192 - 48); i += 4) { |
634 | f16408df | Alexander Graf | if (ldl_p(header+i) == 0x1BADB002) { |
635 | f16408df | Alexander Graf | uint32_t checksum = ldl_p(header+i+8);
|
636 | f16408df | Alexander Graf | flags = ldl_p(header+i+4);
|
637 | f16408df | Alexander Graf | checksum += flags; |
638 | f16408df | Alexander Graf | checksum += (uint32_t)0x1BADB002;
|
639 | f16408df | Alexander Graf | if (!checksum) {
|
640 | f16408df | Alexander Graf | is_multiboot = 1;
|
641 | f16408df | Alexander Graf | break;
|
642 | f16408df | Alexander Graf | } |
643 | f16408df | Alexander Graf | } |
644 | f16408df | Alexander Graf | } |
645 | f16408df | Alexander Graf | |
646 | f16408df | Alexander Graf | if (!is_multiboot)
|
647 | f16408df | Alexander Graf | return 0; /* no multiboot */ |
648 | f16408df | Alexander Graf | |
649 | f16408df | Alexander Graf | #ifdef DEBUG_MULTIBOOT
|
650 | f16408df | Alexander Graf | fprintf(stderr, "qemu: I believe we found a multiboot image!\n");
|
651 | f16408df | Alexander Graf | #endif
|
652 | f16408df | Alexander Graf | |
653 | f16408df | Alexander Graf | if (flags & 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */ |
654 | f16408df | Alexander Graf | fprintf(stderr, "qemu: multiboot knows VBE. we don't.\n");
|
655 | f16408df | Alexander Graf | } |
656 | f16408df | Alexander Graf | if (!(flags & 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */ |
657 | f16408df | Alexander Graf | uint64_t elf_entry; |
658 | f16408df | Alexander Graf | int kernel_size;
|
659 | f16408df | Alexander Graf | fclose(f); |
660 | f16408df | Alexander Graf | kernel_size = load_elf(kernel_filename, 0, &elf_entry, NULL, NULL); |
661 | f16408df | Alexander Graf | if (kernel_size < 0) { |
662 | f16408df | Alexander Graf | fprintf(stderr, "Error while loading elf kernel\n");
|
663 | f16408df | Alexander Graf | exit(1);
|
664 | f16408df | Alexander Graf | } |
665 | f16408df | Alexander Graf | mh_load_addr = mh_entry_addr = elf_entry; |
666 | f16408df | Alexander Graf | mb_kernel_size = kernel_size; |
667 | f16408df | Alexander Graf | |
668 | f16408df | Alexander Graf | #ifdef DEBUG_MULTIBOOT
|
669 | f16408df | Alexander Graf | fprintf(stderr, "qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
|
670 | f16408df | Alexander Graf | mb_kernel_size, (size_t)mh_entry_addr); |
671 | f16408df | Alexander Graf | #endif
|
672 | f16408df | Alexander Graf | } else {
|
673 | f16408df | Alexander Graf | /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
|
674 | f16408df | Alexander Graf | uint32_t mh_header_addr = ldl_p(header+i+12);
|
675 | f16408df | Alexander Graf | mh_load_addr = ldl_p(header+i+16);
|
676 | f16408df | Alexander Graf | #ifdef DEBUG_MULTIBOOT
|
677 | f16408df | Alexander Graf | uint32_t mh_load_end_addr = ldl_p(header+i+20);
|
678 | f16408df | Alexander Graf | uint32_t mh_bss_end_addr = ldl_p(header+i+24);
|
679 | f16408df | Alexander Graf | #endif
|
680 | f16408df | Alexander Graf | uint32_t mb_kernel_text_offset = i - (mh_header_addr - mh_load_addr); |
681 | f16408df | Alexander Graf | |
682 | f16408df | Alexander Graf | mh_entry_addr = ldl_p(header+i+28);
|
683 | f16408df | Alexander Graf | mb_kernel_size = get_file_size(f) - mb_kernel_text_offset; |
684 | f16408df | Alexander Graf | |
685 | f16408df | Alexander Graf | /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE.
|
686 | f16408df | Alexander Graf | uint32_t mh_mode_type = ldl_p(header+i+32);
|
687 | f16408df | Alexander Graf | uint32_t mh_width = ldl_p(header+i+36);
|
688 | f16408df | Alexander Graf | uint32_t mh_height = ldl_p(header+i+40);
|
689 | f16408df | Alexander Graf | uint32_t mh_depth = ldl_p(header+i+44); */
|
690 | f16408df | Alexander Graf | |
691 | f16408df | Alexander Graf | #ifdef DEBUG_MULTIBOOT
|
692 | f16408df | Alexander Graf | fprintf(stderr, "multiboot: mh_header_addr = %#x\n", mh_header_addr);
|
693 | f16408df | Alexander Graf | fprintf(stderr, "multiboot: mh_load_addr = %#x\n", mh_load_addr);
|
694 | f16408df | Alexander Graf | fprintf(stderr, "multiboot: mh_load_end_addr = %#x\n", mh_load_end_addr);
|
695 | f16408df | Alexander Graf | fprintf(stderr, "multiboot: mh_bss_end_addr = %#x\n", mh_bss_end_addr);
|
696 | f16408df | Alexander Graf | #endif
|
697 | f16408df | Alexander Graf | |
698 | f16408df | Alexander Graf | fseek(f, mb_kernel_text_offset, SEEK_SET); |
699 | f16408df | Alexander Graf | |
700 | f16408df | Alexander Graf | #ifdef DEBUG_MULTIBOOT
|
701 | f16408df | Alexander Graf | fprintf(stderr, "qemu: loading multiboot kernel (%#x bytes) at %#x\n",
|
702 | f16408df | Alexander Graf | mb_kernel_size, mh_load_addr); |
703 | f16408df | Alexander Graf | #endif
|
704 | f16408df | Alexander Graf | |
705 | f16408df | Alexander Graf | if (!fread_targphys_ok(mh_load_addr, mb_kernel_size, f)) {
|
706 | f16408df | Alexander Graf | fprintf(stderr, "qemu: read error on multiboot kernel '%s' (%#x)\n",
|
707 | f16408df | Alexander Graf | kernel_filename, mb_kernel_size); |
708 | f16408df | Alexander Graf | exit(1);
|
709 | f16408df | Alexander Graf | } |
710 | f16408df | Alexander Graf | fclose(f); |
711 | f16408df | Alexander Graf | } |
712 | f16408df | Alexander Graf | |
713 | f16408df | Alexander Graf | /* blob size is only the kernel for now */
|
714 | f16408df | Alexander Graf | mb_mod_end = mh_load_addr + mb_kernel_size; |
715 | f16408df | Alexander Graf | |
716 | f16408df | Alexander Graf | /* load modules */
|
717 | f16408df | Alexander Graf | stl_phys(mb_bootinfo + 20, 0x0); /* mods_count */ |
718 | f16408df | Alexander Graf | if (initrd_filename) {
|
719 | f16408df | Alexander Graf | uint32_t mb_mod_info = mb_bootinfo + 0x100;
|
720 | f16408df | Alexander Graf | uint32_t mb_mod_cmdline = mb_bootinfo + 0x300;
|
721 | f16408df | Alexander Graf | uint32_t mb_mod_start = mh_load_addr; |
722 | f16408df | Alexander Graf | uint32_t mb_mod_length = mb_kernel_size; |
723 | f16408df | Alexander Graf | char *next_initrd;
|
724 | f16408df | Alexander Graf | char *next_space;
|
725 | f16408df | Alexander Graf | int mb_mod_count = 0; |
726 | f16408df | Alexander Graf | |
727 | f16408df | Alexander Graf | do {
|
728 | f16408df | Alexander Graf | next_initrd = strchr(initrd_filename, ',');
|
729 | f16408df | Alexander Graf | if (next_initrd)
|
730 | f16408df | Alexander Graf | *next_initrd = '\0';
|
731 | f16408df | Alexander Graf | /* if a space comes after the module filename, treat everything
|
732 | f16408df | Alexander Graf | after that as parameters */
|
733 | f16408df | Alexander Graf | cpu_physical_memory_write(mb_mod_cmdline, (uint8_t*)initrd_filename, |
734 | f16408df | Alexander Graf | strlen(initrd_filename) + 1);
|
735 | f16408df | Alexander Graf | stl_phys(mb_mod_info + 8, mb_mod_cmdline); /* string */ |
736 | f16408df | Alexander Graf | mb_mod_cmdline += strlen(initrd_filename) + 1;
|
737 | f16408df | Alexander Graf | if ((next_space = strchr(initrd_filename, ' '))) |
738 | f16408df | Alexander Graf | *next_space = '\0';
|
739 | f16408df | Alexander Graf | #ifdef DEBUG_MULTIBOOT
|
740 | f16408df | Alexander Graf | printf("multiboot loading module: %s\n", initrd_filename);
|
741 | f16408df | Alexander Graf | #endif
|
742 | f16408df | Alexander Graf | f = fopen(initrd_filename, "rb");
|
743 | f16408df | Alexander Graf | if (f) {
|
744 | f16408df | Alexander Graf | mb_mod_start = (mb_mod_start + mb_mod_length + (TARGET_PAGE_SIZE - 1))
|
745 | f16408df | Alexander Graf | & (TARGET_PAGE_MASK); |
746 | f16408df | Alexander Graf | mb_mod_length = get_file_size(f); |
747 | f16408df | Alexander Graf | mb_mod_end = mb_mod_start + mb_mod_length; |
748 | f16408df | Alexander Graf | |
749 | f16408df | Alexander Graf | if (!fread_targphys_ok(mb_mod_start, mb_mod_length, f)) {
|
750 | f16408df | Alexander Graf | fprintf(stderr, "qemu: read error on multiboot module '%s' (%#x)\n",
|
751 | f16408df | Alexander Graf | initrd_filename, mb_mod_length); |
752 | f16408df | Alexander Graf | exit(1);
|
753 | f16408df | Alexander Graf | } |
754 | f16408df | Alexander Graf | |
755 | f16408df | Alexander Graf | mb_mod_count++; |
756 | f16408df | Alexander Graf | stl_phys(mb_mod_info + 0, mb_mod_start);
|
757 | f16408df | Alexander Graf | stl_phys(mb_mod_info + 4, mb_mod_start + mb_mod_length);
|
758 | f16408df | Alexander Graf | #ifdef DEBUG_MULTIBOOT
|
759 | f16408df | Alexander Graf | printf("mod_start: %#x\nmod_end: %#x\n", mb_mod_start,
|
760 | f16408df | Alexander Graf | mb_mod_start + mb_mod_length); |
761 | f16408df | Alexander Graf | #endif
|
762 | f16408df | Alexander Graf | stl_phys(mb_mod_info + 12, 0x0); /* reserved */ |
763 | f16408df | Alexander Graf | } |
764 | f16408df | Alexander Graf | initrd_filename = next_initrd+1;
|
765 | f16408df | Alexander Graf | mb_mod_info += 16;
|
766 | f16408df | Alexander Graf | } while (next_initrd);
|
767 | f16408df | Alexander Graf | stl_phys(mb_bootinfo + 20, mb_mod_count); /* mods_count */ |
768 | f16408df | Alexander Graf | stl_phys(mb_bootinfo + 24, mb_bootinfo + 0x100); /* mods_addr */ |
769 | f16408df | Alexander Graf | } |
770 | f16408df | Alexander Graf | |
771 | f16408df | Alexander Graf | /* Make sure we're getting kernel + modules back after reset */
|
772 | f16408df | Alexander Graf | option_rom_setup_reset(mh_load_addr, mb_mod_end - mh_load_addr); |
773 | f16408df | Alexander Graf | |
774 | f16408df | Alexander Graf | /* Commandline support */
|
775 | f16408df | Alexander Graf | stl_phys(mb_bootinfo + 16, mb_cmdline);
|
776 | f16408df | Alexander Graf | t = strlen(kernel_filename); |
777 | f16408df | Alexander Graf | cpu_physical_memory_write(mb_cmdline, (uint8_t*)kernel_filename, t); |
778 | f16408df | Alexander Graf | mb_cmdline += t; |
779 | f16408df | Alexander Graf | stb_phys(mb_cmdline++, ' ');
|
780 | f16408df | Alexander Graf | t = strlen(kernel_cmdline) + 1;
|
781 | f16408df | Alexander Graf | cpu_physical_memory_write(mb_cmdline, (uint8_t*)kernel_cmdline, t); |
782 | f16408df | Alexander Graf | |
783 | f16408df | Alexander Graf | /* the kernel is where we want it to be now */
|
784 | f16408df | Alexander Graf | |
785 | f16408df | Alexander Graf | #define MULTIBOOT_FLAGS_MEMORY (1 << 0) |
786 | f16408df | Alexander Graf | #define MULTIBOOT_FLAGS_BOOT_DEVICE (1 << 1) |
787 | f16408df | Alexander Graf | #define MULTIBOOT_FLAGS_CMDLINE (1 << 2) |
788 | f16408df | Alexander Graf | #define MULTIBOOT_FLAGS_MODULES (1 << 3) |
789 | f16408df | Alexander Graf | #define MULTIBOOT_FLAGS_MMAP (1 << 6) |
790 | f16408df | Alexander Graf | stl_phys(mb_bootinfo, MULTIBOOT_FLAGS_MEMORY |
791 | f16408df | Alexander Graf | | MULTIBOOT_FLAGS_BOOT_DEVICE |
792 | f16408df | Alexander Graf | | MULTIBOOT_FLAGS_CMDLINE |
793 | f16408df | Alexander Graf | | MULTIBOOT_FLAGS_MODULES |
794 | f16408df | Alexander Graf | | MULTIBOOT_FLAGS_MMAP); |
795 | f16408df | Alexander Graf | stl_phys(mb_bootinfo + 4, 640); /* mem_lower */ |
796 | f16408df | Alexander Graf | stl_phys(mb_bootinfo + 8, ram_size / 1024); /* mem_upper */ |
797 | f16408df | Alexander Graf | stl_phys(mb_bootinfo + 12, 0x8001ffff); /* XXX: use the -boot switch? */ |
798 | f16408df | Alexander Graf | stl_phys(mb_bootinfo + 48, mmap_addr); /* mmap_addr */ |
799 | f16408df | Alexander Graf | |
800 | f16408df | Alexander Graf | #ifdef DEBUG_MULTIBOOT
|
801 | f16408df | Alexander Graf | fprintf(stderr, "multiboot: mh_entry_addr = %#x\n", mh_entry_addr);
|
802 | f16408df | Alexander Graf | #endif
|
803 | f16408df | Alexander Graf | |
804 | f16408df | Alexander Graf | /* Pass variables to option rom */
|
805 | f16408df | Alexander Graf | fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_entry_addr); |
806 | f16408df | Alexander Graf | fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, mb_bootinfo); |
807 | f16408df | Alexander Graf | fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, mmap_addr); |
808 | f16408df | Alexander Graf | |
809 | f16408df | Alexander Graf | /* Make sure we're getting the config space back after reset */
|
810 | f16408df | Alexander Graf | option_rom_setup_reset(mb_bootinfo, 0x500);
|
811 | f16408df | Alexander Graf | |
812 | f16408df | Alexander Graf | option_rom[nb_option_roms] = "multiboot.bin";
|
813 | f16408df | Alexander Graf | nb_option_roms++; |
814 | f16408df | Alexander Graf | |
815 | f16408df | Alexander Graf | return 1; /* yes, we are multiboot */ |
816 | f16408df | Alexander Graf | } |
817 | f16408df | Alexander Graf | |
818 | f16408df | Alexander Graf | static void load_linux(void *fw_cfg, |
819 | f16408df | Alexander Graf | target_phys_addr_t option_rom, |
820 | 4fc9af53 | aliguori | const char *kernel_filename, |
821 | 642a4f96 | ths | const char *initrd_filename, |
822 | e6ade764 | Glauber Costa | const char *kernel_cmdline, |
823 | e6ade764 | Glauber Costa | target_phys_addr_t max_ram_size) |
824 | 642a4f96 | ths | { |
825 | 642a4f96 | ths | uint16_t protocol; |
826 | 642a4f96 | ths | uint32_t gpr[8];
|
827 | 642a4f96 | ths | uint16_t seg[6];
|
828 | 642a4f96 | ths | uint16_t real_seg; |
829 | 5cea8590 | Paul Brook | int setup_size, kernel_size, initrd_size = 0, cmdline_size; |
830 | 642a4f96 | ths | uint32_t initrd_max; |
831 | f16408df | Alexander Graf | uint8_t header[8192];
|
832 | 5cea8590 | Paul Brook | target_phys_addr_t real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
|
833 | 642a4f96 | ths | FILE *f, *fi; |
834 | bf4e5d92 | Pascal Terjan | char *vmode;
|
835 | 642a4f96 | ths | |
836 | 642a4f96 | ths | /* Align to 16 bytes as a paranoia measure */
|
837 | 642a4f96 | ths | cmdline_size = (strlen(kernel_cmdline)+16) & ~15; |
838 | 642a4f96 | ths | |
839 | 642a4f96 | ths | /* load the kernel header */
|
840 | 642a4f96 | ths | f = fopen(kernel_filename, "rb");
|
841 | 642a4f96 | ths | if (!f || !(kernel_size = get_file_size(f)) ||
|
842 | f16408df | Alexander Graf | fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
|
843 | f16408df | Alexander Graf | MIN(ARRAY_SIZE(header), kernel_size)) { |
844 | 642a4f96 | ths | fprintf(stderr, "qemu: could not load kernel '%s'\n",
|
845 | 642a4f96 | ths | kernel_filename); |
846 | 642a4f96 | ths | exit(1);
|
847 | 642a4f96 | ths | } |
848 | 642a4f96 | ths | |
849 | 642a4f96 | ths | /* kernel protocol version */
|
850 | bc4edd79 | bellard | #if 0
|
851 | 642a4f96 | ths | fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
|
852 | bc4edd79 | bellard | #endif
|
853 | 642a4f96 | ths | if (ldl_p(header+0x202) == 0x53726448) |
854 | 642a4f96 | ths | protocol = lduw_p(header+0x206);
|
855 | f16408df | Alexander Graf | else {
|
856 | f16408df | Alexander Graf | /* This looks like a multiboot kernel. If it is, let's stop
|
857 | f16408df | Alexander Graf | treating it like a Linux kernel. */
|
858 | f16408df | Alexander Graf | if (load_multiboot(fw_cfg, f, kernel_filename,
|
859 | f16408df | Alexander Graf | initrd_filename, kernel_cmdline, header)) |
860 | f16408df | Alexander Graf | return;
|
861 | 642a4f96 | ths | protocol = 0;
|
862 | f16408df | Alexander Graf | } |
863 | 642a4f96 | ths | |
864 | 642a4f96 | ths | if (protocol < 0x200 || !(header[0x211] & 0x01)) { |
865 | 642a4f96 | ths | /* Low kernel */
|
866 | a37af289 | blueswir1 | real_addr = 0x90000;
|
867 | a37af289 | blueswir1 | cmdline_addr = 0x9a000 - cmdline_size;
|
868 | a37af289 | blueswir1 | prot_addr = 0x10000;
|
869 | 642a4f96 | ths | } else if (protocol < 0x202) { |
870 | 642a4f96 | ths | /* High but ancient kernel */
|
871 | a37af289 | blueswir1 | real_addr = 0x90000;
|
872 | a37af289 | blueswir1 | cmdline_addr = 0x9a000 - cmdline_size;
|
873 | a37af289 | blueswir1 | prot_addr = 0x100000;
|
874 | 642a4f96 | ths | } else {
|
875 | 642a4f96 | ths | /* High and recent kernel */
|
876 | a37af289 | blueswir1 | real_addr = 0x10000;
|
877 | a37af289 | blueswir1 | cmdline_addr = 0x20000;
|
878 | a37af289 | blueswir1 | prot_addr = 0x100000;
|
879 | 642a4f96 | ths | } |
880 | 642a4f96 | ths | |
881 | bc4edd79 | bellard | #if 0
|
882 | 642a4f96 | ths | fprintf(stderr,
|
883 | 526ccb7a | balrog | "qemu: real_addr = 0x" TARGET_FMT_plx "\n"
|
884 | 526ccb7a | balrog | "qemu: cmdline_addr = 0x" TARGET_FMT_plx "\n"
|
885 | 526ccb7a | balrog | "qemu: prot_addr = 0x" TARGET_FMT_plx "\n",
|
886 | a37af289 | blueswir1 | real_addr,
|
887 | a37af289 | blueswir1 | cmdline_addr,
|
888 | a37af289 | blueswir1 | prot_addr);
|
889 | bc4edd79 | bellard | #endif
|
890 | 642a4f96 | ths | |
891 | 642a4f96 | ths | /* highest address for loading the initrd */
|
892 | 642a4f96 | ths | if (protocol >= 0x203) |
893 | 642a4f96 | ths | initrd_max = ldl_p(header+0x22c);
|
894 | 642a4f96 | ths | else
|
895 | 642a4f96 | ths | initrd_max = 0x37ffffff;
|
896 | 642a4f96 | ths | |
897 | e6ade764 | Glauber Costa | if (initrd_max >= max_ram_size-ACPI_DATA_SIZE)
|
898 | e6ade764 | Glauber Costa | initrd_max = max_ram_size-ACPI_DATA_SIZE-1;
|
899 | 642a4f96 | ths | |
900 | 642a4f96 | ths | /* kernel command line */
|
901 | a37af289 | blueswir1 | pstrcpy_targphys(cmdline_addr, 4096, kernel_cmdline);
|
902 | 642a4f96 | ths | |
903 | 642a4f96 | ths | if (protocol >= 0x202) { |
904 | a37af289 | blueswir1 | stl_p(header+0x228, cmdline_addr);
|
905 | 642a4f96 | ths | } else {
|
906 | 642a4f96 | ths | stw_p(header+0x20, 0xA33F); |
907 | 642a4f96 | ths | stw_p(header+0x22, cmdline_addr-real_addr);
|
908 | 642a4f96 | ths | } |
909 | 642a4f96 | ths | |
910 | bf4e5d92 | Pascal Terjan | /* handle vga= parameter */
|
911 | bf4e5d92 | Pascal Terjan | vmode = strstr(kernel_cmdline, "vga=");
|
912 | bf4e5d92 | Pascal Terjan | if (vmode) {
|
913 | bf4e5d92 | Pascal Terjan | unsigned int video_mode; |
914 | bf4e5d92 | Pascal Terjan | /* skip "vga=" */
|
915 | bf4e5d92 | Pascal Terjan | vmode += 4;
|
916 | bf4e5d92 | Pascal Terjan | if (!strncmp(vmode, "normal", 6)) { |
917 | bf4e5d92 | Pascal Terjan | video_mode = 0xffff;
|
918 | bf4e5d92 | Pascal Terjan | } else if (!strncmp(vmode, "ext", 3)) { |
919 | bf4e5d92 | Pascal Terjan | video_mode = 0xfffe;
|
920 | bf4e5d92 | Pascal Terjan | } else if (!strncmp(vmode, "ask", 3)) { |
921 | bf4e5d92 | Pascal Terjan | video_mode = 0xfffd;
|
922 | bf4e5d92 | Pascal Terjan | } else {
|
923 | bf4e5d92 | Pascal Terjan | video_mode = strtol(vmode, NULL, 0); |
924 | bf4e5d92 | Pascal Terjan | } |
925 | bf4e5d92 | Pascal Terjan | stw_p(header+0x1fa, video_mode);
|
926 | bf4e5d92 | Pascal Terjan | } |
927 | bf4e5d92 | Pascal Terjan | |
928 | 642a4f96 | ths | /* loader type */
|
929 | 642a4f96 | ths | /* High nybble = B reserved for Qemu; low nybble is revision number.
|
930 | 642a4f96 | ths | If this code is substantially changed, you may want to consider
|
931 | 642a4f96 | ths | incrementing the revision. */
|
932 | 642a4f96 | ths | if (protocol >= 0x200) |
933 | 642a4f96 | ths | header[0x210] = 0xB0; |
934 | 642a4f96 | ths | |
935 | 642a4f96 | ths | /* heap */
|
936 | 642a4f96 | ths | if (protocol >= 0x201) { |
937 | 642a4f96 | ths | header[0x211] |= 0x80; /* CAN_USE_HEAP */ |
938 | 642a4f96 | ths | stw_p(header+0x224, cmdline_addr-real_addr-0x200); |
939 | 642a4f96 | ths | } |
940 | 642a4f96 | ths | |
941 | 642a4f96 | ths | /* load initrd */
|
942 | 642a4f96 | ths | if (initrd_filename) {
|
943 | 642a4f96 | ths | if (protocol < 0x200) { |
944 | 642a4f96 | ths | fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
|
945 | 642a4f96 | ths | exit(1);
|
946 | 642a4f96 | ths | } |
947 | 642a4f96 | ths | |
948 | 642a4f96 | ths | fi = fopen(initrd_filename, "rb");
|
949 | 642a4f96 | ths | if (!fi) {
|
950 | 642a4f96 | ths | fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
|
951 | 642a4f96 | ths | initrd_filename); |
952 | 642a4f96 | ths | exit(1);
|
953 | 642a4f96 | ths | } |
954 | 642a4f96 | ths | |
955 | 642a4f96 | ths | initrd_size = get_file_size(fi); |
956 | a37af289 | blueswir1 | initrd_addr = (initrd_max-initrd_size) & ~4095;
|
957 | 642a4f96 | ths | |
958 | a37af289 | blueswir1 | if (!fread_targphys_ok(initrd_addr, initrd_size, fi)) {
|
959 | 642a4f96 | ths | fprintf(stderr, "qemu: read error on initial ram disk '%s'\n",
|
960 | 642a4f96 | ths | initrd_filename); |
961 | 642a4f96 | ths | exit(1);
|
962 | 642a4f96 | ths | } |
963 | 642a4f96 | ths | fclose(fi); |
964 | 642a4f96 | ths | |
965 | a37af289 | blueswir1 | stl_p(header+0x218, initrd_addr);
|
966 | 642a4f96 | ths | stl_p(header+0x21c, initrd_size);
|
967 | 642a4f96 | ths | } |
968 | 642a4f96 | ths | |
969 | 642a4f96 | ths | /* store the finalized header and load the rest of the kernel */
|
970 | f16408df | Alexander Graf | cpu_physical_memory_write(real_addr, header, ARRAY_SIZE(header)); |
971 | 642a4f96 | ths | |
972 | 642a4f96 | ths | setup_size = header[0x1f1];
|
973 | 642a4f96 | ths | if (setup_size == 0) |
974 | 642a4f96 | ths | setup_size = 4;
|
975 | 642a4f96 | ths | |
976 | 642a4f96 | ths | setup_size = (setup_size+1)*512; |
977 | f16408df | Alexander Graf | /* Size of protected-mode code */
|
978 | f16408df | Alexander Graf | kernel_size -= (setup_size > ARRAY_SIZE(header)) ? setup_size : ARRAY_SIZE(header); |
979 | f16408df | Alexander Graf | |
980 | f16408df | Alexander Graf | /* In case we have read too much already, copy that over */
|
981 | f16408df | Alexander Graf | if (setup_size < ARRAY_SIZE(header)) {
|
982 | f16408df | Alexander Graf | cpu_physical_memory_write(prot_addr, header + setup_size, ARRAY_SIZE(header) - setup_size); |
983 | f16408df | Alexander Graf | prot_addr += (ARRAY_SIZE(header) - setup_size); |
984 | f16408df | Alexander Graf | setup_size = ARRAY_SIZE(header); |
985 | f16408df | Alexander Graf | } |
986 | 642a4f96 | ths | |
987 | f16408df | Alexander Graf | if (!fread_targphys_ok(real_addr + ARRAY_SIZE(header),
|
988 | f16408df | Alexander Graf | setup_size - ARRAY_SIZE(header), f) || |
989 | a37af289 | blueswir1 | !fread_targphys_ok(prot_addr, kernel_size, f)) { |
990 | 642a4f96 | ths | fprintf(stderr, "qemu: read error on kernel '%s'\n",
|
991 | 642a4f96 | ths | kernel_filename); |
992 | 642a4f96 | ths | exit(1);
|
993 | 642a4f96 | ths | } |
994 | 642a4f96 | ths | fclose(f); |
995 | 642a4f96 | ths | |
996 | 642a4f96 | ths | /* generate bootsector to set up the initial register state */
|
997 | a37af289 | blueswir1 | real_seg = real_addr >> 4;
|
998 | 642a4f96 | ths | seg[0] = seg[2] = seg[3] = seg[4] = seg[4] = real_seg; |
999 | 642a4f96 | ths | seg[1] = real_seg+0x20; /* CS */ |
1000 | 642a4f96 | ths | memset(gpr, 0, sizeof gpr); |
1001 | 642a4f96 | ths | gpr[4] = cmdline_addr-real_addr-16; /* SP (-16 is paranoia) */ |
1002 | 642a4f96 | ths | |
1003 | d6ecb036 | Glauber Costa | option_rom_setup_reset(real_addr, setup_size); |
1004 | d6ecb036 | Glauber Costa | option_rom_setup_reset(prot_addr, kernel_size); |
1005 | d6ecb036 | Glauber Costa | option_rom_setup_reset(cmdline_addr, cmdline_size); |
1006 | d6ecb036 | Glauber Costa | if (initrd_filename)
|
1007 | d6ecb036 | Glauber Costa | option_rom_setup_reset(initrd_addr, initrd_size); |
1008 | d6ecb036 | Glauber Costa | |
1009 | 4fc9af53 | aliguori | generate_bootsect(option_rom, gpr, seg, 0);
|
1010 | 642a4f96 | ths | } |
1011 | 642a4f96 | ths | |
1012 | b41a2cd1 | bellard | static const int ide_iobase[2] = { 0x1f0, 0x170 }; |
1013 | b41a2cd1 | bellard | static const int ide_iobase2[2] = { 0x3f6, 0x376 }; |
1014 | b41a2cd1 | bellard | static const int ide_irq[2] = { 14, 15 }; |
1015 | b41a2cd1 | bellard | |
1016 | b41a2cd1 | bellard | #define NE2000_NB_MAX 6 |
1017 | b41a2cd1 | bellard | |
1018 | 8d11df9e | bellard | static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 }; |
1019 | b41a2cd1 | bellard | static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 }; |
1020 | b41a2cd1 | bellard | |
1021 | 8d11df9e | bellard | static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 }; |
1022 | 8d11df9e | bellard | static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 }; |
1023 | 8d11df9e | bellard | |
1024 | 6508fe59 | bellard | static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc }; |
1025 | 6508fe59 | bellard | static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 }; |
1026 | 6508fe59 | bellard | |
1027 | 6a36d84e | bellard | #ifdef HAS_AUDIO
|
1028 | d537cf6c | pbrook | static void audio_init (PCIBus *pci_bus, qemu_irq *pic) |
1029 | 6a36d84e | bellard | { |
1030 | 6a36d84e | bellard | struct soundhw *c;
|
1031 | 6a36d84e | bellard | |
1032 | 3a8bae3e | malc | for (c = soundhw; c->name; ++c) {
|
1033 | 3a8bae3e | malc | if (c->enabled) {
|
1034 | 3a8bae3e | malc | if (c->isa) {
|
1035 | 3a8bae3e | malc | c->init.init_isa(pic); |
1036 | 3a8bae3e | malc | } else {
|
1037 | 3a8bae3e | malc | if (pci_bus) {
|
1038 | 3a8bae3e | malc | c->init.init_pci(pci_bus); |
1039 | 6a36d84e | bellard | } |
1040 | 6a36d84e | bellard | } |
1041 | 6a36d84e | bellard | } |
1042 | 6a36d84e | bellard | } |
1043 | 6a36d84e | bellard | } |
1044 | 6a36d84e | bellard | #endif
|
1045 | 6a36d84e | bellard | |
1046 | 3a38d437 | Jes Sorensen | static void pc_init_ne2k_isa(NICInfo *nd) |
1047 | a41b2ff2 | pbrook | { |
1048 | a41b2ff2 | pbrook | static int nb_ne2k = 0; |
1049 | a41b2ff2 | pbrook | |
1050 | a41b2ff2 | pbrook | if (nb_ne2k == NE2000_NB_MAX)
|
1051 | a41b2ff2 | pbrook | return;
|
1052 | 3a38d437 | Jes Sorensen | isa_ne2000_init(ne2000_io[nb_ne2k], |
1053 | 3a38d437 | Jes Sorensen | isa_reserve_irq(ne2000_irq[nb_ne2k]), nd); |
1054 | a41b2ff2 | pbrook | nb_ne2k++; |
1055 | a41b2ff2 | pbrook | } |
1056 | a41b2ff2 | pbrook | |
1057 | f753ff16 | pbrook | static int load_option_rom(const char *oprom, target_phys_addr_t start, |
1058 | f753ff16 | pbrook | target_phys_addr_t end) |
1059 | f753ff16 | pbrook | { |
1060 | f753ff16 | pbrook | int size;
|
1061 | 5cea8590 | Paul Brook | char *filename;
|
1062 | 5cea8590 | Paul Brook | |
1063 | 5cea8590 | Paul Brook | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, oprom); |
1064 | 5cea8590 | Paul Brook | if (filename) {
|
1065 | 5cea8590 | Paul Brook | size = get_image_size(filename); |
1066 | 5cea8590 | Paul Brook | if (size > 0 && start + size > end) { |
1067 | 5cea8590 | Paul Brook | fprintf(stderr, "Not enough space to load option rom '%s'\n",
|
1068 | 5cea8590 | Paul Brook | oprom); |
1069 | 5cea8590 | Paul Brook | exit(1);
|
1070 | 5cea8590 | Paul Brook | } |
1071 | 5cea8590 | Paul Brook | size = load_image_targphys(filename, start, end - start); |
1072 | 5cea8590 | Paul Brook | qemu_free(filename); |
1073 | 5cea8590 | Paul Brook | } else {
|
1074 | 5cea8590 | Paul Brook | size = -1;
|
1075 | f753ff16 | pbrook | } |
1076 | f753ff16 | pbrook | if (size < 0) { |
1077 | f753ff16 | pbrook | fprintf(stderr, "Could not load option rom '%s'\n", oprom);
|
1078 | f753ff16 | pbrook | exit(1);
|
1079 | f753ff16 | pbrook | } |
1080 | f753ff16 | pbrook | /* Round up optiom rom size to the next 2k boundary */
|
1081 | f753ff16 | pbrook | size = (size + 2047) & ~2047; |
1082 | e28f9884 | Glauber Costa | option_rom_setup_reset(start, size); |
1083 | f753ff16 | pbrook | return size;
|
1084 | f753ff16 | pbrook | } |
1085 | f753ff16 | pbrook | |
1086 | 678e12cc | Gleb Natapov | int cpu_is_bsp(CPUState *env)
|
1087 | 678e12cc | Gleb Natapov | { |
1088 | 678e12cc | Gleb Natapov | return env->cpuid_apic_id == 0; |
1089 | 678e12cc | Gleb Natapov | } |
1090 | 678e12cc | Gleb Natapov | |
1091 | 3a31f36a | Jan Kiszka | static CPUState *pc_new_cpu(const char *cpu_model) |
1092 | 3a31f36a | Jan Kiszka | { |
1093 | 3a31f36a | Jan Kiszka | CPUState *env; |
1094 | 3a31f36a | Jan Kiszka | |
1095 | 3a31f36a | Jan Kiszka | env = cpu_init(cpu_model); |
1096 | 3a31f36a | Jan Kiszka | if (!env) {
|
1097 | 3a31f36a | Jan Kiszka | fprintf(stderr, "Unable to find x86 CPU definition\n");
|
1098 | 3a31f36a | Jan Kiszka | exit(1);
|
1099 | 3a31f36a | Jan Kiszka | } |
1100 | 3a31f36a | Jan Kiszka | if ((env->cpuid_features & CPUID_APIC) || smp_cpus > 1) { |
1101 | 3a31f36a | Jan Kiszka | env->cpuid_apic_id = env->cpu_index; |
1102 | 3a31f36a | Jan Kiszka | /* APIC reset callback resets cpu */
|
1103 | 3a31f36a | Jan Kiszka | apic_init(env); |
1104 | 3a31f36a | Jan Kiszka | } else {
|
1105 | 3a31f36a | Jan Kiszka | qemu_register_reset((QEMUResetHandler*)cpu_reset, env); |
1106 | 3a31f36a | Jan Kiszka | } |
1107 | 3a31f36a | Jan Kiszka | return env;
|
1108 | 3a31f36a | Jan Kiszka | } |
1109 | 3a31f36a | Jan Kiszka | |
1110 | 80cabfad | bellard | /* PC hardware initialisation */
|
1111 | fbe1b595 | Paul Brook | static void pc_init1(ram_addr_t ram_size, |
1112 | 3023f332 | aliguori | const char *boot_device, |
1113 | e8b2a1c6 | Mark McLoughlin | const char *kernel_filename, |
1114 | e8b2a1c6 | Mark McLoughlin | const char *kernel_cmdline, |
1115 | 3dbbdc25 | bellard | const char *initrd_filename, |
1116 | e8b2a1c6 | Mark McLoughlin | const char *cpu_model, |
1117 | caea79a9 | Mark McLoughlin | int pci_enabled)
|
1118 | 80cabfad | bellard | { |
1119 | 5cea8590 | Paul Brook | char *filename;
|
1120 | 642a4f96 | ths | int ret, linux_boot, i;
|
1121 | b584726d | pbrook | ram_addr_t ram_addr, bios_offset, option_rom_offset; |
1122 | 00f82b8a | aurel32 | ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
|
1123 | f753ff16 | pbrook | int bios_size, isa_bios_size, oprom_area_size;
|
1124 | 46e50e9d | bellard | PCIBus *pci_bus; |
1125 | c2cc47a4 | Markus Armbruster | PCIDevice *pci_dev; |
1126 | b3999638 | Gerd Hoffmann | ISADevice *isa_dev; |
1127 | 5c3ff3a7 | pbrook | int piix3_devfn = -1; |
1128 | 59b8ad81 | bellard | CPUState *env; |
1129 | d537cf6c | pbrook | qemu_irq *cpu_irq; |
1130 | 1452411b | Avi Kivity | qemu_irq *isa_irq; |
1131 | d537cf6c | pbrook | qemu_irq *i8259; |
1132 | 1452411b | Avi Kivity | IsaIrqState *isa_irq_state; |
1133 | 751c6a17 | Gerd Hoffmann | DriveInfo *dinfo; |
1134 | e4bcb14c | ths | BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS]; |
1135 | e4bcb14c | ths | BlockDriverState *fd[MAX_FD]; |
1136 | 34b39c2b | aliguori | int using_vga = cirrus_vga_enabled || std_vga_enabled || vmsvga_enabled;
|
1137 | bf483392 | Alexander Graf | void *fw_cfg;
|
1138 | d592d303 | bellard | |
1139 | 00f82b8a | aurel32 | if (ram_size >= 0xe0000000 ) { |
1140 | 00f82b8a | aurel32 | above_4g_mem_size = ram_size - 0xe0000000;
|
1141 | 00f82b8a | aurel32 | below_4g_mem_size = 0xe0000000;
|
1142 | 00f82b8a | aurel32 | } else {
|
1143 | 00f82b8a | aurel32 | below_4g_mem_size = ram_size; |
1144 | 00f82b8a | aurel32 | } |
1145 | 00f82b8a | aurel32 | |
1146 | 80cabfad | bellard | linux_boot = (kernel_filename != NULL);
|
1147 | 80cabfad | bellard | |
1148 | 59b8ad81 | bellard | /* init CPUs */
|
1149 | a049de61 | bellard | if (cpu_model == NULL) { |
1150 | a049de61 | bellard | #ifdef TARGET_X86_64
|
1151 | a049de61 | bellard | cpu_model = "qemu64";
|
1152 | a049de61 | bellard | #else
|
1153 | a049de61 | bellard | cpu_model = "qemu32";
|
1154 | a049de61 | bellard | #endif
|
1155 | a049de61 | bellard | } |
1156 | 3a31f36a | Jan Kiszka | |
1157 | 3a31f36a | Jan Kiszka | for (i = 0; i < smp_cpus; i++) { |
1158 | 3a31f36a | Jan Kiszka | env = pc_new_cpu(cpu_model); |
1159 | 59b8ad81 | bellard | } |
1160 | 59b8ad81 | bellard | |
1161 | 26fb5e48 | aurel32 | vmport_init(); |
1162 | 26fb5e48 | aurel32 | |
1163 | 80cabfad | bellard | /* allocate RAM */
|
1164 | 82b36dc3 | aliguori | ram_addr = qemu_ram_alloc(0xa0000);
|
1165 | 82b36dc3 | aliguori | cpu_register_physical_memory(0, 0xa0000, ram_addr); |
1166 | 82b36dc3 | aliguori | |
1167 | 82b36dc3 | aliguori | /* Allocate, even though we won't register, so we don't break the
|
1168 | 82b36dc3 | aliguori | * phys_ram_base + PA assumption. This range includes vga (0xa0000 - 0xc0000),
|
1169 | 82b36dc3 | aliguori | * and some bios areas, which will be registered later
|
1170 | 82b36dc3 | aliguori | */
|
1171 | 82b36dc3 | aliguori | ram_addr = qemu_ram_alloc(0x100000 - 0xa0000); |
1172 | 82b36dc3 | aliguori | ram_addr = qemu_ram_alloc(below_4g_mem_size - 0x100000);
|
1173 | 82b36dc3 | aliguori | cpu_register_physical_memory(0x100000,
|
1174 | 82b36dc3 | aliguori | below_4g_mem_size - 0x100000,
|
1175 | 82b36dc3 | aliguori | ram_addr); |
1176 | 00f82b8a | aurel32 | |
1177 | 00f82b8a | aurel32 | /* above 4giga memory allocation */
|
1178 | 00f82b8a | aurel32 | if (above_4g_mem_size > 0) { |
1179 | 8a637d44 | Paul Brook | #if TARGET_PHYS_ADDR_BITS == 32 |
1180 | 8a637d44 | Paul Brook | hw_error("To much RAM for 32-bit physical address");
|
1181 | 8a637d44 | Paul Brook | #else
|
1182 | 82b36dc3 | aliguori | ram_addr = qemu_ram_alloc(above_4g_mem_size); |
1183 | 82b36dc3 | aliguori | cpu_register_physical_memory(0x100000000ULL,
|
1184 | 526ccb7a | balrog | above_4g_mem_size, |
1185 | 82b36dc3 | aliguori | ram_addr); |
1186 | 8a637d44 | Paul Brook | #endif
|
1187 | 00f82b8a | aurel32 | } |
1188 | 80cabfad | bellard | |
1189 | 82b36dc3 | aliguori | |
1190 | 970ac5a3 | bellard | /* BIOS load */
|
1191 | 1192dad8 | j_mayer | if (bios_name == NULL) |
1192 | 1192dad8 | j_mayer | bios_name = BIOS_FILENAME; |
1193 | 5cea8590 | Paul Brook | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); |
1194 | 5cea8590 | Paul Brook | if (filename) {
|
1195 | 5cea8590 | Paul Brook | bios_size = get_image_size(filename); |
1196 | 5cea8590 | Paul Brook | } else {
|
1197 | 5cea8590 | Paul Brook | bios_size = -1;
|
1198 | 5cea8590 | Paul Brook | } |
1199 | 5fafdf24 | ths | if (bios_size <= 0 || |
1200 | 970ac5a3 | bellard | (bios_size % 65536) != 0) { |
1201 | 7587cf44 | bellard | goto bios_error;
|
1202 | 7587cf44 | bellard | } |
1203 | 970ac5a3 | bellard | bios_offset = qemu_ram_alloc(bios_size); |
1204 | 5cea8590 | Paul Brook | ret = load_image(filename, qemu_get_ram_ptr(bios_offset)); |
1205 | 7587cf44 | bellard | if (ret != bios_size) {
|
1206 | 7587cf44 | bellard | bios_error:
|
1207 | 5cea8590 | Paul Brook | fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
|
1208 | 80cabfad | bellard | exit(1);
|
1209 | 80cabfad | bellard | } |
1210 | 5cea8590 | Paul Brook | if (filename) {
|
1211 | 5cea8590 | Paul Brook | qemu_free(filename); |
1212 | 5cea8590 | Paul Brook | } |
1213 | 7587cf44 | bellard | /* map the last 128KB of the BIOS in ISA space */
|
1214 | 7587cf44 | bellard | isa_bios_size = bios_size; |
1215 | 7587cf44 | bellard | if (isa_bios_size > (128 * 1024)) |
1216 | 7587cf44 | bellard | isa_bios_size = 128 * 1024; |
1217 | 5fafdf24 | ths | cpu_register_physical_memory(0x100000 - isa_bios_size,
|
1218 | 5fafdf24 | ths | isa_bios_size, |
1219 | 7587cf44 | bellard | (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM); |
1220 | 9ae02555 | ths | |
1221 | 4fc9af53 | aliguori | |
1222 | f753ff16 | pbrook | |
1223 | f753ff16 | pbrook | option_rom_offset = qemu_ram_alloc(0x20000);
|
1224 | f753ff16 | pbrook | oprom_area_size = 0;
|
1225 | 49669fc5 | Glauber Costa | cpu_register_physical_memory(0xc0000, 0x20000, option_rom_offset); |
1226 | f753ff16 | pbrook | |
1227 | f753ff16 | pbrook | if (using_vga) {
|
1228 | 5cea8590 | Paul Brook | const char *vgabios_filename; |
1229 | f753ff16 | pbrook | /* VGA BIOS load */
|
1230 | f753ff16 | pbrook | if (cirrus_vga_enabled) {
|
1231 | 5cea8590 | Paul Brook | vgabios_filename = VGABIOS_CIRRUS_FILENAME; |
1232 | f753ff16 | pbrook | } else {
|
1233 | 5cea8590 | Paul Brook | vgabios_filename = VGABIOS_FILENAME; |
1234 | 970ac5a3 | bellard | } |
1235 | 5cea8590 | Paul Brook | oprom_area_size = load_option_rom(vgabios_filename, 0xc0000, 0xe0000); |
1236 | f753ff16 | pbrook | } |
1237 | f753ff16 | pbrook | /* Although video roms can grow larger than 0x8000, the area between
|
1238 | f753ff16 | pbrook | * 0xc0000 - 0xc8000 is reserved for them. It means we won't be looking
|
1239 | f753ff16 | pbrook | * for any other kind of option rom inside this area */
|
1240 | f753ff16 | pbrook | if (oprom_area_size < 0x8000) |
1241 | f753ff16 | pbrook | oprom_area_size = 0x8000;
|
1242 | f753ff16 | pbrook | |
1243 | 1d108d97 | Alexander Graf | /* map all the bios at the top of memory */
|
1244 | 1d108d97 | Alexander Graf | cpu_register_physical_memory((uint32_t)(-bios_size), |
1245 | 1d108d97 | Alexander Graf | bios_size, bios_offset | IO_MEM_ROM); |
1246 | 1d108d97 | Alexander Graf | |
1247 | bf483392 | Alexander Graf | fw_cfg = bochs_bios_init(); |
1248 | 1d108d97 | Alexander Graf | |
1249 | f753ff16 | pbrook | if (linux_boot) {
|
1250 | f16408df | Alexander Graf | load_linux(fw_cfg, 0xc0000 + oprom_area_size,
|
1251 | e6ade764 | Glauber Costa | kernel_filename, initrd_filename, kernel_cmdline, below_4g_mem_size); |
1252 | f753ff16 | pbrook | oprom_area_size += 2048;
|
1253 | f753ff16 | pbrook | } |
1254 | f753ff16 | pbrook | |
1255 | f753ff16 | pbrook | for (i = 0; i < nb_option_roms; i++) { |
1256 | 406c8df3 | Glauber Costa | oprom_area_size += load_option_rom(option_rom[i], 0xc0000 + oprom_area_size,
|
1257 | 406c8df3 | Glauber Costa | 0xe0000);
|
1258 | 406c8df3 | Glauber Costa | } |
1259 | 406c8df3 | Glauber Costa | |
1260 | 406c8df3 | Glauber Costa | for (i = 0; i < nb_nics; i++) { |
1261 | 406c8df3 | Glauber Costa | char nic_oprom[1024]; |
1262 | 406c8df3 | Glauber Costa | const char *model = nd_table[i].model; |
1263 | 406c8df3 | Glauber Costa | |
1264 | 406c8df3 | Glauber Costa | if (!nd_table[i].bootable)
|
1265 | 406c8df3 | Glauber Costa | continue;
|
1266 | 406c8df3 | Glauber Costa | |
1267 | 406c8df3 | Glauber Costa | if (model == NULL) |
1268 | 0d6b0b1d | Anthony Liguori | model = "e1000";
|
1269 | 406c8df3 | Glauber Costa | snprintf(nic_oprom, sizeof(nic_oprom), "pxe-%s.bin", model); |
1270 | 406c8df3 | Glauber Costa | |
1271 | 406c8df3 | Glauber Costa | oprom_area_size += load_option_rom(nic_oprom, 0xc0000 + oprom_area_size,
|
1272 | 406c8df3 | Glauber Costa | 0xe0000);
|
1273 | 9ae02555 | ths | } |
1274 | 9ae02555 | ths | |
1275 | a5b38b51 | aurel32 | cpu_irq = qemu_allocate_irqs(pic_irq_request, NULL, 1); |
1276 | d537cf6c | pbrook | i8259 = i8259_init(cpu_irq[0]);
|
1277 | 1452411b | Avi Kivity | isa_irq_state = qemu_mallocz(sizeof(*isa_irq_state));
|
1278 | 1452411b | Avi Kivity | isa_irq_state->i8259 = i8259; |
1279 | 1632dc6a | Avi Kivity | isa_irq = qemu_allocate_irqs(isa_irq_handler, isa_irq_state, 24);
|
1280 | d537cf6c | pbrook | |
1281 | 69b91039 | bellard | if (pci_enabled) {
|
1282 | 1452411b | Avi Kivity | pci_bus = i440fx_init(&i440fx_state, isa_irq); |
1283 | 8f1c91d8 | ths | piix3_devfn = piix3_init(pci_bus, -1);
|
1284 | 46e50e9d | bellard | } else {
|
1285 | 46e50e9d | bellard | pci_bus = NULL;
|
1286 | 2091ba23 | Gerd Hoffmann | isa_bus_new(NULL);
|
1287 | 69b91039 | bellard | } |
1288 | 2091ba23 | Gerd Hoffmann | isa_bus_irqs(isa_irq); |
1289 | 69b91039 | bellard | |
1290 | 3a38d437 | Jes Sorensen | ferr_irq = isa_reserve_irq(13);
|
1291 | 3a38d437 | Jes Sorensen | |
1292 | 80cabfad | bellard | /* init basic PC hardware */
|
1293 | b41a2cd1 | bellard | register_ioport_write(0x80, 1, 1, ioport80_write, NULL); |
1294 | 80cabfad | bellard | |
1295 | f929aad6 | bellard | register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL); |
1296 | f929aad6 | bellard | |
1297 | 1f04275e | bellard | if (cirrus_vga_enabled) {
|
1298 | 1f04275e | bellard | if (pci_enabled) {
|
1299 | fbe1b595 | Paul Brook | pci_cirrus_vga_init(pci_bus); |
1300 | 1f04275e | bellard | } else {
|
1301 | fbe1b595 | Paul Brook | isa_cirrus_vga_init(); |
1302 | 1f04275e | bellard | } |
1303 | d34cab9f | ths | } else if (vmsvga_enabled) { |
1304 | d34cab9f | ths | if (pci_enabled)
|
1305 | fbe1b595 | Paul Brook | pci_vmsvga_init(pci_bus); |
1306 | d34cab9f | ths | else
|
1307 | d34cab9f | ths | fprintf(stderr, "%s: vmware_vga: no PCI bus\n", __FUNCTION__);
|
1308 | c2b3b41a | aliguori | } else if (std_vga_enabled) { |
1309 | 89b6b508 | bellard | if (pci_enabled) {
|
1310 | fbe1b595 | Paul Brook | pci_vga_init(pci_bus, 0, 0); |
1311 | 89b6b508 | bellard | } else {
|
1312 | fbe1b595 | Paul Brook | isa_vga_init(); |
1313 | 89b6b508 | bellard | } |
1314 | 1f04275e | bellard | } |
1315 | 80cabfad | bellard | |
1316 | 3a38d437 | Jes Sorensen | rtc_state = rtc_init(0x70, isa_reserve_irq(8), 2000); |
1317 | 80cabfad | bellard | |
1318 | 3b4366de | blueswir1 | qemu_register_boot_set(pc_boot_set, rtc_state); |
1319 | 3b4366de | blueswir1 | |
1320 | e1a23744 | bellard | register_ioport_read(0x92, 1, 1, ioport92_read, NULL); |
1321 | e1a23744 | bellard | register_ioport_write(0x92, 1, 1, ioport92_write, NULL); |
1322 | e1a23744 | bellard | |
1323 | d592d303 | bellard | if (pci_enabled) {
|
1324 | 1632dc6a | Avi Kivity | isa_irq_state->ioapic = ioapic_init(); |
1325 | d592d303 | bellard | } |
1326 | 3a38d437 | Jes Sorensen | pit = pit_init(0x40, isa_reserve_irq(0)); |
1327 | fd06c375 | bellard | pcspk_init(pit); |
1328 | 16b29ae1 | aliguori | if (!no_hpet) {
|
1329 | 1452411b | Avi Kivity | hpet_init(isa_irq); |
1330 | 16b29ae1 | aliguori | } |
1331 | b41a2cd1 | bellard | |
1332 | 8d11df9e | bellard | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
1333 | 8d11df9e | bellard | if (serial_hds[i]) {
|
1334 | 3a38d437 | Jes Sorensen | serial_init(serial_io[i], isa_reserve_irq(serial_irq[i]), 115200,
|
1335 | b6cd0ea1 | aurel32 | serial_hds[i]); |
1336 | 8d11df9e | bellard | } |
1337 | 8d11df9e | bellard | } |
1338 | b41a2cd1 | bellard | |
1339 | 6508fe59 | bellard | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
1340 | 6508fe59 | bellard | if (parallel_hds[i]) {
|
1341 | 3a38d437 | Jes Sorensen | parallel_init(parallel_io[i], isa_reserve_irq(parallel_irq[i]), |
1342 | d537cf6c | pbrook | parallel_hds[i]); |
1343 | 6508fe59 | bellard | } |
1344 | 6508fe59 | bellard | } |
1345 | 6508fe59 | bellard | |
1346 | 9dd986cc | Richard W.M. Jones | watchdog_pc_init(pci_bus); |
1347 | 9dd986cc | Richard W.M. Jones | |
1348 | a41b2ff2 | pbrook | for(i = 0; i < nb_nics; i++) { |
1349 | cb457d76 | aliguori | NICInfo *nd = &nd_table[i]; |
1350 | cb457d76 | aliguori | |
1351 | cb457d76 | aliguori | if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0)) |
1352 | 3a38d437 | Jes Sorensen | pc_init_ne2k_isa(nd); |
1353 | cb457d76 | aliguori | else
|
1354 | 0d6b0b1d | Anthony Liguori | pci_nic_init(nd, "e1000", NULL); |
1355 | a41b2ff2 | pbrook | } |
1356 | b41a2cd1 | bellard | |
1357 | 9d5e77a2 | Isaku Yamahata | piix4_acpi_system_hot_add_init(); |
1358 | 5e3cb534 | aliguori | |
1359 | e4bcb14c | ths | if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
|
1360 | e4bcb14c | ths | fprintf(stderr, "qemu: too many IDE bus\n");
|
1361 | e4bcb14c | ths | exit(1);
|
1362 | e4bcb14c | ths | } |
1363 | e4bcb14c | ths | |
1364 | e4bcb14c | ths | for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) { |
1365 | 751c6a17 | Gerd Hoffmann | dinfo = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS); |
1366 | 751c6a17 | Gerd Hoffmann | hd[i] = dinfo ? dinfo->bdrv : NULL;
|
1367 | e4bcb14c | ths | } |
1368 | e4bcb14c | ths | |
1369 | a41b2ff2 | pbrook | if (pci_enabled) {
|
1370 | 1452411b | Avi Kivity | pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1, isa_irq);
|
1371 | a41b2ff2 | pbrook | } else {
|
1372 | e4bcb14c | ths | for(i = 0; i < MAX_IDE_BUS; i++) { |
1373 | 3a38d437 | Jes Sorensen | isa_ide_init(ide_iobase[i], ide_iobase2[i], |
1374 | 3a38d437 | Jes Sorensen | isa_reserve_irq(ide_irq[i]), |
1375 | e4bcb14c | ths | hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
|
1376 | 69b91039 | bellard | } |
1377 | b41a2cd1 | bellard | } |
1378 | 69b91039 | bellard | |
1379 | e8935eef | Gerd Hoffmann | isa_dev = isa_create_simple("i8042", 0x60, 0x64, 1, 12); |
1380 | 7c29d0c0 | bellard | DMA_init(0);
|
1381 | 6a36d84e | bellard | #ifdef HAS_AUDIO
|
1382 | 1452411b | Avi Kivity | audio_init(pci_enabled ? pci_bus : NULL, isa_irq);
|
1383 | fb065187 | bellard | #endif
|
1384 | 80cabfad | bellard | |
1385 | e4bcb14c | ths | for(i = 0; i < MAX_FD; i++) { |
1386 | 751c6a17 | Gerd Hoffmann | dinfo = drive_get(IF_FLOPPY, 0, i);
|
1387 | 751c6a17 | Gerd Hoffmann | fd[i] = dinfo ? dinfo->bdrv : NULL;
|
1388 | e4bcb14c | ths | } |
1389 | 2091ba23 | Gerd Hoffmann | floppy_controller = fdctrl_init_isa(6, 2, 0x3f0, fd); |
1390 | b41a2cd1 | bellard | |
1391 | 00f82b8a | aurel32 | cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device, hd); |
1392 | 69b91039 | bellard | |
1393 | bb36d470 | bellard | if (pci_enabled && usb_enabled) {
|
1394 | afcc3cdf | ths | usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
|
1395 | bb36d470 | bellard | } |
1396 | bb36d470 | bellard | |
1397 | 6515b203 | bellard | if (pci_enabled && acpi_enabled) {
|
1398 | 3fffc223 | ths | uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */ |
1399 | 0ff596d0 | pbrook | i2c_bus *smbus; |
1400 | 0ff596d0 | pbrook | |
1401 | 0ff596d0 | pbrook | /* TODO: Populate SPD eeprom data. */
|
1402 | 3a38d437 | Jes Sorensen | smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100, |
1403 | 3a38d437 | Jes Sorensen | isa_reserve_irq(9));
|
1404 | 3fffc223 | ths | for (i = 0; i < 8; i++) { |
1405 | 1ea96673 | Paul Brook | DeviceState *eeprom; |
1406 | 02e2da45 | Paul Brook | eeprom = qdev_create((BusState *)smbus, "smbus-eeprom");
|
1407 | ee6847d1 | Gerd Hoffmann | qdev_prop_set_uint32(eeprom, "address", 0x50 + i); |
1408 | ee6847d1 | Gerd Hoffmann | qdev_prop_set_ptr(eeprom, "data", eeprom_buf + (i * 256)); |
1409 | 1ea96673 | Paul Brook | qdev_init(eeprom); |
1410 | 3fffc223 | ths | } |
1411 | 6515b203 | bellard | } |
1412 | 3b46e624 | ths | |
1413 | a5954d5c | bellard | if (i440fx_state) {
|
1414 | a5954d5c | bellard | i440fx_init_memory_mappings(i440fx_state); |
1415 | a5954d5c | bellard | } |
1416 | e4bcb14c | ths | |
1417 | 7d8406be | pbrook | if (pci_enabled) {
|
1418 | e4bcb14c | ths | int max_bus;
|
1419 | 9be5dafe | Paul Brook | int bus;
|
1420 | 96d30e48 | ths | |
1421 | e4bcb14c | ths | max_bus = drive_get_max_bus(IF_SCSI); |
1422 | e4bcb14c | ths | for (bus = 0; bus <= max_bus; bus++) { |
1423 | 9be5dafe | Paul Brook | pci_create_simple(pci_bus, -1, "lsi53c895a"); |
1424 | e4bcb14c | ths | } |
1425 | 7d8406be | pbrook | } |
1426 | 6e02c38d | aliguori | |
1427 | bd322087 | aliguori | /* Add virtio balloon device */
|
1428 | 7d4c3d53 | Markus Armbruster | if (pci_enabled && virtio_balloon) {
|
1429 | 7d4c3d53 | Markus Armbruster | pci_dev = pci_create("virtio-balloon-pci", virtio_balloon_devaddr);
|
1430 | 7d4c3d53 | Markus Armbruster | qdev_init(&pci_dev->qdev); |
1431 | 2d72c572 | Paul Brook | } |
1432 | a2fa19f9 | aliguori | |
1433 | a2fa19f9 | aliguori | /* Add virtio console devices */
|
1434 | a2fa19f9 | aliguori | if (pci_enabled) {
|
1435 | a2fa19f9 | aliguori | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
1436 | 0e058a8a | Paul Brook | if (virtcon_hds[i]) {
|
1437 | caea79a9 | Mark McLoughlin | pci_create_simple(pci_bus, -1, "virtio-console-pci"); |
1438 | 0e058a8a | Paul Brook | } |
1439 | a2fa19f9 | aliguori | } |
1440 | a2fa19f9 | aliguori | } |
1441 | 80cabfad | bellard | } |
1442 | b5ff2d6e | bellard | |
1443 | fbe1b595 | Paul Brook | static void pc_init_pci(ram_addr_t ram_size, |
1444 | 3023f332 | aliguori | const char *boot_device, |
1445 | 5fafdf24 | ths | const char *kernel_filename, |
1446 | 3dbbdc25 | bellard | const char *kernel_cmdline, |
1447 | 94fc95cd | j_mayer | const char *initrd_filename, |
1448 | 94fc95cd | j_mayer | const char *cpu_model) |
1449 | 3dbbdc25 | bellard | { |
1450 | fbe1b595 | Paul Brook | pc_init1(ram_size, boot_device, |
1451 | 3dbbdc25 | bellard | kernel_filename, kernel_cmdline, |
1452 | caea79a9 | Mark McLoughlin | initrd_filename, cpu_model, 1);
|
1453 | 3dbbdc25 | bellard | } |
1454 | 3dbbdc25 | bellard | |
1455 | fbe1b595 | Paul Brook | static void pc_init_isa(ram_addr_t ram_size, |
1456 | 3023f332 | aliguori | const char *boot_device, |
1457 | 5fafdf24 | ths | const char *kernel_filename, |
1458 | 3dbbdc25 | bellard | const char *kernel_cmdline, |
1459 | 94fc95cd | j_mayer | const char *initrd_filename, |
1460 | 94fc95cd | j_mayer | const char *cpu_model) |
1461 | 3dbbdc25 | bellard | { |
1462 | fbe1b595 | Paul Brook | pc_init1(ram_size, boot_device, |
1463 | 3dbbdc25 | bellard | kernel_filename, kernel_cmdline, |
1464 | caea79a9 | Mark McLoughlin | initrd_filename, cpu_model, 0);
|
1465 | 3dbbdc25 | bellard | } |
1466 | 3dbbdc25 | bellard | |
1467 | 0bacd130 | aliguori | /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
|
1468 | 0bacd130 | aliguori | BIOS will read it and start S3 resume at POST Entry */
|
1469 | 0bacd130 | aliguori | void cmos_set_s3_resume(void) |
1470 | 0bacd130 | aliguori | { |
1471 | 0bacd130 | aliguori | if (rtc_state)
|
1472 | 0bacd130 | aliguori | rtc_set_memory(rtc_state, 0xF, 0xFE); |
1473 | 0bacd130 | aliguori | } |
1474 | 0bacd130 | aliguori | |
1475 | f80f9ec9 | Anthony Liguori | static QEMUMachine pc_machine = {
|
1476 | 95747581 | Mark McLoughlin | .name = "pc-0.11",
|
1477 | 95747581 | Mark McLoughlin | .alias = "pc",
|
1478 | a245f2e7 | aurel32 | .desc = "Standard PC",
|
1479 | a245f2e7 | aurel32 | .init = pc_init_pci, |
1480 | b2097003 | aliguori | .max_cpus = 255,
|
1481 | 0c257437 | Anthony Liguori | .is_default = 1,
|
1482 | 3dbbdc25 | bellard | }; |
1483 | 3dbbdc25 | bellard | |
1484 | 96cc1810 | Gerd Hoffmann | static QEMUMachine pc_machine_v0_10 = {
|
1485 | 96cc1810 | Gerd Hoffmann | .name = "pc-0.10",
|
1486 | 96cc1810 | Gerd Hoffmann | .desc = "Standard PC, qemu 0.10",
|
1487 | 96cc1810 | Gerd Hoffmann | .init = pc_init_pci, |
1488 | 96cc1810 | Gerd Hoffmann | .max_cpus = 255,
|
1489 | 96cc1810 | Gerd Hoffmann | .compat_props = (CompatProperty[]) { |
1490 | ab73ff29 | Gerd Hoffmann | { |
1491 | ab73ff29 | Gerd Hoffmann | .driver = "virtio-blk-pci",
|
1492 | ab73ff29 | Gerd Hoffmann | .property = "class",
|
1493 | ab73ff29 | Gerd Hoffmann | .value = stringify(PCI_CLASS_STORAGE_OTHER), |
1494 | d6beee99 | Gerd Hoffmann | },{ |
1495 | d6beee99 | Gerd Hoffmann | .driver = "virtio-console-pci",
|
1496 | d6beee99 | Gerd Hoffmann | .property = "class",
|
1497 | d6beee99 | Gerd Hoffmann | .value = stringify(PCI_CLASS_DISPLAY_OTHER), |
1498 | a1e0fea5 | Gerd Hoffmann | },{ |
1499 | a1e0fea5 | Gerd Hoffmann | .driver = "virtio-net-pci",
|
1500 | a1e0fea5 | Gerd Hoffmann | .property = "vectors",
|
1501 | a1e0fea5 | Gerd Hoffmann | .value = stringify(0),
|
1502 | 177539e0 | Gerd Hoffmann | },{ |
1503 | 177539e0 | Gerd Hoffmann | .driver = "virtio-blk-pci",
|
1504 | 177539e0 | Gerd Hoffmann | .property = "vectors",
|
1505 | 177539e0 | Gerd Hoffmann | .value = stringify(0),
|
1506 | ab73ff29 | Gerd Hoffmann | }, |
1507 | 96cc1810 | Gerd Hoffmann | { /* end of list */ }
|
1508 | 96cc1810 | Gerd Hoffmann | }, |
1509 | 96cc1810 | Gerd Hoffmann | }; |
1510 | 96cc1810 | Gerd Hoffmann | |
1511 | f80f9ec9 | Anthony Liguori | static QEMUMachine isapc_machine = {
|
1512 | a245f2e7 | aurel32 | .name = "isapc",
|
1513 | a245f2e7 | aurel32 | .desc = "ISA-only PC",
|
1514 | a245f2e7 | aurel32 | .init = pc_init_isa, |
1515 | b2097003 | aliguori | .max_cpus = 1,
|
1516 | b5ff2d6e | bellard | }; |
1517 | f80f9ec9 | Anthony Liguori | |
1518 | f80f9ec9 | Anthony Liguori | static void pc_machine_init(void) |
1519 | f80f9ec9 | Anthony Liguori | { |
1520 | f80f9ec9 | Anthony Liguori | qemu_register_machine(&pc_machine); |
1521 | 96cc1810 | Gerd Hoffmann | qemu_register_machine(&pc_machine_v0_10); |
1522 | f80f9ec9 | Anthony Liguori | qemu_register_machine(&isapc_machine); |
1523 | f80f9ec9 | Anthony Liguori | } |
1524 | f80f9ec9 | Anthony Liguori | |
1525 | f80f9ec9 | Anthony Liguori | machine_init(pc_machine_init); |