xref: /illumos-gate/usr/src/man/man3c/makecontext.3c (revision ed093b41)
1.\"
2.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3.\" permission to reproduce portions of its copyrighted documentation.
4.\" Original documentation from The Open Group can be obtained online at
5.\" http://www.opengroup.org/bookstore/.
6.\"
7.\" The Institute of Electrical and Electronics Engineers and The Open
8.\" Group, have given us permission to reprint portions of their
9.\" documentation.
10.\"
11.\" In the following statement, the phrase ``this text'' refers to portions
12.\" of the system documentation.
13.\"
14.\" Portions of this text are reprinted and reproduced in electronic form
15.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16.\" Standard for Information Technology -- Portable Operating System
17.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20.\" between these versions and the original IEEE and The Open Group
21.\" Standard, the original IEEE and The Open Group Standard is the referee
22.\" document.  The original Standard can be obtained online at
23.\" http://www.opengroup.org/unix/online.html.
24.\"
25.\" This notice shall appear on any product containing this material.
26.\"
27.\" The contents of this file are subject to the terms of the
28.\" Common Development and Distribution License (the "License").
29.\" You may not use this file except in compliance with the License.
30.\"
31.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32.\" or http://www.opensolaris.org/os/licensing.
33.\" See the License for the specific language governing permissions
34.\" and limitations under the License.
35.\"
36.\" When distributing Covered Code, include this CDDL HEADER in each
37.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38.\" If applicable, add the following below this CDDL HEADER, with the
39.\" fields enclosed by brackets "[]" replaced with your own identifying
40.\" information: Portions Copyright [yyyy] [name of copyright owner]
41.\"
42.\"
43.\" Copyright 1989 AT&T
44.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
45.\" Copyright (c) 2004, Sun Microsystems, Inc.  All Rights Reserved.
46.\" Copyright 2023 Oxide Computer Company
47.\"
48.Dd March 20, 2023
49.Dt MAKECONTEXT 3C
50.Os
51.Sh NAME
52.Nm makecontext ,
53.Nm swapcontext ,
54.Nm swapcontext_extd
55.Nd manipulate user contexts
56.Sh SYNOPSIS
57.In ucontext.h
58.Ft void
59.Fo makecontext
60.Fa "ucontext_t *ucp"
61.Fa "void (*ifunc)()"
62.Fa "int argc"
63.Fa "..."
64.Fc
65.Ft int
66.Fo swapcontext
67.Fa "ucontext_t *restrict oucp"
68.Fa "const ucontext_t *restrict ucp"
69.Fc
70.Ft int
71.Fo swapcontext_extd
72.Fa "ucontext_t *restrict oucp"
73.Fa "uint32_t flags"
74.Fa "const ucontext_t *restrict ucp"
75.Fc
76.Sh DESCRIPTION
77The
78.Fn makecontext
79function modifies the context specified by
80.Fa ucp ,
81which has been initialized using
82.Xr getcontext 2 or
83.Xr getcontext_extd 2 .
84When this context is resumed using
85.Fn swapcontext ,
86.Fn swapcontext_extd ,
87or
88.Xr setcontext 2 ,
89execution continues by calling the function
90.Fa func ,
91passing it the arguments that follow
92.Fa argc
93in the
94.Fn makecontext
95call.
96The value of
97.Fa argc
98must match the number of pointer-sized integer arguments passed to
99.Fa func ,
100otherwise the behavior is undefined.
101.Pp
102Before a call is made to
103.Fn makecontext ,
104the context being modified should have a stack allocated for it.
105The stack is assigned to the context by initializing the
106.Fa uc_stack
107member.
108.Pp
109The
110.Fa uc_link
111member is used to determine the context that will be resumed when the context
112being modified by
113.Fn makecontext
114returns.
115The
116.Fa uc_link
117member should be initialized prior to the call to
118.Fn makecontext .
119If the
120.Fa uc_link
121member is initialized to
122.Dv NULL ,
123the thread executing
124.Fa func
125will exit when
126.Fa func returns.
127See
128.Xr pthread_exit 3C .
129.Pp
130The
131.Fn swapcontext
132function saves the current context in the context structure pointed to by
133.Fa oucp
134and sets the context to the context structure pointed to by
135.Fa ucp .
136.Pp
137If the
138.Fa ucp
139or
140.Fa oucp
141argument points to an invalid address, the behavior is undefined and
142.Va errno
143may be set to
144.Er EFAULT .
145.Pp
146The
147.Fn swapcontext_extd
148function is similar to
149.Fn swapcontext
150except that it performs a call to
151.Xr getcontext_extd 2
152to get and save the current context, passing the
153.Fa flags
154argument to
155.Xr getcontext_extd 2 .
156Note, the same constraints around the initialization of the
157.Vt ucontext_t
158that are discussed in
159.Xr getcontext_extd 2
160still apply.
161Mainly, the context must either have originally come from
162.Xr ucontext_alloc 3C
163or prior to its first use been zeroed.
164See
165.Xr getcontext_extd 2
166for more information.
167.Sh RETURN VALUES
168On successful completion,
169.Fn swapcontext
170and
171.Fn swapcontext_extd
172return
173.Sy 0 .
174Otherwise,
175.Sy -1
176is returned and
177.Va errno
178is set to indicate the error.
179.Sh EXAMPLES
180.Sy Example 1
181Alternate execution context on a stack whose memory was allocated using
182.Fn mmap .
183.Bd -literal -offset 2
184#include <stdio.h>
185#include <ucontext.h>
186#include <sys/mman.h>
187
188void
189assign(long a, int *b)
190{
191        *b = (int)a;
192}
193
194int
195main(int argc, char **argv)
196{
197        ucontext_t uc, back;
198        size_t sz = 0x10000;
199        int value = 0;
200
201        getcontext(&uc);
202
203        uc.uc_stack.ss_sp = mmap(0, sz,
204            PROT_READ | PROT_WRITE | PROT_EXEC,
205            MAP_PRIVATE | MAP_ANON, -1, 0);
206        uc.uc_stack.ss_size = sz;
207        uc.uc_stack.ss_flags = 0;
208
209        uc.uc_link = &back;
210
211        makecontext(&uc, assign, 2, 100L, &value);
212        swapcontext(&back, &uc);
213
214        printf("done %d\en", value);
215
216        return (0);
217}
218.Ed
219.Sh ERRORS
220The
221.Fn swapcontext
222and
223.Fn swapcontext_extd
224function will fail if:
225.Bl -tag -width Er
226.It Er ENOMEM
227The
228.Fa ucp
229argument does not have enough stack left to complete the operation.
230.El
231.Pp
232The
233.Fn swapcontext
234and
235.Fn swapcontext_extd
236functions may fail if:
237.Bl -tag -width Er
238.It Er EFAULT
239The
240.Fa ucp
241or
242.Fa oucp
243argument points to an invalid address.
244.El
245.Pp
246The
247.Fn swapcontext_extd
248function may additionally fail if:
249.Bl -tag -width Er
250.It Er EINVAL
251The
252.Fa flags
253argument contains invalid values.
254.El
255.Sh USAGE
256These functions are useful for implementing user-level context switching
257between multiple threads of control within a process
258.Pq co-processing .
259More effective multiple threads of control can be obtained by using native
260support for multithreading.
261See
262.Xr threads 7 .
263.Sh INTERFACE STABILITY
264.Sy Committed
265.Sh MT-LEVEL
266.Sy MT-Safe
267.Sh SEE ALSO
268.Xr getcontext 2 ,
269.Xr getcontext_extd 2 ,
270.Xr mmap 2 ,
271.Xr sigaction 2 ,
272.Xr sigprocmask 2 ,
273.Xr pthread_exit 3C ,
274.Xr ucontext_alloc 3C ,
275.Xr ucontext.h 3HEAD ,
276.Xr attributes 7 ,
277.Xr standards 7 ,
278.Xr threads 7
279.Sh NOTES
280The semantics of the
281.Fa uc_stack
282member of the
283.Fa ucontext_t
284structure have changed as they apply to inputs to
285.Fn makecontext .
286Prior to Solaris 10, the
287.Fa ss_sp
288member of the
289.Fa uc_stack
290tructure represented the high memory address of the area reserved for the stack.
291The
292.Fa ss_sp
293member now represents the base
294.Pq low memory address ,
295in keeping with other uses of
296.Fa ss_sp .
297This change in the meaning of
298.Fa ss_sp
299is the default behavior.
300.Pp
301Binary compatibility has been preserved with releases prior to Solaris 10.
302Before recompiling, applications that use
303.Fn makecontext
304must be updated to reflect this behavior change.
305The example below demonstrates a typical change that must be applied:
306.Bd -literal -offset 2
307--- example1_s9.c       Thu Oct  3 11:58:17 2002
308+++ example1.c  Thu Jun 27 13:28:16 2002
309@@ -27,12 +27,9 @@
310        uc.uc_stack.ss_sp = mmap(0, sz,
311            PROT_READ | PROT_WRITE | PROT_EXEC,
312            MAP_PRIVATE | MAP_ANON, -1, 0);
313-       uc.uc_stack.ss_sp = (char *)uc.uc_stack.ss_sp + sz - 8;
314        uc.uc_stack.ss_size = sz;
315        uc.uc_stack.ss_flags = 0;
316
317        uc.uc_link = &back
318
319        makecontext(&uc, assign, 2, 100L, &value);
320.fi
321.Ed
322