Page 1 of 1

Task Switching - Linux 0.01

Posted: Sun Jan 13, 2008 3:33 am
by thepowersgang
I have been trying to get task switching implemented in my OS for quite a while now, despite the immense amount of tutorials I have read.

To try to solve my problem I started to look through the Linux 0.01 source code to see how it was done. I came across a macro called switch_to() that did the actual task switching and noticed that it seemed to copy an undefined value to %dx and then jump to another undefined value.

Code: Select all

#define switch_to(n) {\
struct {long a,b;} __tmp; \
__asm__("cmpl %%ecx,_current\n\t" \
	"je 1f\n\t" \
	"xchgl %%ecx,_current\n\t" \
	"movw %%dx,%1\n\t" \
	"ljmp %0\n\t" \
	"cmpl %%ecx,%2\n\t" \
	"jne 1f\n\t" \
	"clts\n" \
	"1:" \
	::"m" (*&__tmp.a),"m" (*&__tmp.b), \
	"m" (last_task_used_math),"d" _TSS(n),"c" ((long) task[n])); \
}
The macro defines __tmp and then proceeds to use the values undefined, is there some other place in the code that these magically get a useful value inserted into them?

Posted: Sun Jan 13, 2008 3:58 am
by cyr1x
No,
in the Gnu(AT&T) syntax the format is like this

Code: Select all

mnemonic	source, destination
that means that the value from dx is copied into the var.

Posted: Sun Jan 13, 2008 4:10 am
by thepowersgang
Actually I noticed that part about 5 mins later, but I still don't understand why it would copy %dx to %1 (__tmp.b) and then long jump to %0 (__tmp.a)

Posted: Sun Jan 13, 2008 4:24 am
by cyr1x
I'm not sure on this, but I think it is in the reverse order(e.g %0 = task, %1 = TSS, ...)

Posted: Sun Jan 13, 2008 4:29 am
by thepowersgang
Then why not use %ecx

the 'm' code (i think) means that the data is placed in a temporary memory address instead of a register. the 'd' and 'c' before the tss and task mean to put the value passed into %edx and %ecx respectively.