xref: /freebsd/sys/i386/include/pcpu.h (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) Peter Wemm
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  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _MACHINE_PCPU_H_
32 #define	_MACHINE_PCPU_H_
33 
34 #include <machine/segments.h>
35 #include <machine/tss.h>
36 
37 #include <sys/_lock.h>
38 #include <sys/_mutex.h>
39 
40 struct monitorbuf {
41 	int idle_state;		/* Used by cpu_idle_mwait. */
42 	int stop_state;		/* Used by cpustop_handler. */
43 	char padding[128 - (2 * sizeof(int))];
44 };
45 _Static_assert(sizeof(struct monitorbuf) == 128, "2x cache line");
46 
47 /*
48  * The SMP parts are setup in pmap.c and machdep.c for the BSP, and
49  * pmap.c and mp_machdep.c sets up the data for the AP's to "see" when
50  * they awake.  The reason for doing it via a struct is so that an
51  * array of pointers to each CPU's data can be set up for things like
52  * "check curproc on all other processors"
53  */
54 
55 #define	PCPU_MD_FIELDS							\
56 	struct monitorbuf pc_monitorbuf __aligned(128);	/* cache line */\
57 	struct	pcpu *pc_prvspace;	/* Self-reference */		\
58 	struct	pmap *pc_curpmap;					\
59 	struct	segment_descriptor pc_common_tssd;			\
60 	struct	segment_descriptor *pc_tss_gdt;				\
61 	struct	segment_descriptor *pc_fsgs_gdt;			\
62 	struct	i386tss *pc_common_tssp;				\
63 	u_int	pc_kesp0;						\
64 	u_int	pc_trampstk;						\
65 	int	pc_currentldt;						\
66 	u_int   pc_acpi_id;		/* ACPI CPU id */		\
67 	u_int	pc_apic_id;						\
68 	int	pc_private_tss;		/* Flag indicating private tss*/\
69 	u_int	pc_cmci_mask;		/* MCx banks for CMCI */	\
70 	u_int	pc_vcpu_id;		/* Xen vCPU ID */		\
71 	struct	mtx pc_cmap_lock;					\
72 	void	*pc_cmap_pte1;						\
73 	void	*pc_cmap_pte2;						\
74 	caddr_t	pc_cmap_addr1;						\
75 	caddr_t	pc_cmap_addr2;						\
76 	vm_offset_t pc_qmap_addr;	/* KVA for temporary mappings */\
77 	vm_offset_t pc_copyout_maddr;					\
78 	vm_offset_t pc_copyout_saddr;					\
79 	struct	mtx pc_copyout_mlock;					\
80 	struct	sx pc_copyout_slock;					\
81 	char	*pc_copyout_buf;					\
82 	vm_offset_t pc_pmap_eh_va;					\
83 	caddr_t pc_pmap_eh_ptep;					\
84 	uint32_t pc_smp_tlb_done;	/* TLB op acknowledgement */	\
85 	uint32_t pc_ibpb_set;						\
86 	void	*pc_mds_buf;						\
87 	void	*pc_mds_buf64;						\
88 	uint32_t pc_pad[4];						\
89 	uint8_t	pc_mds_tmp[64];						\
90 	u_int	pc_ipi_bitmap;						\
91 	char	__pad[3518]
92 
93 #ifdef _KERNEL
94 
95 #define MONITOR_STOPSTATE_RUNNING	0
96 #define MONITOR_STOPSTATE_STOPPED	1
97 
98 /*
99  * Evaluates to the byte offset of the per-cpu variable name.
100  */
101 #define	__pcpu_offset(name)						\
102 	__offsetof(struct pcpu, name)
103 
104 /*
105  * Evaluates to the type of the per-cpu variable name.
106  */
107 #define	__pcpu_type(name)						\
108 	__typeof(((struct pcpu *)0)->name)
109 
110 /*
111  * Evaluates to the address of the per-cpu variable name.
112  */
113 #define	__PCPU_PTR(name) __extension__ ({				\
114 	__pcpu_type(name) *__p;						\
115 									\
116 	__asm __volatile("movl %%fs:%1,%0; addl %2,%0"			\
117 	    : "=r" (__p)						\
118 	    : "m" (*(struct pcpu *)(__pcpu_offset(pc_prvspace))),	\
119 	      "i" (__pcpu_offset(name)));				\
120 									\
121 	__p;								\
122 })
123 
124 /*
125  * Evaluates to the value of the per-cpu variable name.
126  */
127 #define	__PCPU_GET(name) __extension__ ({				\
128 	__pcpu_type(name) __res;					\
129 	struct __s {							\
130 		u_char	__b[MIN(sizeof(__res), 4)];			\
131 	} __s;								\
132 									\
133 	if (sizeof(__res) == 1 || sizeof(__res) == 2 ||			\
134 	    sizeof(__res) == 4) {					\
135 		__asm __volatile("mov %%fs:%1,%0"			\
136 		    : "=r" (__s)					\
137 		    : "m" (*(struct __s *)(__pcpu_offset(name))));	\
138 		*(struct __s *)(void *)&__res = __s;			\
139 	} else {							\
140 		__res = *__PCPU_PTR(name);				\
141 	}								\
142 	__res;								\
143 })
144 
145 /*
146  * Adds a value of the per-cpu counter name.  The implementation
147  * must be atomic with respect to interrupts.
148  */
149 #define	__PCPU_ADD(name, val) do {					\
150 	__pcpu_type(name) __val;					\
151 	struct __s {							\
152 		u_char	__b[MIN(sizeof(__val), 4)];			\
153 	} __s;								\
154 									\
155 	__val = (val);							\
156 	if (sizeof(__val) == 1 || sizeof(__val) == 2 ||			\
157 	    sizeof(__val) == 4) {					\
158 		__s = *(struct __s *)(void *)&__val;			\
159 		__asm __volatile("add %1,%%fs:%0"			\
160 		    : "=m" (*(struct __s *)(__pcpu_offset(name)))	\
161 		    : "r" (__s));					\
162 	} else								\
163 		*__PCPU_PTR(name) += __val;				\
164 } while (0)
165 
166 /*
167  * Sets the value of the per-cpu variable name to value val.
168  */
169 #define	__PCPU_SET(name, val) do {					\
170 	__pcpu_type(name) __val;					\
171 	struct __s {							\
172 		u_char	__b[MIN(sizeof(__val), 4)];			\
173 	} __s;								\
174 									\
175 	__val = (val);							\
176 	if (sizeof(__val) == 1 || sizeof(__val) == 2 ||			\
177 	    sizeof(__val) == 4) {					\
178 		__s = *(struct __s *)(void *)&__val;			\
179 		__asm __volatile("mov %1,%%fs:%0"			\
180 		    : "=m" (*(struct __s *)(__pcpu_offset(name)))	\
181 		    : "r" (__s));					\
182 	} else {							\
183 		*__PCPU_PTR(name) = __val;				\
184 	}								\
185 } while (0)
186 
187 #define	get_pcpu() __extension__ ({					\
188 	struct pcpu *__pc;						\
189 									\
190 	__asm __volatile("movl %%fs:%1,%0"				\
191 	    : "=r" (__pc)						\
192 	    : "m" (*(struct pcpu *)(__pcpu_offset(pc_prvspace))));	\
193 	__pc;								\
194 })
195 
196 #define	PCPU_GET(member)	__PCPU_GET(pc_ ## member)
197 #define	PCPU_ADD(member, val)	__PCPU_ADD(pc_ ## member, val)
198 #define	PCPU_PTR(member)	__PCPU_PTR(pc_ ## member)
199 #define	PCPU_SET(member, val)	__PCPU_SET(pc_ ## member, val)
200 
201 #define	IS_BSP()	(PCPU_GET(cpuid) == 0)
202 
203 #endif /* _KERNEL */
204 
205 #endif /* !_MACHINE_PCPU_H_ */
206