fiber.cc revision 13435
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 "base/fiber.hh" 31 32#if HAVE_VALGRIND 33#include <valgrind/valgrind.h> 34#endif 35 36#include <sys/mman.h> 37#include <unistd.h> 38 39#include <cerrno> 40#include <cstdio> 41#include <cstring> 42 43#include "base/logging.hh" 44 45using namespace std; 46 47namespace 48{ 49 50/* 51 * The PrimaryFiber class is a special case that attaches to the currently 52 * executing context. That makes handling the "primary" fiber, aka the one 53 * which most of gem5 is running under, no different than other Fibers. 54 */ 55class PrimaryFiber : public Fiber 56{ 57 public: 58 PrimaryFiber() : Fiber(nullptr, 0) { setStarted(); } 59 void main() { panic("PrimaryFiber main executed.\n"); } 60}; 61 62PrimaryFiber _primaryFiber; 63 64// A pointer to whatever the currently executing Fiber is. 65Fiber *_currentFiber = &_primaryFiber; 66 67// A pointer to the Fiber which is currently being started/initialized. 68Fiber *startingFiber = nullptr; 69 70} // anonymous namespace 71 72void 73Fiber::entryTrampoline() 74{ 75 startingFiber->start(); 76} 77 78Fiber::Fiber(size_t stack_size) : Fiber(primaryFiber(), stack_size) 79{} 80 81Fiber::Fiber(Fiber *link, size_t stack_size) : 82 link(link), stack(nullptr), stackSize(stack_size), guardPage(nullptr), 83 guardPageSize(sysconf(_SC_PAGE_SIZE)), started(false), _finished(false) 84{ 85 if (stack_size) { 86 guardPage = mmap(nullptr, guardPageSize + stack_size, 87 PROT_READ | PROT_WRITE, 88 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 89 if (guardPage == (void *)MAP_FAILED) { 90 perror("mmap"); 91 fatal("Could not mmap %d byte fiber stack.\n", stack_size); 92 } 93 stack = (void *)((uint8_t *)guardPage + guardPageSize); 94 if (mprotect(guardPage, guardPageSize, PROT_NONE)) { 95 perror("mprotect"); 96 fatal("Could not forbid access to fiber stack guard page."); 97 } 98 } 99#if HAVE_VALGRIND 100 valgrindStackId = VALGRIND_STACK_REGISTER( 101 stack, (uint8_t *)stack + stack_size); 102#endif 103} 104 105Fiber::~Fiber() 106{ 107 panic_if(stack && _currentFiber == this, "Fiber stack is in use."); 108#if HAVE_VALGRIND 109 VALGRIND_STACK_DEREGISTER(valgrindStackId); 110#endif 111 if (guardPage) 112 munmap(guardPage, guardPageSize + stackSize); 113} 114 115void 116Fiber::createContext() 117{ 118 // Set up a context for the new fiber, starting it in the trampoline. 119 getcontext(&ctx); 120 ctx.uc_stack.ss_sp = stack; 121 ctx.uc_stack.ss_size = stackSize; 122 ctx.uc_link = nullptr; 123 makecontext(&ctx, &entryTrampoline, 0); 124 125 // Swap to the new context so it can enter its start() function. It 126 // will then swap itself back out and return here. 127 startingFiber = this; 128 panic_if(!_currentFiber, "No active Fiber object."); 129 swapcontext(&_currentFiber->ctx, &ctx); 130 131 // The new context is now ready and about to call main(). 132} 133 134void 135Fiber::start() 136{ 137 // Avoid a dangling pointer. 138 startingFiber = nullptr; 139 140 setStarted(); 141 142 // Swap back to the parent context which is still considered "current", 143 // now that we're ready to go. 144 int ret M5_VAR_USED = swapcontext(&ctx, &_currentFiber->ctx); 145 panic_if(ret == -1, strerror(errno)); 146 147 // Call main() when we're been reactivated for the first time. 148 main(); 149 150 // main has returned, so this Fiber has finished. Switch to the "link" 151 // Fiber. 152 _finished = true; 153 link->run(); 154} 155 156void 157Fiber::run() 158{ 159 panic_if(_finished, "Fiber has already run to completion."); 160 161 // If we're already running this fiber, we're done. 162 if (_currentFiber == this) 163 return; 164 165 if (!started) 166 createContext(); 167 168 // Switch out of the current Fiber's context and this one's in. 169 Fiber *prev = _currentFiber; 170 Fiber *next = this; 171 _currentFiber = next; 172 swapcontext(&prev->ctx, &next->ctx); 173} 174 175Fiber *Fiber::currentFiber() { return _currentFiber; } 176Fiber *Fiber::primaryFiber() { return &_primaryFiber; } 177