diff mbox series

[v3,01/14] arch: introduce initjmp() and Kconfig symbol HAVE_INITJMP

Message ID 20250304152510.2832340-2-jerome.forissier@linaro.org
State New
Headers show
Series Uthreads | expand

Commit Message

Jerome Forissier March 4, 2025, 3:24 p.m. UTC
Add the HAVE_INIJMP symbol to be set by architectures that support
initjmp(), a non-standard extension to setjmp()/longjmp() allowing to
initialize a jump buffer with a function pointer and a stack pointer.
This will be useful to later introduce threads. With this new function
it becomes possible to longjmp() to a particular function pointer
(rather than to a point previously reached during program execution as
is the case with setjmp()), and with a custom stack. Both things are
needed to spin off a new thread. Then the usual setjmp()/longjmp() pair
is enough to save and restore a context, i.e., switch thread.

Add the initjmp() prototype to <include/setjmp.h> since it is common to
all architectures.

Add an entry to the API documentation: doc/api/setjmp.rst.

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
 arch/Kconfig       |  7 +++++++
 doc/api/index.rst  |  1 +
 doc/api/setjmp.rst | 10 ++++++++++
 include/setjmp.h   | 29 +++++++++++++++++++++++++++++
 4 files changed, 47 insertions(+)
 create mode 100644 doc/api/setjmp.rst
diff mbox series

Patch

diff --git a/arch/Kconfig b/arch/Kconfig
index 35b19f9bfdc..14111ca14fb 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -13,6 +13,13 @@  config HAVE_SETJMP
 	help
 	 The architecture supports setjmp() and longjmp().
 
+config HAVE_INITJMP
+	bool
+	depends on HAVE_SETJMP
+	help
+	 The architecture supports initjmp(), a non-standard companion to
+	 setjmp() and longjmp().
+
 config SUPPORT_BIG_ENDIAN
 	bool
 
diff --git a/doc/api/index.rst b/doc/api/index.rst
index a108718ea99..0dc9ad45d41 100644
--- a/doc/api/index.rst
+++ b/doc/api/index.rst
@@ -25,6 +25,7 @@  U-Boot API documentation
    rng
    sandbox
    serial
+   setjmp
    sysreset
    timer
    unicode
diff --git a/doc/api/setjmp.rst b/doc/api/setjmp.rst
new file mode 100644
index 00000000000..7718047085c
--- /dev/null
+++ b/doc/api/setjmp.rst
@@ -0,0 +1,10 @@ 
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+Long jump API
+=============
+
+.. kernel-doc:: include/setjmp.h
+   :doc: Overview
+
+.. kernel-doc:: include/setjmp.h
+   :internal:
diff --git a/include/setjmp.h b/include/setjmp.h
index 37d3a8af85d..8b2ff66a4c2 100644
--- a/include/setjmp.h
+++ b/include/setjmp.h
@@ -3,12 +3,26 @@ 
 #ifndef _SETJMP_H_
 #define _SETJMP_H_ 1
 
+/**
+ * DOC: Overview
+ *
+ * The long jump API allows to perform nonlocal gotos, that is jump from one
+ * function to another typically further down in the stack, while properly
+ * restoring the stack's state (unwinding). The two functions needed to do this
+ * are setjmp() and longjmp().
+ *
+ * In addition to these two standard POSIX.1-2001/C89 functions, a third one is
+ * present in U-Boot: initjmp(). It is an extension which allows to implement
+ * user-mode threads.
+ */
+
 #ifdef CONFIG_HAVE_SETJMP
 #include <asm/setjmp.h>
 #else
 struct jmp_buf_data {
 };
 #endif
+#include <linux/compiler_attributes.h>
 
 /**
  * typedef jmp_buf - information needed to restore a calling environment
@@ -37,4 +51,19 @@  int setjmp(jmp_buf env);
  */
 void longjmp(jmp_buf env, int val);
 
+/**
+ * initjmp() - prepare for a long jump to a given function with a given stack
+ *
+ * This function sets up a jump buffer for later use with longjmp(). It allows
+ * to branch to a specific function with a specific stack. Please note that
+ * @func MUST NOT return. It shall typically restore the main stack and resume
+ * execution by doing a long jump to a jump buffer initialized by setjmp()
+ * before the long jump. initjmp() allows to implement multithreading.
+ *
+ * @env:	jump buffer
+ * @func:	function to be called on longjmp(), MUST NOT RETURN
+ * @stack:	the stack to be used by @func
+ */
+int initjmp(jmp_buf env, void __noreturn (*func)(void), void *stack);
+
 #endif /* _SETJMP_H_ */