Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
stack_x86_64.h
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#pragma once
20
21#include <stddef.h>
22#include <stdint.h>
23
24/// Defined in switch_x86_64.S. Not callable from C; only its address is taken,
25/// to seed the initial return address of a fresh fiber.
26extern void srn_fiber_trampoline(void);
27
28/// srn_fiber_frame is the exact frame srn_fiber_swap (switch_x86_64.S) pushes
29/// and pops, and that srn_fiber_ctx_make (ctx_x86_64.c) fabricates for a fresh
30/// fiber. Its field order and offsets ARE the contract with that assembly; the
31/// static asserts pin them so the C side and the assembly cannot drift apart.
32/// Only the matching x86-64 sources include this header.
33typedef struct srn_fiber_frame {
34 uint32_t mxcsr; ///< 0 stmxcsr/ldmxcsr (%rsp)
35 uint32_t _pad0; ///< 4
36 uint16_t fcw; ///< 8 fnstcw/fldcw 8(%rsp)
37 uint16_t _pad1; ///< 10
38 uint32_t _pad2; ///< 12 rounds the FP control block to 16 bytes
39 uintptr_t r15; ///< 16
40 uintptr_t r14; ///< 24
41 uintptr_t r13; ///< 32 entry function
42 uintptr_t r12; ///< 40 entry argument
43 uintptr_t rbx; ///< 48
44 uintptr_t rbp; ///< 56
45 uintptr_t ret_addr; ///< 64 resume address (= srn_fiber_trampoline)
47
48// NOLINTBEGIN(readability-magic-numbers)
49_Static_assert(sizeof(srn_fiber_frame_t) == 72,
50 "srn_fiber_frame must match switch_x86_64.S");
51_Static_assert(offsetof(srn_fiber_frame_t, fcw) == 8, "fcw offset");
52_Static_assert(offsetof(srn_fiber_frame_t, r13) == 32, "r13 (fn) offset");
53_Static_assert(offsetof(srn_fiber_frame_t, r12) == 40, "r12 (arg) offset");
54_Static_assert(offsetof(srn_fiber_frame_t, ret_addr) == 64, "ret_addr offset");
55// NOLINTEND(readability-magic-numbers)
56
57/// SysV AMD64 requires 16-byte stack alignment at a call; srn_fiber_ctx_make
58/// rounds the frame's base down to this so the trampoline's entry rsp is
59/// aligned.
60#define SRN_FIBER_STACK_ALIGN 16U
61
62/// Control-register values a fresh fiber starts with: all FP exceptions masked,
63/// round-to-nearest, extended precision -- the environment a freshly started C
64/// program runs with. srn_fiber_ctx_make seeds them into the frame above.
65///
66/// SSE control/status word (MXCSR)
67#define SRN_FIBER_MXCSR_DEFAULT 0x1F80U
68/// x87 control word
69#define SRN_FIBER_FCW_DEFAULT 0x037FU
void srn_fiber_trampoline(void)
Defined in switch_x86_64.S.
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,...
srn_fiber_frame is the exact frame srn_fiber_swap (switch_x86_64.S) pushes and pops,...
uintptr_t r15
16
uint32_t _pad0
4
uintptr_t rbp
56
uint16_t fcw
8 fnstcw/fldcw 8(rsp)
uintptr_t r14
24
uint32_t mxcsr
0 stmxcsr/ldmxcsr (rsp)
uintptr_t rbx
48
uintptr_t ret_addr
64 resume address (= srn_fiber_trampoline)
uint16_t _pad1
10
uintptr_t r13
32 entry function
uintptr_t r12
40 entry argument
uint32_t _pad2
12 rounds the FP control block to 16 bytes