xref: /freebsd/tools/regression/tls/ttls3/tls-test-lib.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
12546665aSDoug Rabson /*-
22546665aSDoug Rabson  * Copyright (C) 2004 NVIDIA Corporation.
32546665aSDoug Rabson  * All rights reserved.
42546665aSDoug Rabson  *
52546665aSDoug Rabson  * Redistribution and use in source and binary forms, with or without
62546665aSDoug Rabson  * modification, are permitted provided that the following conditions
72546665aSDoug Rabson  * are met:
82546665aSDoug Rabson  * 1. Redistributions of source code must retain the above copyright
92546665aSDoug Rabson  *    notice, this list of conditions and the following disclaimer.
102546665aSDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
112546665aSDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
122546665aSDoug Rabson  *    documentation and/or other materials provided with the distribution.
132546665aSDoug Rabson  *
142546665aSDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152546665aSDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162546665aSDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172546665aSDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182546665aSDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192546665aSDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202546665aSDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212546665aSDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222546665aSDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232546665aSDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242546665aSDoug Rabson  * SUCH DAMAGE.
252546665aSDoug Rabson  */
262546665aSDoug Rabson 
272546665aSDoug Rabson #include <stdio.h>
282546665aSDoug Rabson 
292546665aSDoug Rabson #define __G_TLS_OFFSETS_SIZE    8
302546665aSDoug Rabson unsigned long int __gl_tls_offsets[__G_TLS_OFFSETS_SIZE];
312546665aSDoug Rabson 
322546665aSDoug Rabson void __gl_tls_init_offsets();
332546665aSDoug Rabson 
342546665aSDoug Rabson #ifdef __GL_TLS_SINGLE_INSTRUCTION
352546665aSDoug Rabson #define THREAD_GETMEM(num)                  \
362546665aSDoug Rabson ({                                          \
372546665aSDoug Rabson     void *__value;                          \
382546665aSDoug Rabson     __asm__ __volatile__ (                  \
392546665aSDoug Rabson         "movl %%gs:(%1),%0"                 \
402546665aSDoug Rabson         : "=r" (__value)                    \
412546665aSDoug Rabson         : "r" (__gl_tls_offsets[num])       \
422546665aSDoug Rabson     );                                      \
432546665aSDoug Rabson     __value;                                \
442546665aSDoug Rabson })
452546665aSDoug Rabson 
462546665aSDoug Rabson #define THREAD_SETMEM(num, value)           \
472546665aSDoug Rabson do {                                        \
482546665aSDoug Rabson     void *__value = (value);                \
492546665aSDoug Rabson     __asm__ __volatile__ (                  \
502546665aSDoug Rabson         "movl %0,%%gs:(%1)"                 \
512546665aSDoug Rabson         :                                   \
522546665aSDoug Rabson         : "r" (__value),                    \
532546665aSDoug Rabson           "r" (__gl_tls_offsets[num])       \
542546665aSDoug Rabson     );                                      \
552546665aSDoug Rabson } while (0)
562546665aSDoug Rabson #else
572546665aSDoug Rabson #define __GL_TLS_GET(num)                   \
582546665aSDoug Rabson ({                                          \
592546665aSDoug Rabson     void *__dummy, *__value;                \
602546665aSDoug Rabson     __asm__ __volatile__ (                  \
612546665aSDoug Rabson         "movl %%gs:0,%2     \n\t"           \
622546665aSDoug Rabson         "movl (%2,%1),%0    \n\t"           \
632546665aSDoug Rabson         : "=r" (__value)                    \
642546665aSDoug Rabson         : "r" (__gl_tls_offsets[num]),      \
652546665aSDoug Rabson           "r" (__dummy)                     \
662546665aSDoug Rabson     );                                      \
672546665aSDoug Rabson     __value;                                \
682546665aSDoug Rabson })
692546665aSDoug Rabson 
702546665aSDoug Rabson #define __GL_TLS_SET(num, value)            \
712546665aSDoug Rabson do {                                        \
722546665aSDoug Rabson     void *__dummy, *__value = (value);      \
732546665aSDoug Rabson     __asm__ __volatile__ (                  \
742546665aSDoug Rabson         "movl %%gs:0,%2     \n\t"           \
752546665aSDoug Rabson         "movl %0,(%2,%1)    \n\t"           \
762546665aSDoug Rabson         :                                   \
772546665aSDoug Rabson         : "r" (__value),                    \
782546665aSDoug Rabson           "r" (__gl_tls_offsets[num]),      \
792546665aSDoug Rabson           "r" (__dummy)                     \
802546665aSDoug Rabson     );                                      \
812546665aSDoug Rabson } while (0)
822546665aSDoug Rabson #endif
832546665aSDoug Rabson 
_init(void)842546665aSDoug Rabson void _init(void)
852546665aSDoug Rabson {
862546665aSDoug Rabson     __gl_tls_init_offsets();
872546665aSDoug Rabson 
882546665aSDoug Rabson     __GL_TLS_SET(0, (void *) 0xff000000);
892546665aSDoug Rabson     __GL_TLS_SET(1, (void *) 0xff000001);
902546665aSDoug Rabson     __GL_TLS_SET(2, (void *) 0xff000002);
912546665aSDoug Rabson     __GL_TLS_SET(3, (void *) 0xff000003);
922546665aSDoug Rabson     __GL_TLS_SET(4, (void *) 0xff000004);
932546665aSDoug Rabson     __GL_TLS_SET(5, (void *) 0xff000005);
942546665aSDoug Rabson     __GL_TLS_SET(6, (void *) 0xff000006);
952546665aSDoug Rabson     __GL_TLS_SET(7, (void *) 0xff000007);
962546665aSDoug Rabson }
972546665aSDoug Rabson 
__gl_tls_test(void)982546665aSDoug Rabson void __gl_tls_test(void)
992546665aSDoug Rabson {
1002546665aSDoug Rabson     printf("__GL_TLS_GET(0) = %p\n", __GL_TLS_GET(0));
1012546665aSDoug Rabson     printf("__GL_TLS_GET(1) = %p\n", __GL_TLS_GET(1));
1022546665aSDoug Rabson     printf("__GL_TLS_GET(2) = %p\n", __GL_TLS_GET(2));
1032546665aSDoug Rabson     printf("__GL_TLS_GET(3) = %p\n", __GL_TLS_GET(3));
1042546665aSDoug Rabson     printf("__GL_TLS_GET(4) = %p\n", __GL_TLS_GET(4));
1052546665aSDoug Rabson     printf("__GL_TLS_GET(5) = %p\n", __GL_TLS_GET(5));
1062546665aSDoug Rabson     printf("__GL_TLS_GET(6) = %p\n", __GL_TLS_GET(6));
1072546665aSDoug Rabson     printf("__GL_TLS_GET(7) = %p\n", __GL_TLS_GET(7));
1082546665aSDoug Rabson }
109