init.cc revision 6216:2f4020838149
19609Sandreas.hansson@arm.com/* 29398Sandreas.hansson@arm.com * Copyright (c) 2000-2005 The Regents of The University of Michigan 39398Sandreas.hansson@arm.com * Copyright (c) 2008 The Hewlett-Packard Development Company 49398Sandreas.hansson@arm.com * All rights reserved. 59398Sandreas.hansson@arm.com * 69398Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without 79398Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are 89398Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright 99398Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer; 109398Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright 119398Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the 129398Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution; 139398Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its 149398Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from 159398Sandreas.hansson@arm.com * this software without specific prior written permission. 169398Sandreas.hansson@arm.com * 179398Sandreas.hansson@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 189398Sandreas.hansson@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 199398Sandreas.hansson@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 209398Sandreas.hansson@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 219398Sandreas.hansson@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 229398Sandreas.hansson@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 239398Sandreas.hansson@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 249398Sandreas.hansson@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 259398Sandreas.hansson@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 269398Sandreas.hansson@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 279398Sandreas.hansson@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 289398Sandreas.hansson@arm.com * 299398Sandreas.hansson@arm.com * Authors: Nathan Binkert 309398Sandreas.hansson@arm.com */ 319398Sandreas.hansson@arm.com 329398Sandreas.hansson@arm.com#include <Python.h> 339398Sandreas.hansson@arm.com#include <marshal.h> 349398Sandreas.hansson@arm.com#include <signal.h> 359398Sandreas.hansson@arm.com 369398Sandreas.hansson@arm.com#include <iostream> 379398Sandreas.hansson@arm.com#include <string> 389398Sandreas.hansson@arm.com#include <zlib.h> 399398Sandreas.hansson@arm.com 409398Sandreas.hansson@arm.com#include "base/cprintf.hh" 419398Sandreas.hansson@arm.com#include "base/misc.hh" 429398Sandreas.hansson@arm.com#include "base/types.hh" 439398Sandreas.hansson@arm.com#include "sim/async.hh" 449398Sandreas.hansson@arm.com#include "sim/core.hh" 459398Sandreas.hansson@arm.com#include "sim/init.hh" 469398Sandreas.hansson@arm.com 479398Sandreas.hansson@arm.comusing namespace std; 489398Sandreas.hansson@arm.com 499398Sandreas.hansson@arm.com/// Stats signal handler. 509398Sandreas.hansson@arm.comvoid 519398Sandreas.hansson@arm.comdumpStatsHandler(int sigtype) 529609Sandreas.hansson@arm.com{ 539609Sandreas.hansson@arm.com async_event = true; 549609Sandreas.hansson@arm.com async_statdump = true; 559610Sandreas.hansson@arm.com} 569610Sandreas.hansson@arm.com 579610Sandreas.hansson@arm.comvoid 589398Sandreas.hansson@arm.comdumprstStatsHandler(int sigtype) 599398Sandreas.hansson@arm.com{ 609398Sandreas.hansson@arm.com async_event = true; 619398Sandreas.hansson@arm.com async_statdump = true; 629398Sandreas.hansson@arm.com async_statreset = true; 639609Sandreas.hansson@arm.com} 649610Sandreas.hansson@arm.com 659398Sandreas.hansson@arm.com/// Exit signal handler. 66void 67exitNowHandler(int sigtype) 68{ 69 async_event = true; 70 async_exit = true; 71} 72 73/// Abort signal handler. 74void 75abortHandler(int sigtype) 76{ 77 ccprintf(cerr, "Program aborted at cycle %d\n", curTick); 78} 79 80/* 81 * M5 can do several special things when various signals are sent. 82 * None are mandatory. 83 */ 84void 85initSignals() 86{ 87 // Floating point exceptions may happen on misspeculated paths, so 88 // ignore them 89 signal(SIGFPE, SIG_IGN); 90 91 // We use SIGTRAP sometimes for debugging 92 signal(SIGTRAP, SIG_IGN); 93 94 // Dump intermediate stats 95 signal(SIGUSR1, dumpStatsHandler); 96 97 // Dump intermediate stats and reset them 98 signal(SIGUSR2, dumprstStatsHandler); 99 100 // Exit cleanly on Interrupt (Ctrl-C) 101 signal(SIGINT, exitNowHandler); 102 103 // Print out cycle number on abort 104 signal(SIGABRT, abortHandler); 105} 106 107/* 108 * Uncompress and unmarshal the code object stored in the 109 * EmbeddedPyModule 110 */ 111PyObject * 112getCode(const EmbeddedPyModule *pymod) 113{ 114 assert(pymod->zlen == pymod->code_end - pymod->code); 115 Bytef *marshalled = new Bytef[pymod->mlen]; 116 uLongf unzlen = pymod->mlen; 117 int ret = uncompress(marshalled, &unzlen, (const Bytef *)pymod->code, 118 pymod->zlen); 119 if (ret != Z_OK) 120 panic("Could not uncompress code: %s\n", zError(ret)); 121 assert(unzlen == pymod->mlen); 122 123 return PyMarshal_ReadObjectFromString((char *)marshalled, pymod->mlen); 124} 125 126// The python library is totally messed up with respect to constness, 127// so make a simple macro to make life a little easier 128#define PyCC(x) (const_cast<char *>(x)) 129 130/* 131 * Load and initialize all of the python parts of M5, including Swig 132 * and the embedded module importer. 133 */ 134int 135initM5Python() 136{ 137 extern void initSwig(); 138 139 // initialize SWIG modules. initSwig() is autogenerated and calls 140 // all of the individual swig initialization functions. 141 initSwig(); 142 143 // Load the importer module 144 PyObject *code = getCode(&embeddedPyImporter); 145 PyObject *module = PyImport_ExecCodeModule(PyCC("importer"), code); 146 if (!module) { 147 PyErr_Print(); 148 return 1; 149 } 150 151 // Load the rest of the embedded python files into the embedded 152 // python importer 153 const EmbeddedPyModule *pymod = &embeddedPyModules[0]; 154 while (pymod->filename) { 155 PyObject *code = getCode(pymod); 156 PyObject *result = PyObject_CallMethod(module, PyCC("add_module"), 157 PyCC("ssO"), pymod->filename, pymod->modpath, code); 158 if (!result) { 159 PyErr_Print(); 160 return 1; 161 } 162 Py_DECREF(result); 163 ++pymod; 164 } 165 166 return 0; 167} 168 169/* 170 * Start up the M5 simulator. This mostly vectors into the python 171 * main function. 172 */ 173int 174m5Main(int argc, char **argv) 175{ 176 PySys_SetArgv(argc, argv); 177 178 // We have to set things up in the special __main__ module 179 PyObject *module = PyImport_AddModule(PyCC("__main__")); 180 if (module == NULL) 181 panic("Could not import __main__"); 182 PyObject *dict = PyModule_GetDict(module); 183 184 // import the main m5 module 185 PyObject *result; 186 result = PyRun_String("import m5", Py_file_input, dict, dict); 187 if (!result) { 188 PyErr_Print(); 189 return 1; 190 } 191 Py_DECREF(result); 192 193 // Start m5 194 result = PyRun_String("m5.main()", Py_file_input, dict, dict); 195 if (!result) { 196 PyErr_Print(); 197 return 1; 198 } 199 Py_DECREF(result); 200 201 return 0; 202} 203 204PyMODINIT_FUNC 205initm5(void) 206{ 207 initM5Python(); 208 PyImport_ImportModule(PyCC("m5")); 209} 210