1/*	$OpenBSD: tcb.h,v 1.4 2016/09/04 08:49:35 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	((void *)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/* ELF TLS ABI calls for big TCB, with static TLS data at negative offsets */
34#define TLS_VARIANT	2
35
36/* Read a slot from the TCB */
37static inline void *
38__i386_read_tcb(int offset)
39{
40	void	*val;
41	__asm__ ("movl %%gs:(%1),%0" : "=r" (val) : "r" (offset));
42	return val;
43}
44
45/* Get a pointer to the TCB itself */
46#define TCB_GET()		__i386_read_tcb(0)
47
48/* Setting the TCB pointer can only be done via syscall, so no TCB_SET() */
49
50#endif /* _KERNEL */
51#endif /* _MACHINE_TCB_H_ */
52