syscall_emul: standardized file descriptor name and add return checks.
[gem5.git] / src / sim / init.cc
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 <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"
59 #include "sim/init.hh"
60
61 #if HAVE_PROTOBUF
62 #include <google/protobuf/stubs/common.h>
63 #endif
64
65 using 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
71 EmbeddedPython *EmbeddedPython::importer = NULL;
72 PyObject *EmbeddedPython::importerModule = NULL;
73 EmbeddedPython::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
86 list<EmbeddedPython *> &
87 EmbeddedPython::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 */
97 PyObject *
98 EmbeddedPython::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
110 bool
111 EmbeddedPython::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 */
129 int
130 EmbeddedPython::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
151 EmbeddedSwig::EmbeddedSwig(void (*init_func)())
152 : initFunc(init_func)
153 {
154 getList().push_back(this);
155 }
156
157 list<EmbeddedSwig *> &
158 EmbeddedSwig::getList()
159 {
160 static list<EmbeddedSwig *> the_list;
161 return the_list;
162 }
163
164 void
165 EmbeddedSwig::initAll()
166 {
167 // initialize SWIG modules. initSwig() is autogenerated and calls
168 // all of the individual swig initialization functions.
169 list<EmbeddedSwig *>::iterator i = getList().begin();
170 list<EmbeddedSwig *>::iterator end = getList().end();
171 for (; i != end; ++i)
172 (*i)->initFunc();
173 }
174
175 int
176 initM5Python()
177 {
178 EmbeddedSwig::initAll();
179 return EmbeddedPython::initAll();
180 }
181
182 /*
183 * Make the commands array weak so that they can be overridden (used
184 * by unit tests to specify a different python main function.
185 */
186 const char * __attribute__((weak)) m5MainCommands[] = {
187 "import m5",
188 "m5.main()",
189 0 // sentinel is required
190 };
191
192 /*
193 * Start up the M5 simulator. This mostly vectors into the python
194 * main function.
195 */
196 int
197 m5Main(int argc, char **argv)
198 {
199 #if HAVE_PROTOBUF
200 // Verify that the version of the protobuf library that we linked
201 // against is compatible with the version of the headers we
202 // compiled against.
203 GOOGLE_PROTOBUF_VERIFY_VERSION;
204 #endif
205
206 PySys_SetArgv(argc, argv);
207
208 // We have to set things up in the special __main__ module
209 PyObject *module = PyImport_AddModule(PyCC("__main__"));
210 if (module == NULL)
211 panic("Could not import __main__");
212 PyObject *dict = PyModule_GetDict(module);
213
214 // import the main m5 module
215 PyObject *result;
216 const char **command = m5MainCommands;
217
218 // evaluate each command in the m5MainCommands array (basically a
219 // bunch of python statements.
220 while (*command) {
221 result = PyRun_String(*command, Py_file_input, dict, dict);
222 if (!result) {
223 PyErr_Print();
224 return 1;
225 }
226 Py_DECREF(result);
227
228 command++;
229 }
230
231 #if HAVE_PROTOBUF
232 google::protobuf::ShutdownProtobufLibrary();
233 #endif
234
235 return 0;
236 }
237
238 PyMODINIT_FUNC
239 initm5(void)
240 {
241 initM5Python();
242 PyImport_ImportModule(PyCC("m5"));
243 }