root/include/linux/sched.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. suser
  2. file_from_fd
  3. add_wait_queue
  4. remove_wait_queue
  5. select_wait
  6. down
  7. up

   1 #ifndef _LINUX_SCHED_H
   2 #define _LINUX_SCHED_H
   3 
   4 /*
   5  * define DEBUG if you want the wait-queues to have some extra
   6  * debugging code. It's not normally used, but might catch some
   7  * wait-queue coding errors.
   8  *
   9  *  #define DEBUG
  10  */
  11 
  12 #include <asm/param.h>  /* for HZ */
  13 
  14 extern unsigned long intr_count;
  15 extern unsigned long event;
  16 
  17 #include <linux/binfmts.h>
  18 #include <linux/personality.h>
  19 #include <linux/tasks.h>
  20 #include <linux/kernel.h>
  21 #include <asm/system.h>
  22 #include <asm/page.h>
  23 
  24 #include <linux/smp.h>
  25 #include <linux/tty.h>
  26 #include <linux/sem.h>
  27 
  28 /*
  29  * cloning flags:
  30  */
  31 #define CSIGNAL         0x000000ff      /* signal mask to be sent at exit */
  32 #define CLONE_VM        0x00000100      /* set if VM shared between processes */
  33 #define CLONE_FS        0x00000200      /* set if fs info shared between processes */
  34 #define CLONE_FILES     0x00000400      /* set if open files shared between processes */
  35 #define CLONE_SIGHAND   0x00000800      /* set if signal handlers shared */
  36 #define CLONE_PID       0x00001000      /* set if pid shared */
  37 
  38 /*
  39  * These are the constant used to fake the fixed-point load-average
  40  * counting. Some notes:
  41  *  - 11 bit fractions expand to 22 bits by the multiplies: this gives
  42  *    a load-average precision of 10 bits integer + 11 bits fractional
  43  *  - if you want to count load-averages more often, you need more
  44  *    precision, or rounding will get you. With 2-second counting freq,
  45  *    the EXP_n values would be 1981, 2034 and 2043 if still using only
  46  *    11 bit fractions.
  47  */
  48 extern unsigned long avenrun[];         /* Load averages */
  49 
  50 #define FSHIFT          11              /* nr of bits of precision */
  51 #define FIXED_1         (1<<FSHIFT)     /* 1.0 as fixed-point */
  52 #define LOAD_FREQ       (5*HZ)          /* 5 sec intervals */
  53 #define EXP_1           1884            /* 1/exp(5sec/1min) as fixed-point */
  54 #define EXP_5           2014            /* 1/exp(5sec/5min) */
  55 #define EXP_15          2037            /* 1/exp(5sec/15min) */
  56 
  57 #define CALC_LOAD(load,exp,n) \
  58         load *= exp; \
  59         load += n*(FIXED_1-exp); \
  60         load >>= FSHIFT;
  61 
  62 #define CT_TO_SECS(x)   ((x) / HZ)
  63 #define CT_TO_USECS(x)  (((x) % HZ) * 1000000/HZ)
  64 
  65 extern int nr_running, nr_tasks;
  66 
  67 #define FIRST_TASK task[0]
  68 #define LAST_TASK task[NR_TASKS-1]
  69 
  70 #include <linux/head.h>
  71 #include <linux/fs.h>
  72 #include <linux/signal.h>
  73 #include <linux/time.h>
  74 #include <linux/param.h>
  75 #include <linux/resource.h>
  76 #include <linux/vm86.h>
  77 #include <linux/math_emu.h>
  78 #include <linux/ptrace.h>
  79 #include <linux/timer.h>
  80 
  81 #include <asm/processor.h>
  82 
  83 #define TASK_RUNNING            0
  84 #define TASK_INTERRUPTIBLE      1
  85 #define TASK_UNINTERRUPTIBLE    2
  86 #define TASK_ZOMBIE             3
  87 #define TASK_STOPPED            4
  88 #define TASK_SWAPPING           5
  89 
  90 /*
  91  * Scheduling policies
  92  */
  93 #define SCHED_OTHER             0
  94 #define SCHED_FIFO              1
  95 #define SCHED_RR                2
  96 
  97 struct sched_param {
  98         int sched_priority;
  99 };
 100 
 101 #ifndef NULL
 102 #define NULL ((void *) 0)
 103 #endif
 104 
 105 #ifdef __KERNEL__
 106 
 107 extern void sched_init(void);
 108 extern void show_state(void);
 109 extern void trap_init(void);
 110 
 111 asmlinkage void schedule(void);
 112 
 113 /* ??? */
 114 struct files_struct {
 115         /* ??? */
 116         int count;
 117         /* bit mask to close fds on exec */
 118         fd_set close_on_exec;
 119         /* do we have at most NR_OPEN available fds? I assume fd i maps into
 120          * each open file */    
 121         struct file * fd[NR_OPEN];
 122 };
 123 
 124 #define INIT_FILES { \
 125         1, \
 126         { { 0, } }, \
 127         { NULL, } \
 128 }
 129 
 130 struct fs_struct {
 131         int count;
 132         unsigned short umask;
 133         struct inode * root, * pwd;
 134 };
 135 
 136 #define INIT_FS { \
 137         1, \
 138         0022, \
 139         NULL, NULL \
 140 }
 141 
 142 struct mm_struct {
 143         int count;
 144         pgd_t * pgd;
 145         unsigned long context;
 146         unsigned long start_code, end_code, start_data, end_data;
 147         unsigned long start_brk, brk, start_stack, start_mmap;
 148         unsigned long arg_start, arg_end, env_start, env_end;
 149         unsigned long rss, total_vm, locked_vm;
 150         unsigned long def_flags;
 151         struct vm_area_struct * mmap;
 152         struct vm_area_struct * mmap_avl;
 153 };
 154 
 155 #define INIT_MM { \
 156                 1, \
 157                 swapper_pg_dir, \
 158                 0, \
 159                 0, 0, 0, 0, \
 160                 0, 0, 0, 0, \
 161                 0, 0, 0, 0, \
 162                 0, 0, 0, \
 163                 0, \
 164                 &init_mmap, &init_mmap }
 165 
 166 struct signal_struct {
 167         int count;
 168         struct sigaction action[32];
 169 };
 170 
 171 #define INIT_SIGNALS { \
 172                 1, \
 173                 { {0,}, } }
 174 
 175 struct task_struct {
 176 /* these are hardcoded - don't touch */
 177         volatile long state;    /* -1 unrunnable, 0 runnable, >0 stopped */
 178         long counter;
 179         long priority;
 180         unsigned long signal;
 181         unsigned long blocked;  /* bitmap of masked signals */
 182         unsigned long flags;    /* per process flags, defined below */
 183         int errno;
 184         long debugreg[8];  /* Hardware debugging registers */
 185         struct exec_domain *exec_domain;
 186 /* various fields */
 187         struct linux_binfmt *binfmt;
 188         struct task_struct *next_task, *prev_task;
 189         struct task_struct *next_run,  *prev_run;
 190         unsigned long saved_kernel_stack;
 191         unsigned long kernel_stack_page;
 192         int exit_code, exit_signal;
 193         /* ??? */
 194         unsigned long personality;
 195         int dumpable:1;
 196         int did_exec:1;
 197         /* shouldn't this be pid_t? */
 198         int pid;
 199         int pgrp;
 200         int tty_old_pgrp;
 201         int session;
 202         /* boolean value for session group leader */
 203         int leader;
 204         int     groups[NGROUPS];
 205         /* 
 206          * pointers to (original) parent process, youngest child, younger sibling,
 207          * older sibling, respectively.  (p->father can be replaced with 
 208          * p->p_pptr->pid)
 209          */
 210         struct task_struct *p_opptr, *p_pptr, *p_cptr, *p_ysptr, *p_osptr;
 211         struct wait_queue *wait_chldexit;       /* for wait4() */
 212         unsigned short uid,euid,suid,fsuid;
 213         unsigned short gid,egid,sgid,fsgid;
 214         unsigned long timeout, policy, rt_priority;
 215         unsigned long it_real_value, it_prof_value, it_virt_value;
 216         unsigned long it_real_incr, it_prof_incr, it_virt_incr;
 217         struct timer_list real_timer;
 218         long utime, stime, cutime, cstime, start_time;
 219 /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
 220         unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap;
 221         int swappable:1;
 222         unsigned long swap_address;
 223         unsigned long old_maj_flt;      /* old value of maj_flt */
 224         unsigned long dec_flt;          /* page fault count of the last time */
 225         unsigned long swap_cnt;         /* number of pages to swap on next pass */
 226 /* limits */
 227         struct rlimit rlim[RLIM_NLIMITS];
 228         unsigned short used_math;
 229         char comm[16];
 230 /* file system info */
 231         int link_count;
 232         struct tty_struct *tty; /* NULL if no tty */
 233 /* ipc stuff */
 234         struct sem_undo *semundo;
 235         struct sem_queue *semsleeping;
 236 /* ldt for this task - used by Wine.  If NULL, default_ldt is used */
 237         struct desc_struct *ldt;
 238 /* tss for this task */
 239         struct thread_struct tss;
 240 /* filesystem information */
 241         struct fs_struct *fs;
 242 /* open file information */
 243         struct files_struct *files;
 244 /* memory management info */
 245         struct mm_struct *mm;
 246 /* signal handlers */
 247         struct signal_struct *sig;
 248 #ifdef __SMP__
 249         int processor;
 250         int last_processor;
 251         int lock_depth;         /* Lock depth. We can context switch in and out of holding a syscall kernel lock... */  
 252 #endif  
 253 };
 254 
 255 /*
 256  * Per process flags
 257  */
 258 #define PF_ALIGNWARN    0x00000001      /* Print alignment warning msgs */
 259                                         /* Not implemented yet, only for 486*/
 260 #define PF_PTRACED      0x00000010      /* set if ptrace (0) has been called. */
 261 #define PF_TRACESYS     0x00000020      /* tracing system calls */
 262 #define PF_FORKNOEXEC   0x00000040      /* forked but didn't exec */
 263 #define PF_SUPERPREV    0x00000100      /* used super-user privileges */
 264 #define PF_DUMPCORE     0x00000200      /* dumped core */
 265 #define PF_SIGNALED     0x00000400      /* killed by a signal */
 266 
 267 #define PF_STARTING     0x00000100      /* being created */
 268 #define PF_EXITING      0x00000200      /* getting shut down */
 269 
 270 #define PF_USEDFPU      0x00100000      /* Process used the FPU this quantum (SMP only) */
 271 
 272 /*
 273  * Limit the stack by to some sane default: root can always
 274  * increase this limit if needed..  8MB seems reasonable.
 275  */
 276 #define _STK_LIM        (8*1024*1024)
 277 
 278 #define DEF_PRIORITY    (20*HZ/100)     /* 200 ms time slices */
 279 
 280 /*
 281  *  INIT_TASK is used to set up the first task table, touch at
 282  * your own risk!. Base=0, limit=0x1fffff (=2MB)
 283  */
 284 #define INIT_TASK \
 285 /* state etc */ { 0,DEF_PRIORITY,DEF_PRIORITY,0,0,0,0, \
 286 /* debugregs */ { 0, },            \
 287 /* exec domain */&default_exec_domain, \
 288 /* binfmt */    NULL, \
 289 /* schedlink */ &init_task,&init_task, &init_task, &init_task, \
 290 /* stack */     0,(unsigned long) &init_kernel_stack, \
 291 /* ec,brk... */ 0,0,0,0,0, \
 292 /* pid etc.. */ 0,0,0,0,0, \
 293 /* suppl grps*/ {NOGROUP,}, \
 294 /* proc links*/ &init_task,&init_task,NULL,NULL,NULL,NULL, \
 295 /* uid etc */   0,0,0,0,0,0,0,0, \
 296 /* timeout */   0,SCHED_OTHER,0,0,0,0,0,0,0, \
 297 /* timer */     { NULL, NULL, 0, 0, it_real_fn }, \
 298 /* utime */     0,0,0,0,0, \
 299 /* flt */       0,0,0,0,0,0, \
 300 /* swp */       0,0,0,0,0, \
 301 /* rlimits */   INIT_RLIMITS, \
 302 /* math */      0, \
 303 /* comm */      "swapper", \
 304 /* fs info */   0,NULL, \
 305 /* ipc */       NULL, NULL, \
 306 /* ldt */       NULL, \
 307 /* tss */       INIT_TSS, \
 308 /* fs */        &init_fs, \
 309 /* files */     &init_files, \
 310 /* mm */        &init_mm, \
 311 /* signals */   &init_signals, \
 312 }
 313 
 314 extern struct   mm_struct init_mm;
 315 extern struct task_struct init_task;
 316 extern struct task_struct *task[NR_TASKS];
 317 extern struct task_struct *last_task_used_math;
 318 extern struct task_struct *current_set[NR_CPUS];
 319 /*
 320  *      On a single processor system this comes out as current_set[0] when cpp
 321  *      has finished with it, which gcc will optimise away.
 322  */
 323 #define current (0+current_set[smp_processor_id()])     /* Current on this processor */
 324 extern unsigned long volatile jiffies;
 325 extern unsigned long itimer_ticks;
 326 extern unsigned long itimer_next;
 327 extern struct timeval xtime;
 328 extern int need_resched;
 329 extern void do_timer(struct pt_regs *);
 330 
 331 extern unsigned int * prof_buffer;
 332 extern unsigned long prof_len;
 333 extern unsigned long prof_shift;
 334 
 335 extern int securelevel; /* system security level */
 336 
 337 #define CURRENT_TIME (xtime.tv_sec)
 338 
 339 extern void sleep_on(struct wait_queue ** p);
 340 extern void interruptible_sleep_on(struct wait_queue ** p);
 341 extern void wake_up(struct wait_queue ** p);
 342 extern void wake_up_interruptible(struct wait_queue ** p);
 343 extern void wake_up_process(struct task_struct * tsk);
 344 
 345 extern void notify_parent(struct task_struct * tsk);
 346 extern int send_sig(unsigned long sig,struct task_struct * p,int priv);
 347 extern int in_group_p(gid_t grp);
 348 
 349 extern int request_irq(unsigned int irq,
 350                        void (*handler)(int, void *, struct pt_regs *),
 351                        unsigned long flags, 
 352                        const char *device,
 353                        void *dev_id);
 354 extern void free_irq(unsigned int irq, void *dev_id);
 355 
 356 /*
 357  * This has now become a routine instead of a macro, it sets a flag if
 358  * it returns true (to do BSD-style accounting where the process is flagged
 359  * if it uses root privs). The implication of this is that you should do
 360  * normal permissions checks first, and check suser() last.
 361  */
 362 extern inline int suser(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 363 {
 364         if (current->euid == 0)
 365                 current->flags |= PF_SUPERPREV;
 366         return (current->euid == 0);
 367 }
 368 
 369 extern void copy_thread(int, unsigned long, unsigned long, struct task_struct *, struct pt_regs *);
 370 extern void flush_thread(void);
 371 extern void exit_thread(void);
 372 
 373 extern void exit_fs(struct task_struct *);
 374 extern void exit_files(struct task_struct *);
 375 extern void exit_sighand(struct task_struct *);
 376 extern void release_thread(struct task_struct *);
 377 
 378 extern int do_execve(char *, char **, char **, struct pt_regs *);
 379 extern int do_fork(unsigned long, unsigned long, struct pt_regs *);
 380 
 381 
 382 /* See if we have a valid user level fd.
 383  * If it makes sense, return the file structure it references.
 384  * Otherwise return NULL.
 385  */
 386 extern inline struct file *file_from_fd(const unsigned int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 387 {
 388 
 389         if (fd >= NR_OPEN)
 390                 return NULL;
 391         /* either valid or null */
 392         return current->files->fd[fd];
 393 }
 394         
 395 /*
 396  * The wait-queues are circular lists, and you have to be *very* sure
 397  * to keep them correct. Use only these two functions to add/remove
 398  * entries in the queues.
 399  */
 400 extern inline void add_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 401 {
 402         unsigned long flags;
 403 
 404 #ifdef DEBUG
 405         if (wait->next) {
 406                 __label__ here;
 407                 unsigned long pc;
 408                 pc = (unsigned long) &&here;
 409               here:
 410                 printk("add_wait_queue (%08lx): wait->next = %08lx\n",pc,(unsigned long) wait->next);
 411         }
 412 #endif
 413         save_flags(flags);
 414         cli();
 415         if (!*p) {
 416                 wait->next = wait;
 417                 *p = wait;
 418         } else {
 419                 wait->next = (*p)->next;
 420                 (*p)->next = wait;
 421         }
 422         restore_flags(flags);
 423 }
 424 
 425 extern inline void remove_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 426 {
 427         unsigned long flags;
 428         struct wait_queue * tmp;
 429 #ifdef DEBUG
 430         unsigned long ok = 0;
 431 #endif
 432 
 433         save_flags(flags);
 434         cli();
 435         if ((*p == wait) &&
 436 #ifdef DEBUG
 437             (ok = 1) &&
 438 #endif
 439             ((*p = wait->next) == wait)) {
 440                 *p = NULL;
 441         } else {
 442                 tmp = wait;
 443                 while (tmp->next != wait) {
 444                         tmp = tmp->next;
 445 #ifdef DEBUG
 446                         if (tmp == *p)
 447                                 ok = 1;
 448 #endif
 449                 }
 450                 tmp->next = wait->next;
 451         }
 452         wait->next = NULL;
 453         restore_flags(flags);
 454 #ifdef DEBUG
 455         if (!ok) {
 456                 __label__ here;
 457                 ok = (unsigned long) &&here;
 458                 printk("removed wait_queue not on list.\n");
 459                 printk("list = %08lx, queue = %08lx\n",(unsigned long) p, (unsigned long) wait);
 460               here:
 461                 printk("eip = %08lx\n",ok);
 462         }
 463 #endif
 464 }
 465 
 466 extern inline void select_wait(struct wait_queue ** wait_address, select_table * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 467 {
 468         struct select_table_entry * entry;
 469 
 470         if (!p || !wait_address)
 471                 return;
 472         if (p->nr >= __MAX_SELECT_TABLE_ENTRIES)
 473                 return;
 474         entry = p->entry + p->nr;
 475         entry->wait_address = wait_address;
 476         entry->wait.task = current;
 477         entry->wait.next = NULL;
 478         add_wait_queue(wait_address,&entry->wait);
 479         p->nr++;
 480 }
 481 
 482 extern void __down(struct semaphore * sem);
 483 
 484 /*
 485  * These are not yet interrupt-safe
 486  */
 487 extern inline void down(struct semaphore * sem)
     /* [previous][next][first][last][top][bottom][index][help] */
 488 {
 489         if (sem->count <= 0)
 490                 __down(sem);
 491         sem->count--;
 492 }
 493 
 494 extern inline void up(struct semaphore * sem)
     /* [previous][next][first][last][top][bottom][index][help] */
 495 {
 496         sem->count++;
 497         wake_up(&sem->wait);
 498 }       
 499 
 500 #define REMOVE_LINKS(p) do { unsigned long flags; \
 501         save_flags(flags) ; cli(); \
 502         (p)->next_task->prev_task = (p)->prev_task; \
 503         (p)->prev_task->next_task = (p)->next_task; \
 504         restore_flags(flags); \
 505         if ((p)->p_osptr) \
 506                 (p)->p_osptr->p_ysptr = (p)->p_ysptr; \
 507         if ((p)->p_ysptr) \
 508                 (p)->p_ysptr->p_osptr = (p)->p_osptr; \
 509         else \
 510                 (p)->p_pptr->p_cptr = (p)->p_osptr; \
 511         } while (0)
 512 
 513 #define SET_LINKS(p) do { unsigned long flags; \
 514         save_flags(flags); cli(); \
 515         (p)->next_task = &init_task; \
 516         (p)->prev_task = init_task.prev_task; \
 517         init_task.prev_task->next_task = (p); \
 518         init_task.prev_task = (p); \
 519         restore_flags(flags); \
 520         (p)->p_ysptr = NULL; \
 521         if (((p)->p_osptr = (p)->p_pptr->p_cptr) != NULL) \
 522                 (p)->p_osptr->p_ysptr = p; \
 523         (p)->p_pptr->p_cptr = p; \
 524         } while (0)
 525 
 526 #define for_each_task(p) \
 527         for (p = &init_task ; (p = p->next_task) != &init_task ; )
 528 
 529 #endif /* __KERNEL__ */
 530 
 531 #endif

/* [previous][next][first][last][top][bottom][index][help] */