https://github.com/akkartik/mu/blob/main/linux/111read.subx
  1 # read: analogously to write, support reading from in-memory streams in
  2 # addition to file descriptors.
  3 #
  4 # We can pass it either a file descriptor or an address to a stream. If a
  5 # file descriptor is passed in, we _read from it using the right syscall. If a
  6 # stream is passed in (a fake file descriptor), we read from it instead. This
  7 # lets us initialize input for tests.
  8 #
  9 # A little counter-intuitively, the output of 'read' ends up in.. a stream. So
 10 # tests end up doing a redundant copy. Why? Well, consider the alternatives:
 11 #
 12 #   a) Reading into a string, and returning a pointer to the end of the read
 13 #   region, or a count of bytes written. Now this count or end pointer must be
 14 #   managed separately by the caller, which can be error-prone.
 15 #
 16 #   b) Having 'read' return a buffer that it allocates. But there's no way to
 17 #   know in advance how large to make the buffer. If you read less than the
 18 #   size of the buffer you again end up needing to manage initialized vs
 19 #   uninitialized memory.
 20 #
 21 #   c) Creating more helpful variants like 'read-byte' or 'read-until' which
 22 #   also can take a file descriptor or stream, just like 'write'. But such
 23 #   primitives don't exist in the Linux kernel, so we'd be implementing them
 24 #   somehow, either with more internal buffering or by making multiple
 25 #   syscalls.
 26 #
 27 # Reading into a stream avoids these problems. The buffer is externally
 28 # provided and the caller has control over where it's allocated, its lifetime,
 29 # and so on. The buffer's read and write pointers are internal to it so it's
 30 # easier to keep in a consistent state. And it can now be passed directly to
 31 # helpers like 'read-byte' or 'read-until' that only need to support streams,
 32 # never file descriptors.
 33 #
 34 # Like with 'write', we assume our data segment will never begin at an address
 35 # shorter than 0x08000000, so any smaller arguments are assumed to be real
 36 # file descriptors.
 37 #
 38 # As a reminder, a stream looks like this:
 39 #   write: int  # index at which to write to next
 40 #   read: int  # index at which to read next
 41 #   data: (array byte)  # prefixed by size as usual
 42 
 43 == code
 44 #   instruction                     effective address                                                   register    displacement    immediate
 45 # . op          subop               mod             rm32          base        index         scale       r32
 46 # . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes
 47 
 48 read:  # f: fd or (addr stream byte), s: (addr stream byte) -> num-bytes-read/eax: int
 49     # . prologue
 50     55/push-ebp
 51     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 52     # if (f < 0x08000000) return _read(f, s)  # f can't be a user-mode address, so treat it as a kernel file descriptor
 53     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         0x08000000/imm32  # compare *(ebp+8)
 54     73/jump-if-addr>=  $read:fake/disp8
 55     # . . push args
 56     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
 57     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
 58     # . . call
 59     e8/call  _read/disp32
 60     # . . discard args
 61     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 62     # return
 63     eb/jump  $read:end/disp8
 64 $read:fake:
 65     # otherwise, treat 'f' as a stream to scan from
 66     # . save registers
 67     56/push-esi
 68     57/push-edi
 69     # esi = f
 70     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
 71     # edi = s
 72     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   0xc/disp8       .                 # copy *(ebp+12) to esi
 73     # eax = _buffer-4(out = &s->data[s->write], outend = &s->data[s->size],
 74     #                 in  = &f->data[f->read],  inend  = &f->data[f->write])
 75     # . . push &f->data[f->write]
 76     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy *esi to eax
 77     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  0/index/eax   .           0/r32/eax   0xc/disp8       .                 # copy esi+eax+12 to eax
 78     50/push-eax
 79     # . . push &f->data[f->read]
 80     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   4/disp8         .                 # copy *(esi+4) to eax
 81     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  0/index/eax   .           0/r32/eax   0xc/disp8       .                 # copy esi+eax+12 to eax
 82     50/push-eax
 83     # . . push &s->data[s->size]
 84     8b/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           0/r32/eax   8/disp8         .                 # copy *(edi+8) to eax
 85     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/edi  0/index/eax   .           0/r32/eax   0xc/disp8       .                 # copy edi+eax+12 to eax
 86     50/push-eax
 87     # . . push &s->data[s->write]
 88     8b/copy                         0/mod/indirect  7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy *edi to eax
 89     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/edi  0/index/eax   .           0/r32/eax   0xc/disp8       .                 # copy edi+eax+12 to eax
 90     50/push-eax
 91     # . . call
 92     e8/call  _buffer-4/disp32
 93     # . . discard args
 94     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
 95     # s->write += eax
 96     01/add                          0/mod/indirect  7/rm32/edi    .           .             .           0/r32/eax   .               .                 # add eax to *edi
 97     # f->read += eax
 98     01/add                          1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   4/disp8         .                 # add eax to *(esi+4)
 99     # . restore registers
100     5f/pop-to-edi
101     5e/pop-to-esi
102 $read:end:
103     # . epilogue
104     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
105     5d/pop-to-ebp
106     c3/return
107 
108 # - helpers
109 
110 # '_buffer' is like '_append', but silently stops instead of aborting when it runs out of space
111 
112 # 3-argument variant of _buffer
113 _buffer-3:  # out: address, outend: address, s: (array byte) -> num_bytes_buffered/eax
114     # . prologue
115     55/push-ebp
116     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
117     # . save registers
118     51/push-ecx
119     # eax = _buffer-4(out, outend, &s->data[0], &s->data[s->size])
120     # . . push &s->data[s->size]
121     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         0/r32/eax   0x10/disp8      .                 # copy *(ebp+16) to eax
122     8b/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy *eax to ecx
123     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    0/base/eax  1/index/ecx   .           1/r32/ecx   4/disp8         .                 # copy eax+ecx+4 to ecx
124     51/push-ecx
125     # . . push &s->data[0]
126     8d/copy-address                 1/mod/*+disp8   0/rm32/eax    .           .             .           1/r32/ecx   4/disp8         .                 # copy eax+4 to ecx
127     51/push-ecx
128     # . . push outend
129     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
130     # . . push out
131     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
132     # . . call
133     e8/call  _buffer-4/disp32
134     # . . discard args
135     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
136 $_buffer-3:end:
137     # . restore registers
138     59/pop-to-ecx
139     # . epilogue
140     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
141     5d/pop-to-ebp
142     c3/return
143 
144 # 4-argument variant of _buffer
145 _buffer-4:  # out: address, outend: address, in: address, inend: address -> num_bytes_buffered/eax
146     # . prologue
147     55/push-ebp
148     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
149     # . save registers
150     51/push-ecx
151     52/push-edx
152     53/push-ebx
153     56/push-esi
154     57/push-edi
155     # eax/num_bytes_buffered = 0
156     b8/copy-to-eax  0/imm32
157     # edi = out
158     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   8/disp8         .                 # copy *(ebp+8) to edi
159     # edx = outend
160     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           2/r32/edx   0xc/disp8       .                 # copy *(ebp+12) to edx
161     # esi = in
162     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   0x10/disp8      .                 # copy *(ebp+16) to esi
163     # ecx = inend
164     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   0x14/disp8      .                 # copy *(ebp+20) to ecx
165 $_buffer-4:loop:
166     # if (in >= inend) break
167     39/compare                      3/mod/direct    6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # compare esi with ecx
168     73/jump-if-addr>=  $_buffer-4:end/disp8
169     # if (out >= outend) break  # for now silently ignore filled up buffer
170     39/compare                      3/mod/direct    7/rm32/edi    .           .             .           2/r32/edx   .               .                 # compare edi with edx
171     73/jump-if-addr>=  $_buffer-4:end/disp8
172     # *out = *in
173     8a/copy-byte                    0/mod/indirect  6/rm32/esi    .           .             .           3/r32/BL    .               .                 # copy byte at *esi to BL
174     88/copy-byte                    0/mod/indirect  7/rm32/edi    .           .             .           3/r32/BL    .               .                 # copy byte at BL to *edi
175     # ++num_bytes_buffered
176     40/increment-eax
177     # ++in
178     46/increment-esi
179     # ++out
180     47/increment-edi
181     eb/jump  $_buffer-4:loop/disp8
182 $_buffer-4:end:
183     # . restore registers
184     5f/pop-to-edi
185     5e/pop-to-esi
186     5b/pop-to-ebx
187     5a/pop-to-edx
188     59/pop-to-ecx
189     # . epilogue
190     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
191     5d/pop-to-ebp
192     c3/return
193 
194 # idea: a clear-if-empty method on streams that clears only if f->read == f->write
195 # Unclear how I'd use it, though. Callers seem to need the check anyway.
196 # Maybe a better helper would be 'empty-stream?'
197 
198 _read:  # fd: int, s: (addr stream byte) -> num-bytes-read/eax: int
199     # . prologue
200     55/push-ebp
201     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
202     # . save registers
203     51/push-ecx
204     52/push-edx
205     53/push-ebx
206     56/push-esi
207     # esi = s
208     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   0xc/disp8       .                 # copy *(ebp+12) to esi
209     # eax = s->write
210     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy *esi to eax
211     # edx = s->size
212     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           2/r32/edx   8/disp8         .                 # copy *(esi+8) to edx
213     # syscall(read, fd, &s->data[s->write], s->size - s->write)
214     # . . fd: ebx
215     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           3/r32/ebx   8/disp8         .                 # copy *(ebp+8) to ebx
216     # . . data: ecx = &s->data[s->write]
217     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  0/index/eax   .           1/r32/ecx   0xc/disp8       .                 # copy esi+eax+12 to ecx
218     # . . size: edx = s->size - s->write
219     29/subtract                     3/mod/direct    2/rm32/edx    .           .             .           0/r32/eax   .               .                 # subtract eax from edx
220     # . . syscall
221     e8/call  syscall_read/disp32
222     # add the result eax to s->write
223     01/add                          0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # add eax to *esi
224 $_read:end:
225     # . restore registers
226     5e/pop-to-esi
227     5b/pop-to-ebx
228     5a/pop-to-edx
229     59/pop-to-ecx
230     # . epilogue
231     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
232     5d/pop-to-ebp
233     c3/return
234 
235     # Two options:
236     #   1 (what we have above):
237     #     ecx = s
238     #     eax = s->write
239     #     edx = s->size
240     #     # syscall
241     #     ecx = lea ecx+eax+12
242     #     edx = sub edx eax
243     #
244     #   2:
245     #     ecx = s
246     #     edx = s->size
247     #     ecx = &s->data
248     #     # syscall
249     #     ecx = add ecx, s->write
250     #     edx = sub edx, s->write
251     #
252     # Not much to choose between the two? Option 2 performs a duplicate load to
253     # use one less register, but doesn't increase the amount of spilling (ecx
254     # and edx must be used, and eax must be clobbered anyway).
255 
256 # - tests
257 
258 test-read-single:
259     # - write a single character into _test-stream, then read from it
260     # clear-stream(_test-stream)
261     # . . push args
262     68/push  _test-stream/imm32
263     # . . call
264     e8/call  clear-stream/disp32
265     # . . discard args
266     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
267     # clear-stream(_test-tmp-stream)
268     # . . push args
269     68/push  _test-tmp-stream/imm32
270     # . . call
271     e8/call  clear-stream/disp32
272     # . . discard args
273     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
274     # write(_test-stream, "Ab")
275     # . . push args
276     68/push  "Ab"/imm32
277     68/push  _test-stream/imm32
278     # . . call
279     e8/call  write/disp32
280     # . . discard args
281     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
282     # eax = read(_test-stream, _test-tmp-stream)
283     # . . push args
284     68/push  _test-tmp-stream/imm32
285     68/push  _test-stream/imm32
286     # . . call
287     e8/call  read/disp32
288     # . . discard args
289     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
290     # check-ints-equal(eax, 2, msg)
291     # . . push args
292     68/push  "F - test-read-single: return value"/imm32
293     68/push  2/imm32
294     50/push-eax
295     # . . call
296     e8/call  check-ints-equal/disp32
297     # . . discard args
298     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
299     # check-stream-equal(_test-tmp-stream, "Ab", msg)
300     # . . push args
301     68/push  "F - test-read-single"/imm32
302     68/push  "Ab"/imm32
303     68/push  _test-tmp-stream/imm32
304     # . . call
305     e8/call  check-stream-equal/disp32
306     # . . discard args
307     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
308     # end
309     c3/return
310 
311 test-read-is-stateful:
312     # - make two consecutive reads, check that their results are appended
313     # clear-stream(_test-stream)
314     # . . push args
315     68/push  _test-stream/imm32
316     # . . call
317     e8/call  clear-stream/disp32
318     # . . discard args
319     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
320     # clear-stream(_test-tmp-stream)
321     # . . push args
322     68/push  _test-tmp-stream/imm32
323     # . . call
324     e8/call  clear-stream/disp32
325     # . . discard args
326     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
327     # write(_test-stream, "C")
328     # . . push args
329     68/push  "C"/imm32
330     68/push  _test-stream/imm32
331     # . . call
332     e8/call  write/disp32
333     # . . discard args
334     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
335     # read(_test-stream, _test-tmp-stream)
336     # . . push args
337     68/push  _test-tmp-stream/imm32
338     68/push  _test-stream/imm32
339     # . . call
340     e8/call  read/disp32
341     # . . discard args
342     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
343     # write(_test-stream, "D")
344     # . . push args
345     68/push  "D"/imm32
346     68/push  _test-stream/imm32
347     # . . call
348     e8/call  write/disp32
349     # . . discard args
350     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
351     # read(_test-stream, _test-tmp-stream)
352     # . . push args
353     68/push  _test-tmp-stream/imm32
354     68/push  _test-stream/imm32
355     # . . call
356     e8/call  read/disp32
357     # . . discard args
358     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
359     # check-stream-equal(_test-tmp-stream, "CD", msg)
360     # . . push args
361     68/push  "F - test-read-is-stateful"/imm32
362     68/push  "CD"/imm32
363     68/push  _test-tmp-stream/imm32
364     # . . call
365     e8/call  check-stream-equal/disp32
366     # . . discard args
367     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
368     # end
369     c3/return
370 
371 test-read-returns-0-on-end-of-file:
372     # - read after hitting end-of-file, check that result is 0
373     # setup
374     # . clear-stream(_test-stream)
375     # . . push args
376     68/push  _test-stream/imm32
377     # . . call
378     e8/call  clear-stream/disp32
379     # . . discard args
380     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
381     # . clear-stream(_test-tmp-stream)
382     # . . push args
383     68/push  _test-tmp-stream/imm32
384     # . . call
385     e8/call  clear-stream/disp32
386     # . . discard args
387     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
388     # . write(_test-stream, "Ab")
389     # . . push args
390     68/push  "Ab"/imm32
391     68/push  _test-stream/imm32
392     # . . call
393     e8/call  write/disp32
394     # . . discard args
395     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
396     # first read gets to end-of-file
397     # . read(_test-stream, _test-tmp-stream)
398     # . . push args
399     68/push  _test-tmp-stream/imm32
400     68/push  _test-stream/imm32
401     # . . call
402     e8/call  read/disp32
403     # . . discard args
404     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
405     # second read
406     # . read(_test-stream, _test-tmp-stream)
407     # . . push args
408     68/push  _test-tmp-stream/imm32
409     68/push  _test-stream/imm32
410     # . . call
411     e8/call  read/disp32
412     # . . discard args
413     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
414     # check-ints-equal(eax, 0, msg)
415     # . . push args
416     68/push  "F - test-read-returns-0-on-end-of-file"/imm32
417     68/push  0/imm32
418     50/push-eax
419     # . . call
420     e8/call  check-ints-equal/disp32
421     # . . discard args
422     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
423     # end
424     c3/return
425 
426 == data
427 
428 _test-tmp-stream:  # (stream byte)
429     # current write index
430     0/imm32
431     # current read index
432     0/imm32
433     # size
434     8/imm32
435     # data
436     00 00 00 00 00 00 00 00  # 8 bytes
437 
438 # . . vim:nowrap:textwidth=0