init.cc revision 11988:665cd5f8b52b
1/*
2 * Copyright (c) 2012, 2017 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) 2000-2005 The Regents of The University of Michigan
15 * Copyright (c) 2008 The Hewlett-Packard Development Company
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: Nathan Binkert
42 */
43
44#include <Python.h>
45
46#include "sim/init.hh"
47
48#include <marshal.h>
49#include <zlib.h>
50
51#include <iostream>
52#include <list>
53#include <string>
54
55#include "base/cprintf.hh"
56#include "base/misc.hh"
57#include "base/types.hh"
58#include "config/have_protobuf.hh"
59#include "python/pybind11/pybind.hh"
60#include "sim/async.hh"
61#include "sim/core.hh"
62
63#if HAVE_PROTOBUF
64#include <google/protobuf/stubs/common.h>
65
66#endif
67
68using namespace std;
69namespace py = pybind11;
70
71// The python library is totally messed up with respect to constness,
72// so make a simple macro to make life a little easier
73#define PyCC(x) (const_cast<char *>(x))
74
75EmbeddedPython *EmbeddedPython::importer = NULL;
76PyObject *EmbeddedPython::importerModule = NULL;
77EmbeddedPython::EmbeddedPython(const char *filename, const char *abspath,
78    const char *modpath, const unsigned char *code, int zlen, int len)
79    : filename(filename), abspath(abspath), modpath(modpath), code(code),
80      zlen(zlen), len(len)
81{
82    // if we've added the importer keep track of it because we need it
83    // to bootstrap.
84    if (string(modpath) == string("importer"))
85        importer = this;
86    else
87        getList().push_back(this);
88}
89
90list<EmbeddedPython *> &
91EmbeddedPython::getList()
92{
93    static list<EmbeddedPython *> the_list;
94    return the_list;
95}
96
97/*
98 * Uncompress and unmarshal the code object stored in the
99 * EmbeddedPython
100 */
101PyObject *
102EmbeddedPython::getCode() const
103{
104    Bytef marshalled[len];
105    uLongf unzlen = len;
106    int ret = uncompress(marshalled, &unzlen, (const Bytef *)code, zlen);
107    if (ret != Z_OK)
108        panic("Could not uncompress code: %s\n", zError(ret));
109    assert(unzlen == (uLongf)len);
110
111    return PyMarshal_ReadObjectFromString((char *)marshalled, len);
112}
113
114bool
115EmbeddedPython::addModule() const
116{
117    PyObject *code = getCode();
118    PyObject *result = PyObject_CallMethod(importerModule, PyCC("add_module"),
119        PyCC("sssO"), filename, abspath, modpath, code);
120    if (!result) {
121        PyErr_Print();
122        return false;
123    }
124
125    Py_DECREF(result);
126    return true;
127}
128
129/*
130 * Load and initialize all of the python parts of M5, including Swig
131 * and the embedded module importer.
132 */
133int
134EmbeddedPython::initAll()
135{
136    // Load the importer module
137    PyObject *code = importer->getCode();
138    importerModule = PyImport_ExecCodeModule(PyCC("importer"), code);
139    if (!importerModule) {
140        PyErr_Print();
141        return 1;
142    }
143
144    // Load the rest of the embedded python files into the embedded
145    // python importer
146    list<EmbeddedPython *>::iterator i = getList().begin();
147    list<EmbeddedPython *>::iterator end = getList().end();
148    for (; i != end; ++i)
149        if (!(*i)->addModule())
150            return 1;
151
152    return 0;
153}
154
155EmbeddedSwig::EmbeddedSwig(void (*init_func)(), const string& _context)
156    : initFunc(init_func), context(_context)
157{
158    getList().push_back(this);
159}
160
161list<EmbeddedSwig *> &
162EmbeddedSwig::getList()
163{
164    static list<EmbeddedSwig *> the_list;
165    return the_list;
166}
167
168void
169EmbeddedSwig::initAll()
170{
171    char* old_context = _Py_PackageContext;
172    // initialize SWIG modules. initFunc() is autogenerated and calls
173    // all of the individual swig initialization functions.
174    for (auto i : getList()) {
175        // to ensure that the loaded modules are placed in the right
176        // package we have to be a bit unorthodox and directly
177        // manipulate the package context since swig simply calls
178        // Py_InitModule with nothing but the module name of the
179        // wrapper
180        char* cstr = new char[i->context.size() + 1];
181        strcpy(cstr, i->context.c_str());
182        _Py_PackageContext = cstr;
183        i->initFunc();
184        delete[] cstr;
185    }
186    _Py_PackageContext = old_context;
187}
188
189EmbeddedPyBind::EmbeddedPyBind(const char *_name,
190                               void (*init_func)(py::module &),
191                               const char *_base)
192    : initFunc(init_func), registered(false), name(_name), base(_base)
193{
194    getMap()[_name] = this;
195}
196
197EmbeddedPyBind::EmbeddedPyBind(const char *_name,
198                               void (*init_func)(py::module &))
199    : initFunc(init_func), registered(false), name(_name), base("")
200{
201    getMap()[_name] = this;
202}
203
204void
205EmbeddedPyBind::init(py::module &m)
206{
207    if (!registered) {
208        initFunc(m);
209        registered = true;
210    } else {
211        cprintf("Warning: %s already registered.\n", name);
212    }
213}
214
215bool
216EmbeddedPyBind::depsReady() const
217{
218    return base.empty() || getMap()[base]->registered;
219}
220
221std::map<std::string, EmbeddedPyBind *> &
222EmbeddedPyBind::getMap()
223{
224    static std::map<std::string, EmbeddedPyBind *> objs;
225    return objs;
226}
227
228void
229EmbeddedPyBind::initAll()
230{
231    std::list<EmbeddedPyBind *> pending;
232
233    py::module m_m5 = py::module("_m5");
234    m_m5.attr("__package__") = py::cast("_m5");
235
236    pybind_init_core(m_m5);
237    pybind_init_debug(m_m5);
238
239    pybind_init_event(m_m5);
240    pybind_init_pyobject(m_m5);
241    pybind_init_stats(m_m5);
242
243    for (auto &kv : getMap()) {
244        auto &obj = kv.second;
245        if (obj->base.empty()) {
246            obj->init(m_m5);
247        } else {
248            pending.push_back(obj);
249        }
250    }
251
252    while (!pending.empty()) {
253        for (auto it = pending.begin(); it != pending.end(); ) {
254            EmbeddedPyBind &obj = **it;
255            if (obj.depsReady()) {
256                obj.init(m_m5);
257                it = pending.erase(it);
258            } else {
259                ++it;
260            }
261        }
262    }
263}
264
265int
266initM5Python()
267{
268    EmbeddedSwig::initAll();
269    EmbeddedPyBind::initAll();
270    return EmbeddedPython::initAll();
271}
272
273/*
274 * Make the commands array weak so that they can be overridden (used
275 * by unit tests to specify a different python main function.
276 */
277const char * __attribute__((weak)) m5MainCommands[] = {
278    "import m5",
279    "m5.main()",
280    0 // sentinel is required
281};
282
283/*
284 * Start up the M5 simulator.  This mostly vectors into the python
285 * main function.
286 */
287int
288m5Main(int argc, char **argv)
289{
290#if HAVE_PROTOBUF
291    // Verify that the version of the protobuf library that we linked
292    // against is compatible with the version of the headers we
293    // compiled against.
294    GOOGLE_PROTOBUF_VERIFY_VERSION;
295#endif
296
297    PySys_SetArgv(argc, argv);
298
299    // We have to set things up in the special __main__ module
300    PyObject *module = PyImport_AddModule(PyCC("__main__"));
301    if (module == NULL)
302        panic("Could not import __main__");
303    PyObject *dict = PyModule_GetDict(module);
304
305    // import the main m5 module
306    PyObject *result;
307    const char **command = m5MainCommands;
308
309    // evaluate each command in the m5MainCommands array (basically a
310    // bunch of python statements.
311    while (*command) {
312        result = PyRun_String(*command, Py_file_input, dict, dict);
313        if (!result) {
314            PyErr_Print();
315            return 1;
316        }
317        Py_DECREF(result);
318
319        command++;
320    }
321
322#if HAVE_PROTOBUF
323    google::protobuf::ShutdownProtobufLibrary();
324#endif
325
326    return 0;
327}
328
329PyMODINIT_FUNC
330initm5(void)
331{
332    initM5Python();
333    PyImport_ImportModule(PyCC("m5"));
334}
335