UCAS(9) | Kernel Developer's Manual | UCAS(9)

# NAME

`ucas` —
atomic memory operations on user-space address

# SYNOPSIS

`#include <sys/systm.h>`

int
`ucas_ptr`(volatile void *uptr, void *old, void *new, void *retp);

int
`ucas_int`(volatile int *uptr, int old, int new, int *retp);

# DESCRIPTION

These functions provide compare-and-swap (CAS) functionality on user-space address.

Except that they can be safely used for the kernel to access user-space address, they are semantically equivalents of atomic_cas(3).

uptr
  The pointer to the variable. This should be a user-space pointer.
old
  The value to compare with the variable.
new
  The value to store to the variable.
retp
  The pointer to the memory to store the old value of the variable.

# IMPLEMENTATION NOTES

The `ucas` functions are implemented in machine-independent code, but rely on machine-dependent code to implement optimized primitives, if possible.

The basic `ucas` primitives have the following signatures and are considered private to the implementation and are not to be called by consumers of the `ucas` API:

int `_ucas_32`(volatile uint32_t *uptr, uint32_t old, uint32_t new, uint32_t *retp);
int `_ucas_64`(volatile uint64_t *uptr, uint64_t old, uint64_t new, uint64_t *retp);

If a platform is able to provide a CAS operation that meets the following criteria, it should define `__HAVE_UCAS_FULL` in `<machine/types.h>` and provide complete machine-dependent implementations of `_ucas_32`() (and `_ucas_64`(), if an `_LP64` platform):

- Can be implemented using either native compare-and-swap operations or load-locked / store-conditional code sequences.
- Can be used on uniprocessor and multiprocessor systems.
- Can operate across the kernel-userpsace boundary.

If `__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 “least-common denominator” principle. In particular, kernel preemption is disabled during the critical section (which is comprised of ufetch(9) and ustore(9) operations), and the multiprocessor implementation synchronizes with other CPUs using interprocessor interrupts.

If a particular platform wishes to use the generic implementation on uniprocessors but an optimized implementation on multiprocessors, the the platform should define `__HAVE_UCAS_MP` in `<machine/types.h>` and provide `_ucas_32_mp`() (and `_ucas_64_mp`(), if an `_LP64` platform).

# RETURN VALUES

On success, these functions return 0. In that case, the caller can consult the value returned via retp to check the result of the CAS operation. Otherwise, these functions return an appropriate errno(9) error code, typically `EFAULT`.

# SEE ALSO

atomic_cas(3), intro(9)

# BUGS

Conceptually, the retp argument of `ucas_ptr`() would be of `void **`. The current prototype is a compromise for usability.

March 31, 2019 | NetBSD 9.4