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