CHERIoT RTOS
A compartmentalised RTOS for CHERIoT hardware
Loading...
Searching...
No Matches
setjmp.h
1// Copyright Microsoft and CHERIoT Contributors.
2// SPDX-License-Identifier: MIT
3#pragma once
4
5/**
6 * This is a minimal implementation of setjmp/longjmp.
7 *
8 * CHERIoT cannot store a `jmp_buf` anywhere other than the stack without
9 * clearing tags (which will then cause `longjmp` to fail).
10 */
11
12#include <cdefs.h>
13#include <stddef.h>
14#include <stdint.h>
15
16/**
17 * Jump buffer for setjmp/longjmp.
18 */
20{
21 uintptr_t __cs0;
22 uintptr_t __cs1;
23 uintptr_t __csp;
24 uintptr_t __cra;
25};
26
27/**
28 * C requires that `setjmp` and `longjmp` take a `jmp_buf` by reference and so
29 * this ends up being defined as an array of one element, which allows it to
30 * both be allocated and passed by reference.
31 */
32typedef struct __jmp_buf jmp_buf[1];
33
34#include <setjmp-assembly.h>
35
36__BEGIN_DECLS
37/**
38 * C `setjmp` function. Returns (up to) twice. First returns 0, returns a
39 * value passed to `longjmp` on the second return.
40 */
41__attribute__((returns_twice)) int setjmp(jmp_buf env);
42__asm__(".section .text.setjmp,\"awG\",@progbits,setjmp,comdat\n"
43 ".globl setjmp\n"
44 ".p2align 2\n"
45 ".type setjmp,@function\n"
46 "setjmp:\n"
47 " csc cs0, 0(ca0)\n"
48 " csc cs1, 8(ca0)\n"
49 " csc csp, 16(ca0)\n"
50 " csc cra, 24(ca0)\n"
51 " li a0, 0\n"
52 " cret\n");
53
54/**
55 * C `longjmp` function. Does not return, jumps back to the `setjmp` call.
56 */
57__attribute__((__noreturn__)) void longjmp(jmp_buf env, int val);
58__asm__(".section .text.longjmp,\"awG\",@progbits,longjmp,comdat\n"
59 ".globl longjmp\n"
60 ".p2align 2\n"
61 ".type longjmp,@function\n"
62 "longjmp:\n"
63 " clc cs0, 0(ca0)\n"
64 " clc cs1, 8(ca0)\n"
65 " clc csp, 16(ca0)\n"
66 " clc cra, 24(ca0)\n"
67 " mv a0, a1\n"
68 " cjr cra\n");
69__END_DECLS
This is a minimal implementation of setjmp/longjmp.
Definition setjmp.h:20