/*
 *  linux/include/linux/sched.h
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file README.legal in the main directory of this archive
 * for more details.
 */

/*
 * 680x0 support by Hamish Macdonald
 */

#ifndef _LINUX_SCHED_H
#define _LINUX_SCHED_H

#include <asm/system.h>

/*
 * define DEBUG if you want the wait-queues to have some extra
 * debugging code. It's not normally used, but might catch some
 * wait-queue coding errors.
 *
 *  #define DEBUG
 */


#define HZ 100

#include <linux/tasks.h>

/*
 * User space process size: 3GB. This is hardcoded into a few places,
 * so don't change it unless you know what you are doing.
 */
#define TASK_SIZE	0xc0000000

/*
 * Size of io_bitmap in longwords: 32 is ports 0-0x3ff.
 */
#define IO_BITMAP_SIZE	32

/*
 * These are the constant used to fake the fixed-point load-average
 * counting. Some notes:
 *  - 11 bit fractions expand to 22 bits by the multiplies: this gives
 *    a load-average precision of 10 bits integer + 11 bits fractional
 *  - if you want to count load-averages more often, you need more
 *    precision, or rounding will get you. With 2-second counting freq,
 *    the EXP_n values would be 1981, 2034 and 2043 if still using only
 *    11 bit fractions.
 */
extern unsigned long avenrun[]; 	/* Load averages */

#define FSHIFT		11		/* nr of bits of precision */
#define FIXED_1 	(1<<FSHIFT)     /* 1.0 as fixed-point */
#define LOAD_FREQ	(5*HZ)          /* 5 sec intervals */
#define EXP_1		1884		/* 1/exp(5sec/1min) as fixed-point */
#define EXP_5		2014		/* 1/exp(5sec/5min) */
#define EXP_15		2037		/* 1/exp(5sec/15min) */

#define CALC_LOAD(load,exp,n) \
	load *= exp; \
	load += n*(FIXED_1-exp); \
	load >>= FSHIFT;

#define CT_TO_SECS(x)   ((x) / HZ)
#define CT_TO_USECS(x)  (((x) % HZ) * 1000000/HZ)

#define FIRST_TASK task[0]
#define LAST_TASK task[NR_TASKS-1]

#include <linux/head.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/signal.h>
#include <linux/time.h>
#include <linux/param.h>
#include <linux/resource.h>
#include <linux/vm86.h>
#include <linux/math_emu.h>

#define TASK_RUNNING		0
#define TASK_INTERRUPTIBLE	1
#define TASK_UNINTERRUPTIBLE	2
#define TASK_ZOMBIE		3
#define TASK_STOPPED		4
#define TASK_SWAPPING		5

#ifndef NULL
#define NULL ((void *) 0)
#endif

#define MAX_SHARED_LIBS 16

extern void sched_init(void);
extern void show_state(void);
extern void schedule(void);
extern void trap_init(void);
extern void panic(const char * str);

extern int sys_fork(void);   /* defined in fork.c */

typedef int (*fn_ptr)();

/* do not change this! (without changing resume) */
typedef struct {
    unsigned long   regs[13];		/* data/address registers */
} label_t;

int setjmp(label_t *);

volatile void longjmp(label_t *,int);

struct task_struct {
/* these are hardcoded - don't touch */
    long state;			/* -1 unrunnable, 0 runnable, >0 stopped */
    long counter;
    long priority;
    unsigned long signal;
    unsigned long blocked;	/* bitmap of masked signals */
    unsigned long flags;	/* per process flags, defined below */
    /* various fields */
    struct sigaction sigaction[32];
    unsigned long kernel_stack_page;
    int exit_code;
    int dumpable:1;
    int swappable:1;
    unsigned long start_code,end_code,end_data,brk,start_stack;
    unsigned long arg_start, arg_end, env_start, env_end;
    long pid,pgrp,session,leader;
    int	groups[NGROUPS];
    /*
     * pointers to (original) parent process, youngest child, younger sibling,
     * older sibling, respectively.  (p->father can be replaced with
     * p->p_pptr->pid)
     */
    struct task_struct *p_opptr,*p_pptr, *p_cptr, *p_ysptr, *p_osptr;
    /*
     * For ease of programming... Normal sleeps don't need to
     * keep track of a wait-queue: every task has an entry of it's own
     */
    unsigned short uid,euid,suid;
    unsigned short gid,egid,sgid;
    unsigned long timeout;
    unsigned long it_real_value, it_prof_value, it_virt_value;
    unsigned long it_real_incr, it_prof_incr, it_virt_incr;
    long utime,stime,cutime,cstime,start_time;
    unsigned long min_flt, maj_flt;
    unsigned long cmin_flt, cmaj_flt;
    struct rlimit rlim[RLIM_NLIMITS];
    unsigned short used_math;
    unsigned short rss;	/* number of resident pages */
    char comm[16];
    /* file system info */
    int link_count;
    int tty;		/* -1 if no tty, so it must be signed */
    unsigned short umask;
    struct inode * pwd;
    struct inode * root;
    struct inode * executable;
    struct vm_area_struct * mmap;
    struct {
	struct inode * library;
	unsigned long start;
	unsigned long length;
	unsigned long bss;
    } libraries[MAX_SHARED_LIBS];
    int numlibraries;
    struct file * filp[NR_OPEN];
    fd_set close_on_exec;
    
    pmap_t		pmap;		/* process map */
    unsigned short	sr;		/* status register */
    label_t 	swtch;		/* for context switch */
    label_t 	*pjmp;		/* points to a jump area */
    label_t 	jmpbuff;	/* for longjmp */
    
    struct wait_queue wait; 	/* for when the task waits */
    /* with a per-process stack at the
       same virtual address, one cannot
       use stack allocated wait_queue
       structures, as they must be
       accessible from interrupts */
};

/*
 * Per process flags
 */
#define PF_PTRACED	0x00000010	/* set if ptrace (0) has been called. */
#define PF_TRACESYS	0x00000020	/* tracing system calls */

/*
 *  INIT_TASK is used to set up the first task table, touch at
 * your own risk!. Base=0, limit=0x1fffff (=2MB)
 */
#define INIT_TASK \
/* state etc */ { 0,15,15,0,0,0, \
/* signals */	{{ 0, },}, \
/* stack */     0, \
/* ec,brk... */ 0,0,0,0,0,0,0,0, \
/* argv.. */	0,0,0,0, \
/* pid etc.. */ 0,0,0,0, \
/* suppl grps*/ {NOGROUP,}, \
/* proc links*/ &init_task,&init_task,NULL,NULL,NULL, \
/* uid etc */	0,0,0,0,0,0, \
/* timeout */	0,0,0,0,0,0,0,0,0,0,0,0, \
/* min_flt */	0,0,0,0, \
/* rlimits */	{ {0x7fffffff, 0x7fffffff}, {0x7fffffff, 0x7fffffff},  \
		  {0x7fffffff, 0x7fffffff}, {0x7fffffff, 0x7fffffff}, \
		  {0x7fffffff, 0x7fffffff}, {0x7fffffff, 0x7fffffff}}, \
/* math */	0, \
/* rss */	2, \
/* comm */	"swapper", \
/* fs info */	0,-1,0022,NULL,NULL,NULL,NULL, \
/* libraries */ { { NULL, 0, 0, 0}, }, 0, \
/* filp */	{NULL,}, \
/* cloe */	{{ 0, }}, \
/* l1_tbl*/	0, \
/* crp */	{0,}, \
/* sr  */	0, \
/* swtch */	{0,}, \
/* pjmp */	0, \
/* jmpbuff */	{0,}, \
/* wait */	{0,} \
}

extern struct task_struct *task[NR_TASKS];
extern struct task_struct *last_task_used_math;
extern struct task_struct *current;
extern unsigned long volatile jiffies;
extern unsigned long startup_time;
extern int jiffies_offset;
extern int need_resched;
extern int hard_math;

#define CURRENT_TIME (startup_time+(jiffies+jiffies_offset)/HZ)

extern void add_timer(long jiffies, void (*fn)(void));

extern void sleep_on(struct wait_queue ** p);
extern void interruptible_sleep_on(struct wait_queue ** p);
extern void wake_up(struct wait_queue ** p);
extern void wake_up_interruptible(struct wait_queue ** p);

extern int send_sig(unsigned long sig,struct task_struct * p,int priv);
extern int in_group_p(gid_t grp);

extern int request_irq(unsigned int irq,void (*handler)(int));
extern void free_irq(unsigned int irq);
extern int irqaction(unsigned int irq,struct sigaction * new);

static inline void flush_cache(void)
{
    __asm__ __volatile__ ("movec cacr,d0\n\t"
			  "oril  #0x00000808,d0\n\t"
			  "movec d0,cacr");
}

/*
 * Entry into gdt where to find first TSS. 0-nul, 1-cs, 2-ds, 3-syscall
 * 4-TSS0, 5-LDT0, 6-TSS1 etc ...
 */
#define FIRST_TSS_ENTRY 4
#define FIRST_LDT_ENTRY (FIRST_TSS_ENTRY+1)
#define _TSS(n) ((((unsigned long) n)<<4)+(FIRST_TSS_ENTRY<<3))
#define _LDT(n) ((((unsigned long) n)<<4)+(FIRST_LDT_ENTRY<<3))
#define load_TR(n) __asm__("ltr %%ax"::"a" (_TSS(n)))
#define load_ldt(n) __asm__("lldt %%ax"::"a" (_LDT(n)))
#define store_TR(n) \
__asm__("str %%ax\n\t" \
	"subl %2,%%eax\n\t" \
	"shrl $4,%%eax" \
	:"=a" (n) \
	:"0" (0),"i" (FIRST_TSS_ENTRY<<3))
/*
 *	switch_to(n) should switch tasks to task nr n, first
 * checking that n isn't the current task, in which case it does nothing.
 * This also clears the TS-flag if the task we switched to has used
 * tha math co-processor latest.
 */
#ifdef i386
#define switch_to(n) {\
struct {long a,b;} __tmp; \
__asm__("cmpl %%ecx,_current\n\t" \
	"je 1f\n\t" \
	"movw %%dx,%1\n\t" \
	"cli\n\t" \
	"xchgl %%ecx,_current\n\t" \
	"ljmp %0\n\t" \
	"sti\n\t" \
	"cmpl %%ecx,_last_task_used_math\n\t" \
	"jne 1f\n\t" \
	"clts\n" \
	"1:" \
	::"m" (*&__tmp.a),"m" (*&__tmp.b), \
	"d" (_TSS(n)),"c" ((long) task[n]) \
	:"cx"); \
}
#else
#define switch_to(n) \
    if (current != task[n]) \
	resume(task[n]);
#endif


#define PAGE_ALIGN(n) (((n)+0xfff)&0xfffff000)

#define _set_base(addr,base) \
__asm__("movw %%dx,%0\n\t" \
	"rorl $16,%%edx\n\t" \
	"movb %%dl,%1\n\t" \
	"movb %%dh,%2" \
	::"m" (*((addr)+2)), \
	  "m" (*((addr)+4)), \
	  "m" (*((addr)+7)), \
	  "d" (base) \
	:"dx")

#define _set_limit(addr,limit) \
__asm__("movw %%dx,%0\n\t" \
	"rorl $16,%%edx\n\t" \
	"movb %1,%%dh\n\t" \
	"andb $0xf0,%%dh\n\t" \
	"orb %%dh,%%dl\n\t" \
	"movb %%dl,%1" \
	::"m" (*(addr)), \
	  "m" (*((addr)+6)), \
	  "d" (limit) \
	:"dx")

#define set_base(ldt,base) _set_base( ((char *)&(ldt)) , base )
#define set_limit(ldt,limit) _set_limit( ((char *)&(ldt)) , (limit-1)>>12 )

/*
 * The wait-queues are circular lists, and you have to be *very* sure
 * to keep them correct. Use only these two functions to add/remove
 * entries in the queues.
 */
static inline void add_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
{
    unsigned long flags;

#ifdef DEBUG
    if (wait->next) {
	unsigned long pc;
	__asm__ __volatile__("movel sp@,%0" : "=r" (pc));
	printk("add_wait_queue (%08x): wait->next = %08x\n",pc,wait->next);
    }
#endif
    save_flags(flags);
    cli();
    if (!*p) {
	wait->next = wait;
	*p = wait;
    } else {
	wait->next = (*p)->next;
	(*p)->next = wait;
    }
    restore_flags(flags);
}

static inline void remove_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
{
	unsigned long flags;
	struct wait_queue * tmp;
#ifdef DEBUG
	unsigned long ok = 0;
#endif

	save_flags(flags);
	cli();
	if ((*p == wait) &&
#ifdef DEBUG
	    (ok = 1) &&
#endif
	    ((*p = wait->next) == wait)) {
		*p = NULL;
	} else {
		tmp = wait;
		while (tmp->next != wait) {
			tmp = tmp->next;
#ifdef DEBUG
			if (tmp == *p)
				ok = 1;
#endif
		}
		tmp->next = wait->next;
	}
	wait->next = NULL;
	restore_flags(flags);
#ifdef DEBUG
	if (!ok) {
		printk("removed wait_queue not on list.\n");
		printk("list = %08x, queue = %08x\n",p,wait);
		__asm__ __volatile__ ("movel sp@,%0" : "=r" (ok));
		printk("pc = %08x\n",ok);
	}
#endif
}

static inline void select_wait(struct wait_queue ** wait_address, select_table * p)
{
	struct select_table_entry * entry;

	if (!p || !wait_address)
		return;
	if (p->nr >= __MAX_SELECT_TABLE_ENTRIES)
		return;
	entry = p->entry + p->nr;
	entry->wait_address = wait_address;
	entry->wait.task = current;
	entry->wait.next = NULL;
	add_wait_queue(wait_address,&entry->wait);
	p->nr++;
}

static inline unsigned long _get_base(char * addr)
{
	unsigned long __base;
	__asm__("movb %3,%%dh\n\t"
		"movb %2,%%dl\n\t"
		"shll $16,%%edx\n\t"
		"movw %1,%%dx"
		:"=&d" (__base)
		:"m" (*((addr)+2)),
		 "m" (*((addr)+4)),
		 "m" (*((addr)+7)));
	return __base;
}

#define get_base(ldt) _get_base( ((char *)&(ldt)) )

static inline unsigned long get_limit(unsigned long segment)
{
	unsigned long __limit;
	__asm__("lsll %1,%0"
		:"=r" (__limit):"r" (segment));
	return __limit+1;
}

#define REMOVE_LINKS(p) \
	if ((p)->p_osptr) \
		(p)->p_osptr->p_ysptr = (p)->p_ysptr; \
	if ((p)->p_ysptr) \
		(p)->p_ysptr->p_osptr = (p)->p_osptr; \
	else \
		(p)->p_pptr->p_cptr = (p)->p_osptr

#define SET_LINKS(p) \
	(p)->p_ysptr = NULL; \
	if (((p)->p_osptr = (p)->p_pptr->p_cptr) != NULL) \
		(p)->p_osptr->p_ysptr = p; \
	(p)->p_pptr->p_cptr = p

#endif
