https://github.com/akkartik/mu/blob/master/subx/053new-segment.subx
 1 # Create a new segment (pool of memory for allocating chunks from) in the form
 2 # of an *allocation descriptor* that can be passed to the memory allocator
 3 # (defined in a later layer).
 4 #
 5 # Currently an allocation descriptor consists of just the bounds of the pool of
 6 # available memory:
 7 #
 8 #   curr : address
 9 #   end : address
10 #
11 # This isn't enough information to reclaim individual allocations. We can't
12 # support arbitrary reclamation yet.
13 
14 == code
15 #   instruction                     effective address                                                   register    displacement    immediate
16 # . op          subop               mod             rm32          base        index         scale       r32
17 # . 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
18 
19 Entry:   # manual test
20     # var ad/ECX : (address allocation-descriptor) = {0, 0}
21     68/push  0/imm32/limit
22     68/push  0/imm32/curr
23     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
24     # new-segment(0x1000, ad)
25     # . . push args
26     51/push-ECX
27     68/push  0x1000/imm32
28     # . . call
29     e8/call  new-segment/disp32
30     # . . discard args
31     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
32     # EAX = ad->curr
33     8b/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # copy *ECX to EAX
34     # write to *EAX to check that we have access to the newly-allocated segment
35     c7          0/subop/copy        0/mod/direct    0/rm32/EAX    .           .             .           .           .               0x34/imm32        # copy to *EAX
36     # syscall(exit, EAX)
37     89/copy                         3/mod/direct    3/rm32/EBX    .           .             .           0/r32/EAX   .               .                 # copy EAX to EBX
38     b8/copy-to-EAX  1/imm32/exit
39     cd/syscall  0x80/imm8
40 
41 new-segment:  # len : int, ad : (address allocation-descriptor)
42     # . prolog
43     55/push-EBP
44     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
45     # . save registers
46     50/push-EAX
47     53/push-EBX
48     # copy len to _mmap-new-segment->len
49     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
50     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   _mmap-new-segment:len/disp32      # copy EAX to *_mmap-new-segment:len
51     # mmap(_mmap-new-segment)
52     bb/copy-to-EBX  _mmap-new-segment/imm32
53     b8/copy-to-EAX  0x5a/imm32/mmap
54     cd/syscall  0x80/imm8
55     # copy {EAX, EAX+len} to *ad
56     # . EBX = ad
57     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           3/r32/EBX   0xc/disp8       .                 # copy *(EBP+12) to EBX
58     # . *EBX = EAX
59     89/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           0/r32/EAX   .               .                 # copy EAX to *EBX
60     # . *(EBX+4) = EAX+len
61     03/add                          1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   8/disp8         .                 # add *(EBP+8) to EAX
62     89/copy                         1/mod/*+disp8   3/rm32/EBX    .           .             .           0/r32/EAX   4/disp8         .                 # copy EAX to *(EBX+4)
63 $new-segment:end:
64     # . restore registers
65     5b/pop-to-EBX
66     58/pop-to-EAX
67     # . epilog
68     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
69     5d/pop-to-EBP
70     c3/return
71 
72 == data
73 
74 # various constants used here were found in the Linux sources (search for file mman-common.h)
75 _mmap-new-segment:  # type mmap_arg_struct
76     # addr
77     0/imm32
78 _mmap-new-segment:len:
79     # len
80     0/imm32
81     # protection flags
82     3/imm32  # PROT_READ | PROT_WRITE
83     # sharing flags
84     0x22/imm32  # MAP_PRIVATE | MAP_ANONYMOUS
85     # fd
86     -1/imm32  # since MAP_ANONYMOUS is specified
87     # offset
88     0/imm32  # since MAP_ANONYMOUS is specified
89 
90 # . . vim:nowrap:textwidth=0