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