Hi all: I print the cs ds and ss register in the user space, and it is same as the __USER_CS and __USER_DS which defined in kernel as 73 and 7b. In the kernel __KERNEL_CS and __KERNEL_DS defined as 60 and 68, but when I print this two value in my kernel module, I get 60 and 7b. Why ? It should be 60 and 68, shouldn't it?
On Wed, Oct 24, 2012 at 7:04 PM, Fan Yang <lljyangfan@gmail.com> wrote:
Hi all: I print the cs ds and ss register in the user space, and it is same as the __USER_CS and __USER_DS which defined in kernel as 73 and 7b. In the kernel __KERNEL_CS and __KERNEL_DS defined as 60 and 68, but when I print this two value in my kernel module, I get 60 and 7b. Why ? It should be 60 and 68, shouldn't it?
you're not manually switch the data segment, aren't you? -- regards, Mulyadi Santosa Freelance Linux trainer and consultant blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
Can you post out your codes ? From: Fan Yang Sent: Wednesday, October 24, 2012 8:04 PM To: kernelnewbies@kernelnewbies.org Subject: linux segment Hi all: I print the cs ds and ss register in the user space, and it is same as the __USER_CS and __USER_DS which defined in kernel as 73 and 7b. In the kernel __KERNEL_CS and __KERNEL_DS defined as 60 and 68, but when I print this two value in my kernel module, I get 60 and 7b. Why ? It should be 60 and 68, shouldn't it? -------------------------------------------------------------------------------- _______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
2012/10/27 Jun Hu <duanshuidao@hotmail.com>
Can you post out your codes ?
*From:* Fan Yang <lljyangfan@gmail.com> *Sent:* Wednesday, October 24, 2012 8:04 PM *To:* kernelnewbies@kernelnewbies.org *Subject:* linux segment
Hi all: I print the cs ds and ss register in the user space, and it is same as the __USER_CS and __USER_DS which defined in kernel as 73 and 7b. In the kernel __KERNEL_CS and __KERNEL_DS defined as 60 and 68, but when I print this two value in my kernel module, I get 60 and 7b. Why ? It should be 60 and 68, shouldn't it?
------------------------------ _______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Hi Jun Hu There is my code which run at the user space:
1 #include<stdio.h> 2 main() 3 { 4 unsigned long cs, ds, ss, es, fs, gs; 5 asm volatile("movl %%CS,%0\n\t":"=r"(cs)); 6 asm volatile("movl %%DS,%0\n\t":"=r"(ds)); 7 asm volatile("movl %%SS,%0\n\t":"=r"(ss)); 8 asm volatile("movl %%ES,%0\n\t":"=r"(es)); 9 asm volatile("movl %%FS,%0\n\t":"=r"(fs)); 10 asm volatile("movl %%GS,%0\n\t":"=r"(gs)); 11 printf ("**********************************\n"); 12 printf ("cs %lx\t%ld\n", cs, cs); 13 printf ("ds %lx\t%ld\n", ds, ds); 14 printf ("ss %lx\t%ld\n", ss, ss); 15 printf ("es %lx\t%ld\n", es, es); 16 printf ("fs %lx\t%ld\n", fs, fs); 17 printf ("gs %lx\t%ld\n", gs, gs); 18 printf ("**********************************\n"); 19 } and the result of the progress in my machine is ********************************** cs 73 115 ds 7b 123 ss 7b 123 es 7b 123 fs 0 0 gs 33 51 ********************************** so, you can see the cs and ds register is 73 and 7b which are same as the kernel defined. And the code of the kernel module is 1 #include<linux/init.h> 2 #include<linux/kernel.h> 3 #include<linux/module.h> 4 5 static void __init print_init (void) 6 { 7 unsigned long cs, ds, ss, es, fs, gs,currenttime; 8 asm volatile("movl %%CS,%0\n\t":"=r"(cs)); 9 asm volatile("movl %%DS,%0\n\t":"=r"(ds)); 10 asm volatile("movl %%SS,%0\n\t":"=r"(ss)); 11 asm volatile("movl %%ES,%0\n\t":"=r"(es)); 12 asm volatile("movl %%FS,%0\n\t":"=r"(fs)); 13 asm volatile("movl %%GS,%0\n\t":"=r"(gs)); 14 printk ("**********************************\n"); 15 printk ("cs %lx\t%ld\n", cs, cs); 16 printk ("ds %lx\t%ld\n", ds, ds); 17 printk ("ss %lx\t%ld\n", ss, ss); 18 printk ("es %lx\t%ld\n", es, es); 19 printk ("fs %lx\t%ld\n", fs, fs); 20 printk ("gs %lx\t%ld\n", gs, gs); 21 printk ("**********************************\n"); 22 23 } 24 25 static void __exit print_exit (void) 26 { 27 unsigned long cs, ds, ss; 28 asm volatile("movl %%cs,%0\n\t":"=r"(cs)); 29 asm volatile("movl %%ds,%0\n\t":"=r"(ds)); 30 asm volatile("movl %%ss,%0\n\t":"=r"(ss)); 31 32 printk ("**********************************\n"); 33 printk ("cs %lx\t%ld\n", cs, cs); 34 printk ("ds %lx\t%ld\n", ds, ds); 35 printk ("ss %lx\t%ld\n", ss, ss); 36 printk ("**********************************\n"); 37 printk ("*****************bye***************\n"); 38 } 39 40 module_init (print_init); 41 module_exit (print_exit); 42 MODULE_LICENSE ("GPL"); the result of the running this module is [root@shell--box kernel_mod]# dmesg -c ********************************** cs 60 96 ds 7b 123 ss 68 104 es 7b 123 fs d8 216 gs e0 224 ********************************** The cs and ds in the kernel space is 60 and 7b. But the kernel define the KERNEL_CS as 60 and the KERNEL_DS as 7b. Where am I wrong? Thanks Fan
2012/10/28 Fan Yang <lljyangfan@gmail.com>
2012/10/27 Jun Hu <duanshuidao@hotmail.com>
Can you post out your codes ?
*From:* Fan Yang <lljyangfan@gmail.com> *Sent:* Wednesday, October 24, 2012 8:04 PM *To:* kernelnewbies@kernelnewbies.org *Subject:* linux segment
Hi all: I print the cs ds and ss register in the user space, and it is same as the __USER_CS and __USER_DS which defined in kernel as 73 and 7b. In the kernel __KERNEL_CS and __KERNEL_DS defined as 60 and 68, but when I print this two value in my kernel module, I get 60 and 7b. Why ? It should be 60 and 68, shouldn't it?
------------------------------ _______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Hi Jun Hu There is my code which run at the user space:
1 #include<stdio.h> 2 main() 3 { 4 unsigned long cs, ds, ss, es, fs, gs; 5 asm volatile("movl %%CS,%0\n\t":"=r"(cs)); 6 asm volatile("movl %%DS,%0\n\t":"=r"(ds)); 7 asm volatile("movl %%SS,%0\n\t":"=r"(ss)); 8 asm volatile("movl %%ES,%0\n\t":"=r"(es));
9 asm volatile("movl %%FS,%0\n\t":"=r"(fs)); 10 asm volatile("movl %%GS,%0\n\t":"=r"(gs)); 11 printf ("**********************************\n"); 12 printf ("cs %lx\t%ld\n", cs, cs); 13 printf ("ds %lx\t%ld\n", ds, ds); 14 printf ("ss %lx\t%ld\n", ss, ss); 15 printf ("es %lx\t%ld\n", es, es); 16 printf ("fs %lx\t%ld\n", fs, fs); 17 printf ("gs %lx\t%ld\n", gs, gs); 18 printf ("**********************************\n"); 19 }
and the result of the progress in my machine is
********************************** cs 73 115 ds 7b 123 ss 7b 123 es 7b 123 fs 0 0 gs 33 51 **********************************
so, you can see the cs and ds register is 73 and 7b which are same as the kernel defined. And the code of the kernel module is
1 #include<linux/init.h>
2 #include<linux/kernel.h> 3 #include<linux/module.h> 4 5 static void __init print_init (void) 6 { 7 unsigned long cs, ds, ss, es, fs, gs,currenttime; 8 asm volatile("movl %%CS,%0\n\t":"=r"(cs)); 9 asm volatile("movl %%DS,%0\n\t":"=r"(ds)); 10 asm volatile("movl %%SS,%0\n\t":"=r"(ss)); 11 asm volatile("movl %%ES,%0\n\t":"=r"(es)); 12 asm volatile("movl %%FS,%0\n\t":"=r"(fs)); 13 asm volatile("movl %%GS,%0\n\t":"=r"(gs)); 14 printk ("**********************************\n"); 15 printk ("cs %lx\t%ld\n", cs, cs); 16 printk ("ds %lx\t%ld\n", ds, ds); 17 printk ("ss %lx\t%ld\n", ss, ss); 18 printk ("es %lx\t%ld\n", es, es); 19 printk ("fs %lx\t%ld\n", fs, fs); 20 printk ("gs %lx\t%ld\n", gs, gs); 21 printk ("**********************************\n"); 22 23 } 24 25 static void __exit print_exit (void) 26 { 27 unsigned long cs, ds, ss; 28 asm volatile("movl %%cs,%0\n\t":"=r"(cs)); 29 asm volatile("movl %%ds,%0\n\t":"=r"(ds)); 30 asm volatile("movl %%ss,%0\n\t":"=r"(ss)); 31 32 printk ("**********************************\n"); 33 printk ("cs %lx\t%ld\n", cs, cs); 34 printk ("ds %lx\t%ld\n", ds, ds); 35 printk ("ss %lx\t%ld\n", ss, ss); 36 printk ("**********************************\n"); 37 printk ("*****************bye***************\n"); 38 } 39 40 module_init (print_init); 41 module_exit (print_exit);
42 MODULE_LICENSE ("GPL");
the result of the running this module is
[root@shell--box kernel_mod]# dmesg -c ********************************** cs 60 96 ds 7b 123 ss 68 104 es 7b 123 fs d8 216 gs e0 224 **********************************
The cs and ds in the kernel space is 60 and 7b. But the kernel define the KERNEL_CS as 60 and the KERNEL_DS as 7b. Where am I wrong?
Thanks Fan
sorry, the kernel define the KERNEL_DS as 68, but I get 7b in my machine.
Hi Fan... On Sun, Oct 28, 2012 at 9:02 PM, Fan Yang <lljyangfan@gmail.com> wrote:
[root@shell--box kernel_mod]# dmesg -c ********************************** cs 60 96 ds 7b 123 ss 68 104 es 7b 123 fs d8 216 gs e0 224 **********************************
The cs and ds in the kernel space is 60 and 7b. But the kernel define the KERNEL_CS as 60 and the KERNEL_DS as 7b. Where am I wrong?
you print CS and DS twice, once during init and once during exit of your kernel module. So, which one do you want to confirm? All in all, I have a guess that you see such number (DS belongs to user space in kernel module) because IIRC kernel module loading is done using syscall and with the help of modprobe helper. Thus, it is important to access user space during that stage, hence DS still using user space data segment. -- regards, Mulyadi Santosa Freelance Linux trainer and consultant blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
2012/10/29 Mulyadi Santosa <mulyadi.santosa@gmail.com>
Hi Fan...
On Sun, Oct 28, 2012 at 9:02 PM, Fan Yang <lljyangfan@gmail.com> wrote:
[root@shell--box kernel_mod]# dmesg -c ********************************** cs 60 96 ds 7b 123 ss 68 104 es 7b 123 fs d8 216 gs e0 224 **********************************
The cs and ds in the kernel space is 60 and 7b. But the kernel define the KERNEL_CS as 60 and the KERNEL_DS as 7b. Where am I wrong?
you print CS and DS twice, once during init and once during exit of your kernel module. So, which one do you want to confirm?
All in all, I have a guess that you see such number (DS belongs to user space in kernel module) because IIRC kernel module loading is done using syscall and with the help of modprobe helper.
Thus, it is important to access user space during that stage, hence DS still using user space data segment.
-- regards,
Mulyadi Santosa Freelance Linux trainer and consultant
blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
Hi Mulyadi Santosa I get the same result during the kernel module init and exit. Then I try to add a syscall to print these registers, and nothing changed. It is strange.
On Tue, Oct 30, 2012 at 7:44 AM, Fan Yang <lljyangfan@gmail.com> wrote:
Hi Mulyadi Santosa I get the same result during the kernel module init and exit. Then I try to add a syscall to print these registers, and nothing changed. It is strange.
I think you need to observe deeper, something change this. BTW, are you running this inside a virtualization? and which kernel version do you use? -- regards, Mulyadi Santosa Freelance Linux trainer and consultant blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
On Tue, 30 Oct 2012, Fan Yang wrote:
2012/10/29 Mulyadi Santosa <mulyadi.santosa@gmail.com>
Hi Fan...
On Sun, Oct 28, 2012 at 9:02 PM, Fan Yang <lljyangfan@gmail.com> wrote:
[root@shell--box kernel_mod]# dmesg -c ********************************** cs 60 96 ds 7b 123 ss 68 104 es 7b 123 fs d8 216 gs e0 224 **********************************
The cs and ds in the kernel space is 60 and 7b. But the kernel define the KERNEL_CS as 60 and the KERNEL_DS as 7b. Where am I wrong?
you print CS and DS twice, once during init and once during exit of your kernel module. So, which one do you want to confirm?
All in all, I have a guess that you see such number (DS belongs to user space in kernel module) because IIRC kernel module loading is done using syscall and with the help of modprobe helper.
Thus, it is important to access user space during that stage, hence DS still using user space data segment.
-- regards,
Mulyadi Santosa Freelance Linux trainer and consultant
blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
Hi Mulyadi Santosa I get the same result during the kernel module init and exit. Then I try to add a syscall to print these registers, and nothing changed. It is strange.
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
(Weird, this is the third time, I have to send this. If anybody gets this message multiple times, I apologise but my mail is not in the archives.) If Mulyadi is right and we need DS to be USER_DS to access user space (I really don't know, sorry, but maybe there is something in your <uaccess.h>?) then your attempt to try with a syscall couldn't yield other values because one trait of syscalls is that they can access user space. This means you would get DS = USER_DS precisely _because_ you are in a syscall. Module init and exit are, too, just some stack frames above one and thus fall into this category as well. But shouldn't it be possible to register a timer and then print the segment registers? Timers are fired in softirq context and, hence, have no connection to user space. Regards, Tobi
On 10/24/2012 08:04 PM, Fan Yang wrote:
Hi all: I print the cs ds and ss register in the user space, and it is same as the __USER_CS and __USER_DS which defined in kernel as 73 and 7b. In the kernel __KERNEL_CS and __KERNEL_DS defined as 60 and 68, but when I print this two value in my kernel module, I get 60 and 7b. Why ? It should be 60 and 68, shouldn't it?
Hi Fan, I just talked to you and very impressed. By checking source code, I found what you mentioned is normal. You can check arch/x86/kernel/entry_32.S, and go to the page_fault definition, in error_code, you can see the CS and DS assignment. error_code: /* the function address is in %gs's slot on the stack */ pushl_cfi %fs /*CFI_REL_OFFSET fs, 0*/ pushl_cfi %es /*CFI_REL_OFFSET es, 0*/ pushl_cfi %ds /*CFI_REL_OFFSET ds, 0*/ pushl_cfi %eax CFI_REL_OFFSET eax, 0 pushl_cfi %ebp CFI_REL_OFFSET ebp, 0 pushl_cfi %edi CFI_REL_OFFSET edi, 0 pushl_cfi %esi CFI_REL_OFFSET esi, 0 pushl_cfi %edx CFI_REL_OFFSET edx, 0 pushl_cfi %ecx CFI_REL_OFFSET ecx, 0 pushl_cfi %ebx CFI_REL_OFFSET ebx, 0 cld movl $(__KERNEL_PERCPU), %ecx movl %ecx, %fs UNWIND_ESPFIX_STACK GS_TO_REG %ecx movl PT_GS(%esp), %edi # get the function address movl PT_ORIG_EAX(%esp), %edx # get the error code movl $-1, PT_ORIG_EAX(%esp) # no syscall to restart REG_TO_PTGS %ecx SET_KERNEL_GS %ecx Below is the assignment. This is changed in 2.6, the reason is that in kernel the CPL is 0 and it's safe to operate __USER_DS with DPL is 3. Here if use __KERNEL_DS, even though they have the same content, it need change back when return to user space. So in your original post, you mentioned the cs:ds is 60, 7b, it's normal and correct value. Hope this can help. movl $(__USER_DS), %ecx movl %ecx, %ds movl %ecx, %es TRACE_IRQS_OFF movl %esp,%eax # pt_regs pointer call *%edi
Hi Baoquan: Thank you for your answer. That is to say the designer use __USER_DS here deliberately to improve the efficiency jump from kernel space to user space? BTW,how can you find this email? I write this email On 10/24/2012. 2013/6/20 Baoquan He <baoquan.he@gmail.com>
On 10/24/2012 08:04 PM, Fan Yang wrote:
Hi all: I print the cs ds and ss register in the user space, and it is same as the __USER_CS and __USER_DS which defined in kernel as 73 and 7b. In the kernel __KERNEL_CS and __KERNEL_DS defined as 60 and 68, but when I print this two value in my kernel module, I get 60 and 7b. Why ? It should be 60 and 68, shouldn't it?
Hi Fan,
I just talked to you and very impressed. By checking source code, I found what you mentioned is normal.
You can check arch/x86/kernel/entry_32.S, and go to the page_fault definition, in error_code, you can see the CS and DS assignment.
error_code: /* the function address is in %gs's slot on the stack */ pushl_cfi %fs /*CFI_REL_OFFSET fs, 0*/ pushl_cfi %es /*CFI_REL_OFFSET es, 0*/ pushl_cfi %ds /*CFI_REL_OFFSET ds, 0*/ pushl_cfi %eax CFI_REL_OFFSET eax, 0 pushl_cfi %ebp CFI_REL_OFFSET ebp, 0 pushl_cfi %edi CFI_REL_OFFSET edi, 0 pushl_cfi %esi CFI_REL_OFFSET esi, 0 pushl_cfi %edx CFI_REL_OFFSET edx, 0 pushl_cfi %ecx CFI_REL_OFFSET ecx, 0 pushl_cfi %ebx CFI_REL_OFFSET ebx, 0 cld movl $(__KERNEL_PERCPU), %ecx movl %ecx, %fs UNWIND_ESPFIX_STACK GS_TO_REG %ecx movl PT_GS(%esp), %edi # get the function address movl PT_ORIG_EAX(%esp), %edx # get the error code movl $-1, PT_ORIG_EAX(%esp) # no syscall to restart REG_TO_PTGS %ecx SET_KERNEL_GS %ecx
Below is the assignment. This is changed in 2.6, the reason is that in kernel the CPL is 0 and it's safe to operate __USER_DS with DPL is 3. Here if use __KERNEL_DS, even though they have the same content, it need change back when return to user space.
So in your original post, you mentioned the cs:ds is 60, 7b, it's normal and correct value. Hope this can help.
movl $(__USER_DS), %ecx movl %ecx, %ds movl %ecx, %es TRACE_IRQS_OFF movl %esp,%eax # pt_regs pointer call *%edi
Since you asked this question by phone, we search your email address and found your questions here. On 06/20/2013 06:01 PM, Fan Yang wrote:
Hi Baoquan: Thank you for your answer. That is to say the designer use __USER_DS here deliberately to improve the efficiency jump from kernel space to user space? BTW,how can you find this email? I write this email On 10/24/2012.
participants (5)
-
Baoquan He -
Fan Yang -
Jun Hu -
Mulyadi Santosa -
Tobias Boege