root/include/linux/sched.h

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

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. add_wait_queue
  2. remove_wait_queue
  3. select_wait
  4. _get_base
  5. get_limit

   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 #define HZ 100
  13 
  14 #include <linux/tasks.h>
  15 #include <asm/system.h>
  16 
  17 /*
  18  * User space process size: 3GB. This is hardcoded into a few places,
  19  * so don't change it unless you know what you are doing.
  20  */
  21 #define TASK_SIZE       0xc0000000
  22 
  23 /*
  24  * Size of io_bitmap in longwords: 32 is ports 0-0x3ff.
  25  */
  26 #define IO_BITMAP_SIZE  32
  27 
  28 /*
  29  * These are the constant used to fake the fixed-point load-average
  30  * counting. Some notes:
  31  *  - 11 bit fractions expand to 22 bits by the multiplies: this gives
  32  *    a load-average precision of 10 bits integer + 11 bits fractional
  33  *  - if you want to count load-averages more often, you need more
  34  *    precision, or rounding will get you. With 2-second counting freq,
  35  *    the EXP_n values would be 1981, 2034 and 2043 if still using only
  36  *    11 bit fractions.
  37  */
  38 extern unsigned long avenrun[];         /* Load averages */
  39 
  40 #define FSHIFT          11              /* nr of bits of precision */
  41 #define FIXED_1         (1<<FSHIFT)     /* 1.0 as fixed-point */
  42 #define LOAD_FREQ       (5*HZ)          /* 5 sec intervals */
  43 #define EXP_1           1884            /* 1/exp(5sec/1min) as fixed-point */
  44 #define EXP_5           2014            /* 1/exp(5sec/5min) */
  45 #define EXP_15          2037            /* 1/exp(5sec/15min) */
  46 
  47 #define CALC_LOAD(load,exp,n) \
  48         load *= exp; \
  49         load += n*(FIXED_1-exp); \
  50         load >>= FSHIFT;
  51 
  52 #define CT_TO_SECS(x)   ((x) / HZ)
  53 #define CT_TO_USECS(x)  (((x) % HZ) * 1000000/HZ)
  54 
  55 #define FIRST_TASK task[0]
  56 #define LAST_TASK task[NR_TASKS-1]
  57 
  58 #include <linux/head.h>
  59 #include <linux/fs.h>
  60 #include <linux/mm.h>
  61 #include <linux/signal.h>
  62 #include <linux/time.h>
  63 #include <linux/param.h>
  64 #include <linux/resource.h>
  65 #include <linux/vm86.h>
  66 #include <linux/math_emu.h>
  67 
  68 #define TASK_RUNNING            0
  69 #define TASK_INTERRUPTIBLE      1
  70 #define TASK_UNINTERRUPTIBLE    2
  71 #define TASK_ZOMBIE             3
  72 #define TASK_STOPPED            4
  73 #define TASK_SWAPPING           5
  74 
  75 #ifndef NULL
  76 #define NULL ((void *) 0)
  77 #endif
  78 
  79 #ifdef __KERNEL__
  80 
  81 extern void sched_init(void);
  82 extern void show_state(void);
  83 extern void trap_init(void);
  84 extern void panic(const char * str);
  85 
  86 extern "C" void schedule(void);
  87 
  88 #endif /* __KERNEL__ */
  89 
  90 struct i387_hard_struct {
  91         long    cwd;
  92         long    swd;
  93         long    twd;
  94         long    fip;
  95         long    fcs;
  96         long    foo;
  97         long    fos;
  98         long    st_space[20];   /* 8*10 bytes for each FP-reg = 80 bytes */
  99 };
 100 
 101 struct i387_soft_struct {
 102         long    cwd;
 103         long    swd;
 104         long    twd;
 105         long    fip;
 106         long    fcs;
 107         long    foo;
 108         long    fos;
 109         long    top;
 110         struct fpu_reg  regs[8];        /* 8*16 bytes for each FP-reg = 128 bytes */
 111         unsigned char   lookahead;
 112         struct info     *info;
 113         unsigned long   entry_eip;
 114 };
 115 
 116 union i387_union {
 117         struct i387_hard_struct hard;
 118         struct i387_soft_struct soft;
 119 };
 120 
 121 struct tss_struct {
 122         unsigned short  back_link,__blh;
 123         unsigned long   esp0;
 124         unsigned short  ss0,__ss0h;
 125         unsigned long   esp1;
 126         unsigned short  ss1,__ss1h;
 127         unsigned long   esp2;
 128         unsigned short  ss2,__ss2h;
 129         unsigned long   cr3;
 130         unsigned long   eip;
 131         unsigned long   eflags;
 132         unsigned long   eax,ecx,edx,ebx;
 133         unsigned long   esp;
 134         unsigned long   ebp;
 135         unsigned long   esi;
 136         unsigned long   edi;
 137         unsigned short  es, __esh;
 138         unsigned short  cs, __csh;
 139         unsigned short  ss, __ssh;
 140         unsigned short  ds, __dsh;
 141         unsigned short  fs, __fsh;
 142         unsigned short  gs, __gsh;
 143         unsigned short  ldt, __ldth;
 144         unsigned short  trace, bitmap;
 145         unsigned long   io_bitmap[IO_BITMAP_SIZE+1];
 146         unsigned long   tr;
 147         union i387_union i387;
 148 };
 149 
 150 struct task_struct {
 151 /* these are hardcoded - don't touch */
 152         long state;             /* -1 unrunnable, 0 runnable, >0 stopped */
 153         long counter;
 154         long priority;
 155         unsigned long signal;
 156         unsigned long blocked;  /* bitmap of masked signals */
 157         unsigned long flags;    /* per process flags, defined below */
 158         int errno;
 159 /* various fields */
 160         struct task_struct *next_task, *prev_task;
 161         struct sigaction sigaction[32];
 162         unsigned long saved_kernel_stack;
 163         unsigned long kernel_stack_page;
 164         int exit_code, exit_signal;
 165         int elf_executable:1;
 166         int dumpable:1;
 167         int swappable:1;
 168         unsigned long start_code,end_code,end_data,brk,start_stack,start_mmap;
 169         unsigned long arg_start, arg_end, env_start, env_end;
 170         long pid,pgrp,session,leader;
 171         int     groups[NGROUPS];
 172         /* 
 173          * pointers to (original) parent process, youngest child, younger sibling,
 174          * older sibling, respectively.  (p->father can be replaced with 
 175          * p->p_pptr->pid)
 176          */
 177         struct task_struct *p_opptr,*p_pptr, *p_cptr, *p_ysptr, *p_osptr;
 178         struct wait_queue *wait_chldexit;       /* for wait4() */
 179         /*
 180          * For ease of programming... Normal sleeps don't need to
 181          * keep track of a wait-queue: every task has an entry of it's own
 182          */
 183         unsigned short uid,euid,suid;
 184         unsigned short gid,egid,sgid;
 185         unsigned long timeout;
 186         unsigned long it_real_value, it_prof_value, it_virt_value;
 187         unsigned long it_real_incr, it_prof_incr, it_virt_incr;
 188         long utime,stime,cutime,cstime,start_time;
 189         unsigned long min_flt, maj_flt;
 190         unsigned long cmin_flt, cmaj_flt;
 191         struct rlimit rlim[RLIM_NLIMITS]; 
 192         unsigned short used_math;
 193         unsigned short rss;     /* number of resident pages */
 194         char comm[16];
 195         struct vm86_struct * vm86_info;
 196         unsigned long screen_bitmap;
 197 /* file system info */
 198         int link_count;
 199         int tty;                /* -1 if no tty, so it must be signed */
 200         unsigned short umask;
 201         struct inode * pwd;
 202         struct inode * root;
 203         struct inode * executable;
 204         struct vm_area_struct * mmap;
 205         struct shm_desc *shm;
 206         struct sem_undo *semun;
 207         struct file * filp[NR_OPEN];
 208         fd_set close_on_exec;
 209 /* ldt for this task - not currently used */
 210         struct desc_struct ldt[32];
 211 /* tss for this task */
 212         struct tss_struct tss;
 213 };
 214 
 215 /*
 216  * Per process flags
 217  */
 218 #define PF_ALIGNWARN    0x00000001      /* Print alignment warning msgs */
 219                                         /* Not implemented yet, only for 486*/
 220 #define PF_PTRACED      0x00000010      /* set if ptrace (0) has been called. */
 221 #define PF_TRACESYS     0x00000020      /* tracing system calls */
 222 
 223 /*
 224  * cloning flags:
 225  */
 226 #define CSIGNAL         0x000000ff      /* signal mask to be sent at exit */
 227 #define COPYVM          0x00000100      /* set if VM copy desired (like normal fork()) */
 228 #define COPYFD          0x00000200      /* set if fd's should be copied, not shared (NI) */
 229 
 230 /*
 231  *  INIT_TASK is used to set up the first task table, touch at
 232  * your own risk!. Base=0, limit=0x1fffff (=2MB)
 233  */
 234 #define INIT_TASK \
 235 /* state etc */ { 0,15,15,0,0,0,0, \
 236 /* schedlink */ &init_task,&init_task, \
 237 /* signals */   {{ 0, },}, \
 238 /* stack */     0,0, \
 239 /* ec,brk... */ 0,0,0,0,0,0,0,0,0,0,0, \
 240 /* argv.. */    0,0,0,0, \
 241 /* pid etc.. */ 0,0,0,0, \
 242 /* suppl grps*/ {NOGROUP,}, \
 243 /* proc links*/ &init_task,&init_task,NULL,NULL,NULL,NULL, \
 244 /* uid etc */   0,0,0,0,0,0, \
 245 /* timeout */   0,0,0,0,0,0,0,0,0,0,0,0, \
 246 /* min_flt */   0,0,0,0, \
 247 /* rlimits */   { {0x7fffffff, 0x7fffffff}, {0x7fffffff, 0x7fffffff},  \
 248                   {0x7fffffff, 0x7fffffff}, {0x7fffffff, 0x7fffffff}, \
 249                   {0x7fffffff, 0x7fffffff}, {0x7fffffff, 0x7fffffff}}, \
 250 /* math */      0, \
 251 /* rss */       2, \
 252 /* comm */      "swapper", \
 253 /* vm86_info */ NULL, 0, \
 254 /* fs info */   0,-1,0022,NULL,NULL,NULL,NULL, \
 255 /* ipc */       NULL, NULL, \
 256 /* filp */      {NULL,}, \
 257 /* cloe */      {{ 0, }}, \
 258                 { \
 259 /* ldt */               {0,0}, \
 260                 }, \
 261 /*tss*/ {0,0, \
 262          sizeof(init_kernel_stack) + (long) &init_kernel_stack, KERNEL_DS, 0, \
 263          0,0,0,0,0,0, \
 264          (long) &swapper_pg_dir, \
 265          0,0,0,0,0,0,0,0,0,0, \
 266          USER_DS,0,USER_DS,0,USER_DS,0,USER_DS,0,USER_DS,0,USER_DS,0, \
 267          _LDT(0),0, \
 268          0, 0x8000, \
 269 /* ioperm */    {0xffffffff, }, \
 270          _TSS(0), \
 271 /* 387 state */ { { 0, }, } \
 272         } \
 273 }
 274 
 275 extern struct task_struct init_task;
 276 extern struct task_struct *task[NR_TASKS];
 277 extern struct task_struct *last_task_used_math;
 278 extern struct task_struct *current;
 279 extern unsigned long volatile jiffies;
 280 extern unsigned long startup_time;
 281 extern int jiffies_offset;
 282 extern int need_resched;
 283 extern int hard_math;
 284 extern int ignore_irq13;
 285 
 286 #define CURRENT_TIME (startup_time+(jiffies+jiffies_offset)/HZ)
 287 
 288 extern void sleep_on(struct wait_queue ** p);
 289 extern void interruptible_sleep_on(struct wait_queue ** p);
 290 extern void wake_up(struct wait_queue ** p);
 291 extern void wake_up_interruptible(struct wait_queue ** p);
 292 
 293 extern void notify_parent(struct task_struct * tsk);
 294 extern int send_sig(unsigned long sig,struct task_struct * p,int priv);
 295 extern int in_group_p(gid_t grp);
 296 
 297 extern int request_irq(unsigned int irq,void (*handler)(int));
 298 extern void free_irq(unsigned int irq);
 299 extern int irqaction(unsigned int irq,struct sigaction * sa);
 300 
 301 /*
 302  * Entry into gdt where to find first TSS. GDT layout:
 303  *   0 - nul
 304  *   1 - kernel code segment
 305  *   2 - kernel data segment
 306  *   3 - user code segment
 307  *   4 - user data segment
 308  * ...
 309  *   8 - TSS #0
 310  *   9 - LDT #0
 311  *  10 - TSS #1
 312  *  11 - LDT #1
 313  */
 314 #define FIRST_TSS_ENTRY 8
 315 #define FIRST_LDT_ENTRY (FIRST_TSS_ENTRY+1)
 316 #define _TSS(n) ((((unsigned long) n)<<4)+(FIRST_TSS_ENTRY<<3))
 317 #define _LDT(n) ((((unsigned long) n)<<4)+(FIRST_LDT_ENTRY<<3))
 318 #define load_TR(n) __asm__("ltr %%ax": /* no output */ :"a" (_TSS(n)))
 319 #define load_ldt(n) __asm__("lldt %%ax": /* no output */ :"a" (_LDT(n)))
 320 #define store_TR(n) \
 321 __asm__("str %%ax\n\t" \
 322         "subl %2,%%eax\n\t" \
 323         "shrl $4,%%eax" \
 324         :"=a" (n) \
 325         :"0" (0),"i" (FIRST_TSS_ENTRY<<3))
 326 /*
 327  *      switch_to(n) should switch tasks to task nr n, first
 328  * checking that n isn't the current task, in which case it does nothing.
 329  * This also clears the TS-flag if the task we switched to has used
 330  * tha math co-processor latest.
 331  */
 332 #define switch_to(tsk) \
 333 __asm__("cmpl %%ecx,_current\n\t" \
 334         "je 1f\n\t" \
 335         "cli\n\t" \
 336         "xchgl %%ecx,_current\n\t" \
 337         "ljmp %0\n\t" \
 338         "sti\n\t" \
 339         "cmpl %%ecx,_last_task_used_math\n\t" \
 340         "jne 1f\n\t" \
 341         "clts\n" \
 342         "1:" \
 343         : /* no output */ \
 344         :"m" (*(((char *)&tsk->tss.tr)-4)), \
 345          "c" (tsk) \
 346         :"cx")
 347 
 348 #define PAGE_ALIGN(n) (((n)+0xfff)&0xfffff000)
 349 
 350 #define _set_base(addr,base) \
 351 __asm__("movw %%dx,%0\n\t" \
 352         "rorl $16,%%edx\n\t" \
 353         "movb %%dl,%1\n\t" \
 354         "movb %%dh,%2" \
 355         : /* no output */ \
 356         :"m" (*((addr)+2)), \
 357          "m" (*((addr)+4)), \
 358          "m" (*((addr)+7)), \
 359          "d" (base) \
 360         :"dx")
 361 
 362 #define _set_limit(addr,limit) \
 363 __asm__("movw %%dx,%0\n\t" \
 364         "rorl $16,%%edx\n\t" \
 365         "movb %1,%%dh\n\t" \
 366         "andb $0xf0,%%dh\n\t" \
 367         "orb %%dh,%%dl\n\t" \
 368         "movb %%dl,%1" \
 369         : /* no output */ \
 370         :"m" (*(addr)), \
 371          "m" (*((addr)+6)), \
 372          "d" (limit) \
 373         :"dx")
 374 
 375 #define set_base(ldt,base) _set_base( ((char *)&(ldt)) , base )
 376 #define set_limit(ldt,limit) _set_limit( ((char *)&(ldt)) , (limit-1)>>12 )
 377 
 378 /*
 379  * The wait-queues are circular lists, and you have to be *very* sure
 380  * to keep them correct. Use only these two functions to add/remove
 381  * entries in the queues.
 382  */
 383 extern inline void add_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 384 {
 385         unsigned long flags;
 386 
 387 #ifdef DEBUG
 388         if (wait->next) {
 389                 unsigned long pc;
 390                 __asm__ __volatile__("call 1f\n"
 391                         "1:\tpopl %0":"=r" (pc));
 392                 printk("add_wait_queue (%08x): wait->next = %08x\n",pc,wait->next);
 393         }
 394 #endif
 395         save_flags(flags);
 396         cli();
 397         if (!*p) {
 398                 wait->next = wait;
 399                 *p = wait;
 400         } else {
 401                 wait->next = (*p)->next;
 402                 (*p)->next = wait;
 403         }
 404         restore_flags(flags);
 405 }
 406 
 407 extern inline void remove_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 408 {
 409         unsigned long flags;
 410         struct wait_queue * tmp;
 411 #ifdef DEBUG
 412         unsigned long ok = 0;
 413 #endif
 414 
 415         save_flags(flags);
 416         cli();
 417         if ((*p == wait) &&
 418 #ifdef DEBUG
 419             (ok = 1) &&
 420 #endif
 421             ((*p = wait->next) == wait)) {
 422                 *p = NULL;
 423         } else {
 424                 tmp = wait;
 425                 while (tmp->next != wait) {
 426                         tmp = tmp->next;
 427 #ifdef DEBUG
 428                         if (tmp == *p)
 429                                 ok = 1;
 430 #endif
 431                 }
 432                 tmp->next = wait->next;
 433         }
 434         wait->next = NULL;
 435         restore_flags(flags);
 436 #ifdef DEBUG
 437         if (!ok) {
 438                 printk("removed wait_queue not on list.\n");
 439                 printk("list = %08x, queue = %08x\n",p,wait);
 440                 __asm__("call 1f\n1:\tpopl %0":"=r" (ok));
 441                 printk("eip = %08x\n",ok);
 442         }
 443 #endif
 444 }
 445 
 446 extern inline void select_wait(struct wait_queue ** wait_address, select_table * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 447 {
 448         struct select_table_entry * entry;
 449 
 450         if (!p || !wait_address)
 451                 return;
 452         if (p->nr >= __MAX_SELECT_TABLE_ENTRIES)
 453                 return;
 454         entry = p->entry + p->nr;
 455         entry->wait_address = wait_address;
 456         entry->wait.task = current;
 457         entry->wait.next = NULL;
 458         add_wait_queue(wait_address,&entry->wait);
 459         p->nr++;
 460 }
 461 
 462 static inline unsigned long _get_base(char * addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 463 {
 464         unsigned long __base;
 465         __asm__("movb %3,%%dh\n\t"
 466                 "movb %2,%%dl\n\t"
 467                 "shll $16,%%edx\n\t"
 468                 "movw %1,%%dx"
 469                 :"=&d" (__base)
 470                 :"m" (*((addr)+2)),
 471                  "m" (*((addr)+4)),
 472                  "m" (*((addr)+7)));
 473         return __base;
 474 }
 475 
 476 #define get_base(ldt) _get_base( ((char *)&(ldt)) )
 477 
 478 static inline unsigned long get_limit(unsigned long segment)
     /* [previous][next][first][last][top][bottom][index][help] */
 479 {
 480         unsigned long __limit;
 481         __asm__("lsll %1,%0"
 482                 :"=r" (__limit):"r" (segment));
 483         return __limit+1;
 484 }
 485 
 486 #define REMOVE_LINKS(p) \
 487         (p)->next_task->prev_task = (p)->prev_task; \
 488         (p)->prev_task->next_task = (p)->next_task; \
 489         if ((p)->p_osptr) \
 490                 (p)->p_osptr->p_ysptr = (p)->p_ysptr; \
 491         if ((p)->p_ysptr) \
 492                 (p)->p_ysptr->p_osptr = (p)->p_osptr; \
 493         else \
 494                 (p)->p_pptr->p_cptr = (p)->p_osptr
 495 
 496 #define SET_LINKS(p) \
 497         (p)->next_task = &init_task; \
 498         (p)->prev_task = init_task.prev_task; \
 499         init_task.prev_task->next_task = (p); \
 500         init_task.prev_task = (p); \
 501         (p)->p_ysptr = NULL; \
 502         if (((p)->p_osptr = (p)->p_pptr->p_cptr) != NULL) \
 503                 (p)->p_osptr->p_ysptr = p; \
 504         (p)->p_pptr->p_cptr = p
 505 
 506 #endif

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