#author("2017-09-16T11:47:45+09:00","default:afuruta","afuruta")
#author("2017-10-22T13:19:33+09:00","default:afuruta","afuruta")
* kernel 内外のコンテキスト(Players inside/outside kernel) [#te55c39e]
&ogfiledirect(Documentation/DocBook/kernel-hacking/basic-players.html); に Linux において CPU が実行しているコンテキストについて書かれています。&ogfiledirect(basic-players.html,Documentation/DocBook/kernel-hacking/basic-players.html); ではコンテキストを Players と呼んでいます。次の表は context (players) についてまとめたものです。
|略称|コンテキスト|hardware 割り込み状態|>|説明|h
|user space|user space で実行中&br;&br;running a process in user space|CENTER:許可|>|user space で process を実行している状態です。user space は仮想アドレス空間の構成でアクセスを許可された共有空間を除いて「異なる process は互いのアクセスが出来ない」、「kernel で使用している空間にアクセスできない」ように保護された空間です。身近な例として shell から起動した /bin, /usr/bin, /sbin, /usr/sbin に格納されている実行ファイルに格納されたコードを実行している空間です(もちろん shell それ自身の実行も含まれます)。system call、例外(trap)、プリエンプション、ハードウエア割り込みによって kernel 内の実行(次の user context)に移ります。|
|user context|user space と関連した kernel 内実行&br;&br;running in kernel space, associated with a process (user context)|CENTER:許可|>|user space で実行している process と関連した kernel 内の処理です。system call または例外によって kernel に実行が移った状態です。デバイスドライバを開発する立場であれば system call がきっかけで kernel 内に実行が移った状況だと考えて良いでしょう。system call の open(), close(), read(), write(),  ioctl(), lseek(), mmap() などです。read(), write(), ioctl() system call に対応する処理で &ogdefs(copy_from_user());、&ogdefs(copy_to_user()); などで user space の process と情報を交換できる状況です。|
|user context|user space と関連した kernel 内実行&br;&br;running in kernel space, associated with a process (user context)|CENTER:許可|>|user space で実行している process と関連した kernel 内の処理です。system call または例外によって kernel に実行が移った状態です。デバイスドライバを開発する立場であれば system call がきっかけで kernel 内に実行が移った状況だと考えて良いでしょう。system call の open(), read(), write(),  ioctl(), lseek(), mmap() などです。read(), write(), ioctl() system call に対応する処理で &ogdefs(copy_from_user());、&ogdefs(copy_to_user()); などで user space の process と情報を交換できる状況です。|
|kthread context|user space と関連しない kernel 内実行&br;&br;running in kernel space, not associated with a process|CENTER:許可|>|user space の process と関連していない kernel 内の処理です。kernel thread (&ogfileone(/include/linux/kthread.h);) 実行中、work queue (&ogfileone(/include/linux/workqueue.h);) に登録した関数を実行している最中です。work queue も &ogdefs(__alloc_workqueue_key()); にて kthread で実装されています。user space の process と関連が無く仮想アドレス空間に何らかの(あるいは特定可能な) user space process が占めている領域が無いので user space の process と情報は交換できません。|
|softirq context|softirq 実行中&br;&br;not associated with any process,serving a softirq, tasklet, or timer|CENTER:禁止|~ softirq|ソフトウエア割り込み処理中です。softirq が実行される状況複雑なので [[softirq 処理はいつ行われるか]] に別記しました。&br;softirq, tasklet, timer は bottom halves (単数形の bottom half が使われたり、省略されて bh) と総称されています。tasklet, timer もsoftirq にて実装されています(&ogdefs(TASKLET_SOFTIRQ);, &ogdefs(TIMER_SOFTIRQ);)。&br;Linux kernel のドキュメントでは user space と関連しない kernel 内実行(not associated with any process) と書かれています。このことを確かめるため softirq 処理の中から (&ogdefs(task_struct*);)&ogdefs(current);->comm を読むと何らかの process 名が入っています。多くの状況でハードウエア割り込みの最後の処理として softirq は実行されています。割り込みが発生した時点でたまたま実行していたコンテキスト上で 何か別のコンテキストと選べない・予測できない関連があると言うのが正しいです。&br;割り込み処理と同様に sleep できない制限があります。|
|~|~|~|~ tasklet|tasklet (&ogfileone(/include/linux/interrupt.h);) は softirq で実装されています。ソフトウエア割り込み番号は &ogdefs(TASKLET_SOFTIRQ); で処理関数は &ogdefs(tasklet_action()); です。|
|~|~|~|~ timer|timer (&ogfileone(/include/linux/timer.h);) は softirq で実装されています。ソフトウエア割り込み番号は &ogdefs(TIMER_SOFTIRQ); で処理関数は &ogdefs( run_timer_softirq()); です。|
|interrupt context|hardware 割り込み実行中&br;&br;not associated with any process, serving a hardware interrupt|CENTER:禁止|>|ハードウエア割り込み処理中です。割り込みが発生し割り込み処理が始まった時点で実行していたコンテキストの上で処理します。何か別のコンテキストと選べない・予測できない関連があります。|
#textbox(note, Linux kernel 特有のフレーズ bottom halves の使われ方について){{
kernel API として bottom halves の範囲を定義しているのは &ogdefs(spin_lock_bh());, &ogdefs(spin_unlock_bh()); とこれから派生・これに類似する API です。softirq (ソフトウエア割り込み)を禁止/解除する。という動作に対して接尾辞 _bh を付けています。&br;一方で「割り込み処理のうち後半処理全般」を指す意味として使われることがあります。irq handler を 対語の様に top halves と呼び irq handler から起床された何らかの context を全て総称するものとして使われます。ここでは「後半処理全般」を指すフレーズとして bottom halves は使いません。
}}

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS