Merge zizzer:/bk/m5 into isabel.reinhardt.house:/z/stever/bk/m5
[gem5.git] / cpu / exec_context.cc
1 /*
2 * Copyright (c) 2001-2004 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 */
28
29 #include <string>
30
31 #include "cpu/base_cpu.hh"
32 #include "cpu/exec_context.hh"
33
34 #ifdef FULL_SYSTEM
35 #include "sim/system.hh"
36 #else
37 #include "sim/process.hh"
38 #endif
39
40 using namespace std;
41
42 // constructor
43 #ifdef FULL_SYSTEM
44 ExecContext::ExecContext(BaseCPU *_cpu, int _thread_num, System *_sys,
45 AlphaITB *_itb, AlphaDTB *_dtb,
46 FunctionalMemory *_mem)
47 : _status(ExecContext::Unallocated),
48 kernelStats(this, _cpu), cpu(_cpu), thread_num(_thread_num),
49 cpu_id(-1), mem(_mem), itb(_itb), dtb(_dtb), system(_sys),
50 memCtrl(_sys->memCtrl), physmem(_sys->physmem),
51 swCtx(NULL), func_exe_inst(0), storeCondFailures(0)
52 {
53 memset(&regs, 0, sizeof(RegFile));
54 }
55 #else
56 ExecContext::ExecContext(BaseCPU *_cpu, int _thread_num,
57 Process *_process, int _asid)
58 : _status(ExecContext::Unallocated),
59 cpu(_cpu), thread_num(_thread_num), cpu_id(-1),
60 process(_process), mem(process->getMemory()), asid(_asid),
61 func_exe_inst(0), storeCondFailures(0)
62 {
63 memset(&regs, 0, sizeof(RegFile));
64 }
65
66 ExecContext::ExecContext(BaseCPU *_cpu, int _thread_num,
67 FunctionalMemory *_mem, int _asid)
68 : cpu(_cpu), thread_num(_thread_num), process(0), mem(_mem), asid(_asid),
69 func_exe_inst(0), storeCondFailures(0)
70 {
71 memset(&regs, 0, sizeof(RegFile));
72 }
73 #endif
74
75
76 void
77 ExecContext::takeOverFrom(ExecContext *oldContext)
78 {
79 // some things should already be set up
80 assert(mem == oldContext->mem);
81 #ifdef FULL_SYSTEM
82 assert(system == oldContext->system);
83 #else
84 assert(process == oldContext->process);
85 #endif
86
87 // copy over functional state
88 _status = oldContext->_status;
89 #ifdef FULL_SYSTEM
90 kernelStats = oldContext->kernelStats;
91 #endif
92 regs = oldContext->regs;
93 cpu_id = oldContext->cpu_id;
94 func_exe_inst = oldContext->func_exe_inst;
95
96 storeCondFailures = 0;
97
98 oldContext->_status = ExecContext::Unallocated;
99 }
100
101
102 void
103 ExecContext::serialize(ostream &os)
104 {
105 SERIALIZE_ENUM(_status);
106 regs.serialize(os);
107 // thread_num and cpu_id are deterministic from the config
108 SERIALIZE_SCALAR(func_exe_inst);
109 SERIALIZE_SCALAR(inst);
110
111 #ifdef FULL_SYSTEM
112 bool ctx = false;
113 if (swCtx) {
114 ctx = true;
115 SERIALIZE_SCALAR(ctx);
116 SERIALIZE_SCALAR(swCtx->calls);
117 std::stack<fnCall *> *stack = &(swCtx->callStack);
118 fnCall *top;
119 int size = stack->size();
120 SERIALIZE_SCALAR(size);
121
122 for (int j=0; j<size; ++j) {
123 top = stack->top();
124 paramOut(os, csprintf("stackpos[%d]",j), top->name);
125 delete top;
126 stack->pop();
127 }
128 } else {
129 SERIALIZE_SCALAR(ctx);
130 }
131 if (system->bin) {
132 Stats::MainBin *cur = Stats::MainBin::curBin();
133 string bin_name = cur->name();
134 SERIALIZE_SCALAR(bin_name);
135 }
136 #endif //FULL_SYSTEM
137 }
138
139
140 void
141 ExecContext::unserialize(Checkpoint *cp, const std::string &section)
142 {
143 UNSERIALIZE_ENUM(_status);
144 regs.unserialize(cp, section);
145 // thread_num and cpu_id are deterministic from the config
146 UNSERIALIZE_SCALAR(func_exe_inst);
147 UNSERIALIZE_SCALAR(inst);
148
149 #ifdef FULL_SYSTEM
150 bool ctx;
151 UNSERIALIZE_SCALAR(ctx);
152 if (ctx) {
153 swCtx = new SWContext;
154 UNSERIALIZE_SCALAR(swCtx->calls);
155 int size;
156 UNSERIALIZE_SCALAR(size);
157
158 vector<fnCall *> calls;
159 fnCall *call;
160 for (int i=0; i<size; ++i) {
161 call = new fnCall;
162 paramIn(cp, section, csprintf("stackpos[%d]",i), call->name);
163 call->myBin = system->getBin(call->name);
164 calls.push_back(call);
165 }
166
167 for (int i=size-1; i>=0; --i) {
168 swCtx->callStack.push(calls[i]);
169 }
170
171 }
172
173 if (system->bin) {
174 string bin_name;
175 UNSERIALIZE_SCALAR(bin_name);
176 system->getBin(bin_name)->activate();
177 }
178 #endif //FULL_SYSTEM
179 }
180
181
182 void
183 ExecContext::activate(int delay)
184 {
185 if (status() == Active)
186 return;
187
188 _status = Active;
189 cpu->activateContext(thread_num, delay);
190 }
191
192 void
193 ExecContext::suspend()
194 {
195 if (status() == Suspended)
196 return;
197
198 #ifdef FULL_SYSTEM
199 // Don't change the status from active if there are pending interrupts
200 if (cpu->check_interrupts()) {
201 assert(status() == Active);
202 return;
203 }
204 #endif
205
206 _status = Suspended;
207 cpu->suspendContext(thread_num);
208 }
209
210 void
211 ExecContext::deallocate()
212 {
213 if (status() == Unallocated)
214 return;
215
216 _status = Unallocated;
217 cpu->deallocateContext(thread_num);
218 }
219
220 void
221 ExecContext::halt()
222 {
223 if (status() == Halted)
224 return;
225
226 _status = Halted;
227 cpu->haltContext(thread_num);
228 }
229
230
231 void
232 ExecContext::regStats(const string &name)
233 {
234 #ifdef FULL_SYSTEM
235 kernelStats.regStats(name + ".kern");
236 #endif
237 }
238
239 void
240 ExecContext::trap(Fault fault)
241 {
242 //TheISA::trap(fault); //One possible way to do it...
243
244 /** @todo: Going to hack it for now. Do a true fixup later. */
245 #ifdef FULL_SYSTEM
246 ev5_trap(fault);
247 #else
248 fatal("fault (%d) detected @ PC 0x%08p", fault, readPC());
249 #endif
250 }