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