2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
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"
46 #include "base/trace.hh"
49 #include "kern/kernel_stats.hh"
54 vector
<BaseCPU
*> BaseCPU::cpuList
;
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
59 int maxThreadsPerCPU
= 1;
62 BaseCPU::BaseCPU(Params
*p
)
63 : SimObject(p
->name
), clock(p
->clock
), checkInterrupts(true),
64 params(p
), number_of_threads(p
->numberOfThreads
), system(p
->system
)
66 BaseCPU::BaseCPU(Params
*p
)
67 : SimObject(p
->name
), clock(p
->clock
), params(p
),
68 number_of_threads(p
->numberOfThreads
), system(p
->system
)
71 DPRINTF(FullCPU
, "BaseCPU: Creating object, mem address %#x.\n", this);
73 // add self to global list of CPUs
74 cpuList
.push_back(this);
76 DPRINTF(FullCPU
, "BaseCPU: CPU added to cpuList, mem address %#x.\n",
79 if (number_of_threads
> maxThreadsPerCPU
)
80 maxThreadsPerCPU
= number_of_threads
;
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");
88 // set up instruction-count-based termination events, if any
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");
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
);
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");
113 // set up instruction-count-based termination events, if any
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");
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
);
133 memset(interrupts
, 0, sizeof(interrupts
));
137 functionTracingEnabled
= false;
138 if (p
->functionTrace
) {
139 functionTraceStream
= simout
.find(csprintf("ftrace.%s", name()));
140 currentFunctionStart
= currentFunctionEnd
= 0;
141 functionEntryTick
= p
->functionTraceStart
;
143 if (p
->functionTraceStart
== 0) {
144 functionTracingEnabled
= true;
147 new EventWrapper
<BaseCPU
, &BaseCPU::enableFunctionTrace
>(this,
149 e
->schedule(p
->functionTraceStart
);
155 profileEvent
= new ProfileEvent(this, params
->profile
);
157 kernelStats
= new Kernel::Statistics(system
);
162 BaseCPU::Params::Params()
170 BaseCPU::enableFunctionTrace()
172 functionTracingEnabled
= true;
186 if (!params
->deferRegistration
)
187 registerExecContexts();
194 if (!params
->deferRegistration
&& profileEvent
)
195 profileEvent
->schedule(curTick
);
203 using namespace Stats
;
206 .name(name() + ".numCycles")
207 .desc("number of cpu cycles simulated")
210 int size
= execContexts
.size();
212 for (int i
= 0; i
< size
; ++i
) {
213 stringstream namestr
;
214 ccprintf(namestr
, "%s.ctx%d", name(), i
);
215 execContexts
[i
]->regStats(namestr
.str());
217 } else if (size
== 1)
218 execContexts
[0]->regStats(name());
222 kernelStats
->regStats(name() + ".kern");
228 BaseCPU::registerExecContexts()
230 for (int i
= 0; i
< execContexts
.size(); ++i
) {
231 ExecContext
*xc
= execContexts
[i
];
234 int id
= params
->cpu_id
;
238 xc
->setCpuId(system
->registerExecContext(xc
, id
));
240 xc
->setCpuId(xc
->getProcessPtr()->registerExecContext(xc
));
247 BaseCPU::switchOut(Sampler
*sampler
)
249 panic("This CPU doesn't support sampling!");
253 BaseCPU::takeOverFrom(BaseCPU
*oldCPU
)
255 assert(execContexts
.size() == oldCPU
->execContexts
.size());
257 for (int i
= 0; i
< execContexts
.size(); ++i
) {
258 ExecContext
*newXC
= execContexts
[i
];
259 ExecContext
*oldXC
= oldCPU
->execContexts
[i
];
261 newXC
->takeOverFrom(oldXC
);
262 assert(newXC
->readCpuId() == oldXC
->readCpuId());
264 system
->replaceExecContext(newXC
, newXC
->readCpuId());
266 assert(newXC
->getProcessPtr() == oldXC
->getProcessPtr());
267 newXC
->getProcessPtr()->replaceExecContext(newXC
, newXC
->readCpuId());
272 for (int i
= 0; i
< TheISA::NumInterruptLevels
; ++i
)
273 interrupts
[i
] = oldCPU
->interrupts
[i
];
274 intstatus
= oldCPU
->intstatus
;
276 for (int i
= 0; i
< execContexts
.size(); ++i
)
277 execContexts
[i
]->profileClear();
280 profileEvent
->schedule(curTick
);
286 BaseCPU::ProfileEvent::ProfileEvent(BaseCPU
*_cpu
, int _interval
)
287 : Event(&mainEventQueue
), cpu(_cpu
), interval(_interval
)
291 BaseCPU::ProfileEvent::process()
293 for (int i
= 0, size
= cpu
->execContexts
.size(); i
< size
; ++i
) {
294 ExecContext
*xc
= cpu
->execContexts
[i
];
298 schedule(curTick
+ interval
);
302 BaseCPU::post_interrupt(int int_num
, int index
)
304 DPRINTF(Interrupt
, "Interrupt %d:%d posted\n", int_num
, index
);
306 if (int_num
< 0 || int_num
>= TheISA::NumInterruptLevels
)
307 panic("int_num out of bounds\n");
309 if (index
< 0 || index
>= sizeof(uint64_t) * 8)
310 panic("int_num out of bounds\n");
312 checkInterrupts
= true;
313 interrupts
[int_num
] |= 1 << index
;
314 intstatus
|= (ULL(1) << int_num
);
318 BaseCPU::clear_interrupt(int int_num
, int index
)
320 DPRINTF(Interrupt
, "Interrupt %d:%d cleared\n", int_num
, index
);
322 if (int_num
< 0 || int_num
>= TheISA::NumInterruptLevels
)
323 panic("int_num out of bounds\n");
325 if (index
< 0 || index
>= sizeof(uint64_t) * 8)
326 panic("int_num out of bounds\n");
328 interrupts
[int_num
] &= ~(1 << index
);
329 if (interrupts
[int_num
] == 0)
330 intstatus
&= ~(ULL(1) << int_num
);
334 BaseCPU::clear_interrupts()
336 DPRINTF(Interrupt
, "Interrupts all cleared\n");
338 memset(interrupts
, 0, sizeof(interrupts
));
344 BaseCPU::serialize(std::ostream
&os
)
346 SERIALIZE_ARRAY(interrupts
, TheISA::NumInterruptLevels
);
347 SERIALIZE_SCALAR(intstatus
);
351 kernelStats
->serialize(os
);
357 BaseCPU::unserialize(Checkpoint
*cp
, const std::string
§ion
)
359 UNSERIALIZE_ARRAY(interrupts
, TheISA::NumInterruptLevels
);
360 UNSERIALIZE_SCALAR(intstatus
);
364 kernelStats
->unserialize(cp
, section
);
368 #endif // FULL_SYSTEM
371 BaseCPU::traceFunctionsInternal(Addr pc
)
373 if (!debugSymbolTable
)
376 // if pc enters different function, print new function symbol and
377 // update saved range. Otherwise do nothing.
378 if (pc
< currentFunctionStart
|| pc
>= currentFunctionEnd
) {
380 bool found
= debugSymbolTable
->findNearestSymbol(pc
, sym_str
,
381 currentFunctionStart
,
385 // no symbol found: use addr as label
386 sym_str
= csprintf("0x%x", pc
);
387 currentFunctionStart
= pc
;
388 currentFunctionEnd
= pc
+ 1;
391 ccprintf(*functionTraceStream
, " (%d)\n%d: %s",
392 curTick
- functionEntryTick
, curTick
, sym_str
);
393 functionEntryTick
= curTick
;
398 DEFINE_SIM_OBJECT_CLASS_NAME("BaseCPU", BaseCPU
)