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