What is the best way for user programs to access the kernel?

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
computertrick
Member
Member
Posts: 71
Joined: Wed May 29, 2013 1:07 pm

What is the best way for user programs to access the kernel?

Post by computertrick »

Lets say I have a printf function in my kernel what would be the most wise way for the user program to communicate with it. Lets assume they are using C. In the header that contains printf I could have an interrupt linking to the kernel? Or are their better ways of guiding the user program to the correct routine.

Code: Select all

#ifndef STDIO_H
#define STDIO_H
#include <interrupts.h>
void printf(const char *s, ...)
{
    Int(0x80);
}
#endif
1100110100010011
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: What is the best way for user programs to access the ker

Post by neon »

There is no "best" way. This is one possible method though that might provide some ideas. Note _write and _service_request are system calls defined in your system API that provide the interface between the running processes and kernel or executive software.

Process -> _printf -> _fwrite(stdout) -> _write -> _service_request -> sysenter -> kernel sends virtual driver request. The virtual driver emulates a standard console window and displays the text. It may also be a real console driver.

Basically, you want to separate the C run time library calls from the System API calls. The actual call to kernel land can also be separated from the system API.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
computertrick
Member
Member
Posts: 71
Joined: Wed May 29, 2013 1:07 pm

Re: What is the best way for user programs to access the ker

Post by computertrick »

could you provide more information?
1100110100010011
windows8
Member
Member
Posts: 33
Joined: Sat Feb 23, 2013 3:52 am

Re: What is the best way for user programs to access the ker

Post by windows8 »

I think you can write this macro to call a syscall with three parameters:

Code: Select all

#define __syscall3(ret,name,a1,__a1,a2,__a2,a3,__a3)  \
   ret name(a1 __a1,a2 __a2,a3 __a3) \
   { \
      unsigned long __ret; \
      asm volatile( \
         "int $0xff" \
         : "=a" (__ret) \
         : "a" (__NR_##name),"b" ((unsigned long)__a1), \
           "c" ((unsigned long)__a2),"d" ((unsigned long)__a3) \
      ); \
      return (ret)__ret; \
   }

Then you can write this code below easiler (the "write" syscall):

Code: Select all

#define __NR_write 0x3 

__syscall3(unsigned long,write,int,fd,const void *,buf,unsigned long,size);
In your header file:

Code: Select all

unsigned long write(int fd,const void *buf,unsigned long size);
At last,you are able to write "printf":

Code: Select all

int printf(const char *string,...)
{
   char buf[512];
   /*.....*/
   int n = vsnprinf(buf,sizeof(buf),string,va_args);
   return write(stdout,buf,n);
}
User avatar
hometue
Member
Member
Posts: 100
Joined: Thu Dec 19, 2013 1:40 am
Location: Asia, Singapore

Re: What is the best way for user programs to access the ker

Post by hometue »

Just curious, if you have the same function in the C library and kernel which one would be faster/more efficient to use?
CookieOS. Want a cookie? Its only black and white for now though, probably as bad as my baking skills.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: What is the best way for user programs to access the ker

Post by bluemoon »

hometue wrote:Just curious, if you have the same function in the C library and kernel which one would be faster/more efficient to use?
Technically it runs at same speed.
However, the concern is on the overhead of context switch, sanity checks, extra processing and wrappers.
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: What is the best way for user programs to access the ker

Post by Combuster »

windows8 wrote: char buf[512];
/*.....*/
int n = vsnprinf(buf,sizeof(buf),string,va_args);
Incoming buffer overflow! :wink:
"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
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: What is the best way for user programs to access the ker

Post by neon »

computertrick wrote:could you provide more information?
Thing is, I shouldn't need to. It is something that is easy to pick up from user mode programming.

Basically, no CRT routine should be calling the operating system or executive directly; _printf in particular should be calling _fwrite (stdout) which in tern should be calling your system API _write method that which can issue the operating system call and traps into kernel land. The kernel should issue a driver request to the output console virtual driver to display the message. Routines like _brk may call a system API _heap_alloc routine that which may issue the operating system call.

We also recommend the use of sysenter over software interrupts. The routine that calls the operating system should not be easily accessible by user mode software.

In other words, we have multiple layers:

1. Process, calls CRT;
2. C Run Time Library (CRT) calls System API;
3. System API calls _service_request;
4. _service_request traps into kernel land and calls the operating system;
5. Operating system performs the task or hands it off to some other process or driver to handle it.

The System API acts as the interface between the CRT/Process and the underlying operating system call. System API calls may either be real functions or forwarding functions to a look up table used by _service_request to issue the actual software interrupt or sysenter call.

Note that although this setup is fairly complicated, you never requested the easiest method.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
rdos
Member
Member
Posts: 3306
Joined: Wed Oct 01, 2008 1:55 pm

Re: What is the best way for user programs to access the ker

Post by rdos »

There is a far easier method, which unfortunately on Intel processors (but not as much on AMD processors) have received no optimization attempts and thus typically is slower than the whole bunch of decoding needed for a central kernel entry-point. The method uses one call-gate per syscall and thus enters directly into the service routine in the device driver.
Post Reply