xref: /freebsd/sys/i386/include/pcpu.h (revision ab92c99aa56fdec4a06f9af5f30f87ef08fbeb0e)
1937288c5SPeter Wemm /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
383ef78beSPedro F. Giffuni  *
4374757c7SPeter Wemm  * Copyright (c) Peter Wemm
5937288c5SPeter Wemm  * All rights reserved.
6937288c5SPeter Wemm  *
7937288c5SPeter Wemm  * Redistribution and use in source and binary forms, with or without
8937288c5SPeter Wemm  * modification, are permitted provided that the following conditions
9937288c5SPeter Wemm  * are met:
10937288c5SPeter Wemm  * 1. Redistributions of source code must retain the above copyright
11937288c5SPeter Wemm  *    notice, this list of conditions and the following disclaimer.
12937288c5SPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
13937288c5SPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
14937288c5SPeter Wemm  *    documentation and/or other materials provided with the distribution.
15937288c5SPeter Wemm  *
16937288c5SPeter Wemm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17937288c5SPeter Wemm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18937288c5SPeter Wemm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19937288c5SPeter Wemm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20937288c5SPeter Wemm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21937288c5SPeter Wemm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22937288c5SPeter Wemm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23937288c5SPeter Wemm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24937288c5SPeter Wemm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25937288c5SPeter Wemm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26937288c5SPeter Wemm  * SUCH DAMAGE.
27937288c5SPeter Wemm  */
28937288c5SPeter Wemm 
290bbc8826SJohn Baldwin #ifndef _MACHINE_PCPU_H_
300bbc8826SJohn Baldwin #define	_MACHINE_PCPU_H_
310384fff8SJason Evans 
320384fff8SJason Evans #include <machine/segments.h>
330384fff8SJason Evans #include <machine/tss.h>
340384fff8SJason Evans 
3543aabbefSJason A. Harmening #include <sys/_lock.h>
3643aabbefSJason A. Harmening #include <sys/_mutex.h>
3743aabbefSJason A. Harmening 
3883dc49beSConrad Meyer struct monitorbuf {
3983dc49beSConrad Meyer 	int idle_state;		/* Used by cpu_idle_mwait. */
40665919aaSConrad Meyer 	int stop_state;		/* Used by cpustop_handler. */
41665919aaSConrad Meyer 	char padding[128 - (2 * sizeof(int))];
4283dc49beSConrad Meyer };
4383dc49beSConrad Meyer _Static_assert(sizeof(struct monitorbuf) == 128, "2x cache line");
4483dc49beSConrad Meyer 
45937288c5SPeter Wemm /*
46d86c1f0dSKonstantin Belousov  * The SMP parts are setup in pmap.c and machdep.c for the BSP, and
47d86c1f0dSKonstantin Belousov  * pmap.c and mp_machdep.c sets up the data for the AP's to "see" when
48d86c1f0dSKonstantin Belousov  * they awake.  The reason for doing it via a struct is so that an
49d86c1f0dSKonstantin Belousov  * array of pointers to each CPU's data can be set up for things like
50d86c1f0dSKonstantin Belousov  * "check curproc on all other processors"
51937288c5SPeter Wemm  */
5293ee134aSKip Macy 
530bbc8826SJohn Baldwin #define	PCPU_MD_FIELDS							\
5483dc49beSConrad Meyer 	struct monitorbuf pc_monitorbuf __aligned(128);	/* cache line */\
550bbc8826SJohn Baldwin 	struct	pcpu *pc_prvspace;	/* Self-reference */		\
56b50953ccSPeter Wemm 	struct	pmap *pc_curpmap;					\
570bbc8826SJohn Baldwin 	struct	segment_descriptor pc_common_tssd;			\
580bbc8826SJohn Baldwin 	struct	segment_descriptor *pc_tss_gdt;				\
59e0ab2c6dSPeter Wemm 	struct	segment_descriptor *pc_fsgs_gdt;			\
60d86c1f0dSKonstantin Belousov 	struct	i386tss *pc_common_tssp;				\
61d86c1f0dSKonstantin Belousov 	u_int	pc_kesp0;						\
62d86c1f0dSKonstantin Belousov 	u_int	pc_trampstk;						\
63d74ac681SMatthew Dillon 	int	pc_currentldt;						\
641300fd67SBruce Evans 	u_int   pc_acpi_id;		/* ACPI CPU id */		\
651bfa9108SDavid Xu 	u_int	pc_apic_id;						\
6658ccad7dSJohn Baldwin 	int	pc_private_tss;		/* Flag indicating private tss*/\
675fdd34eeSJustin T. Gibbs 	u_int	pc_cmci_mask;		/* MCx banks for CMCI */	\
68ed95805eSJohn Baldwin 	u_int	pc_vcpu_id;		/* Xen vCPU ID */		\
6943aabbefSJason A. Harmening 	struct	mtx pc_cmap_lock;					\
7043aabbefSJason A. Harmening 	void	*pc_cmap_pte1;						\
7143aabbefSJason A. Harmening 	void	*pc_cmap_pte2;						\
7243aabbefSJason A. Harmening 	caddr_t	pc_cmap_addr1;						\
7343aabbefSJason A. Harmening 	caddr_t	pc_cmap_addr2;						\
74713841afSJason A. Harmening 	vm_offset_t pc_qmap_addr;	/* KVA for temporary mappings */\
75d86c1f0dSKonstantin Belousov 	vm_offset_t pc_copyout_maddr;					\
76d86c1f0dSKonstantin Belousov 	vm_offset_t pc_copyout_saddr;					\
77d86c1f0dSKonstantin Belousov 	struct	mtx pc_copyout_mlock;					\
78d86c1f0dSKonstantin Belousov 	struct	sx pc_copyout_slock;					\
79d86c1f0dSKonstantin Belousov 	char	*pc_copyout_buf;					\
80ded29bd9SKonstantin Belousov 	vm_offset_t pc_pmap_eh_va;					\
81ded29bd9SKonstantin Belousov 	caddr_t pc_pmap_eh_ptep;					\
8283c001d3SKonstantin Belousov 	uint32_t pc_smp_tlb_done;	/* TLB op acknowledgement */	\
83137fd41bSMatt Macy 	uint32_t pc_ibpb_set;						\
847355a02bSKonstantin Belousov 	void	*pc_mds_buf;						\
857355a02bSKonstantin Belousov 	void	*pc_mds_buf64;						\
867355a02bSKonstantin Belousov 	uint32_t pc_pad[4];						\
877355a02bSKonstantin Belousov 	uint8_t	pc_mds_tmp[64];						\
88a8c2fcb2SMateusz Guzik 	u_int	pc_ipi_bitmap;						\
897355a02bSKonstantin Belousov 	char	__pad[3518]
9070d12a18SJohn Baldwin 
9170d12a18SJohn Baldwin #ifdef _KERNEL
9270d12a18SJohn Baldwin 
93665919aaSConrad Meyer #define MONITOR_STOPSTATE_RUNNING	0
94665919aaSConrad Meyer #define MONITOR_STOPSTATE_STOPPED	1
95665919aaSConrad Meyer 
960bbc8826SJohn Baldwin /*
970bbc8826SJohn Baldwin  * Evaluates to the byte offset of the per-cpu variable name.
980bbc8826SJohn Baldwin  */
990bbc8826SJohn Baldwin #define	__pcpu_offset(name)						\
1000bbc8826SJohn Baldwin 	__offsetof(struct pcpu, name)
1010bbc8826SJohn Baldwin 
1020bbc8826SJohn Baldwin /*
1030bbc8826SJohn Baldwin  * Evaluates to the type of the per-cpu variable name.
1040bbc8826SJohn Baldwin  */
1050bbc8826SJohn Baldwin #define	__pcpu_type(name)						\
1060bbc8826SJohn Baldwin 	__typeof(((struct pcpu *)0)->name)
1070bbc8826SJohn Baldwin 
1080bbc8826SJohn Baldwin /*
1090bbc8826SJohn Baldwin  * Evaluates to the address of the per-cpu variable name.
1100bbc8826SJohn Baldwin  */
111*ab92c99aSRyan Libby #define	__PCPU_PTR(name)						\
112*ab92c99aSRyan Libby 	(&get_pcpu()->name)
1130bbc8826SJohn Baldwin 
1140bbc8826SJohn Baldwin /*
1150bbc8826SJohn Baldwin  * Evaluates to the value of the per-cpu variable name.
1160bbc8826SJohn Baldwin  */
1176a0d1abcSBruce Evans #define	__PCPU_GET(name) __extension__ ({				\
1181300fd67SBruce Evans 	__pcpu_type(name) __res;					\
1193764a823SBruce Evans 	struct __s {							\
12002c41ee9SJulian Elischer 		u_char	__b[MIN(sizeof(__res), 4)];			\
121*ab92c99aSRyan Libby 	};								\
1221300fd67SBruce Evans 									\
1231300fd67SBruce Evans 	if (sizeof(__res) == 1 || sizeof(__res) == 2 ||			\
1241300fd67SBruce Evans 	    sizeof(__res) == 4) {					\
125*ab92c99aSRyan Libby 		__asm __volatile("mov %%fs:%c1,%0"			\
126*ab92c99aSRyan Libby 		    : "=r" (*(struct __s *)(void *)&__res)		\
127*ab92c99aSRyan Libby 		    : "i" (__pcpu_offset(name)));			\
1280bbc8826SJohn Baldwin 	} else {							\
1291300fd67SBruce Evans 		__res = *__PCPU_PTR(name);				\
1300bbc8826SJohn Baldwin 	}								\
1311300fd67SBruce Evans 	__res;								\
1320bbc8826SJohn Baldwin })
1330bbc8826SJohn Baldwin 
1340bbc8826SJohn Baldwin /*
13567596082SAttilio Rao  * Adds a value of the per-cpu counter name.  The implementation
13667596082SAttilio Rao  * must be atomic with respect to interrupts.
13767596082SAttilio Rao  */
13867596082SAttilio Rao #define	__PCPU_ADD(name, val) do {					\
13967596082SAttilio Rao 	__pcpu_type(name) __val;					\
14067596082SAttilio Rao 	struct __s {							\
14102c41ee9SJulian Elischer 		u_char	__b[MIN(sizeof(__val), 4)];			\
142*ab92c99aSRyan Libby 	};								\
14367596082SAttilio Rao 									\
14467596082SAttilio Rao 	__val = (val);							\
14567596082SAttilio Rao 	if (sizeof(__val) == 1 || sizeof(__val) == 2 ||			\
14667596082SAttilio Rao 	    sizeof(__val) == 4) {					\
147*ab92c99aSRyan Libby 		__asm __volatile("add %1,%%fs:%c0"			\
148*ab92c99aSRyan Libby 		    :							\
149*ab92c99aSRyan Libby 		    : "i" (__pcpu_offset(name)),			\
150*ab92c99aSRyan Libby 		      "r" (*(struct __s *)(void *)&__val)		\
151*ab92c99aSRyan Libby 		    : "cc", "memory");					\
15267596082SAttilio Rao 	} else								\
15367596082SAttilio Rao 		*__PCPU_PTR(name) += __val;				\
15467596082SAttilio Rao } while (0)
15567596082SAttilio Rao 
15667596082SAttilio Rao /*
1570bbc8826SJohn Baldwin  * Sets the value of the per-cpu variable name to value val.
1580bbc8826SJohn Baldwin  */
15902c41ee9SJulian Elischer #define	__PCPU_SET(name, val) do {					\
1601300fd67SBruce Evans 	__pcpu_type(name) __val;					\
1613764a823SBruce Evans 	struct __s {							\
16202c41ee9SJulian Elischer 		u_char	__b[MIN(sizeof(__val), 4)];			\
163*ab92c99aSRyan Libby 	};								\
1641300fd67SBruce Evans 									\
1651300fd67SBruce Evans 	__val = (val);							\
1661300fd67SBruce Evans 	if (sizeof(__val) == 1 || sizeof(__val) == 2 ||			\
1671300fd67SBruce Evans 	    sizeof(__val) == 4) {					\
168*ab92c99aSRyan Libby 		__asm __volatile("mov %1,%%fs:%c0"			\
169*ab92c99aSRyan Libby 		    :							\
170*ab92c99aSRyan Libby 		    : "i" (__pcpu_offset(name)),			\
171*ab92c99aSRyan Libby 		      "r" (*(struct __s *)(void *)&__val)		\
172*ab92c99aSRyan Libby 		    : "memory");					\
1730bbc8826SJohn Baldwin 	} else {							\
1740bbc8826SJohn Baldwin 		*__PCPU_PTR(name) = __val;				\
1750bbc8826SJohn Baldwin 	}								\
17602c41ee9SJulian Elischer } while (0)
1770bbc8826SJohn Baldwin 
178d9864508SJason A. Harmening #define	get_pcpu() __extension__ ({					\
179d9864508SJason A. Harmening 	struct pcpu *__pc;						\
180d9864508SJason A. Harmening 									\
181*ab92c99aSRyan Libby 	__asm __volatile("movl %%fs:%c1,%0"				\
182d9864508SJason A. Harmening 	    : "=r" (__pc)						\
183*ab92c99aSRyan Libby 	    : "i" (__pcpu_offset(pc_prvspace)));			\
184d9864508SJason A. Harmening 	__pc;								\
185d9864508SJason A. Harmening })
186d9864508SJason A. Harmening 
1870bbc8826SJohn Baldwin #define	PCPU_GET(member)	__PCPU_GET(pc_ ## member)
18867596082SAttilio Rao #define	PCPU_ADD(member, val)	__PCPU_ADD(pc_ ## member, val)
1890bbc8826SJohn Baldwin #define	PCPU_PTR(member)	__PCPU_PTR(pc_ ## member)
1900bbc8826SJohn Baldwin #define	PCPU_SET(member, val)	__PCPU_SET(pc_ ## member, val)
191937288c5SPeter Wemm 
192a02c9cc5SLi-Wen Hsu #define	IS_BSP()	(PCPU_GET(cpuid) == 0)
193a02c9cc5SLi-Wen Hsu 
194589278dbSDavid E. O'Brien #endif /* _KERNEL */
195589278dbSDavid E. O'Brien 
1960bbc8826SJohn Baldwin #endif /* !_MACHINE_PCPU_H_ */
197