Communicating with kernel

According to Wikipedia, an operating system usually segregates virtual memory into kernel space and user space. Kernel space is strictly reserved for running the kernel, device drivers and kernel extensions. In most operating systems, kernel memory is never swapped out to disk. User space is the memory area where all user mode applications work and this memory can be swapped out when necessary.

A user application cannot access kernel space directly and similarly kernel code cannot access the user space without checking whether the page is present in memory or swapped out. Even though these can not access each other directly, user and kernel space can communicate with each other using variety of ways. This is an effort to summarize those used under Linux:

  • System calls
  • As explained in manual of syscall(2), The system call is the fundamental interface between an application and the kernel.

    A system call is a request by a running task to the kernel to provide some sort of service on its behalf. In general, the kernel services invoked by system calls comprise an abstraction layer between hardware and user-space programs.

    The way system calls are handled is up to the processor. Usually, a call to the kernel is due to an interrupt or exception; in the call, there is a request to execute something special.

    Actual code for the system_call entry point can be found in /usr/src/linux/kernel/sys_call.S and the code for many of the system calls can be found in /usr/src/linux/kernel/sys.c. Code for the rest is distributed throughout the source files. Some system calls, like fork, have their own source file (e.g., kernel/fork.c).

    Pros: Well documented, easy to use interface.

  • ioctl
  • Even though ioctl is one of the system calls, its usefulness tempts me to talk about it separately. Sometimes referred to as Swiss army knife, ioctl stands for input/output control and is used to manipulate a character device via a file descriptor. ioctl is a catch-all function that takes a device, a request and a variable number of other parameters. The devices, requests and other parameters are then defined in header files.

    Pros: Provides hardware level access to programmers.
    Cons: device and requests are poorly documented and platform specific.

  • procfs
  • The /proc file system (procfs) is a special file system in the Linux kernel. It is a virtual file system, i.e. not associated with a block device but exists only in memory.

    The files in the procfs are there to allow userland programs access to certain information from the kernel (like process information in /proc/[0-9]+/), but also for debug purposes (like /proc/ksyms).

    Pros: Uniform interface to retrieve process information from command line.
    Cons: Difficult to figure out required information as too much miscellaneous information exposed through single file system.

  • Sysfs
  • Sysfs is a virtual file system provided by the 2.6 Linux kernel. Sysfs exports information about devices and drivers from the kernel device model to userspace, and is also used for configuration.

    Sysfs is designed to export the information present in the device tree which would then no longer clutter up procfs.

    For each object added in the driver model tree (drivers, devices including class devices) a directory in sysfs is created.

    The parent/child relationship is reflected with subdirectories under /sys/devices/ (reflecting the physical layout). The subdirectory /sys/bus/ is populated with symbolic links, reflecting how the devices belong to different busses. /sys/class/ shows devices grouped according to classes, like network, while /sys/block/ contains the block devices.

    Pros: Uniform interface to retrieve/update device specific information

  • Netlink sockets
  • Netlink socket is a special IPC used for transferring information between kernel and user-space processes. It provides a full-duplex communication link between the two by way of standard socket APIs for user-space processes and a special kernel API for kernel modules.

    Netlink socket uses the address family AF_NETLINK.

    The standard socket APIs-socket(), sendmsg(), recvmsg() and close()-can be used by user-space applications to access netlink socket.

    Pros: It is a nontrivial task to add system calls, ioctls or proc files for new features; with the risk of polluting the kernel and damaging the stability of the system. Netlink socket is simple, though: only a constant, the protocol type, needs to be added to netlink.h.

  • relay
  • Relayfs is, yet another virtual filesystem implemented by the kernel; it must be explicitly mounted by user space to be available. Kernel code can then create a relay with relay_open(); it will show up as a file under relayfs.

    User space can then open the relay and employ all of the usual file operations - including mmap() and poll() - to exchange data with the kernel.

    To an application, a relayfs file descriptor looks much like a Unix-domain socket, except that the other end is a piece of kernel code rather than another process.

    The interface on the kernel side is a bit more complex. The expected relay_read() and relay_write() functions exist and can be used to move data to and from user space.

    But relayfs also exposes much of the internal structure to kernel code that needs to know about it. So special-purpose code can obtain a pointer into the relayfs buffer and copy data there directly.

    Pros: Easier to pass large amount of information to and from kernel space.

  • debugfs
  • debugfs is a in-kernel filesystem just for putting debugging stuff some place other than proc and sysfs, and which is easier than both of them to use.

    debugfs is meant for putting stuff that kernel developers need to see exported to userspace, yet don't always want hanging around.

    To create a file using debugfs the call is just:
    struct dentry *debugfs_create_file(const char *name, mode_t mode, struct dentry *parent, void *data, struct file_operations *fops);

    To export a single value to userspace:
    struct dentry *debugfs_create_u8(const char *name, mode_t mode, struct dentry *parent, u8 *value);

    That's it, one line of code and a variable can be read and written to from userspace.

    Pros: Shortens development time with debugging information easily available.

  • Firmware loading
  • While most computer peripherals work right "out of the box," some will not function properly until the host system has downloaded a blob of binary firmware.

    The end result is that the recommended way of dealing with devices needing firmware downloads is to have a user-space process handle it.

    In the new scheme, a device driver needing firmware for a particular device makes a call to:
    int request_firmware(struct firmware **fw, const char *name, struct device *device);

    Here, name is the name of the relevant device, and device is its device model entry. This call will create a directory with the given name under /sys/class/firmware and populate it with two files called loading and data. A hotplug event is then generated which, presumably, will inspire user space to find some firmware to feed the device.

    The resulting user-space process starts by setting the loading sysfs attribute to a value of one. The actual firmware can then be written to the data file; when the process is complete, the loading file should be set back to zero. At that point, request_firmware() will return to the driver with fw pointing to the actual firmware data. The user-space process can chose to abort the firmware load by writing -1 to the loading attribute.

    When the driver has loaded the firmware into its device, it should free up the associated memory with:
    void release_firmware(struct firmware *fw);

    Though, this is specific to firmware loading, but is sometimes used for passing large amount of data to the kernel space.

    Comment viewing options

    Select your preferred way to display the comments and click "Save settings" to activate your changes.
    amedeus1977 Says:
    Tue, 2010-07-27 02:50,best DS flashcarts,Flash Memory,video Games and Cell Phone online store, Provide the latest electronic product, the hottest price and best service. We will give you the best shopping experience.

    mkaymer Says:
    Thu, 2010-08-19 15:50

    V8Ri2A krmshvjvuhis, [url=]smpngeikzyyz[/url], [link=]riaotldbxugj[/link],

    mkaymer Says:
    Sat, 2010-08-21 11:34

    buy biaxin 8-) effexor 428938 paxil kva

    mkaymer Says:
    Mon, 2010-08-23 01:08
    mkaymer Says:
    Wed, 2010-08-25 09:50
    mkaymer Says:
    Thu, 2010-08-26 03:08
    mkaymer Says:
    Thu, 2010-08-26 11:00

    denavir =( zestril %((( indocin lkfm

    mkaymer Says:
    Fri, 2010-08-27 18:50
    mkaymer Says:
    Sat, 2010-08-28 23:08
    mkaymer Says:
    Sun, 2010-08-29 01:18
    dlrudisha Says:
    Thu, 2010-09-02 03:40
    buckstab Says:
    Thu, 2010-09-16 01:00
    faygene Says:
    Fri, 2010-09-24 02:14
    faygene Says:
    Fri, 2010-09-24 02:42
    sunxin123 Says:
    Mon, 2011-02-28 09:16

    For businessmen who desire to impress guys louis vuitton and girls with their awesome taste and amazing confidential styles, it is instead essential to pick the right briefcase which serves since the important factors to finish one's company look. Nowadaysreplica louis vuitton, a briefcase no extended features as one thing just identical to some useful carrier, but a complete great offer more importantly, as an indispensable style product which louis vuitton handbags could include style design to one's expert company image.

    lloverxer Says:
    Tue, 2011-03-01 20:49

    The Best Deals on LCD Monitors, Flat Panel Monitor, Cheap Computer Monitors, Monitors 14"-24"Inches and Up; Viewsonic Monitors, Soyo, Acer, Dell, and More offers the best prices on LCD Monitors, Cheap LCD Monitors, Best LCD Monitors, LCD Computer Monitors, LCD Display, LCD Computer Monitors with lcd monitor

    davidhynes91 Says:
    Thu, 2011-03-03 10:04

    I just came across your website from google and reading your article is very interesting. I hope people will visit and read your articles.

    otisrobinson19 Says:
    Fri, 2011-03-11 10:18

    I’m not much into reading, but somehow I got to read many articles in your webpage. Its fantastic how interesting it is for me to visit you very often.

    patrickirwin79 Says:
    Tue, 2011-03-15 11:46

    It is great to have the opportunity to read a good quality article with useful information on topics that plenty are interested on.
    debt consolidation

    beats666 Says:
    Fri, 2011-04-29 05:42

    Beats Headphones are on Hot Sale NOW! High Quality, Competitive Price! Save Big on Monster Beats This Month Incredible Low Prices + Free Shipping & Fast Delivery. Sound is fashion, Monster Headphones are holistically designed to deliver the soundtrack of your life with clarity and power, as well as satisfy your passion for fashion. Do not hesitate to enjoy now!

    Post new comment


    • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
    • Lines and paragraphs break automatically.