|
|
1.1 root 1: /*
1.1.1.2 root 2: * linux/kernel/sched.c
3: *
4: * (C) 1991 Linus Torvalds
5: */
6:
7: /*
1.1 root 8: * 'sched.c' is the main kernel file. It contains scheduling primitives
9: * (sleep_on, wakeup, schedule etc) as well as a number of simple system
10: * call functions (type getpid(), which just extracts a field from
11: * current-task
12: */
13: #include <linux/sched.h>
1.1.1.5 ! root 14: #include <linux/timer.h>
1.1 root 15: #include <linux/kernel.h>
16: #include <linux/sys.h>
1.1.1.2 root 17: #include <linux/fdreg.h>
1.1 root 18: #include <asm/system.h>
19: #include <asm/io.h>
20: #include <asm/segment.h>
21:
1.1.1.2 root 22: #include <signal.h>
1.1.1.5 ! root 23: #include <errno.h>
1.1.1.2 root 24:
25: #define _S(nr) (1<<((nr)-1))
26: #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
27:
28: void show_task(int nr,struct task_struct * p)
29: {
1.1.1.3 root 30: int i,j = 4096-sizeof(struct task_struct);
31:
1.1.1.4 root 32: printk("%d: pid=%d, state=%d, father=%d, child=%d, ",nr,p->pid,
33: p->state, p->p_pptr->pid, p->p_cptr ? p->p_cptr->pid : -1);
1.1.1.3 root 34: i=0;
35: while (i<j && !((char *)(p+1))[i])
36: i++;
1.1.1.4 root 37: printk("%d/%d chars free in kstack\n\r",i,j);
38: printk(" PC=%08X.", *(1019 + (unsigned long *) p));
39: if (p->p_ysptr || p->p_osptr)
40: printk(" Younger sib=%d, older sib=%d\n\r",
41: p->p_ysptr ? p->p_ysptr->pid : -1,
42: p->p_osptr ? p->p_osptr->pid : -1);
43: else
44: printk("\n\r");
1.1.1.2 root 45: }
46:
1.1.1.4 root 47: void show_state(void)
1.1.1.2 root 48: {
49: int i;
50:
1.1.1.4 root 51: printk("\rTask-info:\n\r");
1.1.1.2 root 52: for (i=0;i<NR_TASKS;i++)
53: if (task[i])
54: show_task(i,task[i]);
55: }
56:
1.1 root 57: #define LATCH (1193180/HZ)
58:
59: extern void mem_use(void);
60:
61: extern int timer_interrupt(void);
62: extern int system_call(void);
63:
64: union task_union {
65: struct task_struct task;
66: char stack[PAGE_SIZE];
67: };
68:
69: static union task_union init_task = {INIT_TASK,};
70:
1.1.1.4 root 71: unsigned long volatile jiffies=0;
72: unsigned long startup_time=0;
73: int jiffies_offset = 0; /* # clock ticks to add to get "true
74: time". Should always be less than
75: 1 second's worth. For time fanatics
76: who like to syncronize their machines
77: to WWV :-) */
78:
1.1.1.2 root 79: struct task_struct *current = &(init_task.task);
80: struct task_struct *last_task_used_math = NULL;
1.1 root 81:
82: struct task_struct * task[NR_TASKS] = {&(init_task.task), };
83:
84: long user_stack [ PAGE_SIZE>>2 ] ;
85:
86: struct {
87: long * a;
88: short b;
89: } stack_start = { & user_stack [PAGE_SIZE>>2] , 0x10 };
90: /*
91: * 'math_state_restore()' saves the current math information in the
92: * old math state array, and gets the new ones from the current task
93: */
94: void math_state_restore()
95: {
1.1.1.2 root 96: if (last_task_used_math == current)
97: return;
1.1.1.3 root 98: __asm__("fwait");
1.1.1.2 root 99: if (last_task_used_math) {
1.1 root 100: __asm__("fnsave %0"::"m" (last_task_used_math->tss.i387));
1.1.1.2 root 101: }
1.1.1.3 root 102: last_task_used_math=current;
1.1.1.2 root 103: if (current->used_math) {
1.1 root 104: __asm__("frstor %0"::"m" (current->tss.i387));
1.1.1.2 root 105: } else {
1.1 root 106: __asm__("fninit"::);
107: current->used_math=1;
108: }
109: }
110:
111: /*
1.1.1.5 ! root 112: * 'schedule()' is the scheduler function. It's a very simple and nice
! 113: * scheduler: it's not perfect, but certainly works for most things.
1.1 root 114: * The one thing you might take a look at is the signal-handler code here.
115: *
116: * NOTE!! Task 0 is the 'idle' task, which gets called when no other
117: * tasks can run. It can not be killed, and it cannot sleep. The 'state'
118: * information in task[0] is never used.
119: */
120: void schedule(void)
121: {
122: int i,next,c;
123: struct task_struct ** p;
124:
125: /* check alarm, wake up any interruptible tasks that have got a signal */
126:
127: for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
128: if (*p) {
1.1.1.4 root 129: if ((*p)->timeout && (*p)->timeout < jiffies) {
130: (*p)->timeout = 0;
131: if ((*p)->state == TASK_INTERRUPTIBLE)
132: (*p)->state = TASK_RUNNING;
133: }
1.1 root 134: if ((*p)->alarm && (*p)->alarm < jiffies) {
1.1.1.4 root 135: (*p)->signal |= (1<<(SIGALRM-1));
136: (*p)->alarm = 0;
137: }
1.1.1.5 ! root 138: if (((*p)->signal & ~(*p)->blocked) &&
1.1.1.2 root 139: (*p)->state==TASK_INTERRUPTIBLE)
1.1 root 140: (*p)->state=TASK_RUNNING;
141: }
142:
143: /* this is the scheduler proper: */
144:
145: while (1) {
146: c = -1;
147: next = 0;
148: i = NR_TASKS;
149: p = &task[NR_TASKS];
150: while (--i) {
151: if (!*--p)
152: continue;
153: if ((*p)->state == TASK_RUNNING && (*p)->counter > c)
154: c = (*p)->counter, next = i;
155: }
156: if (c) break;
157: for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
158: if (*p)
159: (*p)->counter = ((*p)->counter >> 1) +
160: (*p)->priority;
161: }
162: switch_to(next);
163: }
164:
165: int sys_pause(void)
166: {
1.1.1.5 ! root 167: unsigned long old_blocked;
! 168: unsigned long mask;
! 169: struct sigaction * sa = current->sigaction;
! 170:
! 171: old_blocked = current->blocked;
! 172: for (mask=1 ; mask ; sa++,mask += mask)
! 173: if (sa->sa_handler == SIG_IGN)
! 174: current->blocked |= mask;
1.1 root 175: current->state = TASK_INTERRUPTIBLE;
176: schedule();
1.1.1.5 ! root 177: current->blocked = old_blocked;
! 178: return -EINTR;
1.1 root 179: }
180:
1.1.1.4 root 181: static inline void __sleep_on(struct task_struct **p, int state)
1.1 root 182: {
183: struct task_struct *tmp;
1.1.1.5 ! root 184: unsigned int flags;
1.1 root 185:
186: if (!p)
187: return;
188: if (current == &(init_task.task))
189: panic("task[0] trying to sleep");
1.1.1.5 ! root 190: __asm__("pushfl ; popl %0":"=r" (flags));
1.1 root 191: tmp = *p;
192: *p = current;
1.1.1.4 root 193: current->state = state;
1.1.1.5 ! root 194: /* make sure interrupts are enabled: there should be no more races here */
! 195: sti();
1.1.1.4 root 196: repeat: schedule();
197: if (*p && *p != current) {
198: current->state = TASK_UNINTERRUPTIBLE;
1.1.1.5 ! root 199: (**p).state = 0;
1.1.1.4 root 200: goto repeat;
201: }
202: if (*p = tmp)
1.1 root 203: tmp->state=0;
1.1.1.5 ! root 204: __asm__("pushl %0 ; popfl"::"r" (flags));
1.1 root 205: }
206:
207: void interruptible_sleep_on(struct task_struct **p)
208: {
1.1.1.4 root 209: __sleep_on(p,TASK_INTERRUPTIBLE);
210: }
1.1 root 211:
1.1.1.4 root 212: void sleep_on(struct task_struct **p)
213: {
214: __sleep_on(p,TASK_UNINTERRUPTIBLE);
1.1 root 215: }
216:
217: void wake_up(struct task_struct **p)
218: {
219: if (p && *p) {
1.1.1.4 root 220: if ((**p).state == TASK_STOPPED)
221: printk("wake_up: TASK_STOPPED");
222: if ((**p).state == TASK_ZOMBIE)
223: printk("wake_up: TASK_ZOMBIE");
1.1 root 224: (**p).state=0;
225: }
226: }
227:
1.1.1.2 root 228: /*
229: * OK, here are some floppy things that shouldn't be in the kernel
230: * proper. They are here because the floppy needs a timer, and this
231: * was the easiest way of doing it.
232: */
233: static struct task_struct * wait_motor[4] = {NULL,NULL,NULL,NULL};
234: static int mon_timer[4]={0,0,0,0};
235: static int moff_timer[4]={0,0,0,0};
236: unsigned char current_DOR = 0x0C;
237:
238: int ticks_to_floppy_on(unsigned int nr)
239: {
1.1.1.3 root 240: extern unsigned char selected;
241: unsigned char mask = 0x10 << nr;
1.1.1.2 root 242:
243: if (nr>3)
244: panic("floppy_on: nr>3");
245: moff_timer[nr]=10000; /* 100 s = very big :-) */
246: cli(); /* use floppy_off to turn it off */
1.1.1.3 root 247: mask |= current_DOR;
248: if (!selected) {
249: mask &= 0xFC;
250: mask |= nr;
251: }
252: if (mask != current_DOR) {
253: outb(mask,FD_DOR);
254: if ((mask ^ current_DOR) & 0xf0)
255: mon_timer[nr] = HZ/2;
256: else if (mon_timer[nr] < 2)
257: mon_timer[nr] = 2;
258: current_DOR = mask;
1.1.1.2 root 259: }
260: sti();
261: return mon_timer[nr];
262: }
263:
264: void floppy_on(unsigned int nr)
265: {
266: cli();
267: while (ticks_to_floppy_on(nr))
268: sleep_on(nr+wait_motor);
269: sti();
270: }
271:
272: void floppy_off(unsigned int nr)
273: {
274: moff_timer[nr]=3*HZ;
275: }
276:
277: void do_floppy_timer(void)
278: {
279: int i;
280: unsigned char mask = 0x10;
281:
282: for (i=0 ; i<4 ; i++,mask <<= 1) {
283: if (!(mask & current_DOR))
284: continue;
285: if (mon_timer[i]) {
286: if (!--mon_timer[i])
287: wake_up(i+wait_motor);
288: } else if (!moff_timer[i]) {
289: current_DOR &= ~mask;
290: outb(current_DOR,FD_DOR);
291: } else
292: moff_timer[i]--;
293: }
294: }
295:
296: #define TIME_REQUESTS 64
297:
298: static struct timer_list {
299: long jiffies;
300: void (*fn)();
301: struct timer_list * next;
302: } timer_list[TIME_REQUESTS], * next_timer = NULL;
303:
304: void add_timer(long jiffies, void (*fn)(void))
305: {
306: struct timer_list * p;
307:
308: if (!fn)
309: return;
310: cli();
311: if (jiffies <= 0)
312: (fn)();
313: else {
314: for (p = timer_list ; p < timer_list + TIME_REQUESTS ; p++)
315: if (!p->fn)
316: break;
317: if (p >= timer_list + TIME_REQUESTS)
318: panic("No more time requests free");
319: p->fn = fn;
320: p->jiffies = jiffies;
321: p->next = next_timer;
322: next_timer = p;
323: while (p->next && p->next->jiffies < p->jiffies) {
324: p->jiffies -= p->next->jiffies;
325: fn = p->fn;
326: p->fn = p->next->fn;
327: p->next->fn = fn;
328: jiffies = p->jiffies;
329: p->jiffies = p->next->jiffies;
330: p->next->jiffies = jiffies;
331: p = p->next;
332: }
333: }
334: sti();
335: }
336:
1.1.1.5 ! root 337: unsigned long timer_active = 0;
! 338: struct timer_struct timer_table[32];
! 339:
1.1 root 340: void do_timer(long cpl)
341: {
1.1.1.5 ! root 342: unsigned long mask;
! 343: struct timer_struct *tp = timer_table+0;
1.1.1.4 root 344:
1.1.1.5 ! root 345: for (mask = 1 ; mask ; tp++,mask += mask) {
! 346: if (mask > timer_active)
! 347: break;
! 348: if (!(mask & timer_active))
! 349: continue;
! 350: if (tp->expires > jiffies)
! 351: continue;
! 352: timer_active &= ~mask;
! 353: tp->fn();
! 354: }
1.1.1.3 root 355:
1.1 root 356: if (cpl)
357: current->utime++;
358: else
359: current->stime++;
1.1.1.3 root 360:
1.1.1.2 root 361: if (next_timer) {
362: next_timer->jiffies--;
363: while (next_timer && next_timer->jiffies <= 0) {
364: void (*fn)(void);
365:
366: fn = next_timer->fn;
367: next_timer->fn = NULL;
368: next_timer = next_timer->next;
369: (fn)();
370: }
371: }
372: if (current_DOR & 0xf0)
373: do_floppy_timer();
1.1 root 374: if ((--current->counter)>0) return;
375: current->counter=0;
376: if (!cpl) return;
377: schedule();
378: }
379:
380: int sys_alarm(long seconds)
381: {
1.1.1.2 root 382: int old = current->alarm;
383:
384: if (old)
385: old = (old - jiffies) / HZ;
1.1 root 386: current->alarm = (seconds>0)?(jiffies+HZ*seconds):0;
1.1.1.2 root 387: return (old);
1.1 root 388: }
389:
390: int sys_getpid(void)
391: {
392: return current->pid;
393: }
394:
395: int sys_getppid(void)
396: {
1.1.1.4 root 397: return current->p_pptr->pid;
1.1 root 398: }
399:
400: int sys_getuid(void)
401: {
402: return current->uid;
403: }
404:
405: int sys_geteuid(void)
406: {
407: return current->euid;
408: }
409:
410: int sys_getgid(void)
411: {
412: return current->gid;
413: }
414:
415: int sys_getegid(void)
416: {
417: return current->egid;
418: }
419:
420: int sys_nice(long increment)
421: {
1.1.1.5 ! root 422: if (increment < 0 && !suser())
! 423: return -EPERM;
! 424: if (increment > current->priority)
! 425: increment = current->priority-1;
! 426: current->priority -= increment;
1.1 root 427: return 0;
428: }
429:
430: void sched_init(void)
431: {
432: int i;
433: struct desc_struct * p;
434:
1.1.1.2 root 435: if (sizeof(struct sigaction) != 16)
436: panic("Struct sigaction MUST be 16 bytes");
1.1 root 437: set_tss_desc(gdt+FIRST_TSS_ENTRY,&(init_task.task.tss));
438: set_ldt_desc(gdt+FIRST_LDT_ENTRY,&(init_task.task.ldt));
439: p = gdt+2+FIRST_TSS_ENTRY;
1.1.1.5 ! root 440: for(i=1 ; i<NR_TASKS ; i++) {
1.1 root 441: task[i] = NULL;
442: p->a=p->b=0;
443: p++;
444: p->a=p->b=0;
445: p++;
446: }
1.1.1.3 root 447: /* Clear NT, so that we won't have troubles with that later on */
448: __asm__("pushfl ; andl $0xffffbfff,(%esp) ; popfl");
1.1 root 449: ltr(0);
450: lldt(0);
451: outb_p(0x36,0x43); /* binary, mode 3, LSB/MSB, ch 0 */
452: outb_p(LATCH & 0xff , 0x40); /* LSB */
453: outb(LATCH >> 8 , 0x40); /* MSB */
454: set_intr_gate(0x20,&timer_interrupt);
455: outb(inb_p(0x21)&~0x01,0x21);
456: set_system_gate(0x80,&system_call);
457: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.