fiber.cc revision 14042
112787Sgabeblack@google.com/* 212787Sgabeblack@google.com * Copyright 2018 Google, Inc. 312787Sgabeblack@google.com * 412787Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 512787Sgabeblack@google.com * modification, are permitted provided that the following conditions are 612787Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 712787Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 812787Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 912787Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1012787Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1112787Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1212787Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1312787Sgabeblack@google.com * this software without specific prior written permission. 1412787Sgabeblack@google.com * 1512787Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1612787Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1712787Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1812787Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1912787Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2012787Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2112787Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2212787Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2312787Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2412787Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2512787Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2612787Sgabeblack@google.com * 2712787Sgabeblack@google.com * Authors: Gabe Black 2812787Sgabeblack@google.com */ 2912787Sgabeblack@google.com 3012787Sgabeblack@google.com#include "base/fiber.hh" 3112787Sgabeblack@google.com 3212920Sgabeblack@google.com#if HAVE_VALGRIND 3312920Sgabeblack@google.com#include <valgrind/valgrind.h> 3412920Sgabeblack@google.com#endif 3512920Sgabeblack@google.com 3613476Snikos.nikoleris@arm.com// Mac OS requires _DARWIN_C_SOURCE if _POSIX_C_SOURCE is defined, 3713476Snikos.nikoleris@arm.com// otherwise it will mask the definition of MAP_ANONYMOUS. 3813476Snikos.nikoleris@arm.com// _POSIX_C_SOURCE is already defined by including <ucontext.h> in 3913476Snikos.nikoleris@arm.com// base/fiber.hh 4013476Snikos.nikoleris@arm.com#if defined(__APPLE__) && defined(__MACH__) 4113476Snikos.nikoleris@arm.com#define _DARWIN_C_SOURCE 4213476Snikos.nikoleris@arm.com#endif 4313476Snikos.nikoleris@arm.com 4413435Sgabeblack@google.com#include <sys/mman.h> 4513435Sgabeblack@google.com#include <unistd.h> 4613435Sgabeblack@google.com 4712787Sgabeblack@google.com#include <cerrno> 4813435Sgabeblack@google.com#include <cstdio> 4912787Sgabeblack@google.com#include <cstring> 5012787Sgabeblack@google.com 5112787Sgabeblack@google.com#include "base/logging.hh" 5212787Sgabeblack@google.com 5312787Sgabeblack@google.comusing namespace std; 5412787Sgabeblack@google.com 5512787Sgabeblack@google.comnamespace 5612787Sgabeblack@google.com{ 5712787Sgabeblack@google.com 5812787Sgabeblack@google.com/* 5912787Sgabeblack@google.com * The PrimaryFiber class is a special case that attaches to the currently 6012787Sgabeblack@google.com * executing context. That makes handling the "primary" fiber, aka the one 6112787Sgabeblack@google.com * which most of gem5 is running under, no different than other Fibers. 6212787Sgabeblack@google.com */ 6312787Sgabeblack@google.comclass PrimaryFiber : public Fiber 6412787Sgabeblack@google.com{ 6512787Sgabeblack@google.com public: 6612787Sgabeblack@google.com PrimaryFiber() : Fiber(nullptr, 0) { setStarted(); } 6712787Sgabeblack@google.com void main() { panic("PrimaryFiber main executed.\n"); } 6812787Sgabeblack@google.com}; 6912787Sgabeblack@google.com 7012787Sgabeblack@google.comPrimaryFiber _primaryFiber; 7112787Sgabeblack@google.com 7212787Sgabeblack@google.com// A pointer to whatever the currently executing Fiber is. 7312787Sgabeblack@google.comFiber *_currentFiber = &_primaryFiber; 7412787Sgabeblack@google.com 7512787Sgabeblack@google.com// A pointer to the Fiber which is currently being started/initialized. 7612787Sgabeblack@google.comFiber *startingFiber = nullptr; 7712787Sgabeblack@google.com 7812787Sgabeblack@google.com} // anonymous namespace 7912787Sgabeblack@google.com 8012787Sgabeblack@google.comvoid 8112787Sgabeblack@google.comFiber::entryTrampoline() 8212787Sgabeblack@google.com{ 8312787Sgabeblack@google.com startingFiber->start(); 8412787Sgabeblack@google.com} 8512787Sgabeblack@google.com 8613435Sgabeblack@google.comFiber::Fiber(size_t stack_size) : Fiber(primaryFiber(), stack_size) 8713435Sgabeblack@google.com{} 8813435Sgabeblack@google.com 8913435Sgabeblack@google.comFiber::Fiber(Fiber *link, size_t stack_size) : 9013435Sgabeblack@google.com link(link), stack(nullptr), stackSize(stack_size), guardPage(nullptr), 9114042Sgiacomo.travaglini@arm.com guardPageSize(sysconf(_SC_PAGE_SIZE)), _started(false), _finished(false) 9212920Sgabeblack@google.com{ 9313435Sgabeblack@google.com if (stack_size) { 9413435Sgabeblack@google.com guardPage = mmap(nullptr, guardPageSize + stack_size, 9513435Sgabeblack@google.com PROT_READ | PROT_WRITE, 9613435Sgabeblack@google.com MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 9713435Sgabeblack@google.com if (guardPage == (void *)MAP_FAILED) { 9813435Sgabeblack@google.com perror("mmap"); 9913435Sgabeblack@google.com fatal("Could not mmap %d byte fiber stack.\n", stack_size); 10013435Sgabeblack@google.com } 10113435Sgabeblack@google.com stack = (void *)((uint8_t *)guardPage + guardPageSize); 10213435Sgabeblack@google.com if (mprotect(guardPage, guardPageSize, PROT_NONE)) { 10313435Sgabeblack@google.com perror("mprotect"); 10413435Sgabeblack@google.com fatal("Could not forbid access to fiber stack guard page."); 10513435Sgabeblack@google.com } 10613435Sgabeblack@google.com } 10712920Sgabeblack@google.com#if HAVE_VALGRIND 10813435Sgabeblack@google.com valgrindStackId = VALGRIND_STACK_REGISTER( 10913435Sgabeblack@google.com stack, (uint8_t *)stack + stack_size); 11012920Sgabeblack@google.com#endif 11112920Sgabeblack@google.com} 11212787Sgabeblack@google.com 11312787Sgabeblack@google.comFiber::~Fiber() 11412787Sgabeblack@google.com{ 11512787Sgabeblack@google.com panic_if(stack && _currentFiber == this, "Fiber stack is in use."); 11612920Sgabeblack@google.com#if HAVE_VALGRIND 11712920Sgabeblack@google.com VALGRIND_STACK_DEREGISTER(valgrindStackId); 11812920Sgabeblack@google.com#endif 11913435Sgabeblack@google.com if (guardPage) 12013435Sgabeblack@google.com munmap(guardPage, guardPageSize + stackSize); 12112787Sgabeblack@google.com} 12212787Sgabeblack@google.com 12312787Sgabeblack@google.comvoid 12412787Sgabeblack@google.comFiber::createContext() 12512787Sgabeblack@google.com{ 12612787Sgabeblack@google.com // Set up a context for the new fiber, starting it in the trampoline. 12712787Sgabeblack@google.com getcontext(&ctx); 12812787Sgabeblack@google.com ctx.uc_stack.ss_sp = stack; 12912787Sgabeblack@google.com ctx.uc_stack.ss_size = stackSize; 13012787Sgabeblack@google.com ctx.uc_link = nullptr; 13112787Sgabeblack@google.com makecontext(&ctx, &entryTrampoline, 0); 13212787Sgabeblack@google.com 13312787Sgabeblack@google.com // Swap to the new context so it can enter its start() function. It 13412787Sgabeblack@google.com // will then swap itself back out and return here. 13512787Sgabeblack@google.com startingFiber = this; 13612787Sgabeblack@google.com panic_if(!_currentFiber, "No active Fiber object."); 13712787Sgabeblack@google.com swapcontext(&_currentFiber->ctx, &ctx); 13812787Sgabeblack@google.com 13912787Sgabeblack@google.com // The new context is now ready and about to call main(). 14012787Sgabeblack@google.com} 14112787Sgabeblack@google.com 14212787Sgabeblack@google.comvoid 14312787Sgabeblack@google.comFiber::start() 14412787Sgabeblack@google.com{ 14512787Sgabeblack@google.com // Avoid a dangling pointer. 14612787Sgabeblack@google.com startingFiber = nullptr; 14712787Sgabeblack@google.com 14812787Sgabeblack@google.com setStarted(); 14912787Sgabeblack@google.com 15012787Sgabeblack@google.com // Swap back to the parent context which is still considered "current", 15112787Sgabeblack@google.com // now that we're ready to go. 15212787Sgabeblack@google.com int ret M5_VAR_USED = swapcontext(&ctx, &_currentFiber->ctx); 15312787Sgabeblack@google.com panic_if(ret == -1, strerror(errno)); 15412787Sgabeblack@google.com 15512787Sgabeblack@google.com // Call main() when we're been reactivated for the first time. 15612787Sgabeblack@google.com main(); 15712787Sgabeblack@google.com 15812787Sgabeblack@google.com // main has returned, so this Fiber has finished. Switch to the "link" 15912787Sgabeblack@google.com // Fiber. 16012787Sgabeblack@google.com _finished = true; 16112787Sgabeblack@google.com link->run(); 16212787Sgabeblack@google.com} 16312787Sgabeblack@google.com 16412787Sgabeblack@google.comvoid 16512787Sgabeblack@google.comFiber::run() 16612787Sgabeblack@google.com{ 16712787Sgabeblack@google.com panic_if(_finished, "Fiber has already run to completion."); 16812787Sgabeblack@google.com 16912787Sgabeblack@google.com // If we're already running this fiber, we're done. 17012787Sgabeblack@google.com if (_currentFiber == this) 17112787Sgabeblack@google.com return; 17212787Sgabeblack@google.com 17314042Sgiacomo.travaglini@arm.com if (!_started) 17412787Sgabeblack@google.com createContext(); 17512787Sgabeblack@google.com 17612787Sgabeblack@google.com // Switch out of the current Fiber's context and this one's in. 17712787Sgabeblack@google.com Fiber *prev = _currentFiber; 17812787Sgabeblack@google.com Fiber *next = this; 17912787Sgabeblack@google.com _currentFiber = next; 18012787Sgabeblack@google.com swapcontext(&prev->ctx, &next->ctx); 18112787Sgabeblack@google.com} 18212787Sgabeblack@google.com 18312787Sgabeblack@google.comFiber *Fiber::currentFiber() { return _currentFiber; } 18412787Sgabeblack@google.comFiber *Fiber::primaryFiber() { return &_primaryFiber; } 185