xref: /freebsd/tools/regression/tls/ttls3/tls-test-lib.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (C) 2004 NVIDIA Corporation.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *	$FreeBSD$
27  */
28 
29 #include <stdio.h>
30 
31 #define __G_TLS_OFFSETS_SIZE    8
32 unsigned long int __gl_tls_offsets[__G_TLS_OFFSETS_SIZE];
33 
34 void __gl_tls_init_offsets();
35 
36 #ifdef __GL_TLS_SINGLE_INSTRUCTION
37 #define THREAD_GETMEM(num)                  \
38 ({                                          \
39     void *__value;                          \
40     __asm__ __volatile__ (                  \
41         "movl %%gs:(%1),%0"                 \
42         : "=r" (__value)                    \
43         : "r" (__gl_tls_offsets[num])       \
44     );                                      \
45     __value;                                \
46 })
47 
48 #define THREAD_SETMEM(num, value)           \
49 do {                                        \
50     void *__value = (value);                \
51     __asm__ __volatile__ (                  \
52         "movl %0,%%gs:(%1)"                 \
53         :                                   \
54         : "r" (__value),                    \
55           "r" (__gl_tls_offsets[num])       \
56     );                                      \
57 } while (0)
58 #else
59 #define __GL_TLS_GET(num)                   \
60 ({                                          \
61     void *__dummy, *__value;                \
62     __asm__ __volatile__ (                  \
63         "movl %%gs:0,%2     \n\t"           \
64         "movl (%2,%1),%0    \n\t"           \
65         : "=r" (__value)                    \
66         : "r" (__gl_tls_offsets[num]),      \
67           "r" (__dummy)                     \
68     );                                      \
69     __value;                                \
70 })
71 
72 #define __GL_TLS_SET(num, value)            \
73 do {                                        \
74     void *__dummy, *__value = (value);      \
75     __asm__ __volatile__ (                  \
76         "movl %%gs:0,%2     \n\t"           \
77         "movl %0,(%2,%1)    \n\t"           \
78         :                                   \
79         : "r" (__value),                    \
80           "r" (__gl_tls_offsets[num]),      \
81           "r" (__dummy)                     \
82     );                                      \
83 } while (0)
84 #endif
85 
86 void _init(void)
87 {
88     __gl_tls_init_offsets();
89 
90     __GL_TLS_SET(0, (void *) 0xff000000);
91     __GL_TLS_SET(1, (void *) 0xff000001);
92     __GL_TLS_SET(2, (void *) 0xff000002);
93     __GL_TLS_SET(3, (void *) 0xff000003);
94     __GL_TLS_SET(4, (void *) 0xff000004);
95     __GL_TLS_SET(5, (void *) 0xff000005);
96     __GL_TLS_SET(6, (void *) 0xff000006);
97     __GL_TLS_SET(7, (void *) 0xff000007);
98 }
99 
100 void __gl_tls_test(void)
101 {
102     printf("__GL_TLS_GET(0) = %p\n", __GL_TLS_GET(0));
103     printf("__GL_TLS_GET(1) = %p\n", __GL_TLS_GET(1));
104     printf("__GL_TLS_GET(2) = %p\n", __GL_TLS_GET(2));
105     printf("__GL_TLS_GET(3) = %p\n", __GL_TLS_GET(3));
106     printf("__GL_TLS_GET(4) = %p\n", __GL_TLS_GET(4));
107     printf("__GL_TLS_GET(5) = %p\n", __GL_TLS_GET(5));
108     printf("__GL_TLS_GET(6) = %p\n", __GL_TLS_GET(6));
109     printf("__GL_TLS_GET(7) = %p\n", __GL_TLS_GET(7));
110 }
111