To use Ints or not to use Ints

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
User avatar
Daedalus
Member
Member
Posts: 74
Joined: Sun Oct 16, 2005 11:00 pm
Location: Australia
Contact:

To use Ints or not to use Ints

Post by Daedalus »

Hi all,

I'm reworking my OS (which is based on Borealis by Chris Giese) and have come upon a question that will decide how I write a fair amount of the OS.

Chris' LibC functions that use the kernel (eg, read, write) use an interrupt (with parameters given and received).

I'm wondering if instead of using an interrupt all the time, would it be faster to have the interrupt called once, save the address of the required function, and from then on use that function call (which is in kernel space.)

Would this be faster, or better?

Just a note that this is a 32-Bit Protected Mode OS, and that the interrupt looks at EAX, and decides which function to call/return using a switch statement.
Thus to me, it would seem that using a function call instead would be quicker, and such functions in libc would be pointers which would change their own address, eg:

Code: Select all

/* Simple example */
int (*close) (unsigned handle)
{
	int (*fn_address)(unsigned handle);

	/* Call int SYSCALL_INT, with EAX being SYS_CLOSE_ADDRESS */
	/* Address of kernel function close will be moved to fn_address */
	__asm__ __volatile__(
		"int %1\n"
		: "=a"(fn_address)
		: "i"(SYSCALL_INT), "a"(SYS_CLOSE_ADDRESS));

	close = fn_address;
	return close(handle);
}
Legend
Member
Member
Posts: 195
Joined: Tue Nov 02, 2004 12:00 am
Contact:

Re: To use Ints or not to use Ints

Post by Legend »

Daedalus wrote:I'm wondering if instead of using an interrupt all the time, would it be faster to have the interrupt called once, save the address of the required function, and from then on use that function call (which is in kernel space.)
If you don't want your applications to run on Ring 3 that is just impossible, as they can't directly call Ring 0 Code then.

You might want to take a look at SYSCALL/SYSENTER.
User avatar
Daedalus
Member
Member
Posts: 74
Joined: Sun Oct 16, 2005 11:00 pm
Location: Australia
Contact:

Re: To use Ints or not to use Ints

Post by Daedalus »

Hi, thanks for your reply!

A few questions though...
Legend wrote: If you don't want your applications to run on Ring 3 that is just impossible, as they can't directly call Ring 0 Code then.
Arent user apps generally Ring 3? If so, did you mean that Ring 3 can't call Ring 0 code?
Legend wrote:You might want to take a look at SYSCALL/SYSENTER.
Do you mean the SYSCALL interrupts currently being used? In my old version I used this, but wondered if it would be better to use calls instead.

Thanks for your help!
Legend
Member
Member
Posts: 195
Joined: Tue Nov 02, 2004 12:00 am
Contact:

Re: To use Ints or not to use Ints

Post by Legend »

Daedalus wrote:Hi, thanks for your reply!

A few questions though...
Legend wrote: If you don't want your applications to run on Ring 3 that is just impossible, as they can't directly call Ring 0 Code then.
Arent user apps generally Ring 3? If so, did you mean that Ring 3 can't call Ring 0 code?
Most operating systems have applications on Ring 3. In the end it is however your decision. Technically, you can load code and run it on ring 0, too, however, technically, that would be unsafe if you don't ensure memory protection by another way like using a safe language.

And yes, Ring 3 can't call Ring 0 code directly. That is why call gates and interupt gates exist, to give code on ring 3 a controlled way to call ring 0 code.
Legend wrote:You might want to take a look at SYSCALL/SYSENTER.
Do you mean the SYSCALL interrupts currently being used? In my old version I used this, but wondered if it would be better to use calls instead.
Hmm, SYSCALL and SYSENTER are instructions. If you have used those instructions already, then it is what I meant. They should perform a good bit faster then using interrupts.

However, the best way to optimize this is to design an API that doesn't need a lot of calls. Something like SetPixel for graphics is a big no-no.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

I have both int and call table in my os, but my whole os runs in ring0, like eg: game console, xbox, ps2, ds, sp etc.
But if you use this method, you can just have one int that loads the address of all functions.
Last edited by Dex on Tue Oct 03, 2006 12:02 pm, edited 1 time in total.
Legend
Member
Member
Posts: 195
Joined: Tue Nov 02, 2004 12:00 am
Contact:

Post by Legend »

Yes, if you use ring 0 for everything you can directly call everything you want, if you have mapped it with paging. :)
Post Reply