blob: 32dd48803e95f737a43e601bc7b45894ad96ab14 [file] [log] [blame]
Heinrich Schuchardtf6782bb2025-03-02 15:21:19 +01001/* SPDX-License-Identifier: GPL-2.0-or-later */
2
3#ifndef _SETJMP_H_
4#define _SETJMP_H_ 1
5
Jerome Forissierc632c152025-04-18 16:09:29 +02006/**
7 * DOC: Overview
8 *
9 * The long jump API allows to perform nonlocal gotos, that is jump from one
10 * function to another typically further down in the stack, while properly
11 * restoring the stack's state (unwinding). The two functions needed to do this
12 * are setjmp() and longjmp().
13 *
14 * In addition to these two standard POSIX.1-2001/C89 functions, a third one is
15 * present in U-Boot: initjmp(). It is an extension which allows to implement
16 * user-mode threads.
17 */
18
Heinrich Schuchardtf6782bb2025-03-02 15:21:19 +010019#ifdef CONFIG_HAVE_SETJMP
20#include <asm/setjmp.h>
21#else
22struct jmp_buf_data {
23};
24#endif
Jerome Forissierc632c152025-04-18 16:09:29 +020025#include <linux/compiler_attributes.h>
26#include <stddef.h>
Heinrich Schuchardtf6782bb2025-03-02 15:21:19 +010027
28/**
29 * typedef jmp_buf - information needed to restore a calling environment
30 */
31typedef struct jmp_buf_data jmp_buf[1];
32
33/**
34 * setjmp() - prepare for a long jump
35 *
36 * Registers, the stack pointer, and the return address are saved in the
37 * jump bufffer. The function returns zero afterwards. When longjmp() is
38 * executed the function returns a second time with a non-zero value.
39 *
40 * @env: jump buffer used to store register values
41 * Return: 0 after setting up jump buffer, non-zero after longjmp()
42 */
43int setjmp(jmp_buf env);
44
45/**
46 * longjmp() - long jump
47 *
48 * Jump back to the address and the register state saved by setjmp().
49 *
50 * @env: jump buffer
51 * @val: value to be returned by setjmp(), 0 is replaced by 1
52 */
53void longjmp(jmp_buf env, int val);
54
Jerome Forissierc632c152025-04-18 16:09:29 +020055/**
56 * initjmp() - prepare for a long jump to a given function with a given stack
57 *
58 * This function sets up a jump buffer for later use with longjmp(). It allows
59 * to branch to a specific function with a specific stack. Please note that
60 * @func MUST NOT return. It shall typically restore the main stack and resume
61 * execution by doing a long jump to a jump buffer initialized by setjmp()
62 * before the long jump. initjmp() allows to implement multithreading.
63 *
64 * @env: jump buffer
65 * @func: function to be called on longjmp(), MUST NOT RETURN
66 * @stack_base: the stack to be used by @func (lower address)
67 * @stack_sz: the stack size in bytes
68 */
69int initjmp(jmp_buf env, void __noreturn (*func)(void), void *stack_base,
70 size_t stack_sz);
71
Heinrich Schuchardtf6782bb2025-03-02 15:21:19 +010072#endif /* _SETJMP_H_ */