Page 1 of 1

To use Ints or not to use Ints

Posted: Mon Oct 02, 2006 3:08 am
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);
}

Re: To use Ints or not to use Ints

Posted: Mon Oct 02, 2006 4:11 am
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.

Re: To use Ints or not to use Ints

Posted: Mon Oct 02, 2006 4:49 am
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!

Re: To use Ints or not to use Ints

Posted: Mon Oct 02, 2006 8:34 am
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.

Posted: Mon Oct 02, 2006 10:44 am
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.

Posted: Mon Oct 02, 2006 6:32 pm
by Legend
Yes, if you use ring 0 for everything you can directly call everything you want, if you have mapped it with paging. :)