xref: /openbsd-src/sys/arch/amd64/include/cpu.h (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: cpu.h,v 1.43 2009/04/27 17:48:25 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_long ci_spin_locks;
73 	u_long ci_simple_locks;
74 	u_int32_t ci_randseed;
75 
76 	u_int64_t ci_scratch;
77 
78 	struct proc *ci_fpcurproc;
79 	int ci_fpsaving;
80 
81 	volatile u_int32_t ci_tlb_ipi_mask;
82 
83 	struct pcb *ci_curpcb;
84 	struct pcb *ci_idle_pcb;
85 	int ci_idle_tss_sel;
86 
87 	struct intrsource *ci_isources[MAX_INTR_SOURCES];
88 	u_int32_t	ci_ipending;
89 	int		ci_ilevel;
90 	int		ci_idepth;
91 	u_int32_t	ci_imask[NIPL];
92 	u_int32_t	ci_iunmask[NIPL];
93 
94 	paddr_t 	ci_idle_pcb_paddr;
95 	u_int		ci_flags;
96 	u_int32_t	ci_ipis;
97 
98 	u_int32_t	ci_feature_flags;
99 	u_int32_t	ci_feature_eflags;
100 	u_int32_t	ci_signature;
101 	u_int32_t	ci_family;
102 	u_int32_t	ci_model;
103 	u_int64_t	ci_tsc_freq;
104 
105 	struct cpu_functions *ci_func;
106 	void (*cpu_setup)(struct cpu_info *);
107 	void (*ci_info)(struct cpu_info *);
108 
109 	int		ci_want_resched;
110 
111 	struct x86_cache_info ci_cinfo[CAI_COUNT];
112 
113 	struct timeval 	ci_cc_time;
114 	int64_t		ci_cc_cc;
115 	int64_t		ci_cc_ms_delta;
116 	int64_t		ci_cc_denom;
117 
118 	char		*ci_gdt;
119 
120 	volatile int	ci_ddb_paused;
121 #define CI_DDB_RUNNING		0
122 #define CI_DDB_SHOULDSTOP	1
123 #define CI_DDB_STOPPED		2
124 #define CI_DDB_ENTERDDB		3
125 #define CI_DDB_INDDB		4
126 
127 	volatile int ci_setperf_state;
128 #define CI_SETPERF_READY	0
129 #define CI_SETPERF_SHOULDSTOP	1
130 #define CI_SETPERF_INTRANSIT	2
131 #define CI_SETPERF_DONE		3
132 
133 	struct x86_64_tss	ci_doubleflt_tss;
134 
135 	char *ci_doubleflt_stack;
136 
137 	struct ksensordev	ci_sensordev;
138 	struct ksensor		ci_sensor;
139 };
140 
141 #define CPUF_BSP	0x0001		/* CPU is the original BSP */
142 #define CPUF_AP		0x0002		/* CPU is an AP */
143 #define CPUF_SP		0x0004		/* CPU is only processor */
144 #define CPUF_PRIMARY	0x0008		/* CPU is active primary processor */
145 
146 #define CPUF_PRESENT	0x1000		/* CPU is present */
147 #define CPUF_RUNNING	0x2000		/* CPU is running */
148 #define CPUF_PAUSE	0x4000		/* CPU is paused in DDB */
149 #define CPUF_GO		0x8000		/* CPU should start running */
150 
151 #define PROC_PC(p)	((p)->p_md.md_regs->tf_rip)
152 
153 extern struct cpu_info cpu_info_primary;
154 extern struct cpu_info *cpu_info_list;
155 
156 #define CPU_INFO_ITERATOR		int
157 #define CPU_INFO_FOREACH(cii, ci)	for (cii = 0, ci = cpu_info_list; \
158 					    ci != NULL; ci = ci->ci_next)
159 
160 #define CPU_INFO_UNIT(ci)	((ci)->ci_dev ? (ci)->ci_dev->dv_unit : 0)
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(struct cpu_info *);
167 #define clear_resched(ci) (ci)->ci_want_resched = 0
168 
169 #if defined(MULTIPROCESSOR)
170 
171 #define MAXCPUS		32	/* bitmask; can be bumped to 64 */
172 
173 #define CPU_STARTUP(_ci)	((_ci)->ci_func->start(_ci))
174 #define CPU_STOP(_ci)		((_ci)->ci_func->stop(_ci))
175 #define CPU_START_CLEANUP(_ci)	((_ci)->ci_func->cleanup(_ci))
176 
177 #define curcpu()	({struct cpu_info *__ci;                  \
178 			asm volatile("movq %%gs:8,%0" : "=r" (__ci)); \
179 			__ci;})
180 #define cpu_number()	(curcpu()->ci_cpuid)
181 
182 #define CPU_IS_PRIMARY(ci)	((ci)->ci_flags & CPUF_PRIMARY)
183 
184 extern struct cpu_info *cpu_info[MAXCPUS];
185 
186 void cpu_boot_secondary_processors(void);
187 void cpu_init_idle_pcbs(void);
188 
189 void cpu_unidle(struct cpu_info *);
190 
191 #else /* !MULTIPROCESSOR */
192 
193 #define MAXCPUS		1
194 
195 #ifdef _KERNEL
196 extern struct cpu_info cpu_info_primary;
197 
198 #define curcpu()		(&cpu_info_primary)
199 
200 #define cpu_unidle(ci)
201 
202 #endif
203 
204 /*
205  * definitions of cpu-dependent requirements
206  * referenced in generic code
207  */
208 #define	cpu_number()		0
209 #define CPU_IS_PRIMARY(ci)	1
210 
211 #endif	/* MULTIPROCESSOR */
212 
213 #endif /* _KERNEL */
214 
215 #include <machine/psl.h>
216 
217 #ifdef MULTIPROCESSOR
218 #include <sys/mplock.h>
219 #endif
220 
221 #define aston(p)	((p)->p_md.md_astpending = 1)
222 
223 extern u_int32_t cpus_attached;
224 
225 #define curpcb		curcpu()->ci_curpcb
226 
227 /*
228  * Arguments to hardclock, softclock and statclock
229  * encapsulate the previous machine state in an opaque
230  * clockframe; for now, use generic intrframe.
231  */
232 #define clockframe intrframe
233 
234 #define	CLKF_USERMODE(frame)	USERMODE((frame)->if_cs, (frame)->if_rflags)
235 #define CLKF_PC(frame)		((frame)->if_rip)
236 #define CLKF_INTR(frame)	(curcpu()->ci_idepth > 1)
237 
238 /*
239  * This is used during profiling to integrate system time.
240  */
241 #define	PROC_PC(p)		((p)->p_md.md_regs->tf_rip)
242 
243 /*
244  * Give a profiling tick to the current process when the user profiling
245  * buffer pages are invalid.  On the i386, request an ast to send us
246  * through trap(), marking the proc as needing a profiling tick.
247  */
248 #define	need_proftick(p)	aston(p)
249 
250 void signotify(struct proc *);
251 
252 /*
253  * We need a machine-independent name for this.
254  */
255 extern void (*delay_func)(int);
256 struct timeval;
257 
258 #define DELAY(x)		(*delay_func)(x)
259 #define delay(x)		(*delay_func)(x)
260 
261 
262 #ifdef _KERNEL
263 extern int biosbasemem;
264 extern int biosextmem;
265 extern int cpu;
266 extern int cpu_feature;
267 extern int cpu_ecxfeature;
268 extern int cpu_id;
269 extern char cpu_vendor[];
270 extern int cpuid_level;
271 extern int cpuspeed;
272 
273 /* identcpu.c */
274 void	identifycpu(struct cpu_info *);
275 int	cpu_amd64speed(int *);
276 void cpu_probe_features(struct cpu_info *);
277 
278 /* machdep.c */
279 void	dumpconf(void);
280 int	cpu_maxproc(void);
281 void	cpu_reset(void);
282 void	x86_64_proc0_tss_ldt_init(void);
283 void	x86_64_bufinit(void);
284 void	x86_64_init_pcb_tss_ldt(struct cpu_info *);
285 void	cpu_proc_fork(struct proc *, struct proc *);
286 int	amd64_pa_used(paddr_t);
287 
288 struct region_descriptor;
289 void	lgdt(struct region_descriptor *);
290 
291 struct pcb;
292 void	savectx(struct pcb *);
293 void	switch_exit(struct proc *, void (*)(struct proc *));
294 void	proc_trampoline(void);
295 void	child_trampoline(void);
296 
297 /* clock.c */
298 void	initrtclock(void);
299 void	startrtclock(void);
300 void	i8254_delay(int);
301 void	i8254_initclocks(void);
302 void	i8254_inittimecounter(void);
303 void	i8254_inittimecounter_simple(void);
304 
305 
306 void cpu_init_msrs(struct cpu_info *);
307 
308 
309 /* trap.c */
310 void	child_return(void *);
311 
312 /* dkcsum.c */
313 void	dkcsumattach(void);
314 
315 /* consinit.c */
316 void kgdb_port_init(void);
317 
318 /* bus_machdep.c */
319 void x86_bus_space_init(void);
320 void x86_bus_space_mallocok(void);
321 
322 /* powernow-k8.c */
323 void k8_powernow_init(struct cpu_info *);
324 void k8_powernow_setperf(int);
325 
326 void est_init(struct cpu_info *);
327 void est_setperf(int);
328 
329 #ifdef MULTIPROCESSOR
330 /* mp_setperf.c */
331 void mp_setperf_init(void);
332 #endif
333 
334 #endif /* _KERNEL */
335 
336 /*
337  * CTL_MACHDEP definitions.
338  */
339 #define	CPU_CONSDEV		1	/* dev_t: console terminal device */
340 #define	CPU_BIOS		2	/* BIOS variables */
341 #define	CPU_BLK2CHR		3	/* convert blk maj into chr one */
342 #define	CPU_CHR2BLK		4	/* convert chr maj into blk one */
343 #define CPU_ALLOWAPERTURE	5	/* allow mmap of /dev/xf86 */
344 #define CPU_CPUVENDOR		6	/* cpuid vendor string */
345 #define CPU_CPUID		7	/* cpuid */
346 #define CPU_CPUFEATURE		8	/* cpuid features */
347 #define CPU_APMWARN		9	/* APM battery warning percentage */
348 #define CPU_KBDRESET		10	/* keyboard reset under pcvt */
349 #define CPU_APMHALT		11	/* halt -p hack */
350 #define CPU_MAXID		12	/* number of valid machdep ids */
351 
352 #define	CTL_MACHDEP_NAMES { \
353 	{ 0, 0 }, \
354 	{ "console_device", CTLTYPE_STRUCT }, \
355 	{ "bios", CTLTYPE_INT }, \
356 	{ "blk2chr", CTLTYPE_STRUCT }, \
357 	{ "chr2blk", CTLTYPE_STRUCT }, \
358 	{ "allowaperture", CTLTYPE_INT }, \
359 	{ "cpuvendor", CTLTYPE_STRING }, \
360 	{ "cpuid", CTLTYPE_INT }, \
361 	{ "cpufeature", CTLTYPE_INT }, \
362 	{ "apmwarn", CTLTYPE_INT }, \
363 	{ "kbdreset", CTLTYPE_INT }, \
364 	{ "apmhalt", CTLTYPE_INT }, \
365 }
366 
367 /*
368  * Default cr4 flags.
369  * Doesn't really belong here, but doesn't really belong anywhere else
370  * either. Just to avoid painful mismatches of cr4 flags since they are
371  * set in three different places.
372  */
373 #define CR4_DEFAULT (CR4_PAE|CR4_PGE|CR4_PSE|CR4_OSFXSR|CR4_OSXMMEXCPT)
374 
375 #endif /* !_AMD64_CPU_H_ */
376