Syscalls: some info

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
falconfx

Syscalls: some info

Post 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
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Syscalls: some info

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
viral

Re:Syscalls: some info

Post 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....
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:Syscalls: some info

Post 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).
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
viral

Re:Syscalls: some info

Post 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 ?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Syscalls: some info

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
viral

Re:Syscalls: some info

Post by viral »

Ok... Now I got it.
JAAman

Re:Syscalls: some info

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