2 * Copyright (c) 2002-2004 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_cpu.hh"
38 #include "cpu/exec_context.hh"
39 #include "cpu/sampling_cpu/sampling_cpu.hh"
40 #include "sim/param.hh"
41 #include "sim/sim_events.hh"
45 vector
<BaseCPU
*> BaseCPU::cpuList
;
47 // This variable reflects the max number of threads in any CPU. Be
48 // careful to only use it once all the CPUs that you care about have
50 int maxThreadsPerCPU
= 1;
53 BaseCPU::BaseCPU(Params
*p
)
54 : SimObject(p
->name
), frequency(p
->freq
), checkInterrupts(true),
55 params(p
), number_of_threads(p
->numberOfThreads
), system(p
->system
)
57 BaseCPU::BaseCPU(Params
*p
)
58 : SimObject(p
->name
), params(p
), number_of_threads(p
->numberOfThreads
)
61 // add self to global list of CPUs
62 cpuList
.push_back(this);
64 if (number_of_threads
> maxThreadsPerCPU
)
65 maxThreadsPerCPU
= number_of_threads
;
67 // allocate per-thread instruction-based event queues
68 comInstEventQueue
= new EventQueue
*[number_of_threads
];
69 for (int i
= 0; i
< number_of_threads
; ++i
)
70 comInstEventQueue
[i
] = new EventQueue("instruction-based event queue");
73 // set up instruction-count-based termination events, if any
75 if (p
->max_insts_any_thread
!= 0)
76 for (int i
= 0; i
< number_of_threads
; ++i
)
77 new SimExitEvent(comInstEventQueue
[i
], p
->max_insts_any_thread
,
78 "a thread reached the max instruction count");
80 if (p
->max_insts_all_threads
!= 0) {
81 // allocate & initialize shared downcounter: each event will
82 // decrement this when triggered; simulation will terminate
83 // when counter reaches 0
84 int *counter
= new int;
85 *counter
= number_of_threads
;
86 for (int i
= 0; i
< number_of_threads
; ++i
)
87 new CountedExitEvent(comInstEventQueue
[i
],
88 "all threads reached the max instruction count",
89 p
->max_insts_all_threads
, *counter
);
92 // allocate per-thread load-based event queues
93 comLoadEventQueue
= new EventQueue
*[number_of_threads
];
94 for (int i
= 0; i
< number_of_threads
; ++i
)
95 comLoadEventQueue
[i
] = new EventQueue("load-based event queue");
98 // set up instruction-count-based termination events, if any
100 if (p
->max_loads_any_thread
!= 0)
101 for (int i
= 0; i
< number_of_threads
; ++i
)
102 new SimExitEvent(comLoadEventQueue
[i
], p
->max_loads_any_thread
,
103 "a thread reached the max load count");
105 if (p
->max_loads_all_threads
!= 0) {
106 // allocate & initialize shared downcounter: each event will
107 // decrement this when triggered; simulation will terminate
108 // when counter reaches 0
109 int *counter
= new int;
110 *counter
= number_of_threads
;
111 for (int i
= 0; i
< number_of_threads
; ++i
)
112 new CountedExitEvent(comLoadEventQueue
[i
],
113 "all threads reached the max load count",
114 p
->max_loads_all_threads
, *counter
);
118 memset(interrupts
, 0, sizeof(interrupts
));
122 functionTracingEnabled
= false;
123 if (p
->functionTrace
) {
124 functionTraceStream
= simout
.find(csprintf("ftrace.%s", name()));
125 currentFunctionStart
= currentFunctionEnd
= 0;
126 functionEntryTick
= p
->functionTraceStart
;
128 if (p
->functionTraceStart
== 0) {
129 functionTracingEnabled
= true;
132 new EventWrapper
<BaseCPU
, &BaseCPU::enableFunctionTrace
>(this,
134 e
->schedule(p
->functionTraceStart
);
141 BaseCPU::enableFunctionTrace()
143 functionTracingEnabled
= true;
153 if (!params
->deferRegistration
)
154 registerExecContexts();
160 using namespace Stats
;
163 .name(name() + ".numCycles")
164 .desc("number of cpu cycles simulated")
167 int size
= execContexts
.size();
169 for (int i
= 0; i
< size
; ++i
) {
170 stringstream namestr
;
171 ccprintf(namestr
, "%s.ctx%d", name(), i
);
172 execContexts
[i
]->regStats(namestr
.str());
174 } else if (size
== 1)
175 execContexts
[0]->regStats(name());
180 BaseCPU::registerExecContexts()
182 for (int i
= 0; i
< execContexts
.size(); ++i
) {
183 ExecContext
*xc
= execContexts
[i
];
187 cpu_id
= system
->registerExecContext(xc
);
189 cpu_id
= xc
->process
->registerExecContext(xc
);
198 BaseCPU::switchOut(SamplingCPU
*sampler
)
200 // default: do nothing, signal done
201 sampler
->signalSwitched();
205 BaseCPU::takeOverFrom(BaseCPU
*oldCPU
)
207 assert(execContexts
.size() == oldCPU
->execContexts
.size());
209 for (int i
= 0; i
< execContexts
.size(); ++i
) {
210 ExecContext
*newXC
= execContexts
[i
];
211 ExecContext
*oldXC
= oldCPU
->execContexts
[i
];
213 newXC
->takeOverFrom(oldXC
);
214 assert(newXC
->cpu_id
== oldXC
->cpu_id
);
216 system
->replaceExecContext(newXC
, newXC
->cpu_id
);
218 assert(newXC
->process
== oldXC
->process
);
219 newXC
->process
->replaceExecContext(newXC
, newXC
->cpu_id
);
224 for (int i
= 0; i
< NumInterruptLevels
; ++i
)
225 interrupts
[i
] = oldCPU
->interrupts
[i
];
226 intstatus
= oldCPU
->intstatus
;
233 BaseCPU::post_interrupt(int int_num
, int index
)
235 DPRINTF(Interrupt
, "Interrupt %d:%d posted\n", int_num
, index
);
237 if (int_num
< 0 || int_num
>= NumInterruptLevels
)
238 panic("int_num out of bounds\n");
240 if (index
< 0 || index
>= sizeof(uint64_t) * 8)
241 panic("int_num out of bounds\n");
243 checkInterrupts
= true;
244 interrupts
[int_num
] |= 1 << index
;
245 intstatus
|= (ULL(1) << int_num
);
249 BaseCPU::clear_interrupt(int int_num
, int index
)
251 DPRINTF(Interrupt
, "Interrupt %d:%d cleared\n", int_num
, index
);
253 if (int_num
< 0 || int_num
>= NumInterruptLevels
)
254 panic("int_num out of bounds\n");
256 if (index
< 0 || index
>= sizeof(uint64_t) * 8)
257 panic("int_num out of bounds\n");
259 interrupts
[int_num
] &= ~(1 << index
);
260 if (interrupts
[int_num
] == 0)
261 intstatus
&= ~(ULL(1) << int_num
);
265 BaseCPU::clear_interrupts()
267 DPRINTF(Interrupt
, "Interrupts all cleared\n");
269 memset(interrupts
, 0, sizeof(interrupts
));
275 BaseCPU::serialize(std::ostream
&os
)
277 SERIALIZE_ARRAY(interrupts
, NumInterruptLevels
);
278 SERIALIZE_SCALAR(intstatus
);
282 BaseCPU::unserialize(Checkpoint
*cp
, const std::string
§ion
)
284 UNSERIALIZE_ARRAY(interrupts
, NumInterruptLevels
);
285 UNSERIALIZE_SCALAR(intstatus
);
288 #endif // FULL_SYSTEM
291 BaseCPU::traceFunctionsInternal(Addr pc
)
293 if (!debugSymbolTable
)
296 // if pc enters different function, print new function symbol and
297 // update saved range. Otherwise do nothing.
298 if (pc
< currentFunctionStart
|| pc
>= currentFunctionEnd
) {
300 bool found
= debugSymbolTable
->findNearestSymbol(pc
, sym_str
,
301 currentFunctionStart
,
305 // no symbol found: use addr as label
306 sym_str
= csprintf("0x%x", pc
);
307 currentFunctionStart
= pc
;
308 currentFunctionEnd
= pc
+ 1;
311 ccprintf(*functionTraceStream
, " (%d)\n%d: %s",
312 curTick
- functionEntryTick
, curTick
, sym_str
);
313 functionEntryTick
= curTick
;
318 DEFINE_SIM_OBJECT_CLASS_NAME("BaseCPU", BaseCPU
)