tls-test-lib.c revision 256281
1117521Snjl/*- 2228110Sjkim * Copyright (C) 2004 NVIDIA Corporation. 3222544Sjkim * All rights reserved. 4222544Sjkim * 5228110Sjkim * Redistribution and use in source and binary forms, with or without 6222544Sjkim * modification, are permitted provided that the following conditions 7228110Sjkim * are met: 8228110Sjkim * 1. Redistributions of source code must retain the above copyright 9228110Sjkim * notice, this list of conditions and the following disclaimer. 10228110Sjkim * 2. Redistributions in binary form must reproduce the above copyright 11228110Sjkim * notice, this list of conditions and the following disclaimer in the 12228110Sjkim * documentation and/or other materials provided with the distribution. 13228110Sjkim * 14228110Sjkim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15228110Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16228110Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17228110Sjkim * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18228110Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19228110Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20228110Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21228110Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22228110Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23228110Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24228110Sjkim * SUCH DAMAGE. 25228110Sjkim * 26228110Sjkim * $FreeBSD: stable/10/tools/regression/tls/ttls3/tls-test-lib.c 133066 2004-08-03 09:04:01Z dfr $ 27228110Sjkim */ 28228110Sjkim 29228110Sjkim#include <stdio.h> 30228110Sjkim 31228110Sjkim#define __G_TLS_OFFSETS_SIZE 8 32228110Sjkimunsigned long int __gl_tls_offsets[__G_TLS_OFFSETS_SIZE]; 33228110Sjkim 34228110Sjkimvoid __gl_tls_init_offsets(); 35228110Sjkim 36228110Sjkim#ifdef __GL_TLS_SINGLE_INSTRUCTION 37228110Sjkim#define THREAD_GETMEM(num) \ 38228110Sjkim({ \ 39228110Sjkim void *__value; \ 40228110Sjkim __asm__ __volatile__ ( \ 41228110Sjkim "movl %%gs:(%1),%0" \ 42228110Sjkim : "=r" (__value) \ 43228110Sjkim : "r" (__gl_tls_offsets[num]) \ 44228110Sjkim ); \ 45228110Sjkim __value; \ 46228110Sjkim}) 47228110Sjkim 48228110Sjkim#define THREAD_SETMEM(num, value) \ 49228110Sjkimdo { \ 50228110Sjkim void *__value = (value); \ 51228110Sjkim __asm__ __volatile__ ( \ 52228110Sjkim "movl %0,%%gs:(%1)" \ 53228110Sjkim : \ 54228110Sjkim : "r" (__value), \ 55228110Sjkim "r" (__gl_tls_offsets[num]) \ 56228110Sjkim ); \ 57228110Sjkim} while (0) 58228110Sjkim#else 59228110Sjkim#define __GL_TLS_GET(num) \ 60228110Sjkim({ \ 61228110Sjkim void *__dummy, *__value; \ 62228110Sjkim __asm__ __volatile__ ( \ 63228110Sjkim "movl %%gs:0,%2 \n\t" \ 64228110Sjkim "movl (%2,%1),%0 \n\t" \ 65228110Sjkim : "=r" (__value) \ 66228110Sjkim : "r" (__gl_tls_offsets[num]), \ 67228110Sjkim "r" (__dummy) \ 68228110Sjkim ); \ 69228110Sjkim __value; \ 70228110Sjkim}) 71228110Sjkim 72228110Sjkim#define __GL_TLS_SET(num, value) \ 73228110Sjkimdo { \ 74228110Sjkim void *__dummy, *__value = (value); \ 75228110Sjkim __asm__ __volatile__ ( \ 76228110Sjkim "movl %%gs:0,%2 \n\t" \ 77228110Sjkim "movl %0,(%2,%1) \n\t" \ 78228110Sjkim : \ 79228110Sjkim : "r" (__value), \ 80228110Sjkim "r" (__gl_tls_offsets[num]), \ 81228110Sjkim "r" (__dummy) \ 82228110Sjkim ); \ 83228110Sjkim} while (0) 84228110Sjkim#endif 85228110Sjkim 86228110Sjkimvoid _init(void) 87228110Sjkim{ 88228110Sjkim __gl_tls_init_offsets(); 89228110Sjkim 90228110Sjkim __GL_TLS_SET(0, (void *) 0xff000000); 91228110Sjkim __GL_TLS_SET(1, (void *) 0xff000001); 92228110Sjkim __GL_TLS_SET(2, (void *) 0xff000002); 93228110Sjkim __GL_TLS_SET(3, (void *) 0xff000003); 94228110Sjkim __GL_TLS_SET(4, (void *) 0xff000004); 95228110Sjkim __GL_TLS_SET(5, (void *) 0xff000005); 96228110Sjkim __GL_TLS_SET(6, (void *) 0xff000006); 97228110Sjkim __GL_TLS_SET(7, (void *) 0xff000007); 98228110Sjkim} 99228110Sjkim 100228110Sjkimvoid __gl_tls_test(void) 101228110Sjkim{ 102228110Sjkim printf("__GL_TLS_GET(0) = %p\n", __GL_TLS_GET(0)); 103228110Sjkim printf("__GL_TLS_GET(1) = %p\n", __GL_TLS_GET(1)); 104228110Sjkim printf("__GL_TLS_GET(2) = %p\n", __GL_TLS_GET(2)); 105228110Sjkim printf("__GL_TLS_GET(3) = %p\n", __GL_TLS_GET(3)); 106228110Sjkim printf("__GL_TLS_GET(4) = %p\n", __GL_TLS_GET(4)); 107228110Sjkim printf("__GL_TLS_GET(5) = %p\n", __GL_TLS_GET(5)); 108228110Sjkim printf("__GL_TLS_GET(6) = %p\n", __GL_TLS_GET(6)); 109228110Sjkim printf("__GL_TLS_GET(7) = %p\n", __GL_TLS_GET(7)); 110228110Sjkim} 111228110Sjkim