Merge zizzer.eecs.umich.edu:/m5/Bitkeeper/linux
[gem5.git] / cpu / exec_context.cc
1 /*
2 * Copyright (c) 2003 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 #ifdef FS_MEASURE
52 swCtx(NULL),
53 #endif
54 func_exe_inst(0), storeCondFailures(0)
55 {
56 memset(&regs, 0, sizeof(RegFile));
57 }
58 #else
59 ExecContext::ExecContext(BaseCPU *_cpu, int _thread_num,
60 Process *_process, int _asid)
61 : _status(ExecContext::Unallocated),
62 cpu(_cpu), thread_num(_thread_num), cpu_id(-1),
63 process(_process), mem(process->getMemory()), asid(_asid),
64 func_exe_inst(0), storeCondFailures(0)
65 {
66 }
67
68 ExecContext::ExecContext(BaseCPU *_cpu, int _thread_num,
69 FunctionalMemory *_mem, int _asid)
70 : cpu(_cpu), thread_num(_thread_num), process(0), mem(_mem), asid(_asid),
71 func_exe_inst(0), storeCondFailures(0)
72 {
73 }
74 #endif
75
76
77 void
78 ExecContext::takeOverFrom(ExecContext *oldContext)
79 {
80 // some things should already be set up
81 assert(mem == oldContext->mem);
82 #ifdef FULL_SYSTEM
83 assert(system == oldContext->system);
84 #else
85 assert(process == oldContext->process);
86 #endif
87
88 // copy over functional state
89 _status = oldContext->_status;
90 #ifdef FULL_SYSTEM
91 kernelStats = oldContext->kernelStats;
92 #endif
93 regs = oldContext->regs;
94 cpu_id = oldContext->cpu_id;
95 func_exe_inst = oldContext->func_exe_inst;
96
97 storeCondFailures = 0;
98
99 oldContext->_status = ExecContext::Unallocated;
100 }
101
102
103 void
104 ExecContext::serialize(ostream &os)
105 {
106 SERIALIZE_ENUM(_status);
107 regs.serialize(os);
108 // thread_num and cpu_id are deterministic from the config
109 SERIALIZE_SCALAR(func_exe_inst);
110 }
111
112
113 void
114 ExecContext::unserialize(Checkpoint *cp, const std::string &section)
115 {
116 UNSERIALIZE_ENUM(_status);
117 regs.unserialize(cp, section);
118 // thread_num and cpu_id are deterministic from the config
119 UNSERIALIZE_SCALAR(func_exe_inst);
120 }
121
122
123 void
124 ExecContext::activate(int delay)
125 {
126 if (status() == Active)
127 return;
128
129 _status = Active;
130 cpu->activateContext(thread_num, delay);
131 }
132
133 void
134 ExecContext::suspend()
135 {
136 if (status() == Suspended)
137 return;
138
139 #ifdef FULL_SYSTEM
140 // Don't change the status from active if there are pending interrupts
141 if (cpu->check_interrupts()) {
142 assert(status() == Active);
143 return;
144 }
145 #endif
146
147 _status = Suspended;
148 cpu->suspendContext(thread_num);
149 }
150
151 void
152 ExecContext::deallocate()
153 {
154 if (status() == Unallocated)
155 return;
156
157 _status = Unallocated;
158 cpu->deallocateContext(thread_num);
159 }
160
161 void
162 ExecContext::halt()
163 {
164 if (status() == Halted)
165 return;
166
167 _status = Halted;
168 cpu->haltContext(thread_num);
169 }
170
171
172 void
173 ExecContext::regStats(const string &name)
174 {
175 #ifdef FULL_SYSTEM
176 kernelStats.regStats(name + ".kern");
177 #endif
178 }