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/sim_events.hh"
44 #include "base/trace.hh"
48 vector
<BaseCPU
*> BaseCPU::cpuList
;
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
53 int maxThreadsPerCPU
= 1;
56 BaseCPU::BaseCPU(Params
*p
)
57 : SimObject(p
->name
), clock(p
->clock
), checkInterrupts(true),
58 params(p
), number_of_threads(p
->numberOfThreads
), system(p
->system
)
60 BaseCPU::BaseCPU(Params
*p
)
61 : SimObject(p
->name
), clock(p
->clock
), params(p
),
62 number_of_threads(p
->numberOfThreads
)
65 DPRINTF(FullCPU
, "BaseCPU: Creating object, mem address %#x.\n", this);
67 // add self to global list of CPUs
68 cpuList
.push_back(this);
70 DPRINTF(FullCPU
, "BaseCPU: CPU added to cpuList, mem address %#x.\n",
73 if (number_of_threads
> maxThreadsPerCPU
)
74 maxThreadsPerCPU
= number_of_threads
;
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");
82 // set up instruction-count-based termination events, if any
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");
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
);
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");
107 // set up instruction-count-based termination events, if any
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");
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
);
127 memset(interrupts
, 0, sizeof(interrupts
));
131 functionTracingEnabled
= false;
132 if (p
->functionTrace
) {
133 functionTraceStream
= simout
.find(csprintf("ftrace.%s", name()));
134 currentFunctionStart
= currentFunctionEnd
= 0;
135 functionEntryTick
= p
->functionTraceStart
;
137 if (p
->functionTraceStart
== 0) {
138 functionTracingEnabled
= true;
141 new EventWrapper
<BaseCPU
, &BaseCPU::enableFunctionTrace
>(this,
143 e
->schedule(p
->functionTraceStart
);
149 profileEvent
= new ProfileEvent(this, params
->profile
);
153 BaseCPU::Params::Params()
161 BaseCPU::enableFunctionTrace()
163 functionTracingEnabled
= true;
173 if (!params
->deferRegistration
)
174 registerExecContexts();
181 if (!params
->deferRegistration
&& profileEvent
)
182 profileEvent
->schedule(curTick
);
190 using namespace Stats
;
193 .name(name() + ".numCycles")
194 .desc("number of cpu cycles simulated")
197 int size
= execContexts
.size();
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());
204 } else if (size
== 1)
205 execContexts
[0]->regStats(name());
210 BaseCPU::registerExecContexts()
212 for (int i
= 0; i
< execContexts
.size(); ++i
) {
213 ExecContext
*xc
= execContexts
[i
];
215 int id
= params
->cpu_id
;
219 xc
->cpu_id
= system
->registerExecContext(xc
, id
);
221 xc
->cpu_id
= xc
->process
->registerExecContext(xc
);
228 BaseCPU::switchOut(Sampler
*sampler
)
230 panic("This CPU doesn't support sampling!");
234 BaseCPU::takeOverFrom(BaseCPU
*oldCPU
)
236 assert(execContexts
.size() == oldCPU
->execContexts
.size());
238 for (int i
= 0; i
< execContexts
.size(); ++i
) {
239 ExecContext
*newXC
= execContexts
[i
];
240 ExecContext
*oldXC
= oldCPU
->execContexts
[i
];
242 newXC
->takeOverFrom(oldXC
);
243 assert(newXC
->cpu_id
== oldXC
->cpu_id
);
245 system
->replaceExecContext(newXC
, newXC
->cpu_id
);
247 assert(newXC
->process
== oldXC
->process
);
248 newXC
->process
->replaceExecContext(newXC
, newXC
->cpu_id
);
253 for (int i
= 0; i
< TheISA::NumInterruptLevels
; ++i
)
254 interrupts
[i
] = oldCPU
->interrupts
[i
];
255 intstatus
= oldCPU
->intstatus
;
257 for (int i
= 0; i
< execContexts
.size(); ++i
)
258 if (execContexts
[i
]->profile
)
259 execContexts
[i
]->profile
->clear();
262 profileEvent
->schedule(curTick
);
268 BaseCPU::ProfileEvent::ProfileEvent(BaseCPU
*_cpu
, int _interval
)
269 : Event(&mainEventQueue
), cpu(_cpu
), interval(_interval
)
273 BaseCPU::ProfileEvent::process()
275 for (int i
= 0, size
= cpu
->execContexts
.size(); i
< size
; ++i
) {
276 ExecContext
*xc
= cpu
->execContexts
[i
];
277 xc
->profile
->sample(xc
->profileNode
, xc
->profilePC
);
280 schedule(curTick
+ interval
);
284 BaseCPU::post_interrupt(int int_num
, int index
)
286 DPRINTF(Interrupt
, "Interrupt %d:%d posted\n", int_num
, index
);
288 if (int_num
< 0 || int_num
>= TheISA::NumInterruptLevels
)
289 panic("int_num out of bounds\n");
291 if (index
< 0 || index
>= sizeof(uint64_t) * 8)
292 panic("int_num out of bounds\n");
294 checkInterrupts
= true;
295 interrupts
[int_num
] |= 1 << index
;
296 intstatus
|= (ULL(1) << int_num
);
300 BaseCPU::clear_interrupt(int int_num
, int index
)
302 DPRINTF(Interrupt
, "Interrupt %d:%d cleared\n", int_num
, index
);
304 if (int_num
< 0 || int_num
>= TheISA::NumInterruptLevels
)
305 panic("int_num out of bounds\n");
307 if (index
< 0 || index
>= sizeof(uint64_t) * 8)
308 panic("int_num out of bounds\n");
310 interrupts
[int_num
] &= ~(1 << index
);
311 if (interrupts
[int_num
] == 0)
312 intstatus
&= ~(ULL(1) << int_num
);
316 BaseCPU::clear_interrupts()
318 DPRINTF(Interrupt
, "Interrupts all cleared\n");
320 memset(interrupts
, 0, sizeof(interrupts
));
326 BaseCPU::serialize(std::ostream
&os
)
328 SERIALIZE_ARRAY(interrupts
, TheISA::NumInterruptLevels
);
329 SERIALIZE_SCALAR(intstatus
);
333 BaseCPU::unserialize(Checkpoint
*cp
, const std::string
§ion
)
335 UNSERIALIZE_ARRAY(interrupts
, TheISA::NumInterruptLevels
);
336 UNSERIALIZE_SCALAR(intstatus
);
339 #endif // FULL_SYSTEM
342 BaseCPU::traceFunctionsInternal(Addr pc
)
344 if (!debugSymbolTable
)
347 // if pc enters different function, print new function symbol and
348 // update saved range. Otherwise do nothing.
349 if (pc
< currentFunctionStart
|| pc
>= currentFunctionEnd
) {
351 bool found
= debugSymbolTable
->findNearestSymbol(pc
, sym_str
,
352 currentFunctionStart
,
356 // no symbol found: use addr as label
357 sym_str
= csprintf("0x%x", pc
);
358 currentFunctionStart
= pc
;
359 currentFunctionEnd
= pc
+ 1;
362 ccprintf(*functionTraceStream
, " (%d)\n%d: %s",
363 curTick
- functionEntryTick
, curTick
, sym_str
);
364 functionEntryTick
= curTick
;
369 DEFINE_SIM_OBJECT_CLASS_NAME("BaseCPU", BaseCPU
)