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