Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / post.c
1 // 32bit code to Power On Self Test (POST) a machine.
2 //
3 // Copyright (C) 2008-2013  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "biosvar.h" // SET_BDA
9 #include "bregs.h" // struct bregs
10 #include "config.h" // CONFIG_*
11 #include "fw/paravirt.h" // qemu_cfg_preinit
12 #include "fw/xen.h" // xen_preinit
13 #include "hw/ahci.h" // ahci_setup
14 #include "hw/ata.h" // ata_setup
15 #include "hw/esp-scsi.h" // esp_scsi_setup
16 #include "hw/lsi-scsi.h" // lsi_scsi_setup
17 #include "hw/megasas.h" // megasas_setup
18 #include "hw/pvscsi.h" // pvscsi_setup
19 #include "hw/pic.h" // pic_setup
20 #include "hw/ps2port.h" // ps2port_setup
21 #include "hw/rtc.h" // rtc_write
22 #include "hw/serialio.h" // serial_debug_preinit
23 #include "hw/usb.h" // usb_setup
24 #include "hw/virtio-blk.h" // virtio_blk_setup
25 #include "hw/virtio-scsi.h" // virtio_scsi_setup
26 #include "malloc.h" // malloc_init
27 #include "memmap.h" // add_e820
28 #include "output.h" // dprintf
29 #include "string.h" // memset
30 #include "util.h" // kbd_init
31
32
33 /****************************************************************
34  * BIOS initialization and hardware setup
35  ****************************************************************/
36
37 static void
38 ivt_init(void)
39 {
40     dprintf(3, "init ivt\n");
41
42     // Setup reset-vector entry point (controls legacy reboots).
43     HaveRunPost = 1;
44     rtc_write(CMOS_RESET_CODE, 0);
45
46     // Initialize all vectors to the default handler.
47     int i;
48     for (i=0; i<256; i++)
49         SET_IVT(i, FUNC16(entry_iret_official));
50
51     // Initialize all hw vectors to a default hw handler.
52     for (i=BIOS_HWIRQ0_VECTOR; i<BIOS_HWIRQ0_VECTOR+8; i++)
53         SET_IVT(i, FUNC16(entry_hwpic1));
54     for (i=BIOS_HWIRQ8_VECTOR; i<BIOS_HWIRQ8_VECTOR+8; i++)
55         SET_IVT(i, FUNC16(entry_hwpic2));
56
57     // Initialize software handlers.
58     SET_IVT(0x02, FUNC16(entry_02));
59     SET_IVT(0x10, FUNC16(entry_10));
60     SET_IVT(0x11, FUNC16(entry_11));
61     SET_IVT(0x12, FUNC16(entry_12));
62     SET_IVT(0x13, FUNC16(entry_13_official));
63     SET_IVT(0x14, FUNC16(entry_14));
64     SET_IVT(0x15, FUNC16(entry_15_official));
65     SET_IVT(0x16, FUNC16(entry_16));
66     SET_IVT(0x17, FUNC16(entry_17));
67     SET_IVT(0x18, FUNC16(entry_18));
68     SET_IVT(0x19, FUNC16(entry_19_official));
69     SET_IVT(0x1a, FUNC16(entry_1a_official));
70     SET_IVT(0x40, FUNC16(entry_40));
71
72     // INT 60h-66h reserved for user interrupt
73     for (i=0x60; i<=0x66; i++)
74         SET_IVT(i, SEGOFF(0, 0));
75
76     // set vector 0x79 to zero
77     // this is used by 'gardian angel' protection system
78     SET_IVT(0x79, SEGOFF(0, 0));
79 }
80
81 static void
82 bda_init(void)
83 {
84     dprintf(3, "init bda\n");
85
86     struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
87     memset(bda, 0, sizeof(*bda));
88
89     int esize = EBDA_SIZE_START;
90     u16 ebda_seg = EBDA_SEGMENT_START;
91     extern u8 final_varlow_start[];
92     if (!CONFIG_MALLOC_UPPERMEMORY)
93         ebda_seg = FLATPTR_TO_SEG(ALIGN_DOWN((u32)final_varlow_start, 1024)
94                                   - EBDA_SIZE_START*1024);
95     SET_BDA(ebda_seg, ebda_seg);
96
97     SET_BDA(mem_size_kb, ebda_seg / (1024/16));
98
99     // Init ebda
100     struct extended_bios_data_area_s *ebda = get_ebda_ptr();
101     memset(ebda, 0, sizeof(*ebda));
102     ebda->size = esize;
103
104     add_e820((u32)ebda, BUILD_LOWRAM_END-(u32)ebda, E820_RESERVED);
105
106     // Init extra stack
107     StackPos = (void*)(&ExtraStack[BUILD_EXTRA_STACK_SIZE] - zonelow_base);
108 }
109
110 void
111 interface_init(void)
112 {
113     // Running at new code address - do code relocation fixups
114     malloc_init();
115
116     // Setup romfile items.
117     qemu_cfg_init();
118     coreboot_cbfs_init();
119
120     // Setup ivt/bda/ebda
121     ivt_init();
122     bda_init();
123
124     // Other interfaces
125     thread_init();
126     boot_init();
127     bios32_init();
128     pmm_init();
129     pnp_init();
130     kbd_init();
131     mouse_init();
132 }
133
134 // Initialize hardware devices
135 void
136 device_hardware_setup(void)
137 {
138     usb_setup();
139     ps2port_setup();
140     lpt_setup();
141     serial_setup();
142
143     floppy_setup();
144     ata_setup();
145     ahci_setup();
146     sdcard_setup();
147     cbfs_payload_setup();
148     ramdisk_setup();
149     virtio_blk_setup();
150     virtio_scsi_setup();
151     lsi_scsi_setup();
152     esp_scsi_setup();
153     megasas_setup();
154     pvscsi_setup();
155 }
156
157 static void
158 platform_hardware_setup(void)
159 {
160     // Enable CPU caching
161     setcr0(getcr0() & ~(CR0_CD|CR0_NW));
162
163     // Make sure legacy DMA isn't running.
164     dma_setup();
165
166     // Init base pc hardware.
167     pic_setup();
168     mathcp_setup();
169     timer_setup();
170     clock_setup();
171
172     // Platform specific setup
173     qemu_platform_setup();
174     coreboot_platform_setup();
175 }
176
177 void
178 prepareboot(void)
179 {
180     // Run BCVs
181     bcv_prepboot();
182
183     // Finalize data structures before boot
184     cdrom_prepboot();
185     pmm_prepboot();
186     malloc_prepboot();
187     memmap_prepboot();
188
189     HaveRunPost = 2;
190
191     // Setup bios checksum.
192     BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
193 }
194
195 // Begin the boot process by invoking an int0x19 in 16bit mode.
196 void VISIBLE32FLAT
197 startBoot(void)
198 {
199     // Clear low-memory allocations (required by PMM spec).
200     memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
201
202     dprintf(3, "Jump to int19\n");
203     struct bregs br;
204     memset(&br, 0, sizeof(br));
205     br.flags = F_IF;
206     call16_int(0x19, &br);
207 }
208
209 // Main setup code.
210 static void
211 maininit(void)
212 {
213     // Initialize internal interfaces.
214     interface_init();
215
216     // Setup platform devices.
217     platform_hardware_setup();
218
219     // Start hardware initialization (if threads allowed during optionroms)
220     if (threads_during_optionroms())
221         device_hardware_setup();
222
223     // Run vga option rom
224     vgarom_setup();
225
226     // Do hardware initialization (if running synchronously)
227     if (!threads_during_optionroms()) {
228         device_hardware_setup();
229         wait_threads();
230     }
231
232     // Run option roms
233     optionrom_setup();
234
235     // Allow user to modify overall boot order.
236     interactive_bootmenu();
237     wait_threads();
238
239     // Prepare for boot.
240     prepareboot();
241
242     // Write protect bios memory.
243     make_bios_readonly();
244
245     // Invoke int 19 to start boot process.
246     startBoot();
247 }
248
249
250 /****************************************************************
251  * POST entry and code relocation
252  ****************************************************************/
253
254 // Update given relocs for the code at 'dest' with a given 'delta'
255 static void
256 updateRelocs(void *dest, u32 *rstart, u32 *rend, u32 delta)
257 {
258     u32 *reloc;
259     for (reloc = rstart; reloc < rend; reloc++)
260         *((u32*)(dest + *reloc)) += delta;
261 }
262
263 // Relocate init code and then call a function at its new address.
264 // The passed function should be in the "init" section and must not
265 // return.
266 void __noreturn
267 reloc_preinit(void *f, void *arg)
268 {
269     void (*func)(void *) __noreturn = f;
270     if (!CONFIG_RELOCATE_INIT)
271         func(arg);
272     // Symbols populated by the build.
273     extern u8 code32flat_start[];
274     extern u8 _reloc_min_align;
275     extern u32 _reloc_abs_start[], _reloc_abs_end[];
276     extern u32 _reloc_rel_start[], _reloc_rel_end[];
277     extern u32 _reloc_init_start[], _reloc_init_end[];
278     extern u8 code32init_start[], code32init_end[];
279
280     // Allocate space for init code.
281     u32 initsize = code32init_end - code32init_start;
282     u32 codealign = (u32)&_reloc_min_align;
283     void *codedest = memalign_tmp(codealign, initsize);
284     if (!codedest)
285         panic("No space for init relocation.\n");
286
287     // Copy code and update relocs (init absolute, init relative, and runtime)
288     dprintf(1, "Relocating init from %p to %p (size %d)\n"
289             , code32init_start, codedest, initsize);
290     s32 delta = codedest - (void*)code32init_start;
291     memcpy(codedest, code32init_start, initsize);
292     updateRelocs(codedest, _reloc_abs_start, _reloc_abs_end, delta);
293     updateRelocs(codedest, _reloc_rel_start, _reloc_rel_end, -delta);
294     updateRelocs(code32flat_start, _reloc_init_start, _reloc_init_end, delta);
295     if (f >= (void*)code32init_start && f < (void*)code32init_end)
296         func = f + delta;
297
298     // Call function in relocated code.
299     barrier();
300     func(arg);
301 }
302
303 // Setup for code relocation and then relocate.
304 void VISIBLE32INIT
305 dopost(void)
306 {
307     // Detect ram and setup internal malloc.
308     qemu_preinit();
309     coreboot_preinit();
310     malloc_preinit();
311
312     // Relocate initialization code and call maininit().
313     reloc_preinit(maininit, NULL);
314 }
315
316 // Entry point for Power On Self Test (POST) - the BIOS initilization
317 // phase.  This function makes the memory at 0xc0000-0xfffff
318 // read/writable and then calls dopost().
319 void VISIBLE32FLAT
320 handle_post(void)
321 {
322     if (!CONFIG_QEMU && !CONFIG_COREBOOT)
323         return;
324
325     serial_debug_preinit();
326     debug_banner();
327
328     // Check if we are running under Xen.
329     xen_preinit();
330
331     // Allow writes to modify bios area (0xf0000)
332     make_bios_writable();
333
334     // Now that memory is read/writable - start post process.
335     dopost();
336 }