mkfs: move directory entry manipulation
[minix.git] / kernel / system / do_fork.c
blobdfad1db37a25178dc6acfffbf2a3e48d15e1592f
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>
18 #include <minix/u64.h>
20 #if USE_FORK
22 /*===========================================================================*
23 * do_fork *
24 *===========================================================================*/
25 int do_fork(struct proc * caller, message * m_ptr)
27 /* Handle sys_fork(). PR_ENDPT has forked. The child is PR_SLOT. */
28 #if defined(__i386__)
29 char *old_fpu_save_area_p;
30 #endif
31 register struct proc *rpc; /* child process pointer */
32 struct proc *rpp; /* parent process pointer */
33 int gen;
34 int p_proc;
35 int namelen;
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 /* make sure that the FPU context is saved in parent before copy */
53 save_fpu(rpp);
54 /* Copy parent 'proc' struct to child. And reinitialize some fields. */
55 gen = _ENDPOINT_G(rpc->p_endpoint);
56 #if defined(__i386__)
57 old_fpu_save_area_p = rpc->p_seg.fpu_state;
58 #endif
59 *rpc = *rpp; /* copy 'proc' struct */
60 #if defined(__i386__)
61 rpc->p_seg.fpu_state = old_fpu_save_area_p;
62 if(proc_used_fpu(rpp))
63 memcpy(rpc->p_seg.fpu_state, rpp->p_seg.fpu_state, FPU_XFP_SIZE);
64 #endif
65 if(++gen >= _ENDPOINT_MAX_GENERATION) /* increase generation */
66 gen = 1; /* generation number wraparound */
67 rpc->p_nr = m_ptr->PR_SLOT; /* this was obliterated by copy */
68 rpc->p_endpoint = _ENDPOINT(gen, rpc->p_nr); /* new endpoint of slot */
70 rpc->p_reg.retreg = 0; /* child sees pid = 0 to know it is child */
71 rpc->p_user_time = 0; /* set all the accounting times to 0 */
72 rpc->p_sys_time = 0;
74 #if defined(__i386__)
75 rpc->p_reg.psw &= ~TRACEBIT; /* clear trace bit */
76 #endif
77 rpc->p_misc_flags &=
78 ~(MF_VIRT_TIMER | MF_PROF_TIMER | MF_SC_TRACE | MF_SPROF_SEEN);
79 rpc->p_virt_left = 0; /* disable, clear the process-virtual timers */
80 rpc->p_prof_left = 0;
82 /* Mark process name as being a forked copy */
83 namelen = strlen(rpc->p_name);
84 #define FORKSTR "*F"
85 if(namelen+strlen(FORKSTR) < sizeof(rpc->p_name))
86 strcat(rpc->p_name, FORKSTR);
88 /* the child process is not runnable until it's scheduled. */
89 RTS_SET(rpc, RTS_NO_QUANTUM);
90 reset_proc_accounting(rpc);
92 make_zero64(rpc->p_cpu_time_left);
93 make_zero64(rpc->p_cycles);
94 make_zero64(rpc->p_kcall_cycles);
95 make_zero64(rpc->p_kipc_cycles);
97 /* If the parent is a privileged process, take away the privileges from the
98 * child process and inhibit it from running by setting the NO_PRIV flag.
99 * The caller should explicitely set the new privileges before executing.
101 if (priv(rpp)->s_flags & SYS_PROC) {
102 rpc->p_priv = priv_addr(USER_PRIV_ID);
103 rpc->p_rts_flags |= RTS_NO_PRIV;
106 /* Calculate endpoint identifier, so caller knows what it is. */
107 m_ptr->PR_ENDPT = rpc->p_endpoint;
108 m_ptr->PR_FORK_MSGADDR = (char *) rpp->p_delivermsg_vir;
110 /* Don't schedule process in VM mode until it has a new pagetable. */
111 if(m_ptr->PR_FORK_FLAGS & PFF_VMINHIBIT) {
112 RTS_SET(rpc, RTS_VMINHIBIT);
116 * Only one in group should have RTS_SIGNALED, child doesn't inherit tracing.
118 RTS_UNSET(rpc, (RTS_SIGNALED | RTS_SIG_PENDING | RTS_P_STOP));
119 (void) sigemptyset(&rpc->p_pending);
121 #if defined(__i386__)
122 rpc->p_seg.p_cr3 = 0;
123 rpc->p_seg.p_cr3_v = NULL;
124 #elif defined(__arm__)
125 rpc->p_seg.p_ttbr = 0;
126 rpc->p_seg.p_ttbr_v = NULL;
127 #endif
129 return OK;
132 #endif /* USE_FORK */