init.cc revision 9397
15522Snate@binkert.org/*
29397Sandreas.hansson@arm.com * Copyright (c) 2012 ARM Limited
39397Sandreas.hansson@arm.com * All rights reserved
49397Sandreas.hansson@arm.com *
59397Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
69397Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
79397Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
89397Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
99397Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
109397Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
119397Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
129397Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
139397Sandreas.hansson@arm.com *
145522Snate@binkert.org * Copyright (c) 2000-2005 The Regents of The University of Michigan
155522Snate@binkert.org * Copyright (c) 2008 The Hewlett-Packard Development Company
165522Snate@binkert.org * All rights reserved.
175522Snate@binkert.org *
185522Snate@binkert.org * Redistribution and use in source and binary forms, with or without
195522Snate@binkert.org * modification, are permitted provided that the following conditions are
205522Snate@binkert.org * met: redistributions of source code must retain the above copyright
215522Snate@binkert.org * notice, this list of conditions and the following disclaimer;
225522Snate@binkert.org * redistributions in binary form must reproduce the above copyright
235522Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
245522Snate@binkert.org * documentation and/or other materials provided with the distribution;
255522Snate@binkert.org * neither the name of the copyright holders nor the names of its
265522Snate@binkert.org * contributors may be used to endorse or promote products derived from
275522Snate@binkert.org * this software without specific prior written permission.
285522Snate@binkert.org *
295522Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
305522Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
315522Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
325522Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
335522Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
345522Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
355522Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
365522Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
375522Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
385522Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
395522Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
405522Snate@binkert.org *
415522Snate@binkert.org * Authors: Nathan Binkert
425522Snate@binkert.org */
435522Snate@binkert.org
445522Snate@binkert.org#include <Python.h>
458229Snate@binkert.org
465522Snate@binkert.org#include <marshal.h>
478229Snate@binkert.org#include <zlib.h>
485522Snate@binkert.org
498229Snate@binkert.org#include <csignal>
508229Snate@binkert.org#include <iostream>
517674Snate@binkert.org#include <list>
525522Snate@binkert.org#include <string>
535522Snate@binkert.org
545522Snate@binkert.org#include "base/cprintf.hh"
555522Snate@binkert.org#include "base/misc.hh"
566216Snate@binkert.org#include "base/types.hh"
579397Sandreas.hansson@arm.com#include "config/have_protobuf.hh"
585522Snate@binkert.org#include "sim/async.hh"
595522Snate@binkert.org#include "sim/core.hh"
605522Snate@binkert.org#include "sim/init.hh"
615522Snate@binkert.org
629397Sandreas.hansson@arm.com#if HAVE_PROTOBUF
639397Sandreas.hansson@arm.com#include <google/protobuf/stubs/common.h>
649397Sandreas.hansson@arm.com#endif
659397Sandreas.hansson@arm.com
665522Snate@binkert.orgusing namespace std;
675522Snate@binkert.org
685522Snate@binkert.org/// Stats signal handler.
695522Snate@binkert.orgvoid
705522Snate@binkert.orgdumpStatsHandler(int sigtype)
715522Snate@binkert.org{
725522Snate@binkert.org    async_event = true;
735522Snate@binkert.org    async_statdump = true;
745522Snate@binkert.org}
755522Snate@binkert.org
765522Snate@binkert.orgvoid
775522Snate@binkert.orgdumprstStatsHandler(int sigtype)
785522Snate@binkert.org{
795522Snate@binkert.org    async_event = true;
805522Snate@binkert.org    async_statdump = true;
815522Snate@binkert.org    async_statreset = true;
825522Snate@binkert.org}
835522Snate@binkert.org
845522Snate@binkert.org/// Exit signal handler.
855522Snate@binkert.orgvoid
865522Snate@binkert.orgexitNowHandler(int sigtype)
875522Snate@binkert.org{
885522Snate@binkert.org    async_event = true;
895522Snate@binkert.org    async_exit = true;
905522Snate@binkert.org}
915522Snate@binkert.org
925522Snate@binkert.org/// Abort signal handler.
935522Snate@binkert.orgvoid
945522Snate@binkert.orgabortHandler(int sigtype)
955522Snate@binkert.org{
967823Ssteve.reinhardt@amd.com    ccprintf(cerr, "Program aborted at cycle %d\n", curTick());
975522Snate@binkert.org}
985522Snate@binkert.org
995522Snate@binkert.org/*
1005522Snate@binkert.org * M5 can do several special things when various signals are sent.
1015522Snate@binkert.org * None are mandatory.
1025522Snate@binkert.org */
1035522Snate@binkert.orgvoid
1045522Snate@binkert.orginitSignals()
1055522Snate@binkert.org{
1065522Snate@binkert.org    // Floating point exceptions may happen on misspeculated paths, so
1075522Snate@binkert.org    // ignore them
1085522Snate@binkert.org    signal(SIGFPE, SIG_IGN);
1095522Snate@binkert.org
1105522Snate@binkert.org    // We use SIGTRAP sometimes for debugging
1115522Snate@binkert.org    signal(SIGTRAP, SIG_IGN);
1125522Snate@binkert.org
1135522Snate@binkert.org    // Dump intermediate stats
1145522Snate@binkert.org    signal(SIGUSR1, dumpStatsHandler);
1155522Snate@binkert.org
1165522Snate@binkert.org    // Dump intermediate stats and reset them
1175522Snate@binkert.org    signal(SIGUSR2, dumprstStatsHandler);
1185522Snate@binkert.org
1195522Snate@binkert.org    // Exit cleanly on Interrupt (Ctrl-C)
1205522Snate@binkert.org    signal(SIGINT, exitNowHandler);
1215522Snate@binkert.org
1225522Snate@binkert.org    // Print out cycle number on abort
1235522Snate@binkert.org    signal(SIGABRT, abortHandler);
1245522Snate@binkert.org}
1255522Snate@binkert.org
1265522Snate@binkert.org// The python library is totally messed up with respect to constness,
1275522Snate@binkert.org// so make a simple macro to make life a little easier
1285522Snate@binkert.org#define PyCC(x) (const_cast<char *>(x))
1295522Snate@binkert.org
1307674Snate@binkert.orgEmbeddedPython *EmbeddedPython::importer = NULL;
1317674Snate@binkert.orgPyObject *EmbeddedPython::importerModule = NULL;
1327674Snate@binkert.orgEmbeddedPython::EmbeddedPython(const char *filename, const char *abspath,
1338946Sandreas.hansson@arm.com    const char *modpath, const unsigned char *code, int zlen, int len)
1347674Snate@binkert.org    : filename(filename), abspath(abspath), modpath(modpath), code(code),
1357674Snate@binkert.org      zlen(zlen), len(len)
1367674Snate@binkert.org{
1377674Snate@binkert.org    // if we've added the importer keep track of it because we need it
1387674Snate@binkert.org    // to bootstrap.
1397674Snate@binkert.org    if (string(modpath) == string("importer"))
1407674Snate@binkert.org        importer = this;
1417674Snate@binkert.org    else
1427674Snate@binkert.org        getList().push_back(this);
1437674Snate@binkert.org}
1447674Snate@binkert.org
1457674Snate@binkert.orglist<EmbeddedPython *> &
1467674Snate@binkert.orgEmbeddedPython::getList()
1477674Snate@binkert.org{
1487674Snate@binkert.org    static list<EmbeddedPython *> the_list;
1497674Snate@binkert.org    return the_list;
1507674Snate@binkert.org}
1517674Snate@binkert.org
1527674Snate@binkert.org/*
1537674Snate@binkert.org * Uncompress and unmarshal the code object stored in the
1547674Snate@binkert.org * EmbeddedPython
1557674Snate@binkert.org */
1567674Snate@binkert.orgPyObject *
1577674Snate@binkert.orgEmbeddedPython::getCode() const
1587674Snate@binkert.org{
1597674Snate@binkert.org    Bytef marshalled[len];
1607674Snate@binkert.org    uLongf unzlen = len;
1617674Snate@binkert.org    int ret = uncompress(marshalled, &unzlen, (const Bytef *)code, zlen);
1627674Snate@binkert.org    if (ret != Z_OK)
1637674Snate@binkert.org        panic("Could not uncompress code: %s\n", zError(ret));
1647674Snate@binkert.org    assert(unzlen == (uLongf)len);
1657674Snate@binkert.org
1667674Snate@binkert.org    return PyMarshal_ReadObjectFromString((char *)marshalled, len);
1677674Snate@binkert.org}
1687674Snate@binkert.org
1697674Snate@binkert.orgbool
1707674Snate@binkert.orgEmbeddedPython::addModule() const
1717674Snate@binkert.org{
1727674Snate@binkert.org    PyObject *code = getCode();
1737674Snate@binkert.org    PyObject *result = PyObject_CallMethod(importerModule, PyCC("add_module"),
1747674Snate@binkert.org        PyCC("sssO"), filename, abspath, modpath, code);
1757674Snate@binkert.org    if (!result) {
1767674Snate@binkert.org        PyErr_Print();
1777674Snate@binkert.org        return false;
1787674Snate@binkert.org    }
1797674Snate@binkert.org
1807674Snate@binkert.org    Py_DECREF(result);
1817674Snate@binkert.org    return true;
1827674Snate@binkert.org}
1837674Snate@binkert.org
1845522Snate@binkert.org/*
1855522Snate@binkert.org * Load and initialize all of the python parts of M5, including Swig
1865522Snate@binkert.org * and the embedded module importer.
1875522Snate@binkert.org */
1885522Snate@binkert.orgint
1897674Snate@binkert.orgEmbeddedPython::initAll()
1905522Snate@binkert.org{
1915522Snate@binkert.org    // Load the importer module
1927674Snate@binkert.org    PyObject *code = importer->getCode();
1937674Snate@binkert.org    importerModule = PyImport_ExecCodeModule(PyCC("importer"), code);
1947674Snate@binkert.org    if (!importerModule) {
1955522Snate@binkert.org        PyErr_Print();
1965522Snate@binkert.org        return 1;
1975522Snate@binkert.org    }
1985522Snate@binkert.org
1995522Snate@binkert.org    // Load the rest of the embedded python files into the embedded
2005522Snate@binkert.org    // python importer
2017674Snate@binkert.org    list<EmbeddedPython *>::iterator i = getList().begin();
2027674Snate@binkert.org    list<EmbeddedPython *>::iterator end = getList().end();
2037674Snate@binkert.org    for (; i != end; ++i)
2047674Snate@binkert.org        if (!(*i)->addModule())
2055522Snate@binkert.org            return 1;
2065522Snate@binkert.org
2075522Snate@binkert.org    return 0;
2085522Snate@binkert.org}
2095522Snate@binkert.org
2107674Snate@binkert.orgEmbeddedSwig::EmbeddedSwig(void (*init_func)())
2117674Snate@binkert.org    : initFunc(init_func)
2127674Snate@binkert.org{
2137674Snate@binkert.org    getList().push_back(this);
2147674Snate@binkert.org}
2157674Snate@binkert.org
2167674Snate@binkert.orglist<EmbeddedSwig *> &
2177674Snate@binkert.orgEmbeddedSwig::getList()
2187674Snate@binkert.org{
2197674Snate@binkert.org    static list<EmbeddedSwig *> the_list;
2207674Snate@binkert.org    return the_list;
2217674Snate@binkert.org}
2227674Snate@binkert.org
2237674Snate@binkert.orgvoid
2247674Snate@binkert.orgEmbeddedSwig::initAll()
2257674Snate@binkert.org{
2267674Snate@binkert.org    // initialize SWIG modules.  initSwig() is autogenerated and calls
2277674Snate@binkert.org    // all of the individual swig initialization functions.
2287674Snate@binkert.org    list<EmbeddedSwig *>::iterator i = getList().begin();
2297674Snate@binkert.org    list<EmbeddedSwig *>::iterator end = getList().end();
2307674Snate@binkert.org    for (; i != end; ++i)
2317674Snate@binkert.org        (*i)->initFunc();
2327674Snate@binkert.org}
2337674Snate@binkert.org
2347674Snate@binkert.orgint
2357674Snate@binkert.orginitM5Python()
2367674Snate@binkert.org{
2377674Snate@binkert.org    EmbeddedSwig::initAll();
2387674Snate@binkert.org    return EmbeddedPython::initAll();
2397674Snate@binkert.org}
2407674Snate@binkert.org
2415522Snate@binkert.org/*
2428234Snate@binkert.org * Make the commands array weak so that they can be overridden (used
2438234Snate@binkert.org * by unit tests to specify a different python main function.
2448234Snate@binkert.org */
2458234Snate@binkert.orgconst char * __attribute__((weak)) m5MainCommands[] = {
2468234Snate@binkert.org    "import m5",
2478234Snate@binkert.org    "m5.main()",
2488234Snate@binkert.org    0 // sentinel is required
2498234Snate@binkert.org};
2508234Snate@binkert.org
2518234Snate@binkert.org/*
2525522Snate@binkert.org * Start up the M5 simulator.  This mostly vectors into the python
2535522Snate@binkert.org * main function.
2545522Snate@binkert.org */
2555522Snate@binkert.orgint
2565522Snate@binkert.orgm5Main(int argc, char **argv)
2575522Snate@binkert.org{
2589397Sandreas.hansson@arm.com#if HAVE_PROTOBUF
2599397Sandreas.hansson@arm.com    // Verify that the version of the protobuf library that we linked
2609397Sandreas.hansson@arm.com    // against is compatible with the version of the headers we
2619397Sandreas.hansson@arm.com    // compiled against.
2629397Sandreas.hansson@arm.com    GOOGLE_PROTOBUF_VERIFY_VERSION;
2639397Sandreas.hansson@arm.com#endif
2649397Sandreas.hansson@arm.com
2655522Snate@binkert.org    PySys_SetArgv(argc, argv);
2665522Snate@binkert.org
2675522Snate@binkert.org    // We have to set things up in the special __main__ module
2685522Snate@binkert.org    PyObject *module = PyImport_AddModule(PyCC("__main__"));
2695522Snate@binkert.org    if (module == NULL)
2705522Snate@binkert.org        panic("Could not import __main__");
2715522Snate@binkert.org    PyObject *dict = PyModule_GetDict(module);
2725522Snate@binkert.org
2735522Snate@binkert.org    // import the main m5 module
2745522Snate@binkert.org    PyObject *result;
2758234Snate@binkert.org    const char **command = m5MainCommands;
2768234Snate@binkert.org
2778234Snate@binkert.org    // evaluate each command in the m5MainCommands array (basically a
2788234Snate@binkert.org    // bunch of python statements.
2798234Snate@binkert.org    while (*command) {
2808234Snate@binkert.org        result = PyRun_String(*command, Py_file_input, dict, dict);
2818234Snate@binkert.org        if (!result) {
2828234Snate@binkert.org            PyErr_Print();
2838234Snate@binkert.org            return 1;
2848234Snate@binkert.org        }
2858234Snate@binkert.org        Py_DECREF(result);
2868234Snate@binkert.org
2878234Snate@binkert.org        command++;
2885522Snate@binkert.org    }
2895522Snate@binkert.org
2909397Sandreas.hansson@arm.com#if HAVE_PROTOBUF
2919397Sandreas.hansson@arm.com    google::protobuf::ShutdownProtobufLibrary();
2929397Sandreas.hansson@arm.com#endif
2939397Sandreas.hansson@arm.com
2945522Snate@binkert.org    return 0;
2955522Snate@binkert.org}
2965601Snate@binkert.org
2975601Snate@binkert.orgPyMODINIT_FUNC
2985601Snate@binkert.orginitm5(void)
2995601Snate@binkert.org{
3005601Snate@binkert.org    initM5Python();
3015601Snate@binkert.org    PyImport_ImportModule(PyCC("m5"));
3025601Snate@binkert.org}
303