1 /* The kernel call implemented in this file:
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)
12 #include "kernel/system.h"
13 #include "kernel/vm.h"
18 #include <minix/endpoint.h>
19 #include <minix/u64.h>
23 /*===========================================================================*
25 *===========================================================================*/
26 int do_fork(struct proc
* caller
, message
* m_ptr
)
29 * m_lsys_krn_sys_fork.endpt has forked.
30 * The child is m_lsys_krn_sys_fork.slot.
33 char *old_fpu_save_area_p
;
35 register struct proc
*rpc
; /* child process pointer */
36 struct proc
*rpp
; /* parent process pointer */
41 if(!isokendpt(m_ptr
->m_lsys_krn_sys_fork
.endpt
, &p_proc
))
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
);
48 assert(!(rpp
->p_misc_flags
& MF_DELIVERMSG
));
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");
56 /* make sure that the FPU context is saved in parent before copy */
58 /* Copy parent 'proc' struct to child. And reinitialize some fields. */
59 gen
= _ENDPOINT_G(rpc
->p_endpoint
);
61 old_fpu_save_area_p
= rpc
->p_seg
.fpu_state
;
63 *rpc
= *rpp
; /* copy 'proc' struct */
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
);
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 */
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 */
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 */
83 /* Mark process name as being a forked copy */
84 namelen
= strlen(rpc
->p_name
);
86 if(namelen
+strlen(FORKSTR
) < sizeof(rpc
->p_name
))
87 strcat(rpc
->p_name
, FORKSTR
);
89 /* the child process is not runnable until it's scheduled. */
90 RTS_SET(rpc
, RTS_NO_QUANTUM
);
91 reset_proc_accounting(rpc
);
93 rpc
->p_cpu_time_left
= 0;
95 rpc
->p_kcall_cycles
= 0;
96 rpc
->p_kipc_cycles
= 0;
98 rpc
->p_tick_cycles
= 0;
99 cpuavg_init(&rpc
->p_cpuavg
);
101 /* If the parent is a privileged process, take away the privileges from the
102 * child process and inhibit it from running by setting the NO_PRIV flag.
103 * The caller should explicitly set the new privileges before executing.
105 if (priv(rpp
)->s_flags
& SYS_PROC
) {
106 rpc
->p_priv
= priv_addr(USER_PRIV_ID
);
107 rpc
->p_rts_flags
|= RTS_NO_PRIV
;
110 /* Calculate endpoint identifier, so caller knows what it is. */
111 m_ptr
->m_krn_lsys_sys_fork
.endpt
= rpc
->p_endpoint
;
112 m_ptr
->m_krn_lsys_sys_fork
.msgaddr
= rpp
->p_delivermsg_vir
;
114 /* Don't schedule process in VM mode until it has a new pagetable. */
115 if(m_ptr
->m_lsys_krn_sys_fork
.flags
& PFF_VMINHIBIT
) {
116 RTS_SET(rpc
, RTS_VMINHIBIT
);
120 * Only one in group should have RTS_SIGNALED, child doesn't inherit tracing.
122 RTS_UNSET(rpc
, (RTS_SIGNALED
| RTS_SIG_PENDING
| RTS_P_STOP
));
123 (void) sigemptyset(&rpc
->p_pending
);
125 #if defined(__i386__)
126 rpc
->p_seg
.p_cr3
= 0;
127 rpc
->p_seg
.p_cr3_v
= NULL
;
128 #elif defined(__arm__)
129 rpc
->p_seg
.p_ttbr
= 0;
130 rpc
->p_seg
.p_ttbr_v
= NULL
;
136 #endif /* USE_FORK */