system.hh revision 8706:b1838faf3bcc
1/*
2 * Copyright (c) 2012 ARM Limited
3 * All rights reserved
4 *
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.
13 *
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.
17 *
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.
28 *
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.
40 *
41 * Authors: Steve Reinhardt
42 *          Lisa Hsu
43 *          Nathan Binkert
44 *          Rick Strong
45 */
46
47#ifndef __SYSTEM_HH__
48#define __SYSTEM_HH__
49
50#include <string>
51#include <vector>
52
53#include "base/loader/symtab.hh"
54#include "base/misc.hh"
55#include "base/statistics.hh"
56#include "config/full_system.hh"
57#include "cpu/pc_event.hh"
58#include "enums/MemoryMode.hh"
59#include "mem/mem_object.hh"
60#include "mem/port.hh"
61#include "params/System.hh"
62
63#if FULL_SYSTEM
64#include "kern/system_events.hh"
65#endif
66
67class BaseCPU;
68class ThreadContext;
69class ObjectFile;
70class PhysicalMemory;
71
72#if FULL_SYSTEM
73class Platform;
74class PortProxy;
75class FSTranslatingPortProxy;
76#endif
77class GDBListener;
78class BaseRemoteGDB;
79
80class System : public MemObject
81{
82  private:
83
84    /**
85     * Private class for the system port which is only used as a
86     * master for debug access and for non-structural entities that do
87     * not have a port of their own.
88     */
89    class SystemPort : public Port
90    {
91      public:
92
93        /**
94         * Create a system port with a name and an owner.
95         */
96        SystemPort(const std::string &_name, MemObject *_owner)
97            : Port(_name, _owner)
98        { }
99        bool recvTiming(PacketPtr pkt)
100        { panic("SystemPort does not receive timing!\n"); return false; }
101        Tick recvAtomic(PacketPtr pkt)
102        { panic("SystemPort does not receive atomic!\n"); return 0; }
103        void recvFunctional(PacketPtr pkt)
104        { panic("SystemPort does not receive functional!\n"); }
105        void recvStatusChange(Status status) { }
106
107    };
108
109    SystemPort _systemPort;
110
111  public:
112
113    /**
114     * After all objects have been created and all ports are
115     * connected, check that the system port is connected.
116     */
117    virtual void init();
118
119    /**
120     * Get a pointer to the system port that can be used by
121     * non-structural simulation objects like processes or threads, or
122     * external entities like loaders and debuggers, etc, to access
123     * the memory system.
124     *
125     * @return a pointer to the system port we own
126     */
127    Port* getSystemPort() { return &_systemPort; }
128
129    /**
130     * Additional function to return the Port of a memory object.
131     */
132    Port *getPort(const std::string &if_name, int idx = -1);
133
134    static const char *MemoryModeStrings[3];
135
136    Enums::MemoryMode
137    getMemoryMode()
138    {
139        assert(memoryMode);
140        return memoryMode;
141    }
142
143    /** Change the memory mode of the system. This should only be called by the
144     * python!!
145     * @param mode Mode to change to (atomic/timing)
146     */
147    void setMemoryMode(Enums::MemoryMode mode);
148
149    PhysicalMemory *physmem;
150    PCEventQueue pcEventQueue;
151
152    std::vector<ThreadContext *> threadContexts;
153    int _numContexts;
154
155    ThreadContext *getThreadContext(ThreadID tid)
156    {
157        return threadContexts[tid];
158    }
159
160    int numContexts()
161    {
162        assert(_numContexts == (int)threadContexts.size());
163        return _numContexts;
164    }
165
166    /** Return number of running (non-halted) thread contexts in
167     * system.  These threads could be Active or Suspended. */
168    int numRunningContexts();
169
170    /** List to store ranges of memories in this system */
171    AddrRangeList memRanges;
172
173    /** check if an address points to valid system memory
174     * and thus we can fetch instructions out of it
175     */
176    bool isMemory(const Addr addr) const;
177
178#if FULL_SYSTEM
179    Platform *platform;
180    uint64_t init_param;
181
182    /** Port to physical memory used for writing object files into ram at
183     * boot.*/
184    PortProxy* physProxy;
185    FSTranslatingPortProxy* virtProxy;
186
187    /** kernel symbol table */
188    SymbolTable *kernelSymtab;
189
190    /** Object pointer for the kernel code */
191    ObjectFile *kernel;
192
193    /** Begining of kernel code */
194    Addr kernelStart;
195
196    /** End of kernel code */
197    Addr kernelEnd;
198
199    /** Entry point in the kernel to start at */
200    Addr kernelEntry;
201
202    /** Mask that should be anded for binary/symbol loading.
203     * This allows one two different OS requirements for the same ISA to be
204     * handled.  Some OSes are compiled for a virtual address and need to be
205     * loaded into physical memory that starts at address 0, while other
206     * bare metal tools generate images that start at address 0.
207     */
208    Addr loadAddrMask;
209
210#else
211
212    Addr pagePtr;
213
214  protected:
215    uint64_t nextPID;
216
217  public:
218    uint64_t allocatePID()
219    {
220        return nextPID++;
221    }
222
223    /** Amount of physical memory that is still free */
224    Addr freeMemSize();
225
226    /** Amount of physical memory that exists */
227    Addr memSize();
228
229
230#endif // FULL_SYSTEM
231
232  protected:
233    Enums::MemoryMode memoryMode;
234    uint64_t workItemsBegin;
235    uint64_t workItemsEnd;
236    uint32_t numWorkIds;
237    std::vector<bool> activeCpus;
238
239  public:
240    virtual void regStats();
241    /**
242     * Called by pseudo_inst to track the number of work items started by this
243     * system.
244     */
245    uint64_t
246    incWorkItemsBegin()
247    {
248        return ++workItemsBegin;
249    }
250
251    /**
252     * Called by pseudo_inst to track the number of work items completed by
253     * this system.
254     */
255    uint64_t
256    incWorkItemsEnd()
257    {
258        return ++workItemsEnd;
259    }
260
261    /**
262     * Called by pseudo_inst to mark the cpus actively executing work items.
263     * Returns the total number of cpus that have executed work item begin or
264     * ends.
265     */
266    int
267    markWorkItem(int index)
268    {
269        int count = 0;
270        assert(index < activeCpus.size());
271        activeCpus[index] = true;
272        for (std::vector<bool>::iterator i = activeCpus.begin();
273             i < activeCpus.end(); i++) {
274            if (*i) count++;
275        }
276        return count;
277    }
278
279    inline void workItemBegin(uint32_t tid, uint32_t workid)
280    {
281        std::pair<uint32_t,uint32_t> p(tid, workid);
282        lastWorkItemStarted[p] = curTick();
283    }
284
285    void workItemEnd(uint32_t tid, uint32_t workid);
286
287#if FULL_SYSTEM
288    /**
289     * Fix up an address used to match PCs for hooking simulator
290     * events on to target function executions.  See comment in
291     * system.cc for details.
292     */
293    virtual Addr fixFuncEventAddr(Addr addr) = 0;
294
295    /**
296     * Add a function-based event to the given function, to be looked
297     * up in the specified symbol table.
298     */
299    template <class T>
300    T *addFuncEvent(SymbolTable *symtab, const char *lbl)
301    {
302        Addr addr = 0; // initialize only to avoid compiler warning
303
304        if (symtab->findAddress(lbl, addr)) {
305            T *ev = new T(&pcEventQueue, lbl, fixFuncEventAddr(addr));
306            return ev;
307        }
308
309        return NULL;
310    }
311
312    /** Add a function-based event to kernel code. */
313    template <class T>
314    T *addKernelFuncEvent(const char *lbl)
315    {
316        return addFuncEvent<T>(kernelSymtab, lbl);
317    }
318
319#endif
320  public:
321    std::vector<BaseRemoteGDB *> remoteGDB;
322    std::vector<GDBListener *> gdbListen;
323    bool breakpoint();
324
325  public:
326    typedef SystemParams Params;
327
328  protected:
329    Params *_params;
330
331  public:
332    System(Params *p);
333    ~System();
334
335    void initState();
336
337    const Params *params() const { return (const Params *)_params; }
338
339  public:
340
341#if FULL_SYSTEM
342    /**
343     * Returns the addess the kernel starts at.
344     * @return address the kernel starts at
345     */
346    Addr getKernelStart() const { return kernelStart; }
347
348    /**
349     * Returns the addess the kernel ends at.
350     * @return address the kernel ends at
351     */
352    Addr getKernelEnd() const { return kernelEnd; }
353
354    /**
355     * Returns the addess the entry point to the kernel code.
356     * @return entry point of the kernel code
357     */
358    Addr getKernelEntry() const { return kernelEntry; }
359
360#else
361
362    /// Allocate npages contiguous unused physical pages
363    /// @return Starting address of first page
364    Addr allocPhysPages(int npages);
365
366#endif // FULL_SYSTEM
367
368    int registerThreadContext(ThreadContext *tc, int assigned=-1);
369    void replaceThreadContext(ThreadContext *tc, int context_id);
370
371    void serialize(std::ostream &os);
372    void unserialize(Checkpoint *cp, const std::string &section);
373    virtual void resume();
374
375  public:
376    Counter totalNumInsts;
377    EventQueue instEventQueue;
378    std::map<std::pair<uint32_t,uint32_t>, Tick>  lastWorkItemStarted;
379    std::map<uint32_t, Stats::Histogram*> workItemStats;
380
381    ////////////////////////////////////////////
382    //
383    // STATIC GLOBAL SYSTEM LIST
384    //
385    ////////////////////////////////////////////
386
387    static std::vector<System *> systemList;
388    static int numSystemsRunning;
389
390    static void printSystems();
391
392
393};
394
395#endif // __SYSTEM_HH__
396