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

Generated on Wed Sep 30 2020 14:02:14 for gem5 by doxygen 1.8.17