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 | * adjust: see if a proposed segment adjustment is allowed
|
---|
15 | * size_ok: see if the segment sizes are feasible
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "pm.h"
|
---|
19 | #include <signal.h>
|
---|
20 | #include "mproc.h"
|
---|
21 | #include "param.h"
|
---|
22 |
|
---|
23 | #define DATA_CHANGED 1 /* flag value when data segment size changed */
|
---|
24 | #define STACK_CHANGED 2 /* flag value when stack size changed */
|
---|
25 |
|
---|
26 | /*===========================================================================*
|
---|
27 | * do_brk *
|
---|
28 | *===========================================================================*/
|
---|
29 | PUBLIC int do_brk()
|
---|
30 | {
|
---|
31 | /* Perform the brk(addr) system call.
|
---|
32 | *
|
---|
33 | * The call is complicated by the fact that on some machines (e.g., 8088),
|
---|
34 | * the stack pointer can grow beyond the base of the stack segment without
|
---|
35 | * anybody noticing it.
|
---|
36 | * The parameter, 'addr' is the new virtual address in D space.
|
---|
37 | */
|
---|
38 |
|
---|
39 | register struct mproc *rmp;
|
---|
40 | int r;
|
---|
41 | vir_bytes v, new_sp;
|
---|
42 | vir_clicks new_clicks;
|
---|
43 |
|
---|
44 | rmp = mp;
|
---|
45 | v = (vir_bytes) m_in.addr;
|
---|
46 | new_clicks = (vir_clicks) ( ((long) v + CLICK_SIZE - 1) >> CLICK_SHIFT);
|
---|
47 | if (new_clicks < rmp->mp_seg[D].mem_vir) {
|
---|
48 | rmp->mp_reply.reply_ptr = (char *) -1;
|
---|
49 | return(ENOMEM);
|
---|
50 | }
|
---|
51 | new_clicks -= rmp->mp_seg[D].mem_vir;
|
---|
52 | if ((r=get_stack_ptr(who, &new_sp)) != OK) /* ask kernel for sp value */
|
---|
53 | panic(__FILE__,"couldn't get stack pointer", r);
|
---|
54 | r = adjust(rmp, new_clicks, new_sp);
|
---|
55 | rmp->mp_reply.reply_ptr = (r == OK ? m_in.addr : (char *) -1);
|
---|
56 | return(r); /* return new address or -1 */
|
---|
57 | }
|
---|
58 |
|
---|
59 | /*===========================================================================*
|
---|
60 | * adjust *
|
---|
61 | *===========================================================================*/
|
---|
62 | PUBLIC int adjust(rmp, data_clicks, sp)
|
---|
63 | register struct mproc *rmp; /* whose memory is being adjusted? */
|
---|
64 | vir_clicks data_clicks; /* how big is data segment to become? */
|
---|
65 | vir_bytes sp; /* new value of sp */
|
---|
66 | {
|
---|
67 | /* See if data and stack segments can coexist, adjusting them if need be.
|
---|
68 | * Memory is never allocated or freed. Instead it is added or removed from the
|
---|
69 | * gap between data segment and stack segment. If the gap size becomes
|
---|
70 | * negative, the adjustment of data or stack fails and ENOMEM is returned.
|
---|
71 | */
|
---|
72 |
|
---|
73 | register struct mem_map *mem_sp, *mem_dp;
|
---|
74 | vir_clicks sp_click, gap_base, lower, old_clicks;
|
---|
75 | int changed, r, ft;
|
---|
76 | long base_of_stack, delta; /* longs avoid certain problems */
|
---|
77 |
|
---|
78 | mem_dp = &rmp->mp_seg[D]; /* pointer to data segment map */
|
---|
79 | mem_sp = &rmp->mp_seg[S]; /* pointer to stack segment map */
|
---|
80 | changed = 0; /* set when either segment changed */
|
---|
81 |
|
---|
82 | if (mem_sp->mem_len == 0) return(OK); /* don't bother init */
|
---|
83 |
|
---|
84 | /* See if stack size has gone negative (i.e., sp too close to 0xFFFF...) */
|
---|
85 | base_of_stack = (long) mem_sp->mem_vir + (long) mem_sp->mem_len;
|
---|
86 | sp_click = sp >> CLICK_SHIFT; /* click containing sp */
|
---|
87 | if (sp_click >= base_of_stack) return(ENOMEM); /* sp too high */
|
---|
88 |
|
---|
89 | /* Compute size of gap between stack and data segments. */
|
---|
90 | delta = (long) mem_sp->mem_vir - (long) sp_click;
|
---|
91 | lower = (delta > 0 ? sp_click : mem_sp->mem_vir);
|
---|
92 |
|
---|
93 | /* Add a safety margin for future stack growth. Impossible to do right. */
|
---|
94 | #define SAFETY_BYTES (384 * sizeof(char *))
|
---|
95 | #define SAFETY_CLICKS ((SAFETY_BYTES + CLICK_SIZE - 1) / CLICK_SIZE)
|
---|
96 | gap_base = mem_dp->mem_vir + data_clicks + SAFETY_CLICKS;
|
---|
97 | if (lower < gap_base) return(ENOMEM); /* data and stack collided */
|
---|
98 |
|
---|
99 | /* Update data length (but not data orgin) on behalf of brk() system call. */
|
---|
100 | old_clicks = mem_dp->mem_len;
|
---|
101 | if (data_clicks != mem_dp->mem_len) {
|
---|
102 | mem_dp->mem_len = data_clicks;
|
---|
103 | changed |= DATA_CHANGED;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /* Update stack length and origin due to change in stack pointer. */
|
---|
107 | if (delta > 0) {
|
---|
108 | mem_sp->mem_vir -= delta;
|
---|
109 | mem_sp->mem_phys -= delta;
|
---|
110 | mem_sp->mem_len += delta;
|
---|
111 | changed |= STACK_CHANGED;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* Do the new data and stack segment sizes fit in the address space? */
|
---|
115 | ft = (rmp->mp_flags & SEPARATE);
|
---|
116 | r = (rmp->mp_seg[D].mem_vir + rmp->mp_seg[D].mem_len >
|
---|
117 | rmp->mp_seg[S].mem_vir) ? ENOMEM : OK;
|
---|
118 | if (r == OK) {
|
---|
119 | if (changed) sys_newmap((int)(rmp - mproc), rmp->mp_seg);
|
---|
120 | return(OK);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /* New sizes don't fit or require too many page/segment registers. Restore.*/
|
---|
124 | if (changed & DATA_CHANGED) mem_dp->mem_len = old_clicks;
|
---|
125 | if (changed & STACK_CHANGED) {
|
---|
126 | mem_sp->mem_vir += delta;
|
---|
127 | mem_sp->mem_phys += delta;
|
---|
128 | mem_sp->mem_len -= delta;
|
---|
129 | }
|
---|
130 | return(ENOMEM);
|
---|
131 | }
|
---|