Move the 32bit init code to high memory at runtime.
[seabios.git] / src / post.c
1 // 32bit code to Power On Self Test (POST) a machine.
2 //
3 // Copyright (C) 2008-2010  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 "ioport.h" // PORT_*
9 #include "config.h" // CONFIG_*
10 #include "cmos.h" // CMOS_*
11 #include "util.h" // memset
12 #include "biosvar.h" // struct bios_data_area_s
13 #include "disk.h" // floppy_drive_setup
14 #include "ata.h" // ata_setup
15 #include "memmap.h" // add_e820
16 #include "pic.h" // pic_setup
17 #include "pci.h" // create_pirtable
18 #include "acpi.h" // acpi_bios_init
19 #include "bregs.h" // struct bregs
20 #include "mptable.h" // mptable_init
21 #include "boot.h" // IPL
22 #include "usb.h" // usb_setup
23 #include "smbios.h" // smbios_init
24 #include "paravirt.h" // qemu_cfg_port_probe
25 #include "ps2port.h" // ps2port_setup
26 #include "virtio-blk.h" // virtio_blk_setup
27
28
29 /****************************************************************
30  * BIOS init
31  ****************************************************************/
32
33 static void
34 init_ivt(void)
35 {
36     dprintf(3, "init ivt\n");
37
38     // Initialize all vectors to the default handler.
39     int i;
40     for (i=0; i<256; i++)
41         SET_IVT(i, FUNC16(entry_iret_official));
42
43     // Initialize all hw vectors to a default hw handler.
44     for (i=0x08; i<=0x0f; i++)
45         SET_IVT(i, FUNC16(entry_hwpic1));
46     for (i=0x70; i<=0x77; i++)
47         SET_IVT(i, FUNC16(entry_hwpic2));
48
49     // Initialize software handlers.
50     SET_IVT(0x02, FUNC16(entry_02));
51     SET_IVT(0x10, FUNC16(entry_10));
52     SET_IVT(0x11, FUNC16(entry_11));
53     SET_IVT(0x12, FUNC16(entry_12));
54     SET_IVT(0x13, FUNC16(entry_13_official));
55     SET_IVT(0x14, FUNC16(entry_14));
56     SET_IVT(0x15, FUNC16(entry_15));
57     SET_IVT(0x16, FUNC16(entry_16));
58     SET_IVT(0x17, FUNC16(entry_17));
59     SET_IVT(0x18, FUNC16(entry_18));
60     SET_IVT(0x19, FUNC16(entry_19_official));
61     SET_IVT(0x1a, FUNC16(entry_1a));
62     SET_IVT(0x40, FUNC16(entry_40));
63
64     // INT 60h-66h reserved for user interrupt
65     for (i=0x60; i<=0x66; i++)
66         SET_IVT(i, SEGOFF(0, 0));
67
68     // set vector 0x79 to zero
69     // this is used by 'gardian angel' protection system
70     SET_IVT(0x79, SEGOFF(0, 0));
71
72     SET_IVT(0x1E, SEGOFF(SEG_BIOS, (u32)&diskette_param_table2 - BUILD_BIOS_ADDR));
73 }
74
75 static void
76 init_bda(void)
77 {
78     dprintf(3, "init bda\n");
79
80     struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
81     memset(bda, 0, sizeof(*bda));
82
83     int esize = EBDA_SIZE_START;
84     SET_BDA(mem_size_kb, BUILD_LOWRAM_END/1024 - esize);
85     u16 ebda_seg = EBDA_SEGMENT_START;
86     SET_BDA(ebda_seg, ebda_seg);
87
88     // Init ebda
89     struct extended_bios_data_area_s *ebda = get_ebda_ptr();
90     memset(ebda, 0, sizeof(*ebda));
91     ebda->size = esize;
92
93     add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
94              , E820_RESERVED);
95 }
96
97 static void
98 ram_probe(void)
99 {
100     dprintf(3, "Find memory size\n");
101     if (CONFIG_COREBOOT) {
102         coreboot_setup();
103     } else {
104         // On emulators, get memory size from nvram.
105         u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
106                   | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
107         if (rs)
108             rs += 16 * 1024 * 1024;
109         else
110             rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
111                    | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
112                   + 1 * 1024 * 1024);
113         RamSize = rs;
114         add_e820(0, rs, E820_RAM);
115
116         // Check for memory over 4Gig
117         u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
118                     | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
119                     | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
120         RamSizeOver4G = high;
121         add_e820(0x100000000ull, high, E820_RAM);
122
123         /* reserve 256KB BIOS area at the end of 4 GB */
124         add_e820(0xfffc0000, 256*1024, E820_RESERVED);
125     }
126
127     // Don't declare any memory between 0xa0000 and 0x100000
128     add_e820(BUILD_LOWRAM_END, BUILD_BIOS_ADDR-BUILD_LOWRAM_END, E820_HOLE);
129
130     // Mark known areas as reserved.
131     add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
132
133     u32 count = qemu_cfg_e820_entries();
134     if (count) {
135         struct e820_reservation entry;
136         int i;
137
138         for (i = 0; i < count; i++) {
139             qemu_cfg_e820_load_next(&entry);
140             add_e820(entry.address, entry.length, entry.type);
141         }
142     } else if (kvm_para_available()) {
143         // Backwards compatibility - provide hard coded range.
144         // 4 pages before the bios, 3 pages for vmx tss pages, the
145         // other page for EPT real mode pagetable
146         add_e820(0xfffbc000, 4*4096, E820_RESERVED);
147     }
148
149     dprintf(1, "Ram Size=0x%08x (0x%08x%08x high)\n"
150             , RamSize, (u32)(RamSizeOver4G >> 32), (u32)RamSizeOver4G);
151 }
152
153 static void
154 init_bios_tables(void)
155 {
156     if (CONFIG_COREBOOT) {
157         coreboot_copy_biostable();
158         return;
159     }
160
161     create_pirtable();
162
163     mptable_init();
164
165     smbios_init();
166
167     acpi_bios_init();
168 }
169
170 // Initialize hardware devices
171 static void
172 init_hw(void)
173 {
174     usb_setup();
175     ps2port_setup();
176     lpt_setup();
177     serial_setup();
178
179     floppy_setup();
180     ata_setup();
181     ramdisk_setup();
182     virtio_blk_setup();
183 }
184
185 // Begin the boot process by invoking an int0x19 in 16bit mode.
186 void VISIBLE32FLAT
187 startBoot(void)
188 {
189     // Clear low-memory allocations (required by PMM spec).
190     memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
191
192     dprintf(3, "Jump to int19\n");
193     struct bregs br;
194     memset(&br, 0, sizeof(br));
195     br.flags = F_IF;
196     call16_int(0x19, &br);
197 }
198
199 // Main setup code.
200 static void
201 maininit(void)
202 {
203     // Setup ivt/bda/ebda
204     init_ivt();
205     init_bda();
206
207     // Init base pc hardware.
208     thread_setup();
209     pic_setup();
210     timer_setup();
211     mathcp_setup();
212
213     // Initialize mtrr
214     smp_probe_setup();
215     mtrr_setup();
216
217     // Initialize pci
218     pci_setup();
219     smm_init();
220
221     // Initialize internal tables
222     boot_setup();
223     drive_setup();
224
225     // Start hardware initialization (if optionrom threading)
226     if (CONFIG_THREADS && CONFIG_THREAD_OPTIONROMS)
227         init_hw();
228
229     // Find and initialize other cpus
230     smp_probe();
231
232     // Setup interfaces that option roms may need
233     bios32_setup();
234     pmm_setup();
235     pnp_setup();
236     kbd_setup();
237     mouse_setup();
238     init_bios_tables();
239
240     // Run vga option rom
241     vga_setup();
242
243     // Do hardware initialization (if running synchronously)
244     if (!CONFIG_THREADS || !CONFIG_THREAD_OPTIONROMS) {
245         init_hw();
246         wait_threads();
247     }
248
249     // Run option roms
250     optionrom_setup();
251
252     // Run BCVs and show optional boot menu
253     boot_prep();
254
255     // Finalize data structures before boot
256     cdemu_setup();
257     pmm_finalize();
258     malloc_finalize();
259     memmap_finalize();
260
261     // Setup bios checksum.
262     BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
263
264     // Write protect bios memory.
265     make_bios_readonly();
266
267     // Invoke int 19 to start boot process.
268     startBoot();
269 }
270
271
272 /****************************************************************
273  * Code relocation
274  ****************************************************************/
275
276 // Update given relocs for the code at 'dest' with a given 'delta'
277 static void
278 updateRelocs(void *dest, u32 *rstart, u32 *rend, u32 delta)
279 {
280     u32 *reloc;
281     for (reloc = rstart; reloc < rend; reloc++)
282         *((u32*)(dest + *reloc)) += delta;
283 }
284
285 // Start of Power On Self Test - the BIOS initilization.  This
286 // function sets up for and attempts relocation of the init code.
287 static void
288 reloc_init(void)
289 {
290     if (!CONFIG_RELOCATE_INIT) {
291         maininit();
292         return;
293     }
294     // Symbols populated by the build.
295     extern u8 code32flat_start[];
296     extern u8 _reloc_min_align[];
297     extern u32 _reloc_abs_start[], _reloc_abs_end[];
298     extern u32 _reloc_rel_start[], _reloc_rel_end[];
299     extern u32 _reloc_init_start[], _reloc_init_end[];
300     extern u8 code32init_start[], code32init_end[];
301
302     // Allocate space for init code.
303     u32 initsize = code32init_end - code32init_start;
304     u32 align = (u32)&_reloc_min_align;
305     void *dest = memalign_tmp(align, initsize);
306     if (!dest)
307         panic("No space for init relocation.\n");
308
309     // Copy code and update relocs (init absolute, init relative, and runtime)
310     dprintf(1, "Relocating init from %p to %p (size %d)\n"
311             , code32init_start, dest, initsize);
312     s32 delta = dest - (void*)code32init_start;
313     memcpy(dest, code32init_start, initsize);
314     updateRelocs(dest, _reloc_abs_start, _reloc_abs_end, delta);
315     updateRelocs(dest, _reloc_rel_start, _reloc_rel_end, -delta);
316     updateRelocs(code32flat_start, _reloc_init_start, _reloc_init_end, delta);
317
318     // Call maininit() in relocated code.
319     void (*func)(void) = (void*)maininit + delta;
320     barrier();
321     func();
322 }
323
324 static int HaveRunPost;
325
326 // Start of Power On Self Test (POST) - the BIOS initilization phase.
327 // This function sets up for and attempts relocation of the init code.
328 void VISIBLE32INIT
329 post(void)
330 {
331     // Allow writes to modify bios area (0xf0000)
332     make_bios_writable();
333
334     HaveRunPost = 1;
335
336     // Detect ram and setup internal malloc.
337     memmap_setup();
338     qemu_cfg_port_probe();
339     ram_probe();
340     malloc_setup();
341
342     reloc_init();
343 }
344
345
346 /****************************************************************
347  * POST entry point
348  ****************************************************************/
349
350 // Attempt to invoke a hard-reboot.
351 static void
352 tryReboot(void)
353 {
354     dprintf(1, "Attempting a hard reboot\n");
355
356     // Setup for reset on qemu.
357     if (! CONFIG_COREBOOT) {
358         qemu_prep_reset();
359         if (HaveRunPost)
360             apm_shutdown();
361     }
362
363     // Try keyboard controller reboot.
364     i8042_reboot();
365
366     // Try PCI 0xcf9 reboot
367     pci_reboot();
368
369     // Try triple fault
370     asm volatile("int3");
371
372     panic("Could not reboot");
373 }
374
375 // 32-bit entry point.
376 void VISIBLE32FLAT
377 _start(void)
378 {
379     init_dma();
380
381     debug_serial_setup();
382     dprintf(1, "Start bios (version %s)\n", VERSION);
383
384     if (HaveRunPost)
385         // This is a soft reboot - invoke a hard reboot.
386         tryReboot();
387
388     // Perform main setup code.
389     post();
390 }