init.cc (11548:91f58918a76a) init.cc (11793:ef606668d247)
1/*
2 * Copyright (c) 2012 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
1/*
2 * Copyright (c) 2012 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 "sim/init.hh"
45
44#include <Python.h>
45
46#include <marshal.h>
47#include <zlib.h>
48
49#include <iostream>
50#include <list>
51#include <string>
52
53#include "base/cprintf.hh"
54#include "base/misc.hh"
55#include "base/types.hh"
56#include "config/have_protobuf.hh"
57#include "sim/async.hh"
58#include "sim/core.hh"
46#include <Python.h>
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 "sim/async.hh"
60#include "sim/core.hh"
59#include "sim/init.hh"
60
61#if HAVE_PROTOBUF
62#include <google/protobuf/stubs/common.h>
61
62#if HAVE_PROTOBUF
63#include <google/protobuf/stubs/common.h>
64
63#endif
64
65using namespace std;
66
67// The python library is totally messed up with respect to constness,
68// so make a simple macro to make life a little easier
69#define PyCC(x) (const_cast<char *>(x))
70
71EmbeddedPython *EmbeddedPython::importer = NULL;
72PyObject *EmbeddedPython::importerModule = NULL;
73EmbeddedPython::EmbeddedPython(const char *filename, const char *abspath,
74 const char *modpath, const unsigned char *code, int zlen, int len)
75 : filename(filename), abspath(abspath), modpath(modpath), code(code),
76 zlen(zlen), len(len)
77{
78 // if we've added the importer keep track of it because we need it
79 // to bootstrap.
80 if (string(modpath) == string("importer"))
81 importer = this;
82 else
83 getList().push_back(this);
84}
85
86list<EmbeddedPython *> &
87EmbeddedPython::getList()
88{
89 static list<EmbeddedPython *> the_list;
90 return the_list;
91}
92
93/*
94 * Uncompress and unmarshal the code object stored in the
95 * EmbeddedPython
96 */
97PyObject *
98EmbeddedPython::getCode() const
99{
100 Bytef marshalled[len];
101 uLongf unzlen = len;
102 int ret = uncompress(marshalled, &unzlen, (const Bytef *)code, zlen);
103 if (ret != Z_OK)
104 panic("Could not uncompress code: %s\n", zError(ret));
105 assert(unzlen == (uLongf)len);
106
107 return PyMarshal_ReadObjectFromString((char *)marshalled, len);
108}
109
110bool
111EmbeddedPython::addModule() const
112{
113 PyObject *code = getCode();
114 PyObject *result = PyObject_CallMethod(importerModule, PyCC("add_module"),
115 PyCC("sssO"), filename, abspath, modpath, code);
116 if (!result) {
117 PyErr_Print();
118 return false;
119 }
120
121 Py_DECREF(result);
122 return true;
123}
124
125/*
126 * Load and initialize all of the python parts of M5, including Swig
127 * and the embedded module importer.
128 */
129int
130EmbeddedPython::initAll()
131{
132 // Load the importer module
133 PyObject *code = importer->getCode();
134 importerModule = PyImport_ExecCodeModule(PyCC("importer"), code);
135 if (!importerModule) {
136 PyErr_Print();
137 return 1;
138 }
139
140 // Load the rest of the embedded python files into the embedded
141 // python importer
142 list<EmbeddedPython *>::iterator i = getList().begin();
143 list<EmbeddedPython *>::iterator end = getList().end();
144 for (; i != end; ++i)
145 if (!(*i)->addModule())
146 return 1;
147
148 return 0;
149}
150
151EmbeddedSwig::EmbeddedSwig(void (*init_func)(), const string& _context)
152 : initFunc(init_func), context(_context)
153{
154 getList().push_back(this);
155}
156
157list<EmbeddedSwig *> &
158EmbeddedSwig::getList()
159{
160 static list<EmbeddedSwig *> the_list;
161 return the_list;
162}
163
164void
165EmbeddedSwig::initAll()
166{
167 char* old_context = _Py_PackageContext;
168 // initialize SWIG modules. initFunc() is autogenerated and calls
169 // all of the individual swig initialization functions.
170 for (auto i : getList()) {
171 // to ensure that the loaded modules are placed in the right
172 // package we have to be a bit unorthodox and directly
173 // manipulate the package context since swig simply calls
174 // Py_InitModule with nothing but the module name of the
175 // wrapper
176 char* cstr = new char[i->context.size() + 1];
177 strcpy(cstr, i->context.c_str());
178 _Py_PackageContext = cstr;
179 i->initFunc();
180 delete[] cstr;
181 }
182 _Py_PackageContext = old_context;
183}
184
185int
186initM5Python()
187{
188 EmbeddedSwig::initAll();
189 return EmbeddedPython::initAll();
190}
191
192/*
193 * Make the commands array weak so that they can be overridden (used
194 * by unit tests to specify a different python main function.
195 */
196const char * __attribute__((weak)) m5MainCommands[] = {
197 "import m5",
198 "m5.main()",
199 0 // sentinel is required
200};
201
202/*
203 * Start up the M5 simulator. This mostly vectors into the python
204 * main function.
205 */
206int
207m5Main(int argc, char **argv)
208{
209#if HAVE_PROTOBUF
210 // Verify that the version of the protobuf library that we linked
211 // against is compatible with the version of the headers we
212 // compiled against.
213 GOOGLE_PROTOBUF_VERIFY_VERSION;
214#endif
215
216 PySys_SetArgv(argc, argv);
217
218 // We have to set things up in the special __main__ module
219 PyObject *module = PyImport_AddModule(PyCC("__main__"));
220 if (module == NULL)
221 panic("Could not import __main__");
222 PyObject *dict = PyModule_GetDict(module);
223
224 // import the main m5 module
225 PyObject *result;
226 const char **command = m5MainCommands;
227
228 // evaluate each command in the m5MainCommands array (basically a
229 // bunch of python statements.
230 while (*command) {
231 result = PyRun_String(*command, Py_file_input, dict, dict);
232 if (!result) {
233 PyErr_Print();
234 return 1;
235 }
236 Py_DECREF(result);
237
238 command++;
239 }
240
241#if HAVE_PROTOBUF
242 google::protobuf::ShutdownProtobufLibrary();
243#endif
244
245 return 0;
246}
247
248PyMODINIT_FUNC
249initm5(void)
250{
251 initM5Python();
252 PyImport_ImportModule(PyCC("m5"));
253}
65#endif
66
67using namespace std;
68
69// The python library is totally messed up with respect to constness,
70// so make a simple macro to make life a little easier
71#define PyCC(x) (const_cast<char *>(x))
72
73EmbeddedPython *EmbeddedPython::importer = NULL;
74PyObject *EmbeddedPython::importerModule = NULL;
75EmbeddedPython::EmbeddedPython(const char *filename, const char *abspath,
76 const char *modpath, const unsigned char *code, int zlen, int len)
77 : filename(filename), abspath(abspath), modpath(modpath), code(code),
78 zlen(zlen), len(len)
79{
80 // if we've added the importer keep track of it because we need it
81 // to bootstrap.
82 if (string(modpath) == string("importer"))
83 importer = this;
84 else
85 getList().push_back(this);
86}
87
88list<EmbeddedPython *> &
89EmbeddedPython::getList()
90{
91 static list<EmbeddedPython *> the_list;
92 return the_list;
93}
94
95/*
96 * Uncompress and unmarshal the code object stored in the
97 * EmbeddedPython
98 */
99PyObject *
100EmbeddedPython::getCode() const
101{
102 Bytef marshalled[len];
103 uLongf unzlen = len;
104 int ret = uncompress(marshalled, &unzlen, (const Bytef *)code, zlen);
105 if (ret != Z_OK)
106 panic("Could not uncompress code: %s\n", zError(ret));
107 assert(unzlen == (uLongf)len);
108
109 return PyMarshal_ReadObjectFromString((char *)marshalled, len);
110}
111
112bool
113EmbeddedPython::addModule() const
114{
115 PyObject *code = getCode();
116 PyObject *result = PyObject_CallMethod(importerModule, PyCC("add_module"),
117 PyCC("sssO"), filename, abspath, modpath, code);
118 if (!result) {
119 PyErr_Print();
120 return false;
121 }
122
123 Py_DECREF(result);
124 return true;
125}
126
127/*
128 * Load and initialize all of the python parts of M5, including Swig
129 * and the embedded module importer.
130 */
131int
132EmbeddedPython::initAll()
133{
134 // Load the importer module
135 PyObject *code = importer->getCode();
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
144 list<EmbeddedPython *>::iterator i = getList().begin();
145 list<EmbeddedPython *>::iterator end = getList().end();
146 for (; i != end; ++i)
147 if (!(*i)->addModule())
148 return 1;
149
150 return 0;
151}
152
153EmbeddedSwig::EmbeddedSwig(void (*init_func)(), const string& _context)
154 : initFunc(init_func), context(_context)
155{
156 getList().push_back(this);
157}
158
159list<EmbeddedSwig *> &
160EmbeddedSwig::getList()
161{
162 static list<EmbeddedSwig *> the_list;
163 return the_list;
164}
165
166void
167EmbeddedSwig::initAll()
168{
169 char* old_context = _Py_PackageContext;
170 // initialize SWIG modules. initFunc() is autogenerated and calls
171 // all of the individual swig initialization functions.
172 for (auto i : getList()) {
173 // to ensure that the loaded modules are placed in the right
174 // package we have to be a bit unorthodox and directly
175 // manipulate the package context since swig simply calls
176 // Py_InitModule with nothing but the module name of the
177 // wrapper
178 char* cstr = new char[i->context.size() + 1];
179 strcpy(cstr, i->context.c_str());
180 _Py_PackageContext = cstr;
181 i->initFunc();
182 delete[] cstr;
183 }
184 _Py_PackageContext = old_context;
185}
186
187int
188initM5Python()
189{
190 EmbeddedSwig::initAll();
191 return EmbeddedPython::initAll();
192}
193
194/*
195 * Make the commands array weak so that they can be overridden (used
196 * by unit tests to specify a different python main function.
197 */
198const char * __attribute__((weak)) m5MainCommands[] = {
199 "import m5",
200 "m5.main()",
201 0 // sentinel is required
202};
203
204/*
205 * Start up the M5 simulator. This mostly vectors into the python
206 * main function.
207 */
208int
209m5Main(int argc, char **argv)
210{
211#if HAVE_PROTOBUF
212 // Verify that the version of the protobuf library that we linked
213 // against is compatible with the version of the headers we
214 // compiled against.
215 GOOGLE_PROTOBUF_VERIFY_VERSION;
216#endif
217
218 PySys_SetArgv(argc, argv);
219
220 // We have to set things up in the special __main__ module
221 PyObject *module = PyImport_AddModule(PyCC("__main__"));
222 if (module == NULL)
223 panic("Could not import __main__");
224 PyObject *dict = PyModule_GetDict(module);
225
226 // import the main m5 module
227 PyObject *result;
228 const char **command = m5MainCommands;
229
230 // evaluate each command in the m5MainCommands array (basically a
231 // bunch of python statements.
232 while (*command) {
233 result = PyRun_String(*command, Py_file_input, dict, dict);
234 if (!result) {
235 PyErr_Print();
236 return 1;
237 }
238 Py_DECREF(result);
239
240 command++;
241 }
242
243#if HAVE_PROTOBUF
244 google::protobuf::ShutdownProtobufLibrary();
245#endif
246
247 return 0;
248}
249
250PyMODINIT_FUNC
251initm5(void)
252{
253 initM5Python();
254 PyImport_ImportModule(PyCC("m5"));
255}