--- //depot/vendor/freebsd/src/sys/amd64/amd64/machdep.c	2007/03/30 00:11:29
+++ //depot/user/attilio/attilio_schedlock/amd64/amd64/machdep.c	2007/05/10 15:47:06
@@ -221,8 +221,8 @@
 	vm_ksubmap_init(&kmi);
 
 	printf("avail memory  = %ju (%ju MB)\n",
-	    ptoa((uintmax_t)cnt.v_free_count),
-	    ptoa((uintmax_t)cnt.v_free_count) / 1048576);
+	    ptoa((uintmax_t)VMGCNT_GET(free_count)),
+	    ptoa((uintmax_t)VMGCNT_GET(free_count)) / 1048576);
 
 	/*
 	 * Set up buffers, so they can be used to read disk labels.
--- //depot/vendor/freebsd/src/sys/amd64/amd64/pmap.c	2007/04/25 18:17:11
+++ //depot/user/attilio/attilio_schedlock/amd64/amd64/pmap.c	2007/05/10 15:47:06
@@ -620,7 +620,7 @@
 	 * numbers of pv entries.
 	 */
 	TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
-	pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
+	pv_entry_max = shpgperproc * maxproc + VMGCNT_GET(page_count);
 	TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
 	pv_entry_high_water = 9 * (pv_entry_max / 10);
 }
@@ -633,7 +633,7 @@
 
 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
 	if (error == 0 && req->newptr) {
-		shpgperproc = (pv_entry_max - cnt.v_page_count) / maxproc;
+		shpgperproc = (pv_entry_max - VMGCNT_GET(page_count)) / maxproc;
 		pv_entry_high_water = 9 * (pv_entry_max / 10);
 	}
 	return (error);
@@ -648,7 +648,7 @@
 
 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
 	if (error == 0 && req->newptr) {
-		pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
+		pv_entry_max = shpgperproc * maxproc + VMGCNT_GET(page_count);
 		pv_entry_high_water = 9 * (pv_entry_max / 10);
 	}
 	return (error);
@@ -1149,8 +1149,7 @@
 	 */
 	m->right = *free;
 	*free = m;
-	
-	atomic_subtract_int(&cnt.v_wire_count, 1);
+	VMGCNT_DEC(wire_count, 1);
 	return 1;
 }
 
@@ -1460,7 +1459,7 @@
 	pmap->pm_pml4[PML4PML4I] = 0;	/* Recursive Mapping */
 
 	m->wire_count--;
-	atomic_subtract_int(&cnt.v_wire_count, 1);
+	VMGCNT_DEC(wire_count, 1);
 	vm_page_free_zero(m);
 	PMAP_LOCK_DESTROY(pmap);
 }
--- //depot/vendor/freebsd/src/sys/arm/arm/machdep.c	2007/02/14 04:46:47
+++ //depot/user/attilio/attilio_schedlock/arm/arm/machdep.c	2007/05/10 15:47:06
@@ -286,8 +286,8 @@
 	vm_ksubmap_init(&kmi);
 
 	printf("avail memory = %ju (%ju MB)\n",
-	    (uintmax_t)ptoa(cnt.v_free_count),
-	    (uintmax_t)ptoa(cnt.v_free_count) / 1048576);
+	    (uintmax_t)ptoa(VMGCNT_GET(free_count)),
+	    (uintmax_t)ptoa(VMGCNT_GET(free_count)) / 1048576);
 
 	bufinit();
 	vm_pager_bufferinit();
--- //depot/vendor/freebsd/src/sys/arm/arm/pmap.c	2007/02/05 10:36:06
+++ //depot/user/attilio/attilio_schedlock/arm/arm/pmap.c	2007/05/10 15:47:06
@@ -1970,7 +1970,7 @@
 
 	TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
 	
-	pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
+	pv_entry_max = shpgperproc * maxproc + VMGCNT_GET(page_count);
 	pv_entry_high_water = 9 * (pv_entry_max / 10);
 	l2zone = uma_zcreate("L2 Table", L2_TABLE_SIZE_REAL, pmap_l2ptp_ctor,
 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
--- //depot/vendor/freebsd/src/sys/compat/linprocfs/linprocfs.c	2007/05/01 03:14:28
+++ //depot/user/attilio/attilio_schedlock/compat/linprocfs/linprocfs.c	2007/05/10 15:47:06
@@ -146,14 +146,14 @@
 	/*
 	 * The correct thing here would be:
 	 *
-	memfree = cnt.v_free_count * PAGE_SIZE;
+	memfree = VMGCNT_GET(free_count) * PAGE_SIZE;
 	memused = memtotal - memfree;
 	 *
 	 * but it might mislead linux binaries into thinking there
 	 * is very little memory left, so we cheat and tell them that
 	 * all memory that isn't wired down is free.
 	 */
-	memused = cnt.v_wire_count * PAGE_SIZE;
+	memused = VMGCNT_GET(wire_count) * PAGE_SIZE;
 	memfree = memtotal - memused;
 	swap_pager_status(&i, &j);
 	swaptotal = (unsigned long long)i * PAGE_SIZE;
@@ -175,7 +175,7 @@
 	 * like unstaticizing it just for linprocfs's sake.
 	 */
 	buffers = 0;
-	cached = cnt.v_cache_count * PAGE_SIZE;
+	cached = VMGCNT_GET(cache_count) * PAGE_SIZE;
 
 	sbuf_printf(sb,
 	    "	     total:    used:	free:  shared: buffers:	 cached:\n"
@@ -394,12 +394,12 @@
 	    "intr %u\n"
 	    "ctxt %u\n"
 	    "btime %lld\n",
-	    cnt.v_vnodepgsin,
-	    cnt.v_vnodepgsout,
-	    cnt.v_swappgsin,
-	    cnt.v_swappgsout,
-	    cnt.v_intr,
-	    cnt.v_swtch,
+	    VMGCNT_GET(vnodepgsin),
+	    VMGCNT_GET(vnodepgsout),
+	    VMGCNT_GET(swappgsin),
+	    VMGCNT_GET(swappgsout),
+	    VMGCNT_GET(intr),
+	    VMGCNT_GET(swtch),
 	    (long long)boottime.tv_sec);
 	return (0);
 }
--- //depot/vendor/freebsd/src/sys/compat/linux/linux_misc.c	2007/02/24 16:52:52
+++ //depot/user/attilio/attilio_schedlock/compat/linux/linux_misc.c	2007/05/10 15:47:06
@@ -139,7 +139,7 @@
 		    LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
 
 	sysinfo.totalram = physmem * PAGE_SIZE;
-	sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
+	sysinfo.freeram = sysinfo.totalram - VMGCNT_GET(wire_count) * PAGE_SIZE;
 
 	sysinfo.sharedram = 0;
 	mtx_lock(&vm_object_list_mtx);
--- //depot/vendor/freebsd/src/sys/compat/ndis/kern_windrv.c	2005/11/02 18:05:29
+++ //depot/user/attilio/attilio_schedlock/compat/ndis/kern_windrv.c	2007/01/30 21:17:05
@@ -883,9 +883,9 @@
 	struct gdt		gtable;
 	uint16_t		ltable;
 
-	mtx_lock_spin(&sched_lock);
+	t = curthread;
 
-	t = curthread;
+	mtx_lock_spin(&dt_lock);
 
 	/* Grab location of existing GDT. */
 
@@ -904,7 +904,7 @@
 
 	x86_setldt(&gtable, ltable);
 
-	mtx_unlock_spin(&sched_lock);
+	mtx_unlock_spin(&dt_lock);
 
 	return;
 }
@@ -918,9 +918,9 @@
 	struct x86desc		*l;
 	struct thread		*t;
 
-	mtx_lock_spin(&sched_lock);
+	t = curthread;
 
-	t = curthread;
+	mtx_lock_spin(&dt_lock);
 
 	/* Grab location of existing GDT. */
 
@@ -952,7 +952,7 @@
 
 	x86_setldt(&gtable, ltable);
 
-	mtx_unlock_spin(&sched_lock);
+	mtx_unlock_spin(&dt_lock);
 
 	/* Whew. */
 
--- //depot/vendor/freebsd/src/sys/compat/svr4/svr4_misc.c	2006/11/06 13:48:07
+++ //depot/user/attilio/attilio_schedlock/compat/svr4/svr4_misc.c	2007/05/10 15:47:06
@@ -778,14 +778,14 @@
 #if defined(UVM)
 		*retval = uvmexp.free;	/* XXX: free instead of total */
 #else
-		*retval = cnt.v_free_count;	/* XXX: free instead of total */
+		*retval = VMGCNT_GET(free_count);	/* XXX: free instead of total */
 #endif
 		break;
 	case SVR4_CONFIG_AVPHYS_PAGES:
 #if defined(UVM)
 		*retval = uvmexp.active;	/* XXX: active instead of avg */
 #else
-		*retval = cnt.v_active_count;	/* XXX: active instead of avg */
+		*retval = VMGCNT_GET(active_count);	/* XXX: active instead of avg */
 #endif
 		break;
 #endif /* NOTYET */
--- //depot/vendor/freebsd/src/sys/fs/smbfs/smbfs_io.c	2006/08/09 17:46:32
+++ //depot/user/attilio/attilio_schedlock/fs/smbfs/smbfs_io.c	2007/05/10 15:47:06
@@ -475,8 +475,8 @@
 
 	kva = (vm_offset_t) bp->b_data;
 	pmap_qenter(kva, pages, npages);
-	cnt.v_vnodein++;
-	cnt.v_vnodepgsin += npages;
+	VMGCNT_ADD(vnodein, 1);
+	VMGCNT_ADD(vnodepgsin, npages);
 
 	iov.iov_base = (caddr_t) kva;
 	iov.iov_len = count;
@@ -626,8 +626,8 @@
 
 	kva = (vm_offset_t) bp->b_data;
 	pmap_qenter(kva, pages, npages);
-	cnt.v_vnodeout++;
-	cnt.v_vnodepgsout += count;
+	VMGCNT_ADD(vnodeout, 1);
+	VMGCNT_ADD(vnodepgsout, count);
 
 	iov.iov_base = (caddr_t) kva;
 	iov.iov_len = count;
--- //depot/vendor/freebsd/src/sys/i386/i386/machdep.c	2007/03/17 19:47:35
+++ //depot/user/attilio/attilio_schedlock/i386/i386/machdep.c	2007/05/10 19:52:44
@@ -247,8 +247,8 @@
 	vm_ksubmap_init(&kmi);
 
 	printf("avail memory = %ju (%ju MB)\n",
-	    ptoa((uintmax_t)cnt.v_free_count),
-	    ptoa((uintmax_t)cnt.v_free_count) / 1048576);
+	    ptoa((uintmax_t)VMGCNT_GET(free_count)),
+	    ptoa((uintmax_t)VMGCNT_GET(free_count)) / 1048576);
 
 	/*
 	 * Set up buffers, so they can be used to read disk labels.
@@ -1171,8 +1171,10 @@
 	pcb->pcb_gs = _udatasel;
 	load_gs(_udatasel);
 
+	mtx_lock_spin(&dt_lock);
 	if (td->td_proc->p_md.md_ldt)
 		user_ldt_free(td);
+	mtx_unlock_spin(&dt_lock);
   
 	bzero((char *)regs, sizeof(struct trapframe));
 	regs->tf_eip = entry;
@@ -1278,6 +1280,7 @@
 struct gate_descriptor *idt = &idt0[0];	/* interrupt descriptor table */
 union descriptor ldt[NLDT];		/* local descriptor table */
 struct region_descriptor r_gdt, r_idt;	/* table descriptors */
+struct mtx dt_lock;			/* lock for GDT and LDT */
 
 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
 extern int has_f00f_bug;
@@ -2101,6 +2104,7 @@
 
 	r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
 	r_gdt.rd_base =  (int) gdt;
+	mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
 	lgdt(&r_gdt);
 
 	pcpu_init(pc, 0, sizeof(struct pcpu));
--- //depot/vendor/freebsd/src/sys/i386/i386/mp_machdep.c	2007/05/08 22:03:14
+++ //depot/user/attilio/attilio_schedlock/i386/i386/mp_machdep.c	2007/05/10 10:36:01
@@ -1388,11 +1388,9 @@
 
 	if (mp_ncpus == 1) 
 		return;
-	mtx_lock_spin(&sched_lock);
 	atomic_store_rel_int(&aps_ready, 1);
 	while (smp_started == 0)
 		ia32_pause();
-	mtx_unlock_spin(&sched_lock);
 }
 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
 
--- //depot/vendor/freebsd/src/sys/i386/i386/pmap.c	2007/04/25 18:12:24
+++ //depot/user/attilio/attilio_schedlock/i386/i386/pmap.c	2007/05/10 15:47:06
@@ -606,7 +606,7 @@
 	 * numbers of pv entries.
 	 */
 	TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
-	pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
+	pv_entry_max = shpgperproc * maxproc + VMGCNT_GET(page_count);
 	TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
 	pv_entry_max = roundup(pv_entry_max, _NPCPV);
 	pv_entry_high_water = 9 * (pv_entry_max / 10);
@@ -1168,7 +1168,7 @@
 	pmap->pm_pdir[m->pindex] = 0;
 	--pmap->pm_stats.resident_count;
 
-	atomic_subtract_int(&cnt.v_wire_count, 1);
+	VMGCNT_DEC(wire_count, 1);
 
 	/*
 	 * Do an invltlb to make the invalidated mapping
@@ -1536,7 +1536,7 @@
 		    ("pmap_release: got wrong ptd page"));
 #endif
 		m->wire_count--;
-		atomic_subtract_int(&cnt.v_wire_count, 1);
+		VMGCNT_DEC(wire_count, 1);
 		vm_page_free_zero(m);
 	}
 	PMAP_LOCK_DESTROY(pmap);
--- //depot/vendor/freebsd/src/sys/i386/i386/sys_machdep.c	2007/04/22 15:36:42
+++ //depot/user/attilio/attilio_schedlock/i386/i386/sys_machdep.c	2007/05/08 16:03:17
@@ -42,6 +42,7 @@
 #include <sys/mutex.h>
 #include <sys/priv.h>
 #include <sys/proc.h>
+#include <sys/refcount.h>
 #include <sys/smp.h>
 #include <sys/sysproto.h>
 
@@ -115,7 +116,6 @@
 		break;
 	}
 
-	mtx_lock(&Giant);
 	switch(uap->op) {
 	case I386_GET_LDT:
 		error = i386_get_ldt(td, &kargs.largs);
@@ -215,7 +215,6 @@
 		error = EINVAL;
 		break;
 	}
-	mtx_unlock(&Giant);
 	return (error);
 }
 
@@ -351,16 +350,19 @@
 
 /*
  * Update the GDT entry pointing to the LDT to point to the LDT of the
- * current process.
- *
- * This must be called with sched_lock held.  Unfortunately, we can't use a
- * mtx_assert() here because cpu_switch() calls this function after changing
- * curproc but before sched_lock's owner is updated in mi_switch().
+ * current process. Manage dt_lock holding/unholding autonomously.
  */   
 void
 set_user_ldt(struct mdproc *mdp)
 {
 	struct proc_ldt *pldt;
+	int dtlocked;
+
+	dtlocked = 0;
+	if (!mtx_owned(&dt_lock)) {
+		mtx_lock_spin(&dt_lock);
+		dtlocked = 1;
+	}
 
 	pldt = mdp->md_ldt;
 #ifdef SMP
@@ -370,6 +372,8 @@
 #endif
 	lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
 	PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL));
+	if (dtlocked)
+		mtx_unlock_spin(&dt_lock);
 }
 
 #ifdef SMP
@@ -385,17 +389,15 @@
 #endif
 
 /*
- * Must be called with either sched_lock free or held but not recursed.
- * If it does not return NULL, it will return with it owned.
+ * dt_lock must be held. Returns with dt_lock held.
  */
 struct proc_ldt *
 user_ldt_alloc(struct mdproc *mdp, int len)
 {
 	struct proc_ldt *pldt, *new_ldt;
 
-	if (mtx_owned(&sched_lock))
-		mtx_unlock_spin(&sched_lock);
-	mtx_assert(&sched_lock, MA_NOTOWNED);
+	mtx_assert(&dt_lock, MA_OWNED);
+	mtx_unlock_spin(&dt_lock);
 	MALLOC(new_ldt, struct proc_ldt *, sizeof(struct proc_ldt),
 		M_SUBPROC, M_WAITOK);
 
@@ -406,54 +408,49 @@
 		FREE(new_ldt, M_SUBPROC);
 		return NULL;
 	}
-	new_ldt->ldt_refcnt = 1;
+	refcount_init(&new_ldt->ldt_refcnt, 1);
 	new_ldt->ldt_active = 0;
 
-	mtx_lock_spin(&sched_lock);
+	mtx_lock_spin(&dt_lock);
 	gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
 	gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
 	ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
 
-	if ((pldt = mdp->md_ldt)) {
+	if ((pldt = mdp->md_ldt) != NULL) {
 		if (len > pldt->ldt_len)
 			len = pldt->ldt_len;
 		bcopy(pldt->ldt_base, new_ldt->ldt_base,
 		    len * sizeof(union descriptor));
-	} else {
+	} else
 		bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
-	}
-	return new_ldt;
+	
+	return (new_ldt);
 }
 
 /*
- * Must be called either with sched_lock free or held but not recursed.
- * If md_ldt is not NULL, it will return with sched_lock released.
+ * Must be called with dt_lock held.
  */
 void
 user_ldt_free(struct thread *td)
 {
 	struct mdproc *mdp = &td->td_proc->p_md;
-	struct proc_ldt *pldt = mdp->md_ldt;
+	struct proc_ldt *pldt;
 
-	if (pldt == NULL)
+	mtx_assert(&dt_lock, MA_OWNED);
+	if ((pldt = mdp->md_ldt) == NULL)
 		return;
 
-	if (!mtx_owned(&sched_lock))
-		mtx_lock_spin(&sched_lock);
-	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
 	if (td == PCPU_GET(curthread)) {
 		lldt(_default_ldt);
 		PCPU_SET(currentldt, _default_ldt);
 	}
 
 	mdp->md_ldt = NULL;
-	if (--pldt->ldt_refcnt == 0) {
-		mtx_unlock_spin(&sched_lock);
+	if (refcount_release(&pldt->ldt_refcnt)) {
 		kmem_free(kernel_map, (vm_offset_t)pldt->ldt_base,
 			pldt->ldt_len * sizeof(union descriptor));
 		FREE(pldt, M_SUBPROC);
-	} else
-		mtx_unlock_spin(&sched_lock);
+	}
 }
 
 /*
@@ -468,7 +465,7 @@
 	struct i386_ldt_args *uap;
 {
 	int error = 0;
-	struct proc_ldt *pldt = td->td_proc->p_md.md_ldt;
+	struct proc_ldt *pldt;
 	int nldt, num;
 	union descriptor *lp;
 
@@ -477,11 +474,14 @@
 	    uap->start, uap->num, (void *)uap->descs);
 #endif
 
-	if (pldt) {
+	mtx_lock_spin(&dt_lock);
+	if ((pldt = td->td_proc->p_md.md_ldt) != NULL) {
 		nldt = pldt->ldt_len;
+		lp = &((union descriptor *)(pldt->ldt_base))[uap->start];
+		mtx_unlock_spin(&dt_lock);
 		num = min(uap->num, nldt);
-		lp = &((union descriptor *)(pldt->ldt_base))[uap->start];
 	} else {
+		mtx_unlock_spin(&dt_lock);
 		nldt = sizeof(ldt)/sizeof(ldt[0]);
 		num = min(uap->num, nldt);
 		lp = &ldt[uap->start];
@@ -531,10 +531,10 @@
 		}
 		if (uap->num <= 0)
 			return (EINVAL);
-		mtx_lock_spin(&sched_lock);
-		pldt = mdp->md_ldt;
-		if (pldt == NULL || uap->start >= pldt->ldt_len) {
-			mtx_unlock_spin(&sched_lock);
+		mtx_lock_spin(&dt_lock);
+		if ((pldt = mdp->md_ldt) == NULL ||
+		    uap->start >= pldt->ldt_len) {
+			mtx_unlock_spin(&dt_lock);
 			return (0);
 		}
 		largest_ld = uap->start + uap->num;
@@ -543,7 +543,7 @@
 		i = largest_ld - uap->start;
 		bzero(&((union descriptor *)(pldt->ldt_base))[uap->start],
 		    sizeof(union descriptor) * i);
-		mtx_unlock_spin(&sched_lock);
+		mtx_unlock_spin(&dt_lock);
 		return (0);
 	}
 
@@ -626,15 +626,15 @@
 
 	if (uap->start == LDT_AUTO_ALLOC && uap->num == 1) {
 		/* Allocate a free slot */
-		pldt = mdp->md_ldt;
-		if (pldt == NULL) {
-			error = i386_ldt_grow(td, NLDT + 1);
-			if (error)
+		mtx_lock_spin(&dt_lock);
+		if ((pldt = mdp->md_ldt) == NULL) {
+			if ((error = i386_ldt_grow(td, NLDT + 1))) {
+				mtx_unlock_spin(&dt_lock);
 				return (error);
+			}
 			pldt = mdp->md_ldt;
 		}
 again:
-		mtx_lock_spin(&sched_lock);
 		/*
 		 * start scanning a bit up to leave room for NVidia and
 		 * Wine, which still user the "Blat" method of allocation.
@@ -646,24 +646,23 @@
 			dp++;
 		}
 		if (i >= pldt->ldt_len) {
-			mtx_unlock_spin(&sched_lock);
-			error = i386_ldt_grow(td, pldt->ldt_len+1);
-			if (error)
+			if ((error = i386_ldt_grow(td, pldt->ldt_len+1))) {
+				mtx_unlock_spin(&dt_lock);
 				return (error);
+			}
 			goto again;
 		}
 		uap->start = i;
 		error = i386_set_ldt_data(td, i, 1, descs);
-		mtx_unlock_spin(&sched_lock);
+		mtx_unlock_spin(&dt_lock);
 	} else {
 		largest_ld = uap->start + uap->num;
-		error = i386_ldt_grow(td, largest_ld);
-		if (error == 0) {
-			mtx_lock_spin(&sched_lock);
+		mtx_lock_spin(&dt_lock);
+		if (!(error = i386_ldt_grow(td, largest_ld))) {
 			error = i386_set_ldt_data(td, uap->start, uap->num,
 			    descs);
-			mtx_unlock_spin(&sched_lock);
 		}
+		mtx_unlock_spin(&dt_lock);
 	}
 	if (error == 0)
 		td->td_retval[0] = uap->start;
@@ -677,7 +676,7 @@
 	struct mdproc *mdp = &td->td_proc->p_md;
 	struct proc_ldt *pldt = mdp->md_ldt;
 
-	mtx_assert(&sched_lock, MA_OWNED);
+	mtx_assert(&dt_lock, MA_OWNED);
 
 	/* Fill in range */
 	bcopy(descs,
@@ -694,14 +693,15 @@
 	caddr_t old_ldt_base;
 	int old_ldt_len;
 
+	mtx_assert(&dt_lock, MA_OWNED);
+
 	if (len > MAX_LD)
 		return (ENOMEM);
 	if (len < NLDT + 1)
 		len = NLDT + 1;
 
 	/* Allocate a user ldt. */
-	pldt = mdp->md_ldt;
-	if (!pldt || len > pldt->ldt_len) {
+	if ((pldt = mdp->md_ldt) != NULL || len > pldt->ldt_len) {
 		struct proc_ldt *new_ldt;
 
 		new_ldt = user_ldt_alloc(mdp, len);
@@ -709,42 +709,35 @@
 			return (ENOMEM);
 		pldt = mdp->md_ldt;
 
-		/* sched_lock was acquired by user_ldt_alloc. */
-		if (pldt) {
+		if (pldt != NULL) {
 			if (new_ldt->ldt_len > pldt->ldt_len) {
 				old_ldt_base = pldt->ldt_base;
 				old_ldt_len = pldt->ldt_len;
 				pldt->ldt_sd = new_ldt->ldt_sd;
 				pldt->ldt_base = new_ldt->ldt_base;
 				pldt->ldt_len = new_ldt->ldt_len;
-				mtx_unlock_spin(&sched_lock);
 				kmem_free(kernel_map, (vm_offset_t)old_ldt_base,
 					old_ldt_len * sizeof(union descriptor));
 				FREE(new_ldt, M_SUBPROC);
-				mtx_lock_spin(&sched_lock);
 			} else {
 				/*
 				 * If other threads already did the work,
 				 * do nothing.
 				 */
-				mtx_unlock_spin(&sched_lock);
 				kmem_free(kernel_map,
 				   (vm_offset_t)new_ldt->ldt_base,
 				   new_ldt->ldt_len * sizeof(union descriptor));
 				FREE(new_ldt, M_SUBPROC);
 				return (0);
 			}
-		} else {
+		} else
 			mdp->md_ldt = pldt = new_ldt;
-		}
 #ifdef SMP
-		mtx_unlock_spin(&sched_lock);
 		/* signal other cpus to reload ldt */
 		smp_rendezvous(NULL, (void (*)(void *))set_user_ldt_rv,
 		    NULL, td);
 #else
 		set_user_ldt(mdp);
-		mtx_unlock_spin(&sched_lock);
 #endif
 	}
 	return (0);
--- //depot/vendor/freebsd/src/sys/i386/i386/vm_machdep.c	2007/04/24 21:23:32
+++ //depot/user/attilio/attilio_schedlock/i386/i386/vm_machdep.c	2007/05/08 15:57:49
@@ -62,6 +62,7 @@
 #include <sys/mutex.h>
 #include <sys/pioctl.h>
 #include <sys/proc.h>
+#include <sys/refcount.h>
 #include <sys/sf_buf.h>
 #include <sys/smp.h>
 #include <sys/sched.h>
@@ -158,8 +159,9 @@
 			struct mdproc *mdp1 = &p1->p_md;
 			struct proc_ldt *pldt;
 
+			mtx_lock_spin(&dt_lock);
 			pldt = mdp1->md_ldt;
-			if (pldt && pldt->ldt_refcnt > 1) {
+			if (pldt && refcount_value(&pldt->ldt_refcnt) > 1) {
 				pldt = user_ldt_alloc(mdp1, pldt->ldt_len);
 				if (pldt == NULL)
 					panic("could not copy LDT");
@@ -167,6 +169,7 @@
 				set_user_ldt(mdp1);
 				user_ldt_free(td1);
 			}
+			mtx_unlock_spin(&dt_lock);
 		}
 		return;
 	}
@@ -248,10 +251,10 @@
 	pcb2->pcb_ext = 0;
 
 	/* Copy the LDT, if necessary. */
-	mtx_lock_spin(&sched_lock);
+	mtx_lock_spin(&dt_lock);
 	if (mdp2->md_ldt != NULL) {
 		if (flags & RFMEM) {
-			mdp2->md_ldt->ldt_refcnt++;
+			refcount_acquire(&mdp2->md_ldt->ldt_refcnt);
 		} else {
 			mdp2->md_ldt = user_ldt_alloc(mdp2,
 			    mdp2->md_ldt->ldt_len);
@@ -259,7 +262,7 @@
 				panic("could not copy LDT");
 		}
 	}
-	mtx_unlock_spin(&sched_lock);
+	mtx_unlock_spin(&dt_lock);
 
 	/* Setup to release sched_lock in fork_exit(). */
 	td2->td_md.md_spinlock_count = 1;
@@ -304,11 +307,13 @@
 	 * If this process has a custom LDT, release it.  Reset pc->pcb_gs
 	 * and %gs before we free it in case they refer to an LDT entry.
 	 */
+	mtx_lock_spin(&dt_lock);
 	if (td->td_proc->p_md.md_ldt) {
 		td->td_pcb->pcb_gs = _udatasel;
 		load_gs(_udatasel);
 		user_ldt_free(td);
 	}
+	mtx_unlock_spin(&dt_lock);
 }
 
 void
@@ -696,7 +701,7 @@
 }
 
 /*
- * Get an sf_buf from the freelist.  May block if none are available.
+ * Get an sf_buf from the freelist. Will block if none are available.
  */
 struct sf_buf *
 sf_buf_alloc(struct vm_page *m, int flags)
--- //depot/vendor/freebsd/src/sys/i386/include/proc.h	2005/04/04 21:55:27
+++ //depot/user/attilio/attilio_schedlock/i386/include/proc.h	2007/02/06 00:25:30
@@ -45,6 +45,8 @@
 
 /*
  * Machine-dependent part of the proc structure for i386.
+ * Table of MD locks:
+ *       t - Descriptor tables lock
  */
 struct mdthread {
 	int	md_spinlock_count;	/* (k) */
@@ -52,7 +54,7 @@
 };
 
 struct mdproc {
-	struct proc_ldt *md_ldt;	/* (j) per-process ldt */
+	struct proc_ldt *md_ldt;	/* (t) per-process ldt */
 };
 
 #ifdef	_KERNEL
@@ -61,6 +63,8 @@
 struct 	proc_ldt *user_ldt_alloc(struct mdproc *, int);
 void 	user_ldt_free(struct thread *);
 
+extern struct mtx dt_lock;
+
 #endif	/* _KERNEL */
 
 #endif /* !_MACHINE_PROC_H_ */
--- //depot/vendor/freebsd/src/sys/ia64/ia64/machdep.c	2007/05/05 19:54:17
+++ //depot/user/attilio/attilio_schedlock/ia64/ia64/machdep.c	2007/05/10 15:47:06
@@ -283,8 +283,8 @@
 
 	vm_ksubmap_init(&kmi);
 
-	printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
-	    ptoa(cnt.v_free_count) / 1048576);
+	printf("avail memory = %ld (%ld MB)\n", ptoa(VMGCNT_GET(free_count)),
+	    ptoa(VMGCNT_GET(free_count)) / 1048576);
  
 	if (fpswa_iface == NULL)
 		printf("Warning: no FPSWA package supplied\n");
--- //depot/vendor/freebsd/src/sys/ia64/ia64/pmap.c	2007/02/21 06:02:48
+++ //depot/user/attilio/attilio_schedlock/ia64/ia64/pmap.c	2007/05/10 15:47:06
@@ -533,7 +533,7 @@
 	pvzone = uma_zcreate("PV ENTRY", sizeof(struct pv_entry), NULL, NULL,
 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
 	TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
-	pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
+	pv_entry_max = shpgperproc * maxproc + VMGCNT_GET(page_count);
 	TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
 	pv_entry_high_water = 9 * (pv_entry_max / 10);
 
--- //depot/vendor/freebsd/src/sys/kern/init_main.c	2007/03/27 16:53:01
+++ //depot/user/attilio/attilio_schedlock/kern/init_main.c	2007/05/10 15:47:06
@@ -467,7 +467,7 @@
 	    p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
 	p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_cur =
 	    p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
-	i = ptoa(cnt.v_free_count);
+	i = ptoa(VMGCNT_GET(free_count));
 	p->p_limit->pl_rlimit[RLIMIT_RSS].rlim_max = i;
 	p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
 	p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
@@ -496,6 +496,7 @@
 {
 	struct timespec ts;
 	struct proc *p;
+	struct thread *td;
 
 	/*
 	 * Now we can look at the time, having had a chance to verify the
@@ -505,6 +506,10 @@
 	FOREACH_PROC_IN_SYSTEM(p) {
 		microuptime(&p->p_stats->p_start);
 		p->p_rux.rux_runtime = 0;
+		mtx_lock_spin(&sched_lock);
+		FOREACH_THREAD_IN_PROC(p, td)
+			td->td_runtime = 0;
+		mtx_unlock_spin(&sched_lock);
 	}
 	sx_sunlock(&allproc_lock);
 	PCPU_SET(switchtime, cpu_ticks());
--- //depot/vendor/freebsd/src/sys/kern/kern_clock.c	2007/03/08 06:46:41
+++ //depot/user/attilio/attilio_schedlock/kern/kern_clock.c	2007/04/14 23:03:36
@@ -83,6 +83,9 @@
 /* Some of these don't belong here, but it's easiest to concentrate them. */
 long cp_time[CPUSTATES];
 
+/* Spin-lock protecting profiling statistics. */
+struct mtx time_lock;
+
 static int
 sysctl_kern_cp_time(SYSCTL_HANDLER_ARGS)
 {
@@ -172,6 +175,7 @@
 	 * code do its bit.
 	 */
 	cpu_initclocks();
+	mtx_init(&time_lock, "time lock", NULL, MTX_SPIN);
 
 	/*
 	 * Compute profhz/stathz, and fix profhz if needed.
@@ -349,20 +353,15 @@
 	register struct proc *p;
 {
 
-	/*
-	 * XXX; Right now sched_lock protects statclock(), but perhaps
-	 * it should be protected later on by a time_lock, which would
-	 * cover psdiv, etc. as well.
-	 */
 	PROC_LOCK_ASSERT(p, MA_OWNED);
 	if (p->p_flag & P_STOPPROF)
 		return;
 	if ((p->p_flag & P_PROFIL) == 0) {
-		mtx_lock_spin(&sched_lock);
 		p->p_flag |= P_PROFIL;
+		mtx_lock_spin(&time_lock);
 		if (++profprocs == 1)
 			cpu_startprofclock();
-		mtx_unlock_spin(&sched_lock);
+		mtx_unlock_spin(&time_lock);
 	}
 }
 
@@ -385,11 +384,11 @@
 		}
 		if ((p->p_flag & P_PROFIL) == 0)
 			return;
-		mtx_lock_spin(&sched_lock);
 		p->p_flag &= ~P_PROFIL;
+		mtx_lock_spin(&time_lock);
 		if (--profprocs == 0)
 			cpu_stopprofclock();
-		mtx_unlock_spin(&sched_lock);
+		mtx_unlock_spin(&time_lock);
 	}
 }
 
@@ -412,7 +411,6 @@
 	td = curthread;
 	p = td->td_proc;
 
-	mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
 	if (usermode) {
 		/*
 		 * Charge the time as appropriate.
@@ -421,7 +419,10 @@
 		if (p->p_flag & P_SA)
 			thread_statclock(1);
 #endif
+		mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
 		td->td_uticks++;
+		mtx_unlock_spin_flags(&sched_lock, MTX_QUIET);
+		mtx_lock_spin_flags(&time_lock, MTX_QUIET);
 		if (p->p_nice > NZERO)
 			cp_time[CP_NICE]++;
 		else
@@ -441,7 +442,10 @@
 		 */
 		if ((td->td_pflags & TDP_ITHREAD) ||
 		    td->td_intr_nesting_level >= 2) {
+			mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
 			td->td_iticks++;
+			mtx_unlock_spin_flags(&sched_lock, MTX_QUIET);
+			mtx_lock_spin_flags(&time_lock, MTX_QUIET);
 			cp_time[CP_INTR]++;
 		} else {
 #ifdef KSE
@@ -449,16 +453,21 @@
 				thread_statclock(0);
 #endif
 			td->td_pticks++;
+			mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
 			td->td_sticks++;
+			mtx_unlock_spin_flags(&sched_lock, MTX_QUIET);
+			mtx_lock_spin_flags(&time_lock, MTX_QUIET);
 			if (!TD_IS_IDLETHREAD(td))
 				cp_time[CP_SYS]++;
 			else
 				cp_time[CP_IDLE]++;
 		}
 	}
+	mtx_unlock_spin_flags(&time_lock, MTX_QUIET);
 	CTR4(KTR_SCHED, "statclock: %p(%s) prio %d stathz %d",
 	    td, td->td_proc->p_comm, td->td_priority, (stathz)?stathz:hz);
 
+	mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
 	sched_clock(td);
 
 	/* Update resource usage integrals and maximums. */
--- //depot/vendor/freebsd/src/sys/kern/kern_fork.c	2007/04/04 09:12:37
+++ //depot/user/attilio/attilio_schedlock/kern/kern_fork.c	2007/05/10 15:47:06
@@ -665,20 +665,20 @@
 	vm_forkproc(td, p2, td2, flags);
 
 	if (flags == (RFFDG | RFPROC)) {
-		atomic_add_int(&cnt.v_forks, 1);
-		atomic_add_int(&cnt.v_forkpages, p2->p_vmspace->vm_dsize +
+		VMGCNT_ADD(forks, 1);
+		VMGCNT_ADD(forkpages, p2->p_vmspace->vm_dsize +
 		    p2->p_vmspace->vm_ssize);
 	} else if (flags == (RFFDG | RFPROC | RFPPWAIT | RFMEM)) {
-		atomic_add_int(&cnt.v_vforks, 1);
-		atomic_add_int(&cnt.v_vforkpages, p2->p_vmspace->vm_dsize +
+		VMGCNT_ADD(forks, 1);
+		VMGCNT_ADD(forkpages, p2->p_vmspace->vm_dsize +
 		    p2->p_vmspace->vm_ssize);
 	} else if (p1 == &proc0) {
-		atomic_add_int(&cnt.v_kthreads, 1);
-		atomic_add_int(&cnt.v_kthreadpages, p2->p_vmspace->vm_dsize +
+		VMGCNT_ADD(kthreads, 1);
+		VMGCNT_ADD(kthreadpages, p2->p_vmspace->vm_dsize +
 		    p2->p_vmspace->vm_ssize);
 	} else {
-		atomic_add_int(&cnt.v_rforks, 1);
-		atomic_add_int(&cnt.v_rforkpages, p2->p_vmspace->vm_dsize +
+		VMGCNT_ADD(rforks, 1);
+		VMGCNT_ADD(rforkpages, p2->p_vmspace->vm_dsize +
 		    p2->p_vmspace->vm_ssize);
 	}
 
--- //depot/vendor/freebsd/src/sys/kern/kern_malloc.c	2007/04/21 01:17:04
+++ //depot/user/attilio/attilio_schedlock/kern/kern_malloc.c	2007/05/10 15:47:06
@@ -551,7 +551,7 @@
 	 * so make sure that there is enough space.
 	 */
 	vm_kmem_size = VM_KMEM_SIZE + nmbclusters * PAGE_SIZE;
-	mem_size = cnt.v_page_count;
+	mem_size = VMGCNT_GET(page_count);
 
 #if defined(VM_KMEM_SIZE_SCALE)
 	vm_kmem_size_scale = VM_KMEM_SIZE_SCALE;
@@ -589,8 +589,8 @@
 	 * to something sane. Be careful to not overflow the 32bit
 	 * ints while doing the check.
 	 */
-	if (((vm_kmem_size / 2) / PAGE_SIZE) > cnt.v_page_count)
-		vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE;
+	if (((vm_kmem_size / 2) / PAGE_SIZE) > VMGCNT_GET(page_count))
+		vm_kmem_size = 2 * VMGCNT_GET(page_count) * PAGE_SIZE;
 
 	/*
 	 * Tune settings based on the kernel map's size at this time.
@@ -650,7 +650,8 @@
 	struct malloc_type_internal *mtip;
 	struct malloc_type *mtp;
 
-	KASSERT(cnt.v_page_count != 0, ("malloc_register before vm_init"));
+	KASSERT(VMGCNT_GET(page_count) != 0,
+	    ("malloc_register before vm_init"));
 
 	mtp = data;
 	mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO);
--- //depot/vendor/freebsd/src/sys/kern/kern_mib.c	2007/04/09 19:23:10
+++ //depot/user/attilio/attilio_schedlock/kern/kern_mib.c	2007/05/10 15:47:06
@@ -175,7 +175,7 @@
 {
 	u_long val;
 
-	val = ctob(physmem - cnt.v_wire_count);
+	val = ctob(physmem - VMGCNT_GET(wire_count));
 	return (sysctl_handle_long(oidp, &val, 0, req));
 }
 
--- //depot/vendor/freebsd/src/sys/kern/kern_proc.c	2007/03/23 04:03:03
+++ //depot/user/attilio/attilio_schedlock/kern/kern_proc.c	2007/04/14 23:22:40
@@ -620,7 +620,8 @@
 static void
 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
 {
-	struct thread *td0;
+	uint64_t calc_rtime;
+	struct thread *td0, *td1;
 	struct tty *tp;
 	struct session *sp;
 	struct ucred *cred;
@@ -693,7 +694,10 @@
 	kp->ki_swtime = p->p_swtime;
 	kp->ki_pid = p->p_pid;
 	kp->ki_nice = p->p_nice;
-	kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
+	calc_rtime = 0;
+	FOREACH_THREAD_IN_PROC(p, td1)
+		calc_rtime += td1->td_runtime;
+	kp->ki_runtime = cputick2usec(calc_rtime);
 	mtx_unlock_spin(&sched_lock);
 	if ((p->p_sflag & PS_INMEM) && p->p_stats != NULL) {
 		kp->ki_start = p->p_stats->p_start;
--- //depot/vendor/freebsd/src/sys/kern/kern_resource.c	2007/03/05 13:12:17
+++ //depot/user/attilio/attilio_schedlock/kern/kern_resource.c	2007/04/15 12:23:45
@@ -800,23 +800,21 @@
 	mtx_assert(&sched_lock, MA_NOTOWNED);
 	mtx_lock_spin(&sched_lock);
 
-	/*
-	 * If we are getting stats for the current process, then add in the
-	 * stats that this thread has accumulated in its current time slice.
-	 * We reset the thread and CPU state as if we had performed a context
-	 * switch right here.
-	 */
 	if (curthread->td_proc == p) {
-		td = curthread;
 		u = cpu_ticks();
 		p->p_rux.rux_runtime += u - PCPU_GET(switchtime);
 		PCPU_SET(switchtime, u);
+	}
+
+	FOREACH_THREAD_IN_PROC(p, td) {
+		p->p_rux.rux_runtime += td->td_runtime;
+		p->p_rux.rux_sticks += td->td_sticks;
 		p->p_rux.rux_uticks += td->td_uticks;
+		p->p_rux.rux_iticks += td->td_iticks;
+		td->td_runtime = 0;
+		td->td_sticks = 0;
 		td->td_uticks = 0;
-		p->p_rux.rux_iticks += td->td_iticks;
 		td->td_iticks = 0;
-		p->p_rux.rux_sticks += td->td_sticks;
-		td->td_sticks = 0;
 	}
 	/* Work on a copy of p_rux so we can let go of sched_lock */
 	rux = p->p_rux;
--- //depot/vendor/freebsd/src/sys/kern/kern_synch.c	2007/05/08 21:52:37
+++ //depot/user/attilio/attilio_schedlock/kern/kern_synch.c	2007/05/10 15:47:06
@@ -370,8 +370,8 @@
 void
 mi_switch(int flags, struct thread *newtd)
 {
-	uint64_t new_switchtime;
-	struct thread *td;
+	uint64_t new_switchtime, calc_rtime;
+	struct thread *td, *td1;
 	struct proc *p;
 
 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
@@ -410,13 +410,7 @@
 	 * process was running, and add that to its total so far.
 	 */
 	new_switchtime = cpu_ticks();
-	p->p_rux.rux_runtime += (new_switchtime - PCPU_GET(switchtime));
-	p->p_rux.rux_uticks += td->td_uticks;
-	td->td_uticks = 0;
-	p->p_rux.rux_iticks += td->td_iticks;
-	td->td_iticks = 0;
-	p->p_rux.rux_sticks += td->td_sticks;
-	td->td_sticks = 0;
+	td->td_runtime += (new_switchtime - PCPU_GET(switchtime));
 
 	td->td_generation++;	/* bump preempt-detect counter */
 
@@ -424,8 +418,11 @@
 	 * Check if the process exceeds its cpu resource allocation.  If
 	 * it reaches the max, arrange to kill the process in ast().
 	 */
+	calc_rtime = 0;
+	FOREACH_THREAD_IN_PROC(p, td1)
+		calc_rtime += td1->td_runtime;
 	if (p->p_cpulimit != RLIM_INFINITY &&
-	    p->p_rux.rux_runtime >= p->p_cpulimit * cpu_tickrate()) {
+	    calc_rtime >= p->p_cpulimit * cpu_tickrate()) {
 		p->p_sflag |= PS_XCPU;
 		td->td_flags |= TDF_ASTPENDING;
 	}
@@ -433,7 +430,7 @@
 	/*
 	 * Finish up stats for outgoing thread.
 	 */
-	cnt.v_swtch++;
+	VMGCNT_ADD(swtch, 1);
 	PCPU_SET(switchtime, new_switchtime);
 	PCPU_SET(switchticks, ticks);
 	CTR4(KTR_PROC, "mi_switch: old thread %ld (kse %p, pid %ld, %s)",
--- //depot/vendor/freebsd/src/sys/kern/kern_thread.c	2007/03/27 16:53:01
+++ //depot/user/attilio/attilio_schedlock/kern/kern_thread.c	2007/05/10 15:47:06
@@ -401,13 +401,14 @@
 
 	/* Do the same timestamp bookkeeping that mi_switch() would do. */
 	new_switchtime = cpu_ticks();
-	p->p_rux.rux_runtime += (new_switchtime - PCPU_GET(switchtime));
+	p->p_rux.rux_runtime += td->td_runtime + new_switchtime -
+	    PCPU_GET(switchtime);
 	p->p_rux.rux_uticks += td->td_uticks;
 	p->p_rux.rux_sticks += td->td_sticks;
 	p->p_rux.rux_iticks += td->td_iticks;
 	PCPU_SET(switchtime, new_switchtime);
 	PCPU_SET(switchticks, ticks);
-	cnt.v_swtch++;
+	VMGCNT_ADD(swtch, 1);
 
 	/* Add our usage into the usage of all our children. */
 	if (p->p_numthreads == 1)
--- //depot/vendor/freebsd/src/sys/kern/subr_prof.c	2007/03/04 22:41:05
+++ //depot/user/attilio/attilio_schedlock/kern/subr_prof.c	2007/04/11 19:21:40
@@ -423,12 +423,12 @@
 	}
 	PROC_LOCK(p);
 	upp = &td->td_proc->p_stats->p_prof;
-	mtx_lock_spin(&sched_lock);
+	mtx_lock_spin(&time_lock);
 	upp->pr_off = uap->offset;
 	upp->pr_scale = uap->scale;
 	upp->pr_base = uap->samples;
 	upp->pr_size = uap->size;
-	mtx_unlock_spin(&sched_lock);
+	mtx_unlock_spin(&time_lock);
 	startprofclock(p);
 	PROC_UNLOCK(p);
 
@@ -468,15 +468,15 @@
 	if (ticks == 0)
 		return;
 	prof = &td->td_proc->p_stats->p_prof;
-	mtx_lock_spin(&sched_lock);
+	mtx_lock_spin(&time_lock);
 	if (pc < prof->pr_off ||
 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) {
-		mtx_unlock_spin(&sched_lock);		
+		mtx_unlock_spin(&time_lock);		
 		return;			/* out of range; ignore */
 	}
 
 	addr = prof->pr_base + i;
-	mtx_unlock_spin(&sched_lock);
+	mtx_unlock_spin(&time_lock);
 	if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) {
 		td->td_profil_addr = pc;
 		td->td_profil_ticks = ticks;
--- //depot/vendor/freebsd/src/sys/kern/subr_trap.c	2007/03/04 22:41:05
+++ //depot/user/attilio/attilio_schedlock/kern/subr_trap.c	2007/05/10 15:47:06
@@ -145,7 +145,8 @@
 void
 ast(struct trapframe *framep)
 {
-	struct thread *td;
+	uint64_t calc_rtime;
+	struct thread *td, *td1;
 	struct proc *p;
 	struct rlimit rlim;
 	int sflag;
@@ -191,8 +192,8 @@
 #endif
 	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK |
 	    TDF_NEEDRESCHED | TDF_INTERRUPT);
-	cnt.v_trap++;
 	mtx_unlock_spin(&sched_lock);
+	VMGCNT_ADD(trap, 1);
 
 	/*
 	 * XXXKSE While the fact that we owe a user profiling
@@ -234,8 +235,11 @@
 	if (sflag & PS_XCPU) {
 		PROC_LOCK(p);
 		lim_rlimit(p, RLIMIT_CPU, &rlim);
+		calc_rtime = 0;
 		mtx_lock_spin(&sched_lock);
-		if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) {
+		FOREACH_THREAD_IN_PROC(p, td1)
+			calc_rtime += td1->td_runtime;
+		if (calc_rtime >= rlim.rlim_max * cpu_tickrate()) {
 			mtx_unlock_spin(&sched_lock);
 			killproc(p, "exceeded maximum CPU limit");
 		} else {
--- //depot/vendor/freebsd/src/sys/kern/subr_witness.c	2007/04/19 08:07:15
+++ //depot/user/attilio/attilio_schedlock/kern/subr_witness.c	2007/05/04 11:08:21
@@ -394,6 +394,7 @@
 	{ "sio", &lock_class_mtx_spin },
 #ifdef __i386__
 	{ "cy", &lock_class_mtx_spin },
+	{ "descriptor tables", &lock_class_mtx_spin },
 #endif
 	{ "scc_hwmtx", &lock_class_mtx_spin },
 	{ "uart_hwmtx", &lock_class_mtx_spin },
@@ -410,6 +411,8 @@
 	{ "callout", &lock_class_mtx_spin },
 	{ "entropy harvest mutex", &lock_class_mtx_spin },
 	{ "syscons video lock", &lock_class_mtx_spin },
+	{ "time lock", &lock_class_mtx_spin },
+	{ "resource manager", &lock_class_mtx_spin },
 	/*
 	 * leaf locks
 	 */
--- //depot/vendor/freebsd/src/sys/kern/vfs_bio.c	2007/04/24 11:04:30
+++ //depot/user/attilio/attilio_schedlock/kern/vfs_bio.c	2007/05/10 15:47:06
@@ -2919,8 +2919,10 @@
 				 */
 				if ((curproc != pageproc) &&
 				    (VM_PAGE_INQUEUE1(m, PQ_CACHE)) &&
-				    ((cnt.v_free_count + cnt.v_cache_count) <
-			 		(cnt.v_free_min + cnt.v_cache_min))) {
+				    ((VMGCNT_GET(free_count) +
+				    VMGCNT_GET(cache_count)) <
+				    (VMGCNT_GET(free_min) +
+				    VMGCNT_GET(cache_min)))) {
 					pagedaemon_wakeup();
 				}
 				vm_page_wire(m);
--- //depot/vendor/freebsd/src/sys/kern/vfs_subr.c	2007/04/13 23:57:04
+++ //depot/user/attilio/attilio_schedlock/kern/vfs_subr.c	2007/05/10 15:47:06
@@ -297,8 +297,9 @@
 	 * of the kernel's heap size is consumed by vnodes and vm
 	 * objects.
 	 */
-	desiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 * vm_kmem_size /
-	    (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
+	desiredvnodes = min(maxproc + VMGCNT_GET(page_count) / 4, 2 *
+	    vm_kmem_size / (5 * (sizeof(struct vm_object) +
+	    sizeof(struct vnode))));
 	if (desiredvnodes > MAXVNODES_MAX) {
 		if (bootverbose)
 			printf("Reducing kern.maxvnodes %d -> %d\n",
@@ -581,7 +582,7 @@
 	usevnodes = desiredvnodes;
 	if (usevnodes <= 0)
 		usevnodes = 1;
-	trigger = cnt.v_page_count * 2 / usevnodes;
+	trigger = VMGCNT_GET(page_count) * 2 / usevnodes;
 	done = 0;
 	td = curthread;
 	vn_start_write(NULL, &mp, V_WAIT);
--- //depot/vendor/freebsd/src/sys/nfsclient/nfs_bio.c	2007/04/25 20:37:13
+++ //depot/user/attilio/attilio_schedlock/nfsclient/nfs_bio.c	2007/05/10 15:47:06
@@ -159,8 +159,8 @@
 
 	kva = (vm_offset_t) bp->b_data;
 	pmap_qenter(kva, pages, npages);
-	cnt.v_vnodein++;
-	cnt.v_vnodepgsin += npages;
+	VMGCNT_ADD(vnodein, 1);
+	VMGCNT_ADD(vnodepgsin, npages);
 
 	iov.iov_base = (caddr_t) kva;
 	iov.iov_len = count;
@@ -323,8 +323,8 @@
 
 	kva = (vm_offset_t) bp->b_data;
 	pmap_qenter(kva, pages, npages);
-	cnt.v_vnodeout++;
-	cnt.v_vnodepgsout += count;
+	VMGCNT_ADD(vnodeout, 1);
+	VMGCNT_ADD(vnodepgsout, count);
 
 	iov.iov_base = (caddr_t) kva;
 	iov.iov_len = count;
--- //depot/vendor/freebsd/src/sys/pc98/pc98/machdep.c	2007/03/18 03:27:21
+++ //depot/user/attilio/attilio_schedlock/pc98/pc98/machdep.c	2007/05/10 15:47:06
@@ -244,8 +244,8 @@
 	vm_ksubmap_init(&kmi);
 
 	printf("avail memory = %ju (%ju MB)\n",
-	    ptoa((uintmax_t)cnt.v_free_count),
-	    ptoa((uintmax_t)cnt.v_free_count) / 1048576);
+	    ptoa((uintmax_t)VMGCNT_GET(free_count)),
+	    ptoa((uintmax_t)VMGCNT_GET(free_count)) / 1048576);
 
 	/*
 	 * Set up buffers, so they can be used to read disk labels.
--- //depot/vendor/freebsd/src/sys/powerpc/powerpc/machdep.c	2007/02/12 09:03:53
+++ //depot/user/attilio/attilio_schedlock/powerpc/powerpc/machdep.c	2007/05/10 15:47:06
@@ -223,8 +223,8 @@
 
 	vm_ksubmap_init(&kmi);
 
-	printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
-	    ptoa(cnt.v_free_count) / 1048576);
+	printf("avail memory = %ld (%ld MB)\n", ptoa(VMGCNT_GET(free_count)),
+	    ptoa(VMGCNT_GET(free_count)) / 1048576);
 
 	/*
 	 * Set up buffers, so they can be used to read disk labels.
--- //depot/vendor/freebsd/src/sys/sparc64/sparc64/machdep.c	2007/05/01 16:22:38
+++ //depot/user/attilio/attilio_schedlock/sparc64/sparc64/machdep.c	2007/05/10 15:47:06
@@ -211,8 +211,8 @@
 	EVENTHANDLER_REGISTER(shutdown_final, sparc64_shutdown_final, NULL,
 	    SHUTDOWN_PRI_LAST);
 
-	printf("avail memory = %lu (%lu MB)\n", cnt.v_free_count * PAGE_SIZE,
-	    cnt.v_free_count / ((1024 * 1024) / PAGE_SIZE));
+	printf("avail memory = %lu (%lu MB)\n", VMGCNT_GET(free_count) *
+	    PAGE_SIZE, VMGCNT_GET(free_count) / ((1024 * 1024) / PAGE_SIZE));
 
 	if (bootverbose)
 		printf("machine: %s\n", sparc64_model);
--- //depot/vendor/freebsd/src/sys/sparc64/sparc64/pmap.c	2006/11/12 21:52:34
+++ //depot/user/attilio/attilio_schedlock/sparc64/sparc64/pmap.c	2007/05/10 15:47:06
@@ -1088,7 +1088,7 @@
 		    ("pmap_release: freeing held tsb page"));
 		m->md.pmap = NULL;
 		m->wire_count--;
-		atomic_subtract_int(&cnt.v_wire_count, 1);
+		VMGCNT_DEC(wire_count, 1);
 		vm_page_free_zero(m);
 		vm_page_unlock_queues();
 	}
--- //depot/vendor/freebsd/src/sys/sun4v/sun4v/machdep.c	2006/12/06 06:37:04
+++ //depot/user/attilio/attilio_schedlock/sun4v/sun4v/machdep.c	2007/05/10 15:47:06
@@ -223,8 +223,8 @@
 	EVENTHANDLER_REGISTER(shutdown_final, sparc64_shutdown_final, NULL,
 	    SHUTDOWN_PRI_LAST);
 
-	printf("avail memory = %lu (%lu MB)\n", cnt.v_free_count * PAGE_SIZE,
-	    cnt.v_free_count / ((1024 * 1024) / PAGE_SIZE));
+	printf("avail memory = %lu (%lu MB)\n", VMGCNT_GET(free_count) *
+	    PAGE_SIZE, VMGCNT_GET(free_count) / ((1024 * 1024) / PAGE_SIZE));
 
 	if (bootverbose)
 		printf("machine: %s\n", sparc64_model);
--- //depot/vendor/freebsd/src/sys/sun4v/sun4v/pmap.c	2006/12/31 02:51:44
+++ //depot/user/attilio/attilio_schedlock/sun4v/sun4v/pmap.c	2007/05/10 15:47:06
@@ -975,7 +975,7 @@
 	 * way below the low water mark of free pages or way
 	 * above high water mark of used pv entries.
 	 */
-	if (cnt.v_free_count < cnt.v_free_reserved ||
+	if (VMGCNT_GET(free_count) < VMGCNT_GET(free_reserved) ||
 	    pv_entry_count > pv_entry_high_water)
 		return;
 	
@@ -1318,7 +1318,7 @@
 	m = PHYS_TO_VM_PAGE(TLB_DIRECT_TO_PHYS((vm_offset_t)ptr));
 	for (i = 0; i < npages; i++, m++) {
 		m->wire_count--;
-		atomic_subtract_int(&cnt.v_wire_count, 1);
+		VMGCNT_DEC(wire_count, 1);
 		vm_page_free(m);
 	}
 }
@@ -1349,7 +1349,7 @@
 	pvzone = uma_zcreate("PV ENTRY", sizeof(struct pv_entry), NULL, NULL, 
 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
 	TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
-	pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
+	pv_entry_max = shpgperproc * maxproc + VMGCNT_GET(page_count);
 	TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
 	pv_entry_high_water = 9 * (pv_entry_max / 10);
 	uma_zone_set_obj(pvzone, &pvzone_obj, pv_entry_max);
--- //depot/vendor/freebsd/src/sys/sun4v/sun4v/tsb.c	2006/12/18 07:52:23
+++ //depot/user/attilio/attilio_schedlock/sun4v/sun4v/tsb.c	2007/05/10 15:47:06
@@ -104,7 +104,7 @@
 	m = PHYS_TO_VM_PAGE((vm_paddr_t)hvtsb->hti_ra);
 	for (i = 0, tm = m; i < TSB_SIZE; i++, m++) {
 		tm->wire_count--;
-		atomic_subtract_int(&cnt.v_wire_count, 1);
+		VMGCNT_DEC(wire_count, 1);
 		vm_page_free(tm);
 	}
 }
--- //depot/vendor/freebsd/src/sys/sun4v/sun4v/tte_hash.c	2006/12/17 01:03:05
+++ //depot/user/attilio/attilio_schedlock/sun4v/sun4v/tte_hash.c	2007/05/10 15:47:06
@@ -231,7 +231,7 @@
         for (fh = ptr; fh != NULL; fh = fh->thf_head.fh_next) {
                 m = PHYS_TO_VM_PAGE(TLB_DIRECT_TO_PHYS((vm_offset_t)fh));
                 m->wire_count--;
-		atomic_subtract_int(&cnt.v_wire_count, 1);
+		VMGCNT_GET(wire_count, 1);
                 vm_page_free(m);
         }
 }
--- //depot/vendor/freebsd/src/sys/sys/proc.h	2007/04/24 11:04:30
+++ //depot/user/attilio/attilio_schedlock/sys/proc.h	2007/05/04 11:08:21
@@ -253,12 +253,13 @@
 	struct ucred	*td_ucred;	/* (k) Reference to credentials. */
 	struct thread	*td_standin;	/* (k + a) Use this for an upcall. */
 	struct kse_upcall *td_upcall;	/* (k + j) Upcall structure. */
+	uint64_t	td_runtime;	/* (j) Runtime collected. */
+	uint64_t	td_sticks;	/* (j) Statclock hits in system mode. */
+	uint64_t	td_iticks;	/* (j) Statclock hits in intr mode. */
+	uint64_t	td_uticks;	/* (j) Statclock hits in user mode. */
 	u_int		td_estcpu;	/* (j) Sum of the same field in KSEs. */
 	u_int		td_slptime;	/* (j) How long completely blocked. */
 	u_int		td_pticks;	/* (k) Statclock hits for profiling */
-	u_int		td_sticks;	/* (k) Statclock hits in system mode. */
-	u_int		td_iticks;	/* (k) Statclock hits in intr mode. */
-	u_int		td_uticks;	/* (k) Statclock hits in user mode. */
 	u_int		td_uuticks;	/* (k) Statclock hits (usr), for UTS. */
 	u_int		td_usticks;	/* (k) Statclock hits (sys), for UTS. */
 	int		td_intrval;	/* (j) Return value of TDF_INTERRUPT. */
--- //depot/vendor/freebsd/src/sys/sys/refcount.h	2005/09/27 18:05:29
+++ //depot/user/attilio/attilio_schedlock/sys/refcount.h	2007/01/28 20:01:15
@@ -41,6 +41,13 @@
 	*count = value;
 }
 
+static __inline int
+refcount_value(volatile u_int *count)
+{
+
+	return (*count);
+}
+
 static __inline void
 refcount_acquire(volatile u_int *count)
 {
--- //depot/vendor/freebsd/src/sys/sys/resourcevar.h	2005/12/16 22:10:20
+++ //depot/user/attilio/attilio_schedlock/sys/resourcevar.h	2007/04/14 22:17:09
@@ -121,6 +121,7 @@
 	*lim_hold(struct plimit *limp);
 rlim_t	 lim_max(struct proc *p, int which);
 void	 lim_rlimit(struct proc *p, int which, struct rlimit *rlp);
+void	 resinit(void);
 void	 ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2,
 	    struct rusage_ext *rux2);
 int	 suswintr(void *base, int word);
--- //depot/vendor/freebsd/src/sys/sys/systm.h	2007/04/08 23:58:46
+++ //depot/user/attilio/attilio_schedlock/sys/systm.h	2007/04/11 19:21:40
@@ -71,6 +71,8 @@
 
 extern int maxusers;		/* system tune hint */
 
+extern struct mtx time_lock;	/* time lock for profiling */
+
 #ifdef	INVARIANTS		/* The option is always available */
 #define	KASSERT(exp,msg) do {						\
 	if (__predict_false(!(exp)))					\
--- //depot/vendor/freebsd/src/sys/sys/vmmeter.h	2006/11/28 12:47:06
+++ //depot/user/attilio/attilio_schedlock/sys/vmmeter.h	2007/05/10 22:48:38
@@ -102,7 +102,17 @@
 };
 #ifdef _KERNEL
 
-extern struct vmmeter cnt;
+extern volatile struct vmmeter cnt;
+
+#define	VMGCNT_SET(member, val)						\
+	atomic_store_rel_int(__CONCAT(&cnt.v_, member), val)
+#define	VMGCNT_ADD(member, val)						\
+	atomic_add_int(__CONCAT(&cnt.v_, member), val)
+#define	VMGCNT_DEC(member, val)						\
+	atomic_subtract_int(__CONCAT(&cnt.v_, member), val)
+#define	VMGCNT_GET(member)	__CONCAT(cnt.v_, member)
+#define	VMGCNT_PTR(member)						\
+	__DEVOLATILE(u_int *, __CONCAT(&cnt.v_, member))
 
 /*
  * Return TRUE if we are under our reserved low-free-pages threshold
@@ -112,7 +122,8 @@
 int
 vm_page_count_reserved(void)
 {
-    return (cnt.v_free_reserved > (cnt.v_free_count + cnt.v_cache_count));
+    return (VMGCNT_GET(free_reserved) > (VMGCNT_GET(free_count) +
+        VMGCNT_GET(cache_count)));
 }
 
 /*
@@ -126,7 +137,8 @@
 int
 vm_page_count_severe(void)
 {
-    return (cnt.v_free_severe > (cnt.v_free_count + cnt.v_cache_count));
+    return (VMGCNT_GET(free_severe) > (VMGCNT_GET(free_count) +
+        VMGCNT_GET(cache_count)));
 }
 
 /*
@@ -143,7 +155,8 @@
 int
 vm_page_count_min(void)
 {
-    return (cnt.v_free_min > (cnt.v_free_count + cnt.v_cache_count));
+    return (VMGCNT_GET(free_min) > (VMGCNT_GET(free_count) +
+        VMGCNT_GET(cache_count)));
 }
 
 /*
@@ -155,7 +168,8 @@
 int
 vm_page_count_target(void)
 {
-    return (cnt.v_free_target > (cnt.v_free_count + cnt.v_cache_count));
+    return (VMGCNT_GET(free_target) > (VMGCNT_GET(free_count) +
+        VMGCNT_GET(cache_count)));
 }
 
 /*
@@ -168,8 +182,8 @@
 vm_paging_target(void)
 {
     return (
-	(cnt.v_free_target + cnt.v_cache_min) - 
-	(cnt.v_free_count + cnt.v_cache_count)
+	(VMGCNT_GET(free_target) + VMGCNT_GET(cache_min)) - 
+	(VMGCNT_GET(free_count) + VMGCNT_GET(cache_count))
     );
 }
 
@@ -182,8 +196,8 @@
 vm_paging_needed(void)
 {
     return (
-	(cnt.v_free_reserved + cnt.v_cache_min) >
-	(cnt.v_free_count + cnt.v_cache_count)
+	(VMGCNT_GET(free_reserved) + VMGCNT_GET(cache_min)) >
+	(VMGCNT_GET(free_count) + VMGCNT_GET(cache_count))
     );
 }
 
--- //depot/vendor/freebsd/src/sys/vm/swap_pager.c	2007/04/23 14:47:16
+++ //depot/user/attilio/attilio_schedlock/vm/swap_pager.c	2007/05/10 15:47:06
@@ -385,7 +385,7 @@
 	 * can hold 16 pages, so this is probably overkill.  This reservation
 	 * is typically limited to around 32MB by default.
 	 */
-	n = cnt.v_page_count / 2;
+	n = VMGCNT_GET(page_count) / 2;
 	if (maxswzone && n > maxswzone / sizeof(struct swblock))
 		n = maxswzone / sizeof(struct swblock);
 	n2 = n;
@@ -1037,8 +1037,8 @@
 	}
 	bp->b_npages = j - i;
 
-	cnt.v_swapin++;
-	cnt.v_swappgsin += bp->b_npages;
+	VMGCNT_ADD(swapin, 1);
+	VMGCNT_ADD(swappgsin, bp->b_npages);
 
 	/*
 	 * We still hold the lock on mreq, and our automatic completion routine
@@ -1072,7 +1072,7 @@
 		vm_page_lock_queues();
 		vm_page_flag_set(mreq, PG_REFERENCED);
 		vm_page_unlock_queues();
-		cnt.v_intrans++;
+		VMGCNT_ADD(intrans, 1);
 		if (msleep(mreq, VM_OBJECT_MTX(object), PSWP, "swread", hz*20)) {
 			printf(
 "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n",
@@ -1263,8 +1263,8 @@
 		bp->b_dirtyoff = 0;
 		bp->b_dirtyend = bp->b_bcount;
 
-		cnt.v_swapout++;
-		cnt.v_swappgsout += bp->b_npages;
+		VMGCNT_ADD(swapout, 1);
+		VMGCNT_ADD(swappgsout, bp->b_npages);
 
 		/*
 		 * asynchronous
@@ -2135,8 +2135,8 @@
 	 * of data we will have to page back in, plus an epsilon so
 	 * the system doesn't become critically low on swap space.
 	 */
-	if (cnt.v_free_count + cnt.v_cache_count + swap_pager_avail <
-	    nblks + nswap_lowat) {
+	if (VMGCNT_GET(free_count) + VMGCNT_GET(cache_count) +
+	    swap_pager_avail < nblks + nswap_lowat) {
 		return (ENOMEM);
 	}
 
--- //depot/vendor/freebsd/src/sys/vm/uma_core.c	2007/02/11 20:17:10
+++ //depot/user/attilio/attilio_schedlock/vm/uma_core.c	2007/05/10 15:47:06
@@ -271,7 +271,7 @@
 static void
 bucket_enable(void)
 {
-	if (cnt.v_free_count < cnt.v_free_min)
+	if (VMGCNT_GET(free_count) < VMGCNT_GET(free_min))
 		bucketdisable = 1;
 	else
 		bucketdisable = 0;
--- //depot/vendor/freebsd/src/sys/vm/vm_contig.c	2007/04/19 05:41:36
+++ //depot/user/attilio/attilio_schedlock/vm/vm_contig.c	2007/05/10 15:47:06
@@ -204,7 +204,7 @@
 		 * Find first page in array that is free, within range,
 		 * aligned, and such that the boundary won't be crossed.
 		 */
-		for (i = start; i < cnt.v_page_count; i++) {
+		for (i = start; i < VMGCNT_GET(page_count); i++) {
 			phys = VM_PAGE_TO_PHYS(&pga[i]);
 			pqtype = pga[i].queue - pga[i].pc;
 			if (((pqtype == PQ_FREE) || (pqtype == PQ_CACHE)) &&
@@ -217,7 +217,7 @@
 		/*
 		 * If the above failed or we will exceed the upper bound, fail.
 		 */
-		if ((i == cnt.v_page_count) ||
+		if ((i == VMGCNT_GET(page_count)) ||
 			((VM_PAGE_TO_PHYS(&pga[i]) + size) > high)) {
 			mtx_unlock(&vm_page_queue_free_mtx);
 			/*
--- //depot/vendor/freebsd/src/sys/vm/vm_fault.c	2007/04/05 20:52:22
+++ //depot/user/attilio/attilio_schedlock/vm/vm_fault.c	2007/05/10 15:47:06
@@ -1271,7 +1271,8 @@
 	 * try to do any readahead that we might have free pages for.
 	 */
 	if ((rahead + rbehind) >
-		((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) {
+		((VMGCNT_GET(free_count) + VMGCNT_GET(cache_count)) -
+		VMGCNT_GET(free_reserved))) {
 		pagedaemon_wakeup();
 		marray[0] = m;
 		*reqpage = 0;
--- //depot/vendor/freebsd/src/sys/vm/vm_glue.c	2007/01/23 08:52:34
+++ //depot/user/attilio/attilio_schedlock/vm/vm_glue.c	2007/05/10 15:47:06
@@ -219,7 +219,7 @@
 	 * Also, the sysctl code, which is the only present user
 	 * of vslock(), does a hard loop on EAGAIN.
 	 */
-	if (npages + cnt.v_wire_count > vm_page_max_wired)
+	if (npages + VMGCNT_GET(wire_count) > vm_page_max_wired)
 		return (EAGAIN);
 #endif
 	error = vm_map_wire(&curproc->p_vmspace->vm_map, start, end,
@@ -589,7 +589,7 @@
 	limp->pl_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz;
 	limp->pl_rlimit[RLIMIT_DATA].rlim_max = maxdsiz;
 	/* limit the limit to no less than 2MB */
-	rss_limit = max(cnt.v_free_count, 512);
+	rss_limit = max(VMGCNT_GET(free_count), 512);
 	limp->pl_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit);
 	limp->pl_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY;
 }
--- //depot/vendor/freebsd/src/sys/vm/vm_map.c	2007/04/26 05:53:38
+++ //depot/user/attilio/attilio_schedlock/vm/vm_map.c	2007/05/10 15:47:06
@@ -274,7 +274,7 @@
 void
 vm_init2(void)
 {
-	uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
+	uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(VMGCNT_GET(page_count),
 	    (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8 +
 	     maxproc * 2 + maxfiles);
 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
@@ -1488,7 +1488,7 @@
 		 * free pages allocating pv entries.
 		 */
 		if ((flags & MAP_PREFAULT_MADVISE) &&
-		    cnt.v_free_count < cnt.v_free_reserved) {
+		    VMGCNT_GET(free_count) < VMGCNT_GET(free_reserved)) {
 			psize = tmpidx;
 			break;
 		}
--- //depot/vendor/freebsd/src/sys/vm/vm_meter.c	2006/11/20 08:36:24
+++ //depot/user/attilio/attilio_schedlock/vm/vm_meter.c	2007/05/10 19:54:37
@@ -52,26 +52,26 @@
 #include <vm/vm_object.h>
 #include <sys/sysctl.h>
 
-struct vmmeter cnt;
+volatile struct vmmeter cnt;
 
 int maxslp = MAXSLP;
 
 SYSCTL_UINT(_vm, VM_V_FREE_MIN, v_free_min,
-	CTLFLAG_RW, &cnt.v_free_min, 0, "");
+	CTLFLAG_RW, VMGCNT_PTR(free_min), 0, "");
 SYSCTL_UINT(_vm, VM_V_FREE_TARGET, v_free_target,
-	CTLFLAG_RW, &cnt.v_free_target, 0, "");
+	CTLFLAG_RW, VMGCNT_PTR(free_target), 0, "");
 SYSCTL_UINT(_vm, VM_V_FREE_RESERVED, v_free_reserved,
-	CTLFLAG_RW, &cnt.v_free_reserved, 0, "");
+	CTLFLAG_RW, VMGCNT_PTR(free_reserved), 0, "");
 SYSCTL_UINT(_vm, VM_V_INACTIVE_TARGET, v_inactive_target,
-	CTLFLAG_RW, &cnt.v_inactive_target, 0, "");
+	CTLFLAG_RW, VMGCNT_PTR(inactive_target), 0, "");
 SYSCTL_UINT(_vm, VM_V_CACHE_MIN, v_cache_min,
-	CTLFLAG_RW, &cnt.v_cache_min, 0, "");
+	CTLFLAG_RW, VMGCNT_PTR(cache_min), 0, "");
 SYSCTL_UINT(_vm, VM_V_CACHE_MAX, v_cache_max,
-	CTLFLAG_RW, &cnt.v_cache_max, 0, "");
+	CTLFLAG_RW, VMGCNT_PTR(cache_max), 0, "");
 SYSCTL_UINT(_vm, VM_V_PAGEOUT_FREE_MIN, v_pageout_free_min,
-	CTLFLAG_RW, &cnt.v_pageout_free_min, 0, "");
+	CTLFLAG_RW, VMGCNT_PTR(pageout_free_min), 0, "");
 SYSCTL_UINT(_vm, OID_AUTO, v_free_severe,
-	CTLFLAG_RW, &cnt.v_free_severe, 0, "");
+	CTLFLAG_RW, VMGCNT_PTR(free_severe), 0, "");
 
 static int
 sysctl_vm_loadavg(SYSCTL_HANDLER_ARGS)
@@ -235,7 +235,7 @@
 		}
 	}
 	mtx_unlock(&vm_object_list_mtx);
-	total.t_free = cnt.v_free_count + cnt.v_cache_count;
+	total.t_free = VMGCNT_GET(free_count) + VMGCNT_GET(cache_count);
 	return (sysctl_handle_opaque(oidp, &total, sizeof(total), req));
 }
 
@@ -255,7 +255,7 @@
 vcnt(SYSCTL_HANDLER_ARGS)
 {
 	int count = *(int *)arg1;
-	int offset = (char *)arg1 - (char *)&cnt;
+	int offset = (char *)arg1 - __DEVOLATILE(char *, &cnt);
 #ifdef SMP
 	int i;
 
@@ -280,101 +280,103 @@
 SYSCTL_NODE(_vm_stats, OID_AUTO, misc, CTLFLAG_RW, 0, "VM meter misc stats");
 
 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_swtch, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_swtch, 0, vcnt, "IU", "Context switches");
+	VMGCNT_PTR(swtch), 0, vcnt, "IU", "Context switches");
 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_trap, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_trap, 0, vcnt, "IU", "Traps");
+	VMGCNT_PTR(trap), 0, vcnt, "IU", "Traps");
 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_syscall, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_syscall, 0, vcnt, "IU", "Syscalls");
+	VMGCNT_PTR(syscall), 0, vcnt, "IU", "Syscalls");
 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_intr, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_intr, 0, vcnt, "IU", "Hardware interrupts");
+	VMGCNT_PTR(intr), 0, vcnt, "IU", "Hardware interrupts");
 SYSCTL_PROC(_vm_stats_sys, OID_AUTO, v_soft, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_soft, 0, vcnt, "IU", "Software interrupts");
+	VMGCNT_PTR(soft), 0, vcnt, "IU", "Software interrupts");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vm_faults, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_vm_faults, 0, vcnt, "IU", "VM faults");
+	VMGCNT_PTR(vm_faults), 0, vcnt, "IU", "VM faults");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cow_faults, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_cow_faults, 0, vcnt, "IU", "COW faults");
+	VMGCNT_PTR(cow_faults), 0, vcnt, "IU", "COW faults");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cow_optim, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_cow_optim, 0, vcnt, "IU", "Optimized COW faults");
+	VMGCNT_PTR(cow_optim), 0, vcnt, "IU", "Optimized COW faults");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_zfod, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_zfod, 0, vcnt, "IU", "Zero fill");
+	VMGCNT_PTR(zfod), 0, vcnt, "IU", "Zero fill");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_ozfod, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_ozfod, 0, vcnt, "IU", "Optimized zero fill");
+	VMGCNT_PTR(ozfod), 0, vcnt, "IU", "Optimized zero fill");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swapin, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_swapin, 0, vcnt, "IU", "Swapin operations");
+	VMGCNT_PTR(swapin), 0, vcnt, "IU", "Swapin operations");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swapout, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_swapout, 0, vcnt, "IU", "Swapout operations");
+	VMGCNT_PTR(swapout), 0, vcnt, "IU", "Swapout operations");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swappgsin, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_swappgsin, 0, vcnt, "IU", "Swapin pages");
+	VMGCNT_PTR(swappgsin), 0, vcnt, "IU", "Swapin pages");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_swappgsout, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_swappgsout, 0, vcnt, "IU", "Swapout pages");
+	VMGCNT_PTR(swappgsout), 0, vcnt, "IU", "Swapout pages");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodein, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_vnodein, 0, vcnt, "IU", "Vnodein operations");
+	VMGCNT_PTR(vnodein), 0, vcnt, "IU", "Vnodein operations");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodeout, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_vnodeout, 0, vcnt, "IU", "Vnodeout operations");
+	VMGCNT_PTR(vnodeout), 0, vcnt, "IU", "Vnodeout operations");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodepgsin, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_vnodepgsin, 0, vcnt, "IU", "Vnodein pages");
+	VMGCNT_PTR(vnodepgsin), 0, vcnt, "IU", "Vnodein pages");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vnodepgsout, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_vnodepgsout, 0, vcnt, "IU", "Vnodeout pages");
+	VMGCNT_PTR(vnodepgsout), 0, vcnt, "IU", "Vnodeout pages");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_intrans, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_intrans, 0, vcnt, "IU", "In transit page blocking");
+	VMGCNT_PTR(intrans), 0, vcnt, "IU", "In transit page blocking");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_reactivated, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_reactivated, 0, vcnt, "IU", "Reactivated pages");
+	VMGCNT_PTR(reactivated), 0, vcnt, "IU", "Reactivated pages");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pdwakeups, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_pdwakeups, 0, vcnt, "IU", "Pagedaemon wakeups");
+	VMGCNT_PTR(pdwakeups), 0, vcnt, "IU", "Pagedaemon wakeups");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pdpages, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_pdpages, 0, vcnt, "IU", "Pagedaemon page scans");
+	VMGCNT_PTR(pdpages), 0, vcnt, "IU", "Pagedaemon page scans");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_dfree, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_dfree, 0, vcnt, "IU", "");
+	VMGCNT_PTR(dfree), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pfree, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_pfree, 0, vcnt, "IU", "");
+	VMGCNT_PTR(pfree), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_tfree, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_tfree, 0, vcnt, "IU", "");
+	VMGCNT_PTR(tfree), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_page_size, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_page_size, 0, vcnt, "IU", "");
+	VMGCNT_PTR(page_size), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_page_count, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_page_count, 0, vcnt, "IU", "");
+	VMGCNT_PTR(page_count), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_free_reserved, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_free_reserved, 0, vcnt, "IU", "");
+	VMGCNT_PTR(free_reserved), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_free_target, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_free_target, 0, vcnt, "IU", "");
+	VMGCNT_PTR(free_target), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_free_min, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_free_min, 0, vcnt, "IU", "");
+	VMGCNT_PTR(free_min), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_free_count, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_free_count, 0, vcnt, "IU", "");
+	VMGCNT_PTR(free_count), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_wire_count, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_wire_count, 0, vcnt, "IU", "");
+	VMGCNT_PTR(wire_count), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_active_count, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_active_count, 0, vcnt, "IU", "");
+	VMGCNT_PTR(active_count), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_inactive_target, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_inactive_target, 0, vcnt, "IU", "");
+	VMGCNT_PTR(inactive_target), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_inactive_count, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_inactive_count, 0, vcnt, "IU", "");
+	VMGCNT_PTR(inactive_count), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cache_count, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_cache_count, 0, vcnt, "IU", "");
+	VMGCNT_PTR(cache_count), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cache_min, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_cache_min, 0, vcnt, "IU", "");
+	VMGCNT_PTR(cache_min), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_cache_max, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_cache_max, 0, vcnt, "IU", "");
+	VMGCNT_PTR(cache_max), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_pageout_free_min, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_pageout_free_min, 0, vcnt, "IU", "");
-SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_interrupt_free_min, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_interrupt_free_min, 0, vcnt, "IU", "");
+	VMGCNT_PTR(pageout_free_min), 0, vcnt, "IU", "");
+SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_interrupt_free_min, CTLTYPE_UINT |
+	CTLFLAG_RD, VMGCNT_PTR(interrupt_free_min), 0, vcnt, "IU", "");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_forks, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_forks, 0, vcnt, "IU", "Number of fork() calls");
+	VMGCNT_PTR(forks), 0, vcnt, "IU", "Number of fork() calls");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vforks, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_vforks, 0, vcnt, "IU", "Number of vfork() calls");
+	VMGCNT_PTR(vforks), 0, vcnt, "IU", "Number of vfork() calls");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_rforks, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_rforks, 0, vcnt, "IU", "Number of rfork() calls");
+	VMGCNT_PTR(rforks), 0, vcnt, "IU", "Number of rfork() calls");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_kthreads, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_kthreads, 0, vcnt, "IU", "Number of fork() calls by kernel");
+	VMGCNT_PTR(kthreads), 0, vcnt, "IU",
+	"Number of fork() calls by kernel");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_forkpages, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_forkpages, 0, vcnt, "IU", "VM pages affected by fork()");
+	VMGCNT_PTR(forkpages), 0, vcnt, "IU", "VM pages affected by fork()");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_vforkpages, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_vforkpages, 0, vcnt, "IU", "VM pages affected by vfork()");
+	VMGCNT_PTR(vforkpages), 0, vcnt, "IU", "VM pages affected by vfork()");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_rforkpages, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_rforkpages, 0, vcnt, "IU", "VM pages affected by rfork()");
+	VMGCNT_PTR(rforkpages), 0, vcnt, "IU", "VM pages affected by rfork()");
 SYSCTL_PROC(_vm_stats_vm, OID_AUTO, v_kthreadpages, CTLTYPE_UINT|CTLFLAG_RD,
-	&cnt.v_kthreadpages, 0, vcnt, "IU", "VM pages affected by fork() by kernel");
+	VMGCNT_PTR(kthreadpages), 0, vcnt, "IU",
+	"VM pages affected by fork() by kernel");
 
 SYSCTL_INT(_vm_stats_misc, OID_AUTO,
 	zero_page_count, CTLFLAG_RD, &vm_page_zero_count, 0, "");
--- //depot/vendor/freebsd/src/sys/vm/vm_mmap.c	2006/11/06 13:43:21
+++ //depot/user/attilio/attilio_schedlock/vm/vm_mmap.c	2007/05/10 15:47:06
@@ -974,7 +974,7 @@
 		return (ENOMEM);
 	}
 	PROC_UNLOCK(proc);
-	if (npages + cnt.v_wire_count > vm_page_max_wired)
+	if (npages + VMGCNT_GET(wire_count) > vm_page_max_wired)
 		return (EAGAIN);
 	error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
 	    VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
--- //depot/vendor/freebsd/src/sys/vm/vm_object.c	2007/03/27 08:58:09
+++ //depot/user/attilio/attilio_schedlock/vm/vm_object.c	2007/05/10 15:47:06
@@ -655,7 +655,7 @@
 			"p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
 		if (p->wire_count == 0) {
 			vm_page_free(p);
-			cnt.v_pfree++;
+			VMGCNT_ADD(pfree, 1);
 		} else {
 			vm_page_remove(p);
 		}
--- //depot/vendor/freebsd/src/sys/vm/vm_page.c	2007/05/05 19:54:17
+++ //depot/user/attilio/attilio_schedlock/vm/vm_page.c	2007/05/10 22:48:38
@@ -151,9 +151,9 @@
 void
 vm_set_page_size(void)
 {
-	if (cnt.v_page_size == 0)
-		cnt.v_page_size = PAGE_SIZE;
-	if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
+	if (VMGCNT_GET(page_size) == 0)
+		VMGCNT_SET(page_size, PAGE_SIZE);
+	if (((VMGCNT_GET(page_size) - 1) & VMGCNT_GET(page_size)) != 0)
 		panic("vm_set_page_size: page size not a power of two");
 }
 
@@ -357,8 +357,8 @@
 	 * last rather than first.  On large-memory machines, this avoids
 	 * the exhaustion of low physical memory before isa_dma_init has run.
 	 */
-	cnt.v_page_count = 0;
-	cnt.v_free_count = 0;
+	VMGCNT_SET(page_count, 0);
+	VMGCNT_SET(free_count, 0);
 	list = getenv("vm.blacklist");
 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
 		pa = phys_avail[i];
@@ -874,11 +874,11 @@
 
 loop:
 	mtx_lock(&vm_page_queue_free_mtx);
-	if (cnt.v_free_count > cnt.v_free_reserved ||
+	if (VMGCNT_GET(free_count) > VMGCNT_GET(free_reserved) ||
 	    (page_req == VM_ALLOC_SYSTEM && 
-	     cnt.v_cache_count == 0 && 
-	     cnt.v_free_count > cnt.v_interrupt_free_min) ||
-	    (page_req == VM_ALLOC_INTERRUPT && cnt.v_free_count > 0)) {
+	     VMGCNT_GET(cache_count) == 0 && 
+	     VMGCNT_GET(free_count) > VMGCNT_GET(interrupt_free_min)) ||
+	    (page_req == VM_ALLOC_INTERRUPT && VMGCNT_GET(free_count) > 0)) {
 		/*
 		 * Allocate from the free queue if the number of free pages
 		 * exceeds the minimum for the request class.
@@ -893,9 +893,9 @@
 		 */
 		vm_page_lock_queues();
 		if ((m = vm_page_select_cache(color)) == NULL) {
-			KASSERT(cnt.v_cache_count == 0,
+			KASSERT(VMGCNT_GET(cache_count) == 0,
 			    ("vm_page_alloc: cache queue is missing %d pages",
-			    cnt.v_cache_count));
+			    VMGCNT_GET(cache_count)));
 			vm_page_unlock_queues();
 			atomic_add_int(&vm_pageout_deficit, 1);
 			pagedaemon_wakeup();
@@ -904,7 +904,8 @@
 				return (NULL);
 
 			mtx_lock(&vm_page_queue_free_mtx);
-			if (cnt.v_free_count <= cnt.v_interrupt_free_min) {
+			if (VMGCNT_GET(free_count) <=
+			    VMGCNT_GET(interrupt_free_min)) {
 				mtx_unlock(&vm_page_queue_free_mtx);
 				return (NULL);
 			}
@@ -954,7 +955,7 @@
 	else
 		m->oflags = VPO_BUSY;
 	if (req & VM_ALLOC_WIRED) {
-		atomic_add_int(&cnt.v_wire_count, 1);
+		VMGCNT_ADD(wire_count, 1);
 		m->wire_count = 1;
 	} else
 		m->wire_count = 0;
@@ -1000,8 +1001,8 @@
 			vm_pages_needed = 1;
 			wakeup(&vm_pages_needed);
 		}
-		msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
-		    "vmwait", 0);
+		msleep(VMGCNT_PTR(free_count), &vm_page_queue_free_mtx, PDROP |
+		    PVM, "vmwait", 0);
 	}
 }
 
@@ -1024,7 +1025,7 @@
 		vm_pages_needed = 1;
 		wakeup(&vm_pages_needed);
 	}
-	msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
+	msleep(VMGCNT_PTR(free_count), &vm_page_queue_free_mtx, PDROP | PUSER,
 	    "pfault", 0);
 }
 
@@ -1045,7 +1046,7 @@
 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 	if (VM_PAGE_GETKNOWNQUEUE2(m) != PQ_ACTIVE) {
 		if (VM_PAGE_INQUEUE1(m, PQ_CACHE))
-			cnt.v_reactivated++;
+			VMGCNT_ADD(reactivated, 1);
 		vm_pageq_remove(m);
 		if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
 			if (m->act_count < ACT_INIT)
@@ -1078,7 +1079,8 @@
 	 * some free.
 	 */
 	if (vm_pageout_pages_needed &&
-	    cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
+	    VMGCNT_GET(cache_count) + VMGCNT_GET(free_count) >=
+	    VMGCNT_GET(pageout_free_min)) {
 		wakeup(&vm_pageout_pages_needed);
 		vm_pageout_pages_needed = 0;
 	}
@@ -1089,7 +1091,7 @@
 	 */
 	if (vm_pages_needed && !vm_page_count_min()) {
 		vm_pages_needed = 0;
-		wakeup(&cnt.v_free_count);
+		wakeup(VMGCNT_PTR(free_count));
 	}
 }
 
@@ -1112,7 +1114,7 @@
 		mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 	KASSERT(!pmap_page_is_mapped(m),
 	    ("vm_page_free_toq: freeing mapped page %p", m));
-	cnt.v_tfree++;
+	VMGCNT_ADD(tfree, 1);
 
 	if (m->busy || VM_PAGE_INQUEUE1(m, PQ_FREE)) {
 		printf(
@@ -1203,7 +1205,7 @@
 	if (m->wire_count == 0) {
 		if ((m->flags & PG_UNMANAGED) == 0)
 			vm_pageq_remove(m);
-		atomic_add_int(&cnt.v_wire_count, 1);
+		VMGCNT_ADD(wire_count, 1);
 	}
 	m->wire_count++;
 	KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
@@ -1247,7 +1249,7 @@
 	if (m->wire_count > 0) {
 		m->wire_count--;
 		if (m->wire_count == 0) {
-			atomic_subtract_int(&cnt.v_wire_count, 1);
+			VMGCNT_DEC(wire_count, 1);
 			if (m->flags & PG_UNMANAGED) {
 				;
 			} else if (activate)
@@ -1286,7 +1288,7 @@
 		return;
 	if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
 		if (VM_PAGE_INQUEUE1(m, PQ_CACHE))
-			cnt.v_reactivated++;
+			VMGCNT_ADD(reactivated, 1);
 		vm_page_flag_clear(m, PG_WINATCFLS);
 		vm_pageq_remove(m);
 		if (athead)
@@ -1295,7 +1297,7 @@
 			TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
 		VM_PAGE_SETQUEUE2(m, PQ_INACTIVE);
 		vm_page_queues[PQ_INACTIVE].lcnt++;
-		cnt.v_inactive_count++;
+		VMGCNT_ADD(inactive_count, 1);
 	}
 }
 
@@ -1780,16 +1782,16 @@
 
 DB_SHOW_COMMAND(page, vm_page_print_page_info)
 {
-	db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
-	db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
-	db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
-	db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
-	db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
-	db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
-	db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
-	db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
-	db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
-	db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
+	db_printf("cnt.v_free_count: %d\n", VMGCNT_GET(free_count));
+	db_printf("cnt.v_cache_count: %d\n", VMGCNT_GET(cache_count));
+	db_printf("cnt.v_inactive_count: %d\n", VMGCNT_GET(inactive_count));
+	db_printf("cnt.v_active_count: %d\n", VMGCNT_GET(active_count));
+	db_printf("cnt.v_wire_count: %d\n", VMGCNT_GET(wire_count));
+	db_printf("cnt.v_free_reserved: %d\n", VMGCNT_GET(free_reserved));
+	db_printf("cnt.v_free_min: %d\n", VMGCNT_GET(free_min));
+	db_printf("cnt.v_free_target: %d\n", VMGCNT_GET(free_target));
+	db_printf("cnt.v_cache_min: %d\n", VMGCNT_GET(cache_min));
+	db_printf("cnt.v_inactive_target: %d\n", VMGCNT_GET(inactive_target));
 }
 
 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
--- //depot/vendor/freebsd/src/sys/vm/vm_pageout.c	2007/02/07 06:41:32
+++ //depot/user/attilio/attilio_schedlock/vm/vm_pageout.c	2007/05/10 22:48:38
@@ -538,7 +538,7 @@
 				goto unlock_return;
 			}
 			next = TAILQ_NEXT(p, listq);
-			cnt.v_pdpages++;
+			VMGCNT_ADD(pdpages, 1);
 			if (p->wire_count != 0 ||
 			    p->hold_count != 0 ||
 			    p->busy != 0 ||
@@ -739,13 +739,13 @@
 	vm_page_lock_queues();
 rescan0:
 	addl_page_shortage = addl_page_shortage_init;
-	maxscan = cnt.v_inactive_count;
+	maxscan = VMGCNT_GET(inactive_count);
 
 	for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
 	     m != NULL && maxscan-- > 0 && page_shortage > 0;
 	     m = next) {
 
-		cnt.v_pdpages++;
+		VMGCNT_ADD(pdpages, 1);
 
 		if (VM_PAGE_GETQUEUE(m) != PQ_INACTIVE) {
 			goto rescan0;
@@ -856,7 +856,7 @@
 			 * Invalid pages can be easily freed
 			 */
 			vm_page_free(m);
-			cnt.v_dfree++;
+			VMGCNT_ADD(dfree, 1);
 			--page_shortage;
 		} else if (m->dirty == 0) {
 			/*
@@ -1043,8 +1043,8 @@
 	 * Compute the number of pages we want to try to move from the
 	 * active queue to the inactive queue.
 	 */
-	page_shortage = vm_paging_target() +
-		cnt.v_inactive_target - cnt.v_inactive_count;
+	page_shortage = vm_paging_target() + VMGCNT_GET(inactive_target) -
+	    VMGCNT_GET(inactive_count);
 	page_shortage += addl_page_shortage;
 
 	/*
@@ -1052,7 +1052,7 @@
 	 * track the per-page activity counter and use it to locate
 	 * deactivation candidates.
 	 */
-	pcount = cnt.v_active_count;
+	pcount = VMGCNT_GET(active_count);
 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
 
 	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
@@ -1089,7 +1089,7 @@
 		 * The count for pagedaemon pages is done after checking the
 		 * page for eligibility...
 		 */
-		cnt.v_pdpages++;
+		VMGCNT_ADD(pdpages, 1);
 
 		/*
 		 * Check to see "how much" the page has been used.
@@ -1149,8 +1149,9 @@
 	 */
 	cache_cur = cache_last_free;
 	cache_first_failure = -1;
-	while (cnt.v_free_count < cnt.v_free_reserved && (cache_cur =
-	    (cache_cur + PQ_PRIME2) & PQ_COLORMASK) != cache_first_failure) {
+	while (VMGCNT_GET(free_count) < VMGCNT_GET(free_reserved) &&
+	    (cache_cur = (cache_cur + PQ_PRIME2) & PQ_COLORMASK) !=
+	    cache_first_failure) {
 		TAILQ_FOREACH(m, &vm_page_queues[PQ_CACHE + cache_cur].pl,
 		    pageq) {
 			KASSERT(m->dirty == 0,
@@ -1168,7 +1169,7 @@
 				    m));
 				vm_page_free(m);
 				VM_OBJECT_UNLOCK(object);
-				cnt.v_dfree++;
+				VMGCNT_ADD(dfree, 1);
 				cache_last_free = cache_cur;
 				cache_first_failure = -1;
 				break;
@@ -1291,7 +1292,7 @@
 			sched_nice(bigproc, PRIO_MIN);
 			mtx_unlock_spin(&sched_lock);
 			PROC_UNLOCK(bigproc);
-			wakeup(&cnt.v_free_count);
+			wakeup(VMGCNT_PTR(free_count));
 		}
 	}
 	mtx_unlock(&Giant);
@@ -1314,16 +1315,18 @@
 
 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 	page_shortage = 
-	    (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
-	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
+	    (VMGCNT_GET(inactive_target) + VMGCNT_GET(cache_max) +
+	    VMGCNT_GET(free_min)) - (VMGCNT_GET(free_count) +
+	    VMGCNT_GET(inactive_count) + VMGCNT_GET(cache_count));
 
 	if (page_shortage <= 0)
 		return;
 
-	pcount = cnt.v_active_count;
+	pcount = VMGCNT_GET(active_count);
 	fullintervalcount += vm_pageout_stats_interval;
 	if (fullintervalcount < vm_pageout_full_stats_interval) {
-		tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count;
+		tpcount = (vm_pageout_stats_max * VMGCNT_GET(active_count)) /
+		    VMGCNT_GET(page_count);
 		if (pcount > tpcount)
 			pcount = tpcount;
 	} else {
@@ -1409,8 +1412,8 @@
 	/*
 	 * Initialize some paging parameters.
 	 */
-	cnt.v_interrupt_free_min = 2;
-	if (cnt.v_page_count < 2000)
+	VMGCNT_SET(interrupt_free_min, 2);
+	if (VMGCNT_GET(page_count) < 2000)
 		vm_pageout_page_count = 8;
 
 	/*
@@ -1418,17 +1421,16 @@
 	 * swap pager structures plus enough for any pv_entry structs
 	 * when paging. 
 	 */
-	if (cnt.v_page_count > 1024)
-		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
-	else
-		cnt.v_free_min = 4;
-	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
-	    cnt.v_interrupt_free_min;
-	cnt.v_free_reserved = vm_pageout_page_count +
-	    cnt.v_pageout_free_min + (cnt.v_page_count / 768) + PQ_NUMCOLORS;
-	cnt.v_free_severe = cnt.v_free_min / 2;
-	cnt.v_free_min += cnt.v_free_reserved;
-	cnt.v_free_severe += cnt.v_free_reserved;
+	VMGCNT_SET(free_min, (VMGCNT_GET(page_count) > 1024) ? (4 +
+	    (VMGCNT_GET(page_count) - 1024) / 200) : 4);
+	VMGCNT_SET(pageout_free_min, (2 * MAXBSIZE) / PAGE_SIZE +
+	    VMGCNT_GET(interrupt_free_min));
+	VMGCNT_SET(free_reserved, vm_pageout_page_count +
+	    VMGCNT_GET(pageout_free_min) + (VMGCNT_GET(page_count) / 768) +
+	    PQ_NUMCOLORS);
+	VMGCNT_SET(free_severe, VMGCNT_GET(free_min) / 2);
+	VMGCNT_ADD(free_min, VMGCNT_GET(free_reserved));
+	VMGCNT_ADD(free_severe, VMGCNT_GET(free_reserved));
 
 	/*
 	 * v_free_target and v_cache_min control pageout hysteresis.  Note
@@ -1441,29 +1443,27 @@
 	 * be big enough to handle memory needs while the pageout daemon
 	 * is signalled and run to free more pages.
 	 */
-	if (cnt.v_free_count > 6144)
-		cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
-	else
-		cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
+	VMGCNT_SET(free_target, ((VMGCNT_GET(free_count) > 6144) ? 4 : 2) *
+	    VMGCNT_GET(free_min) + VMGCNT_GET(free_reserved));
 
-	if (cnt.v_free_count > 2048) {
-		cnt.v_cache_min = cnt.v_free_target;
-		cnt.v_cache_max = 2 * cnt.v_cache_min;
-		cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
+	if (VMGCNT_GET(free_count) > 2048) {
+		VMGCNT_SET(cache_min, VMGCNT_GET(free_target));
+		VMGCNT_SET(cache_max, 2 * VMGCNT_GET(cache_min));
+		VMGCNT_SET(inactive_target, (3 * VMGCNT_GET(free_target) / 2));
 	} else {
-		cnt.v_cache_min = 0;
-		cnt.v_cache_max = 0;
-		cnt.v_inactive_target = cnt.v_free_count / 4;
+		VMGCNT_SET(cache_min, 0);
+		VMGCNT_SET(cache_max, 0);
+		VMGCNT_SET(inactive_target, VMGCNT_GET(free_count) / 4);
 	}
-	if (cnt.v_inactive_target > cnt.v_free_count / 3)
-		cnt.v_inactive_target = cnt.v_free_count / 3;
+	if (VMGCNT_GET(inactive_target) > VMGCNT_GET(free_count) / 3)
+		VMGCNT_SET(inactive_target, VMGCNT_GET(free_count) / 3);
 
 	/* XXX does not really belong here */
 	if (vm_page_max_wired == 0)
-		vm_page_max_wired = cnt.v_free_count / 3;
+		vm_page_max_wired = VMGCNT_GET(free_count) / 3;
 
 	if (vm_pageout_stats_max == 0)
-		vm_pageout_stats_max = cnt.v_free_target;
+		vm_pageout_stats_max = VMGCNT_GET(free_target);
 
 	/*
 	 * Set interval in seconds for stats scan.
@@ -1489,7 +1489,7 @@
 		if (vm_pages_needed && !vm_page_count_min()) {
 			if (!vm_paging_needed())
 				vm_pages_needed = 0;
-			wakeup(&cnt.v_free_count);
+			wakeup(VMGCNT_PTR(free_count));
 		}
 		if (vm_pages_needed) {
 			/*
@@ -1524,7 +1524,7 @@
 			}
 		}
 		if (vm_pages_needed)
-			cnt.v_pdwakeups++;
+			VMGCNT_ADD(pdwakeups, 1);
 		mtx_unlock(&vm_page_queue_free_mtx);
 		vm_pageout_scan(pass);
 	}
--- //depot/vendor/freebsd/src/sys/vm/vm_pageq.c	2007/02/05 06:07:04
+++ //depot/user/attilio/attilio_schedlock/vm/vm_pageq.c	2007/05/10 19:52:44
@@ -140,14 +140,14 @@
 	vm_coloring_init();
 
 	for (i = 0; i < PQ_NUMCOLORS; ++i) {
-		vm_page_queues[PQ_FREE+i].cnt = &cnt.v_free_count;
+		vm_page_queues[PQ_FREE+i].cnt = VMGCNT_PTR(free_count);
 	}
 	for (i = 0; i < PQ_NUMCOLORS; ++i) {
-		vm_page_queues[PQ_CACHE + i].cnt = &cnt.v_cache_count;
+		vm_page_queues[PQ_CACHE + i].cnt = VMGCNT_PTR(cache_count);
 	}
-	vm_page_queues[PQ_INACTIVE].cnt = &cnt.v_inactive_count;
-	vm_page_queues[PQ_ACTIVE].cnt = &cnt.v_active_count;
-	vm_page_queues[PQ_HOLD].cnt = &cnt.v_active_count;
+	vm_page_queues[PQ_INACTIVE].cnt = VMGCNT_PTR(inactive_count);
+	vm_page_queues[PQ_ACTIVE].cnt = VMGCNT_PTR(active_count);
+	vm_page_queues[PQ_HOLD].cnt = VMGCNT_PTR(active_count);
 
 	for (i = 0; i < PQ_COUNT; i++) {
 		TAILQ_INIT(&vm_page_queues[i].pl);
@@ -192,7 +192,7 @@
 {
 	vm_page_t m;
 
-	atomic_add_int(&cnt.v_page_count, 1);
+	VMGCNT_ADD(page_count, 1);
 	m = PHYS_TO_VM_PAGE(pa);
 	m->phys_addr = pa;
 	m->flags = 0;
--- //depot/vendor/freebsd/src/sys/vm/vm_zeroidle.c	2007/02/11 05:22:38
+++ //depot/user/attilio/attilio_schedlock/vm/vm_zeroidle.c	2007/05/10 19:52:44
@@ -90,9 +90,10 @@
 	 * fast sleeps.  We also do not want to be continuously zeroing
 	 * pages because doing so may flush our L1 and L2 caches too much.
 	 */
-	if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count))
+	if (zero_state && vm_page_zero_count >=
+	    ZIDLE_LO(VMGCNT_GET(free_count)))
 		return (0);
-	if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
+	if (vm_page_zero_count >= ZIDLE_HI(VMGCNT_GET(free_count)))
 		return (0);
 	return (1);
 }
@@ -115,7 +116,7 @@
 		vm_pageq_enqueue(PQ_FREE + m->pc, m);
 		++vm_page_zero_count;
 		++cnt_prezero;
-		if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
+		if (vm_page_zero_count >= ZIDLE_HI(VMGCNT_GET(free_count)))
 			zero_state = 1;
 	}
 	free_rover = (free_rover + PQ_PRIME2) & PQ_COLORMASK;
--- //depot/vendor/freebsd/src/sys/vm/vnode_pager.c	2006/10/14 23:26:11
+++ //depot/user/attilio/attilio_schedlock/vm/vnode_pager.c	2007/05/10 19:52:44
@@ -728,8 +728,8 @@
 			if (i != reqpage)
 				vm_page_free(m[i]);
 		vm_page_unlock_queues();
-		cnt.v_vnodein++;
-		cnt.v_vnodepgsin++;
+		VMGCNT_ADD(vnodein, 1);
+		VMGCNT_ADD(vnodepgsin, 1);
 		error = vnode_pager_input_old(object, m[reqpage]);
 		VM_OBJECT_UNLOCK(object);
 		return (error);
@@ -757,8 +757,8 @@
 				vm_page_free(m[i]);
 		vm_page_unlock_queues();
 		VM_OBJECT_UNLOCK(object);
-		cnt.v_vnodein++;
-		cnt.v_vnodepgsin++;
+		VMGCNT_ADD(vnodein, 1);
+		VMGCNT_ADD(vnodepgsin, 1);
 		return vnode_pager_input_smlfs(object, m[reqpage]);
 	}
 
@@ -909,8 +909,8 @@
 	bp->b_runningbufspace = bp->b_bufsize;
 	atomic_add_int(&runningbufspace, bp->b_runningbufspace);
 
-	cnt.v_vnodein++;
-	cnt.v_vnodepgsin += count;
+	VMGCNT_ADD(vnodein, 1);
+	VMGCNT_ADD(vnodepgsin, 1);
 
 	/* do the input */
 	bp->b_iooffset = dbtob(bp->b_blkno);
@@ -1031,7 +1031,8 @@
 	 * daemon up.  This should be probably be addressed XXX.
 	 */
 
-	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
+	if ((VMGCNT_GET(free_count) + VMGCNT_GET(cache_count)) <
+	    VMGCNT_GET(pageout_free_min))
 		sync |= OBJPC_SYNC;
 
 	/*
@@ -1157,8 +1158,8 @@
 	auio.uio_resid = maxsize;
 	auio.uio_td = (struct thread *) 0;
 	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
-	cnt.v_vnodeout++;
-	cnt.v_vnodepgsout += ncount;
+	VMGCNT_ADD(vnodein, 1);
+	VMGCNT_ADD(vnodepgsin, ncount);
 
 	if (error) {
 		if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
