1. 程式人生 > >用戶空間與內核空間,進程上下文與中斷上下文[總結]【轉】

用戶空間與內核空間,進程上下文與中斷上下文[總結]【轉】

存儲器 com ont article 模式 tab 用戶代碼 ssi 而在

轉自:http://blog.csdn.net/lizuobin2/article/details/51791863

本文轉載自:http://www.cnblogs.com/Anker/p/3269106.html

1、前言

  最近在學習Linux內核方面的知識,經常會看到用戶空間與內核空間及進程上下文與中斷上下文。看著很熟悉,半天又說不出到底是怎麽回事,有什麽區別。看書過程經常被感覺欺騙,似懂非懂的感覺,很是不爽,今天好好結合書和網上的資料總結一下,加深理解。

2、用戶空間與內核空間  

  我們知道現在操作系統都是采用虛擬存儲器,那麽對32位操作系統而言,它的尋址空間(虛擬存儲空間)為4G(2的32次方)。操心系統的核心是內核,獨立於普通的應用程序,可以訪問受保護的內存空間,也有訪問底層硬件設備的所有權限。為了保證用戶進程不能直接操作內核,保證內核的安全,操心系統將虛擬空間劃分為兩部分,一部分為內核空間,一部分為用戶空間。針對linux操作系統而言,將最高的1G字節(從虛擬地址0xC0000000到0xFFFFFFFF),供內核使用,稱為內核空間,而將較低的3G字節(從虛擬地址0x00000000到0xBFFFFFFF),供各個進程使用,稱為用戶空間。每個進程可以通過系統調用進入內核,因此,Linux內核由系統內的所有進程共享。於是,從具體進程的角度來看,每個進程可以擁有4G字節的虛擬空間。空間分配如下圖所示:

技術分享 有了用戶空間和內核空間,整個linux內部結構可以分為三部分,從最底層到最上層依次是:硬件-->內核空間-->用戶空間。如下圖所示: 技術分享

需要註意的細節問題:

(1) 內核空間中存放的是內核代碼和數據,而進程的用戶空間中存放的是用戶程序的代碼和數據。不管是內核空間還是用戶空間,它們都處於虛擬空間中。

(2) Linux使用兩級保護機制:0級供內核使用,3級供用戶程序使用。

內核態與用戶態:

(1)當一個任務(進程)執行系統調用而陷入內核代碼中執行時,稱進程處於內核運行態(內核態)。此時處理器處於特權級最高的(0級)內核代碼中執行。當進程處於內核態時,執行的內核代碼會使用當前進程的內核棧。每個進程都有自己的內核棧。

(2)當進程在執行用戶自己的代碼時,則稱其處於用戶運行態(用戶態)。此時處理器在特權級最低的(3級)用戶代碼中運行。當正在執行用戶程序而突然被中斷程序中斷時,此時用戶程序也可以象征性地稱為處於進程的內核態。因為中斷處理程序將使用當前進程的內核棧。

內核態與用戶態的切換

當進程運行在用戶態,此時無論發生中斷、異常、系統調用(本質是中斷),都會通過異常向量表進入系統狀態(內核態),執行完中斷服務程序時又恢復原狀,返回到用戶態。主要步驟:

[1] 從當前進程的描述符中提取其內核棧的ss0及esp0信息。
[2] 使用ss0和esp0指向的內核棧將當前進程的cs,eip,eflags,ss,esp信息保存起來,這個過程也完成了由用戶棧到內核棧的切換過程,同時保存了被暫停執行的程序的下一條指令。

[3] 將先前由中斷向量檢索得到的中斷處理程序的cs,eip信息裝入相應的寄存器,開始執行中斷處理程序,這時就轉到了內核態的程序執行了。

其實寫過linux內核驅動程序的同學應該就知道,實現一個字符設備驅動,在write方法和read方法中,內核態和用戶態之間的橋梁就是copy_to_user和copy_form_user這兩個接口,在高執行級別下,代碼可以執行特權指令,訪問任意的物理地址,這種CPU執行級別就對應著內核態,而在相應的低級別執行狀態下,代碼的掌控範圍會受到限制。只能在對應級別允許的範圍內活動。其實正好說明了這個問題,程序員或者軟件應用工程師在編寫應用程序去控制設備驅動的時候,首先肯定是會打開相應的文件描述符,然後對相應的文件描述符進行讀寫,ioctl,lseek之類的操作。當在應用層編寫程序即是屬於用戶態,在應用程序不能訪問任意的硬件物理地址,所以當用戶需要讀取文件描述符的內的內容時,就需要調用read,當用戶需要寫數據進文件描述符時,就需要用write,在用戶態調用這兩個接口,進而就會進行系統調用,產生相應的系統調用號,然後內核會根據系統調用號找到相應的驅動程序,此時系統就處在內核態中,在驅動程序中,首先進行驅動程序初始化,然後註冊,產生驅動程序最重要主設備號和次設備號。初始化的過程中由於對相應的read方法和wirte方法進行初始化,故用戶態執行read操作,就會進而使CPU產生異常,然後進入內核態找到相應的read,write當然也是一樣的。

參考資料:

http://blog.csdn.net/f22jay/article/details/7925531

http://blog.csdn.net/zhangskd/article/details/6956638

http://blog.chinaunix.net/uid-26838492-id-3162146.html

3、進程上下文與中斷上下文

  我在看《linux內核設計與實現》這本書的第三章進程管理時候,看到進程上下文。書中說當一個程序執行了系統調用或者觸發某個異常(軟中斷),此時就會陷入內核空間,內核此時代表進程執行,並處於進程上下文中。看後還是沒有弄清楚,什麽是進程上下文,如何上google上面狂搜一把,總結如下:

  程序在執行過程中通常有用戶態和內核態兩種狀態,CPU對處於內核態根據上下文環境進一步細分,因此有了下面三種狀態:

(1)內核態,運行於進程上下文,內核代表進程運行於內核空間。
(2)內核態,運行於中斷上下文,內核代表硬件運行於內核空間。
(3)用戶態,運行於用戶空間。

  上下文context: 上下文簡單說來就是一個環境。

  用戶空間的應用程序,通過系統調用,進入內核空間。這個時候用戶空間的進程要傳遞 很多變量、參數的值給內核,內核態運行的時候也要保存用戶進程的一些寄存 器值、變量等。所謂的“進程上下文”,可以看作是用戶進程傳遞給內核的這些參數以及內核要保存的那一整套的變量和寄存器值和當時的環境等。

  相對於進程而言,就是進程執行時的環境。具體來說就是各個變量和數據,包括所有的寄存器變量、進程打開的文件、內存信息等。一個進程的上下文可以分為三個部分:用戶級上下文、寄存器上下文以及系統級上下文。

(1)用戶級上下文: 正文、數據、用戶堆棧以及共享存儲區;
(2)寄存器上下文: 通用寄存器、程序寄存器(IP)、處理器狀態寄存器(EFLAGS)、棧指針(ESP);
(3)系統級上下文: 進程控制塊task_struct、內存管理信息(mm_struct、vm_area_struct、pgd、pte)、內核棧。

當發生進程調度時,進行進程切換就是上下文切換(context switch).操作系統必須對上面提到的全部信息進行切換,新調度的進程才能運行。而系統調用進行的模式切換(mode switch)。模式切換與進程切換比較起來,容易很多,而且節省時間,因為模式切換最主要的任務只是切換進程寄存器上下文的切換。

  硬件通過觸發信號,導致內核調用中斷處理程序,進入內核空間。這個過程中,硬件的 一些變量和參數也要傳遞給內核,內核通過這些參數進行中斷處理。所謂的“ 中斷上下文”,其實也可以看作就是硬件傳遞過來的這些參數和內核需要保存的一些其他環境(主要是當前被打斷執行的進程環境)。中斷時,內核不代表任何進程運行,它一般只訪問系統空間,而不會訪問進程空間,內核在中斷上下文中執行時一般不會阻塞。

摘錄Linux註釋的內容如下:

Process Context
-------------------------------------------
One of the most important parts of a process is the executing program code. This code is read in from an executable file and executed within the program‘s address space. Normal program execution occurs in user-space. When a program executes a system call or triggers an exception, it enters kernel-space. At this point, the kernel is said to be "executing on behalf of the process" and is in process context. When in process context, the current macro is valid[7]. Upon exiting the kernel, the process resumes execution in user-space, unless a higher-priority process has become runnable in the interim(過渡期), in which case the scheduler is invoked to select the higher priority process.

Other than process context there is interrupt context, In interrupt context, the system is not running on behalf of a process, but is executing an interrupt handler. There is no process tied to interrupt handlers and consequently no process context.

System calls and exception handlers are well-defined interfaces into the kernel. A process can begin executing in kernel-space only through one of these interfaces -- all access to the kernel is through these interfaces.

-------------------------------------------

Interrupt Context
-------------------------------------------
When executing an interrupt handler or bottom half, the kernel is in interrupt context. Recall that process context is the mode of operation the kernel is in while it is executing on behalf of a process -- for example, executing a system call or running a kernel thread. In process context, the current macro points to the associated task. Furthermore, because a process is coupled to the kernel in process context(因為進程是以進程上文的形式連接到內核中的), process context can sleep or otherwise invoke the scheduler.

Interrupt context, on the other hand, is not associated with a process. The current macro is not relevant (although it points to the interrupted process). Without a backing process(由於沒有進程的背景),interrupt context cannot sleep -- how would it ever reschedule?(否則怎麽再對它重新調度?) Therefore, you cannot call certain functions from interrupt context. If a function sleeps, you cannot use it from your interrupt handler -- this limits the functions that one can call from an interrupt handler.(這是對什麽樣的函數可以在中斷處理程序中使用的限制)

Interrupt context is time critical because the interrupt handler interrupts other code. Code should be quick and simple. Busy looping is discouraged. This is a very important point; always keep in mind that your interrupt handler has interrupted other code (possibly even another interrupt handler on a different line!). Because of this asynchronous nature, it is imperative(必須) that all interrupt handlers be as quick and as simple as possible. As much as possible, work should be pushed out from the interrupt handler and performed in a bottom half, which runs at a more convenient time.

The setup of an interrupt handler‘s stacks is a configuration option. Historically, interrupt handlers did not receive(擁有) their own stacks. Instead, they would share the stack of the process that they interrupted[1]. The kernel stack is two pages in size; typically, that is 8KB on 32-bit architectures and 16KB on 64-bit architectures. Because in this setup interrupt handlers share the stack, they must be exceptionally frugal(必須非常節省) with what data they allocate there. Of course, the kernel stack is limited to begin with, so all kernel code should be cautious.

A process is always running. When nothing else is schedulable, the idle task runs.

-------------------------------------------

LINUX完全註釋中的一段話:

當一個進程在執行時,CPU的所有寄存器中的值、進程的狀態以及堆棧中的內容被稱為該進程的上下文。當內核需要切換到另一個進程時,它需要保存當前進程的所有狀態,即保存當前進程的上下文,以便在再次執行該進程時,能夠必得到切換時的狀態執行下去。在LINUX中,當前進程上下文均保存在進程的任務數據結構中。在發生中斷時,內核就在被中斷進程的上下文中,在內核態下執行中斷服務例程。但同時會保留所有需要用到的資源,以便中繼服務結束時能恢復被中斷進程的執行。

參考資料:

http://www.cnblogs.com/hustcat/articles/1505618.html

http://mprc.pku.edu.cn/~zhengyansong/blog/?p=199

http://blog.chinaunix.net/uid-26980210-id-3235544.html

用戶空間與內核空間,進程上下文與中斷上下文[總結]【轉】