Merge from vendor branch GCC:
[dragonfly.git] / lib / libc / sys / tls.2
1 .\" Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
2 .\"
3 .\" This code is derived from software contributed to The DragonFly Project
4 .\" by David Xu <davidxu@freebsd.org> and Matthew Dillon <dillon@backplane.com>
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\"
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\"    notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\"    notice, this list of conditions and the following disclaimer in
14 .\"    the documentation and/or other materials provided with the
15 .\"    distribution.
16 .\" 3. Neither the name of The DragonFly Project nor the names of its
17 .\"    contributors may be used to endorse or promote products derived
18 .\"    from this software without specific, prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 .\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 .\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24 .\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 .\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 .\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 .\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 .\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" SUCH DAMAGE.
32 .\"
33 .\" $DragonFly: src/lib/libc/sys/tls.2,v 1.2 2005/02/23 21:54:01 swildner Exp $
34 .\"
35 .Dd February 21, 2005
36 .Dt TLS 2
37 .Os
38 .Sh NAME
39 .Nm sys_set_tls_area ,
40 .Nm sys_get_tls_area
41 .Nd kernel TLS (thread local storage) support.
42 .Sh LIBRARY
43 .Lb libc
44 .Sh SYNOPSIS
45 .In sys/tls.h
46 .Ft int
47 .Fn sys_set_tls_area "int which" "struct tls_info *info" "int infosize"
48 .Ft int
49 .Fn sys_get_tls_area "int which" "struct tls_info *info" "int infosize"
50 .Sh DESCRIPTION
51 The
52 .Fn sys_set_tls_area
53 system call creates an entry for the TLS facility
54 .Fa which
55 representing thread local storage as specified by the
56 .Fa info
57 structure.  A descriptor representing the facility is returned, or -1 if
58 an error occured.  The facility may be cleared by specifying a NULL pointer
59 and an infosize of 0.
60 The
61 .Fn sys_get_tls_area
62 system call retrieves the requested TLS facility.  A descriptor representing
63 the facility is returned, or -1 if an error occured.  If you simply want the
64 descriptor you may specify a NULL pointer and an infosize of 0.
65 .Pp
66 The returned descriptor and the TLS mechanism is machine-dependant.  On IA32
67 three global segment descriptors are supported  (0, 1, and 2) and the %gs
68 load value is returned.
69 .Pp
70 The
71 .Fa tls_info
72 structure passed to
73 .Fn sys_set_tls_area
74 should first be zerod (to remain compatible with future extensions)
75 and then initialized.
76 .Pp
77 .Bd -literal
78 struct tls_info {
79         void    *base;          /* base address of TLS area */
80         int     size;           /* size of TLS area in bytes */
81 };
82 .Ed
83 .Pp
84 The actual implementation of the area is machine-dependant.  If the kernel
85 is unable to accomodate the supplied size it may create a larger area.
86 If the kernel is unable to accomodate the supplied base address an error
87 will be returned.
88
89 .Sh RETURN VALUES
90 A return value of 0 is returned on success, -1 on error.
91 .Sh ERRORS
92 .Bl -tag -width Er
93 .It Bq Er ERANGE
94 The specified facility index,
95 .Fa which ,
96 is not supported.
97 .It Bq Er EINVAL
98 An invalid parameter has been specified.
99 .It Bq Er ENOENT
100 (sys_get_tls_area) The specified facility has not been initialized with
101 .Fn sys_set_tls_area .
102 .El
103 .Sh EXAMPLE
104 .Bd -literal -compact
105
106 /*
107  * Pseudo example showing how the TLS system calls work on IA32.
108  */
109 #include <stdio.h>
110 #include <unistd.h>
111 #include <stdlib.h>
112 #include <errno.h>
113 #include <sys/tls.h>
114
115 int X;
116
117 static int getdata(int offset);
118
119 int
120 main(int ac, char **av)
121 {
122     int i;
123     int gs;
124     struct tls_info info;
125
126     info.base = &X;
127     info.size = sizeof(X);
128     if ((gs = sys_set_tls_area(0, &info, sizeof(info))) < 0) {
129         perror("setarea");
130         exit(1);
131     }
132     printf("gs = %04x\n", gs);
133     __asm __volatile("movl %0,%%eax; movl %%eax,%%gs" : "=m" (gs) : : "ax");
134
135     if (sys_get_tls_area(0, &info, sizeof(info)) < 0) {
136         perror("getarea");
137         exit(1);
138     }
139     printf("%p/%d\n", info.base, info.size);
140
141     X = 1;
142     printf("should be 1: %d\n", getdata(0));
143     X = 2;
144     printf("should be 2: %d\n", getdata(0));
145
146     printf("this should fault:\n");
147     fflush(stdout);
148     getdata(4);
149
150     return(0);
151 }
152
153 static int
154 getdata(int offset)
155 {
156     int rv;
157     __asm __volatile("movl %%gs:(%0),%%eax; movl %%eax,%1" : "+r" (offset) : "m"
158  (rv) : "ax");
159     return (rv);
160 }
161
162 .Ed
163
164 .Sh SEE ALSO
165 .Xr umtx 2
166 .Sh HISTORY
167 The
168 .Fn sys_set_tls_area ,
169 and
170 .Fn sys_get_tls_area
171 function calls first appeared in DragonFly 1.1 .