--- //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/fs/smbfs/smbfs_io.c	2006/08/09 17:46:32
+++ //depot/user/attilio/attilio_schedlock/fs/smbfs/smbfs_io.c	2007/01/31 20:05:07
@@ -475,8 +475,8 @@
 
 	kva = (vm_offset_t) bp->b_data;
 	pmap_qenter(kva, pages, npages);
-	cnt.v_vnodein++;
-	cnt.v_vnodepgsin += npages;
+	atomic_add_int(&cnt.v_vnodein, 1);
+	atomic_add_int(&cnt.v_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;
+	atomic_add_int(&cnt.v_vnodeout, 1);
+	atomic_add_int(&cnt.v_vnodepgsout, count);
 
 	iov.iov_base = (caddr_t) kva;
 	iov.iov_len = count;
--- //depot/vendor/freebsd/src/sys/i386/i386/machdep.c	2007/01/23 08:02:48
+++ //depot/user/attilio/attilio_schedlock/i386/i386/machdep.c	2007/02/06 00:25:30
@@ -1278,6 +1278,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 +2102,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/02/08 16:52:59
+++ //depot/user/attilio/attilio_schedlock/i386/i386/mp_machdep.c	2007/02/15 13:38:28
@@ -1342,11 +1342,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/sys_machdep.c	2006/11/06 13:48:07
+++ //depot/user/attilio/attilio_schedlock/i386/i386/sys_machdep.c	2007/01/30 22:00:51
@@ -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>
 
@@ -356,17 +357,17 @@
 
 /*
  * 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. Exits with dt_lock unheld.
  */   
 void
 set_user_ldt(struct mdproc *mdp)
 {
 	struct proc_ldt *pldt;
 
+	if (!mtx_owned(&dt_lock))
+		mtx_lock_spin(&dt_lock);
+	mtx_assert(&dt_lock, MA_OWNED | MA_NOTRECURSED);
+
 	pldt = mdp->md_ldt;
 #ifdef SMP
 	gdt[PCPU_GET(cpuid) * NGDT + GUSERLDT_SEL].sd = pldt->ldt_sd;
@@ -375,6 +376,7 @@
 #endif
 	lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
 	PCPU_SET(currentldt, GSEL(GUSERLDT_SEL, SEL_KPL));
+	mtx_unlock_spin(&dt_lock);
 }
 
 #ifdef SMP
@@ -390,17 +392,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 and not recursed. 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 | MA_NOTRECURSED);
+	mtx_unlock_spin(&dt_lock);
 	MALLOC(new_ldt, struct proc_ldt *, sizeof(struct proc_ldt),
 		M_SUBPROC, M_WAITOK);
 
@@ -411,10 +411,10 @@
 		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);
@@ -424,15 +424,14 @@
 			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 either with dt_lock not held.
  */
 void
 user_ldt_free(struct thread *td)
@@ -440,25 +439,23 @@
 	struct mdproc *mdp = &td->td_proc->p_md;
 	struct proc_ldt *pldt = mdp->md_ldt;
 
+	mtx_assert(&dt_lock, MA_NOTOWNED);
 	if (pldt == NULL)
 		return;
 
-	if (!mtx_owned(&sched_lock))
-		mtx_lock_spin(&sched_lock);
-	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
+	mtx_lock_spin(&dt_lock);
 	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);
+	mtx_unlock_spin(&dt_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);
+	}
 }
 
 /*
@@ -482,6 +479,7 @@
 	    uap->start, uap->num, (void *)uap->descs);
 #endif
 
+	mtx_lock_spin(&dt_lock);
 	if (pldt) {
 		nldt = pldt->ldt_len;
 		num = min(uap->num, nldt);
@@ -491,6 +489,7 @@
 		num = min(uap->num, nldt);
 		lp = &ldt[uap->start];
 	}
+	mtx_unlock_spin(&dt_lock);
 
 	if ((uap->start > (unsigned int)nldt) ||
 	    ((unsigned int)num > (unsigned int)nldt) ||
@@ -536,10 +535,10 @@
 		}
 		if (uap->num <= 0)
 			return (EINVAL);
-		mtx_lock_spin(&sched_lock);
+		mtx_lock_spin(&dt_lock);
 		pldt = mdp->md_ldt;
 		if (pldt == NULL || uap->start >= pldt->ldt_len) {
-			mtx_unlock_spin(&sched_lock);
+			mtx_unlock_spin(&dt_lock);
 			return (0);
 		}
 		largest_ld = uap->start + uap->num;
@@ -548,7 +547,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);
 	}
 
@@ -639,7 +638,7 @@
 			pldt = mdp->md_ldt;
 		}
 again:
-		mtx_lock_spin(&sched_lock);
+		mtx_lock_spin(&dt_lock);
 		/*
 		 * start scanning a bit up to leave room for NVidia and
 		 * Wine, which still user the "Blat" method of allocation.
@@ -651,7 +650,7 @@
 			dp++;
 		}
 		if (i >= pldt->ldt_len) {
-			mtx_unlock_spin(&sched_lock);
+			mtx_unlock_spin(&dt_lock);
 			error = i386_ldt_grow(td, pldt->ldt_len+1);
 			if (error)
 				return (error);
@@ -659,15 +658,15 @@
 		}
 		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);
 			error = i386_set_ldt_data(td, uap->start, uap->num,
 			    descs);
-			mtx_unlock_spin(&sched_lock);
+			mtx_unlock_spin(&dt_lock);
 		}
 	}
 	if (error == 0)
@@ -682,7 +681,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,
@@ -705,6 +704,7 @@
 		len = NLDT + 1;
 
 	/* Allocate a user ldt. */
+	mtx_lock_spin(&dt_lock);
 	pldt = mdp->md_ldt;
 	if (!pldt || len > pldt->ldt_len) {
 		struct proc_ldt *new_ldt;
@@ -714,7 +714,6 @@
 			return (ENOMEM);
 		pldt = mdp->md_ldt;
 
-		/* sched_lock was acquired by user_ldt_alloc. */
 		if (pldt) {
 			if (new_ldt->ldt_len > pldt->ldt_len) {
 				old_ldt_base = pldt->ldt_base;
@@ -722,35 +721,34 @@
 				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);
+				mtx_unlock_spin(&dt_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);
+				mtx_lock_spin(&dt_lock);
 			} else {
 				/*
 				 * If other threads already did the work,
 				 * do nothing.
 				 */
-				mtx_unlock_spin(&sched_lock);
+				mtx_unlock_spin(&dt_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
 	}
+	if (mtx_owned(&dt_lock))
+		mtx_unlock_spin(&dt_lock);
 	return (0);
 }
--- //depot/vendor/freebsd/src/sys/i386/i386/vm_machdep.c	2007/01/28 17:41:40
+++ //depot/user/attilio/attilio_schedlock/i386/i386/vm_machdep.c	2007/01/30 22:00:51
@@ -41,7 +41,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/i386/i386/vm_machdep.c,v 1.275 2007/01/28 17:39:03 rwatson Exp $");
+__FBSDID("$FreeBSD: src/sys/i386/i386/vm_machdep.c,v 1.274 2006/05/29 02:58:37 davidxu Exp $");
 
 #include "opt_isa.h"
 #include "opt_npx.h"
@@ -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>
@@ -159,8 +160,10 @@
 			struct proc_ldt *pldt;
 
 			pldt = mdp1->md_ldt;
-			if (pldt && pldt->ldt_refcnt > 1) {
+			if (pldt && refcount_value(&pldt->ldt_refcnt) > 1) {
+				mtx_lock_spin(&dt_lock);
 				pldt = user_ldt_alloc(mdp1, pldt->ldt_len);
+				mtx_unlock_spin(&dt_lock);
 				if (pldt == NULL)
 					panic("could not copy LDT");
 				mdp1->md_ldt = pldt;
@@ -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;
@@ -680,7 +683,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/kern/kern_clock.c	2006/12/15 21:46:31
+++ //depot/user/attilio/attilio_schedlock/kern/kern_clock.c	2007/02/01 20:00:21
@@ -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.
@@ -422,6 +420,7 @@
 			thread_statclock(1);
 #endif
 		td->td_uticks++;
+		mtx_lock_spin_flags(&time_lock, MTX_QUIET);
 		if (p->p_nice > NZERO)
 			cp_time[CP_NICE]++;
 		else
@@ -442,6 +441,7 @@
 		if ((td->td_pflags & TDP_ITHREAD) ||
 		    td->td_intr_nesting_level >= 2) {
 			td->td_iticks++;
+			mtx_lock_spin_flags(&time_lock, MTX_QUIET);
 			cp_time[CP_INTR]++;
 		} else {
 #ifdef KSE
@@ -450,15 +450,18 @@
 #endif
 			td->td_pticks++;
 			td->td_sticks++;
+			mtx_lock_spin_flags(&time_lock, MTX_QUIET);
 			if (td != PCPU_GET(idlethread))
 				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_synch.c	2007/01/03 02:41:10
+++ //depot/user/attilio/attilio_schedlock/kern/kern_synch.c	2007/01/31 20:05:07
@@ -402,7 +402,7 @@
 	/*
 	 * Finish up stats for outgoing thread.
 	 */
-	cnt.v_swtch++;
+	atomic_add_int(&cnt.v_swtch, 1);
 	PCPU_SET(switchtime, new_switchtime);
 	PCPU_SET(switchticks, ticks);
 	CTR4(KTR_PROC, "mi_switch: old thread %p (kse %p, pid %ld, %s)",
--- //depot/vendor/freebsd/src/sys/kern/kern_thread.c	2006/12/31 16:02:37
+++ //depot/user/attilio/attilio_schedlock/kern/kern_thread.c	2007/01/31 20:05:07
@@ -406,7 +406,7 @@
 	p->p_rux.rux_iticks += td->td_iticks;
 	PCPU_SET(switchtime, new_switchtime);
 	PCPU_SET(switchticks, ticks);
-	cnt.v_swtch++;
+	atomic_add_int(&cnt.v_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	2005/12/16 22:10:20
+++ //depot/user/attilio/attilio_schedlock/kern/subr_prof.c	2007/02/01 19:24:07
@@ -426,12 +426,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);
 
@@ -471,15 +471,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	2006/12/06 06:37:04
+++ //depot/user/attilio/attilio_schedlock/kern/subr_trap.c	2007/01/31 20:05:07
@@ -193,8 +193,8 @@
 #endif
 	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK |
 	    TDF_NEEDRESCHED | TDF_INTERRUPT);
-	cnt.v_trap++;
 	mtx_unlock_spin(&sched_lock);
+	atomic_add_int(&cnt.v_trap, 1);
 
 	/*
 	 * XXXKSE While the fact that we owe a user profiling
--- //depot/vendor/freebsd/src/sys/kern/subr_witness.c	2007/02/07 05:47:22
+++ //depot/user/attilio/attilio_schedlock/kern/subr_witness.c	2007/02/15 13:38:28
@@ -386,6 +386,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 },
@@ -401,6 +402,7 @@
 	{ "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 },
 	/*
 	 * leaf locks
 	 */
--- //depot/vendor/freebsd/src/sys/nfsclient/nfs_bio.c	2006/08/09 17:46:32
+++ //depot/user/attilio/attilio_schedlock/nfsclient/nfs_bio.c	2007/01/31 20:05:07
@@ -159,8 +159,8 @@
 
 	kva = (vm_offset_t) bp->b_data;
 	pmap_qenter(kva, pages, npages);
-	cnt.v_vnodein++;
-	cnt.v_vnodepgsin += npages;
+	atomic_add_int(&cnt.v_vnodein, 1);
+	atomic_add_int(&cnt.v_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;
+	atomic_add_int(&cnt.v_vnodeout, 1);
+	atomic_add_int(&cnt.v_vnodepgsout, count);
 
 	iov.iov_base = (caddr_t) kva;
 	iov.iov_len = count;
--- //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/systm.h	2007/02/14 05:22:06
+++ //depot/user/attilio/attilio_schedlock/sys/systm.h	2007/02/15 13:38:28
@@ -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/vm/swap_pager.c	2007/02/07 17:47:13
+++ //depot/user/attilio/attilio_schedlock/vm/swap_pager.c	2007/02/15 13:38:28
@@ -1037,8 +1037,8 @@
 	}
 	bp->b_npages = j - i;
 
-	cnt.v_swapin++;
-	cnt.v_swappgsin += bp->b_npages;
+	atomic_add_int(&cnt.v_swapin, 1);
+	atomic_add_int(&cnt.v_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++;
+		atomic_add_int(&cnt.v_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;
+		atomic_add_int(&cnt.v_swapout, 1);
+		atomic_add_int(&cnt.v_swappgsout, bp->b_npages);
 
 		/*
 		 * asynchronous
--- //depot/vendor/freebsd/src/sys/vm/vm_object.c	2007/01/17 15:06:57
+++ //depot/user/attilio/attilio_schedlock/vm/vm_object.c	2007/01/31 20:05:07
@@ -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++;
+			atomic_add_int(&cnt.v_pfree, 1);
 		} else {
 			vm_page_remove(p);
 		}
--- //depot/vendor/freebsd/src/sys/vm/vm_page.c	2007/02/15 05:47:06
+++ //depot/user/attilio/attilio_schedlock/vm/vm_page.c	2007/02/15 13:38:28
@@ -152,7 +152,7 @@
 vm_set_page_size(void)
 {
 	if (cnt.v_page_size == 0)
-		cnt.v_page_size = PAGE_SIZE;
+		atomic_store_rel_int(&cnt.v_page_size, PAGE_SIZE);
 	if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
 		panic("vm_set_page_size: page size not a power of two");
 }
@@ -349,8 +349,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;
+	atomic_store_rel_int(&cnt.v_page_count, 0);
+	atomic_store_rel_int(&cnt.v_free_count, 0);
 	list = getenv("vm.blacklist");
 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
 		pa = phys_avail[i];
@@ -1038,7 +1038,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++;
+			atomic_add_int(&cnt.v_reactivated, 1);
 		vm_pageq_remove(m);
 		if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
 			if (m->act_count < ACT_INIT)
@@ -1105,7 +1105,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++;
+	atomic_add_int(&cnt.v_tfree, 1);
 
 	if (m->busy || VM_PAGE_INQUEUE1(m, PQ_FREE)) {
 		printf(
@@ -1309,7 +1309,7 @@
 		return;
 	if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
 		if (VM_PAGE_INQUEUE1(m, PQ_CACHE))
-			cnt.v_reactivated++;
+			atomic_add_int(&cnt.v_reactivated, 1);
 		vm_page_flag_clear(m, PG_WINATCFLS);
 		vm_pageq_remove(m);
 		if (athead)
@@ -1318,7 +1318,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++;
+		atomic_add_int(&cnt.v_inactive_count, 1);
 	}
 }
 
--- //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/02/15 13:38:28
@@ -538,7 +538,7 @@
 				goto unlock_return;
 			}
 			next = TAILQ_NEXT(p, listq);
-			cnt.v_pdpages++;
+			atomic_add_int(&cnt.v_pdpages, 1);
 			if (p->wire_count != 0 ||
 			    p->hold_count != 0 ||
 			    p->busy != 0 ||
@@ -856,7 +856,7 @@
 			 * Invalid pages can be easily freed
 			 */
 			vm_page_free(m);
-			cnt.v_dfree++;
+			atomic_add_int(&cnt.v_dfree, 1);
 			--page_shortage;
 		} else if (m->dirty == 0) {
 			/*
@@ -1409,7 +1409,7 @@
 	/*
 	 * Initialize some paging parameters.
 	 */
-	cnt.v_interrupt_free_min = 2;
+	atomic_store_rel_int(&cnt.v_interrupt_free_min, 2);
 	if (cnt.v_page_count < 2000)
 		vm_pageout_page_count = 8;
 
@@ -1418,17 +1418,15 @@
 	 * 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;
+	atomic_store_rel_int(&cnt.v_free_min, (cnt.v_page_count > 1024) ?
+	    (4 + (cnt.v_page_count - 1024) / 200) : 4);
+	atomic_store_rel_int(&cnt.v_pageout_free_min, (2 * MAXBSIZE) /
+	    PAGE_SIZE + cnt.v_interrupt_free_min);
+	atomic_store_rel_int(&cnt.v_free_reserved, vm_pageout_page_count +
+	    cnt.v_pageout_free_min + (cnt.v_page_count / 768) + PQ_NUMCOLORS);
+	atomic_store_rel_int(&cnt.v_free_severe, cnt.v_free_min / 2);
+	atomic_add_int(&cnt.v_free_min, cnt.v_free_reserved);
+	atomic_add_int(&cnt.v_free_severe, cnt.v_free_reserved);
 
 	/*
 	 * v_free_target and v_cache_min control pageout hysteresis.  Note
@@ -1441,22 +1439,23 @@
 	 * 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;
+	atomic_store_rel_int(&cnt.v_free_target, ((cnt.v_free_count > 6144) ?
+	    4 : 2) * cnt.v_free_min + cnt.v_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;
+		atomic_store_rel_int(&cnt.v_cache_min, cnt.v_free_target);
+		atomic_store_rel_int(&cnt.v_cache_max, 2 * cnt.v_cache_min);
+		atomic_store_rel_int(&cnt.v_inactive_target, (3 *
+		    cnt.v_free_target) / 2);
 	} else {
-		cnt.v_cache_min = 0;
-		cnt.v_cache_max = 0;
-		cnt.v_inactive_target = cnt.v_free_count / 4;
+		atomic_store_rel_int(&cnt.v_cache_min, 0);
+		atomic_store_rel_int(&cnt.v_cache_max, 0);
+		atomic_store_rel_int(&cnt.v_inactive_target,
+		    cnt.v_free_count / 4);
 	}
 	if (cnt.v_inactive_target > cnt.v_free_count / 3)
-		cnt.v_inactive_target = cnt.v_free_count / 3;
+		atomic_store_rel_int(&cnt.v_inactive_target,
+		    cnt.v_free_count / 3);
 
 	/* XXX does not really belong here */
 	if (vm_page_max_wired == 0)
@@ -1524,7 +1523,7 @@
 			}
 		}
 		if (vm_pages_needed)
-			cnt.v_pdwakeups++;
+			atomic_add_int(&cnt.v_pdwakeups, 1);
 		mtx_unlock(&vm_page_queue_free_mtx);
 		vm_pageout_scan(pass);
 	}
--- //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/01/31 20:05:07
@@ -728,8 +728,8 @@
 			if (i != reqpage)
 				vm_page_free(m[i]);
 		vm_page_unlock_queues();
-		cnt.v_vnodein++;
-		cnt.v_vnodepgsin++;
+		atomic_add_int(&cnt.v_vnodein, 1);
+		atomic_add_int(&cnt.v_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++;
+		atomic_add_int(&cnt.v_vnodein, 1);
+		atomic_add_int(&cnt.v_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;
+	atomic_add_int(&cnt.v_vnodein, 1);
+	atomic_add_int(&cnt.v_vnodepgsin, count);
 
 	/* do the input */
 	bp->b_iooffset = dbtob(bp->b_blkno);
@@ -1157,8 +1157,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;
+	atomic_add_int(&cnt.v_vnodeout, 1);
+	atomic_add_int(&cnt.v_vnodepgsout, ncount);
 
 	if (error) {
 		if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
