Page 1 of 1

How to write system calls

Posted: Fri Sep 20, 2002 11:00 pm
by rk
I would like to make system calls for my OS, unfortunately I have no idea how. Can anybody help?

RE:How to write system calls

Posted: Fri Sep 20, 2002 11:00 pm
by Khumba
One way is to use interrupts.

RE:How to write system calls

Posted: Fri Sep 20, 2002 11:00 pm
by Christopher Olsen
Yes a very simple way is through an int i will give you some example code:

//Possible ISR
asm(
  ".globl _sysCall              \n"
  "_sysCall:                    \n"
  "  cmpl totalCalls,%eax       \n"
  "  jae invalidCall            \n"
  "  call *systemCalls(,%eax,4) \n"
  "  iret                       \n" /* Exit interrupt               */
  );

typedef void (*functionPTR)();

functionPTR systemCalls[] = {
  functions,listed,here,like,this
  };

int totalCalls = sizeof(systemCalls)/sizeof(functionPTR);


That code should work with a few mods and all you do is int to that isr push ax onto the stack before you int with the syscall number 0 being the first item in your systemCalls list


-Christopher

RE:How to write system calls

Posted: Mon Sep 23, 2002 11:00 pm
by rk
Thank you very much, but could you elaborate on that last part a little more please? Thank you very much!

Rk

RE:How to write system calls

Posted: Mon Sep 23, 2002 11:00 pm
by carbonBased
It's actually a pretty easy concept, and one of the most efficient ways of doing it, as well.

Let's say your ISR has four subsections:
open, read, write, and close

And you want the API to be something like this:
mov ax, 0         ; 0 for open, 1 for read, 2 for write, etc...
mov other_regs, parameters
int 32            ; for example

Simple enough, right?

Your int 32 ISR has to call the correct function (read, write, etc) based on what's in the ax register.  So why not make an array of function pointers:

long int32routines[] = { open, read, write, close };
// where open, read, write and close are functions

Now, in your ISR code, all you really have to do is call int32routines[ax], and you're done.  The ax register becomes the index into that array.  Control is passed to the appropriate function, it does its thing, and returns to the ISR, which irets to the calling program.

Hopefully that all makes sense... I must admit, I'm a little hung-over, so the mind isn't exactly functional, lol :)  Hope you like my mixture of C and ASM :)

Jeff

RE:How to write system calls

Posted: Thu Oct 17, 2002 11:00 pm
by kumvinay
If you are writing it for ARM, there is one assembly instruction known as SWI(software interrupt) for raising software interrupts. after that you can write your own ISR for handling this interrupt. (Obviously)you need to switch processor in Supervisory mode..
Vinay