root / hw / pc.c @ 8f40c388
History | View | Annotate | Download (25.6 kB)
1 | 80cabfad | bellard | /*
|
---|---|---|---|
2 | 80cabfad | bellard | * QEMU PC System Emulator
|
3 | 80cabfad | bellard | *
|
4 | 80cabfad | bellard | * Copyright (c) 2003-2004 Fabrice Bellard
|
5 | 80cabfad | bellard | *
|
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 | 80cabfad | bellard | #include "vl.h" |
25 | 80cabfad | bellard | |
26 | b41a2cd1 | bellard | /* output Bochs bios info messages */
|
27 | b41a2cd1 | bellard | //#define DEBUG_BIOS
|
28 | b41a2cd1 | bellard | |
29 | 80cabfad | bellard | #define BIOS_FILENAME "bios.bin" |
30 | 80cabfad | bellard | #define VGABIOS_FILENAME "vgabios.bin" |
31 | de9258a8 | bellard | #define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin" |
32 | 80cabfad | bellard | #define LINUX_BOOT_FILENAME "linux_boot.bin" |
33 | 80cabfad | bellard | |
34 | 80cabfad | bellard | #define KERNEL_LOAD_ADDR 0x00100000 |
35 | 07de1eaa | bellard | #define INITRD_LOAD_ADDR 0x00600000 |
36 | 80cabfad | bellard | #define KERNEL_PARAMS_ADDR 0x00090000 |
37 | 80cabfad | bellard | #define KERNEL_CMDLINE_ADDR 0x00099000 |
38 | 80cabfad | bellard | |
39 | baca51fa | bellard | static fdctrl_t *floppy_controller;
|
40 | b0a21b53 | bellard | static RTCState *rtc_state;
|
41 | ec844b96 | bellard | static PITState *pit;
|
42 | d592d303 | bellard | static IOAPICState *ioapic;
|
43 | 80cabfad | bellard | |
44 | b41a2cd1 | bellard | static void ioport80_write(void *opaque, uint32_t addr, uint32_t data) |
45 | 80cabfad | bellard | { |
46 | 80cabfad | bellard | } |
47 | 80cabfad | bellard | |
48 | f929aad6 | bellard | /* MSDOS compatibility mode FPU exception support */
|
49 | f929aad6 | bellard | /* XXX: add IGNNE support */
|
50 | f929aad6 | bellard | void cpu_set_ferr(CPUX86State *s)
|
51 | f929aad6 | bellard | { |
52 | f929aad6 | bellard | pic_set_irq(13, 1); |
53 | f929aad6 | bellard | } |
54 | f929aad6 | bellard | |
55 | f929aad6 | bellard | static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data) |
56 | f929aad6 | bellard | { |
57 | f929aad6 | bellard | pic_set_irq(13, 0); |
58 | f929aad6 | bellard | } |
59 | f929aad6 | bellard | |
60 | 28ab0e2e | bellard | /* TSC handling */
|
61 | 28ab0e2e | bellard | uint64_t cpu_get_tsc(CPUX86State *env) |
62 | 28ab0e2e | bellard | { |
63 | 1dce7c3c | bellard | /* Note: when using kqemu, it is more logical to return the host TSC
|
64 | 1dce7c3c | bellard | because kqemu does not trap the RDTSC instruction for
|
65 | 1dce7c3c | bellard | performance reasons */
|
66 | 1dce7c3c | bellard | #if USE_KQEMU
|
67 | 1dce7c3c | bellard | if (env->kqemu_enabled) {
|
68 | 1dce7c3c | bellard | return cpu_get_real_ticks();
|
69 | 1dce7c3c | bellard | } else
|
70 | 1dce7c3c | bellard | #endif
|
71 | 1dce7c3c | bellard | { |
72 | 1dce7c3c | bellard | return cpu_get_ticks();
|
73 | 1dce7c3c | bellard | } |
74 | 28ab0e2e | bellard | } |
75 | 28ab0e2e | bellard | |
76 | 3de388f6 | bellard | /* IRQ handling */
|
77 | 3de388f6 | bellard | int cpu_get_pic_interrupt(CPUState *env)
|
78 | 3de388f6 | bellard | { |
79 | 3de388f6 | bellard | int intno;
|
80 | 3de388f6 | bellard | |
81 | 3de388f6 | bellard | intno = apic_get_interrupt(env); |
82 | 3de388f6 | bellard | if (intno >= 0) { |
83 | 3de388f6 | bellard | /* set irq request if a PIC irq is still pending */
|
84 | 3de388f6 | bellard | /* XXX: improve that */
|
85 | 3de388f6 | bellard | pic_update_irq(isa_pic); |
86 | 3de388f6 | bellard | return intno;
|
87 | 3de388f6 | bellard | } |
88 | 3de388f6 | bellard | /* read the irq from the PIC */
|
89 | 3de388f6 | bellard | intno = pic_read_irq(isa_pic); |
90 | 3de388f6 | bellard | return intno;
|
91 | 3de388f6 | bellard | } |
92 | 3de388f6 | bellard | |
93 | 3de388f6 | bellard | static void pic_irq_request(void *opaque, int level) |
94 | 3de388f6 | bellard | { |
95 | 59b8ad81 | bellard | CPUState *env = opaque; |
96 | 3de388f6 | bellard | if (level)
|
97 | 59b8ad81 | bellard | cpu_interrupt(env, CPU_INTERRUPT_HARD); |
98 | 3de388f6 | bellard | else
|
99 | 59b8ad81 | bellard | cpu_reset_interrupt(env, CPU_INTERRUPT_HARD); |
100 | 3de388f6 | bellard | } |
101 | 3de388f6 | bellard | |
102 | b0a21b53 | bellard | /* PC cmos mappings */
|
103 | b0a21b53 | bellard | |
104 | 80cabfad | bellard | #define REG_EQUIPMENT_BYTE 0x14 |
105 | b0a21b53 | bellard | #define REG_IBM_CENTURY_BYTE 0x32 |
106 | b0a21b53 | bellard | #define REG_IBM_PS2_CENTURY_BYTE 0x37 |
107 | b0a21b53 | bellard | |
108 | b0a21b53 | bellard | |
109 | b0a21b53 | bellard | static inline int to_bcd(RTCState *s, int a) |
110 | b0a21b53 | bellard | { |
111 | b0a21b53 | bellard | return ((a / 10) << 4) | (a % 10); |
112 | b0a21b53 | bellard | } |
113 | 80cabfad | bellard | |
114 | 777428f2 | bellard | static int cmos_get_fd_drive_type(int fd0) |
115 | 777428f2 | bellard | { |
116 | 777428f2 | bellard | int val;
|
117 | 777428f2 | bellard | |
118 | 777428f2 | bellard | switch (fd0) {
|
119 | 777428f2 | bellard | case 0: |
120 | 777428f2 | bellard | /* 1.44 Mb 3"5 drive */
|
121 | 777428f2 | bellard | val = 4;
|
122 | 777428f2 | bellard | break;
|
123 | 777428f2 | bellard | case 1: |
124 | 777428f2 | bellard | /* 2.88 Mb 3"5 drive */
|
125 | 777428f2 | bellard | val = 5;
|
126 | 777428f2 | bellard | break;
|
127 | 777428f2 | bellard | case 2: |
128 | 777428f2 | bellard | /* 1.2 Mb 5"5 drive */
|
129 | 777428f2 | bellard | val = 2;
|
130 | 777428f2 | bellard | break;
|
131 | 777428f2 | bellard | default:
|
132 | 777428f2 | bellard | val = 0;
|
133 | 777428f2 | bellard | break;
|
134 | 777428f2 | bellard | } |
135 | 777428f2 | bellard | return val;
|
136 | 777428f2 | bellard | } |
137 | 777428f2 | bellard | |
138 | ba6c2377 | bellard | static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd) |
139 | ba6c2377 | bellard | { |
140 | ba6c2377 | bellard | RTCState *s = rtc_state; |
141 | ba6c2377 | bellard | int cylinders, heads, sectors;
|
142 | ba6c2377 | bellard | bdrv_get_geometry_hint(hd, &cylinders, &heads, §ors); |
143 | ba6c2377 | bellard | rtc_set_memory(s, type_ofs, 47);
|
144 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs, cylinders); |
145 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 1, cylinders >> 8); |
146 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 2, heads);
|
147 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 3, 0xff); |
148 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 4, 0xff); |
149 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3)); |
150 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 6, cylinders);
|
151 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 7, cylinders >> 8); |
152 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 8, sectors);
|
153 | ba6c2377 | bellard | } |
154 | ba6c2377 | bellard | |
155 | ba6c2377 | bellard | /* hd_table must contain 4 block drivers */
|
156 | ba6c2377 | bellard | static void cmos_init(int ram_size, int boot_device, BlockDriverState **hd_table) |
157 | 80cabfad | bellard | { |
158 | b0a21b53 | bellard | RTCState *s = rtc_state; |
159 | 80cabfad | bellard | int val;
|
160 | b41a2cd1 | bellard | int fd0, fd1, nb;
|
161 | b0a21b53 | bellard | time_t ti; |
162 | b0a21b53 | bellard | struct tm *tm;
|
163 | ba6c2377 | bellard | int i;
|
164 | b0a21b53 | bellard | |
165 | b0a21b53 | bellard | /* set the CMOS date */
|
166 | b0a21b53 | bellard | time(&ti); |
167 | ee22c2f7 | bellard | if (rtc_utc)
|
168 | ee22c2f7 | bellard | tm = gmtime(&ti); |
169 | ee22c2f7 | bellard | else
|
170 | ee22c2f7 | bellard | tm = localtime(&ti); |
171 | b0a21b53 | bellard | rtc_set_date(s, tm); |
172 | b0a21b53 | bellard | |
173 | b0a21b53 | bellard | val = to_bcd(s, (tm->tm_year / 100) + 19); |
174 | b0a21b53 | bellard | rtc_set_memory(s, REG_IBM_CENTURY_BYTE, val); |
175 | b0a21b53 | bellard | rtc_set_memory(s, REG_IBM_PS2_CENTURY_BYTE, val); |
176 | 80cabfad | bellard | |
177 | b0a21b53 | bellard | /* various important CMOS locations needed by PC/Bochs bios */
|
178 | 80cabfad | bellard | |
179 | 80cabfad | bellard | /* memory size */
|
180 | 333190eb | bellard | val = 640; /* base memory in K */ |
181 | 333190eb | bellard | rtc_set_memory(s, 0x15, val);
|
182 | 333190eb | bellard | rtc_set_memory(s, 0x16, val >> 8); |
183 | 333190eb | bellard | |
184 | 80cabfad | bellard | val = (ram_size / 1024) - 1024; |
185 | 80cabfad | bellard | if (val > 65535) |
186 | 80cabfad | bellard | val = 65535;
|
187 | b0a21b53 | bellard | rtc_set_memory(s, 0x17, val);
|
188 | b0a21b53 | bellard | rtc_set_memory(s, 0x18, val >> 8); |
189 | b0a21b53 | bellard | rtc_set_memory(s, 0x30, val);
|
190 | b0a21b53 | bellard | rtc_set_memory(s, 0x31, val >> 8); |
191 | 80cabfad | bellard | |
192 | 9da98861 | bellard | if (ram_size > (16 * 1024 * 1024)) |
193 | 9da98861 | bellard | val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536); |
194 | 9da98861 | bellard | else
|
195 | 9da98861 | bellard | val = 0;
|
196 | 80cabfad | bellard | if (val > 65535) |
197 | 80cabfad | bellard | val = 65535;
|
198 | b0a21b53 | bellard | rtc_set_memory(s, 0x34, val);
|
199 | b0a21b53 | bellard | rtc_set_memory(s, 0x35, val >> 8); |
200 | 80cabfad | bellard | |
201 | 80cabfad | bellard | switch(boot_device) {
|
202 | 80cabfad | bellard | case 'a': |
203 | 80cabfad | bellard | case 'b': |
204 | b0a21b53 | bellard | rtc_set_memory(s, 0x3d, 0x01); /* floppy boot */ |
205 | 52ca8d6a | bellard | if (!fd_bootchk)
|
206 | 52ca8d6a | bellard | rtc_set_memory(s, 0x38, 0x01); /* disable signature check */ |
207 | 80cabfad | bellard | break;
|
208 | 80cabfad | bellard | default:
|
209 | 80cabfad | bellard | case 'c': |
210 | b0a21b53 | bellard | rtc_set_memory(s, 0x3d, 0x02); /* hard drive boot */ |
211 | 80cabfad | bellard | break;
|
212 | 80cabfad | bellard | case 'd': |
213 | b0a21b53 | bellard | rtc_set_memory(s, 0x3d, 0x03); /* CD-ROM boot */ |
214 | 80cabfad | bellard | break;
|
215 | 80cabfad | bellard | } |
216 | 80cabfad | bellard | |
217 | b41a2cd1 | bellard | /* floppy type */
|
218 | b41a2cd1 | bellard | |
219 | baca51fa | bellard | fd0 = fdctrl_get_drive_type(floppy_controller, 0);
|
220 | baca51fa | bellard | fd1 = fdctrl_get_drive_type(floppy_controller, 1);
|
221 | 80cabfad | bellard | |
222 | 777428f2 | bellard | val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
|
223 | b0a21b53 | bellard | rtc_set_memory(s, 0x10, val);
|
224 | b0a21b53 | bellard | |
225 | b0a21b53 | bellard | val = 0;
|
226 | b41a2cd1 | bellard | nb = 0;
|
227 | 80cabfad | bellard | if (fd0 < 3) |
228 | 80cabfad | bellard | nb++; |
229 | 80cabfad | bellard | if (fd1 < 3) |
230 | 80cabfad | bellard | nb++; |
231 | 80cabfad | bellard | switch (nb) {
|
232 | 80cabfad | bellard | case 0: |
233 | 80cabfad | bellard | break;
|
234 | 80cabfad | bellard | case 1: |
235 | b0a21b53 | bellard | val |= 0x01; /* 1 drive, ready for boot */ |
236 | 80cabfad | bellard | break;
|
237 | 80cabfad | bellard | case 2: |
238 | b0a21b53 | bellard | val |= 0x41; /* 2 drives, ready for boot */ |
239 | 80cabfad | bellard | break;
|
240 | 80cabfad | bellard | } |
241 | b0a21b53 | bellard | val |= 0x02; /* FPU is there */ |
242 | b0a21b53 | bellard | val |= 0x04; /* PS/2 mouse installed */ |
243 | b0a21b53 | bellard | rtc_set_memory(s, REG_EQUIPMENT_BYTE, val); |
244 | b0a21b53 | bellard | |
245 | ba6c2377 | bellard | /* hard drives */
|
246 | ba6c2377 | bellard | |
247 | ba6c2377 | bellard | rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0)); |
248 | ba6c2377 | bellard | if (hd_table[0]) |
249 | ba6c2377 | bellard | cmos_init_hd(0x19, 0x1b, hd_table[0]); |
250 | ba6c2377 | bellard | if (hd_table[1]) |
251 | ba6c2377 | bellard | cmos_init_hd(0x1a, 0x24, hd_table[1]); |
252 | ba6c2377 | bellard | |
253 | ba6c2377 | bellard | val = 0;
|
254 | 40b6ecc6 | bellard | for (i = 0; i < 4; i++) { |
255 | ba6c2377 | bellard | if (hd_table[i]) {
|
256 | 46d4767d | bellard | int cylinders, heads, sectors, translation;
|
257 | 46d4767d | bellard | /* NOTE: bdrv_get_geometry_hint() returns the physical
|
258 | 46d4767d | bellard | geometry. It is always such that: 1 <= sects <= 63, 1
|
259 | 46d4767d | bellard | <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
|
260 | 46d4767d | bellard | geometry can be different if a translation is done. */
|
261 | 46d4767d | bellard | translation = bdrv_get_translation_hint(hd_table[i]); |
262 | 46d4767d | bellard | if (translation == BIOS_ATA_TRANSLATION_AUTO) {
|
263 | 46d4767d | bellard | bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, §ors); |
264 | 46d4767d | bellard | if (cylinders <= 1024 && heads <= 16 && sectors <= 63) { |
265 | 46d4767d | bellard | /* No translation. */
|
266 | 46d4767d | bellard | translation = 0;
|
267 | 46d4767d | bellard | } else {
|
268 | 46d4767d | bellard | /* LBA translation. */
|
269 | 46d4767d | bellard | translation = 1;
|
270 | 46d4767d | bellard | } |
271 | 40b6ecc6 | bellard | } else {
|
272 | 46d4767d | bellard | translation--; |
273 | ba6c2377 | bellard | } |
274 | ba6c2377 | bellard | val |= translation << (i * 2);
|
275 | ba6c2377 | bellard | } |
276 | 40b6ecc6 | bellard | } |
277 | ba6c2377 | bellard | rtc_set_memory(s, 0x39, val);
|
278 | 80cabfad | bellard | } |
279 | 80cabfad | bellard | |
280 | 59b8ad81 | bellard | void ioport_set_a20(int enable) |
281 | 59b8ad81 | bellard | { |
282 | 59b8ad81 | bellard | /* XXX: send to all CPUs ? */
|
283 | 59b8ad81 | bellard | cpu_x86_set_a20(first_cpu, enable); |
284 | 59b8ad81 | bellard | } |
285 | 59b8ad81 | bellard | |
286 | 59b8ad81 | bellard | int ioport_get_a20(void) |
287 | 59b8ad81 | bellard | { |
288 | 59b8ad81 | bellard | return ((first_cpu->a20_mask >> 20) & 1); |
289 | 59b8ad81 | bellard | } |
290 | 59b8ad81 | bellard | |
291 | e1a23744 | bellard | static void ioport92_write(void *opaque, uint32_t addr, uint32_t val) |
292 | e1a23744 | bellard | { |
293 | 59b8ad81 | bellard | ioport_set_a20((val >> 1) & 1); |
294 | e1a23744 | bellard | /* XXX: bit 0 is fast reset */
|
295 | e1a23744 | bellard | } |
296 | e1a23744 | bellard | |
297 | e1a23744 | bellard | static uint32_t ioport92_read(void *opaque, uint32_t addr) |
298 | e1a23744 | bellard | { |
299 | 59b8ad81 | bellard | return ioport_get_a20() << 1; |
300 | e1a23744 | bellard | } |
301 | e1a23744 | bellard | |
302 | 80cabfad | bellard | /***********************************************************/
|
303 | 80cabfad | bellard | /* Bochs BIOS debug ports */
|
304 | 80cabfad | bellard | |
305 | b41a2cd1 | bellard | void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val) |
306 | 80cabfad | bellard | { |
307 | a2f659ee | bellard | static const char shutdown_str[8] = "Shutdown"; |
308 | a2f659ee | bellard | static int shutdown_index = 0; |
309 | a2f659ee | bellard | |
310 | 80cabfad | bellard | switch(addr) {
|
311 | 80cabfad | bellard | /* Bochs BIOS messages */
|
312 | 80cabfad | bellard | case 0x400: |
313 | 80cabfad | bellard | case 0x401: |
314 | 80cabfad | bellard | fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
|
315 | 80cabfad | bellard | exit(1);
|
316 | 80cabfad | bellard | case 0x402: |
317 | 80cabfad | bellard | case 0x403: |
318 | 80cabfad | bellard | #ifdef DEBUG_BIOS
|
319 | 80cabfad | bellard | fprintf(stderr, "%c", val);
|
320 | 80cabfad | bellard | #endif
|
321 | 80cabfad | bellard | break;
|
322 | a2f659ee | bellard | case 0x8900: |
323 | a2f659ee | bellard | /* same as Bochs power off */
|
324 | a2f659ee | bellard | if (val == shutdown_str[shutdown_index]) {
|
325 | a2f659ee | bellard | shutdown_index++; |
326 | a2f659ee | bellard | if (shutdown_index == 8) { |
327 | a2f659ee | bellard | shutdown_index = 0;
|
328 | a2f659ee | bellard | qemu_system_shutdown_request(); |
329 | a2f659ee | bellard | } |
330 | a2f659ee | bellard | } else {
|
331 | a2f659ee | bellard | shutdown_index = 0;
|
332 | a2f659ee | bellard | } |
333 | a2f659ee | bellard | break;
|
334 | 80cabfad | bellard | |
335 | 80cabfad | bellard | /* LGPL'ed VGA BIOS messages */
|
336 | 80cabfad | bellard | case 0x501: |
337 | 80cabfad | bellard | case 0x502: |
338 | 80cabfad | bellard | fprintf(stderr, "VGA BIOS panic, line %d\n", val);
|
339 | 80cabfad | bellard | exit(1);
|
340 | 80cabfad | bellard | case 0x500: |
341 | 80cabfad | bellard | case 0x503: |
342 | 80cabfad | bellard | #ifdef DEBUG_BIOS
|
343 | 80cabfad | bellard | fprintf(stderr, "%c", val);
|
344 | 80cabfad | bellard | #endif
|
345 | 80cabfad | bellard | break;
|
346 | 80cabfad | bellard | } |
347 | 80cabfad | bellard | } |
348 | 80cabfad | bellard | |
349 | 80cabfad | bellard | void bochs_bios_init(void) |
350 | 80cabfad | bellard | { |
351 | b41a2cd1 | bellard | register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL); |
352 | b41a2cd1 | bellard | register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL); |
353 | b41a2cd1 | bellard | register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL); |
354 | b41a2cd1 | bellard | register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL); |
355 | a2f659ee | bellard | register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL); |
356 | b41a2cd1 | bellard | |
357 | b41a2cd1 | bellard | register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL); |
358 | b41a2cd1 | bellard | register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL); |
359 | b41a2cd1 | bellard | register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL); |
360 | b41a2cd1 | bellard | register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL); |
361 | 80cabfad | bellard | } |
362 | 80cabfad | bellard | |
363 | 80cabfad | bellard | |
364 | 80cabfad | bellard | int load_kernel(const char *filename, uint8_t *addr, |
365 | 80cabfad | bellard | uint8_t *real_addr) |
366 | 80cabfad | bellard | { |
367 | 80cabfad | bellard | int fd, size;
|
368 | 80cabfad | bellard | int setup_sects;
|
369 | 80cabfad | bellard | |
370 | 096b7ea4 | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
371 | 80cabfad | bellard | if (fd < 0) |
372 | 80cabfad | bellard | return -1; |
373 | 80cabfad | bellard | |
374 | 80cabfad | bellard | /* load 16 bit code */
|
375 | 80cabfad | bellard | if (read(fd, real_addr, 512) != 512) |
376 | 80cabfad | bellard | goto fail;
|
377 | 80cabfad | bellard | setup_sects = real_addr[0x1F1];
|
378 | 80cabfad | bellard | if (!setup_sects)
|
379 | 80cabfad | bellard | setup_sects = 4;
|
380 | 80cabfad | bellard | if (read(fd, real_addr + 512, setup_sects * 512) != |
381 | 80cabfad | bellard | setup_sects * 512)
|
382 | 80cabfad | bellard | goto fail;
|
383 | 80cabfad | bellard | |
384 | 80cabfad | bellard | /* load 32 bit code */
|
385 | 80cabfad | bellard | size = read(fd, addr, 16 * 1024 * 1024); |
386 | 80cabfad | bellard | if (size < 0) |
387 | 80cabfad | bellard | goto fail;
|
388 | 80cabfad | bellard | close(fd); |
389 | 80cabfad | bellard | return size;
|
390 | 80cabfad | bellard | fail:
|
391 | 80cabfad | bellard | close(fd); |
392 | 80cabfad | bellard | return -1; |
393 | 80cabfad | bellard | } |
394 | 80cabfad | bellard | |
395 | 59b8ad81 | bellard | static void main_cpu_reset(void *opaque) |
396 | 59b8ad81 | bellard | { |
397 | 59b8ad81 | bellard | CPUState *env = opaque; |
398 | 59b8ad81 | bellard | cpu_reset(env); |
399 | 59b8ad81 | bellard | } |
400 | 59b8ad81 | bellard | |
401 | 59b8ad81 | bellard | /*************************************************/
|
402 | 59b8ad81 | bellard | |
403 | 59b8ad81 | bellard | static void putb(uint8_t **pp, int val) |
404 | 59b8ad81 | bellard | { |
405 | 59b8ad81 | bellard | uint8_t *q; |
406 | 59b8ad81 | bellard | q = *pp; |
407 | 59b8ad81 | bellard | *q++ = val; |
408 | 59b8ad81 | bellard | *pp = q; |
409 | 59b8ad81 | bellard | } |
410 | 59b8ad81 | bellard | |
411 | 59b8ad81 | bellard | static void putstr(uint8_t **pp, const char *str) |
412 | 59b8ad81 | bellard | { |
413 | 59b8ad81 | bellard | uint8_t *q; |
414 | 59b8ad81 | bellard | q = *pp; |
415 | 59b8ad81 | bellard | while (*str)
|
416 | 59b8ad81 | bellard | *q++ = *str++; |
417 | 59b8ad81 | bellard | *pp = q; |
418 | 59b8ad81 | bellard | } |
419 | 59b8ad81 | bellard | |
420 | 59b8ad81 | bellard | static void putle16(uint8_t **pp, int val) |
421 | 59b8ad81 | bellard | { |
422 | 59b8ad81 | bellard | uint8_t *q; |
423 | 59b8ad81 | bellard | q = *pp; |
424 | 59b8ad81 | bellard | *q++ = val; |
425 | 59b8ad81 | bellard | *q++ = val >> 8;
|
426 | 59b8ad81 | bellard | *pp = q; |
427 | 59b8ad81 | bellard | } |
428 | 59b8ad81 | bellard | |
429 | 59b8ad81 | bellard | static void putle32(uint8_t **pp, int val) |
430 | 59b8ad81 | bellard | { |
431 | 59b8ad81 | bellard | uint8_t *q; |
432 | 59b8ad81 | bellard | q = *pp; |
433 | 59b8ad81 | bellard | *q++ = val; |
434 | 59b8ad81 | bellard | *q++ = val >> 8;
|
435 | 59b8ad81 | bellard | *q++ = val >> 16;
|
436 | 59b8ad81 | bellard | *q++ = val >> 24;
|
437 | 59b8ad81 | bellard | *pp = q; |
438 | 59b8ad81 | bellard | } |
439 | 59b8ad81 | bellard | |
440 | 59b8ad81 | bellard | static int mpf_checksum(const uint8_t *data, int len) |
441 | 59b8ad81 | bellard | { |
442 | 59b8ad81 | bellard | int sum, i;
|
443 | 59b8ad81 | bellard | sum = 0;
|
444 | 59b8ad81 | bellard | for(i = 0; i < len; i++) |
445 | 59b8ad81 | bellard | sum += data[i]; |
446 | 59b8ad81 | bellard | return sum & 0xff; |
447 | 59b8ad81 | bellard | } |
448 | 59b8ad81 | bellard | |
449 | 59b8ad81 | bellard | /* Build the Multi Processor table in the BIOS. Same values as Bochs. */
|
450 | 59b8ad81 | bellard | static void bios_add_mptable(uint8_t *bios_data) |
451 | 59b8ad81 | bellard | { |
452 | 59b8ad81 | bellard | uint8_t *mp_config_table, *q, *float_pointer_struct; |
453 | 59b8ad81 | bellard | int ioapic_id, offset, i, len;
|
454 | 59b8ad81 | bellard | |
455 | 59b8ad81 | bellard | if (smp_cpus <= 1) |
456 | 59b8ad81 | bellard | return;
|
457 | 59b8ad81 | bellard | |
458 | 87022ff5 | bellard | mp_config_table = bios_data + 0xb000;
|
459 | 59b8ad81 | bellard | q = mp_config_table; |
460 | 59b8ad81 | bellard | putstr(&q, "PCMP"); /* "PCMP signature */ |
461 | 59b8ad81 | bellard | putle16(&q, 0); /* table length (patched later) */ |
462 | 59b8ad81 | bellard | putb(&q, 4); /* spec rev */ |
463 | 59b8ad81 | bellard | putb(&q, 0); /* checksum (patched later) */ |
464 | 59b8ad81 | bellard | putstr(&q, "QEMUCPU "); /* OEM id */ |
465 | 59b8ad81 | bellard | putstr(&q, "0.1 "); /* vendor id */ |
466 | 59b8ad81 | bellard | putle32(&q, 0); /* OEM table ptr */ |
467 | 59b8ad81 | bellard | putle16(&q, 0); /* OEM table size */ |
468 | 59b8ad81 | bellard | putle16(&q, 20); /* entry count */ |
469 | 59b8ad81 | bellard | putle32(&q, 0xfee00000); /* local APIC addr */ |
470 | 59b8ad81 | bellard | putle16(&q, 0); /* ext table length */ |
471 | 59b8ad81 | bellard | putb(&q, 0); /* ext table checksum */ |
472 | 59b8ad81 | bellard | putb(&q, 0); /* reserved */ |
473 | 59b8ad81 | bellard | |
474 | 59b8ad81 | bellard | for(i = 0; i < smp_cpus; i++) { |
475 | 59b8ad81 | bellard | putb(&q, 0); /* entry type = processor */ |
476 | 59b8ad81 | bellard | putb(&q, i); /* APIC id */
|
477 | 59b8ad81 | bellard | putb(&q, 0x11); /* local APIC version number */ |
478 | 59b8ad81 | bellard | if (i == 0) |
479 | 59b8ad81 | bellard | putb(&q, 3); /* cpu flags: enabled, bootstrap cpu */ |
480 | 59b8ad81 | bellard | else
|
481 | 59b8ad81 | bellard | putb(&q, 1); /* cpu flags: enabled */ |
482 | 59b8ad81 | bellard | putb(&q, 0); /* cpu signature */ |
483 | 59b8ad81 | bellard | putb(&q, 6);
|
484 | 59b8ad81 | bellard | putb(&q, 0);
|
485 | 59b8ad81 | bellard | putb(&q, 0);
|
486 | 59b8ad81 | bellard | putle16(&q, 0x201); /* feature flags */ |
487 | 59b8ad81 | bellard | putle16(&q, 0);
|
488 | 59b8ad81 | bellard | |
489 | 59b8ad81 | bellard | putle16(&q, 0); /* reserved */ |
490 | 59b8ad81 | bellard | putle16(&q, 0);
|
491 | 59b8ad81 | bellard | putle16(&q, 0);
|
492 | 59b8ad81 | bellard | putle16(&q, 0);
|
493 | 59b8ad81 | bellard | } |
494 | 59b8ad81 | bellard | |
495 | 59b8ad81 | bellard | /* isa bus */
|
496 | 59b8ad81 | bellard | putb(&q, 1); /* entry type = bus */ |
497 | 59b8ad81 | bellard | putb(&q, 0); /* bus ID */ |
498 | 59b8ad81 | bellard | putstr(&q, "ISA ");
|
499 | 59b8ad81 | bellard | |
500 | 59b8ad81 | bellard | /* ioapic */
|
501 | 59b8ad81 | bellard | ioapic_id = smp_cpus; |
502 | 59b8ad81 | bellard | putb(&q, 2); /* entry type = I/O APIC */ |
503 | 59b8ad81 | bellard | putb(&q, ioapic_id); /* apic ID */
|
504 | 59b8ad81 | bellard | putb(&q, 0x11); /* I/O APIC version number */ |
505 | 59b8ad81 | bellard | putb(&q, 1); /* enable */ |
506 | 59b8ad81 | bellard | putle32(&q, 0xfec00000); /* I/O APIC addr */ |
507 | 59b8ad81 | bellard | |
508 | 59b8ad81 | bellard | /* irqs */
|
509 | 59b8ad81 | bellard | for(i = 0; i < 16; i++) { |
510 | 59b8ad81 | bellard | putb(&q, 3); /* entry type = I/O interrupt */ |
511 | 59b8ad81 | bellard | putb(&q, 0); /* interrupt type = vectored interrupt */ |
512 | 59b8ad81 | bellard | putb(&q, 0); /* flags: po=0, el=0 */ |
513 | 59b8ad81 | bellard | putb(&q, 0);
|
514 | 59b8ad81 | bellard | putb(&q, 0); /* source bus ID = ISA */ |
515 | 59b8ad81 | bellard | putb(&q, i); /* source bus IRQ */
|
516 | 59b8ad81 | bellard | putb(&q, ioapic_id); /* dest I/O APIC ID */
|
517 | 59b8ad81 | bellard | putb(&q, i); /* dest I/O APIC interrupt in */
|
518 | 59b8ad81 | bellard | } |
519 | 59b8ad81 | bellard | /* patch length */
|
520 | 59b8ad81 | bellard | len = q - mp_config_table; |
521 | 59b8ad81 | bellard | mp_config_table[4] = len;
|
522 | 59b8ad81 | bellard | mp_config_table[5] = len >> 8; |
523 | 59b8ad81 | bellard | |
524 | 59b8ad81 | bellard | mp_config_table[7] = -mpf_checksum(mp_config_table, q - mp_config_table);
|
525 | 59b8ad81 | bellard | |
526 | 59b8ad81 | bellard | /* align to 16 */
|
527 | 59b8ad81 | bellard | offset = q - bios_data; |
528 | 59b8ad81 | bellard | offset = (offset + 15) & ~15; |
529 | 59b8ad81 | bellard | float_pointer_struct = bios_data + offset; |
530 | 59b8ad81 | bellard | |
531 | 59b8ad81 | bellard | /* floating pointer structure */
|
532 | 59b8ad81 | bellard | q = float_pointer_struct; |
533 | 59b8ad81 | bellard | putstr(&q, "_MP_");
|
534 | 59b8ad81 | bellard | /* pointer to MP config table */
|
535 | 59b8ad81 | bellard | putle32(&q, mp_config_table - bios_data + 0x000f0000);
|
536 | 59b8ad81 | bellard | |
537 | 59b8ad81 | bellard | putb(&q, 1); /* length in 16 byte units */ |
538 | 59b8ad81 | bellard | putb(&q, 4); /* MP spec revision */ |
539 | 59b8ad81 | bellard | putb(&q, 0); /* checksum (patched later) */ |
540 | 59b8ad81 | bellard | putb(&q, 0); /* MP feature byte 1 */ |
541 | 59b8ad81 | bellard | |
542 | 59b8ad81 | bellard | putb(&q, 0);
|
543 | 59b8ad81 | bellard | putb(&q, 0);
|
544 | 59b8ad81 | bellard | putb(&q, 0);
|
545 | 59b8ad81 | bellard | putb(&q, 0);
|
546 | 59b8ad81 | bellard | float_pointer_struct[10] =
|
547 | 59b8ad81 | bellard | -mpf_checksum(float_pointer_struct, q - float_pointer_struct); |
548 | 59b8ad81 | bellard | } |
549 | 59b8ad81 | bellard | |
550 | 59b8ad81 | bellard | |
551 | b41a2cd1 | bellard | static const int ide_iobase[2] = { 0x1f0, 0x170 }; |
552 | b41a2cd1 | bellard | static const int ide_iobase2[2] = { 0x3f6, 0x376 }; |
553 | b41a2cd1 | bellard | static const int ide_irq[2] = { 14, 15 }; |
554 | b41a2cd1 | bellard | |
555 | b41a2cd1 | bellard | #define NE2000_NB_MAX 6 |
556 | b41a2cd1 | bellard | |
557 | 8d11df9e | bellard | static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 }; |
558 | b41a2cd1 | bellard | static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 }; |
559 | b41a2cd1 | bellard | |
560 | 8d11df9e | bellard | static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 }; |
561 | 8d11df9e | bellard | static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 }; |
562 | 8d11df9e | bellard | |
563 | 6508fe59 | bellard | static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc }; |
564 | 6508fe59 | bellard | static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 }; |
565 | 6508fe59 | bellard | |
566 | 6a36d84e | bellard | #ifdef HAS_AUDIO
|
567 | 6a36d84e | bellard | static void audio_init (PCIBus *pci_bus) |
568 | 6a36d84e | bellard | { |
569 | 6a36d84e | bellard | struct soundhw *c;
|
570 | 6a36d84e | bellard | int audio_enabled = 0; |
571 | 6a36d84e | bellard | |
572 | 6a36d84e | bellard | for (c = soundhw; !audio_enabled && c->name; ++c) {
|
573 | 6a36d84e | bellard | audio_enabled = c->enabled; |
574 | 6a36d84e | bellard | } |
575 | 6a36d84e | bellard | |
576 | 6a36d84e | bellard | if (audio_enabled) {
|
577 | 6a36d84e | bellard | AudioState *s; |
578 | 6a36d84e | bellard | |
579 | 6a36d84e | bellard | s = AUD_init (); |
580 | 6a36d84e | bellard | if (s) {
|
581 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
582 | 6a36d84e | bellard | if (c->enabled) {
|
583 | 6a36d84e | bellard | if (c->isa) {
|
584 | 6a36d84e | bellard | c->init.init_isa (s); |
585 | 6a36d84e | bellard | } |
586 | 6a36d84e | bellard | else {
|
587 | 6a36d84e | bellard | if (pci_bus) {
|
588 | 6a36d84e | bellard | c->init.init_pci (pci_bus, s); |
589 | 6a36d84e | bellard | } |
590 | 6a36d84e | bellard | } |
591 | 6a36d84e | bellard | } |
592 | 6a36d84e | bellard | } |
593 | 6a36d84e | bellard | } |
594 | 6a36d84e | bellard | } |
595 | 6a36d84e | bellard | } |
596 | 6a36d84e | bellard | #endif
|
597 | 6a36d84e | bellard | |
598 | a41b2ff2 | pbrook | static void pc_init_ne2k_isa(NICInfo *nd) |
599 | a41b2ff2 | pbrook | { |
600 | a41b2ff2 | pbrook | static int nb_ne2k = 0; |
601 | a41b2ff2 | pbrook | |
602 | a41b2ff2 | pbrook | if (nb_ne2k == NE2000_NB_MAX)
|
603 | a41b2ff2 | pbrook | return;
|
604 | a41b2ff2 | pbrook | isa_ne2000_init(ne2000_io[nb_ne2k], ne2000_irq[nb_ne2k], nd); |
605 | a41b2ff2 | pbrook | nb_ne2k++; |
606 | a41b2ff2 | pbrook | } |
607 | a41b2ff2 | pbrook | |
608 | 80cabfad | bellard | /* PC hardware initialisation */
|
609 | b5ff2d6e | bellard | static void pc_init1(int ram_size, int vga_ram_size, int boot_device, |
610 | b5ff2d6e | bellard | DisplayState *ds, const char **fd_filename, int snapshot, |
611 | b5ff2d6e | bellard | const char *kernel_filename, const char *kernel_cmdline, |
612 | 3dbbdc25 | bellard | const char *initrd_filename, |
613 | 3dbbdc25 | bellard | int pci_enabled)
|
614 | 80cabfad | bellard | { |
615 | 80cabfad | bellard | char buf[1024]; |
616 | a41b2ff2 | pbrook | int ret, linux_boot, initrd_size, i;
|
617 | 7587cf44 | bellard | unsigned long bios_offset, vga_bios_offset; |
618 | 7587cf44 | bellard | int bios_size, isa_bios_size;
|
619 | 46e50e9d | bellard | PCIBus *pci_bus; |
620 | 5c3ff3a7 | pbrook | int piix3_devfn = -1; |
621 | 59b8ad81 | bellard | CPUState *env; |
622 | a41b2ff2 | pbrook | NICInfo *nd; |
623 | d592d303 | bellard | |
624 | 80cabfad | bellard | linux_boot = (kernel_filename != NULL);
|
625 | 80cabfad | bellard | |
626 | 59b8ad81 | bellard | /* init CPUs */
|
627 | 59b8ad81 | bellard | for(i = 0; i < smp_cpus; i++) { |
628 | 59b8ad81 | bellard | env = cpu_init(); |
629 | 59b8ad81 | bellard | if (i != 0) |
630 | ad49ff9d | bellard | env->hflags |= HF_HALTED_MASK; |
631 | 59b8ad81 | bellard | if (smp_cpus > 1) { |
632 | 59b8ad81 | bellard | /* XXX: enable it in all cases */
|
633 | 59b8ad81 | bellard | env->cpuid_features |= CPUID_APIC; |
634 | 59b8ad81 | bellard | } |
635 | e5d13e2f | bellard | register_savevm("cpu", i, 3, cpu_save, cpu_load, env); |
636 | 59b8ad81 | bellard | qemu_register_reset(main_cpu_reset, env); |
637 | 59b8ad81 | bellard | if (pci_enabled) {
|
638 | 59b8ad81 | bellard | apic_init(env); |
639 | 59b8ad81 | bellard | } |
640 | 59b8ad81 | bellard | } |
641 | 59b8ad81 | bellard | |
642 | 80cabfad | bellard | /* allocate RAM */
|
643 | 80cabfad | bellard | cpu_register_physical_memory(0, ram_size, 0); |
644 | 80cabfad | bellard | |
645 | 80cabfad | bellard | /* BIOS load */
|
646 | 7587cf44 | bellard | bios_offset = ram_size + vga_ram_size; |
647 | 7587cf44 | bellard | vga_bios_offset = bios_offset + 256 * 1024; |
648 | 7587cf44 | bellard | |
649 | 80cabfad | bellard | snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME); |
650 | 7587cf44 | bellard | bios_size = get_image_size(buf); |
651 | 7587cf44 | bellard | if (bios_size <= 0 || |
652 | 7587cf44 | bellard | (bios_size % 65536) != 0 || |
653 | 7587cf44 | bellard | bios_size > (256 * 1024)) { |
654 | 7587cf44 | bellard | goto bios_error;
|
655 | 7587cf44 | bellard | } |
656 | 7587cf44 | bellard | ret = load_image(buf, phys_ram_base + bios_offset); |
657 | 7587cf44 | bellard | if (ret != bios_size) {
|
658 | 7587cf44 | bellard | bios_error:
|
659 | 80cabfad | bellard | fprintf(stderr, "qemu: could not load PC bios '%s'\n", buf);
|
660 | 80cabfad | bellard | exit(1);
|
661 | 80cabfad | bellard | } |
662 | 59b8ad81 | bellard | if (bios_size == 65536) { |
663 | 59b8ad81 | bellard | bios_add_mptable(phys_ram_base + bios_offset); |
664 | 59b8ad81 | bellard | } |
665 | 7587cf44 | bellard | |
666 | 80cabfad | bellard | /* VGA BIOS load */
|
667 | de9258a8 | bellard | if (cirrus_vga_enabled) {
|
668 | de9258a8 | bellard | snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_CIRRUS_FILENAME); |
669 | de9258a8 | bellard | } else {
|
670 | de9258a8 | bellard | snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME); |
671 | de9258a8 | bellard | } |
672 | 7587cf44 | bellard | ret = load_image(buf, phys_ram_base + vga_bios_offset); |
673 | 80cabfad | bellard | |
674 | 80cabfad | bellard | /* setup basic memory access */
|
675 | 7587cf44 | bellard | cpu_register_physical_memory(0xc0000, 0x10000, |
676 | 7587cf44 | bellard | vga_bios_offset | IO_MEM_ROM); |
677 | 7587cf44 | bellard | |
678 | 7587cf44 | bellard | /* map the last 128KB of the BIOS in ISA space */
|
679 | 7587cf44 | bellard | isa_bios_size = bios_size; |
680 | 7587cf44 | bellard | if (isa_bios_size > (128 * 1024)) |
681 | 7587cf44 | bellard | isa_bios_size = 128 * 1024; |
682 | 7587cf44 | bellard | cpu_register_physical_memory(0xd0000, (192 * 1024) - isa_bios_size, |
683 | 7587cf44 | bellard | IO_MEM_UNASSIGNED); |
684 | 7587cf44 | bellard | cpu_register_physical_memory(0x100000 - isa_bios_size,
|
685 | 7587cf44 | bellard | isa_bios_size, |
686 | 7587cf44 | bellard | (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM); |
687 | 7587cf44 | bellard | /* map all the bios at the top of memory */
|
688 | 7587cf44 | bellard | cpu_register_physical_memory((uint32_t)(-bios_size), |
689 | 7587cf44 | bellard | bios_size, bios_offset | IO_MEM_ROM); |
690 | 80cabfad | bellard | |
691 | 80cabfad | bellard | bochs_bios_init(); |
692 | 80cabfad | bellard | |
693 | 80cabfad | bellard | if (linux_boot) {
|
694 | 80cabfad | bellard | uint8_t bootsect[512];
|
695 | 41b9be47 | bellard | uint8_t old_bootsect[512];
|
696 | 80cabfad | bellard | |
697 | 80cabfad | bellard | if (bs_table[0] == NULL) { |
698 | 80cabfad | bellard | fprintf(stderr, "A disk image must be given for 'hda' when booting a Linux kernel\n");
|
699 | 80cabfad | bellard | exit(1);
|
700 | 80cabfad | bellard | } |
701 | 80cabfad | bellard | snprintf(buf, sizeof(buf), "%s/%s", bios_dir, LINUX_BOOT_FILENAME); |
702 | 80cabfad | bellard | ret = load_image(buf, bootsect); |
703 | 80cabfad | bellard | if (ret != sizeof(bootsect)) { |
704 | 80cabfad | bellard | fprintf(stderr, "qemu: could not load linux boot sector '%s'\n",
|
705 | 80cabfad | bellard | buf); |
706 | 80cabfad | bellard | exit(1);
|
707 | 80cabfad | bellard | } |
708 | 80cabfad | bellard | |
709 | 41b9be47 | bellard | if (bdrv_read(bs_table[0], 0, old_bootsect, 1) >= 0) { |
710 | 41b9be47 | bellard | /* copy the MSDOS partition table */
|
711 | 41b9be47 | bellard | memcpy(bootsect + 0x1be, old_bootsect + 0x1be, 0x40); |
712 | 41b9be47 | bellard | } |
713 | 41b9be47 | bellard | |
714 | 80cabfad | bellard | bdrv_set_boot_sector(bs_table[0], bootsect, sizeof(bootsect)); |
715 | 80cabfad | bellard | |
716 | 80cabfad | bellard | /* now we can load the kernel */
|
717 | 80cabfad | bellard | ret = load_kernel(kernel_filename, |
718 | 80cabfad | bellard | phys_ram_base + KERNEL_LOAD_ADDR, |
719 | 80cabfad | bellard | phys_ram_base + KERNEL_PARAMS_ADDR); |
720 | 80cabfad | bellard | if (ret < 0) { |
721 | 80cabfad | bellard | fprintf(stderr, "qemu: could not load kernel '%s'\n",
|
722 | 80cabfad | bellard | kernel_filename); |
723 | 80cabfad | bellard | exit(1);
|
724 | 80cabfad | bellard | } |
725 | 80cabfad | bellard | |
726 | 80cabfad | bellard | /* load initrd */
|
727 | 80cabfad | bellard | initrd_size = 0;
|
728 | 80cabfad | bellard | if (initrd_filename) {
|
729 | 80cabfad | bellard | initrd_size = load_image(initrd_filename, phys_ram_base + INITRD_LOAD_ADDR); |
730 | 80cabfad | bellard | if (initrd_size < 0) { |
731 | 80cabfad | bellard | fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
|
732 | 80cabfad | bellard | initrd_filename); |
733 | 80cabfad | bellard | exit(1);
|
734 | 80cabfad | bellard | } |
735 | 80cabfad | bellard | } |
736 | 80cabfad | bellard | if (initrd_size > 0) { |
737 | 80cabfad | bellard | stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x218, INITRD_LOAD_ADDR);
|
738 | 80cabfad | bellard | stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x21c, initrd_size);
|
739 | 80cabfad | bellard | } |
740 | 80cabfad | bellard | pstrcpy(phys_ram_base + KERNEL_CMDLINE_ADDR, 4096,
|
741 | 80cabfad | bellard | kernel_cmdline); |
742 | 80cabfad | bellard | stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x20, 0xA33F); |
743 | 80cabfad | bellard | stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x22,
|
744 | 80cabfad | bellard | KERNEL_CMDLINE_ADDR - KERNEL_PARAMS_ADDR); |
745 | 80cabfad | bellard | /* loader type */
|
746 | 80cabfad | bellard | stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x210, 0x01); |
747 | 80cabfad | bellard | } |
748 | 80cabfad | bellard | |
749 | 69b91039 | bellard | if (pci_enabled) {
|
750 | 46e50e9d | bellard | pci_bus = i440fx_init(); |
751 | 502a5395 | pbrook | piix3_devfn = piix3_init(pci_bus); |
752 | 46e50e9d | bellard | } else {
|
753 | 46e50e9d | bellard | pci_bus = NULL;
|
754 | 69b91039 | bellard | } |
755 | 69b91039 | bellard | |
756 | 80cabfad | bellard | /* init basic PC hardware */
|
757 | b41a2cd1 | bellard | register_ioport_write(0x80, 1, 1, ioport80_write, NULL); |
758 | 80cabfad | bellard | |
759 | f929aad6 | bellard | register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL); |
760 | f929aad6 | bellard | |
761 | 1f04275e | bellard | if (cirrus_vga_enabled) {
|
762 | 1f04275e | bellard | if (pci_enabled) {
|
763 | 46e50e9d | bellard | pci_cirrus_vga_init(pci_bus, |
764 | 46e50e9d | bellard | ds, phys_ram_base + ram_size, ram_size, |
765 | 1f04275e | bellard | vga_ram_size); |
766 | 1f04275e | bellard | } else {
|
767 | 1f04275e | bellard | isa_cirrus_vga_init(ds, phys_ram_base + ram_size, ram_size, |
768 | 1f04275e | bellard | vga_ram_size); |
769 | 1f04275e | bellard | } |
770 | 1f04275e | bellard | } else {
|
771 | 89b6b508 | bellard | if (pci_enabled) {
|
772 | 89b6b508 | bellard | pci_vga_init(pci_bus, ds, phys_ram_base + ram_size, ram_size, |
773 | 89b6b508 | bellard | vga_ram_size, 0, 0); |
774 | 89b6b508 | bellard | } else {
|
775 | 89b6b508 | bellard | isa_vga_init(ds, phys_ram_base + ram_size, ram_size, |
776 | 89b6b508 | bellard | vga_ram_size); |
777 | 89b6b508 | bellard | } |
778 | 1f04275e | bellard | } |
779 | 80cabfad | bellard | |
780 | b0a21b53 | bellard | rtc_state = rtc_init(0x70, 8); |
781 | 80cabfad | bellard | |
782 | e1a23744 | bellard | register_ioport_read(0x92, 1, 1, ioport92_read, NULL); |
783 | e1a23744 | bellard | register_ioport_write(0x92, 1, 1, ioport92_write, NULL); |
784 | e1a23744 | bellard | |
785 | d592d303 | bellard | if (pci_enabled) {
|
786 | d592d303 | bellard | ioapic = ioapic_init(); |
787 | d592d303 | bellard | } |
788 | 59b8ad81 | bellard | isa_pic = pic_init(pic_irq_request, first_cpu); |
789 | ec844b96 | bellard | pit = pit_init(0x40, 0); |
790 | fd06c375 | bellard | pcspk_init(pit); |
791 | d592d303 | bellard | if (pci_enabled) {
|
792 | d592d303 | bellard | pic_set_alt_irq_func(isa_pic, ioapic_set_irq, ioapic); |
793 | d592d303 | bellard | } |
794 | b41a2cd1 | bellard | |
795 | 8d11df9e | bellard | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
796 | 8d11df9e | bellard | if (serial_hds[i]) {
|
797 | e5d13e2f | bellard | serial_init(&pic_set_irq_new, isa_pic, |
798 | e5d13e2f | bellard | serial_io[i], serial_irq[i], serial_hds[i]); |
799 | 8d11df9e | bellard | } |
800 | 8d11df9e | bellard | } |
801 | b41a2cd1 | bellard | |
802 | 6508fe59 | bellard | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
803 | 6508fe59 | bellard | if (parallel_hds[i]) {
|
804 | 6508fe59 | bellard | parallel_init(parallel_io[i], parallel_irq[i], parallel_hds[i]); |
805 | 6508fe59 | bellard | } |
806 | 6508fe59 | bellard | } |
807 | 6508fe59 | bellard | |
808 | a41b2ff2 | pbrook | for(i = 0; i < nb_nics; i++) { |
809 | a41b2ff2 | pbrook | nd = &nd_table[i]; |
810 | a41b2ff2 | pbrook | if (!nd->model) {
|
811 | a41b2ff2 | pbrook | if (pci_enabled) {
|
812 | a41b2ff2 | pbrook | nd->model = "ne2k_pci";
|
813 | a41b2ff2 | pbrook | } else {
|
814 | a41b2ff2 | pbrook | nd->model = "ne2k_isa";
|
815 | a41b2ff2 | pbrook | } |
816 | 69b91039 | bellard | } |
817 | a41b2ff2 | pbrook | if (strcmp(nd->model, "ne2k_isa") == 0) { |
818 | a41b2ff2 | pbrook | pc_init_ne2k_isa(nd); |
819 | a41b2ff2 | pbrook | } else if (pci_enabled) { |
820 | a41b2ff2 | pbrook | pci_nic_init(pci_bus, nd); |
821 | a41b2ff2 | pbrook | } else {
|
822 | a41b2ff2 | pbrook | fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model);
|
823 | a41b2ff2 | pbrook | exit(1);
|
824 | 69b91039 | bellard | } |
825 | a41b2ff2 | pbrook | } |
826 | b41a2cd1 | bellard | |
827 | a41b2ff2 | pbrook | if (pci_enabled) {
|
828 | 502a5395 | pbrook | pci_piix3_ide_init(pci_bus, bs_table, piix3_devfn + 1);
|
829 | a41b2ff2 | pbrook | } else {
|
830 | 69b91039 | bellard | for(i = 0; i < 2; i++) { |
831 | 69b91039 | bellard | isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i], |
832 | 69b91039 | bellard | bs_table[2 * i], bs_table[2 * i + 1]); |
833 | 69b91039 | bellard | } |
834 | b41a2cd1 | bellard | } |
835 | 69b91039 | bellard | |
836 | 80cabfad | bellard | kbd_init(); |
837 | 7c29d0c0 | bellard | DMA_init(0);
|
838 | 6a36d84e | bellard | #ifdef HAS_AUDIO
|
839 | 6a36d84e | bellard | audio_init(pci_enabled ? pci_bus : NULL);
|
840 | fb065187 | bellard | #endif
|
841 | 80cabfad | bellard | |
842 | baca51fa | bellard | floppy_controller = fdctrl_init(6, 2, 0, 0x3f0, fd_table); |
843 | b41a2cd1 | bellard | |
844 | ba6c2377 | bellard | cmos_init(ram_size, boot_device, bs_table); |
845 | 69b91039 | bellard | |
846 | bb36d470 | bellard | if (pci_enabled && usb_enabled) {
|
847 | 0d92ed30 | pbrook | usb_uhci_init(pci_bus, piix3_devfn + 2);
|
848 | bb36d470 | bellard | } |
849 | bb36d470 | bellard | |
850 | 6515b203 | bellard | if (pci_enabled && acpi_enabled) {
|
851 | 502a5395 | pbrook | piix4_pm_init(pci_bus, piix3_devfn + 3);
|
852 | 6515b203 | bellard | } |
853 | 7d8406be | pbrook | |
854 | 7d8406be | pbrook | #if 0
|
855 | 7d8406be | pbrook | /* ??? Need to figure out some way for the user to
|
856 | 7d8406be | pbrook | specify SCSI devices. */
|
857 | 7d8406be | pbrook | if (pci_enabled) {
|
858 | 7d8406be | pbrook | void *scsi;
|
859 | 7d8406be | pbrook | BlockDriverState *bdrv;
|
860 | 7d8406be | pbrook | |
861 | 7d8406be | pbrook | scsi = lsi_scsi_init(pci_bus, -1);
|
862 | 7d8406be | pbrook | bdrv = bdrv_new("scsidisk");
|
863 | 7d8406be | pbrook | bdrv_open(bdrv, "scsi_disk.img", 0);
|
864 | 7d8406be | pbrook | lsi_scsi_attach(scsi, bdrv, -1);
|
865 | 7d8406be | pbrook | bdrv = bdrv_new("scsicd");
|
866 | 7d8406be | pbrook | bdrv_open(bdrv, "scsi_cd.iso", 0);
|
867 | 7d8406be | pbrook | bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
|
868 | 7d8406be | pbrook | lsi_scsi_attach(scsi, bdrv, -1);
|
869 | 7d8406be | pbrook | }
|
870 | 7d8406be | pbrook | #endif
|
871 | 69b91039 | bellard | /* must be done after all PCI devices are instanciated */
|
872 | 69b91039 | bellard | /* XXX: should be done in the Bochs BIOS */
|
873 | 69b91039 | bellard | if (pci_enabled) {
|
874 | 69b91039 | bellard | pci_bios_init(); |
875 | 6515b203 | bellard | if (acpi_enabled)
|
876 | 6515b203 | bellard | acpi_bios_init(); |
877 | 69b91039 | bellard | } |
878 | 80cabfad | bellard | } |
879 | b5ff2d6e | bellard | |
880 | 3dbbdc25 | bellard | static void pc_init_pci(int ram_size, int vga_ram_size, int boot_device, |
881 | 3dbbdc25 | bellard | DisplayState *ds, const char **fd_filename, |
882 | 3dbbdc25 | bellard | int snapshot,
|
883 | 3dbbdc25 | bellard | const char *kernel_filename, |
884 | 3dbbdc25 | bellard | const char *kernel_cmdline, |
885 | 3dbbdc25 | bellard | const char *initrd_filename) |
886 | 3dbbdc25 | bellard | { |
887 | 3dbbdc25 | bellard | pc_init1(ram_size, vga_ram_size, boot_device, |
888 | 3dbbdc25 | bellard | ds, fd_filename, snapshot, |
889 | 3dbbdc25 | bellard | kernel_filename, kernel_cmdline, |
890 | 3dbbdc25 | bellard | initrd_filename, 1);
|
891 | 3dbbdc25 | bellard | } |
892 | 3dbbdc25 | bellard | |
893 | 3dbbdc25 | bellard | static void pc_init_isa(int ram_size, int vga_ram_size, int boot_device, |
894 | 3dbbdc25 | bellard | DisplayState *ds, const char **fd_filename, |
895 | 3dbbdc25 | bellard | int snapshot,
|
896 | 3dbbdc25 | bellard | const char *kernel_filename, |
897 | 3dbbdc25 | bellard | const char *kernel_cmdline, |
898 | 3dbbdc25 | bellard | const char *initrd_filename) |
899 | 3dbbdc25 | bellard | { |
900 | 3dbbdc25 | bellard | pc_init1(ram_size, vga_ram_size, boot_device, |
901 | 3dbbdc25 | bellard | ds, fd_filename, snapshot, |
902 | 3dbbdc25 | bellard | kernel_filename, kernel_cmdline, |
903 | 3dbbdc25 | bellard | initrd_filename, 0);
|
904 | 3dbbdc25 | bellard | } |
905 | 3dbbdc25 | bellard | |
906 | b5ff2d6e | bellard | QEMUMachine pc_machine = { |
907 | b5ff2d6e | bellard | "pc",
|
908 | b5ff2d6e | bellard | "Standard PC",
|
909 | 3dbbdc25 | bellard | pc_init_pci, |
910 | 3dbbdc25 | bellard | }; |
911 | 3dbbdc25 | bellard | |
912 | 3dbbdc25 | bellard | QEMUMachine isapc_machine = { |
913 | 3dbbdc25 | bellard | "isapc",
|
914 | 3dbbdc25 | bellard | "ISA-only PC",
|
915 | 3dbbdc25 | bellard | pc_init_isa, |
916 | b5ff2d6e | bellard | }; |