简体   繁体   English

内核-Linux-内核在哪里与cpu通信?

[英]Kernel - Linux - Where does the kernel talks to the cpu?

Context: 内容:

Linux 64. Intel Core 2 duo. Linux64。英特尔酷睿2双核处理器。

Question: 题:

Where does the Linux kernel "communicate" with the cpu ? Linux内核在哪里与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. 我阅读了调度程序的源代码,但无法理解它们如何通信以及内核如何告知CPU需要处理某些内容。

I understand that there are run queues, but isn't there something that enables the kernel to interrupt the cpu via the bus ? 我知道有运行队列,但是难道不存在使内核能够通过总线中断cpu的东西吗?

Update 更新资料

It expands my initial questions a bit : How can we tell the cpu where the task queues are ? 它扩展了我最初的问题了一下:我们如何判断CPU,可在任务队列?

Because the cpu has to poll something, and i guess we tell it at some point. 因为cpu必须轮询某些内容,所以我想我们会在某个时候告诉它。 Missed that point in the kernel code. 在内核代码中错过了这一点。

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. CPU只做一件事:执行指令。 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. 有时您会遇到一个中断,这将使CPU暂时跳转到另一条指令。

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. “进程”的定义完全取决于内核,因为如您所知,CPU只做一件事。

Now, when the kernel runs (being executed by the CPU), it might decide that one process needs to be executed. 现在,当内核运行时(由CPU执行),它可能会决定需要执行一个进程。 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. 它的完成方式无关紧要,但是在大多数操作系统中,内核会将一个定期中断(CPU会定期跳转)映射到一个函数,该函数决定执行哪个进程并跳转到该进程。 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. 综上所述,CPU并不“知道”任何东西。 The kernel runs, and will jump to other process code to make them run. 内核运行,并将跳转到其他进程代码以使其运行。 Only the kernel "knows". 只有内核“知道”。

The Linux kernel is a program. Linux内核是一个程序。 It doesn't "talk" to the CPU as such; 它不会像这样“与” CPU“对话”。 the CPU has a special register, the program counter (PC), which points to the current execution of the kernel which the CPU is processing. CPU有一个特殊的寄存器,即程序计数器 (PC),它指向CPU正在处理的内核的当前执行。

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. 这样的信息之一就是在其上运行任务的CPU内核。 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. 这些功能由CPU解释的指令组成。 Most of them change the state of the CPU (like advancing the PC, changing register values, setting flags, enabling/disabling CPU cores, ...). 它们中的大多数都会更改CPU的状态(例如,使PC前进,更改寄存器值,设置标志,启用/禁用CPU内核等)。

This means the CPU isn't polling anything. 这意味着CPU没有轮询任何东西。 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 (ie 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. 然后,处理程序将查看任务队列,并根据其策略决定要执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM