User:Thepowersgang/ARM Notes

From OSDev Wiki
Jump to navigation Jump to search

Just some notes on doing OSDev on an ARM platform

Assembly Snippets

Spinlocks

Pre ARMv6

int v = 1;
while(v) {
    __asm__ __volatile__("swp %0, %1, [%2]" : "=r" (v) : "r" (1), "r" (&lock));
}

Post ARMv6 (preferred version)

int tmp = 0; 
__asm__ __volatile__ (
    "1:  ldrex   %0, [%1]" // Exclusive load (obtains a HW lock)
    "    teq     %0, #0"   // Check if the lock is avaliable
    "    strexeq %0, %2, [%1]" // Mark the lock if it's avaliable
    "    teqeq   %0, #0"   // if the lock was avaliable, but we didn't obtain it...
    "    bne     1b"       // Try again
    : "=&r" (tmp)
    : "r" (&lock), "r" (1)
    : "cc" // Condition codes clobbered
    );