CASU(9)

πŸ’– CASU(9) β€” the kernel dev manual, but make it slay πŸ’–

okay bestie, so this is the page about casueword, casueword32, casuword, and casuword32 β€” aka the little guys who go fetch, compare and store data from user-space. it's giving kernel-mode, it's giving atomic, it's giving "please align your pointers or perish". let's get into it πŸ¦‹


✨ NAME ✨

casueword, casueword32, casuword, casuword32 β€” fetch, compare and store data from user-space


🌸 SYNOPSIS 🌸

#include <sys/types.h>
#include <sys/systm.h>

int
casueword(volatile u_long *base, u_long oldval, u_long *oldvalp,
    u_long newval);

int
casueword32(volatile uint32_t *base, uint32_t oldval, uint32_t *oldvalp,
    uint32_t newval);

u_long
casuword(volatile u_long *base, u_long oldval, u_long newval);

uint32_t
casuword32(volatile uint32_t *base, uint32_t oldval, uint32_t newval);

πŸŽ€ DESCRIPTION πŸŽ€

so the casueword girlies are literally designed to do an atomic compare-and-swap operation on the value living in the usermode memory of the current process. main character energy fr fr πŸ’…

here's the tea: the casueword routines read the value from user memory at address base, and then compare the value they read with oldval. if those two are equal β€” slay, match β€” then newval gets written to *base. and for casueword32() and casueword(), the old value is stored into the (kernel-mode) variable pointed to by *oldvalp. no cap, that's just how it works ✨

okay but the way that the userspace value must be naturally aligned β€” like, non-negotiable, bestie. it's giving strict. it's giving boundaries. respect them 🫢

and here's the sad part 😭 the callers of casuword() and casuword32() cannot distinguish between -1 read from userspace and function failure. like... it's the same number either way. tragic. iconic. anyway πŸ’€


⭐ RETURN VALUES ⭐

  • the casuword() and casuword32() functions return the data fetched β€” or -1 on failure. that's it, that's the whole vibe.
  • the casueword() and casueword32() functions are a lil more chatty: they return 0 on success, -1 on failure to access memory, and 1 when comparison or store failed. three whole moods 🌈
  • also!! the store can fail on load-linked/store-conditional architectures. so if it fails, it's not you, it's the architecture πŸ’œ

πŸ‘‘ SEE ALSO πŸ‘‘

atomic(9), fetch(9), store(9)


and that's the page, bestie!! this one came out of the Debian crew, dated April 19, 2019 β€” so like, vintage but still serving. go forth and compare-and-swap responsibly βœ¨πŸ’–

πŸ“¦ where this page comes from