Serene Runtime 1.0.0-dev
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// x86-64 context fabrication. On another architecture this file compiles to
26// nothing and the sibling ctx_<arch>.c supplies srn_fiber_ctx_make instead. The
27// selection is by target macro, so the whole runtime source set can be handed
28// to a cross compiler and only the matching architecture emits symbols.
29#if defined(__x86_64__)
30
31# include "serene/rt/fiber.h"
33
34// NOLINTBEGIN(performance-no-int-to-ptr)
36 srn_fiber_ctx_t *fiber_ctx, srn_fiber_stack_t stack, void (*fn)(void *), void *arg
37) {
38 // Lay srn_fiber_swap's frame (see rt/fiber/stack.h and switch_x86_64.S) at
39 // the top of `stack`, rounded down to SRN_FIBER_STACK_ALIGN so the
40 // return-address slot -- and thus the trampoline's entry rsp -- is correctly
41 // aligned. The compound literal zero-fills the slots not named here. Register
42 // slots are uintptr_t, so storing fn/trampoline is a
43 // function-pointer-to-integer conversion (allowed), not the
44 // function-to-object-pointer conversion that -Wpedantic forbids.
45 uintptr_t top = (uintptr_t)stack.start;
46 uintptr_t sp = (top - sizeof(srn_fiber_frame_t)) & ~(uintptr_t)(SRN_FIBER_STACK_ALIGN - 1);
47
51 .r13 = (uintptr_t)fn,
52 .r12 = (uintptr_t)arg,
53 .ret_addr = (uintptr_t)srn_fiber_trampoline,
54 };
55
56 fiber_ctx->sp = (void *)sp;
57}
58// NOLINTEND(performance-no-int-to-ptr)
59
60#endif // __x86_64__
AI Generated (🤦) Fiber subsystem overview.
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...
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:198
void * sp
Definition fiber.h:199
One stack per fiber, mapped with a guard page at the low end so an overflow faults deterministically ...
Definition fiber.h:218
void * start
High end, stack pointer initialises to this address.
Definition fiber.h:220