SYSCALLS examples are needed

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
Thunder

SYSCALLS examples are needed

Post by Thunder »

Hello, I'm now at stage where software multitasking is enabled with priorities etc.

Now I tried to call system call form user app trought interrupt.
User app and kernel are written in C language, but I use asm inline. And I wonder how could I call syscalls trought interrupt directly to some kernel function.

I created mini syscall.h file what system functions can be available, for example:

When user app tries to call function lets say:

Code: Select all

unsigned short WindowNr = SYSCALL_CreateWindow(Window);
it really calls that function in .h file:

Code: Select all

unsigned short SYSCALL_CreateWindow(GUIWindow ThisWindow)
{
   __asm__(
   "movl 0x1, %eax\n\t"
   "int $0x33");
}
it should leave 0x1 value in eax register after interrupt. But when this 0x33 interrupt triggers, I get in kernel side not this value in eax register.

So, could you help me? Or I wrote bad inline asm, or maybe after an interrupt is trigered some registers changes or etc.?

And one more question. If user app call a functon that requires some input values and stores these values in stack pointer memory, so after an interrupt these input values should leave in stack, right?

Could you point to some good system calls examples?

Thanks, bye ;)
P.S.: sorry for my bad english.
Schol-R-LEA

Re:SYSCALLS examples are needed

Post by Schol-R-LEA »

Assuming you mean to pass the value 0x01 and not the dereference the value of address 0x01, then think what you meant was

Code: Select all

unsigned short SYSCALL_CreateWindow(GUIWindow ThisWindow)
{
   __asm__(
   "movl $0x1, %eax\n\t"
   "int $0x33");
}
The smallest mistakes are often the hardest to catch.

BTW, have you considered using call gates instead of interrupts?
Tim

Re:SYSCALLS examples are needed

Post by Tim »

A less error-prone way to write this would be:

Code: Select all

unsigned short SYSCALL_CreateWindow(GUIWindow ThisWindow)
{
  __asm__("int $0x33" : : "a" (1));
}
This instructs gcc to load the value 1 into register EAX, then run your assembly code.

You're probably seeing a warning about a missing return value (and if you're not, get your @$$ over to -Wall now). Assuming that your system calls return the result in EAX:

Code: Select all

unsigned short SYSCALL_CreateWindow(GUIWindow ThisWindow)
{
  unsigned int ret;
  __asm__("int $0x33" : "=a" (ret) : "a" (1));
  return (unsigned short) ret;
}
Also, surely you want to return a 32-bit value (e.g. unsigned long or unsigned int) instead of a 16-bit one?
Thunder

Re:SYSCALLS examples are needed

Post by Thunder »

Well thank you both, but could you explain me, what are advantages of call gates?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:SYSCALLS examples are needed

Post by Pype.Clicker »

the main advantage is that you can use the same call far instruction to invoque
- a procedure in your 'local' code (the segment being a code segment)
- a procedure in some more priviledged code (the segment being a call gate with DPL2 or DPL1)
- a system call

Thus your application do not need to know 'what' it will call ... though proxy/stub generation techniques 'a la COM' tends to make that approach obsolete. A call gate will also take care of copying N words from the caller stack on the callee stack if they appear to need different stacks.

If you don't plan to have intermediate levels nor far libraries, call gates are quite useless, imho.

oh! just one thing, though: call gates may be in the LDT so code running in different TSSes can easily have different implementation of call GATE:00000000 by having the gate defined in the LDT and pointing towards something else, while the IDT is automatically global and thus 'int $80' points towards the same code for everybody...
Post Reply