OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [kernel/] [fork.c] - Blame information for rev 199

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 *  linux/kernel/fork.c
3
 *
4
 *  Copyright (C) 1991, 1992  Linus Torvalds
5
 */
6
 
7
/*
8
 *  'fork.c' contains the help-routines for the 'fork' system call
9
 * (see also system_call.s).
10
 * Fork is rather simple, once you get the hang of it, but the memory
11
 * management can be a bitch. See 'mm/mm.c': 'copy_page_tables()'
12
 */
13
 
14
/*
15
 * uClinux revisions for NO_MM
16
 * Copyright (C) 1998  Kenneth Albanowski <kjahds@kjahds.com>,
17
 *                     The Silver Hammer Group, Ltd.
18
 */
19
 
20
#include <linux/errno.h>
21
#include <linux/sched.h>
22
#include <linux/kernel.h>
23
#include <linux/mm.h>
24
#include <linux/stddef.h>
25
#include <linux/unistd.h>
26
#include <linux/ptrace.h>
27
#include <linux/malloc.h>
28
#include <linux/ldt.h>
29
#include <linux/smp.h>
30
 
31
#include <asm/segment.h>
32
#include <asm/system.h>
33
#include <asm/pgtable.h>
34
 
35
int nr_tasks=1;
36
int nr_running=1;
37
unsigned long int total_forks=0; /* Handle normal Linux uptimes. */
38
int last_pid=0;
39
 
40
static inline int find_empty_process(void)
41
{
42
        int i;
43
 
44
        if (nr_tasks >= NR_TASKS - MIN_TASKS_LEFT_FOR_ROOT) {
45
                if (current->uid)
46
                        return -EAGAIN;
47
        }
48
        if (current->uid) {
49
                long max_tasks = current->rlim[RLIMIT_NPROC].rlim_cur;
50
 
51
                max_tasks--;    /* count the new process.. */
52
                if (max_tasks < nr_tasks) {
53
                        struct task_struct *p;
54
                        for_each_task (p) {
55
                                if (p->uid == current->uid)
56
                                        if (--max_tasks < 0)
57
                                                return -EAGAIN;
58
                        }
59
                }
60
        }
61
        for (i = 0 ; i < NR_TASKS ; i++) {
62
                if (!task[i])
63
                        return i;
64
        }
65
        return -EAGAIN;
66
}
67
 
68
static int get_pid(unsigned long flags)
69
{
70
        struct task_struct *p;
71
 
72
        if (flags & CLONE_PID)
73
                return current->pid;
74
repeat:
75
        if ((++last_pid) & 0xffff8000)
76
                last_pid=1;
77
        for_each_task (p) {
78
                if (p->pid == last_pid ||
79
                    p->pgrp == last_pid ||
80
                    p->session == last_pid)
81
                        goto repeat;
82
        }
83
        return last_pid;
84
}
85
 
86
#ifndef NO_MM
87
 
88
static inline int dup_mmap(struct mm_struct * mm)
89
{
90
        struct vm_area_struct * mpnt, **p, *tmp;
91
 
92
        mm->mmap = NULL;
93
        p = &mm->mmap;
94
        for (mpnt = current->mm->mmap ; mpnt ; mpnt = mpnt->vm_next) {
95
                tmp = (struct vm_area_struct *) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
96
                if (!tmp) {
97
                        /* exit_mmap is called by the caller */
98
                        return -ENOMEM;
99
                }
100
                *tmp = *mpnt;
101
                tmp->vm_flags &= ~VM_LOCKED;
102
                tmp->vm_mm = mm;
103
                tmp->vm_next = NULL;
104
                if (tmp->vm_inode) {
105
                        tmp->vm_inode->i_count++;
106
                        /* insert tmp into the share list, just after mpnt */
107
                        tmp->vm_next_share->vm_prev_share = tmp;
108
                        mpnt->vm_next_share = tmp;
109
                        tmp->vm_prev_share = mpnt;
110
                }
111
                if (copy_page_range(mm, current->mm, tmp)) {
112
                        /* link into the linked list for exit_mmap */
113
                        *p = tmp;
114
                        p = &tmp->vm_next;
115
                        /* exit_mmap is called by the caller */
116
                        return -ENOMEM;
117
                }
118
                if (tmp->vm_ops && tmp->vm_ops->open)
119
                        tmp->vm_ops->open(tmp);
120
                *p = tmp;
121
                p = &tmp->vm_next;
122
        }
123
        build_mmap_avl(mm);
124
        flush_tlb_mm(current->mm);
125
        return 0;
126
}
127
 
128
static inline int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
129
{
130
        if (!(clone_flags & CLONE_VM)) {
131
                struct mm_struct * mm = kmalloc(sizeof(*tsk->mm), GFP_KERNEL);
132
                if (!mm)
133
                        return -ENOMEM;
134
                *mm = *current->mm;
135
                mm->count = 1;
136
                mm->def_flags = 0;
137
                mm->mmap_sem = MUTEX;
138
                tsk->mm = mm;
139
                tsk->min_flt = tsk->maj_flt = 0;
140
                tsk->cmin_flt = tsk->cmaj_flt = 0;
141
                tsk->nswap = tsk->cnswap = 0;
142
                if (new_page_tables(tsk)) {
143
                        tsk->mm = NULL;
144
                        exit_mmap(mm);
145
                        goto free_mm;
146
                }
147
                down(&mm->mmap_sem);
148
                if (dup_mmap(mm)) {
149
                        up(&mm->mmap_sem);
150
                        tsk->mm = NULL;
151
                        exit_mmap(mm);
152
                        free_page_tables(mm);
153
free_mm:
154
                        kfree(mm);
155
                        return -ENOMEM;
156
                }
157
                up(&mm->mmap_sem);
158
                return 0;
159
        }
160
        SET_PAGE_DIR(tsk, current->mm->pgd);
161
        current->mm->count++;
162
        return 0;
163
}
164
#else /* NO_MM */
165
 
166
static inline int dup_mmap(struct mm_struct * mm)
167
{
168
        struct mm_tblock_struct * tmp = &current->mm->tblock;
169
        struct mm_tblock_struct * newtmp = &mm->tblock;
170
        /*unsigned long flags;*/
171
        extern long realalloc, askedalloc;
172
 
173
        if (!mm)
174
                return -1;
175
 
176
        mm->tblock.rblock = 0;
177
        mm->tblock.next = 0;
178
 
179
        /*save_flags(flags); cli();*/
180
 
181
        while((tmp = tmp->next)) {
182
                newtmp->next = kmalloc(sizeof(struct mm_tblock_struct), GFP_KERNEL);
183
                if (!newtmp->next) {
184
                        /*restore_flags(flags);*/
185
                        return -ENOMEM;
186
                }
187
                realalloc += ksize(newtmp->next);
188
                askedalloc += sizeof(struct mm_tblock_struct);
189
                newtmp->next->rblock = tmp->rblock;
190
                if (tmp->rblock)
191
                        tmp->rblock->refcount++;
192
                newtmp->next->next = 0;
193
                newtmp = newtmp->next;
194
        }
195
        /*restore_flags(flags);*/
196
 
197
        return 0;
198
 
199
}
200
 
201
static inline int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
202
{
203
        if (!(clone_flags & CLONE_VM)) {
204
                struct mm_struct * mm = kmalloc(sizeof(*tsk->mm), GFP_KERNEL);
205
                if (!mm)
206
                        return -ENOMEM;
207
                *mm = *current->mm;
208
                mm->count = 1;
209
                mm->def_flags = 0;
210
                mm->vforkwait = 0;
211
                tsk->mm = mm;
212
                if (tsk->mm->executable)
213
                        tsk->mm->executable->i_count++;
214
                tsk->min_flt = tsk->maj_flt = 0;
215
                tsk->cmin_flt = tsk->cmaj_flt = 0;
216
                tsk->nswap = tsk->cnswap = 0;
217
                if (dup_mmap(mm)) {
218
                        tsk->mm = NULL;
219
                        exit_mmap(mm);
220
                        kfree(mm);
221
                        return -ENOMEM;
222
                }
223
                return 0;
224
        }
225
        current->mm->count++;
226
        return 0;
227
}
228
 
229
#endif /* NO_MM */
230
 
231
static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
232
{
233
        if (clone_flags & CLONE_FS) {
234
                current->fs->count++;
235
                return 0;
236
        }
237
        tsk->fs = kmalloc(sizeof(*tsk->fs), GFP_KERNEL);
238
        if (!tsk->fs)
239
                return -1;
240
        tsk->fs->count = 1;
241
        tsk->fs->umask = current->fs->umask;
242
        if ((tsk->fs->root = current->fs->root))
243
                tsk->fs->root->i_count++;
244
        if ((tsk->fs->pwd = current->fs->pwd))
245
                tsk->fs->pwd->i_count++;
246
        return 0;
247
}
248
 
249
static inline int copy_files(unsigned long clone_flags, struct task_struct * tsk)
250
{
251
        int i;
252
        struct files_struct *oldf, *newf;
253
        struct file **old_fds, **new_fds;
254
 
255
        oldf = current->files;
256
        if (clone_flags & CLONE_FILES) {
257
                oldf->count++;
258
                return 0;
259
        }
260
 
261
        newf = kmalloc(sizeof(*newf), GFP_KERNEL);
262
        tsk->files = newf;
263
        if (!newf)
264
                return -1;
265
 
266
        newf->count = 1;
267
        newf->close_on_exec = oldf->close_on_exec;
268
        newf->open_fds = oldf->open_fds;
269
 
270
        old_fds = oldf->fd;
271
        new_fds = newf->fd;
272
        for (i = NR_OPEN; i != 0; i--) {
273
                struct file * f = *old_fds;
274
                old_fds++;
275
                *new_fds = f;
276
                new_fds++;
277
                if (f)
278
                        f->f_count++;
279
        }
280
        return 0;
281
}
282
 
283
static inline int copy_sighand(unsigned long clone_flags, struct task_struct * tsk)
284
{
285
        if (clone_flags & CLONE_SIGHAND) {
286
                current->sig->count++;
287
                return 0;
288
        }
289
        tsk->sig = kmalloc(sizeof(*tsk->sig), GFP_KERNEL);
290
        if (!tsk->sig)
291
                return -1;
292
        tsk->sig->count = 1;
293
        memcpy(tsk->sig->action, current->sig->action, sizeof(tsk->sig->action));
294
        return 0;
295
}
296
 
297
/*
298
 *  Ok, this is the main fork-routine. It copies the system process
299
 * information (task[nr]) and sets up the necessary registers. It
300
 * also copies the data segment in its entirety.
301
 */
302
int do_fork(unsigned long clone_flags, unsigned long usp, struct pt_regs *regs)
303
{
304
        int nr;
305
        int i;
306
        int error = -ENOMEM;
307
        unsigned long new_stack;
308
        struct task_struct *p;
309
printk("do_fork1\n");
310
        p = (struct task_struct *) kmalloc(sizeof(*p), GFP_KERNEL);
311
        if (!p)
312
                goto bad_fork;
313
printk("do_fork2\n");
314
        new_stack = alloc_kernel_stack();
315
        if (!new_stack)
316
                goto bad_fork_free_p;
317
printk("do_fork3\n");
318
        error = -EAGAIN;
319
        nr = find_empty_process();
320
        if (nr < 0)
321
                goto bad_fork_free_stack;
322
 
323
printk("do_fork4\n");
324
        *p = *current;
325
 
326
        if (p->exec_domain && p->exec_domain->use_count)
327
                (*p->exec_domain->use_count)++;
328
        if (p->binfmt && p->binfmt->use_count)
329
                (*p->binfmt->use_count)++;
330
 
331
        p->did_exec = 0;
332
        p->swappable = 0;
333
        p->kernel_stack_page = new_stack;
334
        *(unsigned long *) p->kernel_stack_page = STACK_MAGIC;
335
        for(i=1;i<(PAGE_SIZE/sizeof(long));i++)
336
                ((unsigned long*)p->kernel_stack_page)[i] = STACK_UNTOUCHED_MAGIC;
337
        p->state = TASK_UNINTERRUPTIBLE;
338
        p->flags &= ~(PF_PTRACED|PF_TRACESYS|PF_SUPERPRIV);
339
        p->flags |= PF_FORKNOEXEC;
340
        p->pid = get_pid(clone_flags);
341
        p->next_run = NULL;
342
        p->prev_run = NULL;
343
        p->p_pptr = p->p_opptr = current;
344
        p->p_cptr = NULL;
345
        init_waitqueue(&p->wait_chldexit);
346
        p->signal = 0;
347
        p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
348
        p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
349
        init_timer(&p->real_timer);
350
        p->real_timer.data = (unsigned long) p;
351
        p->leader = 0;           /* session leadership doesn't inherit */
352
        p->tty_old_pgrp = 0;
353
        p->utime = p->stime = 0;
354
        p->cutime = p->cstime = 0;
355
#ifdef __SMP__
356
        p->processor = NO_PROC_ID;
357
        p->lock_depth = 1;
358
#endif
359
        p->start_time = jiffies;
360
        task[nr] = p;
361
        SET_LINKS(p);
362
        nr_tasks++;
363
 
364
        error = -ENOMEM;
365
        /* copy all the process information */
366
        if (copy_files(clone_flags, p))
367
                goto bad_fork_cleanup;
368
printk("do_fork5\n");
369
        if (copy_fs(clone_flags, p))
370
                goto bad_fork_cleanup_files;
371
printk("do_fork6\n");
372
        if (copy_sighand(clone_flags, p))
373
                goto bad_fork_cleanup_fs;
374
printk("do_fork7\n");
375
        if (copy_mm(clone_flags, p))
376
                goto bad_fork_cleanup_sighand;
377
printk("do_fork8\n");
378
        copy_thread(nr, clone_flags, usp, p, regs);
379
        p->semundo = NULL;
380
 
381
        /* ok, now we should be set up.. */
382
        p->swappable = 1;
383
        p->exit_signal = clone_flags & CSIGNAL;
384
        p->counter = (current->counter >>= 1);
385
        wake_up_process(p);                     /* do this last, just in case */
386
        ++total_forks;
387
#ifdef NO_MM    
388
        if (clone_flags & CLONE_WAIT) {
389
                sleep_on(&current->mm->vforkwait);
390
        }
391
#endif /*NO_MM*/
392
printk("do_fork9\n");
393
        return p->pid;
394
 
395
bad_fork_cleanup_sighand:
396
        exit_sighand(p);
397
bad_fork_cleanup_fs:
398
        exit_fs(p);
399
bad_fork_cleanup_files:
400
        exit_files(p);
401
bad_fork_cleanup:
402
        if (p->exec_domain && p->exec_domain->use_count)
403
                (*p->exec_domain->use_count)--;
404
        if (p->binfmt && p->binfmt->use_count)
405
                (*p->binfmt->use_count)--;
406
        task[nr] = NULL;
407
        REMOVE_LINKS(p);
408
        nr_tasks--;
409
bad_fork_free_stack:
410
        free_kernel_stack(new_stack);
411
bad_fork_free_p:
412
        kfree(p);
413
bad_fork:
414
        return error;
415
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.