Page 1 of 1

Syscalls: some info

Posted: Fri Dec 30, 2005 7:55 am
by falconfx
Hi!

I'd like to include syscalls support into my kernel. I've read that I can use one IDT entry to define a software interrupt (e.g. 0x80).

My questions are: do I have to define it ring 0 or ring 3? Do I have to disable interrupts when a syscall gets called and therefore setting the IDT entry as a trap gate and not as an interrupt one?

Cheers,

falconfx

Re:Syscalls: some info

Posted: Fri Dec 30, 2005 8:07 am
by Brendan
Hi,
falconfx wrote:My questions are: do I have to define it ring 0 or ring 3? Do I have to disable interrupts when a syscall gets called and therefore setting the IDT entry as a trap gate and not as an interrupt one?
You have to set DPL in the IDT entry as 3 so that CPL=3 code can use it (if you set DPL to 0 then only CPL=0 code could use it, and CPL=3 code would generate a general protection fault if it tried).

Depending on how you write your kernel (if it's interruptable or re-entrant or not), and if you do anything unusual with the kernel stack (like having N shared kernel stacks rather than one for each task) you may or may not want interrupts to be disabled automatically by the CPU. You don't have to do it one way or the other - it's your choice...


Cheers,

Brendan

Re:Syscalls: some info

Posted: Fri Dec 30, 2005 1:30 pm
by viral
Hello....
I have one problem. I am having a user task which run under its own address space as its have its own page directory. My question is Do I need to change the address space when syscall is made. i.e. USER -> KERNEL.
I am not getting how values are passed in syscall. integers or numbers are OK as they can be directly passed in registers, but how to pass structures?
Linux is using pointer of the structure to be passed in eax during a syscall, but when address space is changed then this pointer is immaterial....

Re:Syscalls: some info

Posted: Fri Dec 30, 2005 2:11 pm
by Colonel Kernel
viral wrote:I have one problem. I am having a user task which run under its own address space as its have its own page directory. My question is Do I need to change the address space when syscall is made. i.e. USER -> KERNEL.
There is a simple solution to this problem. Make sure that the kernel is mapped to the same virtual addresses in all address spaces. This means every page directory will have a certain number of entries that point to shared kernel page-tables. For example, for a 32-bit higher-half kernel that consumes half the address space, the first 512 entries in each page directory will point to private page tables for that process, while the remaining 512 entries will point to shared kernel page tables that map pages with "supervisor"-level access (this means they can only be touched from ring 0 code).

With this setup, you don't have to change cr3 on every system call, only when switching between different processes.
I am not getting how values are passed in syscall. integers or numbers are OK as they can be directly passed in registers, but how to pass structures?
Linux is using pointer of the structure to be passed in eax during a syscall, but when address space is changed then this pointer is immaterial....
If the kernel is mapped into all address spaces, then it can access structures in the user-mode memory of the calling process. However, you will have to take care that this memory is actually resident (i.e. -- be aware that the kernel may generate page faults when touching this non-kernel memory).

Re:Syscalls: some info

Posted: Fri Dec 30, 2005 2:22 pm
by viral
Hi....
Thanks 4 the reply. My kernel is running at 0xC0000000. I am adding everything after 0xC0000000 into page directory of a user space. So do I need to bother now ?

Re:Syscalls: some info

Posted: Fri Dec 30, 2005 9:08 pm
by Brendan
Hi,
viral wrote: Thanks 4 the reply. My kernel is running at 0xC0000000. I am adding everything after 0xC0000000 into page directory of a user space. So do I need to bother now ?
You'd still need some sort of kernel API (or syscalls), but you don't need to change address spaces when the kernel API is used.


Cheers,

Brendan

Re:Syscalls: some info

Posted: Sat Dec 31, 2005 1:07 pm
by viral
Ok... Now I got it.

Re:Syscalls: some info

Posted: Sun Jan 01, 2006 11:29 am
by JAAman
make sure you set the global bit on the shared portion of the kernel space, it improves performance significantly (most programs spend much of their time making syscalls -- each time the address space changes, the CPU will have to refech the tables before resolving the address, unless the global bit is set)