init.cc revision 9397
111507SCurtis.Dunham@arm.com/* 211507SCurtis.Dunham@arm.com * Copyright (c) 2012 ARM Limited 311507SCurtis.Dunham@arm.com * All rights reserved 411507SCurtis.Dunham@arm.com * 511507SCurtis.Dunham@arm.com * The license below extends only to copyright in the software and shall 611507SCurtis.Dunham@arm.com * not be construed as granting a license to any other intellectual 711507SCurtis.Dunham@arm.com * property including but not limited to intellectual property relating 811507SCurtis.Dunham@arm.com * to a hardware implementation of the functionality of the software 911507SCurtis.Dunham@arm.com * licensed hereunder. You may use the software subject to the license 1011507SCurtis.Dunham@arm.com * terms below provided that you ensure that this notice is replicated 1111507SCurtis.Dunham@arm.com * unmodified and in its entirety in all distributions of the software, 1211507SCurtis.Dunham@arm.com * modified or unmodified, in source code or in binary form. 1311507SCurtis.Dunham@arm.com * 1411507SCurtis.Dunham@arm.com * Copyright (c) 2000-2005 The Regents of The University of Michigan 1511507SCurtis.Dunham@arm.com * Copyright (c) 2008 The Hewlett-Packard Development Company 1611507SCurtis.Dunham@arm.com * All rights reserved. 1711507SCurtis.Dunham@arm.com * 1811507SCurtis.Dunham@arm.com * Redistribution and use in source and binary forms, with or without 1911507SCurtis.Dunham@arm.com * modification, are permitted provided that the following conditions are 2011507SCurtis.Dunham@arm.com * met: redistributions of source code must retain the above copyright 2111507SCurtis.Dunham@arm.com * notice, this list of conditions and the following disclaimer; 2211507SCurtis.Dunham@arm.com * redistributions in binary form must reproduce the above copyright 2311507SCurtis.Dunham@arm.com * notice, this list of conditions and the following disclaimer in the 2411507SCurtis.Dunham@arm.com * documentation and/or other materials provided with the distribution; 2511507SCurtis.Dunham@arm.com * neither the name of the copyright holders nor the names of its 2611507SCurtis.Dunham@arm.com * contributors may be used to endorse or promote products derived from 2711507SCurtis.Dunham@arm.com * this software without specific prior written permission. 2811507SCurtis.Dunham@arm.com * 2911507SCurtis.Dunham@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 3011507SCurtis.Dunham@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3111507SCurtis.Dunham@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 3211507SCurtis.Dunham@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3311507SCurtis.Dunham@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3411507SCurtis.Dunham@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3511507SCurtis.Dunham@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3611507SCurtis.Dunham@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3711507SCurtis.Dunham@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3811507SCurtis.Dunham@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3911507SCurtis.Dunham@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 4011507SCurtis.Dunham@arm.com * 4111507SCurtis.Dunham@arm.com * Authors: Nathan Binkert 4211507SCurtis.Dunham@arm.com */ 4311507SCurtis.Dunham@arm.com 4411507SCurtis.Dunham@arm.com#include <Python.h> 4511507SCurtis.Dunham@arm.com 4611507SCurtis.Dunham@arm.com#include <marshal.h> 4711507SCurtis.Dunham@arm.com#include <zlib.h> 4811507SCurtis.Dunham@arm.com 4911507SCurtis.Dunham@arm.com#include <csignal> 5011507SCurtis.Dunham@arm.com#include <iostream> 5111507SCurtis.Dunham@arm.com#include <list> 5211507SCurtis.Dunham@arm.com#include <string> 5311507SCurtis.Dunham@arm.com 5411507SCurtis.Dunham@arm.com#include "base/cprintf.hh" 5511507SCurtis.Dunham@arm.com#include "base/misc.hh" 5611507SCurtis.Dunham@arm.com#include "base/types.hh" 5711507SCurtis.Dunham@arm.com#include "config/have_protobuf.hh" 5811507SCurtis.Dunham@arm.com#include "sim/async.hh" 5911507SCurtis.Dunham@arm.com#include "sim/core.hh" 6011507SCurtis.Dunham@arm.com#include "sim/init.hh" 6111507SCurtis.Dunham@arm.com 6211507SCurtis.Dunham@arm.com#if HAVE_PROTOBUF 6311507SCurtis.Dunham@arm.com#include <google/protobuf/stubs/common.h> 6411507SCurtis.Dunham@arm.com#endif 6511507SCurtis.Dunham@arm.com 6611507SCurtis.Dunham@arm.comusing namespace std; 6711507SCurtis.Dunham@arm.com 6811507SCurtis.Dunham@arm.com/// Stats signal handler. 6911507SCurtis.Dunham@arm.comvoid 7011507SCurtis.Dunham@arm.comdumpStatsHandler(int sigtype) 7111507SCurtis.Dunham@arm.com{ 7211507SCurtis.Dunham@arm.com async_event = true; 7311507SCurtis.Dunham@arm.com async_statdump = true; 7411507SCurtis.Dunham@arm.com} 7511507SCurtis.Dunham@arm.com 7611507SCurtis.Dunham@arm.comvoid 7711507SCurtis.Dunham@arm.comdumprstStatsHandler(int sigtype) 7811507SCurtis.Dunham@arm.com{ 7911507SCurtis.Dunham@arm.com async_event = true; 8011507SCurtis.Dunham@arm.com async_statdump = true; 8111507SCurtis.Dunham@arm.com async_statreset = true; 8211507SCurtis.Dunham@arm.com} 8311507SCurtis.Dunham@arm.com 8411507SCurtis.Dunham@arm.com/// Exit signal handler. 8511507SCurtis.Dunham@arm.comvoid 8611507SCurtis.Dunham@arm.comexitNowHandler(int sigtype) 8711507SCurtis.Dunham@arm.com{ 8811507SCurtis.Dunham@arm.com async_event = true; 8911507SCurtis.Dunham@arm.com async_exit = true; 9011507SCurtis.Dunham@arm.com} 9111507SCurtis.Dunham@arm.com 9211507SCurtis.Dunham@arm.com/// Abort signal handler. 9311507SCurtis.Dunham@arm.comvoid 9411507SCurtis.Dunham@arm.comabortHandler(int sigtype) 9511507SCurtis.Dunham@arm.com{ 9611507SCurtis.Dunham@arm.com ccprintf(cerr, "Program aborted at cycle %d\n", curTick()); 9711507SCurtis.Dunham@arm.com} 9811507SCurtis.Dunham@arm.com 9911507SCurtis.Dunham@arm.com/* 10011507SCurtis.Dunham@arm.com * M5 can do several special things when various signals are sent. 10111507SCurtis.Dunham@arm.com * None are mandatory. 10211507SCurtis.Dunham@arm.com */ 10311507SCurtis.Dunham@arm.comvoid 10411507SCurtis.Dunham@arm.cominitSignals() 10511507SCurtis.Dunham@arm.com{ 10611507SCurtis.Dunham@arm.com // Floating point exceptions may happen on misspeculated paths, so 10711507SCurtis.Dunham@arm.com // ignore them 10811507SCurtis.Dunham@arm.com signal(SIGFPE, SIG_IGN); 10911507SCurtis.Dunham@arm.com 11011507SCurtis.Dunham@arm.com // We use SIGTRAP sometimes for debugging 11111507SCurtis.Dunham@arm.com signal(SIGTRAP, SIG_IGN); 11211507SCurtis.Dunham@arm.com 11311507SCurtis.Dunham@arm.com // Dump intermediate stats 11411507SCurtis.Dunham@arm.com signal(SIGUSR1, dumpStatsHandler); 11511507SCurtis.Dunham@arm.com 11611507SCurtis.Dunham@arm.com // Dump intermediate stats and reset them 11711507SCurtis.Dunham@arm.com signal(SIGUSR2, dumprstStatsHandler); 11811507SCurtis.Dunham@arm.com 11911507SCurtis.Dunham@arm.com // Exit cleanly on Interrupt (Ctrl-C) 12011507SCurtis.Dunham@arm.com signal(SIGINT, exitNowHandler); 12111507SCurtis.Dunham@arm.com 12211507SCurtis.Dunham@arm.com // Print out cycle number on abort 12311507SCurtis.Dunham@arm.com signal(SIGABRT, abortHandler); 12411507SCurtis.Dunham@arm.com} 12511507SCurtis.Dunham@arm.com 12611507SCurtis.Dunham@arm.com// The python library is totally messed up with respect to constness, 12711507SCurtis.Dunham@arm.com// so make a simple macro to make life a little easier 12811507SCurtis.Dunham@arm.com#define PyCC(x) (const_cast<char *>(x)) 12911507SCurtis.Dunham@arm.com 13011507SCurtis.Dunham@arm.comEmbeddedPython *EmbeddedPython::importer = NULL; 13111507SCurtis.Dunham@arm.comPyObject *EmbeddedPython::importerModule = NULL; 13211507SCurtis.Dunham@arm.comEmbeddedPython::EmbeddedPython(const char *filename, const char *abspath, 13311507SCurtis.Dunham@arm.com const char *modpath, const unsigned char *code, int zlen, int len) 13411507SCurtis.Dunham@arm.com : filename(filename), abspath(abspath), modpath(modpath), code(code), 13511507SCurtis.Dunham@arm.com zlen(zlen), len(len) 13611507SCurtis.Dunham@arm.com{ 13711507SCurtis.Dunham@arm.com // if we've added the importer keep track of it because we need it 13811507SCurtis.Dunham@arm.com // to bootstrap. 13911507SCurtis.Dunham@arm.com if (string(modpath) == string("importer")) 14011507SCurtis.Dunham@arm.com importer = this; 14111507SCurtis.Dunham@arm.com else 14211507SCurtis.Dunham@arm.com getList().push_back(this); 14311507SCurtis.Dunham@arm.com} 14411507SCurtis.Dunham@arm.com 14511507SCurtis.Dunham@arm.comlist<EmbeddedPython *> & 14611507SCurtis.Dunham@arm.comEmbeddedPython::getList() 14711507SCurtis.Dunham@arm.com{ 14811507SCurtis.Dunham@arm.com static list<EmbeddedPython *> the_list; 14911507SCurtis.Dunham@arm.com return the_list; 15011507SCurtis.Dunham@arm.com} 15111507SCurtis.Dunham@arm.com 15211507SCurtis.Dunham@arm.com/* 15311507SCurtis.Dunham@arm.com * Uncompress and unmarshal the code object stored in the 15411507SCurtis.Dunham@arm.com * EmbeddedPython 15511507SCurtis.Dunham@arm.com */ 15611507SCurtis.Dunham@arm.comPyObject * 15711507SCurtis.Dunham@arm.comEmbeddedPython::getCode() const 15811507SCurtis.Dunham@arm.com{ 15911507SCurtis.Dunham@arm.com Bytef marshalled[len]; 16011507SCurtis.Dunham@arm.com uLongf unzlen = len; 16111507SCurtis.Dunham@arm.com int ret = uncompress(marshalled, &unzlen, (const Bytef *)code, zlen); 16211507SCurtis.Dunham@arm.com if (ret != Z_OK) 16311507SCurtis.Dunham@arm.com panic("Could not uncompress code: %s\n", zError(ret)); 16411507SCurtis.Dunham@arm.com assert(unzlen == (uLongf)len); 16511507SCurtis.Dunham@arm.com 16611507SCurtis.Dunham@arm.com return PyMarshal_ReadObjectFromString((char *)marshalled, len); 16711507SCurtis.Dunham@arm.com} 16811507SCurtis.Dunham@arm.com 16911507SCurtis.Dunham@arm.combool 17011507SCurtis.Dunham@arm.comEmbeddedPython::addModule() const 17111507SCurtis.Dunham@arm.com{ 17211507SCurtis.Dunham@arm.com PyObject *code = getCode(); 17311507SCurtis.Dunham@arm.com PyObject *result = PyObject_CallMethod(importerModule, PyCC("add_module"), 17411507SCurtis.Dunham@arm.com PyCC("sssO"), filename, abspath, modpath, code); 17511507SCurtis.Dunham@arm.com if (!result) { 17611507SCurtis.Dunham@arm.com PyErr_Print(); 17711507SCurtis.Dunham@arm.com return false; 17811507SCurtis.Dunham@arm.com } 17911507SCurtis.Dunham@arm.com 18011507SCurtis.Dunham@arm.com Py_DECREF(result); 18111507SCurtis.Dunham@arm.com return true; 18211507SCurtis.Dunham@arm.com} 18311507SCurtis.Dunham@arm.com 18411507SCurtis.Dunham@arm.com/* 18511507SCurtis.Dunham@arm.com * Load and initialize all of the python parts of M5, including Swig 18611507SCurtis.Dunham@arm.com * and the embedded module importer. 18711507SCurtis.Dunham@arm.com */ 18811507SCurtis.Dunham@arm.comint 18911507SCurtis.Dunham@arm.comEmbeddedPython::initAll() 19011507SCurtis.Dunham@arm.com{ 19111507SCurtis.Dunham@arm.com // Load the importer module 19211507SCurtis.Dunham@arm.com PyObject *code = importer->getCode(); 19311507SCurtis.Dunham@arm.com importerModule = PyImport_ExecCodeModule(PyCC("importer"), code); 19411507SCurtis.Dunham@arm.com if (!importerModule) { 19511507SCurtis.Dunham@arm.com PyErr_Print(); 19611507SCurtis.Dunham@arm.com return 1; 19711507SCurtis.Dunham@arm.com } 19811507SCurtis.Dunham@arm.com 19911507SCurtis.Dunham@arm.com // Load the rest of the embedded python files into the embedded 20011507SCurtis.Dunham@arm.com // python importer 20111507SCurtis.Dunham@arm.com list<EmbeddedPython *>::iterator i = getList().begin(); 20211507SCurtis.Dunham@arm.com list<EmbeddedPython *>::iterator end = getList().end(); 20311507SCurtis.Dunham@arm.com for (; i != end; ++i) 20411507SCurtis.Dunham@arm.com if (!(*i)->addModule()) 20511507SCurtis.Dunham@arm.com return 1; 20611507SCurtis.Dunham@arm.com 20711507SCurtis.Dunham@arm.com return 0; 20811507SCurtis.Dunham@arm.com} 20911507SCurtis.Dunham@arm.com 21011507SCurtis.Dunham@arm.comEmbeddedSwig::EmbeddedSwig(void (*init_func)()) 21111507SCurtis.Dunham@arm.com : initFunc(init_func) 21211507SCurtis.Dunham@arm.com{ 21311507SCurtis.Dunham@arm.com getList().push_back(this); 21411507SCurtis.Dunham@arm.com} 21511507SCurtis.Dunham@arm.com 21611507SCurtis.Dunham@arm.comlist<EmbeddedSwig *> & 21711507SCurtis.Dunham@arm.comEmbeddedSwig::getList() 21811507SCurtis.Dunham@arm.com{ 21911507SCurtis.Dunham@arm.com static list<EmbeddedSwig *> the_list; 22011507SCurtis.Dunham@arm.com return the_list; 22111507SCurtis.Dunham@arm.com} 22211507SCurtis.Dunham@arm.com 22311507SCurtis.Dunham@arm.comvoid 22411507SCurtis.Dunham@arm.comEmbeddedSwig::initAll() 22511507SCurtis.Dunham@arm.com{ 22611507SCurtis.Dunham@arm.com // initialize SWIG modules. initSwig() is autogenerated and calls 22711507SCurtis.Dunham@arm.com // all of the individual swig initialization functions. 22811507SCurtis.Dunham@arm.com list<EmbeddedSwig *>::iterator i = getList().begin(); 22911507SCurtis.Dunham@arm.com list<EmbeddedSwig *>::iterator end = getList().end(); 23011507SCurtis.Dunham@arm.com for (; i != end; ++i) 23111507SCurtis.Dunham@arm.com (*i)->initFunc(); 23211507SCurtis.Dunham@arm.com} 23311507SCurtis.Dunham@arm.com 23411507SCurtis.Dunham@arm.comint 23511507SCurtis.Dunham@arm.cominitM5Python() 23611507SCurtis.Dunham@arm.com{ 23711507SCurtis.Dunham@arm.com EmbeddedSwig::initAll(); 23811507SCurtis.Dunham@arm.com return EmbeddedPython::initAll(); 23911507SCurtis.Dunham@arm.com} 24011507SCurtis.Dunham@arm.com 24111507SCurtis.Dunham@arm.com/* 24211507SCurtis.Dunham@arm.com * Make the commands array weak so that they can be overridden (used 24311507SCurtis.Dunham@arm.com * by unit tests to specify a different python main function. 24411507SCurtis.Dunham@arm.com */ 24511507SCurtis.Dunham@arm.comconst char * __attribute__((weak)) m5MainCommands[] = { 24611507SCurtis.Dunham@arm.com "import m5", 24711507SCurtis.Dunham@arm.com "m5.main()", 24811507SCurtis.Dunham@arm.com 0 // sentinel is required 24911507SCurtis.Dunham@arm.com}; 25011507SCurtis.Dunham@arm.com 25111507SCurtis.Dunham@arm.com/* 25211507SCurtis.Dunham@arm.com * Start up the M5 simulator. This mostly vectors into the python 25311507SCurtis.Dunham@arm.com * main function. 25411507SCurtis.Dunham@arm.com */ 25511507SCurtis.Dunham@arm.comint 25611507SCurtis.Dunham@arm.comm5Main(int argc, char **argv) 25711507SCurtis.Dunham@arm.com{ 25811507SCurtis.Dunham@arm.com#if HAVE_PROTOBUF 25911507SCurtis.Dunham@arm.com // Verify that the version of the protobuf library that we linked 26011507SCurtis.Dunham@arm.com // against is compatible with the version of the headers we 26111507SCurtis.Dunham@arm.com // compiled against. 26211507SCurtis.Dunham@arm.com GOOGLE_PROTOBUF_VERIFY_VERSION; 26311507SCurtis.Dunham@arm.com#endif 26411507SCurtis.Dunham@arm.com 26511507SCurtis.Dunham@arm.com PySys_SetArgv(argc, argv); 26611507SCurtis.Dunham@arm.com 26711507SCurtis.Dunham@arm.com // We have to set things up in the special __main__ module 26811507SCurtis.Dunham@arm.com PyObject *module = PyImport_AddModule(PyCC("__main__")); 26911507SCurtis.Dunham@arm.com if (module == NULL) 27011507SCurtis.Dunham@arm.com panic("Could not import __main__"); 27111507SCurtis.Dunham@arm.com PyObject *dict = PyModule_GetDict(module); 27211507SCurtis.Dunham@arm.com 27311507SCurtis.Dunham@arm.com // import the main m5 module 27411507SCurtis.Dunham@arm.com PyObject *result; 27511507SCurtis.Dunham@arm.com const char **command = m5MainCommands; 27611507SCurtis.Dunham@arm.com 27711507SCurtis.Dunham@arm.com // evaluate each command in the m5MainCommands array (basically a 27811507SCurtis.Dunham@arm.com // bunch of python statements. 27911507SCurtis.Dunham@arm.com while (*command) { 28011507SCurtis.Dunham@arm.com result = PyRun_String(*command, Py_file_input, dict, dict); 28111507SCurtis.Dunham@arm.com if (!result) { 28211507SCurtis.Dunham@arm.com PyErr_Print(); 28311507SCurtis.Dunham@arm.com return 1; 28411507SCurtis.Dunham@arm.com } 28511507SCurtis.Dunham@arm.com Py_DECREF(result); 28611507SCurtis.Dunham@arm.com 28711507SCurtis.Dunham@arm.com command++; 28811507SCurtis.Dunham@arm.com } 28911507SCurtis.Dunham@arm.com 29011507SCurtis.Dunham@arm.com#if HAVE_PROTOBUF 29111507SCurtis.Dunham@arm.com google::protobuf::ShutdownProtobufLibrary(); 29211507SCurtis.Dunham@arm.com#endif 29311507SCurtis.Dunham@arm.com 29411507SCurtis.Dunham@arm.com return 0; 29511507SCurtis.Dunham@arm.com} 29611507SCurtis.Dunham@arm.com 29711507SCurtis.Dunham@arm.comPyMODINIT_FUNC 29811507SCurtis.Dunham@arm.cominitm5(void) 29911507SCurtis.Dunham@arm.com{ 30011507SCurtis.Dunham@arm.com initM5Python(); 30111507SCurtis.Dunham@arm.com PyImport_ImportModule(PyCC("m5")); 30211507SCurtis.Dunham@arm.com} 30311507SCurtis.Dunham@arm.com