|Applying C - Kernel Mode, User Mode & Syscall|
|Monday, 01 July 2019|
Page 2 of 2
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:
Now available as a paperback or ebook from Amazon.
Applying C For The IoT With Linux
Also see the companion book: Fundamental C
Remote C/C++ Development With NetBeans
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.
or email your comment to: email@example.com
|Last Updated ( Monday, 01 July 2019 )|