fiber.cc revision 12920
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
3612787Sgabeblack@google.com#include <cerrno>
3712787Sgabeblack@google.com#include <cstring>
3812787Sgabeblack@google.com
3912787Sgabeblack@google.com#include "base/logging.hh"
4012787Sgabeblack@google.com
4112787Sgabeblack@google.comusing namespace std;
4212787Sgabeblack@google.com
4312787Sgabeblack@google.comnamespace
4412787Sgabeblack@google.com{
4512787Sgabeblack@google.com
4612787Sgabeblack@google.com/*
4712787Sgabeblack@google.com * The PrimaryFiber class is a special case that attaches to the currently
4812787Sgabeblack@google.com * executing context. That makes handling the "primary" fiber, aka the one
4912787Sgabeblack@google.com * which most of gem5 is running under, no different than other Fibers.
5012787Sgabeblack@google.com */
5112787Sgabeblack@google.comclass PrimaryFiber : public Fiber
5212787Sgabeblack@google.com{
5312787Sgabeblack@google.com  public:
5412787Sgabeblack@google.com    PrimaryFiber() : Fiber(nullptr, 0) { setStarted(); }
5512787Sgabeblack@google.com    void main() { panic("PrimaryFiber main executed.\n"); }
5612787Sgabeblack@google.com};
5712787Sgabeblack@google.com
5812787Sgabeblack@google.comPrimaryFiber _primaryFiber;
5912787Sgabeblack@google.com
6012787Sgabeblack@google.com// A pointer to whatever the currently executing Fiber is.
6112787Sgabeblack@google.comFiber *_currentFiber = &_primaryFiber;
6212787Sgabeblack@google.com
6312787Sgabeblack@google.com// A pointer to the Fiber which is currently being started/initialized.
6412787Sgabeblack@google.comFiber *startingFiber = nullptr;
6512787Sgabeblack@google.com
6612787Sgabeblack@google.com} // anonymous namespace
6712787Sgabeblack@google.com
6812787Sgabeblack@google.comvoid
6912787Sgabeblack@google.comFiber::entryTrampoline()
7012787Sgabeblack@google.com{
7112787Sgabeblack@google.com    startingFiber->start();
7212787Sgabeblack@google.com}
7312787Sgabeblack@google.com
7412787Sgabeblack@google.comFiber::Fiber(size_t stack_size) :
7512787Sgabeblack@google.com    link(primaryFiber()),
7612787Sgabeblack@google.com    stack(stack_size ? new uint8_t[stack_size] : nullptr),
7712787Sgabeblack@google.com    stackSize(stack_size), started(false), _finished(false)
7812920Sgabeblack@google.com{
7912920Sgabeblack@google.com#if HAVE_VALGRIND
8012920Sgabeblack@google.com    valgrindStackId = VALGRIND_STACK_REGISTER(stack, stack + stack_size);
8112920Sgabeblack@google.com#endif
8212920Sgabeblack@google.com}
8312787Sgabeblack@google.com
8412787Sgabeblack@google.comFiber::Fiber(Fiber *link, size_t stack_size) :
8512787Sgabeblack@google.com    link(link), stack(stack_size ? new uint8_t[stack_size] : nullptr),
8612787Sgabeblack@google.com    stackSize(stack_size), started(false), _finished(false)
8712787Sgabeblack@google.com{}
8812787Sgabeblack@google.com
8912787Sgabeblack@google.comFiber::~Fiber()
9012787Sgabeblack@google.com{
9112787Sgabeblack@google.com    panic_if(stack && _currentFiber == this, "Fiber stack is in use.");
9212920Sgabeblack@google.com#if HAVE_VALGRIND
9312920Sgabeblack@google.com    VALGRIND_STACK_DEREGISTER(valgrindStackId);
9412920Sgabeblack@google.com#endif
9512787Sgabeblack@google.com    delete [] stack;
9612787Sgabeblack@google.com}
9712787Sgabeblack@google.com
9812787Sgabeblack@google.comvoid
9912787Sgabeblack@google.comFiber::createContext()
10012787Sgabeblack@google.com{
10112787Sgabeblack@google.com    // Set up a context for the new fiber, starting it in the trampoline.
10212787Sgabeblack@google.com    getcontext(&ctx);
10312787Sgabeblack@google.com    ctx.uc_stack.ss_sp = stack;
10412787Sgabeblack@google.com    ctx.uc_stack.ss_size = stackSize;
10512787Sgabeblack@google.com    ctx.uc_link = nullptr;
10612787Sgabeblack@google.com    makecontext(&ctx, &entryTrampoline, 0);
10712787Sgabeblack@google.com
10812787Sgabeblack@google.com    // Swap to the new context so it can enter its start() function. It
10912787Sgabeblack@google.com    // will then swap itself back out and return here.
11012787Sgabeblack@google.com    startingFiber = this;
11112787Sgabeblack@google.com    panic_if(!_currentFiber, "No active Fiber object.");
11212787Sgabeblack@google.com    swapcontext(&_currentFiber->ctx, &ctx);
11312787Sgabeblack@google.com
11412787Sgabeblack@google.com    // The new context is now ready and about to call main().
11512787Sgabeblack@google.com}
11612787Sgabeblack@google.com
11712787Sgabeblack@google.comvoid
11812787Sgabeblack@google.comFiber::start()
11912787Sgabeblack@google.com{
12012787Sgabeblack@google.com    // Avoid a dangling pointer.
12112787Sgabeblack@google.com    startingFiber = nullptr;
12212787Sgabeblack@google.com
12312787Sgabeblack@google.com    setStarted();
12412787Sgabeblack@google.com
12512787Sgabeblack@google.com    // Swap back to the parent context which is still considered "current",
12612787Sgabeblack@google.com    // now that we're ready to go.
12712787Sgabeblack@google.com    int ret M5_VAR_USED = swapcontext(&ctx, &_currentFiber->ctx);
12812787Sgabeblack@google.com    panic_if(ret == -1, strerror(errno));
12912787Sgabeblack@google.com
13012787Sgabeblack@google.com    // Call main() when we're been reactivated for the first time.
13112787Sgabeblack@google.com    main();
13212787Sgabeblack@google.com
13312787Sgabeblack@google.com    // main has returned, so this Fiber has finished. Switch to the "link"
13412787Sgabeblack@google.com    // Fiber.
13512787Sgabeblack@google.com    _finished = true;
13612787Sgabeblack@google.com    link->run();
13712787Sgabeblack@google.com}
13812787Sgabeblack@google.com
13912787Sgabeblack@google.comvoid
14012787Sgabeblack@google.comFiber::run()
14112787Sgabeblack@google.com{
14212787Sgabeblack@google.com    panic_if(_finished, "Fiber has already run to completion.");
14312787Sgabeblack@google.com
14412787Sgabeblack@google.com    // If we're already running this fiber, we're done.
14512787Sgabeblack@google.com    if (_currentFiber == this)
14612787Sgabeblack@google.com        return;
14712787Sgabeblack@google.com
14812787Sgabeblack@google.com    if (!started)
14912787Sgabeblack@google.com        createContext();
15012787Sgabeblack@google.com
15112787Sgabeblack@google.com    // Switch out of the current Fiber's context and this one's in.
15212787Sgabeblack@google.com    Fiber *prev = _currentFiber;
15312787Sgabeblack@google.com    Fiber *next = this;
15412787Sgabeblack@google.com    _currentFiber = next;
15512787Sgabeblack@google.com    swapcontext(&prev->ctx, &next->ctx);
15612787Sgabeblack@google.com}
15712787Sgabeblack@google.com
15812787Sgabeblack@google.comFiber *Fiber::currentFiber() { return _currentFiber; }
15912787Sgabeblack@google.comFiber *Fiber::primaryFiber() { return &_primaryFiber; }
160