Annotation of qemu/roms/seabios/src/post.c, revision 1.1.1.7

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.