.\" $NetBSD: ucas.9,v 1.7 2021/10/04 20:25:20 andvar Exp $ .\" .\" 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 .In 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. .Pp Except that they can be safely used for the kernel to access user-space address, they are semantically equivalents of .Xr atomic_cas 3 . .Bl -tag -width uptr .It Fa uptr The pointer to the variable. This should be a user-space pointer. .It Fa old The value to compare with the variable. .It Fa new The value to store to the variable. .It Fa retp The pointer to the memory to store the old value of the variable. .El .\" ------------------------------------------------------------ .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. .Pp 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: .Bl -tag -width _ucas_32 .It Ft int Fn _ucas_32 \ "volatile uint32_t *uptr" "uint32_t old" "uint32_t new" "uint32_t *retp" ; .It Ft int Fn _ucas_64 \ "volatile uint64_t *uptr" "uint64_t old" "uint64_t new" "uint64_t *retp" ; .El .Pp If a platform is able to provide a CAS operation that meets the following criteria, it should define .Dv __HAVE_UCAS_FULL in .In machine/types.h and provide complete machine-dependent implementations of .Fn _ucas_32 .Po and .Fn _ucas_64 , if an .Dv _LP64 platform .Pc : .Pp .Bl -hyphen -compact .It Can be implemented using either native compare-and-swap operations or load-locked / store-conditional code sequences. .It Can be used on uniprocessor and multiprocessor systems. .It Can operate across the kernel-userspace boundary. .El .Pp If .Dv __HAVE_UCAS_FULL is not defined, then 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 .Po which is comprised of .Xr ufetch 9 and .Xr ustore 9 operations .Pc , and the multiprocessor implementation synchronizes with other CPUs using interprocessor interrupts. .Pp If a particular platform wishes to use the generic implementation on uniprocessors but an optimized implementation on multiprocessors, the platform should define .Dv __HAVE_UCAS_MP in .In machine/types.h and provide .Fn _ucas_32_mp .Po and .Fn _ucas_64_mp , if an .Dv _LP64 platform .Pc . .\" ------------------------------------------------------------ .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 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.