1 /* MECHANICALLY GENERATED, DO NOT EDIT!!! */
7 * common.h: Common Linux kernel-isms.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; but version 2 of the License only due
12 * to code included from the Linux kernel.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Copyright (c) 2006 Paul E. McKenney, IBM.
25 * Much code taken from the Linux kernel. For such code, the option
26 * to redistribute under later versions of GPL might not be available.
29 #include <urcu/arch.h>
31 #ifndef __always_inline
32 #define __always_inline inline
35 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
36 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
39 # define stringify_in_c(...) __VA_ARGS__
40 # define ASM_CONST(x) x
42 /* This version of stringify will deal with commas... */
43 # define __stringify_in_c(...) #__VA_ARGS__
44 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
45 # define __ASM_CONST(x) x##UL
46 # define ASM_CONST(x) __ASM_CONST(x)
51 * arch-ppc64.h: Expose PowerPC atomic instructions.
53 * This program is free software; you can redistribute it and/or modify
54 * it under the terms of the GNU General Public License as published by
55 * the Free Software Foundation; but version 2 of the License only due
56 * to code included from the Linux kernel.
58 * This program is distributed in the hope that it will be useful,
59 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61 * GNU General Public License for more details.
63 * You should have received a copy of the GNU General Public License
64 * along with this program; if not, write to the Free Software
65 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
67 * Copyright (c) 2006 Paul E. McKenney, IBM.
69 * Much code taken from the Linux kernel. For such code, the option
70 * to redistribute under later versions of GPL might not be available.
79 /*#define CACHE_LINE_SIZE 128 */
80 #define ____cacheline_internodealigned_in_smp \
81 __attribute__((__aligned__(1 << 7)))
83 #if 0 /* duplicate with arch_atomic.h */
86 * Atomic data structure, initialization, and access.
89 typedef struct { volatile int counter
; } atomic_t
;
91 #define ATOMIC_INIT(i) { (i) }
93 #define atomic_read(v) ((v)->counter)
94 #define atomic_set(v, i) (((v)->counter) = (i))
100 #define LWSYNC lwsync
101 #define PPC405_ERR77(ra,rb)
103 # define LWSYNC_ON_SMP stringify_in_c(LWSYNC) "\n"
104 # define ISYNC_ON_SMP "\n\tisync\n"
106 # define LWSYNC_ON_SMP
107 # define ISYNC_ON_SMP
113 * Changes the memory location '*ptr' to be val and returns
114 * the previous value stored there.
116 static __always_inline
unsigned long
117 __xchg_u32(volatile void *p
, unsigned long val
)
121 __asm__
__volatile__(
123 "1: lwarx %0,0,%2 \n"
128 : "=&r" (prev
), "+m" (*(volatile unsigned int *)p
)
138 * Changes the memory location '*ptr' to be val and returns
139 * the previous value stored there.
141 static __always_inline
unsigned long
142 __xchg_u32_local(volatile void *p
, unsigned long val
)
146 __asm__
__volatile__(
147 "1: lwarx %0,0,%2 \n"
151 : "=&r" (prev
), "+m" (*(volatile unsigned int *)p
)
159 static __always_inline
unsigned long
160 __xchg_u64(volatile void *p
, unsigned long val
)
164 __asm__
__volatile__(
166 "1: ldarx %0,0,%2 \n"
171 : "=&r" (prev
), "+m" (*(volatile unsigned long *)p
)
178 static __always_inline
unsigned long
179 __xchg_u64_local(volatile void *p
, unsigned long val
)
183 __asm__
__volatile__(
184 "1: ldarx %0,0,%2 \n"
188 : "=&r" (prev
), "+m" (*(volatile unsigned long *)p
)
197 * This function doesn't exist, so you'll get a linker error
198 * if something tries to do an invalid xchg().
200 extern void __xchg_called_with_bad_pointer(void);
202 static __always_inline
unsigned long
203 __xchg(volatile void *ptr
, unsigned long x
, unsigned int size
)
207 return __xchg_u32(ptr
, x
);
210 return __xchg_u64(ptr
, x
);
213 __xchg_called_with_bad_pointer();
217 static __always_inline
unsigned long
218 __xchg_local(volatile void *ptr
, unsigned long x
, unsigned int size
)
222 return __xchg_u32_local(ptr
, x
);
225 return __xchg_u64_local(ptr
, x
);
228 __xchg_called_with_bad_pointer();
231 #define xchg(ptr,x) \
233 __typeof__(*(ptr)) _x_ = (x); \
234 (__typeof__(*(ptr))) __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
237 #define xchg_local(ptr,x) \
239 __typeof__(*(ptr)) _x_ = (x); \
240 (__typeof__(*(ptr))) __xchg_local((ptr), \
241 (unsigned long)_x_, sizeof(*(ptr))); \
245 * Compare and exchange - if *p == old, set it to new,
246 * and return the old value of *p.
248 #define __HAVE_ARCH_CMPXCHG 1
250 static __always_inline
unsigned long
251 __cmpxchg_u32(volatile unsigned int *p
, unsigned long old
, unsigned long new)
255 __asm__
__volatile__ (
257 "1: lwarx %0,0,%2 # __cmpxchg_u32\n\
266 : "=&r" (prev
), "+m" (*p
)
267 : "r" (p
), "r" (old
), "r" (new)
273 static __always_inline
unsigned long
274 __cmpxchg_u32_local(volatile unsigned int *p
, unsigned long old
,
279 __asm__
__volatile__ (
280 "1: lwarx %0,0,%2 # __cmpxchg_u32\n\
288 : "=&r" (prev
), "+m" (*p
)
289 : "r" (p
), "r" (old
), "r" (new)
296 static __always_inline
unsigned long
297 __cmpxchg_u64(volatile unsigned long *p
, unsigned long old
, unsigned long new)
301 __asm__
__volatile__ (
303 "1: ldarx %0,0,%2 # __cmpxchg_u64\n\
311 : "=&r" (prev
), "+m" (*p
)
312 : "r" (p
), "r" (old
), "r" (new)
318 static __always_inline
unsigned long
319 __cmpxchg_u64_local(volatile unsigned long *p
, unsigned long old
,
324 __asm__
__volatile__ (
325 "1: ldarx %0,0,%2 # __cmpxchg_u64\n\
332 : "=&r" (prev
), "+m" (*p
)
333 : "r" (p
), "r" (old
), "r" (new)
340 /* This function doesn't exist, so you'll get a linker error
341 if something tries to do an invalid cmpxchg(). */
342 extern void __cmpxchg_called_with_bad_pointer(void);
344 static __always_inline
unsigned long
345 __cmpxchg(volatile void *ptr
, unsigned long old
, unsigned long new,
350 return __cmpxchg_u32(ptr
, old
, new);
353 return __cmpxchg_u64(ptr
, old
, new);
356 __cmpxchg_called_with_bad_pointer();
360 static __always_inline
unsigned long
361 __cmpxchg_local(volatile void *ptr
, unsigned long old
, unsigned long new,
366 return __cmpxchg_u32_local(ptr
, old
, new);
369 return __cmpxchg_u64_local(ptr
, old
, new);
372 __cmpxchg_called_with_bad_pointer();
376 #define cmpxchg(ptr, o, n) \
378 __typeof__(*(ptr)) _o_ = (o); \
379 __typeof__(*(ptr)) _n_ = (n); \
380 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
381 (unsigned long)_n_, sizeof(*(ptr))); \
385 #define cmpxchg_local(ptr, o, n) \
387 __typeof__(*(ptr)) _o_ = (o); \
388 __typeof__(*(ptr)) _n_ = (n); \
389 (__typeof__(*(ptr))) __cmpxchg_local((ptr), (unsigned long)_o_, \
390 (unsigned long)_n_, sizeof(*(ptr))); \
395 * We handle most unaligned accesses in hardware. On the other hand
396 * unaligned DMA can be very expensive on some ppc64 IO chips (it does
397 * powers of 2 writes until it reaches sufficient alignment).
399 * Based on this we disable the IP header alignment in network drivers.
400 * We also modify NET_SKB_PAD to be a cacheline in size, thus maintaining
401 * cacheline alignment of buffers.
403 #define NET_IP_ALIGN 0
404 #define NET_SKB_PAD L1_CACHE_BYTES
406 #define cmpxchg64(ptr, o, n) \
408 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
409 cmpxchg((ptr), (o), (n)); \
411 #define cmpxchg64_local(ptr, o, n) \
413 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
414 cmpxchg_local((ptr), (o), (n)); \
418 #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
419 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
422 * atomic_add - add integer to atomic variable
423 * @i: integer value to add
424 * @v: pointer of type atomic_t
426 * Atomically adds @a to @v.
428 static __inline__
void atomic_add(int a
, atomic_t
*v
)
432 __asm__
__volatile__(
433 "1: lwarx %0,0,%3 # atomic_add\n\
437 : "=&r" (t
), "+m" (v
->counter
)
438 : "r" (a
), "r" (&v
->counter
)
443 * atomic_sub - subtract the atomic variable
444 * @i: integer value to subtract
445 * @v: pointer of type atomic_t
447 * Atomically subtracts @a from @v.
449 static __inline__
void atomic_sub(int a
, atomic_t
*v
)
453 __asm__
__volatile__(
454 "1: lwarx %0,0,%3 # atomic_sub \n\
458 : "=&r" (t
), "+m" (v
->counter
)
459 : "r" (a
), "r" (&v
->counter
)
463 static __inline__
atomic_sub_return(int a
, atomic_t
*v
)
467 __asm__
__volatile__(
469 1: lwarx %0,0,%2 # atomic_sub_return\n\
475 : "r" (a
), "r" (&v
->counter
)
482 * atomic_sub_and_test - subtract value from variable and test result
483 * @i: integer value to subtract
484 * @v: pointer of type atomic_t
486 * Atomically subtracts @i from @v and returns
487 * true if the result is zero, or false for all
490 static __inline__
int atomic_sub_and_test(int a
, atomic_t
*v
)
492 return atomic_sub_return(a
, v
) == 0;
496 * atomic_inc - increment atomic variable
497 * @v: pointer of type atomic_t
499 * Atomically increments @v by 1.
501 static __inline__
void atomic_inc(atomic_t
*v
)
507 * atomic_dec - decrement atomic variable
508 * @v: pointer of type atomic_t
510 * Atomically decrements @v by 1.
512 static __inline__
void atomic_dec(atomic_t
*v
)
518 * atomic_dec_and_test - decrement and test
519 * @v: pointer of type atomic_t
521 * Atomically decrements @v by 1 and
522 * returns true if the result is 0, or false for all other
525 static __inline__
int atomic_dec_and_test(atomic_t
*v
)
527 return atomic_sub_and_test(1, v
);
531 * atomic_inc_and_test - increment and test
532 * @v: pointer of type atomic_t
534 * Atomically increments @v by 1
535 * and returns true if the result is zero, or false for all
538 static __inline__
int atomic_inc_and_test(atomic_t
*v
)
540 return atomic_inc_return(v
);
544 * atomic_add_return - add and return
545 * @v: pointer of type atomic_t
546 * @i: integer value to add
548 * Atomically adds @i to @v and returns @i + @v
550 static __inline__
int atomic_add_return(int a
, atomic_t
*v
)
554 __asm__
__volatile__(
556 1: lwarx %0,0,%2 # atomic_add_return \n\
562 : "r" (a
), "r" (&v
->counter
)
569 * atomic_add_negative - add and test if negative
570 * @v: pointer of type atomic_t
571 * @i: integer value to add
573 * Atomically adds @i to @v and returns true
574 * if the result is negative, or false when
575 * result is greater than or equal to zero.
577 static __inline__
int atomic_add_negative(int a
, atomic_t
*v
)
579 return atomic_add_return(a
, v
) < 0;
583 * atomic_add_unless - add unless the number is a given value
584 * @v: pointer of type atomic_t
585 * @a: the amount to add to v...
586 * @u: ...unless v is equal to u.
588 * Atomically adds @a to @v, so long as it was not @u.
589 * Returns non-zero if @v was not @u, and zero otherwise.
591 static __inline__
int atomic_add_unless(atomic_t
*v
, int a
, int u
)
595 __asm__
__volatile__(
597 1: lwarx %0,0,%1 # atomic_add_unless\n\
607 : "r" (&v
->counter
), "r" (a
), "r" (u
)
613 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
615 #define atomic_inc_return(v) (atomic_add_return(1,v))
616 #define atomic_dec_return(v) (atomic_sub_return(1,v))
618 /* Atomic operations are already serializing on x86 */
619 #define smp_mb__before_atomic_dec() smp_mb()
620 #define smp_mb__after_atomic_dec() smp_mb()
621 #define smp_mb__before_atomic_inc() smp_mb()
622 #define smp_mb__after_atomic_inc() smp_mb()
624 #endif //0 /* duplicate with arch_atomic.h */
627 * api_pthreads.h: API mapping to pthreads environment.
629 * This program is free software; you can redistribute it and/or modify
630 * it under the terms of the GNU General Public License as published by
631 * the Free Software Foundation; either version 2 of the License, or
632 * (at your option) any later version. However, please note that much
633 * of the code in this file derives from the Linux kernel, and that such
634 * code may not be available except under GPLv2.
636 * This program is distributed in the hope that it will be useful,
637 * but WITHOUT ANY WARRANTY; without even the implied warranty of
638 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
639 * GNU General Public License for more details.
641 * You should have received a copy of the GNU General Public License
642 * along with this program; if not, write to the Free Software
643 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
645 * Copyright (c) 2006 Paul E. McKenney, IBM.
652 #include <sys/types.h>
656 #include <sys/param.h>
657 /* #include "atomic.h" */
662 #define container_of(ptr, type, member) ({ \
663 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
664 (type *)( (char *)__mptr - offsetof(type,member) );})
667 * Default machine parameters.
670 #ifndef CACHE_LINE_SIZE
671 /* #define CACHE_LINE_SIZE 128 */
672 #endif /* #ifndef CACHE_LINE_SIZE */
675 * Exclusive locking primitives.
678 typedef pthread_mutex_t spinlock_t
;
680 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
681 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
683 static void spin_lock_init(spinlock_t
*sp
)
685 if (pthread_mutex_init(sp
, NULL
) != 0) {
686 perror("spin_lock_init:pthread_mutex_init");
691 static void spin_lock(spinlock_t
*sp
)
693 if (pthread_mutex_lock(sp
) != 0) {
694 perror("spin_lock:pthread_mutex_lock");
699 static void spin_unlock(spinlock_t
*sp
)
701 if (pthread_mutex_unlock(sp
) != 0) {
702 perror("spin_unlock:pthread_mutex_unlock");
707 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
708 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
711 * Thread creation/destruction primitives.
714 typedef pthread_t thread_id_t
;
716 #define NR_THREADS 128
718 #define __THREAD_ID_MAP_EMPTY 0
719 #define __THREAD_ID_MAP_WAITING 1
720 thread_id_t __thread_id_map
[NR_THREADS
];
721 spinlock_t __thread_id_map_mutex
;
723 #define for_each_thread(t) \
724 for (t = 0; t < NR_THREADS; t++)
726 #define for_each_running_thread(t) \
727 for (t = 0; t < NR_THREADS; t++) \
728 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
729 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
731 #define for_each_tid(t, tid) \
732 for (t = 0; t < NR_THREADS; t++) \
733 if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \
734 ((tid) != __THREAD_ID_MAP_WAITING))
736 pthread_key_t thread_id_key
;
738 static int __smp_thread_id(void)
741 thread_id_t tid
= pthread_self();
743 for (i
= 0; i
< NR_THREADS
; i
++) {
744 if (__thread_id_map
[i
] == tid
) {
745 long v
= i
+ 1; /* must be non-NULL. */
747 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
748 perror("pthread_setspecific");
754 spin_lock(&__thread_id_map_mutex
);
755 for (i
= 0; i
< NR_THREADS
; i
++) {
756 if (__thread_id_map
[i
] == tid
)
757 spin_unlock(&__thread_id_map_mutex
);
760 spin_unlock(&__thread_id_map_mutex
);
761 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
766 static int smp_thread_id(void)
770 id
= pthread_getspecific(thread_id_key
);
772 return __smp_thread_id();
773 return (long)(id
- 1);
776 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
781 spin_lock(&__thread_id_map_mutex
);
782 for (i
= 0; i
< NR_THREADS
; i
++) {
783 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
786 if (i
>= NR_THREADS
) {
787 spin_unlock(&__thread_id_map_mutex
);
788 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
791 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
792 spin_unlock(&__thread_id_map_mutex
);
793 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
794 perror("create_thread:pthread_create");
797 __thread_id_map
[i
] = tid
;
801 static void *wait_thread(thread_id_t tid
)
806 for (i
= 0; i
< NR_THREADS
; i
++) {
807 if (__thread_id_map
[i
] == tid
)
810 if (i
>= NR_THREADS
){
811 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n",
815 if (pthread_join(tid
, &vp
) != 0) {
816 perror("wait_thread:pthread_join");
819 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
823 static void wait_all_threads(void)
828 for (i
= 1; i
< NR_THREADS
; i
++) {
829 tid
= __thread_id_map
[i
];
830 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
831 tid
!= __THREAD_ID_MAP_WAITING
)
832 (void)wait_thread(tid
);
836 static void run_on(int cpu
)
842 sched_setaffinity(0, sizeof(mask
), &mask
);
846 * timekeeping -- very crude -- should use MONOTONIC...
849 long long get_microseconds(void)
853 if (gettimeofday(&tv
, NULL
) != 0)
855 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
859 * Per-thread variables.
862 #define DEFINE_PER_THREAD(type, name) \
865 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
866 } __per_thread_##name[NR_THREADS];
867 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
869 #define per_thread(name, thread) __per_thread_##name[thread].v
870 #define __get_thread_var(name) per_thread(name, smp_thread_id())
872 #define init_per_thread(name, v) \
875 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
876 per_thread(name, __i_p_t_i) = v; \
880 * CPU traversal primitives.
885 #endif /* #ifndef NR_CPUS */
887 #define for_each_possible_cpu(cpu) \
888 for (cpu = 0; cpu < NR_CPUS; cpu++)
889 #define for_each_online_cpu(cpu) \
890 for (cpu = 0; cpu < NR_CPUS; cpu++)
896 #define DEFINE_PER_CPU(type, name) \
899 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
900 } __per_cpu_##name[NR_CPUS]
901 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
903 DEFINE_PER_THREAD(int, smp_processor_id
);
905 #define per_cpu(name, thread) __per_cpu_##name[thread].v
906 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
908 #define init_per_cpu(name, v) \
911 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
912 per_cpu(name, __i_p_c_i) = v; \
916 * CPU state checking (crowbarred).
919 #define idle_cpu(cpu) 0
920 #define in_softirq() 1
921 #define hardirq_count() 0
922 #define PREEMPT_SHIFT 0
923 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
924 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
925 #define PREEMPT_BITS 8
926 #define SOFTIRQ_BITS 8
932 struct notifier_block
{
933 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
934 struct notifier_block
*next
;
938 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
939 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
940 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
941 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
942 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
943 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
944 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
945 * not handling interrupts, soon dead */
946 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
949 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
950 * operation in progress
952 #define CPU_TASKS_FROZEN 0x0010
954 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
955 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
956 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
957 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
958 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
959 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
960 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
962 /* Hibernation and suspend events */
963 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
964 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
965 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
966 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
967 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
968 #define PM_POST_RESTORE 0x0006 /* Restore failed */
970 #define NOTIFY_DONE 0x0000 /* Don't care */
971 #define NOTIFY_OK 0x0001 /* Suits me */
972 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
973 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
974 /* Bad/Veto action */
976 * Clean way to return from the notifier and stop further calls.
978 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
984 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
987 * Initialization -- Must be called before calling any primitives.
990 static void smp_init(void)
994 spin_lock_init(&__thread_id_map_mutex
);
995 __thread_id_map
[0] = pthread_self();
996 for (i
= 1; i
< NR_THREADS
; i
++)
997 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
998 init_per_thread(smp_processor_id
, 0);
999 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
1000 perror("pthread_key_create");
1005 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
1007 #ifndef _LINUX_LIST_H
1008 #define _LINUX_LIST_H
1010 #define LIST_POISON1 ((void *) 0x00100100)
1011 #define LIST_POISON2 ((void *) 0x00200200)
1013 #define container_of(ptr, type, member) ({ \
1014 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
1015 (type *)( (char *)__mptr - offsetof(type,member) );})
1020 * Simple doubly linked list implementation.
1022 * Some of the internal functions ("__xxx") are useful when
1023 * manipulating whole lists rather than single entries, as
1024 * sometimes we already know the next/prev entries and we can
1025 * generate better code by using them directly rather than
1026 * using the generic single-entry routines.
1030 struct list_head
*next
, *prev
;
1033 #define LIST_HEAD_INIT(name) { &(name), &(name) }
1035 #define LIST_HEAD(name) \
1036 struct list_head name = LIST_HEAD_INIT(name)
1038 static inline void INIT_LIST_HEAD(struct list_head
*list
)
1045 * Insert a new entry between two known consecutive entries.
1047 * This is only for internal list manipulation where we know
1048 * the prev/next entries already!
1050 #ifndef CONFIG_DEBUG_LIST
1051 static inline void __list_add(struct list_head
*new,
1052 struct list_head
*prev
,
1053 struct list_head
*next
)
1061 extern void __list_add(struct list_head
*new,
1062 struct list_head
*prev
,
1063 struct list_head
*next
);
1067 * list_add - add a new entry
1068 * @new: new entry to be added
1069 * @head: list head to add it after
1071 * Insert a new entry after the specified head.
1072 * This is good for implementing stacks.
1074 static inline void list_add(struct list_head
*new, struct list_head
*head
)
1076 __list_add(new, head
, head
->next
);
1081 * list_add_tail - add a new entry
1082 * @new: new entry to be added
1083 * @head: list head to add it before
1085 * Insert a new entry before the specified head.
1086 * This is useful for implementing queues.
1088 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
1090 __list_add(new, head
->prev
, head
);
1094 * Delete a list entry by making the prev/next entries
1095 * point to each other.
1097 * This is only for internal list manipulation where we know
1098 * the prev/next entries already!
1100 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
1107 * list_del - deletes entry from list.
1108 * @entry: the element to delete from the list.
1109 * Note: list_empty() on entry does not return true after this, the entry is
1110 * in an undefined state.
1112 #ifndef CONFIG_DEBUG_LIST
1113 static inline void list_del(struct list_head
*entry
)
1115 __list_del(entry
->prev
, entry
->next
);
1116 entry
->next
= LIST_POISON1
;
1117 entry
->prev
= LIST_POISON2
;
1120 extern void list_del(struct list_head
*entry
);
1124 * list_replace - replace old entry by new one
1125 * @old : the element to be replaced
1126 * @new : the new element to insert
1128 * If @old was empty, it will be overwritten.
1130 static inline void list_replace(struct list_head
*old
,
1131 struct list_head
*new)
1133 new->next
= old
->next
;
1134 new->next
->prev
= new;
1135 new->prev
= old
->prev
;
1136 new->prev
->next
= new;
1139 static inline void list_replace_init(struct list_head
*old
,
1140 struct list_head
*new)
1142 list_replace(old
, new);
1143 INIT_LIST_HEAD(old
);
1147 * list_del_init - deletes entry from list and reinitialize it.
1148 * @entry: the element to delete from the list.
1150 static inline void list_del_init(struct list_head
*entry
)
1152 __list_del(entry
->prev
, entry
->next
);
1153 INIT_LIST_HEAD(entry
);
1157 * list_move - delete from one list and add as another's head
1158 * @list: the entry to move
1159 * @head: the head that will precede our entry
1161 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
1163 __list_del(list
->prev
, list
->next
);
1164 list_add(list
, head
);
1168 * list_move_tail - delete from one list and add as another's tail
1169 * @list: the entry to move
1170 * @head: the head that will follow our entry
1172 static inline void list_move_tail(struct list_head
*list
,
1173 struct list_head
*head
)
1175 __list_del(list
->prev
, list
->next
);
1176 list_add_tail(list
, head
);
1180 * list_is_last - tests whether @list is the last entry in list @head
1181 * @list: the entry to test
1182 * @head: the head of the list
1184 static inline int list_is_last(const struct list_head
*list
,
1185 const struct list_head
*head
)
1187 return list
->next
== head
;
1191 * list_empty - tests whether a list is empty
1192 * @head: the list to test.
1194 static inline int list_empty(const struct list_head
*head
)
1196 return head
->next
== head
;
1200 * list_empty_careful - tests whether a list is empty and not being modified
1201 * @head: the list to test
1204 * tests whether a list is empty _and_ checks that no other CPU might be
1205 * in the process of modifying either member (next or prev)
1207 * NOTE: using list_empty_careful() without synchronization
1208 * can only be safe if the only activity that can happen
1209 * to the list entry is list_del_init(). Eg. it cannot be used
1210 * if another CPU could re-list_add() it.
1212 static inline int list_empty_careful(const struct list_head
*head
)
1214 struct list_head
*next
= head
->next
;
1215 return (next
== head
) && (next
== head
->prev
);
1219 * list_is_singular - tests whether a list has just one entry.
1220 * @head: the list to test.
1222 static inline int list_is_singular(const struct list_head
*head
)
1224 return !list_empty(head
) && (head
->next
== head
->prev
);
1227 static inline void __list_cut_position(struct list_head
*list
,
1228 struct list_head
*head
, struct list_head
*entry
)
1230 struct list_head
*new_first
= entry
->next
;
1231 list
->next
= head
->next
;
1232 list
->next
->prev
= list
;
1235 head
->next
= new_first
;
1236 new_first
->prev
= head
;
1240 * list_cut_position - cut a list into two
1241 * @list: a new list to add all removed entries
1242 * @head: a list with entries
1243 * @entry: an entry within head, could be the head itself
1244 * and if so we won't cut the list
1246 * This helper moves the initial part of @head, up to and
1247 * including @entry, from @head to @list. You should
1248 * pass on @entry an element you know is on @head. @list
1249 * should be an empty list or a list you do not care about
1253 static inline void list_cut_position(struct list_head
*list
,
1254 struct list_head
*head
, struct list_head
*entry
)
1256 if (list_empty(head
))
1258 if (list_is_singular(head
) &&
1259 (head
->next
!= entry
&& head
!= entry
))
1262 INIT_LIST_HEAD(list
);
1264 __list_cut_position(list
, head
, entry
);
1267 static inline void __list_splice(const struct list_head
*list
,
1268 struct list_head
*prev
,
1269 struct list_head
*next
)
1271 struct list_head
*first
= list
->next
;
1272 struct list_head
*last
= list
->prev
;
1282 * list_splice - join two lists, this is designed for stacks
1283 * @list: the new list to add.
1284 * @head: the place to add it in the first list.
1286 static inline void list_splice(const struct list_head
*list
,
1287 struct list_head
*head
)
1289 if (!list_empty(list
))
1290 __list_splice(list
, head
, head
->next
);
1294 * list_splice_tail - join two lists, each list being a queue
1295 * @list: the new list to add.
1296 * @head: the place to add it in the first list.
1298 static inline void list_splice_tail(struct list_head
*list
,
1299 struct list_head
*head
)
1301 if (!list_empty(list
))
1302 __list_splice(list
, head
->prev
, head
);
1306 * list_splice_init - join two lists and reinitialise the emptied list.
1307 * @list: the new list to add.
1308 * @head: the place to add it in the first list.
1310 * The list at @list is reinitialised
1312 static inline void list_splice_init(struct list_head
*list
,
1313 struct list_head
*head
)
1315 if (!list_empty(list
)) {
1316 __list_splice(list
, head
, head
->next
);
1317 INIT_LIST_HEAD(list
);
1322 * list_splice_tail_init - join two lists and reinitialise the emptied list
1323 * @list: the new list to add.
1324 * @head: the place to add it in the first list.
1326 * Each of the lists is a queue.
1327 * The list at @list is reinitialised
1329 static inline void list_splice_tail_init(struct list_head
*list
,
1330 struct list_head
*head
)
1332 if (!list_empty(list
)) {
1333 __list_splice(list
, head
->prev
, head
);
1334 INIT_LIST_HEAD(list
);
1339 * list_entry - get the struct for this entry
1340 * @ptr: the &struct list_head pointer.
1341 * @type: the type of the struct this is embedded in.
1342 * @member: the name of the list_struct within the struct.
1344 #define list_entry(ptr, type, member) \
1345 container_of(ptr, type, member)
1348 * list_first_entry - get the first element from a list
1349 * @ptr: the list head to take the element from.
1350 * @type: the type of the struct this is embedded in.
1351 * @member: the name of the list_struct within the struct.
1353 * Note, that list is expected to be not empty.
1355 #define list_first_entry(ptr, type, member) \
1356 list_entry((ptr)->next, type, member)
1359 * list_for_each - iterate over a list
1360 * @pos: the &struct list_head to use as a loop cursor.
1361 * @head: the head for your list.
1363 #define list_for_each(pos, head) \
1364 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
1368 * __list_for_each - iterate over a list
1369 * @pos: the &struct list_head to use as a loop cursor.
1370 * @head: the head for your list.
1372 * This variant differs from list_for_each() in that it's the
1373 * simplest possible list iteration code, no prefetching is done.
1374 * Use this for code that knows the list to be very short (empty
1375 * or 1 entry) most of the time.
1377 #define __list_for_each(pos, head) \
1378 for (pos = (head)->next; pos != (head); pos = pos->next)
1381 * list_for_each_prev - iterate over a list backwards
1382 * @pos: the &struct list_head to use as a loop cursor.
1383 * @head: the head for your list.
1385 #define list_for_each_prev(pos, head) \
1386 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1390 * list_for_each_safe - iterate over a list safe against removal of list entry
1391 * @pos: the &struct list_head to use as a loop cursor.
1392 * @n: another &struct list_head to use as temporary storage
1393 * @head: the head for your list.
1395 #define list_for_each_safe(pos, n, head) \
1396 for (pos = (head)->next, n = pos->next; pos != (head); \
1397 pos = n, n = pos->next)
1400 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1401 * @pos: the &struct list_head to use as a loop cursor.
1402 * @n: another &struct list_head to use as temporary storage
1403 * @head: the head for your list.
1405 #define list_for_each_prev_safe(pos, n, head) \
1406 for (pos = (head)->prev, n = pos->prev; \
1407 prefetch(pos->prev), pos != (head); \
1408 pos = n, n = pos->prev)
1411 * list_for_each_entry - iterate over list of given type
1412 * @pos: the type * to use as a loop cursor.
1413 * @head: the head for your list.
1414 * @member: the name of the list_struct within the struct.
1416 #define list_for_each_entry(pos, head, member) \
1417 for (pos = list_entry((head)->next, typeof(*pos), member); \
1418 prefetch(pos->member.next), &pos->member != (head); \
1419 pos = list_entry(pos->member.next, typeof(*pos), member))
1422 * list_for_each_entry_reverse - iterate backwards over list of given type.
1423 * @pos: the type * to use as a loop cursor.
1424 * @head: the head for your list.
1425 * @member: the name of the list_struct within the struct.
1427 #define list_for_each_entry_reverse(pos, head, member) \
1428 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1429 prefetch(pos->member.prev), &pos->member != (head); \
1430 pos = list_entry(pos->member.prev, typeof(*pos), member))
1433 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1434 * @pos: the type * to use as a start point
1435 * @head: the head of the list
1436 * @member: the name of the list_struct within the struct.
1438 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1440 #define list_prepare_entry(pos, head, member) \
1441 ((pos) ? : list_entry(head, typeof(*pos), member))
1444 * list_for_each_entry_continue - continue iteration over list of given type
1445 * @pos: the type * to use as a loop cursor.
1446 * @head: the head for your list.
1447 * @member: the name of the list_struct within the struct.
1449 * Continue to iterate over list of given type, continuing after
1450 * the current position.
1452 #define list_for_each_entry_continue(pos, head, member) \
1453 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1454 prefetch(pos->member.next), &pos->member != (head); \
1455 pos = list_entry(pos->member.next, typeof(*pos), member))
1458 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1459 * @pos: the type * to use as a loop cursor.
1460 * @head: the head for your list.
1461 * @member: the name of the list_struct within the struct.
1463 * Start to iterate over list of given type backwards, continuing after
1464 * the current position.
1466 #define list_for_each_entry_continue_reverse(pos, head, member) \
1467 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1468 prefetch(pos->member.prev), &pos->member != (head); \
1469 pos = list_entry(pos->member.prev, typeof(*pos), member))
1472 * list_for_each_entry_from - iterate over list of given type from the current point
1473 * @pos: the type * to use as a loop cursor.
1474 * @head: the head for your list.
1475 * @member: the name of the list_struct within the struct.
1477 * Iterate over list of given type, continuing from current position.
1479 #define list_for_each_entry_from(pos, head, member) \
1480 for (; prefetch(pos->member.next), &pos->member != (head); \
1481 pos = list_entry(pos->member.next, typeof(*pos), member))
1484 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1485 * @pos: the type * to use as a loop cursor.
1486 * @n: another type * to use as temporary storage
1487 * @head: the head for your list.
1488 * @member: the name of the list_struct within the struct.
1490 #define list_for_each_entry_safe(pos, n, head, member) \
1491 for (pos = list_entry((head)->next, typeof(*pos), member), \
1492 n = list_entry(pos->member.next, typeof(*pos), member); \
1493 &pos->member != (head); \
1494 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1497 * list_for_each_entry_safe_continue
1498 * @pos: the type * to use as a loop cursor.
1499 * @n: another type * to use as temporary storage
1500 * @head: the head for your list.
1501 * @member: the name of the list_struct within the struct.
1503 * Iterate over list of given type, continuing after current point,
1504 * safe against removal of list entry.
1506 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1507 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1508 n = list_entry(pos->member.next, typeof(*pos), member); \
1509 &pos->member != (head); \
1510 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1513 * list_for_each_entry_safe_from
1514 * @pos: the type * to use as a loop cursor.
1515 * @n: another type * to use as temporary storage
1516 * @head: the head for your list.
1517 * @member: the name of the list_struct within the struct.
1519 * Iterate over list of given type from current point, safe against
1520 * removal of list entry.
1522 #define list_for_each_entry_safe_from(pos, n, head, member) \
1523 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1524 &pos->member != (head); \
1525 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1528 * list_for_each_entry_safe_reverse
1529 * @pos: the type * to use as a loop cursor.
1530 * @n: another type * to use as temporary storage
1531 * @head: the head for your list.
1532 * @member: the name of the list_struct within the struct.
1534 * Iterate backwards over list of given type, safe against removal
1537 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1538 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1539 n = list_entry(pos->member.prev, typeof(*pos), member); \
1540 &pos->member != (head); \
1541 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1546 * Double linked lists with a single pointer list head.
1547 * Mostly useful for hash tables where the two pointer list head is
1549 * You lose the ability to access the tail in O(1).
1553 struct hlist_node
*first
;
1557 struct hlist_node
*next
, **pprev
;
1560 #define HLIST_HEAD_INIT { .first = NULL }
1561 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1562 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1563 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
1569 static inline int hlist_unhashed(const struct hlist_node
*h
)
1574 static inline int hlist_empty(const struct hlist_head
*h
)
1579 static inline void __hlist_del(struct hlist_node
*n
)
1581 struct hlist_node
*next
= n
->next
;
1582 struct hlist_node
**pprev
= n
->pprev
;
1585 next
->pprev
= pprev
;
1588 static inline void hlist_del(struct hlist_node
*n
)
1591 n
->next
= LIST_POISON1
;
1592 n
->pprev
= LIST_POISON2
;
1595 static inline void hlist_del_init(struct hlist_node
*n
)
1597 if (!hlist_unhashed(n
)) {
1603 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
1605 struct hlist_node
*first
= h
->first
;
1608 first
->pprev
= &n
->next
;
1610 n
->pprev
= &h
->first
;
1613 /* next must be != NULL */
1614 static inline void hlist_add_before(struct hlist_node
*n
,
1615 struct hlist_node
*next
)
1617 n
->pprev
= next
->pprev
;
1619 next
->pprev
= &n
->next
;
1623 static inline void hlist_add_after(struct hlist_node
*n
,
1624 struct hlist_node
*next
)
1626 next
->next
= n
->next
;
1628 next
->pprev
= &n
->next
;
1631 next
->next
->pprev
= &next
->next
;
1635 * Move a list from one list head to another. Fixup the pprev
1636 * reference of the first entry if it exists.
1638 static inline void hlist_move_list(struct hlist_head
*old
,
1639 struct hlist_head
*new)
1641 new->first
= old
->first
;
1643 new->first
->pprev
= &new->first
;
1647 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1649 #define hlist_for_each(pos, head) \
1650 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1653 #define hlist_for_each_safe(pos, n, head) \
1654 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1658 * hlist_for_each_entry - iterate over list of given type
1659 * @tpos: the type * to use as a loop cursor.
1660 * @pos: the &struct hlist_node to use as a loop cursor.
1661 * @head: the head for your list.
1662 * @member: the name of the hlist_node within the struct.
1664 #define hlist_for_each_entry(tpos, pos, head, member) \
1665 for (pos = (head)->first; \
1666 pos && ({ prefetch(pos->next); 1;}) && \
1667 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1671 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1672 * @tpos: the type * to use as a loop cursor.
1673 * @pos: the &struct hlist_node to use as a loop cursor.
1674 * @member: the name of the hlist_node within the struct.
1676 #define hlist_for_each_entry_continue(tpos, pos, member) \
1677 for (pos = (pos)->next; \
1678 pos && ({ prefetch(pos->next); 1;}) && \
1679 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1683 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1684 * @tpos: the type * to use as a loop cursor.
1685 * @pos: the &struct hlist_node to use as a loop cursor.
1686 * @member: the name of the hlist_node within the struct.
1688 #define hlist_for_each_entry_from(tpos, pos, member) \
1689 for (; pos && ({ prefetch(pos->next); 1;}) && \
1690 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1694 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1695 * @tpos: the type * to use as a loop cursor.
1696 * @pos: the &struct hlist_node to use as a loop cursor.
1697 * @n: another &struct hlist_node to use as temporary storage
1698 * @head: the head for your list.
1699 * @member: the name of the hlist_node within the struct.
1701 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1702 for (pos = (head)->first; \
1703 pos && ({ n = pos->next; 1; }) && \
1704 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \