GPF when trying to access syscalls

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
lpoulain
Member
Member
Posts: 38
Joined: Mon Dec 21, 2015 7:09 pm

GPF when trying to access syscalls

Post by lpoulain »

All,

After having implemented multitasking, I added the switch to the user mode (I confirmed that I cannot access pages only accessible to kernel mode)

My challenge is now to access system calls from the user mode. No matter what way I try I get a General Protection Fault.

I tried to setup interrupt 0x80 using the same mechanism I use to handle Page Faults or IRQs (and which works fine), but I get a GPF when I try to raise int 0x80.
I tried to use SYSENTER, but just trying to call wrmsr leads to a GPF (note: I'm in 32-bit):

Code: Select all

asm __volatile__ ("wrmsr" : : "a"(0x9A), "d"(0), "c"(0x174));
Thanks
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: GPF when trying to access syscalls

Post by gerryg400 »

Hi, make sure to set the DPL of the IDT entry for your sw int to 3 so that it is accessible from user land.

Also, I think you should be writing your ring 0 code selector to MSR 0x174. A code selector of 0x9a has a privilege level of 2. HINT: Most people have a ring 0 code selector of 0x8. IIRC, you also need your ring 0 data selector to be the next entry in the GDT.
If a trainstation is where trains stop, what is a workstation ?
lpoulain
Member
Member
Posts: 38
Joined: Mon Dec 21, 2015 7:09 pm

Re: GPF when trying to access syscalls

Post by lpoulain »

Thanks a lot for the tips.

Regarding the interruptions, indeed I did not set ring 3 in the DPL. This is fixed, but I still get a GPF. Interestingly, I get a GPF even when I stay in kernel mode, even though keyboard/mouse interrupt handling works fine. So there's something I'm doing wrong with my interruptions.

I tried changing the MSR parameter 0x174 code selector to 0x8 but still get the GPF as well. I set my GDT entries as:

- null segment
- kernel code segment
- kernel data segment
- user mode code segment
- user mode data segment
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: GPF when trying to access syscalls

Post by tsdnz »

lpoulain wrote:Thanks a lot for the tips.

Regarding the interruptions, indeed I did not set ring 3 in the DPL. This is fixed, but I still get a GPF. Interestingly, I get a GPF even when I stay in kernel mode, even though keyboard/mouse interrupt handling works fine. So there's something I'm doing wrong with my interruptions.

I tried changing the MSR parameter 0x174 code selector to 0x8 but still get the GPF as well. I set my GDT entries as:

- null segment
- kernel code segment
- kernel data segment
- user mode code segment
- user mode data segment
I am using 64 bit mode, my GDT looks like this, the User CS and SS are in a different order. See 24594_APM_v3.pdf, lookup sysret.

Code: Select all

Selector     Group               Location	Value
0               Null                 0x5000	0x0000000000000000
1               Kernel Code      0x5008	0x0000920000000000
2               Kernel Data       0x5010	0x0020980000000000
3               User Data          0x5018	0x0020F80000000000
4               User Code         0x5020	0x0000F20000000000
5               TSS[0]              0x5028	
260            TSS[255]           0x6028	
                 END                 0x6038	
lpoulain
Member
Member
Posts: 38
Joined: Mon Dec 21, 2015 7:09 pm

Re: GPF when trying to access syscalls

Post by lpoulain »

You seem to be using an AMD CPU. For the Intel processor and SYSENTER, it looks like ring 0 code / ring 0 data / ring 3 code / ring 3 data is the right order (http://wiki.osdev.org/Sysenter)
lpoulain
Member
Member
Posts: 38
Joined: Mon Dec 21, 2015 7:09 pm

Re: GPF when trying to access syscalls

Post by lpoulain »

Also, I finally found what I did wrong in my interruption code (I forgot a step to catch interrupt 0x80). It would still be nice to be able to use SYSENTER / SYSEXIT though, but my next is anyway to create a wrapper that hides how the system call is made.
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: GPF when trying to access syscalls

Post by tsdnz »

lpoulain wrote:You seem to be using an AMD CPU. For the Intel processor and SYSENTER, it looks like ring 0 code / ring 0 data / ring 3 code / ring 3 data is the right order (http://wiki.osdev.org/Sysenter)
Oops, sorry about that. I am using SYSCALL and just read yours as SYSCALL not as you typed it.

LOL
Post Reply