base.cc revision 2315
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <iostream>
30#include <string>
31#include <sstream>
32
33#include "base/cprintf.hh"
34#include "base/loader/symtab.hh"
35#include "base/misc.hh"
36#include "base/output.hh"
37#include "cpu/base.hh"
38#include "cpu/exec_context.hh"
39#include "cpu/profile.hh"
40#include "cpu/sampler/sampler.hh"
41#include "sim/param.hh"
42#include "sim/process.hh"
43#include "sim/sim_events.hh"
44#include "sim/system.hh"
45
46#include "base/trace.hh"
47
48#if FULL_SYSTEM
49#include "kern/kernel_stats.hh"
50#endif
51
52using namespace std;
53
54vector<BaseCPU *> BaseCPU::cpuList;
55
56// This variable reflects the max number of threads in any CPU.  Be
57// careful to only use it once all the CPUs that you care about have
58// been initialized
59int maxThreadsPerCPU = 1;
60
61#if FULL_SYSTEM
62BaseCPU::BaseCPU(Params *p)
63    : SimObject(p->name), clock(p->clock), checkInterrupts(true),
64      params(p), number_of_threads(p->numberOfThreads), system(p->system)
65#else
66BaseCPU::BaseCPU(Params *p)
67    : SimObject(p->name), clock(p->clock), params(p),
68      number_of_threads(p->numberOfThreads)
69#endif
70{
71    DPRINTF(FullCPU, "BaseCPU: Creating object, mem address %#x.\n", this);
72
73    // add self to global list of CPUs
74    cpuList.push_back(this);
75
76    DPRINTF(FullCPU, "BaseCPU: CPU added to cpuList, mem address %#x.\n",
77            this);
78
79    if (number_of_threads > maxThreadsPerCPU)
80        maxThreadsPerCPU = number_of_threads;
81
82    // allocate per-thread instruction-based event queues
83    comInstEventQueue = new EventQueue *[number_of_threads];
84    for (int i = 0; i < number_of_threads; ++i)
85        comInstEventQueue[i] = new EventQueue("instruction-based event queue");
86
87    //
88    // set up instruction-count-based termination events, if any
89    //
90    if (p->max_insts_any_thread != 0)
91        for (int i = 0; i < number_of_threads; ++i)
92            new SimExitEvent(comInstEventQueue[i], p->max_insts_any_thread,
93                "a thread reached the max instruction count");
94
95    if (p->max_insts_all_threads != 0) {
96        // allocate & initialize shared downcounter: each event will
97        // decrement this when triggered; simulation will terminate
98        // when counter reaches 0
99        int *counter = new int;
100        *counter = number_of_threads;
101        for (int i = 0; i < number_of_threads; ++i)
102            new CountedExitEvent(comInstEventQueue[i],
103                "all threads reached the max instruction count",
104                p->max_insts_all_threads, *counter);
105    }
106
107    // allocate per-thread load-based event queues
108    comLoadEventQueue = new EventQueue *[number_of_threads];
109    for (int i = 0; i < number_of_threads; ++i)
110        comLoadEventQueue[i] = new EventQueue("load-based event queue");
111
112    //
113    // set up instruction-count-based termination events, if any
114    //
115    if (p->max_loads_any_thread != 0)
116        for (int i = 0; i < number_of_threads; ++i)
117            new SimExitEvent(comLoadEventQueue[i], p->max_loads_any_thread,
118                "a thread reached the max load count");
119
120    if (p->max_loads_all_threads != 0) {
121        // allocate & initialize shared downcounter: each event will
122        // decrement this when triggered; simulation will terminate
123        // when counter reaches 0
124        int *counter = new int;
125        *counter = number_of_threads;
126        for (int i = 0; i < number_of_threads; ++i)
127            new CountedExitEvent(comLoadEventQueue[i],
128                "all threads reached the max load count",
129                p->max_loads_all_threads, *counter);
130    }
131
132#if FULL_SYSTEM
133    memset(interrupts, 0, sizeof(interrupts));
134    intstatus = 0;
135#endif
136
137    functionTracingEnabled = false;
138    if (p->functionTrace) {
139        functionTraceStream = simout.find(csprintf("ftrace.%s", name()));
140        currentFunctionStart = currentFunctionEnd = 0;
141        functionEntryTick = p->functionTraceStart;
142
143        if (p->functionTraceStart == 0) {
144            functionTracingEnabled = true;
145        } else {
146            Event *e =
147                new EventWrapper<BaseCPU, &BaseCPU::enableFunctionTrace>(this,
148                                                                         true);
149            e->schedule(p->functionTraceStart);
150        }
151    }
152#if FULL_SYSTEM
153    profileEvent = NULL;
154    if (params->profile)
155        profileEvent = new ProfileEvent(this, params->profile);
156
157    kernelStats = new Kernel::Statistics(system);
158#endif
159
160}
161
162BaseCPU::Params::Params()
163{
164#if FULL_SYSTEM
165    profile = false;
166#endif
167    checker = NULL;
168}
169
170void
171BaseCPU::enableFunctionTrace()
172{
173    functionTracingEnabled = true;
174}
175
176BaseCPU::~BaseCPU()
177{
178#if FULL_SYSTEM
179    if (kernelStats)
180        delete kernelStats;
181#endif
182}
183
184void
185BaseCPU::init()
186{
187    if (!params->deferRegistration)
188        registerExecContexts();
189}
190
191void
192BaseCPU::startup()
193{
194#if FULL_SYSTEM
195    if (!params->deferRegistration && profileEvent)
196        profileEvent->schedule(curTick);
197#endif
198}
199
200
201void
202BaseCPU::regStats()
203{
204    using namespace Stats;
205
206    numCycles
207        .name(name() + ".numCycles")
208        .desc("number of cpu cycles simulated")
209        ;
210
211    int size = execContexts.size();
212    if (size > 1) {
213        for (int i = 0; i < size; ++i) {
214            stringstream namestr;
215            ccprintf(namestr, "%s.ctx%d", name(), i);
216            execContexts[i]->regStats(namestr.str());
217        }
218    } else if (size == 1)
219        execContexts[0]->regStats(name());
220
221#if FULL_SYSTEM
222    if (kernelStats)
223        kernelStats->regStats(name() + ".kern");
224#endif
225}
226
227
228void
229BaseCPU::registerExecContexts()
230{
231    for (int i = 0; i < execContexts.size(); ++i) {
232        ExecContext *xc = execContexts[i];
233
234        if (xc->status() == ExecContext::Suspended) {
235#if FULL_SYSTEM
236            int id = params->cpu_id;
237            if (id != -1)
238                id += i;
239
240        xc->setCpuId(system->registerExecContext(xc, id));
241#else
242        xc->setCpuId(xc->getProcessPtr()->registerExecContext(xc));
243#endif
244        }
245    }
246}
247
248
249void
250BaseCPU::switchOut(Sampler *sampler)
251{
252    panic("This CPU doesn't support sampling!");
253}
254
255void
256BaseCPU::takeOverFrom(BaseCPU *oldCPU)
257{
258    assert(execContexts.size() == oldCPU->execContexts.size());
259
260    for (int i = 0; i < execContexts.size(); ++i) {
261        ExecContext *newXC = execContexts[i];
262        ExecContext *oldXC = oldCPU->execContexts[i];
263
264        newXC->takeOverFrom(oldXC);
265        assert(newXC->readCpuId() == oldXC->readCpuId());
266#if FULL_SYSTEM
267        system->replaceExecContext(newXC, newXC->readCpuId());
268#else
269        assert(newXC->getProcessPtr() == oldXC->getProcessPtr());
270        newXC->getProcessPtr()->replaceExecContext(newXC, newXC->readCpuId());
271#endif
272    }
273
274#if FULL_SYSTEM
275    for (int i = 0; i < TheISA::NumInterruptLevels; ++i)
276        interrupts[i] = oldCPU->interrupts[i];
277    intstatus = oldCPU->intstatus;
278
279    for (int i = 0; i < execContexts.size(); ++i)
280        execContexts[i]->profileClear();
281
282    if (profileEvent)
283        profileEvent->schedule(curTick);
284#endif
285}
286
287
288#if FULL_SYSTEM
289BaseCPU::ProfileEvent::ProfileEvent(BaseCPU *_cpu, int _interval)
290    : Event(&mainEventQueue), cpu(_cpu), interval(_interval)
291{ }
292
293void
294BaseCPU::ProfileEvent::process()
295{
296    for (int i = 0, size = cpu->execContexts.size(); i < size; ++i) {
297        ExecContext *xc = cpu->execContexts[i];
298        xc->profileSample();
299    }
300
301    schedule(curTick + interval);
302}
303
304void
305BaseCPU::post_interrupt(int int_num, int index)
306{
307    DPRINTF(Interrupt, "Interrupt %d:%d posted\n", int_num, index);
308
309    if (int_num < 0 || int_num >= TheISA::NumInterruptLevels)
310        panic("int_num out of bounds\n");
311
312    if (index < 0 || index >= sizeof(uint64_t) * 8)
313        panic("int_num out of bounds\n");
314
315    checkInterrupts = true;
316    interrupts[int_num] |= 1 << index;
317    intstatus |= (ULL(1) << int_num);
318}
319
320void
321BaseCPU::clear_interrupt(int int_num, int index)
322{
323    DPRINTF(Interrupt, "Interrupt %d:%d cleared\n", int_num, index);
324
325    if (int_num < 0 || int_num >= TheISA::NumInterruptLevels)
326        panic("int_num out of bounds\n");
327
328    if (index < 0 || index >= sizeof(uint64_t) * 8)
329        panic("int_num out of bounds\n");
330
331    interrupts[int_num] &= ~(1 << index);
332    if (interrupts[int_num] == 0)
333        intstatus &= ~(ULL(1) << int_num);
334}
335
336void
337BaseCPU::clear_interrupts()
338{
339    DPRINTF(Interrupt, "Interrupts all cleared\n");
340
341    memset(interrupts, 0, sizeof(interrupts));
342    intstatus = 0;
343}
344
345
346void
347BaseCPU::serialize(std::ostream &os)
348{
349    SERIALIZE_ARRAY(interrupts, TheISA::NumInterruptLevels);
350    SERIALIZE_SCALAR(intstatus);
351
352#if FULL_SYSTEM
353    if (kernelStats)
354        kernelStats->serialize(os);
355#endif
356
357}
358
359void
360BaseCPU::unserialize(Checkpoint *cp, const std::string &section)
361{
362    UNSERIALIZE_ARRAY(interrupts, TheISA::NumInterruptLevels);
363    UNSERIALIZE_SCALAR(intstatus);
364
365#if FULL_SYSTEM
366    if (kernelStats)
367        kernelStats->unserialize(cp, section);
368#endif
369}
370
371#endif // FULL_SYSTEM
372
373void
374BaseCPU::traceFunctionsInternal(Addr pc)
375{
376    if (!debugSymbolTable)
377        return;
378
379    // if pc enters different function, print new function symbol and
380    // update saved range.  Otherwise do nothing.
381    if (pc < currentFunctionStart || pc >= currentFunctionEnd) {
382        string sym_str;
383        bool found = debugSymbolTable->findNearestSymbol(pc, sym_str,
384                                                         currentFunctionStart,
385                                                         currentFunctionEnd);
386
387        if (!found) {
388            // no symbol found: use addr as label
389            sym_str = csprintf("0x%x", pc);
390            currentFunctionStart = pc;
391            currentFunctionEnd = pc + 1;
392        }
393
394        ccprintf(*functionTraceStream, " (%d)\n%d: %s",
395                 curTick - functionEntryTick, curTick, sym_str);
396        functionEntryTick = curTick;
397    }
398}
399
400
401DEFINE_SIM_OBJECT_CLASS_NAME("BaseCPU", BaseCPU)
402