system.cc revision 5612
15132Sgblack@eecs.umich.edu/* 25132Sgblack@eecs.umich.edu * Copyright (c) 2007 The Hewlett-Packard Development Company 35132Sgblack@eecs.umich.edu * All rights reserved. 45132Sgblack@eecs.umich.edu * 55132Sgblack@eecs.umich.edu * Redistribution and use of this software in source and binary forms, 65132Sgblack@eecs.umich.edu * with or without modification, are permitted provided that the 75132Sgblack@eecs.umich.edu * following conditions are met: 85132Sgblack@eecs.umich.edu * 95132Sgblack@eecs.umich.edu * The software must be used only for Non-Commercial Use which means any 105132Sgblack@eecs.umich.edu * use which is NOT directed to receiving any direct monetary 115132Sgblack@eecs.umich.edu * compensation for, or commercial advantage from such use. Illustrative 125132Sgblack@eecs.umich.edu * examples of non-commercial use are academic research, personal study, 135132Sgblack@eecs.umich.edu * teaching, education and corporate research & development. 145132Sgblack@eecs.umich.edu * Illustrative examples of commercial use are distributing products for 155132Sgblack@eecs.umich.edu * commercial advantage and providing services using the software for 165132Sgblack@eecs.umich.edu * commercial advantage. 175132Sgblack@eecs.umich.edu * 185132Sgblack@eecs.umich.edu * If you wish to use this software or functionality therein that may be 195132Sgblack@eecs.umich.edu * covered by patents for commercial use, please contact: 205132Sgblack@eecs.umich.edu * Director of Intellectual Property Licensing 215132Sgblack@eecs.umich.edu * Office of Strategy and Technology 225132Sgblack@eecs.umich.edu * Hewlett-Packard Company 235132Sgblack@eecs.umich.edu * 1501 Page Mill Road 245132Sgblack@eecs.umich.edu * Palo Alto, California 94304 255132Sgblack@eecs.umich.edu * 265132Sgblack@eecs.umich.edu * Redistributions of source code must retain the above copyright notice, 275132Sgblack@eecs.umich.edu * this list of conditions and the following disclaimer. Redistributions 285132Sgblack@eecs.umich.edu * in binary form must reproduce the above copyright notice, this list of 295132Sgblack@eecs.umich.edu * conditions and the following disclaimer in the documentation and/or 305132Sgblack@eecs.umich.edu * other materials provided with the distribution. Neither the name of 315132Sgblack@eecs.umich.edu * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its 325132Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from 335132Sgblack@eecs.umich.edu * this software without specific prior written permission. No right of 345132Sgblack@eecs.umich.edu * sublicense is granted herewith. Derivatives of the software and 355132Sgblack@eecs.umich.edu * output created using the software may be prepared, but only for 365132Sgblack@eecs.umich.edu * Non-Commercial Uses. Derivatives of the software may be shared with 375132Sgblack@eecs.umich.edu * others provided: (i) the others agree to abide by the list of 385132Sgblack@eecs.umich.edu * conditions herein which includes the Non-Commercial Use restrictions; 395132Sgblack@eecs.umich.edu * and (ii) such Derivatives of the software include the above copyright 405132Sgblack@eecs.umich.edu * notice to acknowledge the contribution from this software where 415132Sgblack@eecs.umich.edu * applicable, this list of conditions and the disclaimer below. 425132Sgblack@eecs.umich.edu * 435132Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 445132Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 455132Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 465132Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 475132Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 485132Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 495132Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 505132Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 515132Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 525132Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 535132Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 545132Sgblack@eecs.umich.edu * 555132Sgblack@eecs.umich.edu * Authors: Gabe Black 565132Sgblack@eecs.umich.edu */ 575132Sgblack@eecs.umich.edu 585612Sgblack@eecs.umich.edu#include "arch/x86/bios/smbios.hh" 595299Sgblack@eecs.umich.edu#include "arch/x86/miscregs.hh" 605132Sgblack@eecs.umich.edu#include "arch/x86/system.hh" 615132Sgblack@eecs.umich.edu#include "arch/vtophys.hh" 625132Sgblack@eecs.umich.edu#include "base/remote_gdb.hh" 635132Sgblack@eecs.umich.edu#include "base/loader/object_file.hh" 645132Sgblack@eecs.umich.edu#include "base/loader/symtab.hh" 655132Sgblack@eecs.umich.edu#include "base/trace.hh" 665299Sgblack@eecs.umich.edu#include "cpu/thread_context.hh" 675132Sgblack@eecs.umich.edu#include "mem/physical.hh" 685132Sgblack@eecs.umich.edu#include "params/X86System.hh" 695132Sgblack@eecs.umich.edu#include "sim/byteswap.hh" 705132Sgblack@eecs.umich.edu 715132Sgblack@eecs.umich.edu 725299Sgblack@eecs.umich.eduusing namespace LittleEndianGuest; 735299Sgblack@eecs.umich.eduusing namespace X86ISA; 745132Sgblack@eecs.umich.edu 755132Sgblack@eecs.umich.eduX86System::X86System(Params *p) 765132Sgblack@eecs.umich.edu : System(p) 775132Sgblack@eecs.umich.edu{ 785334Sgblack@eecs.umich.edu smbiosTable = new X86ISA::SMBios::SMBiosTable; 795334Sgblack@eecs.umich.edu smbiosTable->smbiosHeader.majorVersion = 2; 805334Sgblack@eecs.umich.edu smbiosTable->smbiosHeader.minorVersion = 5; 815334Sgblack@eecs.umich.edu smbiosTable->smbiosHeader.intermediateHeader.smbiosBCDRevision = 0x25; 825132Sgblack@eecs.umich.edu} 835132Sgblack@eecs.umich.edu 845299Sgblack@eecs.umich.eduvoid 855299Sgblack@eecs.umich.eduX86System::startup() 865299Sgblack@eecs.umich.edu{ 875299Sgblack@eecs.umich.edu System::startup(); 885299Sgblack@eecs.umich.edu // This is the boot strap processor (BSP). Initialize it to look like 895299Sgblack@eecs.umich.edu // the boot loader has just turned control over to the 64 bit OS. We 905299Sgblack@eecs.umich.edu // won't actually set up real mode or legacy protected mode descriptor 915299Sgblack@eecs.umich.edu // tables because we aren't executing any code that would require 925299Sgblack@eecs.umich.edu // them. We do, however toggle the control bits in the correct order 935299Sgblack@eecs.umich.edu // while allowing consistency checks and the underlying mechansims 945299Sgblack@eecs.umich.edu // just to be safe. 955299Sgblack@eecs.umich.edu 965299Sgblack@eecs.umich.edu const int NumPDTs = 4; 975299Sgblack@eecs.umich.edu 985299Sgblack@eecs.umich.edu const Addr PageMapLevel4 = 0x70000; 995299Sgblack@eecs.umich.edu const Addr PageDirPtrTable = 0x71000; 1005299Sgblack@eecs.umich.edu const Addr PageDirTable[NumPDTs] = 1015299Sgblack@eecs.umich.edu {0x72000, 0x73000, 0x74000, 0x75000}; 1025299Sgblack@eecs.umich.edu const Addr GDTBase = 0x76000; 1035299Sgblack@eecs.umich.edu 1045299Sgblack@eecs.umich.edu const int PML4Bits = 9; 1055299Sgblack@eecs.umich.edu const int PDPTBits = 9; 1065299Sgblack@eecs.umich.edu const int PDTBits = 9; 1075299Sgblack@eecs.umich.edu 1085299Sgblack@eecs.umich.edu // Get a port to write the page tables and descriptor tables. 1095299Sgblack@eecs.umich.edu FunctionalPort * physPort = threadContexts[0]->getPhysPort(); 1105299Sgblack@eecs.umich.edu 1115299Sgblack@eecs.umich.edu /* 1125299Sgblack@eecs.umich.edu * Set up the gdt. 1135299Sgblack@eecs.umich.edu */ 1145299Sgblack@eecs.umich.edu // Place holder at selector 0 1155299Sgblack@eecs.umich.edu uint64_t nullDescriptor = 0; 1165299Sgblack@eecs.umich.edu physPort->writeBlob(GDTBase, (uint8_t *)(&nullDescriptor), 8); 1175299Sgblack@eecs.umich.edu 1185299Sgblack@eecs.umich.edu //64 bit code segment 1195299Sgblack@eecs.umich.edu SegDescriptor csDesc = 0; 1205299Sgblack@eecs.umich.edu csDesc.type.c = 0; // Not conforming 1215299Sgblack@eecs.umich.edu csDesc.dpl = 0; // Privelege level 0 1225299Sgblack@eecs.umich.edu csDesc.p = 1; // Present 1235299Sgblack@eecs.umich.edu csDesc.l = 1; // 64 bit 1245299Sgblack@eecs.umich.edu csDesc.d = 0; // default operand size 1255299Sgblack@eecs.umich.edu //Because we're dealing with a pointer and I don't think it's 1265299Sgblack@eecs.umich.edu //guaranteed that there isn't anything in a nonvirtual class between 1275299Sgblack@eecs.umich.edu //it's beginning in memory and it's actual data, we'll use an 1285299Sgblack@eecs.umich.edu //intermediary. 1295299Sgblack@eecs.umich.edu uint64_t csDescVal = csDesc; 1305299Sgblack@eecs.umich.edu physPort->writeBlob(GDTBase, (uint8_t *)(&csDescVal), 8); 1315299Sgblack@eecs.umich.edu 1325299Sgblack@eecs.umich.edu threadContexts[0]->setMiscReg(MISCREG_TSG_BASE, GDTBase); 1335299Sgblack@eecs.umich.edu threadContexts[0]->setMiscReg(MISCREG_TSG_LIMIT, 0xF); 1345299Sgblack@eecs.umich.edu 1355299Sgblack@eecs.umich.edu /* 1365299Sgblack@eecs.umich.edu * Identity map the first 4GB of memory. In order to map this region 1375299Sgblack@eecs.umich.edu * of memory in long mode, there needs to be one actual page map level 1385299Sgblack@eecs.umich.edu * 4 entry which points to one page directory pointer table which 1395299Sgblack@eecs.umich.edu * points to 4 different page directory tables which are full of two 1405299Sgblack@eecs.umich.edu * megabyte pages. All of the other entries in valid tables are set 1415299Sgblack@eecs.umich.edu * to indicate that they don't pertain to anything valid and will 1425299Sgblack@eecs.umich.edu * cause a fault if used. 1435299Sgblack@eecs.umich.edu */ 1445299Sgblack@eecs.umich.edu 1455299Sgblack@eecs.umich.edu // Put valid values in all of the various table entries which indicate 1465299Sgblack@eecs.umich.edu // that those entries don't point to further tables or pages. Then 1475299Sgblack@eecs.umich.edu // set the values of those entries which are needed. 1485299Sgblack@eecs.umich.edu 1495299Sgblack@eecs.umich.edu // Page Map Level 4 1505299Sgblack@eecs.umich.edu 1515299Sgblack@eecs.umich.edu // read/write, user, not present 1525299Sgblack@eecs.umich.edu uint64_t pml4e = X86ISA::htog(0x6); 1535299Sgblack@eecs.umich.edu for (int offset = 0; offset < (1 << PML4Bits) * 8; offset += 8) { 1545299Sgblack@eecs.umich.edu physPort->writeBlob(PageMapLevel4 + offset, (uint8_t *)(&pml4e), 8); 1555299Sgblack@eecs.umich.edu } 1565299Sgblack@eecs.umich.edu // Point to the only PDPT 1575299Sgblack@eecs.umich.edu pml4e = X86ISA::htog(0x7 | PageDirPtrTable); 1585299Sgblack@eecs.umich.edu physPort->writeBlob(PageMapLevel4, (uint8_t *)(&pml4e), 8); 1595299Sgblack@eecs.umich.edu 1605299Sgblack@eecs.umich.edu // Page Directory Pointer Table 1615299Sgblack@eecs.umich.edu 1625299Sgblack@eecs.umich.edu // read/write, user, not present 1635299Sgblack@eecs.umich.edu uint64_t pdpe = X86ISA::htog(0x6); 1645299Sgblack@eecs.umich.edu for (int offset = 0; offset < (1 << PDPTBits) * 8; offset += 8) { 1655299Sgblack@eecs.umich.edu physPort->writeBlob(PageDirPtrTable + offset, 1665299Sgblack@eecs.umich.edu (uint8_t *)(&pdpe), 8); 1675299Sgblack@eecs.umich.edu } 1685299Sgblack@eecs.umich.edu // Point to the PDTs 1695299Sgblack@eecs.umich.edu for (int table = 0; table < NumPDTs; table++) { 1705299Sgblack@eecs.umich.edu pdpe = X86ISA::htog(0x7 | PageDirTable[table]); 1715299Sgblack@eecs.umich.edu physPort->writeBlob(PageDirPtrTable + table * 8, 1725299Sgblack@eecs.umich.edu (uint8_t *)(&pdpe), 8); 1735299Sgblack@eecs.umich.edu } 1745299Sgblack@eecs.umich.edu 1755299Sgblack@eecs.umich.edu // Page Directory Tables 1765299Sgblack@eecs.umich.edu 1775299Sgblack@eecs.umich.edu Addr base = 0; 1785299Sgblack@eecs.umich.edu const Addr pageSize = 2 << 20; 1795299Sgblack@eecs.umich.edu for (int table = 0; table < NumPDTs; table++) { 1805299Sgblack@eecs.umich.edu for (int offset = 0; offset < (1 << PDTBits) * 8; offset += 8) { 1815299Sgblack@eecs.umich.edu // read/write, user, present, 4MB 1825299Sgblack@eecs.umich.edu uint64_t pdte = X86ISA::htog(0x87 | base); 1835299Sgblack@eecs.umich.edu physPort->writeBlob(PageDirTable[table] + offset, 1845299Sgblack@eecs.umich.edu (uint8_t *)(&pdte), 8); 1855299Sgblack@eecs.umich.edu base += pageSize; 1865299Sgblack@eecs.umich.edu } 1875299Sgblack@eecs.umich.edu } 1885299Sgblack@eecs.umich.edu 1895299Sgblack@eecs.umich.edu /* 1905299Sgblack@eecs.umich.edu * Transition from real mode all the way up to Long mode 1915299Sgblack@eecs.umich.edu */ 1925299Sgblack@eecs.umich.edu CR0 cr0 = threadContexts[0]->readMiscRegNoEffect(MISCREG_CR0); 1935299Sgblack@eecs.umich.edu //Turn off paging. 1945299Sgblack@eecs.umich.edu cr0.pg = 0; 1955299Sgblack@eecs.umich.edu threadContexts[0]->setMiscReg(MISCREG_CR0, cr0); 1965299Sgblack@eecs.umich.edu //Turn on protected mode. 1975299Sgblack@eecs.umich.edu cr0.pe = 1; 1985299Sgblack@eecs.umich.edu threadContexts[0]->setMiscReg(MISCREG_CR0, cr0); 1995299Sgblack@eecs.umich.edu 2005299Sgblack@eecs.umich.edu CR4 cr4 = threadContexts[0]->readMiscRegNoEffect(MISCREG_CR4); 2015299Sgblack@eecs.umich.edu //Turn on pae. 2025299Sgblack@eecs.umich.edu cr4.pae = 1; 2035299Sgblack@eecs.umich.edu threadContexts[0]->setMiscReg(MISCREG_CR4, cr4); 2045299Sgblack@eecs.umich.edu 2055299Sgblack@eecs.umich.edu //Point to the page tables. 2065299Sgblack@eecs.umich.edu threadContexts[0]->setMiscReg(MISCREG_CR3, PageMapLevel4); 2075299Sgblack@eecs.umich.edu 2085299Sgblack@eecs.umich.edu Efer efer = threadContexts[0]->readMiscRegNoEffect(MISCREG_EFER); 2095299Sgblack@eecs.umich.edu //Enable long mode. 2105299Sgblack@eecs.umich.edu efer.lme = 1; 2115299Sgblack@eecs.umich.edu threadContexts[0]->setMiscReg(MISCREG_EFER, efer); 2125299Sgblack@eecs.umich.edu 2135299Sgblack@eecs.umich.edu //Activate long mode. 2145299Sgblack@eecs.umich.edu cr0.pg = 1; 2155299Sgblack@eecs.umich.edu threadContexts[0]->setMiscReg(MISCREG_CR0, cr0); 2165299Sgblack@eecs.umich.edu 2175299Sgblack@eecs.umich.edu /* 2185299Sgblack@eecs.umich.edu * Far jump into 64 bit mode. 2195299Sgblack@eecs.umich.edu */ 2205299Sgblack@eecs.umich.edu // Set the selector 2215299Sgblack@eecs.umich.edu threadContexts[0]->setMiscReg(MISCREG_CS, 1); 2225299Sgblack@eecs.umich.edu // Manually set up the segment attributes. In the future when there's 2235299Sgblack@eecs.umich.edu // other existing functionality to do this, that could be used 2245299Sgblack@eecs.umich.edu // instead. 2255299Sgblack@eecs.umich.edu SegAttr csAttr = 0; 2265299Sgblack@eecs.umich.edu csAttr.writable = 0; 2275299Sgblack@eecs.umich.edu csAttr.readable = 1; 2285299Sgblack@eecs.umich.edu csAttr.expandDown = 0; 2295299Sgblack@eecs.umich.edu csAttr.dpl = 0; 2305299Sgblack@eecs.umich.edu csAttr.defaultSize = 0; 2315299Sgblack@eecs.umich.edu csAttr.longMode = 1; 2325299Sgblack@eecs.umich.edu threadContexts[0]->setMiscReg(MISCREG_CS_ATTR, csAttr); 2335299Sgblack@eecs.umich.edu 2345299Sgblack@eecs.umich.edu threadContexts[0]->setPC(threadContexts[0]->getSystemPtr()->kernelEntry); 2355299Sgblack@eecs.umich.edu threadContexts[0]->setNextPC(threadContexts[0]->readPC()); 2365299Sgblack@eecs.umich.edu 2375299Sgblack@eecs.umich.edu // We should now be in long mode. Yay! 2385334Sgblack@eecs.umich.edu 2395334Sgblack@eecs.umich.edu //Write out the SMBios/DMI table 2405334Sgblack@eecs.umich.edu writeOutSMBiosTable(0xF0000); 2415299Sgblack@eecs.umich.edu} 2425299Sgblack@eecs.umich.edu 2435334Sgblack@eecs.umich.eduvoid 2445334Sgblack@eecs.umich.eduX86System::writeOutSMBiosTable(Addr header, Addr table) 2455334Sgblack@eecs.umich.edu{ 2465334Sgblack@eecs.umich.edu // Get a port to write the table and header to memory. 2475334Sgblack@eecs.umich.edu FunctionalPort * physPort = threadContexts[0]->getPhysPort(); 2485334Sgblack@eecs.umich.edu 2495334Sgblack@eecs.umich.edu // If the table location isn't specified, just put it after the header. 2505334Sgblack@eecs.umich.edu // The header size as of the 2.5 SMBios specification is 0x1F bytes 2515334Sgblack@eecs.umich.edu if (!table) { 2525334Sgblack@eecs.umich.edu if (!smbiosTable->smbiosHeader.intermediateHeader.tableAddr) 2535334Sgblack@eecs.umich.edu smbiosTable->smbiosHeader. 2545334Sgblack@eecs.umich.edu intermediateHeader.tableAddr = header + 0x1F; 2555334Sgblack@eecs.umich.edu } else { 2565334Sgblack@eecs.umich.edu smbiosTable->smbiosHeader.intermediateHeader.tableAddr = table; 2575334Sgblack@eecs.umich.edu } 2585334Sgblack@eecs.umich.edu 2595334Sgblack@eecs.umich.edu smbiosTable->writeOut(physPort, header); 2605334Sgblack@eecs.umich.edu} 2615334Sgblack@eecs.umich.edu 2625334Sgblack@eecs.umich.edu 2635132Sgblack@eecs.umich.eduX86System::~X86System() 2645132Sgblack@eecs.umich.edu{ 2655334Sgblack@eecs.umich.edu delete smbiosTable; 2665132Sgblack@eecs.umich.edu} 2675132Sgblack@eecs.umich.edu 2685132Sgblack@eecs.umich.eduvoid 2695132Sgblack@eecs.umich.eduX86System::serialize(std::ostream &os) 2705132Sgblack@eecs.umich.edu{ 2715132Sgblack@eecs.umich.edu System::serialize(os); 2725132Sgblack@eecs.umich.edu} 2735132Sgblack@eecs.umich.edu 2745132Sgblack@eecs.umich.edu 2755132Sgblack@eecs.umich.eduvoid 2765132Sgblack@eecs.umich.eduX86System::unserialize(Checkpoint *cp, const std::string §ion) 2775132Sgblack@eecs.umich.edu{ 2785132Sgblack@eecs.umich.edu System::unserialize(cp,section); 2795132Sgblack@eecs.umich.edu} 2805132Sgblack@eecs.umich.edu 2815132Sgblack@eecs.umich.eduX86System * 2825132Sgblack@eecs.umich.eduX86SystemParams::create() 2835132Sgblack@eecs.umich.edu{ 2845132Sgblack@eecs.umich.edu return new X86System(this); 2855132Sgblack@eecs.umich.edu} 286