/*
       * linux/arch/i386/kernel/sys_i386.c
       *
       * This file contains various random system calls that
       * have a non-standard calling sequence on the Linux/i386
       * platform.
       */
      
      #include <linux/errno.h>
      #include <linux/sched.h>
      #include <linux/mm.h>
      #include <linux/smp.h>
      #include <linux/smp_lock.h>
      #include <linux/sem.h>
      #include <linux/msg.h>
      #include <linux/shm.h>
      #include <linux/stat.h>
      #include <linux/mman.h>
      #include <linux/file.h>
      #include <linux/utsname.h>
      
      #include <asm/uaccess.h>
      #include <asm/ipc.h>
      
      /*
       * sys_pipe() is the normal C calling standard for creating
       * a pipe. It's not the way Unix traditionally does this, though.
       */
  29  asmlinkage int sys_pipe(unsigned long * fildes)
      {
      	int fd[2];
      	int error;
      
      	error = do_pipe(fd);
  35  	if (!error) {
  36  		if (copy_to_user(fildes, fd, 2*sizeof(int)))
      			error = -EFAULT;
      	}
  39  	return error;
      }
      
      /* common code for old and new mmaps */
  43  static inline long do_mmap2(
      	unsigned long addr, unsigned long len,
      	unsigned long prot, unsigned long flags,
      	unsigned long fd, unsigned long pgoff)
      {
      	int error = -EBADF;
      	struct file * file = NULL;
      
      	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  52  	if (!(flags & MAP_ANONYMOUS)) {
      		file = fget(fd);
  54  		if (!file)
  55  			goto out;
      	}
      
      	down(¤t->mm->mmap_sem);
      	error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
      	up(¤t->mm->mmap_sem);
      
  62  	if (file)
      		fput(file);
      out:
  65  	return error;
      }
      
  68  asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
      	unsigned long prot, unsigned long flags,
      	unsigned long fd, unsigned long pgoff)
      {
  72  	return do_mmap2(addr, len, prot, flags, fd, pgoff);
      }
      
      /*
       * Perform the select(nd, in, out, ex, tv) and mmap() system
       * calls. Linux/i386 didn't use to be able to handle more than
       * 4 system call parameters, so these system calls used a memory
       * block for parameter passing..
       */
      
      struct mmap_arg_struct {
      	unsigned long addr;
      	unsigned long len;
      	unsigned long prot;
      	unsigned long flags;
      	unsigned long fd;
      	unsigned long offset;
      };
      
  91  asmlinkage int old_mmap(struct mmap_arg_struct *arg)
      {
      	struct mmap_arg_struct a;
      	int err = -EFAULT;
      
  96  	if (copy_from_user(&a, arg, sizeof(a)))
  97  		goto out;
      
      	err = -EINVAL;
 100  	if (a.offset & ~PAGE_MASK)
 101  		goto out;
      
      	err = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT);
      out:
 105  	return err;
      }
      
      
      extern asmlinkage int sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
      
      struct sel_arg_struct {
      	unsigned long n;
      	fd_set *inp, *outp, *exp;
      	struct timeval *tvp;
      };
      
 117  asmlinkage int old_select(struct sel_arg_struct *arg)
      {
      	struct sel_arg_struct a;
      
 121  	if (copy_from_user(&a, arg, sizeof(a)))
 122  		return -EFAULT;
      	/* sys_select() does the appropriate kernel locking */
 124  	return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp);
      }
      
      /*
       * sys_ipc() is the de-multiplexer for the SysV IPC calls..
       *
       * This is really horribly ugly.
       */
 132  asmlinkage int sys_ipc (uint call, int first, int second,
      			int third, void *ptr, long fifth)
      {
      	int version, ret;
      
      	version = call >> 16; /* hack for backward compatibility */
      	call &= 0xffff;
      
 140  	switch (call) {
 141  	case SEMOP:
 142  		return sys_semop (first, (struct sembuf *)ptr, second);
 143  	case SEMGET:
 144  		return sys_semget (first, second, third);
 145  	case SEMCTL: {
      		union semun fourth;
 147  		if (!ptr)
 148  			return -EINVAL;
 149  		if (get_user(fourth.__pad, (void **) ptr))
 150  			return -EFAULT;
 151  		return sys_semctl (first, second, third, fourth);
      	}
      
 154  	case MSGSND:
      		return sys_msgsnd (first, (struct msgbuf *) ptr, 
 156  				   second, third);
 157  	case MSGRCV:
 158  		switch (version) {
 159  		case 0: {
      			struct ipc_kludge tmp;
 161  			if (!ptr)
 162  				return -EINVAL;
      			
      			if (copy_from_user(&tmp,
      					   (struct ipc_kludge *) ptr, 
 166  					   sizeof (tmp)))
 167  				return -EFAULT;
      			return sys_msgrcv (first, tmp.msgp, second,
 169  					   tmp.msgtyp, third);
      		}
 171  		default:
      			return sys_msgrcv (first,
      					   (struct msgbuf *) ptr,
 174  					   second, fifth, third);
      		}
 176  	case MSGGET:
 177  		return sys_msgget ((key_t) first, second);
 178  	case MSGCTL:
 179  		return sys_msgctl (first, second, (struct msqid_ds *) ptr);
      
 181  	case SHMAT:
 182  		switch (version) {
 183  		default: {
      			ulong raddr;
      			ret = sys_shmat (first, (char *) ptr, second, &raddr);
 186  			if (ret)
 187  				return ret;
 188  			return put_user (raddr, (ulong *) third);
      		}
 190  		case 1:	/* iBCS2 emulator entry point */
 191  			if (!segment_eq(get_fs(), get_ds()))
 192  				return -EINVAL;
 193  			return sys_shmat (first, (char *) ptr, second, (ulong *) third);
      		}
 195  	case SHMDT: 
 196  		return sys_shmdt ((char *)ptr);
 197  	case SHMGET:
 198  		return sys_shmget (first, second, third);
 199  	case SHMCTL:
      		return sys_shmctl (first, second,
 201  				   (struct shmid_ds *) ptr);
 202  	default:
 203  		return -EINVAL;
      	}
      }
      
      /*
       * Old cruft
       */
 210  asmlinkage int sys_uname(struct old_utsname * name)
      {
      	int err;
 213  	if (!name)
 214  		return -EFAULT;
      	down_read(&uts_sem);
      	err=copy_to_user(name, &system_utsname, sizeof (*name));
      	up_read(&uts_sem);
 218  	return err?-EFAULT:0;
      }
      
 221  asmlinkage int sys_olduname(struct oldold_utsname * name)
      {
      	int error;
      
 225  	if (!name)
 226  		return -EFAULT;
 227  	if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
 228  		return -EFAULT;
        
        	down_read(&uts_sem);
      	
      	error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
      	error |= __put_user(0,name->sysname+__OLD_UTS_LEN);
      	error |= __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
      	error |= __put_user(0,name->nodename+__OLD_UTS_LEN);
      	error |= __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN);
      	error |= __put_user(0,name->release+__OLD_UTS_LEN);
      	error |= __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN);
      	error |= __put_user(0,name->version+__OLD_UTS_LEN);
      	error |= __copy_to_user(&name->machine,&system_utsname.machine,__OLD_UTS_LEN);
      	error |= __put_user(0,name->machine+__OLD_UTS_LEN);
      	
      	up_read(&uts_sem);
      	
      	error = error ? -EFAULT : 0;
      
 247  	return error;
      }
      
 250  asmlinkage int sys_pause(void)
      {
      	current->state = TASK_INTERRUPTIBLE;
      	schedule();
 254  	return -ERESTARTNOHAND;
      }