get_fs( ) and set_fs( )

Previous thread: Netmark Patent, NETMARK KOBİ ile Sektöre Yeni by NETMARK PATENT on Thursday, October 25, 2007 - 4:11 am. (1 message)

Next thread: none
From: Wang Yu
Date: Thursday, October 25, 2007 - 2:54 am

Hi,all
I have seen the following codes, but do not know what's the meaning of them:
       mm_segment_t old_fs;
      old_fs = get_fs();
      set_fs (KERNEL_DS);
     ........
     set_fs(old_fs);
What is the usage of these codes and in what condition they will be used?
Thanks!

-- 
National Research Center for Intelligent Computing Systems
Institute of Computing Technology, Chinese Academy of Sciences
From: Thippeswamy, Aravind
Date: Thursday, October 25, 2007 - 6:25 am

[Empty message]
From: Saumendra Dash
Date: Thursday, October 25, 2007 - 5:45 am

> I have seen the following codes, but do not know what's the meaning of =
used?



 If you want to invoke a sys call from the kernel space, then you need =
to save the kernel data segment before the call and restore that after =
you are done.
The piece of code you mention does exactly that.
=20
Thanks,
Saumendra

From: Thippeswamy, Aravind
Date: Thursday, October 25, 2007 - 5:50 am

When you make a system call from user space, the first thing that is
checked is if the address of the parameter is well within the legal
virtual address space (i.e. 0 to 3 GB for the user space). If this is
not so, the call will fail. If you want to make the same system call
from the Kernel Space( Virtual Address 3 - 4 GB) however, this address
checking has to be avoided so that the call will not fail. Now, every
process has a tak_struct associated with it and this structure contains
the legal virtual address boundaries for that process( Virtual Address
space represented by mm_segment_t). The get_fs() macro will retrieve
this boundary and the set_fs() will set it with a value. So, when you
want to access a memory region which is beyond the User Space Virtual
Address limit( i.e. falling in the Kernel Space Virtual Address region),
you first of all store the current limit by doing=20

=20

            mm_segment_t old_fs;
            old_fs =3D get_fs();

=20

 Then set this limit to that of the Kernel (i.e. the whole of 4 GB) by
doing

           =20

set_fs (KERNEL_DS);

=20

 Do your memory accessing operations here (for ex: - read from a buffer
which is in the kernel space from a user context thru a system call)

            .......;

=20

Set the address limit back to the original limit that was stored in the
old_fs variable by doing.

set_fs(old_fs);


Hope this helped. Google for more answers and please let me know if you
find more details.

=20

=20

Regards,,

Aravind.

=20

"Dovie'andi se tovya sagain"

 -Mat Cauthon (WoT).

________________________________

From: kernelnewbies-bounce@nl.linux.org
[mailto:kernelnewbies-bounce@nl.linux.org] On Behalf Of Wang Yu
Sent: Thursday, October 25, 2007 3:24 PM
To: kernelnewbies
Subject: get_fs( ) and set_fs( )

=20

Hi,all
I have seen the following codes, but do not know what's the meaning of
them:
       mm_segment_t old_fs;
      old_fs =3D get_fs();
      set_fs (KERNEL_DS);
     ...
Previous thread: Netmark Patent, NETMARK KOBİ ile Sektöre Yeni by NETMARK PATENT on Thursday, October 25, 2007 - 4:11 am. (1 message)

Next thread: none