/*
 *  linux/kernel/sys.c
 *
 *  (C) 1991  Linus Torvalds
 */

#include <errno.h>
#include <asm/io.h>
#include <linux/sched.h>
#include <linux/tty.h>
#include <linux/kernel.h>
#include <asm/segment.h>
#include <sys/times.h>
#include <sys/utsname.h>

int sys_ftime()
{
	return -ENOSYS;
}

int sys_break()
{
	return -ENOSYS;
}

int sys_ptrace()
{
	return -ENOSYS;
}

int sys_stty()
{
	return -ENOSYS;
}

int sys_gtty()
{
	return -ENOSYS;
}

int sys_rename()
{
	return -ENOSYS;
}

int sys_prof()
{
	return -ENOSYS;
}

int sys_setregid(int rgid, int egid)
{
	if (rgid>0) {
		if ((current->gid == rgid) || 
		    suser())
			current->gid = rgid;
		else
			return(-EPERM);
	}
	if (egid>0) {
		if ((current->gid == egid) ||
		    (current->egid == egid) ||
		    suser()) {
			current->egid = egid;
			current->sgid = egid;
		} else
			return(-EPERM);
	}
	return 0;
}

int sys_setgid(int gid)
{
/*	return(sys_setregid(gid, gid)); */
	if (suser())
		current->gid = current->egid = current->sgid = gid;
	else if ((gid == current->gid) || (gid == current->sgid))
		current->egid = gid;
	else
		return -EPERM;
	return 0;
}

int sys_acct()
{
	return -ENOSYS;
}

int sys_phys()
{
	return -ENOSYS;
}

int sys_lock()
{
	return -ENOSYS;
}

int sys_mpx()
{
	return -ENOSYS;
}

int sys_ulimit()
{
	return -ENOSYS;
}

int sys_time(long * tloc)
{
	int i;

	i = CURRENT_TIME;
	if (tloc) {
		verify_area(tloc,4);
		put_fs_long(i,(unsigned long *)tloc);
	}
	return i;
}

/*
 * Unprivileged users may change the real user id to the effective uid
 * or vice versa.
 */
int sys_setreuid(int ruid, int euid)
{
	int old_ruid = current->uid;
	
	if (ruid>0) {
		if ((current->euid==ruid) ||
                    (old_ruid == ruid) ||
		    suser())
			current->uid = ruid;
		else
			return(-EPERM);
	}
	if (euid>0) {
		if ((old_ruid == euid) ||
                    (current->euid == euid) ||
		    suser()) {
			current->euid = euid;
			current->suid = euid;
		} else {
			current->uid = old_ruid;
			return(-EPERM);
		}
	}
	return 0;
}

int sys_setuid(int uid)
{
/*	return(sys_setreuid(uid, uid)); */
	if (suser())
		current->uid = current->euid = current->suid = uid;
	else if ((uid == current->uid) || (uid == current->suid))
		current->euid = uid;
	else
		return -EPERM;
	return(0);
}

int sys_stime(long * tptr)
{
	if (!suser())
		return -EPERM;
	startup_time = get_fs_long((unsigned long *)tptr) - jiffies/HZ;
	return 0;
}

int sys_times(struct tms * tbuf)
{
	if (tbuf) {
		verify_area(tbuf,sizeof *tbuf);
		put_fs_long(current->utime,(unsigned long *)&tbuf->tms_utime);
		put_fs_long(current->stime,(unsigned long *)&tbuf->tms_stime);
		put_fs_long(current->cutime,(unsigned long *)&tbuf->tms_cutime);
		put_fs_long(current->cstime,(unsigned long *)&tbuf->tms_cstime);
	}
	return jiffies;
}

int sys_brk(unsigned long end_data_seg)
{
	if (end_data_seg >= current->end_code &&
	    end_data_seg < current->start_stack - 16384)
		current->brk = end_data_seg;
	return current->brk;
}

/*
 * This needs some heave checking ...
 * I just haven't get the stomach for it. I also don't fully
 * understand sessions/pgrp etc. Let somebody who does explain it.
 */
int sys_setpgid(int pid, int pgid)
{
	int i;

	if (!pid)
		pid = current->pid;
	if (!pgid)
		pgid = current->pid;
	for (i=0 ; i<NR_TASKS ; i++)
		if (task[i] && task[i]->pid==pid) {
			if (task[i]->leader)
				return -EPERM;
			if (task[i]->session != current->session)
				return -EPERM;
			task[i]->pgrp = pgid;
			return 0;
		}
	return -ESRCH;
}

int sys_getpgrp(void)
{
	return current->pgrp;
}

int sys_setsid(void)
{
	if (current->leader && !suser())
		return -EPERM;
	current->leader = 1;
	current->session = current->pgrp = current->pid;
	current->tty = -1;
	return current->pgrp;
}

int sys_getgroups()
{
	return -ENOSYS;
}

int sys_setgroups()
{
	return -ENOSYS;
}

int sys_uname(struct utsname * name)
{
	static struct utsname thisname = {
		"linux .0","nodename","release ","version ","machine "
	};
	int i;

	if (!name) return -ERROR;
	verify_area(name,sizeof *name);
	for(i=0;i<sizeof *name;i++)
		put_fs_byte(((char *) &thisname)[i],i+(char *) name);
	return 0;
}

int sys_sethostname()
{
	return -ENOSYS;
}

int sys_getrlimit()
{
	return -ENOSYS;
}

int sys_setrlimit()
{
	return -ENOSYS;
}

int sys_getrusage()
{
	return -ENOSYS;
}

int sys_gettimeofday()
{
	return -ENOSYS;
}

int sys_settimeofday()
{
	return -ENOSYS;
}


int sys_umask(int mask)
{
	int old = current->umask;

	current->umask = mask & 0777;
	return (old);
}
#define cursor_side 6
static int flag=0;
int sys_init_graphics(int color)
{
	int i;
	char *p=vga_graph_memstart;
	if(!flag){
		/* 图形控制器,拼接器有关寄存器端口是0x3CE和0x3C4 */
		/* 先往寄存器端口写索引号,端口地址下一个写命令或读入数据 */
		/* 在0x3CE端口中有对应索引号0x06的寄存器,
		第0位用来控制字符模式,1启动图形模式;2,3位决定显存位置*/
		/* 0x3CE中索引号0x05的寄存器,
		第六位设置为1,设定为256色*/
		outb(0x05,0x3CE);
		outb(0x40,0x3CF);/* 设定256色,且取出方式为移动拼装*/

		/*显存区域 A0000H - AFFFFH(64KB) */
		outb(0x06,0x3CE);
		outb(0x05,0x3CF);/*设定现存的地址区域,禁止字符模式*/

		/*0x4C4下的索引号为0x04的寄存器
		第四位为chain4,用于将4个显存片连在一起*/
		outb(0x04,0x3C4);
		outb(0x08,0x3C5);/*设定将4个现存片连在一起*/

		/*设置End Horizontal Display 为79*/
		/*用于控制在屏幕上显示的扫描线长度 */
		outb(0x01,0x3D4);
		outb(0x4F,0x3D5);
		/*0x03寄存器,0-4位表示End Horizontal Blanking
		5,6位表示Display Enable Skew;7位表示EVRA*/
		outb(0x03,0x3D4);
		outb(0x82,0x3D5); //设置Skew 为0

		outb(0x07,0x3D4);
		outb(0x1F,0x3D5);/*vertical display end No8,9 bit=1,0*/

		outb(0x12,0x3D4);
		outb(0x8F,0x3D5);/*vertical display end low 7b =0x8F*/
		/*CRT控制器中索引号为0x17的寄存器中的SLDIV位设置为1,
		Scanline Counter加一的周期为两个扫描周期*/
		outb(0x17,0x3D4);
		outb(0xA3,0x3D5);/*SLDIV=1 ,scanline clock/2*/

		outb(0x14,0x3D4);
		outb(0x40,0x3D5);/*DW=1*/
		/*移动距离 = 2*Memory Address Size
		* Offset Register/Pixels Per Address*/
		outb(0x13,0x3D4);
		outb(0x28,0x3D5);/*Offset=40, 20:bian chang*/
		/*开始绘制屏幕*/
		outb(0x0C,0x3D4);
		outb(0x00,0x3D5);
		outb(0x0D,0x3D4);
		outb(0x00,0x3D5);/*Start Address=0xA0000*/
		flag=1;
	}
	
	for(i=0;i<vga_graph_memsize;i++) *p++=color; //背景色 3-blue 4-red 12-purple
    // for(i=x-cursor_side;i<=x+cursor_side;i++)
    //     for(j=y-cursor_side;j<=y+cursor_side;j++){
    //         p=(char *) vga_graph_memstart+j*320+i;
    //         *p=12; //鼠标为红色
    //     }	
	return 0;
}

message *message_head=NULL;
message *message_tail=NULL;
int sys_get_message(unsigned char *msg)
{
	if(!message_head){ 
		put_fs_byte(0,msg);	//空
		return 1;
	}
	message *m=message_head;
	message_head=message_head->next;
	put_fs_byte(m->mid,msg);	//消息的id类型
	free(m);
	return 1;
}


user_timer *timer_head=NULL;
int sys_timer_create(long milliseconds,int type)
{
	long jiffies = milliseconds / 10;
	user_timer *timer = (user_timer*)malloc(sizeof(user_timer));
	timer->init_jiffies=jiffies;
	timer->jiffies=jiffies;
	timer->next=timer_head;
	timer->type=type;
	timer->pid=-1;
	timer_head=timer;
	return 1;
}

int sys_paint(unsigned char *ob){
	int i,j,x,y,w,h,color;
	x=get_fs_byte(ob);
	y=get_fs_byte(ob+1);
	w=get_fs_byte(ob+2);
	h=get_fs_byte(ob+3);
	color=get_fs_byte(ob+4);
	w+=x;
	h+=y;
	char *p;
	for(i=x;i<=w;i++){
		for(j=y;j<=h;j++){
			p=(char *)vga_graph_memstart+j*vga_width+i;
			*p=color;
		}
	}
	return 1;
}