kernel/vm: change pde table info from single buffer to explicit per-process.
[minix.git] / kernel / system / do_fork.c
blob11629afb930a833c5875493d74eb13359939ec21
1 /* The kernel call implemented in this file:
2 * m_type: SYS_FORK
4 * The parameters for this kernel call are:
5 * m1_i1: PR_ENDPT (parent, process that forked)
6 * m1_i2: PR_SLOT (child's process table slot)
7 * m1_p1: PR_MEM_PTR (new memory map for the child)
8 * m1_i3: PR_FORK_FLAGS (fork flags)
9 */
11 #include "kernel/system.h"
12 #include "kernel/vm.h"
13 #include <signal.h>
14 #include <string.h>
15 #include <assert.h>
17 #include <minix/endpoint.h>
19 #if USE_FORK
21 /*===========================================================================*
22 * do_fork *
23 *===========================================================================*/
24 PUBLIC int do_fork(struct proc * caller, message * m_ptr)
26 /* Handle sys_fork(). PR_ENDPT has forked. The child is PR_SLOT. */
27 #if (_MINIX_CHIP == _CHIP_INTEL)
28 reg_t old_ldt_sel;
29 void *old_fpu_save_area_p;
30 #endif
31 register struct proc *rpc; /* child process pointer */
32 struct proc *rpp; /* parent process pointer */
33 struct mem_map *map_ptr; /* virtual address of map inside caller (PM) */
34 int gen, r;
35 int p_proc;
37 if(!isokendpt(m_ptr->PR_ENDPT, &p_proc))
38 return EINVAL;
40 rpp = proc_addr(p_proc);
41 rpc = proc_addr(m_ptr->PR_SLOT);
42 if (isemptyp(rpp) || ! isemptyp(rpc)) return(EINVAL);
44 assert(!(rpp->p_misc_flags & MF_DELIVERMSG));
46 /* needs to be receiving so we know where the message buffer is */
47 if(!RTS_ISSET(rpp, RTS_RECEIVING)) {
48 printf("kernel: fork not done synchronously?\n");
49 return EINVAL;
52 map_ptr= (struct mem_map *) m_ptr->PR_MEM_PTR;
54 /* Copy parent 'proc' struct to child. And reinitialize some fields. */
55 gen = _ENDPOINT_G(rpc->p_endpoint);
56 #if (_MINIX_CHIP == _CHIP_INTEL)
57 old_ldt_sel = rpc->p_seg.p_ldt_sel; /* backup local descriptors */
58 old_fpu_save_area_p = rpc->p_fpu_state.fpu_save_area_p;
59 #endif
60 *rpc = *rpp; /* copy 'proc' struct */
61 #if (_MINIX_CHIP == _CHIP_INTEL)
62 rpc->p_seg.p_ldt_sel = old_ldt_sel; /* restore descriptors */
63 rpc->p_fpu_state.fpu_save_area_p = old_fpu_save_area_p;
64 if(rpp->p_misc_flags & MF_FPU_INITIALIZED)
65 memcpy(rpc->p_fpu_state.fpu_save_area_p,
66 rpp->p_fpu_state.fpu_save_area_p,
67 FPU_XFP_SIZE);
68 #endif
69 if(++gen >= _ENDPOINT_MAX_GENERATION) /* increase generation */
70 gen = 1; /* generation number wraparound */
71 rpc->p_nr = m_ptr->PR_SLOT; /* this was obliterated by copy */
72 rpc->p_endpoint = _ENDPOINT(gen, rpc->p_nr); /* new endpoint of slot */
74 rpc->p_reg.retreg = 0; /* child sees pid = 0 to know it is child */
75 rpc->p_user_time = 0; /* set all the accounting times to 0 */
76 rpc->p_sys_time = 0;
78 rpc->p_reg.psw &= ~TRACEBIT; /* clear trace bit */
79 rpc->p_misc_flags &= ~(MF_VIRT_TIMER | MF_PROF_TIMER | MF_SC_TRACE);
80 rpc->p_virt_left = 0; /* disable, clear the process-virtual timers */
81 rpc->p_prof_left = 0;
84 * if the child process inherited a scheduler, the child process is not
85 * runnable until it's scheduled. Otherwise the default kernel policy applies.
86 * This is only the case of system servers, drivers and similar sensitive
87 * processes
89 if (rpc->p_scheduler)
90 RTS_SET(rpc, RTS_NO_QUANTUM);
92 /* If the parent is a privileged process, take away the privileges from the
93 * child process and inhibit it from running by setting the NO_PRIV flag.
94 * The caller should explicitely set the new privileges before executing.
96 if (priv(rpp)->s_flags & SYS_PROC) {
97 rpc->p_priv = priv_addr(USER_PRIV_ID);
98 rpc->p_rts_flags |= RTS_NO_PRIV;
101 /* Calculate endpoint identifier, so caller knows what it is. */
102 m_ptr->PR_ENDPT = rpc->p_endpoint;
103 m_ptr->PR_FORK_MSGADDR = (char *) rpp->p_delivermsg_vir;
105 /* Install new map */
106 r = newmap(caller, rpc, map_ptr);
107 FIXLINMSG(rpc);
109 /* Don't schedule process in VM mode until it has a new pagetable. */
110 if(m_ptr->PR_FORK_FLAGS & PFF_VMINHIBIT) {
111 RTS_SET(rpc, RTS_VMINHIBIT);
115 * Only one in group should have RTS_SIGNALED, child doesn't inherit tracing.
117 RTS_UNSET(rpc, (RTS_SIGNALED | RTS_SIG_PENDING | RTS_P_STOP));
118 sigemptyset(&rpc->p_pending);
120 rpc->p_seg.p_cr3 = 0;
121 rpc->p_seg.p_cr3_v = NULL;
123 return r;
126 #endif /* USE_FORK */