1 .\" Copyright 2014 Garrett D'Amore <garrett@damore.org> 2 .\" Copyright 1989 AT&T. Copyright (c) 2004, Sun Microsystems, Inc. All Rights Reserved. Portions Copyright (c) 1992, X/Open Company Limited. All Rights Reserved. 3 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at 4 .\" http://www.opengroup.org/bookstore/. 5 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html. 6 .\" This notice shall appear on any product containing this material. 7 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. 8 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License. 9 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner] 10 .Dd "Jul 14, 2014" 11 .Dt MAKECONTEXT 3C 12 .Os 13 .Sh NAME 14 .Nm makecontext, swapcontext 15 .Nd manipulate user contexts 16 .Sh SYNOPSIS 17 .In ucontext.h 18 .Ft void 19 .Fn makecontext "ucontext_t *ucp" "void \*(lp*func\*(rp\*(lp\*(rp, int argc, ..." 20 .Ft int 21 .Fn swapcontext "ucontext_t *restrict oucp" "const ucontext_t *restrict ucp" 22 .Sh DESCRIPTION 23 The 24 .Fn makecontext 25 function modifies the context specified by 26 .Fa ucp , 27 which has been initialized using 28 .Xr getcontext 2 . 29 When this context is 30 resumed using 31 .Fn swapcontext 32 or 33 .Xr setcontext 2 , 34 execution continues by calling the function 35 .Fa func , 36 passing it the arguments that follow 37 .Fa argc 38 in the 39 .Fn makecontext 40 call. The value of 41 .Fa argc 42 must match the number of pointer-sized integer arguments passed to 43 .Fn func , 44 otherwise the behavior is undefined. 45 .Lp 46 Before a call is made to 47 .Fn makecontext , 48 the context being modified should 49 have a stack allocated for it. The stack is assigned to the context by 50 initializing the 51 .Fa uc_stack 52 member. 53 .Lp 54 The 55 .Fa uc_link 56 member is used to determine the context that will be resumed 57 when the context being modified by 58 .Fn makecontext 59 returns. The 60 .Fa uc_link 61 member should be initialized prior to the call to 62 .Fn makecontext . 63 If the 64 .Fa uc_link 65 member is initialized to 66 .Dv NULL , 67 the thread executing 68 .Fa func 69 will exit when 70 .Fa func 71 returns. See 72 .Xr pthread_exit 3C . 73 .Lp 74 The 75 .Fn swapcontext 76 function saves the current context in the context 77 structure pointed to by 78 .Fa oucp 79 and sets the context to the context 80 structure pointed to by 81 .Fa ucp . 82 .Lp 83 If the 84 .Fa ucp 85 or 86 .Fa oucp 87 argument points to an invalid address, the 88 behavior is undefined and 89 .Va errno 90 may be set to 91 .Er EFAULT . 92 .Sh RETURN VALUES 93 .Rv -std swapcontext 94 .Sh EXAMPLES 95 .Ss Example 1 96 The following example illustrates execution context on a stack whose memory was 97 allocated using 98 .Xr mmap 2 : 99 .Bd -literal -offset indent 100 #include <stdio.h> 101 #include <ucontext.h> 102 #include <sys/mman.h> 103 104 void 105 assign(long a, int *b) 106 { 107 *b = (int)a; 108 } 109 110 int 111 main(int argc, char **argv) 112 { 113 ucontext_t uc, back; 114 size_t sz = 0x10000; 115 int value = 0; 116 117 getcontext(&uc); 118 119 uc.uc_stack.ss_sp = mmap(0, sz, 120 PROT_READ | PROT_WRITE | PROT_EXEC, 121 MAP_PRIVATE | MAP_ANON, -1, 0); 122 uc.uc_stack.ss_size = sz; 123 uc.uc_stack.ss_flags = 0; 124 125 uc.uc_link = &back; 126 127 makecontext(&uc, assign, 2, 100L, &value); 128 swapcontext(&back, &uc); 129 130 printf("done %d\en", value); 131 132 return (0); 133 } 134 .Ed 135 .Sh ERRORS 136 The 137 .Fn swapcontext 138 function will fail if: 139 .Bl -tag -width Er 140 .It Er ENOMEM 141 The 142 .Fa ucp 143 argument does not have enough stack left to complete the operation. 144 .El 145 .Lp 146 The 147 .Fn swapcontext 148 function may fail if: 149 .Bl -tag -width Er 150 .It Er EFAULT 151 The 152 .Fa ucp 153 or 154 .Fa oucp 155 argument points to an invalid address. 156 .El 157 .Sh USAGE 158 These functions are useful for implementing user-level context switching 159 between multiple threads of control within a process (co-processing). More 160 effective multiple threads of control can be obtained by using native support 161 for multithreading. See 162 .Xr pthreads 5 . 163 .Sh INTERFACE STABILITY 164 .Sy Obsolete Standard . 165 .Sh MT-LEVEL 166 .Sy MT-Safe . 167 .Sh SEE ALSO 168 .Xr mmap 2 , 169 .Xr getcontext 2 , 170 .Xr sigaction 2 , 171 .Xr sigprocmask 2 , 172 .Xr pthread_exit 3C , 173 .Xr ucontext.h 3HEAD , 174 .Xr standards 5 , 175 .Xr pthreads 5 176 .Sh NOTES 177 The semantics of the 178 .Fa uc_stack 179 member of the 180 .Ft ucontext_t 181 structure have changed as they apply to inputs to 182 .Fn makecontex . 183 Prior to Solaris 10, the 184 .Fa ss_sp 185 member of the 186 .Fa uc_stack 187 structure represented the high 188 memory address of the area reserved for the stack. The \fBss_sp\fR member now 189 represents the base (low memory address), in keeping with other uses of 190 .Fa ss_sp . 191 .Lp 192 This change in the meaning of 193 .Fa ss_sp 194 is now the default behavior. The 195 .Dv -D__MAKECONTEXT_V2_SOURCE 196 compilation flag used in Solaris 9 update 197 releases to access this behavior is obsolete. 198 .Lp 199 Binary compatibility has been preserved with releases prior to Solaris 10. 200 Before recompiling, applications that use 201 .Fn makecontext 202 must be updated 203 to reflect this behavior change. 204 .Lp 205 Portable applications should not use this function. Instead, applications 206 should use 207 .Xr pthreads 5 208 routines. 209 .Lp 210 Note that the definition of 211 .Fn makecontext 212 violates 213 .St -isoC . 214 There is no way to declare this function that does not violate that 215 standard. 216 .Sh STANDARDS 217 This function was introduced in 218 .St -xpg4.2 , 219 and subsequently removed from 220 .St -p1003.1-2008 .