| 1 | /*	$OpenBSD: tcb.h,v 1.3 2021/05/12 01:20:52 jsg Exp $	*/ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2019 Brian Bamsch <bbamsch@google.com> |
| 5 | * Copyright (c) 2011 Philip Guenther <guenther@openbsd.org> |
| 6 | * |
| 7 | * Permission to use, copy, modify, and distribute this software for any |
| 8 | * purpose with or without fee is hereby granted, provided that the above |
| 9 | * copyright notice and this permission notice appear in all copies. |
| 10 | * |
| 11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | */ |
| 19 | |
| 20 | #ifndef _MACHINE_TCB_H_ |
| 21 | #define _MACHINE_TCB_H_ |
| 22 | |
| 23 | #ifdef _KERNEL |
| 24 | |
| 25 | #include <machine/pcb.h> |
| 26 | |
| 27 | #define TCB_GET(p)		\ |
| 28 | 	((void *)(((struct pcb *)(p)->p_addr)->pcb_tf->tf_tp)) |
| 29 | |
| 30 | #define TCB_SET(p, addr)						\ |
| 31 | 	do {								\ |
| 32 | 		((struct pcb *)(p)->p_addr)->pcb_tf->tf_tp = (long)(addr); \ |
| 33 | 	} while (0) |
| 34 | |
| 35 | #else /* _KERNEL */ |
| 36 | |
| 37 | /* ELF TLS ABI calls for small TCB, with static TLS data after it */ |
| 38 | #define TLS_VARIANT	1 |
| 39 | |
| 40 | static inline void * |
| 41 | __riscv64_read_tcb(void) |
| 42 | { |
| 43 | 	void *tcb; |
| 44 | 	__asm volatile("mv %0, tp": "=r" (tcb)); |
| 45 | 	return tcb; |
| 46 | } |
| 47 | |
| 48 | #define TCB_GET()		__riscv64_read_tcb() |
| 49 | |
| 50 | #endif /* _KERNEL */ |
| 51 | |
| 52 | #endif /* _MACHINE_TCB_H_ */ |