2.9
[glibc/nacl-glibc.git] / sysdeps / mach / hurd / brk.c
blob931b260b10f1f0415537f193e19ef7946e308aa5
1 /* Copyright (C) 1991,92,93,94,95,96,97,99,2000 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <hurd.h>
21 #include <hurd/resource.h>
22 #include <cthreads.h> /* For `struct mutex'. */
25 /* Initial maximum size of the data segment (this is arbitrary). */
26 #define DATA_SIZE (128 * 1024 * 1024)
28 /* Up to the page including this address is allocated from the kernel.
29 This address is the data resource limit. */
30 vm_address_t _hurd_data_end;
32 /* Up to this address is actually available to the user.
33 Pages beyond the one containing this address allow no access. */
34 vm_address_t _hurd_brk = 0;
36 /* This name is used by the Linux crtbeginS.o for reasons you don't even
37 want to think about it. It's just easier to provide some definition for
38 it than even to explain the braindamage involved. */
39 weak_alias (_hurd_brk, ___brk_addr)
41 struct mutex _hurd_brk_lock;
43 extern int __data_start, _end;
44 weak_extern (__data_start)
45 static vm_address_t static_data_start;
48 /* Set the end of the process's data space to INADDR.
49 Return 0 if successful, -1 if not. */
50 int
51 __brk (void *inaddr)
53 int ret;
54 HURD_CRITICAL_BEGIN;
55 __mutex_lock (&_hurd_brk_lock);
56 ret = _hurd_set_brk ((vm_address_t) inaddr);
57 __mutex_unlock (&_hurd_brk_lock);
58 HURD_CRITICAL_END;
59 return ret;
61 weak_alias (__brk, brk)
64 int
65 _hurd_set_brk (vm_address_t addr)
67 error_t err;
68 vm_address_t pagend = round_page (addr);
69 vm_address_t pagebrk = round_page (_hurd_brk);
70 long int rlimit;
72 if (pagend <= pagebrk)
74 if (pagend < pagebrk)
76 /* XXX wish this were atomic... */
77 /* First deallocate the memory to release its backing space. */
78 __vm_deallocate (__mach_task_self (), pagend, pagebrk - pagend);
79 /* Now reallocate it with no access allowed. */
80 err = __vm_map (__mach_task_self (),
81 &pagend, pagebrk - pagend,
82 0, 0, MACH_PORT_NULL, 0, 0,
83 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE,
84 VM_INHERIT_COPY);
85 /* XXX what if error? */
87 _hurd_brk = addr;
88 return 0;
91 __mutex_lock (&_hurd_rlimit_lock);
92 rlimit = _hurd_rlimits[RLIMIT_DATA].rlim_cur;
93 __mutex_unlock (&_hurd_rlimit_lock);
95 if (addr - static_data_start > rlimit)
97 /* Need to increase the resource limit. */
98 errno = ENOMEM;
99 return -1;
102 if (pagend > _hurd_data_end)
104 /* We didn't allocate enough space! Hopefully we can get some more! */
105 err = __vm_allocate (__mach_task_self (), &pagebrk, pagend - pagebrk, 0);
106 if (! err)
107 _hurd_data_end = pagend;
109 else
110 /* Make the memory accessible. */
111 err = __vm_protect (__mach_task_self (), pagebrk, pagend - pagebrk,
112 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
114 if (err)
115 return __hurd_fail (err);
117 _hurd_brk = addr;
118 return 0;
121 static void
122 init_brk (void)
124 vm_address_t pagend;
126 __mutex_init (&_hurd_brk_lock);
128 static_data_start = (vm_address_t) (&__data_start ?: &_end);
130 /* If _hurd_brk is already set, don't change it. The assumption is that
131 it was set in a previous run before something like Emacs's unexec was
132 called and dumped all the data up to the break at that point. */
133 if (_hurd_brk == 0)
134 _hurd_brk = (vm_address_t) &_end;
136 pagend = round_page (_hurd_brk);
138 _hurd_data_end = round_page (static_data_start + DATA_SIZE);
140 if (pagend < _hurd_data_end)
142 /* We use vm_map to allocate and change permissions atomically. */
143 if (__vm_map (__mach_task_self (), &pagend, _hurd_data_end - pagend,
144 0, 0, MACH_PORT_NULL, 0, 0,
145 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE,
146 VM_INHERIT_COPY))
147 /* Couldn't allocate the memory. The break will be very short. */
148 _hurd_data_end = pagend;
151 (void) &init_brk; /* Avoid ``defined but not used'' warning. */
153 text_set_element (_hurd_preinit_hook, init_brk);