1

What I specifically want to know here is: Irrespective of the underlying hardware it is on how Linux kernel manages to work on all different PCs.

Or in general how any kernel can (Microsoft's NT kernel ,mac os and Linux ) work on any PC irrespective of PCs hardware configuration.

To explain my doubt here is a scenario: compared to PC-2, PC-1's bluetooth, keyboard, wifi, etc are of different configuration and developed by different manufacturer. To make all these hardware be functional, I guess kernel should have hardware specific code to enable those hardware. I believe kernel has device driver layer where it should enable these hardware? Or it just loads device specific firmware from somewhere else? Where exactly hardware specific code lies in kernel? Also while clearing my doubt I came across HAL, BSP. What are these and where these layers lies in kernel?

  • Why shouldn't it work? – user253751 Mar 11 at 18:23
  • 1
    And it's not irrespective of the platform. Tons of options have to be set specifically for your platform or else the kernel won't work. My smartphone's kernel won't work on my PC. But all PCs are designed to run the same kernel so my PC's kernel probably works on your PC. – user253751 Mar 11 at 18:24
  • @user253751, all PCs are sufficiently similar so the above works (thanks, Microsoft virtual monopoly! Apple machines used to be radically different, for one.). – vonbrand Mar 13 at 22:53
5

What I specifically want know here is irrespective of the underlying hardwares it is on how Linux kernel manages to work on all different pcs

Before anything else, the kernel can run on any PC as long it's compiled for that specific architecture. With your general purpose computers, the architecture tends to be the same (x86_64/amd64) but this changes when you're dealing with embedded devices.

Device-tree

Other than compilation, the kernel itself is hardware agnostic. I give you the device-tree!

The way the linux kernel takes care of non-discoverable hardware is through the device-tree. This tells the kernel information about the CPU, memory, buses, devices connected over I2C, SPI and what not. Instead of the device layout being hard-coded, it can be changed through the device-tree. It's all really magical when you understand it. The device-tree also tells the kernel which driver it should use for a specific device through the compatible parameter.

The device-tree can be stored in all sorts of different ways depending on how the kernel was compiled but generally the hierarchy can be seen under /proc/device-tree or /sys/firmware/devicetree/base.

For things like USB devices, it's all taken care of by the USB driver. The kernel has basic code for controlling things like keyboards and mice. Some vendors have custom drivers which is then shipped with the device.

Modules

For drivers, the linux kernel uses something called kernel modules. All the modules can be found under /lib/modules. There are two types of modules: loadable modules and built-in modules. You can view all the loaded modules using the lsmod command. When the kernel finds a certain device in the device-tree or even when you plug a device into a peripheral, it'll look to see if there's a module it can load for that device and the module becomes the driver for that device.

The nice thing about loadable modules is that it keeps the kernel image smaller. The modules can be compiled separately. Built-in modules get compiled into the kernel and thus increases the actual image size. You can also compile and load modules into the kernel while it's running but that's a whole different topic.

BSP

You get into BSP's (Board Support Package) when a vendor ships the kernel and a bunch of specific modules for their own hardware (this includes the processor and connected hardware). Sometimes vendors make changes to existing modules or ship custom modules with the kernel. Some vendors have changes made to the kernel itself and that whole custom package becomes the BSP. This happens a lot in the embedded world.

HAL

HAL (Hardware Abstraction Layer) can be synonymous to BSP. I haven't seen it being used all that much in the linux world but when you get into microcontrollers, it's used quite often. Essentially, the HAL is a bunch of libraries used to access the hardware, making interfacing with the hardware a lot easier. For example, with a wifi chip, it'll give you basic routines to connect to an AP, scan the network, create an access point e.t.c. It kinda acts a driver in that sense.

| improve this answer | |
  • Device tree is only used for hardware which can’t be detected, isn’t it? For example on the PCI bus, hardware is discovered without a device tree. – Stephen Kitt Mar 11 at 19:11
  • Yes you're right. The device-tree is used for non-discoverable hardware. I will make that edit – hakskel Mar 11 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.