1*d74b3acfSguenther /* $OpenBSD: tcb.h,v 1.6 2019/12/11 07:21:40 guenther Exp $ */
2022e6e66Sguenther
3022e6e66Sguenther /*
4022e6e66Sguenther * Copyright (c) 2011 Philip Guenther <guenther@openbsd.org>
5022e6e66Sguenther *
6022e6e66Sguenther * Permission to use, copy, modify, and distribute this software for any
7022e6e66Sguenther * purpose with or without fee is hereby granted, provided that the above
8022e6e66Sguenther * copyright notice and this permission notice appear in all copies.
9022e6e66Sguenther *
10022e6e66Sguenther * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11022e6e66Sguenther * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12022e6e66Sguenther * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13022e6e66Sguenther * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14022e6e66Sguenther * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15022e6e66Sguenther * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16022e6e66Sguenther * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17022e6e66Sguenther */
18022e6e66Sguenther
19022e6e66Sguenther #ifndef _MACHINE_TCB_H_
20022e6e66Sguenther #define _MACHINE_TCB_H_
21022e6e66Sguenther
22cf143958Smiod /*
23*d74b3acfSguenther * In userspace, register %r27 contains the address of the thread's TCB.
24cf143958Smiod * It is the responsibility of the kernel to set %r27 to the proper value
25*d74b3acfSguenther * when creating the thread.
26cf143958Smiod */
27cf143958Smiod
28022e6e66Sguenther #ifdef _KERNEL
29022e6e66Sguenther
30cf143958Smiod #include <machine/reg.h>
31cf143958Smiod
32cf143958Smiod #define TCB_GET(p) \
33cf143958Smiod ((void *)(p)->p_md.md_tf->tf_r[27])
34cf143958Smiod #define TCB_SET(p, addr) \
35cf143958Smiod ((p)->p_md.md_tf->tf_r[27] = (register_t)(addr))
36022e6e66Sguenther
37022e6e66Sguenther #else /* _KERNEL */
38022e6e66Sguenther
39cf143958Smiod /*
40cf143958Smiod * It is unknown whether the m88k ELF ABI mentions TLS. On m88k, since only
41cf143958Smiod * unsigned offsets in (register + immediate offset) addressing is supported
42cf143958Smiod * on all processors, it makes sense to use a small TCB, with static TLS data
43cf143958Smiod * after it.
44cf143958Smiod */
45cf143958Smiod #define TLS_VARIANT 1
46cf143958Smiod
47d031e57eSmiod #if defined(__GNUC__) && __GNUC__ >= 4
48cf143958Smiod
4996b8842bSguenther register void *__tcb __asm__ ("%r27");
50cf143958Smiod #define TCB_GET() (__tcb)
51cf143958Smiod #define TCB_SET(tcb) ((__tcb) = (tcb))
52cf143958Smiod
53d031e57eSmiod #else /* __GNUC__ >= 4 */
54cf143958Smiod
55cf143958Smiod /* Get a pointer to the TCB itself */
56cf143958Smiod static inline void *
__m88k_get_tcb(void)57cf143958Smiod __m88k_get_tcb(void)
58cf143958Smiod {
59cf143958Smiod void *val;
60cf143958Smiod __asm__ ("or %0,%%r27,%%r0" : "=r" (val));
61cf143958Smiod return val;
62cf143958Smiod }
63cf143958Smiod
64cf143958Smiod #define TCB_GET() __m88k_get_tcb()
652df76cc2Sguenther #define TCB_SET(tcb) __asm volatile("or %%r27,%0,%r0" : : "r" (tcb))
66cf143958Smiod
67d031e57eSmiod #endif /* __GNUC__ >= 4 */
68022e6e66Sguenther
69022e6e66Sguenther #endif /* _KERNEL */
70022e6e66Sguenther #endif /* _MACHINE_TCB_H_ */
71