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