init.cc revision 12334
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/logging.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.
131 */
132int
133EmbeddedPython::initAll()
134{
135    // Load the importer module
136    PyObject *code = importer->getCode();
137    importerModule = PyImport_ExecCodeModule(PyCC("importer"), code);
138    if (!importerModule) {
139        PyErr_Print();
140        return 1;
141    }
142
143    // Load the rest of the embedded python files into the embedded
144    // python importer
145    list<EmbeddedPython *>::iterator i = getList().begin();
146    list<EmbeddedPython *>::iterator end = getList().end();
147    for (; i != end; ++i)
148        if (!(*i)->addModule())
149            return 1;
150
151    return 0;
152}
153
154EmbeddedPyBind::EmbeddedPyBind(const char *_name,
155                               void (*init_func)(py::module &),
156                               const char *_base)
157    : initFunc(init_func), registered(false), name(_name), base(_base)
158{
159    getMap()[_name] = this;
160}
161
162EmbeddedPyBind::EmbeddedPyBind(const char *_name,
163                               void (*init_func)(py::module &))
164    : initFunc(init_func), registered(false), name(_name), base("")
165{
166    getMap()[_name] = this;
167}
168
169void
170EmbeddedPyBind::init(py::module &m)
171{
172    if (!registered) {
173        initFunc(m);
174        registered = true;
175    } else {
176        cprintf("Warning: %s already registered.\n", name);
177    }
178}
179
180bool
181EmbeddedPyBind::depsReady() const
182{
183    return base.empty() || getMap()[base]->registered;
184}
185
186std::map<std::string, EmbeddedPyBind *> &
187EmbeddedPyBind::getMap()
188{
189    static std::map<std::string, EmbeddedPyBind *> objs;
190    return objs;
191}
192
193void
194EmbeddedPyBind::initAll()
195{
196    std::list<EmbeddedPyBind *> pending;
197
198    py::module m_m5 = py::module("_m5");
199    m_m5.attr("__package__") = py::cast("_m5");
200
201    pybind_init_core(m_m5);
202    pybind_init_debug(m_m5);
203
204    pybind_init_event(m_m5);
205    pybind_init_pyobject(m_m5);
206    pybind_init_stats(m_m5);
207
208    for (auto &kv : getMap()) {
209        auto &obj = kv.second;
210        if (obj->base.empty()) {
211            obj->init(m_m5);
212        } else {
213            pending.push_back(obj);
214        }
215    }
216
217    while (!pending.empty()) {
218        for (auto it = pending.begin(); it != pending.end(); ) {
219            EmbeddedPyBind &obj = **it;
220            if (obj.depsReady()) {
221                obj.init(m_m5);
222                it = pending.erase(it);
223            } else {
224                ++it;
225            }
226        }
227    }
228}
229
230int
231initM5Python()
232{
233    EmbeddedPyBind::initAll();
234    return EmbeddedPython::initAll();
235}
236
237/*
238 * Make the commands array weak so that they can be overridden (used
239 * by unit tests to specify a different python main function.
240 */
241const char * __attribute__((weak)) m5MainCommands[] = {
242    "import m5",
243    "m5.main()",
244    0 // sentinel is required
245};
246
247/*
248 * Start up the M5 simulator.  This mostly vectors into the python
249 * main function.
250 */
251int
252m5Main(int argc, char **argv)
253{
254#if HAVE_PROTOBUF
255    // Verify that the version of the protobuf library that we linked
256    // against is compatible with the version of the headers we
257    // compiled against.
258    GOOGLE_PROTOBUF_VERIFY_VERSION;
259#endif
260
261    PySys_SetArgv(argc, argv);
262
263    // We have to set things up in the special __main__ module
264    PyObject *module = PyImport_AddModule(PyCC("__main__"));
265    if (module == NULL)
266        panic("Could not import __main__");
267    PyObject *dict = PyModule_GetDict(module);
268
269    // import the main m5 module
270    PyObject *result;
271    const char **command = m5MainCommands;
272
273    // evaluate each command in the m5MainCommands array (basically a
274    // bunch of python statements.
275    while (*command) {
276        result = PyRun_String(*command, Py_file_input, dict, dict);
277        if (!result) {
278            PyErr_Print();
279            return 1;
280        }
281        Py_DECREF(result);
282
283        command++;
284    }
285
286#if HAVE_PROTOBUF
287    google::protobuf::ShutdownProtobufLibrary();
288#endif
289
290    return 0;
291}
292
293PyMODINIT_FUNC
294initm5(void)
295{
296    initM5Python();
297    PyImport_ImportModule(PyCC("m5"));
298}
299