Larry
Larry

Reputation: 1765

Kernel - Linux - Where does the kernel talks to the cpu?

Context:

Linux 64. Intel Core 2 duo.

Question:

Where does the Linux kernel "communicate" with the cpu ? I read the source code for scheduler but could not understand how they communicate and how the kernel tells the cpu that something need to be processed.

I understand that there are run queues, but isn't there something that enables the kernel to interrupt the cpu via the bus ?

Update

It expands my initial questions a bit : How can we tell the cpu where the task queues are ?

Because the cpu has to poll something, and i guess we tell it at some point. Missed that point in the kernel code.

Upvotes: 0

Views: 1828

Answers (2)

ElderBug
ElderBug

Reputation: 6145

I will try to write a simplified explanation of how it works, tell me if anything is unclear.

A CPU only do one thing : execute instructions. It will start at a predefined address, and execute. That's all. Sometime you can have an interrupt, that will temporarily make the CPU jump to another instruction.

A kernel is a program (=a sequence of instructions) that will make it easy to execute other programs. The kernel will do his business to setup what it needs. This often include building a list of process to run. The definition of "process" is totally up to the kernel because, as you know, the CPU only do one thing.

Now, when the kernel runs (being executed by the CPU), it might decide that one process needs to be executed. To do so, the kernel will simply jump to the process program. How it is done doesn't matter, but in most OSes, the kernel will map a periodic interrupt (the CPU will periodically jump) to a function that decide which process to execute and jump to it. It isn't required, but it is convenient because programs will be forcefully "interrupted" periodically so others can also be executed.

To sum up, the CPU doesn't "know" anything. The kernel runs, and will jump to other process code to make them run. Only the kernel "knows".

Upvotes: 6

Aaron Digulla
Aaron Digulla

Reputation: 328594

The Linux kernel is a program. It doesn't "talk" to the CPU as such; the CPU has a special register, the program counter (PC), which points to the current execution of the kernel which the CPU is processing.

The kernel itself contains many services. One of them manages the task queues. Each entry in the task queue contains information about the task. One such information is the CPU core on which the task is running. When the kernel decides that the service should do some work, it will call it's functions. The functions are made up from instructions which the CPU interprets. Most of them change the state of the CPU (like advancing the PC, changing register values, setting flags, enabling/disabling CPU cores, ...).

This means the CPU isn't polling anything. Depending on the scheduler, different strategies are used to process the task queue. The most simple one is timer based: The kernel install a timer interrupt (i.e. it writes the address of an interrupt handler somewhere plus it configured the timer to cause an interrupt every few milliseconds).

The handler then looks at the task queue and decides what to do, depending on its strategy.

Upvotes: 2

Related Questions