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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [kernel/] [fork.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1634 jcastillo
/*
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
 
310
        p = (struct task_struct *) kmalloc(sizeof(*p), GFP_KERNEL);
311
        if (!p)
312
                goto bad_fork;
313
 
314
        new_stack = alloc_kernel_stack();
315
        if (!new_stack)
316
                goto bad_fork_free_p;
317
 
318
        error = -EAGAIN;
319
        nr = find_empty_process();
320
        if (nr < 0)
321
                goto bad_fork_free_stack;
322
 
323
        *p = *current;
324
 
325
        if (p->exec_domain && p->exec_domain->use_count)
326
                (*p->exec_domain->use_count)++;
327
        if (p->binfmt && p->binfmt->use_count)
328
                (*p->binfmt->use_count)++;
329
 
330
        p->did_exec = 0;
331
        p->swappable = 0;
332
        p->kernel_stack_page = new_stack;
333
        *(unsigned long *) p->kernel_stack_page = STACK_MAGIC;
334
        for(i=1;i<(PAGE_SIZE/sizeof(long));i++)
335
                ((unsigned long*)p->kernel_stack_page)[i] = STACK_UNTOUCHED_MAGIC;
336
        p->state = TASK_UNINTERRUPTIBLE;
337
        p->flags &= ~(PF_PTRACED|PF_TRACESYS|PF_SUPERPRIV);
338
        p->flags |= PF_FORKNOEXEC;
339
        p->pid = get_pid(clone_flags);
340
        p->next_run = NULL;
341
        p->prev_run = NULL;
342
        p->p_pptr = p->p_opptr = current;
343
        p->p_cptr = NULL;
344
        init_waitqueue(&p->wait_chldexit);
345
        p->signal = 0;
346
        p->it_real_value = p->it_virt_value = p->it_prof_value = 0;
347
        p->it_real_incr = p->it_virt_incr = p->it_prof_incr = 0;
348
        init_timer(&p->real_timer);
349
        p->real_timer.data = (unsigned long) p;
350
        p->leader = 0;           /* session leadership doesn't inherit */
351
        p->tty_old_pgrp = 0;
352
        p->utime = p->stime = 0;
353
        p->cutime = p->cstime = 0;
354
#ifdef __SMP__
355
        p->processor = NO_PROC_ID;
356
        p->lock_depth = 1;
357
#endif
358
        p->start_time = jiffies;
359
        task[nr] = p;
360
        SET_LINKS(p);
361
        nr_tasks++;
362
 
363
        error = -ENOMEM;
364
        /* copy all the process information */
365
        if (copy_files(clone_flags, p))
366
                goto bad_fork_cleanup;
367
 
368
        if (copy_fs(clone_flags, p))
369
                goto bad_fork_cleanup_files;
370
 
371
        if (copy_sighand(clone_flags, p))
372
                goto bad_fork_cleanup_fs;
373
 
374
        if (copy_mm(clone_flags, p))
375
                goto bad_fork_cleanup_sighand;
376
 
377
        copy_thread(nr, clone_flags, usp, p, regs);
378
        p->semundo = NULL;
379
 
380
        /* ok, now we should be set up.. */
381
        p->swappable = 1;
382
        p->exit_signal = clone_flags & CSIGNAL;
383
        p->counter = (current->counter >>= 1);
384
        wake_up_process(p);                     /* do this last, just in case */
385
        ++total_forks;
386
#ifdef NO_MM    
387
        if (clone_flags & CLONE_WAIT) {
388
                sleep_on(&current->mm->vforkwait);
389
        }
390
#endif /*NO_MM*/
391
 
392
        return p->pid;
393
 
394
bad_fork_cleanup_sighand:
395
        exit_sighand(p);
396
bad_fork_cleanup_fs:
397
        exit_fs(p);
398
bad_fork_cleanup_files:
399
        exit_files(p);
400
bad_fork_cleanup:
401
        if (p->exec_domain && p->exec_domain->use_count)
402
                (*p->exec_domain->use_count)--;
403
        if (p->binfmt && p->binfmt->use_count)
404
                (*p->binfmt->use_count)--;
405
        task[nr] = NULL;
406
        REMOVE_LINKS(p);
407
        nr_tasks--;
408
bad_fork_free_stack:
409
        free_kernel_stack(new_stack);
410
bad_fork_free_p:
411
        kfree(p);
412
bad_fork:
413
        return error;
414
}

powered by: WebSVN 2.1.0

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