How to write system calls

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
rk

How to write system calls

Post by rk »

I would like to make system calls for my OS, unfortunately I have no idea how. Can anybody help?
Khumba

RE:How to write system calls

Post by Khumba »

One way is to use interrupts.
Christopher Olsen

RE:How to write system calls

Post 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
rk

RE:How to write system calls

Post by rk »

Thank you very much, but could you elaborate on that last part a little more please? Thank you very much!

Rk
carbonBased

RE:How to write system calls

Post 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
kumvinay

RE:How to write system calls

Post 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
Post Reply