Syscalls versus Call Gates

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.
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Syscalls versus Call Gates

Post by Jeko »

Which are pros and cons of syscalls and pros and cons of call gates? Which is the fastest method to handle user to kernel interfacing?
Rewriting virtual memory manager - Working on ELF support - Working on Device Drivers Handling

http://sourceforge.net/projects/jeko - Jeko Operating System
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Syscalls versus Call Gates

Post by Solar »

Wikipedia writes about Call Gates:
Modern X86 operating systems are transitioning away from CALL FAR callgates. With the introduction of SYSENTER/SYSEXIT and SYSCALL/SYSRET, a new faster mechanism was introduced for control transfers for x86 programs. [...]

It should be noted that call gates are more flexible than the SYSENTER/SYSEXIT and SYSCALL/SYSRET instructions since unlike the latter two, call gates allow for changing from an arbitrary privilege level to an arbitrary privilege level. The fast SYS* instruction only allow control transfers from ring 3->0 and vice versa. Upon comparing call gates to interrupts, call gates are significantly faster.
Every good solution is obvious once you've found it.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Syscalls versus Call Gates

Post by AJ »

Hi,

I don't know much about call gates as I never use them, but generally you will use one of three methods for user/kernel transitions:

1. System Call Interrupt (as you suggest - this is like Linux Int 0x80). Each call requires a privilege transition, register storage, stack switch and possible task space switch (if the call requires it, which will happen a lot if you are writing a microkernel) - sloooow (but secure if implemented well)!
2. SYSENTER/SYSEXIT, SYSCALL/SYSRET - if supported (check with CPUID), this is a lot faster than a basic system call interrupt and foregoes a lot of the privilege transitions. You also have a proper specification for this written by Intel/AMD which will tell you which registers are scratch registers and which need to be preserved.
3. Managed Code - this has the potential to be a very fast system as you do not need any privilege level / task space switches. It also has the potential to be very unsecure/unstable if you don't implement your base kernel and JIT compiler well. Although the system calls are potentially fast, JIT compilation could slow down your code generally.

Cheers,
Adam
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Re: Syscalls versus Call Gates

Post by Jeko »

Solar wrote:Wikipedia writes about Call Gates:
Modern X86 operating systems are transitioning away from CALL FAR callgates. With the introduction of SYSENTER/SYSEXIT and SYSCALL/SYSRET, a new faster mechanism was introduced for control transfers for x86 programs. [...]

It should be noted that call gates are more flexible than the SYSENTER/SYSEXIT and SYSCALL/SYSRET instructions since unlike the latter two, call gates allow for changing from an arbitrary privilege level to an arbitrary privilege level. The fast SYS* instruction only allow control transfers from ring 3->0 and vice versa. Upon comparing call gates to interrupts, call gates are significantly faster.
So call gates are faster... But why most operating systems use interrupts?

However I'll use SYSENTER/SYSEXIT and SYSCALL/SYSRET (and syscalls for processors that doesn't support these instructions)
Rewriting virtual memory manager - Working on ELF support - Working on Device Drivers Handling

http://sourceforge.net/projects/jeko - Jeko Operating System
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Re: Syscalls versus Call Gates

Post by Jeko »

AJ wrote:3. Managed Code - this has the potential to be a very fast system as you do not need any privilege level / task space switches. It also has the potential to be very unsecure/unstable if you don't implement your base kernel and JIT compiler well. Although the system calls are potentially fast, JIT compilation could slow down your code generally.
But managed code is slower than machine code. Or not?
And with managed code there isn't a fully virtual space for each process. Am I right?

However I think that it's better an OS in normal code than an OS in managed code
Rewriting virtual memory manager - Working on ELF support - Working on Device Drivers Handling

http://sourceforge.net/projects/jeko - Jeko Operating System
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Syscalls versus Call Gates

Post by Brendan »

Hi,
Jeko wrote:Which are pros and cons of syscalls and pros and cons of call gates? Which is the fastest method to handle user to kernel interfacing?
Given the choice between software interrupts, call gates, SYSENTER and SYSCALL, why not just implement all of them?

Note1: if the CPU doesn't support the SYSENTER and/or SYSCALL instructions, you can emulate the instructions inside your invalid opcode exception handler. This would be slower than a software interrupt, but it's more fun than doing "if (CPU_supports_SYSENTER() ) { FOO } else { BAR }" everywhere.

Note2: software interrupts are slower than call gates, but "INT n" only costs 2 bytes while a "CALL FAR" costs 7 bytes, so if you're optimizing for size (e.g. application startup code that's only run once) and don't want to know if SYSENTER/SYSCALL are supported then using a software interrupt is better. The "INT3" instruction is only one byte (which makes it the smallest possible option) but it's probably better to use it for debugging purposes.

Note3: For 64-bit code, just use SYSCALL. The other options are for 16-bit and/or 32-bit code (including 16-bit and/or 32-bit code running under a 64-bit OS).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Re: Syscalls versus Call Gates

Post by Jeko »

Brendan wrote:Hi,
Jeko wrote:Which are pros and cons of syscalls and pros and cons of call gates? Which is the fastest method to handle user to kernel interfacing?
Given the choice between software interrupts, call gates, SYSENTER and SYSCALL, why not just implement all of them?

Note1: if the CPU doesn't support the SYSENTER and/or SYSCALL instructions, you can emulate the instructions inside your invalid opcode exception handler. This would be slower than a software interrupt, but it's more fun than doing "if (CPU_supports_SYSENTER() ) { FOO } else { BAR }" everywhere.

Note2: software interrupts are slower than call gates, but "INT n" only costs 2 bytes while a "CALL FAR" costs 7 bytes, so if you're optimizing for size (e.g. application startup code that's only run once) and don't want to know if SYSENTER/SYSCALL are supported then using a software interrupt is better. The "INT3" instruction is only one byte (which makes it the smallest possible option) but it's probably better to use it for debugging purposes.

Note3: For 64-bit code, just use SYSCALL. The other options are for 16-bit and/or 32-bit code (including 16-bit and/or 32-bit code running under a 64-bit OS).


Cheers,

Brendan
So why don't use INT3 instead of INT80?

However I'd like to know how to implement the first thing. But I must do an IF to check if there is SYSENTER or there is SYSCALL
Rewriting virtual memory manager - Working on ELF support - Working on Device Drivers Handling

http://sourceforge.net/projects/jeko - Jeko Operating System
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Syscalls versus Call Gates

Post by AJ »

Jeko wrote:So why don't use INT3 instead of INT80?
Do you mean int 0x06 which is the Invalid Opcode Exception? If you did it that way, you wouldn't know if it really is an invalid opcode, so you need to do the checks suggested by Brendan.

Cheers,
Adam
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Re: Syscalls versus Call Gates

Post by Jeko »

AJ wrote:
Jeko wrote:So why don't use INT3 instead of INT80?
Do you mean int 0x06 which is the Invalid Opcode Exception? If you did it that way, you wouldn't know if it really is an invalid opcode, so you need to do the checks suggested by Brendan.

Cheers,
Adam
I mean, why for syscall Linux use int 0x80 and not int 3 if int 3 opcode is smaller?

And why most OSes use interrupts instead of call gates if call gates are faster?

However when an invalid opcode exception occurs how can I check that it's a SYSCALL/SYSENTER or a SYSRET/SYSEXIT?
Rewriting virtual memory manager - Working on ELF support - Working on Device Drivers Handling

http://sourceforge.net/projects/jeko - Jeko Operating System
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Syscalls versus Call Gates

Post by AJ »

Hi,

You really need to do some background reading and look at the Intel Manuals. Int 0x03 is the Breakpoint Exception - INT 0x00-0x1F should be reserved by your kernel for handling CPU exceptions.
However when an invalid opcode exception occurs how can I check that it's a SYSCALL/SYSENTER or a SYSRET/SYSEXIT?
You have the EIP where the exception occurred - simply check the Opcode (which can be found in the Intel Manuals).

Cheers,
Adam
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Re: Syscalls versus Call Gates

Post by Jeko »

AJ wrote:You really need to do some background reading and look at the Intel Manuals. Int 0x03 is the Breakpoint Exception - INT 0x00-0x1F should be reserved by your kernel for handling CPU exceptions.
I know that INT 3 is the Breakpoint Exception...
But:
Brendan wrote:The "INT3" instruction is only one byte (which makes it the smallest possible option) but it's probably better to use it for debugging purposes.
INT 3 is the breakpoint exception, but I can use it how I want.
Rewriting virtual memory manager - Working on ELF support - Working on Device Drivers Handling

http://sourceforge.net/projects/jeko - Jeko Operating System
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re: Syscalls versus Call Gates

Post by Colonel Kernel »

AJ wrote:3. Managed Code - this has the potential to be a very fast system as you do not need any privilege level / task space switches. It also has the potential to be very unsecure/unstable if you don't implement your base kernel and JIT compiler well. Although the system calls are potentially fast, JIT compilation could slow down your code generally.
JIT compilation is not a requirement for so-called "managed code". The only requirement is the ability to verify the code for type-safety before it runs, and to disallow modifying the code.
Jeko wrote:But managed code is slower than machine code. Or not?
Depends how it's implemented. There is a lot of general misunderstanding about how it works. I suggest reading the Singularity research papers if you want to know more.
And with managed code there isn't a fully virtual space for each process. Am I right?
Whether or not there is a virtual address space for each process becomes optional with software isolation. In Singularity for example, it is a configurable option (maybe a compile-time option for the kernel, I'm not sure).
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Syscalls versus Call Gates

Post by jnc100 »

Jeko wrote:INT 3 is the breakpoint exception, but I can use it how I want.
You can, however then you become limited with your debugging options. Int 3 (as a single opcode) is useful because a debugger can replace any instruction with it to create a breakpoint. You can replace a two byte instruction with a two byte opcode easily enough and so on, but it becomes more difficult when the instruction you want to break on is only one byte. For example, say you have:

Code: Select all

func1:
  add eax, 2;
  ret;

func2:
  mov eax, 5;
  call func1;
  ret;
Now you can easily break on the first line of func1 by replacing it with int3, or int 4,5,6 etc. Then your handler will trigger the 'break_point_hit' code, and restore the actual opcodes before iret to continue. To be short, it doesn't matter how much of the 'add eax, 2' opcodes you overwrite, because you will restore them before they are executed. The problem is if you want to break on the ret in func1. Replacing it with anything longer than a single opcode will overwrite the start of func2, and you cannot guarantee that that code will not be executed before the breakpoint (in func1) is hit (and the code restored), therefore a single byte interrupt instruction is very useful to have around. IMHO its best to preserve the special encoding of int 3 for this purpose.

Regards,
John.
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Re: Syscalls versus Call Gates

Post by Jeko »

Colonel Kernel wrote:
Jeko wrote:But managed code is slower than machine code. Or not?
Depends how it's implemented. There is a lot of general misunderstanding about how it works. I suggest reading the Singularity research papers if you want to know more.
I think managed code MUST be slower than machine code. It's normal.

jnc100 you're right.

However I think I'll use interrupts syscalls and sysenter/syscall and sysexit/sysret. The only problem is that I must do an IF for each call.
Rewriting virtual memory manager - Working on ELF support - Working on Device Drivers Handling

http://sourceforge.net/projects/jeko - Jeko Operating System
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Syscalls versus Call Gates

Post by jal »

Jeko wrote:I think managed code MUST be slower than machine code. It's normal.
No, not necessarily. Managed code is checked once, to verify that it doesn't do anything dangerous. After that, it can be run at full speed.


JAL
Post Reply