https://github.com/akkartik/mu/blob/master/060read.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 length 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 (address stream), s : (address stream) -> num-bytes-read/eax
 49     # . prolog
 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-greater-unsigned-or-equal  $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->length],
 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->length]
 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     # . epilog
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     # . prolog
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->length])
120     # . . push &s->data[s->length]
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     # . epilog
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     # . prolog
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-greater-or-equal-unsigned  $_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-greater-or-equal-unsigned  $_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     # . epilog
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 
195 # idea: a clear-if-empty method on streams that clears only if f->read == f->write
196 # Unclear how I'd use it, though. Callers seem to need the check anyway.
197 # Maybe a better helper would be 'empty-stream?'
198 
199 _read:  # fd : int, s : (address stream) -> num-bytes-read/eax
200     # . prolog
201     55/push-ebp
202     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
203     # . save registers
204     51/push-ecx
205     52/push-edx
206     53/push-ebx
207     56/push-esi
208     # esi = s
209     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   0xc/disp8       .                 # copy *(ebp+12) to esi
210     # eax = s->write
211     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy *esi to eax
212     # edx = s->length
213     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           2/r32/edx   8/disp8         .                 # copy *(esi+8) to edx
214     # syscall(read, fd, &s->data[s->write], s->length - s->write)
215     # . . fd : ebx
216     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           3/r32/ebx   8/disp8         .                 # copy *(ebp+8) to ebx
217     # . . data : ecx = &s->data[s->write]
218     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
219     # . . size : edx = s->length - s->write
220     29/subtract                     3/mod/direct    2/rm32/edx    .           .             .           0/r32/eax   .               .                 # subtract eax from edx
221     # . . syscall
222     b8/copy-to-eax  3/imm32/read
223     cd/syscall  0x80/imm8
224     # add the result eax to s->write
225     01/add                          0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # add eax to *esi
226 $_read:end:
227     # . restore registers
228     5e/pop-to-esi
229     5b/pop-to-ebx
230     5a/pop-to-edx
231     59/pop-to-ecx
232     # . epilog
233     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
234     5d/pop-to-ebp
235     c3/return
236 
237     # Two options:
238     #   1 (what we have above):
239     #     ecx = s
240     #     eax = s->write
241     #     edx = s->length
242     #     # syscall
243     #     ecx = lea ecx+eax+12
244     #     edx = sub edx eax
245     #
246     #   2:
247     #     ecx = s
248     #     edx = s->length
249     #     ecx = &s->data
250     #     # syscall
251     #     ecx = add ecx, s->write
252     #     edx = sub edx, s->write
253     #
254     # Not much to choose between the two? Option 2 performs a duplicate load to
255     # use one less register, but doesn't increase the amount of spilling (ecx
256     # and edx must be used, and eax must be clobbered anyway).
257 
258 # - tests
259 
260 test-read-single:
261     # - write a single character into _test-stream, then read from it
262     # clear-stream(_test-stream)
263     # . . push args
264     68/push  _test-stream/imm32
265     # . . call
266     e8/call  clear-stream/disp32
267     # . . discard args
268     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
269     # clear-stream(_test-tmp-stream)
270     # . . push args
271     68/push  _test-tmp-stream/imm32
272     # . . call
273     e8/call  clear-stream/disp32
274     # . . discard args
275     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
276     # write(_test-stream, "Ab")
277     # . . push args
278     68/push  "Ab"/imm32
279     68/push  _test-stream/imm32
280     # . . call
281     e8/call  write/disp32
282     # . . discard args
283     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
284     # eax = read(_test-stream, _test-tmp-stream)
285     # . . push args
286     68/push  _test-tmp-stream/imm32
287     68/push  _test-stream/imm32
288     # . . call
289     e8/call  read/disp32
290     # . . discard args
291     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
292     # check-ints-equal(eax, 2, msg)
293     # . . push args
294     68/push  "F - test-read-single: return value"/imm32
295     68/push  2/imm32
296     50/push-eax
297     # . . call
298     e8/call  check-ints-equal/disp32
299     # . . discard args
300     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
301     # check-stream-equal(_test-tmp-stream, "Ab", msg)
302     # . . push args
303     68/push  "F - test-read-single"/imm32
304     68/push  "Ab"/imm32
305     68/push  _test-tmp-stream/imm32
306     # . . call
307     e8/call  check-stream-equal/disp32
308     # . . discard args
309     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
310     # end
311     c3/return
312 
313 test-read-is-stateful:
314     # - make two consecutive reads, check that their results are appended
315     # clear-stream(_test-stream)
316     # . . push args
317     68/push  _test-stream/imm32
318     # . . call
319     e8/call  clear-stream/disp32
320     # . . discard args
321     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
322     # clear-stream(_test-tmp-stream)
323     # . . push args
324     68/push  _test-tmp-stream/imm32
325     # . . call
326     e8/call  clear-stream/disp32
327     # . . discard args
328     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
329     # write(_test-stream, "C")
330     # . . push args
331     68/push  "C"/imm32
332     68/push  _test-stream/imm32
333     # . . call
334     e8/call  write/disp32
335     # . . discard args
336     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
337     # read(_test-stream, _test-tmp-stream)
338     # . . push args
339     68/push  _test-tmp-stream/imm32
340     68/push  _test-stream/imm32
341     # . . call
342     e8/call  read/disp32
343     # . . discard args
344     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
345     # write(_test-stream, "D")
346     # . . push args
347     68/push  "D"/imm32
348     68/push  _test-stream/imm32
349     # . . call
350     e8/call  write/disp32
351     # . . discard args
352     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
353     # read(_test-stream, _test-tmp-stream)
354     # . . push args
355     68/push  _test-tmp-stream/imm32
356     68/push  _test-stream/imm32
357     # . . call
358     e8/call  read/disp32
359     # . . discard args
360     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
361     # check-stream-equal(_test-tmp-stream, "CD", msg)
362     # . . push args
363     68/push  "F - test-read-is-stateful"/imm32
364     68/push  "CD"/imm32
365     68/push  _test-tmp-stream/imm32
366     # . . call
367     e8/call  check-stream-equal/disp32
368     # . . discard args
369     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
370     # end
371     c3/return
372 
373 test-read-returns-0-on-end-of-file:
374     # - read after hitting end-of-file, check that result is 0
375     # setup
376     # . clear-stream(_test-stream)
377     # . . push args
378     68/push  _test-stream/imm32
379     # . . call
380     e8/call  clear-stream/disp32
381     # . . discard args
382     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
383     # . clear-stream(_test-tmp-stream)
384     # . . push args
385     68/push  _test-tmp-stream/imm32
386     # . . call
387     e8/call  clear-stream/disp32
388     # . . discard args
389     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
390     # . write(_test-stream, "Ab")
391     # . . push args
392     68/push  "Ab"/imm32
393     68/push  _test-stream/imm32
394     # . . call
395     e8/call  write/disp32
396     # . . discard args
397     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
398     # first read gets to end-of-file
399     # . read(_test-stream, _test-tmp-stream)
400     # . . push args
401     68/push  _test-tmp-stream/imm32
402     68/push  _test-stream/imm32
403     # . . call
404     e8/call  read/disp32
405     # . . discard args
406     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
407     # second read
408     # . read(_test-stream, _test-tmp-stream)
409     # . . push args
410     68/push  _test-tmp-stream/imm32
411     68/push  _test-stream/imm32
412     # . . call
413     e8/call  read/disp32
414     # . . discard args
415     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
416     # check-ints-equal(eax, 0, msg)
417     # . . push args
418     68/push  "F - test-read-returns-0-on-end-of-file"/imm32
419     68/push  0/imm32
420     50/push-eax
421     # . . call
422     e8/call  check-ints-equal/disp32
423     # . . discard args
424     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
425     # end
426     c3/return
427 
428 == data
429 
430 _test-tmp-stream:
431     # current write index
432     0/imm32
433     # current read index
434     0/imm32
435     # length
436     8/imm32
437     # data
438     00 00 00 00 00 00 00 00  # 8 bytes
439 
440 # . . vim:nowrap:textwidth=0