Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
180 lines (148 loc) · 5.95 KB
/
Copy pathmain.c
File metadata and controls
180 lines (148 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* This file contains the main program of the memory manager and some related
* procedures. When MINIX starts up, the kernel runs for a little while,
* initializing itself and its tasks, and then it runs MM and FS. Both MM
* and FS initialize themselves as far as they can. FS then makes a call to
* MM, because MM has to wait for FS to acquire a RAM disk. MM asks the
* kernel for all free memory and starts serving requests.
*
* The entry points into this file are:
* main: starts MM running
* setreply: set the reply to be sent to process making an MM system call
*/
#include "mm.h"
#include <minix/callnr.h>
#include <minix/com.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/ioc_memory.h>
#include "mproc.h"
#include "param.h"
FORWARD _PROTOTYPE( void get_work, (void) );
FORWARD _PROTOTYPE( void mm_init, (void) );
#define click_to_round_k(n) \
((unsigned) ((((unsigned long) (n) << CLICK_SHIFT) + 512) / 1024))
/*===========================================================================*
* main *
*===========================================================================*/
PUBLIC void main()
{
/* Main routine of the memory manager. */
int result, proc_nr;
struct mproc *rmp;
mm_init(); /* initialize memory manager tables */
/* This is MM's main loop- get work and do it, forever and forever. */
while (TRUE) {
get_work(); /* wait for an MM system call */
/* If the call number is valid, perform the call. */
if ((unsigned) mm_call >= NCALLS) {
result = ENOSYS;
} else {
result = (*call_vec[mm_call])();
}
/* Send the results back to the user to indicate completion. */
if (result != SUSPEND) setreply(who, result);
swap_in(); /* maybe a process can be swapped in? */
/* Send out all pending reply messages, including the answer to
* the call just made above. The processes must not be swapped out.
*/
for (proc_nr = 0, rmp = mproc; proc_nr < NR_PROCS; proc_nr++, rmp++) {
if ((rmp->mp_flags & (REPLY | ONSWAP)) == REPLY) {
if (send(proc_nr, &rmp->mp_reply) != OK)
panic("MM can't reply to", proc_nr);
rmp->mp_flags &= ~REPLY;
}
}
}
}
/*===========================================================================*
* get_work *
*===========================================================================*/
PRIVATE void get_work()
{
/* Wait for the next message and extract useful information from it. */
if (receive(ANY, &mm_in) != OK) panic("MM receive error", NO_NUM);
who = mm_in.m_source; /* who sent the message */
mm_call = mm_in.m_type; /* system call number */
/* Process slot of caller. Misuse MM's own process slot for tasks (KSIG?). */
mp = &mproc[who < 0 ? MM_PROC_NR : who];
}
/*===========================================================================*
* setreply *
*===========================================================================*/
PUBLIC void setreply(proc_nr, result)
int proc_nr; /* process to reply to */
int result; /* result of the call (usually OK or error #)*/
{
/* Fill in a reply message to be sent later to a user process. System calls
* may occasionally fill in other fields, this is only for the main return
* value, and for setting the "must send reply" flag.
*/
register struct mproc *rmp = &mproc[proc_nr];
rmp->reply_res = result;
rmp->mp_flags |= REPLY; /* reply pending */
if (rmp->mp_flags & ONSWAP)
swap_inqueue(rmp); /* must swap this process back in */
}
/*===========================================================================*
* mm_init *
*===========================================================================*/
PRIVATE void mm_init()
{
/* Initialize the memory manager. */
static char core_sigs[] = { SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
SIGEMT, SIGFPE, SIGUSR1, SIGSEGV, SIGUSR2 };
static char ign_sigs[] = { SIGCHLD };
register int proc_nr;
register struct mproc *rmp;
register char *sig_ptr;
phys_clicks ram_clicks, total_clicks, minix_clicks, free_clicks;
message mess;
struct mem_map kernel_map[NR_SEGS];
int mem;
/* Build the set of signals which cause core dumps, and the set of signals
* that are by default ignored.
*/
sigemptyset(&core_sset);
for (sig_ptr = core_sigs; sig_ptr < core_sigs+sizeof(core_sigs); sig_ptr++)
sigaddset(&core_sset, *sig_ptr);
sigemptyset(&ign_sset);
for (sig_ptr = ign_sigs; sig_ptr < ign_sigs+sizeof(ign_sigs); sig_ptr++)
sigaddset(&ign_sset, *sig_ptr);
/* Get the memory map of the kernel to see how much memory it uses. */
sys_getmap(SYSTASK, kernel_map);
minix_clicks = (kernel_map[S].mem_phys + kernel_map[S].mem_len)
- kernel_map[T].mem_phys;
/* Initialize MM's tables. */
for (proc_nr = 0; proc_nr <= INIT_PROC_NR; proc_nr++) {
rmp = &mproc[proc_nr];
rmp->mp_flags |= IN_USE;
sys_getmap(proc_nr, rmp->mp_seg);
if (rmp->mp_seg[T].mem_len != 0) rmp->mp_flags |= SEPARATE;
minix_clicks += (rmp->mp_seg[S].mem_phys + rmp->mp_seg[S].mem_len)
- rmp->mp_seg[T].mem_phys;
}
mproc[INIT_PROC_NR].mp_pid = INIT_PID;
sigemptyset(&mproc[INIT_PROC_NR].mp_ignore);
sigemptyset(&mproc[INIT_PROC_NR].mp_catch);
procs_in_use = LOW_USER + 1;
/* Wait for FS to send a message telling the RAM disk size then go "on-line".
*/
if (receive(FS_PROC_NR, &mess) != OK)
panic("MM can't obtain RAM disk size from FS", NO_NUM);
ram_clicks = mess.m1_
4233
i1;
/* Initialize tables to all physical mem. */
mem_init(&total_clicks, &free_clicks);
/* Print memory information. */
printf("\nMemory size = %uK ", click_to_round_k(total_clicks));
printf("MINIX = %uK ", click_to_round_k(minix_clicks));
printf("RAM disk = %uK ", click_to_round_k(ram_clicks));
printf("Available = %uK\n\n", click_to_round_k(free_clicks));
/* Tell FS to continue. */
if (send(FS_PROC_NR, &mess) != OK)
panic("MM can't sync up with FS", NO_NUM);
/* Tell the memory task where my process table is for the sake of ps(1). */
if ((mem = open("/dev/ram", O_RDWR)) != -1) {
ioctl(mem, MIOCSPSINFO, (void *) mproc);
close(mem);
}
}