Build: do i need xadd instruction?

I am reading smphello.s code from allan cruse code

in the next part, he tries to configure a stack segment for each processor.

The fact is that he used xadd without using the lock prefix, but in the description of xadd, as in here . may be a lock prefix.

Is this a mistake or is everything all right? and why?

# setup an exclusive stack-area for this processor mov $0x1000, %ax # paragraphs in segment xadd %ax, newSS # 'atomic' xchg-and-add mov %ax, %ss # segment-address in SS xor %esp, %esp # top-of-stack into ESP 
+5
source share
2 answers

The race condition does not apply to "newSS". As the instruction explains,

xadd %ax, newSS means:

 temp = ax + newSS newSS = ax ax = temp ; the last 2 are actual xchg 

So, the race condition is on the ax register. But if we think carefully, we know that each thread has its own registry file or context. So there will be no problems. (register values ​​will not change when switching context from the last knowledge of the latter about them)

So there is no need to block for xadd in this example.

+3
source

After another thought, I came up with a different scenario for this case.

if the xadd microcode xadd is like this:

 temp = ax + newSS newSS = ax ax = temp ; the last 2 are actual xchg 

we have a problem in this scenario:

Suppose newSS is split between two threads.

Topic # 0 ( t0 with her ax equal to 5) loads and adds newSS with ax and puts it in temp register .

Suppose that at this point we have a context switch. Then t1 with it ax is 5 attempts to load newSS and add it to ax and put the result in temp register . and then the context switches to t0 ... Both stack segment registers point to the same address.

Obviously, there is a problem. If the microcode implementation is not like this:

 tmp register = ax xchg ax, newSS ax = ax + tmpRegister 

in any other way that the newSS variable is read more than once or read and written in different instructions, we need a lock.

+1
source

All Articles