1 /* $OpenBSD: tcb.h,v 1.3 2014/03/29 18:09:29 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 /* 23 * In userspace, register %r27 contains the address of the thread's TCB, 24 * and register %r26 contains the address of the thread's errno. 25 * It is the responsibility of the kernel to set %r27 to the proper value 26 * when creating the thread, while initialization of %r26 is done in 27 * userland within libpthread on a needed basis. 28 */ 29 30 #ifdef _KERNEL 31 32 #include <machine/reg.h> 33 34 #define TCB_GET(p) \ 35 ((void *)(p)->p_md.md_tf->tf_r[27]) 36 #define TCB_SET(p, addr) \ 37 ((p)->p_md.md_tf->tf_r[27] = (register_t)(addr)) 38 39 #else /* _KERNEL */ 40 41 /* 42 * It is unknown whether the m88k ELF ABI mentions TLS. On m88k, since only 43 * unsigned offsets in (register + immediate offset) addressing is supported 44 * on all processors, it makes sense to use a small TCB, with static TLS data 45 * after it. 46 */ 47 #define TLS_VARIANT 1 48 49 #if defined(__GNUC__) && __GNUC__ > 4 50 51 struct thread_control_block; 52 __register__ struct thread_control_block *__tcb __asm__ ("%r27"); 53 #define TCB_GET() (__tcb) 54 #define TCB_SET(tcb) ((__tcb) = (tcb)) 55 #define TCB_GET_MEMBER(member) ((void *)(__tcb->member)) 56 57 #else /* __GNUC__ > 4 */ 58 59 #include <stddef.h> /* for offsetof */ 60 61 /* Get a pointer to the TCB itself */ 62 static inline void * 63 __m88k_get_tcb(void) 64 { 65 void *val; 66 __asm__ ("or %0,%%r27,%%r0" : "=r" (val)); 67 return val; 68 } 69 70 /* Get the value of a specific member in the TCB */ 71 static inline void * 72 __m88k_read_tcb(size_t offset) 73 { 74 void *val; 75 /* XXX the `offset' constraint ought to be "I" but this causes a warning */ 76 __asm__ ("ld %0,%%r27,%1" : "=r" (val) : "r" (offset)); 77 return val; 78 } 79 80 #define TCB_GET() __m88k_get_tcb() 81 #define TCB_SET(tcb) __asm volatile("or %%r27,%0,%r0" : : "r" (tcb)) 82 83 #define TCB_GET_MEMBER(member) \ 84 __m88k_read_tcb(offsetof(struct thread_control_block, member)) 85 86 #endif /* __GNUC__ > 4 */ 87 88 #endif /* _KERNEL */ 89 90 #endif /* _MACHINE_TCB_H_ */ 91