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

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17