System calls triggered by page faults

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.
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

System calls triggered by page faults

Post by Antti »

I know that this is not as efficient as "real" system calls. However, I was thinking whether this would be elegant or not. I am sure that this is already implemented somewhere.

Code: Select all

#define SYSCALL_TRAP 0xFFFFFFFC          /* "Some "kernel" address */

void Syscall(int number)
{
	*((int *)0xFFFFFFFC) = number;       /* Deliberate page faulting */
}
The page-fault handler would recognize the syscall number the user is trying to store at this "illegal" address. With C programming language, it is not very easy to recognize the call number portably. What about the parameters? Putting them to "parameter passing" space?

All the implementation details aside, what do you think about this in general? System calls could be triggered without any platform-specific opcodes. Programs could see system services like they see memory-mapped devices.
Last edited by Antti on Tue Jul 16, 2013 3:28 am, edited 1 time in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: System calls trigged by page faults

Post by Combuster »

- No concurrent system calls (race conditions).
- Significantly slower system calls.
- Actual pagefaults may be mistaken for system calls - try free(0) and it might just access that address
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: System calls triggered by page faults

Post by iansjack »

I think you are addressing a problem that doesn't exist. Some parts of an OS are always going to be non-portable; for example the actual paging mechanism to implement your system would differ on different processors (as, almost certainly, would the fake address used).

Until all computers use the same instruction set you aren't going to get binary compatibility of a kernel, so why focus on the system call mechanism in particular? It can be made portable except for a small amount of architecture-dependent code. I don't see that as being a problem.
User avatar
skeen
Member
Member
Posts: 59
Joined: Tue Sep 27, 2011 6:45 am
Location: Denmark

Re: System calls triggered by page faults

Post by skeen »

// Skeen
// Developing a yet unnamed microkernel in C++14.
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: System calls triggered by page faults

Post by Antti »

I want to emphasize that efficiency is not very important if looking the idea itself. It is just platform-specific "detail".

In short: Memory-mapped system services with standarized inteface.
User avatar
skeen
Member
Member
Posts: 59
Joined: Tue Sep 27, 2011 6:45 am
Location: Denmark

Re: System calls trigged by page faults

Post by skeen »

Combuster wrote: ... try free(0) and it might just access that address
According to ISO-IEC 9899 (N1124) the behavior is; (7.20.3.2)
The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs.
Link to N1124; http://www.open-std.org/JTC1/SC22/wg14/ ... /n1124.pdf
// Skeen
// Developing a yet unnamed microkernel in C++14.
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: System calls triggered by page faults

Post by Antti »

skeen wrote:Has been discussed
Exactly the same thing discussed already! I will look at those threads. Maybe there is not much more to talk about...
User avatar
skeen
Member
Member
Posts: 59
Joined: Tue Sep 27, 2011 6:45 am
Location: Denmark

Re: System calls trigged by page faults

Post by skeen »

Combuster wrote:No concurrent system calls (race conditions).
One could have a sys call page-fault address per thread. In which case you'd be able to have concurrent sys calls.
// Skeen
// Developing a yet unnamed microkernel in C++14.
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: System calls triggered by page faults

Post by Antti »

If we had a plain toy OS (to avoid criticism of efficiency), we could consider to have this kind of implementation of "putchar":

Code: Select all

#define SYSCALL_PUTCHAR ???? ????    /* A trap address */

void putchar(int c)
{
	*((char *)SYSCALL_PUTCHAR) = c;
}
The basic idea seems quite elegant to me. I have always liked the memory-mapped I/O, like "when you write to this address, it has some effect".
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: System calls triggered by page faults

Post by Combuster »

That gives the next problem, a pagefault doesn't show you the value that was attempted to be read or written, only the target address and the instruction that caused it. The pagefault handler couldn't sanely access the character value.
One could have a sys call page-fault address per thread.
And which portable mechanism are you going to use to get that address?
According to ISO-IEC 9899 (N1124) the behavior is
I must have gotten used to broken implementations. :?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
skeen
Member
Member
Posts: 59
Joined: Tue Sep 27, 2011 6:45 am
Location: Denmark

Re: System calls triggered by page faults

Post by skeen »

Combuster wrote:
One could have a sys call page-fault address per thread.
And which portable mechanism are you going to use to get that address?
Assuming you get the first one, when creating a process, it's just a matter of implementing the allocation of a sys call page-fault address as a part of the create thread method, I guess.

Assuming that threads are created from old threads, and that you're able to bootstrap 1 thread.
// Skeen
// Developing a yet unnamed microkernel in C++14.
User avatar
skeen
Member
Member
Posts: 59
Joined: Tue Sep 27, 2011 6:45 am
Location: Denmark

Re: System calls triggered by page faults

Post by skeen »

Combuster wrote:That gives the next problem, a pagefault doesn't show you the value that was attempted to be read or written, only the target address and the instruction that caused it. The pagefault handler couldn't sanely access the character value.
What one could do is to use two pages per thread to support sys calls. One to pass all arguments and such, and one to trigger the sys call. First one being present, last one not being.

However then writing to the non-present page would be somewhat alike doing a software interrupt, as it would just be a trap into the kernel.
Last edited by skeen on Tue Jul 16, 2013 4:59 am, edited 1 time in total.
// Skeen
// Developing a yet unnamed microkernel in C++14.
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: System calls triggered by page faults

Post by Antti »

Combuster wrote:That gives the next problem, a pagefault doesn't show you the value that was attempted to be read or written, only the target address and the instruction that caused it.
A real problem. I already mentioned it in the original post (when using C). In assembly, something like "mov [address], eax" would be possible. Then the page-fault handler would have to take that eax value (or whatever register would be) and store it. However, it is starting to look like the traditional system calls. It does not seem good if we had to care how the value is written. The simplicity and elegancy are lost.

Maybe this is not possible to implement on x86-platforms but it does not mean the idea is totally bad.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: System calls triggered by page faults

Post by Kevin »

Well, you could write an instruction emulator to "parse" the parameter from the faulting instruction and then skip the instruction before reentering userspace. This way you also get rid of the race conditions.

Possible? Absolutely. Sane? Not really.
Developer of tyndur - community OS of Lowlevel (German)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: System calls triggered by page faults

Post by Combuster »

skeen wrote:Assuming you get the first one, when creating a process, it's just a matter of implementing the allocation of a sys call page-fault address as a part of the create thread method, I guess.
And then what? Add an additional argument to every possible function in the app so you can pass it through?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply