11a62e9bcSJohn Baldwin /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31a62e9bcSJohn Baldwin *
41a62e9bcSJohn Baldwin * Copyright (C) 2003 David Xu <davidxu@freebsd.org>
51a62e9bcSJohn Baldwin * Copyright (c) 2001 Daniel Eischen <deischen@freebsd.org>
61a62e9bcSJohn Baldwin * All rights reserved.
71a62e9bcSJohn Baldwin *
81a62e9bcSJohn Baldwin * Redistribution and use in source and binary forms, with or without
91a62e9bcSJohn Baldwin * modification, are permitted provided that the following conditions
101a62e9bcSJohn Baldwin * are met:
111a62e9bcSJohn Baldwin * 1. Redistributions of source code must retain the above copyright
121a62e9bcSJohn Baldwin * notice, this list of conditions and the following disclaimer.
131a62e9bcSJohn Baldwin * 2. Neither the name of the author nor the names of its contributors
141a62e9bcSJohn Baldwin * may be used to endorse or promote products derived from this software
151a62e9bcSJohn Baldwin * without specific prior written permission.
161a62e9bcSJohn Baldwin *
171a62e9bcSJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
181a62e9bcSJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191a62e9bcSJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201a62e9bcSJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
211a62e9bcSJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221a62e9bcSJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231a62e9bcSJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241a62e9bcSJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251a62e9bcSJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261a62e9bcSJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271a62e9bcSJohn Baldwin * SUCH DAMAGE.
281a62e9bcSJohn Baldwin */
291a62e9bcSJohn Baldwin
301a62e9bcSJohn Baldwin #ifndef _MACHINE_TLS_H_
311a62e9bcSJohn Baldwin #define _MACHINE_TLS_H_
321a62e9bcSJohn Baldwin
331a62e9bcSJohn Baldwin #include <x86/sysarch.h>
341a62e9bcSJohn Baldwin
351a62e9bcSJohn Baldwin #define TLS_VARIANT_II
361a62e9bcSJohn Baldwin
371a62e9bcSJohn Baldwin struct pthread;
381a62e9bcSJohn Baldwin
391a62e9bcSJohn Baldwin /*
401a62e9bcSJohn Baldwin * Variant II tcb, first two members are required by rtld,
411a62e9bcSJohn Baldwin * %fs (amd64) / %gs (i386) points to the structure.
421a62e9bcSJohn Baldwin */
431a62e9bcSJohn Baldwin struct tcb {
441a62e9bcSJohn Baldwin struct tcb *tcb_self; /* required by rtld */
451a62e9bcSJohn Baldwin uintptr_t *tcb_dtv; /* required by rtld */
461a62e9bcSJohn Baldwin struct pthread *tcb_thread;
471a62e9bcSJohn Baldwin };
481a62e9bcSJohn Baldwin
491a62e9bcSJohn Baldwin #define TLS_DTV_OFFSET 0
501a62e9bcSJohn Baldwin #ifdef __amd64__
511a62e9bcSJohn Baldwin #define TLS_TCB_ALIGN 16
521a62e9bcSJohn Baldwin #else
531a62e9bcSJohn Baldwin #define TLS_TCB_ALIGN 4
541a62e9bcSJohn Baldwin #endif
551a62e9bcSJohn Baldwin #define TLS_TCB_SIZE sizeof(struct tcb)
561a62e9bcSJohn Baldwin #define TLS_TP_OFFSET 0
571a62e9bcSJohn Baldwin
581a62e9bcSJohn Baldwin static __inline void
_tcb_set(struct tcb * tcb)591a62e9bcSJohn Baldwin _tcb_set(struct tcb *tcb)
601a62e9bcSJohn Baldwin {
611a62e9bcSJohn Baldwin #ifdef __amd64__
621a62e9bcSJohn Baldwin amd64_set_fsbase(tcb);
631a62e9bcSJohn Baldwin #else
641a62e9bcSJohn Baldwin i386_set_gsbase(tcb);
651a62e9bcSJohn Baldwin #endif
661a62e9bcSJohn Baldwin }
671a62e9bcSJohn Baldwin
681a62e9bcSJohn Baldwin static __inline struct tcb *
_tcb_get(void)691a62e9bcSJohn Baldwin _tcb_get(void)
701a62e9bcSJohn Baldwin {
711a62e9bcSJohn Baldwin struct tcb *tcb;
721a62e9bcSJohn Baldwin
731a62e9bcSJohn Baldwin #ifdef __amd64__
741a62e9bcSJohn Baldwin __asm __volatile("movq %%fs:0, %0" : "=r" (tcb));
751a62e9bcSJohn Baldwin #else
761a62e9bcSJohn Baldwin __asm __volatile("movl %%gs:0, %0" : "=r" (tcb));
771a62e9bcSJohn Baldwin #endif
781a62e9bcSJohn Baldwin return (tcb);
791a62e9bcSJohn Baldwin }
801a62e9bcSJohn Baldwin
811a62e9bcSJohn Baldwin #endif /* !_MACHINE_TLS_H_ */
82