João Silva
João Silva

Reputation: 413

Do OS APIs use interrupts?

I mean, deep down in the core of an Operating System API, is there an interrupt 'call' for each basic function a program requests from the OS?

Upvotes: 1

Views: 158

Answers (2)

Alexey Frunze
Alexey Frunze

Reputation: 62048

Yes, there very often is such an interrupt.

Interrupt and exception handlers and system calls are often implemented using the same mechanism. The reason for this is that in all these cases the control has to be transferred into the kernel and a number of registers has to be preserved before and restored after the handling of the event. Interrupt handling occurs in the kernel, is ubiquitous (rare CPUs don't support interrupts) and it's a natural choice for interrupt handling, exception handling and system calls.

What's more, in some cases it's desirable to invoke system calls from already within the kernel.

In Windows, for example, kernel code often has an option of calling either ZwFoo() or NtFoo(), where Foo is some meaningful function name and Zw and Nt are name prefixes, differentiating between the two versions of Foo(). If ZwFoo() is chosen, Foo() is called directly. OTOH, if NtFoo() is chosen, the control first has to go through the system call (AKA trap) mechanism/code and only then will it reach the actual Foo(). The choice between the two versions has to do with something called Previous Mode.

A few words on Previous Mode... Kernel code is trusted. User code is untrusted. When Foo() is called from user mode, the kernel will do its best to validate all untrusted inputs and check that all the permissions are in place before doing what's requested. That's how it's ought to be. Now the kernel itself sometimes needs to call Foo(). If it calls Foo() on behalf of itself, a number of rigorous checks don't need to be made. But if it calls Foo() on behalf of user code with inputs from user mode (and all that is untrusted), those checks must be made. Hence, depending on the situation the kernel calls either ZwFoo() or NtFoo(). When NtFoo() is called PreviousMode gets set to UserMode and that signals the necessity of the aforementioned checks. When ZwFoo() is called, PreviousMode remains UserMode for user mode callers or gets set to KernelMode for kernel mode callers. So, user mode callers can't avoid the checks but the kernel has a choice of doing them (when they are necessary) or not (when they aren't).

Upvotes: 2

James
James

Reputation: 9278

At least on Linux there are syscalls. When userspace wants something from the kernel is issues a software interrupt 0x80 with the index of the function it wants to call in a register.

More here

Upvotes: 1

Related Questions