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級)使用者代碼中運作。當正在執行使用者程式而突然被中斷程式中斷時,此時使用者程式也可以象征性地稱為處于程序的核心态。因為中斷處理程式将使用目前程序的核心棧。
參考資料:
<a href="http://blog.csdn.net/f22jay/article/details/7925531">http://blog.csdn.net/f22jay/article/details/7925531</a>
<a href="http://blog.csdn.net/zhangskd/article/details/6956638">http://blog.csdn.net/zhangskd/article/details/6956638</a>
<a href="http://blog.chinaunix.net/uid-26838492-id-3162146.html">http://blog.chinaunix.net/uid-26838492-id-3162146.html</a>
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中,目前程序上下文均儲存在程序的任務資料結構中。在發生中斷時,核心就在被中斷程序的上下文中,在核心态下執行中斷服務例程。但同時會保留所有需要用到的資源,以便中繼服務結束時能恢複被中斷程序的執行。