Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
ctx_x86_64.c File Reference

x86-64 fabrication of a fresh fiber's saved context. More...

#include <stdint.h>
#include "serene/rt/fiber.h"
#include "serene/rt/fiber/arch/stack_x86_64.h"
Include dependency graph for ctx_x86_64.c:

Go to the source code of this file.

Functions

void srn_fiber_ctx_make (srn_fiber_ctx_t *fiber_ctx, srn_fiber_stack_t stack, void(*fn)(void *), void *arg)
 Initialise a fresh fiber context so the first srn_fiber_swap into it begins executing fn(arg) on stack.
 

Detailed Description

x86-64 fabrication of a fresh fiber's saved context.

Paired with switch_x86_64.S; both are selected by CPU family in meson.build.

Definition in file ctx_x86_64.c.

Function Documentation

◆ srn_fiber_ctx_make()

void srn_fiber_ctx_make ( srn_fiber_ctx_t * fiber_ctx,
srn_fiber_stack_t stack,
void(* fn )(void *),
void * arg )

Initialise a fresh fiber context so the first srn_fiber_swap into it begins executing fn(arg) on stack.

fn is the fiber's entry: a void (*)(void *) that runs on the new stack and receives arg. It must NEVER return – there is no frame beneath it, so a return traps in the trampoline. Instead it transfers control away with srn_fiber_switch (to yield) or srn_fiber_switch_final (when finished), and as its first action it calls srn_fiber_on_entry(). Typical shape:

static void worker(void *arg) { srn_fiber_on_entry(&scheduler); // hand the switch to the sanitizer ... do the fiber's work using arg ... srn_fiber_switch_final(&scheduler); // finished, never returns }

Definition at line 29 of file ctx_x86_64.c.

30 {
31 // Lay srn_fiber_swap's frame (see rt/fiber/stack.h and switch_x86_64.S) at
32 // the top of `stack`, rounded down to SRN_FIBER_STACK_ALIGN so the
33 // return-address slot -- and thus the trampoline's entry rsp -- is correctly
34 // aligned. The compound literal zero-fills the slots not named here. Register
35 // slots are uintptr_t, so storing fn/trampoline is a
36 // function-pointer-to-integer conversion (allowed), not the
37 // function-to-object-pointer conversion that -Wpedantic forbids.
38 uintptr_t top = (uintptr_t)stack.start;
39 uintptr_t sp = (top - sizeof(srn_fiber_frame_t)) & ~(uintptr_t)(SRN_FIBER_STACK_ALIGN - 1);
40
44 .r13 = (uintptr_t)fn,
45 .r12 = (uintptr_t)arg,
46 .ret_addr = (uintptr_t)srn_fiber_trampoline,
47 };
48
49 fiber_ctx->sp = (void *)sp;
50}
void srn_fiber_trampoline(void)
Defined in switch_x86_64.S.
#define SRN_FIBER_FCW_DEFAULT
x87 control word
#define SRN_FIBER_MXCSR_DEFAULT
Control-register values a fresh fiber starts with: all FP exceptions masked, round-to-nearest,...
#define SRN_FIBER_STACK_ALIGN
SysV AMD64 requires 16-byte stack alignment at a call; srn_fiber_ctx_make rounds the frame's base dow...
struct srn_fiber_frame srn_fiber_frame_t
srn_fiber_frame is the exact frame srn_fiber_swap (switch_x86_64.S) pushes and pops,...
void * sp
Definition fiber.h:148
void * start
High end, stack pointer initialises to this address.
Definition fiber.h:170
Here is the call graph for this function:
Here is the caller graph for this function: