annotate slide/CbCXv6.md @ 23:49d691a92b41

fix slide
author tobaru
date Sat, 08 Feb 2020 19:45:10 +0900
parents 6d84ce92ff35
children 69ac4230c3f2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
1 # CbCインターフェースによる CbCXv6 の書き換え
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
2 - 並列信頼研
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
3 - 桃原 優
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
4
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
5 ---
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
6
21
tobaru
parents: 19
diff changeset
7 # 概要
23
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
8 - OS の信頼性を上げたい
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
9 - CbCを使って xv6 を書き換える
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
10 - Paging の書き換えを行った
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
11 - まだ実装中
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
12 - 将来はコンテナやVMをメタ計算として実装できるはず
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
13
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
14 ---
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
15
23
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
16 # OS の信頼性を上げたい
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
17 - 仕様を満たすことを証明する
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
18 - 証明しやすい形の記述を作る
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
19 - CbCの goto で書く
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
20 - 状態遷移系に近い形で記述できる
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
21 - 関数型の記述
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
22 - CbCのinterface で書く
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
23 - 記述のモジュール化
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
24
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
25 ---
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
26
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
27 # メタレベルとノーマルレベル
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
28 - ノーマルレベル
23
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
29 - CbCで記述される普通のアルゴリズム
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
30 - メタレベル
23
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
31 - Paging などのメモリやCPU自体の操作
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
32 - プログラムの正しさの証明
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
33 - Context
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
34 - メタレベルで使用される Meta Data を置く場所
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
35
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
36 ---
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
37
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
38 # Continuation based C
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
39 - ノーマルレベルとメタレベルの処理を同じ言語で行えるようにした言語(以下CbC)
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
40 - Code Gear
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
41 - 基本的な処理の単位
23
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
42 - goto 文で遷移する
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
43 - Data Gear
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
44 - データの単位
23
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
45 - Meta Code Gear
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
46 - goto meta
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
47 - Code Gear の間に挟める計算
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
48 - Meta Data Gear
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
49 - Context
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
50 - Code Gearの間の接続など
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
51
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
52 ---
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
53
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
54 # goto による継続
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
55
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
56 - Code Gear の処理の間を goto によって遷移していく
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
57
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
58 ![](https://i.imgur.com/etfQund.png)
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
59
19
tobaru
parents: 18
diff changeset
60
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
61 ---
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
62
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
63 # Data Gear の継続
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
64 - goto の際に Data Gear も継続される
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
65
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
66 ![](https://i.imgur.com/3E0DGWA.png)
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
67
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
68 ---
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
69
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
70 # インターフェース
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
71 - 書き換えを防ぐために見える Data Gear に違いが生じる
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
72 - -> Gears OS のノーマルレベルからメタレベルの記述が記述が煩雑になるためインターフェースを導入
21
tobaru
parents: 19
diff changeset
73 - インターフェースによる他のメリット
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
74 - 煩雑な記述の解消
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
75 - 機能の入れ替え
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
76 - Agda による証明
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
77
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
78 ---
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
79
23
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
80
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
81 # Meta Code Gear
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
82 - 実際にはノーマルレベルの間にメタレベルの処理がある
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
83 - Meta Level では Data Gear の見え方は変わる(Meta Data Gear)
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
84 - 書き換えやアクセスを防ぐため
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
85
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
86 ![](https://i.imgur.com/vy0NxrG.png)
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
87
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
88 ---
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
89
23
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
90 # Geas OS
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
91 - Xv6 をCbCで書き換える
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
92 - MIT の講義用教材として作られたOS
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
93 - 企画課される前のCで書かれたUNIX V6 を書き換えた
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
94 - 1万行程の軽量なOS
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
95 - Linuxだと数千万行
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
96 - Xv6 を参考に CbC で書き直すことで Gears OS を実装する
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
97
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
98 ---
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
99
23
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
100
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
101 # Xv6の構成
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
102 - systemcall
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
103 - Scheduler
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
104 - VM
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
105 - file system
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
106 - tty
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
107
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
108
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
109 ---
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
110
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
111 # Context
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
112 - Meta Data Gear
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
113 - ユーザープロセスに対応して1つのcontextがある
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
114 - Contextには実行されるすべてのCode Gear が登録される
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
115 - Data GearもContext上にある
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
116
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
117 ---
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
118 # カーネル空間
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
119 - OS の中核となるプログラムで Meta Level に相当する
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
120 - Xv6 ではカーネルとユーザープログラムは分離されている
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
121 - ユーザープログラムはカーネルに直接アクセスできない。
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
122 - 書き換えやアクセスを防ぐため
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
123 - 呼び出す場合は system call
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
124
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
125 ---
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
126
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
127 # system call
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
128 - system call 呼び出し
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
129 - トラップ の発生
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
130 - ユーザープログラムの中断
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
131 - 処理がカーネルに切り替わる
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
132
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
133 # CbCXv6 での Paging
23
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
134 - OS の信頼性の1つであるメモリ管理部分
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
135 - メモリ管理するpage table
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
136 - Page と呼ばれる固定長のブロックに分割して、メモリとスワップ領域で Page を入れ替えて管理
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
137 - 仮想メモリとして扱うことでフラグメンテーションの解消と空き番地を探す必要がなくなる
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
138
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
139 ---
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
140
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
141 # Pagingの図
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
142 - 必要?
21
tobaru
parents: 19
diff changeset
143 ![](https://i.imgur.com/ZNxOsNf.png)
tobaru
parents: 19
diff changeset
144
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
145
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
146 ---
19
tobaru
parents: 18
diff changeset
147 # CbC インターフェース
tobaru
parents: 18
diff changeset
148 - インターフェースは Data Gear に対しての操作を行う Code Gear
tobaru
parents: 18
diff changeset
149 - 実装は別で定義し、呼び出す
tobaru
parents: 18
diff changeset
150 - インターフェースによって機能を置き換えることができる
tobaru
parents: 18
diff changeset
151
tobaru
parents: 18
diff changeset
152 ---
tobaru
parents: 18
diff changeset
153
tobaru
parents: 18
diff changeset
154 # インターフェースの定義
tobaru
parents: 18
diff changeset
155 - Data Gear と Data Gear に対して操作を行う Code Gear の集合を表現する **Meta Data Gear**
tobaru
parents: 18
diff changeset
156
tobaru
parents: 18
diff changeset
157 ---
tobaru
parents: 18
diff changeset
158
tobaru
parents: 18
diff changeset
159 # インターフェースのソースコード
tobaru
parents: 18
diff changeset
160 - vm.c をインターフェースで書き換えた vm.h のコードの説明をしていく
tobaru
parents: 18
diff changeset
161
tobaru
parents: 18
diff changeset
162
tobaru
parents: 18
diff changeset
163 ---
tobaru
parents: 18
diff changeset
164
tobaru
parents: 18
diff changeset
165
tobaru
parents: 18
diff changeset
166 # 実装名の定義
tobaru
parents: 18
diff changeset
167 - typedef struct の直後に実装名(vm)を書く
tobaru
parents: 18
diff changeset
168
tobaru
parents: 18
diff changeset
169
tobaru
parents: 18
diff changeset
170 ``` c
tobaru
parents: 18
diff changeset
171 typedef struct vm<Type,Impl> {
tobaru
parents: 18
diff changeset
172 ```
tobaru
parents: 18
diff changeset
173
tobaru
parents: 18
diff changeset
174 ---
tobaru
parents: 18
diff changeset
175
23
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
176 # init_vmm
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
177
49d691a92b41 fix slide
tobaru
parents: 21
diff changeset
178
19
tobaru
parents: 18
diff changeset
179 # Code Gear の定義
tobaru
parents: 18
diff changeset
180 - Code Gear は __Code CodeGear名(引数); で記述する
tobaru
parents: 18
diff changeset
181 - 第1引数の Impl* vm が Code Gear の型になる
tobaru
parents: 18
diff changeset
182 - 初期化された Data Gear が それぞれの Code Gear の引数として扱われる
tobaru
parents: 18
diff changeset
183 - 例)定義された uinit が kpt_freerange の Code Gear の第1引数と対応している
tobaru
parents: 18
diff changeset
184
tobaru
parents: 18
diff changeset
185
tobaru
parents: 18
diff changeset
186 ``` c
21
tobaru
parents: 19
diff changeset
187 typedef struct vm<Type,Impl> {
19
tobaru
parents: 18
diff changeset
188 __code init_vmm(Impl* vm, __code next(...));
tobaru
parents: 18
diff changeset
189 __code kpt_freerange(Impl* vm, uint low, uint hi, __code next(...));
tobaru
parents: 18
diff changeset
190 __code kpt_alloc(Impl* vm ,__code next(...));
tobaru
parents: 18
diff changeset
191 __code switchuvm(Impl* vm ,struct proc* p, __code next(...));
tobaru
parents: 18
diff changeset
192 __code init_inituvm(Impl* vm, pde_t* pgdir, char* init, uint sz, __code next(...));
tobaru
parents: 18
diff changeset
193 __code loaduvm(Impl* vm,pde_t* pgdir, char* addr, struct inode* ip, uint offset, uint sz, __code next(...));
tobaru
parents: 18
diff changeset
194 __code allocuvm(Impl* vm, pde_t* pgdir, uint oldsz, uint newsz, __code next(...));
tobaru
parents: 18
diff changeset
195
tobaru
parents: 18
diff changeset
196 ```
tobaru
parents: 18
diff changeset
197
tobaru
parents: 18
diff changeset
198 ---
tobaru
parents: 18
diff changeset
199
tobaru
parents: 18
diff changeset
200
tobaru
parents: 18
diff changeset
201 # next(...)
tobaru
parents: 18
diff changeset
202 - __code next(...) は条件分岐によって複数の継続先が設定される
tobaru
parents: 18
diff changeset
203 - それぞれの Code Gear の引数の1つに設定する
tobaru
parents: 18
diff changeset
204 ``` c
tobaru
parents: 18
diff changeset
205 __code kpt_freerange(Impl* vm, uint low, uint hi, __code next(...));
tobaru
parents: 18
diff changeset
206 ....
tobaru
parents: 18
diff changeset
207 __code next(...);
tobaru
parents: 18
diff changeset
208 } vm;
tobaru
parents: 18
diff changeset
209 ```
tobaru
parents: 18
diff changeset
210
tobaru
parents: 18
diff changeset
211 ---
tobaru
parents: 18
diff changeset
212
tobaru
parents: 18
diff changeset
213 # インターフェースの実装
tobaru
parents: 18
diff changeset
214 - インターフェースの実装は別ファイルで定義する(vm_impl.cbc)
tobaru
parents: 18
diff changeset
215 - ヘッダーファイルの呼び出しは #include ではなく #interface で呼び出す
tobaru
parents: 18
diff changeset
216
tobaru
parents: 18
diff changeset
217 ``` c
tobaru
parents: 18
diff changeset
218 #include "../../context.h"
tobaru
parents: 18
diff changeset
219 #interface "vm.h"
tobaru
parents: 18
diff changeset
220
tobaru
parents: 18
diff changeset
221 ```
tobaru
parents: 18
diff changeset
222
tobaru
parents: 18
diff changeset
223
tobaru
parents: 18
diff changeset
224 ---
tobaru
parents: 18
diff changeset
225
tobaru
parents: 18
diff changeset
226 # create_impl
tobaru
parents: 18
diff changeset
227 - create_imple の関数内で vm の型を定義し、vm->CodeGear名 で対応させていく
tobaru
parents: 18
diff changeset
228 - 実装を Code Gear で記述していく。
tobaru
parents: 18
diff changeset
229
tobaru
parents: 18
diff changeset
230
tobaru
parents: 18
diff changeset
231 ``` c
tobaru
parents: 18
diff changeset
232
tobaru
parents: 18
diff changeset
233 vm* createvm_impl(struct Context* cbc_context) {
tobaru
parents: 18
diff changeset
234 struct vm* vm = new vm();
tobaru
parents: 18
diff changeset
235 ....
tobaru
parents: 18
diff changeset
236 vm->void_ret = C_vm_void_ret;
tobaru
parents: 18
diff changeset
237 vm->init_vmm = C_init_vmmvm_impl;
tobaru
parents: 18
diff changeset
238 vm->kpt_freerange = C_kpt_freerangevm_impl;
tobaru
parents: 18
diff changeset
239 vm->kpt_alloc = C_kpt_allocvm_impl;
tobaru
parents: 18
diff changeset
240 ...
tobaru
parents: 18
diff changeset
241 __code init_vmmvm_impl(struct vm_impl* vm,__code next(...)) {
tobaru
parents: 18
diff changeset
242 initlock(&kpt_mem.lock, "vm");
tobaru
parents: 18
diff changeset
243 kpt_mem.freelist = NULL;
tobaru
parents: 18
diff changeset
244
tobaru
parents: 18
diff changeset
245 goto next(...);
tobaru
parents: 18
diff changeset
246 }
tobaru
parents: 18
diff changeset
247 ```
tobaru
parents: 18
diff changeset
248
tobaru
parents: 18
diff changeset
249 ---
tobaru
parents: 18
diff changeset
250
tobaru
parents: 18
diff changeset
251 # private
tobaru
parents: 18
diff changeset
252 - CbC は信頼性を保証するためにそれぞれの Code Gear を細かくする必要があるので、for文やif文がある場合はさらに実装を分ける
tobaru
parents: 18
diff changeset
253 - Code Gear は基本的にインターフェースで指定された Code Gear 内からのみ継続さ れるため、Java の private メソッドのように扱われる。
tobaru
parents: 18
diff changeset
254 - 実際に vm.c の loaduvm の実装を分けた記述を説明する
tobaru
parents: 18
diff changeset
255
tobaru
parents: 18
diff changeset
256
tobaru
parents: 18
diff changeset
257 ``` c
tobaru
parents: 18
diff changeset
258 int loaduvm (pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz)
tobaru
parents: 18
diff changeset
259 {
tobaru
parents: 18
diff changeset
260 uint i, pa, n;
tobaru
parents: 18
diff changeset
261 pte_t *pte;
tobaru
parents: 18
diff changeset
262
tobaru
parents: 18
diff changeset
263 if ((uint) addr % PTE_SZ != 0) {
tobaru
parents: 18
diff changeset
264 panic("loaduvm: addr must be page aligned");
tobaru
parents: 18
diff changeset
265 }
tobaru
parents: 18
diff changeset
266
tobaru
parents: 18
diff changeset
267 for (i = 0; i < sz; i += PTE_SZ) {
tobaru
parents: 18
diff changeset
268 if ((pte = walkpgdir(pgdir, addr + i, 0)) == 0) {
tobaru
parents: 18
diff changeset
269 panic("loaduvm: address should exist");
tobaru
parents: 18
diff changeset
270 }
tobaru
parents: 18
diff changeset
271
tobaru
parents: 18
diff changeset
272 pa = PTE_ADDR(*pte);
tobaru
parents: 18
diff changeset
273
tobaru
parents: 18
diff changeset
274 if (sz - i < PTE_SZ) {
tobaru
parents: 18
diff changeset
275 n = sz - i;
tobaru
parents: 18
diff changeset
276 } else {
tobaru
parents: 18
diff changeset
277 n = PTE_SZ;
tobaru
parents: 18
diff changeset
278 }
tobaru
parents: 18
diff changeset
279
tobaru
parents: 18
diff changeset
280 if (readi(ip, p2v(pa), offset + i, n) != n) {
tobaru
parents: 18
diff changeset
281 return -1;
tobaru
parents: 18
diff changeset
282 }
tobaru
parents: 18
diff changeset
283 }
tobaru
parents: 18
diff changeset
284
tobaru
parents: 18
diff changeset
285 return 0;
tobaru
parents: 18
diff changeset
286 }
tobaru
parents: 18
diff changeset
287 ```
tobaru
parents: 18
diff changeset
288
tobaru
parents: 18
diff changeset
289 ---
tobaru
parents: 18
diff changeset
290
tobaru
parents: 18
diff changeset
291 # goto private
tobaru
parents: 18
diff changeset
292 - vm と同じ create_impl 内で vm_impl を定義し、private で実装する Code Gear を定義する
tobaru
parents: 18
diff changeset
293 - loaduvmvm_impl で goto によって private に遷移する
tobaru
parents: 18
diff changeset
294
tobaru
parents: 18
diff changeset
295
tobaru
parents: 18
diff changeset
296 ``` c
tobaru
parents: 18
diff changeset
297 vm* createvm_impl(struct Context* cbc_context) {
tobaru
parents: 18
diff changeset
298 ...
tobaru
parents: 18
diff changeset
299 struct vm_impl* vm_impl = new vm_impl();
tobaru
parents: 18
diff changeset
300 ...
tobaru
parents: 18
diff changeset
301 vm_impl->loaduvm_ptesize_check = C_loaduvm_ptesize_checkvm_impl;
tobaru
parents: 18
diff changeset
302 ....
tobaru
parents: 18
diff changeset
303 vm->loaduvm = C_loaduvmvm_impl;
tobaru
parents: 18
diff changeset
304 ....
tobaru
parents: 18
diff changeset
305 }
tobaru
parents: 18
diff changeset
306
tobaru
parents: 18
diff changeset
307 __code loaduvmvm_impl(struct vm_impl* vm, pde_t* pgdir, char* addr, struct inode* ip, uint offset, uint sz, __code next(...)) {
tobaru
parents: 18
diff changeset
308 Gearef(cbc_context, vm_impl)->pgdir = pgdir;
tobaru
parents: 18
diff changeset
309 Gearef(cbc_context, vm_impl)->addr = addr;
tobaru
parents: 18
diff changeset
310 Gearef(cbc_context, vm_impl)->ip = ip;
tobaru
parents: 18
diff changeset
311 Gearef(cbc_context, vm_impl)->offset = offset;
tobaru
parents: 18
diff changeset
312 Gearef(cbc_context, vm_impl)->sz = sz;
tobaru
parents: 18
diff changeset
313 Gearef(cbc_context, vm_impl)->next = next;
tobaru
parents: 18
diff changeset
314
tobaru
parents: 18
diff changeset
315 goto loaduvm_ptesize_checkvm_impl(vm, next(...));
tobaru
parents: 18
diff changeset
316 }
tobaru
parents: 18
diff changeset
317 ```
tobaru
parents: 18
diff changeset
318 ---
tobaru
parents: 18
diff changeset
319
tobaru
parents: 18
diff changeset
320 # private のヘッダー
tobaru
parents: 18
diff changeset
321 - private 側のヘッダーファイルも vm_impl と同じように用意する
tobaru
parents: 18
diff changeset
322
tobaru
parents: 18
diff changeset
323 ``` c
tobaru
parents: 18
diff changeset
324 typedef struct vm_impl<Impl, Isa> impl vm{
tobaru
parents: 18
diff changeset
325 ...
tobaru
parents: 18
diff changeset
326 __code loaduvm_ptesize_check(Type* vm_impl, uint i, pte_t* pte, uint sz,
tobaru
parents: 18
diff changeset
327 __code next(int ret, ...));
tobaru
parents: 18
diff changeset
328 ```
tobaru
parents: 18
diff changeset
329
tobaru
parents: 18
diff changeset
330 ---
tobaru
parents: 18
diff changeset
331
tobaru
parents: 18
diff changeset
332 # private の記述
tobaru
parents: 18
diff changeset
333
tobaru
parents: 18
diff changeset
334 ``` c
tobaru
parents: 18
diff changeset
335 #interface "vm_impl.h"
tobaru
parents: 18
diff changeset
336
tobaru
parents: 18
diff changeset
337 __code loaduvm_ptesize_checkvm_impl(struct vm_impl* vm_impl, __code next(int ret, ...)) {
tobaru
parents: 18
diff changeset
338 char* addr = vm_impl->addr;
tobaru
parents: 18
diff changeset
339
tobaru
parents: 18
diff changeset
340 if ((uint) addr %PTE_SZ != 0) {
tobaru
parents: 18
diff changeset
341 // goto panic
tobaru
parents: 18
diff changeset
342 }
tobaru
parents: 18
diff changeset
343
tobaru
parents: 18
diff changeset
344 goto loaduvm_loopvm_impl(vm_impl, next(ret, ...));
tobaru
parents: 18
diff changeset
345 }
tobaru
parents: 18
diff changeset
346 ```
tobaru
parents: 18
diff changeset
347
tobaru
parents: 18
diff changeset
348 - vm.cではここから for だが CbC は if文の中と外にgoto を用意して実装する
tobaru
parents: 18
diff changeset
349
tobaru
parents: 18
diff changeset
350 ![](https://i.imgur.com/ByA7GVJ.png)
tobaru
parents: 18
diff changeset
351
tobaru
parents: 18
diff changeset
352 ```c
tobaru
parents: 18
diff changeset
353 __code loaduvm_loopvm_impl(struct vm_impl* vm_impl, __code next(int ret, ...)) {
tobaru
parents: 18
diff changeset
354 uint i = vm_impl->i;
tobaru
parents: 18
diff changeset
355 uint sz = vm_impl->sz;
tobaru
parents: 18
diff changeset
356
tobaru
parents: 18
diff changeset
357 if (i < sz) {
tobaru
parents: 18
diff changeset
358 goto loaduvm_check_pgdir(vm_impl, next(ret, ...));
tobaru
parents: 18
diff changeset
359 }
tobaru
parents: 18
diff changeset
360
tobaru
parents: 18
diff changeset
361 goto loaduvm_exit(vm_impl, next(ret, ...));
tobaru
parents: 18
diff changeset
362 }
tobaru
parents: 18
diff changeset
363
tobaru
parents: 18
diff changeset
364
tobaru
parents: 18
diff changeset
365 __code loaduvm_check_pgdir(struct vm_impl* vm_impl, __code next(int ret, ...)) {
tobaru
parents: 18
diff changeset
366 pte_t* pte = vm_impl->pte;
tobaru
parents: 18
diff changeset
367 pde_t* pgdir = vm_impl->pgdir;
tobaru
parents: 18
diff changeset
368 uint i = vm_impl->i;
tobaru
parents: 18
diff changeset
369 char* addr = vm_impl->addr;
tobaru
parents: 18
diff changeset
370 uint pa = vm_impl->pa;
tobaru
parents: 18
diff changeset
371
tobaru
parents: 18
diff changeset
372 if ((pte = walkpgdir(pgdir, addr + i, 0)) == 0) {
tobaru
parents: 18
diff changeset
373 // goto panic
tobaru
parents: 18
diff changeset
374 }
tobaru
parents: 18
diff changeset
375 pa = PTE_ADDR(*pte);
tobaru
parents: 18
diff changeset
376
tobaru
parents: 18
diff changeset
377 vm_impl->pte = pte;
tobaru
parents: 18
diff changeset
378 vm_impl->pgdir = pgdir;
tobaru
parents: 18
diff changeset
379 vm_impl->addr = addr;
tobaru
parents: 18
diff changeset
380 vm_impl->pa = pa;
tobaru
parents: 18
diff changeset
381
tobaru
parents: 18
diff changeset
382 goto loaduvm_check_PTE_SZ(vm_impl, next(ret, ...));
tobaru
parents: 18
diff changeset
383 }
tobaru
parents: 18
diff changeset
384
tobaru
parents: 18
diff changeset
385 __code loaduvm_check_PTE_SZ(struct vm_impl* vm_impl, __code next(int ret, ...)) {
tobaru
parents: 18
diff changeset
386 uint sz = vm_impl->sz;
tobaru
parents: 18
diff changeset
387 uint i = vm_impl->i;
tobaru
parents: 18
diff changeset
388 uint n = vm_impl->n;
tobaru
parents: 18
diff changeset
389 struct inode* ip = vm_impl->ip;
tobaru
parents: 18
diff changeset
390 uint pa = vm_impl->pa;
tobaru
parents: 18
diff changeset
391 uint offset = vm_impl->offset;
tobaru
parents: 18
diff changeset
392
tobaru
parents: 18
diff changeset
393 if (sz - i < PTE_SZ) {
tobaru
parents: 18
diff changeset
394 n = sz - i;
tobaru
parents: 18
diff changeset
395 } else {
tobaru
parents: 18
diff changeset
396 n = PTE_SZ;
tobaru
parents: 18
diff changeset
397 }
tobaru
parents: 18
diff changeset
398
tobaru
parents: 18
diff changeset
399 if (readi(ip, p2v(pa), offset + i, n) != n) {
tobaru
parents: 18
diff changeset
400 ret = -1;
tobaru
parents: 18
diff changeset
401 goto next(ret, ...);
tobaru
parents: 18
diff changeset
402 }
tobaru
parents: 18
diff changeset
403
tobaru
parents: 18
diff changeset
404 vm_impl->n = n;
tobaru
parents: 18
diff changeset
405
tobaru
parents: 18
diff changeset
406 goto loaduvm_loopvm_impl(vm_impl, next(ret, ...));
tobaru
parents: 18
diff changeset
407 }
tobaru
parents: 18
diff changeset
408
tobaru
parents: 18
diff changeset
409 __code loaduvm_exit(struct vm_impl* vm_impl, __code next(int ret, ...)) {
tobaru
parents: 18
diff changeset
410 ret = 0;
tobaru
parents: 18
diff changeset
411 goto next(ret, ...);
tobaru
parents: 18
diff changeset
412 }
tobaru
parents: 18
diff changeset
413 ```
18
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
414
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
415
afc36230cf4f slide chapter3,4
tobaru
parents:
diff changeset
416
19
tobaru
parents: 18
diff changeset
417 ``` c
tobaru
parents: 18
diff changeset
418
tobaru
parents: 18
diff changeset
419 int loaduvm (pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz)
tobaru
parents: 18
diff changeset
420 {
tobaru
parents: 18
diff changeset
421 uint i, pa, n;
tobaru
parents: 18
diff changeset
422 pte_t *pte;
tobaru
parents: 18
diff changeset
423
tobaru
parents: 18
diff changeset
424 if ((uint) addr % PTE_SZ != 0) {
tobaru
parents: 18
diff changeset
425 panic("loaduvm: addr must be page aligned");
tobaru
parents: 18
diff changeset
426 }
tobaru
parents: 18
diff changeset
427
tobaru
parents: 18
diff changeset
428 for (i = 0; i < sz; i += PTE_SZ) {
tobaru
parents: 18
diff changeset
429 if ((pte = walkpgdir(pgdir, addr + i, 0)) == 0) {
tobaru
parents: 18
diff changeset
430 panic("loaduvm: address should exist");
tobaru
parents: 18
diff changeset
431 }
tobaru
parents: 18
diff changeset
432
tobaru
parents: 18
diff changeset
433 pa = PTE_ADDR(*pte);
tobaru
parents: 18
diff changeset
434
tobaru
parents: 18
diff changeset
435 if (sz - i < PTE_SZ) {
tobaru
parents: 18
diff changeset
436 n = sz - i;
tobaru
parents: 18
diff changeset
437 } else {
tobaru
parents: 18
diff changeset
438 n = PTE_SZ;
tobaru
parents: 18
diff changeset
439 }
tobaru
parents: 18
diff changeset
440
tobaru
parents: 18
diff changeset
441 if (readi(ip, p2v(pa), offset + i, n) != n) {
tobaru
parents: 18
diff changeset
442 return -1;
tobaru
parents: 18
diff changeset
443 }
tobaru
parents: 18
diff changeset
444 }
tobaru
parents: 18
diff changeset
445
tobaru
parents: 18
diff changeset
446 return 0;
tobaru
parents: 18
diff changeset
447 }
tobaru
parents: 18
diff changeset
448 ```
tobaru
parents: 18
diff changeset
449
tobaru
parents: 18
diff changeset
450
tobaru
parents: 18
diff changeset
451 # インターフェースの呼び出し
tobaru
parents: 18
diff changeset
452 - 定義したインターフェースの呼び出しについて説明する
tobaru
parents: 18
diff changeset
453 - CbC の場合 goto による 遷移を行うので、関数呼び出しのように goto 以降のコードを実行できない
tobaru
parents: 18
diff changeset
454 - 例) goto すると戻ってこれないため それ以降が実行されなくなる。
tobaru
parents: 18
diff changeset
455
tobaru
parents: 18
diff changeset
456 ``` c
tobaru
parents: 18
diff changeset
457 void userinit(void)
tobaru
parents: 18
diff changeset
458 {
tobaru
parents: 18
diff changeset
459 struct proc* p;
tobaru
parents: 18
diff changeset
460 extern char _binary_initcode_start[], _binary_initcode_size[];
tobaru
parents: 18
diff changeset
461
tobaru
parents: 18
diff changeset
462 p = allocproc();
tobaru
parents: 18
diff changeset
463 initContext(&p->cbc_context);
tobaru
parents: 18
diff changeset
464
tobaru
parents: 18
diff changeset
465 initproc = p;
tobaru
parents: 18
diff changeset
466
tobaru
parents: 18
diff changeset
467 if((p->pgdir = kpt_alloc()) == NULL) {
tobaru
parents: 18
diff changeset
468 panic("userinit: out of memory?");
tobaru
parents: 18
diff changeset
469 }
tobaru
parents: 18
diff changeset
470
tobaru
parents: 18
diff changeset
471 goto cbc_init_vmm_dummy(&p->cbc_context, p, p->pgdir, _binary_initcode_start, (int)_binary_initcode_size);
tobaru
parents: 18
diff changeset
472 p->sz = PTE_SZ;
tobaru
parents: 18
diff changeset
473
tobaru
parents: 18
diff changeset
474 // craft the trapframe as if
tobaru
parents: 18
diff changeset
475 memset(p->tf, 0, sizeof(*p->tf));
tobaru
parents: 18
diff changeset
476 ~
tobaru
parents: 18
diff changeset
477 ```
tobaru
parents: 18
diff changeset
478
tobaru
parents: 18
diff changeset
479 # 呼び出しの解決
tobaru
parents: 18
diff changeset
480 - 最初の命令は next で戻ってこれるので、dummy の関数を用意してそこで実行する
tobaru
parents: 18
diff changeset
481
tobaru
parents: 18
diff changeset
482 ``` c
tobaru
parents: 18
diff changeset
483 void dummy(struct proc *p, char _binary_initcode_start[], char _binary_initcode_size[])
tobaru
parents: 18
diff changeset
484 {
tobaru
parents: 18
diff changeset
485 // inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size);
tobaru
parents: 18
diff changeset
486 goto cbc_init_vmm_dummy(&p->cbc_context, p, p->pgdir, _binary_initcode_start, (int)_binary_initcode_size);
tobaru
parents: 18
diff changeset
487
tobaru
parents: 18
diff changeset
488 }
tobaru
parents: 18
diff changeset
489
tobaru
parents: 18
diff changeset
490
tobaru
parents: 18
diff changeset
491
tobaru
parents: 18
diff changeset
492 __ncode cbc_init_vmm_dummy(struct Context* cbc_context, struct proc* p, pde_t* pgdir, char* init, uint sz){//:skip
tobaru
parents: 18
diff changeset
493
tobaru
parents: 18
diff changeset
494 struct vm* vm = createvm_impl(cbc_context);
tobaru
parents: 18
diff changeset
495 // goto vm->init_vmm(vm, pgdir, init, sz , vm->void_ret);
tobaru
parents: 18
diff changeset
496 Gearef(cbc_context, vm)->vm = (union Data*) vm;
tobaru
parents: 18
diff changeset
497 Gearef(cbc_context, vm)->pgdir = pgdir;
tobaru
parents: 18
diff changeset
498 Gearef(cbc_context, vm)->init = init;
tobaru
parents: 18
diff changeset
499 Gearef(cbc_context, vm)->sz = sz ;
tobaru
parents: 18
diff changeset
500 Gearef(cbc_context, vm)->next = C_vm_void_ret ;
tobaru
parents: 18
diff changeset
501 goto meta(cbc_context, vm->init_inituvm);
tobaru
parents: 18
diff changeset
502 }
tobaru
parents: 18
diff changeset
503
tobaru
parents: 18
diff changeset
504
tobaru
parents: 18
diff changeset
505 void userinit(void)
tobaru
parents: 18
diff changeset
506 {
tobaru
parents: 18
diff changeset
507 struct proc* p;
tobaru
parents: 18
diff changeset
508 extern char _binary_initcode_start[], _binary_initcode_size[];
tobaru
parents: 18
diff changeset
509
tobaru
parents: 18
diff changeset
510 p = allocproc();
tobaru
parents: 18
diff changeset
511 initContext(&p->cbc_context);
tobaru
parents: 18
diff changeset
512
tobaru
parents: 18
diff changeset
513 initproc = p;
tobaru
parents: 18
diff changeset
514
tobaru
parents: 18
diff changeset
515 if((p->pgdir = kpt_alloc()) == NULL) {
tobaru
parents: 18
diff changeset
516 panic("userinit: out of memory?");
tobaru
parents: 18
diff changeset
517 }
tobaru
parents: 18
diff changeset
518
21
tobaru
parents: 19
diff changeset
519 dummy(p, _binary_initcode_start, _binary_initcode_size);
tobaru
parents: 19
diff changeset
520 ```
19
tobaru
parents: 18
diff changeset
521
21
tobaru
parents: 19
diff changeset
522