Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
ctx_x86_64.c
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <[email protected]>
4 *
5 * This library is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19/// @file
20/// x86-64 fabrication of a fresh fiber's saved context. Paired with
21/// switch_x86_64.S; both are selected by CPU family in meson.build.
22
23#include <stdint.h>
24
25#include "serene/rt/fiber.h"
27
28// NOLINTBEGIN(performance-no-int-to-ptr)
29void srn_fiber_ctx_make(srn_fiber_ctx_t *fiber_ctx, srn_fiber_stack_t stack, void (*fn)(void *),
30 void *arg) {
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}
51// NOLINTEND(performance-no-int-to-ptr)
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 stac...
Definition ctx_x86_64.c:29
AI Generated (🤦) Fiber subsystem overview.
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,...
The saved context of a suspended fiber is a single word: its stack pointer at the moment it was switc...
Definition fiber.h:147
void * sp
Definition fiber.h:148
One stack per fiber, mapped with a guard page at the low end so an overflow faults deterministically ...
Definition fiber.h:168
void * start
High end, stack pointer initialises to this address.
Definition fiber.h:170