cpu.h revision 1.26
1/*	$NetBSD: cpu.h,v 1.26 2007/09/25 17:08:09 ad 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#endif
45
46/*
47 * Definitions unique to x86-64 cpu support.
48 */
49#include <machine/frame.h>
50#include <machine/segments.h>
51#include <machine/tss.h>
52#include <machine/intrdefs.h>
53#include <x86/cacheinfo.h>
54
55#include <sys/device.h>
56#include <sys/simplelock.h>
57#include <sys/cpu_data.h>
58#include <sys/cc_microtime.h>
59
60struct cpu_info {
61	struct device *ci_dev;
62	struct cpu_info *ci_self;
63
64	/*
65	 * Will be accessed by other CPUs.
66	 */
67	struct cpu_info *ci_next;
68	struct lwp *ci_curlwp;
69	struct pmap_cpu *ci_pmap_cpu;
70	struct lwp *ci_fpcurlwp;
71	int ci_fpsaving;
72	u_int ci_cpuid;
73	int ci_cpumask;			/* (1 << CPU ID) */
74	u_int ci_apicid;
75	struct cpu_data ci_data;	/* MI per-cpu data */
76	struct cc_microtime_state ci_cc;/* cc_microtime state */
77
78	/*
79	 * Private members.
80	 */
81	struct evcnt ci_tlb_evcnt;	/* tlb shootdown counter */
82	int ci_need_tlbwait;		/* need to wait for TLB invalidations */
83	u_int64_t ci_scratch;
84	struct intrsource *ci_isources[MAX_INTR_SOURCES];
85	volatile int	ci_mtx_count;	/* Negative count of spin mutexes */
86	volatile int	ci_mtx_oldspl;	/* Old SPL at this ci_idepth */
87
88	/* The following must be aligned for cmpxchg8b. */
89	struct {
90		uint32_t	ipending;
91		int		ilevel;
92	} ci_istate __aligned(8);
93#define ci_ipending	ci_istate.ipending
94#define	ci_ilevel	ci_istate.ilevel
95
96	int		ci_idepth;
97	u_int32_t	ci_imask[NIPL];
98	u_int32_t	ci_iunmask[NIPL];
99
100	paddr_t 	ci_idle_pcb_paddr;
101	u_int		ci_flags;
102	u_int32_t	ci_ipis;
103
104	int32_t		ci_cpuid_level;
105	uint32_t	ci_signature;
106	uint32_t	ci_feature_flags;
107	uint32_t	ci_feature2_flags;
108	uint32_t	ci_vendor[4];	 /* vendor string */
109	u_int64_t	ci_tsc_freq;
110
111	const struct cpu_functions *ci_func;
112	void (*cpu_setup)(struct cpu_info *);
113	void (*ci_info)(struct cpu_info *);
114
115	int		ci_want_resched;
116	struct trapframe *ci_ddb_regs;
117
118	struct x86_cache_info ci_cinfo[CAI_COUNT];
119
120	char		*ci_gdt;
121
122	struct x86_64_tss	ci_doubleflt_tss;
123	struct x86_64_tss	ci_ddbipi_tss;
124
125	char *ci_doubleflt_stack;
126	char *ci_ddbipi_stack;
127
128	struct evcnt ci_ipi_events[X86_NIPI];
129};
130
131#define CPUF_BSP	0x0001		/* CPU is the original BSP */
132#define CPUF_AP		0x0002		/* CPU is an AP */
133#define CPUF_SP		0x0004		/* CPU is only processor */
134#define CPUF_PRIMARY	0x0008		/* CPU is active primary processor */
135
136#define CPUF_PRESENT	0x1000		/* CPU is present */
137#define CPUF_RUNNING	0x2000		/* CPU is running */
138#define CPUF_PAUSE	0x4000		/* CPU is paused in DDB */
139#define CPUF_GO		0x8000		/* CPU should start running */
140
141
142extern struct cpu_info cpu_info_primary;
143extern struct cpu_info *cpu_info_list;
144
145#define CPU_INFO_ITERATOR		int
146#define CPU_INFO_FOREACH(cii, ci)	cii = 0, ci = cpu_info_list; \
147					ci != NULL; ci = ci->ci_next
148
149#if defined(MULTIPROCESSOR)
150
151#define X86_MAXPROCS		32	/* bitmask; can be bumped to 64 */
152
153#define CPU_STARTUP(_ci)	((_ci)->ci_func->start(_ci))
154#define CPU_STOP(_ci)		((_ci)->ci_func->stop(_ci))
155#define CPU_START_CLEANUP(_ci)	((_ci)->ci_func->cleanup(_ci))
156
157#define curcpu()	({struct cpu_info *__ci;                  \
158			__asm volatile("movq %%gs:8,%0" : "=r" (__ci)); \
159			__ci;})
160#define cpu_number()	(curcpu()->ci_cpuid)
161
162#define CPU_IS_PRIMARY(ci)	((ci)->ci_flags & CPUF_PRIMARY)
163
164extern struct cpu_info *cpu_info[X86_MAXPROCS];
165
166void cpu_boot_secondary_processors(void);
167void cpu_init_idle_lwps(void);
168
169
170#else /* !MULTIPROCESSOR */
171
172#define X86_MAXPROCS		1
173
174extern struct cpu_info cpu_info_primary;
175
176#define curcpu()		(&cpu_info_primary)
177
178/*
179 * definitions of cpu-dependent requirements
180 * referenced in generic code
181 */
182#define	cpu_number()		0
183#define CPU_IS_PRIMARY(ci)	1
184
185#endif
186
187#define aston(l)	((l)->l_md.md_astpending = 1)
188
189extern u_int32_t cpus_attached;
190
191#define curlwp		curcpu()->ci_curlwp
192#define curpcb		(&curlwp->l_addr->u_pcb)
193
194/*
195 * Arguments to hardclock, softclock and statclock
196 * encapsulate the previous machine state in an opaque
197 * clockframe; for now, use generic intrframe.
198 */
199struct clockframe {
200	struct intrframe cf_if;
201};
202
203#define	CLKF_USERMODE(frame)	USERMODE((frame)->cf_if.if_cs, (frame)->cf_if.if_rflags)
204#define CLKF_PC(frame)		((frame)->cf_if.if_rip)
205#define CLKF_INTR(frame)	(curcpu()->ci_idepth > 0)
206
207/*
208 * This is used during profiling to integrate system time.  It can safely
209 * assume that the process is resident.
210 */
211#define LWP_PC(l)		((l)->l_md.md_regs->tf_rip)
212
213/*
214 * Give a profiling tick to the current process when the user profiling
215 * buffer pages are invalid.  On the i386, request an ast to send us
216 * through trap(), marking the proc as needing a profiling tick.
217 */
218extern void cpu_need_proftick(struct lwp *);
219
220/*
221 * Notify an LWP that it has a signal pending, process as soon as possible.
222 */
223extern void cpu_signotify(struct lwp *);
224
225/*
226 * We need a machine-independent name for this.
227 */
228extern void (*delay_func)(int);
229
230#define DELAY(x)		(*delay_func)(x)
231#define delay(x)		(*delay_func)(x)
232
233
234/*
235 * pull in #defines for kinds of processors
236 */
237
238extern int biosbasemem;
239extern int biosextmem;
240extern int cpu;
241extern int cpu_feature;
242extern int cpu_feature2;
243extern int cpu_id;
244extern char cpu_vendor[];
245extern int cpuid_level;
246
247/* identcpu.c */
248
249void	identifycpu(struct cpu_info *);
250void cpu_probe_features(struct cpu_info *);
251
252/* machdep.c */
253void	dumpconf(void);
254int	cpu_maxproc(void);
255void	cpu_reset(void);
256void	x86_64_proc0_tss_ldt_init(void);
257void	x86_64_init_pcb_tss_ldt(struct cpu_info *);
258void	cpu_proc_fork(struct proc *, struct proc *);
259
260struct region_descriptor;
261void	lgdt(struct region_descriptor *);
262void	fillw(short, void *, size_t);
263
264struct pcb;
265void	savectx(struct pcb *);
266void	lwp_trampoline(void);
267void	child_trampoline(void);
268
269/* clock.c */
270void	initrtclock(u_long);
271void	startrtclock(void);
272void	i8254_delay(int);
273void	i8254_microtime(struct timeval *);
274void	i8254_initclocks(void);
275
276void cpu_init_msrs(struct cpu_info *);
277
278
279/* vm_machdep.c */
280int kvtop(void *);
281
282/* trap.c */
283void	child_return(void *);
284
285/* consinit.c */
286void kgdb_port_init(void);
287
288/* bus_machdep.c */
289void x86_bus_space_init(void);
290void x86_bus_space_mallocok(void);
291
292#endif /* _KERNEL */
293
294#include <machine/psl.h>
295
296/*
297 * CTL_MACHDEP definitions.
298 */
299#define	CPU_CONSDEV		1	/* dev_t: console terminal device */
300#define	CPU_BIOSBASEMEM		2	/* int: bios-reported base mem (K) */
301#define	CPU_BIOSEXTMEM		3	/* int: bios-reported ext. mem (K) */
302#define	CPU_NKPDE		4	/* int: number of kernel PDEs */
303#define	CPU_BOOTED_KERNEL	5	/* string: booted kernel name */
304#define CPU_DISKINFO		6	/* disk geometry information */
305#define CPU_FPU_PRESENT		7	/* FPU is present */
306#define	CPU_MAXID		8	/* number of valid machdep ids */
307
308#define	CTL_MACHDEP_NAMES { \
309	{ 0, 0 }, \
310	{ "console_device", CTLTYPE_STRUCT }, \
311	{ "biosbasemem", CTLTYPE_INT }, \
312	{ "biosextmem", CTLTYPE_INT }, \
313	{ "nkpde", CTLTYPE_INT }, \
314	{ "booted_kernel", CTLTYPE_STRING }, \
315	{ "diskinfo", CTLTYPE_STRUCT }, \
316	{ "fpu_present", CTLTYPE_INT }, \
317}
318
319
320/*
321 * Structure for CPU_DISKINFO sysctl call.
322 * XXX this should be somewhere else.
323 */
324#define MAX_BIOSDISKS	16
325
326struct disklist {
327	int dl_nbiosdisks;			   /* number of bios disks */
328	struct biosdisk_info {
329		int bi_dev;			   /* BIOS device # (0x80 ..) */
330		int bi_cyl;			   /* cylinders on disk */
331		int bi_head;			   /* heads per track */
332		int bi_sec;			   /* sectors per track */
333		u_int64_t bi_lbasecs;		   /* total sec. (iff ext13) */
334#define BIFLAG_INVALID		0x01
335#define BIFLAG_EXTINT13		0x02
336		int bi_flags;
337	} dl_biosdisks[MAX_BIOSDISKS];
338
339	int dl_nnativedisks;			   /* number of native disks */
340	struct nativedisk_info {
341		char ni_devname[16];		   /* native device name */
342		int ni_nmatches; 		   /* # of matches w/ BIOS */
343		int ni_biosmatches[MAX_BIOSDISKS]; /* indices in dl_biosdisks */
344	} dl_nativedisks[1];			   /* actually longer */
345};
346
347#endif /* !_AMD64_CPU_H_ */
348