This is code snipper from header.S file in kernel code. I could not understand what the lretw instruction does. I’ve checked out so many online sources for the instruction.
# We will have entered with %cs = %ds+0x20, normalize %cs so
# it is on par with the other segments.
pushw %ds
pushw $6f
lretw
Can any one help me in understanding this instruction?
retis the instruction to return from a procedure. So basically it pops the return address from the stack into the EIP register.the
lprefix is here to tell that it is a far return from procedure. In this case, the instruction first pops a value from the stack into the EIP register and then pops a second value into the CS register.the
wsuffix is here because at this step we are running in real mode, and operands are 16 bits wide.The exact code is:
The
6:is very important here. So what this does is: push the value of ds into the stack, push the adress of the6label into the stack, and then trigger thislretwinstruction. So basically, it will load the address of label6into the instruction pointer register, and load thecsregister with the value of thedsregister. So this is just a trick to continue the execution at label6with a change of thecsregister value.You should download http://www.intel.com/design/intarch/manuals/243191.htm which gives precise details for all instructions, including a pseudo-code that details what each instruction is doing.