xref: /openbsd/sys/arch/i386/include/tcb.h (revision a6445c1d)
1 /*	$OpenBSD: tcb.h,v 1.2 2011/12/24 04:21:19 guenther Exp $	*/
2 /*
3  * Copyright (c) 2011 Philip Guenther <guenther@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef _MACHINE_TCB_H_
19 #define _MACHINE_TCB_H_
20 
21 #ifdef _KERNEL
22 
23 #include <machine/sysarch.h>
24 #include <machine/pcb.h>
25 
26 #define TCB_GET(p)		\
27 	i386_get_threadbase(p, TSEG_GS)
28 #define TCB_SET(p, addr)	\
29 	i386_set_threadbase(p, (uint32_t)(addr), TSEG_GS)
30 
31 #else /* _KERNEL */
32 
33 #include <stddef.h>		/* for offsetof */
34 
35 /* ELF TLS ABI calls for big TCB, with static TLS data at negative offsets */
36 #define TLS_VARIANT	2
37 
38 /* Read a slot from the TCB */
39 static inline void *
40 __i386_read_tcb(int offset)
41 {
42 	void	*val;
43 	__asm__ ("movl %%gs:(%1),%0" : "=r" (val) : "r" (offset));
44 	return val;
45 }
46 
47 /* Get a pointer to the TCB itself */
48 #define TCB_GET()		__i386_read_tcb(0)
49 
50 /* Get the value of a specific member in the TCB */
51 #define TCB_GET_MEMBER(member)	\
52 	__i386_read_tcb(offsetof(struct thread_control_block, member))
53 
54 /* Setting the TCB pointer can only be done via syscall, so no TCB_SET() */
55 
56 #endif /* _KERNEL */
57 
58 #endif /* _MACHINE_TCB_H_ */
59