cpu.h revision 1.27
11.27Sad/*	$NetBSD: cpu.h,v 1.27 2007/09/26 19:48:40 ad Exp $	*/
21.1Sfvdl
31.1Sfvdl/*-
41.1Sfvdl * Copyright (c) 1990 The Regents of the University of California.
51.1Sfvdl * All rights reserved.
61.1Sfvdl *
71.1Sfvdl * This code is derived from software contributed to Berkeley by
81.1Sfvdl * William Jolitz.
91.1Sfvdl *
101.1Sfvdl * Redistribution and use in source and binary forms, with or without
111.1Sfvdl * modification, are permitted provided that the following conditions
121.1Sfvdl * are met:
131.1Sfvdl * 1. Redistributions of source code must retain the above copyright
141.1Sfvdl *    notice, this list of conditions and the following disclaimer.
151.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
161.1Sfvdl *    notice, this list of conditions and the following disclaimer in the
171.1Sfvdl *    documentation and/or other materials provided with the distribution.
181.2Sagc * 3. Neither the name of the University nor the names of its contributors
191.1Sfvdl *    may be used to endorse or promote products derived from this software
201.1Sfvdl *    without specific prior written permission.
211.1Sfvdl *
221.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231.1Sfvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241.1Sfvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251.1Sfvdl * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261.1Sfvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271.1Sfvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281.1Sfvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291.1Sfvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301.1Sfvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311.1Sfvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321.1Sfvdl * SUCH DAMAGE.
331.1Sfvdl *
341.1Sfvdl *	@(#)cpu.h	5.4 (Berkeley) 5/9/91
351.1Sfvdl */
361.1Sfvdl
371.1Sfvdl#ifndef _AMD64_CPU_H_
381.1Sfvdl#define _AMD64_CPU_H_
391.1Sfvdl
401.6Syamt#if defined(_KERNEL)
411.1Sfvdl#if defined(_KERNEL_OPT)
421.1Sfvdl#include "opt_multiprocessor.h"
431.1Sfvdl#include "opt_lockdebug.h"
441.1Sfvdl#endif
451.1Sfvdl
461.1Sfvdl/*
471.1Sfvdl * Definitions unique to x86-64 cpu support.
481.1Sfvdl */
491.1Sfvdl#include <machine/frame.h>
501.1Sfvdl#include <machine/segments.h>
511.1Sfvdl#include <machine/tss.h>
521.1Sfvdl#include <machine/intrdefs.h>
531.1Sfvdl#include <x86/cacheinfo.h>
541.1Sfvdl
551.1Sfvdl#include <sys/device.h>
561.17Sad#include <sys/simplelock.h>
571.4Syamt#include <sys/cpu_data.h>
581.4Syamt#include <sys/cc_microtime.h>
591.1Sfvdl
601.1Sfvdlstruct cpu_info {
611.1Sfvdl	struct device *ci_dev;
621.1Sfvdl	struct cpu_info *ci_self;
631.25Sad
641.25Sad	/*
651.25Sad	 * Will be accessed by other CPUs.
661.25Sad	 */
671.1Sfvdl	struct cpu_info *ci_next;
681.1Sfvdl	struct lwp *ci_curlwp;
691.25Sad	struct pmap_cpu *ci_pmap_cpu;
701.25Sad	struct lwp *ci_fpcurlwp;
711.25Sad	int ci_fpsaving;
721.1Sfvdl	u_int ci_cpuid;
731.25Sad	int ci_cpumask;			/* (1 << CPU ID) */
741.1Sfvdl	u_int ci_apicid;
751.25Sad	struct cpu_data ci_data;	/* MI per-cpu data */
761.25Sad	struct cc_microtime_state ci_cc;/* cc_microtime state */
771.1Sfvdl
781.25Sad	/*
791.25Sad	 * Private members.
801.25Sad	 */
811.25Sad	struct evcnt ci_tlb_evcnt;	/* tlb shootdown counter */
821.25Sad	int ci_need_tlbwait;		/* need to wait for TLB invalidations */
831.1Sfvdl	u_int64_t ci_scratch;
841.1Sfvdl	struct intrsource *ci_isources[MAX_INTR_SOURCES];
851.13Sad	volatile int	ci_mtx_count;	/* Negative count of spin mutexes */
861.13Sad	volatile int	ci_mtx_oldspl;	/* Old SPL at this ci_idepth */
871.13Sad
881.13Sad	/* The following must be aligned for cmpxchg8b. */
891.13Sad	struct {
901.13Sad		uint32_t	ipending;
911.13Sad		int		ilevel;
921.13Sad	} ci_istate __aligned(8);
931.13Sad#define ci_ipending	ci_istate.ipending
941.13Sad#define	ci_ilevel	ci_istate.ilevel
951.13Sad
961.1Sfvdl	int		ci_idepth;
971.1Sfvdl	u_int32_t	ci_imask[NIPL];
981.1Sfvdl	u_int32_t	ci_iunmask[NIPL];
991.1Sfvdl
1001.1Sfvdl	paddr_t 	ci_idle_pcb_paddr;
1011.1Sfvdl	u_int		ci_flags;
1021.1Sfvdl	u_int32_t	ci_ipis;
1031.1Sfvdl
1041.25Sad	int32_t		ci_cpuid_level;
1051.25Sad	uint32_t	ci_signature;
1061.25Sad	uint32_t	ci_feature_flags;
1071.19Sxtraeme	uint32_t	ci_feature2_flags;
1081.25Sad	uint32_t	ci_vendor[4];	 /* vendor string */
1091.1Sfvdl	u_int64_t	ci_tsc_freq;
1101.1Sfvdl
1111.16Sdrochner	const struct cpu_functions *ci_func;
1121.18Sxtraeme	void (*cpu_setup)(struct cpu_info *);
1131.18Sxtraeme	void (*ci_info)(struct cpu_info *);
1141.1Sfvdl
1151.1Sfvdl	int		ci_want_resched;
1161.1Sfvdl	struct trapframe *ci_ddb_regs;
1171.1Sfvdl
1181.1Sfvdl	struct x86_cache_info ci_cinfo[CAI_COUNT];
1191.1Sfvdl
1201.1Sfvdl	char		*ci_gdt;
1211.1Sfvdl
1221.1Sfvdl	struct x86_64_tss	ci_doubleflt_tss;
1231.1Sfvdl	struct x86_64_tss	ci_ddbipi_tss;
1241.1Sfvdl
1251.1Sfvdl	char *ci_doubleflt_stack;
1261.1Sfvdl	char *ci_ddbipi_stack;
1271.1Sfvdl
1281.1Sfvdl	struct evcnt ci_ipi_events[X86_NIPI];
1291.1Sfvdl};
1301.1Sfvdl
1311.1Sfvdl#define CPUF_BSP	0x0001		/* CPU is the original BSP */
1321.1Sfvdl#define CPUF_AP		0x0002		/* CPU is an AP */
1331.1Sfvdl#define CPUF_SP		0x0004		/* CPU is only processor */
1341.1Sfvdl#define CPUF_PRIMARY	0x0008		/* CPU is active primary processor */
1351.1Sfvdl
1361.1Sfvdl#define CPUF_PRESENT	0x1000		/* CPU is present */
1371.1Sfvdl#define CPUF_RUNNING	0x2000		/* CPU is running */
1381.1Sfvdl#define CPUF_PAUSE	0x4000		/* CPU is paused in DDB */
1391.1Sfvdl#define CPUF_GO		0x8000		/* CPU should start running */
1401.1Sfvdl
1411.1Sfvdl
1421.1Sfvdlextern struct cpu_info cpu_info_primary;
1431.1Sfvdlextern struct cpu_info *cpu_info_list;
1441.1Sfvdl
1451.1Sfvdl#define CPU_INFO_ITERATOR		int
1461.1Sfvdl#define CPU_INFO_FOREACH(cii, ci)	cii = 0, ci = cpu_info_list; \
1471.1Sfvdl					ci != NULL; ci = ci->ci_next
1481.1Sfvdl
1491.1Sfvdl#define X86_MAXPROCS		32	/* bitmask; can be bumped to 64 */
1501.1Sfvdl
1511.1Sfvdl#define CPU_STARTUP(_ci)	((_ci)->ci_func->start(_ci))
1521.1Sfvdl#define CPU_STOP(_ci)		((_ci)->ci_func->stop(_ci))
1531.1Sfvdl#define CPU_START_CLEANUP(_ci)	((_ci)->ci_func->cleanup(_ci))
1541.1Sfvdl
1551.27Sad#if defined(__GNUC__) && defined(_KERNEL)
1561.27Sadstatic struct cpu_info *x86_curcpu(void);
1571.27Sadstatic lwp_t *x86_curlwp(void);
1581.27Sad
1591.27Sad__inline static struct cpu_info * __attribute__((__unused__))
1601.27Sadx86_curcpu(void)
1611.27Sad{
1621.27Sad	struct cpu_info *ci;
1631.27Sad
1641.27Sad	__asm volatile("movq %%gs:%1, %0" :
1651.27Sad	    "=r" (ci) :
1661.27Sad	    "m"
1671.27Sad	    (*(struct cpu_info * const *)offsetof(struct cpu_info, ci_self)));
1681.27Sad	return ci;
1691.27Sad}
1701.27Sad
1711.27Sad__inline static lwp_t * __attribute__((__unused__))
1721.27Sadx86_curlwp(void)
1731.27Sad{
1741.27Sad	lwp_t *l;
1751.27Sad
1761.27Sad	__asm volatile("movq %%gs:%1, %0" :
1771.27Sad	    "=r" (l) :
1781.27Sad	    "m"
1791.27Sad	    (*(struct cpu_info * const *)offsetof(struct cpu_info, ci_curlwp)));
1801.27Sad	return l;
1811.27Sad}
1821.27Sad#else	/* __GNUC__ && _KERNEL */
1831.27Sad/* For non-GCC and LKMs */
1841.27Sadstruct cpu_info	*x86_curcpu(void);
1851.27Sadlwp_t	*x86_curlwp(void);
1861.27Sad#endif	/* __GNUC__ && _KERNEL */
1871.27Sad
1881.1Sfvdl#define cpu_number()	(curcpu()->ci_cpuid)
1891.1Sfvdl
1901.1Sfvdl#define CPU_IS_PRIMARY(ci)	((ci)->ci_flags & CPUF_PRIMARY)
1911.1Sfvdl
1921.1Sfvdlextern struct cpu_info *cpu_info[X86_MAXPROCS];
1931.1Sfvdl
1941.18Sxtraemevoid cpu_boot_secondary_processors(void);
1951.23Syamtvoid cpu_init_idle_lwps(void);
1961.1Sfvdl
1971.13Sad#define aston(l)	((l)->l_md.md_astpending = 1)
1981.1Sfvdl
1991.1Sfvdlextern u_int32_t cpus_attached;
2001.1Sfvdl
2011.27Sad#define curcpu()	x86_curcpu()
2021.27Sad#define curlwp		x86_curlwp()
2031.23Syamt#define curpcb		(&curlwp->l_addr->u_pcb)
2041.1Sfvdl
2051.1Sfvdl/*
2061.1Sfvdl * Arguments to hardclock, softclock and statclock
2071.1Sfvdl * encapsulate the previous machine state in an opaque
2081.1Sfvdl * clockframe; for now, use generic intrframe.
2091.1Sfvdl */
2101.7Scubestruct clockframe {
2111.7Scube	struct intrframe cf_if;
2121.7Scube};
2131.1Sfvdl
2141.7Scube#define	CLKF_USERMODE(frame)	USERMODE((frame)->cf_if.if_cs, (frame)->cf_if.if_rflags)
2151.7Scube#define CLKF_PC(frame)		((frame)->cf_if.if_rip)
2161.25Sad#define CLKF_INTR(frame)	(curcpu()->ci_idepth > 0)
2171.1Sfvdl
2181.1Sfvdl/*
2191.1Sfvdl * This is used during profiling to integrate system time.  It can safely
2201.1Sfvdl * assume that the process is resident.
2211.1Sfvdl */
2221.1Sfvdl#define LWP_PC(l)		((l)->l_md.md_regs->tf_rip)
2231.1Sfvdl
2241.1Sfvdl/*
2251.1Sfvdl * Give a profiling tick to the current process when the user profiling
2261.1Sfvdl * buffer pages are invalid.  On the i386, request an ast to send us
2271.1Sfvdl * through trap(), marking the proc as needing a profiling tick.
2281.1Sfvdl */
2291.13Sadextern void cpu_need_proftick(struct lwp *);
2301.1Sfvdl
2311.1Sfvdl/*
2321.13Sad * Notify an LWP that it has a signal pending, process as soon as possible.
2331.1Sfvdl */
2341.13Sadextern void cpu_signotify(struct lwp *);
2351.1Sfvdl
2361.1Sfvdl/*
2371.1Sfvdl * We need a machine-independent name for this.
2381.1Sfvdl */
2391.18Sxtraemeextern void (*delay_func)(int);
2401.1Sfvdl
2411.1Sfvdl#define DELAY(x)		(*delay_func)(x)
2421.1Sfvdl#define delay(x)		(*delay_func)(x)
2431.1Sfvdl
2441.1Sfvdl
2451.1Sfvdl/*
2461.1Sfvdl * pull in #defines for kinds of processors
2471.1Sfvdl */
2481.1Sfvdl
2491.1Sfvdlextern int biosbasemem;
2501.1Sfvdlextern int biosextmem;
2511.1Sfvdlextern int cpu;
2521.1Sfvdlextern int cpu_feature;
2531.25Sadextern int cpu_feature2;
2541.1Sfvdlextern int cpu_id;
2551.1Sfvdlextern char cpu_vendor[];
2561.1Sfvdlextern int cpuid_level;
2571.1Sfvdl
2581.1Sfvdl/* identcpu.c */
2591.1Sfvdl
2601.18Sxtraemevoid	identifycpu(struct cpu_info *);
2611.18Sxtraemevoid cpu_probe_features(struct cpu_info *);
2621.1Sfvdl
2631.1Sfvdl/* machdep.c */
2641.18Sxtraemevoid	dumpconf(void);
2651.18Sxtraemeint	cpu_maxproc(void);
2661.18Sxtraemevoid	cpu_reset(void);
2671.18Sxtraemevoid	x86_64_proc0_tss_ldt_init(void);
2681.18Sxtraemevoid	x86_64_init_pcb_tss_ldt(struct cpu_info *);
2691.18Sxtraemevoid	cpu_proc_fork(struct proc *, struct proc *);
2701.1Sfvdl
2711.1Sfvdlstruct region_descriptor;
2721.18Sxtraemevoid	lgdt(struct region_descriptor *);
2731.18Sxtraemevoid	fillw(short, void *, size_t);
2741.1Sfvdl
2751.1Sfvdlstruct pcb;
2761.18Sxtraemevoid	savectx(struct pcb *);
2771.23Syamtvoid	lwp_trampoline(void);
2781.18Sxtraemevoid	child_trampoline(void);
2791.1Sfvdl
2801.1Sfvdl/* clock.c */
2811.18Sxtraemevoid	initrtclock(u_long);
2821.18Sxtraemevoid	startrtclock(void);
2831.18Sxtraemevoid	i8254_delay(int);
2841.18Sxtraemevoid	i8254_microtime(struct timeval *);
2851.18Sxtraemevoid	i8254_initclocks(void);
2861.1Sfvdl
2871.18Sxtraemevoid cpu_init_msrs(struct cpu_info *);
2881.1Sfvdl
2891.1Sfvdl
2901.1Sfvdl/* vm_machdep.c */
2911.18Sxtraemeint kvtop(void *);
2921.1Sfvdl
2931.1Sfvdl/* trap.c */
2941.18Sxtraemevoid	child_return(void *);
2951.1Sfvdl
2961.1Sfvdl/* consinit.c */
2971.18Sxtraemevoid kgdb_port_init(void);
2981.1Sfvdl
2991.1Sfvdl/* bus_machdep.c */
3001.18Sxtraemevoid x86_bus_space_init(void);
3011.18Sxtraemevoid x86_bus_space_mallocok(void);
3021.12Sxtraeme
3031.1Sfvdl#endif /* _KERNEL */
3041.1Sfvdl
3051.1Sfvdl#include <machine/psl.h>
3061.1Sfvdl
3071.1Sfvdl/*
3081.1Sfvdl * CTL_MACHDEP definitions.
3091.1Sfvdl */
3101.1Sfvdl#define	CPU_CONSDEV		1	/* dev_t: console terminal device */
3111.1Sfvdl#define	CPU_BIOSBASEMEM		2	/* int: bios-reported base mem (K) */
3121.1Sfvdl#define	CPU_BIOSEXTMEM		3	/* int: bios-reported ext. mem (K) */
3131.1Sfvdl#define	CPU_NKPDE		4	/* int: number of kernel PDEs */
3141.1Sfvdl#define	CPU_BOOTED_KERNEL	5	/* string: booted kernel name */
3151.1Sfvdl#define CPU_DISKINFO		6	/* disk geometry information */
3161.1Sfvdl#define CPU_FPU_PRESENT		7	/* FPU is present */
3171.1Sfvdl#define	CPU_MAXID		8	/* number of valid machdep ids */
3181.1Sfvdl
3191.1Sfvdl#define	CTL_MACHDEP_NAMES { \
3201.1Sfvdl	{ 0, 0 }, \
3211.1Sfvdl	{ "console_device", CTLTYPE_STRUCT }, \
3221.1Sfvdl	{ "biosbasemem", CTLTYPE_INT }, \
3231.1Sfvdl	{ "biosextmem", CTLTYPE_INT }, \
3241.1Sfvdl	{ "nkpde", CTLTYPE_INT }, \
3251.1Sfvdl	{ "booted_kernel", CTLTYPE_STRING }, \
3261.1Sfvdl	{ "diskinfo", CTLTYPE_STRUCT }, \
3271.1Sfvdl	{ "fpu_present", CTLTYPE_INT }, \
3281.1Sfvdl}
3291.1Sfvdl
3301.1Sfvdl
3311.1Sfvdl/*
3321.1Sfvdl * Structure for CPU_DISKINFO sysctl call.
3331.1Sfvdl * XXX this should be somewhere else.
3341.1Sfvdl */
3351.1Sfvdl#define MAX_BIOSDISKS	16
3361.1Sfvdl
3371.1Sfvdlstruct disklist {
3381.1Sfvdl	int dl_nbiosdisks;			   /* number of bios disks */
3391.1Sfvdl	struct biosdisk_info {
3401.1Sfvdl		int bi_dev;			   /* BIOS device # (0x80 ..) */
3411.1Sfvdl		int bi_cyl;			   /* cylinders on disk */
3421.1Sfvdl		int bi_head;			   /* heads per track */
3431.1Sfvdl		int bi_sec;			   /* sectors per track */
3441.1Sfvdl		u_int64_t bi_lbasecs;		   /* total sec. (iff ext13) */
3451.1Sfvdl#define BIFLAG_INVALID		0x01
3461.1Sfvdl#define BIFLAG_EXTINT13		0x02
3471.1Sfvdl		int bi_flags;
3481.1Sfvdl	} dl_biosdisks[MAX_BIOSDISKS];
3491.1Sfvdl
3501.1Sfvdl	int dl_nnativedisks;			   /* number of native disks */
3511.1Sfvdl	struct nativedisk_info {
3521.1Sfvdl		char ni_devname[16];		   /* native device name */
3531.1Sfvdl		int ni_nmatches; 		   /* # of matches w/ BIOS */
3541.1Sfvdl		int ni_biosmatches[MAX_BIOSDISKS]; /* indices in dl_biosdisks */
3551.1Sfvdl	} dl_nativedisks[1];			   /* actually longer */
3561.1Sfvdl};
3571.1Sfvdl
3581.1Sfvdl#endif /* !_AMD64_CPU_H_ */
359