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