xref: /freebsd/sys/i386/include/pcpu.h (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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 
29 #ifndef _MACHINE_PCPU_H_
30 #define	_MACHINE_PCPU_H_
31 
32 #include <machine/segments.h>
33 #include <machine/tss.h>
34 
35 #include <sys/_lock.h>
36 #include <sys/_mutex.h>
37 
38 struct monitorbuf {
39 	int idle_state;		/* Used by cpu_idle_mwait. */
40 	int stop_state;		/* Used by cpustop_handler. */
41 	char padding[128 - (2 * sizeof(int))];
42 };
43 _Static_assert(sizeof(struct monitorbuf) == 128, "2x cache line");
44 
45 /*
46  * The SMP parts are setup in pmap.c and machdep.c for the BSP, and
47  * pmap.c and mp_machdep.c sets up the data for the AP's to "see" when
48  * they awake.  The reason for doing it via a struct is so that an
49  * array of pointers to each CPU's data can be set up for things like
50  * "check curproc on all other processors"
51  */
52 
53 #define	PCPU_MD_FIELDS							\
54 	struct monitorbuf pc_monitorbuf __aligned(128);	/* cache line */\
55 	struct	pcpu *pc_prvspace;	/* Self-reference */		\
56 	struct	pmap *pc_curpmap;					\
57 	struct	segment_descriptor pc_common_tssd;			\
58 	struct	segment_descriptor *pc_tss_gdt;				\
59 	struct	segment_descriptor *pc_fsgs_gdt;			\
60 	struct	i386tss *pc_common_tssp;				\
61 	u_int	pc_kesp0;						\
62 	u_int	pc_trampstk;						\
63 	int	pc_currentldt;						\
64 	u_int   pc_acpi_id;		/* ACPI CPU id */		\
65 	u_int	pc_apic_id;						\
66 	int	pc_private_tss;		/* Flag indicating private tss*/\
67 	u_int	pc_cmci_mask;		/* MCx banks for CMCI */	\
68 	u_int	pc_vcpu_id;		/* Xen vCPU ID */		\
69 	struct	mtx pc_cmap_lock;					\
70 	void	*pc_cmap_pte1;						\
71 	void	*pc_cmap_pte2;						\
72 	caddr_t	pc_cmap_addr1;						\
73 	caddr_t	pc_cmap_addr2;						\
74 	vm_offset_t pc_qmap_addr;	/* KVA for temporary mappings */\
75 	vm_offset_t pc_copyout_maddr;					\
76 	vm_offset_t pc_copyout_saddr;					\
77 	struct	mtx pc_copyout_mlock;					\
78 	struct	sx pc_copyout_slock;					\
79 	char	*pc_copyout_buf;					\
80 	vm_offset_t pc_pmap_eh_va;					\
81 	caddr_t pc_pmap_eh_ptep;					\
82 	uint32_t pc_smp_tlb_done;	/* TLB op acknowledgement */	\
83 	uint32_t pc_ibpb_set;						\
84 	void	*pc_mds_buf;						\
85 	void	*pc_mds_buf64;						\
86 	uint32_t pc_pad[4];						\
87 	uint8_t	pc_mds_tmp[64];						\
88 	u_int	pc_ipi_bitmap;						\
89 	char	__pad[3518]
90 
91 #ifdef _KERNEL
92 
93 #define MONITOR_STOPSTATE_RUNNING	0
94 #define MONITOR_STOPSTATE_STOPPED	1
95 
96 /*
97  * Evaluates to the byte offset of the per-cpu variable name.
98  */
99 #define	__pcpu_offset(name)						\
100 	__offsetof(struct pcpu, name)
101 
102 /*
103  * Evaluates to the type of the per-cpu variable name.
104  */
105 #define	__pcpu_type(name)						\
106 	__typeof(((struct pcpu *)0)->name)
107 
108 /*
109  * Evaluates to the address of the per-cpu variable name.
110  */
111 #define	__PCPU_PTR(name) __extension__ ({				\
112 	__pcpu_type(name) *__p;						\
113 									\
114 	__asm __volatile("movl %%fs:%1,%0; addl %2,%0"			\
115 	    : "=r" (__p)						\
116 	    : "m" (*(struct pcpu *)(__pcpu_offset(pc_prvspace))),	\
117 	      "i" (__pcpu_offset(name)));				\
118 									\
119 	__p;								\
120 })
121 
122 /*
123  * Evaluates to the value of the per-cpu variable name.
124  */
125 #define	__PCPU_GET(name) __extension__ ({				\
126 	__pcpu_type(name) __res;					\
127 	struct __s {							\
128 		u_char	__b[MIN(sizeof(__res), 4)];			\
129 	} __s;								\
130 									\
131 	if (sizeof(__res) == 1 || sizeof(__res) == 2 ||			\
132 	    sizeof(__res) == 4) {					\
133 		__asm __volatile("mov %%fs:%1,%0"			\
134 		    : "=r" (__s)					\
135 		    : "m" (*(struct __s *)(__pcpu_offset(name))));	\
136 		*(struct __s *)(void *)&__res = __s;			\
137 	} else {							\
138 		__res = *__PCPU_PTR(name);				\
139 	}								\
140 	__res;								\
141 })
142 
143 /*
144  * Adds a value of the per-cpu counter name.  The implementation
145  * must be atomic with respect to interrupts.
146  */
147 #define	__PCPU_ADD(name, val) do {					\
148 	__pcpu_type(name) __val;					\
149 	struct __s {							\
150 		u_char	__b[MIN(sizeof(__val), 4)];			\
151 	} __s;								\
152 									\
153 	__val = (val);							\
154 	if (sizeof(__val) == 1 || sizeof(__val) == 2 ||			\
155 	    sizeof(__val) == 4) {					\
156 		__s = *(struct __s *)(void *)&__val;			\
157 		__asm __volatile("add %1,%%fs:%0"			\
158 		    : "=m" (*(struct __s *)(__pcpu_offset(name)))	\
159 		    : "r" (__s));					\
160 	} else								\
161 		*__PCPU_PTR(name) += __val;				\
162 } while (0)
163 
164 /*
165  * Sets the value of the per-cpu variable name to value val.
166  */
167 #define	__PCPU_SET(name, val) do {					\
168 	__pcpu_type(name) __val;					\
169 	struct __s {							\
170 		u_char	__b[MIN(sizeof(__val), 4)];			\
171 	} __s;								\
172 									\
173 	__val = (val);							\
174 	if (sizeof(__val) == 1 || sizeof(__val) == 2 ||			\
175 	    sizeof(__val) == 4) {					\
176 		__s = *(struct __s *)(void *)&__val;			\
177 		__asm __volatile("mov %1,%%fs:%0"			\
178 		    : "=m" (*(struct __s *)(__pcpu_offset(name)))	\
179 		    : "r" (__s));					\
180 	} else {							\
181 		*__PCPU_PTR(name) = __val;				\
182 	}								\
183 } while (0)
184 
185 #define	get_pcpu() __extension__ ({					\
186 	struct pcpu *__pc;						\
187 									\
188 	__asm __volatile("movl %%fs:%1,%0"				\
189 	    : "=r" (__pc)						\
190 	    : "m" (*(struct pcpu *)(__pcpu_offset(pc_prvspace))));	\
191 	__pc;								\
192 })
193 
194 #define	PCPU_GET(member)	__PCPU_GET(pc_ ## member)
195 #define	PCPU_ADD(member, val)	__PCPU_ADD(pc_ ## member, val)
196 #define	PCPU_PTR(member)	__PCPU_PTR(pc_ ## member)
197 #define	PCPU_SET(member, val)	__PCPU_SET(pc_ ## member, val)
198 
199 #define	IS_BSP()	(PCPU_GET(cpuid) == 0)
200 
201 #endif /* _KERNEL */
202 
203 #endif /* !_MACHINE_PCPU_H_ */
204