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

 

Summary

  • 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

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

Also see the companion book: Fundamental C

<ASIN:1871962609>

<ASIN:1871962463>

<ASIN:1871962617>

<ASIN:1871962455>

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.

Banner


Hadoop 3.3 Adds ARM Support
30/07/2020

Hadoop has been updated with improvements including support for ARM architectures, Java 11 runtime support, and S3A enhancements.



Ants Show The Way To Manage Vehicles
27/07/2020

Before you start fantasizing about ants sitting round a table planning how to route vehicles I'd better tell you that its ant algorithms that are being charged with the task, not the real thing. Of co [ ... ]


More News

graphics

 



 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Monday, 01 July 2019 )