xref: /dragonfly/lib/libc/sys/tls.2 (revision 0600465e)
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.Dd February 21, 2005
34.Dt TLS 2
35.Os
36.Sh NAME
37.Nm set_tls_area ,
38.Nm get_tls_area
39.Nd kernel TLS (thread local storage) support
40.Sh LIBRARY
41.Lb libc
42.Sh SYNOPSIS
43.In sys/tls.h
44.Ft int
45.Fn set_tls_area "int which" "struct tls_info *info" "size_t infosize"
46.Ft int
47.Fn get_tls_area "int which" "struct tls_info *info" "size_t infosize"
48.Sh DESCRIPTION
49The
50.Fn set_tls_area
51system call creates an entry for the TLS facility
52.Fa which
53representing thread local storage as specified by the
54.Fa info
55structure.
56A descriptor representing the facility is returned, or -1 if an error occurred.
57The facility may be cleared by specifying a NULL pointer and an infosize of 0.
58The
59.Fn get_tls_area
60system call retrieves the requested TLS facility.
61A descriptor representing the facility is returned, or -1 if an error occurred.
62If you simply want the
63descriptor you may specify a NULL pointer and an infosize of 0.
64.Pp
65The returned descriptor and the TLS mechanism is machine-dependent.
66On IA32
67three global segment descriptors are supported  (0, 1, and 2) and the %gs
68load value is returned.
69.Pp
70The
71.Fa tls_info
72structure passed to
73.Fn set_tls_area
74should first be zerod (to remain compatible with future extensions)
75and then initialized.
76.Bd -literal
77struct tls_info {
78        void	*base;		/* base address of TLS area */
79        int	size;		/* size of TLS area in bytes */
80};
81.Ed
82.Pp
83The actual implementation of the area is machine-dependent.
84If the kernel
85is unable to accommodate the supplied size it may create a larger area.
86If the kernel is unable to accommodate the supplied base address an error
87will be returned.
88.Sh RETURN VALUES
89A return value of 0 is returned on success, -1 on error.
90.Sh EXAMPLES
91.Bd -literal -compact
92
93/*
94 * Pseudo example showing how the TLS system calls work on IA32.
95 */
96#include <stdio.h>
97#include <unistd.h>
98#include <stdlib.h>
99#include <errno.h>
100#include <sys/tls.h>
101
102int X;
103
104static int getdata(int offset);
105
106int
107main(int ac, char **av)
108{
109    int i;
110    int gs;
111    struct tls_info info;
112
113    info.base = &X;
114    info.size = sizeof(X);
115    if ((gs = set_tls_area(0, &info, sizeof(info))) < 0) {
116        perror("setarea");
117        exit(1);
118    }
119    printf("gs = %04x\en", gs);
120    __asm __volatile("mov %0,%%gs" : : "g" (gs) );
121
122    if (get_tls_area(0, &info, sizeof(info)) < 0) {
123        perror("getarea");
124        exit(1);
125    }
126    printf("%p/%d\en", info.base, info.size);
127
128    X = 1;
129    printf("should be 1: %d\en", getdata(0));
130    X = 2;
131    printf("should be 2: %d\en", getdata(0));
132
133    printf("this should fault:\en");
134    fflush(stdout);
135    getdata(4);
136
137    return(0);
138}
139
140static int
141getdata(int offset)
142{
143    int rv;
144    __asm __volatile("movl %%gs:(%0),%%eax; movl %%eax,%1" : "+r" (offset) : "m"
145 (rv) : "ax");
146    return (rv);
147}
148
149.Ed
150.Sh ERRORS
151.Bl -tag -width Er
152.It Bq Er ERANGE
153The specified facility index,
154.Fa which ,
155is not supported.
156.It Bq Er EINVAL
157An invalid parameter has been specified.
158.It Bq Er ENOENT
159(get_tls_area) The specified facility has not been initialized with
160.Fn sys_set_tls_area .
161.El
162.Sh SEE ALSO
163.Xr umtx 2
164.Sh HISTORY
165The
166.Fn set_tls_area ,
167and
168.Fn get_tls_area
169function calls first appeared in
170.Dx 1.1 .
171