kernel.cc revision 13207
112837Sgabeblack@google.com/* 212837Sgabeblack@google.com * Copyright 2018 Google, Inc. 312837Sgabeblack@google.com * 412837Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 512837Sgabeblack@google.com * modification, are permitted provided that the following conditions are 612837Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 712837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 812837Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 912837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1012837Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1112837Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1212837Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1312837Sgabeblack@google.com * this software without specific prior written permission. 1412837Sgabeblack@google.com * 1512837Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1612837Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1712837Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1812837Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1912837Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2012837Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2112837Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2212837Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2312837Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2412837Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2512837Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2612837Sgabeblack@google.com * 2712837Sgabeblack@google.com * Authors: Gabe Black 2812837Sgabeblack@google.com */ 2912837Sgabeblack@google.com 3012837Sgabeblack@google.com#include "systemc/core/kernel.hh" 3112982Sgabeblack@google.com 3212982Sgabeblack@google.com#include "base/logging.hh" 3313059Sgabeblack@google.com#include "systemc/core/channel.hh" 3412982Sgabeblack@google.com#include "systemc/core/module.hh" 3513207Sgabeblack@google.com#include "systemc/core/port.hh" 3612953Sgabeblack@google.com#include "systemc/core/scheduler.hh" 3712837Sgabeblack@google.com 3812982Sgabeblack@google.comnamespace sc_gem5 3912837Sgabeblack@google.com{ 4012837Sgabeblack@google.com 4112990Sgabeblack@google.comnamespace 4212990Sgabeblack@google.com{ 4312990Sgabeblack@google.com 4413077Sgabeblack@google.combool scMainDone = false; 4512990Sgabeblack@google.combool stopAfterCallbacks = false; 4612990Sgabeblack@google.combool startComplete = false; 4712990Sgabeblack@google.combool endComplete = false; 4812990Sgabeblack@google.com 4912990Sgabeblack@google.comsc_core::sc_status _status = sc_core::SC_ELABORATION; 5012990Sgabeblack@google.com 5112990Sgabeblack@google.com} // anonymous namespace 5212990Sgabeblack@google.com 5312990Sgabeblack@google.combool Kernel::startOfSimulationComplete() { return startComplete; } 5412990Sgabeblack@google.combool Kernel::endOfSimulationComplete() { return endComplete; } 5512990Sgabeblack@google.com 5613077Sgabeblack@google.combool Kernel::scMainFinished() { return scMainDone; } 5713077Sgabeblack@google.comvoid Kernel::scMainFinished(bool finished) { scMainDone = finished; } 5813077Sgabeblack@google.com 5912990Sgabeblack@google.comsc_core::sc_status Kernel::status() { return _status; } 6012990Sgabeblack@google.comvoid Kernel::status(sc_core::sc_status s) { _status = s; } 6112990Sgabeblack@google.com 6212954Sgabeblack@google.comKernel::Kernel(Params *params) : 6313042Sgabeblack@google.com SimObject(params), t0Event(this, false, EventBase::Default_Pri - 1) 6413042Sgabeblack@google.com{ 6513042Sgabeblack@google.com // Install ourselves as the scheduler's event manager. 6613042Sgabeblack@google.com ::sc_gem5::scheduler.setEventQueue(eventQueue()); 6713042Sgabeblack@google.com} 6812982Sgabeblack@google.com 6912982Sgabeblack@google.comvoid 7012982Sgabeblack@google.comKernel::init() 7112982Sgabeblack@google.com{ 7213077Sgabeblack@google.com if (scMainDone) 7313077Sgabeblack@google.com return; 7413077Sgabeblack@google.com 7513094Sgabeblack@google.com if (stopAfterCallbacks) 7613094Sgabeblack@google.com fatal("Simulation called sc_stop during elaboration.\n"); 7713094Sgabeblack@google.com 7812990Sgabeblack@google.com status(::sc_core::SC_BEFORE_END_OF_ELABORATION); 7913207Sgabeblack@google.com for (auto p: allPorts) 8013207Sgabeblack@google.com p->sc_port_base()->before_end_of_elaboration(); 8113191Sgabeblack@google.com for (auto m: sc_gem5::allModules) 8213191Sgabeblack@google.com m->beforeEndOfElaboration(); 8313059Sgabeblack@google.com for (auto c: sc_gem5::allChannels) 8413059Sgabeblack@google.com c->sc_chan()->before_end_of_elaboration(); 8512982Sgabeblack@google.com} 8612982Sgabeblack@google.com 8712982Sgabeblack@google.comvoid 8812982Sgabeblack@google.comKernel::regStats() 8912982Sgabeblack@google.com{ 9013156Sgabeblack@google.com if (scMainDone || stopAfterCallbacks) 9113077Sgabeblack@google.com return; 9213077Sgabeblack@google.com 9313188Sgabeblack@google.com try { 9413207Sgabeblack@google.com for (auto p: allPorts) 9513207Sgabeblack@google.com p->finalize(); 9613053Sgabeblack@google.com 9713188Sgabeblack@google.com status(::sc_core::SC_END_OF_ELABORATION); 9813207Sgabeblack@google.com for (auto p: allPorts) 9913207Sgabeblack@google.com p->sc_port_base()->end_of_elaboration(); 10013191Sgabeblack@google.com for (auto m: sc_gem5::allModules) 10113191Sgabeblack@google.com m->endOfElaboration(); 10213188Sgabeblack@google.com for (auto c: sc_gem5::allChannels) 10313188Sgabeblack@google.com c->sc_chan()->end_of_elaboration(); 10413188Sgabeblack@google.com } catch (...) { 10513188Sgabeblack@google.com ::sc_gem5::scheduler.throwToScMain(); 10613059Sgabeblack@google.com } 10713188Sgabeblack@google.com 10813188Sgabeblack@google.com ::sc_gem5::scheduler.elaborationDone(true); 10912982Sgabeblack@google.com} 11012953Sgabeblack@google.com 11112953Sgabeblack@google.comvoid 11212953Sgabeblack@google.comKernel::startup() 11312837Sgabeblack@google.com{ 11413077Sgabeblack@google.com if (scMainDone) 11513077Sgabeblack@google.com return; 11613077Sgabeblack@google.com 11713156Sgabeblack@google.com schedule(t0Event, curTick()); 11813156Sgabeblack@google.com 11913156Sgabeblack@google.com if (stopAfterCallbacks) 12013156Sgabeblack@google.com return; 12113156Sgabeblack@google.com 12213188Sgabeblack@google.com try { 12313188Sgabeblack@google.com status(::sc_core::SC_START_OF_SIMULATION); 12413207Sgabeblack@google.com for (auto p: allPorts) 12513207Sgabeblack@google.com p->sc_port_base()->start_of_simulation(); 12613191Sgabeblack@google.com for (auto m: sc_gem5::allModules) 12713191Sgabeblack@google.com m->startOfSimulation(); 12813188Sgabeblack@google.com for (auto c: sc_gem5::allChannels) 12913188Sgabeblack@google.com c->sc_chan()->start_of_simulation(); 13013188Sgabeblack@google.com } catch (...) { 13113188Sgabeblack@google.com ::sc_gem5::scheduler.throwToScMain(); 13213059Sgabeblack@google.com } 13312982Sgabeblack@google.com 13412990Sgabeblack@google.com startComplete = true; 13512982Sgabeblack@google.com 13612990Sgabeblack@google.com if (stopAfterCallbacks) 13712982Sgabeblack@google.com stopWork(); 13812982Sgabeblack@google.com 13912982Sgabeblack@google.com kernel->status(::sc_core::SC_RUNNING); 14012953Sgabeblack@google.com} 14112953Sgabeblack@google.com 14212953Sgabeblack@google.comvoid 14312982Sgabeblack@google.comKernel::stop() 14412982Sgabeblack@google.com{ 14512982Sgabeblack@google.com if (status() < ::sc_core::SC_RUNNING) 14612990Sgabeblack@google.com stopAfterCallbacks = true; 14712982Sgabeblack@google.com else 14812982Sgabeblack@google.com stopWork(); 14912982Sgabeblack@google.com} 15012982Sgabeblack@google.com 15112982Sgabeblack@google.comvoid 15212982Sgabeblack@google.comKernel::stopWork() 15312982Sgabeblack@google.com{ 15412990Sgabeblack@google.com status(::sc_core::SC_END_OF_SIMULATION); 15513188Sgabeblack@google.com try { 15613207Sgabeblack@google.com for (auto p: allPorts) 15713207Sgabeblack@google.com p->sc_port_base()->end_of_simulation(); 15813191Sgabeblack@google.com for (auto m: sc_gem5::allModules) 15913191Sgabeblack@google.com m->endOfSimulation(); 16013188Sgabeblack@google.com for (auto c: sc_gem5::allChannels) 16113188Sgabeblack@google.com c->sc_chan()->end_of_simulation(); 16213188Sgabeblack@google.com } catch (...) { 16313188Sgabeblack@google.com ::sc_gem5::scheduler.throwToScMain(); 16413059Sgabeblack@google.com } 16512982Sgabeblack@google.com 16612990Sgabeblack@google.com endComplete = true; 16712982Sgabeblack@google.com 16812990Sgabeblack@google.com status(::sc_core::SC_STOPPED); 16912982Sgabeblack@google.com} 17012982Sgabeblack@google.com 17112982Sgabeblack@google.comvoid 17212953Sgabeblack@google.comKernel::t0Handler() 17312953Sgabeblack@google.com{ 17413156Sgabeblack@google.com if (stopAfterCallbacks) { 17513156Sgabeblack@google.com scheduler.clear(); 17613156Sgabeblack@google.com ::sc_gem5::scheduler.initPhase(); 17713156Sgabeblack@google.com scheduler.scheduleStop(false); 17813156Sgabeblack@google.com } else { 17913156Sgabeblack@google.com ::sc_gem5::scheduler.initPhase(); 18013156Sgabeblack@google.com status(::sc_core::SC_RUNNING); 18113156Sgabeblack@google.com } 18212837Sgabeblack@google.com} 18312837Sgabeblack@google.com 18412982Sgabeblack@google.comKernel *kernel; 18512837Sgabeblack@google.com 18612982Sgabeblack@google.com} // namespace sc_gem5 18712982Sgabeblack@google.com 18812982Sgabeblack@google.comsc_gem5::Kernel * 18912837Sgabeblack@google.comSystemC_KernelParams::create() 19012837Sgabeblack@google.com{ 19112982Sgabeblack@google.com panic_if(sc_gem5::kernel, 19212982Sgabeblack@google.com "Only one systemc kernel object may be defined.\n"); 19312982Sgabeblack@google.com sc_gem5::kernel = new sc_gem5::Kernel(this); 19412982Sgabeblack@google.com return sc_gem5::kernel; 19512837Sgabeblack@google.com} 196