| 1 |
/** |
| 2 |
* The fiber module provides lightweight threads aka fibers. |
| 3 |
* |
| 4 |
* Copyright: Copyright Sean Kelly 2005 - 2012. |
| 5 |
* License: Distributed under the |
| 6 |
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0). |
| 7 |
* (See accompanying file LICENSE) |
| 8 |
* Authors: Sean Kelly, Walter Bright, Alex Rønne Petersen, Martin Nowak |
| 9 |
* Source: $(DRUNTIMESRC core/thread/fiber/package.d) |
| 10 |
*/ |
| 11 |
|
| 12 |
/* NOTE: This file has been patched from the original DMD distribution to |
| 13 |
* work with the GDC compiler. |
| 14 |
*/ |
| 15 |
module core.thread.fiber; |
| 16 |
|
| 17 |
import core.thread.context; |
| 18 |
import core.thread.fiber.base : fiber_entryPoint, FiberBase; |
| 19 |
import core.thread.threadbase; |
| 20 |
import core.thread.threadgroup; |
| 21 |
import core.thread.types; |
| 22 |
|
| 23 |
import core.memory : pageSize; |
| 24 |
|
| 25 |
/////////////////////////////////////////////////////////////////////////////// |
| 26 |
// Fiber Platform Detection |
| 27 |
/////////////////////////////////////////////////////////////////////////////// |
| 28 |
|
| 29 |
version (GNU) |
| 30 |
{ |
| 31 |
import gcc.builtins; |
| 32 |
version (GNU_StackGrowsDown) |
| 33 |
version = StackGrowsDown; |
| 34 |
} |
| 35 |
else |
| 36 |
{ |
| 37 |
// this should be true for most architectures |
| 38 |
version = StackGrowsDown; |
| 39 |
} |
| 40 |
|
| 41 |
version (Windows) |
| 42 |
{ |
| 43 |
import core.stdc.stdlib : free, malloc; |
| 44 |
import core.sys.windows.winbase; |
| 45 |
import core.sys.windows.winnt; |
| 46 |
} |
| 47 |
|
| 48 |
package |
| 49 |
{ |
| 50 |
version (D_InlineAsm_X86) |
| 51 |
{ |
| 52 |
version (Windows) |
| 53 |
version = AsmX86_Windows; |
| 54 |
else version (Posix) |
| 55 |
version = AsmX86_Posix; |
| 56 |
|
| 57 |
version = AlignFiberStackTo16Byte; |
| 58 |
} |
| 59 |
else version (D_InlineAsm_X86_64) |
| 60 |
{ |
| 61 |
version (Windows) |
| 62 |
{ |
| 63 |
version = AsmX86_64_Windows; |
| 64 |
version = AlignFiberStackTo16Byte; |
| 65 |
} |
| 66 |
else version (Posix) |
| 67 |
{ |
| 68 |
version = AsmX86_64_Posix; |
| 69 |
version = AlignFiberStackTo16Byte; |
| 70 |
} |
| 71 |
} |
| 72 |
else version (X86) |
| 73 |
{ |
| 74 |
version = AlignFiberStackTo16Byte; |
| 75 |
|
| 76 |
version (GNU_CET) |
| 77 |
{ |
| 78 |
// fiber_switchContext does not support shadow stack from |
| 79 |
// Intel CET. So use ucontext implementation. |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
version = AsmExternal; |
| 84 |
|
| 85 |
version (MinGW) |
| 86 |
version = GNU_AsmX86_Windows; |
| 87 |
else version (OSX) |
| 88 |
version = AsmX86_Posix; |
| 89 |
else version (Posix) |
| 90 |
version = AsmX86_Posix; |
| 91 |
} |
| 92 |
} |
| 93 |
else version (X86_64) |
| 94 |
{ |
| 95 |
version = AlignFiberStackTo16Byte; |
| 96 |
|
| 97 |
version (GNU_CET) |
| 98 |
{ |
| 99 |
// fiber_switchContext does not support shadow stack from |
| 100 |
// Intel CET. So use ucontext implementation. |
| 101 |
} |
| 102 |
else version (D_X32) |
| 103 |
{ |
| 104 |
// let X32 be handled by ucontext swapcontext |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
version = AsmExternal; |
| 109 |
|
| 110 |
version (MinGW) |
| 111 |
version = GNU_AsmX86_64_Windows; |
| 112 |
else version (OSX) |
| 113 |
version = AsmX86_64_Posix; |
| 114 |
else version (Posix) |
| 115 |
version = AsmX86_64_Posix; |
| 116 |
} |
| 117 |
} |
| 118 |
else version (PPC) |
| 119 |
{ |
| 120 |
version (OSX) |
| 121 |
{ |
| 122 |
version = AsmPPC_Darwin; |
| 123 |
version = AsmExternal; |
| 124 |
version = AlignFiberStackTo16Byte; |
| 125 |
} |
| 126 |
else version (Posix) |
| 127 |
{ |
| 128 |
version = AsmPPC_Posix; |
| 129 |
version = AsmExternal; |
| 130 |
} |
| 131 |
} |
| 132 |
else version (PPC64) |
| 133 |
{ |
| 134 |
version (OSX) |
| 135 |
{ |
| 136 |
version = AsmPPC_Darwin; |
| 137 |
version = AsmExternal; |
| 138 |
version = AlignFiberStackTo16Byte; |
| 139 |
} |
| 140 |
else version (Posix) |
| 141 |
{ |
| 142 |
version = AlignFiberStackTo16Byte; |
| 143 |
} |
| 144 |
} |
| 145 |
else version (MIPS_O32) |
| 146 |
{ |
| 147 |
version (Posix) |
| 148 |
{ |
| 149 |
version = AsmMIPS_O32_Posix; |
| 150 |
version = AsmExternal; |
| 151 |
} |
| 152 |
} |
| 153 |
else version (MIPS_N64) |
| 154 |
{ |
| 155 |
version (Posix) |
| 156 |
{ |
| 157 |
version = AsmMIPS_N64_Posix; |
| 158 |
version = AsmExternal; |
| 159 |
} |
| 160 |
} |
| 161 |
else version (AArch64) |
| 162 |
{ |
| 163 |
version (Posix) |
| 164 |
{ |
| 165 |
version = AsmAArch64_Posix; |
| 166 |
version = AsmExternal; |
| 167 |
version = AlignFiberStackTo16Byte; |
| 168 |
} |
| 169 |
} |
| 170 |
else version (ARM) |
| 171 |
{ |
| 172 |
version (Posix) |
| 173 |
{ |
| 174 |
version = AsmARM_Posix; |
| 175 |
version = AsmExternal; |
| 176 |
} |
| 177 |
} |
| 178 |
else version (SPARC) |
| 179 |
{ |
| 180 |
// NOTE: The SPARC ABI specifies only doubleword alignment. |
| 181 |
version = AlignFiberStackTo16Byte; |
| 182 |
} |
| 183 |
else version (SPARC64) |
| 184 |
{ |
| 185 |
version = AlignFiberStackTo16Byte; |
| 186 |
} |
| 187 |
else version (LoongArch64) |
| 188 |
{ |
| 189 |
version (Posix) |
| 190 |
{ |
| 191 |
version = AsmLoongArch64_Posix; |
| 192 |
version = AsmExternal; |
| 193 |
version = AlignFiberStackTo16Byte; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
version (Posix) |
| 198 |
{ |
| 199 |
version (AsmX86_Windows) {} else |
| 200 |
version (AsmX86_Posix) {} else |
| 201 |
version (AsmX86_64_Windows) {} else |
| 202 |
version (AsmX86_64_Posix) {} else |
| 203 |
version (AsmExternal) {} else |
| 204 |
{ |
| 205 |
// NOTE: The ucontext implementation requires architecture specific |
| 206 |
// data definitions to operate so testing for it must be done |
| 207 |
// by checking for the existence of ucontext_t rather than by |
| 208 |
// a version identifier. Please note that this is considered |
| 209 |
// an obsolescent feature according to the POSIX spec, so a |
| 210 |
// custom solution is still preferred. |
| 211 |
import core.sys.posix.ucontext : getcontext, makecontext, MINSIGSTKSZ, swapcontext, ucontext_t; |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/////////////////////////////////////////////////////////////////////////////// |
| 217 |
// Fiber Entry Point and Context Switch |
| 218 |
/////////////////////////////////////////////////////////////////////////////// |
| 219 |
|
| 220 |
package |
| 221 |
{ |
| 222 |
import core.atomic : atomicStore, cas, MemoryOrder; |
| 223 |
import core.exception : onOutOfMemoryError; |
| 224 |
import core.stdc.stdlib : abort; |
| 225 |
|
| 226 |
// Look above the definition of 'class Fiber' for some information about the implementation of this routine |
| 227 |
version (AsmExternal) |
| 228 |
{ |
| 229 |
extern (C) void fiber_switchContext( void** oldp, void* newp ) nothrow @nogc; |
| 230 |
version (AArch64) |
| 231 |
extern (C) void fiber_trampoline() nothrow; |
| 232 |
} |
| 233 |
else |
| 234 |
extern (C) void fiber_switchContext( void** oldp, void* newp ) nothrow @nogc |
| 235 |
{ |
| 236 |
// NOTE: The data pushed and popped in this routine must match the |
| 237 |
// default stack created by Fiber.initStack or the initial |
| 238 |
// switch into a new context will fail. |
| 239 |
|
| 240 |
version (AsmX86_Windows) |
| 241 |
{ |
| 242 |
asm pure nothrow @nogc |
| 243 |
{ |
| 244 |
naked; |
| 245 |
|
| 246 |
// save current stack state |
| 247 |
push EBP; |
| 248 |
mov EBP, ESP; |
| 249 |
push EDI; |
| 250 |
push ESI; |
| 251 |
push EBX; |
| 252 |
push dword ptr FS:[0]; |
| 253 |
push dword ptr FS:[4]; |
| 254 |
push dword ptr FS:[8]; |
| 255 |
push EAX; |
| 256 |
|
| 257 |
// store oldp again with more accurate address |
| 258 |
mov EAX, dword ptr 8[EBP]; |
| 259 |
mov [EAX], ESP; |
| 260 |
// load newp to begin context switch |
| 261 |
mov ESP, dword ptr 12[EBP]; |
| 262 |
|
| 263 |
// load saved state from new stack |
| 264 |
pop EAX; |
| 265 |
pop dword ptr FS:[8]; |
| 266 |
pop dword ptr FS:[4]; |
| 267 |
pop dword ptr FS:[0]; |
| 268 |
pop EBX; |
| 269 |
pop ESI; |
| 270 |
pop EDI; |
| 271 |
pop EBP; |
| 272 |
|
| 273 |
// 'return' to complete switch |
| 274 |
pop ECX; |
| 275 |
jmp ECX; |
| 276 |
} |
| 277 |
} |
| 278 |
else version (AsmX86_64_Windows) |
| 279 |
{ |
| 280 |
asm pure nothrow @nogc |
| 281 |
{ |
| 282 |
naked; |
| 283 |
|
| 284 |
// save current stack state |
| 285 |
// NOTE: When changing the layout of registers on the stack, |
| 286 |
// make sure that the XMM registers are still aligned. |
| 287 |
// On function entry, the stack is guaranteed to not |
| 288 |
// be aligned to 16 bytes because of the return address |
| 289 |
// on the stack. |
| 290 |
push RBP; |
| 291 |
mov RBP, RSP; |
| 292 |
push R12; |
| 293 |
push R13; |
| 294 |
push R14; |
| 295 |
push R15; |
| 296 |
push RDI; |
| 297 |
push RSI; |
| 298 |
// 7 registers = 56 bytes; stack is now aligned to 16 bytes |
| 299 |
sub RSP, 160; |
| 300 |
movdqa [RSP + 144], XMM6; |
| 301 |
movdqa [RSP + 128], XMM7; |
| 302 |
movdqa [RSP + 112], XMM8; |
| 303 |
movdqa [RSP + 96], XMM9; |
| 304 |
movdqa [RSP + 80], XMM10; |
| 305 |
movdqa [RSP + 64], XMM11; |
| 306 |
movdqa [RSP + 48], XMM12; |
| 307 |
movdqa [RSP + 32], XMM13; |
| 308 |
movdqa [RSP + 16], XMM14; |
| 309 |
movdqa [RSP], XMM15; |
| 310 |
push RBX; |
| 311 |
xor RAX,RAX; |
| 312 |
push qword ptr GS:[RAX]; |
| 313 |
push qword ptr GS:8[RAX]; |
| 314 |
push qword ptr GS:16[RAX]; |
| 315 |
|
| 316 |
// store oldp |
| 317 |
mov [RCX], RSP; |
| 318 |
// load newp to begin context switch |
| 319 |
mov RSP, RDX; |
| 320 |
|
| 321 |
// load saved state from new stack |
| 322 |
pop qword ptr GS:16[RAX]; |
| 323 |
pop qword ptr GS:8[RAX]; |
| 324 |
pop qword ptr GS:[RAX]; |
| 325 |
pop RBX; |
| 326 |
movdqa XMM15, [RSP]; |
| 327 |
movdqa XMM14, [RSP + 16]; |
| 328 |
movdqa XMM13, [RSP + 32]; |
| 329 |
movdqa XMM12, [RSP + 48]; |
| 330 |
movdqa XMM11, [RSP + 64]; |
| 331 |
movdqa XMM10, [RSP + 80]; |
| 332 |
movdqa XMM9, [RSP + 96]; |
| 333 |
movdqa XMM8, [RSP + 112]; |
| 334 |
movdqa XMM7, [RSP + 128]; |
| 335 |
movdqa XMM6, [RSP + 144]; |
| 336 |
add RSP, 160; |
| 337 |
pop RSI; |
| 338 |
pop RDI; |
| 339 |
pop R15; |
| 340 |
pop R14; |
| 341 |
pop R13; |
| 342 |
pop R12; |
| 343 |
pop RBP; |
| 344 |
|
| 345 |
// 'return' to complete switch |
| 346 |
pop RCX; |
| 347 |
jmp RCX; |
| 348 |
} |
| 349 |
} |
| 350 |
else version (AsmX86_Posix) |
| 351 |
{ |
| 352 |
asm pure nothrow @nogc |
| 353 |
{ |
| 354 |
naked; |
| 355 |
|
| 356 |
// save current stack state |
| 357 |
push EBP; |
| 358 |
mov EBP, ESP; |
| 359 |
push EDI; |
| 360 |
push ESI; |
| 361 |
push EBX; |
| 362 |
push EAX; |
| 363 |
|
| 364 |
// store oldp again with more accurate address |
| 365 |
mov EAX, dword ptr 8[EBP]; |
| 366 |
mov [EAX], ESP; |
| 367 |
// load newp to begin context switch |
| 368 |
mov ESP, dword ptr 12[EBP]; |
| 369 |
|
| 370 |
// load saved state from new stack |
| 371 |
pop EAX; |
| 372 |
pop EBX; |
| 373 |
pop ESI; |
| 374 |
pop EDI; |
| 375 |
pop EBP; |
| 376 |
|
| 377 |
// 'return' to complete switch |
| 378 |
pop ECX; |
| 379 |
jmp ECX; |
| 380 |
} |
| 381 |
} |
| 382 |
else version (AsmX86_64_Posix) |
| 383 |
{ |
| 384 |
asm pure nothrow @nogc |
| 385 |
{ |
| 386 |
naked; |
| 387 |
|
| 388 |
// save current stack state |
| 389 |
push RBP; |
| 390 |
mov RBP, RSP; |
| 391 |
push RBX; |
| 392 |
push R12; |
| 393 |
push R13; |
| 394 |
push R14; |
| 395 |
push R15; |
| 396 |
|
| 397 |
// store oldp |
| 398 |
mov [RDI], RSP; |
| 399 |
// load newp to begin context switch |
| 400 |
mov RSP, RSI; |
| 401 |
|
| 402 |
// load saved state from new stack |
| 403 |
pop R15; |
| 404 |
pop R14; |
| 405 |
pop R13; |
| 406 |
pop R12; |
| 407 |
pop RBX; |
| 408 |
pop RBP; |
| 409 |
|
| 410 |
// 'return' to complete switch |
| 411 |
pop RCX; |
| 412 |
jmp RCX; |
| 413 |
} |
| 414 |
} |
| 415 |
else static if ( __traits( compiles, ucontext_t ) ) |
| 416 |
{ |
| 417 |
Fiber cfib = Fiber.getThis(); |
| 418 |
void* ucur = cfib.m_ucur; |
| 419 |
|
| 420 |
*oldp = &ucur; |
| 421 |
swapcontext( **(cast(ucontext_t***) oldp), |
| 422 |
*(cast(ucontext_t**) newp) ); |
| 423 |
} |
| 424 |
else |
| 425 |
static assert(0, "Not implemented"); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
/////////////////////////////////////////////////////////////////////////////// |
| 431 |
// Fiber |
| 432 |
/////////////////////////////////////////////////////////////////////////////// |
| 433 |
/* |
| 434 |
* Documentation of Fiber internals: |
| 435 |
* |
| 436 |
* The main routines to implement when porting Fibers to new architectures are |
| 437 |
* fiber_switchContext and initStack. Some version constants have to be defined |
| 438 |
* for the new platform as well, search for "Fiber Platform Detection and Memory Allocation". |
| 439 |
* |
| 440 |
* Fibers are based on a concept called 'Context'. A Context describes the execution |
| 441 |
* state of a Fiber or main thread which is fully described by the stack, some |
| 442 |
* registers and a return address at which the Fiber/Thread should continue executing. |
| 443 |
* Please note that not only each Fiber has a Context, but each thread also has got a |
| 444 |
* Context which describes the threads stack and state. If you call Fiber fib; fib.call |
| 445 |
* the first time in a thread you switch from Threads Context into the Fibers Context. |
| 446 |
* If you call fib.yield in that Fiber you switch out of the Fibers context and back |
| 447 |
* into the Thread Context. (However, this is not always the case. You can call a Fiber |
| 448 |
* from within another Fiber, then you switch Contexts between the Fibers and the Thread |
| 449 |
* Context is not involved) |
| 450 |
* |
| 451 |
* In all current implementations the registers and the return address are actually |
| 452 |
* saved on a Contexts stack. |
| 453 |
* |
| 454 |
* The fiber_switchContext routine has got two parameters: |
| 455 |
* void** a: This is the _location_ where we have to store the current stack pointer, |
| 456 |
* the stack pointer of the currently executing Context (Fiber or Thread). |
| 457 |
* void* b: This is the pointer to the stack of the Context which we want to switch into. |
| 458 |
* Note that we get the same pointer here as the one we stored into the void** a |
| 459 |
* in a previous call to fiber_switchContext. |
| 460 |
* |
| 461 |
* In the simplest case, a fiber_switchContext rountine looks like this: |
| 462 |
* fiber_switchContext: |
| 463 |
* push {return Address} |
| 464 |
* push {registers} |
| 465 |
* copy {stack pointer} into {location pointed to by a} |
| 466 |
* //We have now switch to the stack of a different Context! |
| 467 |
* copy {b} into {stack pointer} |
| 468 |
* pop {registers} |
| 469 |
* pop {return Address} |
| 470 |
* jump to {return Address} |
| 471 |
* |
| 472 |
* The GC uses the value returned in parameter a to scan the Fibers stack. It scans from |
| 473 |
* the stack base to that value. As the GC dislikes false pointers we can actually optimize |
| 474 |
* this a little: By storing registers which can not contain references to memory managed |
| 475 |
* by the GC outside of the region marked by the stack base pointer and the stack pointer |
| 476 |
* saved in fiber_switchContext we can prevent the GC from scanning them. |
| 477 |
* Such registers are usually floating point registers and the return address. In order to |
| 478 |
* implement this, we return a modified stack pointer from fiber_switchContext. However, |
| 479 |
* we have to remember that when we restore the registers from the stack! |
| 480 |
* |
| 481 |
* --------------------------- <= Stack Base |
| 482 |
* | Frame | <= Many other stack frames |
| 483 |
* | Frame | |
| 484 |
* |-------------------------| <= The last stack frame. This one is created by fiber_switchContext |
| 485 |
* | registers with pointers | |
| 486 |
* | | <= Stack pointer. GC stops scanning here |
| 487 |
* | return address | |
| 488 |
* |floating point registers | |
| 489 |
* --------------------------- <= Real Stack End |
| 490 |
* |
| 491 |
* fiber_switchContext: |
| 492 |
* push {registers with pointers} |
| 493 |
* copy {stack pointer} into {location pointed to by a} |
| 494 |
* push {return Address} |
| 495 |
* push {Floating point registers} |
| 496 |
* //We have now switch to the stack of a different Context! |
| 497 |
* copy {b} into {stack pointer} |
| 498 |
* //We now have to adjust the stack pointer to point to 'Real Stack End' so we can pop |
| 499 |
* //the FP registers |
| 500 |
* //+ or - depends on if your stack grows downwards or upwards |
| 501 |
* {stack pointer} = {stack pointer} +- ({FPRegisters}.sizeof + {return address}.sizeof} |
| 502 |
* pop {Floating point registers} |
| 503 |
* pop {return Address} |
| 504 |
* pop {registers with pointers} |
| 505 |
* jump to {return Address} |
| 506 |
* |
| 507 |
* So the question now is which registers need to be saved? This depends on the specific |
| 508 |
* architecture ABI of course, but here are some general guidelines: |
| 509 |
* - If a register is callee-save (if the callee modifies the register it must saved and |
| 510 |
* restored by the callee) it needs to be saved/restored in switchContext |
| 511 |
* - If a register is caller-save it needn't be saved/restored. (Calling fiber_switchContext |
| 512 |
* is a function call and the compiler therefore already must save these registers before |
| 513 |
* calling fiber_switchContext) |
| 514 |
* - Argument registers used for passing parameters to functions needn't be saved/restored |
| 515 |
* - The return register needn't be saved/restored (fiber_switchContext hasn't got a return type) |
| 516 |
* - All scratch registers needn't be saved/restored |
| 517 |
* - The link register usually needn't be saved/restored (but sometimes it must be cleared - |
| 518 |
* see below for details) |
| 519 |
* - The frame pointer register - if it exists - is usually callee-save |
| 520 |
* - All current implementations do not save control registers |
| 521 |
* |
| 522 |
* What happens on the first switch into a Fiber? We never saved a state for this fiber before, |
| 523 |
* but the initial state is prepared in the initStack routine. (This routine will also be called |
| 524 |
* when a Fiber is being resetted). initStack must produce exactly the same stack layout as the |
| 525 |
* part of fiber_switchContext which saves the registers. Pay special attention to set the stack |
| 526 |
* pointer correctly if you use the GC optimization mentioned before. the return Address saved in |
| 527 |
* initStack must be the address of fiber_entrypoint. |
| 528 |
* |
| 529 |
* There's now a small but important difference between the first context switch into a fiber and |
| 530 |
* further context switches. On the first switch, Fiber.call is used and the returnAddress in |
| 531 |
* fiber_switchContext will point to fiber_entrypoint. The important thing here is that this jump |
| 532 |
* is a _function call_, we call fiber_entrypoint by jumping before it's function prologue. On later |
| 533 |
* calls, the user used yield() in a function, and therefore the return address points into a user |
| 534 |
* function, after the yield call. So here the jump in fiber_switchContext is a _function return_, |
| 535 |
* not a function call! |
| 536 |
* |
| 537 |
* The most important result of this is that on entering a function, i.e. fiber_entrypoint, we |
| 538 |
* would have to provide a return address / set the link register once fiber_entrypoint |
| 539 |
* returns. Now fiber_entrypoint does never return and therefore the actual value of the return |
| 540 |
* address / link register is never read/used and therefore doesn't matter. When fiber_switchContext |
| 541 |
* performs a _function return_ the value in the link register doesn't matter either. |
| 542 |
* However, the link register will still be saved to the stack in fiber_entrypoint and some |
| 543 |
* exception handling / stack unwinding code might read it from this stack location and crash. |
| 544 |
* The exact solution depends on your architecture, but see the ARM implementation for a way |
| 545 |
* to deal with this issue. |
| 546 |
* |
| 547 |
* The ARM implementation is meant to be used as a kind of documented example implementation. |
| 548 |
* Look there for a concrete example. |
| 549 |
* |
| 550 |
* FIXME: fiber_entrypoint might benefit from a @noreturn attribute, but D doesn't have one. |
| 551 |
*/ |
| 552 |
|
| 553 |
/** |
| 554 |
* This class provides a cooperative concurrency mechanism integrated with the |
| 555 |
* threading and garbage collection functionality. Calling a fiber may be |
| 556 |
* considered a blocking operation that returns when the fiber yields (via |
| 557 |
* Fiber.yield()). Execution occurs within the context of the calling thread |
| 558 |
* so synchronization is not necessary to guarantee memory visibility so long |
| 559 |
* as the same thread calls the fiber each time. Please note that there is no |
| 560 |
* requirement that a fiber be bound to one specific thread. Rather, fibers |
| 561 |
* may be freely passed between threads so long as they are not currently |
| 562 |
* executing. Like threads, a new fiber thread may be created using either |
| 563 |
* derivation or composition, as in the following example. |
| 564 |
* |
| 565 |
* Warning: |
| 566 |
* Status registers are not saved by the current implementations. This means |
| 567 |
* floating point exception status bits (overflow, divide by 0), rounding mode |
| 568 |
* and similar stuff is set per-thread, not per Fiber! |
| 569 |
* |
| 570 |
* Warning: |
| 571 |
* On ARM FPU registers are not saved if druntime was compiled as ARM_SoftFloat. |
| 572 |
* If such a build is used on a ARM_SoftFP system which actually has got a FPU |
| 573 |
* and other libraries are using the FPU registers (other code is compiled |
| 574 |
* as ARM_SoftFP) this can cause problems. Druntime must be compiled as |
| 575 |
* ARM_SoftFP in this case. |
| 576 |
* |
| 577 |
* Authors: Based on a design by Mikola Lysenko. |
| 578 |
*/ |
| 579 |
class Fiber : FiberBase |
| 580 |
{ |
| 581 |
/////////////////////////////////////////////////////////////////////////// |
| 582 |
// Initialization |
| 583 |
/////////////////////////////////////////////////////////////////////////// |
| 584 |
|
| 585 |
version (Windows) |
| 586 |
// exception handling walks the stack, invoking DbgHelp.dll which |
| 587 |
// needs up to 16k of stack space depending on the version of DbgHelp.dll, |
| 588 |
// the existence of debug symbols and other conditions. Avoid causing |
| 589 |
// stack overflows by defaulting to a larger stack size |
| 590 |
enum defaultStackPages = 8; |
| 591 |
else version (OSX) |
| 592 |
{ |
| 593 |
version (X86_64) |
| 594 |
// libunwind on macOS 11 now requires more stack space than 16k, so |
| 595 |
// default to a larger stack size. This is only applied to X86 as |
| 596 |
// the pageSize is still 4k, however on AArch64 it is 16k. |
| 597 |
enum defaultStackPages = 8; |
| 598 |
else |
| 599 |
enum defaultStackPages = 4; |
| 600 |
} |
| 601 |
else |
| 602 |
enum defaultStackPages = 4; |
| 603 |
|
| 604 |
/** |
| 605 |
* Initializes a fiber object which is associated with a static |
| 606 |
* D function. |
| 607 |
* |
| 608 |
* Params: |
| 609 |
* fn = The fiber function. |
| 610 |
* sz = The stack size for this fiber. |
| 611 |
* guardPageSize = size of the guard page to trap fiber's stack |
| 612 |
* overflows. Beware that using this will increase |
| 613 |
* the number of mmaped regions on platforms using mmap |
| 614 |
* so an OS-imposed limit may be hit. |
| 615 |
* |
| 616 |
* In: |
| 617 |
* fn must not be null. |
| 618 |
*/ |
| 619 |
this( void function() fn, size_t sz = pageSize * defaultStackPages, |
| 620 |
size_t guardPageSize = pageSize ) nothrow |
| 621 |
{ |
| 622 |
super( fn, sz, guardPageSize ); |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
/** |
| 627 |
* Initializes a fiber object which is associated with a dynamic |
| 628 |
* D function. |
| 629 |
* |
| 630 |
* Params: |
| 631 |
* dg = The fiber function. |
| 632 |
* sz = The stack size for this fiber. |
| 633 |
* guardPageSize = size of the guard page to trap fiber's stack |
| 634 |
* overflows. Beware that using this will increase |
| 635 |
* the number of mmaped regions on platforms using mmap |
| 636 |
* so an OS-imposed limit may be hit. |
| 637 |
* |
| 638 |
* In: |
| 639 |
* dg must not be null. |
| 640 |
*/ |
| 641 |
this( void delegate() dg, size_t sz = pageSize * defaultStackPages, |
| 642 |
size_t guardPageSize = pageSize ) nothrow |
| 643 |
{ |
| 644 |
super( dg, sz, guardPageSize ); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
/////////////////////////////////////////////////////////////////////////// |
| 649 |
// Fiber Accessors |
| 650 |
/////////////////////////////////////////////////////////////////////////// |
| 651 |
|
| 652 |
|
| 653 |
/** |
| 654 |
* Provides a reference to the calling fiber or null if no fiber is |
| 655 |
* currently active. |
| 656 |
* |
| 657 |
* Returns: |
| 658 |
* The fiber object representing the calling fiber or null if no fiber |
| 659 |
* is currently active within this thread. The result of deleting this object is undefined. |
| 660 |
*/ |
| 661 |
static Fiber getThis() @safe nothrow @nogc |
| 662 |
{ |
| 663 |
return cast(Fiber) FiberBase.getThis(); |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
/////////////////////////////////////////////////////////////////////////// |
| 668 |
// Static Initialization |
| 669 |
/////////////////////////////////////////////////////////////////////////// |
| 670 |
|
| 671 |
|
| 672 |
version (Posix) |
| 673 |
{ |
| 674 |
static this() |
| 675 |
{ |
| 676 |
static if ( __traits( compiles, ucontext_t ) ) |
| 677 |
{ |
| 678 |
int status = getcontext( &sm_utxt ); |
| 679 |
assert( status == 0 ); |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
protected: |
| 685 |
/////////////////////////////////////////////////////////////////////////// |
| 686 |
// Stack Management |
| 687 |
/////////////////////////////////////////////////////////////////////////// |
| 688 |
|
| 689 |
|
| 690 |
// |
| 691 |
// Allocate a new stack for this fiber. |
| 692 |
// |
| 693 |
final override void allocStack( size_t sz, size_t guardPageSize ) nothrow |
| 694 |
in |
| 695 |
{ |
| 696 |
assert( !m_pmem && !m_ctxt ); |
| 697 |
} |
| 698 |
do |
| 699 |
{ |
| 700 |
// adjust alloc size to a multiple of pageSize |
| 701 |
sz += pageSize - 1; |
| 702 |
sz -= sz % pageSize; |
| 703 |
|
| 704 |
// NOTE: This instance of Thread.Context is dynamic so Fiber objects |
| 705 |
// can be collected by the GC so long as no user level references |
| 706 |
// to the object exist. If m_ctxt were not dynamic then its |
| 707 |
// presence in the global context list would be enough to keep |
| 708 |
// this object alive indefinitely. An alternative to allocating |
| 709 |
// room for this struct explicitly would be to mash it into the |
| 710 |
// base of the stack being allocated below. However, doing so |
| 711 |
// requires too much special logic to be worthwhile. |
| 712 |
m_ctxt = new StackContext; |
| 713 |
|
| 714 |
version (Windows) |
| 715 |
{ |
| 716 |
// reserve memory for stack |
| 717 |
m_pmem = VirtualAlloc( null, |
| 718 |
sz + guardPageSize, |
| 719 |
MEM_RESERVE, |
| 720 |
PAGE_NOACCESS ); |
| 721 |
if ( !m_pmem ) |
| 722 |
onOutOfMemoryError(); |
| 723 |
|
| 724 |
version (StackGrowsDown) |
| 725 |
{ |
| 726 |
void* stack = m_pmem + guardPageSize; |
| 727 |
void* guard = m_pmem; |
| 728 |
void* pbase = stack + sz; |
| 729 |
} |
| 730 |
else |
| 731 |
{ |
| 732 |
void* stack = m_pmem; |
| 733 |
void* guard = m_pmem + sz; |
| 734 |
void* pbase = stack; |
| 735 |
} |
| 736 |
|
| 737 |
// allocate reserved stack segment |
| 738 |
stack = VirtualAlloc( stack, |
| 739 |
sz, |
| 740 |
MEM_COMMIT, |
| 741 |
PAGE_READWRITE ); |
| 742 |
if ( !stack ) |
| 743 |
onOutOfMemoryError(); |
| 744 |
|
| 745 |
if (guardPageSize) |
| 746 |
{ |
| 747 |
// allocate reserved guard page |
| 748 |
guard = VirtualAlloc( guard, |
| 749 |
guardPageSize, |
| 750 |
MEM_COMMIT, |
| 751 |
PAGE_READWRITE | PAGE_GUARD ); |
| 752 |
if ( !guard ) |
| 753 |
onOutOfMemoryError(); |
| 754 |
} |
| 755 |
|
| 756 |
m_ctxt.bstack = pbase; |
| 757 |
m_ctxt.tstack = pbase; |
| 758 |
m_size = sz; |
| 759 |
} |
| 760 |
else |
| 761 |
{ |
| 762 |
version (Posix) import core.sys.posix.sys.mman : MAP_ANON, MAP_FAILED, MAP_PRIVATE, mmap, |
| 763 |
mprotect, PROT_NONE, PROT_READ, PROT_WRITE; |
| 764 |
version (OpenBSD) import core.sys.posix.sys.mman : MAP_STACK; |
| 765 |
|
| 766 |
static if ( __traits( compiles, ucontext_t ) ) |
| 767 |
{ |
| 768 |
// Stack size must be at least the minimum allowable by the OS. |
| 769 |
if (sz < MINSIGSTKSZ) |
| 770 |
sz = MINSIGSTKSZ; |
| 771 |
} |
| 772 |
|
| 773 |
static if ( __traits( compiles, mmap ) ) |
| 774 |
{ |
| 775 |
// Allocate more for the memory guard |
| 776 |
sz += guardPageSize; |
| 777 |
|
| 778 |
int mmap_flags = MAP_PRIVATE | MAP_ANON; |
| 779 |
version (OpenBSD) |
| 780 |
mmap_flags |= MAP_STACK; |
| 781 |
|
| 782 |
m_pmem = mmap( null, |
| 783 |
sz, |
| 784 |
PROT_READ | PROT_WRITE, |
| 785 |
mmap_flags, |
| 786 |
-1, |
| 787 |
0 ); |
| 788 |
if ( m_pmem == MAP_FAILED ) |
| 789 |
m_pmem = null; |
| 790 |
} |
| 791 |
else static if ( __traits( compiles, valloc ) ) |
| 792 |
{ |
| 793 |
m_pmem = valloc( sz ); |
| 794 |
} |
| 795 |
else |
| 796 |
{ |
| 797 |
import core.stdc.stdlib : malloc; |
| 798 |
|
| 799 |
m_pmem = malloc( sz ); |
| 800 |
} |
| 801 |
|
| 802 |
if ( !m_pmem ) |
| 803 |
onOutOfMemoryError(); |
| 804 |
|
| 805 |
version (StackGrowsDown) |
| 806 |
{ |
| 807 |
m_ctxt.bstack = m_pmem + sz; |
| 808 |
m_ctxt.tstack = m_pmem + sz; |
| 809 |
void* guard = m_pmem; |
| 810 |
} |
| 811 |
else |
| 812 |
{ |
| 813 |
m_ctxt.bstack = m_pmem; |
| 814 |
m_ctxt.tstack = m_pmem; |
| 815 |
void* guard = m_pmem + sz - guardPageSize; |
| 816 |
} |
| 817 |
m_size = sz; |
| 818 |
|
| 819 |
static if ( __traits( compiles, mmap ) ) |
| 820 |
{ |
| 821 |
if (guardPageSize) |
| 822 |
{ |
| 823 |
// protect end of stack |
| 824 |
if ( mprotect(guard, guardPageSize, PROT_NONE) == -1 ) |
| 825 |
abort(); |
| 826 |
} |
| 827 |
} |
| 828 |
else |
| 829 |
{ |
| 830 |
// Supported only for mmap allocated memory - results are |
| 831 |
// undefined if applied to memory not obtained by mmap |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
ThreadBase.add( m_ctxt ); |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
// |
| 840 |
// Free this fiber's stack. |
| 841 |
// |
| 842 |
final override void freeStack() nothrow @nogc |
| 843 |
in(m_pmem) |
| 844 |
in(m_ctxt) |
| 845 |
{ |
| 846 |
// NOTE: m_ctxt is guaranteed to be alive because it is held in the |
| 847 |
// global context list. |
| 848 |
ThreadBase.slock.lock_nothrow(); |
| 849 |
scope(exit) ThreadBase.slock.unlock_nothrow(); |
| 850 |
ThreadBase.remove( m_ctxt ); |
| 851 |
|
| 852 |
version (Windows) |
| 853 |
{ |
| 854 |
VirtualFree( m_pmem, 0, MEM_RELEASE ); |
| 855 |
} |
| 856 |
else |
| 857 |
{ |
| 858 |
import core.sys.posix.sys.mman : mmap, munmap; |
| 859 |
|
| 860 |
static if ( __traits( compiles, mmap ) ) |
| 861 |
{ |
| 862 |
munmap( m_pmem, m_size ); |
| 863 |
} |
| 864 |
else |
| 865 |
{ |
| 866 |
free( m_pmem ); |
| 867 |
} |
| 868 |
} |
| 869 |
m_pmem = null; |
| 870 |
m_ctxt = null; |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
// |
| 875 |
// Initialize the allocated stack. |
| 876 |
// Look above the definition of 'class Fiber' for some information about the implementation of this routine |
| 877 |
// |
| 878 |
final override void initStack() nothrow @nogc |
| 879 |
in |
| 880 |
{ |
| 881 |
assert( m_ctxt.tstack && m_ctxt.tstack == m_ctxt.bstack ); |
| 882 |
assert( cast(size_t) m_ctxt.bstack % (void*).sizeof == 0 ); |
| 883 |
} |
| 884 |
do |
| 885 |
{ |
| 886 |
void* pstack = m_ctxt.tstack; |
| 887 |
scope( exit ) m_ctxt.tstack = pstack; |
| 888 |
|
| 889 |
void push( size_t val ) nothrow |
| 890 |
{ |
| 891 |
version (StackGrowsDown) |
| 892 |
{ |
| 893 |
pstack -= size_t.sizeof; |
| 894 |
*(cast(size_t*) pstack) = val; |
| 895 |
} |
| 896 |
else |
| 897 |
{ |
| 898 |
pstack += size_t.sizeof; |
| 899 |
*(cast(size_t*) pstack) = val; |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
// NOTE: On OS X the stack must be 16-byte aligned according |
| 904 |
// to the IA-32 call spec. For x86_64 the stack also needs to |
| 905 |
// be aligned to 16-byte according to SysV AMD64 ABI. |
| 906 |
version (AlignFiberStackTo16Byte) |
| 907 |
{ |
| 908 |
version (StackGrowsDown) |
| 909 |
{ |
| 910 |
pstack = cast(void*)(cast(size_t)(pstack) - (cast(size_t)(pstack) & 0x0F)); |
| 911 |
} |
| 912 |
else |
| 913 |
{ |
| 914 |
pstack = cast(void*)(cast(size_t)(pstack) + (cast(size_t)(pstack) & 0x0F)); |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
version (AsmX86_Windows) |
| 919 |
{ |
| 920 |
version (StackGrowsDown) {} else static assert( false ); |
| 921 |
|
| 922 |
// On Windows Server 2008 and 2008 R2, an exploit mitigation |
| 923 |
// technique known as SEHOP is activated by default. To avoid |
| 924 |
// hijacking of the exception handler chain, the presence of a |
| 925 |
// Windows-internal handler (ntdll.dll!FinalExceptionHandler) at |
| 926 |
// its end is tested by RaiseException. If it is not present, all |
| 927 |
// handlers are disregarded, and the program is thus aborted |
| 928 |
// (see http://blogs.technet.com/b/srd/archive/2009/02/02/ |
| 929 |
// preventing-the-exploitation-of-seh-overwrites-with-sehop.aspx). |
| 930 |
// For new threads, this handler is installed by Windows immediately |
| 931 |
// after creation. To make exception handling work in fibers, we |
| 932 |
// have to insert it for our new stacks manually as well. |
| 933 |
// |
| 934 |
// To do this, we first determine the handler by traversing the SEH |
| 935 |
// chain of the current thread until its end, and then construct a |
| 936 |
// registration block for the last handler on the newly created |
| 937 |
// thread. We then continue to push all the initial register values |
| 938 |
// for the first context switch as for the other implementations. |
| 939 |
// |
| 940 |
// Note that this handler is never actually invoked, as we install |
| 941 |
// our own one on top of it in the fiber entry point function. |
| 942 |
// Thus, it should not have any effects on OSes not implementing |
| 943 |
// exception chain verification. |
| 944 |
|
| 945 |
alias fp_t = void function(); // Actual signature not relevant. |
| 946 |
static struct EXCEPTION_REGISTRATION |
| 947 |
{ |
| 948 |
EXCEPTION_REGISTRATION* next; // sehChainEnd if last one. |
| 949 |
fp_t handler; |
| 950 |
} |
| 951 |
enum sehChainEnd = cast(EXCEPTION_REGISTRATION*) 0xFFFFFFFF; |
| 952 |
|
| 953 |
__gshared fp_t finalHandler = null; |
| 954 |
if ( finalHandler is null ) |
| 955 |
{ |
| 956 |
static EXCEPTION_REGISTRATION* fs0() nothrow |
| 957 |
{ |
| 958 |
asm pure nothrow @nogc |
| 959 |
{ |
| 960 |
naked; |
| 961 |
mov EAX, FS:[0]; |
| 962 |
ret; |
| 963 |
} |
| 964 |
} |
| 965 |
auto reg = fs0(); |
| 966 |
while ( reg.next != sehChainEnd ) reg = reg.next; |
| 967 |
|
| 968 |
// Benign races are okay here, just to avoid re-lookup on every |
| 969 |
// fiber creation. |
| 970 |
finalHandler = reg.handler; |
| 971 |
} |
| 972 |
|
| 973 |
// When linking with /safeseh (supported by LDC, but not DMD) |
| 974 |
// the exception chain must not extend to the very top |
| 975 |
// of the stack, otherwise the exception chain is also considered |
| 976 |
// invalid. Reserving additional 4 bytes at the top of the stack will |
| 977 |
// keep the EXCEPTION_REGISTRATION below that limit |
| 978 |
size_t reserve = EXCEPTION_REGISTRATION.sizeof + 4; |
| 979 |
pstack -= reserve; |
| 980 |
*(cast(EXCEPTION_REGISTRATION*)pstack) = |
| 981 |
EXCEPTION_REGISTRATION( sehChainEnd, finalHandler ); |
| 982 |
auto pChainEnd = pstack; |
| 983 |
|
| 984 |
push( cast(size_t) &fiber_entryPoint ); // EIP |
| 985 |
push( cast(size_t) m_ctxt.bstack - reserve ); // EBP |
| 986 |
push( 0x00000000 ); // EDI |
| 987 |
push( 0x00000000 ); // ESI |
| 988 |
push( 0x00000000 ); // EBX |
| 989 |
push( cast(size_t) pChainEnd ); // FS:[0] |
| 990 |
push( cast(size_t) m_ctxt.bstack ); // FS:[4] |
| 991 |
push( cast(size_t) m_ctxt.bstack - m_size ); // FS:[8] |
| 992 |
push( 0x00000000 ); // EAX |
| 993 |
} |
| 994 |
else version (AsmX86_64_Windows) |
| 995 |
{ |
| 996 |
// Using this trampoline instead of the raw fiber_entryPoint |
| 997 |
// ensures that during context switches, source and destination |
| 998 |
// stacks have the same alignment. Otherwise, the stack would need |
| 999 |
// to be shifted by 8 bytes for the first call, as fiber_entryPoint |
| 1000 |
// is an actual function expecting a stack which is not aligned |
| 1001 |
// to 16 bytes. |
| 1002 |
static void trampoline() |
| 1003 |
{ |
| 1004 |
asm pure nothrow @nogc |
| 1005 |
{ |
| 1006 |
naked; |
| 1007 |
sub RSP, 32; // Shadow space (Win64 calling convention) |
| 1008 |
call fiber_entryPoint; |
| 1009 |
xor RCX, RCX; // This should never be reached, as |
| 1010 |
jmp RCX; // fiber_entryPoint must never return. |
| 1011 |
} |
| 1012 |
} |
| 1013 |
|
| 1014 |
push( cast(size_t) &trampoline ); // RIP |
| 1015 |
push( 0x00000000_00000000 ); // RBP |
| 1016 |
push( 0x00000000_00000000 ); // R12 |
| 1017 |
push( 0x00000000_00000000 ); // R13 |
| 1018 |
push( 0x00000000_00000000 ); // R14 |
| 1019 |
push( 0x00000000_00000000 ); // R15 |
| 1020 |
push( 0x00000000_00000000 ); // RDI |
| 1021 |
push( 0x00000000_00000000 ); // RSI |
| 1022 |
push( 0x00000000_00000000 ); // XMM6 (high) |
| 1023 |
push( 0x00000000_00000000 ); // XMM6 (low) |
| 1024 |
push( 0x00000000_00000000 ); // XMM7 (high) |
| 1025 |
push( 0x00000000_00000000 ); // XMM7 (low) |
| 1026 |
push( 0x00000000_00000000 ); // XMM8 (high) |
| 1027 |
push( 0x00000000_00000000 ); // XMM8 (low) |
| 1028 |
push( 0x00000000_00000000 ); // XMM9 (high) |
| 1029 |
push( 0x00000000_00000000 ); // XMM9 (low) |
| 1030 |
push( 0x00000000_00000000 ); // XMM10 (high) |
| 1031 |
push( 0x00000000_00000000 ); // XMM10 (low) |
| 1032 |
push( 0x00000000_00000000 ); // XMM11 (high) |
| 1033 |
push( 0x00000000_00000000 ); // XMM11 (low) |
| 1034 |
push( 0x00000000_00000000 ); // XMM12 (high) |
| 1035 |
push( 0x00000000_00000000 ); // XMM12 (low) |
| 1036 |
push( 0x00000000_00000000 ); // XMM13 (high) |
| 1037 |
push( 0x00000000_00000000 ); // XMM13 (low) |
| 1038 |
push( 0x00000000_00000000 ); // XMM14 (high) |
| 1039 |
push( 0x00000000_00000000 ); // XMM14 (low) |
| 1040 |
push( 0x00000000_00000000 ); // XMM15 (high) |
| 1041 |
push( 0x00000000_00000000 ); // XMM15 (low) |
| 1042 |
push( 0x00000000_00000000 ); // RBX |
| 1043 |
push( 0xFFFFFFFF_FFFFFFFF ); // GS:[0] |
| 1044 |
version (StackGrowsDown) |
| 1045 |
{ |
| 1046 |
push( cast(size_t) m_ctxt.bstack ); // GS:[8] |
| 1047 |
push( cast(size_t) m_ctxt.bstack - m_size ); // GS:[16] |
| 1048 |
} |
| 1049 |
else |
| 1050 |
{ |
| 1051 |
push( cast(size_t) m_ctxt.bstack ); // GS:[8] |
| 1052 |
push( cast(size_t) m_ctxt.bstack + m_size ); // GS:[16] |
| 1053 |
} |
| 1054 |
} |
| 1055 |
else version (AsmX86_Posix) |
| 1056 |
{ |
| 1057 |
push( 0x00000000 ); // Return address of fiber_entryPoint call |
| 1058 |
push( cast(size_t) &fiber_entryPoint ); // EIP |
| 1059 |
push( cast(size_t) m_ctxt.bstack ); // EBP |
| 1060 |
push( 0x00000000 ); // EDI |
| 1061 |
push( 0x00000000 ); // ESI |
| 1062 |
push( 0x00000000 ); // EBX |
| 1063 |
push( 0x00000000 ); // EAX |
| 1064 |
} |
| 1065 |
else version (AsmX86_64_Posix) |
| 1066 |
{ |
| 1067 |
push( 0x00000000_00000000 ); // Return address of fiber_entryPoint call |
| 1068 |
push( cast(size_t) &fiber_entryPoint ); // RIP |
| 1069 |
push( cast(size_t) m_ctxt.bstack ); // RBP |
| 1070 |
push( 0x00000000_00000000 ); // RBX |
| 1071 |
push( 0x00000000_00000000 ); // R12 |
| 1072 |
push( 0x00000000_00000000 ); // R13 |
| 1073 |
push( 0x00000000_00000000 ); // R14 |
| 1074 |
push( 0x00000000_00000000 ); // R15 |
| 1075 |
} |
| 1076 |
else version (AsmPPC_Posix) |
| 1077 |
{ |
| 1078 |
version (StackGrowsDown) |
| 1079 |
{ |
| 1080 |
pstack -= int.sizeof * 5; |
| 1081 |
} |
| 1082 |
else |
| 1083 |
{ |
| 1084 |
pstack += int.sizeof * 5; |
| 1085 |
} |
| 1086 |
|
| 1087 |
push( cast(size_t) &fiber_entryPoint ); // link register |
| 1088 |
push( 0x00000000 ); // control register |
| 1089 |
push( 0x00000000 ); // old stack pointer |
| 1090 |
|
| 1091 |
// GPR values |
| 1092 |
version (StackGrowsDown) |
| 1093 |
{ |
| 1094 |
pstack -= int.sizeof * 20; |
| 1095 |
} |
| 1096 |
else |
| 1097 |
{ |
| 1098 |
pstack += int.sizeof * 20; |
| 1099 |
} |
| 1100 |
|
| 1101 |
assert( (cast(size_t) pstack & 0x0f) == 0 ); |
| 1102 |
} |
| 1103 |
else version (AsmPPC_Darwin) |
| 1104 |
{ |
| 1105 |
version (StackGrowsDown) {} |
| 1106 |
else static assert(false, "PowerPC Darwin only supports decrementing stacks"); |
| 1107 |
|
| 1108 |
uint wsize = size_t.sizeof; |
| 1109 |
|
| 1110 |
// linkage + regs + FPRs + VRs |
| 1111 |
uint space = 8 * wsize + 20 * wsize + 18 * 8 + 12 * 16; |
| 1112 |
(cast(ubyte*)pstack - space)[0 .. space] = 0; |
| 1113 |
|
| 1114 |
pstack -= wsize * 6; |
| 1115 |
*cast(size_t*)pstack = cast(size_t) &fiber_entryPoint; // LR |
| 1116 |
pstack -= wsize * 22; |
| 1117 |
|
| 1118 |
// On Darwin PPC64 pthread self is in R13 (which is reserved). |
| 1119 |
// At present, it is not safe to migrate fibers between threads, but if that |
| 1120 |
// changes, then updating the value of R13 will also need to be handled. |
| 1121 |
version (PPC64) |
| 1122 |
*cast(size_t*)(pstack + wsize) = cast(size_t) ThreadBase.getThis().m_addr; |
| 1123 |
assert( (cast(size_t) pstack & 0x0f) == 0 ); |
| 1124 |
} |
| 1125 |
else version (AsmMIPS_O32_Posix) |
| 1126 |
{ |
| 1127 |
version (StackGrowsDown) {} |
| 1128 |
else static assert(0); |
| 1129 |
|
| 1130 |
/* We keep the FP registers and the return address below |
| 1131 |
* the stack pointer, so they don't get scanned by the |
| 1132 |
* GC. The last frame before swapping the stack pointer is |
| 1133 |
* organized like the following. |
| 1134 |
* |
| 1135 |
* |-----------|<= frame pointer |
| 1136 |
* | $gp | |
| 1137 |
* | $s0-8 | |
| 1138 |
* |-----------|<= stack pointer |
| 1139 |
* | $ra | |
| 1140 |
* | align(8) | |
| 1141 |
* | $f20-30 | |
| 1142 |
* |-----------| |
| 1143 |
* |
| 1144 |
*/ |
| 1145 |
enum SZ_GP = 10 * size_t.sizeof; // $gp + $s0-8 |
| 1146 |
enum SZ_RA = size_t.sizeof; // $ra |
| 1147 |
version (MIPS_HardFloat) |
| 1148 |
{ |
| 1149 |
enum SZ_FP = 6 * 8; // $f20-30 |
| 1150 |
enum ALIGN = -(SZ_FP + SZ_RA) & (8 - 1); |
| 1151 |
} |
| 1152 |
else |
| 1153 |
{ |
| 1154 |
enum SZ_FP = 0; |
| 1155 |
enum ALIGN = 0; |
| 1156 |
} |
| 1157 |
|
| 1158 |
enum BELOW = SZ_FP + ALIGN + SZ_RA; |
| 1159 |
enum ABOVE = SZ_GP; |
| 1160 |
enum SZ = BELOW + ABOVE; |
| 1161 |
|
| 1162 |
(cast(ubyte*)pstack - SZ)[0 .. SZ] = 0; |
| 1163 |
pstack -= ABOVE; |
| 1164 |
*cast(size_t*)(pstack - SZ_RA) = cast(size_t)&fiber_entryPoint; |
| 1165 |
} |
| 1166 |
else version (AsmMIPS_N64_Posix) |
| 1167 |
{ |
| 1168 |
version (StackGrowsDown) {} |
| 1169 |
else static assert(0); |
| 1170 |
|
| 1171 |
/* We keep the FP registers and the return address below |
| 1172 |
* the stack pointer, so they don't get scanned by the |
| 1173 |
* GC. The last frame before swapping the stack pointer is |
| 1174 |
* organized like the following. |
| 1175 |
* |
| 1176 |
* |-----------|<= frame pointer |
| 1177 |
* | $fp/$gp | |
| 1178 |
* | $s0-7 | |
| 1179 |
* |-----------|<= stack pointer |
| 1180 |
* | $ra | |
| 1181 |
* | $f24-31 | |
| 1182 |
* |-----------| |
| 1183 |
* |
| 1184 |
*/ |
| 1185 |
enum SZ_GP = 10 * size_t.sizeof; // $fp + $gp + $s0-7 |
| 1186 |
enum SZ_RA = size_t.sizeof; // $ra |
| 1187 |
version (MIPS_HardFloat) |
| 1188 |
{ |
| 1189 |
enum SZ_FP = 8 * double.sizeof; // $f24-31 |
| 1190 |
} |
| 1191 |
else |
| 1192 |
{ |
| 1193 |
enum SZ_FP = 0; |
| 1194 |
} |
| 1195 |
|
| 1196 |
enum BELOW = SZ_FP + SZ_RA; |
| 1197 |
enum ABOVE = SZ_GP; |
| 1198 |
enum SZ = BELOW + ABOVE; |
| 1199 |
|
| 1200 |
(cast(ubyte*)pstack - SZ)[0 .. SZ] = 0; |
| 1201 |
pstack -= ABOVE; |
| 1202 |
*cast(size_t*)(pstack - SZ_RA) = cast(size_t)&fiber_entryPoint; |
| 1203 |
} |
| 1204 |
else version (AsmLoongArch64_Posix) |
| 1205 |
{ |
| 1206 |
// Like others, FP registers and return address ($r1) are kept |
| 1207 |
// below the saved stack top (tstack) to hide from GC scanning. |
| 1208 |
// fiber_switchContext expects newp sp to look like this: |
| 1209 |
// 10: $r21 (reserved) |
| 1210 |
// 9: $r22 (frame pointer) |
| 1211 |
// 8: $r23 |
| 1212 |
// ... |
| 1213 |
// 0: $r31 <-- newp tstack |
| 1214 |
// -1: $r1 (return address) [&fiber_entryPoint] |
| 1215 |
// -2: $f24 |
| 1216 |
// ... |
| 1217 |
// -9: $f31 |
| 1218 |
|
| 1219 |
version (StackGrowsDown) {} |
| 1220 |
else |
| 1221 |
static assert(false, "Only full descending stacks supported on LoongArch64"); |
| 1222 |
|
| 1223 |
// Only need to set return address ($r1). Everything else is fine |
| 1224 |
// zero initialized. |
| 1225 |
pstack -= size_t.sizeof * 11; // skip past space reserved for $r21-$r31 |
| 1226 |
push(cast(size_t) &fiber_entryPoint); |
| 1227 |
pstack += size_t.sizeof; // adjust sp (newp) above lr |
| 1228 |
} |
| 1229 |
else version (AsmAArch64_Posix) |
| 1230 |
{ |
| 1231 |
// Like others, FP registers and return address (lr) are kept |
| 1232 |
// below the saved stack top (tstack) to hide from GC scanning. |
| 1233 |
// fiber_switchContext expects newp sp to look like this: |
| 1234 |
// 19: x19 |
| 1235 |
// ... |
| 1236 |
// 9: x29 (fp) <-- newp tstack |
| 1237 |
// 8: x30 (lr) [&fiber_entryPoint] |
| 1238 |
// 7: d8 |
| 1239 |
// ... |
| 1240 |
// 0: d15 |
| 1241 |
|
| 1242 |
version (StackGrowsDown) {} |
| 1243 |
else |
| 1244 |
static assert(false, "Only full descending stacks supported on AArch64"); |
| 1245 |
|
| 1246 |
// Only need to set return address (lr). Everything else is fine |
| 1247 |
// zero initialized. |
| 1248 |
pstack -= size_t.sizeof * 11; // skip past x19-x29 |
| 1249 |
push(cast(size_t) &fiber_trampoline); // see threadasm.S for docs |
| 1250 |
pstack += size_t.sizeof; // adjust sp (newp) above lr |
| 1251 |
} |
| 1252 |
else version (AsmARM_Posix) |
| 1253 |
{ |
| 1254 |
/* We keep the FP registers and the return address below |
| 1255 |
* the stack pointer, so they don't get scanned by the |
| 1256 |
* GC. The last frame before swapping the stack pointer is |
| 1257 |
* organized like the following. |
| 1258 |
* |
| 1259 |
* | |-----------|<= 'frame starts here' |
| 1260 |
* | | fp | (the actual frame pointer, r11 isn't |
| 1261 |
* | | r10-r4 | updated and still points to the previous frame) |
| 1262 |
* | |-----------|<= stack pointer |
| 1263 |
* | | lr | |
| 1264 |
* | | 4byte pad | |
| 1265 |
* | | d15-d8 |(if FP supported) |
| 1266 |
* | |-----------| |
| 1267 |
* Y |
| 1268 |
* stack grows down: The pointer value here is smaller than some lines above |
| 1269 |
*/ |
| 1270 |
// frame pointer can be zero, r10-r4 also zero initialized |
| 1271 |
version (StackGrowsDown) |
| 1272 |
pstack -= int.sizeof * 8; |
| 1273 |
else |
| 1274 |
static assert(false, "Only full descending stacks supported on ARM"); |
| 1275 |
|
| 1276 |
// link register |
| 1277 |
push( cast(size_t) &fiber_entryPoint ); |
| 1278 |
/* |
| 1279 |
* We do not push padding and d15-d8 as those are zero initialized anyway |
| 1280 |
* Position the stack pointer above the lr register |
| 1281 |
*/ |
| 1282 |
pstack += int.sizeof * 1; |
| 1283 |
} |
| 1284 |
else version (GNU_AsmX86_Windows) |
| 1285 |
{ |
| 1286 |
version (StackGrowsDown) {} else static assert( false ); |
| 1287 |
|
| 1288 |
// Currently, MinGW doesn't utilize SEH exceptions. |
| 1289 |
// See DMD AsmX86_Windows If this code ever becomes fails and SEH is used. |
| 1290 |
|
| 1291 |
push( 0x00000000 ); // Return address of fiber_entryPoint call |
| 1292 |
push( cast(size_t) &fiber_entryPoint ); // EIP |
| 1293 |
push( 0x00000000 ); // EBP |
| 1294 |
push( 0x00000000 ); // EDI |
| 1295 |
push( 0x00000000 ); // ESI |
| 1296 |
push( 0x00000000 ); // EBX |
| 1297 |
push( 0xFFFFFFFF ); // FS:[0] - Current SEH frame |
| 1298 |
push( cast(size_t) m_ctxt.bstack ); // FS:[4] - Top of stack |
| 1299 |
push( cast(size_t) m_ctxt.bstack - m_size ); // FS:[8] - Bottom of stack |
| 1300 |
push( 0x00000000 ); // EAX |
| 1301 |
} |
| 1302 |
else version (GNU_AsmX86_64_Windows) |
| 1303 |
{ |
| 1304 |
push( 0x00000000_00000000 ); // Return address of fiber_entryPoint call |
| 1305 |
push( cast(size_t) &fiber_entryPoint ); // RIP |
| 1306 |
push( 0x00000000_00000000 ); // RBP |
| 1307 |
push( 0x00000000_00000000 ); // RBX |
| 1308 |
push( 0x00000000_00000000 ); // R12 |
| 1309 |
push( 0x00000000_00000000 ); // R13 |
| 1310 |
push( 0x00000000_00000000 ); // R14 |
| 1311 |
push( 0x00000000_00000000 ); // R15 |
| 1312 |
push( 0xFFFFFFFF_FFFFFFFF ); // GS:[0] - Current SEH frame |
| 1313 |
version (StackGrowsDown) |
| 1314 |
{ |
| 1315 |
push( cast(size_t) m_ctxt.bstack ); // GS:[8] - Top of stack |
| 1316 |
push( cast(size_t) m_ctxt.bstack - m_size ); // GS:[16] - Bottom of stack |
| 1317 |
} |
| 1318 |
else |
| 1319 |
{ |
| 1320 |
push( cast(size_t) m_ctxt.bstack ); // GS:[8] - Top of stack |
| 1321 |
push( cast(size_t) m_ctxt.bstack + m_size ); // GS:[16] - Bottom of stack |
| 1322 |
} |
| 1323 |
} |
| 1324 |
else static if ( __traits( compiles, ucontext_t ) ) |
| 1325 |
{ |
| 1326 |
getcontext( &m_utxt ); |
| 1327 |
m_utxt.uc_stack.ss_sp = m_pmem; |
| 1328 |
m_utxt.uc_stack.ss_size = m_size; |
| 1329 |
makecontext( &m_utxt, &fiber_entryPoint, 0 ); |
| 1330 |
// NOTE: If ucontext is being used then the top of the stack will |
| 1331 |
// be a pointer to the ucontext_t struct for that fiber. |
| 1332 |
push( cast(size_t) &m_utxt ); |
| 1333 |
} |
| 1334 |
else |
| 1335 |
static assert(0, "Not implemented"); |
| 1336 |
} |
| 1337 |
} |
| 1338 |
|
| 1339 |
|
| 1340 |
version (AsmX86_64_Windows) |
| 1341 |
{ |
| 1342 |
// Test Windows x64 calling convention |
| 1343 |
unittest |
| 1344 |
{ |
| 1345 |
void testNonvolatileRegister(alias REG)() |
| 1346 |
{ |
| 1347 |
auto zeroRegister = new Fiber(() { |
| 1348 |
mixin("asm pure nothrow @nogc { naked; xor "~REG~", "~REG~"; ret; }"); |
| 1349 |
}); |
| 1350 |
long after; |
| 1351 |
|
| 1352 |
mixin("asm pure nothrow @nogc { mov "~REG~", 0xFFFFFFFFFFFFFFFF; }"); |
| 1353 |
zeroRegister.call(); |
| 1354 |
mixin("asm pure nothrow @nogc { mov after, "~REG~"; }"); |
| 1355 |
|
| 1356 |
assert(after == -1); |
| 1357 |
} |
| 1358 |
|
| 1359 |
void testNonvolatileRegisterSSE(alias REG)() |
| 1360 |
{ |
| 1361 |
auto zeroRegister = new Fiber(() { |
| 1362 |
mixin("asm pure nothrow @nogc { naked; xorpd "~REG~", "~REG~"; ret; }"); |
| 1363 |
}); |
| 1364 |
long[2] before = [0xFFFFFFFF_FFFFFFFF, 0xFFFFFFFF_FFFFFFFF], after; |
| 1365 |
|
| 1366 |
mixin("asm pure nothrow @nogc { movdqu "~REG~", before; }"); |
| 1367 |
zeroRegister.call(); |
| 1368 |
mixin("asm pure nothrow @nogc { movdqu after, "~REG~"; }"); |
| 1369 |
|
| 1370 |
assert(before == after); |
| 1371 |
} |
| 1372 |
|
| 1373 |
testNonvolatileRegister!("R12")(); |
| 1374 |
testNonvolatileRegister!("R13")(); |
| 1375 |
testNonvolatileRegister!("R14")(); |
| 1376 |
testNonvolatileRegister!("R15")(); |
| 1377 |
testNonvolatileRegister!("RDI")(); |
| 1378 |
testNonvolatileRegister!("RSI")(); |
| 1379 |
testNonvolatileRegister!("RBX")(); |
| 1380 |
|
| 1381 |
testNonvolatileRegisterSSE!("XMM6")(); |
| 1382 |
testNonvolatileRegisterSSE!("XMM7")(); |
| 1383 |
testNonvolatileRegisterSSE!("XMM8")(); |
| 1384 |
testNonvolatileRegisterSSE!("XMM9")(); |
| 1385 |
testNonvolatileRegisterSSE!("XMM10")(); |
| 1386 |
testNonvolatileRegisterSSE!("XMM11")(); |
| 1387 |
testNonvolatileRegisterSSE!("XMM12")(); |
| 1388 |
testNonvolatileRegisterSSE!("XMM13")(); |
| 1389 |
testNonvolatileRegisterSSE!("XMM14")(); |
| 1390 |
testNonvolatileRegisterSSE!("XMM15")(); |
| 1391 |
} |
| 1392 |
} |
| 1393 |
|
| 1394 |
|
| 1395 |
version (D_InlineAsm_X86_64) |
| 1396 |
{ |
| 1397 |
unittest |
| 1398 |
{ |
| 1399 |
void testStackAlignment() |
| 1400 |
{ |
| 1401 |
void* pRSP; |
| 1402 |
asm pure nothrow @nogc |
| 1403 |
{ |
| 1404 |
mov pRSP, RSP; |
| 1405 |
} |
| 1406 |
assert((cast(size_t)pRSP & 0xF) == 0); |
| 1407 |
} |
| 1408 |
|
| 1409 |
auto fib = new Fiber(&testStackAlignment); |
| 1410 |
fib.call(); |
| 1411 |
} |
| 1412 |
} |