source: branches/minix3-book/kernel/system.c@ 11

Last change on this file since 11 was 4, checked in by Mattia Monga, 14 years ago

Importazione sorgenti libro

File size: 17.0 KB
Line 
1/* This task provides an interface between the kernel and user-space system
2 * processes. System services can be accessed by doing a kernel call. Kernel
3 * calls are transformed into request messages, which are handled by this
4 * task. By convention, a sys_call() is transformed in a SYS_CALL request
5 * message that is handled in a function named do_call().
6 *
7 * A private call vector is used to map all kernel calls to the functions that
8 * handle them. The actual handler functions are contained in separate files
9 * to keep this file clean. The call vector is used in the system task's main
10 * loop to handle all incoming requests.
11 *
12 * In addition to the main sys_task() entry point, which starts the main loop,
13 * there are several other minor entry points:
14 * get_priv: assign privilege structure to user or system process
15 * send_sig: send a signal directly to a system process
16 * cause_sig: take action to cause a signal to occur via PM
17 * umap_local: map virtual address in LOCAL_SEG to physical
18 * umap_remote: map virtual address in REMOTE_SEG to physical
19 * umap_bios: map virtual address in BIOS_SEG to physical
20 * virtual_copy: copy bytes from one virtual address to another
21 * get_randomness: accumulate randomness in a buffer
22 *
23 * Changes:
24 * Aug 04, 2005 check if kernel call is allowed (Jorrit N. Herder)
25 * Jul 20, 2005 send signal to services with message (Jorrit N. Herder)
26 * Jan 15, 2005 new, generalized virtual copy function (Jorrit N. Herder)
27 * Oct 10, 2004 dispatch system calls from call vector (Jorrit N. Herder)
28 * Sep 30, 2004 source code documentation updated (Jorrit N. Herder)
29 */
30
31#include "kernel.h"
32#include "system.h"
33#include <stdlib.h>
34#include <signal.h>
35#include <unistd.h>
36#include <sys/sigcontext.h>
37#include <ibm/memory.h>
38#include "protect.h"
39
40/* Declaration of the call vector that defines the mapping of kernel calls
41 * to handler functions. The vector is initialized in sys_init() with map(),
42 * which makes sure the kernel call numbers are ok. No space is allocated,
43 * because the dummy is declared extern. If an illegal call is given, the
44 * array size will be negative and this won't compile.
45 */
46PUBLIC int (*call_vec[NR_SYS_CALLS])(message *m_ptr);
47
48#define map(call_nr, handler) \
49 {extern int dummy[NR_SYS_CALLS>(unsigned)(call_nr-KERNEL_CALL) ? 1:-1];} \
50 call_vec[(call_nr-KERNEL_CALL)] = (handler)
51
52FORWARD _PROTOTYPE( void initialize, (void));
53
54/*===========================================================================*
55 * sys_task *
56 *===========================================================================*/
57PUBLIC void sys_task()
58{
59/* Main entry point of sys_task. Get the message and dispatch on type. */
60 static message m;
61 register int result;
62 register struct proc *caller_ptr;
63 unsigned int call_nr;
64 int s;
65
66 /* Initialize the system task. */
67 initialize();
68
69 while (TRUE) {
70 /* Get work. Block and wait until a request message arrives. */
71 receive(ANY, &m);
72 call_nr = (unsigned) m.m_type - KERNEL_CALL;
73 caller_ptr = proc_addr(m.m_source);
74
75 /* See if the caller made a valid request and try to handle it. */
76 if (! (priv(caller_ptr)->s_call_mask & (1<<call_nr))) {
77 kprintf("SYSTEM: request %d from %d denied.\n", call_nr,m.m_source);
78 result = ECALLDENIED; /* illegal message type */
79 } else if (call_nr >= NR_SYS_CALLS) { /* check call number */
80 kprintf("SYSTEM: illegal request %d from %d.\n", call_nr,m.m_source);
81 result = EBADREQUEST; /* illegal message type */
82 }
83 else {
84 result = (*call_vec[call_nr])(&m); /* handle the kernel call */
85 }
86
87 /* Send a reply, unless inhibited by a handler function. Use the kernel
88 * function lock_send() to prevent a system call trap. The destination
89 * is known to be blocked waiting for a message.
90 */
91 if (result != EDONTREPLY) {
92 m.m_type = result; /* report status of call */
93 if (OK != (s=lock_send(m.m_source, &m))) {
94 kprintf("SYSTEM, reply to %d failed: %d\n", m.m_source, s);
95 }
96 }
97 }
98}
99
100/*===========================================================================*
101 * initialize *
102 *===========================================================================*/
103PRIVATE void initialize(void)
104{
105 register struct priv *sp;
106 int i;
107
108 /* Initialize IRQ handler hooks. Mark all hooks available. */
109 for (i=0; i<NR_IRQ_HOOKS; i++) {
110 irq_hooks[i].proc_nr = NONE;
111 }
112
113 /* Initialize all alarm timers for all processes. */
114 for (sp=BEG_PRIV_ADDR; sp < END_PRIV_ADDR; sp++) {
115 tmr_inittimer(&(sp->s_alarm_timer));
116 }
117
118 /* Initialize the call vector to a safe default handler. Some kernel calls
119 * may be disabled or nonexistant. Then explicitly map known calls to their
120 * handler functions. This is done with a macro that gives a compile error
121 * if an illegal call number is used. The ordering is not important here.
122 */
123 for (i=0; i<NR_SYS_CALLS; i++) {
124 call_vec[i] = do_unused;
125 }
126
127 /* Process management. */
128 map(SYS_FORK, do_fork); /* a process forked a new process */
129 map(SYS_EXEC, do_exec); /* update process after execute */
130 map(SYS_EXIT, do_exit); /* clean up after process exit */
131 map(SYS_NICE, do_nice); /* set scheduling priority */
132 map(SYS_PRIVCTL, do_privctl); /* system privileges control */
133 map(SYS_TRACE, do_trace); /* request a trace operation */
134
135 /* Signal handling. */
136 map(SYS_KILL, do_kill); /* cause a process to be signaled */
137 map(SYS_GETKSIG, do_getksig); /* PM checks for pending signals */
138 map(SYS_ENDKSIG, do_endksig); /* PM finished processing signal */
139 map(SYS_SIGSEND, do_sigsend); /* start POSIX-style signal */
140 map(SYS_SIGRETURN, do_sigreturn); /* return from POSIX-style signal */
141
142 /* Device I/O. */
143 map(SYS_IRQCTL, do_irqctl); /* interrupt control operations */
144 map(SYS_DEVIO, do_devio); /* inb, inw, inl, outb, outw, outl */
145 map(SYS_SDEVIO, do_sdevio); /* phys_insb, _insw, _outsb, _outsw */
146 map(SYS_VDEVIO, do_vdevio); /* vector with devio requests */
147 map(SYS_INT86, do_int86); /* real-mode BIOS calls */
148
149 /* Memory management. */
150 map(SYS_NEWMAP, do_newmap); /* set up a process memory map */
151 map(SYS_SEGCTL, do_segctl); /* add segment and get selector */
152 map(SYS_MEMSET, do_memset); /* write char to memory area */
153
154 /* Copying. */
155 map(SYS_UMAP, do_umap); /* map virtual to physical address */
156 map(SYS_VIRCOPY, do_vircopy); /* use pure virtual addressing */
157 map(SYS_PHYSCOPY, do_physcopy); /* use physical addressing */
158 map(SYS_VIRVCOPY, do_virvcopy); /* vector with copy requests */
159 map(SYS_PHYSVCOPY, do_physvcopy); /* vector with copy requests */
160
161 /* Clock functionality. */
162 map(SYS_TIMES, do_times); /* get uptime and process times */
163 map(SYS_SETALARM, do_setalarm); /* schedule a synchronous alarm */
164
165 /* System control. */
166 map(SYS_ABORT, do_abort); /* abort MINIX */
167 map(SYS_GETINFO, do_getinfo); /* request system information */
168}
169
170/*===========================================================================*
171 * get_priv *
172 *===========================================================================*/
173PUBLIC int get_priv(rc, proc_type)
174register struct proc *rc; /* new (child) process pointer */
175int proc_type; /* system or user process flag */
176{
177/* Get a privilege structure. All user processes share the same privilege
178 * structure. System processes get their own privilege structure.
179 */
180 register struct priv *sp; /* privilege structure */
181
182 if (proc_type == SYS_PROC) { /* find a new slot */
183 for (sp = BEG_PRIV_ADDR; sp < END_PRIV_ADDR; ++sp)
184 if (sp->s_proc_nr == NONE && sp->s_id != USER_PRIV_ID) break;
185 if (sp->s_proc_nr != NONE) return(ENOSPC);
186 rc->p_priv = sp; /* assign new slot */
187 rc->p_priv->s_proc_nr = proc_nr(rc); /* set association */
188 rc->p_priv->s_flags = SYS_PROC; /* mark as privileged */
189 } else {
190 rc->p_priv = &priv[USER_PRIV_ID]; /* use shared slot */
191 rc->p_priv->s_proc_nr = INIT_PROC_NR; /* set association */
192 rc->p_priv->s_flags = 0; /* no initial flags */
193 }
194 return(OK);
195}
196
197/*===========================================================================*
198 * get_randomness *
199 *===========================================================================*/
200PUBLIC void get_randomness(source)
201int source;
202{
203/* On machines with the RDTSC (cycle counter read instruction - pentium
204 * and up), use that for high-resolution raw entropy gathering. Otherwise,
205 * use the realtime clock (tick resolution).
206 *
207 * Unfortunately this test is run-time - we don't want to bother with
208 * compiling different kernels for different machines.
209 *
210 * On machines without RDTSC, we use read_clock().
211 */
212 int r_next;
213 unsigned long tsc_high, tsc_low;
214
215 source %= RANDOM_SOURCES;
216 r_next= krandom.bin[source].r_next;
217 if (machine.processor > 486) {
218 read_tsc(&tsc_high, &tsc_low);
219 krandom.bin[source].r_buf[r_next] = tsc_low;
220 } else {
221 krandom.bin[source].r_buf[r_next] = read_clock();
222 }
223 if (krandom.bin[source].r_size < RANDOM_ELEMENTS) {
224 krandom.bin[source].r_size ++;
225 }
226 krandom.bin[source].r_next = (r_next + 1 ) % RANDOM_ELEMENTS;
227}
228
229/*===========================================================================*
230 * send_sig *
231 *===========================================================================*/
232PUBLIC void send_sig(proc_nr, sig_nr)
233int proc_nr; /* system process to be signalled */
234int sig_nr; /* signal to be sent, 1 to _NSIG */
235{
236/* Notify a system process about a signal. This is straightforward. Simply
237 * set the signal that is to be delivered in the pending signals map and
238 * send a notification with source SYSTEM.
239 */
240 register struct proc *rp;
241
242 rp = proc_addr(proc_nr);
243 sigaddset(&priv(rp)->s_sig_pending, sig_nr);
244 lock_notify(SYSTEM, proc_nr);
245}
246
247/*===========================================================================*
248 * cause_sig *
249 *===========================================================================*/
250PUBLIC void cause_sig(proc_nr, sig_nr)
251int proc_nr; /* process to be signalled */
252int sig_nr; /* signal to be sent, 1 to _NSIG */
253{
254/* A system process wants to send a signal to a process. Examples are:
255 * - HARDWARE wanting to cause a SIGSEGV after a CPU exception
256 * - TTY wanting to cause SIGINT upon getting a DEL
257 * - FS wanting to cause SIGPIPE for a broken pipe
258 * Signals are handled by sending a message to PM. This function handles the
259 * signals and makes sure the PM gets them by sending a notification. The
260 * process being signaled is blocked while PM has not finished all signals
261 * for it.
262 * Race conditions between calls to this function and the system calls that
263 * process pending kernel signals cannot exist. Signal related functions are
264 * only called when a user process causes a CPU exception and from the kernel
265 * process level, which runs to completion.
266 */
267 register struct proc *rp;
268
269 /* Check if the signal is already pending. Process it otherwise. */
270 rp = proc_addr(proc_nr);
271 if (! sigismember(&rp->p_pending, sig_nr)) {
272 sigaddset(&rp->p_pending, sig_nr);
273 if (! (rp->p_rts_flags & SIGNALED)) { /* other pending */
274 if (rp->p_rts_flags == 0) lock_dequeue(rp); /* make not ready */
275 rp->p_rts_flags |= SIGNALED | SIG_PENDING; /* update flags */
276 send_sig(PM_PROC_NR, SIGKSIG);
277 }
278 }
279}
280
281/*===========================================================================*
282 * umap_local *
283 *===========================================================================*/
284PUBLIC phys_bytes umap_local(rp, seg, vir_addr, bytes)
285register struct proc *rp; /* pointer to proc table entry for process */
286int seg; /* T, D, or S segment */
287vir_bytes vir_addr; /* virtual address in bytes within the seg */
288vir_bytes bytes; /* # of bytes to be copied */
289{
290/* Calculate the physical memory address for a given virtual address. */
291 vir_clicks vc; /* the virtual address in clicks */
292 phys_bytes pa; /* intermediate variables as phys_bytes */
293 phys_bytes seg_base;
294
295 /* If 'seg' is D it could really be S and vice versa. T really means T.
296 * If the virtual address falls in the gap, it causes a problem. On the
297 * 8088 it is probably a legal stack reference, since "stackfaults" are
298 * not detected by the hardware. On 8088s, the gap is called S and
299 * accepted, but on other machines it is called D and rejected.
300 * The Atari ST behaves like the 8088 in this respect.
301 */
302
303 if (bytes <= 0) return( (phys_bytes) 0);
304 if (vir_addr + bytes <= vir_addr) return 0; /* overflow */
305 vc = (vir_addr + bytes - 1) >> CLICK_SHIFT; /* last click of data */
306
307 if (seg != T)
308 seg = (vc < rp->p_memmap[D].mem_vir + rp->p_memmap[D].mem_len ? D : S);
309
310 if ((vir_addr>>CLICK_SHIFT) >= rp->p_memmap[seg].mem_vir +
311 rp->p_memmap[seg].mem_len) return( (phys_bytes) 0 );
312
313 if (vc >= rp->p_memmap[seg].mem_vir +
314 rp->p_memmap[seg].mem_len) return( (phys_bytes) 0 );
315
316 seg_base = (phys_bytes) rp->p_memmap[seg].mem_phys;
317 seg_base = seg_base << CLICK_SHIFT; /* segment origin in bytes */
318 pa = (phys_bytes) vir_addr;
319 pa -= rp->p_memmap[seg].mem_vir << CLICK_SHIFT;
320 return(seg_base + pa);
321}
322
323/*===========================================================================*
324 * umap_remote *
325 *===========================================================================*/
326PUBLIC phys_bytes umap_remote(rp, seg, vir_addr, bytes)
327register struct proc *rp; /* pointer to proc table entry for process */
328int seg; /* index of remote segment */
329vir_bytes vir_addr; /* virtual address in bytes within the seg */
330vir_bytes bytes; /* # of bytes to be copied */
331{
332/* Calculate the physical memory address for a given virtual address. */
333 struct far_mem *fm;
334
335 if (bytes <= 0) return( (phys_bytes) 0);
336 if (seg < 0 || seg >= NR_REMOTE_SEGS) return( (phys_bytes) 0);
337
338 fm = &rp->p_priv->s_farmem[seg];
339 if (! fm->in_use) return( (phys_bytes) 0);
340 if (vir_addr + bytes > fm->mem_len) return( (phys_bytes) 0);
341
342 return(fm->mem_phys + (phys_bytes) vir_addr);
343}
344
345/*===========================================================================*
346 * umap_bios *
347 *===========================================================================*/
348PUBLIC phys_bytes umap_bios(rp, vir_addr, bytes)
349register struct proc *rp; /* pointer to proc table entry for process */
350vir_bytes vir_addr; /* virtual address in BIOS segment */
351vir_bytes bytes; /* # of bytes to be copied */
352{
353/* Calculate the physical memory address at the BIOS. Note: currently, BIOS
354 * address zero (the first BIOS interrupt vector) is not considered as an
355 * error here, but since the physical address will be zero as well, the
356 * calling function will think an error occurred. This is not a problem,
357 * since no one uses the first BIOS interrupt vector.
358 */
359
360 /* Check all acceptable ranges. */
361 if (vir_addr >= BIOS_MEM_BEGIN && vir_addr + bytes <= BIOS_MEM_END)
362 return (phys_bytes) vir_addr;
363 else if (vir_addr >= BASE_MEM_TOP && vir_addr + bytes <= UPPER_MEM_END)
364 return (phys_bytes) vir_addr;
365 kprintf("Warning, error in umap_bios, virtual address 0x%x\n", vir_addr);
366 return 0;
367}
368
369/*===========================================================================*
370 * virtual_copy *
371 *===========================================================================*/
372PUBLIC int virtual_copy(src_addr, dst_addr, bytes)
373struct vir_addr *src_addr; /* source virtual address */
374struct vir_addr *dst_addr; /* destination virtual address */
375vir_bytes bytes; /* # of bytes to copy */
376{
377/* Copy bytes from virtual address src_addr to virtual address dst_addr.
378 * Virtual addresses can be in ABS, LOCAL_SEG, REMOTE_SEG, or BIOS_SEG.
379 */
380 struct vir_addr *vir_addr[2]; /* virtual source and destination address */
381 phys_bytes phys_addr[2]; /* absolute source and destination */
382 int seg_index;
383 int i;
384
385 /* Check copy count. */
386 if (bytes <= 0) return(EDOM);
387
388 /* Do some more checks and map virtual addresses to physical addresses. */
389 vir_addr[_SRC_] = src_addr;
390 vir_addr[_DST_] = dst_addr;
391 for (i=_SRC_; i<=_DST_; i++) {
392
393 /* Get physical address. */
394 switch((vir_addr[i]->segment & SEGMENT_TYPE)) {
395 case LOCAL_SEG:
396 seg_index = vir_addr[i]->segment & SEGMENT_INDEX;
397 phys_addr[i] = umap_local( proc_addr(vir_addr[i]->proc_nr),
398 seg_index, vir_addr[i]->offset, bytes );
399 break;
400 case REMOTE_SEG:
401 seg_index = vir_addr[i]->segment & SEGMENT_INDEX;
402 phys_addr[i] = umap_remote( proc_addr(vir_addr[i]->proc_nr),
403 seg_index, vir_addr[i]->offset, bytes );
404 break;
405 case BIOS_SEG:
406 phys_addr[i] = umap_bios( proc_addr(vir_addr[i]->proc_nr),
407 vir_addr[i]->offset, bytes );
408 break;
409 case PHYS_SEG:
410 phys_addr[i] = vir_addr[i]->offset;
411 break;
412 default:
413 return(EINVAL);
414 }
415
416 /* Check if mapping succeeded. */
417 if (phys_addr[i] <= 0 && vir_addr[i]->segment != PHYS_SEG)
418 return(EFAULT);
419 }
420
421 /* Now copy bytes between physical addresseses. */
422 phys_copy(phys_addr[_SRC_], phys_addr[_DST_], (phys_bytes) bytes);
423 return(OK);
424}
425
Note: See TracBrowser for help on using the repository browser.