xref: /openbsd/sys/arch/mips64/include/tcb.h (revision 4cfece93)
1 /*	$OpenBSD: tcb.h,v 1.4 2017/04/20 16:07:52 visa 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 static inline void
25 __mips64_set_tcb(struct proc *p, void *tcb)
26 {
27 #ifdef CPU_MIPS64R2
28 	extern int cpu_has_userlocal;
29 
30 	if (cpu_has_userlocal)
31 		cp0_set_userlocal(tcb);
32 #endif
33 
34 	p->p_md.md_tcb = tcb;
35 }
36 
37 #define TCB_SET(p, addr)	__mips64_set_tcb(p, addr)
38 #define TCB_GET(p)		((p)->p_md.md_tcb)
39 
40 #else /* _KERNEL */
41 
42 /* ELF TLS ABI calls for small TCB, with static TLS data after it */
43 #define TLS_VARIANT	1
44 
45 static inline void *
46 __mips64_get_tcb(void)
47 {
48 	void *tcb;
49 
50 	/*
51 	 * This invokes emulation in kernel if the system does not implement
52 	 * the RDHWR instruction or the UserLocal register.
53 	 */
54 	__asm__ volatile (
55 	"	.set	push\n"
56 	"	.set	mips64r2\n"
57 	"	rdhwr	%0, $29\n"
58 	"	.set	pop\n" : "=r" (tcb));
59 	return tcb;
60 }
61 
62 #define TCB_GET()		__mips64_get_tcb()
63 
64 #endif /* _KERNEL */
65 
66 #endif /* _MACHINE_TCB_H_ */
67