xref: /openbsd/sys/arch/amd64/include/cpu.h (revision 404b540a)
1 /*	$OpenBSD: cpu.h,v 1.49 2009/07/22 20:33:13 deraadt Exp $	*/
2 /*	$NetBSD: cpu.h,v 1.1 2003/04/26 18:39:39 fvdl Exp $	*/
3 
4 /*-
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * William Jolitz.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)cpu.h	5.4 (Berkeley) 5/9/91
36  */
37 
38 #ifndef _AMD64_CPU_H_
39 #define _AMD64_CPU_H_
40 
41 /*
42  * Definitions unique to x86-64 cpu support.
43  */
44 #include <machine/frame.h>
45 #include <machine/segments.h>
46 #include <machine/tss.h>
47 #include <machine/intrdefs.h>
48 #include <machine/cacheinfo.h>
49 
50 #ifdef MULTIPROCESSOR
51 #include <machine/i82489reg.h>
52 #include <machine/i82489var.h>
53 #endif
54 
55 #include <sys/device.h>
56 #include <sys/lock.h>
57 #include <sys/sched.h>
58 #include <sys/sensors.h>
59 
60 #ifdef _KERNEL
61 
62 struct cpu_info {
63 	struct device *ci_dev;
64 	struct cpu_info *ci_self;
65 	struct schedstate_percpu ci_schedstate; /* scheduler state */
66 	struct cpu_info *ci_next;
67 
68 	struct proc *ci_curproc;
69 	struct simplelock ci_slock;
70 	u_int ci_cpuid;
71 	u_int ci_apicid;
72 	u_int32_t ci_randseed;
73 
74 	u_int64_t ci_scratch;
75 
76 	struct proc *ci_fpcurproc;
77 	int ci_fpsaving;
78 
79 	struct pcb *ci_curpcb;
80 	struct pcb *ci_idle_pcb;
81 	int ci_idle_tss_sel;
82 
83 	struct intrsource *ci_isources[MAX_INTR_SOURCES];
84 	u_int32_t	ci_ipending;
85 	int		ci_ilevel;
86 	int		ci_idepth;
87 	u_int32_t	ci_imask[NIPL];
88 	u_int32_t	ci_iunmask[NIPL];
89 
90 	u_int		ci_flags;
91 	u_int32_t	ci_ipis;
92 
93 	u_int32_t	ci_feature_flags;
94 	u_int32_t	ci_feature_eflags;
95 	u_int32_t	ci_signature;
96 	u_int32_t	ci_family;
97 	u_int32_t	ci_model;
98 	u_int64_t	ci_tsc_freq;
99 
100 	struct cpu_functions *ci_func;
101 	void (*cpu_setup)(struct cpu_info *);
102 	void (*ci_info)(struct cpu_info *);
103 
104 	int		ci_want_resched;
105 
106 	struct x86_cache_info ci_cinfo[CAI_COUNT];
107 
108 	char		*ci_gdt;
109 
110 	volatile int	ci_ddb_paused;
111 #define CI_DDB_RUNNING		0
112 #define CI_DDB_SHOULDSTOP	1
113 #define CI_DDB_STOPPED		2
114 #define CI_DDB_ENTERDDB		3
115 #define CI_DDB_INDDB		4
116 
117 	volatile int ci_setperf_state;
118 #define CI_SETPERF_READY	0
119 #define CI_SETPERF_SHOULDSTOP	1
120 #define CI_SETPERF_INTRANSIT	2
121 #define CI_SETPERF_DONE		3
122 
123 	struct x86_64_tss	ci_doubleflt_tss;
124 
125 	char *ci_doubleflt_stack;
126 
127 	struct ksensordev	ci_sensordev;
128 	struct ksensor		ci_sensor;
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 #define PROC_PC(p)	((p)->p_md.md_regs->tf_rip)
142 
143 extern struct cpu_info cpu_info_primary;
144 extern struct cpu_info *cpu_info_list;
145 
146 #define CPU_INFO_ITERATOR		int
147 #define CPU_INFO_FOREACH(cii, ci)	for (cii = 0, ci = cpu_info_list; \
148 					    ci != NULL; ci = ci->ci_next)
149 
150 #define CPU_INFO_UNIT(ci)	((ci)->ci_dev ? (ci)->ci_dev->dv_unit : 0)
151 
152 /*
153  * Preempt the current process if in interrupt from user mode,
154  * or after the current trap/syscall if in system mode.
155  */
156 extern void need_resched(struct cpu_info *);
157 #define clear_resched(ci) (ci)->ci_want_resched = 0
158 
159 #if defined(MULTIPROCESSOR)
160 
161 #define MAXCPUS		32	/* bitmask; can be bumped to 64 */
162 
163 #define CPU_STARTUP(_ci)	((_ci)->ci_func->start(_ci))
164 #define CPU_STOP(_ci)		((_ci)->ci_func->stop(_ci))
165 #define CPU_START_CLEANUP(_ci)	((_ci)->ci_func->cleanup(_ci))
166 
167 #define curcpu()	({struct cpu_info *__ci;                  \
168 			asm volatile("movq %%gs:8,%0" : "=r" (__ci)); \
169 			__ci;})
170 #define cpu_number()	(curcpu()->ci_cpuid)
171 
172 #define CPU_IS_PRIMARY(ci)	((ci)->ci_flags & CPUF_PRIMARY)
173 
174 extern struct cpu_info *cpu_info[MAXCPUS];
175 
176 void cpu_boot_secondary_processors(void);
177 void cpu_init_idle_pcbs(void);
178 
179 void cpu_unidle(struct cpu_info *);
180 
181 #else /* !MULTIPROCESSOR */
182 
183 #define MAXCPUS		1
184 
185 #ifdef _KERNEL
186 extern struct cpu_info cpu_info_primary;
187 
188 #define curcpu()		(&cpu_info_primary)
189 
190 #define cpu_unidle(ci)
191 
192 #endif
193 
194 /*
195  * definitions of cpu-dependent requirements
196  * referenced in generic code
197  */
198 #define	cpu_number()		0
199 #define CPU_IS_PRIMARY(ci)	1
200 
201 #endif	/* MULTIPROCESSOR */
202 
203 #endif /* _KERNEL */
204 
205 #include <machine/psl.h>
206 
207 #ifdef MULTIPROCESSOR
208 #include <sys/mplock.h>
209 #endif
210 
211 #define aston(p)	((p)->p_md.md_astpending = 1)
212 
213 extern u_int32_t cpus_attached;
214 
215 #define curpcb		curcpu()->ci_curpcb
216 
217 /*
218  * Arguments to hardclock, softclock and statclock
219  * encapsulate the previous machine state in an opaque
220  * clockframe; for now, use generic intrframe.
221  */
222 #define clockframe intrframe
223 
224 #define	CLKF_USERMODE(frame)	USERMODE((frame)->if_cs, (frame)->if_rflags)
225 #define CLKF_PC(frame)		((frame)->if_rip)
226 #define CLKF_INTR(frame)	(curcpu()->ci_idepth > 1)
227 
228 /*
229  * This is used during profiling to integrate system time.
230  */
231 #define	PROC_PC(p)		((p)->p_md.md_regs->tf_rip)
232 
233 /*
234  * Give a profiling tick to the current process when the user profiling
235  * buffer pages are invalid.  On the i386, request an ast to send us
236  * through trap(), marking the proc as needing a profiling tick.
237  */
238 #define	need_proftick(p)	aston(p)
239 
240 void signotify(struct proc *);
241 
242 /*
243  * We need a machine-independent name for this.
244  */
245 extern void (*delay_func)(int);
246 struct timeval;
247 
248 #define DELAY(x)		(*delay_func)(x)
249 #define delay(x)		(*delay_func)(x)
250 
251 
252 #ifdef _KERNEL
253 extern int biosbasemem;
254 extern int biosextmem;
255 extern int cpu;
256 extern int cpu_feature;
257 extern int cpu_ecxfeature;
258 extern int cpu_id;
259 extern char cpu_vendor[];
260 extern int cpuid_level;
261 extern int cpuspeed;
262 
263 /* identcpu.c */
264 void	identifycpu(struct cpu_info *);
265 int	cpu_amd64speed(int *);
266 void cpu_probe_features(struct cpu_info *);
267 
268 /* machdep.c */
269 void	dumpconf(void);
270 int	cpu_maxproc(void);
271 void	cpu_reset(void);
272 void	x86_64_proc0_tss_ldt_init(void);
273 void	x86_64_bufinit(void);
274 void	x86_64_init_pcb_tss_ldt(struct cpu_info *);
275 void	cpu_proc_fork(struct proc *, struct proc *);
276 int	amd64_pa_used(paddr_t);
277 extern void (*cpu_idle_enter_fcn)(void);
278 extern void (*cpu_idle_cycle_fcn)(void);
279 extern void (*cpu_idle_leave_fcn)(void);
280 
281 struct region_descriptor;
282 void	lgdt(struct region_descriptor *);
283 
284 struct pcb;
285 void	savectx(struct pcb *);
286 void	switch_exit(struct proc *, void (*)(struct proc *));
287 void	proc_trampoline(void);
288 void	child_trampoline(void);
289 
290 /* clock.c */
291 void	initrtclock(void);
292 void	startrtclock(void);
293 void	i8254_delay(int);
294 void	i8254_initclocks(void);
295 void	i8254_inittimecounter(void);
296 void	i8254_inittimecounter_simple(void);
297 
298 
299 void cpu_init_msrs(struct cpu_info *);
300 
301 
302 /* trap.c */
303 void	child_return(void *);
304 
305 /* dkcsum.c */
306 void	dkcsumattach(void);
307 
308 /* consinit.c */
309 void kgdb_port_init(void);
310 
311 /* bus_machdep.c */
312 void x86_bus_space_init(void);
313 void x86_bus_space_mallocok(void);
314 
315 /* powernow-k8.c */
316 void k8_powernow_init(struct cpu_info *);
317 void k8_powernow_setperf(int);
318 
319 void est_init(struct cpu_info *);
320 void est_setperf(int);
321 
322 #ifdef MULTIPROCESSOR
323 /* mp_setperf.c */
324 void mp_setperf_init(void);
325 #endif
326 
327 #endif /* _KERNEL */
328 
329 /*
330  * CTL_MACHDEP definitions.
331  */
332 #define	CPU_CONSDEV		1	/* dev_t: console terminal device */
333 #define	CPU_BIOS		2	/* BIOS variables */
334 #define	CPU_BLK2CHR		3	/* convert blk maj into chr one */
335 #define	CPU_CHR2BLK		4	/* convert chr maj into blk one */
336 #define CPU_ALLOWAPERTURE	5	/* allow mmap of /dev/xf86 */
337 #define CPU_CPUVENDOR		6	/* cpuid vendor string */
338 #define CPU_CPUID		7	/* cpuid */
339 #define CPU_CPUFEATURE		8	/* cpuid features */
340 #define CPU_APMWARN		9	/* APM battery warning percentage */
341 #define CPU_KBDRESET		10	/* keyboard reset under pcvt */
342 #define CPU_APMHALT		11	/* halt -p hack */
343 #define CPU_XCRYPT		12	/* supports VIA xcrypt in userland */
344 #define CPU_MAXID		13	/* number of valid machdep ids */
345 
346 #define	CTL_MACHDEP_NAMES { \
347 	{ 0, 0 }, \
348 	{ "console_device", CTLTYPE_STRUCT }, \
349 	{ "bios", CTLTYPE_INT }, \
350 	{ "blk2chr", CTLTYPE_STRUCT }, \
351 	{ "chr2blk", CTLTYPE_STRUCT }, \
352 	{ "allowaperture", CTLTYPE_INT }, \
353 	{ "cpuvendor", CTLTYPE_STRING }, \
354 	{ "cpuid", CTLTYPE_INT }, \
355 	{ "cpufeature", CTLTYPE_INT }, \
356 	{ "apmwarn", CTLTYPE_INT }, \
357 	{ "kbdreset", CTLTYPE_INT }, \
358 	{ "apmhalt", CTLTYPE_INT }, \
359 	{ "xcrypt", CTLTYPE_INT }, \
360 }
361 
362 /*
363  * Default cr4 flags.
364  * Doesn't really belong here, but doesn't really belong anywhere else
365  * either. Just to avoid painful mismatches of cr4 flags since they are
366  * set in three different places.
367  */
368 #define CR4_DEFAULT (CR4_PAE|CR4_PGE|CR4_PSE|CR4_OSFXSR|CR4_OSXMMEXCPT)
369 
370 #endif /* !_AMD64_CPU_H_ */
371