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 / disk / disk.om

275 lines · 5559 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/disk/disk.om: Pure-Omega AIENOS Block-Device Interface and Bounded Request Queue.
3note Converted from upstream aienux native/disk/disk.{h,c}.
4note Compose with: si/directives/om2.om aienux/disk/disk.om aienux/disk/disk_file.om aienux/disk/disk_sdhci.om <test.om>
5
6struct DiskDev backend uint block_size uint block_count ulong max_blocks uint end
7
8const DISK_SECTOR_BYTES uint 512
9const DISK_QUEUE_DEPTH ulong 32
10
11const DISK_OK int 0
12const DISK_EIO int -1
13const DISK_ERANGE int -2
14const DISK_EARG int -3
15const DISK_EGEOMETRY int -4
16const DISK_EFULL int -5
17const DISK_EPOWER int -6
18const DISK_ESTATE int -7
19
20const DISK_OP_READ uint 1
21const DISK_OP_WRITE uint 2
22const DISK_OP_FLUSH uint 3
23
24const DISK_BACKEND_NONE uint 0
25const DISK_BACKEND_FILE uint 1
26const DISK_BACKEND_SDHCI uint 2
27
28note Bounded request queue state (depth 32)
29global dq_op uint 32
30global dq_lba ulong 32
31global dq_cnt uint 32
32global dq_off ulong 32
33global dq_status int 32
34note dq_meta: 0=head, 1=len, 2=failed, 3=dev_set
35global dq_meta ulong 4
36
37fn disk_check d view DiskDev to int
38bk = get d backend
39if and ne bk DISK_BACKEND_FILE ne bk DISK_BACKEND_SDHCI
40ret DISK_EARG
41end
42bsz = get d block_size
43if and ne bsz 512 ne bsz 4096
44ret DISK_EGEOMETRY
45end
46if or eq get d block_count 0 eq get d max_blocks 0
47ret DISK_EGEOMETRY
48end
49ret DISK_OK
50end
51
52fn disk_range_ok d view DiskDev lba ulong cnt uint to int
53if eq cnt 0
54ret DISK_EARG
55end
56bcnt = get d block_count
57if ge lba bcnt
58ret DISK_ERANGE
59end
60if gt ulong cnt sub bcnt lba
61ret DISK_ERANGE
62end
63ret DISK_OK
64end
65
66fn disk_backend_rc brc int to int
67if or eq brc DISK_EPOWER eq brc DISK_ESTATE
68ret brc
69end
70ret DISK_EIO
71end
72
73fn disk_dispatch_read bk uint lba ulong n uint buf edit uchar * off ulong to int
74if eq bk DISK_BACKEND_FILE
75ret disk_file_read lba n buf off
76end
77if eq bk DISK_BACKEND_SDHCI
78ret sdhci_read lba n buf off
79end
80ret DISK_EARG
81end
82
83fn disk_dispatch_write bk uint lba ulong n uint buf view uchar * off ulong to int
84if eq bk DISK_BACKEND_FILE
85ret disk_file_write lba n buf off
86end
87if eq bk DISK_BACKEND_SDHCI
88ret sdhci_write lba n buf off
89end
90ret DISK_EARG
91end
92
93fn disk_dispatch_flush bk uint to int
94if eq bk DISK_BACKEND_FILE
95ret disk_file_flush
96end
97if eq bk DISK_BACKEND_SDHCI
98ret sdhci_flush
99end
100ret DISK_EARG
101end
102
103fn disk_read_at d view DiskDev lba ulong cnt uint buf edit uchar * off ulong to int
104rc = disk_check d
105if ne rc DISK_OK
106ret rc
107end
108rc = disk_range_ok d lba cnt
109if ne rc DISK_OK
110ret rc
111end
112if gt add off mul ulong cnt ulong get d block_size len buf
113ret DISK_EARG
114end
115cur_lba = lba
116rem_cnt = cnt
117cur_off = off
118while gt rem_cnt 0 bound 65536
119n = rem_cnt
120if gt n get d max_blocks
121n = get d max_blocks
122end
123brc = disk_dispatch_read get d backend cur_lba n buf cur_off
124if ne brc DISK_OK
125ret disk_backend_rc brc
126end
127cur_lba = add cur_lba ulong n
128rem_cnt = sub rem_cnt n
129cur_off = add cur_off mul ulong n ulong get d block_size
130end
131ret DISK_OK
132end
133
134fn disk_write_at d view DiskDev lba ulong cnt uint buf view uchar * off ulong to int
135rc = disk_check d
136if ne rc DISK_OK
137ret rc
138end
139rc = disk_range_ok d lba cnt
140if ne rc DISK_OK
141ret rc
142end
143if gt add off mul ulong cnt ulong get d block_size len buf
144ret DISK_EARG
145end
146cur_lba = lba
147rem_cnt = cnt
148cur_off = off
149while gt rem_cnt 0 bound 65536
150n = rem_cnt
151if gt n get d max_blocks
152n = get d max_blocks
153end
154brc = disk_dispatch_write get d backend cur_lba n buf cur_off
155if ne brc DISK_OK
156ret disk_backend_rc brc
157end
158cur_lba = add cur_lba ulong n
159rem_cnt = sub rem_cnt n
160cur_off = add cur_off mul ulong n ulong get d block_size
161end
162ret DISK_OK
163end
164
165fn disk_read d view DiskDev lba ulong cnt uint buf edit uchar * to int
166ret disk_read_at d lba cnt buf 0
167end
168
169fn disk_write d view DiskDev lba ulong cnt uint buf view uchar * to int
170ret disk_write_at d lba cnt buf 0
171end
172
173fn disk_flush d view DiskDev to int
174rc = disk_check d
175if ne rc DISK_OK
176ret rc
177end
178brc = disk_dispatch_flush get d backend
179if ne brc DISK_OK
180ret disk_backend_rc brc
181end
182ret DISK_OK
183end
184
185fn disk_queue_init to void
186for i ulong 32
187put dq_op i 0
188put dq_lba i 0
189put dq_cnt i 0
190put dq_off i 0
191put dq_status i 0
192end
193put dq_meta 0 0
194put dq_meta 1 0
195put dq_meta 2 0
196put dq_meta 3 1
197end
198
199fn disk_queue_reset to void
200put dq_meta 0 0
201put dq_meta 1 0
202put dq_meta 2 0
203end
204
205fn disk_queue_len to ulong
206ret get dq_meta 1
207end
208
209fn disk_queue_failed to bool
210ret ne get dq_meta 2 0
211end
212
213fn disk_queue_push d view DiskDev op uint lba ulong cnt uint buf_off ulong to int
214if eq get dq_meta 3 0
215ret DISK_EARG
216end
217if ne get dq_meta 2 0
218ret DISK_ESTATE
219end
220if and ne op DISK_OP_READ and ne op DISK_OP_WRITE ne op DISK_OP_FLUSH
221ret DISK_EARG
222end
223qlen = get dq_meta 1
224if ge qlen DISK_QUEUE_DEPTH
225ret DISK_EFULL
226end
227if ne op DISK_OP_FLUSH
228rc = disk_range_ok d lba cnt
229if ne rc DISK_OK
230ret rc
231end
232end
233slot = rem add get dq_meta 0 qlen DISK_QUEUE_DEPTH
234put dq_op slot op
235put dq_lba slot lba
236put dq_cnt slot cnt
237put dq_off slot buf_off
238put dq_status slot 1
239put dq_meta 1 add qlen 1
240ret DISK_OK
241end
242
243fn disk_queue_drain d view DiskDev buf edit uchar * to int
244if eq get dq_meta 3 0
245ret DISK_EARG
246end
247if ne get dq_meta 2 0
248ret DISK_ESTATE
249end
250while gt get dq_meta 1 0 bound 64
251hd = get dq_meta 0
252op = get dq_op hd
253lba = get dq_lba hd
254cnt = get dq_cnt hd
255boff = get dq_off hd
256rc = DISK_OK
257if eq op DISK_OP_READ
258rc = disk_read_at d lba cnt buf boff
259else
260if eq op DISK_OP_WRITE
261rc = disk_write_at d lba cnt buf boff
262else
263rc = disk_flush d
264end
265end
266put dq_status hd rc
267put dq_meta 0 rem add hd 1 DISK_QUEUE_DEPTH
268put dq_meta 1 sub get dq_meta 1 1
269if ne rc DISK_OK
270put dq_meta 2 1
271ret rc
272end
273end
274ret DISK_OK
275end