--- //depot/projects/smpng/sys/amd64/include/atomic.h 2008/12/04 20:58:27 +++ //depot/user/jhb/ktrace/amd64/include/atomic.h 2008/12/04 22:14:32 @@ -80,7 +80,7 @@ u_int atomic_fetchadd_int(volatile u_int *p, u_int v); u_long atomic_fetchadd_long(volatile u_long *p, u_long v); -#define ATOMIC_STORE_LOAD(TYPE, LOP, SOP) \ +#define ATOMIC_STORE_LOAD(TYPE) \ u_##TYPE atomic_load_acq_##TYPE(volatile u_##TYPE *p); \ void atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v) @@ -201,13 +201,12 @@ #if defined(_KERNEL) && !defined(SMP) /* - * We assume that a = b will do atomic loads and stores. However, on a - * PentiumPro or higher, reads may pass writes, so for that case we have - * to use a serializing instruction (i.e. with LOCK) to do the load in - * SMP kernels. For UP kernels, however, the cache of the single processor + * We assume that a = b will do atomic loads and stores. However, reads + * may pass writes, so we have to use fences in SMP kernels to preserve + * ordering. For UP kernels, however, the cache of the single processor * is always consistent, so we don't need any memory barriers. */ -#define ATOMIC_STORE_LOAD(TYPE, LOP, SOP) \ +#define ATOMIC_STORE_LOAD(TYPE) \ static __inline u_##TYPE \ atomic_load_acq_##TYPE(volatile u_##TYPE *p) \ { \ @@ -223,31 +222,22 @@ #else /* !(_KERNEL && !SMP) */ -#define ATOMIC_STORE_LOAD(TYPE, LOP, SOP) \ +#define ATOMIC_STORE_LOAD(TYPE) \ static __inline u_##TYPE \ atomic_load_acq_##TYPE(volatile u_##TYPE *p) \ { \ - u_##TYPE res; \ + u_##TYPE v; \ \ - __asm __volatile(MPLOCKED LOP \ - : "=a" (res), /* 0 */ \ - "=m" (*p) /* 1 */ \ - : "m" (*p) /* 2 */ \ - : "memory"); \ - \ - return (res); \ + v = *p; \ + __asm __volatile("lfence" ::: "memory"); \ + return (v); \ } \ \ -/* \ - * The XCHG instruction asserts LOCK automagically. \ - */ \ static __inline void \ atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\ { \ - __asm __volatile(SOP \ - : "=m" (*p), /* 0 */ \ - "+r" (v) /* 1 */ \ - : "m" (*p)); /* 2 */ \ + __asm __volatile("sfence"); \ + *p = v; \ } \ struct __hack @@ -275,10 +265,10 @@ ATOMIC_ASM(add, long, "addq %1,%0", "ir", v); ATOMIC_ASM(subtract, long, "subq %1,%0", "ir", v); -ATOMIC_STORE_LOAD(char, "cmpxchgb %b0,%1", "xchgb %b1,%0"); -ATOMIC_STORE_LOAD(short,"cmpxchgw %w0,%1", "xchgw %w1,%0"); -ATOMIC_STORE_LOAD(int, "cmpxchgl %0,%1", "xchgl %1,%0"); -ATOMIC_STORE_LOAD(long, "cmpxchgq %0,%1", "xchgq %1,%0"); +ATOMIC_STORE_LOAD(char); +ATOMIC_STORE_LOAD(short); +ATOMIC_STORE_LOAD(int); +ATOMIC_STORE_LOAD(long); #undef ATOMIC_ASM #undef ATOMIC_STORE_LOAD --- //depot/projects/smpng/sys/amd64/include/bus.h 2006/03/14 21:22:36 +++ //depot/user/jhb/ktrace/amd64/include/bus.h 2006/03/14 22:11:25 @@ -278,8 +278,8 @@ 1: movb (%2),%%al \n\ stosb \n\ loop 1b" : - "=D" (addr), "=c" (count) : - "r" (bsh + offset), "0" (addr), "1" (count) : + "+D" (addr), "+c" (count) : + "r" (bsh + offset) : "%eax", "memory"); #endif } @@ -299,8 +299,8 @@ 1: movw (%2),%%ax \n\ stosw \n\ loop 1b" : - "=D" (addr), "=c" (count) : - "r" (bsh + offset), "0" (addr), "1" (count) : + "+D" (addr), "+c" (count) : + "r" (bsh + offset) : "%eax", "memory"); #endif } @@ -320,8 +320,8 @@ 1: movl (%2),%%eax \n\ stosl \n\ loop 1b" : - "=D" (addr), "=c" (count) : - "r" (bsh + offset), "0" (addr), "1" (count) : + "+D" (addr), "+c" (count) : + "r" (bsh + offset) : "%eax", "memory"); #endif } @@ -366,8 +366,7 @@ stosb \n\ incl %2 \n\ loop 1b" : - "=D" (addr), "=c" (count), "=d" (_port_) : - "0" (addr), "1" (count), "2" (_port_) : + "+D" (addr), "+c" (count), "+d" (_port_) :: "%eax", "memory", "cc"); #endif } else { @@ -377,8 +376,7 @@ cld \n\ repne \n\ movsb" : - "=D" (addr), "=c" (count), "=S" (_port_) : - "0" (addr), "1" (count), "2" (_port_) : + "+D" (addr), "+c" (count), "+S" (_port_) :: "memory", "cc"); #endif } @@ -398,8 +396,7 @@ stosw \n\ addl $2,%2 \n\ loop 1b" : - "=D" (addr), "=c" (count), "=d" (_port_) : - "0" (addr), "1" (count), "2" (_port_) : + "+D" (addr), "+c" (count), "+d" (_port_) :: "%eax", "memory", "cc"); #endif } else { @@ -409,8 +406,7 @@ cld \n\ repne \n\ movsw" : - "=D" (addr), "=c" (count), "=S" (_port_) : - "0" (addr), "1" (count), "2" (_port_) : + "+D" (addr), "+c" (count), "+S" (_port_) :: "memory", "cc"); #endif } @@ -430,8 +426,7 @@ stosl \n\ addl $4,%2 \n\ loop 1b" : - "=D" (addr), "=c" (count), "=d" (_port_) : - "0" (addr), "1" (count), "2" (_port_) : + "+D" (addr), "+c" (count), "+d" (_port_) :: "%eax", "memory", "cc"); #endif } else { @@ -441,8 +436,7 @@ cld \n\ repne \n\ movsl" : - "=D" (addr), "=c" (count), "=S" (_port_) : - "0" (addr), "1" (count), "2" (_port_) : + "+D" (addr), "+c" (count), "+S" (_port_) :: "memory", "cc"); #endif } @@ -542,8 +536,8 @@ 1: lodsb \n\ movb %%al,(%2) \n\ loop 1b" : - "=S" (addr), "=c" (count) : - "r" (bsh + offset), "0" (addr), "1" (count) : + "+S" (addr), "+c" (count) : + "r" (bsh + offset) : "%eax", "memory", "cc"); #endif } @@ -563,8 +557,8 @@ 1: lodsw \n\ movw %%ax,(%2) \n\ loop 1b" : - "=S" (addr), "=c" (count) : - "r" (bsh + offset), "0" (addr), "1" (count) : + "+S" (addr), "+c" (count) : + "r" (bsh + offset) : "%eax", "memory", "cc"); #endif } @@ -584,8 +578,8 @@ 1: lodsl \n\ movl %%eax,(%2) \n\ loop 1b" : - "=S" (addr), "=c" (count) : - "r" (bsh + offset), "0" (addr), "1" (count) : + "+S" (addr), "+c" (count) : + "r" (bsh + offset) : "%eax", "memory", "cc"); #endif } @@ -631,8 +625,7 @@ outb %%al,%w0 \n\ incl %0 \n\ loop 1b" : - "=d" (_port_), "=S" (addr), "=c" (count) : - "0" (_port_), "1" (addr), "2" (count) : + "+d" (_port_), "+S" (addr), "+c" (count) :: "%eax", "memory", "cc"); #endif } else { @@ -642,8 +635,7 @@ cld \n\ repne \n\ movsb" : - "=D" (_port_), "=S" (addr), "=c" (count) : - "0" (_port_), "1" (addr), "2" (count) : + "+D" (_port_), "+S" (addr), "+c" (count) :: "memory", "cc"); #endif } @@ -663,8 +655,7 @@ outw %%ax,%w0 \n\ addl $2,%0 \n\ loop 1b" : - "=d" (_port_), "=S" (addr), "=c" (count) : - "0" (_port_), "1" (addr), "2" (count) : + "+d" (_port_), "+S" (addr), "+c" (count) :: "%eax", "memory", "cc"); #endif } else { @@ -674,8 +665,7 @@ cld \n\ repne \n\ movsw" : - "=D" (_port_), "=S" (addr), "=c" (count) : - "0" (_port_), "1" (addr), "2" (count) : + "+D" (_port_), "+S" (addr), "+c" (count) :: "memory", "cc"); #endif } @@ -695,8 +685,7 @@ outl %%eax,%w0 \n\ addl $4,%0 \n\ loop 1b" : - "=d" (_port_), "=S" (addr), "=c" (count) : - "0" (_port_), "1" (addr), "2" (count) : + "+d" (_port_), "+S" (addr), "+c" (count) :: "%eax", "memory", "cc"); #endif } else { @@ -706,8 +695,7 @@ cld \n\ repne \n\ movsl" : - "=D" (_port_), "=S" (addr), "=c" (count) : - "0" (_port_), "1" (addr), "2" (count) : + "+D" (_port_), "+S" (addr), "+c" (count) :: "memory", "cc"); #endif } @@ -997,10 +985,13 @@ bus_size_t offset __unused, bus_size_t len __unused, int flags) { #ifdef __GNUCLIKE_ASM - if (flags & BUS_SPACE_BARRIER_READ) - __asm __volatile("lock; addl $0,0(%%rsp)" : : : "memory"); + if ((flags & (BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE)) == + (BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE)) + __asm __volatile("mfence" : : : "memory"); + else if (flags & BUS_SPACE_BARRIER_READ) + __asm __volatile("lfence" : : : "memory"); else - __asm __volatile("" : : : "memory"); + __asm __volatile("sfence"); #endif } --- //depot/projects/smpng/sys/i386/include/bus.h 2005/12/07 22:12:14 +++ //depot/user/jhb/ktrace/i386/include/bus.h 2005/12/08 18:03:18 @@ -282,8 +282,8 @@ 1: movb (%2),%%al \n\ stosb \n\ loop 1b" : - "=D" (addr), "=c" (count) : - "r" (bsh + offset), "0" (addr), "1" (count) : + "+D" (addr), "+c" (count) : + "r" (bsh + offset) : "%eax", "memory"); #else # ifndef lint @@ -307,8 +307,8 @@ 1: movw (%2),%%ax \n\ stosw \n\ loop 1b" : - "=D" (addr), "=c" (count) : - "r" (bsh + offset), "0" (addr), "1" (count) : + "+D" (addr), "+c" (count) : + "r" (bsh + offset) : "%eax", "memory"); #else # ifndef lint @@ -332,8 +332,8 @@ 1: movl (%2),%%eax \n\ stosl \n\ loop 1b" : - "=D" (addr), "=c" (count) : - "r" (bsh + offset), "0" (addr), "1" (count) : + "+D" (addr), "+c" (count) : + "r" (bsh + offset) : "%eax", "memory"); #else # ifndef lint @@ -382,8 +382,7 @@ stosb \n\ incl %2 \n\ loop 1b" : - "=D" (addr), "=c" (count), "=d" (_port_) : - "0" (addr), "1" (count), "2" (_port_) : + "+D" (addr), "+c" (count), "+d" (_port_) : : "%eax", "memory", "cc"); #else # ifndef lint @@ -397,8 +396,7 @@ cld \n\ repne \n\ movsb" : - "=D" (addr), "=c" (count), "=S" (_port_) : - "0" (addr), "1" (count), "2" (_port_) : + "+D" (addr), "+c" (count), "+S" (_port_) : : "memory", "cc"); #else # ifndef lint @@ -422,8 +420,7 @@ stosw \n\ addl $2,%2 \n\ loop 1b" : - "=D" (addr), "=c" (count), "=d" (_port_) : - "0" (addr), "1" (count), "2" (_port_) : + "+D" (addr), "+c" (count), "+d" (_port_) : : "%eax", "memory", "cc"); #else # ifndef lint @@ -437,8 +434,7 @@ cld \n\ repne \n\ movsw" : - "=D" (addr), "=c" (count), "=S" (_port_) : - "0" (addr), "1" (count), "2" (_port_) : + "+D" (addr), "+c" (count), "+S" (_port_) : : "memory", "cc"); #else # ifndef lint @@ -462,8 +458,7 @@ stosl \n\ addl $4,%2 \n\ loop 1b" : - "=D" (addr), "=c" (count), "=d" (_port_) : - "0" (addr), "1" (count), "2" (_port_) : + "+D" (addr), "+c" (count), "+d" (_port_) : : "%eax", "memory", "cc"); #else # ifndef lint @@ -477,8 +472,7 @@ cld \n\ repne \n\ movsl" : - "=D" (addr), "=c" (count), "=S" (_port_) : - "0" (addr), "1" (count), "2" (_port_) : + "+D" (addr), "+c" (count), "+S" (_port_) : : "memory", "cc"); #else # ifndef lint @@ -582,8 +576,8 @@ 1: lodsb \n\ movb %%al,(%2) \n\ loop 1b" : - "=S" (addr), "=c" (count) : - "r" (bsh + offset), "0" (addr), "1" (count) : + "+S" (addr), "+c" (count) : + "r" (bsh + offset) : "%eax", "memory", "cc"); #else # ifndef lint @@ -607,8 +601,8 @@ 1: lodsw \n\ movw %%ax,(%2) \n\ loop 1b" : - "=S" (addr), "=c" (count) : - "r" (bsh + offset), "0" (addr), "1" (count) : + "+S" (addr), "+c" (count) : + "r" (bsh + offset) : "%eax", "memory", "cc"); #else # ifndef lint @@ -632,8 +626,8 @@ 1: lodsl \n\ movl %%eax,(%2) \n\ loop 1b" : - "=S" (addr), "=c" (count) : - "r" (bsh + offset), "0" (addr), "1" (count) : + "+S" (addr), "+c" (count) : + "r" (bsh + offset) : "%eax", "memory", "cc"); #else # ifndef lint @@ -683,8 +677,7 @@ outb %%al,%w0 \n\ incl %0 \n\ loop 1b" : - "=d" (_port_), "=S" (addr), "=c" (count) : - "0" (_port_), "1" (addr), "2" (count) : + "+d" (_port_), "+S" (addr), "+c" (count) : : "%eax", "memory", "cc"); #else # ifndef lint @@ -698,8 +691,7 @@ cld \n\ repne \n\ movsb" : - "=D" (_port_), "=S" (addr), "=c" (count) : - "0" (_port_), "1" (addr), "2" (count) : + "+D" (_port_), "+S" (addr), "+c" (count) : : "memory", "cc"); #else # ifndef lint @@ -723,8 +715,7 @@ outw %%ax,%w0 \n\ addl $2,%0 \n\ loop 1b" : - "=d" (_port_), "=S" (addr), "=c" (count) : - "0" (_port_), "1" (addr), "2" (count) : + "+d" (_port_), "+S" (addr), "+c" (count) : : "%eax", "memory", "cc"); #else # ifndef lint @@ -738,8 +729,7 @@ cld \n\ repne \n\ movsw" : - "=D" (_port_), "=S" (addr), "=c" (count) : - "0" (_port_), "1" (addr), "2" (count) : + "+D" (_port_), "+S" (addr), "+c" (count) : : "memory", "cc"); #else # ifndef lint @@ -763,8 +753,7 @@ outl %%eax,%w0 \n\ addl $4,%0 \n\ loop 1b" : - "=d" (_port_), "=S" (addr), "=c" (count) : - "0" (_port_), "1" (addr), "2" (count) : + "+d" (_port_), "+S" (addr), "+c" (count) : : "%eax", "memory", "cc"); #else # ifndef lint @@ -778,8 +767,7 @@ cld \n\ repne \n\ movsl" : - "=D" (_port_), "=S" (addr), "=c" (count) : - "0" (_port_), "1" (addr), "2" (count) : + "+D" (_port_), "+S" (addr), "+c" (count) : : "memory", "cc"); #else # ifndef lint @@ -1076,7 +1064,7 @@ if (flags & BUS_SPACE_BARRIER_READ) __asm __volatile("lock; addl $0,0(%%esp)" : : : "memory"); else - __asm __volatile("" : : : "memory"); + /* nothing */; #else # ifndef lint # error "no assembler code for your compiler" --- //depot/projects/smpng/sys/kern/subr_kdb.c 2009/05/13 13:56:17 +++ //depot/user/jhb/ktrace/kern/subr_kdb.c 2009/05/13 17:57:30 @@ -39,6 +39,7 @@ #include #include +#include #include #include @@ -511,11 +512,23 @@ return (0); /* We reenter the debugger through kdb_reenter(). */ - if (kdb_active) + if (kdb_active == PCPU_GET(cpuid) + 1) return (0); intr = intr_disable(); + critical_enter(); + /* + * If more than one CPU tries to enter KDB at the same time + * then force them to serialize and go one at a time. + */ + while (!atomic_cmpset_int(&kdb_active, 0, PCPU_GET(cpuid) + 1)) { + critical_exit(); + while (kdb_active) + cpu_spinwait(); + critical_enter(); + } + #ifdef SMP if ((did_stop_cpus = kdb_stop_cpus) != 0) stop_cpus(PCPU_GET(other_cpus)); @@ -533,7 +546,11 @@ handled = kdb_dbbe->dbbe_trap(type, code); - kdb_active--; + /* + * We have to exit KDB before resuming the other CPUs so that they + * may run in a debugger-less context. + */ + kdb_active = 0; #ifdef SMP if (did_stop_cpus) --- //depot/projects/smpng/sys/notes 2009/02/18 22:05:55 +++ //depot/user/jhb/ktrace/notes 2009/02/20 16:15:34 @@ -73,3 +73,4 @@ - jhb_socket - socket hacking Space reserved for child branches: +- Abuse this branch for asm changes for the time being --- //depot/projects/smpng/sys/pc98/include/bus.h 2008/09/17 20:27:47 +++ //depot/user/jhb/ktrace/pc98/include/bus.h 2008/09/18 17:18:53 @@ -328,11 +328,10 @@ \ __asm __volatile("call *%2" \ :"=a" (result), \ - "=d" (offset) \ + "+d" (offset) \ :"o" (bsh->bsh_bam.bs_read_##BWN), \ - "b" (bsh), \ - "1" (offset) \ - ); \ + "b" (bsh) \ + :"ecx","memory"); \ \ return result; \ } @@ -354,12 +353,11 @@ { \ \ __asm __volatile("call *%1" \ - :"=d" (offset) \ + :"+d" (offset) \ :"o" (bsh->bsh_bam.bs_write_##BWN), \ "a" (val), \ - "b" (bsh), \ - "0" (offset) \ - ); \ + "b" (bsh) \ + :"ecx","memory"); \ } _BUS_SPACE_WRITE(u_int8_t,1) @@ -380,15 +378,12 @@ { \ \ __asm __volatile("call *%3" \ - :"=c" (cnt), \ - "=d" (offset), \ - "=D" (buf) \ + :"+c" (cnt), \ + "+d" (offset), \ + "+D" (buf) \ :"o" (bsh->bsh_bam.bs_read_multi_##BWN), \ - "b" (bsh), \ - "0" (cnt), \ - "1" (offset), \ - "2" (buf) \ - :"memory"); \ + "b" (bsh) \ + :"eax","memory"); \ } _BUS_SPACE_READ_MULTI(u_int8_t,1) @@ -409,15 +404,12 @@ { \ \ __asm __volatile("call *%3" \ - :"=c" (cnt), \ - "=d" (offset), \ - "=S" (buf) \ + :"+c" (cnt), \ + "+d" (offset), \ + "+S" (buf) \ :"o" (bsh->bsh_bam.bs_write_multi_##BWN), \ - "b" (bsh), \ - "0" (cnt), \ - "1" (offset), \ - "2" (buf) \ - ); \ + "b" (bsh) \ + :"eax","memory"); \ } _BUS_SPACE_WRITE_MULTI(u_int8_t,1) @@ -433,20 +425,17 @@ bus_space_tag_t tag; \ bus_space_handle_t bsh; \ bus_size_t offset; \ - TYPE *buf; \ + TYPE *buf; \ size_t cnt; \ { \ \ __asm __volatile("call *%3" \ - :"=c" (cnt), \ - "=d" (offset), \ - "=D" (buf) \ + :"+c" (cnt), \ + "+d" (offset), \ + "+D" (buf) \ :"o" (bsh->bsh_bam.bs_read_region_##BWN), \ - "b" (bsh), \ - "0" (cnt), \ - "1" (offset), \ - "2" (buf) \ - :"memory"); \ + "b" (bsh) \ + :"eax","memory"); \ } _BUS_SPACE_READ_REGION(u_int8_t,1) @@ -467,15 +456,12 @@ { \ \ __asm __volatile("call *%3" \ - :"=c" (cnt), \ - "=d" (offset), \ - "=S" (buf) \ + :"+c" (cnt), \ + "+d" (offset), \ + "+S" (buf) \ :"o" (bsh->bsh_bam.bs_write_region_##BWN), \ - "b" (bsh), \ - "0" (cnt), \ - "1" (offset), \ - "2" (buf) \ - ); \ + "b" (bsh) \ + :"eax","memory"); \ } _BUS_SPACE_WRITE_REGION(u_int8_t,1) @@ -496,14 +482,12 @@ { \ \ __asm __volatile("call *%2" \ - :"=c" (cnt), \ - "=d" (offset) \ + :"+c" (cnt), \ + "+d" (offset) \ :"o" (bsh->bsh_bam.bs_set_multi_##BWN), \ "a" (val), \ - "b" (bsh), \ - "0" (cnt), \ - "1" (offset) \ - ); \ + "b" (bsh) \ + :"memory"); \ } _BUS_SPACE_SET_MULTI(u_int8_t,1) @@ -524,14 +508,12 @@ { \ \ __asm __volatile("call *%2" \ - :"=c" (cnt), \ - "=d" (offset) \ + :"+c" (cnt), \ + "+d" (offset) \ :"o" (bsh->bsh_bam.bs_set_region_##BWN), \ "a" (val), \ - "b" (bsh), \ - "0" (cnt), \ - "1" (offset) \ - ); \ + "b" (bsh) \ + :"memory"); \ } _BUS_SPACE_SET_REGION(u_int8_t,1) @@ -556,16 +538,13 @@ panic("bus_space_copy_region: funcs mismatch (ENOSUPPORT)");\ \ __asm __volatile("call *%3" \ - :"=c" (cnt), \ - "=S" (src), \ - "=D" (dst) \ + :"+c" (cnt), \ + "+S" (src), \ + "+D" (dst) \ :"o" (dbsh->bsh_bam.bs_copy_region_##BWN), \ "a" (sbsh), \ - "b" (dbsh), \ - "0" (cnt), \ - "1" (src), \ - "2" (dst) \ - ); \ + "b" (dbsh) \ + :"edx","memory"); \ } _BUS_SPACE_COPY_REGION(1) --- //depot/projects/smpng/sys/sys/ktr.h 2009/05/08 11:53:25 +++ //depot/user/jhb/ktrace/sys/ktr.h 2009/05/08 14:17:47 @@ -36,6 +36,15 @@ #ifndef _SYS_KTR_H_ #define _SYS_KTR_H_ +struct ktr_class { + const char *kc_desc; + const char *kc_name; + int kc_enabled; +}; + +#define KTR_DECLARE(class) \ + extern struct ktr_class ktr_ ## class + /* * Trace classes *