1use vstd::prelude::*;
14use vstd_extra::{ghost_tree::*, ownership::*};
15
16use crate::specs::{
17 arch::*,
18 mm::{
19 frame::{
20 mapping::frame_to_index, meta_owners::PageUsage, meta_region_owners::MetaRegionOwners,
21 },
22 page_table::{
23 Mapping,
24 cursor::owners::{CursorContinuation, CursorOwner},
25 node::entry_owners::EntryOwner,
26 owners::*,
27 },
28 },
29};
30
31use crate::mm::{PagingConstsTrait, frame::meta::REF_COUNT_UNUSED, page_size, page_table::*};
32
33verus! {
34
35impl<'rcu, C: PageTableConfig> CursorOwner<'rcu, C> {
36 pub open spec fn no_node_at_idx(self, idx: int) -> bool {
44 &&& self.map_full_tree(
45 |e: EntryOwner<C>, _p: TreePath<NR_ENTRIES>|
46 e.is_node() && e.meta_slot_paddr() is Some ==> frame_to_index(
47 e.meta_slot_paddr()->0,
48 ) != idx,
49 )
50 &&& forall|i: int|
51 #![trigger self.continuations[i]]
52 self.level - 1 <= i < C::NR_LEVELS ==> {
53 let e = self.continuations[i].entry_own;
54 e.is_node() && e.meta_slot_paddr() is Some ==> frame_to_index(
55 e.meta_slot_paddr()->0,
56 ) != idx
57 }
58 }
59
60 pub proof fn and_map_full_tree(
66 self,
67 f: spec_fn(EntryOwner<C>, TreePath<NR_ENTRIES>) -> bool,
68 guard: spec_fn(EntryOwner<C>, TreePath<NR_ENTRIES>) -> bool,
69 )
70 requires
71 self.inv(),
72 self.map_full_tree(f),
73 self.map_full_tree(guard),
74 ensures
75 self.map_full_tree(|e: EntryOwner<C>, p: TreePath<NR_ENTRIES>| f(e, p) && guard(e, p)),
76 {
77 C::lemma_paging_consts_properties();
78 C::lemma_page_table_config_constant_properties();
79 let combined = |e: EntryOwner<C>, p: TreePath<NR_ENTRIES>| f(e, p) && guard(e, p);
80 assert forall|i: int|
81 #![trigger self.continuations[i]]
82 self.level - 1 <= i < C::NR_LEVELS implies self.continuations[i].map_children(
83 combined,
84 ) by {
85 reveal(CursorContinuation::map_children);
86 let cont = self.continuations[i];
87 reveal(CursorContinuation::inv_children);
88 assert forall|j: int|
89 #![trigger cont.children[j]]
90 0 <= j < cont.children.len()
91 && cont.children[j] is Some implies cont.children[j].unwrap().subtree_satisfies(
92 cont.path().push_tail(j),
93 combined,
94 ) by {
95 cont.lemma_inv_children_unroll(j);
96 OwnerSubtree::lemma_subtree_satisfies_implies_and(
97 cont.children[j].unwrap(),
98 cont.path().push_tail(j),
99 f,
100 guard,
101 combined,
102 );
103 };
104 };
105 }
106
107 pub proof fn no_node_at_idx_from_slot_key(self, regions: MetaRegionOwners, changed_idx: int)
117 requires
118 self.inv(),
119 regions.inv(),
120 self.metaregion_sound(regions),
121 regions.contains(changed_idx),
122 regions.slot_owners[changed_idx].usage !is PageTable,
123 ensures
124 self.no_node_at_idx(changed_idx),
125 {
126 C::lemma_paging_consts_properties();
127 C::lemma_page_table_config_constant_properties();
128 let msp = PageTableOwner::<C>::metaregion_sound_pred(regions);
129 let target = |e: EntryOwner<C>, _p: TreePath<NR_ENTRIES>|
130 e.is_node() && e.meta_slot_paddr() is Some ==> frame_to_index(
131 e.meta_slot_paddr().unwrap(),
132 ) != changed_idx;
133
134 self.lemma_map_children_implies(msp, target);
135
136 assert forall|i: int|
137 #![trigger self.continuations[i]]
138 self.level - 1 <= i < C::NR_LEVELS implies {
139 let e = self.continuations[i].entry_own;
140 e.is_node() && e.meta_slot_paddr() is Some ==> frame_to_index(
141 e.meta_slot_paddr().unwrap(),
142 ) != changed_idx
143 } by {
144 self.lemma_cont_entry_metaregion_at(regions, i);
145 let entry = self.continuations[i].entry_own;
146 if entry.is_node() && entry.meta_slot_paddr() is Some {
147 let idx = frame_to_index(entry.meta_slot_paddr().unwrap());
148 if idx == changed_idx {
149 assert(false);
150 }
151 }
152 };
153 }
154
155 pub proof fn metaregion_preserved_under_path_insert(
159 self,
160 regions0: MetaRegionOwners,
161 regions1: MetaRegionOwners,
162 changed_idx: int,
163 new_path: TreePath<NR_ENTRIES>,
164 )
165 requires
166 self.inv(),
167 self.metaregion_sound(regions0),
168 regions0.inv(),
169 regions0.contains(changed_idx),
170 regions1.slots == regions0.slots,
171 regions1.slot_owners.dom() =~= regions0.slot_owners.dom(),
172 forall|i: int|
173 #![trigger regions1.slot_owners[i]]
174 i != changed_idx ==> regions0.slot_owners[i] == regions1.slot_owners[i],
175 regions1.slot_owners[changed_idx].same_permissions(regions0.slot_owners[changed_idx]),
176 regions1.slot_owners[changed_idx].slot_vaddr
177 == regions0.slot_owners[changed_idx].slot_vaddr,
178 regions1.slot_owners[changed_idx].usage == regions0.slot_owners[changed_idx].usage,
179 regions1.slot_owners[changed_idx].paths_in_pt
180 == regions0.slot_owners[changed_idx].paths_in_pt.insert(new_path),
181 self.no_node_at_idx(changed_idx),
182 ensures
183 self.metaregion_sound(regions1),
184 {
185 C::lemma_paging_consts_properties();
186 C::lemma_page_table_config_constant_properties();
187 let f = PageTableOwner::<C>::metaregion_sound_pred(regions0);
188 let g = PageTableOwner::<C>::metaregion_sound_pred(regions1);
189 let guard = |entry: EntryOwner<C>, _p: TreePath<NR_ENTRIES>|
190 entry.is_node() && entry.meta_slot_paddr() is Some ==> frame_to_index(
191 entry.meta_slot_paddr().unwrap(),
192 ) != changed_idx;
193 let f_strong = |entry: EntryOwner<C>, path: TreePath<NR_ENTRIES>|
194 f(entry, path) && guard(entry, path);
195
196 self.and_map_full_tree(f, guard);
197 assert(OwnerSubtree::implies(f_strong, g)) by {
198 assert forall|entry: EntryOwner<C>, path: TreePath<NR_ENTRIES>|
199 entry.inv() && f_strong(entry, path) implies #[trigger] g(entry, path) by {
200 if entry.is_frame() {
201 let pa = entry.frame().mapped_pa;
202 let idx = frame_to_index(pa);
203 assert(regions0.slots[idx] == regions1.slots[idx]);
204 assert(regions0.slot_owners[idx].metadata_perm.id()
205 == regions1.slot_owners[idx].metadata_perm.id());
206 C::lemma_perm_well_formed_with_region_preserved(
207 pa,
208 Tracked(entry.frame_permission()),
209 regions0,
210 regions1,
211 );
212 }
213 };
214 };
215 self.lemma_map_children_implies(f_strong, g);
216
217 reveal(CursorOwner::path_metaregion_sound);
218 assert forall|i: int|
219 #![trigger self.continuations[i]]
220 self.level - 1 <= i
221 < C::NR_LEVELS implies self.continuations[i].entry_own.metaregion_sound(
222 regions1,
223 ) by {
224 let cont_entry = self.continuations[i].entry_own;
225 if cont_entry.meta_slot_paddr() is Some {
226 cont_entry.lemma_metaregion_sound_one_slot_changed(regions0, regions1, changed_idx);
228 }
229 };
230 }
231
232 pub open spec fn path_removable_at_idx(
242 self,
243 idx: int,
244 removed_path: TreePath<NR_ENTRIES>,
245 ) -> bool {
246 &&& self.map_full_tree(
247 |e: EntryOwner<C>, _p: TreePath<NR_ENTRIES>|
248 e.meta_slot_paddr() is Some && frame_to_index(e.meta_slot_paddr()->0) == idx
249 ==> !e.is_node() && (e.is_frame() ==> e.path != removed_path),
250 )
251 &&& forall|i: int|
252 #![trigger self.continuations[i]]
253 self.level - 1 <= i < C::NR_LEVELS ==> {
254 let e = self.continuations[i].entry_own;
255 e.meta_slot_paddr() is Some && frame_to_index(e.meta_slot_paddr()->0) == idx
256 ==> !e.is_node() && (e.is_frame() ==> e.path != removed_path)
257 }
258 }
259
260 pub open spec fn no_frame_with_path(self, removed_path: TreePath<NR_ENTRIES>) -> bool {
267 &&& self.map_full_tree(
268 |e: EntryOwner<C>, _p: TreePath<NR_ENTRIES>| e.is_frame() ==> e.path != removed_path,
269 )
270 &&& forall|i: int|
271 #![trigger self.continuations[i]]
272 self.level - 1 <= i < C::NR_LEVELS ==> {
273 let e = self.continuations[i].entry_own;
274 e.is_frame() ==> e.path != removed_path
275 }
276 }
277
278 pub proof fn no_frame_with_path_from_no_view_mapping(self, removed_path: TreePath<NR_ENTRIES>)
292 requires
293 self.inv(),
294 forall|m: Mapping|
295 #![trigger self@.mappings.contains(m)]
296 self@.mappings.contains(m) ==> m.va_range.start != vaddr_of::<C>(removed_path),
297 ensures
298 self.no_frame_with_path(removed_path),
299 {
300 C::lemma_paging_consts_properties();
301 C::lemma_page_table_config_constant_properties();
302 broadcast use CursorContinuation::group_lemmas;
303
304 let g = |e: EntryOwner<C>, _p: TreePath<NR_ENTRIES>|
305 e.is_frame() ==> e.path != removed_path;
306
307 assert forall|i: int|
310 #![trigger self.continuations[i]]
311 self.level - 1 <= i < C::NR_LEVELS implies self.continuations[i].map_children(g) by {
312 reveal(CursorContinuation::map_children);
313 self.lemma_inv_continuation(i);
314 let cont = self.continuations[i];
315 reveal(CursorContinuation::inv_children);
316 assert forall|j: int|
317 #![trigger cont.children[j]]
318 0 <= j < cont.children.len()
319 && cont.children[j] is Some implies cont.children[j].unwrap().subtree_satisfies(
320 cont.path().push_tail(j),
321 g,
322 ) by {
323 cont.lemma_inv_children_unroll(j);
324 cont.lemma_pt_inv_children_unroll(j);
325 cont.lemma_inv_children_rel_unroll(j);
326 let child = cont.children[j].unwrap();
327 let child_path = cont.path().push_tail(j);
328 PageTableOwner::<C>::lemma_pt_inv_implies_path_correct(child, child_path);
332 assert forall|m: Mapping|
334 #![trigger self@.mappings.contains(m)]
335 PageTableOwner(child).view_rec(child_path).contains(
336 m,
337 ) implies self@.mappings.contains(m) by {
338 self.lemma_view_mappings_intro(m, i);
339 };
340 PageTableOwner(child).lemma_no_frame_with_path_rec(
342 child_path,
343 removed_path,
344 self@.mappings,
345 );
346 };
347 };
348
349 assert forall|i: int|
351 #![trigger self.continuations[i]]
352 self.level - 1 <= i < C::NR_LEVELS implies {
353 let e = self.continuations[i].entry_own;
354 e.is_frame() ==> e.path != removed_path
355 } by {
356 self.lemma_inv_continuation(i);
357 };
358 }
359
360 pub proof fn path_removable_from_no_node_and_no_frame_path(
367 self,
368 idx: int,
369 removed_path: TreePath<NR_ENTRIES>,
370 )
371 requires
372 self.inv(),
373 self.no_node_at_idx(idx),
374 self.no_frame_with_path(removed_path),
375 ensures
376 self.path_removable_at_idx(idx, removed_path),
377 {
378 C::lemma_paging_consts_properties();
379 C::lemma_page_table_config_constant_properties();
380 let nn = |e: EntryOwner<C>, _p: TreePath<NR_ENTRIES>|
381 e.is_node() && e.meta_slot_paddr() is Some ==> frame_to_index(
382 e.meta_slot_paddr().unwrap(),
383 ) != idx;
384 let nf = |e: EntryOwner<C>, _p: TreePath<NR_ENTRIES>|
385 e.is_frame() ==> e.path != removed_path;
386 let r = |e: EntryOwner<C>, _p: TreePath<NR_ENTRIES>|
387 e.meta_slot_paddr() is Some && frame_to_index(e.meta_slot_paddr().unwrap()) == idx
388 ==> !e.is_node() && (e.is_frame() ==> e.path != removed_path);
389
390 self.and_map_full_tree(nn, nf);
393 self.lemma_map_children_implies(
394 |e: EntryOwner<C>, p: TreePath<NR_ENTRIES>| nn(e, p) && nf(e, p),
395 r,
396 );
397
398 assert forall|i: int|
400 #![trigger self.continuations[i]]
401 self.level - 1 <= i < C::NR_LEVELS implies {
402 let e = self.continuations[i].entry_own;
403 e.meta_slot_paddr() is Some && frame_to_index(e.meta_slot_paddr().unwrap()) == idx
404 ==> !e.is_node() && (e.is_frame() ==> e.path != removed_path)
405 } by {
406 let e = self.continuations[i].entry_own;
407 }
408 }
409
410 pub proof fn metaregion_preserved_under_path_remove(
419 self,
420 regions0: MetaRegionOwners,
421 regions1: MetaRegionOwners,
422 changed_idx: int,
423 removed_path: TreePath<NR_ENTRIES>,
424 )
425 requires
426 self.inv(),
427 self.metaregion_sound(regions0),
428 regions0.inv(),
429 regions0.contains(changed_idx),
430 regions1.slots == regions0.slots,
431 regions1.slot_owners.dom() =~= regions0.slot_owners.dom(),
432 forall|i: int|
433 #![trigger regions1.slot_owners[i]]
434 i != changed_idx ==> regions0.slot_owners[i] == regions1.slot_owners[i],
435 regions1.slot_owners[changed_idx].same_permissions(regions0.slot_owners[changed_idx]),
436 regions1.slot_owners[changed_idx].slot_vaddr
437 == regions0.slot_owners[changed_idx].slot_vaddr,
438 regions1.slot_owners[changed_idx].usage == regions0.slot_owners[changed_idx].usage,
439 regions1.slot_owners[changed_idx].paths_in_pt
440 == regions0.slot_owners[changed_idx].paths_in_pt.remove(removed_path),
441 self.path_removable_at_idx(changed_idx, removed_path),
442 ensures
443 self.metaregion_sound(regions1),
444 {
445 C::lemma_paging_consts_properties();
446 C::lemma_page_table_config_constant_properties();
447 let f = PageTableOwner::<C>::metaregion_sound_pred(regions0);
448 let g = PageTableOwner::<C>::metaregion_sound_pred(regions1);
449 let guard = |entry: EntryOwner<C>, _p: TreePath<NR_ENTRIES>|
450 entry.meta_slot_paddr() is Some && frame_to_index(entry.meta_slot_paddr().unwrap())
451 == changed_idx ==> !entry.is_node() && (entry.is_frame() ==> entry.path
452 != removed_path);
453 let f_strong = |entry: EntryOwner<C>, path: TreePath<NR_ENTRIES>|
454 f(entry, path) && guard(entry, path);
455
456 self.and_map_full_tree(f, guard);
457 assert(OwnerSubtree::implies(f_strong, g)) by {
458 assert forall|entry: EntryOwner<C>, path: TreePath<NR_ENTRIES>|
459 entry.inv() && f_strong(entry, path) implies #[trigger] g(entry, path) by {
460 if entry.is_frame() {
461 let pa = entry.frame().mapped_pa;
462 let idx = frame_to_index(pa);
463 assert(regions0.slots[idx] == regions1.slots[idx]);
464 assert(regions0.slot_owners[idx].metadata_perm.id()
465 == regions1.slot_owners[idx].metadata_perm.id());
466 C::lemma_perm_well_formed_with_region_preserved(
467 pa,
468 Tracked(entry.frame_permission()),
469 regions0,
470 regions1,
471 );
472 }
473 };
474 };
475 self.lemma_map_children_implies(f_strong, g);
476
477 reveal(CursorOwner::path_metaregion_sound);
478 assert forall|i: int|
479 #![trigger self.continuations[i]]
480 self.level - 1 <= i
481 < C::NR_LEVELS implies self.continuations[i].entry_own.metaregion_sound(
482 regions1,
483 ) by {
484 let cont_entry = self.continuations[i].entry_own;
485 if cont_entry.meta_slot_paddr() is Some {
486 let eidx = frame_to_index(cont_entry.meta_slot_paddr().unwrap());
487 if eidx != changed_idx {
488 cont_entry.lemma_metaregion_sound_one_slot_changed(
489 regions0,
490 regions1,
491 changed_idx,
492 );
493 }
494 }
495 };
496 }
497
498 pub proof fn take_next_remove_path_preserves_metaregion(
501 self,
502 owner_before_replace: Self,
503 regions0: MetaRegionOwners,
504 regions1: MetaRegionOwners,
505 removed_idx: int,
506 removed_path: TreePath<NR_ENTRIES>,
507 target: Mapping,
508 )
509 requires
510 self.inv(),
511 owner_before_replace.inv(),
512 owner_before_replace.in_barrier_va(),
513 self.metaregion_sound(regions0),
514 regions0.inv(),
515 regions0.contains(removed_idx),
516 regions0.slot_owners[removed_idx].usage !is PageTable,
517 self@.mappings == owner_before_replace@.mappings - PageTableOwner(
518 owner_before_replace.cur_subtree(),
519 )@.mappings,
520 PageTableOwner(owner_before_replace.cur_subtree())@.mappings == set![target],
521 owner_before_replace.cur_subtree().value().path == removed_path,
522 regions1.slots == regions0.slots,
523 regions1.slot_owners.dom() =~= regions0.slot_owners.dom(),
524 forall|i: int|
525 #![trigger regions1.slot_owners[i]]
526 i != removed_idx ==> regions0.slot_owners[i] == regions1.slot_owners[i],
527 regions1.slot_owners[removed_idx].same_permissions(regions0.slot_owners[removed_idx]),
528 regions1.slot_owners[removed_idx].slot_vaddr
529 == regions0.slot_owners[removed_idx].slot_vaddr,
530 regions1.slot_owners[removed_idx].usage == regions0.slot_owners[removed_idx].usage,
531 regions1.slot_owners[removed_idx].paths_in_pt
532 == regions0.slot_owners[removed_idx].paths_in_pt.remove(removed_path),
533 ensures
534 self.metaregion_sound(regions1),
535 {
536 C::lemma_paging_consts_properties();
537 C::lemma_page_table_config_constant_properties();
538 self.no_node_at_idx_from_slot_key(regions0, removed_idx);
539
540 owner_before_replace.lemma_cur_subtree_eq_filtered_mappings_path();
541
542 let ghost sv = vaddr_of::<C>(removed_path) as int;
543 let ghost sz = page_size::<C>(owner_before_replace.level) as int;
544 assert(sz > 0) by {
545 crate::specs::mm::page_table::cursor::page_size_lemmas::lemma_page_size_ge_base::<C>(
546 owner_before_replace.level,
547 );
548 };
549 assert forall|mm: Mapping| #[trigger] self@.mappings.contains(mm) implies mm.va_range.start
550 != sv by {};
551
552 self.no_frame_with_path_from_no_view_mapping(removed_path);
553 self.path_removable_from_no_node_and_no_frame_path(removed_idx, removed_path);
554 self.metaregion_preserved_under_path_remove(regions0, regions1, removed_idx, removed_path);
555 }
556}
557
558}