Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

help in understanding this code snippet

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?

like image 903
kernel_os Avatar asked Sep 08 '11 03:09

kernel_os


People also ask

How do you explain a code snippet?

Code snippets are small blocks of reusable code that can be inserted in a code file using a right-click menu (context menu) command or a combination of hotkeys. They typically contain commonly used code blocks such as try-finally or if-else blocks, but they can be used to insert entire classes or methods.

How do you find the code on a snippet?

With a code file open in the editor, choose Snippets > Insert Snippet from the right-click menu, then My Code Snippets. You should see a snippet named Square Root. Double-click it. The snippet code is inserted in the code file.

What is sample code snippet?

A code snippet is real code; it is not pseudocode. Code sample: A code sample is intended to demonstrate programming tasks or scenarios, or to demonstrate a particular program architecture that is not easily demonstrated in a code snippet (for example, how to create, populate, and manage a list).

How do you use a snippet?

You can also insert a snippet when logging an activity or leaving a comment on a record using the HubSpot mobile app for Android. There are two ways to add a snippet: Type the # symbol into the text editor. Start typing the snippet shortcut, then select the snippet from the dropdown menu.


1 Answers

ret is the instruction to return from a procedure. So basically it pops the return address from the stack into the EIP register.

the l prefix 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 w suffix is here because at this step we are running in real mode, and operands are 16 bits wide.

The exact code is:

    pushw   %ds
    pushw   $6f
    lretw
6:

The 6: is very important here. So what this does is: push the value of ds into the stack, push the adress of the 6 label into the stack, and then trigger this lretw instruction. So basically, it will load the address of label 6 into the instruction pointer register, and load the cs register with the value of the ds register. So this is just a trick to continue the execution at label 6 with a change of the cs register 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.

like image 70
Thomas Petazzoni Avatar answered Oct 23 '22 03:10

Thomas Petazzoni