Applying C - Kernel Mode, User Mode & Syscall
Monday, 01 July 2019
Article Index
Applying C - Kernel Mode, User Mode & Syscall
Your Code In Kernel Mode

Your Code In Kernel Mode

Now that we know how to call kernel mode functions, we can move on to the next question, does your code need to run in kernel mode? If you are used to running code with no operating system, or perhaps a minimal runtime, then you might think that kernel mode is essential. This isn't the case. Moving to kernel mode is a lot more work than coding in user mode. For one thing, you don't have access to the libraries that you are so familiar with, and they don't have easy access to floating point. They do, however, have complete access to all of the hardware; they can respond to interrupts and they have a higher level of execution privilege, but they can still be interrupted. Even a kernel function doesn't have the machine all to itself.

If you still think that kernel code is an attractive proposition you need to know that you can achieve nearly as much in user space with the right techniques, as explained later in this book. My advice is to always start implementing your project in user space and only transition it to kernel mode if there is no other way to meet its requirements.

How do you create kernel mode code?

There are two routes - one hard and one easier. The "hard" way is to add to the kernel by compiling it and including your own syscalls. The problem with this is that you create a non-standard operating system and face the task of maintaining it through subsequent kernel releases.

A simpler way of doing the same job is to write a kernel module. A kernel module is close to what would be called an installable driver in another operating system. A module can be installed by the user via the insmod or more commonly the modprobe command. You can find out about modules using the lsmod command and you can remove a module using the rmmod command. Modules are completely general and don't have to be implemented as drivers, but the way that they interact with the kernel is fixed - there has to be an initialization function and a cleanup function. Once you move beyond this you have to learn how to implement the specific type of module you are trying to create – a character device driver, for example. Getting into kernel mode is just the start of the story, you also have to learn how to do what you want within it.

Learning to write kernel modules is a worthwhile, but tough, education and it would fill a book on its own, so for the remainder of this book we concentrate on achieving many of the same results but in user space.

In book but omitted from this extract:

  • Security



  • Most operating systems have different levels of execution. In Linux the kernel code can do more or less anything, but user space code is restricted in various ways.

  • One of the many services the operating system provides is multi-tasking. It allows multiple user space programs to be loaded into memory and it allocates some processor time to each so as to make it look as if multiple programs are running at the same time.

  • You can access the services provided by the operating system by making system calls or sys calls.

  • The glibc library allows you to avoid having to implement your own sys calls by providing functions, wrappers, which do the same job.

  • You can write your own kernel extensions as installable modules, but this isn't usually necessary and it is more complicated.

  • Security is a big problem for the IoT and embedded programs in general. Conventional security often gets between your program and the hardware and does little to improve real security.

  • IoT and embedded security is more about checking what inputs are being received by sanitizing the data, identifying its source using passwords or key files and encrypting data being transported.


Now available as a paperback or ebook from Amazon.

Applying C For The IoT With Linux

  2. Kernel Mode, User Mode & Syscall
  3. Execution, Permissions & Systemd
    Extract Running Programs With Systemd
  4. Signals & Exceptions
    Extract  Signals
  5. Integer Arithmetic
    Extract: Basic Arithmetic As Bit Operations 
  6. Fixed Point
    Extract: Simple Fixed Point Arithmetic
  7. Floating Point
  8. File Descriptors
    Extract: Simple File Descriptors 
    Extract: Pipes 
  9. The Pseudo-File System
    Extract: The Pseudo File System 
  10. Graphics
    Extract: framebuffer 
  11. Sockets
    Extract: Sockets The Client
    Extract: Socket Server 
  12. Threading
    Extract:  Condition Variables
    Extract:  Deadline Scheduling
  13. Cores Atomics & Memory Management
  14. Interupts & Polling
    Extract: Interrupts & Polling ***NEW
  15. Assembler
    Extract: Assembler 

Also see the companion book: Fundamental C





Related Articles

Remote C/C++ Development With NetBeans

Raspberry Pi And The IoT In C

Getting Started With C/C++ On The Micro:bit

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.


Imagine Cup World Champions 2023

Three teams, the top team from each of the contest's three regions took part in the ultimate stage of the Imagine Cup and travelled to Seattle to make their final 3-minute pitches to the panel of thre [ ... ]

Learn With The Spring Academy

VMWare has launched a project-based learning platform that provides the Spring community with the knowledge and experience necessary to stay ahead of the curve.

More News





or email your comment to:

Last Updated ( Monday, 01 July 2019 )