aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / spinlock.h
| 1 | /* spinlock.h -- the kernel's one spinlock type: a fair (FIFO) ticket lock. Its default acquire masks interrupts |
| 2 | * on the calling CPU for as long as the lock is held. |
| 3 | * |
| 4 | * The lock is the ticket lock that core/pipe.c carried privately (pipe.c:40-51 at d01b2f19, pool_lock / |
| 5 | * pool_unlock, the same __atomic_* orders core/smp.c uses), moved here. Every spinning lock in the kernel tree is |
| 6 | * this type: the pipe pool lock (core/pipe.c), Allen's note lock (ui/allen_ui.c) and the iMac frame lock |
| 7 | * (x86/kmain_x86.c draw_lock). |
| 8 | * |
| 9 | * ticket the next number handed out; a caller takes one with a relaxed fetch-add. |
| 10 | * serving the number now inside; the caller spins (acquire) until serving == its number, and the |
| 11 | * holder releases by storing serving + 1 (release). Both equal means unlocked. |
| 12 | * |
| 13 | * Fair: callers enter in the order they took tickets. Mutual exclusion holds while fewer than 2^32 callers wait on |
| 14 | * one lock at once (the counters wrap; only equality is compared). Proof of the arithmetic and of the interrupt |
| 15 | * ordering below: proofs/TicketLock.lean (make ticketlock-proof). |
| 16 | * |
| 17 | * Interrupts (the default pair). ck_spin_lock_irqsave masks interrupts on this CPU BEFORE it takes a ticket and |
| 18 | * returns the previous mask; ck_spin_unlock_irqrestore releases the lock and THEN puts that mask back. So an |
| 19 | * interrupt handler on the CPU that holds (or is queued for) a lock cannot spin on that same lock and deadlock |
| 20 | * the CPU against itself; the interrupt is taken after the unlock instead. Nested locks restore in reverse order |
| 21 | * and the outermost restore re-opens interrupts. A caller that already had interrupts masked gets them back masked. |
| 22 | * |
| 23 | * aarch64 kernel DAIF.I and DAIF.F are set ("msr daifset, #3"); the saved DAIF is written back. arch/arch.h |
| 24 | * ck_irq_off masks I only; this lock also masks F so a FIQ handler, if one is ever installed, is |
| 25 | * covered too. SError (A) and debug (D) are not masked. |
| 26 | * x86-64 kernel RFLAGS is saved and "cli" runs; IF is set again only if it was set before. NMI cannot be masked |
| 27 | * by cli: an NMI handler must never take a ck_spinlock. Reading RFLAGS uses pushfq inside inline |
| 28 | * asm, which is safe only because the x86 kernel is built with -mno-red-zone (X86CFLAGS). |
| 29 | * hosted build (host unit tests, __STDC_HOSTED__) a thread's interrupt is the signal CK_SPIN_HOST_IRQ_SIGNAL |
| 30 | * (default SIGUSR1); "masked" means that signal is blocked for the thread, and the saved value |
| 31 | * says whether it already was. Only that one signal is touched: synchronous faults (SIGSEGV, |
| 32 | * SIGBUS) are never deferred. While waiting, a host thread yields its CPU (ck_spin_relax). |
| 33 | * |
| 34 | * ck_spin_lock / ck_spin_unlock are the same pair with the saved state kept in the lock itself: only the holder |
| 35 | * writes l->irq (after it entered) and reads it (before it leaves), so the next holder cannot see or clobber it. |
| 36 | * Locks must be released in reverse order of taking, with either pair. |
| 37 | * |
| 38 | * The _nomask forms (lock, trylock, unlock) take the same ticket lock and leave interrupts as they are. Use them |
| 39 | * only for a lock that no interrupt handler ever takes AND whose holders or waiters must keep taking interrupts: |
| 40 | * the iMac kernel's drawing cores keep interrupts on to acknowledge TLB-shootdown IPIs (x86/kmain_x86.c), and a |
| 41 | * core spinning with interrupts masked for a lock whose holder waits on that acknowledgement would deadlock. |
| 42 | * A trylock succeeds only when nobody holds or waits (it never jumps the queue). |
| 43 | * |
| 44 | * Code that holds a lock must not sleep or park: a wait hook runs after the unlock (see core/pipe.c). |
| 45 | * |
| 46 | * Why C and not Omega: every user is a C translation unit of the C kernel, the lock is inlined into the caller's |
| 47 | * critical section, and it runs privileged instructions (mrs/msr daif, pushfq/cli/sti). The newest omc builds |
| 48 | * standalone programs, and the omega-bridge freestanding route emits separate units; neither gives an inline unit a |
| 49 | * C kernel includes, with system-register access (aienux AGENTS.md section 6: C where a hardware reason exists). |
| 50 | * |
| 51 | * TEST-ONLY (host builds only; a freestanding build refuses them): |
| 52 | * CK_SPIN_TEST_STEP(l) called between the two steps of acquire (mask, ticket) and of release |
| 53 | * (release, restore); test_spinlock raises an interrupt there. |
| 54 | * CK_SPIN_TEST_NO_IRQ_MASK mutant: the mask is a no-op. |
| 55 | * CK_SPIN_TEST_TICKET_BEFORE_MASK mutant: the ticket is taken before the mask. |
| 56 | * CK_SPIN_TEST_RESTORE_BEFORE_RELEASE mutant: the mask is restored before the release. |
| 57 | */ |
| 58 | #ifndef AIENOS_CK_SPINLOCK_H |
| 59 | #define AIENOS_CK_SPINLOCK_H |
| 60 | |
| 61 | #include <stdint.h> |
| 62 | |
| 63 | /* Saved interrupt state returned by ck_spin_lock_irqsave; pass it back unchanged. */ |
| 64 | typedef uint64_t ck_irqflags_t; |
| 65 | |
| 66 | struct ck_spinlock { |
| 67 | uint32_t ticket; /* next number handed out */ |
| 68 | uint32_t serving; /* number now inside */ |
| 69 | ck_irqflags_t irq; /* ck_spin_lock only: the holder's saved state; written after entry, read before exit */ |
| 70 | }; |
| 71 | |
| 72 | #define CK_SPINLOCK_INIT { 0u, 0u, 0u } |
| 73 | |
| 74 | #if (defined(CK_SPIN_TEST_NO_IRQ_MASK) || defined(CK_SPIN_TEST_TICKET_BEFORE_MASK) || \ |
| 75 | defined(CK_SPIN_TEST_RESTORE_BEFORE_RELEASE) || defined(CK_SPIN_TEST_STEP)) && \ |
| 76 | !__STDC_HOSTED__ |
| 77 | #error "CK_SPIN_TEST_* are TEST-ONLY host hooks and mutants" |
| 78 | #endif |
| 79 | |
| 80 | #ifndef CK_SPIN_TEST_STEP |
| 81 | #define CK_SPIN_TEST_STEP(l) ((void)(l)) |
| 82 | #endif |
| 83 | |
| 84 | #if __STDC_HOSTED__ |
| 85 | #include <pthread.h> |
| 86 | #include <signal.h> |
| 87 | int sched_yield(void); /* POSIX <sched.h>; declared here because some host builds put core/ (core/sched.h) on -I */ |
| 88 | #ifndef CK_SPIN_HOST_IRQ_SIGNAL |
| 89 | #define CK_SPIN_HOST_IRQ_SIGNAL SIGUSR1 |
| 90 | #endif |
| 91 | #endif |
| 92 | |
| 93 | static inline void ck_spin_relax(void) |
| 94 | { |
| 95 | #if __STDC_HOSTED__ |
| 96 | /* A host thread is not a CPU: the holder or the next ticket may be descheduled on this very CPU, so give it the |
| 97 | * CPU (with fewer CPUs than threads a fair lock otherwise convoys for seconds per handoff). */ |
| 98 | sched_yield(); |
| 99 | #elif defined(__aarch64__) |
| 100 | __asm__ volatile("yield" ::: "memory"); |
| 101 | #elif defined(__x86_64__) || defined(__i386__) |
| 102 | __asm__ volatile("pause" ::: "memory"); |
| 103 | #else |
| 104 | __asm__ volatile("" ::: "memory"); |
| 105 | #endif |
| 106 | } |
| 107 | |
| 108 | /* Mask interrupts on this CPU; return the previous state. */ |
| 109 | static inline ck_irqflags_t ck_irq_save(void) |
| 110 | { |
| 111 | #if defined(CK_SPIN_TEST_NO_IRQ_MASK) |
| 112 | return 0; |
| 113 | #elif __STDC_HOSTED__ |
| 114 | sigset_t one, old; |
| 115 | sigemptyset(&one); |
| 116 | sigaddset(&one, CK_SPIN_HOST_IRQ_SIGNAL); |
| 117 | pthread_sigmask(SIG_BLOCK, &one, &old); |
| 118 | return (ck_irqflags_t)(sigismember(&old, CK_SPIN_HOST_IRQ_SIGNAL) == 1); |
| 119 | #elif defined(__aarch64__) |
| 120 | uint64_t daif; |
| 121 | __asm__ volatile("mrs %0, daif\n\tmsr daifset, #3" : "=r"(daif) : : "memory"); |
| 122 | return daif; |
| 123 | #elif defined(__x86_64__) |
| 124 | uint64_t rflags; |
| 125 | __asm__ volatile("pushfq\n\tpopq %0\n\tcli" : "=r"(rflags) : : "memory"); |
| 126 | return rflags; |
| 127 | #else |
| 128 | #error "ck_irq_save: no interrupt mask for this architecture" |
| 129 | #endif |
| 130 | } |
| 131 | |
| 132 | /* Put back a state returned by ck_irq_save. */ |
| 133 | static inline void ck_irq_restore(ck_irqflags_t f) |
| 134 | { |
| 135 | #if defined(CK_SPIN_TEST_NO_IRQ_MASK) |
| 136 | (void)f; |
| 137 | #elif __STDC_HOSTED__ |
| 138 | if (!f) { |
| 139 | sigset_t one; |
| 140 | sigemptyset(&one); |
| 141 | sigaddset(&one, CK_SPIN_HOST_IRQ_SIGNAL); |
| 142 | pthread_sigmask(SIG_UNBLOCK, &one, 0); |
| 143 | } |
| 144 | #elif defined(__aarch64__) |
| 145 | __asm__ volatile("msr daif, %0" : : "r"(f) : "memory"); |
| 146 | #elif defined(__x86_64__) |
| 147 | if (f & (1u << 9)) /* RFLAGS.IF */ |
| 148 | __asm__ volatile("sti" ::: "memory"); |
| 149 | #endif |
| 150 | } |
| 151 | |
| 152 | /* Unlocked. Single-threaded, before the lock is shared. */ |
| 153 | static inline void ck_spin_init(struct ck_spinlock *l) |
| 154 | { |
| 155 | __atomic_store_n(&l->ticket, 0u, __ATOMIC_RELAXED); |
| 156 | __atomic_store_n(&l->serving, 0u, __ATOMIC_RELAXED); |
| 157 | l->irq = 0; |
| 158 | } |
| 159 | |
| 160 | /* Take a ticket and wait for it. Interrupts are left as they are. */ |
| 161 | static inline void ck_spin_lock_nomask(struct ck_spinlock *l) |
| 162 | { |
| 163 | uint32_t me = __atomic_fetch_add(&l->ticket, 1u, __ATOMIC_RELAXED); |
| 164 | while (__atomic_load_n(&l->serving, __ATOMIC_ACQUIRE) != me) |
| 165 | ck_spin_relax(); |
| 166 | } |
| 167 | |
| 168 | /* 1 and the lock is held if nobody held or waited for it; 0 and nothing changed otherwise. */ |
| 169 | static inline int ck_spin_trylock_nomask(struct ck_spinlock *l) |
| 170 | { |
| 171 | uint32_t s = __atomic_load_n(&l->serving, __ATOMIC_ACQUIRE); /* pairs with the last holder's release */ |
| 172 | uint32_t t = s; |
| 173 | return __atomic_compare_exchange_n(&l->ticket, &t, s + 1u, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED); |
| 174 | } |
| 175 | |
| 176 | /* Hand the lock to the next ticket. Interrupts are left as they are. */ |
| 177 | static inline void ck_spin_unlock_nomask(struct ck_spinlock *l) |
| 178 | { |
| 179 | uint32_t next = __atomic_load_n(&l->serving, __ATOMIC_RELAXED) + 1u; |
| 180 | __atomic_store_n(&l->serving, next, __ATOMIC_RELEASE); |
| 181 | } |
| 182 | |
| 183 | /* Mask interrupts, then wait for this caller's turn. Returns the state to restore. */ |
| 184 | static inline ck_irqflags_t ck_spin_lock_irqsave(struct ck_spinlock *l) |
| 185 | { |
| 186 | #if defined(CK_SPIN_TEST_TICKET_BEFORE_MASK) |
| 187 | uint32_t me = __atomic_fetch_add(&l->ticket, 1u, __ATOMIC_RELAXED); |
| 188 | CK_SPIN_TEST_STEP(l); |
| 189 | ck_irqflags_t f = ck_irq_save(); |
| 190 | while (__atomic_load_n(&l->serving, __ATOMIC_ACQUIRE) != me) |
| 191 | ck_spin_relax(); |
| 192 | #else |
| 193 | ck_irqflags_t f = ck_irq_save(); |
| 194 | CK_SPIN_TEST_STEP(l); |
| 195 | ck_spin_lock_nomask(l); |
| 196 | #endif |
| 197 | return f; |
| 198 | } |
| 199 | |
| 200 | /* Hand the lock to the next ticket, then restore the interrupt state saved by the matching lock. */ |
| 201 | static inline void ck_spin_unlock_irqrestore(struct ck_spinlock *l, ck_irqflags_t f) |
| 202 | { |
| 203 | #if defined(CK_SPIN_TEST_RESTORE_BEFORE_RELEASE) |
| 204 | ck_irq_restore(f); |
| 205 | CK_SPIN_TEST_STEP(l); |
| 206 | ck_spin_unlock_nomask(l); |
| 207 | #else |
| 208 | ck_spin_unlock_nomask(l); |
| 209 | CK_SPIN_TEST_STEP(l); |
| 210 | ck_irq_restore(f); |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | /* The same pair, with the saved state kept in the lock (see the header comment). */ |
| 215 | static inline void ck_spin_lock(struct ck_spinlock *l) |
| 216 | { |
| 217 | ck_irqflags_t f = ck_spin_lock_irqsave(l); |
| 218 | l->irq = f; |
| 219 | } |
| 220 | |
| 221 | static inline void ck_spin_unlock(struct ck_spinlock *l) |
| 222 | { |
| 223 | ck_spin_unlock_irqrestore(l, l->irq); |
| 224 | } |
| 225 | |
| 226 | /* 1 while some caller holds or waits for the lock. A snapshot, for assertions and tests only. */ |
| 227 | static inline int ck_spin_busy(const struct ck_spinlock *l) |
| 228 | { |
| 229 | return __atomic_load_n(&l->ticket, __ATOMIC_RELAXED) != __atomic_load_n(&l->serving, __ATOMIC_RELAXED); |
| 230 | } |
| 231 | |
| 232 | #endif |