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
|
#define ATS_DYNLOADFLAG 0
#include "kernel/prelude/kernel_prelude.hats"
staload "./table.sats"
staload "./entry.sats"
staload "lib/SATS/valid.sats"
staload "lib/DATS/valid.dats"
assume table_t(c:int) = tablePtr1 c
// Calculate table address of next table based on current table and index: P4->P3->P2->P1
fn next_table_address{n:int|n < 3}(table : !table_t(n), index : sizeLt(ENTRY_COUNT)):<> Valid Ptr1 = let
val entry = table_get_at(table,index)
in
if (contains_flag(entry,PRESENT) && ~contains_flag(entry,HUGE_PAGE)) then
let
val next = g1ofg0((Ptr1_to_sz(table.3) << 9) lor (index << 12))
in
if next > i2sz(0) then
create_valid(sz_to_Ptr1(next))
else
create_unvalid()
end
else
create_unvalid()
end
// Checks if next table exists and returns bool
fn next_table_exists{n:int|n < 3}(table: !table_t(n),index : sizeLt(ENTRY_COUNT)):<> bool = let
val vnext = next_table_address(table,index)
in
if is_valid(vnext) then
let val _ = unwrap_valid(vnext) in true end
else
(destroy_unvalid(vnext); false)
end
implement table_get_at(table,i) = let
val entry = (table.3->[i]) : entry_t
in
entry
end
implement table_set_at(table,i,x) = table.3->[i] := x
// Set all the entries in the table to unused (zero)
fn set_zero(table: &Table_t):<!wrt> void = let
fun loop{i:nat | i < ENTRY_COUNT}.<ENTRY_COUNT-i>.(i : size_t i,t : !Table_t):<!wrt> void = let
in
set_unused(t.3->[i]);
if i < ENTRY_COUNT - 1 then
loop(succ(i),t)
end
in
loop(i2sz(0),table)
end
// UNSAFE: unsafe block
local
staload UN = "prelude/SATS/unsafe.sats"
in
// TODO: define safe unique ownership for this table
// see https://os.phil-opp.com/page-tables/#safety-2
implement get_P4() = let
val (pf,fpf | p) = $UN.ptr_vtake(ulint_to_Ptr1(0xfffffffffffff000ul))
in
(pf,fpf,0u | p)
end
implement next_table(table,index) = let
val next = next_table_address(table,index)
prval level = table.2
in
consume(table);
if is_valid(next) then
let
val t = unwrap_valid(next)
val (pf, fpf | p) = $UN.ptr_vtake(t)
in
create_valid ((pf,fpf, succ(level) | p))
end
else
let
val () = destroy_unvalid(next)
in
create_unvalid();
end
end
implement next_table_create(table,index) = let
staload "kernel/memory/frame.sats"
in
if ~next_table_exists(table,index) then (
println!("not exists");
assertloc(~contains_flag(table[index],HUGE_PAGE));
table[index] := create_entry(allocate_frame(),flags,i2sz(2)) where {
var flags = @[entry_flag](PRESENT,WRITABLE)
};
let
val vnext = next_table(table,index)
in
if is_valid(vnext) then
let var next = unwrap_valid(vnext) in (set_zero(next); create_valid(next)) end
else (
destroy_unvalid(vnext);
create_unvalid()
)
end
) else
$UN.castvwtp0(next_table(table,index)) //This is safe
end
end
|