kernel.cc revision 13188
1/* 2 * Copyright 2018 Google, Inc. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer; 8 * redistributions in binary form must reproduce the above copyright 9 * notice, this list of conditions and the following disclaimer in the 10 * documentation and/or other materials provided with the distribution; 11 * neither the name of the copyright holders nor the names of its 12 * contributors may be used to endorse or promote products derived from 13 * this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * Authors: Gabe Black 28 */ 29 30#include "systemc/core/kernel.hh" 31 32#include "base/logging.hh" 33#include "systemc/core/channel.hh" 34#include "systemc/core/module.hh" 35#include "systemc/core/scheduler.hh" 36 37namespace sc_gem5 38{ 39 40namespace 41{ 42 43bool scMainDone = false; 44bool stopAfterCallbacks = false; 45bool startComplete = false; 46bool endComplete = false; 47 48sc_core::sc_status _status = sc_core::SC_ELABORATION; 49 50} // anonymous namespace 51 52bool Kernel::startOfSimulationComplete() { return startComplete; } 53bool Kernel::endOfSimulationComplete() { return endComplete; } 54 55bool Kernel::scMainFinished() { return scMainDone; } 56void Kernel::scMainFinished(bool finished) { scMainDone = finished; } 57 58sc_core::sc_status Kernel::status() { return _status; } 59void Kernel::status(sc_core::sc_status s) { _status = s; } 60 61Kernel::Kernel(Params *params) : 62 SimObject(params), t0Event(this, false, EventBase::Default_Pri - 1) 63{ 64 // Install ourselves as the scheduler's event manager. 65 ::sc_gem5::scheduler.setEventQueue(eventQueue()); 66} 67 68void 69Kernel::init() 70{ 71 if (scMainDone) 72 return; 73 74 if (stopAfterCallbacks) 75 fatal("Simulation called sc_stop during elaboration.\n"); 76 77 status(::sc_core::SC_BEFORE_END_OF_ELABORATION); 78 for (auto m: sc_gem5::allModules) { 79 callbackModule(m); 80 m->sc_mod()->before_end_of_elaboration(); 81 for (auto p: m->ports) 82 p->before_end_of_elaboration(); 83 for (auto e: m->exports) 84 e->before_end_of_elaboration(); 85 } 86 callbackModule(nullptr); 87 for (auto c: sc_gem5::allChannels) 88 c->sc_chan()->before_end_of_elaboration(); 89} 90 91void 92Kernel::regStats() 93{ 94 if (scMainDone || stopAfterCallbacks) 95 return; 96 97 try { 98 for (auto m: sc_gem5::allModules) 99 for (auto p: m->ports) 100 p->_gem5Finalize(); 101 102 status(::sc_core::SC_END_OF_ELABORATION); 103 for (auto m: sc_gem5::allModules) { 104 callbackModule(m); 105 m->sc_mod()->end_of_elaboration(); 106 for (auto p: m->ports) 107 p->end_of_elaboration(); 108 for (auto e: m->exports) 109 e->end_of_elaboration(); 110 } 111 callbackModule(nullptr); 112 for (auto c: sc_gem5::allChannels) 113 c->sc_chan()->end_of_elaboration(); 114 } catch (...) { 115 ::sc_gem5::scheduler.throwToScMain(); 116 } 117 118 ::sc_gem5::scheduler.elaborationDone(true); 119} 120 121void 122Kernel::startup() 123{ 124 if (scMainDone) 125 return; 126 127 schedule(t0Event, curTick()); 128 129 if (stopAfterCallbacks) 130 return; 131 132 try { 133 status(::sc_core::SC_START_OF_SIMULATION); 134 for (auto m: sc_gem5::allModules) { 135 m->sc_mod()->start_of_simulation(); 136 for (auto p: m->ports) 137 p->start_of_simulation(); 138 for (auto e: m->exports) 139 e->start_of_simulation(); 140 } 141 callbackModule(nullptr); 142 for (auto c: sc_gem5::allChannels) 143 c->sc_chan()->start_of_simulation(); 144 } catch (...) { 145 ::sc_gem5::scheduler.throwToScMain(); 146 } 147 148 startComplete = true; 149 150 if (stopAfterCallbacks) 151 stopWork(); 152 153 kernel->status(::sc_core::SC_RUNNING); 154} 155 156void 157Kernel::stop() 158{ 159 if (status() < ::sc_core::SC_RUNNING) 160 stopAfterCallbacks = true; 161 else 162 stopWork(); 163} 164 165void 166Kernel::stopWork() 167{ 168 status(::sc_core::SC_END_OF_SIMULATION); 169 try { 170 for (auto m: sc_gem5::allModules) { 171 m->sc_mod()->end_of_simulation(); 172 for (auto p: m->ports) 173 p->end_of_simulation(); 174 for (auto e: m->exports) 175 e->end_of_simulation(); 176 } 177 callbackModule(nullptr); 178 for (auto c: sc_gem5::allChannels) 179 c->sc_chan()->end_of_simulation(); 180 } catch (...) { 181 ::sc_gem5::scheduler.throwToScMain(); 182 } 183 184 endComplete = true; 185 186 status(::sc_core::SC_STOPPED); 187} 188 189void 190Kernel::t0Handler() 191{ 192 if (stopAfterCallbacks) { 193 scheduler.clear(); 194 ::sc_gem5::scheduler.initPhase(); 195 scheduler.scheduleStop(false); 196 } else { 197 ::sc_gem5::scheduler.initPhase(); 198 status(::sc_core::SC_RUNNING); 199 } 200} 201 202Kernel *kernel; 203 204} // namespace sc_gem5 205 206sc_gem5::Kernel * 207SystemC_KernelParams::create() 208{ 209 panic_if(sc_gem5::kernel, 210 "Only one systemc kernel object may be defined.\n"); 211 sc_gem5::kernel = new sc_gem5::Kernel(this); 212 return sc_gem5::kernel; 213} 214