core.cc revision 12011:1279b1d30ccd
1/*
2 * Copyright (c) 2017 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2010 Advanced Micro Devices, Inc.
15 * Copyright (c) 2006 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Nathan Binkert
42 *          Steve Reinhardt
43 *          Gabe Black
44 *          Andreas Sandberg
45 */
46
47#include "pybind11/pybind11.h"
48
49#include <ctime>
50
51#include "base/addr_range.hh"
52#include "base/inet.hh"
53#include "base/misc.hh"
54#include "base/random.hh"
55#include "base/socket.hh"
56#include "base/types.hh"
57#include "sim/core.hh"
58#include "sim/drain.hh"
59#include "sim/serialize.hh"
60#include "sim/sim_object.hh"
61
62namespace py = pybind11;
63
64/** Resolve a SimObject name using the Pybind configuration */
65class PybindSimObjectResolver : public SimObjectResolver
66{
67    SimObject *resolveSimObject(const std::string &name);
68};
69
70PybindSimObjectResolver pybindSimObjectResolver;
71
72SimObject *
73PybindSimObjectResolver::resolveSimObject(const std::string &name)
74{
75    // TODO
76    py::module m = py::module::import("m5.SimObject");
77    auto f = m.attr("resolveSimObject");
78
79    return f(name).cast<SimObject *>();
80}
81
82extern const char *compileDate;
83
84#ifdef DEBUG
85const bool flag_DEBUG = true;
86#else
87const bool flag_DEBUG = false;
88#endif
89#ifdef NDEBUG
90const bool flag_NDEBUG = true;
91#else
92const bool flag_NDEBUG = false;
93#endif
94const bool flag_TRACING_ON = TRACING_ON;
95
96static void
97init_drain(py::module &m_native)
98{
99    py::module m = m_native.def_submodule("drain");
100
101    py::enum_<DrainState>(m, "DrainState")
102        .value("Running", DrainState::Running)
103        .value("Draining", DrainState::Draining)
104        .value("Drained", DrainState::Drained)
105        ;
106
107    py::class_<Drainable, std::unique_ptr<Drainable, py::nodelete>>(
108        m, "Drainable")
109        .def("drainState", &Drainable::drainState)
110        .def("notifyFork", &Drainable::notifyFork)
111        ;
112
113    // The drain manager is a singleton with a private
114    // destructor. Disable deallocation from the Python binding.
115    py::class_<DrainManager, std::unique_ptr<DrainManager, py::nodelete>>(
116        m, "DrainManager")
117        .def("tryDrain", &DrainManager::tryDrain)
118        .def("resume", &DrainManager::resume)
119        .def("preCheckpointRestore", &DrainManager::preCheckpointRestore)
120        .def("isDrained", &DrainManager::isDrained)
121        .def("state", &DrainManager::state)
122        .def("signalDrainDone", &DrainManager::signalDrainDone)
123        .def_static("instance", &DrainManager::instance,
124                    py::return_value_policy::reference)
125        ;
126}
127
128static void
129init_serialize(py::module &m_native)
130{
131    py::module m = m_native.def_submodule("serialize");
132
133    py::class_<Serializable>(m, "Serializable")
134        ;
135
136    py::class_<CheckpointIn>(m, "CheckpointIn")
137        ;
138}
139
140static void
141init_range(py::module &m_native)
142{
143    py::module m = m_native.def_submodule("range");
144
145    py::class_<AddrRange>(m, "AddrRange")
146        .def(py::init<>())
147        .def(py::init<Addr &, Addr &>())
148        .def(py::init<const std::vector<AddrRange> &>())
149        .def(py::init<Addr, Addr, uint8_t, uint8_t, uint8_t, uint8_t>())
150
151        .def("__str__", &AddrRange::to_string)
152
153        .def("interleaved", &AddrRange::interleaved)
154        .def("hashed", &AddrRange::hashed)
155        .def("granularity", &AddrRange::granularity)
156        .def("stripes", &AddrRange::stripes)
157        .def("size", &AddrRange::size)
158        .def("valid", &AddrRange::valid)
159        .def("start", &AddrRange::start)
160        .def("end", &AddrRange::end)
161        .def("mergesWith", &AddrRange::mergesWith)
162        .def("intersects", &AddrRange::intersects)
163        .def("isSubset", &AddrRange::isSubset)
164        ;
165
166    m.def("RangeEx", &RangeEx);
167    m.def("RangeIn", &RangeIn);
168    m.def("RangeSize", &RangeSize);
169}
170
171static void
172init_net(py::module &m_native)
173{
174    py::module m = m_native.def_submodule("net");
175
176    py::class_<Net::EthAddr>(m, "EthAddr")
177        .def(py::init<>())
178        .def(py::init<const std::string &>())
179        ;
180
181    py::class_<Net::IpAddress>(m, "IpAddress")
182        .def(py::init<>())
183        .def(py::init<uint32_t>())
184        ;
185
186    py::class_<Net::IpNetmask, Net::IpAddress>(m, "IpNetmask")
187        .def(py::init<>())
188        .def(py::init<uint32_t, uint8_t>())
189        ;
190
191    py::class_<Net::IpWithPort, Net::IpAddress>(m, "IpWithPort")
192        .def(py::init<>())
193        .def(py::init<uint32_t, uint16_t>())
194        ;
195}
196
197void
198pybind_init_core(py::module &m_native)
199{
200    py::module m_core = m_native.def_submodule("core");
201
202    py::class_<Cycles>(m_core, "Cycles")
203        .def(py::init<>())
204        .def(py::init<uint64_t>())
205        .def("__int__", &Cycles::operator uint64_t)
206        .def("__add__", &Cycles::operator+)
207        .def("__sub__", &Cycles::operator-)
208        ;
209
210    py::class_<tm>(m_core, "tm")
211        .def_static("gmtime", [](std::time_t t) { return *std::gmtime(&t); })
212        .def_readwrite("tm_sec", &tm::tm_sec)
213        .def_readwrite("tm_min", &tm::tm_min)
214        .def_readwrite("tm_hour", &tm::tm_hour)
215        .def_readwrite("tm_mday", &tm::tm_mday)
216        .def_readwrite("tm_mon", &tm::tm_mon)
217        .def_readwrite("tm_wday", &tm::tm_wday)
218        .def_readwrite("tm_yday", &tm::tm_yday)
219        .def_readwrite("tm_isdst", &tm::tm_isdst)
220        ;
221
222    py::enum_<Logger::LogLevel>(m_core, "LogLevel")
223        .value("PANIC", Logger::PANIC)
224        .value("FATAL", Logger::FATAL)
225        .value("WARN", Logger::WARN)
226        .value("INFO", Logger::INFO)
227        .value("HACK", Logger::HACK)
228        ;
229
230    m_core
231        .def("setLogLevel", &Logger::setLevel)
232        .def("setOutputDir", &setOutputDir)
233        .def("doExitCleanup", &doExitCleanup)
234
235        .def("disableAllListeners", &ListenSocket::disableAll)
236        .def("listenersDisabled", &ListenSocket::allDisabled)
237        .def("listenersLoopbackOnly", &ListenSocket::loopbackOnly)
238        .def("seedRandom", [](uint64_t seed) { random_mt.init(seed); })
239
240
241        .def("setClockFrequency", &setClockFrequency)
242        .def("curTick", curTick)
243        ;
244
245    /* TODO: These should be read-only */
246    m_core.attr("compileDate") = py::cast(compileDate);
247
248    m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
249    m_core.attr("flag_DEBUG") = py::cast(flag_DEBUG);
250    m_core.attr("flag_NDEBUG") = py::cast(flag_NDEBUG);
251    m_core.attr("flag_TRACING_ON") = py::cast(flag_TRACING_ON);
252
253    m_core.attr("MaxTick") = py::cast(MaxTick);
254
255    /*
256     * Serialization helpers
257     */
258    m_core
259        .def("serializeAll", &Serializable::serializeAll)
260        .def("unserializeGlobals", &Serializable::unserializeGlobals)
261        .def("getCheckpoint", [](const std::string &cpt_dir) {
262            return new CheckpointIn(cpt_dir, pybindSimObjectResolver);
263        })
264
265        ;
266
267
268    init_drain(m_native);
269    init_serialize(m_native);
270    init_range(m_native);
271    init_net(m_native);
272}
273
274