xref: /freebsd/sys/amd64/include/pcpu.h (revision dba6dd177bdee890cf445fbe21a5dccefd5de18e)
1 /*-
2  * Copyright (c) Peter Wemm <peter@netplex.com.au>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _MACHINE_PCPU_H_
30 #define _MACHINE_PCPU_H_
31 
32 #ifdef _KERNEL
33 
34 /*
35  * The SMP parts are setup in pmap.c and locore.s for the BSP, and
36  * mp_machdep.c sets up the data for the AP's to "see" when they awake.
37  * The reason for doing it via a struct is so that an array of pointers
38  * to each CPU's data can be set up for things like "check curproc on all
39  * other processors"
40  */
41 #define	PCPU_MD_FIELDS							\
42 	struct	pcpu *pc_prvspace;	/* Self-reference */		\
43 	struct	pmap *pc_curpmap;					\
44 	struct	amd64tss *pc_tssp;					\
45 	register_t pc_rsp0;						\
46 	register_t pc_scratch_rsp;	/* User %rsp in syscall */	\
47 	u_int	pc_apic_id;						\
48 	u_int   pc_acpi_id		/* ACPI CPU id */
49 
50 #if defined(lint)
51 
52 extern struct pcpu *pcpup;
53 
54 #define PCPU_GET(member)        (pcpup->pc_ ## member)
55 #define PCPU_PTR(member)        (&pcpup->pc_ ## member)
56 #define PCPU_SET(member,value)  (pcpup->pc_ ## member = (value))
57 
58 #elif defined(__GNUC__)
59 
60 /*
61  * Evaluates to the byte offset of the per-cpu variable name.
62  */
63 #define	__pcpu_offset(name)						\
64 	__offsetof(struct pcpu, name)
65 
66 /*
67  * Evaluates to the type of the per-cpu variable name.
68  */
69 #define	__pcpu_type(name)						\
70 	__typeof(((struct pcpu *)0)->name)
71 
72 /*
73  * Evaluates to the address of the per-cpu variable name.
74  */
75 #define	__PCPU_PTR(name) __extension__ ({				\
76 	__pcpu_type(name) *__p;						\
77 									\
78 	__asm __volatile("movq %%gs:%1,%0; addq %2,%0"			\
79 	    : "=r" (__p)						\
80 	    : "m" (*(struct pcpu *)(__pcpu_offset(pc_prvspace))),	\
81 	      "i" (__pcpu_offset(name)));				\
82 									\
83 	__p;								\
84 })
85 
86 /*
87  * Evaluates to the value of the per-cpu variable name.
88  */
89 #define	__PCPU_GET(name) __extension__ ({				\
90 	__pcpu_type(name) __result;					\
91 									\
92 	if (sizeof(__result) == 1) {					\
93 		u_char __b;						\
94 		__asm __volatile("movb %%gs:%1,%0"			\
95 		    : "=r" (__b)					\
96 		    : "m" (*(u_char *)(__pcpu_offset(name))));		\
97 		__result = *(__pcpu_type(name) *)&__b;			\
98 	} else if (sizeof(__result) == 2) {				\
99 		u_short __w;						\
100 		__asm __volatile("movw %%gs:%1,%0"			\
101 		    : "=r" (__w)					\
102 		    : "m" (*(u_short *)(__pcpu_offset(name))));		\
103 		__result = *(__pcpu_type(name) *)&__w;			\
104 	} else if (sizeof(__result) == 4) {				\
105 		u_int __i;						\
106 		__asm __volatile("movl %%gs:%1,%0"			\
107 		    : "=r" (__i)					\
108 		    : "m" (*(u_int *)(__pcpu_offset(name))));		\
109 		__result = *(__pcpu_type(name) *)&__i;			\
110 	} else if (sizeof(__result) == 8) {				\
111 		u_long __l;						\
112 		__asm __volatile("movq %%gs:%1,%0"			\
113 		    : "=r" (__l)					\
114 		    : "m" (*(u_long *)(__pcpu_offset(name))));		\
115 		__result = *(__pcpu_type(name) *)&__l;			\
116 	} else {							\
117 		__result = *__PCPU_PTR(name);				\
118 	}								\
119 									\
120 	__result;							\
121 })
122 
123 /*
124  * Sets the value of the per-cpu variable name to value val.
125  */
126 #define	__PCPU_SET(name, val) {						\
127 	__pcpu_type(name) __val = (val);				\
128 									\
129 	if (sizeof(__val) == 1) {					\
130 		u_char __b;						\
131 		__b = *(u_char *)&__val;				\
132 		__asm __volatile("movb %1,%%gs:%0"			\
133 		    : "=m" (*(u_char *)(__pcpu_offset(name)))		\
134 		    : "r" (__b));					\
135 	} else if (sizeof(__val) == 2) {				\
136 		u_short __w;						\
137 		__w = *(u_short *)&__val;				\
138 		__asm __volatile("movw %1,%%gs:%0"			\
139 		    : "=m" (*(u_short *)(__pcpu_offset(name)))		\
140 		    : "r" (__w));					\
141 	} else if (sizeof(__val) == 4) {				\
142 		u_int __i;						\
143 		__i = *(u_int *)&__val;					\
144 		__asm __volatile("movl %1,%%gs:%0"			\
145 		    : "=m" (*(u_int *)(__pcpu_offset(name)))		\
146 		    : "r" (__i));					\
147 	} else if (sizeof(__val) == 8) {				\
148 		u_long __l;						\
149 		__l = *(u_long *)&__val;				\
150 		__asm __volatile("movq %1,%%gs:%0"			\
151 		    : "=m" (*(u_long *)(__pcpu_offset(name)))		\
152 		    : "r" (__l));					\
153 	} else {							\
154 		*__PCPU_PTR(name) = __val;				\
155 	}								\
156 }
157 
158 #define	PCPU_GET(member)	__PCPU_GET(pc_ ## member)
159 #define	PCPU_PTR(member)	__PCPU_PTR(pc_ ## member)
160 #define	PCPU_SET(member, val)	__PCPU_SET(pc_ ## member, val)
161 
162 static __inline struct thread *
163 __curthread(void)
164 {
165 	struct thread *td;
166 
167 	__asm __volatile("movq %%gs:0,%0" : "=r" (td));
168 	return (td);
169 }
170 #define	curthread (__curthread())
171 
172 #else
173 #error gcc or lint is required to use this file
174 #endif
175 
176 #endif	/* _KERNEL */
177 
178 #endif	/* ! _MACHINE_PCPU_H_ */
179