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
Syscalls: some info
Re:Syscalls: some info
Hi,
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
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).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?
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.
Re:Syscalls: some info
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....
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....
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re:Syscalls: some info
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).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.
With this setup, you don't have to change cr3 on every system call, only when switching between different processes.
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).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....
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
Re:Syscalls: some info
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 ?
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
Hi,
Cheers,
Brendan
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.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 ?
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.
Re:Syscalls: some info
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)