sim_object.hh revision 11800
110779SCurtis.Dunham@arm.com/*
210779SCurtis.Dunham@arm.com * Copyright (c) 2015 ARM Limited
310779SCurtis.Dunham@arm.com * All rights reserved
410779SCurtis.Dunham@arm.com *
510779SCurtis.Dunham@arm.com * The license below extends only to copyright in the software and shall
610779SCurtis.Dunham@arm.com * not be construed as granting a license to any other intellectual
710779SCurtis.Dunham@arm.com * property including but not limited to intellectual property relating
810779SCurtis.Dunham@arm.com * to a hardware implementation of the functionality of the software
910779SCurtis.Dunham@arm.com * licensed hereunder.  You may use the software subject to the license
1010779SCurtis.Dunham@arm.com * terms below provided that you ensure that this notice is replicated
1110779SCurtis.Dunham@arm.com * unmodified and in its entirety in all distributions of the software,
1210779SCurtis.Dunham@arm.com * modified or unmodified, in source code or in binary form.
1310779SCurtis.Dunham@arm.com *
1410779SCurtis.Dunham@arm.com * Copyright (c) 2001-2005 The Regents of The University of Michigan
1510779SCurtis.Dunham@arm.com * Copyright (c) 2010 Advanced Micro Devices, Inc.
1610779SCurtis.Dunham@arm.com * All rights reserved.
1710779SCurtis.Dunham@arm.com *
1810779SCurtis.Dunham@arm.com * Redistribution and use in source and binary forms, with or without
1910779SCurtis.Dunham@arm.com * modification, are permitted provided that the following conditions are
2010779SCurtis.Dunham@arm.com * met: redistributions of source code must retain the above copyright
2110779SCurtis.Dunham@arm.com * notice, this list of conditions and the following disclaimer;
2210779SCurtis.Dunham@arm.com * redistributions in binary form must reproduce the above copyright
2310779SCurtis.Dunham@arm.com * notice, this list of conditions and the following disclaimer in the
2410779SCurtis.Dunham@arm.com * documentation and/or other materials provided with the distribution;
2510779SCurtis.Dunham@arm.com * neither the name of the copyright holders nor the names of its
2610779SCurtis.Dunham@arm.com * contributors may be used to endorse or promote products derived from
2710779SCurtis.Dunham@arm.com * this software without specific prior written permission.
2810779SCurtis.Dunham@arm.com *
2910779SCurtis.Dunham@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3010779SCurtis.Dunham@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3110779SCurtis.Dunham@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3210779SCurtis.Dunham@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3310779SCurtis.Dunham@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3410779SCurtis.Dunham@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3510779SCurtis.Dunham@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3610779SCurtis.Dunham@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3710779SCurtis.Dunham@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3810779SCurtis.Dunham@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3910779SCurtis.Dunham@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4010779SCurtis.Dunham@arm.com *
4110779SCurtis.Dunham@arm.com * Authors: Steve Reinhardt
4210779SCurtis.Dunham@arm.com *          Nathan Binkert
4310779SCurtis.Dunham@arm.com */
4410779SCurtis.Dunham@arm.com
4510779SCurtis.Dunham@arm.com/* @file
4610779SCurtis.Dunham@arm.com * User Console Definitions
4710779SCurtis.Dunham@arm.com */
4810779SCurtis.Dunham@arm.com
4910779SCurtis.Dunham@arm.com#ifndef __SIM_OBJECT_HH__
5010779SCurtis.Dunham@arm.com#define __SIM_OBJECT_HH__
5110779SCurtis.Dunham@arm.com
5210779SCurtis.Dunham@arm.com#include <string>
5310779SCurtis.Dunham@arm.com#include <vector>
5410779SCurtis.Dunham@arm.com
5510779SCurtis.Dunham@arm.com#include "params/SimObject.hh"
5610779SCurtis.Dunham@arm.com#include "sim/drain.hh"
5710779SCurtis.Dunham@arm.com#include "sim/eventq.hh"
5810779SCurtis.Dunham@arm.com#include "sim/eventq_impl.hh"
5910779SCurtis.Dunham@arm.com#include "sim/serialize.hh"
6010779SCurtis.Dunham@arm.com
6110779SCurtis.Dunham@arm.comclass EventManager;
6210779SCurtis.Dunham@arm.comclass ProbeManager;
6310779SCurtis.Dunham@arm.com
6410779SCurtis.Dunham@arm.com/**
6510779SCurtis.Dunham@arm.com * Abstract superclass for simulation objects.  Represents things that
6610779SCurtis.Dunham@arm.com * correspond to physical components and can be specified via the
6710779SCurtis.Dunham@arm.com * config file (CPUs, caches, etc.).
6810779SCurtis.Dunham@arm.com *
6910779SCurtis.Dunham@arm.com * SimObject initialization is controlled by the instantiate method in
7010779SCurtis.Dunham@arm.com * src/python/m5/simulate.py. There are slightly different
7110779SCurtis.Dunham@arm.com * initialization paths when starting the simulation afresh and when
7210779SCurtis.Dunham@arm.com * loading from a checkpoint.  After instantiation and connecting
7310779SCurtis.Dunham@arm.com * ports, simulate.py initializes the object using the following call
7410779SCurtis.Dunham@arm.com * sequence:
7510779SCurtis.Dunham@arm.com *
7610779SCurtis.Dunham@arm.com * <ol>
7710779SCurtis.Dunham@arm.com * <li>SimObject::init()
7810779SCurtis.Dunham@arm.com * <li>SimObject::regStats()
7910779SCurtis.Dunham@arm.com * <li><ul>
8010779SCurtis.Dunham@arm.com *     <li>SimObject::initState() if starting afresh.
8110779SCurtis.Dunham@arm.com *     <li>SimObject::loadState() if restoring from a checkpoint.
8210779SCurtis.Dunham@arm.com *     </ul>
8310779SCurtis.Dunham@arm.com * <li>SimObject::resetStats()
8410779SCurtis.Dunham@arm.com * <li>SimObject::startup()
85 * <li>Drainable::drainResume() if resuming from a checkpoint.
86 * </ol>
87 *
88 * @note Whenever a method is called on all objects in the simulator's
89 * object tree (e.g., init(), startup(), or loadState()), a pre-order
90 * depth-first traversal is performed (see descendants() in
91 * SimObject.py). This has the effect of calling the method on the
92 * parent node <i>before</i> its children.
93 */
94class SimObject : public EventManager, public Serializable, public Drainable
95{
96  private:
97    typedef std::vector<SimObject *> SimObjectList;
98
99    /** List of all instantiated simulation objects. */
100    static SimObjectList simObjectList;
101
102    /** Manager coordinates hooking up probe points with listeners. */
103    ProbeManager *probeManager;
104
105  protected:
106    /** Cached copy of the object parameters. */
107    const SimObjectParams *_params;
108
109  public:
110    typedef SimObjectParams Params;
111    const Params *params() const { return _params; }
112    SimObject(const Params *_params);
113    virtual ~SimObject();
114
115  public:
116
117    virtual const std::string name() const { return params()->name; }
118
119    /**
120     * init() is called after all C++ SimObjects have been created and
121     * all ports are connected.  Initializations that are independent
122     * of unserialization but rely on a fully instantiated and
123     * connected SimObject graph should be done here.
124     */
125    virtual void init();
126
127    /**
128     * loadState() is called on each SimObject when restoring from a
129     * checkpoint.  The default implementation simply calls
130     * unserialize() if there is a corresponding section in the
131     * checkpoint.  However, objects can override loadState() to get
132     * other behaviors, e.g., doing other programmed initializations
133     * after unserialize(), or complaining if no checkpoint section is
134     * found.
135     *
136     * @param cp Checkpoint to restore the state from.
137     */
138    virtual void loadState(CheckpointIn &cp);
139
140    /**
141     * initState() is called on each SimObject when *not* restoring
142     * from a checkpoint.  This provides a hook for state
143     * initializations that are only required for a "cold start".
144     */
145    virtual void initState();
146
147    /**
148     * Register statistics for this object.
149     */
150    virtual void regStats();
151
152    /**
153     * Reset statistics associated with this object.
154     */
155    virtual void resetStats();
156
157    /**
158     * Register probe points for this object.
159     */
160    virtual void regProbePoints();
161
162    /**
163     * Register probe listeners for this object.
164     */
165    virtual void regProbeListeners();
166
167    /**
168     * Get the probe manager for this object.
169     */
170    ProbeManager *getProbeManager();
171
172    /**
173     * startup() is the final initialization call before simulation.
174     * All state is initialized (including unserialized state, if any,
175     * such as the curTick() value), so this is the appropriate place to
176     * schedule initial event(s) for objects that need them.
177     */
178    virtual void startup();
179
180    /**
181     * Provide a default implementation of the drain interface for
182     * objects that don't need draining.
183     */
184    DrainState drain() override { return DrainState::Drained; }
185
186    /**
187     * Write back dirty buffers to memory using functional writes.
188     *
189     * After returning, an object implementing this method should have
190     * written all its dirty data back to memory. This method is
191     * typically used to prepare a system with caches for
192     * checkpointing.
193     */
194    virtual void memWriteback() {};
195
196    /**
197     * Invalidate the contents of memory buffers.
198     *
199     * When the switching to hardware virtualized CPU models, we need
200     * to make sure that we don't have any cached state in the system
201     * that might become stale when we return. This method is used to
202     * flush all such state back to main memory.
203     *
204     * @warn This does <i>not</i> cause any dirty state to be written
205     * back to memory.
206     */
207    virtual void memInvalidate() {};
208
209    void serialize(CheckpointOut &cp) const override {};
210    void unserialize(CheckpointIn &cp) override {};
211
212    /**
213     * Serialize all SimObjects in the system.
214     */
215    static void serializeAll(CheckpointOut &cp);
216
217#ifdef DEBUG
218  public:
219    bool doDebugBreak;
220    static void debugObjectBreak(const std::string &objs);
221#endif
222
223    /**
224     * Find the SimObject with the given name and return a pointer to
225     * it.  Primarily used for interactive debugging.  Argument is
226     * char* rather than std::string to make it callable from gdb.
227     */
228    static SimObject *find(const char *name);
229};
230
231/**
232 * Base class to wrap object resolving functionality.
233 *
234 * This can be provided to the serialization framework to allow it to
235 * map object names onto C++ objects.
236 */
237class SimObjectResolver
238{
239  public:
240    virtual ~SimObjectResolver() { }
241
242    // Find a SimObject given a full path name
243    virtual SimObject *resolveSimObject(const std::string &name) = 0;
244};
245
246#ifdef DEBUG
247void debugObjectBreak(const char *objs);
248#endif
249
250#endif // __SIM_OBJECT_HH__
251