1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * from: src/lib/libthr/arch/arm/include/pthread_md.h,v 1.3 2005/10/29 13:40:31 davidxu
29 * $FreeBSD: stable/12/lib/libthr/arch/mips/include/pthread_md.h 326219 2017-11-26 02:00:33Z pfg $
30 */
31
32 /*
33 * Machine-dependent thread prototypes/definitions.
34 */
35 #ifndef _PTHREAD_MD_H_
36 #define _PTHREAD_MD_H_
37
38 #include <sys/types.h>
39 #include <machine/sysarch.h>
40 #include <machine/tls.h>
41 #include <stddef.h>
42
43 #define CPU_SPINWAIT
44 #define DTV_OFFSET offsetof(struct tcb, tcb_dtv)
45
46 /*
47 * Variant I tcb. The structure layout is fixed, don't blindly
48 * change it!
49 */
50 struct tcb {
51 void *tcb_dtv;
52 struct pthread *tcb_thread;
53 };
54
55 /* Called from the thread to set its private data. */
56 static __inline void
_tcb_set(struct tcb * tcb)57 _tcb_set(struct tcb *tcb)
58 {
59
60 sysarch(MIPS_SET_TLS, tcb);
61 }
62
63 /*
64 * Get the current tcb.
65 */
66 #ifdef TLS_USE_SYSARCH
67 static __inline struct tcb *
_tcb_get(void)68 _tcb_get(void)
69 {
70 struct tcb *tcb;
71
72 sysarch(MIPS_GET_TLS, &tcb);
73 return tcb;
74 }
75
76 #else /* ! TLS_USE_SYSARCH */
77
78 # if defined(__mips_n64)
79 static __inline struct tcb *
_tcb_get(void)80 _tcb_get(void)
81 {
82 uint64_t _rv;
83
84 __asm__ __volatile__ (
85 ".set\tpush\n\t"
86 ".set\tmips64r2\n\t"
87 "rdhwr\t%0, $29\n\t"
88 ".set\tpop"
89 : "=r" (_rv));
90
91 /*
92 * XXXSS See 'git show c6be4f4d2d1b71c04de5d3bbb6933ce2dbcdb317'
93 *
94 * Remove the offset since this really a request to get the TLS
95 * pointer via sysarch() (in theory). Of course, this may go away
96 * once the TLS code is rewritten.
97 */
98 return (struct tcb *)(_rv - TLS_TP_OFFSET - TLS_TCB_SIZE);
99 }
100 # else /* mips 32 */
101 static __inline struct tcb *
_tcb_get(void)102 _tcb_get(void)
103 {
104 uint32_t _rv;
105
106 __asm__ __volatile__ (
107 ".set\tpush\n\t"
108 ".set\tmips32r2\n\t"
109 "rdhwr\t%0, $29\n\t"
110 ".set\tpop"
111 : "=r" (_rv));
112
113 /*
114 * XXXSS See 'git show c6be4f4d2d1b71c04de5d3bbb6933ce2dbcdb317'
115 *
116 * Remove the offset since this really a request to get the TLS
117 * pointer via sysarch() (in theory). Of course, this may go away
118 * once the TLS code is rewritten.
119 */
120 return (struct tcb *)(_rv - TLS_TP_OFFSET - TLS_TCB_SIZE);
121 }
122 # endif /* ! __mips_n64 */
123 #endif /* ! TLS_USE_SYSARCH */
124
125 static __inline struct pthread *
_get_curthread(void)126 _get_curthread(void)
127 {
128 if (_thr_initial)
129 return (_tcb_get()->tcb_thread);
130 return (NULL);
131 }
132
133 #endif /* _PTHREAD_MD_H_ */
134