gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
51namespace gem5
52{
53
54namespace
55{
56
57/*
58 * The PrimaryFiber class is a special case that attaches to the currently
59 * executing context. That makes handling the "primary" fiber, aka the one
60 * which most of gem5 is running under, no different than other Fibers.
61 */
62class PrimaryFiber : public Fiber
63{
64 public:
65 PrimaryFiber() : Fiber(nullptr, 0) { setStarted(); }
66 void main() { panic("PrimaryFiber main executed.\n"); }
67};
68
69PrimaryFiber _primaryFiber;
70
71// A pointer to whatever the currently executing Fiber is.
72Fiber *_currentFiber = &_primaryFiber;
73
74// A pointer to the Fiber which is currently being started/initialized.
75Fiber *startingFiber = nullptr;
76
77} // anonymous namespace
78
79void
81{
82 startingFiber->start();
83}
84
85Fiber::Fiber(size_t stack_size) : Fiber(primaryFiber(), stack_size)
86{}
87
88Fiber::Fiber(Fiber *link, size_t stack_size) :
89 link(link), stack(nullptr), stackSize(stack_size), guardPage(nullptr),
90 guardPageSize(sysconf(_SC_PAGE_SIZE)), _started(false), _finished(false)
91{
92 if (stack_size) {
93 guardPage = mmap(nullptr, guardPageSize + stack_size,
94 PROT_READ | PROT_WRITE,
95 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
96 if (guardPage == (void *)MAP_FAILED) {
97 perror("mmap");
98 fatal("Could not mmap %d byte fiber stack.\n", stack_size);
99 }
100 stack = (void *)((uint8_t *)guardPage + guardPageSize);
101 if (mprotect(guardPage, guardPageSize, PROT_NONE)) {
102 perror("mprotect");
103 fatal("Could not forbid access to fiber stack guard page.");
104 }
105 }
106#if HAVE_VALGRIND
107 valgrindStackId = VALGRIND_STACK_REGISTER(
108 stack, (uint8_t *)stack + stack_size);
109#endif
110}
111
113{
114 panic_if(stack && _currentFiber == this, "Fiber stack is in use.");
115#if HAVE_VALGRIND
116 VALGRIND_STACK_DEREGISTER(valgrindStackId);
117#endif
118 if (guardPage)
120}
121
122void
124{
125 // Set up a context for the new fiber, starting it in the trampoline.
126 getcontext(&ctx);
127 ctx.uc_stack.ss_sp = stack;
128 ctx.uc_stack.ss_size = stackSize;
129 ctx.uc_link = nullptr;
130 makecontext(&ctx, &entryTrampoline, 0);
131
132 // Swap to the new context so it can enter its start() function. It
133 // will then swap itself back out and return here.
134 startingFiber = this;
135 panic_if(!_currentFiber, "No active Fiber object.");
136 swapcontext(&_currentFiber->ctx, &ctx);
137
138 // The new context is now ready and about to call main().
139}
140
141void
143{
144 // Avoid a dangling pointer.
145 startingFiber = nullptr;
146
147 setStarted();
148
149 if (_setjmp(jmp) == 0) {
150 // Swap back to the parent context which is still considered "current",
151 // now that we're ready to go.
152 int ret = swapcontext(&ctx, &_currentFiber->ctx);
153 panic_if(ret == -1, strerror(errno));
154 }
155
156 // Call main() when we're been reactivated for the first time.
157 main();
158
159 // main has returned, so this Fiber has finished. Switch to the "link"
160 // Fiber.
161 _finished = true;
162 link->run();
163}
164
165void
167{
168 panic_if(_finished, "Fiber has already run to completion.");
169
170 // If we're already running this fiber, we're done.
171 if (_currentFiber == this)
172 return;
173
174 if (!_started)
176
177 // Switch out of the current Fiber's context and this one's in.
178 Fiber *prev = _currentFiber;
179 Fiber *next = this;
180 _currentFiber = next;
181 if (_setjmp(prev->jmp) == 0)
182 _longjmp(next->jmp, 1);
183}
184
185Fiber *Fiber::currentFiber() { return _currentFiber; }
186Fiber *Fiber::primaryFiber() { return &_primaryFiber; }
187
188} // namespace gem5
This class represents a fiber, which is a light weight sort of thread which is cooperatively schedule...
Definition fiber.hh:72
void * guardPage
Definition fiber.hh:158
size_t guardPageSize
Definition fiber.hh:159
bool _started
Definition fiber.hh:164
jmp_buf jmp
Definition fiber.hh:151
size_t stackSize
Definition fiber.hh:157
virtual void main()=0
This method is called when this fiber is first run.
static void entryTrampoline()
Definition fiber.cc:80
void * stack
Definition fiber.hh:156
Fiber * link
Definition fiber.hh:153
void createContext()
Definition fiber.cc:123
void setStarted()
Definition fiber.hh:142
bool _finished
Definition fiber.hh:165
void start()
Definition fiber.cc:142
ucontext_t ctx
Definition fiber.hh:148
int main()
static Fiber * currentFiber()
Get a pointer to the current running Fiber.
Definition fiber.cc:185
static Fiber * primaryFiber()
Get a pointer to the primary Fiber.
Definition fiber.cc:186
Fiber(size_t stack_size=DefaultStackSize)
Definition fiber.cc:85
void run()
Start executing the fiber represented by this object.
Definition fiber.cc:166
virtual ~Fiber()
Definition fiber.cc:112
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
#define fatal(...)
This implements a cprintf based fatal() function.
Definition logging.hh:200
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:214
Bitfield< 17, 16 > stack
Definition misc.hh:602
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0