base.cc revision 2935
11689SN/A/*
21689SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
39919Ssteve.reinhardt@amd.com * All rights reserved.
41689SN/A *
51689SN/A * Redistribution and use in source and binary forms, with or without
61689SN/A * modification, are permitted provided that the following conditions are
71689SN/A * met: redistributions of source code must retain the above copyright
81689SN/A * notice, this list of conditions and the following disclaimer;
91689SN/A * redistributions in binary form must reproduce the above copyright
101689SN/A * notice, this list of conditions and the following disclaimer in the
111689SN/A * documentation and/or other materials provided with the distribution;
121689SN/A * neither the name of the copyright holders nor the names of its
131689SN/A * contributors may be used to endorse or promote products derived from
141689SN/A * this software without specific prior written permission.
151689SN/A *
161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271689SN/A *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292665Ssaidi@eecs.umich.edu */
301689SN/A
311464SN/A#include "arch/utility.hh"
3211793Sbrandon.potter@amd.com#include "base/cprintf.hh"
3311793Sbrandon.potter@amd.com#include "base/inifile.hh"
341464SN/A#include "base/loader/symtab.hh"
351060SN/A#include "base/misc.hh"
368232Snate@binkert.org#include "base/pollevent.hh"
371060SN/A#include "base/range.hh"
381464SN/A#include "base/stats/events.hh"
391464SN/A#include "base/trace.hh"
409919Ssteve.reinhardt@amd.com#include "cpu/base.hh"
411060SN/A#include "cpu/exetrace.hh"
429919Ssteve.reinhardt@amd.com#include "cpu/profile.hh"
4310537Sandreas.hansson@arm.com#include "cpu/simple/base.hh"
441060SN/A#include "cpu/simple_thread.hh"
452292SN/A#include "cpu/smt.hh"
462292SN/A#include "cpu/static_inst.hh"
471061SN/A#include "cpu/thread_context.hh"
481060SN/A#include "kern/kernel_stats.hh"
499919Ssteve.reinhardt@amd.com#include "mem/packet_impl.hh"
509919Ssteve.reinhardt@amd.com#include "sim/builder.hh"
511060SN/A#include "sim/byteswap.hh"
529919Ssteve.reinhardt@amd.com#include "sim/debug.hh"
539919Ssteve.reinhardt@amd.com#include "sim/host.hh"
549919Ssteve.reinhardt@amd.com#include "sim/sim_events.hh"
559919Ssteve.reinhardt@amd.com#include "sim/sim_object.hh"
569919Ssteve.reinhardt@amd.com#include "sim/stats.hh"
579919Ssteve.reinhardt@amd.com#include "sim/system.hh"
581060SN/A
591060SN/A#if FULL_SYSTEM
601060SN/A#include "base/remote_gdb.hh"
611060SN/A#include "arch/tlb.hh"
621060SN/A#include "arch/stacktrace.hh"
631060SN/A#include "arch/vtophys.hh"
641060SN/A#else // !FULL_SYSTEM
659919Ssteve.reinhardt@amd.com#include "mem/mem_object.hh"
669919Ssteve.reinhardt@amd.com#endif // FULL_SYSTEM
679919Ssteve.reinhardt@amd.com
681060SN/Ausing namespace std;
699919Ssteve.reinhardt@amd.comusing namespace TheISA;
709919Ssteve.reinhardt@amd.com
719919Ssteve.reinhardt@amd.comBaseSimpleCPU::BaseSimpleCPU(Params *p)
729919Ssteve.reinhardt@amd.com    : BaseCPU(p), mem(p->mem), thread(NULL)
731060SN/A{
749919Ssteve.reinhardt@amd.com#if FULL_SYSTEM
751060SN/A    thread = new SimpleThread(this, 0, p->system, p->itb, p->dtb);
769919Ssteve.reinhardt@amd.com#else
779919Ssteve.reinhardt@amd.com    thread = new SimpleThread(this, /* thread_num */ 0, p->process,
789919Ssteve.reinhardt@amd.com            /* asid */ 0, mem);
791060SN/A#endif // !FULL_SYSTEM
801060SN/A
813867Sbinkertn@umich.edu    thread->setStatus(ThreadContext::Suspended);
823867Sbinkertn@umich.edu
833867Sbinkertn@umich.edu    tc = thread->getTC();
841060SN/A
851060SN/A    numInst = 0;
861060SN/A    startNumInst = 0;
879919Ssteve.reinhardt@amd.com    numLoad = 0;
889919Ssteve.reinhardt@amd.com    startNumLoad = 0;
899919Ssteve.reinhardt@amd.com    lastIcacheStall = 0;
909919Ssteve.reinhardt@amd.com    lastDcacheStall = 0;
919919Ssteve.reinhardt@amd.com
929919Ssteve.reinhardt@amd.com    threadContexts.push_back(tc);
939919Ssteve.reinhardt@amd.com}
949919Ssteve.reinhardt@amd.com
959919Ssteve.reinhardt@amd.comBaseSimpleCPU::~BaseSimpleCPU()
969919Ssteve.reinhardt@amd.com{
979919Ssteve.reinhardt@amd.com}
989919Ssteve.reinhardt@amd.com
999919Ssteve.reinhardt@amd.comvoid
1009919Ssteve.reinhardt@amd.comBaseSimpleCPU::deallocateContext(int thread_num)
1019920Syasuko.eckert@amd.com{
10210897Snilay@cs.wisc.edu    // for now, these are equivalent
1039919Ssteve.reinhardt@amd.com    suspendContext(thread_num);
1049919Ssteve.reinhardt@amd.com}
1059919Ssteve.reinhardt@amd.com
1069919Ssteve.reinhardt@amd.com
1079919Ssteve.reinhardt@amd.comvoid
1089919Ssteve.reinhardt@amd.comBaseSimpleCPU::haltContext(int thread_num)
1099919Ssteve.reinhardt@amd.com{
1109919Ssteve.reinhardt@amd.com    // for now, these are equivalent
1119919Ssteve.reinhardt@amd.com    suspendContext(thread_num);
1129919Ssteve.reinhardt@amd.com}
1139919Ssteve.reinhardt@amd.com
1149919Ssteve.reinhardt@amd.com
1159919Ssteve.reinhardt@amd.comvoid
1169919Ssteve.reinhardt@amd.comBaseSimpleCPU::regStats()
1179919Ssteve.reinhardt@amd.com{
1189920Syasuko.eckert@amd.com    using namespace Stats;
1199920Syasuko.eckert@amd.com
1209920Syasuko.eckert@amd.com    BaseCPU::regStats();
1219919Ssteve.reinhardt@amd.com
1229919Ssteve.reinhardt@amd.com    numInsts
1239919Ssteve.reinhardt@amd.com        .name(name() + ".num_insts")
1249919Ssteve.reinhardt@amd.com        .desc("Number of instructions executed")
1259919Ssteve.reinhardt@amd.com        ;
1269919Ssteve.reinhardt@amd.com
1279919Ssteve.reinhardt@amd.com    numMemRefs
1289919Ssteve.reinhardt@amd.com        .name(name() + ".num_refs")
1299919Ssteve.reinhardt@amd.com        .desc("Number of memory references")
1309919Ssteve.reinhardt@amd.com        ;
1311060SN/A
1329919Ssteve.reinhardt@amd.com    notIdleFraction
1331060SN/A        .name(name() + ".not_idle_fraction")
1349919Ssteve.reinhardt@amd.com        .desc("Percentage of non-idle cycles")
1351060SN/A        ;
1369919Ssteve.reinhardt@amd.com
1379919Ssteve.reinhardt@amd.com    idleFraction
1389919Ssteve.reinhardt@amd.com        .name(name() + ".idle_fraction")
1399919Ssteve.reinhardt@amd.com        .desc("Percentage of idle cycles")
1409919Ssteve.reinhardt@amd.com        ;
1419919Ssteve.reinhardt@amd.com
1429919Ssteve.reinhardt@amd.com    icacheStallCycles
1439920Syasuko.eckert@amd.com        .name(name() + ".icache_stall_cycles")
1449920Syasuko.eckert@amd.com        .desc("ICache total stall cycles")
1459920Syasuko.eckert@amd.com        .prereq(icacheStallCycles)
1469919Ssteve.reinhardt@amd.com        ;
1479919Ssteve.reinhardt@amd.com
1489919Ssteve.reinhardt@amd.com    dcacheStallCycles
1499919Ssteve.reinhardt@amd.com        .name(name() + ".dcache_stall_cycles")
1509919Ssteve.reinhardt@amd.com        .desc("DCache total stall cycles")
1519919Ssteve.reinhardt@amd.com        .prereq(dcacheStallCycles)
1521060SN/A        ;
1531060SN/A
1541060SN/A    icacheRetryCycles
1551060SN/A        .name(name() + ".icache_retry_cycles")
1569919Ssteve.reinhardt@amd.com        .desc("ICache total retry cycles")
1571060SN/A        .prereq(icacheRetryCycles)
1589919Ssteve.reinhardt@amd.com        ;
1591060SN/A
1609919Ssteve.reinhardt@amd.com    dcacheRetryCycles
1619919Ssteve.reinhardt@amd.com        .name(name() + ".dcache_retry_cycles")
1629919Ssteve.reinhardt@amd.com        .desc("DCache total retry cycles")
1631060SN/A        .prereq(dcacheRetryCycles)
1649919Ssteve.reinhardt@amd.com        ;
1659919Ssteve.reinhardt@amd.com
1669919Ssteve.reinhardt@amd.com    idleFraction = constant(1.0) - notIdleFraction;
1679920Syasuko.eckert@amd.com}
1689920Syasuko.eckert@amd.com
1699920Syasuko.eckert@amd.comvoid
1709919Ssteve.reinhardt@amd.comBaseSimpleCPU::resetStats()
1719919Ssteve.reinhardt@amd.com{
1729919Ssteve.reinhardt@amd.com    startNumInst = numInst;
1739919Ssteve.reinhardt@amd.com    // notIdleFraction = (_status != Idle);
1749919Ssteve.reinhardt@amd.com}
1759919Ssteve.reinhardt@amd.com
1769919Ssteve.reinhardt@amd.comvoid
1779919Ssteve.reinhardt@amd.comBaseSimpleCPU::serialize(ostream &os)
1789919Ssteve.reinhardt@amd.com{
1799919Ssteve.reinhardt@amd.com    BaseCPU::serialize(os);
1809919Ssteve.reinhardt@amd.com//    SERIALIZE_SCALAR(inst);
1811060SN/A    nameOut(os, csprintf("%s.xc.0", name()));
1821060SN/A    thread->serialize(os);
183}
184
185void
186BaseSimpleCPU::unserialize(Checkpoint *cp, const string &section)
187{
188    BaseCPU::unserialize(cp, section);
189//    UNSERIALIZE_SCALAR(inst);
190    thread->unserialize(cp, csprintf("%s.xc.0", section));
191}
192
193void
194change_thread_state(int thread_number, int activate, int priority)
195{
196}
197
198Fault
199BaseSimpleCPU::copySrcTranslate(Addr src)
200{
201#if 0
202    static bool no_warn = true;
203    int blk_size = (dcacheInterface) ? dcacheInterface->getBlockSize() : 64;
204    // Only support block sizes of 64 atm.
205    assert(blk_size == 64);
206    int offset = src & (blk_size - 1);
207
208    // Make sure block doesn't span page
209    if (no_warn &&
210        (src & PageMask) != ((src + blk_size) & PageMask) &&
211        (src >> 40) != 0xfffffc) {
212        warn("Copied block source spans pages %x.", src);
213        no_warn = false;
214    }
215
216    memReq->reset(src & ~(blk_size - 1), blk_size);
217
218    // translate to physical address
219    Fault fault = thread->translateDataReadReq(req);
220
221    if (fault == NoFault) {
222        thread->copySrcAddr = src;
223        thread->copySrcPhysAddr = memReq->paddr + offset;
224    } else {
225        assert(!fault->isAlignmentFault());
226
227        thread->copySrcAddr = 0;
228        thread->copySrcPhysAddr = 0;
229    }
230    return fault;
231#else
232    return NoFault;
233#endif
234}
235
236Fault
237BaseSimpleCPU::copy(Addr dest)
238{
239#if 0
240    static bool no_warn = true;
241    int blk_size = (dcacheInterface) ? dcacheInterface->getBlockSize() : 64;
242    // Only support block sizes of 64 atm.
243    assert(blk_size == 64);
244    uint8_t data[blk_size];
245    //assert(thread->copySrcAddr);
246    int offset = dest & (blk_size - 1);
247
248    // Make sure block doesn't span page
249    if (no_warn &&
250        (dest & PageMask) != ((dest + blk_size) & PageMask) &&
251        (dest >> 40) != 0xfffffc) {
252        no_warn = false;
253        warn("Copied block destination spans pages %x. ", dest);
254    }
255
256    memReq->reset(dest & ~(blk_size -1), blk_size);
257    // translate to physical address
258    Fault fault = thread->translateDataWriteReq(req);
259
260    if (fault == NoFault) {
261        Addr dest_addr = memReq->paddr + offset;
262        // Need to read straight from memory since we have more than 8 bytes.
263        memReq->paddr = thread->copySrcPhysAddr;
264        thread->mem->read(memReq, data);
265        memReq->paddr = dest_addr;
266        thread->mem->write(memReq, data);
267        if (dcacheInterface) {
268            memReq->cmd = Copy;
269            memReq->completionEvent = NULL;
270            memReq->paddr = thread->copySrcPhysAddr;
271            memReq->dest = dest_addr;
272            memReq->size = 64;
273            memReq->time = curTick;
274            memReq->flags &= ~INST_READ;
275            dcacheInterface->access(memReq);
276        }
277    }
278    else
279        assert(!fault->isAlignmentFault());
280
281    return fault;
282#else
283    panic("copy not implemented");
284    return NoFault;
285#endif
286}
287
288#if FULL_SYSTEM
289Addr
290BaseSimpleCPU::dbg_vtophys(Addr addr)
291{
292    return vtophys(tc, addr);
293}
294#endif // FULL_SYSTEM
295
296#if FULL_SYSTEM
297void
298BaseSimpleCPU::post_interrupt(int int_num, int index)
299{
300    BaseCPU::post_interrupt(int_num, index);
301
302    if (thread->status() == ThreadContext::Suspended) {
303                DPRINTF(IPI,"Suspended Processor awoke\n");
304        thread->activate();
305    }
306}
307#endif // FULL_SYSTEM
308
309void
310BaseSimpleCPU::checkForInterrupts()
311{
312#if FULL_SYSTEM
313    if (checkInterrupts && check_interrupts() && !thread->inPalMode()) {
314        int ipl = 0;
315        int summary = 0;
316        checkInterrupts = false;
317
318        if (thread->readMiscReg(IPR_SIRR)) {
319            for (int i = INTLEVEL_SOFTWARE_MIN;
320                 i < INTLEVEL_SOFTWARE_MAX; i++) {
321                if (thread->readMiscReg(IPR_SIRR) & (ULL(1) << i)) {
322                    // See table 4-19 of 21164 hardware reference
323                    ipl = (i - INTLEVEL_SOFTWARE_MIN) + 1;
324                    summary |= (ULL(1) << i);
325                }
326            }
327        }
328
329        uint64_t interrupts = thread->cpu->intr_status();
330        for (int i = INTLEVEL_EXTERNAL_MIN;
331            i < INTLEVEL_EXTERNAL_MAX; i++) {
332            if (interrupts & (ULL(1) << i)) {
333                // See table 4-19 of 21164 hardware reference
334                ipl = i;
335                summary |= (ULL(1) << i);
336            }
337        }
338
339        if (thread->readMiscReg(IPR_ASTRR))
340            panic("asynchronous traps not implemented\n");
341
342        if (ipl && ipl > thread->readMiscReg(IPR_IPLR)) {
343            thread->setMiscReg(IPR_ISR, summary);
344            thread->setMiscReg(IPR_INTID, ipl);
345
346            Fault(new InterruptFault)->invoke(tc);
347
348            DPRINTF(Flow, "Interrupt! IPLR=%d ipl=%d summary=%x\n",
349                    thread->readMiscReg(IPR_IPLR), ipl, summary);
350        }
351    }
352#endif
353}
354
355
356Fault
357BaseSimpleCPU::setupFetchRequest(Request *req)
358{
359    // set up memory request for instruction fetch
360#if THE_ISA == ALPHA_ISA
361    DPRINTF(Fetch,"Fetch: PC:%08p NPC:%08p",thread->readPC(),
362            thread->readNextPC());
363#else
364    DPRINTF(Fetch,"Fetch: PC:%08p NPC:%08p NNPC:%08p\n",thread->readPC(),
365            thread->readNextPC(),thread->readNextNPC());
366#endif
367
368    req->setVirt(0, thread->readPC() & ~3, sizeof(MachInst),
369                 (FULL_SYSTEM && (thread->readPC() & 1)) ? PHYSICAL : 0,
370                 thread->readPC());
371
372    Fault fault = thread->translateInstReq(req);
373
374    return fault;
375}
376
377
378void
379BaseSimpleCPU::preExecute()
380{
381    // maintain $r0 semantics
382    thread->setIntReg(ZeroReg, 0);
383#if THE_ISA == ALPHA_ISA
384    thread->setFloatReg(ZeroReg, 0.0);
385#endif // ALPHA_ISA
386
387    // keep an instruction count
388    numInst++;
389    numInsts++;
390
391    thread->funcExeInst++;
392
393    // check for instruction-count-based events
394    comInstEventQueue[0]->serviceEvents(numInst);
395
396    // decode the instruction
397    inst = gtoh(inst);
398    curStaticInst = StaticInst::decode(makeExtMI(inst, thread->readPC()));
399
400    traceData = Trace::getInstRecord(curTick, tc, this, curStaticInst,
401                                     thread->readPC());
402
403    DPRINTF(Decode,"Decode: Decoded %s instruction (opcode: 0x%x): 0x%x\n",
404            curStaticInst->getName(), curStaticInst->getOpcode(),
405            curStaticInst->machInst);
406
407#if FULL_SYSTEM
408    thread->setInst(inst);
409#endif // FULL_SYSTEM
410}
411
412void
413BaseSimpleCPU::postExecute()
414{
415#if FULL_SYSTEM
416    if (thread->profile) {
417        bool usermode =
418            (thread->readMiscReg(AlphaISA::IPR_DTB_CM) & 0x18) != 0;
419        thread->profilePC = usermode ? 1 : thread->readPC();
420        ProfileNode *node = thread->profile->consume(tc, inst);
421        if (node)
422            thread->profileNode = node;
423    }
424#endif
425
426    if (curStaticInst->isMemRef()) {
427        numMemRefs++;
428    }
429
430    if (curStaticInst->isLoad()) {
431        ++numLoad;
432        comLoadEventQueue[0]->serviceEvents(numLoad);
433    }
434
435    traceFunctions(thread->readPC());
436
437    if (traceData) {
438        traceData->finalize();
439    }
440}
441
442
443void
444BaseSimpleCPU::advancePC(Fault fault)
445{
446    if (fault != NoFault) {
447        fault->invoke(tc);
448    }
449    else {
450        // go to the next instruction
451        thread->setPC(thread->readNextPC());
452#if THE_ISA == ALPHA_ISA
453        thread->setNextPC(thread->readNextPC() + sizeof(MachInst));
454#else
455        thread->setNextPC(thread->readNextNPC());
456        thread->setNextNPC(thread->readNextNPC() + sizeof(MachInst));
457        assert(thread->readNextPC() != thread->readNextNPC());
458#endif
459
460    }
461
462#if FULL_SYSTEM
463    Addr oldpc;
464    do {
465        oldpc = thread->readPC();
466        system->pcEventQueue.service(tc);
467    } while (oldpc != thread->readPC());
468#endif
469}
470
471