core.cc revision 14047:91279ed7ec5e
11689SN/A/*
210333Smitch.hayenga@arm.com * Copyright (c) 2017 ARM Limited
39920Syasuko.eckert@amd.com * All rights reserved
47944SGiacomo.Gabrielli@arm.com *
57944SGiacomo.Gabrielli@arm.com * The license below extends only to copyright in the software and shall
67944SGiacomo.Gabrielli@arm.com * not be construed as granting a license to any other intellectual
77944SGiacomo.Gabrielli@arm.com * property including but not limited to intellectual property relating
87944SGiacomo.Gabrielli@arm.com * to a hardware implementation of the functionality of the software
97944SGiacomo.Gabrielli@arm.com * licensed hereunder.  You may use the software subject to the license
107944SGiacomo.Gabrielli@arm.com * terms below provided that you ensure that this notice is replicated
117944SGiacomo.Gabrielli@arm.com * unmodified and in its entirety in all distributions of the software,
127944SGiacomo.Gabrielli@arm.com * modified or unmodified, in source code or in binary form.
137944SGiacomo.Gabrielli@arm.com *
147944SGiacomo.Gabrielli@arm.com * Copyright (c) 2010 Advanced Micro Devices, Inc.
152326SN/A * Copyright (c) 2006 The Regents of The University of Michigan
161689SN/A * All rights reserved.
171689SN/A *
181689SN/A * Redistribution and use in source and binary forms, with or without
191689SN/A * modification, are permitted provided that the following conditions are
201689SN/A * met: redistributions of source code must retain the above copyright
211689SN/A * notice, this list of conditions and the following disclaimer;
221689SN/A * redistributions in binary form must reproduce the above copyright
231689SN/A * notice, this list of conditions and the following disclaimer in the
241689SN/A * documentation and/or other materials provided with the distribution;
251689SN/A * neither the name of the copyright holders nor the names of its
261689SN/A * contributors may be used to endorse or promote products derived from
271689SN/A * this software without specific prior written permission.
281689SN/A *
291689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
301689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
311689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
321689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
331689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
341689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
351689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
361689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
371689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
381689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
391689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402665Ssaidi@eecs.umich.edu *
412665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
422831Sksewell@umich.edu *          Steve Reinhardt
431689SN/A *          Gabe Black
441689SN/A *          Andreas Sandberg
459944Smatt.horsnell@ARM.com */
469944Smatt.horsnell@ARM.com
479944Smatt.horsnell@ARM.com#include "pybind11/pybind11.h"
482064SN/A
491060SN/A#include "python/pybind11/core.hh"
501060SN/A
512292SN/A#include <ctime>
521717SN/A
538232Snate@binkert.org#include "base/addr_range.hh"
544762Snate@binkert.org#include "base/inet.hh"
556221Snate@binkert.org#include "base/logging.hh"
564762Snate@binkert.org#include "base/random.hh"
571060SN/A#include "base/socket.hh"
588737Skoansin.tan@gmail.com#include "base/types.hh"
598737Skoansin.tan@gmail.com#include "sim/core.hh"
608737Skoansin.tan@gmail.com#include "sim/drain.hh"
615529Snate@binkert.org#include "sim/serialize.hh"
621061SN/A#include "sim/sim_object.hh"
632292SN/A
645606Snate@binkert.orgnamespace py = pybind11;
658581Ssteve.reinhardt@amd.com
668581Ssteve.reinhardt@amd.com/** Resolve a SimObject name using the Pybind configuration */
671060SN/Aclass PybindSimObjectResolver : public SimObjectResolver
682292SN/A{
692292SN/A    SimObject *resolveSimObject(const std::string &name);
702292SN/A};
712292SN/A
722292SN/APybindSimObjectResolver pybindSimObjectResolver;
732292SN/A
742326SN/ASimObject *
752292SN/APybindSimObjectResolver::resolveSimObject(const std::string &name)
762292SN/A{
772292SN/A    // TODO
782292SN/A    py::module m = py::module::import("m5.SimObject");
792292SN/A    auto f = m.attr("resolveSimObject");
802292SN/A
815336Shines@cs.fsu.edu    return f(name).cast<SimObject *>();
822292SN/A}
834873Sstever@eecs.umich.edu
842292SN/Aextern const char *compileDate;
852292SN/A
862292SN/A#ifdef DEBUG
874329Sktlim@umich.educonst bool flag_DEBUG = true;
885529Snate@binkert.org#else
894329Sktlim@umich.educonst bool flag_DEBUG = false;
904329Sktlim@umich.edu#endif
914329Sktlim@umich.edu#ifdef NDEBUG
922292SN/Aconst bool flag_NDEBUG = true;
932292SN/A#else
942292SN/Aconst bool flag_NDEBUG = false;
952292SN/A#endif
962292SN/Aconst bool flag_TRACING_ON = TRACING_ON;
972292SN/A
985529Snate@binkert.orgstatic void
991060SN/Ainit_drain(py::module &m_native)
1009920Syasuko.eckert@amd.com{
1019920Syasuko.eckert@amd.com    py::module m = m_native.def_submodule("drain");
1029920Syasuko.eckert@amd.com
1031060SN/A    py::enum_<DrainState>(m, "DrainState")
1041060SN/A        .value("Running", DrainState::Running)
1051060SN/A        .value("Draining", DrainState::Draining)
1062326SN/A        .value("Drained", DrainState::Drained)
1071060SN/A        ;
1081060SN/A
1091060SN/A    py::class_<Drainable, std::unique_ptr<Drainable, py::nodelete>>(
1101060SN/A        m, "Drainable")
1112292SN/A        .def("drainState", &Drainable::drainState)
1126221Snate@binkert.org        .def("notifyFork", &Drainable::notifyFork)
1136221Snate@binkert.org        ;
1146221Snate@binkert.org
1151060SN/A    // The drain manager is a singleton with a private
1161060SN/A    // destructor. Disable deallocation from the Python binding.
1172307SN/A    py::class_<DrainManager, std::unique_ptr<DrainManager, py::nodelete>>(
1182292SN/A        m, "DrainManager")
1192980Sgblack@eecs.umich.edu        .def("tryDrain", &DrainManager::tryDrain)
1202292SN/A        .def("resume", &DrainManager::resume)
1212292SN/A        .def("preCheckpointRestore", &DrainManager::preCheckpointRestore)
1222292SN/A        .def("isDrained", &DrainManager::isDrained)
1232292SN/A        .def("state", &DrainManager::state)
1242292SN/A        .def("signalDrainDone", &DrainManager::signalDrainDone)
1252292SN/A        .def_static("instance", &DrainManager::instance,
1262292SN/A                    py::return_value_policy::reference)
1272292SN/A        ;
1282292SN/A}
1292292SN/A
1306221Snate@binkert.orgstatic void
1316221Snate@binkert.orginit_serialize(py::module &m_native)
1322292SN/A{
1332292SN/A    py::module m = m_native.def_submodule("serialize");
1342292SN/A
1352292SN/A    py::class_<Serializable, std::unique_ptr<Serializable, py::nodelete>>(
1362292SN/A        m, "Serializable")
1372292SN/A        ;
1382292SN/A
1392292SN/A    py::class_<CheckpointIn>(m, "CheckpointIn")
1402292SN/A        ;
1416221Snate@binkert.org}
1426221Snate@binkert.org
1432292SN/Astatic void
1442292SN/Ainit_range(py::module &m_native)
1452831Sksewell@umich.edu{
1462292SN/A    py::module m = m_native.def_submodule("range");
1472292SN/A
1482292SN/A    py::class_<AddrRange>(m, "AddrRange")
1492292SN/A        .def(py::init<>())
1502292SN/A        .def(py::init<Addr &, Addr &>())
1512292SN/A        .def(py::init<const std::vector<AddrRange> &>())
1522292SN/A        .def(py::init<Addr, Addr, uint8_t, uint8_t, uint8_t, uint8_t>())
1532292SN/A
1542292SN/A        .def("__str__", &AddrRange::to_string)
1556221Snate@binkert.org
1566221Snate@binkert.org        .def("interleaved", &AddrRange::interleaved)
1572292SN/A        .def("granularity", &AddrRange::granularity)
1582292SN/A        .def("stripes", &AddrRange::stripes)
1592831Sksewell@umich.edu        .def("size", &AddrRange::size)
1602292SN/A        .def("valid", &AddrRange::valid)
1612292SN/A        .def("start", &AddrRange::start)
1622292SN/A        .def("end", &AddrRange::end)
1632292SN/A        .def("mergesWith", &AddrRange::mergesWith)
1642292SN/A        .def("intersects", &AddrRange::intersects)
1652292SN/A        .def("isSubset", &AddrRange::isSubset)
1662292SN/A        ;
1672292SN/A
1682292SN/A    // We need to make vectors of AddrRange opaque to avoid weird
1692292SN/A    // memory allocation issues in PyBind's STL wrappers.
1702326SN/A    py::bind_vector<std::vector<AddrRange>>(m, "AddrRangeVector");
1712348SN/A
1722326SN/A    m.def("RangeEx", &RangeEx);
1732326SN/A    m.def("RangeIn", &RangeIn);
1742348SN/A    m.def("RangeSize", &RangeSize);
1752292SN/A}
1762292SN/A
1772292SN/Astatic void
1782292SN/Ainit_net(py::module &m_native)
1792292SN/A{
1802292SN/A    py::module m = m_native.def_submodule("net");
1812292SN/A
1821060SN/A    py::class_<Net::EthAddr>(m, "EthAddr")
1831060SN/A        .def(py::init<>())
1841061SN/A        .def(py::init<const std::string &>())
1851060SN/A        ;
1861062SN/A
1871062SN/A    py::class_<Net::IpAddress>(m, "IpAddress")
1882301SN/A        .def(py::init<>())
1891062SN/A        .def(py::init<uint32_t>())
1901062SN/A        ;
1911062SN/A
1921062SN/A    py::class_<Net::IpNetmask, Net::IpAddress>(m, "IpNetmask")
1931062SN/A        .def(py::init<>())
1941062SN/A        .def(py::init<uint32_t, uint8_t>())
1951062SN/A        ;
1961062SN/A
1971062SN/A    py::class_<Net::IpWithPort, Net::IpAddress>(m, "IpWithPort")
1981062SN/A        .def(py::init<>())
1992301SN/A        .def(py::init<uint32_t, uint16_t>())
2002301SN/A        ;
2012301SN/A}
2022301SN/A
2031062SN/Avoid
2041062SN/Apybind_init_core(py::module &m_native)
2051062SN/A{
2061062SN/A    py::module m_core = m_native.def_submodule("core");
2071062SN/A
2081062SN/A    py::class_<Cycles>(m_core, "Cycles")
2091062SN/A        .def(py::init<>())
2101062SN/A        .def(py::init<uint64_t>())
2111062SN/A        .def("__int__", &Cycles::operator uint64_t)
2121062SN/A        .def("__add__", &Cycles::operator+)
2131062SN/A        .def("__sub__", &Cycles::operator-)
2141062SN/A        ;
2151062SN/A
2161062SN/A    py::class_<tm>(m_core, "tm")
2171062SN/A        .def_static("gmtime", [](std::time_t t) { return *std::gmtime(&t); })
2181062SN/A        .def_readwrite("tm_sec", &tm::tm_sec)
2191062SN/A        .def_readwrite("tm_min", &tm::tm_min)
2201062SN/A        .def_readwrite("tm_hour", &tm::tm_hour)
2211062SN/A        .def_readwrite("tm_mday", &tm::tm_mday)
2221062SN/A        .def_readwrite("tm_mon", &tm::tm_mon)
2231062SN/A        .def_readwrite("tm_wday", &tm::tm_wday)
2241062SN/A        .def_readwrite("tm_yday", &tm::tm_yday)
2251062SN/A        .def_readwrite("tm_isdst", &tm::tm_isdst)
2261062SN/A        ;
2271062SN/A
2281062SN/A    py::enum_<Logger::LogLevel>(m_core, "LogLevel")
2291062SN/A        .value("PANIC", Logger::PANIC)
2301062SN/A        .value("FATAL", Logger::FATAL)
2311062SN/A        .value("WARN", Logger::WARN)
2321062SN/A        .value("INFO", Logger::INFO)
2331062SN/A        .value("HACK", Logger::HACK)
2341062SN/A        ;
2351062SN/A
2361062SN/A    m_core
2371062SN/A        .def("setLogLevel", &Logger::setLevel)
2381062SN/A        .def("setOutputDir", &setOutputDir)
2391062SN/A        .def("doExitCleanup", &doExitCleanup)
2401062SN/A
2411062SN/A        .def("disableAllListeners", &ListenSocket::disableAll)
2421062SN/A        .def("listenersDisabled", &ListenSocket::allDisabled)
2431062SN/A        .def("listenersLoopbackOnly", &ListenSocket::loopbackOnly)
2441062SN/A        .def("seedRandom", [](uint64_t seed) { random_mt.init(seed); })
2451062SN/A
2461062SN/A
2471062SN/A        .def("fixClockFrequency", &fixClockFrequency)
2481062SN/A        .def("clockFrequencyFixed", &clockFrequencyFixed)
2491062SN/A
2502361SN/A        .def("setClockFrequency", &setClockFrequency)
2512326SN/A        .def("getClockFrequency", &getClockFrequency)
2522301SN/A        .def("curTick", curTick)
2532301SN/A        ;
2542301SN/A
2552301SN/A    /* TODO: These should be read-only */
2562301SN/A    m_core.attr("compileDate") = py::cast(compileDate);
2572301SN/A
2582326SN/A    m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
2592301SN/A    m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
2602361SN/A    m_core.attr("flag_NDEBUG") = py::cast(flag_NDEBUG);
2612326SN/A    m_core.attr("flag_TRACING_ON") = py::cast(flag_TRACING_ON);
2622307SN/A
2638240Snate@binkert.org    m_core.attr("MaxTick") = py::cast(MaxTick);
2642301SN/A
2652307SN/A    /*
2662301SN/A     * Serialization helpers
2672301SN/A     */
2682301SN/A    m_core
2692301SN/A        .def("serializeAll", &Serializable::serializeAll)
2708240Snate@binkert.org        .def("unserializeGlobals", &Serializable::unserializeGlobals)
2712301SN/A        .def("getCheckpoint", [](const std::string &cpt_dir) {
2722301SN/A            return new CheckpointIn(cpt_dir, pybindSimObjectResolver);
2732301SN/A        })
2742301SN/A
2752301SN/A        ;
2762301SN/A
2772301SN/A
2782326SN/A    init_drain(m_native);
2794762Snate@binkert.org    init_serialize(m_native);
2808240Snate@binkert.org    init_range(m_native);
2812301SN/A    init_net(m_native);
2822301SN/A}
2832301SN/A
2844762Snate@binkert.org