The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_unix.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (c) 1988 University of Utah.
    5  * Copyright (c) 1991, 1993
    6  *      The Regents of the University of California.  All rights reserved.
    7  *
    8  * This code is derived from software contributed to Berkeley by
    9  * the Systems Programming Group of the University of Utah Computer
   10  * Science Department.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. Neither the name of the University nor the names of its contributors
   21  *    may be used to endorse or promote products derived from this software
   22  *    without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   34  * SUCH DAMAGE.
   35  *
   36  * from: Utah $Hdr: vm_unix.c 1.1 89/11/07$
   37  *
   38  *      @(#)vm_unix.c   8.1 (Berkeley) 6/11/93
   39  */
   40 
   41 /*
   42  * Traditional sbrk/grow interface to VM
   43  */
   44 
   45 #include <sys/cdefs.h>
   46 __FBSDID("$FreeBSD$");
   47 
   48 #include <sys/param.h>
   49 #include <sys/lock.h>
   50 #include <sys/mutex.h>
   51 #include <sys/proc.h>
   52 #include <sys/racct.h>
   53 #include <sys/resourcevar.h>
   54 #include <sys/syscallsubr.h>
   55 #include <sys/sysent.h>
   56 #include <sys/sysproto.h>
   57 #include <sys/systm.h>
   58 
   59 #include <vm/vm.h>
   60 #include <vm/vm_param.h>
   61 #include <vm/pmap.h>
   62 #include <vm/vm_map.h>
   63 
   64 #ifndef _SYS_SYSPROTO_H_
   65 struct break_args {
   66         char *nsize;
   67 };
   68 #endif
   69 int
   70 sys_break(struct thread *td, struct break_args *uap)
   71 {
   72 #if !defined(__aarch64__) && !defined(__riscv)
   73         uintptr_t addr;
   74         int error;
   75 
   76         addr = (uintptr_t)uap->nsize;
   77         error = kern_break(td, &addr);
   78         if (error == 0)
   79                 td->td_retval[0] = addr;
   80         return (error);
   81 #else /* defined(__aarch64__) || defined(__riscv) */
   82         return (ENOSYS);
   83 #endif /* defined(__aarch64__) || defined(__riscv) */
   84 }
   85 
   86 int
   87 kern_break(struct thread *td, uintptr_t *addr)
   88 {
   89         struct vmspace *vm = td->td_proc->p_vmspace;
   90         vm_map_t map = &vm->vm_map;
   91         vm_offset_t new, old, base;
   92         rlim_t datalim, lmemlim, vmemlim;
   93         int prot, rv;
   94         int error = 0;
   95 
   96         datalim = lim_cur(td, RLIMIT_DATA);
   97         lmemlim = lim_cur(td, RLIMIT_MEMLOCK);
   98         vmemlim = lim_cur(td, RLIMIT_VMEM);
   99 
  100         new = round_page(*addr);
  101         vm_map_lock(map);
  102 
  103         base = round_page((vm_offset_t) vm->vm_daddr);
  104         old = base + ctob(vm->vm_dsize);
  105         if (new > base) {
  106                 /*
  107                  * Check the resource limit, but allow a process to reduce
  108                  * its usage, even if it remains over the limit.
  109                  */
  110                 if (new - base > datalim && new > old) {
  111                         error = ENOMEM;
  112                         goto done;
  113                 }
  114                 if (new > vm_map_max(map)) {
  115                         error = ENOMEM;
  116                         goto done;
  117                 }
  118         } else if (new < base) {
  119                 /*
  120                  * Simply return the current break address without
  121                  * modifying any state.  This is an ad-hoc interface
  122                  * used by libc to determine the initial break address,
  123                  * avoiding a dependency on magic features in the system
  124                  * linker.
  125                  */
  126                 new = old;
  127                 goto done;
  128         }
  129 
  130         if (new > old) {
  131                 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
  132                         if (ptoa(pmap_wired_count(map->pmap)) +
  133                             (new - old) > lmemlim) {
  134                                 error = ENOMEM;
  135                                 goto done;
  136                         }
  137                 }
  138                 if (map->size + (new - old) > vmemlim) {
  139                         error = ENOMEM;
  140                         goto done;
  141                 }
  142 #ifdef RACCT
  143                 if (racct_enable) {
  144                         PROC_LOCK(td->td_proc);
  145                         error = racct_set(td->td_proc, RACCT_DATA, new - base);
  146                         if (error != 0) {
  147                                 PROC_UNLOCK(td->td_proc);
  148                                 error = ENOMEM;
  149                                 goto done;
  150                         }
  151                         error = racct_set(td->td_proc, RACCT_VMEM,
  152                             map->size + (new - old));
  153                         if (error != 0) {
  154                                 racct_set_force(td->td_proc, RACCT_DATA,
  155                                     old - base);
  156                                 PROC_UNLOCK(td->td_proc);
  157                                 error = ENOMEM;
  158                                 goto done;
  159                         }
  160                         if (!old_mlock && map->flags & MAP_WIREFUTURE) {
  161                                 error = racct_set(td->td_proc, RACCT_MEMLOCK,
  162                                     ptoa(pmap_wired_count(map->pmap)) +
  163                                     (new - old));
  164                                 if (error != 0) {
  165                                         racct_set_force(td->td_proc, RACCT_DATA,
  166                                             old - base);
  167                                         racct_set_force(td->td_proc, RACCT_VMEM,
  168                                             map->size);
  169                                         PROC_UNLOCK(td->td_proc);
  170                                         error = ENOMEM;
  171                                         goto done;
  172                                 }
  173                         }
  174                         PROC_UNLOCK(td->td_proc);
  175                 }
  176 #endif
  177                 prot = VM_PROT_RW;
  178 #ifdef COMPAT_FREEBSD32
  179 #if defined(__amd64__)
  180                 if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32))
  181                         prot |= VM_PROT_EXECUTE;
  182 #endif
  183 #endif
  184                 rv = vm_map_insert(map, NULL, 0, old, new, prot, VM_PROT_ALL,
  185                     0);
  186                 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
  187                         rv = vm_map_wire_locked(map, old, new,
  188                             VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
  189                         if (rv != KERN_SUCCESS)
  190                                 (void)vm_map_delete(map, old, new);
  191                 }
  192                 if (rv != KERN_SUCCESS) {
  193 #ifdef RACCT
  194                         if (racct_enable) {
  195                                 PROC_LOCK(td->td_proc);
  196                                 racct_set_force(td->td_proc,
  197                                     RACCT_DATA, old - base);
  198                                 racct_set_force(td->td_proc,
  199                                     RACCT_VMEM, map->size);
  200                                 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
  201                                         racct_set_force(td->td_proc,
  202                                             RACCT_MEMLOCK,
  203                                             ptoa(pmap_wired_count(map->pmap)));
  204                                 }
  205                                 PROC_UNLOCK(td->td_proc);
  206                         }
  207 #endif
  208                         error = ENOMEM;
  209                         goto done;
  210                 }
  211                 vm->vm_dsize += btoc(new - old);
  212         } else if (new < old) {
  213                 rv = vm_map_delete(map, new, old);
  214                 if (rv != KERN_SUCCESS) {
  215                         error = ENOMEM;
  216                         goto done;
  217                 }
  218                 vm->vm_dsize -= btoc(old - new);
  219 #ifdef RACCT
  220                 if (racct_enable) {
  221                         PROC_LOCK(td->td_proc);
  222                         racct_set_force(td->td_proc, RACCT_DATA, new - base);
  223                         racct_set_force(td->td_proc, RACCT_VMEM, map->size);
  224                         if (!old_mlock && map->flags & MAP_WIREFUTURE) {
  225                                 racct_set_force(td->td_proc, RACCT_MEMLOCK,
  226                                     ptoa(pmap_wired_count(map->pmap)));
  227                         }
  228                         PROC_UNLOCK(td->td_proc);
  229                 }
  230 #endif
  231         }
  232 done:
  233         vm_map_unlock(map);
  234 
  235         if (error == 0)
  236                 *addr = new;
  237 
  238         return (error);
  239 }
  240 
  241 #ifdef COMPAT_FREEBSD11
  242 int
  243 freebsd11_vadvise(struct thread *td, struct freebsd11_vadvise_args *uap)
  244 {
  245 
  246         return (EINVAL);
  247 }
  248 #endif

Cache object: 95390cac6dc7cab44946eb20751afa15


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.