fiber.cc revision 13435
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
3613435Sgabeblack@google.com#include <sys/mman.h>
3713435Sgabeblack@google.com#include <unistd.h>
3813435Sgabeblack@google.com
3912787Sgabeblack@google.com#include <cerrno>
4013435Sgabeblack@google.com#include <cstdio>
4112787Sgabeblack@google.com#include <cstring>
4212787Sgabeblack@google.com
4312787Sgabeblack@google.com#include "base/logging.hh"
4412787Sgabeblack@google.com
4512787Sgabeblack@google.comusing namespace std;
4612787Sgabeblack@google.com
4712787Sgabeblack@google.comnamespace
4812787Sgabeblack@google.com{
4912787Sgabeblack@google.com
5012787Sgabeblack@google.com/*
5112787Sgabeblack@google.com * The PrimaryFiber class is a special case that attaches to the currently
5212787Sgabeblack@google.com * executing context. That makes handling the "primary" fiber, aka the one
5312787Sgabeblack@google.com * which most of gem5 is running under, no different than other Fibers.
5412787Sgabeblack@google.com */
5512787Sgabeblack@google.comclass PrimaryFiber : public Fiber
5612787Sgabeblack@google.com{
5712787Sgabeblack@google.com  public:
5812787Sgabeblack@google.com    PrimaryFiber() : Fiber(nullptr, 0) { setStarted(); }
5912787Sgabeblack@google.com    void main() { panic("PrimaryFiber main executed.\n"); }
6012787Sgabeblack@google.com};
6112787Sgabeblack@google.com
6212787Sgabeblack@google.comPrimaryFiber _primaryFiber;
6312787Sgabeblack@google.com
6412787Sgabeblack@google.com// A pointer to whatever the currently executing Fiber is.
6512787Sgabeblack@google.comFiber *_currentFiber = &_primaryFiber;
6612787Sgabeblack@google.com
6712787Sgabeblack@google.com// A pointer to the Fiber which is currently being started/initialized.
6812787Sgabeblack@google.comFiber *startingFiber = nullptr;
6912787Sgabeblack@google.com
7012787Sgabeblack@google.com} // anonymous namespace
7112787Sgabeblack@google.com
7212787Sgabeblack@google.comvoid
7312787Sgabeblack@google.comFiber::entryTrampoline()
7412787Sgabeblack@google.com{
7512787Sgabeblack@google.com    startingFiber->start();
7612787Sgabeblack@google.com}
7712787Sgabeblack@google.com
7813435Sgabeblack@google.comFiber::Fiber(size_t stack_size) : Fiber(primaryFiber(), stack_size)
7913435Sgabeblack@google.com{}
8013435Sgabeblack@google.com
8113435Sgabeblack@google.comFiber::Fiber(Fiber *link, size_t stack_size) :
8213435Sgabeblack@google.com    link(link), stack(nullptr), stackSize(stack_size), guardPage(nullptr),
8313435Sgabeblack@google.com    guardPageSize(sysconf(_SC_PAGE_SIZE)), started(false), _finished(false)
8412920Sgabeblack@google.com{
8513435Sgabeblack@google.com    if (stack_size) {
8613435Sgabeblack@google.com        guardPage = mmap(nullptr, guardPageSize + stack_size,
8713435Sgabeblack@google.com                         PROT_READ | PROT_WRITE,
8813435Sgabeblack@google.com                         MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
8913435Sgabeblack@google.com        if (guardPage == (void *)MAP_FAILED) {
9013435Sgabeblack@google.com            perror("mmap");
9113435Sgabeblack@google.com            fatal("Could not mmap %d byte fiber stack.\n", stack_size);
9213435Sgabeblack@google.com        }
9313435Sgabeblack@google.com        stack = (void *)((uint8_t *)guardPage + guardPageSize);
9413435Sgabeblack@google.com        if (mprotect(guardPage, guardPageSize, PROT_NONE)) {
9513435Sgabeblack@google.com            perror("mprotect");
9613435Sgabeblack@google.com            fatal("Could not forbid access to fiber stack guard page.");
9713435Sgabeblack@google.com        }
9813435Sgabeblack@google.com    }
9912920Sgabeblack@google.com#if HAVE_VALGRIND
10013435Sgabeblack@google.com    valgrindStackId = VALGRIND_STACK_REGISTER(
10113435Sgabeblack@google.com            stack, (uint8_t *)stack + stack_size);
10212920Sgabeblack@google.com#endif
10312920Sgabeblack@google.com}
10412787Sgabeblack@google.com
10512787Sgabeblack@google.comFiber::~Fiber()
10612787Sgabeblack@google.com{
10712787Sgabeblack@google.com    panic_if(stack && _currentFiber == this, "Fiber stack is in use.");
10812920Sgabeblack@google.com#if HAVE_VALGRIND
10912920Sgabeblack@google.com    VALGRIND_STACK_DEREGISTER(valgrindStackId);
11012920Sgabeblack@google.com#endif
11113435Sgabeblack@google.com    if (guardPage)
11213435Sgabeblack@google.com        munmap(guardPage, guardPageSize + stackSize);
11312787Sgabeblack@google.com}
11412787Sgabeblack@google.com
11512787Sgabeblack@google.comvoid
11612787Sgabeblack@google.comFiber::createContext()
11712787Sgabeblack@google.com{
11812787Sgabeblack@google.com    // Set up a context for the new fiber, starting it in the trampoline.
11912787Sgabeblack@google.com    getcontext(&ctx);
12012787Sgabeblack@google.com    ctx.uc_stack.ss_sp = stack;
12112787Sgabeblack@google.com    ctx.uc_stack.ss_size = stackSize;
12212787Sgabeblack@google.com    ctx.uc_link = nullptr;
12312787Sgabeblack@google.com    makecontext(&ctx, &entryTrampoline, 0);
12412787Sgabeblack@google.com
12512787Sgabeblack@google.com    // Swap to the new context so it can enter its start() function. It
12612787Sgabeblack@google.com    // will then swap itself back out and return here.
12712787Sgabeblack@google.com    startingFiber = this;
12812787Sgabeblack@google.com    panic_if(!_currentFiber, "No active Fiber object.");
12912787Sgabeblack@google.com    swapcontext(&_currentFiber->ctx, &ctx);
13012787Sgabeblack@google.com
13112787Sgabeblack@google.com    // The new context is now ready and about to call main().
13212787Sgabeblack@google.com}
13312787Sgabeblack@google.com
13412787Sgabeblack@google.comvoid
13512787Sgabeblack@google.comFiber::start()
13612787Sgabeblack@google.com{
13712787Sgabeblack@google.com    // Avoid a dangling pointer.
13812787Sgabeblack@google.com    startingFiber = nullptr;
13912787Sgabeblack@google.com
14012787Sgabeblack@google.com    setStarted();
14112787Sgabeblack@google.com
14212787Sgabeblack@google.com    // Swap back to the parent context which is still considered "current",
14312787Sgabeblack@google.com    // now that we're ready to go.
14412787Sgabeblack@google.com    int ret M5_VAR_USED = swapcontext(&ctx, &_currentFiber->ctx);
14512787Sgabeblack@google.com    panic_if(ret == -1, strerror(errno));
14612787Sgabeblack@google.com
14712787Sgabeblack@google.com    // Call main() when we're been reactivated for the first time.
14812787Sgabeblack@google.com    main();
14912787Sgabeblack@google.com
15012787Sgabeblack@google.com    // main has returned, so this Fiber has finished. Switch to the "link"
15112787Sgabeblack@google.com    // Fiber.
15212787Sgabeblack@google.com    _finished = true;
15312787Sgabeblack@google.com    link->run();
15412787Sgabeblack@google.com}
15512787Sgabeblack@google.com
15612787Sgabeblack@google.comvoid
15712787Sgabeblack@google.comFiber::run()
15812787Sgabeblack@google.com{
15912787Sgabeblack@google.com    panic_if(_finished, "Fiber has already run to completion.");
16012787Sgabeblack@google.com
16112787Sgabeblack@google.com    // If we're already running this fiber, we're done.
16212787Sgabeblack@google.com    if (_currentFiber == this)
16312787Sgabeblack@google.com        return;
16412787Sgabeblack@google.com
16512787Sgabeblack@google.com    if (!started)
16612787Sgabeblack@google.com        createContext();
16712787Sgabeblack@google.com
16812787Sgabeblack@google.com    // Switch out of the current Fiber's context and this one's in.
16912787Sgabeblack@google.com    Fiber *prev = _currentFiber;
17012787Sgabeblack@google.com    Fiber *next = this;
17112787Sgabeblack@google.com    _currentFiber = next;
17212787Sgabeblack@google.com    swapcontext(&prev->ctx, &next->ctx);
17312787Sgabeblack@google.com}
17412787Sgabeblack@google.com
17512787Sgabeblack@google.comFiber *Fiber::currentFiber() { return _currentFiber; }
17612787Sgabeblack@google.comFiber *Fiber::primaryFiber() { return &_primaryFiber; }
177