]>
Commit | Line | Data |
---|---|---|
80cabfad FB |
1 | /* |
2 | * QEMU PC System Emulator | |
5fafdf24 | 3 | * |
80cabfad | 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
5fafdf24 | 5 | * |
80cabfad FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
87ecb68b PB |
24 | #include "hw.h" |
25 | #include "pc.h" | |
26 | #include "fdc.h" | |
27 | #include "pci.h" | |
28 | #include "block.h" | |
29 | #include "sysemu.h" | |
30 | #include "audio/audio.h" | |
31 | #include "net.h" | |
32 | #include "smbus.h" | |
33 | #include "boards.h" | |
cfa2af1f | 34 | #include "console.h" |
3cce6243 | 35 | #include "fw_cfg.h" |
6e02c38d | 36 | #include "virtio-blk.h" |
80cabfad | 37 | |
b41a2cd1 FB |
38 | /* output Bochs bios info messages */ |
39 | //#define DEBUG_BIOS | |
40 | ||
80cabfad FB |
41 | #define BIOS_FILENAME "bios.bin" |
42 | #define VGABIOS_FILENAME "vgabios.bin" | |
de9258a8 | 43 | #define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin" |
80cabfad | 44 | |
7fb4fdcf AZ |
45 | #define PC_MAX_BIOS_SIZE (4 * 1024 * 1024) |
46 | ||
a80274c3 PB |
47 | /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables. */ |
48 | #define ACPI_DATA_SIZE 0x10000 | |
3cce6243 | 49 | #define BIOS_CFG_IOPORT 0x510 |
80cabfad | 50 | |
e4bcb14c TS |
51 | #define MAX_IDE_BUS 2 |
52 | ||
baca51fa | 53 | static fdctrl_t *floppy_controller; |
b0a21b53 | 54 | static RTCState *rtc_state; |
ec844b96 | 55 | static PITState *pit; |
d592d303 | 56 | static IOAPICState *ioapic; |
a5954d5c | 57 | static PCIDevice *i440fx_state; |
80cabfad | 58 | |
b41a2cd1 | 59 | static void ioport80_write(void *opaque, uint32_t addr, uint32_t data) |
80cabfad FB |
60 | { |
61 | } | |
62 | ||
f929aad6 | 63 | /* MSDOS compatibility mode FPU exception support */ |
d537cf6c | 64 | static qemu_irq ferr_irq; |
f929aad6 FB |
65 | /* XXX: add IGNNE support */ |
66 | void cpu_set_ferr(CPUX86State *s) | |
67 | { | |
d537cf6c | 68 | qemu_irq_raise(ferr_irq); |
f929aad6 FB |
69 | } |
70 | ||
71 | static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data) | |
72 | { | |
d537cf6c | 73 | qemu_irq_lower(ferr_irq); |
f929aad6 FB |
74 | } |
75 | ||
28ab0e2e | 76 | /* TSC handling */ |
28ab0e2e FB |
77 | uint64_t cpu_get_tsc(CPUX86State *env) |
78 | { | |
1dce7c3c FB |
79 | /* Note: when using kqemu, it is more logical to return the host TSC |
80 | because kqemu does not trap the RDTSC instruction for | |
81 | performance reasons */ | |
eb38c52c | 82 | #ifdef USE_KQEMU |
1dce7c3c FB |
83 | if (env->kqemu_enabled) { |
84 | return cpu_get_real_ticks(); | |
5fafdf24 | 85 | } else |
1dce7c3c FB |
86 | #endif |
87 | { | |
88 | return cpu_get_ticks(); | |
89 | } | |
28ab0e2e FB |
90 | } |
91 | ||
a5954d5c FB |
92 | /* SMM support */ |
93 | void cpu_smm_update(CPUState *env) | |
94 | { | |
95 | if (i440fx_state && env == first_cpu) | |
96 | i440fx_set_smm(i440fx_state, (env->hflags >> HF_SMM_SHIFT) & 1); | |
97 | } | |
98 | ||
99 | ||
3de388f6 FB |
100 | /* IRQ handling */ |
101 | int cpu_get_pic_interrupt(CPUState *env) | |
102 | { | |
103 | int intno; | |
104 | ||
3de388f6 FB |
105 | intno = apic_get_interrupt(env); |
106 | if (intno >= 0) { | |
107 | /* set irq request if a PIC irq is still pending */ | |
108 | /* XXX: improve that */ | |
5fafdf24 | 109 | pic_update_irq(isa_pic); |
3de388f6 FB |
110 | return intno; |
111 | } | |
3de388f6 | 112 | /* read the irq from the PIC */ |
0e21e12b TS |
113 | if (!apic_accept_pic_intr(env)) |
114 | return -1; | |
115 | ||
3de388f6 FB |
116 | intno = pic_read_irq(isa_pic); |
117 | return intno; | |
118 | } | |
119 | ||
d537cf6c | 120 | static void pic_irq_request(void *opaque, int irq, int level) |
3de388f6 | 121 | { |
a5b38b51 AJ |
122 | CPUState *env = first_cpu; |
123 | ||
d5529471 AJ |
124 | if (env->apic_state) { |
125 | while (env) { | |
126 | if (apic_accept_pic_intr(env)) | |
1a7de94a | 127 | apic_deliver_pic_intr(env, level); |
d5529471 AJ |
128 | env = env->next_cpu; |
129 | } | |
130 | } else { | |
b614106a AJ |
131 | if (level) |
132 | cpu_interrupt(env, CPU_INTERRUPT_HARD); | |
133 | else | |
134 | cpu_reset_interrupt(env, CPU_INTERRUPT_HARD); | |
a5b38b51 | 135 | } |
3de388f6 FB |
136 | } |
137 | ||
b0a21b53 FB |
138 | /* PC cmos mappings */ |
139 | ||
80cabfad FB |
140 | #define REG_EQUIPMENT_BYTE 0x14 |
141 | ||
777428f2 FB |
142 | static int cmos_get_fd_drive_type(int fd0) |
143 | { | |
144 | int val; | |
145 | ||
146 | switch (fd0) { | |
147 | case 0: | |
148 | /* 1.44 Mb 3"5 drive */ | |
149 | val = 4; | |
150 | break; | |
151 | case 1: | |
152 | /* 2.88 Mb 3"5 drive */ | |
153 | val = 5; | |
154 | break; | |
155 | case 2: | |
156 | /* 1.2 Mb 5"5 drive */ | |
157 | val = 2; | |
158 | break; | |
159 | default: | |
160 | val = 0; | |
161 | break; | |
162 | } | |
163 | return val; | |
164 | } | |
165 | ||
5fafdf24 | 166 | static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd) |
ba6c2377 FB |
167 | { |
168 | RTCState *s = rtc_state; | |
169 | int cylinders, heads, sectors; | |
170 | bdrv_get_geometry_hint(hd, &cylinders, &heads, §ors); | |
171 | rtc_set_memory(s, type_ofs, 47); | |
172 | rtc_set_memory(s, info_ofs, cylinders); | |
173 | rtc_set_memory(s, info_ofs + 1, cylinders >> 8); | |
174 | rtc_set_memory(s, info_ofs + 2, heads); | |
175 | rtc_set_memory(s, info_ofs + 3, 0xff); | |
176 | rtc_set_memory(s, info_ofs + 4, 0xff); | |
177 | rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3)); | |
178 | rtc_set_memory(s, info_ofs + 6, cylinders); | |
179 | rtc_set_memory(s, info_ofs + 7, cylinders >> 8); | |
180 | rtc_set_memory(s, info_ofs + 8, sectors); | |
181 | } | |
182 | ||
6ac0e82d AZ |
183 | /* convert boot_device letter to something recognizable by the bios */ |
184 | static int boot_device2nibble(char boot_device) | |
185 | { | |
186 | switch(boot_device) { | |
187 | case 'a': | |
188 | case 'b': | |
189 | return 0x01; /* floppy boot */ | |
190 | case 'c': | |
191 | return 0x02; /* hard drive boot */ | |
192 | case 'd': | |
193 | return 0x03; /* CD-ROM boot */ | |
194 | case 'n': | |
195 | return 0x04; /* Network boot */ | |
196 | } | |
197 | return 0; | |
198 | } | |
199 | ||
0ecdffbb AJ |
200 | /* copy/pasted from cmos_init, should be made a general function |
201 | and used there as well */ | |
3b4366de | 202 | static int pc_boot_set(void *opaque, const char *boot_device) |
0ecdffbb AJ |
203 | { |
204 | #define PC_MAX_BOOT_DEVICES 3 | |
3b4366de | 205 | RTCState *s = (RTCState *)opaque; |
0ecdffbb AJ |
206 | int nbds, bds[3] = { 0, }; |
207 | int i; | |
208 | ||
209 | nbds = strlen(boot_device); | |
210 | if (nbds > PC_MAX_BOOT_DEVICES) { | |
211 | term_printf("Too many boot devices for PC\n"); | |
212 | return(1); | |
213 | } | |
214 | for (i = 0; i < nbds; i++) { | |
215 | bds[i] = boot_device2nibble(boot_device[i]); | |
216 | if (bds[i] == 0) { | |
217 | term_printf("Invalid boot device for PC: '%c'\n", | |
218 | boot_device[i]); | |
219 | return(1); | |
220 | } | |
221 | } | |
222 | rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]); | |
223 | rtc_set_memory(s, 0x38, (bds[2] << 4)); | |
224 | return(0); | |
225 | } | |
226 | ||
ba6c2377 | 227 | /* hd_table must contain 4 block drivers */ |
00f82b8a AJ |
228 | static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size, |
229 | const char *boot_device, BlockDriverState **hd_table) | |
80cabfad | 230 | { |
b0a21b53 | 231 | RTCState *s = rtc_state; |
28c5af54 | 232 | int nbds, bds[3] = { 0, }; |
80cabfad | 233 | int val; |
b41a2cd1 | 234 | int fd0, fd1, nb; |
ba6c2377 | 235 | int i; |
b0a21b53 | 236 | |
b0a21b53 | 237 | /* various important CMOS locations needed by PC/Bochs bios */ |
80cabfad FB |
238 | |
239 | /* memory size */ | |
333190eb FB |
240 | val = 640; /* base memory in K */ |
241 | rtc_set_memory(s, 0x15, val); | |
242 | rtc_set_memory(s, 0x16, val >> 8); | |
243 | ||
80cabfad FB |
244 | val = (ram_size / 1024) - 1024; |
245 | if (val > 65535) | |
246 | val = 65535; | |
b0a21b53 FB |
247 | rtc_set_memory(s, 0x17, val); |
248 | rtc_set_memory(s, 0x18, val >> 8); | |
249 | rtc_set_memory(s, 0x30, val); | |
250 | rtc_set_memory(s, 0x31, val >> 8); | |
80cabfad | 251 | |
00f82b8a AJ |
252 | if (above_4g_mem_size) { |
253 | rtc_set_memory(s, 0x5b, (unsigned int)above_4g_mem_size >> 16); | |
254 | rtc_set_memory(s, 0x5c, (unsigned int)above_4g_mem_size >> 24); | |
255 | rtc_set_memory(s, 0x5d, (uint64_t)above_4g_mem_size >> 32); | |
256 | } | |
257 | ||
9da98861 FB |
258 | if (ram_size > (16 * 1024 * 1024)) |
259 | val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536); | |
260 | else | |
261 | val = 0; | |
80cabfad FB |
262 | if (val > 65535) |
263 | val = 65535; | |
b0a21b53 FB |
264 | rtc_set_memory(s, 0x34, val); |
265 | rtc_set_memory(s, 0x35, val >> 8); | |
3b46e624 | 266 | |
298e01b6 AJ |
267 | /* set the number of CPU */ |
268 | rtc_set_memory(s, 0x5f, smp_cpus - 1); | |
269 | ||
6ac0e82d | 270 | /* set boot devices, and disable floppy signature check if requested */ |
28c5af54 JM |
271 | #define PC_MAX_BOOT_DEVICES 3 |
272 | nbds = strlen(boot_device); | |
273 | if (nbds > PC_MAX_BOOT_DEVICES) { | |
274 | fprintf(stderr, "Too many boot devices for PC\n"); | |
275 | exit(1); | |
276 | } | |
277 | for (i = 0; i < nbds; i++) { | |
278 | bds[i] = boot_device2nibble(boot_device[i]); | |
279 | if (bds[i] == 0) { | |
280 | fprintf(stderr, "Invalid boot device for PC: '%c'\n", | |
281 | boot_device[i]); | |
282 | exit(1); | |
283 | } | |
284 | } | |
285 | rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]); | |
286 | rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1)); | |
80cabfad | 287 | |
b41a2cd1 FB |
288 | /* floppy type */ |
289 | ||
baca51fa FB |
290 | fd0 = fdctrl_get_drive_type(floppy_controller, 0); |
291 | fd1 = fdctrl_get_drive_type(floppy_controller, 1); | |
80cabfad | 292 | |
777428f2 | 293 | val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1); |
b0a21b53 | 294 | rtc_set_memory(s, 0x10, val); |
3b46e624 | 295 | |
b0a21b53 | 296 | val = 0; |
b41a2cd1 | 297 | nb = 0; |
80cabfad FB |
298 | if (fd0 < 3) |
299 | nb++; | |
300 | if (fd1 < 3) | |
301 | nb++; | |
302 | switch (nb) { | |
303 | case 0: | |
304 | break; | |
305 | case 1: | |
b0a21b53 | 306 | val |= 0x01; /* 1 drive, ready for boot */ |
80cabfad FB |
307 | break; |
308 | case 2: | |
b0a21b53 | 309 | val |= 0x41; /* 2 drives, ready for boot */ |
80cabfad FB |
310 | break; |
311 | } | |
b0a21b53 FB |
312 | val |= 0x02; /* FPU is there */ |
313 | val |= 0x04; /* PS/2 mouse installed */ | |
314 | rtc_set_memory(s, REG_EQUIPMENT_BYTE, val); | |
315 | ||
ba6c2377 FB |
316 | /* hard drives */ |
317 | ||
318 | rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0)); | |
319 | if (hd_table[0]) | |
320 | cmos_init_hd(0x19, 0x1b, hd_table[0]); | |
5fafdf24 | 321 | if (hd_table[1]) |
ba6c2377 FB |
322 | cmos_init_hd(0x1a, 0x24, hd_table[1]); |
323 | ||
324 | val = 0; | |
40b6ecc6 | 325 | for (i = 0; i < 4; i++) { |
ba6c2377 | 326 | if (hd_table[i]) { |
46d4767d FB |
327 | int cylinders, heads, sectors, translation; |
328 | /* NOTE: bdrv_get_geometry_hint() returns the physical | |
329 | geometry. It is always such that: 1 <= sects <= 63, 1 | |
330 | <= heads <= 16, 1 <= cylinders <= 16383. The BIOS | |
331 | geometry can be different if a translation is done. */ | |
332 | translation = bdrv_get_translation_hint(hd_table[i]); | |
333 | if (translation == BIOS_ATA_TRANSLATION_AUTO) { | |
334 | bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, §ors); | |
335 | if (cylinders <= 1024 && heads <= 16 && sectors <= 63) { | |
336 | /* No translation. */ | |
337 | translation = 0; | |
338 | } else { | |
339 | /* LBA translation. */ | |
340 | translation = 1; | |
341 | } | |
40b6ecc6 | 342 | } else { |
46d4767d | 343 | translation--; |
ba6c2377 | 344 | } |
ba6c2377 FB |
345 | val |= translation << (i * 2); |
346 | } | |
40b6ecc6 | 347 | } |
ba6c2377 | 348 | rtc_set_memory(s, 0x39, val); |
80cabfad FB |
349 | } |
350 | ||
59b8ad81 FB |
351 | void ioport_set_a20(int enable) |
352 | { | |
353 | /* XXX: send to all CPUs ? */ | |
354 | cpu_x86_set_a20(first_cpu, enable); | |
355 | } | |
356 | ||
357 | int ioport_get_a20(void) | |
358 | { | |
359 | return ((first_cpu->a20_mask >> 20) & 1); | |
360 | } | |
361 | ||
e1a23744 FB |
362 | static void ioport92_write(void *opaque, uint32_t addr, uint32_t val) |
363 | { | |
59b8ad81 | 364 | ioport_set_a20((val >> 1) & 1); |
e1a23744 FB |
365 | /* XXX: bit 0 is fast reset */ |
366 | } | |
367 | ||
368 | static uint32_t ioport92_read(void *opaque, uint32_t addr) | |
369 | { | |
59b8ad81 | 370 | return ioport_get_a20() << 1; |
e1a23744 FB |
371 | } |
372 | ||
80cabfad FB |
373 | /***********************************************************/ |
374 | /* Bochs BIOS debug ports */ | |
375 | ||
9596ebb7 | 376 | static void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val) |
80cabfad | 377 | { |
a2f659ee FB |
378 | static const char shutdown_str[8] = "Shutdown"; |
379 | static int shutdown_index = 0; | |
3b46e624 | 380 | |
80cabfad FB |
381 | switch(addr) { |
382 | /* Bochs BIOS messages */ | |
383 | case 0x400: | |
384 | case 0x401: | |
385 | fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val); | |
386 | exit(1); | |
387 | case 0x402: | |
388 | case 0x403: | |
389 | #ifdef DEBUG_BIOS | |
390 | fprintf(stderr, "%c", val); | |
391 | #endif | |
392 | break; | |
a2f659ee FB |
393 | case 0x8900: |
394 | /* same as Bochs power off */ | |
395 | if (val == shutdown_str[shutdown_index]) { | |
396 | shutdown_index++; | |
397 | if (shutdown_index == 8) { | |
398 | shutdown_index = 0; | |
399 | qemu_system_shutdown_request(); | |
400 | } | |
401 | } else { | |
402 | shutdown_index = 0; | |
403 | } | |
404 | break; | |
80cabfad FB |
405 | |
406 | /* LGPL'ed VGA BIOS messages */ | |
407 | case 0x501: | |
408 | case 0x502: | |
409 | fprintf(stderr, "VGA BIOS panic, line %d\n", val); | |
410 | exit(1); | |
411 | case 0x500: | |
412 | case 0x503: | |
413 | #ifdef DEBUG_BIOS | |
414 | fprintf(stderr, "%c", val); | |
415 | #endif | |
416 | break; | |
417 | } | |
418 | } | |
419 | ||
9596ebb7 | 420 | static void bochs_bios_init(void) |
80cabfad | 421 | { |
3cce6243 BS |
422 | void *fw_cfg; |
423 | ||
b41a2cd1 FB |
424 | register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL); |
425 | register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL); | |
426 | register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL); | |
427 | register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL); | |
a2f659ee | 428 | register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL); |
b41a2cd1 FB |
429 | |
430 | register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL); | |
431 | register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL); | |
432 | register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL); | |
433 | register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL); | |
3cce6243 BS |
434 | |
435 | fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0); | |
436 | fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1); | |
905fdcb5 | 437 | fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size); |
80cabfad FB |
438 | } |
439 | ||
642a4f96 TS |
440 | /* Generate an initial boot sector which sets state and jump to |
441 | a specified vector */ | |
4fc9af53 AL |
442 | static void generate_bootsect(uint8_t *option_rom, |
443 | uint32_t gpr[8], uint16_t segs[6], uint16_t ip) | |
642a4f96 | 444 | { |
4fc9af53 AL |
445 | uint8_t rom[512], *p, *reloc; |
446 | uint8_t sum; | |
642a4f96 TS |
447 | int i; |
448 | ||
4fc9af53 AL |
449 | memset(rom, 0, sizeof(rom)); |
450 | ||
451 | p = rom; | |
452 | /* Make sure we have an option rom signature */ | |
453 | *p++ = 0x55; | |
454 | *p++ = 0xaa; | |
642a4f96 | 455 | |
4fc9af53 AL |
456 | /* ROM size in sectors*/ |
457 | *p++ = 1; | |
642a4f96 | 458 | |
4fc9af53 | 459 | /* Hook int19 */ |
642a4f96 | 460 | |
4fc9af53 AL |
461 | *p++ = 0x50; /* push ax */ |
462 | *p++ = 0x1e; /* push ds */ | |
463 | *p++ = 0x31; *p++ = 0xc0; /* xor ax, ax */ | |
464 | *p++ = 0x8e; *p++ = 0xd8; /* mov ax, ds */ | |
642a4f96 | 465 | |
4fc9af53 AL |
466 | *p++ = 0xc7; *p++ = 0x06; /* movvw _start,0x64 */ |
467 | *p++ = 0x64; *p++ = 0x00; | |
468 | reloc = p; | |
469 | *p++ = 0x00; *p++ = 0x00; | |
470 | ||
471 | *p++ = 0x8c; *p++ = 0x0e; /* mov cs,0x66 */ | |
472 | *p++ = 0x66; *p++ = 0x00; | |
473 | ||
474 | *p++ = 0x1f; /* pop ds */ | |
475 | *p++ = 0x58; /* pop ax */ | |
476 | *p++ = 0xcb; /* lret */ | |
477 | ||
642a4f96 | 478 | /* Actual code */ |
4fc9af53 AL |
479 | *reloc = (p - rom); |
480 | ||
642a4f96 TS |
481 | *p++ = 0xfa; /* CLI */ |
482 | *p++ = 0xfc; /* CLD */ | |
483 | ||
484 | for (i = 0; i < 6; i++) { | |
485 | if (i == 1) /* Skip CS */ | |
486 | continue; | |
487 | ||
488 | *p++ = 0xb8; /* MOV AX,imm16 */ | |
489 | *p++ = segs[i]; | |
490 | *p++ = segs[i] >> 8; | |
491 | *p++ = 0x8e; /* MOV <seg>,AX */ | |
492 | *p++ = 0xc0 + (i << 3); | |
493 | } | |
494 | ||
495 | for (i = 0; i < 8; i++) { | |
496 | *p++ = 0x66; /* 32-bit operand size */ | |
497 | *p++ = 0xb8 + i; /* MOV <reg>,imm32 */ | |
498 | *p++ = gpr[i]; | |
499 | *p++ = gpr[i] >> 8; | |
500 | *p++ = gpr[i] >> 16; | |
501 | *p++ = gpr[i] >> 24; | |
502 | } | |
503 | ||
504 | *p++ = 0xea; /* JMP FAR */ | |
505 | *p++ = ip; /* IP */ | |
506 | *p++ = ip >> 8; | |
507 | *p++ = segs[1]; /* CS */ | |
508 | *p++ = segs[1] >> 8; | |
509 | ||
4fc9af53 AL |
510 | /* sign rom */ |
511 | sum = 0; | |
512 | for (i = 0; i < (sizeof(rom) - 1); i++) | |
513 | sum += rom[i]; | |
514 | rom[sizeof(rom) - 1] = -sum; | |
515 | ||
516 | memcpy(option_rom, rom, sizeof(rom)); | |
642a4f96 | 517 | } |
80cabfad | 518 | |
642a4f96 TS |
519 | static long get_file_size(FILE *f) |
520 | { | |
521 | long where, size; | |
522 | ||
523 | /* XXX: on Unix systems, using fstat() probably makes more sense */ | |
524 | ||
525 | where = ftell(f); | |
526 | fseek(f, 0, SEEK_END); | |
527 | size = ftell(f); | |
528 | fseek(f, where, SEEK_SET); | |
529 | ||
530 | return size; | |
531 | } | |
532 | ||
4fc9af53 AL |
533 | static void load_linux(uint8_t *option_rom, |
534 | const char *kernel_filename, | |
642a4f96 TS |
535 | const char *initrd_filename, |
536 | const char *kernel_cmdline) | |
537 | { | |
538 | uint16_t protocol; | |
539 | uint32_t gpr[8]; | |
540 | uint16_t seg[6]; | |
541 | uint16_t real_seg; | |
542 | int setup_size, kernel_size, initrd_size, cmdline_size; | |
543 | uint32_t initrd_max; | |
544 | uint8_t header[1024]; | |
a37af289 | 545 | target_phys_addr_t real_addr, prot_addr, cmdline_addr, initrd_addr; |
642a4f96 TS |
546 | FILE *f, *fi; |
547 | ||
548 | /* Align to 16 bytes as a paranoia measure */ | |
549 | cmdline_size = (strlen(kernel_cmdline)+16) & ~15; | |
550 | ||
551 | /* load the kernel header */ | |
552 | f = fopen(kernel_filename, "rb"); | |
553 | if (!f || !(kernel_size = get_file_size(f)) || | |
554 | fread(header, 1, 1024, f) != 1024) { | |
555 | fprintf(stderr, "qemu: could not load kernel '%s'\n", | |
556 | kernel_filename); | |
557 | exit(1); | |
558 | } | |
559 | ||
560 | /* kernel protocol version */ | |
bc4edd79 | 561 | #if 0 |
642a4f96 | 562 | fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202)); |
bc4edd79 | 563 | #endif |
642a4f96 TS |
564 | if (ldl_p(header+0x202) == 0x53726448) |
565 | protocol = lduw_p(header+0x206); | |
566 | else | |
567 | protocol = 0; | |
568 | ||
569 | if (protocol < 0x200 || !(header[0x211] & 0x01)) { | |
570 | /* Low kernel */ | |
a37af289 BS |
571 | real_addr = 0x90000; |
572 | cmdline_addr = 0x9a000 - cmdline_size; | |
573 | prot_addr = 0x10000; | |
642a4f96 TS |
574 | } else if (protocol < 0x202) { |
575 | /* High but ancient kernel */ | |
a37af289 BS |
576 | real_addr = 0x90000; |
577 | cmdline_addr = 0x9a000 - cmdline_size; | |
578 | prot_addr = 0x100000; | |
642a4f96 TS |
579 | } else { |
580 | /* High and recent kernel */ | |
a37af289 BS |
581 | real_addr = 0x10000; |
582 | cmdline_addr = 0x20000; | |
583 | prot_addr = 0x100000; | |
642a4f96 TS |
584 | } |
585 | ||
bc4edd79 | 586 | #if 0 |
642a4f96 | 587 | fprintf(stderr, |
526ccb7a AZ |
588 | "qemu: real_addr = 0x" TARGET_FMT_plx "\n" |
589 | "qemu: cmdline_addr = 0x" TARGET_FMT_plx "\n" | |
590 | "qemu: prot_addr = 0x" TARGET_FMT_plx "\n", | |
a37af289 BS |
591 | real_addr, |
592 | cmdline_addr, | |
593 | prot_addr); | |
bc4edd79 | 594 | #endif |
642a4f96 TS |
595 | |
596 | /* highest address for loading the initrd */ | |
597 | if (protocol >= 0x203) | |
598 | initrd_max = ldl_p(header+0x22c); | |
599 | else | |
600 | initrd_max = 0x37ffffff; | |
601 | ||
602 | if (initrd_max >= ram_size-ACPI_DATA_SIZE) | |
603 | initrd_max = ram_size-ACPI_DATA_SIZE-1; | |
604 | ||
605 | /* kernel command line */ | |
a37af289 | 606 | pstrcpy_targphys(cmdline_addr, 4096, kernel_cmdline); |
642a4f96 TS |
607 | |
608 | if (protocol >= 0x202) { | |
a37af289 | 609 | stl_p(header+0x228, cmdline_addr); |
642a4f96 TS |
610 | } else { |
611 | stw_p(header+0x20, 0xA33F); | |
612 | stw_p(header+0x22, cmdline_addr-real_addr); | |
613 | } | |
614 | ||
615 | /* loader type */ | |
616 | /* High nybble = B reserved for Qemu; low nybble is revision number. | |
617 | If this code is substantially changed, you may want to consider | |
618 | incrementing the revision. */ | |
619 | if (protocol >= 0x200) | |
620 | header[0x210] = 0xB0; | |
621 | ||
622 | /* heap */ | |
623 | if (protocol >= 0x201) { | |
624 | header[0x211] |= 0x80; /* CAN_USE_HEAP */ | |
625 | stw_p(header+0x224, cmdline_addr-real_addr-0x200); | |
626 | } | |
627 | ||
628 | /* load initrd */ | |
629 | if (initrd_filename) { | |
630 | if (protocol < 0x200) { | |
631 | fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n"); | |
632 | exit(1); | |
633 | } | |
634 | ||
635 | fi = fopen(initrd_filename, "rb"); | |
636 | if (!fi) { | |
637 | fprintf(stderr, "qemu: could not load initial ram disk '%s'\n", | |
638 | initrd_filename); | |
639 | exit(1); | |
640 | } | |
641 | ||
642 | initrd_size = get_file_size(fi); | |
a37af289 | 643 | initrd_addr = (initrd_max-initrd_size) & ~4095; |
642a4f96 | 644 | |
526ccb7a AZ |
645 | fprintf(stderr, "qemu: loading initrd (%#x bytes) at 0x" TARGET_FMT_plx |
646 | "\n", initrd_size, initrd_addr); | |
642a4f96 | 647 | |
a37af289 | 648 | if (!fread_targphys_ok(initrd_addr, initrd_size, fi)) { |
642a4f96 TS |
649 | fprintf(stderr, "qemu: read error on initial ram disk '%s'\n", |
650 | initrd_filename); | |
651 | exit(1); | |
652 | } | |
653 | fclose(fi); | |
654 | ||
a37af289 | 655 | stl_p(header+0x218, initrd_addr); |
642a4f96 TS |
656 | stl_p(header+0x21c, initrd_size); |
657 | } | |
658 | ||
659 | /* store the finalized header and load the rest of the kernel */ | |
a37af289 | 660 | cpu_physical_memory_write(real_addr, header, 1024); |
642a4f96 TS |
661 | |
662 | setup_size = header[0x1f1]; | |
663 | if (setup_size == 0) | |
664 | setup_size = 4; | |
665 | ||
666 | setup_size = (setup_size+1)*512; | |
667 | kernel_size -= setup_size; /* Size of protected-mode code */ | |
668 | ||
a37af289 BS |
669 | if (!fread_targphys_ok(real_addr+1024, setup_size-1024, f) || |
670 | !fread_targphys_ok(prot_addr, kernel_size, f)) { | |
642a4f96 TS |
671 | fprintf(stderr, "qemu: read error on kernel '%s'\n", |
672 | kernel_filename); | |
673 | exit(1); | |
674 | } | |
675 | fclose(f); | |
676 | ||
677 | /* generate bootsector to set up the initial register state */ | |
a37af289 | 678 | real_seg = real_addr >> 4; |
642a4f96 TS |
679 | seg[0] = seg[2] = seg[3] = seg[4] = seg[4] = real_seg; |
680 | seg[1] = real_seg+0x20; /* CS */ | |
681 | memset(gpr, 0, sizeof gpr); | |
682 | gpr[4] = cmdline_addr-real_addr-16; /* SP (-16 is paranoia) */ | |
683 | ||
4fc9af53 | 684 | generate_bootsect(option_rom, gpr, seg, 0); |
642a4f96 TS |
685 | } |
686 | ||
59b8ad81 FB |
687 | static void main_cpu_reset(void *opaque) |
688 | { | |
689 | CPUState *env = opaque; | |
690 | cpu_reset(env); | |
691 | } | |
692 | ||
b41a2cd1 FB |
693 | static const int ide_iobase[2] = { 0x1f0, 0x170 }; |
694 | static const int ide_iobase2[2] = { 0x3f6, 0x376 }; | |
695 | static const int ide_irq[2] = { 14, 15 }; | |
696 | ||
697 | #define NE2000_NB_MAX 6 | |
698 | ||
8d11df9e | 699 | static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 }; |
b41a2cd1 FB |
700 | static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 }; |
701 | ||
8d11df9e FB |
702 | static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 }; |
703 | static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 }; | |
704 | ||
6508fe59 FB |
705 | static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc }; |
706 | static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 }; | |
707 | ||
6a36d84e | 708 | #ifdef HAS_AUDIO |
d537cf6c | 709 | static void audio_init (PCIBus *pci_bus, qemu_irq *pic) |
6a36d84e FB |
710 | { |
711 | struct soundhw *c; | |
712 | int audio_enabled = 0; | |
713 | ||
714 | for (c = soundhw; !audio_enabled && c->name; ++c) { | |
715 | audio_enabled = c->enabled; | |
716 | } | |
717 | ||
718 | if (audio_enabled) { | |
719 | AudioState *s; | |
720 | ||
721 | s = AUD_init (); | |
722 | if (s) { | |
723 | for (c = soundhw; c->name; ++c) { | |
724 | if (c->enabled) { | |
725 | if (c->isa) { | |
d537cf6c | 726 | c->init.init_isa (s, pic); |
6a36d84e FB |
727 | } |
728 | else { | |
729 | if (pci_bus) { | |
730 | c->init.init_pci (pci_bus, s); | |
731 | } | |
732 | } | |
733 | } | |
734 | } | |
735 | } | |
736 | } | |
737 | } | |
738 | #endif | |
739 | ||
d537cf6c | 740 | static void pc_init_ne2k_isa(NICInfo *nd, qemu_irq *pic) |
a41b2ff2 PB |
741 | { |
742 | static int nb_ne2k = 0; | |
743 | ||
744 | if (nb_ne2k == NE2000_NB_MAX) | |
745 | return; | |
d537cf6c | 746 | isa_ne2000_init(ne2000_io[nb_ne2k], pic[ne2000_irq[nb_ne2k]], nd); |
a41b2ff2 PB |
747 | nb_ne2k++; |
748 | } | |
749 | ||
80cabfad | 750 | /* PC hardware initialisation */ |
00f82b8a | 751 | static void pc_init1(ram_addr_t ram_size, int vga_ram_size, |
b881c2c6 | 752 | const char *boot_device, DisplayState *ds, |
b5ff2d6e | 753 | const char *kernel_filename, const char *kernel_cmdline, |
3dbbdc25 | 754 | const char *initrd_filename, |
a049de61 | 755 | int pci_enabled, const char *cpu_model) |
80cabfad FB |
756 | { |
757 | char buf[1024]; | |
642a4f96 | 758 | int ret, linux_boot, i; |
970ac5a3 | 759 | ram_addr_t ram_addr, vga_ram_addr, bios_offset, vga_bios_offset; |
00f82b8a | 760 | ram_addr_t below_4g_mem_size, above_4g_mem_size = 0; |
970ac5a3 | 761 | int bios_size, isa_bios_size, vga_bios_size; |
46e50e9d | 762 | PCIBus *pci_bus; |
5c3ff3a7 | 763 | int piix3_devfn = -1; |
59b8ad81 | 764 | CPUState *env; |
a41b2ff2 | 765 | NICInfo *nd; |
d537cf6c PB |
766 | qemu_irq *cpu_irq; |
767 | qemu_irq *i8259; | |
e4bcb14c TS |
768 | int index; |
769 | BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS]; | |
770 | BlockDriverState *fd[MAX_FD]; | |
d592d303 | 771 | |
00f82b8a AJ |
772 | if (ram_size >= 0xe0000000 ) { |
773 | above_4g_mem_size = ram_size - 0xe0000000; | |
774 | below_4g_mem_size = 0xe0000000; | |
775 | } else { | |
776 | below_4g_mem_size = ram_size; | |
777 | } | |
778 | ||
80cabfad FB |
779 | linux_boot = (kernel_filename != NULL); |
780 | ||
59b8ad81 | 781 | /* init CPUs */ |
a049de61 FB |
782 | if (cpu_model == NULL) { |
783 | #ifdef TARGET_X86_64 | |
784 | cpu_model = "qemu64"; | |
785 | #else | |
786 | cpu_model = "qemu32"; | |
787 | #endif | |
788 | } | |
789 | ||
59b8ad81 | 790 | for(i = 0; i < smp_cpus; i++) { |
aaed909a FB |
791 | env = cpu_init(cpu_model); |
792 | if (!env) { | |
793 | fprintf(stderr, "Unable to find x86 CPU definition\n"); | |
794 | exit(1); | |
795 | } | |
59b8ad81 | 796 | if (i != 0) |
ce5232c5 | 797 | env->halted = 1; |
59b8ad81 FB |
798 | if (smp_cpus > 1) { |
799 | /* XXX: enable it in all cases */ | |
800 | env->cpuid_features |= CPUID_APIC; | |
801 | } | |
59b8ad81 FB |
802 | qemu_register_reset(main_cpu_reset, env); |
803 | if (pci_enabled) { | |
804 | apic_init(env); | |
805 | } | |
806 | } | |
807 | ||
26fb5e48 AJ |
808 | vmport_init(); |
809 | ||
80cabfad | 810 | /* allocate RAM */ |
82b36dc3 AL |
811 | ram_addr = qemu_ram_alloc(0xa0000); |
812 | cpu_register_physical_memory(0, 0xa0000, ram_addr); | |
813 | ||
814 | /* Allocate, even though we won't register, so we don't break the | |
815 | * phys_ram_base + PA assumption. This range includes vga (0xa0000 - 0xc0000), | |
816 | * and some bios areas, which will be registered later | |
817 | */ | |
818 | ram_addr = qemu_ram_alloc(0x100000 - 0xa0000); | |
819 | ram_addr = qemu_ram_alloc(below_4g_mem_size - 0x100000); | |
820 | cpu_register_physical_memory(0x100000, | |
821 | below_4g_mem_size - 0x100000, | |
822 | ram_addr); | |
00f82b8a AJ |
823 | |
824 | /* above 4giga memory allocation */ | |
825 | if (above_4g_mem_size > 0) { | |
82b36dc3 AL |
826 | ram_addr = qemu_ram_alloc(above_4g_mem_size); |
827 | cpu_register_physical_memory(0x100000000ULL, | |
526ccb7a | 828 | above_4g_mem_size, |
82b36dc3 | 829 | ram_addr); |
00f82b8a | 830 | } |
80cabfad | 831 | |
82b36dc3 | 832 | |
970ac5a3 FB |
833 | /* allocate VGA RAM */ |
834 | vga_ram_addr = qemu_ram_alloc(vga_ram_size); | |
7587cf44 | 835 | |
970ac5a3 | 836 | /* BIOS load */ |
1192dad8 JM |
837 | if (bios_name == NULL) |
838 | bios_name = BIOS_FILENAME; | |
839 | snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name); | |
7587cf44 | 840 | bios_size = get_image_size(buf); |
5fafdf24 | 841 | if (bios_size <= 0 || |
970ac5a3 | 842 | (bios_size % 65536) != 0) { |
7587cf44 FB |
843 | goto bios_error; |
844 | } | |
970ac5a3 | 845 | bios_offset = qemu_ram_alloc(bios_size); |
7587cf44 FB |
846 | ret = load_image(buf, phys_ram_base + bios_offset); |
847 | if (ret != bios_size) { | |
848 | bios_error: | |
970ac5a3 | 849 | fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", buf); |
80cabfad FB |
850 | exit(1); |
851 | } | |
7587cf44 | 852 | |
80cabfad | 853 | /* VGA BIOS load */ |
de9258a8 FB |
854 | if (cirrus_vga_enabled) { |
855 | snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_CIRRUS_FILENAME); | |
856 | } else { | |
857 | snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME); | |
858 | } | |
970ac5a3 | 859 | vga_bios_size = get_image_size(buf); |
5fafdf24 | 860 | if (vga_bios_size <= 0 || vga_bios_size > 65536) |
970ac5a3 FB |
861 | goto vga_bios_error; |
862 | vga_bios_offset = qemu_ram_alloc(65536); | |
863 | ||
7587cf44 | 864 | ret = load_image(buf, phys_ram_base + vga_bios_offset); |
970ac5a3 FB |
865 | if (ret != vga_bios_size) { |
866 | vga_bios_error: | |
867 | fprintf(stderr, "qemu: could not load VGA BIOS '%s'\n", buf); | |
868 | exit(1); | |
869 | } | |
870 | ||
80cabfad | 871 | /* setup basic memory access */ |
5fafdf24 | 872 | cpu_register_physical_memory(0xc0000, 0x10000, |
7587cf44 FB |
873 | vga_bios_offset | IO_MEM_ROM); |
874 | ||
875 | /* map the last 128KB of the BIOS in ISA space */ | |
876 | isa_bios_size = bios_size; | |
877 | if (isa_bios_size > (128 * 1024)) | |
878 | isa_bios_size = 128 * 1024; | |
5fafdf24 TS |
879 | cpu_register_physical_memory(0x100000 - isa_bios_size, |
880 | isa_bios_size, | |
7587cf44 | 881 | (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM); |
9ae02555 | 882 | |
970ac5a3 FB |
883 | { |
884 | ram_addr_t option_rom_offset; | |
885 | int size, offset; | |
886 | ||
887 | offset = 0; | |
4fc9af53 AL |
888 | if (linux_boot) { |
889 | option_rom_offset = qemu_ram_alloc(TARGET_PAGE_SIZE); | |
890 | load_linux(phys_ram_base + option_rom_offset, | |
891 | kernel_filename, initrd_filename, kernel_cmdline); | |
892 | cpu_register_physical_memory(0xd0000, TARGET_PAGE_SIZE, | |
893 | option_rom_offset | IO_MEM_ROM); | |
894 | offset = TARGET_PAGE_SIZE; | |
895 | } | |
896 | ||
970ac5a3 FB |
897 | for (i = 0; i < nb_option_roms; i++) { |
898 | size = get_image_size(option_rom[i]); | |
899 | if (size < 0) { | |
5fafdf24 | 900 | fprintf(stderr, "Could not load option rom '%s'\n", |
970ac5a3 FB |
901 | option_rom[i]); |
902 | exit(1); | |
903 | } | |
904 | if (size > (0x10000 - offset)) | |
905 | goto option_rom_error; | |
906 | option_rom_offset = qemu_ram_alloc(size); | |
907 | ret = load_image(option_rom[i], phys_ram_base + option_rom_offset); | |
908 | if (ret != size) { | |
909 | option_rom_error: | |
910 | fprintf(stderr, "Too many option ROMS\n"); | |
911 | exit(1); | |
912 | } | |
913 | size = (size + 4095) & ~4095; | |
914 | cpu_register_physical_memory(0xd0000 + offset, | |
915 | size, option_rom_offset | IO_MEM_ROM); | |
916 | offset += size; | |
917 | } | |
9ae02555 TS |
918 | } |
919 | ||
7587cf44 | 920 | /* map all the bios at the top of memory */ |
5fafdf24 | 921 | cpu_register_physical_memory((uint32_t)(-bios_size), |
7587cf44 | 922 | bios_size, bios_offset | IO_MEM_ROM); |
3b46e624 | 923 | |
80cabfad FB |
924 | bochs_bios_init(); |
925 | ||
a5b38b51 | 926 | cpu_irq = qemu_allocate_irqs(pic_irq_request, NULL, 1); |
d537cf6c PB |
927 | i8259 = i8259_init(cpu_irq[0]); |
928 | ferr_irq = i8259[13]; | |
929 | ||
69b91039 | 930 | if (pci_enabled) { |
d537cf6c | 931 | pci_bus = i440fx_init(&i440fx_state, i8259); |
8f1c91d8 | 932 | piix3_devfn = piix3_init(pci_bus, -1); |
46e50e9d FB |
933 | } else { |
934 | pci_bus = NULL; | |
69b91039 FB |
935 | } |
936 | ||
80cabfad | 937 | /* init basic PC hardware */ |
b41a2cd1 | 938 | register_ioport_write(0x80, 1, 1, ioport80_write, NULL); |
80cabfad | 939 | |
f929aad6 FB |
940 | register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL); |
941 | ||
1f04275e FB |
942 | if (cirrus_vga_enabled) { |
943 | if (pci_enabled) { | |
5fafdf24 TS |
944 | pci_cirrus_vga_init(pci_bus, |
945 | ds, phys_ram_base + vga_ram_addr, | |
970ac5a3 | 946 | vga_ram_addr, vga_ram_size); |
1f04275e | 947 | } else { |
5fafdf24 | 948 | isa_cirrus_vga_init(ds, phys_ram_base + vga_ram_addr, |
970ac5a3 | 949 | vga_ram_addr, vga_ram_size); |
1f04275e | 950 | } |
d34cab9f TS |
951 | } else if (vmsvga_enabled) { |
952 | if (pci_enabled) | |
45e4522e AZ |
953 | pci_vmsvga_init(pci_bus, ds, phys_ram_base + vga_ram_addr, |
954 | vga_ram_addr, vga_ram_size); | |
d34cab9f TS |
955 | else |
956 | fprintf(stderr, "%s: vmware_vga: no PCI bus\n", __FUNCTION__); | |
1f04275e | 957 | } else { |
89b6b508 | 958 | if (pci_enabled) { |
5fafdf24 | 959 | pci_vga_init(pci_bus, ds, phys_ram_base + vga_ram_addr, |
970ac5a3 | 960 | vga_ram_addr, vga_ram_size, 0, 0); |
89b6b508 | 961 | } else { |
5fafdf24 | 962 | isa_vga_init(ds, phys_ram_base + vga_ram_addr, |
970ac5a3 | 963 | vga_ram_addr, vga_ram_size); |
89b6b508 | 964 | } |
1f04275e | 965 | } |
80cabfad | 966 | |
d537cf6c | 967 | rtc_state = rtc_init(0x70, i8259[8]); |
80cabfad | 968 | |
3b4366de BS |
969 | qemu_register_boot_set(pc_boot_set, rtc_state); |
970 | ||
e1a23744 FB |
971 | register_ioport_read(0x92, 1, 1, ioport92_read, NULL); |
972 | register_ioport_write(0x92, 1, 1, ioport92_write, NULL); | |
973 | ||
d592d303 | 974 | if (pci_enabled) { |
d592d303 FB |
975 | ioapic = ioapic_init(); |
976 | } | |
d537cf6c | 977 | pit = pit_init(0x40, i8259[0]); |
fd06c375 | 978 | pcspk_init(pit); |
d592d303 FB |
979 | if (pci_enabled) { |
980 | pic_set_alt_irq_func(isa_pic, ioapic_set_irq, ioapic); | |
981 | } | |
b41a2cd1 | 982 | |
8d11df9e FB |
983 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
984 | if (serial_hds[i]) { | |
b6cd0ea1 AJ |
985 | serial_init(serial_io[i], i8259[serial_irq[i]], 115200, |
986 | serial_hds[i]); | |
8d11df9e FB |
987 | } |
988 | } | |
b41a2cd1 | 989 | |
6508fe59 FB |
990 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
991 | if (parallel_hds[i]) { | |
d537cf6c PB |
992 | parallel_init(parallel_io[i], i8259[parallel_irq[i]], |
993 | parallel_hds[i]); | |
6508fe59 FB |
994 | } |
995 | } | |
996 | ||
a41b2ff2 PB |
997 | for(i = 0; i < nb_nics; i++) { |
998 | nd = &nd_table[i]; | |
999 | if (!nd->model) { | |
1000 | if (pci_enabled) { | |
1001 | nd->model = "ne2k_pci"; | |
1002 | } else { | |
1003 | nd->model = "ne2k_isa"; | |
1004 | } | |
69b91039 | 1005 | } |
a41b2ff2 | 1006 | if (strcmp(nd->model, "ne2k_isa") == 0) { |
d537cf6c | 1007 | pc_init_ne2k_isa(nd, i8259); |
a41b2ff2 | 1008 | } else if (pci_enabled) { |
c4a7060c BS |
1009 | if (strcmp(nd->model, "?") == 0) |
1010 | fprintf(stderr, "qemu: Supported ISA NICs: ne2k_isa\n"); | |
abcebc7e | 1011 | pci_nic_init(pci_bus, nd, -1); |
c4a7060c BS |
1012 | } else if (strcmp(nd->model, "?") == 0) { |
1013 | fprintf(stderr, "qemu: Supported ISA NICs: ne2k_isa\n"); | |
1014 | exit(1); | |
a41b2ff2 PB |
1015 | } else { |
1016 | fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model); | |
1017 | exit(1); | |
69b91039 | 1018 | } |
a41b2ff2 | 1019 | } |
b41a2cd1 | 1020 | |
e4bcb14c TS |
1021 | if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) { |
1022 | fprintf(stderr, "qemu: too many IDE bus\n"); | |
1023 | exit(1); | |
1024 | } | |
1025 | ||
1026 | for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) { | |
1027 | index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS); | |
1028 | if (index != -1) | |
1029 | hd[i] = drives_table[index].bdrv; | |
1030 | else | |
1031 | hd[i] = NULL; | |
1032 | } | |
1033 | ||
a41b2ff2 | 1034 | if (pci_enabled) { |
e4bcb14c | 1035 | pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1, i8259); |
a41b2ff2 | 1036 | } else { |
e4bcb14c | 1037 | for(i = 0; i < MAX_IDE_BUS; i++) { |
d537cf6c | 1038 | isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]], |
e4bcb14c | 1039 | hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]); |
69b91039 | 1040 | } |
b41a2cd1 | 1041 | } |
69b91039 | 1042 | |
d537cf6c | 1043 | i8042_init(i8259[1], i8259[12], 0x60); |
7c29d0c0 | 1044 | DMA_init(0); |
6a36d84e | 1045 | #ifdef HAS_AUDIO |
d537cf6c | 1046 | audio_init(pci_enabled ? pci_bus : NULL, i8259); |
fb065187 | 1047 | #endif |
80cabfad | 1048 | |
e4bcb14c TS |
1049 | for(i = 0; i < MAX_FD; i++) { |
1050 | index = drive_get_index(IF_FLOPPY, 0, i); | |
1051 | if (index != -1) | |
1052 | fd[i] = drives_table[index].bdrv; | |
1053 | else | |
1054 | fd[i] = NULL; | |
1055 | } | |
1056 | floppy_controller = fdctrl_init(i8259[6], 2, 0, 0x3f0, fd); | |
b41a2cd1 | 1057 | |
00f82b8a | 1058 | cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device, hd); |
69b91039 | 1059 | |
bb36d470 | 1060 | if (pci_enabled && usb_enabled) { |
afcc3cdf | 1061 | usb_uhci_piix3_init(pci_bus, piix3_devfn + 2); |
bb36d470 FB |
1062 | } |
1063 | ||
6515b203 | 1064 | if (pci_enabled && acpi_enabled) { |
3fffc223 | 1065 | uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */ |
0ff596d0 PB |
1066 | i2c_bus *smbus; |
1067 | ||
1068 | /* TODO: Populate SPD eeprom data. */ | |
cf7a2fe2 | 1069 | smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100, i8259[9]); |
3fffc223 | 1070 | for (i = 0; i < 8; i++) { |
0ff596d0 | 1071 | smbus_eeprom_device_init(smbus, 0x50 + i, eeprom_buf + (i * 256)); |
3fffc223 | 1072 | } |
6515b203 | 1073 | } |
3b46e624 | 1074 | |
a5954d5c FB |
1075 | if (i440fx_state) { |
1076 | i440fx_init_memory_mappings(i440fx_state); | |
1077 | } | |
e4bcb14c | 1078 | |
7d8406be | 1079 | if (pci_enabled) { |
e4bcb14c TS |
1080 | int max_bus; |
1081 | int bus, unit; | |
7d8406be | 1082 | void *scsi; |
96d30e48 | 1083 | |
e4bcb14c TS |
1084 | max_bus = drive_get_max_bus(IF_SCSI); |
1085 | ||
1086 | for (bus = 0; bus <= max_bus; bus++) { | |
1087 | scsi = lsi_scsi_init(pci_bus, -1); | |
1088 | for (unit = 0; unit < LSI_MAX_DEVS; unit++) { | |
1089 | index = drive_get_index(IF_SCSI, bus, unit); | |
1090 | if (index == -1) | |
1091 | continue; | |
1092 | lsi_scsi_attach(scsi, drives_table[index].bdrv, unit); | |
1093 | } | |
1094 | } | |
7d8406be | 1095 | } |
6e02c38d AL |
1096 | |
1097 | /* Add virtio block devices */ | |
1098 | if (pci_enabled) { | |
1099 | int index; | |
1100 | int unit_id = 0; | |
1101 | ||
1102 | while ((index = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) { | |
1103 | virtio_blk_init(pci_bus, 0x1AF4, 0x1001, | |
1104 | drives_table[index].bdrv); | |
1105 | unit_id++; | |
1106 | } | |
1107 | } | |
80cabfad | 1108 | } |
b5ff2d6e | 1109 | |
00f82b8a | 1110 | static void pc_init_pci(ram_addr_t ram_size, int vga_ram_size, |
b881c2c6 | 1111 | const char *boot_device, DisplayState *ds, |
5fafdf24 | 1112 | const char *kernel_filename, |
3dbbdc25 | 1113 | const char *kernel_cmdline, |
94fc95cd JM |
1114 | const char *initrd_filename, |
1115 | const char *cpu_model) | |
3dbbdc25 | 1116 | { |
b881c2c6 | 1117 | pc_init1(ram_size, vga_ram_size, boot_device, ds, |
3dbbdc25 | 1118 | kernel_filename, kernel_cmdline, |
a049de61 | 1119 | initrd_filename, 1, cpu_model); |
3dbbdc25 FB |
1120 | } |
1121 | ||
00f82b8a | 1122 | static void pc_init_isa(ram_addr_t ram_size, int vga_ram_size, |
b881c2c6 | 1123 | const char *boot_device, DisplayState *ds, |
5fafdf24 | 1124 | const char *kernel_filename, |
3dbbdc25 | 1125 | const char *kernel_cmdline, |
94fc95cd JM |
1126 | const char *initrd_filename, |
1127 | const char *cpu_model) | |
3dbbdc25 | 1128 | { |
b881c2c6 | 1129 | pc_init1(ram_size, vga_ram_size, boot_device, ds, |
3dbbdc25 | 1130 | kernel_filename, kernel_cmdline, |
a049de61 | 1131 | initrd_filename, 0, cpu_model); |
3dbbdc25 FB |
1132 | } |
1133 | ||
b5ff2d6e | 1134 | QEMUMachine pc_machine = { |
a245f2e7 AJ |
1135 | .name = "pc", |
1136 | .desc = "Standard PC", | |
1137 | .init = pc_init_pci, | |
1138 | .ram_require = VGA_RAM_SIZE + PC_MAX_BIOS_SIZE, | |
b2097003 | 1139 | .max_cpus = 255, |
3dbbdc25 FB |
1140 | }; |
1141 | ||
1142 | QEMUMachine isapc_machine = { | |
a245f2e7 AJ |
1143 | .name = "isapc", |
1144 | .desc = "ISA-only PC", | |
1145 | .init = pc_init_isa, | |
1146 | .ram_require = VGA_RAM_SIZE + PC_MAX_BIOS_SIZE, | |
b2097003 | 1147 | .max_cpus = 1, |
b5ff2d6e | 1148 | }; |