initial import
[glibc.git] / sysdeps / mach / hurd / brk.c
bloba96286b2d936fdfc16bedc561c3541f36d776771
1 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <errno.h>
21 #include <hurd.h>
22 #include <hurd/resource.h>
23 #include <cthreads.h> /* For `struct mutex'. */
26 /* Initial maximum size of the data segment (32MB, which is arbitrary). */
27 #define DATA_SIZE (32 * 1024 * 1024)
30 /* Up to the page including this address is allocated from the kernel.
31 This address is the data resource limit. */
32 vm_address_t _hurd_data_end;
34 /* Up to this address is actually available to the user.
35 Pages beyond the one containing this address allow no access. */
36 vm_address_t _hurd_brk;
38 struct mutex _hurd_brk_lock;
40 extern int __data_start, _end;
43 /* Set the end of the process's data space to INADDR.
44 Return 0 if successful, -1 if not. */
45 int
46 DEFUN(__brk, (inaddr), PTR inaddr)
48 int ret;
49 HURD_CRITICAL_BEGIN;
50 __mutex_lock (&_hurd_brk_lock);
51 ret = _hurd_set_brk ((vm_address_t) inaddr);
52 __mutex_unlock (&_hurd_brk_lock);
53 HURD_CRITICAL_END;
54 return ret;
56 weak_alias (__brk, brk)
59 int
60 _hurd_set_brk (vm_address_t addr)
62 error_t err;
63 vm_address_t pagend = round_page (addr);
64 vm_address_t pagebrk = round_page (_hurd_brk);
65 long int rlimit;
67 if (pagend <= pagebrk)
69 if (pagend < pagebrk)
70 /* Make that memory inaccessible. */
71 __vm_protect (__mach_task_self (), pagend, pagebrk - pagend,
72 0, VM_PROT_NONE);
73 _hurd_brk = addr;
74 return 0;
77 __mutex_lock (&_hurd_rlimit_lock);
78 rlimit = _hurd_rlimits[RLIMIT_DATA].rlim_cur;
79 __mutex_unlock (&_hurd_rlimit_lock);
81 if (addr - (vm_address_t) &__data_start > rlimit)
83 /* Need to increase the resource limit. */
84 errno = ENOMEM;
85 return -1;
88 /* Make the memory accessible. */
89 if (err = __vm_protect (__mach_task_self (), pagebrk, pagend - pagebrk,
90 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE))
92 errno = err;
93 return -1;
96 _hurd_brk = addr;
97 return 0;
100 static void
101 init_brk (void)
103 vm_address_t pagend;
105 __mutex_init (&_hurd_brk_lock);
107 /* If _hurd_brk is already set, don't change it. The assumption is that
108 it was set in a previous run before something like Emacs's unexec was
109 called and dumped all the data up to the break at that point. */
110 if (_hurd_brk == 0)
111 _hurd_brk = (vm_address_t) &_end;
113 pagend = round_page (_hurd_brk);
115 _hurd_data_end = (vm_address_t) &__data_start + DATA_SIZE;
117 if (pagend < _hurd_data_end)
119 /* We use vm_map to allocate and change permissions atomically. */
120 if (__vm_map (__mach_task_self (), &pagend, _hurd_data_end - pagend,
121 0, 0, MACH_PORT_NULL, 0, 0,
122 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE,
123 VM_INHERIT_COPY))
124 /* Couldn't allocate the memory. The break will be very short. */
125 _hurd_data_end = pagend;
128 (void) &init_brk; /* Avoid ``defined but not used'' warning. */
130 text_set_element (_hurd_preinit_hook, init_brk);