首頁 > 系統教程 > Linux > 主體

什麼是Linux核心空間與使用者空間?

PHPz
發布: 2024-02-05 12:57:09
轉載
861 人瀏覽過

核心空間與使用者空間

#對於32位元作業系統而言,它的尋址空間(也稱為虛擬位址空間或線性位址空間)大小為4G(即2的32次方)。這意味著一個行程可以擁有最大4G的位址空間。

作業系統的核心是核心(kernel),它是與普通應用程式分離的,有權限存取受保護的記憶體空間和底層硬體設備。為了確保核心的安全,現代作業系統通常限制使用者進程直接操作核心。

通常,這透過將虛擬位址空間劃分為兩個部分來實現,即核心空間和使用者空間。就Linux作業系統而言,最高的1G位元組(從虛擬位址0xC0000000到0xFFFFFFFF)被核心使用,稱為核心空間。而較低的3G位元組(從虛擬位址0x00000000到0xBFFFFFFF)則由各個程序使用,稱為用戶空間。

換句話說,每個行程的4G位址空間中,最高的1G是相同的,也就是核心空間。只有剩餘的3G才是進程本身的可用空間。

可以這樣理解:「最高1G的核心空間是在所有進程之間共享的!」下圖展示了每個進程4G位址空間的分配情況(圖片來自互聯網):

什麼是Linux核心空間與使用者空間?

為什麼要區分核心空間與使用者空間

#在 CPU 的所有指令中,有些指令是非常危險的,如果錯用,會導致系統崩潰,例如清除記憶體、設定時鐘等。如果允許所有的程式都可以使用這些指令,那麼系統崩潰的機率將大大增加。

所以,CPU 將指令分為特權指令和非特權指令,對於那些危險的指令,只允許作業系統及其相關模組使用,普通應用程式只能使用那些不會造成災難的指令。

例如 Intel 的 CPU 將特權等級分成 4 個等級:Ring0~Ring3。其實 Linux 系統只使用了 Ring0 和 Ring3 兩個運行等級(Windows 系統也是一樣的)。

當進程運行在 Ring3 層級時稱為運行在用戶態,而運行在 Ring0 層級時稱為運行在核心態。

核心態與使用者狀態

#好了我們現在需要再解釋一下什麼是核心態、用戶態:「當進程運行在內核空間時就處於內核態,而進程運行在用戶空間時則處於用戶態。」

在核心態下,進程運行在核心位址空間中,此時 CPU 可以執行任何指令。運行的程式碼也不受任何的限制,可以自由地存取任何有效位址,也可以直接進行連接埠的存取。

在使用者狀態下,進程運行在使用者位址空間中,被執行的程式碼要受到CPU 的諸多檢查,它們只能存取映射其位址空間的頁表項中規定的在使用者狀態下可存取頁面的虛擬位址,且只能對任務狀態段(TSS)中I/O 許可位圖(I/O Permission Bitmap)中規定的可存取連接埠進行直接存取。

對於先前的 DOS 作業系統來說,是沒有核心空間、使用者空間以及核心態、使用者態這些概念的。可以認為所有的程式碼都是運行在內核態的,因而用戶編寫的應用程式程式碼可以很容易的讓作業系統崩潰掉。

對於 Linux 來說,透過區分核心空間和使用者空間的設計,隔離了作業系統程式碼(作業系統的程式碼要比應用程式的程式碼健壯很多)與應用程式碼。

即使是單一應用程式出現錯誤也不會影響到作業系統的穩定性,這樣其它的程式還可以正常的運作(Linux 可是個多任務系統啊!)。

「所以,區分核心空間和使用者空間本質上是要提高作業系統的穩定性及可用性。」

#

How to enter kernel space from user space

In fact, all system resource management is completed in the kernel space. For example, reading and writing disk files, allocating and recycling memory, reading and writing data from network interfaces, etc.

Our application cannot directly perform such operations. But we can accomplish such tasks through the interface provided by the kernel.

For example, if an application wants to read a file on the disk, it can initiate a "system call" to the kernel and tell the kernel: "I want to read a certain file on the disk."

In fact, a special instruction is used to allow the process to enter the kernel state (to the kernel space) from the user state. In the kernel space, the CPU can execute any instructions, including reading data from the disk. The specific process is to first read the data into the kernel space, then copy the data to the user space and switch from the kernel mode to the user mode.

At this point, the application has returned from the system call and obtained the desired data, and can happily continue execution. To put it simply, the application outsources high-tech things (reading files from disk) to the system kernel, and the system kernel does these things professionally and efficiently.

For a process, the process of entering kernel space from user space and finally returning to user space is very complicated. For example, the concept "stack" that we often come into contact with actually has a stack in the kernel mode and user mode.

When running in user space, the process uses the stack in user space, and when running in kernel space, the process uses the stack in kernel space. Therefore, each process in Linux has two stacks, one for user mode and one for kernel mode.

The following figure briefly describes the conversion between user mode and kernel mode:

什麼是Linux核心空間與使用者空間?

Since the user mode process must switch to the kernel mode in order to use the system resources, let's take a look at how many ways the process can enter from the user mode to the kernel mode.

In summary, there are three ways: system call, software interrupt and hardware interrupt. Each of these three methods involves a lot of operating system knowledge, so I will not expand on it here.

the whole frame

Next, let’s take a look at the structure of the entire Linux system from the perspective of kernel space and user space. It can be roughly divided into three parts, from bottom to top: hardware -> kernel space -> user space. As shown in the picture below (this picture comes from the Internet):

什麼是Linux核心空間與使用者空間?

On top of the hardware, the code in the kernel space controls the use of hardware resources. The code in the user space can only use the hardware resources in the system through the system call interface (System Call Interface) exposed by the kernel. . In fact, not only Linux, but also the design of Windows operating systems is similar.

In fact we can summarize the activity of each processor at any given point in time as one of the following three:

  • Runs in user space and executes user processes.
  • Runs in the kernel space, in the process context, and executes on behalf of a specific process.
  • Runs in kernel space, is in interrupt context, has nothing to do with any process, and handles a specific interrupt.

The above three points include almost all situations. For example, when the CPU is idle, the kernel runs an empty process, which is in the process context but runs in the kernel space.

Note: The interrupt service routines of Linux systems are not executed in the context of the process. They are executed in a specialized interrupt context that is independent of all processes.

The reason why there is a special execution environment is to ensure that the interrupt service program can respond to and handle the interrupt request as soon as possible, and then exit quickly.

Summarize

Most modern operating systems protect the security and stability of the operating system itself through the design of kernel space and user space. Therefore, when we read information about operating systems, we often encounter concepts such as kernel space, user space, kernel mode, and user mode. I hope this article can help you understand these basic concepts.

以上是什麼是Linux核心空間與使用者空間?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:lxlinux.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板