github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/pkg/abi/linux/mm.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package linux
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"golang.org/x/sys/unix"
    21  )
    22  
    23  // Protections for mmap(2).
    24  const (
    25  	PROT_NONE      = 0
    26  	PROT_READ      = 1 << 0
    27  	PROT_WRITE     = 1 << 1
    28  	PROT_EXEC      = 1 << 2
    29  	PROT_SEM       = 1 << 3
    30  	PROT_GROWSDOWN = 1 << 24
    31  	PROT_GROWSUP   = 1 << 25
    32  )
    33  
    34  // Flags for mmap(2).
    35  const (
    36  	MAP_SHARED     = 1 << 0
    37  	MAP_PRIVATE    = 1 << 1
    38  	MAP_FIXED      = 1 << 4
    39  	MAP_ANONYMOUS  = 1 << 5
    40  	MAP_32BIT      = 1 << 6 // arch/x86/include/uapi/asm/mman.h
    41  	MAP_GROWSDOWN  = 1 << 8
    42  	MAP_DENYWRITE  = 1 << 11
    43  	MAP_EXECUTABLE = 1 << 12
    44  	MAP_LOCKED     = 1 << 13
    45  	MAP_NORESERVE  = 1 << 14
    46  	MAP_POPULATE   = 1 << 15
    47  	MAP_NONBLOCK   = 1 << 16
    48  	MAP_STACK      = 1 << 17
    49  	MAP_HUGETLB    = 1 << 18
    50  )
    51  
    52  // Flags for mremap(2).
    53  const (
    54  	MREMAP_MAYMOVE = 1 << 0
    55  	MREMAP_FIXED   = 1 << 1
    56  )
    57  
    58  // Flags for mlock2(2).
    59  const (
    60  	MLOCK_ONFAULT = 0x01
    61  )
    62  
    63  // Flags for mlockall(2).
    64  const (
    65  	MCL_CURRENT = 1
    66  	MCL_FUTURE  = 2
    67  	MCL_ONFAULT = 4
    68  )
    69  
    70  // Advice for madvise(2).
    71  const (
    72  	MADV_NORMAL       = 0
    73  	MADV_RANDOM       = 1
    74  	MADV_SEQUENTIAL   = 2
    75  	MADV_WILLNEED     = 3
    76  	MADV_DONTNEED     = 4
    77  	MADV_REMOVE       = 9
    78  	MADV_DONTFORK     = 10
    79  	MADV_DOFORK       = 11
    80  	MADV_MERGEABLE    = 12
    81  	MADV_UNMERGEABLE  = 13
    82  	MADV_HUGEPAGE     = 14
    83  	MADV_NOHUGEPAGE   = 15
    84  	MADV_DONTDUMP     = 16
    85  	MADV_DODUMP       = 17
    86  	MADV_HWPOISON     = 100
    87  	MADV_SOFT_OFFLINE = 101
    88  	MADV_NOMAJFAULT   = 200
    89  	MADV_DONTCHGME    = 201
    90  )
    91  
    92  // Flags for msync(2).
    93  const (
    94  	MS_ASYNC      = 1 << 0
    95  	MS_INVALIDATE = 1 << 1
    96  	MS_SYNC       = 1 << 2
    97  )
    98  
    99  // NumaPolicy is the NUMA memory policy for a memory range. See numa(7).
   100  //
   101  // +marshal
   102  type NumaPolicy int32
   103  
   104  // Policies for get_mempolicy(2)/set_mempolicy(2).
   105  const (
   106  	MPOL_DEFAULT    NumaPolicy = 0
   107  	MPOL_PREFERRED  NumaPolicy = 1
   108  	MPOL_BIND       NumaPolicy = 2
   109  	MPOL_INTERLEAVE NumaPolicy = 3
   110  	MPOL_LOCAL      NumaPolicy = 4
   111  	MPOL_MAX        NumaPolicy = 5
   112  )
   113  
   114  // Flags for get_mempolicy(2).
   115  const (
   116  	MPOL_F_NODE         = 1 << 0
   117  	MPOL_F_ADDR         = 1 << 1
   118  	MPOL_F_MEMS_ALLOWED = 1 << 2
   119  )
   120  
   121  // Flags for set_mempolicy(2).
   122  const (
   123  	MPOL_F_RELATIVE_NODES = 1 << 14
   124  	MPOL_F_STATIC_NODES   = 1 << 15
   125  
   126  	MPOL_MODE_FLAGS = (MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES)
   127  )
   128  
   129  // Flags for mbind(2).
   130  const (
   131  	MPOL_MF_STRICT   = 1 << 0
   132  	MPOL_MF_MOVE     = 1 << 1
   133  	MPOL_MF_MOVE_ALL = 1 << 2
   134  
   135  	MPOL_MF_VALID = MPOL_MF_STRICT | MPOL_MF_MOVE | MPOL_MF_MOVE_ALL
   136  )
   137  
   138  // TaskSize is the address space size.
   139  var TaskSize = func() uintptr {
   140  	pageSize := uintptr(unix.Getpagesize())
   141  	for _, s := range feasibleTaskSizes {
   142  		// mmap returns ENOMEM if addr is greater than TASK_SIZE,
   143  		// otherwise it returns EINVAL, because addr isn't aligned to
   144  		// the page size.
   145  		_, _, errno := unix.RawSyscall6(
   146  			unix.SYS_MMAP,
   147  			s-pageSize-1,
   148  			512,
   149  			uintptr(unix.PROT_NONE),
   150  			uintptr(unix.MAP_ANONYMOUS|unix.MAP_PRIVATE|unix.MAP_FIXED), 0, 0)
   151  		if errno == unix.EINVAL {
   152  			return s
   153  		}
   154  		if errno != unix.ENOMEM {
   155  			panic(fmt.Sprintf("mmap returned unexpected error: %d", errno))
   156  		}
   157  	}
   158  	panic("None of the address space sizes could be successfully mmaped")
   159  }()