base.cc revision 5217
113521Sgabeblack@google.com/*
213521Sgabeblack@google.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
313521Sgabeblack@google.com * All rights reserved.
413521Sgabeblack@google.com *
513521Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
613521Sgabeblack@google.com * modification, are permitted provided that the following conditions are
713521Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
813521Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
913521Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1013521Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1113521Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1213521Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1313521Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1413521Sgabeblack@google.com * this software without specific prior written permission.
1513521Sgabeblack@google.com *
1613521Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1713521Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1813521Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1913521Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2013521Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2113521Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2213521Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2313521Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2413521Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2513521Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2613521Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2713521Sgabeblack@google.com *
2813521Sgabeblack@google.com * Authors: Steve Reinhardt
2913521Sgabeblack@google.com *          Nathan Binkert
3013521Sgabeblack@google.com */
3113521Sgabeblack@google.com
3213521Sgabeblack@google.com#include <iostream>
3313521Sgabeblack@google.com#include <string>
3413521Sgabeblack@google.com#include <sstream>
3513521Sgabeblack@google.com
3613521Sgabeblack@google.com#include "base/cprintf.hh"
3713521Sgabeblack@google.com#include "base/loader/symtab.hh"
3813521Sgabeblack@google.com#include "base/misc.hh"
3913521Sgabeblack@google.com#include "base/output.hh"
4013521Sgabeblack@google.com#include "cpu/base.hh"
4113521Sgabeblack@google.com#include "cpu/cpuevent.hh"
4213521Sgabeblack@google.com#include "cpu/thread_context.hh"
4313521Sgabeblack@google.com#include "cpu/profile.hh"
4413521Sgabeblack@google.com#include "sim/sim_exit.hh"
4513521Sgabeblack@google.com#include "sim/process.hh"
4613521Sgabeblack@google.com#include "sim/sim_events.hh"
4713521Sgabeblack@google.com#include "sim/system.hh"
4813521Sgabeblack@google.com
4913521Sgabeblack@google.com#include "base/trace.hh"
5013521Sgabeblack@google.com
5113521Sgabeblack@google.com// Hack
5213521Sgabeblack@google.com#include "sim/stat_control.hh"
5313521Sgabeblack@google.com
5413521Sgabeblack@google.comusing namespace std;
5513521Sgabeblack@google.com
5613521Sgabeblack@google.comvector<BaseCPU *> BaseCPU::cpuList;
5713521Sgabeblack@google.com
5813521Sgabeblack@google.com// This variable reflects the max number of threads in any CPU.  Be
5913521Sgabeblack@google.com// careful to only use it once all the CPUs that you care about have
6013521Sgabeblack@google.com// been initialized
6113521Sgabeblack@google.comint maxThreadsPerCPU = 1;
6213521Sgabeblack@google.com
6313521Sgabeblack@google.comCPUProgressEvent::CPUProgressEvent(EventQueue *q, Tick ival,
6413521Sgabeblack@google.com                                   BaseCPU *_cpu)
6513521Sgabeblack@google.com    : Event(q, Event::Progress_Event_Pri), interval(ival),
6613521Sgabeblack@google.com      lastNumInst(0), cpu(_cpu)
6713521Sgabeblack@google.com{
6813521Sgabeblack@google.com    if (interval)
6913521Sgabeblack@google.com        schedule(curTick + interval);
7013521Sgabeblack@google.com}
7113521Sgabeblack@google.com
7213521Sgabeblack@google.comvoid
7313521Sgabeblack@google.comCPUProgressEvent::process()
7413521Sgabeblack@google.com{
7513521Sgabeblack@google.com    Counter temp = cpu->totalInstructions();
7613521Sgabeblack@google.com#ifndef NDEBUG
7713521Sgabeblack@google.com    double ipc = double(temp - lastNumInst) / (interval / cpu->ticks(1));
7813521Sgabeblack@google.com
7913521Sgabeblack@google.com    DPRINTFN("%s progress event, instructions committed: %lli, IPC: %0.8d\n",
8013521Sgabeblack@google.com             cpu->name(), temp - lastNumInst, ipc);
8113521Sgabeblack@google.com    ipc = 0.0;
8213521Sgabeblack@google.com#else
8313521Sgabeblack@google.com    cprintf("%lli: %s progress event, instructions committed: %lli\n",
8413521Sgabeblack@google.com            curTick, cpu->name(), temp - lastNumInst);
8513521Sgabeblack@google.com#endif
8613521Sgabeblack@google.com    lastNumInst = temp;
8713521Sgabeblack@google.com    schedule(curTick + interval);
8813521Sgabeblack@google.com}
8913521Sgabeblack@google.com
9013521Sgabeblack@google.comconst char *
9113521Sgabeblack@google.comCPUProgressEvent::description()
9213521Sgabeblack@google.com{
9313521Sgabeblack@google.com    return "CPU Progress";
9413521Sgabeblack@google.com}
9513521Sgabeblack@google.com
9613521Sgabeblack@google.com#if FULL_SYSTEM
9713521Sgabeblack@google.comBaseCPU::BaseCPU(Params *p)
9813521Sgabeblack@google.com    : MemObject(makeParams(p->name)), clock(p->clock), instCnt(0),
9913521Sgabeblack@google.com      params(p), number_of_threads(p->numberOfThreads), system(p->system),
10013521Sgabeblack@google.com      phase(p->phase)
10113521Sgabeblack@google.com#else
10213521Sgabeblack@google.comBaseCPU::BaseCPU(Params *p)
10313521Sgabeblack@google.com    : MemObject(makeParams(p->name)), clock(p->clock), params(p),
10413521Sgabeblack@google.com      number_of_threads(p->numberOfThreads), system(p->system),
10513521Sgabeblack@google.com      phase(p->phase)
10613521Sgabeblack@google.com#endif
10713521Sgabeblack@google.com{
10813521Sgabeblack@google.com//    currentTick = curTick;
10913521Sgabeblack@google.com
11013521Sgabeblack@google.com    // add self to global list of CPUs
11113521Sgabeblack@google.com    cpuList.push_back(this);
11213521Sgabeblack@google.com
11313521Sgabeblack@google.com    if (number_of_threads > maxThreadsPerCPU)
11413521Sgabeblack@google.com        maxThreadsPerCPU = number_of_threads;
11513521Sgabeblack@google.com
11613521Sgabeblack@google.com    // allocate per-thread instruction-based event queues
11713521Sgabeblack@google.com    comInstEventQueue = new EventQueue *[number_of_threads];
11813521Sgabeblack@google.com    for (int i = 0; i < number_of_threads; ++i)
11913521Sgabeblack@google.com        comInstEventQueue[i] = new EventQueue("instruction-based event queue");
12013521Sgabeblack@google.com
12113521Sgabeblack@google.com    //
12213521Sgabeblack@google.com    // set up instruction-count-based termination events, if any
12313521Sgabeblack@google.com    //
12413521Sgabeblack@google.com    if (p->max_insts_any_thread != 0)
12513521Sgabeblack@google.com        for (int i = 0; i < number_of_threads; ++i)
12613521Sgabeblack@google.com            schedExitSimLoop("a thread reached the max instruction count",
12713521Sgabeblack@google.com                             p->max_insts_any_thread, 0,
12813521Sgabeblack@google.com                             comInstEventQueue[i]);
12913521Sgabeblack@google.com
13013521Sgabeblack@google.com    if (p->max_insts_all_threads != 0) {
13113521Sgabeblack@google.com        // allocate & initialize shared downcounter: each event will
13213521Sgabeblack@google.com        // decrement this when triggered; simulation will terminate
13313521Sgabeblack@google.com        // when counter reaches 0
13413521Sgabeblack@google.com        int *counter = new int;
13513521Sgabeblack@google.com        *counter = number_of_threads;
13613521Sgabeblack@google.com        for (int i = 0; i < number_of_threads; ++i)
13713521Sgabeblack@google.com            new CountedExitEvent(comInstEventQueue[i],
13813521Sgabeblack@google.com                "all threads reached the max instruction count",
13913521Sgabeblack@google.com                p->max_insts_all_threads, *counter);
14013521Sgabeblack@google.com    }
14113521Sgabeblack@google.com
14213521Sgabeblack@google.com    // allocate per-thread load-based event queues
14313521Sgabeblack@google.com    comLoadEventQueue = new EventQueue *[number_of_threads];
14413521Sgabeblack@google.com    for (int i = 0; i < number_of_threads; ++i)
14513521Sgabeblack@google.com        comLoadEventQueue[i] = new EventQueue("load-based event queue");
14613521Sgabeblack@google.com
14713521Sgabeblack@google.com    //
14813521Sgabeblack@google.com    // set up instruction-count-based termination events, if any
14913521Sgabeblack@google.com    //
15013521Sgabeblack@google.com    if (p->max_loads_any_thread != 0)
15113521Sgabeblack@google.com        for (int i = 0; i < number_of_threads; ++i)
15213521Sgabeblack@google.com            schedExitSimLoop("a thread reached the max load count",
15313521Sgabeblack@google.com                             p->max_loads_any_thread, 0,
15413521Sgabeblack@google.com                             comLoadEventQueue[i]);
15513521Sgabeblack@google.com
156    if (p->max_loads_all_threads != 0) {
157        // allocate & initialize shared downcounter: each event will
158        // decrement this when triggered; simulation will terminate
159        // when counter reaches 0
160        int *counter = new int;
161        *counter = number_of_threads;
162        for (int i = 0; i < number_of_threads; ++i)
163            new CountedExitEvent(comLoadEventQueue[i],
164                "all threads reached the max load count",
165                p->max_loads_all_threads, *counter);
166    }
167
168    functionTracingEnabled = false;
169    if (p->functionTrace) {
170        functionTraceStream = simout.find(csprintf("ftrace.%s", name()));
171        currentFunctionStart = currentFunctionEnd = 0;
172        functionEntryTick = p->functionTraceStart;
173
174        if (p->functionTraceStart == 0) {
175            functionTracingEnabled = true;
176        } else {
177            new EventWrapper<BaseCPU, &BaseCPU::enableFunctionTrace>(this,
178                                                                     p->functionTraceStart,
179                                                                     true);
180        }
181    }
182#if FULL_SYSTEM
183    profileEvent = NULL;
184    if (params->profile)
185        profileEvent = new ProfileEvent(this, params->profile);
186#endif
187    tracer = params->tracer;
188}
189
190BaseCPU::Params::Params()
191{
192#if FULL_SYSTEM
193    profile = false;
194#endif
195    checker = NULL;
196    tracer = NULL;
197}
198
199void
200BaseCPU::enableFunctionTrace()
201{
202    functionTracingEnabled = true;
203}
204
205BaseCPU::~BaseCPU()
206{
207}
208
209void
210BaseCPU::init()
211{
212    if (!params->deferRegistration)
213        registerThreadContexts();
214}
215
216void
217BaseCPU::startup()
218{
219#if FULL_SYSTEM
220    if (!params->deferRegistration && profileEvent)
221        profileEvent->schedule(curTick);
222#endif
223
224    if (params->progress_interval) {
225        new CPUProgressEvent(&mainEventQueue,
226                             ticks(params->progress_interval),
227                             this);
228    }
229}
230
231
232void
233BaseCPU::regStats()
234{
235    using namespace Stats;
236
237    numCycles
238        .name(name() + ".numCycles")
239        .desc("number of cpu cycles simulated")
240        ;
241
242    int size = threadContexts.size();
243    if (size > 1) {
244        for (int i = 0; i < size; ++i) {
245            stringstream namestr;
246            ccprintf(namestr, "%s.ctx%d", name(), i);
247            threadContexts[i]->regStats(namestr.str());
248        }
249    } else if (size == 1)
250        threadContexts[0]->regStats(name());
251
252#if FULL_SYSTEM
253#endif
254}
255
256Tick
257BaseCPU::nextCycle()
258{
259    Tick next_tick = curTick - phase + clock - 1;
260    next_tick -= (next_tick % clock);
261    next_tick += phase;
262    return next_tick;
263}
264
265Tick
266BaseCPU::nextCycle(Tick begin_tick)
267{
268    Tick next_tick = begin_tick;
269    if (next_tick % clock != 0)
270        next_tick = next_tick - (next_tick % clock) + clock;
271    next_tick += phase;
272
273    assert(next_tick >= curTick);
274    return next_tick;
275}
276
277void
278BaseCPU::registerThreadContexts()
279{
280    for (int i = 0; i < threadContexts.size(); ++i) {
281        ThreadContext *tc = threadContexts[i];
282
283#if FULL_SYSTEM
284        int id = params->cpu_id;
285        if (id != -1)
286            id += i;
287
288        tc->setCpuId(system->registerThreadContext(tc, id));
289#else
290        tc->setCpuId(tc->getProcessPtr()->registerThreadContext(tc));
291#endif
292    }
293}
294
295
296int
297BaseCPU::findContext(ThreadContext *tc)
298{
299    for (int i = 0; i < threadContexts.size(); ++i) {
300        if (tc == threadContexts[i])
301            return i;
302    }
303    return 0;
304}
305
306void
307BaseCPU::switchOut()
308{
309//    panic("This CPU doesn't support sampling!");
310#if FULL_SYSTEM
311    if (profileEvent && profileEvent->scheduled())
312        profileEvent->deschedule();
313#endif
314}
315
316void
317BaseCPU::takeOverFrom(BaseCPU *oldCPU, Port *ic, Port *dc)
318{
319    assert(threadContexts.size() == oldCPU->threadContexts.size());
320
321    for (int i = 0; i < threadContexts.size(); ++i) {
322        ThreadContext *newTC = threadContexts[i];
323        ThreadContext *oldTC = oldCPU->threadContexts[i];
324
325        newTC->takeOverFrom(oldTC);
326
327        CpuEvent::replaceThreadContext(oldTC, newTC);
328
329        assert(newTC->readCpuId() == oldTC->readCpuId());
330#if FULL_SYSTEM
331        system->replaceThreadContext(newTC, newTC->readCpuId());
332#else
333        assert(newTC->getProcessPtr() == oldTC->getProcessPtr());
334        newTC->getProcessPtr()->replaceThreadContext(newTC, newTC->readCpuId());
335#endif
336
337        if (DTRACE(Context))
338            ThreadContext::compare(oldTC, newTC);
339    }
340
341#if FULL_SYSTEM
342    interrupts = oldCPU->interrupts;
343
344    for (int i = 0; i < threadContexts.size(); ++i)
345        threadContexts[i]->profileClear();
346
347    if (profileEvent)
348        profileEvent->schedule(curTick);
349#endif
350
351    // Connect new CPU to old CPU's memory only if new CPU isn't
352    // connected to anything.  Also connect old CPU's memory to new
353    // CPU.
354    Port *peer;
355    if (ic->getPeer() == NULL) {
356        peer = oldCPU->getPort("icache_port")->getPeer();
357        ic->setPeer(peer);
358    } else {
359        peer = ic->getPeer();
360    }
361    peer->setPeer(ic);
362
363    if (dc->getPeer() == NULL) {
364        peer = oldCPU->getPort("dcache_port")->getPeer();
365        dc->setPeer(peer);
366    } else {
367        peer = dc->getPeer();
368    }
369    peer->setPeer(dc);
370}
371
372
373#if FULL_SYSTEM
374BaseCPU::ProfileEvent::ProfileEvent(BaseCPU *_cpu, int _interval)
375    : Event(&mainEventQueue), cpu(_cpu), interval(_interval)
376{ }
377
378void
379BaseCPU::ProfileEvent::process()
380{
381    for (int i = 0, size = cpu->threadContexts.size(); i < size; ++i) {
382        ThreadContext *tc = cpu->threadContexts[i];
383        tc->profileSample();
384    }
385
386    schedule(curTick + interval);
387}
388
389void
390BaseCPU::post_interrupt(int int_num, int index)
391{
392    interrupts.post(int_num, index);
393}
394
395void
396BaseCPU::clear_interrupt(int int_num, int index)
397{
398    interrupts.clear(int_num, index);
399}
400
401void
402BaseCPU::clear_interrupts()
403{
404    interrupts.clear_all();
405}
406
407uint64_t
408BaseCPU::get_interrupts(int int_num)
409{
410    return interrupts.get_vec(int_num);
411}
412
413void
414BaseCPU::serialize(std::ostream &os)
415{
416    SERIALIZE_SCALAR(instCnt);
417    interrupts.serialize(os);
418}
419
420void
421BaseCPU::unserialize(Checkpoint *cp, const std::string &section)
422{
423    UNSERIALIZE_SCALAR(instCnt);
424    interrupts.unserialize(cp, section);
425}
426
427#endif // FULL_SYSTEM
428
429void
430BaseCPU::traceFunctionsInternal(Addr pc)
431{
432    if (!debugSymbolTable)
433        return;
434
435    // if pc enters different function, print new function symbol and
436    // update saved range.  Otherwise do nothing.
437    if (pc < currentFunctionStart || pc >= currentFunctionEnd) {
438        string sym_str;
439        bool found = debugSymbolTable->findNearestSymbol(pc, sym_str,
440                                                         currentFunctionStart,
441                                                         currentFunctionEnd);
442
443        if (!found) {
444            // no symbol found: use addr as label
445            sym_str = csprintf("0x%x", pc);
446            currentFunctionStart = pc;
447            currentFunctionEnd = pc + 1;
448        }
449
450        ccprintf(*functionTraceStream, " (%d)\n%d: %s",
451                 curTick - functionEntryTick, curTick, sym_str);
452        functionEntryTick = curTick;
453    }
454}
455