panic() cleanup.
[minix.git] / kernel / system / do_fork.c
blob294b0f23866efc9d020815e54bbf34e30872495e
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 "../system.h"
12 #include "../vm.h"
13 #include <signal.h>
14 #include <string.h>
16 #include <minix/endpoint.h>
18 #if USE_FORK
20 /*===========================================================================*
21 * do_fork *
22 *===========================================================================*/
23 PUBLIC int do_fork(struct proc * caller, message * m_ptr)
25 /* Handle sys_fork(). PR_ENDPT has forked. The child is PR_SLOT. */
26 #if (_MINIX_CHIP == _CHIP_INTEL)
27 reg_t old_ldt_sel;
28 void *old_fpu_save_area_p;
29 #endif
30 register struct proc *rpc; /* child process pointer */
31 struct proc *rpp; /* parent process pointer */
32 struct mem_map *map_ptr; /* virtual address of map inside caller (PM) */
33 int gen, r;
34 int p_proc;
36 if(!isokendpt(m_ptr->PR_ENDPT, &p_proc))
37 return EINVAL;
39 rpp = proc_addr(p_proc);
40 rpc = proc_addr(m_ptr->PR_SLOT);
41 if (isemptyp(rpp) || ! isemptyp(rpc)) return(EINVAL);
43 vmassert(!(rpp->p_misc_flags & MF_DELIVERMSG));
45 /* needs to be receiving so we know where the message buffer is */
46 if(!RTS_ISSET(rpp, RTS_RECEIVING)) {
47 printf("kernel: fork not done synchronously?\n");
48 return EINVAL;
51 map_ptr= (struct mem_map *) m_ptr->PR_MEM_PTR;
53 /* Copy parent 'proc' struct to child. And reinitialize some fields. */
54 gen = _ENDPOINT_G(rpc->p_endpoint);
55 #if (_MINIX_CHIP == _CHIP_INTEL)
56 old_ldt_sel = rpc->p_seg.p_ldt_sel; /* backup local descriptors */
57 old_fpu_save_area_p = rpc->p_fpu_state.fpu_save_area_p;
58 #endif
59 *rpc = *rpp; /* copy 'proc' struct */
60 #if (_MINIX_CHIP == _CHIP_INTEL)
61 rpc->p_seg.p_ldt_sel = old_ldt_sel; /* restore descriptors */
62 rpc->p_fpu_state.fpu_save_area_p = old_fpu_save_area_p;
63 if(rpp->p_misc_flags & MF_FPU_INITIALIZED)
64 memcpy(rpc->p_fpu_state.fpu_save_area_p,
65 rpp->p_fpu_state.fpu_save_area_p,
66 FPU_XFP_SIZE);
67 #endif
68 if(++gen >= _ENDPOINT_MAX_GENERATION) /* increase generation */
69 gen = 1; /* generation number wraparound */
70 rpc->p_nr = m_ptr->PR_SLOT; /* this was obliterated by copy */
71 rpc->p_endpoint = _ENDPOINT(gen, rpc->p_nr); /* new endpoint of slot */
73 rpc->p_reg.retreg = 0; /* child sees pid = 0 to know it is child */
74 rpc->p_user_time = 0; /* set all the accounting times to 0 */
75 rpc->p_sys_time = 0;
77 rpc->p_reg.psw &= ~TRACEBIT; /* clear trace bit */
78 rpc->p_misc_flags &= ~(MF_VIRT_TIMER | MF_PROF_TIMER | MF_SC_TRACE);
79 rpc->p_virt_left = 0; /* disable, clear the process-virtual timers */
80 rpc->p_prof_left = 0;
82 /* Parent and child have to share the quantum that the forked process had,
83 * so that queued processes do not have to wait longer because of the fork.
84 * If the time left is odd, the child gets an extra tick.
86 rpc->p_ticks_left = (rpc->p_ticks_left + 1) / 2;
87 rpp->p_ticks_left = rpp->p_ticks_left / 2;
89 /* If the parent is a privileged process, take away the privileges from the
90 * child process and inhibit it from running by setting the NO_PRIV flag.
91 * The caller should explicitely set the new privileges before executing.
93 if (priv(rpp)->s_flags & SYS_PROC) {
94 rpc->p_priv = priv_addr(USER_PRIV_ID);
95 rpc->p_rts_flags |= RTS_NO_PRIV;
98 /* Calculate endpoint identifier, so caller knows what it is. */
99 m_ptr->PR_ENDPT = rpc->p_endpoint;
100 m_ptr->PR_FORK_MSGADDR = (char *) rpp->p_delivermsg_vir;
102 /* Install new map */
103 r = newmap(caller, rpc, map_ptr);
104 FIXLINMSG(rpc);
106 /* Don't schedule process in VM mode until it has a new pagetable. */
107 if(m_ptr->PR_FORK_FLAGS & PFF_VMINHIBIT) {
108 RTS_SET(rpc, RTS_VMINHIBIT);
112 * Only one in group should have RTS_SIGNALED, child doesn't inherit tracing.
114 RTS_UNSET(rpc, (RTS_SIGNALED | RTS_SIG_PENDING | RTS_P_STOP));
115 sigemptyset(&rpc->p_pending);
117 return r;
120 #endif /* USE_FORK */