Find the answer to your Linux question:
Results 1 to 6 of 6
(The following refers to GCC/Linux/32Bit) I'm currently taking an OS course. I'm asked to write a User-Level Thread Library. I'm given the following C method by the school, alongside with ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    5

    Thumbs up %gs:0x18


    (The following refers to GCC/Linux/32Bit)
    I'm currently taking an OS course.
    I'm asked to write a User-Level Thread Library.

    I'm given the following C method by the school,
    alongside with a demo on how to use it:
    //A translation required when using an address of a variable

    //Use this as a black box in your code.
    unsigned int translate_address(unsigned int addr)
    {
    unsigned int tmp;
    asm volatile("mov %%gs, %0\n"
    :"=g" (tmp));
    printf ("%%gs=%u\n", tmp);
    unsigned int ret;
    asm volatile("xor %%gs:0x18,%0\n"
    "rol $0x9,%0\n"
    : "=g" (ret)
    : "0" (addr));
    printf("ret=%u\n", ret);
    return ret;
    }

    I anyone can explain to me how this works, this would be useful.
    Most of all I'm interested in the cryptic %gs:0x18.
    What happens at the GS data segment? What's so special about the 24 bits (=0x1 bits offset?

    Attached is the demo file using the above method.

    Thanks, David.
    Attached Files Attached Files

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    11,158
    FWIW, this code won't work on 64bit systems. Nonetheless, it sets up the stack frame to allow calls to switchThreads() to vector to the correct address. You need to review Intel i386 assembly language to understand exactly that is going on in the translate_address() function. I know what the mov and xor instructions are, but I'm not remembering what rol does (rotate bits?) since I haven't touched Intel assembly code for well over 15 years. Also, you need to understand what the Linux assembly directives are, since they are also involved here.

    One final thing. It is fine to ask people on these forums to help you understand what is going on, but it isn't ok (IMO) to have them do your work for you. I imagine that your class materials cover this stuff, or have references for you to follow in order to learn what is going on here. We can help you clarify what you think you know, but we won't be happy "spoon feeding" you. Ok?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Apr 2009
    Posts
    5
    Dear Rubberman,

    Thanks for taking the time replying.
    I actually know what the code does - it's pretty obvious from the demo.
    What I'm interested is the inner mechanism
    If you can elaborate more on that.
    I even know some Assembly - and I can grasp what every line of code does.
    What I don't know, is why the code is so.
    Why XOR addresses? What's so special about the selected ROL (and it does shift bits, but does not throw bit to the bits-bin, rather inserts them from the end)?

    And one last thing:
    I actually don't have material covering this - this is why the documentation says "use this as a black box". We're working over C/C++, and I guess the course staff chose not to add Assembly grasping problems to the main conceptual problem of the Ex - User-Level Thread Library.


    baum

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    11,158
    I appreciate your conundrum. The problem with black boxes is the amount of "faith" that is required. If there is a problem, who's is it? Without understanding exactly what is going on, all bets are off.

    I would suggest that you go to Laptop, Notebook, Desktop, Server and Embedded Processor Technology - Intel and dig up their docs on the pentium assembly language. I think, though this may not be correct, that the rol command rotates the bits, but doesn't lose them - they should (if my memory is correct), wrap around. Don't take me at my word for this. As I said, it has been 15 years since I last did any Intel assembly programming, and with the advent of the Pentium family, a lot has changed. Intel has a Developer's Network as well that provides a lot of tools and documentation for this sort of thing. Do check them out. I have always found that their data sheets and documentation to be exceptionally complete.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Apr 2009
    Posts
    5
    That is a great idea!
    I'll post a thread over there - didn't think of it.

    Thanks

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    11,158
    Good luck, and let us know how you fair.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •