Tag Archives: interrupt

Operating System Independent Interrupts!!! – Now with bacon!

Filed under Rants & Misc
Tagged as , , ,

This document is intended for those who program in high level languages. Do not try this at home unless you know what your doing. Do so at your own risk.

Prerequisite for this text:

Basic knowledge of C

Intermediate knowledge of Assembly

Basic knowledge of Operating Systems

What is a system call?

A system call is a procedure that is native to the operating system. Most system calls allow a program to perform a complex task with the operating system that would other wise be hard for the programmer to write themselves without intimate knowledge of the OS and hardware. It is basically a short-cut to advanced computing.

Why create an OS independent system call?

The operating system can be rather slow, sometimes, with the handling of your code and may allow other events to occur that you wish not to take up your systems time. A good example of this would be a program that reboots the computer after the OS has crashed due to a hardware device sending a NMI (non-maskable interrupt). This would occur if memory failed to operate correctly, a D.M.A. controller (primary or slave) failed, or other such catastrophic failure.

The problem lies in the fact that the CPU will halt the system directly after receiving such an interrupt and you have no idea the NMI will occur until the interrupt is handled. To skip this problem you use an OS independent system call. Before explaining how this situation is solved, I must define how an OS independent system call works in better detail.

How does an OS independent system call work?

All system interrupts are defined in a table called the system interrupt vector table. This table exists at the base of system memory, IE the very first byte of ram until the table runs out of vectors. The computer has normally 256 interrupt and can generally process from 16 to 32 at any one time via the programmable interrupt controller. When an interrupt is called by the system or a program, the CPU stops what it is doing and saves it’s work in the stack, a place in memory reserved for quick storage by the CPU. Next the CPU finds the interrupt vector for the specific interrupt called. This is done by multiplying the interrupt number by 4 as all interrupts get 4 bytes of ram in the vector table. This means that interrupt 147’s vector is memory address H24C to H24F. If you were to look at this address you’d probably find the assembly command JMP fallowed by a memory address. This is used to point to the real location of this interrupt in memory as 4 bytes will never hold the entire interrupt. After completion of the interrupt, the CPU goes back to the memory address it was previously executing and resumes it’s work.

To create an OS independent system call we need only create our own interrupt. This is done by finding a free interrupt and inserting code that points to a place in memory where your system call would be found. To find a free interrupt you need only open debug (an ms dos application), or AS (a GNU assembler), and look through the interrupt vector table for free, unused, sections of memory. Once you have found one just take the current memory address your in and do the simple math: interrupt = (current_address -(current_address % 4)/4).

Add a JMP command to that address fallowed by some NOP commands. Now place the system call executable in the start up files for your computer as a TSR program. When it installs it should add that jump command each time to the vector table before terminating.

Calling an OS independent system call

The code to call an OS independent system call from a high level language is simple enough. Our example uses the language C and the GNU C compiler. We will use in-line assembly to call the interrupt 147, our OS independent system call interrupt number.

#include <stdio.h>

int main(void) {

//Lets call interrupt 147!

asm(“int 93”); // 147 is 93 in hex

return 0;

}

To recap what I was talking about in the second part of this text, “Why create an OS independent system call?”, the way to beat the NMI is simply to change the JMP statement in the vector table for interrupt 2 to point somewhere else, such as your code. This allows you to reboot the system and have a computer that recovers from a crash by it’s self!

It’s as easy as that. I hope you have a lot of fun making your own system calls.