Copyright (c) 2019 Jason R. Thorpe.
Copyright (c) 2011 YAMAMOTO Takashi,
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
------------------------------------------------------------
.Dd March 31, 2019 .Dt UCAS 9 .Os ------------------------------------------------------------
.Sh NAME .Nm ucas .Nd atomic memory operations on user-space address ------------------------------------------------------------
.Sh SYNOPSIS n sys/systm.h - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Ft int .Fn ucas_ptr \ "volatile void *uptr" "void *old" "void *new" "void *retp" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
.Ft int .Fn ucas_int \ "volatile int *uptr" "int old" "int new" "int *retp" ------------------------------------------------------------
.Sh DESCRIPTION These functions provide compare-and-swap (CAS) functionality on user-space address.
p Except that they can be safely used for the kernel to access user-space address, they are semantically equivalents of .Xr atomic_cas 3 .
p
l -tag -width uptr t Fa uptr The pointer to the variable.
This should be a user-space pointer.
t Fa old The value to compare with the variable.
t Fa new The value to store to the variable.
t Fa retp The pointer to the memory to store the old value of the variable.
.El
------------------------------------------------------------
.Sh RETURN VALUES
On success, these functions return 0.
In that case, the caller can consult the value returned via
.Fa retp
to check the result of the CAS operation.
Otherwise, these functions return an appropriate
.Xr errno 9
error code, typically
.Dv EFAULT .
------------------------------------------------------------
.Sh IMPLEMENTATION NOTES
The
.Nm ucas
functions are implemented in machine-independent code, but rely on
machine-dependent code to implement optimized primitives, if possible.
p The basic .Nm ucas primitives have the following signatures and are considered private to the implementation and are not to be called by consumers of the .Nm ucas API:
p l -tag -width _ucas_32 t Ft int Fn _ucas_32 \ "volatile uint32_t *uptr" "uint32_t old" "uint32_t new" "uint32_t *retp" ; t Ft int Fn _ucas_64 \ "volatile uint64_t *uptr" "uint64_t old" "uint64_t new" "uint64_t *retp" ; .El
p If a platform is able to provide a CAS operation that meets the following criteria, it should define .Dv __HAVE_UCAS_FULL in n machine/types.h and provide complete machine-dependent implementations of .Fn _ucas_32
o and .Fn _ucas_64 , if an .Dv _LP64 platform
c :
p l -hyphen -compact t Can be implemented using either native compare-and-swap operations or load-locked / store-conditional code sequences. t Can be used on uniprocessor and multiprocessor systems. t Can operate across the kernel-userpsace boundary. .El
p If .Dv __HAVE_UCAS_FULL is not defined, than a generic implementation will be provided by machine-dependent code. This generic implementation is suitable for uniprocessor and multiprocessor systems, but works on a .Dq least-common denominator principle. In particular, kernel preemption is disabled during the critical section
o which is comprised of .Xr ufetch 9 and .Xr ustore 9 operations
c , and the multiprocessor implementation synchronizes with other CPUs using interprocessor interrupts.
p If a particular platform wishes ro use the generic implementation on uniprocessors but an optimized implementation on multiprocessors, the the platform should define .Dv __HAVE_UCAS_MP in n machine/types.h and provide .Fn _ucas_32_mp
o and .Fn _ucas_64_mp , if an .Dv _LP64 platform
c .
------------------------------------------------------------
.Sh SEE ALSO
.Xr atomic_cas 3 ,
.Xr intro 9
------------------------------------------------------------
.Sh BUGS
Conceptually, the
.Fa retp
argument of
.Fn ucas_ptr
would be of
.Dv void ** .
The current prototype is a compromise for usability.