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
);
149 BaseCPU::enableFunctionTrace()
151 functionTracingEnabled
= true;
161 if (!params
->deferRegistration
)
162 registerExecContexts();
168 using namespace Stats
;
171 .name(name() + ".numCycles")
172 .desc("number of cpu cycles simulated")
175 int size
= execContexts
.size();
177 for (int i
= 0; i
< size
; ++i
) {
178 stringstream namestr
;
179 ccprintf(namestr
, "%s.ctx%d", name(), i
);
180 execContexts
[i
]->regStats(namestr
.str());
182 } else if (size
== 1)
183 execContexts
[0]->regStats(name());
188 BaseCPU::registerExecContexts()
190 for (int i
= 0; i
< execContexts
.size(); ++i
) {
191 ExecContext
*xc
= execContexts
[i
];
195 cpu_id
= system
->registerExecContext(xc
);
197 cpu_id
= xc
->process
->registerExecContext(xc
);
206 BaseCPU::switchOut(Sampler
*sampler
)
208 panic("This CPU doesn't support sampling!");
212 BaseCPU::takeOverFrom(BaseCPU
*oldCPU
)
214 assert(execContexts
.size() == oldCPU
->execContexts
.size());
216 for (int i
= 0; i
< execContexts
.size(); ++i
) {
217 ExecContext
*newXC
= execContexts
[i
];
218 ExecContext
*oldXC
= oldCPU
->execContexts
[i
];
220 newXC
->takeOverFrom(oldXC
);
221 assert(newXC
->cpu_id
== oldXC
->cpu_id
);
223 system
->replaceExecContext(newXC
, newXC
->cpu_id
);
225 assert(newXC
->process
== oldXC
->process
);
226 newXC
->process
->replaceExecContext(newXC
, newXC
->cpu_id
);
231 for (int i
= 0; i
< NumInterruptLevels
; ++i
)
232 interrupts
[i
] = oldCPU
->interrupts
[i
];
233 intstatus
= oldCPU
->intstatus
;
240 BaseCPU::post_interrupt(int int_num
, int index
)
242 DPRINTF(Interrupt
, "Interrupt %d:%d posted\n", int_num
, index
);
244 if (int_num
< 0 || int_num
>= NumInterruptLevels
)
245 panic("int_num out of bounds\n");
247 if (index
< 0 || index
>= sizeof(uint64_t) * 8)
248 panic("int_num out of bounds\n");
250 checkInterrupts
= true;
251 interrupts
[int_num
] |= 1 << index
;
252 intstatus
|= (ULL(1) << int_num
);
256 BaseCPU::clear_interrupt(int int_num
, int index
)
258 DPRINTF(Interrupt
, "Interrupt %d:%d cleared\n", int_num
, index
);
260 if (int_num
< 0 || int_num
>= NumInterruptLevels
)
261 panic("int_num out of bounds\n");
263 if (index
< 0 || index
>= sizeof(uint64_t) * 8)
264 panic("int_num out of bounds\n");
266 interrupts
[int_num
] &= ~(1 << index
);
267 if (interrupts
[int_num
] == 0)
268 intstatus
&= ~(ULL(1) << int_num
);
272 BaseCPU::clear_interrupts()
274 DPRINTF(Interrupt
, "Interrupts all cleared\n");
276 memset(interrupts
, 0, sizeof(interrupts
));
282 BaseCPU::serialize(std::ostream
&os
)
284 SERIALIZE_ARRAY(interrupts
, NumInterruptLevels
);
285 SERIALIZE_SCALAR(intstatus
);
289 BaseCPU::unserialize(Checkpoint
*cp
, const std::string
§ion
)
291 UNSERIALIZE_ARRAY(interrupts
, NumInterruptLevels
);
292 UNSERIALIZE_SCALAR(intstatus
);
295 #endif // FULL_SYSTEM
298 BaseCPU::traceFunctionsInternal(Addr pc
)
300 if (!debugSymbolTable
)
303 // if pc enters different function, print new function symbol and
304 // update saved range. Otherwise do nothing.
305 if (pc
< currentFunctionStart
|| pc
>= currentFunctionEnd
) {
307 bool found
= debugSymbolTable
->findNearestSymbol(pc
, sym_str
,
308 currentFunctionStart
,
312 // no symbol found: use addr as label
313 sym_str
= csprintf("0x%x", pc
);
314 currentFunctionStart
= pc
;
315 currentFunctionEnd
= pc
+ 1;
318 ccprintf(*functionTraceStream
, " (%d)\n%d: %s",
319 curTick
- functionEntryTick
, curTick
, sym_str
);
320 functionEntryTick
= curTick
;
325 DEFINE_SIM_OBJECT_CLASS_NAME("BaseCPU", BaseCPU
)