interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / kernel / arm64 / frames.om

284 lines · 5603 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/kernel/arm64/frames.om: Pure-Omega 121.69+ GiB unified LPDDR5x interval frame allocator
3note + pre-carved Normal-NC DMA pool allocator (ported from aienux-macbook@5e0c797:mm/frames.c).
4note Ingests ONLY EfiConventionalMemory (type 7) and reserves all non-conventional regions,
5note image, UEFI memory map, ACPI tables, Normal-NC DMA pool, and GB10 IORT RMR carveouts.
6
7const FR_PAGE ulong 4096
8const FR_MAX_RANGES ulong 256
9const FR_EFI_CONVENTIONAL ulong 7
10const FR_PA_40BIT_LIMIT ulong 0x0000010000000000
11
12note fr_meta: 0=count, 1=dropped, 2=total_pages_ingested, 3=nc_base, 4=nc_cur, 5=nc_end
13global fr_meta ulong 8
14global fr_base ulong 256
15global fr_end ulong 256
16
17fn fr_align_up v ulong a ulong to ulong
18if eq a 0
19ret v
20end
21m = sub a 1
22if gt m sub 0xffffffffffffffff v
23ret 0xffffffffffffffff
24end
25ret and add v m xor 0xffffffffffffffff m
26end
27
28fn fr_align_down v ulong a ulong to ulong
29if eq a 0
30ret v
31end
32m = sub a 1
33ret and v xor 0xffffffffffffffff m
34end
35
36fn frames_init to void
37for i ulong 8
38put fr_meta i 0
39end
40for j ulong 256
41put fr_base j 0
42put fr_end j 0
43end
44end
45
46fn fr_insert_at idx ulong b ulong e ulong to int
47n = get fr_meta 0
48if ge n FR_MAX_RANGES
49put fr_meta 1 add get fr_meta 1 1
50ret -1
51end
52j = n
53while gt j idx bound 256
54put fr_base j get fr_base sub j 1
55put fr_end j get fr_end sub j 1
56j = sub j 1
57end
58put fr_base idx b
59put fr_end idx e
60put fr_meta 0 add n 1
61ret 0
62end
63
64fn fr_remove_at idx ulong to void
65n = get fr_meta 0
66j = idx
67while lt add j 1 n bound 256
68put fr_base j get fr_base add j 1
69put fr_end j get fr_end add j 1
70j = add j 1
71end
72put fr_meta 0 sub n 1
73end
74
75fn frames_reserve base ulong end_pa ulong to void
76b = fr_align_down base FR_PAGE
77e = fr_align_up end_pa FR_PAGE
78if le e b
79ret
80end
81i = ulong 0
82while lt i get fr_meta 0 bound 512
83rb = get fr_base i
84re = get fr_end i
85if or le re b ge rb e
86i = add i 1
87else
88if and ge rb b le re e
89fr_remove_at i
90else
91if and lt rb b gt re e
92put fr_end i b
93rc = fr_insert_at add i 1 e re
94if ne rc 0
95ret
96end
97i = add i 2
98else
99if lt rb b
100put fr_end i b
101else
102put fr_base i e
103end
104i = add i 1
105end
106end
107end
108end
109end
110
111fn frames_add base ulong end_pa ulong to int
112b = fr_align_up base FR_PAGE
113e = fr_align_down end_pa FR_PAGE
114if le e b
115ret 0
116end
117frames_reserve b e
118n = get fr_meta 0
119i = ulong 0
120while lt i n bound 256
121if ge get fr_base i b
122break
123end
124i = add i 1
125end
126if gt i 0
127if eq get fr_end sub i 1 b
128put fr_end sub i 1 e
129if lt i n
130if eq get fr_base i e
131put fr_end sub i 1 get fr_end i
132fr_remove_at i
133end
134end
135ret 0
136end
137end
138if lt i n
139if eq get fr_base i e
140put fr_base i b
141ret 0
142end
143end
144ret fr_insert_at i b e
145end
146
147note Allocate pages * 4 KiB aligned to align (power of 2, >= 4096) with end <= max_pa (0 = no cap).
148note Returns physical base address, or 0 on failure (never returns page 0).
149fn frames_alloc pages ulong align ulong max_pa ulong to ulong
150if or eq pages 0 gt pages 0x000fffffffffffff
151ret 0
152end
153a = align
154if lt a FR_PAGE
155a = FR_PAGE
156end
157bytes = mul pages FR_PAGE
158n = get fr_meta 0
159i = ulong 0
160while lt i n bound 256
161rb = get fr_base i
162re = get fr_end i
163cand = fr_align_up rb a
164if eq cand 0
165cand = a
166end
167if and ne cand 0xffffffffffffffff lt cand re
168if le bytes sub re cand
169cend = add cand bytes
170ok_cap = true
171if ne max_pa 0
172if gt cend max_pa
173ok_cap = false
174end
175end
176if ok_cap
177frames_reserve cand cend
178ret cand
179end
180end
181end
182i = add i 1
183end
184ret 0
185end
186
187fn frames_free_bytes to ulong
188total = ulong 0
189n = get fr_meta 0
190i = ulong 0
191while lt i n bound 256
192total = add total sub get fr_end i get fr_base i
193i = add i 1
194end
195ret total
196end
197
198fn frames_region_count to ulong
199ret get fr_meta 0
200end
201
202note Pre-carved Normal-NC DMA pool (registered before SCTLR_EL1.M=1).
203fn dma_pool_init base ulong length ulong to void
204put fr_meta 3 base
205put fr_meta 4 base
206put fr_meta 5 add base length
207end
208
209fn dma_pool_base to ulong
210ret get fr_meta 3
211end
212
213fn dma_pool_size to ulong
214ret sub get fr_meta 5 get fr_meta 3
215end
216
217note Allocate from the pre-carved Normal-NC DMA pool (aligned to align, >= 4096). Returns PA or 0.
218fn dma_alloc_nc pages ulong align ulong to ulong
219if eq pages 0
220ret 0
221end
222a = align
223if lt a FR_PAGE
224a = FR_PAGE
225end
226cur = fr_align_up get fr_meta 4 a
227bytes = mul pages FR_PAGE
228limit = get fr_meta 5
229if or eq cur 0 gt cur limit
230ret 0
231end
232if gt bytes sub limit cur
233ret 0
234end
235put fr_meta 4 add cur bytes
236ret cur
237end
238
239note Ingest UEFI memory map from raw physical buffer (map_pa, map_size, desc_size).
240note Only type 7 (EfiConventionalMemory) is added; page 0 is always reserved; all non-7 types are reserved.
241fn frames_ingest_efi_raw map_pa ulong map_size ulong desc_size ulong to ulong
242if or eq map_pa 0 or lt desc_size 40 eq map_size 0
243ret 0
244end
245cnt = ulong 0
246off = ulong 0
247while le add off desc_size map_size bound 4096
248d = add map_pa off
249mtype = fwld d 4
250pstart = fwld add d 8 8
251npages = fwld add d 24 8
252if and gt npages 0 le npages 0x000fffffffffffff
253bytes = mul npages FR_PAGE
254if le bytes sub 0xffffffffffffffff pstart
255pend = add pstart bytes
256if eq mtype FR_EFI_CONVENTIONAL
257rc = frames_add pstart pend
258if eq rc 0
259cnt = add cnt npages
260end
261end
262end
263end
264off = add off desc_size
265end
266note Second pass: explicitly reserve every non-EfiConventionalMemory descriptor + page 0.
267frames_reserve 0 FR_PAGE
268off = ulong 0
269while le add off desc_size map_size bound 4096
270d = add map_pa off
271mtype = fwld d 4
272pstart = fwld add d 8 8
273npages = fwld add d 24 8
274if and gt npages 0 and le npages 0x000fffffffffffff ne mtype FR_EFI_CONVENTIONAL
275bytes = mul npages FR_PAGE
276if le bytes sub 0xffffffffffffffff pstart
277frames_reserve pstart add pstart bytes
278end
279end
280off = add off desc_size
281end
282put fr_meta 2 cnt
283ret cnt
284end