core.cc revision 12011:1279b1d30ccd
112950Sgabeblack@google.com/*
212950Sgabeblack@google.com * Copyright (c) 2017 ARM Limited
312950Sgabeblack@google.com * All rights reserved
412950Sgabeblack@google.com *
512950Sgabeblack@google.com * The license below extends only to copyright in the software and shall
612950Sgabeblack@google.com * not be construed as granting a license to any other intellectual
712950Sgabeblack@google.com * property including but not limited to intellectual property relating
812950Sgabeblack@google.com * to a hardware implementation of the functionality of the software
912950Sgabeblack@google.com * licensed hereunder.  You may use the software subject to the license
1012950Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated
1112950Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software,
1212950Sgabeblack@google.com * modified or unmodified, in source code or in binary form.
1312950Sgabeblack@google.com *
1412950Sgabeblack@google.com * Copyright (c) 2010 Advanced Micro Devices, Inc.
1512950Sgabeblack@google.com * Copyright (c) 2006 The Regents of The University of Michigan
1612950Sgabeblack@google.com * All rights reserved.
1712950Sgabeblack@google.com *
1812950Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
1912950Sgabeblack@google.com * modification, are permitted provided that the following conditions are
2012950Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
2112950Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
2212950Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
2312950Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
2412950Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
2512950Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
2612950Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
2712950Sgabeblack@google.com * this software without specific prior written permission.
2812950Sgabeblack@google.com *
2912950Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3012950Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3112950Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3212988Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3312988Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3412950Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3513127Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3612950Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3712953Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3812950Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3912950Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4012950Sgabeblack@google.com *
4112950Sgabeblack@google.com * Authors: Nathan Binkert
4212950Sgabeblack@google.com *          Steve Reinhardt
4312950Sgabeblack@google.com *          Gabe Black
4412950Sgabeblack@google.com *          Andreas Sandberg
4512950Sgabeblack@google.com */
4612950Sgabeblack@google.com
4712950Sgabeblack@google.com#include "pybind11/pybind11.h"
4812950Sgabeblack@google.com
4912950Sgabeblack@google.com#include <ctime>
5012950Sgabeblack@google.com
5112950Sgabeblack@google.com#include "base/addr_range.hh"
5212950Sgabeblack@google.com#include "base/inet.hh"
5312950Sgabeblack@google.com#include "base/misc.hh"
5412950Sgabeblack@google.com#include "base/random.hh"
5512950Sgabeblack@google.com#include "base/socket.hh"
5612950Sgabeblack@google.com#include "base/types.hh"
5712950Sgabeblack@google.com#include "sim/core.hh"
5812950Sgabeblack@google.com#include "sim/drain.hh"
5912950Sgabeblack@google.com#include "sim/serialize.hh"
6012950Sgabeblack@google.com#include "sim/sim_object.hh"
6112950Sgabeblack@google.com
6212950Sgabeblack@google.comnamespace py = pybind11;
6312950Sgabeblack@google.com
6412950Sgabeblack@google.com/** Resolve a SimObject name using the Pybind configuration */
6512950Sgabeblack@google.comclass PybindSimObjectResolver : public SimObjectResolver
6612950Sgabeblack@google.com{
6712950Sgabeblack@google.com    SimObject *resolveSimObject(const std::string &name);
6812950Sgabeblack@google.com};
6912950Sgabeblack@google.com
7012950Sgabeblack@google.comPybindSimObjectResolver pybindSimObjectResolver;
7113127Sgabeblack@google.com
7213127Sgabeblack@google.comSimObject *
7313127Sgabeblack@google.comPybindSimObjectResolver::resolveSimObject(const std::string &name)
7413127Sgabeblack@google.com{
7513127Sgabeblack@google.com    // TODO
7613127Sgabeblack@google.com    py::module m = py::module::import("m5.SimObject");
7713127Sgabeblack@google.com    auto f = m.attr("resolveSimObject");
7813127Sgabeblack@google.com
7913127Sgabeblack@google.com    return f(name).cast<SimObject *>();
8013127Sgabeblack@google.com}
8113127Sgabeblack@google.com
8213127Sgabeblack@google.comextern const char *compileDate;
8312950Sgabeblack@google.com
8412950Sgabeblack@google.com#ifdef DEBUG
8512955Sgabeblack@google.comconst bool flag_DEBUG = true;
8612950Sgabeblack@google.com#else
8712955Sgabeblack@google.comconst bool flag_DEBUG = false;
8813085Sgabeblack@google.com#endif
8912950Sgabeblack@google.com#ifdef NDEBUG
9012950Sgabeblack@google.comconst bool flag_NDEBUG = true;
9112950Sgabeblack@google.com#else
9212950Sgabeblack@google.comconst bool flag_NDEBUG = false;
9312950Sgabeblack@google.com#endif
9413045Sgabeblack@google.comconst bool flag_TRACING_ON = TRACING_ON;
9513045Sgabeblack@google.com
9612950Sgabeblack@google.comstatic void
9712950Sgabeblack@google.cominit_drain(py::module &m_native)
9812950Sgabeblack@google.com{
9912950Sgabeblack@google.com    py::module m = m_native.def_submodule("drain");
10012950Sgabeblack@google.com
10112950Sgabeblack@google.com    py::enum_<DrainState>(m, "DrainState")
10212950Sgabeblack@google.com        .value("Running", DrainState::Running)
10312950Sgabeblack@google.com        .value("Draining", DrainState::Draining)
10412950Sgabeblack@google.com        .value("Drained", DrainState::Drained)
10513045Sgabeblack@google.com        ;
10612955Sgabeblack@google.com
10712953Sgabeblack@google.com    py::class_<Drainable, std::unique_ptr<Drainable, py::nodelete>>(
10812953Sgabeblack@google.com        m, "Drainable")
10912953Sgabeblack@google.com        .def("drainState", &Drainable::drainState)
11013126Sgabeblack@google.com        .def("notifyFork", &Drainable::notifyFork)
11113127Sgabeblack@google.com        ;
11213127Sgabeblack@google.com
11313127Sgabeblack@google.com    // The drain manager is a singleton with a private
11413127Sgabeblack@google.com    // destructor. Disable deallocation from the Python binding.
11513126Sgabeblack@google.com    py::class_<DrainManager, std::unique_ptr<DrainManager, py::nodelete>>(
11613127Sgabeblack@google.com        m, "DrainManager")
11712955Sgabeblack@google.com        .def("tryDrain", &DrainManager::tryDrain)
11812950Sgabeblack@google.com        .def("resume", &DrainManager::resume)
11912955Sgabeblack@google.com        .def("preCheckpointRestore", &DrainManager::preCheckpointRestore)
12012950Sgabeblack@google.com        .def("isDrained", &DrainManager::isDrained)
12112950Sgabeblack@google.com        .def("state", &DrainManager::state)
12212950Sgabeblack@google.com        .def("signalDrainDone", &DrainManager::signalDrainDone)
12312950Sgabeblack@google.com        .def_static("instance", &DrainManager::instance,
12412950Sgabeblack@google.com                    py::return_value_policy::reference)
12512952Sgabeblack@google.com        ;
12612950Sgabeblack@google.com}
12712950Sgabeblack@google.com
12812950Sgabeblack@google.comstatic void
12912955Sgabeblack@google.cominit_serialize(py::module &m_native)
13012955Sgabeblack@google.com{
13112950Sgabeblack@google.com    py::module m = m_native.def_submodule("serialize");
13212950Sgabeblack@google.com
13312950Sgabeblack@google.com    py::class_<Serializable>(m, "Serializable")
13412950Sgabeblack@google.com        ;
13512950Sgabeblack@google.com
13612950Sgabeblack@google.com    py::class_<CheckpointIn>(m, "CheckpointIn")
13712950Sgabeblack@google.com        ;
13812950Sgabeblack@google.com}
13912950Sgabeblack@google.com
14012950Sgabeblack@google.comstatic void
14112984Sgabeblack@google.cominit_range(py::module &m_native)
14212984Sgabeblack@google.com{
14312984Sgabeblack@google.com    py::module m = m_native.def_submodule("range");
14412984Sgabeblack@google.com
14512984Sgabeblack@google.com    py::class_<AddrRange>(m, "AddrRange")
14612984Sgabeblack@google.com        .def(py::init<>())
14712950Sgabeblack@google.com        .def(py::init<Addr &, Addr &>())
14812950Sgabeblack@google.com        .def(py::init<const std::vector<AddrRange> &>())
14912950Sgabeblack@google.com        .def(py::init<Addr, Addr, uint8_t, uint8_t, uint8_t, uint8_t>())
15012950Sgabeblack@google.com
15112950Sgabeblack@google.com        .def("__str__", &AddrRange::to_string)
15212950Sgabeblack@google.com
15312950Sgabeblack@google.com        .def("interleaved", &AddrRange::interleaved)
15412950Sgabeblack@google.com        .def("hashed", &AddrRange::hashed)
15512950Sgabeblack@google.com        .def("granularity", &AddrRange::granularity)
15612950Sgabeblack@google.com        .def("stripes", &AddrRange::stripes)
15712950Sgabeblack@google.com        .def("size", &AddrRange::size)
15812950Sgabeblack@google.com        .def("valid", &AddrRange::valid)
15912950Sgabeblack@google.com        .def("start", &AddrRange::start)
16012950Sgabeblack@google.com        .def("end", &AddrRange::end)
16112950Sgabeblack@google.com        .def("mergesWith", &AddrRange::mergesWith)
16212950Sgabeblack@google.com        .def("intersects", &AddrRange::intersects)
16312950Sgabeblack@google.com        .def("isSubset", &AddrRange::isSubset)
16412950Sgabeblack@google.com        ;
16512950Sgabeblack@google.com
16612950Sgabeblack@google.com    m.def("RangeEx", &RangeEx);
16712950Sgabeblack@google.com    m.def("RangeIn", &RangeIn);
16812950Sgabeblack@google.com    m.def("RangeSize", &RangeSize);
16912950Sgabeblack@google.com}
17012950Sgabeblack@google.com
17112950Sgabeblack@google.comstatic void
17212950Sgabeblack@google.cominit_net(py::module &m_native)
17312950Sgabeblack@google.com{
17412950Sgabeblack@google.com    py::module m = m_native.def_submodule("net");
17512950Sgabeblack@google.com
17612950Sgabeblack@google.com    py::class_<Net::EthAddr>(m, "EthAddr")
17712955Sgabeblack@google.com        .def(py::init<>())
17812950Sgabeblack@google.com        .def(py::init<const std::string &>())
17912950Sgabeblack@google.com        ;
18012950Sgabeblack@google.com
18112950Sgabeblack@google.com    py::class_<Net::IpAddress>(m, "IpAddress")
18212950Sgabeblack@google.com        .def(py::init<>())
18312950Sgabeblack@google.com        .def(py::init<uint32_t>())
18412950Sgabeblack@google.com        ;
18512950Sgabeblack@google.com
18612950Sgabeblack@google.com    py::class_<Net::IpNetmask, Net::IpAddress>(m, "IpNetmask")
18712950Sgabeblack@google.com        .def(py::init<>())
18812950Sgabeblack@google.com        .def(py::init<uint32_t, uint8_t>())
18912950Sgabeblack@google.com        ;
19012950Sgabeblack@google.com
19112950Sgabeblack@google.com    py::class_<Net::IpWithPort, Net::IpAddress>(m, "IpWithPort")
19212950Sgabeblack@google.com        .def(py::init<>())
19312950Sgabeblack@google.com        .def(py::init<uint32_t, uint16_t>())
19412950Sgabeblack@google.com        ;
19512950Sgabeblack@google.com}
19612950Sgabeblack@google.com
19712950Sgabeblack@google.comvoid
19812950Sgabeblack@google.compybind_init_core(py::module &m_native)
19912950Sgabeblack@google.com{
20012950Sgabeblack@google.com    py::module m_core = m_native.def_submodule("core");
20112950Sgabeblack@google.com
20212950Sgabeblack@google.com    py::class_<Cycles>(m_core, "Cycles")
20312950Sgabeblack@google.com        .def(py::init<>())
20412950Sgabeblack@google.com        .def(py::init<uint64_t>())
20512950Sgabeblack@google.com        .def("__int__", &Cycles::operator uint64_t)
20612950Sgabeblack@google.com        .def("__add__", &Cycles::operator+)
20712950Sgabeblack@google.com        .def("__sub__", &Cycles::operator-)
20812950Sgabeblack@google.com        ;
20912950Sgabeblack@google.com
21012950Sgabeblack@google.com    py::class_<tm>(m_core, "tm")
21112950Sgabeblack@google.com        .def_static("gmtime", [](std::time_t t) { return *std::gmtime(&t); })
21212950Sgabeblack@google.com        .def_readwrite("tm_sec", &tm::tm_sec)
21312950Sgabeblack@google.com        .def_readwrite("tm_min", &tm::tm_min)
21412950Sgabeblack@google.com        .def_readwrite("tm_hour", &tm::tm_hour)
21512950Sgabeblack@google.com        .def_readwrite("tm_mday", &tm::tm_mday)
21612950Sgabeblack@google.com        .def_readwrite("tm_mon", &tm::tm_mon)
21712950Sgabeblack@google.com        .def_readwrite("tm_wday", &tm::tm_wday)
21812950Sgabeblack@google.com        .def_readwrite("tm_yday", &tm::tm_yday)
21912950Sgabeblack@google.com        .def_readwrite("tm_isdst", &tm::tm_isdst)
22012950Sgabeblack@google.com        ;
22112950Sgabeblack@google.com
22212950Sgabeblack@google.com    py::enum_<Logger::LogLevel>(m_core, "LogLevel")
22312950Sgabeblack@google.com        .value("PANIC", Logger::PANIC)
22412950Sgabeblack@google.com        .value("FATAL", Logger::FATAL)
22512950Sgabeblack@google.com        .value("WARN", Logger::WARN)
22612950Sgabeblack@google.com        .value("INFO", Logger::INFO)
22712950Sgabeblack@google.com        .value("HACK", Logger::HACK)
22812950Sgabeblack@google.com        ;
22912950Sgabeblack@google.com
23012950Sgabeblack@google.com    m_core
23112950Sgabeblack@google.com        .def("setLogLevel", &Logger::setLevel)
23212950Sgabeblack@google.com        .def("setOutputDir", &setOutputDir)
23312950Sgabeblack@google.com        .def("doExitCleanup", &doExitCleanup)
23412950Sgabeblack@google.com
23512950Sgabeblack@google.com        .def("disableAllListeners", &ListenSocket::disableAll)
23612950Sgabeblack@google.com        .def("listenersDisabled", &ListenSocket::allDisabled)
23712950Sgabeblack@google.com        .def("listenersLoopbackOnly", &ListenSocket::loopbackOnly)
23812950Sgabeblack@google.com        .def("seedRandom", [](uint64_t seed) { random_mt.init(seed); })
23912950Sgabeblack@google.com
24012950Sgabeblack@google.com
24112950Sgabeblack@google.com        .def("setClockFrequency", &setClockFrequency)
24212950Sgabeblack@google.com        .def("curTick", curTick)
24312950Sgabeblack@google.com        ;
24412950Sgabeblack@google.com
24512950Sgabeblack@google.com    /* TODO: These should be read-only */
24612955Sgabeblack@google.com    m_core.attr("compileDate") = py::cast(compileDate);
24712955Sgabeblack@google.com
24812955Sgabeblack@google.com    m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
24912955Sgabeblack@google.com    m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
25012955Sgabeblack@google.com    m_core.attr("flag_NDEBUG") = py::cast(flag_NDEBUG);
25112955Sgabeblack@google.com    m_core.attr("flag_TRACING_ON") = py::cast(flag_TRACING_ON);
25212955Sgabeblack@google.com
25312988Sgabeblack@google.com    m_core.attr("MaxTick") = py::cast(MaxTick);
25412955Sgabeblack@google.com
25512988Sgabeblack@google.com    /*
25612988Sgabeblack@google.com     * Serialization helpers
25712955Sgabeblack@google.com     */
25812955Sgabeblack@google.com    m_core
25912955Sgabeblack@google.com        .def("serializeAll", &Serializable::serializeAll)
26012955Sgabeblack@google.com        .def("unserializeGlobals", &Serializable::unserializeGlobals)
26113127Sgabeblack@google.com        .def("getCheckpoint", [](const std::string &cpt_dir) {
26213127Sgabeblack@google.com            return new CheckpointIn(cpt_dir, pybindSimObjectResolver);
26313127Sgabeblack@google.com        })
26413127Sgabeblack@google.com
26513127Sgabeblack@google.com        ;
26613127Sgabeblack@google.com
26713127Sgabeblack@google.com
26813127Sgabeblack@google.com    init_drain(m_native);
26913127Sgabeblack@google.com    init_serialize(m_native);
27013127Sgabeblack@google.com    init_range(m_native);
27113127Sgabeblack@google.com    init_net(m_native);
27213127Sgabeblack@google.com}
27313127Sgabeblack@google.com
27413127Sgabeblack@google.com