Mercurial > hg > Papers > 2020 > tobaru-master
changeset 19:98cee2f6c919
slide
author | tobaru |
---|---|
date | Sat, 08 Feb 2020 15:06:50 +0900 |
parents | afc36230cf4f |
children | ff1f2c66a84d |
files | paper/cbc_interface.tex paper/master_paper.log paper/master_paper.pdf paper/master_paper.synctex.gz slide/CbCXv6.html slide/CbCXv6.md slide/fig/cbc_for_private.graffle slide/fig/cbc_for_private.pdf slide/fig/cbc_for_private.png |
diffstat | 9 files changed, 781 insertions(+), 47 deletions(-) [+] |
line wrap: on
line diff
--- a/paper/cbc_interface.tex Fri Feb 07 20:06:13 2020 +0900 +++ b/paper/cbc_interface.tex Sat Feb 08 15:06:50 2020 +0900 @@ -29,7 +29,7 @@ \lstinputlisting[label=interface, caption={\footnotesize vm のインターフェースの定義(vm.h)}]{./src/vm.h} -1行目ので実装名を定義している。 +1行目で実装名を定義している。 typedef struct の直後に実装名(vm)を書く。 @@ -45,6 +45,7 @@ \_\_code next(...) の引数 ... は複数の Input Data Gear を持つという意味である。 後述する実装によって条件分岐によって複数の継続先が設定されることがある。 +それぞれの Code Gear の引数の1つに設定する。引数の最後に設定しているが遷移先で引数を受け取る順番が正しければよい。 Code Gaer は 20行目から33行目のように "\_\_code [Code Gear名]([引数])"で定義する。 @@ -64,13 +65,13 @@ \lstinputlisting[frame=lrbt,label=impl_vm,caption={\footnotesize vm インターフェースの実装}]{./src/vm_impl.cbc} -2行目のようにインターフェースのヘッダーファイルは \#interface で呼び出す。 +2行目のようにインターフェースのヘッダーファイルは \#include ではなく \#interface で呼び出す。 -create\_impl の関数内で、インターフェースを vm で定義し、23行目の vm-$>$void\_ret のようにそれぞれのインターフェースに対応させていく。 +create\_impl の関数内で、インターフェースを vm で定義し、23行目の vm-$>$void\_ret のようにそれぞれの Code Gear 名に対応させていく。 -CbCは1つ1つの関数の信頼性を保障させるために細かくする必要があるので、for文やif文がある場合はさらに実装を分ける。vm と同じように vm\_impl を定義し、遷移する関数名に対応させていく。分けた実装はさらに別で実装する(vm\_impl\_private.cbc)。 +CbCは1つ1つの Code Gear の信頼性を保障させるために細かくする必要があるので、for文やif文がある場合はさらに実装を分ける。vm と同じように vm\_impl を定義し、遷移する関数名に対応させていく。分けた実装はさらに別で実装する(vm\_impl\_private.cbc)。 % インターフェースを呼び出す場合は、struct vm* vm = createvm_impl(cbc_context); @@ -89,7 +90,6 @@ この Code Gear は基本的にインターフェースで指定された Code Gear 内からのみ継続されるため、 Java の private メソッドのように扱われる。 - インターフェースと同じようにヘッダーファイルをソースコード \ref{impl_vm_privateh} で定義する。 @@ -279,8 +279,10 @@ \end{lstlisting} +for文から末尾再起の変換について +static なものに関しては後々実装する \section{インターフェースの呼び出し} @@ -295,9 +297,9 @@ ソースコードの説明 +- dummy を使って解決する -
--- a/paper/master_paper.log Fri Feb 07 20:06:13 2020 +0900 +++ b/paper/master_paper.log Sat Feb 08 15:06:50 2020 +0900 @@ -1,4 +1,4 @@ -This is e-pTeX, Version 3.14159265-p3.8.2-190131-2.6 (utf8.euc) (TeX Live 2019) (preloaded format=platex 2020.1.16) 7 FEB 2020 20:05 +This is e-pTeX, Version 3.14159265-p3.8.2-190131-2.6 (utf8.euc) (TeX Live 2019) (preloaded format=platex 2020.1.16) 8 FEB 2020 10:31 entering extended mode restricted \write18 enabled. file:line:error style messages enabled. @@ -489,4 +489,4 @@ 107 hyphenation exceptions out of 8191 33i,11n,36p,410b,1835s stack positions out of 5000i,500n,10000p,200000b,80000s -Output written on master_paper.dvi (56 pages, 243800 bytes). +Output written on master_paper.dvi (56 pages, 243796 bytes).
--- a/slide/CbCXv6.html Fri Feb 07 20:06:13 2020 +0900 +++ b/slide/CbCXv6.html Sat Feb 08 15:06:50 2020 +0900 @@ -34,10 +34,10 @@ </head> <body> - <div id="doc" class="markdown-body container-fluid comment-enabled" data-hard-breaks="true" style="position: relative;"><h1 id="CbCインターフェースによる-CbCXv6-の書き換え" style=""><a class="anchor hidden-xs" href="#CbCインターフェースによる-CbCXv6-の書き換え" title="CbCインターフェースによる-CbCXv6-の書き換え"><span class="octicon octicon-link"></span></a>CbCインターフェースによる CbCXv6 の書き換え</h1><ul> + <div id="doc" class="markdown-body container-fluid comment-enabled" data-hard-breaks="true"><h1 id="CbCインターフェースによる-CbCXv6-の書き換え" style=""><a class="anchor hidden-xs" href="#CbCインターフェースによる-CbCXv6-の書き換え" title="CbCインターフェースによる-CbCXv6-の書き換え"><span class="octicon octicon-link"></span></a>CbCインターフェースによる CbCXv6 の書き換え</h1><ul> <li>並列信頼研</li> <li>桃原 優</li> -</ul><hr><h1 id="目次0" style=""><a class="anchor hidden-xs" href="#目次0" title="目次0"><span class="octicon octicon-link"></span></a>目次</h1><ol> +</ul><hr><h1 id="目次" style=""><a class="anchor hidden-xs" href="#目次" title="目次"><span class="octicon octicon-link"></span></a>目次</h1><ol> <li><strong>OS の信頼性の保証</strong></li> <li>CbC による Gears OS の開発</li> <li>Xv6</li> @@ -132,7 +132,7 @@ </ul><hr><h1 id="Geas-OS" style=""><a class="anchor hidden-xs" href="#Geas-OS" title="Geas-OS"><span class="octicon octicon-link"></span></a>Geas OS</h1><ul> <li>CbC を使って信頼性の保証と拡張性を持たせる Gears OS の開発を行なっている</li> <li>Xv6 という OS を参考に書き換えをしている</li> -</ul><hr><h1 id="メモリ管理0" style=""><a class="anchor hidden-xs" href="#メモリ管理0" title="メモリ管理0"><span class="octicon octicon-link"></span></a>メモリ管理</h1><ul> +</ul><hr><h1 id="メモリ管理" style=""><a class="anchor hidden-xs" href="#メモリ管理" title="メモリ管理"><span class="octicon octicon-link"></span></a>メモリ管理</h1><ul> <li>OS の信頼性の1つであるメモリ管理部分を CbC で書き換える <ul> <li>Page のバリデーションチェック</li> @@ -149,7 +149,7 @@ <li>Agda による証明</li> </ul> </li> -</ul><hr><h1 id="目次0" style=""><a class="anchor hidden-xs" href="#目次0" title="目次0"><span class="octicon octicon-link"></span></a>目次</h1><ol> +</ul><hr><h1 id="目次1" style=""><a class="anchor hidden-xs" href="#目次1" title="目次1"><span class="octicon octicon-link"></span></a>目次</h1><ol> <li>OS の信頼性の保証</li> <li><strong>CbC による Gears OS の開発</strong></li> <li>Xv6</li> @@ -161,7 +161,7 @@ <li>a</li> </ul><hr><h1 id="Context" style=""><a class="anchor hidden-xs" href="#Context" title="Context"><span class="octicon octicon-link"></span></a>Context</h1><ul> <li>a</li> -</ul><hr><h1 id="目次1" style=""><a class="anchor hidden-xs" href="#目次1" title="目次1"><span class="octicon octicon-link"></span></a>目次</h1><ol> +</ul><hr><h1 id="目次2" style=""><a class="anchor hidden-xs" href="#目次2" title="目次2"><span class="octicon octicon-link"></span></a>目次</h1><ol> <li>OS の信頼性の保証</li> <li>CbC による Gears OS の開発</li> <li><strong>Xv6</strong></li> @@ -169,7 +169,7 @@ <li>CbC インターフェース</li> <li>評価</li> <li>まとめ</li> -</ol><hr><h1 id="Xv60" style=""><a class="anchor hidden-xs" href="#Xv60" title="Xv60"><span class="octicon octicon-link"></span></a>Xv6</h1><ul> +</ol><hr><h1 id="Xv6" style=""><a class="anchor hidden-xs" href="#Xv6" title="Xv6"><span class="octicon octicon-link"></span></a>Xv6</h1><ul> <li>MIT の講義用教材として作られたOS <ul> <li>企画課される前のCで書かれたUNIX V6 を書き換えた</li> @@ -203,12 +203,12 @@ <li>ハードウェアが一時的に特権レベルをあげ、カーネルのプログラムが実行される</li> </ul> </li> -</ul><hr><h1 id="system-call0" style=""><a class="anchor hidden-xs" href="#system-call0" title="system-call0"><span class="octicon octicon-link"></span></a>system call</h1><ul> +</ul><hr><h1 id="system-call" style=""><a class="anchor hidden-xs" href="#system-call" title="system-call"><span class="octicon octicon-link"></span></a>system call</h1><ul> <li>system call 呼び出し</li> <li>トラップ の発生</li> <li>ユーザープログラムの中断</li> <li>処理がカーネルに切り替わる</li> -</ul><hr><h1 id="system-call" style=""><a class="anchor hidden-xs" href="#system-call" title="system-call"><span class="octicon octicon-link"></span></a>system call</h1><ul> +</ul><hr><h1 id="system-call1" style=""><a class="anchor hidden-xs" href="#system-call1" title="system-call1"><span class="octicon octicon-link"></span></a>system call</h1><ul> <li>ソースコード載せる</li> </ul><hr><h1 id="Xv6-rpi" style=""><a class="anchor hidden-xs" href="#Xv6-rpi" title="Xv6-rpi"><span class="octicon octicon-link"></span></a>Xv6-rpi</h1><ul> <li>Xv6 は Arm のバイナリを出力するので様々なハードウェアで動かすことができる @@ -222,7 +222,7 @@ <li>CbCxv6 とは別</li> </ul> </li> -</ul><hr><h1 id="目次0" style=""><a class="anchor hidden-xs" href="#目次0" title="目次0"><span class="octicon octicon-link"></span></a>目次</h1><ol> +</ul><hr><h1 id="目次3" style=""><a class="anchor hidden-xs" href="#目次3" title="目次3"><span class="octicon octicon-link"></span></a>目次</h1><ol> <li>OS の信頼性の保証</li> <li>CbC による Gears OS の開発</li> <li>Xv6</li> @@ -230,7 +230,7 @@ <li>CbC インターフェース</li> <li>評価</li> <li>まとめ</li> -</ol><hr><h1 id="CbCXv6-での-Paging0" style=""><a class="anchor hidden-xs" href="#CbCXv6-での-Paging0" title="CbCXv6-での-Paging0"><span class="octicon octicon-link"></span></a>CbCXv6 での Paging</h1><ul> +</ol><hr><h1 id="CbCXv6-での-Paging" style=""><a class="anchor hidden-xs" href="#CbCXv6-での-Paging" title="CbCXv6-での-Paging"><span class="octicon octicon-link"></span></a>CbCXv6 での Paging</h1><ul> <li>OS の信頼性の1つであるメモリ管理部分の書き換えについて説明</li> </ul><hr><h1 id="実メモリの直接操作" style=""><a class="anchor hidden-xs" href="#実メモリの直接操作" title="実メモリの直接操作"><span class="octicon octicon-link"></span></a>実メモリの直接操作</h1><ul> <li>実メモリを直接扱うと様々な問題が生じる @@ -243,7 +243,7 @@ </li> </ul> </li> -</ul><hr><h1 id="Paging0" style=""><a class="anchor hidden-xs" href="#Paging0" title="Paging0"><span class="octicon octicon-link"></span></a>Paging</h1><ul> +</ul><hr><h1 id="Paging" style=""><a class="anchor hidden-xs" href="#Paging" title="Paging"><span class="octicon octicon-link"></span></a>Paging</h1><ul> <li>メモリ管理の手法</li> <li>Page と呼ばれる固定長のブロックに分割して、メモリとスワップ領域で Page を入れ替えて管理</li> <li>仮想メモリとして扱うことでフラグメンテーションの解消と空き番地を探す必要がなくなる</li> @@ -262,15 +262,318 @@ </ul><hr><h1 id="Paging-の書き換え" style=""><a class="anchor hidden-xs" href="#Paging-の書き換え" title="Paging-の書き換え"><span class="octicon octicon-link"></span></a>Paging の書き換え</h1><ul> <li>Xv6 では実メモリから仮想メモリの変換をvm.cで行なっている。</li> <li>次の章で書き換えについて説明する</li> -</ul><hr><h1 id="目次" style=""><a class="anchor hidden-xs" href="#目次" title="目次"><span class="octicon octicon-link"></span></a>目次</h1><ol> +</ul><hr><h1 id="目次4" style=""><a class="anchor hidden-xs" href="#目次4" title="目次4"><span class="octicon octicon-link"></span></a>目次</h1><ol> <li>OS の信頼性の保証</li> <li>CbC による Gears OS の開発</li> <li>Xv6</li> -<li>CbCXv6 での Paging**</li> +<li>CbCXv6 での Paging</li> <li><strong>CbC インターフェース</strong></li> <li>評価</li> <li>まとめ</li> -</ol><hr></div> +</ol><hr><h1 id="CbC-インターフェースの導入" style=""><a class="anchor hidden-xs" href="#CbC-インターフェースの導入" title="CbC-インターフェースの導入"><span class="octicon octicon-link"></span></a>CbC インターフェースの導入</h1><ul> +<li>継続の記述が煩雑になる +<ul> +<li>Code Gear がどの Data Gear の番号に対応するか指定する必要がある</li> +<li>ノーマルレベルとメタレベルで Data Gear の見え方が異なるため調整する必要がある</li> +</ul> +</li> +<li>->インターフェースの導入</li> +</ul><hr><h1 id="CbC-インターフェース" style=""><a class="anchor hidden-xs" href="#CbC-インターフェース" title="CbC-インターフェース"><span class="octicon octicon-link"></span></a>CbC インターフェース</h1><ul> +<li>インターフェースは Data Gear に対しての操作を行う Code Gear</li> +<li>実装は別で定義し、呼び出す</li> +<li>インターフェースによって機能を置き換えることができる</li> +</ul><hr><h1 id="インターフェースの定義" style=""><a class="anchor hidden-xs" href="#インターフェースの定義" title="インターフェースの定義"><span class="octicon octicon-link"></span></a>インターフェースの定義</h1><ul> +<li>Data Gear と Data Gear に対して操作を行う Code Gear の集合を表現する <strong>Meta Data Gear</strong></li> +</ul><hr><h1 id="インターフェースのソースコード" style=""><a class="anchor hidden-xs" href="#インターフェースのソースコード" title="インターフェースのソースコード"><span class="octicon octicon-link"></span></a>インターフェースのソースコード</h1><ul> +<li>vm.c をインターフェースで書き換えた vm.h のコードの説明をしていく</li> +</ul><hr><h1 id="実装名の定義" style=""><a class="anchor hidden-xs" href="#実装名の定義" title="実装名の定義"><span class="octicon octicon-link"></span></a>実装名の定義</h1><ul> +<li>typedef struct の直後に実装名(vm)を書く</li> +</ul><pre><code class="c hljs"><span class="token keyword">typedef</span> <span class="token keyword">struct</span> vm<span class="token operator"><</span>Type<span class="token punctuation">,</span>Impl<span class="token operator">></span> <span class="token punctuation">{</span> +</code></pre><hr><h1 id="Data-Gear-の定義" style=""><a class="anchor hidden-xs" href="#Data-Gear-の定義" title="Data-Gear-の定義"><span class="octicon octicon-link"></span></a>Data Gear の定義</h1><ul> +<li>2行目から19行目で引数の Data Gear を定義している</li> +</ul><pre><code class="c hljs"><span class="token keyword">typedef</span> <span class="token keyword">struct</span> vm<span class="token operator"><</span>Type<span class="token punctuation">,</span>Impl<span class="token operator">></span> <span class="token punctuation">{</span> + <span class="token keyword">union</span> Data<span class="token operator">*</span> vm<span class="token punctuation">;</span> + uint low<span class="token punctuation">;</span> + uint hi<span class="token punctuation">;</span> + <span class="token keyword">struct</span> proc<span class="token operator">*</span> p<span class="token punctuation">;</span> +<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> +</code></pre><hr><h1 id="Code-Gear-の定義" style=""><a class="anchor hidden-xs" href="#Code-Gear-の定義" title="Code-Gear-の定義"><span class="octicon octicon-link"></span></a>Code Gear の定義</h1><ul> +<li>Code Gear は __Code CodeGear名(引数); で記述する</li> +<li>第1引数の Impl* vm が Code Gear の型になる</li> +<li>初期化された Data Gear が それぞれの Code Gear の引数として扱われる</li> +<li>例)定義された uinit が kpt_freerange の Code Gear の第1引数と対応している</li> +</ul><pre><code class="c hljs"> __code <span class="token function">init_vmm</span><span class="token punctuation">(</span>Impl<span class="token operator">*</span> vm<span class="token punctuation">,</span> __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + __code <span class="token function">kpt_freerange</span><span class="token punctuation">(</span>Impl<span class="token operator">*</span> vm<span class="token punctuation">,</span> uint low<span class="token punctuation">,</span> uint hi<span class="token punctuation">,</span> __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + __code <span class="token function">kpt_alloc</span><span class="token punctuation">(</span>Impl<span class="token operator">*</span> vm <span class="token punctuation">,</span>__code <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + __code <span class="token function">switchuvm</span><span class="token punctuation">(</span>Impl<span class="token operator">*</span> vm <span class="token punctuation">,</span><span class="token keyword">struct</span> proc<span class="token operator">*</span> p<span class="token punctuation">,</span> __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + __code <span class="token function">init_inituvm</span><span class="token punctuation">(</span>Impl<span class="token operator">*</span> vm<span class="token punctuation">,</span> pde_t<span class="token operator">*</span> pgdir<span class="token punctuation">,</span> <span class="token keyword">char</span><span class="token operator">*</span> init<span class="token punctuation">,</span> uint sz<span class="token punctuation">,</span> __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + __code <span class="token function">loaduvm</span><span class="token punctuation">(</span>Impl<span class="token operator">*</span> vm<span class="token punctuation">,</span>pde_t<span class="token operator">*</span> pgdir<span class="token punctuation">,</span> <span class="token keyword">char</span><span class="token operator">*</span> addr<span class="token punctuation">,</span> <span class="token keyword">struct</span> inode<span class="token operator">*</span> ip<span class="token punctuation">,</span> uint offset<span class="token punctuation">,</span> uint sz<span class="token punctuation">,</span> __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + __code <span class="token function">allocuvm</span><span class="token punctuation">(</span>Impl<span class="token operator">*</span> vm<span class="token punctuation">,</span> pde_t<span class="token operator">*</span> pgdir<span class="token punctuation">,</span> uint oldsz<span class="token punctuation">,</span> uint newsz<span class="token punctuation">,</span> __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + +<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> + +</code></pre><hr><h1 id="next…" style=""><a class="anchor hidden-xs" href="#next…" title="next…"><span class="octicon octicon-link"></span></a>next(…)</h1><ul> +<li>__code next(…) は条件分岐によって複数の継続先が設定される</li> +<li>それぞれの Code Gear の引数の1つに設定する</li> +</ul><pre><code class="c hljs"> __code <span class="token function">kpt_freerange</span><span class="token punctuation">(</span>Impl<span class="token operator">*</span> vm<span class="token punctuation">,</span> uint low<span class="token punctuation">,</span> uint hi<span class="token punctuation">,</span> __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> + __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token punctuation">}</span> vm<span class="token punctuation">;</span> +</code></pre><hr><h1 id="インターフェースの実装" style=""><a class="anchor hidden-xs" href="#インターフェースの実装" title="インターフェースの実装"><span class="octicon octicon-link"></span></a>インターフェースの実装</h1><ul> +<li>インターフェースの実装は別ファイルで定義する(vm_impl.cbc)</li> +<li>ヘッダーファイルの呼び出しは #include ではなく #interface で呼び出す</li> +</ul><pre><code class="c hljs"><span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">"../../context.h"</span></span> +<span class="token macro property">#interface "vm.h"</span> + +</code></pre><hr><h1 id="create_impl" style=""><a class="anchor hidden-xs" href="#create_impl" title="create_impl"><span class="octicon octicon-link"></span></a>create_impl</h1><ul> +<li>create_imple の関数内で vm の型を定義し、vm->CodeGear名 で対応させていく</li> +<li>実装を Code Gear で記述していく。</li> +</ul><pre><code class="c hljs"> +vm<span class="token operator">*</span> <span class="token function">createvm_impl</span><span class="token punctuation">(</span><span class="token keyword">struct</span> Context<span class="token operator">*</span> cbc_context<span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token keyword">struct</span> vm<span class="token operator">*</span> vm <span class="token operator">=</span> new <span class="token function">vm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> + vm<span class="token operator">-></span>void_ret <span class="token operator">=</span> C_vm_void_ret<span class="token punctuation">;</span> + vm<span class="token operator">-></span>init_vmm <span class="token operator">=</span> C_init_vmmvm_impl<span class="token punctuation">;</span> + vm<span class="token operator">-></span>kpt_freerange <span class="token operator">=</span> C_kpt_freerangevm_impl<span class="token punctuation">;</span> + vm<span class="token operator">-></span>kpt_alloc <span class="token operator">=</span> C_kpt_allocvm_impl<span class="token punctuation">;</span> +<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> +__code <span class="token function">init_vmmvm_impl</span><span class="token punctuation">(</span><span class="token keyword">struct</span> vm_impl<span class="token operator">*</span> vm<span class="token punctuation">,</span>__code <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token function">initlock</span><span class="token punctuation">(</span><span class="token operator">&</span>kpt_mem<span class="token punctuation">.</span>lock<span class="token punctuation">,</span> <span class="token string">"vm"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + kpt_mem<span class="token punctuation">.</span>freelist <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span> + + <span class="token keyword">goto</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token punctuation">}</span> +</code></pre><hr><h1 id="private" style=""><a class="anchor hidden-xs" href="#private" title="private"><span class="octicon octicon-link"></span></a>private</h1><ul> +<li>CbC は信頼性を保証するためにそれぞれの Code Gear を細かくする必要があるので、for文やif文がある場合はさらに実装を分ける</li> +<li>Code Gear は基本的にインターフェースで指定された Code Gear 内からのみ継続さ れるため、Java の private メソッドのように扱われる。</li> +<li>実際に vm.c の loaduvm の実装を分けた記述を説明する</li> +</ul><pre><code class="c hljs"><span class="token keyword">int</span> loaduvm <span class="token punctuation">(</span>pde_t <span class="token operator">*</span>pgdir<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span>addr<span class="token punctuation">,</span> <span class="token keyword">struct</span> inode <span class="token operator">*</span>ip<span class="token punctuation">,</span> uint offset<span class="token punctuation">,</span> uint sz<span class="token punctuation">)</span> +<span class="token punctuation">{</span> + uint i<span class="token punctuation">,</span> pa<span class="token punctuation">,</span> n<span class="token punctuation">;</span> + pte_t <span class="token operator">*</span>pte<span class="token punctuation">;</span> + + <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>uint<span class="token punctuation">)</span> addr <span class="token operator">%</span> PTE_SZ <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token function">panic</span><span class="token punctuation">(</span><span class="token string">"loaduvm: addr must be page aligned"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + <span class="token punctuation">}</span> + + <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> sz<span class="token punctuation">;</span> i <span class="token operator">+</span><span class="token operator">=</span> PTE_SZ<span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>pte <span class="token operator">=</span> <span class="token function">walkpgdir</span><span class="token punctuation">(</span>pgdir<span class="token punctuation">,</span> addr <span class="token operator">+</span> i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token function">panic</span><span class="token punctuation">(</span><span class="token string">"loaduvm: address should exist"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + <span class="token punctuation">}</span> + + pa <span class="token operator">=</span> <span class="token function">PTE_ADDR</span><span class="token punctuation">(</span><span class="token operator">*</span>pte<span class="token punctuation">)</span><span class="token punctuation">;</span> + + <span class="token keyword">if</span> <span class="token punctuation">(</span>sz <span class="token operator">-</span> i <span class="token operator"><</span> PTE_SZ<span class="token punctuation">)</span> <span class="token punctuation">{</span> + n <span class="token operator">=</span> sz <span class="token operator">-</span> i<span class="token punctuation">;</span> + <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> + n <span class="token operator">=</span> PTE_SZ<span class="token punctuation">;</span> + <span class="token punctuation">}</span> + + <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">readi</span><span class="token punctuation">(</span>ip<span class="token punctuation">,</span> <span class="token function">p2v</span><span class="token punctuation">(</span>pa<span class="token punctuation">)</span><span class="token punctuation">,</span> offset <span class="token operator">+</span> i<span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token operator">!=</span> n<span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> + <span class="token punctuation">}</span> + <span class="token punctuation">}</span> + + <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> +<span class="token punctuation">}</span> +</code></pre><hr><h1 id="goto-private" style=""><a class="anchor hidden-xs" href="#goto-private" title="goto-private"><span class="octicon octicon-link"></span></a>goto private</h1><ul> +<li>vm と同じ create_impl 内で vm_impl を定義し、private で実装する Code Gear を定義する</li> +<li>loaduvmvm_impl で goto によって private に遷移する</li> +</ul><pre><code class="c hljs">vm<span class="token operator">*</span> <span class="token function">createvm_impl</span><span class="token punctuation">(</span><span class="token keyword">struct</span> Context<span class="token operator">*</span> cbc_context<span class="token punctuation">)</span> <span class="token punctuation">{</span> +<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> + <span class="token keyword">struct</span> vm_impl<span class="token operator">*</span> vm_impl <span class="token operator">=</span> new <span class="token function">vm_impl</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> + vm_impl<span class="token operator">-></span>loaduvm_ptesize_check <span class="token operator">=</span> C_loaduvm_ptesize_checkvm_impl<span class="token punctuation">;</span> +<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> + vm<span class="token operator">-></span>loaduvm <span class="token operator">=</span> C_loaduvmvm_impl<span class="token punctuation">;</span> +<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> +<span class="token punctuation">}</span> + +__code <span class="token function">loaduvmvm_impl</span><span class="token punctuation">(</span><span class="token keyword">struct</span> vm_impl<span class="token operator">*</span> vm<span class="token punctuation">,</span> pde_t<span class="token operator">*</span> pgdir<span class="token punctuation">,</span> <span class="token keyword">char</span><span class="token operator">*</span> addr<span class="token punctuation">,</span> <span class="token keyword">struct</span> inode<span class="token operator">*</span> ip<span class="token punctuation">,</span> uint offset<span class="token punctuation">,</span> uint sz<span class="token punctuation">,</span> __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token function">Gearef</span><span class="token punctuation">(</span>cbc_context<span class="token punctuation">,</span> vm_impl<span class="token punctuation">)</span><span class="token operator">-></span>pgdir <span class="token operator">=</span> pgdir<span class="token punctuation">;</span> + <span class="token function">Gearef</span><span class="token punctuation">(</span>cbc_context<span class="token punctuation">,</span> vm_impl<span class="token punctuation">)</span><span class="token operator">-></span>addr <span class="token operator">=</span> addr<span class="token punctuation">;</span> + <span class="token function">Gearef</span><span class="token punctuation">(</span>cbc_context<span class="token punctuation">,</span> vm_impl<span class="token punctuation">)</span><span class="token operator">-></span>ip <span class="token operator">=</span> ip<span class="token punctuation">;</span> + <span class="token function">Gearef</span><span class="token punctuation">(</span>cbc_context<span class="token punctuation">,</span> vm_impl<span class="token punctuation">)</span><span class="token operator">-></span>offset <span class="token operator">=</span> offset<span class="token punctuation">;</span> + <span class="token function">Gearef</span><span class="token punctuation">(</span>cbc_context<span class="token punctuation">,</span> vm_impl<span class="token punctuation">)</span><span class="token operator">-></span>sz <span class="token operator">=</span> sz<span class="token punctuation">;</span> + <span class="token function">Gearef</span><span class="token punctuation">(</span>cbc_context<span class="token punctuation">,</span> vm_impl<span class="token punctuation">)</span><span class="token operator">-></span>next <span class="token operator">=</span> next<span class="token punctuation">;</span> + + <span class="token keyword">goto</span> <span class="token function">loaduvm_ptesize_checkvm_impl</span><span class="token punctuation">(</span>vm<span class="token punctuation">,</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token punctuation">}</span> +</code></pre><hr><h1 id="private-のヘッダー" style=""><a class="anchor hidden-xs" href="#private-のヘッダー" title="private-のヘッダー"><span class="octicon octicon-link"></span></a>private のヘッダー</h1><ul> +<li>private 側のヘッダーファイルも vm_impl と同じように用意する</li> +</ul><pre><code class="c hljs"><span class="token keyword">typedef</span> <span class="token keyword">struct</span> vm_impl<span class="token operator"><</span>Impl<span class="token punctuation">,</span> Isa<span class="token operator">></span> impl vm<span class="token punctuation">{</span> +<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> + __code <span class="token function">loaduvm_ptesize_check</span><span class="token punctuation">(</span>Type<span class="token operator">*</span> vm_impl<span class="token punctuation">,</span> uint i<span class="token punctuation">,</span> pte_t<span class="token operator">*</span> pte<span class="token punctuation">,</span> uint sz<span class="token punctuation">,</span> +__code <span class="token function">next</span><span class="token punctuation">(</span><span class="token keyword">int</span> ret<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> +</code></pre><hr><h1 id="private-の記述" style=""><a class="anchor hidden-xs" href="#private-の記述" title="private-の記述"><span class="octicon octicon-link"></span></a>private の記述</h1><pre><code class="c hljs"><span class="token macro property">#interface "vm_impl.h"</span> + +__code <span class="token function">loaduvm_ptesize_checkvm_impl</span><span class="token punctuation">(</span><span class="token keyword">struct</span> vm_impl<span class="token operator">*</span> vm_impl<span class="token punctuation">,</span> __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token keyword">int</span> ret<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token keyword">char</span><span class="token operator">*</span> addr <span class="token operator">=</span> vm_impl<span class="token operator">-></span>addr<span class="token punctuation">;</span> + + <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>uint<span class="token punctuation">)</span> addr <span class="token operator">%</span>PTE_SZ <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token comment">// goto panic</span> + <span class="token punctuation">}</span> + + <span class="token keyword">goto</span> <span class="token function">loaduvm_loopvm_impl</span><span class="token punctuation">(</span>vm_impl<span class="token punctuation">,</span> <span class="token function">next</span><span class="token punctuation">(</span>ret<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token punctuation">}</span> +</code></pre><ul> +<li>vm.cではここから for だが CbC は if文の中と外にgoto を用意して実装する</li> +</ul><p><img src="https://i.imgur.com/ByA7GVJ.png" alt=""></p><pre><code class="c hljs">__code <span class="token function">loaduvm_loopvm_impl</span><span class="token punctuation">(</span><span class="token keyword">struct</span> vm_impl<span class="token operator">*</span> vm_impl<span class="token punctuation">,</span> __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token keyword">int</span> ret<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + uint i <span class="token operator">=</span> vm_impl<span class="token operator">-></span>i<span class="token punctuation">;</span> + uint sz <span class="token operator">=</span> vm_impl<span class="token operator">-></span>sz<span class="token punctuation">;</span> + + <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator"><</span> sz<span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token keyword">goto</span> <span class="token function">loaduvm_check_pgdir</span><span class="token punctuation">(</span>vm_impl<span class="token punctuation">,</span> <span class="token function">next</span><span class="token punctuation">(</span>ret<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + <span class="token punctuation">}</span> + + <span class="token keyword">goto</span> <span class="token function">loaduvm_exit</span><span class="token punctuation">(</span>vm_impl<span class="token punctuation">,</span> <span class="token function">next</span><span class="token punctuation">(</span>ret<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token punctuation">}</span> + + +__code <span class="token function">loaduvm_check_pgdir</span><span class="token punctuation">(</span><span class="token keyword">struct</span> vm_impl<span class="token operator">*</span> vm_impl<span class="token punctuation">,</span> __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token keyword">int</span> ret<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + pte_t<span class="token operator">*</span> pte <span class="token operator">=</span> vm_impl<span class="token operator">-></span>pte<span class="token punctuation">;</span> + pde_t<span class="token operator">*</span> pgdir <span class="token operator">=</span> vm_impl<span class="token operator">-></span>pgdir<span class="token punctuation">;</span> + uint i <span class="token operator">=</span> vm_impl<span class="token operator">-></span>i<span class="token punctuation">;</span> + <span class="token keyword">char</span><span class="token operator">*</span> addr <span class="token operator">=</span> vm_impl<span class="token operator">-></span>addr<span class="token punctuation">;</span> + uint pa <span class="token operator">=</span> vm_impl<span class="token operator">-></span>pa<span class="token punctuation">;</span> + + <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>pte <span class="token operator">=</span> <span class="token function">walkpgdir</span><span class="token punctuation">(</span>pgdir<span class="token punctuation">,</span> addr <span class="token operator">+</span> i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token comment">// goto panic</span> + <span class="token punctuation">}</span> + pa <span class="token operator">=</span> <span class="token function">PTE_ADDR</span><span class="token punctuation">(</span><span class="token operator">*</span>pte<span class="token punctuation">)</span><span class="token punctuation">;</span> + + vm_impl<span class="token operator">-></span>pte <span class="token operator">=</span> pte<span class="token punctuation">;</span> + vm_impl<span class="token operator">-></span>pgdir <span class="token operator">=</span> pgdir<span class="token punctuation">;</span> + vm_impl<span class="token operator">-></span>addr <span class="token operator">=</span> addr<span class="token punctuation">;</span> + vm_impl<span class="token operator">-></span>pa <span class="token operator">=</span> pa<span class="token punctuation">;</span> + + <span class="token keyword">goto</span> <span class="token function">loaduvm_check_PTE_SZ</span><span class="token punctuation">(</span>vm_impl<span class="token punctuation">,</span> <span class="token function">next</span><span class="token punctuation">(</span>ret<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token punctuation">}</span> + +__code <span class="token function">loaduvm_check_PTE_SZ</span><span class="token punctuation">(</span><span class="token keyword">struct</span> vm_impl<span class="token operator">*</span> vm_impl<span class="token punctuation">,</span> __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token keyword">int</span> ret<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + uint sz <span class="token operator">=</span> vm_impl<span class="token operator">-></span>sz<span class="token punctuation">;</span> + uint i <span class="token operator">=</span> vm_impl<span class="token operator">-></span>i<span class="token punctuation">;</span> + uint n <span class="token operator">=</span> vm_impl<span class="token operator">-></span>n<span class="token punctuation">;</span> + <span class="token keyword">struct</span> inode<span class="token operator">*</span> ip <span class="token operator">=</span> vm_impl<span class="token operator">-></span>ip<span class="token punctuation">;</span> + uint pa <span class="token operator">=</span> vm_impl<span class="token operator">-></span>pa<span class="token punctuation">;</span> + uint offset <span class="token operator">=</span> vm_impl<span class="token operator">-></span>offset<span class="token punctuation">;</span> + + <span class="token keyword">if</span> <span class="token punctuation">(</span>sz <span class="token operator">-</span> i <span class="token operator"><</span> PTE_SZ<span class="token punctuation">)</span> <span class="token punctuation">{</span> + n <span class="token operator">=</span> sz <span class="token operator">-</span> i<span class="token punctuation">;</span> + <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> + n <span class="token operator">=</span> PTE_SZ<span class="token punctuation">;</span> + <span class="token punctuation">}</span> + + <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">readi</span><span class="token punctuation">(</span>ip<span class="token punctuation">,</span> <span class="token function">p2v</span><span class="token punctuation">(</span>pa<span class="token punctuation">)</span><span class="token punctuation">,</span> offset <span class="token operator">+</span> i<span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token operator">!=</span> n<span class="token punctuation">)</span> <span class="token punctuation">{</span> + ret <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> + <span class="token keyword">goto</span> <span class="token function">next</span><span class="token punctuation">(</span>ret<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + <span class="token punctuation">}</span> + + vm_impl<span class="token operator">-></span>n <span class="token operator">=</span> n<span class="token punctuation">;</span> + + <span class="token keyword">goto</span> <span class="token function">loaduvm_loopvm_impl</span><span class="token punctuation">(</span>vm_impl<span class="token punctuation">,</span> <span class="token function">next</span><span class="token punctuation">(</span>ret<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token punctuation">}</span> + +__code <span class="token function">loaduvm_exit</span><span class="token punctuation">(</span><span class="token keyword">struct</span> vm_impl<span class="token operator">*</span> vm_impl<span class="token punctuation">,</span> __code <span class="token function">next</span><span class="token punctuation">(</span><span class="token keyword">int</span> ret<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + ret <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> + <span class="token keyword">goto</span> <span class="token function">next</span><span class="token punctuation">(</span>ret<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token punctuation">}</span> +</code></pre><pre><code class="c hljs"> +<span class="token keyword">int</span> loaduvm <span class="token punctuation">(</span>pde_t <span class="token operator">*</span>pgdir<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span>addr<span class="token punctuation">,</span> <span class="token keyword">struct</span> inode <span class="token operator">*</span>ip<span class="token punctuation">,</span> uint offset<span class="token punctuation">,</span> uint sz<span class="token punctuation">)</span> +<span class="token punctuation">{</span> + uint i<span class="token punctuation">,</span> pa<span class="token punctuation">,</span> n<span class="token punctuation">;</span> + pte_t <span class="token operator">*</span>pte<span class="token punctuation">;</span> + + <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>uint<span class="token punctuation">)</span> addr <span class="token operator">%</span> PTE_SZ <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token function">panic</span><span class="token punctuation">(</span><span class="token string">"loaduvm: addr must be page aligned"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + <span class="token punctuation">}</span> + + <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> sz<span class="token punctuation">;</span> i <span class="token operator">+</span><span class="token operator">=</span> PTE_SZ<span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>pte <span class="token operator">=</span> <span class="token function">walkpgdir</span><span class="token punctuation">(</span>pgdir<span class="token punctuation">,</span> addr <span class="token operator">+</span> i<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token function">panic</span><span class="token punctuation">(</span><span class="token string">"loaduvm: address should exist"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + <span class="token punctuation">}</span> + + pa <span class="token operator">=</span> <span class="token function">PTE_ADDR</span><span class="token punctuation">(</span><span class="token operator">*</span>pte<span class="token punctuation">)</span><span class="token punctuation">;</span> + + <span class="token keyword">if</span> <span class="token punctuation">(</span>sz <span class="token operator">-</span> i <span class="token operator"><</span> PTE_SZ<span class="token punctuation">)</span> <span class="token punctuation">{</span> + n <span class="token operator">=</span> sz <span class="token operator">-</span> i<span class="token punctuation">;</span> + <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> + n <span class="token operator">=</span> PTE_SZ<span class="token punctuation">;</span> + <span class="token punctuation">}</span> + + <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">readi</span><span class="token punctuation">(</span>ip<span class="token punctuation">,</span> <span class="token function">p2v</span><span class="token punctuation">(</span>pa<span class="token punctuation">)</span><span class="token punctuation">,</span> offset <span class="token operator">+</span> i<span class="token punctuation">,</span> n<span class="token punctuation">)</span> <span class="token operator">!=</span> n<span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> + <span class="token punctuation">}</span> + <span class="token punctuation">}</span> + + <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> +<span class="token punctuation">}</span> +</code></pre><h1 id="インターフェースの呼び出し" style=""><a class="anchor hidden-xs" href="#インターフェースの呼び出し" title="インターフェースの呼び出し"><span class="octicon octicon-link"></span></a>インターフェースの呼び出し</h1><ul> +<li>定義したインターフェースの呼び出しについて説明する</li> +<li>CbC の場合 goto による 遷移を行うので、関数呼び出しのように goto 以降のコードを実行できない</li> +<li>例) goto すると戻ってこれないため それ以降が実行されなくなる。</li> +</ul><pre><code class="c hljs"><span class="token keyword">void</span> <span class="token function">userinit</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span> +<span class="token punctuation">{</span> + <span class="token keyword">struct</span> proc<span class="token operator">*</span> p<span class="token punctuation">;</span> + <span class="token keyword">extern</span> <span class="token keyword">char</span> _binary_initcode_start<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> _binary_initcode_size<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> + + p <span class="token operator">=</span> <span class="token function">allocproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + <span class="token function">initContext</span><span class="token punctuation">(</span><span class="token operator">&</span>p<span class="token operator">-></span>cbc_context<span class="token punctuation">)</span><span class="token punctuation">;</span> + + initproc <span class="token operator">=</span> p<span class="token punctuation">;</span> + + <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>p<span class="token operator">-></span>pgdir <span class="token operator">=</span> <span class="token function">kpt_alloc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token function">panic</span><span class="token punctuation">(</span><span class="token string">"userinit: out of memory?"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + <span class="token punctuation">}</span> + + <span class="token keyword">goto</span> <span class="token function">cbc_init_vmm_dummy</span><span class="token punctuation">(</span><span class="token operator">&</span>p<span class="token operator">-></span>cbc_context<span class="token punctuation">,</span> p<span class="token punctuation">,</span> p<span class="token operator">-></span>pgdir<span class="token punctuation">,</span> _binary_initcode_start<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span>_binary_initcode_size<span class="token punctuation">)</span><span class="token punctuation">;</span> + p<span class="token operator">-></span>sz <span class="token operator">=</span> PTE_SZ<span class="token punctuation">;</span> + + <span class="token comment">// craft the trapframe as if</span> + <span class="token function">memset</span><span class="token punctuation">(</span>p<span class="token operator">-></span>tf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token operator">*</span>p<span class="token operator">-></span>tf<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token operator">~</span> +</code></pre><h1 id="呼び出しの解決" style=""><a class="anchor hidden-xs" href="#呼び出しの解決" title="呼び出しの解決"><span class="octicon octicon-link"></span></a>呼び出しの解決</h1><ul> +<li>最初の命令は next で戻ってこれるので、dummy の関数を用意してそこで実行する</li> +</ul><pre><code class="c hljs"><span class="token keyword">void</span> <span class="token function">dummy</span><span class="token punctuation">(</span><span class="token keyword">struct</span> proc <span class="token operator">*</span>p<span class="token punctuation">,</span> <span class="token keyword">char</span> _binary_initcode_start<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">char</span> _binary_initcode_size<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> +<span class="token punctuation">{</span> + <span class="token comment">// inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size);</span> + <span class="token keyword">goto</span> <span class="token function">cbc_init_vmm_dummy</span><span class="token punctuation">(</span><span class="token operator">&</span>p<span class="token operator">-></span>cbc_context<span class="token punctuation">,</span> p<span class="token punctuation">,</span> p<span class="token operator">-></span>pgdir<span class="token punctuation">,</span> _binary_initcode_start<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span>_binary_initcode_size<span class="token punctuation">)</span><span class="token punctuation">;</span> + +<span class="token punctuation">}</span> + + + +__ncode <span class="token function">cbc_init_vmm_dummy</span><span class="token punctuation">(</span><span class="token keyword">struct</span> Context<span class="token operator">*</span> cbc_context<span class="token punctuation">,</span> <span class="token keyword">struct</span> proc<span class="token operator">*</span> p<span class="token punctuation">,</span> pde_t<span class="token operator">*</span> pgdir<span class="token punctuation">,</span> <span class="token keyword">char</span><span class="token operator">*</span> init<span class="token punctuation">,</span> uint sz<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//:skip</span> + + <span class="token keyword">struct</span> vm<span class="token operator">*</span> vm <span class="token operator">=</span> <span class="token function">createvm_impl</span><span class="token punctuation">(</span>cbc_context<span class="token punctuation">)</span><span class="token punctuation">;</span> + <span class="token comment">// goto vm->init_vmm(vm, pgdir, init, sz , vm->void_ret);</span> + <span class="token function">Gearef</span><span class="token punctuation">(</span>cbc_context<span class="token punctuation">,</span> vm<span class="token punctuation">)</span><span class="token operator">-></span>vm <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">union</span> Data<span class="token operator">*</span><span class="token punctuation">)</span> vm<span class="token punctuation">;</span> + <span class="token function">Gearef</span><span class="token punctuation">(</span>cbc_context<span class="token punctuation">,</span> vm<span class="token punctuation">)</span><span class="token operator">-></span>pgdir <span class="token operator">=</span> pgdir<span class="token punctuation">;</span> + <span class="token function">Gearef</span><span class="token punctuation">(</span>cbc_context<span class="token punctuation">,</span> vm<span class="token punctuation">)</span><span class="token operator">-></span>init <span class="token operator">=</span> init<span class="token punctuation">;</span> + <span class="token function">Gearef</span><span class="token punctuation">(</span>cbc_context<span class="token punctuation">,</span> vm<span class="token punctuation">)</span><span class="token operator">-></span>sz <span class="token operator">=</span> sz <span class="token punctuation">;</span> + <span class="token function">Gearef</span><span class="token punctuation">(</span>cbc_context<span class="token punctuation">,</span> vm<span class="token punctuation">)</span><span class="token operator">-></span>next <span class="token operator">=</span> C_vm_void_ret <span class="token punctuation">;</span> + <span class="token keyword">goto</span> <span class="token function">meta</span><span class="token punctuation">(</span>cbc_context<span class="token punctuation">,</span> vm<span class="token operator">-></span>init_inituvm<span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token punctuation">}</span> + + +<span class="token keyword">void</span> <span class="token function">userinit</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span> +<span class="token punctuation">{</span> + <span class="token keyword">struct</span> proc<span class="token operator">*</span> p<span class="token punctuation">;</span> + <span class="token keyword">extern</span> <span class="token keyword">char</span> _binary_initcode_start<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> _binary_initcode_size<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> + + p <span class="token operator">=</span> <span class="token function">allocproc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + <span class="token function">initContext</span><span class="token punctuation">(</span><span class="token operator">&</span>p<span class="token operator">-></span>cbc_context<span class="token punctuation">)</span><span class="token punctuation">;</span> + + initproc <span class="token operator">=</span> p<span class="token punctuation">;</span> + + <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>p<span class="token operator">-></span>pgdir <span class="token operator">=</span> <span class="token function">kpt_alloc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> + <span class="token function">panic</span><span class="token punctuation">(</span><span class="token string">"userinit: out of memory?"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> + <span class="token punctuation">}</span> + + <span class="token function">dummy</span><span class="token punctuation">(</span>p<span class="token punctuation">,</span> _binary_initcode_start<span class="token punctuation">,</span> _binary_initcode_size<span class="token punctuation">)</span><span class="token punctuation">;</span> +<span class="token operator">~</span> + +</code></pre></div> <div class="ui-toc dropup unselectable hidden-print" style="display:none;"> <div class="pull-right dropdown"> <a id="tocLabel" class="ui-toc-label btn btn-default" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false" title="Table of content"> @@ -278,8 +581,8 @@ </a> <ul id="ui-toc" class="ui-toc-dropdown dropdown-menu" aria-labelledby="tocLabel"> <div class="toc"><ul class="nav"> -<li><a href="#CbCインターフェースによる-CbCXv6-の書き換え" title="CbCインターフェースによる CbCXv6 の書き換え">CbCインターフェースによる CbCXv6 の書き換え</a></li> -<li><a href="#目次0" title="目次">目次</a></li> +<li class=""><a href="#CbCインターフェースによる-CbCXv6-の書き換え" title="CbCインターフェースによる CbCXv6 の書き換え">CbCインターフェースによる CbCXv6 の書き換え</a></li> +<li><a href="#目次" title="目次">目次</a></li> <li><a href="#OS-の信頼性の重要性" title="OS の信頼性の重要性">OS の信頼性の重要性</a></li> <li><a href="#OS-の信頼性の重要性1" title="OS の信頼性の重要性">OS の信頼性の重要性</a></li> <li><a href="#メタレベルとノーマルレベル" title="メタレベルとノーマルレベル">メタレベルとノーマルレベル</a></li> @@ -291,28 +594,44 @@ <li><a href="#Agda-による検証" title="Agda による検証">Agda による検証</a></li> <li><a href="#Haure-Logic" title="Haure Logic">Haure Logic</a></li> <li><a href="#Geas-OS" title="Geas OS">Geas OS</a></li> -<li><a href="#メモリ管理0" title="メモリ管理">メモリ管理</a></li> +<li><a href="#メモリ管理" title="メモリ管理">メモリ管理</a></li> <li><a href="#インターフェース" title="インターフェース">インターフェース</a></li> -<li><a href="#目次0" title="目次">目次</a></li> +<li><a href="#目次1" title="目次">目次</a></li> <li><a href="#CbC-による-Gears-OS-の開発" title="CbC による Gears OS の開発">CbC による Gears OS の開発</a></li> <li><a href="#Context" title="Context">Context</a></li> -<li><a href="#目次1" title="目次">目次</a></li> -<li><a href="#Xv60" title="Xv6">Xv6</a></li> +<li><a href="#目次2" title="目次">目次</a></li> +<li><a href="#Xv6" title="Xv6">Xv6</a></li> <li><a href="#カーネル空間" title="カーネル空間">カーネル空間</a></li> <li><a href="#カーネルが提供するもの" title="カーネルが提供するもの">カーネルが提供するもの</a></li> <li><a href="#カーネルの保護機構" title="カーネルの保護機構">カーネルの保護機構</a></li> -<li><a href="#system-call0" title="system call">system call</a></li> <li><a href="#system-call" title="system call">system call</a></li> +<li><a href="#system-call1" title="system call">system call</a></li> <li><a href="#Xv6-rpi" title="Xv6-rpi">Xv6-rpi</a></li> -<li><a href="#目次0" title="目次">目次</a></li> -<li><a href="#CbCXv6-での-Paging0" title="CbCXv6 での Paging">CbCXv6 での Paging</a></li> +<li><a href="#目次3" title="目次">目次</a></li> +<li><a href="#CbCXv6-での-Paging" title="CbCXv6 での Paging">CbCXv6 での Paging</a></li> <li><a href="#実メモリの直接操作" title="実メモリの直接操作">実メモリの直接操作</a></li> -<li><a href="#Paging0" title="Paging">Paging</a></li> +<li><a href="#Paging" title="Paging">Paging</a></li> <li><a href="#Pagingの図" title="Pagingの図">Pagingの図</a></li> <li><a href="#メタレベルでの-Paging-の操作" title="メタレベルでの Paging の操作">メタレベルでの Paging の操作</a></li> -<li class=""><a href="#Paging-の信頼性" title="Paging の信頼性">Paging の信頼性</a></li> +<li><a href="#Paging-の信頼性" title="Paging の信頼性">Paging の信頼性</a></li> <li><a href="#Paging-の書き換え" title="Paging の書き換え">Paging の書き換え</a></li> -<li><a href="#目次" title="目次">目次</a></li> +<li><a href="#目次4" title="目次">目次</a></li> +<li><a href="#CbC-インターフェースの導入" title="CbC インターフェースの導入">CbC インターフェースの導入</a></li> +<li><a href="#CbC-インターフェース" title="CbC インターフェース">CbC インターフェース</a></li> +<li><a href="#インターフェースの定義" title="インターフェースの定義">インターフェースの定義</a></li> +<li><a href="#インターフェースのソースコード" title="インターフェースのソースコード">インターフェースのソースコード</a></li> +<li><a href="#実装名の定義" title="実装名の定義">実装名の定義</a></li> +<li><a href="#Data-Gear-の定義" title="Data Gear の定義">Data Gear の定義</a></li> +<li><a href="#Code-Gear-の定義" title="Code Gear の定義">Code Gear の定義</a></li> +<li><a href="#next…" title="next(…)">next(…)</a></li> +<li><a href="#インターフェースの実装" title="インターフェースの実装">インターフェースの実装</a></li> +<li><a href="#create_impl" title="create_impl">create_impl</a></li> +<li><a href="#private" title="private">private</a></li> +<li><a href="#goto-private" title="goto private">goto private</a></li> +<li><a href="#private-のヘッダー" title="private のヘッダー">private のヘッダー</a></li> +<li class=""><a href="#private-の記述" title="private の記述">private の記述</a></li> +<li class=""><a href="#インターフェースの呼び出し" title="インターフェースの呼び出し">インターフェースの呼び出し</a></li> +<li class=""><a href="#呼び出しの解決" title="呼び出しの解決">呼び出しの解決</a></li> </ul> </div><div class="toc-menu"><a class="expand-toggle" href="#">Expand all</a><a class="back-to-top" href="#">Back to top</a><a class="go-to-bottom" href="#">Go to bottom</a></div> </ul> @@ -320,8 +639,8 @@ </div> <div id="ui-toc-affix" class="ui-affix-toc ui-toc-dropdown unselectable hidden-print" data-spy="affix" style="top:17px;display:none;" null null> <div class="toc"><ul class="nav"> -<li><a href="#CbCインターフェースによる-CbCXv6-の書き換え" title="CbCインターフェースによる CbCXv6 の書き換え">CbCインターフェースによる CbCXv6 の書き換え</a></li> -<li><a href="#目次0" title="目次">目次</a></li> +<li class=""><a href="#CbCインターフェースによる-CbCXv6-の書き換え" title="CbCインターフェースによる CbCXv6 の書き換え">CbCインターフェースによる CbCXv6 の書き換え</a></li> +<li><a href="#目次" title="目次">目次</a></li> <li><a href="#OS-の信頼性の重要性" title="OS の信頼性の重要性">OS の信頼性の重要性</a></li> <li><a href="#OS-の信頼性の重要性1" title="OS の信頼性の重要性">OS の信頼性の重要性</a></li> <li><a href="#メタレベルとノーマルレベル" title="メタレベルとノーマルレベル">メタレベルとノーマルレベル</a></li> @@ -333,28 +652,44 @@ <li><a href="#Agda-による検証" title="Agda による検証">Agda による検証</a></li> <li><a href="#Haure-Logic" title="Haure Logic">Haure Logic</a></li> <li><a href="#Geas-OS" title="Geas OS">Geas OS</a></li> -<li><a href="#メモリ管理0" title="メモリ管理">メモリ管理</a></li> +<li><a href="#メモリ管理" title="メモリ管理">メモリ管理</a></li> <li><a href="#インターフェース" title="インターフェース">インターフェース</a></li> -<li><a href="#目次0" title="目次">目次</a></li> +<li><a href="#目次1" title="目次">目次</a></li> <li><a href="#CbC-による-Gears-OS-の開発" title="CbC による Gears OS の開発">CbC による Gears OS の開発</a></li> <li><a href="#Context" title="Context">Context</a></li> -<li><a href="#目次1" title="目次">目次</a></li> -<li><a href="#Xv60" title="Xv6">Xv6</a></li> +<li><a href="#目次2" title="目次">目次</a></li> +<li><a href="#Xv6" title="Xv6">Xv6</a></li> <li><a href="#カーネル空間" title="カーネル空間">カーネル空間</a></li> <li><a href="#カーネルが提供するもの" title="カーネルが提供するもの">カーネルが提供するもの</a></li> <li><a href="#カーネルの保護機構" title="カーネルの保護機構">カーネルの保護機構</a></li> -<li><a href="#system-call0" title="system call">system call</a></li> <li><a href="#system-call" title="system call">system call</a></li> +<li><a href="#system-call1" title="system call">system call</a></li> <li><a href="#Xv6-rpi" title="Xv6-rpi">Xv6-rpi</a></li> -<li><a href="#目次0" title="目次">目次</a></li> -<li><a href="#CbCXv6-での-Paging0" title="CbCXv6 での Paging">CbCXv6 での Paging</a></li> +<li><a href="#目次3" title="目次">目次</a></li> +<li><a href="#CbCXv6-での-Paging" title="CbCXv6 での Paging">CbCXv6 での Paging</a></li> <li><a href="#実メモリの直接操作" title="実メモリの直接操作">実メモリの直接操作</a></li> -<li><a href="#Paging0" title="Paging">Paging</a></li> +<li><a href="#Paging" title="Paging">Paging</a></li> <li><a href="#Pagingの図" title="Pagingの図">Pagingの図</a></li> <li><a href="#メタレベルでの-Paging-の操作" title="メタレベルでの Paging の操作">メタレベルでの Paging の操作</a></li> -<li class=""><a href="#Paging-の信頼性" title="Paging の信頼性">Paging の信頼性</a></li> +<li><a href="#Paging-の信頼性" title="Paging の信頼性">Paging の信頼性</a></li> <li><a href="#Paging-の書き換え" title="Paging の書き換え">Paging の書き換え</a></li> -<li><a href="#目次" title="目次">目次</a></li> +<li><a href="#目次4" title="目次">目次</a></li> +<li><a href="#CbC-インターフェースの導入" title="CbC インターフェースの導入">CbC インターフェースの導入</a></li> +<li><a href="#CbC-インターフェース" title="CbC インターフェース">CbC インターフェース</a></li> +<li><a href="#インターフェースの定義" title="インターフェースの定義">インターフェースの定義</a></li> +<li><a href="#インターフェースのソースコード" title="インターフェースのソースコード">インターフェースのソースコード</a></li> +<li><a href="#実装名の定義" title="実装名の定義">実装名の定義</a></li> +<li><a href="#Data-Gear-の定義" title="Data Gear の定義">Data Gear の定義</a></li> +<li><a href="#Code-Gear-の定義" title="Code Gear の定義">Code Gear の定義</a></li> +<li><a href="#next…" title="next(…)">next(…)</a></li> +<li><a href="#インターフェースの実装" title="インターフェースの実装">インターフェースの実装</a></li> +<li><a href="#create_impl" title="create_impl">create_impl</a></li> +<li><a href="#private" title="private">private</a></li> +<li><a href="#goto-private" title="goto private">goto private</a></li> +<li><a href="#private-のヘッダー" title="private のヘッダー">private のヘッダー</a></li> +<li class=""><a href="#private-の記述" title="private の記述">private の記述</a></li> +<li class=""><a href="#インターフェースの呼び出し" title="インターフェースの呼び出し">インターフェースの呼び出し</a></li> +<li class=""><a href="#呼び出しの解決" title="呼び出しの解決">呼び出しの解決</a></li> </ul> </div><div class="toc-menu"><a class="expand-toggle" href="#">Expand all</a><a class="back-to-top" href="#">Back to top</a><a class="go-to-bottom" href="#">Go to bottom</a></div> </div>
--- a/slide/CbCXv6.md Fri Feb 07 20:06:13 2020 +0900 +++ b/slide/CbCXv6.md Sat Feb 08 15:06:50 2020 +0900 @@ -62,6 +62,7 @@ ![](https://i.imgur.com/etfQund.png) + --- # Data Gear の継続 @@ -279,13 +280,409 @@ 1. OS の信頼性の保証 2. CbC による Gears OS の開発 3. Xv6 -4. CbCXv6 での Paging** +4. CbCXv6 での Paging 5. **CbC インターフェース** 6. 評価 7. まとめ --- +# CbC インターフェースの導入 +- 継続の記述が煩雑になる + - Code Gear がどの Data Gear の番号に対応するか指定する必要がある + - ノーマルレベルとメタレベルで Data Gear の見え方が異なるため調整する必要がある +- ->インターフェースの導入 + + +--- + +# CbC インターフェース +- インターフェースは Data Gear に対しての操作を行う Code Gear +- 実装は別で定義し、呼び出す +- インターフェースによって機能を置き換えることができる + +--- + +# インターフェースの定義 +- Data Gear と Data Gear に対して操作を行う Code Gear の集合を表現する **Meta Data Gear** + +--- + +# インターフェースのソースコード +- vm.c をインターフェースで書き換えた vm.h のコードの説明をしていく + + +--- + + +# 実装名の定義 +- typedef struct の直後に実装名(vm)を書く + + +``` c +typedef struct vm<Type,Impl> { +``` + +--- + +# Data Gear の定義 +- 2行目から19行目で引数の Data Gear を定義している + + +``` c +typedef struct vm<Type,Impl> { + union Data* vm; + uint low; + uint hi; + struct proc* p; +.... +``` + +--- + +# Code Gear の定義 +- Code Gear は __Code CodeGear名(引数); で記述する +- 第1引数の Impl* vm が Code Gear の型になる +- 初期化された Data Gear が それぞれの Code Gear の引数として扱われる +- 例)定義された uinit が kpt_freerange の Code Gear の第1引数と対応している + + +``` c + __code init_vmm(Impl* vm, __code next(...)); + __code kpt_freerange(Impl* vm, uint low, uint hi, __code next(...)); + __code kpt_alloc(Impl* vm ,__code next(...)); + __code switchuvm(Impl* vm ,struct proc* p, __code next(...)); + __code init_inituvm(Impl* vm, pde_t* pgdir, char* init, uint sz, __code next(...)); + __code loaduvm(Impl* vm,pde_t* pgdir, char* addr, struct inode* ip, uint offset, uint sz, __code next(...)); + __code allocuvm(Impl* vm, pde_t* pgdir, uint oldsz, uint newsz, __code next(...)); + +.... + +``` + +--- + + +# next(...) +- __code next(...) は条件分岐によって複数の継続先が設定される +- それぞれの Code Gear の引数の1つに設定する +``` c + __code kpt_freerange(Impl* vm, uint low, uint hi, __code next(...)); +.... + __code next(...); +} vm; +``` + +--- + +# インターフェースの実装 +- インターフェースの実装は別ファイルで定義する(vm_impl.cbc) +- ヘッダーファイルの呼び出しは #include ではなく #interface で呼び出す + +``` c +#include "../../context.h" +#interface "vm.h" + +``` + + +--- + +# create_impl +- create_imple の関数内で vm の型を定義し、vm->CodeGear名 で対応させていく +- 実装を Code Gear で記述していく。 + + +``` c + +vm* createvm_impl(struct Context* cbc_context) { + struct vm* vm = new vm(); +.... + vm->void_ret = C_vm_void_ret; + vm->init_vmm = C_init_vmmvm_impl; + vm->kpt_freerange = C_kpt_freerangevm_impl; + vm->kpt_alloc = C_kpt_allocvm_impl; +... +__code init_vmmvm_impl(struct vm_impl* vm,__code next(...)) { + initlock(&kpt_mem.lock, "vm"); + kpt_mem.freelist = NULL; + + goto next(...); +} +``` + +--- + +# private +- CbC は信頼性を保証するためにそれぞれの Code Gear を細かくする必要があるので、for文やif文がある場合はさらに実装を分ける +- Code Gear は基本的にインターフェースで指定された Code Gear 内からのみ継続さ れるため、Java の private メソッドのように扱われる。 +- 実際に vm.c の loaduvm の実装を分けた記述を説明する + + +``` c +int loaduvm (pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz) +{ + uint i, pa, n; + pte_t *pte; + + if ((uint) addr % PTE_SZ != 0) { + panic("loaduvm: addr must be page aligned"); + } + + for (i = 0; i < sz; i += PTE_SZ) { + if ((pte = walkpgdir(pgdir, addr + i, 0)) == 0) { + panic("loaduvm: address should exist"); + } + + pa = PTE_ADDR(*pte); + + if (sz - i < PTE_SZ) { + n = sz - i; + } else { + n = PTE_SZ; + } + + if (readi(ip, p2v(pa), offset + i, n) != n) { + return -1; + } + } + + return 0; +} +``` + +--- + +# goto private +- vm と同じ create_impl 内で vm_impl を定義し、private で実装する Code Gear を定義する +- loaduvmvm_impl で goto によって private に遷移する + + +``` c +vm* createvm_impl(struct Context* cbc_context) { +... + struct vm_impl* vm_impl = new vm_impl(); +... + vm_impl->loaduvm_ptesize_check = C_loaduvm_ptesize_checkvm_impl; +.... + vm->loaduvm = C_loaduvmvm_impl; +.... +} + +__code loaduvmvm_impl(struct vm_impl* vm, pde_t* pgdir, char* addr, struct inode* ip, uint offset, uint sz, __code next(...)) { + Gearef(cbc_context, vm_impl)->pgdir = pgdir; + Gearef(cbc_context, vm_impl)->addr = addr; + Gearef(cbc_context, vm_impl)->ip = ip; + Gearef(cbc_context, vm_impl)->offset = offset; + Gearef(cbc_context, vm_impl)->sz = sz; + Gearef(cbc_context, vm_impl)->next = next; + + goto loaduvm_ptesize_checkvm_impl(vm, next(...)); +} +``` +--- + +# private のヘッダー +- private 側のヘッダーファイルも vm_impl と同じように用意する + +``` c +typedef struct vm_impl<Impl, Isa> impl vm{ +... + __code loaduvm_ptesize_check(Type* vm_impl, uint i, pte_t* pte, uint sz, +__code next(int ret, ...)); +``` + +--- + +# private の記述 + + +``` c +#interface "vm_impl.h" + +__code loaduvm_ptesize_checkvm_impl(struct vm_impl* vm_impl, __code next(int ret, ...)) { + char* addr = vm_impl->addr; + + if ((uint) addr %PTE_SZ != 0) { + // goto panic + } + + goto loaduvm_loopvm_impl(vm_impl, next(ret, ...)); +} +``` + +- vm.cではここから for だが CbC は if文の中と外にgoto を用意して実装する + +![](https://i.imgur.com/ByA7GVJ.png) + +```c +__code loaduvm_loopvm_impl(struct vm_impl* vm_impl, __code next(int ret, ...)) { + uint i = vm_impl->i; + uint sz = vm_impl->sz; + + if (i < sz) { + goto loaduvm_check_pgdir(vm_impl, next(ret, ...)); + } + + goto loaduvm_exit(vm_impl, next(ret, ...)); +} + + +__code loaduvm_check_pgdir(struct vm_impl* vm_impl, __code next(int ret, ...)) { + pte_t* pte = vm_impl->pte; + pde_t* pgdir = vm_impl->pgdir; + uint i = vm_impl->i; + char* addr = vm_impl->addr; + uint pa = vm_impl->pa; + + if ((pte = walkpgdir(pgdir, addr + i, 0)) == 0) { + // goto panic + } + pa = PTE_ADDR(*pte); + + vm_impl->pte = pte; + vm_impl->pgdir = pgdir; + vm_impl->addr = addr; + vm_impl->pa = pa; + + goto loaduvm_check_PTE_SZ(vm_impl, next(ret, ...)); +} + +__code loaduvm_check_PTE_SZ(struct vm_impl* vm_impl, __code next(int ret, ...)) { + uint sz = vm_impl->sz; + uint i = vm_impl->i; + uint n = vm_impl->n; + struct inode* ip = vm_impl->ip; + uint pa = vm_impl->pa; + uint offset = vm_impl->offset; + + if (sz - i < PTE_SZ) { + n = sz - i; + } else { + n = PTE_SZ; + } + + if (readi(ip, p2v(pa), offset + i, n) != n) { + ret = -1; + goto next(ret, ...); + } + + vm_impl->n = n; + + goto loaduvm_loopvm_impl(vm_impl, next(ret, ...)); +} + +__code loaduvm_exit(struct vm_impl* vm_impl, __code next(int ret, ...)) { + ret = 0; + goto next(ret, ...); +} +``` +``` c + +int loaduvm (pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz) +{ + uint i, pa, n; + pte_t *pte; + + if ((uint) addr % PTE_SZ != 0) { + panic("loaduvm: addr must be page aligned"); + } + + for (i = 0; i < sz; i += PTE_SZ) { + if ((pte = walkpgdir(pgdir, addr + i, 0)) == 0) { + panic("loaduvm: address should exist"); + } + + pa = PTE_ADDR(*pte); + + if (sz - i < PTE_SZ) { + n = sz - i; + } else { + n = PTE_SZ; + } + + if (readi(ip, p2v(pa), offset + i, n) != n) { + return -1; + } + } + + return 0; +} +``` + + +# インターフェースの呼び出し +- 定義したインターフェースの呼び出しについて説明する +- CbC の場合 goto による 遷移を行うので、関数呼び出しのように goto 以降のコードを実行できない +- 例) goto すると戻ってこれないため それ以降が実行されなくなる。 + +``` c +void userinit(void) +{ + struct proc* p; + extern char _binary_initcode_start[], _binary_initcode_size[]; + + p = allocproc(); + initContext(&p->cbc_context); + + initproc = p; + + if((p->pgdir = kpt_alloc()) == NULL) { + panic("userinit: out of memory?"); + } + + goto cbc_init_vmm_dummy(&p->cbc_context, p, p->pgdir, _binary_initcode_start, (int)_binary_initcode_size); + p->sz = PTE_SZ; + + // craft the trapframe as if + memset(p->tf, 0, sizeof(*p->tf)); +~ +``` + +# 呼び出しの解決 +- 最初の命令は next で戻ってこれるので、dummy の関数を用意してそこで実行する + +``` c +void dummy(struct proc *p, char _binary_initcode_start[], char _binary_initcode_size[]) +{ + // inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size); + goto cbc_init_vmm_dummy(&p->cbc_context, p, p->pgdir, _binary_initcode_start, (int)_binary_initcode_size); + +} + + + +__ncode cbc_init_vmm_dummy(struct Context* cbc_context, struct proc* p, pde_t* pgdir, char* init, uint sz){//:skip + + struct vm* vm = createvm_impl(cbc_context); + // goto vm->init_vmm(vm, pgdir, init, sz , vm->void_ret); + Gearef(cbc_context, vm)->vm = (union Data*) vm; + Gearef(cbc_context, vm)->pgdir = pgdir; + Gearef(cbc_context, vm)->init = init; + Gearef(cbc_context, vm)->sz = sz ; + Gearef(cbc_context, vm)->next = C_vm_void_ret ; + goto meta(cbc_context, vm->init_inituvm); +} + + +void userinit(void) +{ + struct proc* p; + extern char _binary_initcode_start[], _binary_initcode_size[]; + + p = allocproc(); + initContext(&p->cbc_context); + + initproc = p; + + if((p->pgdir = kpt_alloc()) == NULL) { + panic("userinit: out of memory?"); + } + + dummy(p, _binary_initcode_start, _binary_initcode_size); +~ + +```