cpu.h revision 1.40
1/*	$NetBSD: cpu.h,v 1.40 2008/01/05 21:37:25 yamt Exp $	*/
2
3/*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * William Jolitz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *	@(#)cpu.h	5.4 (Berkeley) 5/9/91
35 */
36
37#ifndef _AMD64_CPU_H_
38#define _AMD64_CPU_H_
39
40#if defined(_KERNEL)
41#if defined(_KERNEL_OPT)
42#include "opt_multiprocessor.h"
43#include "opt_lockdebug.h"
44#include "opt_xen.h"
45#endif
46
47/*
48 * Definitions unique to x86-64 cpu support.
49 */
50#include <machine/frame.h>
51#include <machine/segments.h>
52#include <machine/tss.h>
53#include <machine/intrdefs.h>
54#include <x86/cacheinfo.h>
55
56#include <sys/device.h>
57#include <sys/simplelock.h>
58#include <sys/cpu_data.h>
59#include <sys/cc_microtime.h>
60
61struct pmap;
62
63struct cpu_info {
64	struct device *ci_dev;
65	struct cpu_info *ci_self;
66
67	/*
68	 * Will be accessed by other CPUs.
69	 */
70	struct cpu_info *ci_next;
71	struct lwp *ci_curlwp;
72	struct pmap_cpu *ci_pmap_cpu;
73	struct lwp *ci_fpcurlwp;
74	int ci_fpsaving;
75	u_int ci_cpuid;
76	int ci_cpumask;			/* (1 << CPU ID) */
77	u_int ci_apicid;
78	uint8_t ci_initapicid;		/* our intitial APIC ID */
79	uint8_t ci_packageid;
80	uint8_t ci_coreid;
81	uint8_t ci_smtid;
82	struct cpu_data ci_data;	/* MI per-cpu data */
83	struct cc_microtime_state ci_cc;/* cc_microtime state */
84
85	/*
86	 * Private members.
87	 */
88	struct evcnt ci_tlb_evcnt;	/* tlb shootdown counter */
89	struct pmap *ci_pmap;		/* current pmap */
90	int ci_need_tlbwait;		/* need to wait for TLB invalidations */
91	int ci_want_pmapload;		/* pmap_load() is needed */
92	volatile int ci_tlbstate;	/* one of TLBSTATE_ states. see below */
93#define	TLBSTATE_VALID	0	/* all user tlbs are valid */
94#define	TLBSTATE_LAZY	1	/* tlbs are valid but won't be kept uptodate */
95#define	TLBSTATE_STALE	2	/* we might have stale user tlbs */
96	u_int64_t ci_scratch;
97#ifdef XEN
98	struct iplsource *ci_isources[NIPL];
99#else
100	struct intrsource *ci_isources[MAX_INTR_SOURCES];
101#endif
102	volatile int	ci_mtx_count;	/* Negative count of spin mutexes */
103	volatile int	ci_mtx_oldspl;	/* Old SPL at this ci_idepth */
104
105	/* The following must be aligned for cmpxchg8b. */
106	struct {
107		uint32_t	ipending;
108		int		ilevel;
109	} ci_istate __aligned(8);
110#define ci_ipending	ci_istate.ipending
111#define	ci_ilevel	ci_istate.ilevel
112
113	int		ci_idepth;
114	u_int32_t	ci_imask[NIPL];
115	u_int32_t	ci_iunmask[NIPL];
116
117	u_int		ci_flags;
118	u_int32_t	ci_ipis;
119
120	int32_t		ci_cpuid_level;
121	uint32_t	ci_signature;
122	uint32_t	ci_feature_flags;
123	uint32_t	ci_feature2_flags;
124	uint32_t	ci_vendor[4];	 /* vendor string */
125	u_int64_t	ci_tsc_freq;
126	volatile uint32_t	ci_lapic_counter;
127
128	const struct cpu_functions *ci_func;
129	void (*cpu_setup)(struct cpu_info *);
130	void (*ci_info)(struct cpu_info *);
131
132	int		ci_want_resched;
133	struct trapframe *ci_ddb_regs;
134
135	struct x86_cache_info ci_cinfo[CAI_COUNT];
136
137	char		*ci_gdt;
138
139	struct x86_64_tss	ci_doubleflt_tss;
140	struct x86_64_tss	ci_ddbipi_tss;
141
142	char *ci_doubleflt_stack;
143	char *ci_ddbipi_stack;
144
145	struct evcnt ci_ipi_events[X86_NIPI];
146
147	/*
148	 * The following two are actually region_descriptors,
149	 * but that would pollute the namespace.
150	 */
151	uint64_t	ci_suspend_gdt;
152	uint16_t	ci_suspend_gdt_padding;
153	uint64_t	ci_suspend_idt;
154	uint16_t	ci_suspend_idt_padding;
155
156	uint16_t	ci_suspend_tr;
157	uint16_t	ci_suspend_ldt;
158	uint32_t	ci_suspend_fs_base_l;
159	uint32_t	ci_suspend_fs_base_h;
160	uint32_t	ci_suspend_gs_base_l;
161	uint32_t	ci_suspend_gs_base_h;
162	uint32_t	ci_suspend_gs_kernelbase_l;
163	uint32_t	ci_suspend_gs_kernelbase_h;
164	uint32_t	ci_suspend_msr_efer;
165	uint64_t	ci_suspend_rbx;
166	uint64_t	ci_suspend_rbp;
167	uint64_t	ci_suspend_rsp;
168	uint64_t	ci_suspend_r12;
169	uint64_t	ci_suspend_r13;
170	uint64_t	ci_suspend_r14;
171	uint64_t	ci_suspend_r15;
172	uint64_t	ci_suspend_rfl;
173	uint64_t	ci_suspend_cr0;
174	uint64_t	ci_suspend_cr2;
175	uint64_t	ci_suspend_cr3;
176	uint64_t	ci_suspend_cr4;
177	uint64_t	ci_suspend_cr8;
178};
179
180#define CPUF_BSP	0x0001		/* CPU is the original BSP */
181#define CPUF_AP		0x0002		/* CPU is an AP */
182#define CPUF_SP		0x0004		/* CPU is only processor */
183#define CPUF_PRIMARY	0x0008		/* CPU is active primary processor */
184
185#define CPUF_PRESENT	0x1000		/* CPU is present */
186#define CPUF_RUNNING	0x2000		/* CPU is running */
187#define CPUF_PAUSE	0x4000		/* CPU is paused in DDB */
188#define CPUF_GO		0x8000		/* CPU should start running */
189
190
191extern struct cpu_info cpu_info_primary;
192extern struct cpu_info *cpu_info_list;
193
194#define CPU_INFO_ITERATOR		int
195#define CPU_INFO_FOREACH(cii, ci)	cii = 0, ci = cpu_info_list; \
196					ci != NULL; ci = ci->ci_next
197
198#define X86_MAXPROCS		32	/* bitmask; can be bumped to 64 */
199
200#define CPU_STARTUP(_ci, _target)	((_ci)->ci_func->start(_ci, _target))
201#define CPU_STOP(_ci)			((_ci)->ci_func->stop(_ci))
202#define CPU_START_CLEANUP(_ci)		((_ci)->ci_func->cleanup(_ci))
203
204#if defined(__GNUC__) && defined(_KERNEL)
205static struct cpu_info *x86_curcpu(void);
206static lwp_t *x86_curlwp(void);
207
208__inline static struct cpu_info * __unused
209x86_curcpu(void)
210{
211	struct cpu_info *ci;
212
213	__asm volatile("movq %%gs:%1, %0" :
214	    "=r" (ci) :
215	    "m"
216	    (*(struct cpu_info * const *)offsetof(struct cpu_info, ci_self)));
217	return ci;
218}
219
220__inline static lwp_t * __unused
221x86_curlwp(void)
222{
223	lwp_t *l;
224
225	__asm volatile("movq %%gs:%1, %0" :
226	    "=r" (l) :
227	    "m"
228	    (*(struct cpu_info * const *)offsetof(struct cpu_info, ci_curlwp)));
229	return l;
230}
231#else	/* __GNUC__ && _KERNEL */
232/* For non-GCC and LKMs */
233struct cpu_info	*x86_curcpu(void);
234lwp_t	*x86_curlwp(void);
235#endif	/* __GNUC__ && _KERNEL */
236
237#define cpu_number()	(curcpu()->ci_cpuid)
238
239#define CPU_IS_PRIMARY(ci)	((ci)->ci_flags & CPUF_PRIMARY)
240
241extern struct cpu_info *cpu_info[X86_MAXPROCS];
242
243void cpu_boot_secondary_processors(void);
244void cpu_init_idle_lwps(void);
245
246#define aston(l)	((l)->l_md.md_astpending = 1)
247
248extern u_int32_t cpus_attached;
249
250#define curcpu()	x86_curcpu()
251#define curlwp		x86_curlwp()
252#define curpcb		(&curlwp->l_addr->u_pcb)
253
254/*
255 * Arguments to hardclock, softclock and statclock
256 * encapsulate the previous machine state in an opaque
257 * clockframe; for now, use generic intrframe.
258 */
259struct clockframe {
260	struct intrframe cf_if;
261};
262
263#define	CLKF_USERMODE(frame)	USERMODE((frame)->cf_if.if_tf.tf_cs, \
264				    (frame)->cf_if.if_tf.tf_rflags)
265#define CLKF_PC(frame)		((frame)->cf_if.if_tf.tf_rip)
266#define CLKF_INTR(frame)	(curcpu()->ci_idepth > 0)
267
268/*
269 * This is used during profiling to integrate system time.  It can safely
270 * assume that the process is resident.
271 */
272#define LWP_PC(l)		((l)->l_md.md_regs->tf_rip)
273
274/*
275 * Give a profiling tick to the current process when the user profiling
276 * buffer pages are invalid.  On the i386, request an ast to send us
277 * through trap(), marking the proc as needing a profiling tick.
278 */
279extern void cpu_need_proftick(struct lwp *);
280
281/*
282 * Notify an LWP that it has a signal pending, process as soon as possible.
283 */
284extern void cpu_signotify(struct lwp *);
285
286/*
287 * We need a machine-independent name for this.
288 */
289extern void (*delay_func)(unsigned int);
290
291#define DELAY(x)		(*delay_func)(x)
292#define delay(x)		(*delay_func)(x)
293
294
295/*
296 * pull in #defines for kinds of processors
297 */
298
299extern int biosbasemem;
300extern int biosextmem;
301extern int cpu;
302extern int cpu_feature;
303extern int cpu_feature2;
304extern int cpu_id;
305extern int cpuid_level;
306extern char cpu_vendorname[];
307
308/* identcpu.c */
309
310void	identifycpu(struct cpu_info *);
311void cpu_probe_features(struct cpu_info *);
312
313/* machdep.c */
314void	dumpconf(void);
315int	cpu_maxproc(void);
316void	cpu_reset(void);
317void	x86_64_proc0_tss_ldt_init(void);
318void	x86_64_init_pcb_tss_ldt(struct cpu_info *);
319void	cpu_proc_fork(struct proc *, struct proc *);
320
321struct region_descriptor;
322void	lgdt(struct region_descriptor *);
323#ifdef XEN
324void	lgdt_finish(void);
325#endif
326void	fillw(short, void *, size_t);
327
328struct pcb;
329void	savectx(struct pcb *);
330void	lwp_trampoline(void);
331void	child_trampoline(void);
332
333#ifdef XEN
334void	startrtclock(void);
335void	xen_delay(unsigned int);
336void	xen_initclocks(void);
337#else
338/* clock.c */
339void	initrtclock(u_long);
340void	startrtclock(void);
341void	i8254_delay(unsigned int);
342void	i8254_microtime(struct timeval *);
343void	i8254_initclocks(void);
344#endif
345
346void cpu_init_msrs(struct cpu_info *, bool);
347
348
349/* vm_machdep.c */
350int kvtop(void *);
351
352/* trap.c */
353void	child_return(void *);
354
355/* consinit.c */
356void kgdb_port_init(void);
357
358/* bus_machdep.c */
359void x86_bus_space_init(void);
360void x86_bus_space_mallocok(void);
361
362#endif /* _KERNEL */
363
364#include <machine/psl.h>
365
366/*
367 * CTL_MACHDEP definitions.
368 */
369#define	CPU_CONSDEV		1	/* dev_t: console terminal device */
370#define	CPU_BIOSBASEMEM		2	/* int: bios-reported base mem (K) */
371#define	CPU_BIOSEXTMEM		3	/* int: bios-reported ext. mem (K) */
372#define	CPU_NKPDE		4	/* int: number of kernel PDEs */
373#define	CPU_BOOTED_KERNEL	5	/* string: booted kernel name */
374#define CPU_DISKINFO		6	/* disk geometry information */
375#define CPU_FPU_PRESENT		7	/* FPU is present */
376#define	CPU_MAXID		8	/* number of valid machdep ids */
377
378#define	CTL_MACHDEP_NAMES { \
379	{ 0, 0 }, \
380	{ "console_device", CTLTYPE_STRUCT }, \
381	{ "biosbasemem", CTLTYPE_INT }, \
382	{ "biosextmem", CTLTYPE_INT }, \
383	{ "nkpde", CTLTYPE_INT }, \
384	{ "booted_kernel", CTLTYPE_STRING }, \
385	{ "diskinfo", CTLTYPE_STRUCT }, \
386	{ "fpu_present", CTLTYPE_INT }, \
387}
388
389
390/*
391 * Structure for CPU_DISKINFO sysctl call.
392 * XXX this should be somewhere else.
393 */
394#define MAX_BIOSDISKS	16
395
396struct disklist {
397	int dl_nbiosdisks;			   /* number of bios disks */
398	struct biosdisk_info {
399		int bi_dev;			   /* BIOS device # (0x80 ..) */
400		int bi_cyl;			   /* cylinders on disk */
401		int bi_head;			   /* heads per track */
402		int bi_sec;			   /* sectors per track */
403		u_int64_t bi_lbasecs;		   /* total sec. (iff ext13) */
404#define BIFLAG_INVALID		0x01
405#define BIFLAG_EXTINT13		0x02
406		int bi_flags;
407	} dl_biosdisks[MAX_BIOSDISKS];
408
409	int dl_nnativedisks;			   /* number of native disks */
410	struct nativedisk_info {
411		char ni_devname[16];		   /* native device name */
412		int ni_nmatches; 		   /* # of matches w/ BIOS */
413		int ni_biosmatches[MAX_BIOSDISKS]; /* indices in dl_biosdisks */
414	} dl_nativedisks[1];			   /* actually longer */
415};
416
417#endif /* !_AMD64_CPU_H_ */
418