base.cc revision 2107
12139SN/A/*
22139SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32139SN/A * All rights reserved.
42139SN/A *
52139SN/A * Redistribution and use in source and binary forms, with or without
62139SN/A * modification, are permitted provided that the following conditions are
72139SN/A * met: redistributions of source code must retain the above copyright
82139SN/A * notice, this list of conditions and the following disclaimer;
92139SN/A * redistributions in binary form must reproduce the above copyright
102139SN/A * notice, this list of conditions and the following disclaimer in the
112139SN/A * documentation and/or other materials provided with the distribution;
122139SN/A * neither the name of the copyright holders nor the names of its
132139SN/A * contributors may be used to endorse or promote products derived from
142139SN/A * this software without specific prior written permission.
152139SN/A *
162139SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172139SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182139SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192139SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202139SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212139SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222139SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232139SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242139SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252139SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262139SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272139SN/A */
282665Ssaidi@eecs.umich.edu
292665Ssaidi@eecs.umich.edu#include <iostream>
302139SN/A#include <string>
314202Sbinkertn@umich.edu#include <sstream>
322139SN/A
334202Sbinkertn@umich.edu#include "base/cprintf.hh"
342152SN/A#include "base/loader/symtab.hh"
352152SN/A#include "base/misc.hh"
362139SN/A#include "base/output.hh"
372139SN/A#include "cpu/base.hh"
382139SN/A#include "cpu/exec_context.hh"
392139SN/A#include "cpu/profile.hh"
402139SN/A#include "cpu/sampler/sampler.hh"
412152SN/A#include "sim/param.hh"
422152SN/A#include "sim/sim_events.hh"
432139SN/A
442139SN/A#include "base/trace.hh"
452139SN/A
464781Snate@binkert.orgusing namespace std;
474781Snate@binkert.org
484781Snate@binkert.orgvector<BaseCPU *> BaseCPU::cpuList;
494781Snate@binkert.org
504781Snate@binkert.org// This variable reflects the max number of threads in any CPU.  Be
513170Sstever@eecs.umich.edu// careful to only use it once all the CPUs that you care about have
525664Sgblack@eecs.umich.edu// been initialized
533806Ssaidi@eecs.umich.eduint maxThreadsPerCPU = 1;
546179Sksewell@umich.edu
554781Snate@binkert.org#if FULL_SYSTEM
564781Snate@binkert.orgBaseCPU::BaseCPU(Params *p)
574781Snate@binkert.org    : SimObject(p->name), clock(p->clock), checkInterrupts(true),
584781Snate@binkert.org      params(p), number_of_threads(p->numberOfThreads), system(p->system)
594781Snate@binkert.org#else
604781Snate@binkert.orgBaseCPU::BaseCPU(Params *p)
614781Snate@binkert.org    : SimObject(p->name), clock(p->clock), params(p),
624781Snate@binkert.org      number_of_threads(p->numberOfThreads)
634781Snate@binkert.org#endif
642139SN/A{
652139SN/A    DPRINTF(FullCPU, "BaseCPU: Creating object, mem address %#x.\n", this);
663546Sgblack@eecs.umich.edu
674202Sbinkertn@umich.edu    // add self to global list of CPUs
682152SN/A    cpuList.push_back(this);
692152SN/A
702152SN/A    DPRINTF(FullCPU, "BaseCPU: CPU added to cpuList, mem address %#x.\n",
712152SN/A            this);
722152SN/A
732152SN/A    if (number_of_threads > maxThreadsPerCPU)
742152SN/A        maxThreadsPerCPU = number_of_threads;
752152SN/A
762152SN/A    // allocate per-thread instruction-based event queues
772152SN/A    comInstEventQueue = new EventQueue *[number_of_threads];
782152SN/A    for (int i = 0; i < number_of_threads; ++i)
792152SN/A        comInstEventQueue[i] = new EventQueue("instruction-based event queue");
802504SN/A
812504SN/A    //
822504SN/A    // set up instruction-count-based termination events, if any
832504SN/A    //
842152SN/A    if (p->max_insts_any_thread != 0)
852504SN/A        for (int i = 0; i < number_of_threads; ++i)
862152SN/A            new SimExitEvent(comInstEventQueue[i], p->max_insts_any_thread,
872152SN/A                "a thread reached the max instruction count");
882152SN/A
892152SN/A    if (p->max_insts_all_threads != 0) {
902152SN/A        // allocate & initialize shared downcounter: each event will
912152SN/A        // decrement this when triggered; simulation will terminate
922152SN/A        // when counter reaches 0
932152SN/A        int *counter = new int;
942632Sstever@eecs.umich.edu        *counter = number_of_threads;
952155SN/A        for (int i = 0; i < number_of_threads; ++i)
962155SN/A            new CountedExitEvent(comInstEventQueue[i],
972155SN/A                "all threads reached the max instruction count",
982155SN/A                p->max_insts_all_threads, *counter);
992155SN/A    }
1002155SN/A
1015228Sgblack@eecs.umich.edu    // allocate per-thread load-based event queues
1022155SN/A    comLoadEventQueue = new EventQueue *[number_of_threads];
1032155SN/A    for (int i = 0; i < number_of_threads; ++i)
1042155SN/A        comLoadEventQueue[i] = new EventQueue("load-based event queue");
1052152SN/A
1062766Sktlim@umich.edu    //
1072766Sktlim@umich.edu    // set up instruction-count-based termination events, if any
1082766Sktlim@umich.edu    //
1092766Sktlim@umich.edu    if (p->max_loads_any_thread != 0)
1102766Sktlim@umich.edu        for (int i = 0; i < number_of_threads; ++i)
1112152SN/A            new SimExitEvent(comLoadEventQueue[i], p->max_loads_any_thread,
1122152SN/A                "a thread reached the max load count");
1132152SN/A
1142155SN/A    if (p->max_loads_all_threads != 0) {
1152152SN/A        // allocate & initialize shared downcounter: each event will
1162152SN/A        // decrement this when triggered; simulation will terminate
1172718Sstever@eecs.umich.edu        // when counter reaches 0
1182921Sktlim@umich.edu        int *counter = new int;
1192921Sktlim@umich.edu        *counter = number_of_threads;
1202921Sktlim@umich.edu        for (int i = 0; i < number_of_threads; ++i)
1212921Sktlim@umich.edu            new CountedExitEvent(comLoadEventQueue[i],
1222921Sktlim@umich.edu                "all threads reached the max load count",
1232921Sktlim@umich.edu                p->max_loads_all_threads, *counter);
1242921Sktlim@umich.edu    }
1252921Sktlim@umich.edu
1262921Sktlim@umich.edu#if FULL_SYSTEM
1272152SN/A    memset(interrupts, 0, sizeof(interrupts));
1282152SN/A    intstatus = 0;
1295944Sgblack@eecs.umich.edu#endif
1305944Sgblack@eecs.umich.edu
1315944Sgblack@eecs.umich.edu    functionTracingEnabled = false;
1325944Sgblack@eecs.umich.edu    if (p->functionTrace) {
1335944Sgblack@eecs.umich.edu        functionTraceStream = simout.find(csprintf("ftrace.%s", name()));
134        currentFunctionStart = currentFunctionEnd = 0;
135        functionEntryTick = p->functionTraceStart;
136
137        if (p->functionTraceStart == 0) {
138            functionTracingEnabled = true;
139        } else {
140            Event *e =
141                new EventWrapper<BaseCPU, &BaseCPU::enableFunctionTrace>(this,
142                                                                         true);
143            e->schedule(p->functionTraceStart);
144        }
145    }
146#if FULL_SYSTEM
147    profileEvent = NULL;
148    if (params->profile)
149        profileEvent = new ProfileEvent(this, params->profile);
150#endif
151}
152
153BaseCPU::Params::Params()
154{
155#if FULL_SYSTEM
156    profile = false;
157#endif
158}
159
160void
161BaseCPU::enableFunctionTrace()
162{
163    functionTracingEnabled = true;
164}
165
166BaseCPU::~BaseCPU()
167{
168}
169
170void
171BaseCPU::init()
172{
173    if (!params->deferRegistration)
174        registerExecContexts();
175}
176
177void
178BaseCPU::startup()
179{
180#if FULL_SYSTEM
181    if (!params->deferRegistration && profileEvent)
182        profileEvent->schedule(curTick);
183#endif
184}
185
186
187void
188BaseCPU::regStats()
189{
190    using namespace Stats;
191
192    numCycles
193        .name(name() + ".numCycles")
194        .desc("number of cpu cycles simulated")
195        ;
196
197    int size = execContexts.size();
198    if (size > 1) {
199        for (int i = 0; i < size; ++i) {
200            stringstream namestr;
201            ccprintf(namestr, "%s.ctx%d", name(), i);
202            execContexts[i]->regStats(namestr.str());
203        }
204    } else if (size == 1)
205        execContexts[0]->regStats(name());
206}
207
208
209void
210BaseCPU::registerExecContexts()
211{
212    for (int i = 0; i < execContexts.size(); ++i) {
213        ExecContext *xc = execContexts[i];
214#if FULL_SYSTEM
215        int id = params->cpu_id;
216        if (id != -1)
217            id += i;
218
219        xc->cpu_id = system->registerExecContext(xc, id);
220#else
221        xc->cpu_id = xc->process->registerExecContext(xc);
222#endif
223    }
224}
225
226
227void
228BaseCPU::switchOut(Sampler *sampler)
229{
230    panic("This CPU doesn't support sampling!");
231}
232
233void
234BaseCPU::takeOverFrom(BaseCPU *oldCPU)
235{
236    assert(execContexts.size() == oldCPU->execContexts.size());
237
238    for (int i = 0; i < execContexts.size(); ++i) {
239        ExecContext *newXC = execContexts[i];
240        ExecContext *oldXC = oldCPU->execContexts[i];
241
242        newXC->takeOverFrom(oldXC);
243        assert(newXC->cpu_id == oldXC->cpu_id);
244#if FULL_SYSTEM
245        system->replaceExecContext(newXC, newXC->cpu_id);
246#else
247        assert(newXC->process == oldXC->process);
248        newXC->process->replaceExecContext(newXC, newXC->cpu_id);
249#endif
250    }
251
252#if FULL_SYSTEM
253    for (int i = 0; i < TheISA::NumInterruptLevels; ++i)
254        interrupts[i] = oldCPU->interrupts[i];
255    intstatus = oldCPU->intstatus;
256
257    for (int i = 0; i < execContexts.size(); ++i)
258        if (execContexts[i]->profile)
259            execContexts[i]->profile->clear();
260
261    if (profileEvent)
262        profileEvent->schedule(curTick);
263#endif
264}
265
266
267#if FULL_SYSTEM
268BaseCPU::ProfileEvent::ProfileEvent(BaseCPU *_cpu, int _interval)
269    : Event(&mainEventQueue), cpu(_cpu), interval(_interval)
270{ }
271
272void
273BaseCPU::ProfileEvent::process()
274{
275    for (int i = 0, size = cpu->execContexts.size(); i < size; ++i) {
276        ExecContext *xc = cpu->execContexts[i];
277        xc->profile->sample(xc->profileNode, xc->profilePC);
278    }
279
280    schedule(curTick + interval);
281}
282
283void
284BaseCPU::post_interrupt(int int_num, int index)
285{
286    DPRINTF(Interrupt, "Interrupt %d:%d posted\n", int_num, index);
287
288    if (int_num < 0 || int_num >= TheISA::NumInterruptLevels)
289        panic("int_num out of bounds\n");
290
291    if (index < 0 || index >= sizeof(uint64_t) * 8)
292        panic("int_num out of bounds\n");
293
294    checkInterrupts = true;
295    interrupts[int_num] |= 1 << index;
296    intstatus |= (ULL(1) << int_num);
297}
298
299void
300BaseCPU::clear_interrupt(int int_num, int index)
301{
302    DPRINTF(Interrupt, "Interrupt %d:%d cleared\n", int_num, index);
303
304    if (int_num < 0 || int_num >= TheISA::NumInterruptLevels)
305        panic("int_num out of bounds\n");
306
307    if (index < 0 || index >= sizeof(uint64_t) * 8)
308        panic("int_num out of bounds\n");
309
310    interrupts[int_num] &= ~(1 << index);
311    if (interrupts[int_num] == 0)
312        intstatus &= ~(ULL(1) << int_num);
313}
314
315void
316BaseCPU::clear_interrupts()
317{
318    DPRINTF(Interrupt, "Interrupts all cleared\n");
319
320    memset(interrupts, 0, sizeof(interrupts));
321    intstatus = 0;
322}
323
324
325void
326BaseCPU::serialize(std::ostream &os)
327{
328    SERIALIZE_ARRAY(interrupts, TheISA::NumInterruptLevels);
329    SERIALIZE_SCALAR(intstatus);
330}
331
332void
333BaseCPU::unserialize(Checkpoint *cp, const std::string &section)
334{
335    UNSERIALIZE_ARRAY(interrupts, TheISA::NumInterruptLevels);
336    UNSERIALIZE_SCALAR(intstatus);
337}
338
339#endif // FULL_SYSTEM
340
341void
342BaseCPU::traceFunctionsInternal(Addr pc)
343{
344    if (!debugSymbolTable)
345        return;
346
347    // if pc enters different function, print new function symbol and
348    // update saved range.  Otherwise do nothing.
349    if (pc < currentFunctionStart || pc >= currentFunctionEnd) {
350        string sym_str;
351        bool found = debugSymbolTable->findNearestSymbol(pc, sym_str,
352                                                         currentFunctionStart,
353                                                         currentFunctionEnd);
354
355        if (!found) {
356            // no symbol found: use addr as label
357            sym_str = csprintf("0x%x", pc);
358            currentFunctionStart = pc;
359            currentFunctionEnd = pc + 1;
360        }
361
362        ccprintf(*functionTraceStream, " (%d)\n%d: %s",
363                 curTick - functionEntryTick, curTick, sym_str);
364        functionEntryTick = curTick;
365    }
366}
367
368
369DEFINE_SIM_OBJECT_CLASS_NAME("BaseCPU", BaseCPU)
370