-
Notifications
You must be signed in to change notification settings - Fork 0
/
Retype_AI.thy
3523 lines (2997 loc) · 128 KB
/
Retype_AI.thy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* Copyright 2014, General Dynamics C4 Systems
*
* This software may be distributed and modified according to the terms of
* the GNU General Public License version 2. Note that NO WARRANTY is provided.
* See "LICENSE_GPLv2.txt" for details.
*
* @TAG(GD_GPL)
*)
(*
Retype refinement
*)
theory Retype_AI
imports VSpace_AI
begin
lemma upto_enum_inc_1:
"a < 2^word_bits - 1 \<Longrightarrow> [(0::word32).e.1 + a] = [0.e.a] @ [(1+a)]"
apply (simp add:upto_enum_word)
apply (subgoal_tac "unat (1 +a) = 1 + unat a")
apply simp
apply (subst unat_plus_simple[THEN iffD1])
apply (rule word_plus_mono_right2[where b = "2^32 - 2"])
apply (simp add:word_bits_def minus_one_norm)+
apply unat_arith
apply simp
done
(* FIXME: move *)
lemma monad_eq_split:
assumes tail:"\<And>r s. Q r s \<Longrightarrow> f r s = f' r s"
and hoare: "\<lbrace>P\<rbrace>g\<lbrace>\<lambda>r s. Q r s\<rbrace>" "P s"
shows "(g>>=f) s = (g>>= f') s"
proof -
have pre: "\<And>aa bb. \<lbrakk>(aa, bb) \<in> fst (g s)\<rbrakk> \<Longrightarrow> Q aa bb"
using hoare
apply (clarsimp simp:valid_def)
apply (erule_tac x = s in allE)
apply simp
apply (drule bspec)
apply simp
apply simp
done
show ?thesis
apply (simp add:bind_def image_def)
apply (intro conjI)
apply (rule set_eqI)+
apply (clarsimp simp:Union_eq)
apply (rule iffI)
apply clarsimp
apply (rule_tac x=x in exI)
apply (clarsimp simp: tail[OF pre])
apply (rule exI)
apply (rule_tac x = "(aa,bb)" in bexI)
apply simp
apply clarsimp+
apply (rule_tac x=x in exI)
apply (clarsimp simp: tail[symmetric,OF pre])
apply (rule exI)
apply (rule_tac x = "(aa,bb)" in bexI)
apply simp
apply clarsimp
apply (rule iffI)
apply (clarsimp simp: tail[OF pre])
apply (rule exI)
apply (rule_tac x = "(aa,b)" in bexI)
apply simp
apply (clarsimp simp: tail[symmetric,OF pre])+
apply (rule exI)
apply (rule_tac x = "(aa,b)" in bexI)
apply simp
apply clarsimp
done
qed
lemma clearMemoryVM_return [simp]:
"clearMemoryVM a b = return ()"
by (simp add: clearMemoryVM_def storeWordVM_def)
lemma clearMemoryVM_return_raw:
"clearMemoryVM = (\<lambda>x y. return ())"
apply (rule ext)+
by (simp add: clearMemoryVM_def storeWordVM_def)
lemma unat_of_nat_minus_1:
"\<lbrakk>n < 2^len_of TYPE('a);n\<noteq> 0\<rbrakk> \<Longrightarrow> (unat (((of_nat n):: 'a :: len word) - 1)) = n - 1"
apply (subst unat_minus_one)
apply (rule of_nat_neq_0)
apply simp
apply (simp add:of_nat_neq_0 word_bits_def)
apply (simp add:unat_of_nat word_bits_def)
done
definition monad_commute where
"monad_commute P a b \<equiv>
(\<forall>s. (P s \<longrightarrow> ((do x\<leftarrow>a;y\<leftarrow>b; return (x, y) od) s) = ((do y\<leftarrow>b;x\<leftarrow>a; return (x, y) od) s)))"
lemma monad_eq:
"a s = b s \<Longrightarrow> (a >>= g) s = (b >>= g) s"
by (auto simp:bind_def)
lemma monad_commute_simple:
"\<lbrakk>monad_commute P a b;P s\<rbrakk> \<Longrightarrow> ((do x\<leftarrow>a;y\<leftarrow>b; g x y od) s) = ((do y\<leftarrow>b;x\<leftarrow>a; g x y od) s)"
apply (clarsimp simp:monad_commute_def)
apply (drule spec)
apply (erule(1) impE)
apply (drule_tac g = "(\<lambda>t. g (fst t) (snd t))" in monad_eq)
apply (simp add:bind_assoc)
done
lemma commute_commute:
"monad_commute P f h \<Longrightarrow> monad_commute P h f"
apply (simp (no_asm) add: monad_commute_def)
apply (clarsimp)
apply (erule monad_commute_simple[symmetric])
apply simp
done
lemma assert_commute: "monad_commute (K G) (assert G) f"
by (clarsimp simp:assert_def monad_commute_def)
lemma cond_fail_commute: "monad_commute (K (\<not>G)) (when G fail) f"
by (clarsimp simp:when_def fail_def monad_commute_def)
lemma return_commute: "monad_commute \<top> (return a) f"
by (clarsimp simp: return_def bind_def monad_commute_def)
lemma monad_commute_guard_imp:
"\<lbrakk>monad_commute P f h; \<And>s. Q s \<Longrightarrow> P s \<rbrakk> \<Longrightarrow> monad_commute Q f h"
by (clarsimp simp:monad_commute_def)
lemma monad_commute_split:
"\<lbrakk>\<And>r. monad_commute (Q r) f (g r); monad_commute P f h;
\<lbrace>P'\<rbrace> h \<lbrace>\<lambda>r. Q r\<rbrace>\<rbrakk>
\<Longrightarrow> monad_commute (P and P') f (h>>=g)"
apply (simp (no_asm) add:monad_commute_def)
apply (clarsimp simp:bind_assoc)
apply (subst monad_commute_simple)
apply simp+
apply (rule_tac Q = "(\<lambda>x. Q x)" in monad_eq_split)
apply (subst monad_commute_simple[where a = f])
apply assumption
apply simp+
done
lemma monad_commute_get:
assumes hf: "\<And>P. \<lbrace>P\<rbrace> f \<lbrace>\<lambda>r. P\<rbrace>"
and hg: "\<And>P. \<lbrace>P\<rbrace> g \<lbrace>\<lambda>r. P\<rbrace>"
and eptyf: "empty_fail f" "empty_fail g"
shows "monad_commute \<top> f g"
proof -
have fsame: "\<And>a b s. (a,b) \<in> fst (f s) \<Longrightarrow> b = s"
by (drule use_valid[OF _ hf],auto)
have gsame: "\<And>a b s. (a,b) \<in> fst (g s) \<Longrightarrow> b = s"
by (drule use_valid[OF _ hg],auto)
note ef = empty_fail_not_snd[OF _ eptyf(1)]
note eg = empty_fail_not_snd[OF _ eptyf(2)]
show ?thesis
apply (simp add:monad_commute_def)
apply (clarsimp simp:bind_def split_def return_def)
apply (intro conjI)
apply (rule set_eqI)
apply (rule iffI)
apply (clarsimp simp:Union_eq dest!: singletonD)
apply (frule fsame)
apply clarsimp
apply (frule gsame)
apply (metis fst_conv snd_conv)
apply (clarsimp simp:Union_eq dest!: singletonD)
apply (frule gsame)
apply clarsimp
apply (frule fsame)
apply clarsimp
apply (metis fst_conv snd_conv)
apply (rule iffI)
apply (erule disjE)
apply (clarsimp simp:image_def)
apply (metis fsame)
apply (clarsimp simp:image_def)
apply (drule eg)
apply clarsimp
apply (rule bexI [rotated], assumption)
apply (frule gsame)
apply clarsimp
apply (erule disjE)
apply (clarsimp simp:image_def dest!:gsame)
apply (clarsimp simp:image_def)
apply (drule ef)
apply clarsimp
apply (frule fsame)
apply (erule bexI[rotated])
apply simp
done
qed
lemma range_to_bl':
"\<lbrakk> is_aligned (ptr :: 'a :: len word) bits; bits < len_of TYPE('a) \<rbrakk> \<Longrightarrow>
{ptr .. ptr + (2 ^ bits) - 1} = {x. take (len_of TYPE('a) - bits) (to_bl x)
= take (len_of TYPE('a) - bits) (to_bl ptr)}"
apply (rule set_eqI, rule iffI)
apply clarsimp
apply (subgoal_tac "\<exists>y. x = ptr + y \<and> y < 2 ^ bits")
apply clarsimp
apply (subst is_aligned_add_conv)
apply assumption
apply simp
apply simp
apply (rule_tac x="x - ptr" in exI)
apply (simp add: add_diff_eq[symmetric])
apply (simp only: word_less_sub_le[symmetric])
apply (rule word_diff_ls')
apply (simp add: field_simps)
apply assumption
apply simp
apply (subgoal_tac "\<exists>y. y < 2 ^ bits \<and> to_bl (ptr + y) = to_bl x")
apply clarsimp
apply (rule conjI)
apply (erule(1) is_aligned_no_wrap')
apply (simp only: add_diff_eq[symmetric])
apply (rule word_plus_mono_right)
apply simp
apply (erule is_aligned_no_wrap')
apply simp
apply (rule_tac x="of_bl (drop (len_of TYPE('a) - bits) (to_bl x))" in exI)
apply (rule context_conjI)
apply (rule order_less_le_trans [OF of_bl_length])
apply simp
apply simp
apply (subst is_aligned_add_conv)
apply assumption
apply simp
apply (drule sym)
apply (simp add: word_rep_drop)
done
lemma range_to_bl:
"is_aligned (ptr :: 'a :: len word) bits \<Longrightarrow>
{ptr..ptr + 2 ^ bits - 1} =
{x. take (len_of TYPE('a) - bits) (to_bl x) =
take (len_of TYPE('a) - bits) (to_bl ptr)}"
apply (erule is_aligned_get_word_bits)
apply (erule(1) range_to_bl')
apply (rule set_eqI)
apply (simp add: power_overflow)
done
lemma aligned_ranges_subset_or_disjoint:
"\<lbrakk> is_aligned (p :: 'a :: len word) n; is_aligned (p' :: 'a :: len word) n' \<rbrakk>
\<Longrightarrow> {p .. p + 2 ^ n - 1} \<inter> {p' .. p' + 2 ^ n' - 1} = {}
\<or> {p .. p + 2 ^ n - 1} \<subseteq> {p' .. p' + 2 ^ n' - 1}
\<or> {p .. p + 2 ^ n - 1} \<supseteq> {p' .. p' + 2 ^ n' - 1}"
apply (simp add: range_to_bl)
apply (rule disjCI2)
apply (erule nonemptyE)
apply simp
apply (subgoal_tac "(\<exists>n''. len_of TYPE('a) - n = (len_of TYPE('a) - n') + n'')
\<or> (\<exists>n''. len_of TYPE('a) - n' = (len_of TYPE('a) - n) + n'')")
apply (elim conjE disjE exE)
apply (rule disjI1)
apply (clarsimp simp: take_add)
apply (rule disjI2)
apply (clarsimp simp: take_add)
apply arith
done
lemma aligned_diff:
"\<lbrakk>is_aligned (dest :: 'a :: len word) bits; is_aligned (ptr :: 'a :: len word) sz; bits \<le> sz; sz < len_of TYPE('a);
dest < ptr\<rbrakk>
\<Longrightarrow> (2^ bits - 1) + dest < ptr"
apply (frule_tac p' = ptr in aligned_ranges_subset_or_disjoint)
apply assumption
apply (elim disjE)
apply clarsimp
apply (drule_tac ptr = dest in is_aligned_no_overflow)
apply simp
apply (drule is_aligned_no_overflow)
apply clarsimp
apply (erule impE)
apply (erule order_trans[OF less_imp_le])
apply (clarsimp simp:field_simps)
apply (clarsimp simp:not_less field_simps not_le)
apply clarsimp
apply (drule_tac ptr = dest in is_aligned_no_overflow)
apply simp
apply fastforce
apply clarsimp
apply (frule is_aligned_no_overflow)
apply (erule impE)
apply (frule(1) is_aligned_no_overflow)
apply (rule ccontr)
apply (clarsimp simp:not_less p_assoc_help)
apply (subst (asm) add_commute[where b = "(2^ sz - 1)"])
apply (subst (asm) add_commute[where b = "(2^ bits - 1)"])+
apply (drule word_sub_mono2)
apply (rule word_le_minus_mono_left)
apply (erule(1) two_power_increasing)
apply (simp add:word_1_le_power)
apply (simp add:field_simps is_aligned_no_overflow)+
done
lemma default_object_tcbE:
"\<lbrakk> default_object ty us = TCB tcb; ty \<noteq> Untyped;
\<lbrakk> tcb = default_tcb; ty = Structures_A.TCBObject \<rbrakk> \<Longrightarrow> R \<rbrakk> \<Longrightarrow> R"
unfolding default_object_def by (cases ty, auto)
lemma obj_bits_api_default_object:
"\<lbrakk> ty \<noteq> Untyped\<rbrakk> \<Longrightarrow> obj_bits_api ty us
= obj_bits (default_object ty us)"
unfolding obj_bits_api_def default_object_def
by (cases ty)
(simp_all add: slot_bits_def wf_empty_bits cte_level_bits_def)
lemma obj_bits_api_default_CapTableObject:
"obj_bits (default_object Structures_A.apiobject_type.CapTableObject us)
= cte_level_bits + us"
unfolding default_object_def
by (simp add: cte_level_bits_def wf_empty_bits)
lemma empty_cnode_dom:
"x \<in> dom (empty_cnode n) \<Longrightarrow> length x = n"
unfolding dom_def empty_cnode_def by (simp split: split_if_asm)
lemma obj_bits_api_def2:
"obj_bits_api type obj_size_bits =
(case type of Structures_A.Untyped \<Rightarrow> obj_size_bits
| _ \<Rightarrow> obj_bits (default_object type obj_size_bits))"
by (simp add: obj_bits_api_def default_object_def obj_bits.simps
wf_empty_bits dom_empty_cnode ex_with_length
slot_bits_def cte_level_bits_def
split: Structures_A.apiobject_type.split)
lemma obj_bits_api_def3:
"obj_bits_api type obj_size_bits =
(if type = Structures_A.Untyped then obj_size_bits
else obj_bits (default_object type obj_size_bits))"
by (simp add: obj_bits_api_def2 split: Structures_A.apiobject_type.split)
definition
"retype_addrs \<equiv> \<lambda>ptr' ty n us. map (\<lambda>p. ptr_add ptr' (p * 2 ^ obj_bits_api ty us))
[0..< n]"
lemma retype_addrs_base [simp]:
"0 < n \<Longrightarrow> x \<in> set (retype_addrs x ty n us)"
unfolding retype_addrs_def
apply (simp add: ptr_add_def)
apply (rule image_eqI [where x = 0])
apply simp
apply (simp add: power_sub[symmetric])
done
lemma retype_addrs_aligned:
assumes xin: "x \<in> set (retype_addrs ptr ty n us)"
and al: "is_aligned ptr (obj_bits_api ty us)"
and nv: "sz < word_bits"
and oav: "obj_bits_api ty us \<le> sz"
shows "is_aligned x (obj_bits_api ty us)"
using xin unfolding retype_addrs_def ptr_add_def
apply (clarsimp simp: word_unat_power [symmetric])
apply (subst mult_commute, subst shiftl_t2n [symmetric])
apply (rule aligned_add_aligned[OF al is_aligned_shift])
apply (insert assms)
apply simp+
done
lemma (in pspace_update_eq) pspace_no_overlap_update [simp]:
"pspace_no_overlap ptr bits (f s) = pspace_no_overlap ptr bits s"
by (simp add: pspace_no_overlap_def pspace)
(* FIXME: move *)
lemma multi_lessD:
"\<lbrakk>(a::nat)*b < c;0<a;0<b\<rbrakk> \<Longrightarrow> a < c \<and> b < c"
by (cases a, simp_all,cases b,simp_all)
lemma unat_le_helper:
"(x :: 'a :: len word) \<le> of_nat n \<Longrightarrow> unat x \<le> n"
apply (case_tac "x = of_nat n")
apply (simp add:unat_of_nat)
apply (rule less_imp_le[OF unat_less_helper])
apply simp
done
lemma word_of_nat_plus:
"of_nat (a + b) = of_nat a + (of_nat b :: ('a :: len) word)"
by (rule of_nat_add)
lemma word_of_nat_minus:
"b<= a ==> of_nat (a - b) = of_nat a - (of_nat b :: ('a :: len) word)"
by (simp add: word_of_nat word_of_int_hom_syms)
lemma unat_shiftl_absorb:
"\<lbrakk>x \<le> 2^p ; p + k < len_of TYPE('a)\<rbrakk>
\<Longrightarrow> unat (x :: 'a :: len word) * 2^k = unat (x * 2^k)"
apply (simp add:unat_word_ariths)
apply (subst mod_less)
apply (rule le_less_trans[OF mult_le_mono1])
apply (erule iffD1[OF word_le_nat_alt])
apply (clarsimp simp: power_add[symmetric])+
done
lemma word_up_bound:
"(ptr::('a::len) word) \<le> 2^(len_of TYPE('a)) - 1 "
by auto
lemma word_plus_mono_right_split:
"\<lbrakk>unat ((x :: 'a :: len word) && mask sz) + unat z < 2 ^ sz ; sz < len_of TYPE('a)\<rbrakk>
\<Longrightarrow>x \<le> x + z"
(is "\<lbrakk>?bound;?len\<rbrakk> \<Longrightarrow>?rhs \<le> ?lhs")
apply (subgoal_tac "(x && ~~ mask sz) + (x && mask sz) \<le> (x && ~~ mask sz) + ((x && mask sz) + z)")
apply (simp add:word_plus_and_or_coroll2 field_simps)
apply (rule word_plus_mono_right)
apply (simp add:no_olen_add )
apply (rule less_le_trans)
apply (simp add:uint_nat)
apply (subst zadd_int)
apply (drule iffD2[OF zless_int])
apply simp
apply (rule less_imp_le)
apply (rule less_le_trans[where y = "2^len_of TYPE('a)"] )
apply simp
apply (simp add:word_bits_def)
apply (rule word_plus_mono_right2)
apply (rule is_aligned_no_overflow')
apply (rule is_aligned_neg_mask[OF le_refl])
apply (rule le_m1_iff_lt[THEN iffD1,THEN iffD2])
apply (simp add:p2_gt_0 word_bits_def)
apply (rule iffD2[OF word_less_nat_alt])
apply (auto simp:unat_plus_if_size word_size word_bits_def not_less)
done
lemmas word32_plus_mono_right_split = word_plus_mono_right_split[where 'a=32, folded word_bits_def]
(* range_cover locale:
proves properties when a small range is inside in a large range
*)
locale range_cover =
fixes ptr :: "'a :: len word"
and sz sbit n
assumes aligned: "is_aligned ptr sbit"
and sz:"sz< len_of TYPE('a)" "sbit \<le> sz" "n + unat (ptr && mask sz >> sbit) \<le> 2 ^ (sz - sbit)"
begin
lemma range_cover_compare_bound:
"n * 2 ^ sbit + unat (ptr && mask sz) \<le> 2 ^ sz"
proof -
have mask_before_neg_mask: "(ptr && mask sz) && ~~ mask sbit = ptr && mask sz"
using aligned sz
by (simp add:mask_twice is_aligned_mask mask_out_sub_mask min_def)
show ?thesis using aligned sz
apply (drule_tac i = "?a +?b" in Nat.mult_le_mono[where k = "2^sbit",OF _ le_refl])
apply (subst (asm) add_mult_distrib)
apply (clarsimp simp: power_add[symmetric])
apply (subst (asm) unat_shiftl_absorb[where p = "sz - sbit"])
apply (rule less_imp_le)
apply (rule shiftr_less_t2n)
apply (rule less_le_trans)
apply (rule and_mask_less')
apply (simp add:word_bits_def)
apply (rule two_power_increasing)
apply simp
apply (simp add:word_bits_def field_simps)
apply simp
apply (subst (asm) mult_commute[where b = "2^sbit"],
subst (asm) shiftl_t2n[symmetric])
apply (subst (asm) and_not_mask[symmetric])
apply (simp add:mask_before_neg_mask)
done
qed
lemma range_cover_compare:
assumes pointer:"p < n"
shows "unat (ptr && mask sz) + unat (((of_nat p) :: 'a :: len word) * 2 ^ sbit) < 2 ^ sz"
proof -
have mask_before_neg_mask: "(ptr && mask sz) && ~~ mask sbit = ptr && mask sz"
using aligned sz
by (simp add:mask_twice is_aligned_mask mask_out_sub_mask min_def)
have absolute_compare:"n * 2 ^ sbit + unat (ptr && mask sz) \<le> 2 ^ sz"
by (rule range_cover_compare_bound)
have no_overflow_n:"n * 2^sbit < 2^len_of TYPE('a)"
using aligned sz
apply (clarsimp dest!:add_leD1)
apply (rule le_less_trans)
apply (drule Nat.mult_le_mono[where i = n and k = "2^sbit",OF _ le_refl])
apply (clarsimp simp: power_add[symmetric])
apply (assumption)
apply clarsimp
done
have no_overflow_p:"p * 2^sbit < 2^len_of TYPE('a)"
apply (rule le_less_trans[OF _ no_overflow_n])
apply (simp add:pointer less_imp_le)
done
show ?thesis
apply (rule less_le_trans[OF _ absolute_compare])
apply (subst add_commute)
apply clarsimp
apply (case_tac "p = 0")
apply (insert pointer)
apply (clarsimp simp: range_cover_def pointer)
apply (simp add:unat_word_ariths)
apply (rule le_less_trans[OF mod_le_dividend])
apply (rule less_le_trans[OF mult_less_mono1[where j = n]])
apply (cut_tac no_overflow_p)
apply (drule multi_lessD[OF no_overflow_p],simp)
apply (clarsimp simp:unat_of_nat word_bits_def)
using sz
apply (simp add:unat_gt_0 range_cover_def)
apply (rule mult_le_mono2)
apply (rule unat_le_helper)
apply simp
done
qed
lemma range_cover_n_le:
"n \<le> 2 ^ (len_of TYPE('a) - sbit)"
"n \<le> 2 ^ (sz - sbit)"
using aligned sz
by (auto elim: le_trans[OF add_leD1])
lemma range_cover_n_less:
shows weak: "n < 2 ^ len_of TYPE('a)"
and string: "n < 2 ^ (len_of TYPE('a) - sbit)"
proof -
show str: "n < 2 ^ (len_of TYPE('a) - sbit)"
using aligned sz
by (auto intro: le_less_trans range_cover_n_le(2))
show "n<2^len_of TYPE('a)"
using str by (rule less_le_trans) simp
qed
lemma range_cover_le_n_less:
"p \<le> n \<Longrightarrow> p < 2^ len_of TYPE('a)"
"p \<le> n \<Longrightarrow> p < 2^ (len_of TYPE('a) - sbit)"
apply (erule le_less_trans[OF _ range_cover_n_less(1)])
apply (erule le_less_trans[OF _ range_cover_n_less(2)])
done
lemma unat_of_nat_n :"unat ((of_nat n):: 'a :: len word) = n"
using range_cover_n_less
apply (simp add:unat_of_nat)
done
lemma unat_of_nat_n_shift:
"gbits \<le> sbit \<Longrightarrow> unat (((of_nat n):: 'a :: len word) << gbits) = (n * 2^ gbits)"
apply (simp add:shiftl_t2n field_simps)
apply (subst mult_commute)
apply (subst unat_mult_power_lem)
apply (case_tac "gbits = sbit")
apply (rule le_less_trans[OF range_cover_n_le(2)])
apply clarsimp
apply (rule diff_less_mono)
apply (rule sz)
apply (rule sz)
apply (rule le_less_trans[OF range_cover_n_le(1)])
apply clarsimp
apply (rule diff_less_mono2)
apply simp
using sz
apply simp
apply simp
done
lemma unat_of_nat_shift:
"\<lbrakk>gbits \<le> sbit;p\<le> n\<rbrakk> \<Longrightarrow> (unat (((of_nat p):: 'a :: len word) * 2 ^ gbits)) = (p * 2^ gbits)"
apply (subst mult_commute[where a = "of_nat p"])
apply (subst mult_commute[where a = "p "])
apply (subst unat_mult_power_lem)
apply (case_tac "gbits = sbit")
apply simp
apply (erule le_less_trans[OF _ range_cover_le_n_less(2) ])
apply simp
apply (erule le_less_trans)
apply (rule less_le_trans[OF range_cover_n_less(2)])
apply clarsimp
apply (erule diff_le_mono2)
using assms
apply (simp add:range_cover_def)+
done
lemma range_cover_base_le:
"(ptr && mask sz) \<le> (ptr && mask sz) + (of_nat n << sbit)"
apply (clarsimp simp:no_olen_add_nat shiftl_t2n unat_of_nat_shift field_simps)
apply (subst add_commute)
apply (rule le_less_trans[OF range_cover_compare_bound])
apply (rule less_le_trans[OF power_strict_increasing])
using sz
apply simp+
done
end
lemma range_cover_subset:
fixes ptr :: "'a :: len word"
assumes cover: "range_cover ptr sz sbit n"
assumes pointer: "p<n"
assumes not_0: "n \<noteq> 0"
shows "{ptr + of_nat p * 2^sbit .. ptr + of_nat p * 2 ^ sbit + 2 ^ sbit - 1} \<subseteq> {ptr .. ptr + of_nat n * 2 ^ sbit - 1}"
apply clarsimp
apply (intro conjI)
apply (rule word_plus_mono_right_split[OF range_cover.range_cover_compare[OF cover pointer]])
using cover
apply (simp add:range_cover_def)
proof -
note n_less = range_cover.range_cover_n_less[OF cover]
have unat_of_nat_m1: "unat (of_nat n - (1 :: 'a :: len word)) < n"
using not_0 n_less
by (simp add:unat_of_nat_minus_1)
have decomp:"of_nat n * 2 ^ sbit = of_nat (n - 1) * 2 ^ sbit + (2 :: 'a :: len word) ^ sbit"
apply (simp add:distrib_right[where b = "1::'a word",simplified,symmetric])
using not_0 n_less
apply (simp add:unat_of_nat_minus_1)
done
show "ptr + of_nat p * 2 ^ sbit + 2 ^ sbit - 1 \<le> ptr + of_nat n * 2 ^ sbit - 1"
apply (subst decomp)
apply (simp add:add_assoc[symmetric])
apply (simp add:p_assoc_help)
apply (rule order_trans[OF word_plus_mono_left word_plus_mono_right])
apply (rule word_plus_mono_right)
apply (rule word_mult_le_mono1[OF word_of_nat_le])
apply (insert n_less not_0 pointer)
apply (simp add:unat_of_nat_minus_1)
apply (rule p2_gt_0[THEN iffD2])
using cover
apply (simp add:word_bits_def range_cover_def)
using cover
apply (simp only: word_bits_def[symmetric] unat_power_lower range_cover_def)
apply (clarsimp simp: unat_of_nat_minus_1 )
apply (rule nat_less_power_trans2[OF range_cover.range_cover_le_n_less(2),OF cover])
apply (simp add:unat_of_nat_m1 less_imp_le)
using cover
apply (simp add:range_cover_def)
apply (rule word_plus_mono_right_split[where sz = sz])
using range_cover.range_cover_compare[OF cover,where p = "unat (of_nat n - (1 :: 'a :: len word))"]
apply (clarsimp simp:unat_of_nat_m1)
using cover
apply (simp add:range_cover_def)
apply (rule olen_add_eqv[THEN iffD2])
apply (subst add_commute[where a = "2^sbit - 1"])
apply (subst p_assoc_help[symmetric])
apply (rule is_aligned_no_overflow)
apply (insert cover)
apply (clarsimp simp:range_cover_def)
apply (erule aligned_add_aligned[OF _ is_aligned_mult_triv2])
apply (simp add:range_cover_def)+
done
qed
lemma range_cover_rel:
assumes cover: "range_cover ptr sz sbit n"
assumes le:"sbit' \<le> sbit"
assumes num_r: "m = 2 ^ (sbit - sbit') * n"
shows "range_cover ptr sz sbit' m"
using cover
apply (clarsimp simp:num_r range_cover_def)
apply (intro conjI)
apply (erule is_aligned_weaken[OF _ le])
apply (erule le_trans[OF le])
apply (drule is_aligned_weaken[OF _ le])+
apply (drule mult_le_mono2[where j = "2^(sz-sbit)" and k = "2^(sbit-sbit')"])
apply (subst (asm) power_add[symmetric])
apply (clarsimp simp:field_simps le)
apply (erule le_trans[rotated])
apply clarsimp
apply (rule unat_le_helper)
apply clarsimp
apply (insert le)
apply (fold shiftl_t2n)
apply (simp add: shiftr_shiftl1)
apply (rule eq_refl[OF is_aligned_neg_mask_eq[symmetric]])
apply (rule is_aligned_shiftr[OF is_aligned_weaken])
apply (rule aligned_already_mask[where n = "sbit"])
apply (insert cover)
apply (simp add:range_cover_def)
apply simp
done
lemma unat_plus_gt:
"unat ((a::('a::len word)) + b) \<le> (unat a + unat b)"
by (clarsimp simp:unat_plus_if_size)
lemma retype_addrs_subset_ptr_bits:
assumes cover: "range_cover ptr sz (obj_bits_api ty us) n"
shows "set (retype_addrs ptr ty n us) \<subseteq> {ptr .. (ptr &&~~ mask sz) + (2 ^ sz - 1)}"
apply (clarsimp simp:retype_addrs_def ptr_add_def)
apply (intro conjI)
apply (rule word_plus_mono_right_split)
apply (erule range_cover.range_cover_compare[OF cover])
using cover
apply (simp add:range_cover_def)
apply (subst word_plus_and_or_coroll2[symmetric,where w = "mask sz"])
apply (subst add_commute)
apply (subst add_assoc)
apply (rule word_plus_mono_right)
apply (insert cover)
apply (drule(1) range_cover.range_cover_compare)
apply (rule iffD1[OF le_m1_iff_lt,THEN iffD2])
using cover
apply (simp add: p2_gt_0 range_cover_def word_bits_def)
apply (rule iffD2[OF word_less_nat_alt])
apply (rule le_less_trans[OF unat_plus_gt])
using cover
apply (clarsimp simp:unat_power_lower range_cover_def)
apply (insert cover)
apply (rule is_aligned_no_wrap'[OF is_aligned_neg_mask,OF le_refl ])
apply (simp add:range_cover_def)+
done
lemma pspace_no_overlapE:
"\<lbrakk> pspace_no_overlap ptr sz s; kheap s x = Some ko;
{x..x + (2 ^ obj_bits ko - 1)} \<inter> {ptr..(ptr && ~~ mask sz) + (2 ^ sz - 1)} = {} \<Longrightarrow> R\<rbrakk> \<Longrightarrow> R"
unfolding pspace_no_overlap_def by auto
lemma base_member_set:
fixes x :: "'a :: len word"
assumes al: "is_aligned x sz"
and szv: "sz < len_of TYPE('a)"
shows "x \<in> {x .. x + (2 ^ sz - 1)}"
proof (simp, rule is_aligned_no_wrap')
show "(2 :: 'a :: len word) ^ sz - 1 < 2 ^ sz" using szv
by (simp add: word_less_nat_alt word_neq_0_conv unat_minus_one)
qed fact+
lemma pspace_no_overlap_into_Int_none:
assumes ps: "pspace_no_overlap ptr sz s"
and vp: "valid_pspace s"
and cover: "range_cover ptr sz (obj_bits_api ty us) n"
shows "set (retype_addrs ptr ty n us) \<inter> dom (kheap s) = {}"
proof -
{
fix x ko
assume ps': "kheap s x = Some ko"
have "x \<notin> {ptr .. (ptr && ~~ mask sz) + (2 ^ sz - 1)}"
proof (rule orthD1)
show "x \<in> {x .. x + (2 ^ obj_bits ko - 1)}"
proof (rule base_member_set)
from vp show "is_aligned x (obj_bits ko)" using ps'
by (auto elim!: valid_pspaceE pspace_alignedE)
show "obj_bits ko < len_of TYPE(32)"
by (rule valid_pspace_obj_sizes [OF _ ranI, unfolded word_bits_def]) fact+
qed
show "{x..x + (2 ^ obj_bits ko - 1)} \<inter> {ptr..(ptr && ~~ mask sz) + (2 ^ sz - 1)} = {}" using ps
by (rule pspace_no_overlapE) fact+
qed
hence "x \<notin> set (retype_addrs ptr ty n us)"
using assms subsetD[OF retype_addrs_subset_ptr_bits[OF cover]]
by auto
}
thus ?thesis by auto
qed
lemma pspace_no_overlapD1:
"\<lbrakk> pspace_no_overlap ptr sz s; kheap s x = Some ko;
range_cover ptr sz (obj_bits_api ty us) n;
valid_pspace s\<rbrakk> \<Longrightarrow>
x \<notin> set (retype_addrs ptr ty n us)"
apply (drule(2) pspace_no_overlap_into_Int_none)
apply (simp add:range_cover_def)
apply (erule orthD2)
apply (erule domI)
done
lemma pspace_no_overlapD2:
"\<lbrakk> pspace_no_overlap ptr sz s; x \<in> set (retype_addrs ptr ty n us);
range_cover ptr sz (obj_bits_api ty us) n;
valid_pspace s \<rbrakk> \<Longrightarrow> x \<notin> dom (kheap s)"
apply (drule(2) pspace_no_overlap_into_Int_none)
apply (simp add:range_cover_def)
apply (erule(1) orthD1)
done
lemma pspace_no_overlapC:
"\<lbrakk> pspace_no_overlap ptr sz s; x \<in> set (retype_addrs ptr ty n us); kheap s x = Some ko;
range_cover ptr sz (obj_bits_api ty us) n;valid_pspace s \<rbrakk> \<Longrightarrow> P"
by (auto dest: pspace_no_overlapD1)
lemma null_filterE:
"\<lbrakk> null_filter cps x = Some cap;
\<lbrakk> cps x = Some cap; cap \<noteq> cap.NullCap \<rbrakk> \<Longrightarrow> R \<rbrakk>
\<Longrightarrow> R"
by (simp add: null_filter_def split: split_if_asm)
lemma across_null_filter_eq:
assumes eq: "null_filter xs = null_filter ys"
shows "\<lbrakk> xs x = Some v; ys x = Some v \<Longrightarrow> R;
\<lbrakk> v = cap.NullCap; ys x = None \<rbrakk> \<Longrightarrow> R \<rbrakk>
\<Longrightarrow> R"
apply (cases "null_filter xs x")
apply (subgoal_tac "null_filter ys x = None")
apply (simp add: null_filter_def split: split_if_asm)
apply (simp add: eq)
apply (subgoal_tac "null_filter ys x = Some a")
apply (simp add: null_filter_def split: split_if_asm)
apply (simp add: eq)
done
lemma mdb_cte_at_no_descendants:
"\<lbrakk> mdb_cte_at f m; \<not> f x \<rbrakk> \<Longrightarrow> descendants_of x m = {}"
apply (clarsimp simp add: descendants_of_def)
apply (erule tranclE2)
apply (simp add: cdt_parent_of_def)
apply (drule(1) mdb_cte_atD)
apply simp
apply (simp add: cdt_parent_of_def)
apply (drule(1) mdb_cte_atD)
apply simp
done
lemma caps_of_state_foldr:
assumes tyun: "ty \<noteq> Untyped"
fixes s sz ptr us addrs
defines "s' \<equiv> (s\<lparr>kheap := foldr (\<lambda>p ps. ps(p \<mapsto> default_object ty us))
addrs (kheap s)\<rparr>)"
shows
"caps_of_state s' =
(\<lambda>(oref,cref). if oref \<in> set addrs
then (case ty of Structures_A.CapTableObject \<Rightarrow> empty_cnode us
| Structures_A.TCBObject \<Rightarrow> option_map (\<lambda>x. cap.NullCap) \<circ> tcb_cap_cases
| _ \<Rightarrow> empty) cref
else caps_of_state s (oref,cref))"
apply (rule ext)+
apply (case_tac x)
apply (rename_tac oref cref)
apply (simp add: caps_of_state_cte_wp_at split del: split_if)
apply (case_tac "\<exists>cap. cte_wp_at (op = cap) (oref, cref) s'")
apply clarsimp
apply (simp add: s'_def cte_wp_at_cases)
apply (erule disjE)
apply (clarsimp simp add: foldr_upd_app_if default_object_def caps_of_state_cte_wp_at
cte_wp_at_cases tyun empty_cnode_def
split: split_if_asm Structures_A.apiobject_type.splits)
apply (clarsimp simp add: foldr_upd_app_if default_object_def caps_of_state_cte_wp_at
cte_wp_at_cases tyun empty_cnode_def default_tcb_def
split: split_if_asm Structures_A.apiobject_type.splits)
apply (clarsimp simp: tcb_cap_cases_def split: split_if_asm)
apply simp
apply (simp add: cte_wp_at_cases s'_def foldr_upd_app_if)
apply (rule conjI)
apply (clarsimp simp: default_object_def wf_empty_bits
split: Structures_A.apiobject_type.split_asm)
apply (fastforce simp: tcb_cap_cases_def split: split_if_asm)
apply clarsimp
apply (simp add: caps_of_state_cte_wp_at)
apply (simp add: cte_wp_at_cases)
done
lemma null_filter_caps_of_state_foldr:
fixes s sz ptr us addrs
assumes tyun: "ty \<noteq> Untyped"
and nondom: "\<forall>x \<in> set addrs. x \<notin> dom (kheap s)"
defines "s' \<equiv> (s\<lparr>kheap := foldr (\<lambda>p ps. ps(p \<mapsto> default_object ty us))
addrs (kheap s)\<rparr>)"
shows
"null_filter (caps_of_state s') =
null_filter (caps_of_state s)"
unfolding s'_def
apply (subst caps_of_state_foldr[OF tyun])
apply (rule ext)
apply (clarsimp simp add: null_filter_def split_def empty_cnode_def
split: Structures_A.apiobject_type.splits)
apply (subgoal_tac "a \<in> set addrs \<longrightarrow> caps_of_state s (a, b) \<noteq> Some cap.NullCap
\<longrightarrow> None = caps_of_state s (a, b)", simp)
apply clarsimp
apply (subgoal_tac "tcb_cap_cases b = None", simp)
apply (rule ccontr, clarsimp)
apply clarsimp
apply (rule sym, rule ccontr, clarsimp)
apply (drule bspec[OF nondom])
apply (drule caps_of_state_cteD)
apply (erule cte_wp_atE, fastforce+)
done
lemma retype_addrs_fold:
" map (\<lambda>p. ptr_add ptr' (p * 2 ^ obj_bits_api ty us)) [0..< n ]
= retype_addrs ptr' ty n us"
by (simp add: retype_addrs_def power_sub)
lemma mult_div_rearrange:
"(b::nat) \<le> a \<Longrightarrow> (2::nat) ^ a * (p div 2 ^ b) =
2 ^ (a - b) * (2 ^ b * (p div 2 ^ b))"
by (auto simp:field_simps power_add[symmetric])
lemma shiftr_mask_cmp:
"\<lbrakk>c \<le> n; n \<le> len_of TYPE('a)\<rbrakk>
\<Longrightarrow> ((a::('a::len) word) \<le> mask n) = ((a >> c) \<le> mask (n - c))"
apply (rule iffI)
apply (drule le_shiftr[where n = c])
apply (simp add:mask_2pm1[symmetric] shiftr_mask2)+
apply (simp add:le_mask_iff shiftr_shiftr)
done
definition
"no_gs_types \<equiv> UNIV - {Structures_A.CapTableObject,
ArchObject SmallPageObj, ArchObject LargePageObj,
ArchObject SectionObj, ArchObject SuperSectionObj}"
lemma no_gs_types_simps[simp]:
"Untyped \<in> no_gs_types"
"Structures_A.TCBObject \<in> no_gs_types"
"Structures_A.EndpointObject \<in> no_gs_types"
"Structures_A.AsyncEndpointObject \<in> no_gs_types"
"ArchObject PageTableObj \<in> no_gs_types"
"ArchObject PageDirectoryObj \<in> no_gs_types"
"ArchObject ASIDPoolObj \<in> no_gs_types"
by (simp_all add: no_gs_types_def)
lemma measure_unat': "p \<noteq> 0 \<Longrightarrow> unat (p - 1) \<le> unat p - 1"
apply (insert measure_unat[where p = p])
apply simp
done
(* FIXME: move *)
lemma range_cover_not_zero:
"\<lbrakk>n \<noteq> 0; range_cover (ptr :: 'a :: len word) sz bits n\<rbrakk> \<Longrightarrow> ((of_nat n) :: 'a :: len word) \<noteq> 0"
apply (rule of_nat_neq_0)
apply simp
apply (drule range_cover.range_cover_n_less)
apply simp
done
lemma range_cover_not_zero_shift:
"\<lbrakk>n \<noteq> 0; range_cover (ptr :: 'a :: len word) sz bits n; gbits \<le> bits\<rbrakk>
\<Longrightarrow> ((of_nat n) :: 'a :: len word) << gbits \<noteq> 0"
apply (rule word_shift_nonzero[where m = "sz-gbits"])
prefer 2
apply (clarsimp simp:range_cover_def)
apply (clarsimp simp:word_le_nat_alt)
apply (subst unat_power_lower)
apply (rule less_le_trans[OF diff_less_Suc])
apply (simp add:range_cover_def)
apply (simp add:range_cover.unat_of_nat_n)
apply (erule le_trans[OF range_cover.range_cover_n_le(2)])
apply (rule power_increasing)
apply simp+