xref: /dflybsd-src/lib/libc/sys/tls.2 (revision e96f55de9c053d11dd80a1090c8924743713559d)
1806bf111SMatthew Dillon.\" Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
2806bf111SMatthew Dillon.\"
3806bf111SMatthew Dillon.\" This code is derived from software contributed to The DragonFly Project
4806bf111SMatthew Dillon.\" by David Xu <davidxu@freebsd.org> and Matthew Dillon <dillon@backplane.com>
5806bf111SMatthew Dillon.\"
6806bf111SMatthew Dillon.\" Redistribution and use in source and binary forms, with or without
7806bf111SMatthew Dillon.\" modification, are permitted provided that the following conditions
8806bf111SMatthew Dillon.\" are met:
9806bf111SMatthew Dillon.\"
10806bf111SMatthew Dillon.\" 1. Redistributions of source code must retain the above copyright
11806bf111SMatthew Dillon.\"    notice, this list of conditions and the following disclaimer.
12806bf111SMatthew Dillon.\" 2. Redistributions in binary form must reproduce the above copyright
13806bf111SMatthew Dillon.\"    notice, this list of conditions and the following disclaimer in
14806bf111SMatthew Dillon.\"    the documentation and/or other materials provided with the
15806bf111SMatthew Dillon.\"    distribution.
16806bf111SMatthew Dillon.\" 3. Neither the name of The DragonFly Project nor the names of its
17806bf111SMatthew Dillon.\"    contributors may be used to endorse or promote products derived
18806bf111SMatthew Dillon.\"    from this software without specific, prior written permission.
19806bf111SMatthew Dillon.\"
20806bf111SMatthew Dillon.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21806bf111SMatthew Dillon.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22806bf111SMatthew Dillon.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23806bf111SMatthew Dillon.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24806bf111SMatthew Dillon.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25806bf111SMatthew Dillon.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26806bf111SMatthew Dillon.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27806bf111SMatthew Dillon.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28806bf111SMatthew Dillon.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29806bf111SMatthew Dillon.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30806bf111SMatthew Dillon.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31806bf111SMatthew Dillon.\" SUCH DAMAGE.
32806bf111SMatthew Dillon.\"
33*e96f55deSSimon Schubert.\" $DragonFly: src/lib/libc/sys/tls.2,v 1.8 2007/04/13 12:12:27 corecode Exp $
34806bf111SMatthew Dillon.\"
35806bf111SMatthew Dillon.Dd February 21, 2005
36806bf111SMatthew Dillon.Dt TLS 2
37806bf111SMatthew Dillon.Os
38806bf111SMatthew Dillon.Sh NAME
39ba491dfbSMatthew Dillon.Nm set_tls_area ,
40ba491dfbSMatthew Dillon.Nm get_tls_area
41806bf111SMatthew Dillon.Nd kernel TLS (thread local storage) support.
42806bf111SMatthew Dillon.Sh LIBRARY
43806bf111SMatthew Dillon.Lb libc
44806bf111SMatthew Dillon.Sh SYNOPSIS
45806bf111SMatthew Dillon.In sys/tls.h
46806bf111SMatthew Dillon.Ft int
47ba491dfbSMatthew Dillon.Fn set_tls_area "int which" "struct tls_info *info" "size_t infosize"
48806bf111SMatthew Dillon.Ft int
49ba491dfbSMatthew Dillon.Fn get_tls_area "int which" "struct tls_info *info" "size_t infosize"
50806bf111SMatthew Dillon.Sh DESCRIPTION
51806bf111SMatthew DillonThe
52ba491dfbSMatthew Dillon.Fn set_tls_area
53806bf111SMatthew Dillonsystem call creates an entry for the TLS facility
54806bf111SMatthew Dillon.Fa which
55806bf111SMatthew Dillonrepresenting thread local storage as specified by the
56806bf111SMatthew Dillon.Fa info
57806bf111SMatthew Dillonstructure.  A descriptor representing the facility is returned, or -1 if
58806bf111SMatthew Dillonan error occured.  The facility may be cleared by specifying a NULL pointer
59806bf111SMatthew Dillonand an infosize of 0.
60806bf111SMatthew DillonThe
61ba491dfbSMatthew Dillon.Fn get_tls_area
62806bf111SMatthew Dillonsystem call retrieves the requested TLS facility.  A descriptor representing
63806bf111SMatthew Dillonthe facility is returned, or -1 if an error occured.  If you simply want the
64806bf111SMatthew Dillondescriptor you may specify a NULL pointer and an infosize of 0.
65806bf111SMatthew Dillon.Pp
66806bf111SMatthew DillonThe returned descriptor and the TLS mechanism is machine-dependant.  On IA32
67806bf111SMatthew Dillonthree global segment descriptors are supported  (0, 1, and 2) and the %gs
68806bf111SMatthew Dillonload value is returned.
69806bf111SMatthew Dillon.Pp
70806bf111SMatthew DillonThe
71806bf111SMatthew Dillon.Fa tls_info
72806bf111SMatthew Dillonstructure passed to
73ba491dfbSMatthew Dillon.Fn set_tls_area
74806bf111SMatthew Dillonshould first be zerod (to remain compatible with future extensions)
75806bf111SMatthew Dillonand then initialized.
76806bf111SMatthew Dillon.Pp
77806bf111SMatthew Dillon.Bd -literal
78806bf111SMatthew Dillonstruct tls_info {
79806bf111SMatthew Dillon        void	*base;		/* base address of TLS area */
80806bf111SMatthew Dillon        int	size;		/* size of TLS area in bytes */
81806bf111SMatthew Dillon};
82806bf111SMatthew Dillon.Ed
83806bf111SMatthew Dillon.Pp
84806bf111SMatthew DillonThe actual implementation of the area is machine-dependant.  If the kernel
85806bf111SMatthew Dillonis unable to accomodate the supplied size it may create a larger area.
86806bf111SMatthew DillonIf the kernel is unable to accomodate the supplied base address an error
87806bf111SMatthew Dillonwill be returned.
88806bf111SMatthew Dillon.Sh RETURN VALUES
89806bf111SMatthew DillonA return value of 0 is returned on success, -1 on error.
90806bf111SMatthew Dillon.Sh EXAMPLE
91806bf111SMatthew Dillon.Bd -literal -compact
92806bf111SMatthew Dillon
93806bf111SMatthew Dillon/*
94806bf111SMatthew Dillon * Pseudo example showing how the TLS system calls work on IA32.
95806bf111SMatthew Dillon */
96806bf111SMatthew Dillon#include <stdio.h>
97806bf111SMatthew Dillon#include <unistd.h>
98806bf111SMatthew Dillon#include <stdlib.h>
99806bf111SMatthew Dillon#include <errno.h>
100806bf111SMatthew Dillon#include <sys/tls.h>
101806bf111SMatthew Dillon
102806bf111SMatthew Dillonint X;
103806bf111SMatthew Dillon
104806bf111SMatthew Dillonstatic int getdata(int offset);
105806bf111SMatthew Dillon
106806bf111SMatthew Dillonint
107806bf111SMatthew Dillonmain(int ac, char **av)
108806bf111SMatthew Dillon{
109806bf111SMatthew Dillon    int i;
110806bf111SMatthew Dillon    int gs;
111806bf111SMatthew Dillon    struct tls_info info;
112806bf111SMatthew Dillon
113806bf111SMatthew Dillon    info.base = &X;
114806bf111SMatthew Dillon    info.size = sizeof(X);
115ba491dfbSMatthew Dillon    if ((gs = set_tls_area(0, &info, sizeof(info))) < 0) {
116806bf111SMatthew Dillon        perror("setarea");
117806bf111SMatthew Dillon        exit(1);
118806bf111SMatthew Dillon    }
119a3220ac5SSascha Wildner    printf("gs = %04x\en", gs);
120*e96f55deSSimon Schubert    __asm __volatile("mov %0,%%gs" : : "g" (gs) );
121806bf111SMatthew Dillon
122ba491dfbSMatthew Dillon    if (get_tls_area(0, &info, sizeof(info)) < 0) {
123806bf111SMatthew Dillon        perror("getarea");
124806bf111SMatthew Dillon        exit(1);
125806bf111SMatthew Dillon    }
126a3220ac5SSascha Wildner    printf("%p/%d\en", info.base, info.size);
127806bf111SMatthew Dillon
128806bf111SMatthew Dillon    X = 1;
129a3220ac5SSascha Wildner    printf("should be 1: %d\en", getdata(0));
130806bf111SMatthew Dillon    X = 2;
131a3220ac5SSascha Wildner    printf("should be 2: %d\en", getdata(0));
132806bf111SMatthew Dillon
133a3220ac5SSascha Wildner    printf("this should fault:\en");
134806bf111SMatthew Dillon    fflush(stdout);
135806bf111SMatthew Dillon    getdata(4);
136806bf111SMatthew Dillon
137806bf111SMatthew Dillon    return(0);
138806bf111SMatthew Dillon}
139806bf111SMatthew Dillon
140806bf111SMatthew Dillonstatic int
141806bf111SMatthew Dillongetdata(int offset)
142806bf111SMatthew Dillon{
143806bf111SMatthew Dillon    int rv;
144806bf111SMatthew Dillon    __asm __volatile("movl %%gs:(%0),%%eax; movl %%eax,%1" : "+r" (offset) : "m"
145806bf111SMatthew Dillon (rv) : "ax");
146806bf111SMatthew Dillon    return (rv);
147806bf111SMatthew Dillon}
148806bf111SMatthew Dillon
149806bf111SMatthew Dillon.Ed
1500b84df5cSSascha Wildner.Sh ERRORS
1510b84df5cSSascha Wildner.Bl -tag -width Er
1520b84df5cSSascha Wildner.It Bq Er ERANGE
1530b84df5cSSascha WildnerThe specified facility index,
1540b84df5cSSascha Wildner.Fa which ,
1550b84df5cSSascha Wildneris not supported.
1560b84df5cSSascha Wildner.It Bq Er EINVAL
1570b84df5cSSascha WildnerAn invalid parameter has been specified.
1580b84df5cSSascha Wildner.It Bq Er ENOENT
159ba491dfbSMatthew Dillon(get_tls_area) The specified facility has not been initialized with
1600b84df5cSSascha Wildner.Fn sys_set_tls_area .
1610b84df5cSSascha Wildner.El
162806bf111SMatthew Dillon.Sh SEE ALSO
163806bf111SMatthew Dillon.Xr umtx 2
164806bf111SMatthew Dillon.Sh HISTORY
165806bf111SMatthew DillonThe
166ba491dfbSMatthew Dillon.Fn set_tls_area ,
167806bf111SMatthew Dillonand
168ba491dfbSMatthew Dillon.Fn get_tls_area
169a3220ac5SSascha Wildnerfunction calls first appeared in
170a3220ac5SSascha Wildner.Dx 1.1 .
171