135069Speter/*-
2158502Speter * Copyright (c) Peter Wemm
335069Speter * All rights reserved.
435069Speter *
535069Speter * Redistribution and use in source and binary forms, with or without
635069Speter * modification, are permitted provided that the following conditions
735069Speter * are met:
835069Speter * 1. Redistributions of source code must retain the above copyright
935069Speter *    notice, this list of conditions and the following disclaimer.
1035069Speter * 2. Redistributions in binary form must reproduce the above copyright
1135069Speter *    notice, this list of conditions and the following disclaimer in the
1235069Speter *    documentation and/or other materials provided with the distribution.
1335069Speter *
1435069Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1535069Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1635069Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1735069Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1835069Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1935069Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2035069Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2135069Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2235069Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2335069Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2435069Speter * SUCH DAMAGE.
2535069Speter *
2650477Speter * $FreeBSD$
2735069Speter */
2835069Speter
2987702Sjhb#ifndef _MACHINE_PCPU_H_
30166540Sbde#define	_MACHINE_PCPU_H_
3165557Sjasone
32143063Sjoerg#ifndef _SYS_CDEFS_H_
33166540Sbde#error "sys/cdefs.h is a prerequisite for this file"
34143063Sjoerg#endif
35143063Sjoerg
3665557Sjasone#include <machine/segments.h>
3765557Sjasone#include <machine/tss.h>
3865557Sjasone
3935069Speter/*
4035069Speter * The SMP parts are setup in pmap.c and locore.s for the BSP, and
4135069Speter * mp_machdep.c sets up the data for the AP's to "see" when they awake.
4235069Speter * The reason for doing it via a struct is so that an array of pointers
4335069Speter * to each CPU's data can be set up for things like "check curproc on all
4435069Speter * other processors"
4535069Speter */
46181775Skmacy
47216956Srwatson#if defined(XEN)
48216956Srwatson
49184198Skmacy/* These are peridically updated in shared_info, and then copied here. */
50184198Skmacystruct shadow_time_info {
51184198Skmacy	uint64_t tsc_timestamp;     /* TSC at last update of time vals.  */
52184198Skmacy	uint64_t system_timestamp;  /* Time, in nanosecs, since boot.    */
53184198Skmacy	uint32_t tsc_to_nsec_mul;
54184198Skmacy	uint32_t tsc_to_usec_mul;
55184198Skmacy	int tsc_shift;
56184198Skmacy	uint32_t version;
57184198Skmacy};
58184198Skmacy
59208742Sjhb#define	PCPU_XEN_FIELDS							\
60208742Sjhb	;								\
61208742Sjhb	u_int	pc_cr3;		/* track cr3 for R1/R3*/		\
62208742Sjhb	vm_paddr_t *pc_pdir_shadow;					\
63208742Sjhb	uint64_t pc_processed_system_time;				\
64184198Skmacy	struct shadow_time_info pc_shadow_time;				\
65256073Sgibbs	char	__pad[185]
66216956Srwatson
67255040Sgibbs#else /* !XEN */
68216956Srwatson
69249265Sglebius#define PCPU_XEN_FIELDS							\
70249265Sglebius	;								\
71256073Sgibbs	char	__pad[233]
72216956Srwatson
73208742Sjhb#endif
74181775Skmacy
75181775Skmacy#define	PCPU_MD_FIELDS							\
76181775Skmacy	char	pc_monitorbuf[128] __aligned(128); /* cache line */	\
77181775Skmacy	struct	pcpu *pc_prvspace;	/* Self-reference */		\
78181775Skmacy	struct	pmap *pc_curpmap;					\
79181775Skmacy	struct	i386tss pc_common_tss;					\
80181775Skmacy	struct	segment_descriptor pc_common_tssd;			\
81181775Skmacy	struct	segment_descriptor *pc_tss_gdt;				\
82181775Skmacy	struct	segment_descriptor *pc_fsgs_gdt;			\
83181775Skmacy	int	pc_currentldt;						\
84181775Skmacy	u_int   pc_acpi_id;		/* ACPI CPU id */		\
85181775Skmacy	u_int	pc_apic_id;						\
86208507Sjhb	int	pc_private_tss;		/* Flag indicating private tss*/\
87256073Sgibbs	u_int	pc_cmci_mask;		/* MCx banks for CMCI */	\
88256073Sgibbs	u_int	pc_vcpu_id		/* Xen vCPU ID */		\
89208742Sjhb	PCPU_XEN_FIELDS
9035069Speter
91181875Sjhb#ifdef _KERNEL
92181875Sjhb
93166540Sbde#ifdef lint
94166540Sbde
95104291Sphkextern struct pcpu *pcpup;
96104291Sphk
97166540Sbde#define	PCPU_GET(member)	(pcpup->pc_ ## member)
98184198Skmacy#define	PCPU_ADD(member, val)	(pcpup->pc_ ## member += (val))
99170291Sattilio#define	PCPU_INC(member)	PCPU_ADD(member, 1)
100166540Sbde#define	PCPU_PTR(member)	(&pcpup->pc_ ## member)
101166540Sbde#define	PCPU_SET(member, val)	(pcpup->pc_ ## member = (val))
102166540Sbde
103166540Sbde#elif defined(__GNUCLIKE_ASM) && defined(__GNUCLIKE___TYPEOF)
104166540Sbde
10587702Sjhb/*
10687702Sjhb * Evaluates to the byte offset of the per-cpu variable name.
10787702Sjhb */
10887702Sjhb#define	__pcpu_offset(name)						\
10987702Sjhb	__offsetof(struct pcpu, name)
11087702Sjhb
11187702Sjhb/*
11287702Sjhb * Evaluates to the type of the per-cpu variable name.
11387702Sjhb */
11487702Sjhb#define	__pcpu_type(name)						\
11587702Sjhb	__typeof(((struct pcpu *)0)->name)
11687702Sjhb
11787702Sjhb/*
11887702Sjhb * Evaluates to the address of the per-cpu variable name.
11987702Sjhb */
120122833Sbde#define	__PCPU_PTR(name) __extension__ ({				\
12187702Sjhb	__pcpu_type(name) *__p;						\
12287702Sjhb									\
12387702Sjhb	__asm __volatile("movl %%fs:%1,%0; addl %2,%0"			\
12487702Sjhb	    : "=r" (__p)						\
12587702Sjhb	    : "m" (*(struct pcpu *)(__pcpu_offset(pc_prvspace))),	\
12687702Sjhb	      "i" (__pcpu_offset(name)));				\
12787702Sjhb									\
12887702Sjhb	__p;								\
12987702Sjhb})
13087702Sjhb
13187702Sjhb/*
13287702Sjhb * Evaluates to the value of the per-cpu variable name.
13387702Sjhb */
134122833Sbde#define	__PCPU_GET(name) __extension__ ({				\
135166540Sbde	__pcpu_type(name) __res;					\
136166540Sbde	struct __s {							\
137196816Sjulian		u_char	__b[MIN(sizeof(__res), 4)];			\
138166540Sbde	} __s;								\
13987702Sjhb									\
140166540Sbde	if (sizeof(__res) == 1 || sizeof(__res) == 2 ||			\
141166540Sbde	    sizeof(__res) == 4) {					\
142166536Sbde		__asm __volatile("mov %%fs:%1,%0"			\
143166536Sbde		    : "=r" (__s)					\
144166536Sbde		    : "m" (*(struct __s *)(__pcpu_offset(name))));	\
145166540Sbde		*(struct __s *)(void *)&__res = __s;			\
14687702Sjhb	} else {							\
147166540Sbde		__res = *__PCPU_PTR(name);				\
14887702Sjhb	}								\
149166540Sbde	__res;								\
15087702Sjhb})
15187702Sjhb
15287702Sjhb/*
153170291Sattilio * Adds a value of the per-cpu counter name.  The implementation
154170291Sattilio * must be atomic with respect to interrupts.
155170291Sattilio */
156170291Sattilio#define	__PCPU_ADD(name, val) do {					\
157170291Sattilio	__pcpu_type(name) __val;					\
158170291Sattilio	struct __s {							\
159196816Sjulian		u_char	__b[MIN(sizeof(__val), 4)];			\
160170291Sattilio	} __s;								\
161170291Sattilio									\
162170291Sattilio	__val = (val);							\
163170291Sattilio	if (sizeof(__val) == 1 || sizeof(__val) == 2 ||			\
164170291Sattilio	    sizeof(__val) == 4) {					\
165170291Sattilio		__s = *(struct __s *)(void *)&__val;			\
166170291Sattilio		__asm __volatile("add %1,%%fs:%0"			\
167170291Sattilio		    : "=m" (*(struct __s *)(__pcpu_offset(name)))	\
168170291Sattilio		    : "r" (__s));					\
169170291Sattilio	} else								\
170170291Sattilio		*__PCPU_PTR(name) += __val;				\
171170291Sattilio} while (0)
172170291Sattilio
173170291Sattilio/*
174167429Salc * Increments the value of the per-cpu counter name.  The implementation
175167429Salc * must be atomic with respect to interrupts.
176167429Salc */
177170291Sattilio#define	__PCPU_INC(name) do {						\
178167429Salc	CTASSERT(sizeof(__pcpu_type(name)) == 1 ||			\
179167429Salc	    sizeof(__pcpu_type(name)) == 2 ||				\
180167429Salc	    sizeof(__pcpu_type(name)) == 4);				\
181167429Salc	if (sizeof(__pcpu_type(name)) == 1) {				\
182167429Salc		__asm __volatile("incb %%fs:%0"				\
183167429Salc		    : "=m" (*(__pcpu_type(name) *)(__pcpu_offset(name)))\
184167429Salc		    : "m" (*(__pcpu_type(name) *)(__pcpu_offset(name))));\
185167429Salc	} else if (sizeof(__pcpu_type(name)) == 2) {			\
186167429Salc		__asm __volatile("incw %%fs:%0"				\
187167429Salc		    : "=m" (*(__pcpu_type(name) *)(__pcpu_offset(name)))\
188167429Salc		    : "m" (*(__pcpu_type(name) *)(__pcpu_offset(name))));\
189167429Salc	} else if (sizeof(__pcpu_type(name)) == 4) {			\
190167429Salc		__asm __volatile("incl %%fs:%0"				\
191167429Salc		    : "=m" (*(__pcpu_type(name) *)(__pcpu_offset(name)))\
192167429Salc		    : "m" (*(__pcpu_type(name) *)(__pcpu_offset(name))));\
193167429Salc	}								\
194167429Salc} while (0)
195167429Salc
196167429Salc/*
19787702Sjhb * Sets the value of the per-cpu variable name to value val.
19887702Sjhb */
199196811Sjulian#define	__PCPU_SET(name, val) do {					\
200166540Sbde	__pcpu_type(name) __val;					\
201166540Sbde	struct __s {							\
202196811Sjulian		u_char	__b[MIN(sizeof(__val), 4)];			\
203166540Sbde	} __s;								\
20487702Sjhb									\
205166540Sbde	__val = (val);							\
206166536Sbde	if (sizeof(__val) == 1 || sizeof(__val) == 2 ||			\
207166536Sbde	    sizeof(__val) == 4) {					\
208166536Sbde		__s = *(struct __s *)(void *)&__val;			\
209166536Sbde		__asm __volatile("mov %1,%%fs:%0"			\
210166536Sbde		    : "=m" (*(struct __s *)(__pcpu_offset(name)))	\
211166536Sbde		    : "r" (__s));					\
21287702Sjhb	} else {							\
21387702Sjhb		*__PCPU_PTR(name) = __val;				\
21487702Sjhb	}								\
215196811Sjulian} while (0)
21687702Sjhb
21787702Sjhb#define	PCPU_GET(member)	__PCPU_GET(pc_ ## member)
218170291Sattilio#define	PCPU_ADD(member, val)	__PCPU_ADD(pc_ ## member, val)
219170291Sattilio#define	PCPU_INC(member)	__PCPU_INC(pc_ ## member)
22087702Sjhb#define	PCPU_PTR(member)	__PCPU_PTR(pc_ ## member)
22187702Sjhb#define	PCPU_SET(member, val)	__PCPU_SET(pc_ ## member, val)
22287702Sjhb
223238792Skib#define	OFFSETOF_CURTHREAD	0
224238792Skib#ifdef __clang__
225238792Skib#pragma clang diagnostic push
226238792Skib#pragma clang diagnostic ignored "-Wnull-dereference"
227238792Skib#endif
228210623Sjhbstatic __inline __pure2 struct thread *
229122931Speter__curthread(void)
230122931Speter{
231122931Speter	struct thread *td;
232122931Speter
233238792Skib	__asm("movl %%fs:%1,%0" : "=r" (td)
234238792Skib	    : "m" (*(char *)OFFSETOF_CURTHREAD));
235122931Speter	return (td);
236122931Speter}
237238792Skib#ifdef __clang__
238238792Skib#pragma clang diagnostic pop
239238792Skib#endif
240166540Sbde#define	curthread		(__curthread())
241122931Speter
242238792Skib#define	OFFSETOF_CURPCB		16
243238792Skibstatic __inline __pure2 struct pcb *
244238792Skib__curpcb(void)
245238792Skib{
246238792Skib	struct pcb *pcb;
247238792Skib
248238792Skib	__asm("movl %%fs:%1,%0" : "=r" (pcb) : "m" (*(char *)OFFSETOF_CURPCB));
249238792Skib	return (pcb);
250238792Skib}
251238792Skib#define	curpcb		(__curpcb())
252238792Skib
253166540Sbde#else /* !lint || defined(__GNUCLIKE_ASM) && defined(__GNUCLIKE___TYPEOF) */
254100079Smarkm
255166540Sbde#error "this file needs to be ported to your compiler"
25681763Sobrien
257166540Sbde#endif /* lint, etc. */
258166540Sbde
259166540Sbde#endif /* _KERNEL */
260166540Sbde
261166540Sbde#endif /* !_MACHINE_PCPU_H_ */
262