Integrating Linux kernel module inside Linux kernel source

If you have already followed “Writing first Linux kernel Module and understanding Kernel module compilation, loading and unloading”, you might have already understood how to build simple kernel module for x86 which we compiled from outside kernel source code for a x86 system which was already running with Linux kernel, Now, Lets try to integrate … Read more

Building kernel module as part of kernel compilation procedure

If you have already followed “Writing first Linux kernel Module and understanding Kernel module compilation, loading and unloading”, you might have already understood how to build simple kernel module for x86 which we compiled from outside kernel source code for a x86 system which was already running with Linux kernel, Now, Lets try to integrate … Read more

Implementation of Interrupt Handler (IRQ) in Linux kernel driver

Following driver implements, how we can allocate an interrupt line using request_irq kernel API and how to implement an interrupt handler ( e.g. my_interrupt_handlerin this example ) and how to pass a structure with some values from module to IRQ handler. This code is almost self explainatory with added comments inline. If you want to … Read more

Passing command line Arguments / Parameters to Linux kernel module

In Our previous post, “Writing first Linux kernel Module and understanding Kernel module compilation, loading and unloading” We learned how to write a simple Linux kernel module. Lets see an example of how to pass the loading time parameters to the kernel module. The load time parameter passing is very helpful to enable / disable … Read more

How initrd is mounted by Linux kernel

Lets first try to understand what are the ways to mount the root file system, Altough the documentation from Documentation/early-userspace/README inside kernel source code is bit outdated, it is still has valid information as below, The kernel has currently 3 ways to mount the root filesystem: a) all required device and filesystem drivers compiled into … Read more

How kernel images are built / compiled

/bin/bash scripts/link-vmlinux.sh arm-linux-gnueabihf-ld -EL -p –no-undefined -X –pic-veneer –build-id LD vmlinux.o scripts/mod/modpost -m -a -o ./Module.symvers -S vmlinux.o GEN .version /bin/bash ./scripts/mkcompile_h include/generated/compile.h \“arm” “y” “” “arm-linux-gnueabihf-gcc -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -std=gnu89 -fno-dwarf2-cfi-asm -mabi=aapcs-linux -mno-thumb-interwork -mfpu=vfp -funwind-tables -marm -D__LINUX_ARM_ARCH__=6 -march=armv6k -mtune=arm1136j-s -msoft-float -Uarm -fno-delete-null-pointer-checks -O2 –param=allow-store-data-races=0 -Wframe-larger-than=1024 -fno-stack-protector -Wno-unused-but-set-variable -fomit-frame-pointer -fno-var-tracking-assignments … Read more

Kernel startup entry point / how Linux kernel boots

We will consider our hardware platform as ARM, so the kernel startup entry point code is at arch/arm/kernel/head.S This is normally called from the decompressor code. The requirementsare: MMU = off, D-cache = off, I-cache = dont care, r0 = 0,r1 = machine nr, r2 = atags or dtb pointer. This code is mostly position … Read more

How Linux Kernel Module Loading works ?

The kernel bus drivers probe for devices. For every detected device, the kernel creates an internal device structure and the driver core sends an event to udev. Devices identify themselves by an id, which tells what kind of device it is. Usually these id’s consist of vendor and product id and other susbsytem specific values. … Read more

Understanding Linux kernel likely and unlikely macros

Linux kernel defines likely and unlikely macros in kernel source code include/linux/compiler.h as # define likely(x) __builtin_expect(!!(x), 1) # define unlikely(x) __builtin_expect(!!(x), 0) As per GCC documentation, __builtin_expect has been described as, [bash] Built-in Function: long __builtin_expect (long exp, long c) You may use __builtin_expect to provide the compiler with branch prediction information. [/bash] if … Read more