Skip to content

p37: Introduce atomic spinlocks for multiprocessor concurrency#358

Open
2023cs50578 wants to merge 5 commits intocodenet:p31-umallocfrom
2023cs50578:p37-spinlocks
Open

p37: Introduce atomic spinlocks for multiprocessor concurrency#358
2023cs50578 wants to merge 5 commits intocodenet:p31-umallocfrom
2023cs50578:p37-spinlocks

Conversation

@2023cs50578
Copy link
Copy Markdown

Objective

This branch initiates the transition from a uniprocessor architecture to Symmetric Multiprocessing (SMP). It introduces the core mutual exclusion primitives (spinlocks) required to protect shared kernel data structures from race conditions once multiple CPU cores are booted.

Core Changes

  • x86.h: Ensured the atomic hardware instruction xchg (exchange) is available for read-modify-write operations, leveraging the CPU's hardware-level locking.
  • spinlock.h: Defined the struct spinlock to track lock state (locked) and diagnostic data (holding cpu).
  • spinlock.c: Implemented the core lock lifecycle functions:
    • initlock: Initializes lock state.
    • acquire: Uses the atomic xchg instruction in a tight loop to spin until the lock is available. Includes __sync_synchronize() barriers to prevent compiler/hardware instruction reordering and wraps the logic in pushcli() to prevent deadlocks from interrupt handlers.
    • release: Uses an atomic movl to clear the lock state and issues memory barriers to ensure critical section stores are visible before the lock is released.
    • holding: Diagnostic utility to verify ownership.
  • defs.h: Added the respective prototypes for the spinlock API.

Next Steps

With the atomic primitives in place, subsequent branches can begin retrofitting global kernel structures (e.g., the physical memory allocator kmem, the process table ptable) with these locks in preparation for booting the Application Processors (APs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants