1433d6423SLionel Sambuc /* The kernel call implemented in this file:
2433d6423SLionel Sambuc * m_type: SYS_FORK
3433d6423SLionel Sambuc *
4433d6423SLionel Sambuc * The parameters for this kernel call are:
5433d6423SLionel Sambuc * m_lsys_krn_sys_fork.endpt (parent, process that forked)
6433d6423SLionel Sambuc * m_lsys_krn_sys_fork.slot (child's process table slot)
7433d6423SLionel Sambuc * m_lsys_krn_sys_fork.flags (fork flags)
8433d6423SLionel Sambuc * m_krn_lsys_sys_fork.endpt (endpoint of the child)
9433d6423SLionel Sambuc * m_krn_lsys_sys_fork.msgaddr (new memory map for the child)
10433d6423SLionel Sambuc */
11433d6423SLionel Sambuc
12433d6423SLionel Sambuc #include "kernel/system.h"
13433d6423SLionel Sambuc #include "kernel/vm.h"
14433d6423SLionel Sambuc #include <signal.h>
15433d6423SLionel Sambuc #include <string.h>
16433d6423SLionel Sambuc #include <assert.h>
17433d6423SLionel Sambuc
18433d6423SLionel Sambuc #include <minix/endpoint.h>
19433d6423SLionel Sambuc #include <minix/u64.h>
20433d6423SLionel Sambuc
21433d6423SLionel Sambuc #if USE_FORK
22433d6423SLionel Sambuc
23433d6423SLionel Sambuc /*===========================================================================*
24433d6423SLionel Sambuc * do_fork *
25433d6423SLionel Sambuc *===========================================================================*/
do_fork(struct proc * caller,message * m_ptr)26433d6423SLionel Sambuc int do_fork(struct proc * caller, message * m_ptr)
27433d6423SLionel Sambuc {
28433d6423SLionel Sambuc /* Handle sys_fork().
29433d6423SLionel Sambuc * m_lsys_krn_sys_fork.endpt has forked.
30433d6423SLionel Sambuc * The child is m_lsys_krn_sys_fork.slot.
31433d6423SLionel Sambuc */
32433d6423SLionel Sambuc #if defined(__i386__)
33433d6423SLionel Sambuc char *old_fpu_save_area_p;
34433d6423SLionel Sambuc #endif
35433d6423SLionel Sambuc register struct proc *rpc; /* child process pointer */
36433d6423SLionel Sambuc struct proc *rpp; /* parent process pointer */
37433d6423SLionel Sambuc int gen;
38433d6423SLionel Sambuc int p_proc;
39433d6423SLionel Sambuc int namelen;
40433d6423SLionel Sambuc
41433d6423SLionel Sambuc if(!isokendpt(m_ptr->m_lsys_krn_sys_fork.endpt, &p_proc))
42433d6423SLionel Sambuc return EINVAL;
43433d6423SLionel Sambuc
44433d6423SLionel Sambuc rpp = proc_addr(p_proc);
45433d6423SLionel Sambuc rpc = proc_addr(m_ptr->m_lsys_krn_sys_fork.slot);
46433d6423SLionel Sambuc if (isemptyp(rpp) || ! isemptyp(rpc)) return(EINVAL);
47433d6423SLionel Sambuc
48433d6423SLionel Sambuc assert(!(rpp->p_misc_flags & MF_DELIVERMSG));
49433d6423SLionel Sambuc
50433d6423SLionel Sambuc /* needs to be receiving so we know where the message buffer is */
51433d6423SLionel Sambuc if(!RTS_ISSET(rpp, RTS_RECEIVING)) {
52433d6423SLionel Sambuc printf("kernel: fork not done synchronously?\n");
53433d6423SLionel Sambuc return EINVAL;
54433d6423SLionel Sambuc }
55433d6423SLionel Sambuc
56433d6423SLionel Sambuc /* make sure that the FPU context is saved in parent before copy */
57433d6423SLionel Sambuc save_fpu(rpp);
58433d6423SLionel Sambuc /* Copy parent 'proc' struct to child. And reinitialize some fields. */
59433d6423SLionel Sambuc gen = _ENDPOINT_G(rpc->p_endpoint);
60433d6423SLionel Sambuc #if defined(__i386__)
61433d6423SLionel Sambuc old_fpu_save_area_p = rpc->p_seg.fpu_state;
62433d6423SLionel Sambuc #endif
63433d6423SLionel Sambuc *rpc = *rpp; /* copy 'proc' struct */
64433d6423SLionel Sambuc #if defined(__i386__)
65433d6423SLionel Sambuc rpc->p_seg.fpu_state = old_fpu_save_area_p;
66433d6423SLionel Sambuc if(proc_used_fpu(rpp))
67433d6423SLionel Sambuc memcpy(rpc->p_seg.fpu_state, rpp->p_seg.fpu_state, FPU_XFP_SIZE);
68433d6423SLionel Sambuc #endif
69433d6423SLionel Sambuc if(++gen >= _ENDPOINT_MAX_GENERATION) /* increase generation */
70433d6423SLionel Sambuc gen = 1; /* generation number wraparound */
71433d6423SLionel Sambuc rpc->p_nr = m_ptr->m_lsys_krn_sys_fork.slot; /* this was obliterated by copy */
72433d6423SLionel Sambuc rpc->p_endpoint = _ENDPOINT(gen, rpc->p_nr); /* new endpoint of slot */
73433d6423SLionel Sambuc
74433d6423SLionel Sambuc rpc->p_reg.retreg = 0; /* child sees pid = 0 to know it is child */
75433d6423SLionel Sambuc rpc->p_user_time = 0; /* set all the accounting times to 0 */
76433d6423SLionel Sambuc rpc->p_sys_time = 0;
77433d6423SLionel Sambuc
78433d6423SLionel Sambuc rpc->p_misc_flags &=
79433d6423SLionel Sambuc ~(MF_VIRT_TIMER | MF_PROF_TIMER | MF_SC_TRACE | MF_SPROF_SEEN | MF_STEP);
80433d6423SLionel Sambuc rpc->p_virt_left = 0; /* disable, clear the process-virtual timers */
81433d6423SLionel Sambuc rpc->p_prof_left = 0;
82433d6423SLionel Sambuc
83433d6423SLionel Sambuc /* Mark process name as being a forked copy */
84433d6423SLionel Sambuc namelen = strlen(rpc->p_name);
85433d6423SLionel Sambuc #define FORKSTR "*F"
86433d6423SLionel Sambuc if(namelen+strlen(FORKSTR) < sizeof(rpc->p_name))
87433d6423SLionel Sambuc strcat(rpc->p_name, FORKSTR);
88433d6423SLionel Sambuc
89433d6423SLionel Sambuc /* the child process is not runnable until it's scheduled. */
90433d6423SLionel Sambuc RTS_SET(rpc, RTS_NO_QUANTUM);
91433d6423SLionel Sambuc reset_proc_accounting(rpc);
92433d6423SLionel Sambuc
93433d6423SLionel Sambuc rpc->p_cpu_time_left = 0;
94433d6423SLionel Sambuc rpc->p_cycles = 0;
95433d6423SLionel Sambuc rpc->p_kcall_cycles = 0;
96433d6423SLionel Sambuc rpc->p_kipc_cycles = 0;
97433d6423SLionel Sambuc
98*1f3ef2b2SDavid van Moolenbroek rpc->p_tick_cycles = 0;
99*1f3ef2b2SDavid van Moolenbroek cpuavg_init(&rpc->p_cpuavg);
100*1f3ef2b2SDavid van Moolenbroek
101433d6423SLionel Sambuc /* If the parent is a privileged process, take away the privileges from the
102433d6423SLionel Sambuc * child process and inhibit it from running by setting the NO_PRIV flag.
103433d6423SLionel Sambuc * The caller should explicitly set the new privileges before executing.
104433d6423SLionel Sambuc */
105433d6423SLionel Sambuc if (priv(rpp)->s_flags & SYS_PROC) {
106433d6423SLionel Sambuc rpc->p_priv = priv_addr(USER_PRIV_ID);
107433d6423SLionel Sambuc rpc->p_rts_flags |= RTS_NO_PRIV;
108433d6423SLionel Sambuc }
109433d6423SLionel Sambuc
110433d6423SLionel Sambuc /* Calculate endpoint identifier, so caller knows what it is. */
111433d6423SLionel Sambuc m_ptr->m_krn_lsys_sys_fork.endpt = rpc->p_endpoint;
112433d6423SLionel Sambuc m_ptr->m_krn_lsys_sys_fork.msgaddr = rpp->p_delivermsg_vir;
113433d6423SLionel Sambuc
114433d6423SLionel Sambuc /* Don't schedule process in VM mode until it has a new pagetable. */
115433d6423SLionel Sambuc if(m_ptr->m_lsys_krn_sys_fork.flags & PFF_VMINHIBIT) {
116433d6423SLionel Sambuc RTS_SET(rpc, RTS_VMINHIBIT);
117433d6423SLionel Sambuc }
118433d6423SLionel Sambuc
119433d6423SLionel Sambuc /*
120433d6423SLionel Sambuc * Only one in group should have RTS_SIGNALED, child doesn't inherit tracing.
121433d6423SLionel Sambuc */
122433d6423SLionel Sambuc RTS_UNSET(rpc, (RTS_SIGNALED | RTS_SIG_PENDING | RTS_P_STOP));
123433d6423SLionel Sambuc (void) sigemptyset(&rpc->p_pending);
124433d6423SLionel Sambuc
125433d6423SLionel Sambuc #if defined(__i386__)
126433d6423SLionel Sambuc rpc->p_seg.p_cr3 = 0;
127433d6423SLionel Sambuc rpc->p_seg.p_cr3_v = NULL;
128433d6423SLionel Sambuc #elif defined(__arm__)
129433d6423SLionel Sambuc rpc->p_seg.p_ttbr = 0;
130433d6423SLionel Sambuc rpc->p_seg.p_ttbr_v = NULL;
131433d6423SLionel Sambuc #endif
132433d6423SLionel Sambuc
133433d6423SLionel Sambuc return OK;
134433d6423SLionel Sambuc }
135433d6423SLionel Sambuc
136433d6423SLionel Sambuc #endif /* USE_FORK */
137