From a67048ec2ab6c366590817bb227836241b87c779 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sun, 19 Jun 2011 22:55:29 +0200
Subject: [PATCH 06/18] hwpmc: Some DragonFly specific adjustments to kernel part, headers and utils.

* Adjust #include paths to our hierarchy, remove unused headers and add
  headers we need

* In the module Makefile, remove an unnecessary .PATH and vnode_if.h, and
  comment out unused architectures.

* Remove CTLFLAG_INT from sysctls

* kmalloc, kfree, kprintf, etc. adjustments

* __amd64__ -> __x86_64__

* Silence -Wold-style-definition warnings

* *_FOREACH_SAFE -> *_FOREACH_MUTABLE

* Put the CVS id into the comments
---
 sys/cpu/i386/include/pmc_mdep.h    |   14 ++--
 sys/cpu/x86_64/include/pmc_mdep.h  |   10 ++--
 sys/dev/misc/hwpmc/Makefile        |   32 ++++-----
 sys/dev/misc/hwpmc/hwpmc_amd.c     |   26 ++++----
 sys/dev/misc/hwpmc/hwpmc_core.c    |   20 +++---
 sys/dev/misc/hwpmc/hwpmc_intel.c   |   21 +++---
 sys/dev/misc/hwpmc/hwpmc_logging.c |   22 +++---
 sys/dev/misc/hwpmc/hwpmc_mod.c     |  127 ++++++++++++++++++------------------
 sys/dev/misc/hwpmc/hwpmc_pentium.c |    6 +-
 sys/dev/misc/hwpmc/hwpmc_piv.c     |   18 +++---
 sys/dev/misc/hwpmc/hwpmc_ppro.c    |   17 ++---
 sys/dev/misc/hwpmc/hwpmc_tsc.c     |   13 ++--
 sys/dev/misc/hwpmc/hwpmc_uncore.c  |   18 ++---
 sys/dev/misc/hwpmc/hwpmc_x86.c     |   10 ++--
 sys/kern/kern_pmc.c                |    7 +-
 sys/sys/pmc.h                      |    9 ++-
 sys/sys/pmckern.h                  |    1 -
 usr.sbin/pmcannotate/pmcannotate.c |    6 +-
 usr.sbin/pmccontrol/pmccontrol.c   |    6 +-
 usr.sbin/pmcstat/pmcpl_annotate.c  |    5 +-
 usr.sbin/pmcstat/pmcpl_callgraph.c |    9 +--
 usr.sbin/pmcstat/pmcpl_calltree.c  |    7 +-
 usr.sbin/pmcstat/pmcpl_gprof.c     |    7 +-
 usr.sbin/pmcstat/pmcstat.c         |    9 +--
 usr.sbin/pmcstat/pmcstat_log.c     |   21 +++---
 25 files changed, 210 insertions(+), 231 deletions(-)

diff --git a/sys/cpu/i386/include/pmc_mdep.h b/sys/cpu/i386/include/pmc_mdep.h
index f125cc0..a409bde 100644
--- a/sys/cpu/i386/include/pmc_mdep.h
+++ b/sys/cpu/i386/include/pmc_mdep.h
@@ -53,13 +53,13 @@ struct pmc_mdep;
  * UCF		Intel Uncore fixed-function PMCs.
  */
 
-#include <dev/hwpmc/hwpmc_amd.h> /* K7 and K8 */
-#include <dev/hwpmc/hwpmc_core.h>
-#include <dev/hwpmc/hwpmc_piv.h>
-#include <dev/hwpmc/hwpmc_ppro.h>
-#include <dev/hwpmc/hwpmc_pentium.h>
-#include <dev/hwpmc/hwpmc_tsc.h>
-#include <dev/hwpmc/hwpmc_uncore.h>
+#include <dev/misc/hwpmc/hwpmc_amd.h> /* K7 and K8 */
+#include <dev/misc/hwpmc/hwpmc_core.h>
+#include <dev/misc/hwpmc/hwpmc_piv.h>
+#include <dev/misc/hwpmc/hwpmc_ppro.h>
+#include <dev/misc/hwpmc/hwpmc_pentium.h>
+#include <dev/misc/hwpmc/hwpmc_tsc.h>
+#include <dev/misc/hwpmc/hwpmc_uncore.h>
 
 /*
  * Intel processors implementing V2 and later of the Intel performance
diff --git a/sys/cpu/x86_64/include/pmc_mdep.h b/sys/cpu/x86_64/include/pmc_mdep.h
index 16fa11d..19ab0a0 100644
--- a/sys/cpu/x86_64/include/pmc_mdep.h
+++ b/sys/cpu/x86_64/include/pmc_mdep.h
@@ -39,11 +39,11 @@
 struct pmc_mdep;
 #endif
 
-#include <dev/hwpmc/hwpmc_amd.h>
-#include <dev/hwpmc/hwpmc_core.h>
-#include <dev/hwpmc/hwpmc_piv.h>
-#include <dev/hwpmc/hwpmc_tsc.h>
-#include <dev/hwpmc/hwpmc_uncore.h>
+#include <dev/misc/hwpmc/hwpmc_amd.h>
+#include <dev/misc/hwpmc/hwpmc_core.h>
+#include <dev/misc/hwpmc/hwpmc_piv.h>
+#include <dev/misc/hwpmc/hwpmc_tsc.h>
+#include <dev/misc/hwpmc/hwpmc_uncore.h>
 
 /*
  * Intel processors implementing V2 and later of the Intel performance
diff --git a/sys/dev/misc/hwpmc/Makefile b/sys/dev/misc/hwpmc/Makefile
index 3f65faa..ff556b7 100644
--- a/sys/dev/misc/hwpmc/Makefile
+++ b/sys/dev/misc/hwpmc/Makefile
@@ -2,38 +2,36 @@
 # $FreeBSD: src/sys/modules/hwpmc/Makefile,v 1.10 2010/08/23 06:13:29 imp Exp $
 #
 
-.PATH: ${.CURDIR}/../../dev/hwpmc
-
 KMOD=	hwpmc
 
-SRCS=	hwpmc_mod.c hwpmc_logging.c vnode_if.h
+SRCS=	hwpmc_mod.c hwpmc_logging.c
 
-.if ${MACHINE_CPUARCH} == "amd64"
+.if ${MACHINE_ARCH} == "x86_64"
 SRCS+=	hwpmc_amd.c hwpmc_core.c hwpmc_intel.c hwpmc_piv.c hwpmc_tsc.c
 SRCS+=	hwpmc_x86.c hwpmc_uncore.c
 SRCS+=	device_if.h bus_if.h
 .endif
 
-.if ${MACHINE_CPUARCH} == "arm"
-SRCS+=	hwpmc_arm.c
-.endif
+#.if ${MACHINE_ARCH} == "arm"
+#SRCS+=	hwpmc_arm.c
+#.endif
 
-.if ${MACHINE_CPUARCH} == "i386"
+.if ${MACHINE_ARCH} == "i386"
 SRCS+=	hwpmc_amd.c hwpmc_core.c hwpmc_intel.c hwpmc_piv.c hwpmc_ppro.c
 SRCS+=	hwpmc_pentium.c hwpmc_tsc.c hwpmc_x86.c hwpmc_uncore.c
 SRCS+=	device_if.h bus_if.h
 .endif
 
-.if ${MACHINE_CPUARCH} == "ia64"
-SRCS+=	hwpmc_ia64.c
-.endif
+#.if ${MACHINE_ARCH} == "ia64"
+#SRCS+=	hwpmc_ia64.c
+#.endif
 
-.if ${MACHINE_CPUARCH} == "powerpc"
-SRCS+=	hwpmc_powerpc.c
-.endif
+#.if ${MACHINE_ARCH} == "powerpc"
+#SRCS+=	hwpmc_powerpc.c
+#.endif
 
-.if ${MACHINE_CPUARCH} == "sparc64"
-SRCS+=	hwpmc_sparc64.c
-.endif
+#.if ${MACHINE_ARCH} == "sparc64"
+#SRCS+=	hwpmc_sparc64.c
+#.endif
 
 .include <bsd.kmod.mk>
diff --git a/sys/dev/misc/hwpmc/hwpmc_amd.c b/sys/dev/misc/hwpmc/hwpmc_amd.c
index d755c55..0f3c7b3 100644
--- a/sys/dev/misc/hwpmc/hwpmc_amd.c
+++ b/sys/dev/misc/hwpmc/hwpmc_amd.c
@@ -26,11 +26,10 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/sys/dev/hwpmc/hwpmc_amd.c,v 1.21 2008/12/02 10:46:35 jkoshy Exp $
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_amd.c,v 1.21 2008/12/02 10:46:35 jkoshy Exp $");
-
 /* Support for the AMD K7 and later processors */
 
 #include <sys/param.h>
@@ -39,7 +38,6 @@ __FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_amd.c,v 1.21 2008/12/02 10:46:35 jko
 #include <sys/mutex.h>
 #include <sys/pmc.h>
 #include <sys/pmckern.h>
-#include <sys/smp.h>
 #include <sys/systm.h>
 
 #include <machine/cpu.h>
@@ -770,7 +768,7 @@ amd_pcpu_init(struct pmc_mdep *md, int cpu)
 
 	PMCDBG(MDP,INI,1,"amd-init cpu=%d", cpu);
 
-	amd_pcpu[cpu] = pac = malloc(sizeof(struct amd_cpu), M_PMC,
+	amd_pcpu[cpu] = pac = kmalloc(sizeof(struct amd_cpu), M_PMC,
 	    M_WAITOK|M_ZERO);
 
 	/*
@@ -778,7 +776,7 @@ amd_pcpu_init(struct pmc_mdep *md, int cpu)
 	 * state and initialize pointers in the MI per-cpu descriptor.
 	 */
 	pc = pmc_pcpu[cpu];
-#if	defined(__amd64__)
+#if	defined(__x86_64__)
 	classindex = PMC_MDEP_CLASS_INDEX_K8;
 #elif	defined(__i386__)
 	classindex = md->pmd_cputype == PMC_CPU_AMD_K8 ?
@@ -846,7 +844,7 @@ amd_pcpu_fini(struct pmc_mdep *md, int cpu)
 	pc = pmc_pcpu[cpu];
 	KASSERT(pc != NULL, ("[amd,%d] NULL per-cpu state", __LINE__));
 
-#if	defined(__amd64__)
+#if	defined(__x86_64__)
 	classindex = PMC_MDEP_CLASS_INDEX_K8;
 #elif	defined(__i386__)
 	classindex = md->pmd_cputype == PMC_CPU_AMD_K8 ? PMC_MDEP_CLASS_INDEX_K8 :
@@ -862,7 +860,7 @@ amd_pcpu_fini(struct pmc_mdep *md, int cpu)
 	}
 
 
-	free(pac, M_PMC);
+	kfree(pac, M_PMC);
 
 	return (0);
 }
@@ -909,7 +907,7 @@ pmc_amd_initialize(void)
 	}
 
 	if ((int) cputype == -1) {
-		(void) printf("pmc: Unknown AMD CPU.\n");
+		(void) kprintf("pmc: Unknown AMD CPU.\n");
 		return NULL;
 	}
 
@@ -921,7 +919,7 @@ pmc_amd_initialize(void)
 	 * Allocate space for pointers to PMC HW descriptors and for
 	 * the MDEP structure used by MI code.
 	 */
-	amd_pcpu = malloc(sizeof(struct amd_cpu *) * pmc_cpu_max(), M_PMC,
+	amd_pcpu = kmalloc(sizeof(struct amd_cpu *) * pmc_cpu_max(), M_PMC,
 	    M_WAITOK|M_ZERO);
 
 	/*
@@ -929,7 +927,7 @@ pmc_amd_initialize(void)
 	 * programmable PMCs.
 	 */
 	nclasses = 2;
-	pmc_mdep = malloc(sizeof(struct pmc_mdep) + nclasses * sizeof (struct pmc_classdep),
+	pmc_mdep = kmalloc(sizeof(struct pmc_mdep) + nclasses * sizeof (struct pmc_classdep),
 	    M_PMC, M_WAITOK|M_ZERO);
 
 	pmc_mdep->pmd_cputype = cputype;
@@ -953,7 +951,7 @@ pmc_amd_initialize(void)
 
 	/* fill in the correct pmc name and class */
 	for (i = 0; i < AMD_NPMCS; i++) {
-		(void) snprintf(amd_pmcdesc[i].pm_descr.pd_name,
+		(void) ksnprintf(amd_pmcdesc[i].pm_descr.pd_name,
 		    sizeof(amd_pmcdesc[i].pm_descr.pd_name), "%s-%d",
 		    name, i);
 		amd_pmcdesc[i].pm_descr.pd_class = class;
@@ -986,7 +984,7 @@ pmc_amd_initialize(void)
 
   error:
 	if (error) {
-		free(pmc_mdep, M_PMC);
+		kfree(pmc_mdep, M_PMC);
 		pmc_mdep = NULL;
 	}
 
@@ -1032,6 +1030,6 @@ pmc_amd_finalize(struct pmc_mdep *md)
 		    __LINE__));
 #endif
 
-	free(amd_pcpu, M_PMC);
+	kfree(amd_pcpu, M_PMC);
 	amd_pcpu = NULL;
 }
diff --git a/sys/dev/misc/hwpmc/hwpmc_core.c b/sys/dev/misc/hwpmc/hwpmc_core.c
index 57d4bef..b1e21db 100644
--- a/sys/dev/misc/hwpmc/hwpmc_core.c
+++ b/sys/dev/misc/hwpmc/hwpmc_core.c
@@ -22,15 +22,14 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/sys/dev/hwpmc/hwpmc_core.c,v 1.13 2010/09/05 13:31:14 fabient Exp $
  */
 
 /*
  * Intel Core, Core 2 and Atom PMCs.
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_core.c,v 1.13 2010/09/05 13:31:14 fabient Exp $");
-
 #include <sys/param.h>
 #include <sys/bus.h>
 #include <sys/pmc.h>
@@ -38,10 +37,11 @@ __FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_core.c,v 1.13 2010/09/05 13:31:14 fa
 #include <sys/systm.h>
 
 #include <machine/intr_machdep.h>
-#include <machine/apicvar.h>
 #include <machine/cpu.h>
 #include <machine/cpufunc.h>
+#include <machine/smp.h>
 #include <machine/specialreg.h>
+#include <machine_base/apic/lapic.h>
 
 #define	CORE_CPUID_REQUEST		0xA
 #define	CORE_CPUID_REQUEST_SIZE		0x4
@@ -121,7 +121,7 @@ core_pcpu_init(struct pmc_mdep *md, int cpu)
 	if (core_cputype != PMC_CPU_INTEL_CORE)
 		npmc += md->pmd_classdep[PMC_MDEP_CLASS_INDEX_IAF].pcd_num;
 
-	cc = malloc(sizeof(struct core_cpu) + npmc * sizeof(struct pmc_hw),
+	cc = kmalloc(sizeof(struct core_cpu) + npmc * sizeof(struct pmc_hw),
 	    M_PMC, M_WAITOK | M_ZERO);
 
 	core_pcpu[cpu] = cc;
@@ -181,7 +181,7 @@ core_pcpu_fini(struct pmc_mdep *md, int cpu)
 	for (n = 0; n < npmc; n++)
 		pc->pc_hwpmcs[n + core_ri] = NULL;
 
-	free(cc, M_PMC);
+	kfree(cc, M_PMC);
 
 	return (0);
 }
@@ -290,7 +290,7 @@ iaf_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
 
 	phw = &core_pcpu[cpu]->pc_corepmcs[ri + core_iaf_ri];
 
-	(void) snprintf(iaf_name, sizeof(iaf_name), "IAF-%d", ri);
+	(void) ksnprintf(iaf_name, sizeof(iaf_name), "IAF-%d", ri);
 	if ((error = copystr(iaf_name, pi->pm_name, PMC_NAME_MAX,
 	    NULL)) != 0)
 		return (error);
@@ -1756,7 +1756,7 @@ iap_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
 
 	phw = &core_pcpu[cpu]->pc_corepmcs[ri];
 
-	(void) snprintf(iap_name, sizeof(iap_name), "IAP-%d", ri);
+	(void) ksnprintf(iap_name, sizeof(iap_name), "IAP-%d", ri);
 	if ((error = copystr(iap_name, pi->pm_name, PMC_NAME_MAX,
 	    NULL)) != 0)
 		return (error);
@@ -2241,7 +2241,7 @@ pmc_core_initialize(struct pmc_mdep *md, int maxcpu)
 	PMCDBG(MDP,INI,1,"core-init pmcmask=0x%jx iafri=%d", core_pmcmask,
 	    core_iaf_ri);
 
-	core_pcpu = malloc(sizeof(struct core_cpu **) * maxcpu, M_PMC,
+	core_pcpu = kmalloc(sizeof(struct core_cpu **) * maxcpu, M_PMC,
 	    M_ZERO | M_WAITOK);
 
 	/*
@@ -2263,6 +2263,6 @@ pmc_core_finalize(struct pmc_mdep *md)
 {
 	PMCDBG(MDP,INI,1, "%s", "core-finalize");
 
-	free(core_pcpu, M_PMC);
+	kfree(core_pcpu, M_PMC);
 	core_pcpu = NULL;
 }
diff --git a/sys/dev/misc/hwpmc/hwpmc_intel.c b/sys/dev/misc/hwpmc/hwpmc_intel.c
index 29d231d..6e27102 100644
--- a/sys/dev/misc/hwpmc/hwpmc_intel.c
+++ b/sys/dev/misc/hwpmc/hwpmc_intel.c
@@ -22,15 +22,14 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/sys/dev/hwpmc/hwpmc_intel.c,v 1.13 2010/04/02 13:23:49 fabient Exp $
  */
 
 /*
  * Common code for handling Intel CPUs.
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_intel.c,v 1.13 2010/04/02 13:23:49 fabient Exp $");
-
 #include <sys/param.h>
 #include <sys/pmc.h>
 #include <sys/pmckern.h>
@@ -144,7 +143,7 @@ pmc_intel_initialize(void)
 			break;
 		}
 		break;
-#if	defined(__i386__) || defined(__amd64__)
+#if	defined(__i386__) || defined(__x86_64__)
 	case 0xF00:		/* P4 */
 		if (model >= 0 && model <= 6) /* known models */
 			cputype = PMC_CPU_INTEL_PIV;
@@ -153,11 +152,11 @@ pmc_intel_initialize(void)
 #endif
 
 	if ((int) cputype == -1) {
-		printf("pmc: Unknown Intel CPU.\n");
+		kprintf("pmc: Unknown Intel CPU.\n");
 		return (NULL);
 	}
 
-	pmc_mdep = malloc(sizeof(struct pmc_mdep) + nclasses *
+	pmc_mdep = kmalloc(sizeof(struct pmc_mdep) + nclasses *
 	    sizeof(struct pmc_classdep), M_PMC, M_WAITOK|M_ZERO);
 
 	pmc_mdep->pmd_cputype 	 = cputype;
@@ -173,7 +172,7 @@ pmc_intel_initialize(void)
 		goto error;
 
 	switch (cputype) {
-#if	defined(__i386__) || defined(__amd64__)
+#if	defined(__i386__) || defined(__x86_64__)
 		/*
 		 * Intel Core, Core 2 and Atom processors.
 		 */
@@ -236,7 +235,7 @@ pmc_intel_initialize(void)
 	/*
 	 * Init the uncore class.
 	 */
-#if	defined(__i386__) || defined(__amd64__)
+#if	defined(__i386__) || defined(__x86_64__)
 	switch (cputype) {
 		/*
 		 * Intel Corei7 and Westmere processors.
@@ -252,7 +251,7 @@ pmc_intel_initialize(void)
 
   error:
 	if (error) {
-		free(pmc_mdep, M_PMC);
+		kfree(pmc_mdep, M_PMC);
 		pmc_mdep = NULL;
 	}
 
@@ -265,7 +264,7 @@ pmc_intel_finalize(struct pmc_mdep *md)
 	pmc_tsc_finalize(md);
 
 	switch (md->pmd_cputype) {
-#if	defined(__i386__) || defined(__amd64__)
+#if	defined(__i386__) || defined(__x86_64__)
 	case PMC_CPU_INTEL_ATOM:
 	case PMC_CPU_INTEL_CORE:
 	case PMC_CPU_INTEL_CORE2:
@@ -298,7 +297,7 @@ pmc_intel_finalize(struct pmc_mdep *md)
 	/*
 	 * Uncore.
 	 */
-#if	defined(__i386__) || defined(__amd64__)
+#if	defined(__i386__) || defined(__x86_64__)
 	switch (md->pmd_cputype) {
 	case PMC_CPU_INTEL_COREI7:
 	case PMC_CPU_INTEL_WESTMERE:
diff --git a/sys/dev/misc/hwpmc/hwpmc_logging.c b/sys/dev/misc/hwpmc/hwpmc_logging.c
index ff31d5d..4496e5d 100644
--- a/sys/dev/misc/hwpmc/hwpmc_logging.c
+++ b/sys/dev/misc/hwpmc/hwpmc_logging.c
@@ -27,17 +27,16 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
+ * $FreeBSD: src/sys/dev/hwpmc/hwpmc_logging.c,v 1.18 2010/03/26 14:35:48 fabient Exp $
  */
 
 /*
  * Logging code for hwpmc(4)
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_logging.c,v 1.18 2010/03/26 14:35:48 fabient Exp $");
-
 #include <sys/param.h>
 #include <sys/file.h>
+#include <sys/file2.h>
 #include <sys/kernel.h>
 #include <sys/kthread.h>
 #include <sys/lock.h>
@@ -48,6 +47,7 @@ __FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_logging.c,v 1.18 2010/03/26 14:35:48
 #include <sys/pmclog.h>
 #include <sys/proc.h>
 #include <sys/signalvar.h>
+#include <sys/spinlock2.h>
 #include <sys/sysctl.h>
 #include <sys/systm.h>
 #include <sys/uio.h>
@@ -66,7 +66,7 @@ SYSCTL_DECL(_kern_hwpmc);
 
 static int pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "logbuffersize", &pmclog_buffer_size);
-SYSCTL_INT(_kern_hwpmc, OID_AUTO, logbuffersize, CTLFLAG_TUN|CTLFLAG_RD,
+SYSCTL_INT(_kern_hwpmc, OID_AUTO, logbuffersize, CTLFLAG_RD,
     &pmclog_buffer_size, 0, "size of log buffers in kilobytes");
 
 /*
@@ -75,7 +75,7 @@ SYSCTL_INT(_kern_hwpmc, OID_AUTO, logbuffersize, CTLFLAG_TUN|CTLFLAG_RD,
 
 static int pmc_nlogbuffers = PMC_NLOGBUFFERS;
 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nbuffers", &pmc_nlogbuffers);
-SYSCTL_INT(_kern_hwpmc, OID_AUTO, nbuffers, CTLFLAG_TUN|CTLFLAG_RD,
+SYSCTL_INT(_kern_hwpmc, OID_AUTO, nbuffers, CTLFLAG_RD,
     &pmc_nlogbuffers, 0, "number of global log buffers");
 
 /*
@@ -974,26 +974,26 @@ pmclog_process_userlog(struct pmc_owner *po, struct pmc_op_writelog *wl)
  */
 
 void
-pmclog_initialize()
+pmclog_initialize(void)
 {
 	int n;
 	struct pmclog_buffer *plb;
 
 	if (pmclog_buffer_size <= 0) {
-		(void) printf("hwpmc: tunable logbuffersize=%d must be "
+		(void) kprintf("hwpmc: tunable logbuffersize=%d must be "
 		    "greater than zero.\n", pmclog_buffer_size);
 		pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
 	}
 
 	if (pmc_nlogbuffers <= 0) {
-		(void) printf("hwpmc: tunable nlogbuffers=%d must be greater "
+		(void) kprintf("hwpmc: tunable nlogbuffers=%d must be greater "
 		    "than zero.\n", pmc_nlogbuffers);
 		pmc_nlogbuffers = PMC_NLOGBUFFERS;
 	}
 
 	/* create global pool of log buffers */
 	for (n = 0; n < pmc_nlogbuffers; n++) {
-		plb = malloc(1024 * pmclog_buffer_size, M_PMC,
+		plb = kmalloc(1024 * pmclog_buffer_size, M_PMC,
 		    M_WAITOK|M_ZERO);
 		PMCLOG_INIT_BUFFER_DESCRIPTOR(plb);
 		TAILQ_INSERT_HEAD(&pmc_bufferlist, plb, plb_next);
@@ -1010,7 +1010,7 @@ pmclog_initialize()
  */
 
 void
-pmclog_shutdown()
+pmclog_shutdown(void)
 {
 	struct pmclog_buffer *plb;
 
@@ -1019,6 +1019,6 @@ pmclog_shutdown()
 
 	while ((plb = TAILQ_FIRST(&pmc_bufferlist)) != NULL) {
 		TAILQ_REMOVE(&pmc_bufferlist, plb, plb_next);
-		free(plb, M_PMC);
+		kfree(plb, M_PMC);
 	}
 }
diff --git a/sys/dev/misc/hwpmc/hwpmc_mod.c b/sys/dev/misc/hwpmc/hwpmc_mod.c
index 53d4f63..fa03e8c 100644
--- a/sys/dev/misc/hwpmc/hwpmc_mod.c
+++ b/sys/dev/misc/hwpmc/hwpmc_mod.c
@@ -27,11 +27,9 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
+ * $FreeBSD: src/sys/dev/hwpmc/hwpmc_mod.c,v 1.52 2011/05/16 23:35:14 attilio Exp $
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_mod.c,v 1.52 2011/05/16 23:35:14 attilio Exp $");
-
 #include <sys/param.h>
 #include <sys/eventhandler.h>
 #include <sys/jail.h>
@@ -48,14 +46,15 @@ __FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_mod.c,v 1.52 2011/05/16 23:35:14 att
 #include <sys/pmclog.h>
 #include <sys/priv.h>
 #include <sys/proc.h>
+#include <sys/ptrace.h>		/* for PT_ATTACH */
 #include <sys/queue.h>
 #include <sys/resourcevar.h>
 #include <sys/sched.h>
 #include <sys/signalvar.h>
-#include <sys/smp.h>
-#include <sys/sx.h>
+#include <sys/spinlock2.h>
 #include <sys/sysctl.h>
 #include <sys/sysent.h>
+#include <sys/sysref2.h>
 #include <sys/systm.h>
 #include <sys/vnode.h>
 
@@ -227,7 +226,7 @@ SYSCTL_NODE(_kern, OID_AUTO, hwpmc, CTLFLAG_RW, 0, "HWPMC parameters");
 
 static int pmc_callchaindepth = PMC_CALLCHAIN_DEPTH;
 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "callchaindepth", &pmc_callchaindepth);
-SYSCTL_INT(_kern_hwpmc, OID_AUTO, callchaindepth, CTLFLAG_TUN|CTLFLAG_RD,
+SYSCTL_INT(_kern_hwpmc, OID_AUTO, callchaindepth, CTLFLAG_RD,
     &pmc_callchaindepth, 0, "depth of call chain records");
 
 #ifdef	DEBUG
@@ -236,7 +235,7 @@ char	pmc_debugstr[PMC_DEBUG_STRSIZE];
 TUNABLE_STR(PMC_SYSCTL_NAME_PREFIX "debugflags", pmc_debugstr,
     sizeof(pmc_debugstr));
 SYSCTL_PROC(_kern_hwpmc, OID_AUTO, debugflags,
-    CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_TUN,
+    CTLTYPE_STRING|CTLFLAG_RW,
     0, 0, pmc_debugflags_sysctl_handler, "A", "debug flags");
 #endif
 
@@ -247,7 +246,7 @@ SYSCTL_PROC(_kern_hwpmc, OID_AUTO, debugflags,
 
 static int pmc_hashsize = PMC_HASH_SIZE;
 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "hashsize", &pmc_hashsize);
-SYSCTL_INT(_kern_hwpmc, OID_AUTO, hashsize, CTLFLAG_TUN|CTLFLAG_RD,
+SYSCTL_INT(_kern_hwpmc, OID_AUTO, hashsize, CTLFLAG_RD,
     &pmc_hashsize, 0, "rows in hash tables");
 
 /*
@@ -256,7 +255,7 @@ SYSCTL_INT(_kern_hwpmc, OID_AUTO, hashsize, CTLFLAG_TUN|CTLFLAG_RD,
 
 static int pmc_nsamples = PMC_NSAMPLES;
 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nsamples", &pmc_nsamples);
-SYSCTL_INT(_kern_hwpmc, OID_AUTO, nsamples, CTLFLAG_TUN|CTLFLAG_RD,
+SYSCTL_INT(_kern_hwpmc, OID_AUTO, nsamples, CTLFLAG_RD,
     &pmc_nsamples, 0, "number of PC samples per CPU");
 
 
@@ -343,7 +342,7 @@ pmc_debugflags_parse(char *newstr, char *fence)
 	int error, found, *newbits, tmp;
 	size_t kwlen;
 
-	tmpflags = malloc(sizeof(*tmpflags), M_PMC, M_WAITOK|M_ZERO);
+	tmpflags = kmalloc(sizeof(*tmpflags), M_PMC, M_WAITOK|M_ZERO);
 
 	p = newstr;
 	error = 0;
@@ -461,7 +460,7 @@ pmc_debugflags_parse(char *newstr, char *fence)
 	bcopy(tmpflags, &pmc_debugflags, sizeof(pmc_debugflags));
 
  done:
-	free(tmpflags, M_PMC);
+	kfree(tmpflags, M_PMC);
 	return error;
 }
 
@@ -475,7 +474,7 @@ pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS)
 	(void) arg1; (void) arg2; /* unused parameters */
 
 	n = sizeof(pmc_debugstr);
-	newstr = malloc(n, M_PMC, M_WAITOK|M_ZERO);
+	newstr = kmalloc(n, M_PMC, M_WAITOK|M_ZERO);
 	(void) strlcpy(newstr, pmc_debugstr, n);
 
 	error = sysctl_handle_string(oidp, newstr, n, req);
@@ -488,7 +487,7 @@ pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS)
 			    sizeof(pmc_debugstr));
 	}
 
-	free(newstr, M_PMC);
+	kfree(newstr, M_PMC);
 
 	return error;
 }
@@ -739,7 +738,7 @@ pmc_remove_owner(struct pmc_owner *po)
 	LIST_REMOVE(po, po_next);
 
 	/* release all owned PMC descriptors */
-	LIST_FOREACH_SAFE(pm, &po->po_pmcs, pm_next, tmp) {
+	LIST_FOREACH_MUTABLE(pm, &po->po_pmcs, pm_next, tmp) {
 		PMCDBG(OWN,ORM,2, "pmc=%p", pm);
 		KASSERT(pm->pm_owner == po,
 		    ("[pmc,%d] owner %p != po %p", __LINE__, pm->pm_owner, po));
@@ -813,7 +812,7 @@ pmc_link_target_process(struct pmc *pm, struct pmc_process *pp)
 				__LINE__, pp, pm));
 #endif
 
-	pt = malloc(sizeof(struct pmc_target), M_PMC, M_WAITOK|M_ZERO);
+	pt = kmalloc(sizeof(struct pmc_target), M_PMC, M_WAITOK|M_ZERO);
 	pt->pt_process = pp;
 
 	LIST_INSERT_HEAD(&pm->pm_targets, pt, pt_next);
@@ -883,7 +882,7 @@ pmc_unlink_target_process(struct pmc *pm, struct pmc_process *pp)
 		    "in pmc %p", __LINE__, pp->pp_proc, pp, pm));
 
 	LIST_REMOVE(ptgt, pt_next);
-	free(ptgt, M_PMC);
+	kfree(ptgt, M_PMC);
 
 	/* if the PMC now lacks targets, send the owner a SIGIO */
 	if (LIST_EMPTY(&pm->pm_targets)) {
@@ -1006,7 +1005,7 @@ pmc_attach_one_process(struct proc *p, struct pmc *pm)
 		} else
 			pmclog_process_pmcattach(pm, p->p_pid, fullpath);
 		if (freepath)
-			free(freepath, M_TEMP);
+			kfree(freepath, M_TEMP);
 		if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
 			pmc_log_process_mappings(pm->pm_owner, p);
 	}
@@ -1127,7 +1126,7 @@ pmc_detach_one_process(struct proc *p, struct pmc *pm, int flags)
 	pmc_remove_process_descriptor(pp);
 
 	if (flags & PMC_FLAG_REMOVE)
-		free(pp, M_PMC);
+		kfree(pp, M_PMC);
 
 	PROC_LOCK(p);
 	p->p_flag &= ~P_HWPMC;
@@ -1550,7 +1549,7 @@ pmc_process_mmap(struct thread *td, struct pmckern_map_in *pkm)
 
   done:
 	if (freepath)
-		free(freepath, M_TEMP);
+		kfree(freepath, M_TEMP);
 }
 
 
@@ -1614,7 +1613,7 @@ pmc_log_kernel_mappings(struct pmc *pm)
 		pmclog_process_map_in(po, (pid_t) -1, km->pm_address,
 		    km->pm_file);
 	}
-	free(kmbase, M_LINKER);
+	kfree(kmbase, M_LINKER);
 
 	po->po_flags |= PMC_PO_INITIAL_MAPPINGS_DONE;
 }
@@ -1743,7 +1742,7 @@ pmc_log_process_mappings(struct pmc_owner *po, struct proc *p)
 		vp = NULL;
 		pmclog_process_map_in(po, p->p_pid, start_addr, fullpath);
 		if (freepath)
-			free(freepath, M_TEMP);
+			kfree(freepath, M_TEMP);
 
 		vm_map_lock_read(map);
 
@@ -1881,7 +1880,7 @@ pmc_hook_handler(struct thread *td, int function, void *arg)
 
 		if (!is_using_hwpmcs) {
 			if (freepath)
-				free(freepath, M_TEMP);
+				kfree(freepath, M_TEMP);
 			break;
 		}
 
@@ -1901,7 +1900,7 @@ pmc_hook_handler(struct thread *td, int function, void *arg)
 		 */
 		if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) {
 			if (freepath)
-				free(freepath, M_TEMP);
+				kfree(freepath, M_TEMP);
 			break;
 		}
 
@@ -1921,7 +1920,7 @@ pmc_hook_handler(struct thread *td, int function, void *arg)
 			}
 
 		if (freepath)
-			free(freepath, M_TEMP);
+			kfree(freepath, M_TEMP);
 
 
 		PMCDBG(PRC,EXC,1, "exec proc=%p (%d, %s) cred-changed=%d",
@@ -1954,7 +1953,7 @@ pmc_hook_handler(struct thread *td, int function, void *arg)
 
 		if (pp->pp_refcnt == 0) {
 			pmc_remove_process_descriptor(pp);
-			free(pp, M_PMC);
+			kfree(pp, M_PMC);
 			break;
 		}
 
@@ -2053,7 +2052,7 @@ pmc_allocate_owner_descriptor(struct proc *p)
 	poh = &pmc_ownerhash[hindex];
 
 	/* allocate space for N pointers and one descriptor struct */
-	po = malloc(sizeof(struct pmc_owner), M_PMC, M_WAITOK|M_ZERO);
+	po = kmalloc(sizeof(struct pmc_owner), M_PMC, M_WAITOK|M_ZERO);
 	po->po_sscount = po->po_error = po->po_flags = po->po_logprocmaps = 0;
 	po->po_file  = NULL;
 	po->po_owner = p;
@@ -2078,7 +2077,7 @@ pmc_destroy_owner_descriptor(struct pmc_owner *po)
 	    po, po->po_owner, po->po_owner->p_pid, po->po_owner->p_comm);
 
 	mtx_destroy(&po->po_mtx);
-	free(po, M_PMC);
+	kfree(po, M_PMC);
 }
 
 /*
@@ -2100,10 +2099,10 @@ pmc_find_process_descriptor(struct proc *p, uint32_t mode)
 
 	/*
 	 * Pre-allocate memory in the FIND_ALLOCATE case since we
-	 * cannot call malloc(9) once we hold a spin lock.
+	 * cannot call kmalloc(9) once we hold a spin lock.
 	 */
 	if (mode & PMC_FLAG_ALLOCATE)
-		ppnew = malloc(sizeof(struct pmc_process) + md->pmd_npmc *
+		ppnew = kmalloc(sizeof(struct pmc_process) + md->pmd_npmc *
 		    sizeof(struct pmc_targetstate), M_PMC, M_WAITOK|M_ZERO);
 
 	mtx_lock_spin(&pmc_processhash_mtx);
@@ -2124,7 +2123,7 @@ pmc_find_process_descriptor(struct proc *p, uint32_t mode)
 	mtx_unlock_spin(&pmc_processhash_mtx);
 
 	if (pp != NULL && ppnew != NULL)
-		free(ppnew, M_PMC);
+		kfree(ppnew, M_PMC);
 
 	return pp;
 }
@@ -2183,7 +2182,7 @@ pmc_allocate_pmc_descriptor(void)
 {
 	struct pmc *pmc;
 
-	pmc = malloc(sizeof(struct pmc), M_PMC, M_WAITOK|M_ZERO);
+	pmc = kmalloc(sizeof(struct pmc), M_PMC, M_WAITOK|M_ZERO);
 
 	if (pmc != NULL) {
 		pmc->pm_owner = NULL;
@@ -2362,7 +2361,7 @@ pmc_release_pmc_descriptor(struct pmc *pm)
 		 * it from the hash table.  The module-wide SX lock
 		 * protects us from races.
 		 */
-		LIST_FOREACH_SAFE(ptgt, &pm->pm_targets, pt_next, tmp) {
+		LIST_FOREACH_MUTABLE(ptgt, &pm->pm_targets, pt_next, tmp) {
 			pp = ptgt->pt_process;
 			pmc_unlink_target_process(pm, pp); /* frees 'ptgt' */
 
@@ -2375,7 +2374,7 @@ pmc_release_pmc_descriptor(struct pmc *pm)
 
 			if (pp->pp_refcnt == 0) {
 				pmc_remove_process_descriptor(pp);
-				free(pp, M_PMC);
+				kfree(pp, M_PMC);
 			}
 		}
 
@@ -3032,7 +3031,7 @@ pmc_syscall_handler(struct thread *td, void *syscall_args)
 		npmc = md->pmd_npmc;
 
 		pmcinfo_size = npmc * sizeof(struct pmc_info);
-		pmcinfo = malloc(pmcinfo_size, M_PMC, M_WAITOK);
+		pmcinfo = kmalloc(pmcinfo_size, M_PMC, M_WAITOK);
 
 		p = pmcinfo;
 
@@ -3080,7 +3079,7 @@ pmc_syscall_handler(struct thread *td, void *syscall_args)
 		if (error == 0)
 			error = copyout(pmcinfo, &gpi->pm_pmcs, pmcinfo_size);
 
-		free(pmcinfo, M_PMC);
+		kfree(pmcinfo, M_PMC);
 	}
 	break;
 
@@ -3345,7 +3344,7 @@ pmc_syscall_handler(struct thread *td, void *syscall_args)
 
 		if (n == (int) md->pmd_npmc) {
 			pmc_destroy_pmc_descriptor(pmc);
-			free(pmc, M_PMC);
+			kfree(pmc, M_PMC);
 			pmc = NULL;
 			error = EINVAL;
 			break;
@@ -3381,7 +3380,7 @@ pmc_syscall_handler(struct thread *td, void *syscall_args)
 			    (error = pcd->pcd_config_pmc(cpu, adjri, pmc)) != 0) {
 				(void) pcd->pcd_release_pmc(cpu, adjri, pmc);
 				pmc_destroy_pmc_descriptor(pmc);
-				free(pmc, M_PMC);
+				kfree(pmc, M_PMC);
 				pmc = NULL;
 				pmc_restore_cpu_binding(&pb);
 				error = EPERM;
@@ -3409,7 +3408,7 @@ pmc_syscall_handler(struct thread *td, void *syscall_args)
 		if ((error =
 		    pmc_register_owner(curthread->td_proc, pmc)) != 0) {
 			pmc_release_pmc_descriptor(pmc);
-			free(pmc, M_PMC);
+			kfree(pmc, M_PMC);
 			pmc = NULL;
 			break;
 		}
@@ -3653,7 +3652,7 @@ pmc_syscall_handler(struct thread *td, void *syscall_args)
 		pmc_release_pmc_descriptor(pm);
 		pmc_maybe_remove_owner(po);
 
-		free(pm, M_PMC);
+		kfree(pm, M_PMC);
 	}
 	break;
 
@@ -4445,7 +4444,7 @@ pmc_process_exit(void *arg __unused, struct proc *p)
 					pmclog_process_procexit(pm, pp);
 				pmc_unlink_target_process(pm, pp);
 			}
-		free(pp, M_PMC);
+		kfree(pp, M_PMC);
 
 	} else
 		critical_exit(); /* pp == NULL */
@@ -4592,10 +4591,10 @@ pmc_initialize(void)
 	/* check kernel version */
 	if (pmc_kernel_version != PMC_VERSION) {
 		if (pmc_kernel_version == 0)
-			printf("hwpmc: this kernel has not been compiled with "
+			kprintf("hwpmc: this kernel has not been compiled with "
 			    "'options HWPMC_HOOKS'.\n");
 		else
-			printf("hwpmc: kernel version (0x%x) does not match "
+			kprintf("hwpmc: kernel version (0x%x) does not match "
 			    "module version (0x%x).\n", pmc_kernel_version,
 			    PMC_VERSION);
 		return EPROGMISMATCH;
@@ -4606,20 +4605,20 @@ pmc_initialize(void)
 	 */
 
 	if (pmc_hashsize <= 0) {
-		(void) printf("hwpmc: tunable \"hashsize\"=%d must be "
+		(void) kprintf("hwpmc: tunable \"hashsize\"=%d must be "
 		    "greater than zero.\n", pmc_hashsize);
 		pmc_hashsize = PMC_HASH_SIZE;
 	}
 
 	if (pmc_nsamples <= 0 || pmc_nsamples > 65535) {
-		(void) printf("hwpmc: tunable \"nsamples\"=%d out of "
+		(void) kprintf("hwpmc: tunable \"nsamples\"=%d out of "
 		    "range.\n", pmc_nsamples);
 		pmc_nsamples = PMC_NSAMPLES;
 	}
 
 	if (pmc_callchaindepth <= 0 ||
 	    pmc_callchaindepth > PMC_CALLCHAIN_DEPTH_MAX) {
-		(void) printf("hwpmc: tunable \"callchaindepth\"=%d out of "
+		(void) kprintf("hwpmc: tunable \"callchaindepth\"=%d out of "
 		    "range.\n", pmc_callchaindepth);
 		pmc_callchaindepth = PMC_CALLCHAIN_DEPTH;
 	}
@@ -4633,7 +4632,7 @@ pmc_initialize(void)
 	    ("[pmc,%d] no classes or pmcs", __LINE__));
 
 	/* Compute the map from row-indices to classdep pointers. */
-	pmc_rowindex_to_classdep = malloc(sizeof(struct pmc_classdep *) *
+	pmc_rowindex_to_classdep = kmalloc(sizeof(struct pmc_classdep *) *
 	    md->pmd_npmc, M_PMC, M_WAITOK|M_ZERO);
 
 	for (n = 0; n < md->pmd_npmc; n++)
@@ -4651,11 +4650,11 @@ pmc_initialize(void)
 	maxcpu = pmc_cpu_max();
 
 	/* allocate space for the per-cpu array */
-	pmc_pcpu = malloc(maxcpu * sizeof(struct pmc_cpu *), M_PMC,
+	pmc_pcpu = kmalloc(maxcpu * sizeof(struct pmc_cpu *), M_PMC,
 	    M_WAITOK|M_ZERO);
 
 	/* per-cpu 'saved values' for managing process-mode PMCs */
-	pmc_pcpu_saved = malloc(sizeof(pmc_value_t) * maxcpu * md->pmd_npmc,
+	pmc_pcpu_saved = kmalloc(sizeof(pmc_value_t) * maxcpu * md->pmd_npmc,
 	    M_PMC, M_WAITOK);
 
 	/* Perform CPU-dependent initialization. */
@@ -4665,7 +4664,7 @@ pmc_initialize(void)
 		if (!pmc_cpu_is_active(cpu))
 			continue;
 		pmc_select_cpu(cpu);
-		pmc_pcpu[cpu] = malloc(sizeof(struct pmc_cpu) +
+		pmc_pcpu[cpu] = kmalloc(sizeof(struct pmc_cpu) +
 		    md->pmd_npmc * sizeof(struct pmc_hw *), M_PMC,
 		    M_WAITOK|M_ZERO);
 		if (md->pmd_pcpu_init)
@@ -4683,7 +4682,7 @@ pmc_initialize(void)
 		if (!pmc_cpu_is_active(cpu))
 			continue;
 
-		sb = malloc(sizeof(struct pmc_samplebuffer) +
+		sb = kmalloc(sizeof(struct pmc_samplebuffer) +
 		    pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
 		    M_WAITOK|M_ZERO);
 		sb->ps_read = sb->ps_write = sb->ps_samples;
@@ -4692,7 +4691,7 @@ pmc_initialize(void)
 		KASSERT(pmc_pcpu[cpu] != NULL,
 		    ("[pmc,%d] cpu=%d Null per-cpu data", __LINE__, cpu));
 
-		sb->ps_callchains = malloc(pmc_callchaindepth * pmc_nsamples *
+		sb->ps_callchains = kmalloc(pmc_callchaindepth * pmc_nsamples *
 		    sizeof(uintptr_t), M_PMC, M_WAITOK|M_ZERO);
 
 		for (n = 0, ps = sb->ps_samples; n < pmc_nsamples; n++, ps++)
@@ -4703,7 +4702,7 @@ pmc_initialize(void)
 	}
 
 	/* allocate space for the row disposition array */
-	pmc_pmcdisp = malloc(sizeof(enum pmc_mode) * md->pmd_npmc,
+	pmc_pmcdisp = kmalloc(sizeof(enum pmc_mode) * md->pmd_npmc,
 	    M_PMC, M_WAITOK|M_ZERO);
 
 	KASSERT(pmc_pmcdisp != NULL,
@@ -4747,10 +4746,10 @@ pmc_initialize(void)
 	pmc_hook = pmc_hook_handler;
 
 	if (error == 0) {
-		printf(PMC_MODULE_NAME ":");
+		kprintf(PMC_MODULE_NAME ":");
 		for (n = 0; n < (int) md->pmd_nclass; n++) {
 			pcd = &md->pmd_classdep[n];
-			printf(" %s/%d/%d/0x%b",
+			kprintf(" %s/%d/%d/0x%b",
 			    pmc_name_of_pmcclass[pcd->pcd_class],
 			    pcd->pcd_num,
 			    pcd->pcd_width,
@@ -4760,7 +4759,7 @@ pmc_initialize(void)
 			    "\6REA\7WRI\10INV\11QUA\12PRC"
 			    "\13TAG\14CSC");
 		}
-		printf("\n");
+		kprintf("\n");
 	}
 
 	return (error);
@@ -4802,7 +4801,7 @@ pmc_cleanup(void)
 		for (ph = pmc_ownerhash;
 		     ph <= &pmc_ownerhash[pmc_ownerhashmask];
 		     ph++) {
-			LIST_FOREACH_SAFE(po, ph, po_next, tmp) {
+			LIST_FOREACH_MUTABLE(po, ph, po_next, tmp) {
 				pmc_remove_owner(po);
 
 				/* send SIGBUS to owner processes */
@@ -4871,7 +4870,7 @@ pmc_cleanup(void)
 
 		pmc_md_finalize(md);
 
-		free(md, M_PMC);
+		kfree(md, M_PMC);
 		md = NULL;
 		pmc_restore_cpu_binding(&pb);
 	}
@@ -4883,24 +4882,24 @@ pmc_cleanup(void)
 		KASSERT(pmc_pcpu[cpu]->pc_sb != NULL,
 		    ("[pmc,%d] Null cpu sample buffer cpu=%d", __LINE__,
 			cpu));
-		free(pmc_pcpu[cpu]->pc_sb->ps_callchains, M_PMC);
-		free(pmc_pcpu[cpu]->pc_sb, M_PMC);
-		free(pmc_pcpu[cpu], M_PMC);
+		kfree(pmc_pcpu[cpu]->pc_sb->ps_callchains, M_PMC);
+		kfree(pmc_pcpu[cpu]->pc_sb, M_PMC);
+		kfree(pmc_pcpu[cpu], M_PMC);
 	}
 
-	free(pmc_pcpu, M_PMC);
+	kfree(pmc_pcpu, M_PMC);
 	pmc_pcpu = NULL;
 
-	free(pmc_pcpu_saved, M_PMC);
+	kfree(pmc_pcpu_saved, M_PMC);
 	pmc_pcpu_saved = NULL;
 
 	if (pmc_pmcdisp) {
-		free(pmc_pmcdisp, M_PMC);
+		kfree(pmc_pmcdisp, M_PMC);
 		pmc_pmcdisp = NULL;
 	}
 
 	if (pmc_rowindex_to_classdep) {
-		free(pmc_rowindex_to_classdep, M_PMC);
+		kfree(pmc_rowindex_to_classdep, M_PMC);
 		pmc_rowindex_to_classdep = NULL;
 	}
 
diff --git a/sys/dev/misc/hwpmc/hwpmc_pentium.c b/sys/dev/misc/hwpmc/hwpmc_pentium.c
index ba679ea..6571b05 100644
--- a/sys/dev/misc/hwpmc/hwpmc_pentium.c
+++ b/sys/dev/misc/hwpmc/hwpmc_pentium.c
@@ -22,17 +22,15 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/sys/dev/hwpmc/hwpmc_pentium.c,v 1.5 2008/11/09 17:37:54 jkoshy Exp $
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_pentium.c,v 1.5 2008/11/09 17:37:54 jkoshy Exp $");
-
 #include <sys/param.h>
 #include <sys/lock.h>
 #include <sys/mutex.h>
 #include <sys/pmc.h>
 #include <sys/pmckern.h>
-#include <sys/smp.h>
 #include <sys/systm.h>
 
 #include <machine/cpufunc.h>
diff --git a/sys/dev/misc/hwpmc/hwpmc_piv.c b/sys/dev/misc/hwpmc/hwpmc_piv.c
index 5d74cdb..6a8c5ae 100644
--- a/sys/dev/misc/hwpmc/hwpmc_piv.c
+++ b/sys/dev/misc/hwpmc/hwpmc_piv.c
@@ -26,27 +26,27 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/sys/dev/hwpmc/hwpmc_piv.c,v 1.21 2009/08/14 21:05:08 jhb Exp $
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_piv.c,v 1.21 2009/08/14 21:05:08 jhb Exp $");
-
 #include <sys/param.h>
 #include <sys/bus.h>
 #include <sys/lock.h>
 #include <sys/mutex.h>
 #include <sys/pmc.h>
 #include <sys/pmckern.h>
-#include <sys/smp.h>
+#include <sys/spinlock2.h>
 #include <sys/systm.h>
 
 #include <machine/intr_machdep.h>
-#include <machine/apicvar.h>
 #include <machine/cpu.h>
 #include <machine/cpufunc.h>
 #include <machine/cputypes.h>
 #include <machine/md_var.h>
+#include <machine/smp.h>
 #include <machine/specialreg.h>
+#include <machine_base/apic/lapic.h>
 
 /*
  * PENTIUM 4 SUPPORT
@@ -600,7 +600,7 @@ p4_pcpu_init(struct pmc_mdep *md, int cpu)
 		return (0);
 	}
 
-	p4c = malloc(sizeof(struct p4_cpu), M_PMC, M_WAITOK|M_ZERO);
+	p4c = kmalloc(sizeof(struct p4_cpu), M_PMC, M_WAITOK|M_ZERO);
 
 	if (p4c == NULL)
 		return (ENOMEM);
@@ -661,7 +661,7 @@ p4_pcpu_fini(struct pmc_mdep *md, int cpu)
 
 	mtx_destroy(&p4c->pc_mtx);
 
-	free(p4c, M_PMC);
+	kfree(p4c, M_PMC);
 
 	p4_pcpu[cpu] = NULL;
 
@@ -1620,7 +1620,7 @@ pmc_p4_initialize(struct pmc_mdep *md, int ncpus)
 	PMCDBG(MDP,INI,1, "%s", "p4-initialize");
 
 	/* Allocate space for pointers to per-cpu descriptors. */
-	p4_pcpu = malloc(sizeof(struct p4_cpu **) * ncpus, M_PMC,
+	p4_pcpu = kmalloc(sizeof(struct p4_cpu **) * ncpus, M_PMC,
 	    M_ZERO|M_WAITOK);
 
 	/* Fill in the class dependent descriptor. */
@@ -1693,6 +1693,6 @@ pmc_p4_finalize(struct pmc_mdep *md)
 		    __LINE__, i));
 #endif
 
-	free(p4_pcpu, M_PMC);
+	kfree(p4_pcpu, M_PMC);
 	p4_pcpu = NULL;
 }
diff --git a/sys/dev/misc/hwpmc/hwpmc_ppro.c b/sys/dev/misc/hwpmc/hwpmc_ppro.c
index 84d301e..3341359 100644
--- a/sys/dev/misc/hwpmc/hwpmc_ppro.c
+++ b/sys/dev/misc/hwpmc/hwpmc_ppro.c
@@ -26,28 +26,27 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/sys/dev/hwpmc/hwpmc_ppro.c,v 1.17 2009/08/14 21:05:08 jhb Exp $
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_ppro.c,v 1.17 2009/08/14 21:05:08 jhb Exp $");
-
 #include <sys/param.h>
 #include <sys/bus.h>
 #include <sys/lock.h>
 #include <sys/mutex.h>
 #include <sys/pmc.h>
 #include <sys/pmckern.h>
-#include <sys/smp.h>
 #include <sys/systm.h>
 
 #include <machine/intr_machdep.h>
-#include <machine/apicvar.h>
 #include <machine/cpu.h>
 #include <machine/cpufunc.h>
 #include <machine/cputypes.h>
 #include <machine/md_var.h>
 #include <machine/pmc_mdep.h>
+#include <machine/smp.h>
 #include <machine/specialreg.h>
+#include <machine_base/apic/lapic.h>
 
 /*
  * PENTIUM PRO SUPPORT
@@ -341,7 +340,7 @@ p6_pcpu_init(struct pmc_mdep *md, int cpu)
 
 	PMCDBG(MDP,INI,0,"p6-init cpu=%d", cpu);
 
-	p6c = malloc(sizeof (struct p6_cpu), M_PMC, M_WAITOK|M_ZERO);
+	p6c = kmalloc(sizeof (struct p6_cpu), M_PMC, M_WAITOK|M_ZERO);
 	pc = pmc_pcpu[cpu];
 
 	KASSERT(pc != NULL, ("[p6,%d] cpu %d null per-cpu", __LINE__, cpu));
@@ -378,7 +377,7 @@ p6_pcpu_fini(struct pmc_mdep *md, int cpu)
 
 	KASSERT(p6c != NULL, ("[p6,%d] null pcpu", __LINE__));
 
-	free(p6c, M_PMC);
+	kfree(p6c, M_PMC);
 
 	first_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_P6].pcd_ri;
 	pc = pmc_pcpu[cpu];
@@ -791,7 +790,7 @@ pmc_p6_initialize(struct pmc_mdep *md, int ncpus)
 	PMCDBG(MDP,INI,1, "%s", "p6-initialize");
 
 	/* Allocate space for pointers to per-cpu descriptors. */
-	p6_pcpu = malloc(sizeof(struct p6_cpu **) * ncpus, M_PMC,
+	p6_pcpu = kmalloc(sizeof(struct p6_cpu **) * ncpus, M_PMC,
 	    M_ZERO|M_WAITOK);
 
 	/* Fill in the class dependent descriptor. */
@@ -861,6 +860,6 @@ pmc_p6_finalize(struct pmc_mdep *md)
 		    __LINE__, i));
 #endif
 
-	free(p6_pcpu, M_PMC);
+	kfree(p6_pcpu, M_PMC);
 	p6_pcpu = NULL;
 }
diff --git a/sys/dev/misc/hwpmc/hwpmc_tsc.c b/sys/dev/misc/hwpmc/hwpmc_tsc.c
index bb3c00e..d7b20d0 100644
--- a/sys/dev/misc/hwpmc/hwpmc_tsc.c
+++ b/sys/dev/misc/hwpmc/hwpmc_tsc.c
@@ -22,11 +22,10 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/sys/dev/hwpmc/hwpmc_tsc.c,v 1.1 2008/11/09 17:37:54 jkoshy Exp $
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_tsc.c,v 1.1 2008/11/09 17:37:54 jkoshy Exp $");
-
 #include <sys/param.h>
 #include <sys/pmc.h>
 #include <sys/pmckern.h>
@@ -185,7 +184,7 @@ tsc_pcpu_fini(struct pmc_mdep *md, int cpu)
 	    ("[tsc,%d] illegal cpu %d", __LINE__, cpu));
 	KASSERT(tsc_pcpu[cpu] != NULL, ("[tsc,%d] null pcpu", __LINE__));
 
-	free(tsc_pcpu[cpu], M_PMC);
+	kfree(tsc_pcpu[cpu], M_PMC);
 	tsc_pcpu[cpu] = NULL;
 
 	ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_TSC].pcd_ri;
@@ -213,7 +212,7 @@ tsc_pcpu_init(struct pmc_mdep *md, int cpu)
 	KASSERT(tsc_pcpu[cpu] == NULL, ("[tsc,%d] non-null per-cpu",
 	    __LINE__));
 
-	tsc_pc = malloc(sizeof(struct tsc_cpu), M_PMC, M_WAITOK|M_ZERO);
+	tsc_pc = kmalloc(sizeof(struct tsc_cpu), M_PMC, M_WAITOK|M_ZERO);
 
 	tsc_pc->tc_hw.phw_state = PMC_PHW_FLAG_IS_ENABLED |
 	    PMC_PHW_CPU_TO_STATE(cpu) | PMC_PHW_INDEX_TO_STATE(0) |
@@ -336,7 +335,7 @@ pmc_tsc_initialize(struct pmc_mdep *md, int maxcpu)
 	KASSERT(md->pmd_nclass >= 1, ("[tsc,%d] dubious md->nclass %d",
 	    __LINE__, md->pmd_nclass));
 
-	tsc_pcpu = malloc(sizeof(struct tsc_cpu *) * maxcpu, M_PMC,
+	tsc_pcpu = kmalloc(sizeof(struct tsc_cpu *) * maxcpu, M_PMC,
 	    M_ZERO|M_WAITOK);
 
 	pcd = &md->pmd_classdep[PMC_MDEP_CLASS_INDEX_TSC];
@@ -383,6 +382,6 @@ pmc_tsc_finalize(struct pmc_mdep *md)
 	(void) md;
 #endif
 
-	free(tsc_pcpu, M_PMC);
+	kfree(tsc_pcpu, M_PMC);
 	tsc_pcpu = NULL;
 }
diff --git a/sys/dev/misc/hwpmc/hwpmc_uncore.c b/sys/dev/misc/hwpmc/hwpmc_uncore.c
index ff3a2f2..d3241f8 100644
--- a/sys/dev/misc/hwpmc/hwpmc_uncore.c
+++ b/sys/dev/misc/hwpmc/hwpmc_uncore.c
@@ -22,15 +22,14 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/sys/dev/hwpmc/hwpmc_uncore.c,v 1.1 2010/04/02 13:23:49 fabient Exp $
  */
 
 /*
  * Intel Uncore PMCs.
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_uncore.c,v 1.1 2010/04/02 13:23:49 fabient Exp $");
-
 #include <sys/param.h>
 #include <sys/bus.h>
 #include <sys/pmc.h>
@@ -38,7 +37,6 @@ __FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_uncore.c,v 1.1 2010/04/02 13:23:49 f
 #include <sys/systm.h>
 
 #include <machine/intr_machdep.h>
-#include <machine/apicvar.h>
 #include <machine/cpu.h>
 #include <machine/cpufunc.h>
 #include <machine/specialreg.h>
@@ -95,7 +93,7 @@ uncore_pcpu_init(struct pmc_mdep *md, int cpu)
 	npmc = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCP].pcd_num;
 	npmc += md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCF].pcd_num;
 
-	cc = malloc(sizeof(struct uncore_cpu) + npmc * sizeof(struct pmc_hw),
+	cc = kmalloc(sizeof(struct uncore_cpu) + npmc * sizeof(struct pmc_hw),
 	    M_PMC, M_WAITOK | M_ZERO);
 
 	uncore_pcpu[cpu] = cc;
@@ -149,7 +147,7 @@ uncore_pcpu_fini(struct pmc_mdep *md, int cpu)
 	for (n = 0; n < npmc; n++)
 		pc->pc_hwpmcs[n + uncore_ri] = NULL;
 
-	free(cc, M_PMC);
+	kfree(cc, M_PMC);
 
 	return (0);
 }
@@ -234,7 +232,7 @@ ucf_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
 
 	phw = &uncore_pcpu[cpu]->pc_uncorepmcs[ri + uncore_ucf_ri];
 
-	(void) snprintf(ucf_name, sizeof(ucf_name), "UCF-%d", ri);
+	(void) ksnprintf(ucf_name, sizeof(ucf_name), "UCF-%d", ri);
 	if ((error = copystr(ucf_name, pi->pm_name, PMC_NAME_MAX,
 	    NULL)) != 0)
 		return (error);
@@ -865,7 +863,7 @@ ucp_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
 
 	phw = &uncore_pcpu[cpu]->pc_uncorepmcs[ri];
 
-	(void) snprintf(ucp_name, sizeof(ucp_name), "UCP-%d", ri);
+	(void) ksnprintf(ucp_name, sizeof(ucp_name), "UCP-%d", ri);
 	if ((error = copystr(ucp_name, pi->pm_name, PMC_NAME_MAX,
 	    NULL)) != 0)
 		return (error);
@@ -1105,7 +1103,7 @@ pmc_uncore_initialize(struct pmc_mdep *md, int maxcpu)
 	PMCDBG(MDP,INI,1,"uncore-init pmcmask=0x%jx ucfri=%d", uncore_pmcmask,
 	    uncore_ucf_ri);
 
-	uncore_pcpu = malloc(sizeof(struct uncore_cpu **) * maxcpu, M_PMC,
+	uncore_pcpu = kmalloc(sizeof(struct uncore_cpu **) * maxcpu, M_PMC,
 	    M_ZERO | M_WAITOK);
 
 	return (0);
@@ -1116,6 +1114,6 @@ pmc_uncore_finalize(struct pmc_mdep *md)
 {
 	PMCDBG(MDP,INI,1, "%s", "uncore-finalize");
 
-	free(uncore_pcpu, M_PMC);
+	kfree(uncore_pcpu, M_PMC);
 	uncore_pcpu = NULL;
 }
diff --git a/sys/dev/misc/hwpmc/hwpmc_x86.c b/sys/dev/misc/hwpmc/hwpmc_x86.c
index c77d6e3..311bdb3 100644
--- a/sys/dev/misc/hwpmc/hwpmc_x86.c
+++ b/sys/dev/misc/hwpmc/hwpmc_x86.c
@@ -26,11 +26,10 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/sys/dev/hwpmc/hwpmc_x86.c,v 1.14 2009/12/03 14:59:42 jkoshy Exp $
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_x86.c,v 1.14 2009/12/03 14:59:42 jkoshy Exp $");
-
 #include <sys/param.h>
 #include <sys/bus.h>
 #include <sys/pmc.h>
@@ -40,9 +39,10 @@ __FBSDID("$FreeBSD: src/sys/dev/hwpmc/hwpmc_x86.c,v 1.14 2009/12/03 14:59:42 jko
 #include <machine/cpu.h>
 #include <machine/cputypes.h>
 #include <machine/intr_machdep.h>
-#include <machine/apicvar.h>
 #include <machine/pmc_mdep.h>
 #include <machine/md_var.h>
+#include <machine/smp.h>
+#include <machine_base/apic/lapic.h>
 
 #include <vm/vm.h>
 #include <vm/vm_param.h>
@@ -236,7 +236,7 @@ pmc_save_kernel_callchain(uintptr_t *cc, int nframes, struct trapframe *tf)
  */
 
 struct pmc_mdep *
-pmc_md_initialize()
+pmc_md_initialize(void)
 {
 	int i;
 	struct pmc_mdep *md;
diff --git a/sys/kern/kern_pmc.c b/sys/kern/kern_pmc.c
index c3516a9..8ce0423 100644
--- a/sys/kern/kern_pmc.c
+++ b/sys/kern/kern_pmc.c
@@ -26,18 +26,17 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/sys/kern/kern_pmc.c,v 1.9 2011/02/25 10:11:01 netchild Exp $
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/kern/kern_pmc.c,v 1.9 2011/02/25 10:11:01 netchild Exp $");
-
 #include "opt_hwpmc_hooks.h"
 
 #include <sys/types.h>
 #include <sys/pmc.h>
 #include <sys/pmckern.h>
-#include <sys/smp.h>
 #include <sys/sysctl.h>
+#include <sys/systm.h>
 
 #ifdef	HWPMC_HOOKS
 FEATURE(hwpmc_hooks, "Kernel support for HW PMC");
diff --git a/sys/sys/pmc.h b/sys/sys/pmc.h
index 00e4814..c67d925 100644
--- a/sys/sys/pmc.h
+++ b/sys/sys/pmc.h
@@ -33,7 +33,9 @@
 #ifndef _SYS_PMC_H_
 #define	_SYS_PMC_H_
 
-#include <dev/hwpmc/pmc_events.h>
+#include <sys/sysmsg.h>
+
+#include <dev/misc/hwpmc/pmc_events.h>
 
 #include <machine/pmc_mdep.h>
 #include <machine/profile.h>
@@ -266,7 +268,7 @@ enum pmc_caps
 /*
  * PMC Event Numbers
  *
- * These are generated from the definitions in "dev/hwpmc/pmc_events.h".
+ * These are generated from the definitions in "dev/misc/hwpmc/pmc_events.h".
  */
 
 enum pmc_event {
@@ -559,6 +561,7 @@ struct pmc_op_getmsr {
 #ifdef _KERNEL
 
 #include <sys/malloc.h>
+#include <sys/spinlock.h>
 #include <sys/sysctl.h>
 
 #include <machine/frame.h>
@@ -974,7 +977,7 @@ extern struct pmc_debugflags pmc_debugflags;
 
 #define	PMCDBG(M,N,L,F,...) do {					\
 	if (pmc_debugflags.pdb_ ## M & (1 << PMC_DEBUG_MIN_ ## N))	\
-		printf(#M ":" #N ":" #L  ": " F "\n", __VA_ARGS__);	\
+		kprintf(#M ":" #N ":" #L  ": " F "\n", __VA_ARGS__);	\
 } while (0)
 
 /* Major numbers */
diff --git a/sys/sys/pmckern.h b/sys/sys/pmckern.h
index 7676748..f5bbc97 100644
--- a/sys/sys/pmckern.h
+++ b/sys/sys/pmckern.h
@@ -41,7 +41,6 @@
 #include <sys/kernel.h>
 #include <sys/lock.h>
 #include <sys/proc.h>
-#include <sys/sx.h>
 
 #define	PMC_FN_PROCESS_EXEC		1
 #define	PMC_FN_CSW_IN			2
diff --git a/usr.sbin/pmcannotate/pmcannotate.c b/usr.sbin/pmcannotate/pmcannotate.c
index 3ce8e5b..759d268 100644
--- a/usr.sbin/pmcannotate/pmcannotate.c
+++ b/usr.sbin/pmcannotate/pmcannotate.c
@@ -26,11 +26,9 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
+ * $FreeBSD: src/usr.sbin/pmcannotate/pmcannotate.c,v 1.4 2010/10/16 17:30:27 bcr Exp $
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.sbin/pmcannotate/pmcannotate.c,v 1.4 2010/10/16 17:30:27 bcr Exp $");
-
 #include <sys/param.h>
 #include <sys/queue.h>
 
@@ -316,7 +314,7 @@ fqueue_compact(float th)
 
 	/* Revert the percentage calculation. */
 	thi = th * totalsamples / 100;
-	TAILQ_FOREACH_SAFE(agg, &fqueue, ag_fiter, tmpagg)
+	TAILQ_FOREACH_MUTABLE(agg, &fqueue, ag_fiter, tmpagg)
 		if (agg->ag_nsamples < thi)
 			TAILQ_REMOVE(&fqueue, agg, ag_fiter);
 }
diff --git a/usr.sbin/pmccontrol/pmccontrol.c b/usr.sbin/pmccontrol/pmccontrol.c
index 03a3b1d..6b43299 100644
--- a/usr.sbin/pmccontrol/pmccontrol.c
+++ b/usr.sbin/pmccontrol/pmccontrol.c
@@ -23,11 +23,9 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
+ * $FreeBSD: src/usr.sbin/pmccontrol/pmccontrol.c,v 1.9 2010/10/11 14:31:24 gnn Exp $
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.sbin/pmccontrol/pmccontrol.c,v 1.9 2010/10/11 14:31:24 gnn Exp $");
-
 #include <sys/types.h>
 #include <sys/queue.h>
 #include <sys/sysctl.h>
@@ -255,7 +253,7 @@ pmcc_do_list_state(void)
 	ncpu = pc->pm_ncpu;
 
 	for (c = cpu = 0; cpu < ncpu; cpu++) {
-#if	defined(__i386__) || defined(__amd64__)
+#if	defined(__i386__) || defined(__x86_64__)
 		if (pc->pm_cputype == PMC_CPU_INTEL_PIV &&
 		    (logical_cpus_mask & (1 << cpu)))
 			continue; /* skip P4-style 'logical' cpus */
diff --git a/usr.sbin/pmcstat/pmcpl_annotate.c b/usr.sbin/pmcstat/pmcpl_annotate.c
index 99c26be..f9fa275 100644
--- a/usr.sbin/pmcstat/pmcpl_annotate.c
+++ b/usr.sbin/pmcstat/pmcpl_annotate.c
@@ -26,6 +26,8 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/usr.sbin/pmcstat/pmcpl_annotate.c,v 1.1 2010/02/11 22:51:44 fabient Exp $
  */
 
 /*
@@ -33,9 +35,6 @@
  * gprof(1) compatible profiles.
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.sbin/pmcstat/pmcpl_annotate.c,v 1.1 2010/02/11 22:51:44 fabient Exp $");
-
 #include <sys/param.h>
 #include <sys/endian.h>
 #include <sys/gmon.h>
diff --git a/usr.sbin/pmcstat/pmcpl_callgraph.c b/usr.sbin/pmcstat/pmcpl_callgraph.c
index 47a6838..7c9cf62 100644
--- a/usr.sbin/pmcstat/pmcpl_callgraph.c
+++ b/usr.sbin/pmcstat/pmcpl_callgraph.c
@@ -26,6 +26,8 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/usr.sbin/pmcstat/pmcpl_callgraph.c,v 1.5 2010/09/03 13:54:02 fabient Exp $
  */
 
 /*
@@ -33,9 +35,6 @@
  * gprof(1) compatible profiles.
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.sbin/pmcstat/pmcpl_callgraph.c,v 1.5 2010/09/03 13:54:02 fabient Exp $");
-
 #include <sys/param.h>
 #include <sys/endian.h>
 #include <sys/gmon.h>
@@ -112,7 +111,7 @@ pmcstat_cgnode_free(struct pmcstat_cgnode *cg)
 {
 	struct pmcstat_cgnode *cgc, *cgtmp;
 
-	LIST_FOREACH_SAFE(cgc, &cg->pcg_children, pcg_sibling, cgtmp)
+	LIST_FOREACH_MUTABLE(cgc, &cg->pcg_children, pcg_sibling, cgtmp)
 		pmcstat_cgnode_free(cgc);
 	free(cg);
 }
@@ -676,7 +675,7 @@ pmcpl_cg_shutdown(FILE *mf)
 	 * Free memory.
 	 */
 	for (i = 0; i < PMCSTAT_NHASH; i++) {
-		LIST_FOREACH_SAFE(pch, &pmcstat_cgnode_hash[i], pch_next,
+		LIST_FOREACH_MUTABLE(pch, &pmcstat_cgnode_hash[i], pch_next,
 		    pchtmp) {
 			pmcstat_cgnode_free(pch->pch_cgnode);
 			LIST_REMOVE(pch, pch_next);
diff --git a/usr.sbin/pmcstat/pmcpl_calltree.c b/usr.sbin/pmcstat/pmcpl_calltree.c
index efb92f8..d4cce02 100644
--- a/usr.sbin/pmcstat/pmcpl_calltree.c
+++ b/usr.sbin/pmcstat/pmcpl_calltree.c
@@ -22,6 +22,8 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/usr.sbin/pmcstat/pmcpl_calltree.c,v 1.9 2010/09/03 13:54:02 fabient Exp $
  */
 
 /*
@@ -31,9 +33,6 @@
  * Handle top mode with a sorted tree display.
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.sbin/pmcstat/pmcpl_calltree.c,v 1.9 2010/09/03 13:54:02 fabient Exp $");
-
 #include <sys/param.h>
 #include <sys/endian.h>
 #include <sys/queue.h>
@@ -1030,7 +1029,7 @@ pmcpl_ct_shutdown(FILE *mf)
 	 */
 
 	for (i = 0; i < PMCSTAT_NHASH; i++) {
-		LIST_FOREACH_SAFE(pch, &pmcpl_ct_node_hash[i], pch_next,
+		LIST_FOREACH_MUTABLE(pch, &pmcpl_ct_node_hash[i], pch_next,
 		    pchtmp) {
 			pmcpl_ct_node_free(pch->pch_ctnode);
 			free(pch);
diff --git a/usr.sbin/pmcstat/pmcpl_gprof.c b/usr.sbin/pmcstat/pmcpl_gprof.c
index 68137ae..e710fa6 100644
--- a/usr.sbin/pmcstat/pmcpl_gprof.c
+++ b/usr.sbin/pmcstat/pmcpl_gprof.c
@@ -27,6 +27,8 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/usr.sbin/pmcstat/pmcpl_gprof.c,v 1.2 2010/04/14 21:53:27 fabient Exp $
  */
 
 /*
@@ -34,9 +36,6 @@
  * gprof(1) compatible profiles.
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.sbin/pmcstat/pmcpl_gprof.c,v 1.2 2010/04/14 21:53:27 fabient Exp $");
-
 #include <sys/param.h>
 #include <sys/endian.h>
 #include <sys/gmon.h>
@@ -353,7 +352,7 @@ pmcpl_gmon_shutdownimage(struct pmcstat_image *pi)
 {
 	struct pmcstat_gmonfile *pgf, *pgftmp;
 
-	LIST_FOREACH_SAFE(pgf, &pi->pi_gmlist, pgf_next, pgftmp) {
+	LIST_FOREACH_MUTABLE(pgf, &pi->pi_gmlist, pgf_next, pgftmp) {
 		if (pgf->pgf_file)
 			(void) fclose(pgf->pgf_file);
 		LIST_REMOVE(pgf, pgf_next);
diff --git a/usr.sbin/pmcstat/pmcstat.c b/usr.sbin/pmcstat/pmcstat.c
index e970b41..2519346 100644
--- a/usr.sbin/pmcstat/pmcstat.c
+++ b/usr.sbin/pmcstat/pmcstat.c
@@ -26,11 +26,11 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/usr.sbin/pmcstat/pmcstat.c,v 1.30 2010/08/03 13:46:25 fabient Exp $
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.sbin/pmcstat/pmcstat.c,v 1.30 2010/08/03 13:46:25 fabient Exp $");
-
+#include <sys/user.h>
 #include <sys/types.h>
 #include <sys/event.h>
 #include <sys/param.h>
@@ -40,7 +40,6 @@ __FBSDID("$FreeBSD: src/usr.sbin/pmcstat/pmcstat.c,v 1.30 2010/08/03 13:46:25 fa
 #include <sys/sysctl.h>
 #include <sys/time.h>
 #include <sys/ttycom.h>
-#include <sys/user.h>
 #include <sys/wait.h>
 
 #include <assert.h>
@@ -146,7 +145,7 @@ pmcstat_cleanup(void)
 	struct pmcstat_ev *ev, *tmp;
 
 	/* release allocated PMCs. */
-	STAILQ_FOREACH_SAFE(ev, &args.pa_events, ev_next, tmp)
+	STAILQ_FOREACH_MUTABLE(ev, &args.pa_events, ev_next, tmp)
 	    if (ev->ev_pmcid != PMC_ID_INVALID) {
 		if (pmc_stop(ev->ev_pmcid) < 0)
 			err(EX_OSERR, "ERROR: cannot stop pmc 0x%x "
diff --git a/usr.sbin/pmcstat/pmcstat_log.c b/usr.sbin/pmcstat/pmcstat_log.c
index 374f02c..d6cf020 100644
--- a/usr.sbin/pmcstat/pmcstat_log.c
+++ b/usr.sbin/pmcstat/pmcstat_log.c
@@ -26,6 +26,8 @@
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/usr.sbin/pmcstat/pmcstat_log.c,v 1.24 2010/09/03 13:54:02 fabient Exp $
  */
 
 /*
@@ -33,9 +35,6 @@
  * gprof(1) compatible profiles.
  */
 
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.sbin/pmcstat/pmcstat_log.c,v 1.24 2010/09/03 13:54:02 fabient Exp $");
-
 #include <sys/param.h>
 #include <sys/endian.h>
 #include <sys/gmon.h>
@@ -393,7 +392,7 @@ pmcstat_string_shutdown(void)
 	struct pmcstat_string *ps, *pstmp;
 
 	for (i = 0; i < PMCSTAT_NHASH; i++)
-		LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next,
+		LIST_FOREACH_MUTABLE(ps, &pmcstat_string_hash[i], ps_next,
 		    pstmp) {
 			LIST_REMOVE(ps, ps_next);
 			free(ps->ps_string);
@@ -891,7 +890,7 @@ pmcstat_image_unmap(struct pmcstat_process *pp, uintfptr_t start,
 	 * - we could have either 'start' or 'end' falling in the
 	 *   middle of a pcmap; in this case shorten the entry.
 	 */
-	TAILQ_FOREACH_SAFE(pcm, &pp->pp_map, ppm_next, pcmtmp) {
+	TAILQ_FOREACH_MUTABLE(pcm, &pp->pp_map, ppm_next, pcmtmp) {
 		assert(pcm->ppm_lowpc < pcm->ppm_highpc);
 		if (pcm->ppm_highpc <= start)
 			continue;
@@ -1212,12 +1211,12 @@ pmcstat_process_lookup(pid_t pid, int allocate)
 
 	hash = (uint32_t) pid & PMCSTAT_HASH_MASK;	/* simplicity wins */
 
-	LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[hash], pp_next, pptmp)
+	LIST_FOREACH_MUTABLE(pp, &pmcstat_process_hash[hash], pp_next, pptmp)
 	    if (pp->pp_pid == pid) {
 		    /* Found a descriptor, check and process zombies */
 		    if (allocate && pp->pp_isactive == 0) {
 			    /* remove maps */
-			    TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next,
+			    TAILQ_FOREACH_MUTABLE(ppm, &pp->pp_map, ppm_next,
 				ppmtmp) {
 				    TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
 				    free(ppm);
@@ -1486,7 +1485,7 @@ pmcstat_analyze_log(void)
 			    PMCSTAT_ALLOCATE);
 
 			/* delete the current process map */
-			TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
+			TAILQ_FOREACH_MUTABLE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
 				TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
 				free(ppm);
 			}
@@ -2122,7 +2121,7 @@ pmcstat_shutdown_logging(void)
 		plugins[args.pa_pplugin].pl_shutdown(mf);
 
 	for (i = 0; i < PMCSTAT_NHASH; i++) {
-		LIST_FOREACH_SAFE(pi, &pmcstat_image_hash[i], pi_next,
+		LIST_FOREACH_MUTABLE(pi, &pmcstat_image_hash[i], pi_next,
 		    pitmp) {
 			if (plugins[args.pa_plugin].pl_shutdownimage != NULL)
 				plugins[args.pa_plugin].pl_shutdownimage(pi);
@@ -2136,9 +2135,9 @@ pmcstat_shutdown_logging(void)
 			free(pi);
 		}
 
-		LIST_FOREACH_SAFE(pp, &pmcstat_process_hash[i], pp_next,
+		LIST_FOREACH_MUTABLE(pp, &pmcstat_process_hash[i], pp_next,
 		    pptmp) {
-			TAILQ_FOREACH_SAFE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
+			TAILQ_FOREACH_MUTABLE(ppm, &pp->pp_map, ppm_next, ppmtmp) {
 				TAILQ_REMOVE(&pp->pp_map, ppm, ppm_next);
 				free(ppm);
 			}
-- 
1.7.0.5

