arch, cpu, dev, gpu, mem, sim, python: start using getPort.
[gem5.git] / src / sim / init.cc
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 #include <vector>
55
56 #include "base/cprintf.hh"
57 #include "base/logging.hh"
58 #include "base/types.hh"
59 #include "config/have_protobuf.hh"
60 #include "python/pybind11/pybind.hh"
61 #include "sim/async.hh"
62 #include "sim/core.hh"
63
64 #if HAVE_PROTOBUF
65 #include <google/protobuf/stubs/common.h>
66
67 #endif
68
69 using namespace std;
70 namespace py = pybind11;
71
72 // The python library is totally messed up with respect to constness,
73 // so make a simple macro to make life a little easier
74 #define PyCC(x) (const_cast<char *>(x))
75
76 EmbeddedPython *EmbeddedPython::importer = NULL;
77 PyObject *EmbeddedPython::importerModule = NULL;
78 EmbeddedPython::EmbeddedPython(const char *filename, const char *abspath,
79 const char *modpath, const unsigned char *code, int zlen, int len)
80 : filename(filename), abspath(abspath), modpath(modpath), code(code),
81 zlen(zlen), len(len)
82 {
83 // if we've added the importer keep track of it because we need it
84 // to bootstrap.
85 if (string(modpath) == string("importer"))
86 importer = this;
87 else
88 getList().push_back(this);
89 }
90
91 list<EmbeddedPython *> &
92 EmbeddedPython::getList()
93 {
94 static list<EmbeddedPython *> the_list;
95 return the_list;
96 }
97
98 /*
99 * Uncompress and unmarshal the code object stored in the
100 * EmbeddedPython
101 */
102 PyObject *
103 EmbeddedPython::getCode() const
104 {
105 Bytef marshalled[len];
106 uLongf unzlen = len;
107 int ret = uncompress(marshalled, &unzlen, (const Bytef *)code, zlen);
108 if (ret != Z_OK)
109 panic("Could not uncompress code: %s\n", zError(ret));
110 assert(unzlen == (uLongf)len);
111
112 return PyMarshal_ReadObjectFromString((char *)marshalled, len);
113 }
114
115 bool
116 EmbeddedPython::addModule() const
117 {
118 PyObject *code = getCode();
119 PyObject *result = PyObject_CallMethod(importerModule, PyCC("add_module"),
120 PyCC("sssO"), filename, abspath, modpath, code);
121 if (!result) {
122 PyErr_Print();
123 return false;
124 }
125
126 Py_DECREF(result);
127 return true;
128 }
129
130 /*
131 * Load and initialize all of the python parts of M5.
132 */
133 int
134 EmbeddedPython::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
155 EmbeddedPyBind::EmbeddedPyBind(const char *_name,
156 void (*init_func)(py::module &),
157 const char *_base)
158 : initFunc(init_func), registered(false), name(_name), base(_base)
159 {
160 getMap()[_name] = this;
161 }
162
163 EmbeddedPyBind::EmbeddedPyBind(const char *_name,
164 void (*init_func)(py::module &))
165 : initFunc(init_func), registered(false), name(_name), base("")
166 {
167 getMap()[_name] = this;
168 }
169
170 void
171 EmbeddedPyBind::init(py::module &m)
172 {
173 if (!registered) {
174 initFunc(m);
175 registered = true;
176 } else {
177 cprintf("Warning: %s already registered.\n", name);
178 }
179 }
180
181 bool
182 EmbeddedPyBind::depsReady() const
183 {
184 return base.empty() || getMap()[base]->registered;
185 }
186
187 std::map<std::string, EmbeddedPyBind *> &
188 EmbeddedPyBind::getMap()
189 {
190 static std::map<std::string, EmbeddedPyBind *> objs;
191 return objs;
192 }
193
194 #if PY_MAJOR_VERSION >= 3
195 PyObject *
196 #else
197 void
198 #endif
199 EmbeddedPyBind::initAll()
200 {
201 std::list<EmbeddedPyBind *> pending;
202
203 py::module m_m5 = py::module("_m5");
204 m_m5.attr("__package__") = py::cast("_m5");
205
206 pybind_init_core(m_m5);
207 pybind_init_debug(m_m5);
208
209 pybind_init_event(m_m5);
210 pybind_init_stats(m_m5);
211
212 for (auto &kv : getMap()) {
213 auto &obj = kv.second;
214 if (obj->base.empty()) {
215 obj->init(m_m5);
216 } else {
217 pending.push_back(obj);
218 }
219 }
220
221 while (!pending.empty()) {
222 for (auto it = pending.begin(); it != pending.end(); ) {
223 EmbeddedPyBind &obj = **it;
224 if (obj.depsReady()) {
225 obj.init(m_m5);
226 it = pending.erase(it);
227 } else {
228 ++it;
229 }
230 }
231 }
232
233 #if PY_MAJOR_VERSION >= 3
234 return m_m5.ptr();
235 #endif
236 }
237
238 void
239 registerNativeModules()
240 {
241 auto result = PyImport_AppendInittab("_m5", EmbeddedPyBind::initAll);
242 if (result == -1)
243 panic("Failed to add _m5 to Python's inittab\n");
244 }
245
246 /*
247 * Make the commands array weak so that they can be overridden (used
248 * by unit tests to specify a different python main function.
249 */
250 const char * __attribute__((weak)) m5MainCommands[] = {
251 "import m5",
252 "m5.main()",
253 0 // sentinel is required
254 };
255
256 /*
257 * Start up the M5 simulator. This mostly vectors into the python
258 * main function.
259 */
260 int
261 m5Main(int argc, char **_argv)
262 {
263 #if HAVE_PROTOBUF
264 // Verify that the version of the protobuf library that we linked
265 // against is compatible with the version of the headers we
266 // compiled against.
267 GOOGLE_PROTOBUF_VERIFY_VERSION;
268 #endif
269
270
271 #if PY_MAJOR_VERSION >= 3
272 typedef std::unique_ptr<wchar_t[], decltype(&PyMem_RawFree)> WArgUPtr;
273 std::vector<WArgUPtr> v_argv;
274 std::vector<wchar_t *> vp_argv;
275 v_argv.reserve(argc);
276 vp_argv.reserve(argc);
277 for (int i = 0; i < argc; i++) {
278 v_argv.emplace_back(Py_DecodeLocale(_argv[i], NULL), &PyMem_RawFree);
279 vp_argv.emplace_back(v_argv.back().get());
280 }
281
282 wchar_t **argv = vp_argv.data();
283 #else
284 char **argv = _argv;
285 #endif
286
287 PySys_SetArgv(argc, argv);
288
289 // We have to set things up in the special __main__ module
290 PyObject *module = PyImport_AddModule(PyCC("__main__"));
291 if (module == NULL)
292 panic("Could not import __main__");
293 PyObject *dict = PyModule_GetDict(module);
294
295 // import the main m5 module
296 PyObject *result;
297 const char **command = m5MainCommands;
298
299 // evaluate each command in the m5MainCommands array (basically a
300 // bunch of python statements.
301 while (*command) {
302 result = PyRun_String(*command, Py_file_input, dict, dict);
303 if (!result) {
304 PyErr_Print();
305 return 1;
306 }
307 Py_DECREF(result);
308
309 command++;
310 }
311
312 #if HAVE_PROTOBUF
313 google::protobuf::ShutdownProtobufLibrary();
314 #endif
315
316 return 0;
317 }