There is this section under include/asm-arm/spinlock.h
#if __LINUX_ARM_ARCH__ < 6
#error SMP not supported on pre-ARMv6 CPUs
#endif
Okay, the premise is: only ARM core version >= 6 can continue:
All spin lock primitives end up using the following basic type:
static inline void __raw_spin_lock(raw_spinlock_t *lock)
{
unsigned long tmp;
1 __asm__ __volatile__(
2"1: ldrex %0, [%1]n"
3" teq %0, #0n"
4" strexeq %0, %2, [%1]n"
5" teqeq %0, #0n"
6" bne 1b"
7 : "=&r" (tmp)
8 : "r" (&lock->lock), "r" (1)
9 : " cc");
smp_mb();
}
[Key Points of Instruction]:
The ldrex instruction is only available after core 6. It is paired with strex. You can ask the bus to monitor whether there are other CPUs or DMAs between ldrex and strex to access this address. If so, strex will The value in the first register is set to 1 (non-exclusive by this CPU) and the store action fails. If not, strex will set the value in the first register to 0 (exclusive access by this CPU) and the store action is made. success.
Code Trace Discussion:
Line 1: __volatile__ tells the compiler not to perform optimization actions on this assembly template, because we have a loop to read the memory. The optimization results may cause the compiler to use a register to cache its value, which will not be honest. Really read the memory... Haha, this is not the action we want!
Line 2: Read the lock into tmp and ask the bus monitor for this memory
Line 3: Test whether the lock is 0. If it is not 0, it means that the lock has been obtained by someone else, then Line 4 and 5 will not be executed, and then Line 6 will branch and perform the spin action. If it is 0, it means there is a chance to obtain the lock and continue to Line 4.5.
Line 4: Here comes the point! , check the results of the bus monitor. If it is exclusive access, tmp is set to 0 and 1 is stored in the lock. If it is non-exclusive access (other CPU has touched it), tmp is set to 1 and no action is taken to store the lock.
Line 5: Test tmp
Line 6: If tmp is 0, it means that the lock action just performed is exclusive and you can leave the loop. If tmp is 1, the spin action is performed.
Line 7: tmp uses register to operate, both input and output, let it be %0
Line 8: &lock->lock uses register to operate, let it be %1, value 1 uses register to operate, let it be %2
Line 9: This template will be changed to condition code and added to the clobber list to tell the compiler that this is the case
Well, I finally finished reading it. I really admire those coding kernel hackers....
Questions to think about: Before ARM v6, there was a SWP instruction that could lock bus and swap memory. It could also be used to complete exclusive access. But compared with ldrex and strex, what are the disadvantages of this pair of instructions?
ANS: Use the mouse to highlight the next few lines
Previous article:Software and hardware collaborative processing flow after an exception occurs in the ARM user layer
Next article:ARM Linux kernel layout in memory
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications