xref: /minix/minix/kernel/system/do_fork.c (revision 83133719)
1 /* The kernel call implemented in this file:
2  *   m_type:	SYS_FORK
3  *
4  * The parameters for this kernel call are:
5  *   m_lsys_krn_sys_fork.endpt		(parent, process that forked)
6  *   m_lsys_krn_sys_fork.slot		(child's process table slot)
7  *   m_lsys_krn_sys_fork.flags		(fork flags)
8  *   m_krn_lsys_sys_fork.endpt		(endpoint of the child)
9  *   m_krn_lsys_sys_fork.msgaddr	(new memory map for the child)
10  */
11 
12 #include "kernel/system.h"
13 #include "kernel/vm.h"
14 #include <signal.h>
15 #include <string.h>
16 #include <assert.h>
17 
18 #include <minix/endpoint.h>
19 #include <minix/u64.h>
20 
21 #if USE_FORK
22 
23 /*===========================================================================*
24  *				do_fork					     *
25  *===========================================================================*/
26 int do_fork(struct proc * caller, message * m_ptr)
27 {
28 /* Handle sys_fork().
29  * m_lsys_krn_sys_fork.endpt has forked.
30  * The child is m_lsys_krn_sys_fork.slot.
31  */
32 #if defined(__i386__)
33   char *old_fpu_save_area_p;
34 #endif
35   register struct proc *rpc;		/* child process pointer */
36   struct proc *rpp;			/* parent process pointer */
37   int gen;
38   int p_proc;
39   int namelen;
40 
41   if(!isokendpt(m_ptr->m_lsys_krn_sys_fork.endpt, &p_proc))
42 	return EINVAL;
43 
44   rpp = proc_addr(p_proc);
45   rpc = proc_addr(m_ptr->m_lsys_krn_sys_fork.slot);
46   if (isemptyp(rpp) || ! isemptyp(rpc)) return(EINVAL);
47 
48   assert(!(rpp->p_misc_flags & MF_DELIVERMSG));
49 
50   /* needs to be receiving so we know where the message buffer is */
51   if(!RTS_ISSET(rpp, RTS_RECEIVING)) {
52 	printf("kernel: fork not done synchronously?\n");
53 	return EINVAL;
54   }
55 
56   /* make sure that the FPU context is saved in parent before copy */
57   save_fpu(rpp);
58   /* Copy parent 'proc' struct to child. And reinitialize some fields. */
59   gen = _ENDPOINT_G(rpc->p_endpoint);
60 #if defined(__i386__)
61   old_fpu_save_area_p = rpc->p_seg.fpu_state;
62 #endif
63   *rpc = *rpp;				/* copy 'proc' struct */
64 #if defined(__i386__)
65   rpc->p_seg.fpu_state = old_fpu_save_area_p;
66   if(proc_used_fpu(rpp))
67 	memcpy(rpc->p_seg.fpu_state, rpp->p_seg.fpu_state, 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->m_lsys_krn_sys_fork.slot;	/* this was obliterated by copy */
72   rpc->p_endpoint = _ENDPOINT(gen, rpc->p_nr);	/* new endpoint of slot */
73 
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;
77 
78   rpc->p_misc_flags &=
79 	~(MF_VIRT_TIMER | MF_PROF_TIMER | MF_SC_TRACE | MF_SPROF_SEEN | MF_STEP);
80   rpc->p_virt_left = 0;		/* disable, clear the process-virtual timers */
81   rpc->p_prof_left = 0;
82 
83   /* Mark process name as being a forked copy */
84   namelen = strlen(rpc->p_name);
85 #define FORKSTR "*F"
86   if(namelen+strlen(FORKSTR) < sizeof(rpc->p_name))
87 	strcat(rpc->p_name, FORKSTR);
88 
89   /* the child process is not runnable until it's scheduled. */
90   RTS_SET(rpc, RTS_NO_QUANTUM);
91   reset_proc_accounting(rpc);
92 
93   rpc->p_cpu_time_left = 0;
94   rpc->p_cycles = 0;
95   rpc->p_kcall_cycles = 0;
96   rpc->p_kipc_cycles = 0;
97   rpc->p_signal_received = 0;
98 
99   /* If the parent is a privileged process, take away the privileges from the
100    * child process and inhibit it from running by setting the NO_PRIV flag.
101    * The caller should explicitly set the new privileges before executing.
102    */
103   if (priv(rpp)->s_flags & SYS_PROC) {
104       rpc->p_priv = priv_addr(USER_PRIV_ID);
105       rpc->p_rts_flags |= RTS_NO_PRIV;
106   }
107 
108   /* Calculate endpoint identifier, so caller knows what it is. */
109   m_ptr->m_krn_lsys_sys_fork.endpt = rpc->p_endpoint;
110   m_ptr->m_krn_lsys_sys_fork.msgaddr = rpp->p_delivermsg_vir;
111 
112   /* Don't schedule process in VM mode until it has a new pagetable. */
113   if(m_ptr->m_lsys_krn_sys_fork.flags & PFF_VMINHIBIT) {
114   	RTS_SET(rpc, RTS_VMINHIBIT);
115   }
116 
117   /*
118    * Only one in group should have RTS_SIGNALED, child doesn't inherit tracing.
119    */
120   RTS_UNSET(rpc, (RTS_SIGNALED | RTS_SIG_PENDING | RTS_P_STOP));
121   (void) sigemptyset(&rpc->p_pending);
122 
123 #if defined(__i386__)
124   rpc->p_seg.p_cr3 = 0;
125   rpc->p_seg.p_cr3_v = NULL;
126 #elif defined(__arm__)
127   rpc->p_seg.p_ttbr = 0;
128   rpc->p_seg.p_ttbr_v = NULL;
129 #endif
130 
131   return OK;
132 }
133 
134 #endif /* USE_FORK */
135