Page 1 of 3

Issues with system call

Posted: Fri Nov 29, 2013 6:03 pm
by riparoony
I've recently been trying to follow JamesM's tutorial to implement system calls in my operating system, with minimal luck. Using the following code to perform the jump to usemode, I get a general protection fault.

Code: Select all

            asm volatile("  \ 
     cli; \ 
     mov $0x23, %ax; \ 
     mov %ax, %ds; \ 
     mov %ax, %es; \ 
     mov %ax, %fs; \ 
     mov %ax, %gs; \ 
                   \ 
     mov %esp, %eax; \ 
     pushl $0x23; \ 
     pushl %eax; \ 
     pushf; \
     pop %eax; \
     orl $0x0202, %eax; \
     push %eax; \
     pushl $0x1B; \ 
     push $1f; \ 
     iret; \ 
   1: \
     ");
I'm assuming it works, as when I don't try to call the system, everything works as normal. However, when I attempt to call my test system call on interrupt 0x7F, it GPFs.

Code: Select all

global test_func
test_func:
    mov eax, 0x0
    int 0x7F
I've double checked my GDT, IDT, and IRQ code, and can't seem to find a problem with them. The GPF gives 0 as the error code, and reports the instruction pointer to be 0 as well. Any ideas as to what might be the problem?

Re: Issues with system call

Posted: Sat Nov 30, 2013 12:48 am
by BMW
Does the descriptor for the system call interrupt have a DPL (descriptor privilege level) of 3 so that it can be used in user-mode?

Re: Issues with system call

Posted: Sat Nov 30, 2013 12:54 am
by zhiayang
Indeed; as BMW said, you need to set the DPL of the IDT descriptor to 3.

If you're using JMolloy's tutorial for that, it would be

Code: Select all

idt_set_gate( 0, (u32int)isr0 , 0x08, 0xEE);
Notice it's 0xEE, not 0x8E -- At least that's what I have in my system.

Re: Issues with system call

Posted: Sat Nov 30, 2013 1:29 am
by BMW
requimrar wrote:Indeed; as BMW said, you need to set the DPL of the IDT descriptor to 3.

If you're using JMolloy's tutorial for that, it would be

Code: Select all

idt_set_gate( 0, (u32int)isr0 , 0x08, 0xEE);
Notice it's 0xEE, not 0x8E -- At least that's what I have in my system.
Exactly what I have in my system as well. I remember I had exactly the same problem as the OP, probably wasted half a day on it!

Re: Issues with system call

Posted: Sat Nov 30, 2013 9:49 am
by zhiayang
Hello again.

I've looked at your kernel's source a bit (linked in your signature), and I've a few points;

1. You're using IRQ_InstallHandler (or whatever it's named)
This wouldn't usually cause an error, but double-check your code: your array irq_routines is only 16 entires large, while in install_syscall (or what have you), you're trying to index 0x7F. That's one issue.

2. In a similar vein, you didn't put an entry in the IDT for your IRQ entry (strictly it's not an IRQ), which is another reason it'll fault -- but note your sys call handler won't even be called in the first place.

3. I know tutorials do this, but for things like the PIT and your Syscall handler, don't go through the 'global handler' -- install your function directly into the IDT, like this:

Code: Select all

 idt::set_gate(0x7F, (unsigned long)handler, 0x08, 0xEE);
Note that the last parameter is 0xEE, so you can call it from Ring3 without a GPF.



All in all, here's what you should do:
- in init_syscalls(), do the idt::set_gate example above. replace (handler) with your sys call handler.


That's it. This bypasses your generic handler completely and should result in a negligible speed increase.
Most importantly, it fixes your GPF.

Re: Issues with system call

Posted: Sat Nov 30, 2013 10:21 am
by riparoony
Thank you so much! The issue was, as you predicted, incorrectly setup IDT.

Re: Issues with system call

Posted: Sat Nov 30, 2013 10:47 am
by zhiayang
joshbeitler wrote:Thank you so much! The issue was, as you predicted, incorrectly setup IDT.
No problem! Don't put it that way though, I didn't 'predict' anything, I merely looked :wink:

Re: Issues with system call

Posted: Sat Nov 30, 2013 11:21 am
by riparoony
OK, so it calls now. However, I am having trouble getting the correct value into eax in order to actually perform a call. I'm using the following code as a simple test:

Code: Select all

asm volatile("mov $0, %eax");
asm volatile("mov $0, %ebx");
asm volatile("mov $0, %ecx");
asm volatile("mov $0, %edx");
asm volatile("mov $0, %esi");
asm volatile("mov $0, %edi");
asm volatile("int $0x7F");
However, I keep getting ridiculous values for eax. Any ideas?

Re: Issues with system call

Posted: Sat Nov 30, 2013 12:55 pm
by BMW
I think your problem lies in how you set up your system call handler:

Code: Select all

 void init_syscalls()
{
    cpu::idt::set_gate(0x7F, (unsigned long)syscall_handler, 0x8, 0xEE);
}
You should make the system call IRQ go through your IRQ handler before the syscall handler, because your IRQ handler pushes the registers onto the stack whereas if you go straight to the syscall_handler() routine, the values of eax etc are not pushed onto the stack (except the ones pushed by processor automatically). This results in an incorrect stack upon calling the syscall_handler(), causing in the eax value to be wrong.

So, what you need to do is change the init_syscalls() function so that it sets up your system call handler correctly by going to your IRQ handler. So if you were using IRQ 2 for system calls (what I use in my OS):

Code: Select all

cpu::idt::set_gate(34, (unsigned long)irq2, 0x8, 0xEE);
Then add the syscall_handler() function as the irq handler for irq 2

Code: Select all

cpu::irq::install_handler(2, (irq_handler_t)syscall_handler);
I'm not sure why you are using interrupt 0x7F as your IRQs are only interrupts 32-47. I suggest you use one in that range. And since the PIT usually uses IRQ 0 and the keyboard uses IRQ 1, I wouldn't use them.

EDIT: Oh, just read this, I guess that's why you went straight to the syscall_handler():
requimrar wrote:I know tutorials do this, but for things like the PIT and your Syscall handler, don't go through the 'global handler'
But if you do that, EAX will not be pushed onto the stack. @requimrar how did you expect him to get EAX in his syscall_handler()? I think it would be easier to use the 'global handler' for now, focus on the "negligible" speed optimisations later :)

EDIT #2: Actually, if he goes straight to his syscall_handler() (i.e. puts that in the IDT), the value of EAX should still be in the register shouldn't it? So @joshbeitler, either use the value of the register EAX in your syscall_handler() or use the 'global handler'.

Re: Issues with system call

Posted: Sat Nov 30, 2013 7:39 pm
by zhiayang
BMW wrote:<snip> So if you were using IRQ 2 for system calls (what I use in my OS): <snip> I'm not sure why you are using interrupt 0x7F as your IRQs are only interrupts 32-47. <snip>
Well... I'm not sure why you'd limit yourself to the IRQ range? Those things are usually wired up to devices (legacy devices yes, but still) and when you go into the APIC or the PCI business, you'll want to allocate IRQs from that range to various devices.

Nothing stops you from using values greater than 47. IIRC the IDT is able to handle up to 256 entries, so you can use anything from 0x30 to 0xFF. Linux uses 0x80, Windows uses something else, I personally use 0xF8.
BMW wrote:EDIT #2: Actually, if he goes straight to his syscall_handler() (i.e. puts that in the IDT), the value of EAX should still be in the register shouldn't it? So @joshbeitler, either use the value of the register EAX in your syscall_handler() or use the 'global handler'.
Mmhmm. But looking at your code Josh, a couple of things;

1. You should handle the system call in assembly.
2. In your void syscall_handler(cpu::regs* r), you're expecting a pointer to a structure on the stack. Forgive me if I'm wrong, but I think %eax is used to pass the first parameter in x86... so your 'pointer' is the value of %eax, which is zero; so you're probably getting rubbish values from the BIOS IVT.

Instead, make your system call handler in assembly, it's not too difficult;

Code: Select all

	// multiply %r10 by 8
	salq $3, %r10
	movq SyscallTable(%r10), %r10
	jmp *%r10

CleanUp:
	iretq


// Syscall functions
PrintChar:
	call Syscall_PrintChar
	jmp CleanUp

Fail:
	jmp CleanUp
// End


.section .data

// Function table
.align 8
SyscallTable:
	.quad		Fail
	.quad		PrintChar
That's what I use. You'd put the sys call number in some register, and since you're in 32-bit mode, shift it by two, not three; then replace '.quad' with '.long'.

Not difficult.
Your problem lies in the face that you're still expecting syscall_handler to get registers passed to it on the stack, by means of your global handler. Since you bypass that, you can just pluck the values straight from the register. Pushing all the values on the stack would be unnecessary.

Remember though what you call in the assembly (Syscall_PrintChar in my case), needs to be prefixed with extern "C".



@BMW: no offence but we're probably confusing him with conflicting advice hahaha :wink:

Re: Issues with system call

Posted: Sat Nov 30, 2013 7:49 pm
by BMW
requimrar wrote:Well... I'm not sure why you'd limit yourself to the IRQ range? Those things are usually wired up to devices (legacy devices yes, but still) and when you go into the APIC or the PCI business, you'll want to allocate IRQs from that range to various devices.
Well he has only set up that IRQ range.

Re: Issues with system call

Posted: Sat Nov 30, 2013 8:18 pm
by zhiayang
BMW wrote: Well he has only set up that IRQ range.

Nothing stops him from using idt::set_gate to setup more.

Re: Issues with system call

Posted: Mon Dec 09, 2013 5:41 pm
by riparoony
BMW: Using the current method I have now, how would I assure that a register state is passed?

Re: Issues with system call

Posted: Mon Dec 09, 2013 6:49 pm
by zhiayang
joshbeitler wrote:BMW: Using the current method I have now, how would I assure that a register state is passed?

Alright, looking through your code again:

1. In void init_syscalls(), use only one of the methods described here -- either install directly into the IDT, or use your global handler. Don't do both, one will overwrite the other.

2. In order to ensure your (cpu::regs* r) structure contains values that make sense
a) If you use the your global handler, I don't think you need to do anything further, since your global handler already pushes all the registers on the stack.

b) However, if you want to plug your system call straight into the IDT and bypass the global handler, you would need to again, push all the registers on the stack.


However, using option (b) essentially negates the benefits of going directly through the IDT. If you really want C-level structure access to register values, (ie. cpu::regs* r) then you should use your global handler.


I hope I haven't confused you further.

Re: Issues with system call

Posted: Mon Dec 09, 2013 10:00 pm
by BMW
I recommend go with option (a) from requimrar's post above. Just go through your global handler, then the registers will be pushed into the stack, and can be used in your syscall_handler via the struct.