The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
spinlock.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * kernel locking rules:
5 *
6 * - syscalls run with interrupts enabled (int 0x80 is a trap gate) and the
7 * timer can switch processes anywhere in kernel code, so any data shared
8 * between processes needs a lock, not just data shared with IRQs.
9 * - IRQ handlers run with interrupts masked and must never sleep.
10 * - short critical sections, and anything an IRQ handler touches: a
11 * spinlock with spin_lock_irqsave / spin_unlock_irqrestore. examples:
12 * heap, pmm, process table, pipes, keyboard ring, console, the reference
13 * counts of open-file descriptions (kernel/vfs/file.c).
14 * - long sections in process context (disk and filesystem I/O, a whole
15 * console write): a mutex_t (kernel/sync/mutex.h), which sleeps instead
16 * of masking interrupts.
17 * - to wait for a condition guarded by a spinlock, test it with the lock
18 * held and sleep with wait_queue_block_locked(), which releases the lock
19 * only once the process is queued, so a wakeup cannot be lost. re-test
20 * in a loop after waking.
21 * - nothing can block before the scheduler is active (the first /bin/sh
22 * runs on the boot stack until it starts a child); see
23 * scheduler_is_active().
24 * - never sleep (mutex, wait queue) while holding a spinlock, except
25 * through wait_queue_block_locked() on that same lock.
26 * - the lock order: a lock on the left can be held while the lock on its
27 * right is taken, never the other way round. sched_lock is the
28 * innermost: code that holds it takes no other lock.
29 * a mutex -> any spinlock
30 * signal_lock (signal.c) -> the process table lock -> sched_lock
31 * the lock of a wait_queue_block_locked() caller -> sched_lock
32 * a futex bucket lock -> sched_lock
33 * the page lock of a user space (vma.h) -> pmm_lock
34 * the page lock of a user space -> the TLB flush lock (tlb.c); code
35 * that holds the flush lock takes no other lock
36 * heap_lock -> pmm_lock (heap growth)
37 * - a core that waits for a lock does the TLB flush that another core asks
38 * of it (tlb_poll), so a holder can wait for the other cores to flush.
39 * - use spin_lock / spin_unlock only where interrupts are masked already
40 * (an IRQ handler, or inside an irqsave lock). they do not change the
41 * interrupt state.
42 * - "interrupts are masked" is not a lock: it stops only this core. data
43 * that two cores can change needs a spinlock.
44 * - lock assertions (KASSERT) are compiled out under NDEBUG; they only fire
45 * in `make debug` builds.
46 */
47
48/**
49 * a ticket spinlock, as Linux had before its queued spinlocks. a core takes
50 * the next ticket with one atomic add and spins until the owner field
51 * comes to its ticket, so the cores get the lock in the order they asked
52 * for it and no core waits for ever.
53 *
54 * the lock does not stop the timer from switching to another process, so
55 * a holder must have interrupts masked on its core: use the irqsave
56 * variants, or spin_lock() when interrupts are off already. a process that
57 * holds a spinlock must never sleep (wait_queue_block_locked() is the one
58 * exception: it releases the lock).
59 */
60
61#include "common/stdint.h"
62
63/**
64 * a spinlock. zero-initialized means unlocked.
65 */
66typedef struct
67{
68 volatile uint32_t next; // the ticket that the next core gets
69 volatile uint32_t owner; // the ticket that holds the lock
70 volatile uint32_t holder; // the index + 1 of the core that holds it, 0 when free
72
73/**
74 * initialize a spinlock to the unlocked state.
75 * @param lock the lock to initialize.
76 */
77void spinlock_init(spinlock_t *lock);
78
79/**
80 * acquire a lock. it does not change the interrupt state, so interrupts
81 * must be masked already (as in Linux's spin_lock in an IRQ handler).
82 * pairs with spin_unlock().
83 * @param lock the lock to acquire.
84 */
85void spin_lock(spinlock_t *lock);
86
87/**
88 * release a lock that spin_lock() took. it does not change the interrupt
89 * state.
90 * @param lock the lock to release.
91 */
92void spin_unlock(spinlock_t *lock);
93
94/**
95 * mask interrupts on this core, then acquire a lock.
96 * safe to nest: the returned flags carry whether interrupts were enabled
97 * on entry. pairs with spin_unlock_irqrestore().
98 * @param lock the lock to acquire.
99 * @return the interrupt state to hand back to spin_unlock_irqrestore().
100 */
102
103/**
104 * release a lock and restore the interrupt state captured at acquire time.
105 * pairs with spin_lock_irqsave().
106 * @param lock the lock to release.
107 * @param flags the value returned by spin_lock_irqsave().
108 */
110
111/**
112 * check if the core that runs the call holds a lock. for assertions.
113 * @param lock the lock to check.
114 * @return true if this core holds it.
115 */
116bool spin_is_held(const spinlock_t *lock);
uint8_t flags
Definition idt.c:3
unsigned int uint32_t
Definition fb.h:17
uint32_t spin_lock_irqsave(spinlock_t *lock)
mask interrupts on this core, then acquire a lock.
Definition spinlock.c:67
void spin_unlock(spinlock_t *lock)
release a lock that spin_lock() took.
Definition spinlock.c:57
bool spin_is_held(const spinlock_t *lock)
check if the core that runs the call holds a lock.
Definition spinlock.c:80
void spin_lock(spinlock_t *lock)
acquire a lock.
Definition spinlock.c:31
void spin_unlock_irqrestore(spinlock_t *lock, uint32_t flags)
release a lock and restore the interrupt state captured at acquire time.
Definition spinlock.c:74
void spinlock_init(spinlock_t *lock)
initialize a spinlock to the unlocked state.
Definition spinlock.c:24
kernel locking rules:
Definition spinlock.h:67
volatile uint32_t owner
Definition spinlock.h:69
volatile uint32_t holder
Definition spinlock.h:70
volatile uint32_t next
Definition spinlock.h:68