xref: /minix3/minix/servers/vm/break.c (revision 5f6c420586374d552d4a81aa129ada5aa46b797d)
1*433d6423SLionel Sambuc /* The MINIX model of memory allocation reserves a fixed amount of memory for
2*433d6423SLionel Sambuc  * the combined text, data, and stack segments.  The amount used for a child
3*433d6423SLionel Sambuc  * process created by FORK is the same as the parent had.  If the child does
4*433d6423SLionel Sambuc  * an EXEC later, the new size is taken from the header of the file EXEC'ed.
5*433d6423SLionel Sambuc  *
6*433d6423SLionel Sambuc  * The layout in memory consists of the text segment, followed by the data
7*433d6423SLionel Sambuc  * segment, followed by a gap (unused memory), followed by the stack segment.
8*433d6423SLionel Sambuc  * The data segment grows upward and the stack grows downward, so each can
9*433d6423SLionel Sambuc  * take memory from the gap.  If they meet, the process must be killed.  The
10*433d6423SLionel Sambuc  * procedures in this file deal with the growth of the data and stack segments.
11*433d6423SLionel Sambuc  *
12*433d6423SLionel Sambuc  * The entry points into this file are:
13*433d6423SLionel Sambuc  *   do_brk:      BRK/SBRK system calls to grow or shrink the data segment
14*433d6423SLionel Sambuc  */
15*433d6423SLionel Sambuc 
16*433d6423SLionel Sambuc #define _SYSTEM 1
17*433d6423SLionel Sambuc 
18*433d6423SLionel Sambuc #include <minix/callnr.h>
19*433d6423SLionel Sambuc #include <minix/com.h>
20*433d6423SLionel Sambuc #include <minix/config.h>
21*433d6423SLionel Sambuc #include <minix/const.h>
22*433d6423SLionel Sambuc #include <minix/ds.h>
23*433d6423SLionel Sambuc #include <minix/endpoint.h>
24*433d6423SLionel Sambuc #include <minix/minlib.h>
25*433d6423SLionel Sambuc #include <minix/type.h>
26*433d6423SLionel Sambuc #include <minix/ipc.h>
27*433d6423SLionel Sambuc #include <minix/sysutil.h>
28*433d6423SLionel Sambuc #include <minix/syslib.h>
29*433d6423SLionel Sambuc #include <minix/bitmap.h>
30*433d6423SLionel Sambuc 
31*433d6423SLionel Sambuc #include <errno.h>
32*433d6423SLionel Sambuc 
33*433d6423SLionel Sambuc #include "glo.h"
34*433d6423SLionel Sambuc #include "vm.h"
35*433d6423SLionel Sambuc #include "proto.h"
36*433d6423SLionel Sambuc #include "util.h"
37*433d6423SLionel Sambuc 
38*433d6423SLionel Sambuc #define DATA_CHANGED       1    /* flag value when data segment size changed */
39*433d6423SLionel Sambuc #define STACK_CHANGED      2    /* flag value when stack size changed */
40*433d6423SLionel Sambuc 
41*433d6423SLionel Sambuc /*===========================================================================*
42*433d6423SLionel Sambuc  *				do_brk					     *
43*433d6423SLionel Sambuc  *===========================================================================*/
do_brk(message * msg)44*433d6423SLionel Sambuc int do_brk(message *msg)
45*433d6423SLionel Sambuc {
46*433d6423SLionel Sambuc /* Perform the brk(addr) system call.
47*433d6423SLionel Sambuc  * The parameter, 'addr' is the new virtual address in D space.
48*433d6423SLionel Sambuc  */
49*433d6423SLionel Sambuc 	int proc;
50*433d6423SLionel Sambuc 
51*433d6423SLionel Sambuc 	if (vm_isokendpt(msg->m_source, &proc) != OK) {
52*433d6423SLionel Sambuc 		printf("VM: bogus endpoint VM_BRK %d\n", msg->m_source);
53*433d6423SLionel Sambuc 		return EINVAL;
54*433d6423SLionel Sambuc 	}
55*433d6423SLionel Sambuc 
56*433d6423SLionel Sambuc 	return real_brk(&vmproc[proc], (vir_bytes) msg->m_lc_vm_brk.addr);
57*433d6423SLionel Sambuc }
58*433d6423SLionel Sambuc 
59*433d6423SLionel Sambuc /*===========================================================================*
60*433d6423SLionel Sambuc  *				real_brk				     *
61*433d6423SLionel Sambuc  *===========================================================================*/
real_brk(struct vmproc * vmp,vir_bytes v)62*433d6423SLionel Sambuc int real_brk(struct vmproc *vmp, vir_bytes v)
63*433d6423SLionel Sambuc {
64*433d6423SLionel Sambuc 	if(map_region_extend_upto_v(vmp, v) == OK) {
65*433d6423SLionel Sambuc 		return OK;
66*433d6423SLionel Sambuc 	}
67*433d6423SLionel Sambuc 
68*433d6423SLionel Sambuc 	return(ENOMEM);
69*433d6423SLionel Sambuc }
70