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.
28 * Authors: Steve Reinhardt
36 #include "base/cprintf.hh"
37 #include "base/loader/symtab.hh"
38 #include "base/misc.hh"
39 #include "base/output.hh"
40 #include "cpu/base.hh"
41 #include "cpu/cpuevent.hh"
42 #include "cpu/thread_context.hh"
43 #include "cpu/profile.hh"
44 #include "sim/param.hh"
45 #include "sim/process.hh"
46 #include "sim/sim_events.hh"
47 #include "sim/system.hh"
49 #include "base/trace.hh"
52 #include "sim/stat_control.hh"
56 vector
<BaseCPU
*> BaseCPU::cpuList
;
58 // This variable reflects the max number of threads in any CPU. Be
59 // careful to only use it once all the CPUs that you care about have
61 int maxThreadsPerCPU
= 1;
63 CPUProgressEvent::CPUProgressEvent(EventQueue
*q
, Tick ival
,
65 : Event(q
, Event::Stat_Event_Pri
), interval(ival
),
66 lastNumInst(0), cpu(_cpu
)
69 schedule(curTick
+ interval
);
73 CPUProgressEvent::process()
75 Counter temp
= cpu
->totalInstructions();
77 double ipc
= double(temp
- lastNumInst
) / (interval
/ cpu
->cycles(1));
79 DPRINTFN("%s progress event, instructions committed: %lli, IPC: %0.8d\n",
80 cpu
->name(), temp
- lastNumInst
, ipc
);
83 cprintf("%lli: %s progress event, instructions committed: %lli\n",
84 curTick
, cpu
->name(), temp
- lastNumInst
);
87 schedule(curTick
+ interval
);
91 CPUProgressEvent::description()
93 return "CPU Progress event";
97 BaseCPU::BaseCPU(Params
*p
)
98 : MemObject(p
->name
), clock(p
->clock
), checkInterrupts(true),
99 params(p
), number_of_threads(p
->numberOfThreads
), system(p
->system
)
101 BaseCPU::BaseCPU(Params
*p
)
102 : MemObject(p
->name
), clock(p
->clock
), params(p
),
103 number_of_threads(p
->numberOfThreads
), system(p
->system
)
106 // currentTick = curTick;
107 DPRINTF(FullCPU
, "BaseCPU: Creating object, mem address %#x.\n", this);
109 // add self to global list of CPUs
110 cpuList
.push_back(this);
112 DPRINTF(FullCPU
, "BaseCPU: CPU added to cpuList, mem address %#x.\n",
115 if (number_of_threads
> maxThreadsPerCPU
)
116 maxThreadsPerCPU
= number_of_threads
;
118 // allocate per-thread instruction-based event queues
119 comInstEventQueue
= new EventQueue
*[number_of_threads
];
120 for (int i
= 0; i
< number_of_threads
; ++i
)
121 comInstEventQueue
[i
] = new EventQueue("instruction-based event queue");
124 // set up instruction-count-based termination events, if any
126 if (p
->max_insts_any_thread
!= 0)
127 for (int i
= 0; i
< number_of_threads
; ++i
)
128 new SimLoopExitEvent(comInstEventQueue
[i
], p
->max_insts_any_thread
,
129 "a thread reached the max instruction count");
131 if (p
->max_insts_all_threads
!= 0) {
132 // allocate & initialize shared downcounter: each event will
133 // decrement this when triggered; simulation will terminate
134 // when counter reaches 0
135 int *counter
= new int;
136 *counter
= number_of_threads
;
137 for (int i
= 0; i
< number_of_threads
; ++i
)
138 new CountedExitEvent(comInstEventQueue
[i
],
139 "all threads reached the max instruction count",
140 p
->max_insts_all_threads
, *counter
);
143 // allocate per-thread load-based event queues
144 comLoadEventQueue
= new EventQueue
*[number_of_threads
];
145 for (int i
= 0; i
< number_of_threads
; ++i
)
146 comLoadEventQueue
[i
] = new EventQueue("load-based event queue");
149 // set up instruction-count-based termination events, if any
151 if (p
->max_loads_any_thread
!= 0)
152 for (int i
= 0; i
< number_of_threads
; ++i
)
153 new SimLoopExitEvent(comLoadEventQueue
[i
], p
->max_loads_any_thread
,
154 "a thread reached the max load count");
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
);
169 memset(interrupts
, 0, sizeof(interrupts
));
173 functionTracingEnabled
= false;
174 if (p
->functionTrace
) {
175 functionTraceStream
= simout
.find(csprintf("ftrace.%s", name()));
176 currentFunctionStart
= currentFunctionEnd
= 0;
177 functionEntryTick
= p
->functionTraceStart
;
179 if (p
->functionTraceStart
== 0) {
180 functionTracingEnabled
= true;
183 new EventWrapper
<BaseCPU
, &BaseCPU::enableFunctionTrace
>(this,
185 e
->schedule(p
->functionTraceStart
);
191 profileEvent
= new ProfileEvent(this, params
->profile
);
195 BaseCPU::Params::Params()
204 BaseCPU::enableFunctionTrace()
206 functionTracingEnabled
= true;
216 if (!params
->deferRegistration
)
217 registerThreadContexts();
224 if (!params
->deferRegistration
&& profileEvent
)
225 profileEvent
->schedule(curTick
);
228 if (params
->progress_interval
) {
229 new CPUProgressEvent(&mainEventQueue
, params
->progress_interval
,
238 using namespace Stats
;
241 .name(name() + ".numCycles")
242 .desc("number of cpu cycles simulated")
245 int size
= threadContexts
.size();
247 for (int i
= 0; i
< size
; ++i
) {
248 stringstream namestr
;
249 ccprintf(namestr
, "%s.ctx%d", name(), i
);
250 threadContexts
[i
]->regStats(namestr
.str());
252 } else if (size
== 1)
253 threadContexts
[0]->regStats(name());
261 BaseCPU::registerThreadContexts()
263 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
264 ThreadContext
*tc
= threadContexts
[i
];
267 int id
= params
->cpu_id
;
271 tc
->setCpuId(system
->registerThreadContext(tc
, id
));
273 tc
->setCpuId(tc
->getProcessPtr()->registerThreadContext(tc
));
282 // panic("This CPU doesn't support sampling!");
284 if (profileEvent
&& profileEvent
->scheduled())
285 profileEvent
->deschedule();
290 BaseCPU::takeOverFrom(BaseCPU
*oldCPU
)
292 assert(threadContexts
.size() == oldCPU
->threadContexts
.size());
294 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
295 ThreadContext
*newTC
= threadContexts
[i
];
296 ThreadContext
*oldTC
= oldCPU
->threadContexts
[i
];
298 newTC
->takeOverFrom(oldTC
);
300 CpuEvent::replaceThreadContext(oldTC
, newTC
);
302 assert(newTC
->readCpuId() == oldTC
->readCpuId());
304 system
->replaceThreadContext(newTC
, newTC
->readCpuId());
306 assert(newTC
->getProcessPtr() == oldTC
->getProcessPtr());
307 newTC
->getProcessPtr()->replaceThreadContext(newTC
, newTC
->readCpuId());
310 // TheISA::compareXCs(oldXC, newXC);
314 for (int i
= 0; i
< TheISA::NumInterruptLevels
; ++i
)
315 interrupts
[i
] = oldCPU
->interrupts
[i
];
316 intstatus
= oldCPU
->intstatus
;
317 checkInterrupts
= oldCPU
->checkInterrupts
;
319 for (int i
= 0; i
< threadContexts
.size(); ++i
)
320 threadContexts
[i
]->profileClear();
322 // The Sampler must take care of this!
324 // profileEvent->schedule(curTick);
330 BaseCPU::ProfileEvent::ProfileEvent(BaseCPU
*_cpu
, int _interval
)
331 : Event(&mainEventQueue
), cpu(_cpu
), interval(_interval
)
335 BaseCPU::ProfileEvent::process()
337 for (int i
= 0, size
= cpu
->threadContexts
.size(); i
< size
; ++i
) {
338 ThreadContext
*tc
= cpu
->threadContexts
[i
];
342 schedule(curTick
+ interval
);
346 BaseCPU::post_interrupt(int int_num
, int index
)
348 DPRINTF(Interrupt
, "Interrupt %d:%d posted\n", int_num
, index
);
350 if (int_num
< 0 || int_num
>= TheISA::NumInterruptLevels
)
351 panic("int_num out of bounds\n");
353 if (index
< 0 || index
>= sizeof(uint64_t) * 8)
354 panic("int_num out of bounds\n");
356 checkInterrupts
= true;
357 interrupts
[int_num
] |= 1 << index
;
358 intstatus
|= (ULL(1) << int_num
);
362 BaseCPU::clear_interrupt(int int_num
, int index
)
364 DPRINTF(Interrupt
, "Interrupt %d:%d cleared\n", int_num
, index
);
366 if (int_num
< 0 || int_num
>= TheISA::NumInterruptLevels
)
367 panic("int_num out of bounds\n");
369 if (index
< 0 || index
>= sizeof(uint64_t) * 8)
370 panic("int_num out of bounds\n");
372 interrupts
[int_num
] &= ~(1 << index
);
373 if (interrupts
[int_num
] == 0)
374 intstatus
&= ~(ULL(1) << int_num
);
378 BaseCPU::clear_interrupts()
380 DPRINTF(Interrupt
, "Interrupts all cleared\n");
382 memset(interrupts
, 0, sizeof(interrupts
));
388 BaseCPU::serialize(std::ostream
&os
)
390 SERIALIZE_ARRAY(interrupts
, TheISA::NumInterruptLevels
);
391 SERIALIZE_SCALAR(intstatus
);
395 BaseCPU::unserialize(Checkpoint
*cp
, const std::string
§ion
)
397 UNSERIALIZE_ARRAY(interrupts
, TheISA::NumInterruptLevels
);
398 UNSERIALIZE_SCALAR(intstatus
);
401 #endif // FULL_SYSTEM
404 BaseCPU::traceFunctionsInternal(Addr pc
)
406 if (!debugSymbolTable
)
409 // if pc enters different function, print new function symbol and
410 // update saved range. Otherwise do nothing.
411 if (pc
< currentFunctionStart
|| pc
>= currentFunctionEnd
) {
413 bool found
= debugSymbolTable
->findNearestSymbol(pc
, sym_str
,
414 currentFunctionStart
,
418 // no symbol found: use addr as label
419 sym_str
= csprintf("0x%x", pc
);
420 currentFunctionStart
= pc
;
421 currentFunctionEnd
= pc
+ 1;
424 ccprintf(*functionTraceStream
, " (%d)\n%d: %s",
425 curTick
- functionEntryTick
, curTick
, sym_str
);
426 functionEntryTick
= curTick
;
431 DEFINE_SIM_OBJECT_CLASS_NAME("BaseCPU", BaseCPU
)