Added support for exclusive state, defined the MESI and MOESI protocols
[gem5.git] / sim / system.cc
1 /*
2 * Copyright (c) 2002-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 "base/loader/object_file.hh"
30 #include "base/loader/symtab.hh"
31 #include "base/remote_gdb.hh"
32 #include "cpu/exec_context.hh"
33 #include "kern/kernel_stats.hh"
34 #include "mem/functional_mem/memory_control.hh"
35 #include "mem/functional_mem/physical_memory.hh"
36 #include "targetarch/vtophys.hh"
37 #include "sim/param.hh"
38 #include "sim/system.hh"
39 #include "base/trace.hh"
40
41 using namespace std;
42
43 vector<System *> System::systemList;
44
45 int System::numSystemsRunning = 0;
46
47 extern SymbolTable *debugSymbolTable;
48
49 System::System(Params *p)
50 : SimObject(p->name), memctrl(p->memctrl), physmem(p->physmem),
51 init_param(p->init_param), params(p)
52 {
53 // add self to global system list
54 systemList.push_back(this);
55
56 kernelSymtab = new SymbolTable;
57 consoleSymtab = new SymbolTable;
58 palSymtab = new SymbolTable;
59 debugSymbolTable = new SymbolTable;
60
61 /**
62 * Load the kernel, pal, and console code into memory
63 */
64 // Load kernel code
65 kernel = createObjectFile(params->kernel_path);
66 if (kernel == NULL)
67 fatal("Could not load kernel file %s", params->kernel_path);
68
69 // Load Console Code
70 console = createObjectFile(params->console_path);
71 if (console == NULL)
72 fatal("Could not load console file %s", params->console_path);
73
74 // Load pal file
75 pal = createObjectFile(params->palcode);
76 if (pal == NULL)
77 fatal("Could not load PALcode file %s", params->palcode);
78
79
80 // Load program sections into memory
81 pal->loadSections(physmem, true);
82 console->loadSections(physmem, true);
83 kernel->loadSections(physmem, true);
84
85 // setup entry points
86 kernelStart = kernel->textBase();
87 kernelEnd = kernel->bssBase() + kernel->bssSize();
88 kernelEntry = kernel->entryPoint();
89
90 // load symbols
91 if (!kernel->loadGlobalSymbols(kernelSymtab))
92 panic("could not load kernel symbols\n");
93
94 if (!kernel->loadLocalSymbols(kernelSymtab))
95 panic("could not load kernel local symbols\n");
96
97 if (!console->loadGlobalSymbols(consoleSymtab))
98 panic("could not load console symbols\n");
99
100 if (!pal->loadGlobalSymbols(palSymtab))
101 panic("could not load pal symbols\n");
102
103 if (!pal->loadLocalSymbols(palSymtab))
104 panic("could not load pal symbols\n");
105
106 if (!kernel->loadGlobalSymbols(debugSymbolTable))
107 panic("could not load kernel symbols\n");
108
109 if (!kernel->loadLocalSymbols(debugSymbolTable))
110 panic("could not load kernel local symbols\n");
111
112 if (!console->loadGlobalSymbols(debugSymbolTable))
113 panic("could not load console symbols\n");
114
115 if (!pal->loadGlobalSymbols(debugSymbolTable))
116 panic("could not load pal symbols\n");
117
118 if (!pal->loadLocalSymbols(debugSymbolTable))
119 panic("could not load pal symbols\n");
120
121
122 DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
123 DPRINTF(Loader, "Kernel end = %#x\n", kernelEnd);
124 DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
125 DPRINTF(Loader, "Kernel loaded...\n");
126
127 Addr addr = 0;
128 #ifdef DEBUG
129 consolePanicEvent = new BreakPCEvent(&pcEventQueue, "console panic");
130 if (consoleSymtab->findAddress("panic", addr))
131 consolePanicEvent->schedule(addr);
132 #endif
133
134 /**
135 * Copy the osflags (kernel arguments) into the consoles
136 * memory. (Presently Linux does not use the console service
137 * routine to get these command line arguments, but Tru64 and
138 * others do.)
139 */
140 if (consoleSymtab->findAddress("env_booted_osflags", addr)) {
141 Addr paddr = vtophys(physmem, addr);
142 char *osflags = (char *)physmem->dma_addr(paddr, sizeof(uint32_t));
143
144 if (osflags)
145 strcpy(osflags, params->boot_osflags.c_str());
146 }
147
148 /**
149 * Set the hardware reset parameter block system type and revision
150 * information to Tsunami.
151 */
152 if (consoleSymtab->findAddress("xxm_rpb", addr)) {
153 Addr paddr = vtophys(physmem, addr);
154 char *hwrpb = (char *)physmem->dma_addr(paddr, sizeof(uint64_t));
155
156 if (!hwrpb)
157 panic("could not translate hwrpb addr\n");
158
159 *(uint64_t*)(hwrpb+0x50) = params->system_type;
160 *(uint64_t*)(hwrpb+0x58) = params->system_rev;
161 } else
162 panic("could not find hwrpb\n");
163
164 // increment the number of running systms
165 numSystemsRunning++;
166
167 kernelBinning = new Kernel::Binning(this);
168 }
169
170 System::~System()
171 {
172 delete kernelSymtab;
173 delete consoleSymtab;
174 delete kernel;
175 delete console;
176 delete pal;
177
178 delete kernelBinning;
179
180 #ifdef DEBUG
181 delete consolePanicEvent;
182 #endif
183 }
184
185 bool
186 System::breakpoint()
187 {
188 return remoteGDB[0]->trap(ALPHA_KENTRY_INT);
189 }
190
191 int
192 System::registerExecContext(ExecContext *xc)
193 {
194 int xcIndex = execContexts.size();
195 execContexts.push_back(xc);
196
197 if (xcIndex == 0) {
198 // activate with zero delay so that we start ticking right
199 // away on cycle 0
200 xc->activate(0);
201 }
202
203 RemoteGDB *rgdb = new RemoteGDB(this, xc);
204 GDBListener *gdbl = new GDBListener(rgdb, 7000 + xcIndex);
205 gdbl->listen();
206 /**
207 * Uncommenting this line waits for a remote debugger to connect
208 * to the simulator before continuing.
209 */
210 //gdbl->accept();
211
212 if (remoteGDB.size() <= xcIndex) {
213 remoteGDB.resize(xcIndex+1);
214 }
215
216 remoteGDB[xcIndex] = rgdb;
217
218 return xcIndex;
219 }
220
221 void
222 System::replaceExecContext(ExecContext *xc, int xcIndex)
223 {
224 if (xcIndex >= execContexts.size()) {
225 panic("replaceExecContext: bad xcIndex, %d >= %d\n",
226 xcIndex, execContexts.size());
227 }
228
229 execContexts[xcIndex] = xc;
230 remoteGDB[xcIndex]->replaceExecContext(xc);
231 }
232
233 void
234 System::regStats()
235 {
236 kernelBinning->regStats(name() + ".kern");
237 }
238
239 void
240 System::serialize(ostream &os)
241 {
242 kernelBinning->serialize(os);
243 }
244
245
246 void
247 System::unserialize(Checkpoint *cp, const string &section)
248 {
249 kernelBinning->unserialize(cp, section);
250 }
251
252 void
253 System::printSystems()
254 {
255 vector<System *>::iterator i = systemList.begin();
256 vector<System *>::iterator end = systemList.end();
257 for (; i != end; ++i) {
258 System *sys = *i;
259 cerr << "System " << sys->name() << ": " << hex << sys << endl;
260 }
261 }
262
263 extern "C"
264 void
265 printSystems()
266 {
267 System::printSystems();
268 }
269
270 DEFINE_SIM_OBJECT_CLASS_NAME("System", System)
271