- mm_segment_t oldfs;
- struct kvec iov = {
- .iov_base = buf,
- .iov_len = size,
- };
- struct msghdr msg = {
- .msg_iovlen = 1,
- .msg_iov = (struct iovec *)&iov,
- .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
- };
- int rv;
- oldfs = get_fs();
- set_fs(KERNEL_DS);
- rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
- set_fs(oldfs);
再看看内核中的定义:
- #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFFUL)
- #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
- #define get_ds() (KERNEL_DS)
- #define get_fs() (current_thread_info()->addr_limit)
- #define set_fs(x) (current_thread_info()->addr_limit = (x))
其作用是用来修改当前进程的内存访问权限。
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)。
set KERNEL_DS when user space address is used