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