system.cc revision 8852
1/*
2 * Copyright (c) 2007 The Hewlett-Packard Development Company
3 * All rights reserved.
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Gabe Black
38 */
39
40#include "arch/x86/bios/intelmp.hh"
41#include "arch/x86/bios/smbios.hh"
42#include "arch/x86/regs/misc.hh"
43#include "arch/x86/isa_traits.hh"
44#include "arch/x86/system.hh"
45#include "arch/vtophys.hh"
46#include "base/loader/object_file.hh"
47#include "base/loader/symtab.hh"
48#include "base/intmath.hh"
49#include "base/trace.hh"
50#include "cpu/thread_context.hh"
51#include "mem/port_proxy.hh"
52#include "params/X86System.hh"
53#include "sim/byteswap.hh"
54
55using namespace LittleEndianGuest;
56using namespace X86ISA;
57
58X86System::X86System(Params *p) :
59    System(p), smbiosTable(p->smbios_table),
60    mpFloatingPointer(p->intel_mp_pointer),
61    mpConfigTable(p->intel_mp_table),
62    rsdp(p->acpi_description_table_pointer)
63{
64}
65
66static void
67installSegDesc(ThreadContext *tc, SegmentRegIndex seg,
68        SegDescriptor desc, bool longmode)
69{
70    uint64_t base = desc.baseLow + (desc.baseHigh << 24);
71    bool honorBase = !longmode || seg == SEGMENT_REG_FS ||
72                                  seg == SEGMENT_REG_GS ||
73                                  seg == SEGMENT_REG_TSL ||
74                                  seg == SYS_SEGMENT_REG_TR;
75    uint64_t limit = desc.limitLow | (desc.limitHigh << 16);
76
77    SegAttr attr = 0;
78
79    attr.dpl = desc.dpl;
80    attr.unusable = 0;
81    attr.defaultSize = desc.d;
82    attr.longMode = desc.l;
83    attr.avl = desc.avl;
84    attr.granularity = desc.g;
85    attr.present = desc.p;
86    attr.system = desc.s;
87    attr.type = desc.type;
88    if (desc.s) {
89        if (desc.type.codeOrData) {
90            // Code segment
91            attr.expandDown = 0;
92            attr.readable = desc.type.r;
93            attr.writable = 0;
94        } else {
95            // Data segment
96            attr.expandDown = desc.type.e;
97            attr.readable = 1;
98            attr.writable = desc.type.w;
99        }
100    } else {
101        attr.readable = 1;
102        attr.writable = 1;
103        attr.expandDown = 0;
104    }
105
106    tc->setMiscReg(MISCREG_SEG_BASE(seg), base);
107    tc->setMiscReg(MISCREG_SEG_EFF_BASE(seg), honorBase ? base : 0);
108    tc->setMiscReg(MISCREG_SEG_LIMIT(seg), limit);
109    tc->setMiscReg(MISCREG_SEG_ATTR(seg), (MiscReg)attr);
110}
111
112void
113X86System::initState()
114{
115    System::initState();
116
117    if (kernel->getArch() == ObjectFile::I386)
118        fatal("Loading a 32 bit x86 kernel is not supported.\n");
119
120    ThreadContext *tc = threadContexts[0];
121    // This is the boot strap processor (BSP). Initialize it to look like
122    // the boot loader has just turned control over to the 64 bit OS. We
123    // won't actually set up real mode or legacy protected mode descriptor
124    // tables because we aren't executing any code that would require
125    // them. We do, however toggle the control bits in the correct order
126    // while allowing consistency checks and the underlying mechansims
127    // just to be safe.
128
129    const int NumPDTs = 4;
130
131    const Addr PageMapLevel4 = 0x70000;
132    const Addr PageDirPtrTable = 0x71000;
133    const Addr PageDirTable[NumPDTs] =
134        {0x72000, 0x73000, 0x74000, 0x75000};
135    const Addr GDTBase = 0x76000;
136
137    const int PML4Bits = 9;
138    const int PDPTBits = 9;
139    const int PDTBits = 9;
140
141    /*
142     * Set up the gdt.
143     */
144    uint8_t numGDTEntries = 0;
145    // Place holder at selector 0
146    uint64_t nullDescriptor = 0;
147    physProxy.writeBlob(GDTBase + numGDTEntries * 8,
148                        (uint8_t *)(&nullDescriptor), 8);
149    numGDTEntries++;
150
151    //64 bit code segment
152    SegDescriptor csDesc = 0;
153    csDesc.type.codeOrData = 1;
154    csDesc.type.c = 0; // Not conforming
155    csDesc.type.r = 1; // Readable
156    csDesc.dpl = 0; // Privelege level 0
157    csDesc.p = 1; // Present
158    csDesc.l = 1; // 64 bit
159    csDesc.d = 0; // default operand size
160    csDesc.g = 1; // Page granularity
161    csDesc.s = 1; // Not a system segment
162    csDesc.limitHigh = 0xF;
163    csDesc.limitLow = 0xFF;
164    //Because we're dealing with a pointer and I don't think it's
165    //guaranteed that there isn't anything in a nonvirtual class between
166    //it's beginning in memory and it's actual data, we'll use an
167    //intermediary.
168    uint64_t csDescVal = csDesc;
169    physProxy.writeBlob(GDTBase + numGDTEntries * 8,
170                        (uint8_t *)(&csDescVal), 8);
171
172    numGDTEntries++;
173
174    SegSelector cs = 0;
175    cs.si = numGDTEntries - 1;
176
177    tc->setMiscReg(MISCREG_CS, (MiscReg)cs);
178
179    //32 bit data segment
180    SegDescriptor dsDesc = 0;
181    dsDesc.type.codeOrData = 0;
182    dsDesc.type.e = 0; // Not expand down
183    dsDesc.type.w = 1; // Writable
184    dsDesc.dpl = 0; // Privelege level 0
185    dsDesc.p = 1; // Present
186    dsDesc.d = 1; // default operand size
187    dsDesc.g = 1; // Page granularity
188    dsDesc.s = 1; // Not a system segment
189    dsDesc.limitHigh = 0xF;
190    dsDesc.limitLow = 0xFF;
191    uint64_t dsDescVal = dsDesc;
192    physProxy.writeBlob(GDTBase + numGDTEntries * 8,
193                        (uint8_t *)(&dsDescVal), 8);
194
195    numGDTEntries++;
196
197    SegSelector ds = 0;
198    ds.si = numGDTEntries - 1;
199
200    tc->setMiscReg(MISCREG_DS, (MiscReg)ds);
201    tc->setMiscReg(MISCREG_ES, (MiscReg)ds);
202    tc->setMiscReg(MISCREG_FS, (MiscReg)ds);
203    tc->setMiscReg(MISCREG_GS, (MiscReg)ds);
204    tc->setMiscReg(MISCREG_SS, (MiscReg)ds);
205
206    tc->setMiscReg(MISCREG_TSL, 0);
207    tc->setMiscReg(MISCREG_TSG_BASE, GDTBase);
208    tc->setMiscReg(MISCREG_TSG_LIMIT, 8 * numGDTEntries - 1);
209
210    SegDescriptor tssDesc = 0;
211    tssDesc.type = 0xB;
212    tssDesc.dpl = 0; // Privelege level 0
213    tssDesc.p = 1; // Present
214    tssDesc.d = 1; // default operand size
215    tssDesc.g = 1; // Page granularity
216    tssDesc.s = 1; // Not a system segment
217    tssDesc.limitHigh = 0xF;
218    tssDesc.limitLow = 0xFF;
219    uint64_t tssDescVal = tssDesc;
220    physProxy.writeBlob(GDTBase + numGDTEntries * 8,
221                        (uint8_t *)(&tssDescVal), 8);
222
223    numGDTEntries++;
224
225    SegSelector tss = 0;
226    tss.si = numGDTEntries - 1;
227
228    tc->setMiscReg(MISCREG_TR, (MiscReg)tss);
229    installSegDesc(tc, SYS_SEGMENT_REG_TR, tssDesc, true);
230
231    /*
232     * Identity map the first 4GB of memory. In order to map this region
233     * of memory in long mode, there needs to be one actual page map level
234     * 4 entry which points to one page directory pointer table which
235     * points to 4 different page directory tables which are full of two
236     * megabyte pages. All of the other entries in valid tables are set
237     * to indicate that they don't pertain to anything valid and will
238     * cause a fault if used.
239     */
240
241    // Put valid values in all of the various table entries which indicate
242    // that those entries don't point to further tables or pages. Then
243    // set the values of those entries which are needed.
244
245    // Page Map Level 4
246
247    // read/write, user, not present
248    uint64_t pml4e = X86ISA::htog(0x6);
249    for (int offset = 0; offset < (1 << PML4Bits) * 8; offset += 8) {
250        physProxy.writeBlob(PageMapLevel4 + offset, (uint8_t *)(&pml4e), 8);
251    }
252    // Point to the only PDPT
253    pml4e = X86ISA::htog(0x7 | PageDirPtrTable);
254    physProxy.writeBlob(PageMapLevel4, (uint8_t *)(&pml4e), 8);
255
256    // Page Directory Pointer Table
257
258    // read/write, user, not present
259    uint64_t pdpe = X86ISA::htog(0x6);
260    for (int offset = 0; offset < (1 << PDPTBits) * 8; offset += 8) {
261        physProxy.writeBlob(PageDirPtrTable + offset,
262                            (uint8_t *)(&pdpe), 8);
263    }
264    // Point to the PDTs
265    for (int table = 0; table < NumPDTs; table++) {
266        pdpe = X86ISA::htog(0x7 | PageDirTable[table]);
267        physProxy.writeBlob(PageDirPtrTable + table * 8,
268                            (uint8_t *)(&pdpe), 8);
269    }
270
271    // Page Directory Tables
272
273    Addr base = 0;
274    const Addr pageSize = 2 << 20;
275    for (int table = 0; table < NumPDTs; table++) {
276        for (int offset = 0; offset < (1 << PDTBits) * 8; offset += 8) {
277            // read/write, user, present, 4MB
278            uint64_t pdte = X86ISA::htog(0x87 | base);
279            physProxy.writeBlob(PageDirTable[table] + offset,
280                                (uint8_t *)(&pdte), 8);
281            base += pageSize;
282        }
283    }
284
285    /*
286     * Transition from real mode all the way up to Long mode
287     */
288    CR0 cr0 = tc->readMiscRegNoEffect(MISCREG_CR0);
289    //Turn off paging.
290    cr0.pg = 0;
291    tc->setMiscReg(MISCREG_CR0, cr0);
292    //Turn on protected mode.
293    cr0.pe = 1;
294    tc->setMiscReg(MISCREG_CR0, cr0);
295
296    CR4 cr4 = tc->readMiscRegNoEffect(MISCREG_CR4);
297    //Turn on pae.
298    cr4.pae = 1;
299    tc->setMiscReg(MISCREG_CR4, cr4);
300
301    //Point to the page tables.
302    tc->setMiscReg(MISCREG_CR3, PageMapLevel4);
303
304    Efer efer = tc->readMiscRegNoEffect(MISCREG_EFER);
305    //Enable long mode.
306    efer.lme = 1;
307    tc->setMiscReg(MISCREG_EFER, efer);
308
309    //Start using longmode segments.
310    installSegDesc(tc, SEGMENT_REG_CS, csDesc, true);
311    installSegDesc(tc, SEGMENT_REG_DS, dsDesc, true);
312    installSegDesc(tc, SEGMENT_REG_ES, dsDesc, true);
313    installSegDesc(tc, SEGMENT_REG_FS, dsDesc, true);
314    installSegDesc(tc, SEGMENT_REG_GS, dsDesc, true);
315    installSegDesc(tc, SEGMENT_REG_SS, dsDesc, true);
316
317    //Activate long mode.
318    cr0.pg = 1;
319    tc->setMiscReg(MISCREG_CR0, cr0);
320
321    tc->pcState(tc->getSystemPtr()->kernelEntry);
322
323    // We should now be in long mode. Yay!
324
325    Addr ebdaPos = 0xF0000;
326    Addr fixed, table;
327
328    //Write out the SMBios/DMI table
329    writeOutSMBiosTable(ebdaPos, fixed, table);
330    ebdaPos += (fixed + table);
331    ebdaPos = roundUp(ebdaPos, 16);
332
333    //Write out the Intel MP Specification configuration table
334    writeOutMPTable(ebdaPos, fixed, table);
335    ebdaPos += (fixed + table);
336}
337
338void
339X86System::writeOutSMBiosTable(Addr header,
340        Addr &headerSize, Addr &structSize, Addr table)
341{
342    // If the table location isn't specified, just put it after the header.
343    // The header size as of the 2.5 SMBios specification is 0x1F bytes
344    if (!table)
345        table = header + 0x1F;
346    smbiosTable->setTableAddr(table);
347
348    smbiosTable->writeOut(physProxy, header, headerSize, structSize);
349
350    // Do some bounds checking to make sure we at least didn't step on
351    // ourselves.
352    assert(header > table || header + headerSize <= table);
353    assert(table > header || table + structSize <= header);
354}
355
356void
357X86System::writeOutMPTable(Addr fp,
358        Addr &fpSize, Addr &tableSize, Addr table)
359{
360    // If the table location isn't specified and it exists, just put
361    // it after the floating pointer. The fp size as of the 1.4 Intel MP
362    // specification is 0x10 bytes.
363    if (mpConfigTable) {
364        if (!table)
365            table = fp + 0x10;
366        mpFloatingPointer->setTableAddr(table);
367    }
368
369    fpSize = mpFloatingPointer->writeOut(physProxy, fp);
370    if (mpConfigTable)
371        tableSize = mpConfigTable->writeOut(physProxy, table);
372    else
373        tableSize = 0;
374
375    // Do some bounds checking to make sure we at least didn't step on
376    // ourselves and the fp structure was the size we thought it was.
377    assert(fp > table || fp + fpSize <= table);
378    assert(table > fp || table + tableSize <= fp);
379    assert(fpSize == 0x10);
380}
381
382
383X86System::~X86System()
384{
385    delete smbiosTable;
386}
387
388void
389X86System::serialize(std::ostream &os)
390{
391    System::serialize(os);
392}
393
394
395void
396X86System::unserialize(Checkpoint *cp, const std::string &section)
397{
398    System::unserialize(cp,section);
399}
400
401X86System *
402X86SystemParams::create()
403{
404    return new X86System(this);
405}
406