atomic.h revision 48797
138517Sdfr/*-
238517Sdfr * Copyright (c) 1998 Doug Rabson
338517Sdfr * All rights reserved.
438517Sdfr *
538517Sdfr * Redistribution and use in source and binary forms, with or without
638517Sdfr * modification, are permitted provided that the following conditions
738517Sdfr * are met:
838517Sdfr * 1. Redistributions of source code must retain the above copyright
938517Sdfr *    notice, this list of conditions and the following disclaimer.
1038517Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1138517Sdfr *    notice, this list of conditions and the following disclaimer in the
1238517Sdfr *    documentation and/or other materials provided with the distribution.
1338517Sdfr *
1438517Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1538517Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1638517Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1738517Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1838517Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1938517Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2038517Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2138517Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2238517Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2338517Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2438517Sdfr * SUCH DAMAGE.
2538517Sdfr *
2648797Salc *	$Id: atomic.h,v 1.2 1999/07/13 03:32:17 alc Exp $
2738517Sdfr */
2838517Sdfr#ifndef _MACHINE_ATOMIC_H_
2938517Sdfr#define _MACHINE_ATOMIC_H_
3038517Sdfr
3138517Sdfr/*
3238517Sdfr * Various simple arithmetic on memory which is atomic in the presence
3348797Salc * of interrupts and multiple processors.
3438517Sdfr *
3548797Salc * atomic_set_char(P, V)	(*(u_char*)(P) |= (V))
3648797Salc * atomic_clear_char(P, V)	(*(u_char*)(P) &= ~(V))
3748797Salc * atomic_add_char(P, V)	(*(u_char*)(P) += (V))
3848797Salc * atomic_subtract_char(P, V)	(*(u_char*)(P) -= (V))
3948797Salc *
4048797Salc * atomic_set_short(P, V)	(*(u_short*)(P) |= (V))
4148797Salc * atomic_clear_short(P, V)	(*(u_short*)(P) &= ~(V))
4248797Salc * atomic_add_short(P, V)	(*(u_short*)(P) += (V))
4348797Salc * atomic_subtract_short(P, V)	(*(u_short*)(P) -= (V))
4448797Salc *
4548797Salc * atomic_set_int(P, V)		(*(u_int*)(P) |= (V))
4648797Salc * atomic_clear_int(P, V)	(*(u_int*)(P) &= ~(V))
4748797Salc * atomic_add_int(P, V)		(*(u_int*)(P) += (V))
4848797Salc * atomic_subtract_int(P, V)	(*(u_int*)(P) -= (V))
4948797Salc *
5048797Salc * atomic_set_long(P, V)	(*(u_long*)(P) |= (V))
5148797Salc * atomic_clear_long(P, V)	(*(u_long*)(P) &= ~(V))
5248797Salc * atomic_add_long(P, V)	(*(u_long*)(P) += (V))
5348797Salc * atomic_subtract_long(P, V)	(*(u_long*)(P) -= (V))
5438517Sdfr */
5538517Sdfr
5648797Salc/*
5748797Salc * Make kernel modules portable between UP and SMP.
5848797Salc */
5948797Salc#if defined(SMP) || defined(KLD_MODULE)
6048797Salc#define MPLOCKED	"lock ; "
6148796Salc#else
6248797Salc#define MPLOCKED
6348796Salc#endif
6438517Sdfr
6548797Salc/*
6648797Salc * The assembly is volatilized to demark potential before-and-after side
6748797Salc * effects if an interrupt or SMP collision were to occur.
6848797Salc */
6948797Salc#define ATOMIC_ASM(NAME, TYPE, OP, V)			\
7048797Salcstatic __inline void					\
7148797Salcatomic_##NAME##_##TYPE(void *p, u_##TYPE v)		\
7248797Salc{							\
7348797Salc	__asm __volatile(MPLOCKED OP			\
7448797Salc			 : "=m" (*(u_##TYPE *)p)	\
7548797Salc			 :  "0" (*(u_##TYPE *)p), "ir" (V)); \
7648797Salc}
7738517Sdfr
7848797SalcATOMIC_ASM(set,	     char,  "orb %2,%0",   v)
7948797SalcATOMIC_ASM(clear,    char,  "andb %2,%0", ~v)
8048797SalcATOMIC_ASM(add,	     char,  "addb %2,%0",  v)
8148797SalcATOMIC_ASM(subtract, char,  "subb %2,%0",  v)
8238517Sdfr
8348797SalcATOMIC_ASM(set,	     short, "orw %2,%0",   v)
8448797SalcATOMIC_ASM(clear,    short, "andw %2,%0", ~v)
8548797SalcATOMIC_ASM(add,	     short, "addw %2,%0",  v)
8648797SalcATOMIC_ASM(subtract, short, "subw %2,%0",  v)
8738517Sdfr
8848797SalcATOMIC_ASM(set,	     int,   "orl %2,%0",   v)
8948797SalcATOMIC_ASM(clear,    int,   "andl %2,%0", ~v)
9048797SalcATOMIC_ASM(add,	     int,   "addl %2,%0",  v)
9148797SalcATOMIC_ASM(subtract, int,   "subl %2,%0",  v)
9248796Salc
9348797SalcATOMIC_ASM(set,	     long,  "orl %2,%0",   v)
9448797SalcATOMIC_ASM(clear,    long,  "andl %2,%0", ~v)
9548797SalcATOMIC_ASM(add,	     long,  "addl %2,%0",  v)
9648797SalcATOMIC_ASM(subtract, long,  "subl %2,%0",  v)
9748796Salc
9838517Sdfr#endif /* ! _MACHINE_ATOMIC_H_ */
99