sorry if this is too broad of a question but what the heck is a kernel, i know it sits between the hardware and the OS but what does it do even

this is not a bad question

the kernel is arguably the most complex software entity in the modern computer. it is software that controls the hardware-software interface

think of it like this: your computer is just a bunch of hardware wired together. at minimum it has a CPU, memory (RAM), and some kind of disk (nonvolatile storage). memory & disk are similar in that they both store 1′s and 0′s, a CPU accepts those 1′s and 0′s from memory as input and outputs 1′s and 0′s back to memory. this is all a computer is

this hardware operates according to global and well-known standards, meaning you can write software (1′s and 0′s) to defined places and expect the hardware to pick it up and run it according to these standards. this is your entry point

like i said before, all a disk does is store 1′s and 0′s when the computer is not powered on. that is *all* it does. memory does the same thing when the computer is running but that is *not* all it does. like a disk, you have some address buffer for specifying where you want to read or write data. nowadays this buffer is 64 bits wide, meaning you can express any address from 0 to 9223372036854775807. assuming a single 8-bit byte resides at each address, the maximum size of your disk would be 8388608 terrabytes (!)

obviously, there is no disk that big & you can only properly use addresses from 0 to <disk size>. memory works the same way, however

since the address space obviously outweighs the space needed by any sort of memory, there are a lot of unused addresses. with disks, this extra address space isn’t really used for much. but for memory it is: those extra address spaces are detected forwarded to system busses meaning the bits you read and write at those addresses are translated into logical voltages sent to the pins on hardware. this is how software/hardware interact. it’s a beautiful thing

finally, the kernel refers to the software that does this stuff. the kernel contains drivers which are bodies of software that mediate interaction with one discrete block of hardware (controller drivers). for example, a driver for a disk controller read/writes to the address space specific to a disk, sending and interpreting electrical signals comprising SATA messages passed between your motherboard’s disk controller and your hard drive’s internal circuitry. it presents an interface that simplifies interaction with that device enormously: instead of having to tap out all those messages yourself, you simply call the read($address) or write ($addr, $data) functions that the driver presents

so at its core, the kernel is a collection of these controller drivers. there are also higher-level drivers that generalize and coalesce the lower-level controller drivers as to present a consistent interface across hardware. what i mean by this is, for example, the controller driver i am trying to fix right is the one meant to control the i.MX6 SoC’s sdhc controller (the hardware that deals with SD cards). this is an ARMv7 chip of a very specific sub-architecture whose sdhc controller is unique (and really stupid). the driver code i’m writing for this will only ever be relevant or work for this specific chip (machine dependent), but the driver plugs in to a higher-level driver that generalizes its functionality (machine independent)

to clarify, the driver im fixing is called ‘imxesdhc’. it is specific to the i.MX6 subarchitecture. it plugs into a driver called ‘sdmmc’ which is what every SD or MMC card controller will plug into across all systems and all architectures. this is how you write code that will work on many systems without targeting them each individually

so at this point, we know the kernel controls hardware on an electrical level through the bus-memory interface, and coalesces each individual hardware’s functionality into a generalized interface consistent across machines. there is one more thing that it does:

on my computer, right now, there are many individual programs running:


this is mysterious: there is only one CPU in my computer which itself is only capable of running 8 discrete sequences of executions at once. there are a lot more than 8 programs there. how is this happening?

it is because the kernel is responsible for mediating userspace, or the part of the operating system you actually see and deal with. when you go to run a program, that program makes system calls (special instructions to talk to the kernel) which register it as a process with the scheduler

you see

all the processes you see above are not actually running simultaneously. that would be impossible (only 8 cores, remember?). what is actually going on is that each proc is running for a handful of milliseconds or so before the CPU stops it and switches it out for another one. this happens so fast that it creates the illusion of many procs running simultaneously. this is done by the scheduling subsystem, a part of the kernel mediating userspace.

this kernel part also does a lot of other stuff to meditate userspace, such as present interprocess IO schemes for programs to talk in-between each other. it handles all the standard library calls for opening (for example) a network connection or whatever and translates & routes them down the proper driver path that eventually ends with your computer’s specific machine-dependent drivers that are sending electrical signals to hardware

of course, it also encapsulates programs run in userspace according to sane boundries, i.e. a program run by an arbitrary user cannot arbitrarily allocate 90% of my memory. it prevents userspace programs from reading or interacting with code running in kernel space, or address spaces it does not have access to (memory addresses in userspace are virtual addresses that must be translated through an MMU/TLBs)

the kernel also presents the filesystem and user hierarchy to userspace, allowing you to interact with the disk and a general and machine-independent way. it stops you from trying to read you don’t have access to and it allows you to set the permissions for files you own according to whatever policy

kernels are very complicated. i touched on the basics here but i encourage you to send me asks about more specific parts of them