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. */
22 #include <hurd/resource.h>
23 #include <cthreads.h> /* For `struct mutex'. */
26 /* Initial maximum size of the data segment (this is arbitrary). */
27 #define DATA_SIZE (128 * 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. */
46 DEFUN(__brk
, (inaddr
), PTR inaddr
)
50 __mutex_lock (&_hurd_brk_lock
);
51 ret
= _hurd_set_brk ((vm_address_t
) inaddr
);
52 __mutex_unlock (&_hurd_brk_lock
);
56 weak_alias (__brk
, brk
)
60 _hurd_set_brk (vm_address_t addr
)
63 vm_address_t pagend
= round_page (addr
);
64 vm_address_t pagebrk
= round_page (_hurd_brk
);
67 if (pagend
<= pagebrk
)
70 /* Make that memory inaccessible. */
71 __vm_protect (__mach_task_self (), pagend
, pagebrk
- pagend
,
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. */
88 if (pagend
> _hurd_data_end
)
90 /* We didn't allocate enough space! Hopefully we can get some more! */
91 err
= __vm_allocate (__mach_task_self (), &pagebrk
, pagend
- pagebrk
, 0);
93 _hurd_data_end
= pagend
;
96 /* Make the memory accessible. */
97 err
= __vm_protect (__mach_task_self (), pagebrk
, pagend
- pagebrk
,
98 0, VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
101 return __hurd_fail (err
);
112 __mutex_init (&_hurd_brk_lock
);
114 /* If _hurd_brk is already set, don't change it. The assumption is that
115 it was set in a previous run before something like Emacs's unexec was
116 called and dumped all the data up to the break at that point. */
118 _hurd_brk
= (vm_address_t
) &_end
;
120 pagend
= round_page (_hurd_brk
);
122 _hurd_data_end
= round_page ((vm_address_t
) &__data_start
+ DATA_SIZE
);
124 if (pagend
< _hurd_data_end
)
126 /* We use vm_map to allocate and change permissions atomically. */
127 if (__vm_map (__mach_task_self (), &pagend
, _hurd_data_end
- pagend
,
128 0, 0, MACH_PORT_NULL
, 0, 0,
129 0, VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
,
131 /* Couldn't allocate the memory. The break will be very short. */
132 _hurd_data_end
= pagend
;
135 (void) &init_brk
; /* Avoid ``defined but not used'' warning. */
137 text_set_element (_hurd_preinit_hook
, init_brk
);