2 * Copyright (c) 2012 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2011 Regents of the University of California
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * Authors: Steve Reinhardt
54 #include "base/loader/symtab.hh"
55 #include "base/misc.hh"
56 #include "base/statistics.hh"
57 #include "cpu/pc_event.hh"
58 #include "enums/MemoryMode.hh"
59 #include "kern/system_events.hh"
60 #include "mem/fs_translating_port_proxy.hh"
61 #include "mem/mem_object.hh"
62 #include "mem/port.hh"
63 #include "mem/physical.hh"
64 #include "params/System.hh"
73 class System : public MemObject
78 * Private class for the system port which is only used as a
79 * master for debug access and for non-structural entities that do
80 * not have a port of their own.
82 class SystemPort : public MasterPort
87 * Create a system port with a name and an owner.
89 SystemPort(const std::string &_name, MemObject *_owner)
90 : MasterPort(_name, _owner)
92 bool recvTimingResp(PacketPtr pkt)
93 { panic("SystemPort does not receive timing!\n"); return false; }
95 { panic("SystemPort does not expect retry!\n"); }
98 SystemPort _systemPort;
103 * After all objects have been created and all ports are
104 * connected, check that the system port is connected.
109 * Get a reference to the system port that can be used by
110 * non-structural simulation objects like processes or threads, or
111 * external entities like loaders and debuggers, etc, to access
114 * @return a reference to the system port we own
116 MasterPort& getSystemPort() { return _systemPort; }
119 * Additional function to return the Port of a memory object.
121 BaseMasterPort& getMasterPort(const std::string &if_name,
122 PortID idx = InvalidPortID);
124 static const char *MemoryModeStrings[4];
128 * Is the system in atomic mode?
130 * There are currently two different atomic memory modes:
131 * 'atomic', which supports caches; and 'atomic_noncaching', which
132 * bypasses caches. The latter is used by hardware virtualized
133 * CPUs. SimObjects are expected to use Port::sendAtomic() and
134 * Port::recvAtomic() when accessing memory in this mode.
136 bool isAtomicMode() const {
137 return memoryMode == Enums::atomic ||
138 memoryMode == Enums::atomic_noncaching;
142 * Is the system in timing mode?
144 * SimObjects are expected to use Port::sendTiming() and
145 * Port::recvTiming() when accessing memory in this mode.
147 bool isTimingMode() const {
148 return memoryMode == Enums::timing;
152 * Should caches be bypassed?
154 * Some CPUs need to bypass caches to allow direct memory
155 * accesses, which is required for hardware virtualization.
157 bool bypassCaches() const {
158 return memoryMode == Enums::atomic_noncaching;
164 * Get the memory mode of the system.
166 * \warn This should only be used by the Python world. The C++
167 * world should use one of the query functions above
168 * (isAtomicMode(), isTimingMode(), bypassCaches()).
170 Enums::MemoryMode getMemoryMode() const { return memoryMode; }
173 * Change the memory mode of the system.
175 * \warn This should only be called by the Python!
177 * @param mode Mode to change to (atomic/timing/...)
179 void setMemoryMode(Enums::MemoryMode mode);
182 PCEventQueue pcEventQueue;
184 std::vector<ThreadContext *> threadContexts;
187 ThreadContext *getThreadContext(ThreadID tid)
189 return threadContexts[tid];
194 assert(_numContexts == (int)threadContexts.size());
198 /** Return number of running (non-halted) thread contexts in
199 * system. These threads could be Active or Suspended. */
200 int numRunningContexts();
206 /** Port to physical memory used for writing object files into ram at
209 FSTranslatingPortProxy virtProxy;
211 /** kernel symbol table */
212 SymbolTable *kernelSymtab;
214 /** Object pointer for the kernel code */
217 /** Begining of kernel code */
220 /** End of kernel code */
223 /** Entry point in the kernel to start at */
226 /** Mask that should be anded for binary/symbol loading.
227 * This allows one two different OS requirements for the same ISA to be
228 * handled. Some OSes are compiled for a virtual address and need to be
229 * loaded into physical memory that starts at address 0, while other
230 * bare metal tools generate images that start at address 0.
238 uint64_t allocatePID()
243 /** Get a pointer to access the physical memory of the system */
244 PhysicalMemory& getPhysMem() { return physmem; }
246 /** Amount of physical memory that is still free */
247 Addr freeMemSize() const;
249 /** Amount of physical memory that exists */
250 Addr memSize() const;
253 * Check if a physical address is within a range of a memory that
254 * is part of the global address map.
256 * @param addr A physical address
257 * @return Whether the address corresponds to a memory
259 bool isMemAddr(Addr addr) const;
263 PhysicalMemory physmem;
265 Enums::MemoryMode memoryMode;
266 uint64_t workItemsBegin;
267 uint64_t workItemsEnd;
269 std::vector<bool> activeCpus;
271 /** This array is a per-sytem list of all devices capable of issuing a
272 * memory system request and an associated string for each master id.
273 * It's used to uniquely id any master in the system by name for things
274 * like cache statistics.
276 std::vector<std::string> masterIds;
280 /** Request an id used to create a request object in the system. All objects
281 * that intend to issues requests into the memory system must request an id
282 * in the init() phase of startup. All master ids must be fixed by the
283 * regStats() phase that immediately preceeds it. This allows objects in the
284 * memory system to understand how many masters may exist and
285 * appropriately name the bins of their per-master stats before the stats
288 MasterID getMasterId(std::string req_name);
290 /** Get the name of an object for a given request id.
292 std::string getMasterName(MasterID master_id);
294 /** Get the number of masters registered in the system */
295 MasterID maxMasters()
297 return masterIds.size();
300 virtual void regStats();
302 * Called by pseudo_inst to track the number of work items started by this
308 return ++workItemsBegin;
312 * Called by pseudo_inst to track the number of work items completed by
318 return ++workItemsEnd;
322 * Called by pseudo_inst to mark the cpus actively executing work items.
323 * Returns the total number of cpus that have executed work item begin or
327 markWorkItem(int index)
330 assert(index < activeCpus.size());
331 activeCpus[index] = true;
332 for (std::vector<bool>::iterator i = activeCpus.begin();
333 i < activeCpus.end(); i++) {
339 inline void workItemBegin(uint32_t tid, uint32_t workid)
341 std::pair<uint32_t,uint32_t> p(tid, workid);
342 lastWorkItemStarted[p] = curTick();
345 void workItemEnd(uint32_t tid, uint32_t workid);
348 * Fix up an address used to match PCs for hooking simulator
349 * events on to target function executions. See comment in
350 * system.cc for details.
352 virtual Addr fixFuncEventAddr(Addr addr)
354 panic("Base fixFuncEventAddr not implemented.\n");
359 * Add a function-based event to the given function, to be looked
360 * up in the specified symbol table.
362 * The ...OrPanic flavor of the method causes the simulator to
363 * panic if the symbol can't be found.
365 * @param symtab Symbol table to use for look up.
366 * @param lbl Function to hook the event to.
367 * @param desc Description to be passed to the event.
368 * @param args Arguments to be forwarded to the event constructor.
370 template <class T, typename... Args>
371 T *addFuncEvent(const SymbolTable *symtab, const char *lbl,
372 const std::string &desc, Args... args)
374 Addr addr = 0; // initialize only to avoid compiler warning
376 if (symtab->findAddress(lbl, addr)) {
377 T *ev = new T(&pcEventQueue, desc, fixFuncEventAddr(addr),
378 std::forward<Args>(args)...);
386 T *addFuncEvent(const SymbolTable *symtab, const char *lbl)
388 return addFuncEvent<T>(symtab, lbl, lbl);
391 template <class T, typename... Args>
392 T *addFuncEventOrPanic(const SymbolTable *symtab, const char *lbl,
395 T *e(addFuncEvent<T>(symtab, lbl, std::forward<Args>(args)...));
397 panic("Failed to find symbol '%s'", lbl);
404 * Add a function-based event to a kernel symbol.
406 * These functions work like their addFuncEvent() and
407 * addFuncEventOrPanic() counterparts. The only difference is that
408 * they automatically use the kernel symbol table. All arguments
409 * are forwarded to the underlying method.
411 * @see addFuncEvent()
412 * @see addFuncEventOrPanic()
414 * @param lbl Function to hook the event to.
415 * @param args Arguments to be passed to addFuncEvent
417 template <class T, typename... Args>
418 T *addKernelFuncEvent(const char *lbl, Args... args)
420 return addFuncEvent<T>(kernelSymtab, lbl,
421 std::forward<Args>(args)...);
424 template <class T, typename... Args>
425 T *addKernelFuncEventOrPanic(const char *lbl, Args... args)
427 T *e(addFuncEvent<T>(kernelSymtab, lbl,
428 std::forward<Args>(args)...));
430 panic("Failed to find kernel symbol '%s'", lbl);
436 std::vector<BaseRemoteGDB *> remoteGDB;
437 std::vector<GDBListener *> gdbListen;
441 typedef SystemParams Params;
452 const Params *params() const { return (const Params *)_params; }
457 * Returns the addess the kernel starts at.
458 * @return address the kernel starts at
460 Addr getKernelStart() const { return kernelStart; }
463 * Returns the addess the kernel ends at.
464 * @return address the kernel ends at
466 Addr getKernelEnd() const { return kernelEnd; }
469 * Returns the addess the entry point to the kernel code.
470 * @return entry point of the kernel code
472 Addr getKernelEntry() const { return kernelEntry; }
474 /// Allocate npages contiguous unused physical pages
475 /// @return Starting address of first page
476 Addr allocPhysPages(int npages);
478 int registerThreadContext(ThreadContext *tc, int assigned=-1);
479 void replaceThreadContext(ThreadContext *tc, int context_id);
481 void serialize(std::ostream &os);
482 void unserialize(Checkpoint *cp, const std::string §ion);
484 unsigned int drain(DrainManager *dm);
488 Counter totalNumInsts;
489 EventQueue instEventQueue;
490 std::map<std::pair<uint32_t,uint32_t>, Tick> lastWorkItemStarted;
491 std::map<uint32_t, Stats::Histogram*> workItemStats;
493 ////////////////////////////////////////////
495 // STATIC GLOBAL SYSTEM LIST
497 ////////////////////////////////////////////
499 static std::vector<System *> systemList;
500 static int numSystemsRunning;
502 static void printSystems();
504 // For futex system call
505 std::map<uint64_t, std::list<ThreadContext *> * > futexMap;
510 * If needed, serialize additional symbol table entries for a
511 * specific subclass of this sytem. Currently this is used by
514 * @param os stream to serialize to
516 virtual void serializeSymtab(std::ostream &os) {}
519 * If needed, unserialize additional symbol table entries for a
520 * specific subclass of this system.
522 * @param cp checkpoint to unserialize from
523 * @param section relevant section in the checkpoint
525 virtual void unserializeSymtab(Checkpoint *cp,
526 const std::string §ion) {}
532 #endif // __SYSTEM_HH__