base.cc revision 2420
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/sim_events.hh"
43
44#include "base/trace.hh"
45
46using namespace std;
47
48vector<BaseCPU *> BaseCPU::cpuList;
49
50// This variable reflects the max number of threads in any CPU.  Be
51// careful to only use it once all the CPUs that you care about have
52// been initialized
53int maxThreadsPerCPU = 1;
54
55#if FULL_SYSTEM
56BaseCPU::BaseCPU(Params *p)
57    : SimObject(p->name), clock(p->clock), checkInterrupts(true),
58      params(p), number_of_threads(p->numberOfThreads), system(p->system)
59#else
60BaseCPU::BaseCPU(Params *p)
61    : SimObject(p->name), clock(p->clock), params(p),
62      number_of_threads(p->numberOfThreads), system(p->system)
63#endif
64{
65    DPRINTF(FullCPU, "BaseCPU: Creating object, mem address %#x.\n", this);
66
67    // add self to global list of CPUs
68    cpuList.push_back(this);
69
70    DPRINTF(FullCPU, "BaseCPU: CPU added to cpuList, mem address %#x.\n",
71            this);
72
73    if (number_of_threads > maxThreadsPerCPU)
74        maxThreadsPerCPU = number_of_threads;
75
76    // allocate per-thread instruction-based event queues
77    comInstEventQueue = new EventQueue *[number_of_threads];
78    for (int i = 0; i < number_of_threads; ++i)
79        comInstEventQueue[i] = new EventQueue("instruction-based event queue");
80
81    //
82    // set up instruction-count-based termination events, if any
83    //
84    if (p->max_insts_any_thread != 0)
85        for (int i = 0; i < number_of_threads; ++i)
86            new SimExitEvent(comInstEventQueue[i], p->max_insts_any_thread,
87                "a thread reached the max instruction count");
88
89    if (p->max_insts_all_threads != 0) {
90        // allocate & initialize shared downcounter: each event will
91        // decrement this when triggered; simulation will terminate
92        // when counter reaches 0
93        int *counter = new int;
94        *counter = number_of_threads;
95        for (int i = 0; i < number_of_threads; ++i)
96            new CountedExitEvent(comInstEventQueue[i],
97                "all threads reached the max instruction count",
98                p->max_insts_all_threads, *counter);
99    }
100
101    // allocate per-thread load-based event queues
102    comLoadEventQueue = new EventQueue *[number_of_threads];
103    for (int i = 0; i < number_of_threads; ++i)
104        comLoadEventQueue[i] = new EventQueue("load-based event queue");
105
106    //
107    // set up instruction-count-based termination events, if any
108    //
109    if (p->max_loads_any_thread != 0)
110        for (int i = 0; i < number_of_threads; ++i)
111            new SimExitEvent(comLoadEventQueue[i], p->max_loads_any_thread,
112                "a thread reached the max load count");
113
114    if (p->max_loads_all_threads != 0) {
115        // allocate & initialize shared downcounter: each event will
116        // decrement this when triggered; simulation will terminate
117        // when counter reaches 0
118        int *counter = new int;
119        *counter = number_of_threads;
120        for (int i = 0; i < number_of_threads; ++i)
121            new CountedExitEvent(comLoadEventQueue[i],
122                "all threads reached the max load count",
123                p->max_loads_all_threads, *counter);
124    }
125
126#if FULL_SYSTEM
127    memset(interrupts, 0, sizeof(interrupts));
128    intstatus = 0;
129#endif
130
131    functionTracingEnabled = false;
132    if (p->functionTrace) {
133        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
215#if FULL_SYSTEM
216        int id = params->cpu_id;
217        if (id != -1)
218            id += i;
219
220        xc->cpu_id = system->registerExecContext(xc, id);
221#else
222        xc->cpu_id = xc->process->registerExecContext(xc);
223#endif
224    }
225}
226
227
228void
229BaseCPU::switchOut(Sampler *sampler)
230{
231    panic("This CPU doesn't support sampling!");
232}
233
234void
235BaseCPU::takeOverFrom(BaseCPU *oldCPU)
236{
237    assert(execContexts.size() == oldCPU->execContexts.size());
238
239    for (int i = 0; i < execContexts.size(); ++i) {
240        ExecContext *newXC = execContexts[i];
241        ExecContext *oldXC = oldCPU->execContexts[i];
242
243        newXC->takeOverFrom(oldXC);
244        assert(newXC->cpu_id == oldXC->cpu_id);
245#if FULL_SYSTEM
246        system->replaceExecContext(newXC, newXC->cpu_id);
247#else
248        assert(newXC->process == oldXC->process);
249        newXC->process->replaceExecContext(newXC, newXC->cpu_id);
250#endif
251    }
252
253#if FULL_SYSTEM
254    for (int i = 0; i < NumInterruptLevels; ++i)
255        interrupts[i] = oldCPU->interrupts[i];
256    intstatus = oldCPU->intstatus;
257
258    for (int i = 0; i < execContexts.size(); ++i)
259        if (execContexts[i]->profile)
260            execContexts[i]->profile->clear();
261
262    if (profileEvent)
263        profileEvent->schedule(curTick);
264#endif
265}
266
267
268#if FULL_SYSTEM
269BaseCPU::ProfileEvent::ProfileEvent(BaseCPU *_cpu, int _interval)
270    : Event(&mainEventQueue), cpu(_cpu), interval(_interval)
271{ }
272
273void
274BaseCPU::ProfileEvent::process()
275{
276    for (int i = 0, size = cpu->execContexts.size(); i < size; ++i) {
277        ExecContext *xc = cpu->execContexts[i];
278        xc->profile->sample(xc->profileNode, xc->profilePC);
279    }
280
281    schedule(curTick + interval);
282}
283
284void
285BaseCPU::post_interrupt(int int_num, int index)
286{
287    DPRINTF(Interrupt, "Interrupt %d:%d posted\n", int_num, index);
288
289    if (int_num < 0 || int_num >= NumInterruptLevels)
290        panic("int_num out of bounds\n");
291
292    if (index < 0 || index >= sizeof(uint64_t) * 8)
293        panic("int_num out of bounds\n");
294
295    checkInterrupts = true;
296    interrupts[int_num] |= 1 << index;
297    intstatus |= (ULL(1) << int_num);
298}
299
300void
301BaseCPU::clear_interrupt(int int_num, int index)
302{
303    DPRINTF(Interrupt, "Interrupt %d:%d cleared\n", int_num, index);
304
305    if (int_num < 0 || int_num >= NumInterruptLevels)
306        panic("int_num out of bounds\n");
307
308    if (index < 0 || index >= sizeof(uint64_t) * 8)
309        panic("int_num out of bounds\n");
310
311    interrupts[int_num] &= ~(1 << index);
312    if (interrupts[int_num] == 0)
313        intstatus &= ~(ULL(1) << int_num);
314}
315
316void
317BaseCPU::clear_interrupts()
318{
319    DPRINTF(Interrupt, "Interrupts all cleared\n");
320
321    memset(interrupts, 0, sizeof(interrupts));
322    intstatus = 0;
323}
324
325
326void
327BaseCPU::serialize(std::ostream &os)
328{
329    SERIALIZE_ARRAY(interrupts, NumInterruptLevels);
330    SERIALIZE_SCALAR(intstatus);
331}
332
333void
334BaseCPU::unserialize(Checkpoint *cp, const std::string &section)
335{
336    UNSERIALIZE_ARRAY(interrupts, NumInterruptLevels);
337    UNSERIALIZE_SCALAR(intstatus);
338}
339
340#endif // FULL_SYSTEM
341
342void
343BaseCPU::traceFunctionsInternal(Addr pc)
344{
345    if (!debugSymbolTable)
346        return;
347
348    // if pc enters different function, print new function symbol and
349    // update saved range.  Otherwise do nothing.
350    if (pc < currentFunctionStart || pc >= currentFunctionEnd) {
351        string sym_str;
352        bool found = debugSymbolTable->findNearestSymbol(pc, sym_str,
353                                                         currentFunctionStart,
354                                                         currentFunctionEnd);
355
356        if (!found) {
357            // no symbol found: use addr as label
358            sym_str = csprintf("0x%x", pc);
359            currentFunctionStart = pc;
360            currentFunctionEnd = pc + 1;
361        }
362
363        ccprintf(*functionTraceStream, " (%d)\n%d: %s",
364                 curTick - functionEntryTick, curTick, sym_str);
365        functionEntryTick = curTick;
366    }
367}
368
369
370DEFINE_SIM_OBJECT_CLASS_NAME("BaseCPU", BaseCPU)
371