core.cc revision 12036
112027Sjungma@eit.uni-kl.de/* 212027Sjungma@eit.uni-kl.de * Copyright (c) 2017 ARM Limited 312027Sjungma@eit.uni-kl.de * All rights reserved 412027Sjungma@eit.uni-kl.de * 512027Sjungma@eit.uni-kl.de * The license below extends only to copyright in the software and shall 612027Sjungma@eit.uni-kl.de * not be construed as granting a license to any other intellectual 712027Sjungma@eit.uni-kl.de * property including but not limited to intellectual property relating 812027Sjungma@eit.uni-kl.de * to a hardware implementation of the functionality of the software 912027Sjungma@eit.uni-kl.de * licensed hereunder. You may use the software subject to the license 1012027Sjungma@eit.uni-kl.de * terms below provided that you ensure that this notice is replicated 1112027Sjungma@eit.uni-kl.de * unmodified and in its entirety in all distributions of the software, 1212027Sjungma@eit.uni-kl.de * modified or unmodified, in source code or in binary form. 1312027Sjungma@eit.uni-kl.de * 1412027Sjungma@eit.uni-kl.de * Copyright (c) 2010 Advanced Micro Devices, Inc. 1512027Sjungma@eit.uni-kl.de * Copyright (c) 2006 The Regents of The University of Michigan 1612027Sjungma@eit.uni-kl.de * All rights reserved. 1712027Sjungma@eit.uni-kl.de * 1812027Sjungma@eit.uni-kl.de * Redistribution and use in source and binary forms, with or without 1912027Sjungma@eit.uni-kl.de * modification, are permitted provided that the following conditions are 2012027Sjungma@eit.uni-kl.de * met: redistributions of source code must retain the above copyright 2112027Sjungma@eit.uni-kl.de * notice, this list of conditions and the following disclaimer; 2212027Sjungma@eit.uni-kl.de * redistributions in binary form must reproduce the above copyright 2312027Sjungma@eit.uni-kl.de * notice, this list of conditions and the following disclaimer in the 2412027Sjungma@eit.uni-kl.de * documentation and/or other materials provided with the distribution; 2512027Sjungma@eit.uni-kl.de * neither the name of the copyright holders nor the names of its 2612027Sjungma@eit.uni-kl.de * contributors may be used to endorse or promote products derived from 2712027Sjungma@eit.uni-kl.de * this software without specific prior written permission. 2812027Sjungma@eit.uni-kl.de * 2912027Sjungma@eit.uni-kl.de * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 3012027Sjungma@eit.uni-kl.de * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3112027Sjungma@eit.uni-kl.de * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 3212027Sjungma@eit.uni-kl.de * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3312027Sjungma@eit.uni-kl.de * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3412027Sjungma@eit.uni-kl.de * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3512027Sjungma@eit.uni-kl.de * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3612027Sjungma@eit.uni-kl.de * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3712027Sjungma@eit.uni-kl.de * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3812027Sjungma@eit.uni-kl.de * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3912027Sjungma@eit.uni-kl.de * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 4012027Sjungma@eit.uni-kl.de * 4112027Sjungma@eit.uni-kl.de * Authors: Nathan Binkert 4212027Sjungma@eit.uni-kl.de * Steve Reinhardt 4312027Sjungma@eit.uni-kl.de * Gabe Black 4412027Sjungma@eit.uni-kl.de * Andreas Sandberg 4512027Sjungma@eit.uni-kl.de */ 4612027Sjungma@eit.uni-kl.de 4712027Sjungma@eit.uni-kl.de#include "pybind11/pybind11.h" 4812027Sjungma@eit.uni-kl.de 4912027Sjungma@eit.uni-kl.de#include "python/pybind11/core.hh" 5012027Sjungma@eit.uni-kl.de 5112027Sjungma@eit.uni-kl.de#include <ctime> 5212027Sjungma@eit.uni-kl.de 5312027Sjungma@eit.uni-kl.de#include "base/addr_range.hh" 5412027Sjungma@eit.uni-kl.de#include "base/inet.hh" 5512027Sjungma@eit.uni-kl.de#include "base/misc.hh" 5612027Sjungma@eit.uni-kl.de#include "base/random.hh" 5712027Sjungma@eit.uni-kl.de#include "base/socket.hh" 5812027Sjungma@eit.uni-kl.de#include "base/types.hh" 5912027Sjungma@eit.uni-kl.de#include "sim/core.hh" 6012027Sjungma@eit.uni-kl.de#include "sim/drain.hh" 6112027Sjungma@eit.uni-kl.de#include "sim/serialize.hh" 6212027Sjungma@eit.uni-kl.de#include "sim/sim_object.hh" 6312027Sjungma@eit.uni-kl.de 6412027Sjungma@eit.uni-kl.denamespace py = pybind11; 6512027Sjungma@eit.uni-kl.de 6612027Sjungma@eit.uni-kl.de/** Resolve a SimObject name using the Pybind configuration */ 6712027Sjungma@eit.uni-kl.declass PybindSimObjectResolver : public SimObjectResolver 6812027Sjungma@eit.uni-kl.de{ 6912027Sjungma@eit.uni-kl.de SimObject *resolveSimObject(const std::string &name); 7012027Sjungma@eit.uni-kl.de}; 7112027Sjungma@eit.uni-kl.de 7212027Sjungma@eit.uni-kl.dePybindSimObjectResolver pybindSimObjectResolver; 7312027Sjungma@eit.uni-kl.de 7412027Sjungma@eit.uni-kl.deSimObject * 7512027Sjungma@eit.uni-kl.dePybindSimObjectResolver::resolveSimObject(const std::string &name) 7612027Sjungma@eit.uni-kl.de{ 7712027Sjungma@eit.uni-kl.de // TODO 7812027Sjungma@eit.uni-kl.de py::module m = py::module::import("m5.SimObject"); 7912027Sjungma@eit.uni-kl.de auto f = m.attr("resolveSimObject"); 8012027Sjungma@eit.uni-kl.de 8112027Sjungma@eit.uni-kl.de return f(name).cast<SimObject *>(); 8212027Sjungma@eit.uni-kl.de} 8312027Sjungma@eit.uni-kl.de 8412027Sjungma@eit.uni-kl.deextern const char *compileDate; 8512027Sjungma@eit.uni-kl.de 8612027Sjungma@eit.uni-kl.de#ifdef DEBUG 8712027Sjungma@eit.uni-kl.deconst bool flag_DEBUG = true; 8812027Sjungma@eit.uni-kl.de#else 8912027Sjungma@eit.uni-kl.deconst bool flag_DEBUG = false; 9012027Sjungma@eit.uni-kl.de#endif 9112027Sjungma@eit.uni-kl.de#ifdef NDEBUG 9212027Sjungma@eit.uni-kl.deconst bool flag_NDEBUG = true; 9312027Sjungma@eit.uni-kl.de#else 9412027Sjungma@eit.uni-kl.deconst bool flag_NDEBUG = false; 9512027Sjungma@eit.uni-kl.de#endif 9612027Sjungma@eit.uni-kl.deconst bool flag_TRACING_ON = TRACING_ON; 9712027Sjungma@eit.uni-kl.de 9812027Sjungma@eit.uni-kl.destatic void 9912027Sjungma@eit.uni-kl.deinit_drain(py::module &m_native) 10012027Sjungma@eit.uni-kl.de{ 10112027Sjungma@eit.uni-kl.de py::module m = m_native.def_submodule("drain"); 10212027Sjungma@eit.uni-kl.de 10312027Sjungma@eit.uni-kl.de py::enum_<DrainState>(m, "DrainState") 10412027Sjungma@eit.uni-kl.de .value("Running", DrainState::Running) 10512027Sjungma@eit.uni-kl.de .value("Draining", DrainState::Draining) 10612027Sjungma@eit.uni-kl.de .value("Drained", DrainState::Drained) 10712027Sjungma@eit.uni-kl.de ; 10812027Sjungma@eit.uni-kl.de 10912027Sjungma@eit.uni-kl.de py::class_<Drainable, std::unique_ptr<Drainable, py::nodelete>>( 11012027Sjungma@eit.uni-kl.de m, "Drainable") 11112027Sjungma@eit.uni-kl.de .def("drainState", &Drainable::drainState) 11212027Sjungma@eit.uni-kl.de .def("notifyFork", &Drainable::notifyFork) 11312027Sjungma@eit.uni-kl.de ; 11412027Sjungma@eit.uni-kl.de 11512027Sjungma@eit.uni-kl.de // The drain manager is a singleton with a private 11612027Sjungma@eit.uni-kl.de // destructor. Disable deallocation from the Python binding. 11712027Sjungma@eit.uni-kl.de py::class_<DrainManager, std::unique_ptr<DrainManager, py::nodelete>>( 11812027Sjungma@eit.uni-kl.de m, "DrainManager") 11912027Sjungma@eit.uni-kl.de .def("tryDrain", &DrainManager::tryDrain) 12012027Sjungma@eit.uni-kl.de .def("resume", &DrainManager::resume) 12112027Sjungma@eit.uni-kl.de .def("preCheckpointRestore", &DrainManager::preCheckpointRestore) 12212027Sjungma@eit.uni-kl.de .def("isDrained", &DrainManager::isDrained) 12312027Sjungma@eit.uni-kl.de .def("state", &DrainManager::state) 12412027Sjungma@eit.uni-kl.de .def("signalDrainDone", &DrainManager::signalDrainDone) 12512027Sjungma@eit.uni-kl.de .def_static("instance", &DrainManager::instance, 12612027Sjungma@eit.uni-kl.de py::return_value_policy::reference) 12712027Sjungma@eit.uni-kl.de ; 12812027Sjungma@eit.uni-kl.de} 12912027Sjungma@eit.uni-kl.de 13012027Sjungma@eit.uni-kl.destatic void 13112027Sjungma@eit.uni-kl.deinit_serialize(py::module &m_native) 13212027Sjungma@eit.uni-kl.de{ 13312027Sjungma@eit.uni-kl.de py::module m = m_native.def_submodule("serialize"); 13412027Sjungma@eit.uni-kl.de 13512027Sjungma@eit.uni-kl.de py::class_<Serializable, std::unique_ptr<Serializable, py::nodelete>>( 13612027Sjungma@eit.uni-kl.de m, "Serializable") 13712027Sjungma@eit.uni-kl.de ; 13812027Sjungma@eit.uni-kl.de 13912027Sjungma@eit.uni-kl.de py::class_<CheckpointIn>(m, "CheckpointIn") 14012027Sjungma@eit.uni-kl.de ; 141} 142 143static void 144init_range(py::module &m_native) 145{ 146 py::module m = m_native.def_submodule("range"); 147 148 py::class_<AddrRange>(m, "AddrRange") 149 .def(py::init<>()) 150 .def(py::init<Addr &, Addr &>()) 151 .def(py::init<const std::vector<AddrRange> &>()) 152 .def(py::init<Addr, Addr, uint8_t, uint8_t, uint8_t, uint8_t>()) 153 154 .def("__str__", &AddrRange::to_string) 155 156 .def("interleaved", &AddrRange::interleaved) 157 .def("hashed", &AddrRange::hashed) 158 .def("granularity", &AddrRange::granularity) 159 .def("stripes", &AddrRange::stripes) 160 .def("size", &AddrRange::size) 161 .def("valid", &AddrRange::valid) 162 .def("start", &AddrRange::start) 163 .def("end", &AddrRange::end) 164 .def("mergesWith", &AddrRange::mergesWith) 165 .def("intersects", &AddrRange::intersects) 166 .def("isSubset", &AddrRange::isSubset) 167 ; 168 169 // We need to make vectors of AddrRange opaque to avoid weird 170 // memory allocation issues in PyBind's STL wrappers. 171 py::bind_vector<std::vector<AddrRange>>(m, "AddrRangeVector"); 172 173 m.def("RangeEx", &RangeEx); 174 m.def("RangeIn", &RangeIn); 175 m.def("RangeSize", &RangeSize); 176} 177 178static void 179init_net(py::module &m_native) 180{ 181 py::module m = m_native.def_submodule("net"); 182 183 py::class_<Net::EthAddr>(m, "EthAddr") 184 .def(py::init<>()) 185 .def(py::init<const std::string &>()) 186 ; 187 188 py::class_<Net::IpAddress>(m, "IpAddress") 189 .def(py::init<>()) 190 .def(py::init<uint32_t>()) 191 ; 192 193 py::class_<Net::IpNetmask, Net::IpAddress>(m, "IpNetmask") 194 .def(py::init<>()) 195 .def(py::init<uint32_t, uint8_t>()) 196 ; 197 198 py::class_<Net::IpWithPort, Net::IpAddress>(m, "IpWithPort") 199 .def(py::init<>()) 200 .def(py::init<uint32_t, uint16_t>()) 201 ; 202} 203 204void 205pybind_init_core(py::module &m_native) 206{ 207 py::module m_core = m_native.def_submodule("core"); 208 209 py::class_<Cycles>(m_core, "Cycles") 210 .def(py::init<>()) 211 .def(py::init<uint64_t>()) 212 .def("__int__", &Cycles::operator uint64_t) 213 .def("__add__", &Cycles::operator+) 214 .def("__sub__", &Cycles::operator-) 215 ; 216 217 py::class_<tm>(m_core, "tm") 218 .def_static("gmtime", [](std::time_t t) { return *std::gmtime(&t); }) 219 .def_readwrite("tm_sec", &tm::tm_sec) 220 .def_readwrite("tm_min", &tm::tm_min) 221 .def_readwrite("tm_hour", &tm::tm_hour) 222 .def_readwrite("tm_mday", &tm::tm_mday) 223 .def_readwrite("tm_mon", &tm::tm_mon) 224 .def_readwrite("tm_wday", &tm::tm_wday) 225 .def_readwrite("tm_yday", &tm::tm_yday) 226 .def_readwrite("tm_isdst", &tm::tm_isdst) 227 ; 228 229 py::enum_<Logger::LogLevel>(m_core, "LogLevel") 230 .value("PANIC", Logger::PANIC) 231 .value("FATAL", Logger::FATAL) 232 .value("WARN", Logger::WARN) 233 .value("INFO", Logger::INFO) 234 .value("HACK", Logger::HACK) 235 ; 236 237 m_core 238 .def("setLogLevel", &Logger::setLevel) 239 .def("setOutputDir", &setOutputDir) 240 .def("doExitCleanup", &doExitCleanup) 241 242 .def("disableAllListeners", &ListenSocket::disableAll) 243 .def("listenersDisabled", &ListenSocket::allDisabled) 244 .def("listenersLoopbackOnly", &ListenSocket::loopbackOnly) 245 .def("seedRandom", [](uint64_t seed) { random_mt.init(seed); }) 246 247 248 .def("setClockFrequency", &setClockFrequency) 249 .def("curTick", curTick) 250 ; 251 252 /* TODO: These should be read-only */ 253 m_core.attr("compileDate") = py::cast(compileDate); 254 255 m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG); 256 m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG); 257 m_core.attr("flag_NDEBUG") = py::cast(flag_NDEBUG); 258 m_core.attr("flag_TRACING_ON") = py::cast(flag_TRACING_ON); 259 260 m_core.attr("MaxTick") = py::cast(MaxTick); 261 262 /* 263 * Serialization helpers 264 */ 265 m_core 266 .def("serializeAll", &Serializable::serializeAll) 267 .def("unserializeGlobals", &Serializable::unserializeGlobals) 268 .def("getCheckpoint", [](const std::string &cpt_dir) { 269 return new CheckpointIn(cpt_dir, pybindSimObjectResolver); 270 }) 271 272 ; 273 274 275 init_drain(m_native); 276 init_serialize(m_native); 277 init_range(m_native); 278 init_net(m_native); 279} 280 281