https://github.com/akkartik/mu/blob/main/linux/104new-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: allocation-descriptor
    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     # var eax: (addr _) = 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     e8/call  syscall_exit/disp32
    39
    40 new-segment:  # len: int, ad: (addr allocation-descriptor)
    41     # . prologue
    42     55/push-ebp
    43     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    44     # . save registers
    45     50/push-eax
    46     53/push-ebx
    47     # copy len to _mmap-new-segment->len
    48     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   8/disp8         .                 # copy *(ebp+8) to eax
    49     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   $_mmap-new-segment:len/disp32     # copy eax to *$_mmap-new-segment:len
    50     # mmap(_mmap-new-segment)
    51     bb/copy-to-ebx  _mmap-new-segment/imm32
    52     e8/call  syscall_mmap/disp32
    53     # copy {eax, eax+len} to *ad
    54     # . ebx = ad
    55     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           3/r32/ebx   0xc/disp8       .                 # copy *(ebp+12) to ebx
    56     # . ad->curr = eax
    57     89/copy                         0/mod/indirect  3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to *ebx
    58     # . ad->end = eax+len
    59     03/add                          1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   8/disp8         .                 # add *(ebp+8) to eax
    60     89/copy                         1/mod/*+disp8   3/rm32/ebx    .           .             .           0/r32/eax   4/disp8         .                 # copy eax to *(ebx+4)
    61 $new-segment:end:
    62     # . restore registers
    63     5b/pop-to-ebx
    64     58/pop-to-eax
    65     # . epilogue
    66     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    67     5d/pop-to-ebp
    68     c3/return
    69
    70 == data
    71
    72 # various constants used here were found in the Linux sources (search for file mman-common.h)
    73 _mmap-new-segment:  # mmap_arg_struct
    74     # addr
    75     0/imm32
    76 $_mmap-new-segment:len:
    77     # len
    78     0/imm32
    79     # protection flags
    80     3/imm32  # PROT_READ | PROT_WRITE
    81     # sharing flags
    82     0x22/imm32  # MAP_PRIVATE | MAP_ANONYMOUS
    83     # fd
    84     -1/imm32  # since MAP_ANONYMOUS is specified
    85     # offset
    86     0/imm32  # since MAP_ANONYMOUS is specified
    87
    88 # . . vim:nowrap:textwidth=0