eolas/neuron/dc239556-fd21-4147-b56e-3d8b474984ad/Relation_between_kernel_and_CPU.md

47 lines
2.3 KiB
Markdown
Raw Normal View History

2024-12-09 18:34:15 +00:00
---
tags:
- CPU
- computer-architecture
- operating-systems
---
# Relation between the kernel and CPU
It can be confusing to understand how the kernel and CPU interact with one
another. Whilst it is true to say the kernel mediates between the hardware and
user processes, this led me to think that the kernel was somehow 'over and
above' the actions of the CPU. This is wrong and if you think about it, how
could this be since any process, kernel included, requires the processor? The
kernel is executed by and loaded into the CPU just like any other instruction in
the
[fetch, decode, execute cycle](Fetch_decode_execute.md)
of the CPU.
However as a process, the kernel is the 'first amongst equals' given that it is
the core embodiment of the operating system. At boot time, the kernel is
injected into memory and at this point the CPU _looks to this address_ in memory
in order to source its first instruction. That's how the kernel gets going. It
is the first instruction that the CPU fetches and this is what allows the kernel
to play its mediatory role. However most of the fetch, decode, execute cycles of
the CPU take place independently of the kernel.
![](static/kernel-cpu-interaction.svg)
> Fetch decode and execute refer to processor pipeline stages. They occur
> automatically as part of normal processor operation, the kernel doesnt
> generally have any direct control over how that happens...When the boot loader
> loads the kernel, it points the CPUs program counter to an entry point in the
> kernels code. From there, the CPU will continue executing (fetching decoding
> and executing) kernel code until you point the program counter to a userspace
> program, where it will continue executing userspace code until an interrupt
> points the program counter back into kernel code. Thats basically what the
> CPU will be doing from boot up to shut down; switching between executing
> kernel code and executing userspace code
> ([Reddit]())
This is because of [context switching](), when the CPU is running its cycle, the
kernel is idle waiting for it to complete. Once control switches back to the
kernel it can then run its next duty which will be either process, memory or
device management or system calls. Then it will hand control over to either the
processor or user space.