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/sampler/sampler.hh"
40 #include "sim/param.hh"
41 #include "sim/sim_events.hh"
43 #include "base/trace.hh"
47 vector
<BaseCPU
*> BaseCPU::cpuList
;
49 // This variable reflects the max number of threads in any CPU. Be
50 // careful to only use it once all the CPUs that you care about have
52 int maxThreadsPerCPU
= 1;
55 BaseCPU::BaseCPU(Params
*p
)
56 : SimObject(p
->name
), clock(p
->clock
), checkInterrupts(true),
57 params(p
), number_of_threads(p
->numberOfThreads
), system(p
->system
)
59 BaseCPU::BaseCPU(Params
*p
)
60 : SimObject(p
->name
), clock(p
->clock
), params(p
),
61 number_of_threads(p
->numberOfThreads
)
64 DPRINTF(FullCPU
, "BaseCPU: Creating object, mem address %#x.\n", this);
66 // add self to global list of CPUs
67 cpuList
.push_back(this);
69 DPRINTF(FullCPU
, "BaseCPU: CPU added to cpuList, mem address %#x.\n",
72 if (number_of_threads
> maxThreadsPerCPU
)
73 maxThreadsPerCPU
= number_of_threads
;
75 // allocate per-thread instruction-based event queues
76 comInstEventQueue
= new EventQueue
*[number_of_threads
];
77 for (int i
= 0; i
< number_of_threads
; ++i
)
78 comInstEventQueue
[i
] = new EventQueue("instruction-based event queue");
81 // set up instruction-count-based termination events, if any
83 if (p
->max_insts_any_thread
!= 0)
84 for (int i
= 0; i
< number_of_threads
; ++i
)
85 new SimExitEvent(comInstEventQueue
[i
], p
->max_insts_any_thread
,
86 "a thread reached the max instruction count");
88 if (p
->max_insts_all_threads
!= 0) {
89 // allocate & initialize shared downcounter: each event will
90 // decrement this when triggered; simulation will terminate
91 // when counter reaches 0
92 int *counter
= new int;
93 *counter
= number_of_threads
;
94 for (int i
= 0; i
< number_of_threads
; ++i
)
95 new CountedExitEvent(comInstEventQueue
[i
],
96 "all threads reached the max instruction count",
97 p
->max_insts_all_threads
, *counter
);
100 // allocate per-thread load-based event queues
101 comLoadEventQueue
= new EventQueue
*[number_of_threads
];
102 for (int i
= 0; i
< number_of_threads
; ++i
)
103 comLoadEventQueue
[i
] = new EventQueue("load-based event queue");
106 // set up instruction-count-based termination events, if any
108 if (p
->max_loads_any_thread
!= 0)
109 for (int i
= 0; i
< number_of_threads
; ++i
)
110 new SimExitEvent(comLoadEventQueue
[i
], p
->max_loads_any_thread
,
111 "a thread reached the max load count");
113 if (p
->max_loads_all_threads
!= 0) {
114 // allocate & initialize shared downcounter: each event will
115 // decrement this when triggered; simulation will terminate
116 // when counter reaches 0
117 int *counter
= new int;
118 *counter
= number_of_threads
;
119 for (int i
= 0; i
< number_of_threads
; ++i
)
120 new CountedExitEvent(comLoadEventQueue
[i
],
121 "all threads reached the max load count",
122 p
->max_loads_all_threads
, *counter
);
126 memset(interrupts
, 0, sizeof(interrupts
));
130 functionTracingEnabled
= false;
131 if (p
->functionTrace
) {
132 functionTraceStream
= simout
.find(csprintf("ftrace.%s", name()));
133 currentFunctionStart
= currentFunctionEnd
= 0;
134 functionEntryTick
= p
->functionTraceStart
;
136 if (p
->functionTraceStart
== 0) {
137 functionTracingEnabled
= true;
140 new EventWrapper
<BaseCPU
, &BaseCPU::enableFunctionTrace
>(this,
142 e
->schedule(p
->functionTraceStart
);
148 profileEvent
= new ProfileEvent(this, params
->profile
);
152 BaseCPU::Params::Params()
160 BaseCPU::enableFunctionTrace()
162 functionTracingEnabled
= true;
172 if (!params
->deferRegistration
)
173 registerExecContexts();
180 if (!params
->deferRegistration
&& profileEvent
)
181 profileEvent
->schedule(curTick
);
189 using namespace Stats
;
192 .name(name() + ".numCycles")
193 .desc("number of cpu cycles simulated")
196 int size
= execContexts
.size();
198 for (int i
= 0; i
< size
; ++i
) {
199 stringstream namestr
;
200 ccprintf(namestr
, "%s.ctx%d", name(), i
);
201 execContexts
[i
]->regStats(namestr
.str());
203 } else if (size
== 1)
204 execContexts
[0]->regStats(name());
209 BaseCPU::registerExecContexts()
211 for (int i
= 0; i
< execContexts
.size(); ++i
) {
212 ExecContext
*xc
= execContexts
[i
];
214 int id
= params
->cpu_id
;
218 xc
->cpu_id
= system
->registerExecContext(xc
, id
);
220 xc
->cpu_id
= xc
->process
->registerExecContext(xc
);
227 BaseCPU::switchOut(Sampler
*sampler
)
229 panic("This CPU doesn't support sampling!");
233 BaseCPU::takeOverFrom(BaseCPU
*oldCPU
)
235 assert(execContexts
.size() == oldCPU
->execContexts
.size());
237 for (int i
= 0; i
< execContexts
.size(); ++i
) {
238 ExecContext
*newXC
= execContexts
[i
];
239 ExecContext
*oldXC
= oldCPU
->execContexts
[i
];
241 newXC
->takeOverFrom(oldXC
);
242 assert(newXC
->cpu_id
== oldXC
->cpu_id
);
244 system
->replaceExecContext(newXC
, newXC
->cpu_id
);
246 assert(newXC
->process
== oldXC
->process
);
247 newXC
->process
->replaceExecContext(newXC
, newXC
->cpu_id
);
252 for (int i
= 0; i
< NumInterruptLevels
; ++i
)
253 interrupts
[i
] = oldCPU
->interrupts
[i
];
254 intstatus
= oldCPU
->intstatus
;
256 for (int i
= 0; i
< execContexts
.size(); ++i
)
257 execContexts
[i
]->profile
->clear();
260 profileEvent
->schedule(curTick
);
266 BaseCPU::ProfileEvent::ProfileEvent(BaseCPU
*_cpu
, int _interval
)
267 : Event(&mainEventQueue
), cpu(_cpu
), interval(_interval
)
271 BaseCPU::ProfileEvent::process()
273 for (int i
= 0, size
= cpu
->execContexts
.size(); i
< size
; ++i
) {
274 ExecContext
*xc
= cpu
->execContexts
[i
];
275 xc
->profile
->sample(xc
->profileNode
, xc
->profilePC
);
278 schedule(curTick
+ interval
);
282 BaseCPU::post_interrupt(int int_num
, int index
)
284 DPRINTF(Interrupt
, "Interrupt %d:%d posted\n", int_num
, index
);
286 if (int_num
< 0 || int_num
>= NumInterruptLevels
)
287 panic("int_num out of bounds\n");
289 if (index
< 0 || index
>= sizeof(uint64_t) * 8)
290 panic("int_num out of bounds\n");
292 checkInterrupts
= true;
293 interrupts
[int_num
] |= 1 << index
;
294 intstatus
|= (ULL(1) << int_num
);
298 BaseCPU::clear_interrupt(int int_num
, int index
)
300 DPRINTF(Interrupt
, "Interrupt %d:%d cleared\n", int_num
, index
);
302 if (int_num
< 0 || int_num
>= NumInterruptLevels
)
303 panic("int_num out of bounds\n");
305 if (index
< 0 || index
>= sizeof(uint64_t) * 8)
306 panic("int_num out of bounds\n");
308 interrupts
[int_num
] &= ~(1 << index
);
309 if (interrupts
[int_num
] == 0)
310 intstatus
&= ~(ULL(1) << int_num
);
314 BaseCPU::clear_interrupts()
316 DPRINTF(Interrupt
, "Interrupts all cleared\n");
318 memset(interrupts
, 0, sizeof(interrupts
));
324 BaseCPU::serialize(std::ostream
&os
)
326 SERIALIZE_ARRAY(interrupts
, NumInterruptLevels
);
327 SERIALIZE_SCALAR(intstatus
);
331 BaseCPU::unserialize(Checkpoint
*cp
, const std::string
§ion
)
333 UNSERIALIZE_ARRAY(interrupts
, NumInterruptLevels
);
334 UNSERIALIZE_SCALAR(intstatus
);
337 #endif // FULL_SYSTEM
340 BaseCPU::traceFunctionsInternal(Addr pc
)
342 if (!debugSymbolTable
)
345 // if pc enters different function, print new function symbol and
346 // update saved range. Otherwise do nothing.
347 if (pc
< currentFunctionStart
|| pc
>= currentFunctionEnd
) {
349 bool found
= debugSymbolTable
->findNearestSymbol(pc
, sym_str
,
350 currentFunctionStart
,
354 // no symbol found: use addr as label
355 sym_str
= csprintf("0x%x", pc
);
356 currentFunctionStart
= pc
;
357 currentFunctionEnd
= pc
+ 1;
360 ccprintf(*functionTraceStream
, " (%d)\n%d: %s",
361 curTick
- functionEntryTick
, curTick
, sym_str
);
362 functionEntryTick
= curTick
;
367 DEFINE_SIM_OBJECT_CLASS_NAME("BaseCPU", BaseCPU
)