/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by Mike Parker.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include <errno.h>
#include <vax/psl.h>

#include "syscall.h"
#include "regs.h"

extern int errno;

/* for nonexistent syscalls */
int sys_nosys();

/* syscalls with the same numbers as in 4.3 */
int sys_brk();
int sys_cmkrnl();
int sys_close();
int sys_exit();
int sys_fstat();
int sys_getpagesize();
int sys_ioctl();
int sys_read();
int sys_write();

/* syscalls peculiar to the slave kernel */
int sys_gettime100();
int sys_getmapplace();
int sys_gettimesch();
int sys_schslippage();
int sys_sendsignal();
int sys_ast100();
int sys_astsch();
int sys_getmem();
int sys_clearast();
int sys_blockast();
int sys_handlesig();
int sys_badaddr();
int sys_debugger();
int sys_halt();
int sys_exportclock();
int sys_schedast();
int sys_physmem();

#define NOSYS { 0, 0x7fffffff, sys_nosys }
struct {
  int minargs;
  int maxargs;
  int (*handler)(); } syscalltbl[] = { NOSYS,			/* 0 = (illegal) */
				       { 1, 1, sys_exit },	/* 1 */
				       NOSYS,			/* 2 = fork */
				       { 3, 3, sys_read },	/* 3 */
				       { 3, 3, sys_write },	/* 4 */
				       NOSYS,			/* 5 = open */
				       { 1, 1, sys_close },	/* 6 = close */
				       NOSYS,			/* 7 = old: wait */
				       NOSYS,			/* 8 = creat */
				       NOSYS,			/* 9 = link */
				       NOSYS,			/* 10 = unlink */
				       NOSYS,			/* 11 = execv */
				       NOSYS,			/* 12 = chdir */
				       NOSYS,			/* 13 = old: time */
				       NOSYS,			/* 14 = mknod */
				       NOSYS,			/* 15 = chmod */
				       NOSYS,			/* 16 = chown */
				       { 1, 1, sys_brk },	/* 17 = old: sbreak (brk??) */
				       NOSYS,			/* 18 = old: stat */
				       NOSYS,			/* 19 = lseek */
				       NOSYS,			/* 20 = getpid */
				       NOSYS,			/* 21 = mount */
				       NOSYS,			/* 22 = umount */
				       NOSYS,			/* 23 = old: setuid */
				       NOSYS,			/* 24 = getuid */
				       NOSYS,			/* 25 = old: stime */
				       NOSYS,			/* 26 = ptrace */
				       NOSYS,			/* 27 = old: alarm */
				       NOSYS,			/* 28 = old: fstat */
				       NOSYS,			/* 29 = old: pause */
				       NOSYS,			/* 30 = old: utime */
				       NOSYS,			/* 31 = old: stty */
				       NOSYS,			/* 32 = old: gtty */
				       NOSYS,			/* 33 = access */
				       NOSYS,			/* 34 = old: nice */
				       NOSYS,			/* 35 = old: ftime */
				       NOSYS,			/* 36 = sync */
				       NOSYS,			/* 37 = kill */
				       NOSYS,			/* 38 = stat */
				       NOSYS,			/* 39 = old: setpgrp */
				       NOSYS,			/* 40 = lstat */
				       NOSYS,			/* 41 = dup */
				       NOSYS,			/* 42 = pipe */
				       NOSYS,			/* 43 = old: times */
				       NOSYS,			/* 44 = profil */
				       NOSYS,			/* 45 = (unused) */
				       NOSYS,			/* 46 = old: setgid */
				       NOSYS,			/* 47 = getgid */
				       NOSYS,			/* 48 = old: sigsys */
				       NOSYS,			/* 49 = reserved to USG */
				       NOSYS,			/* 50 = reserved to USG */
				       NOSYS,			/* 51 = acct */
				       NOSYS,			/* 52 = old: phys */
				       NOSYS,			/* 53 = old: syslock */
				       { 3, 3, sys_ioctl },	/* 54 = ioctl */
				       NOSYS,			/* 55 = reboot */
				       NOSYS,			/* 56 = old: mpxchan */
				       NOSYS,			/* 57 = symlink */
				       NOSYS,			/* 58 = readlink */
				       NOSYS,			/* 59 = execve */
				       NOSYS,			/* 60 = umask */
				       NOSYS,			/* 61 = chroot */
				       { 2, 2, sys_fstat },	/* 62 = fstat */
				       NOSYS,			/* 63 = reserved */
				       { 0, 0, sys_getpagesize }, /* 64 */
				       NOSYS,			/* 65 = mremap */
				       NOSYS,			/* 66 = vfork */
				       NOSYS,			/* 67 = old: vread */
				       NOSYS,			/* 68 = old: vwrite */
				       NOSYS,			/* 69 = sbrk */
				       NOSYS,			/* 70 = sstk */
				       NOSYS,			/* 71 = mmap */
				       NOSYS,			/* 72 = old: vadvise */
				       NOSYS,			/* 73 = munmap */
				       NOSYS,			/* 74 = mprotect */
				       NOSYS,			/* 75 = madvise */
				       NOSYS,			/* 76 = vhangup */
				       NOSYS,			/* 77 = old: vlimit */
				       NOSYS,			/* 78 = mincore */
				       NOSYS,			/* 79 = getgroups */
				       NOSYS,			/* 80 = setgroups */
				       NOSYS,			/* 81 = getpgrp */
				       NOSYS,			/* 82 = setpgrp */
				       NOSYS,			/* 83 = setitimer */
				       NOSYS,			/* 84 = wait */
				       NOSYS,			/* 85 = swapon */
				       NOSYS,			/* 86 = getitimer */
				       NOSYS,			/* 87 = gethostname */
				       NOSYS,			/* 88 = sethostname */
				       NOSYS,			/* 89 = getdtablesize */
				       NOSYS,			/* 90 = dup2 */
				       NOSYS,			/* 91 = getdopt */
				       NOSYS,			/* 92 = fcntl */
				       NOSYS,			/* 93 = select */
				       NOSYS,			/* 94 = setdopt */
				       NOSYS,			/* 95 = fsync */
				       NOSYS,			/* 96 = setpriority */
				       NOSYS,			/* 97 = socket */
				       NOSYS,			/* 98 = connect */
				       NOSYS,			/* 99 = accept */
				       NOSYS,			/* 100 = getpriority */
				       NOSYS,			/* 101 = send */
				       NOSYS,			/* 102 = recv */
				       NOSYS,			/* 103 = sigreturn */
				       NOSYS,			/* 104 = bind */
				       NOSYS,			/* 105 = setsockopt */
				       NOSYS,			/* 106 = listen */
				       NOSYS,			/* 107 = old: vtimes */
				       NOSYS,			/* 108 = sigvec */
				       NOSYS,			/* 109 = sigblock */
				       NOSYS,			/* 110 = sigsetmask */
				       NOSYS,			/* 111 = sigpause */
				       NOSYS,			/* 112 = sigstack */
				       NOSYS,			/* 113 = recvmsg */
				       NOSYS,			/* 114 = sendmsg */
				       NOSYS,			/* 115 = old: vtrace */
				       NOSYS,			/* 116 = gettimeofday */
				       NOSYS,			/* 117 = getrusage */
				       NOSYS,			/* 118 = getsockopt */
				       NOSYS,			/* 119 = resuba */
				       NOSYS,			/* 120 = readv */
				       NOSYS,			/* 121 = writev */
				       NOSYS,			/* 122 = settimeofday */
				       NOSYS,			/* 123 = fchown */
				       NOSYS,			/* 124 = fchmod */
				       NOSYS,			/* 125 = recvfrom */
				       NOSYS,			/* 126 = setreuid */
				       NOSYS,			/* 127 = setregid */
				       NOSYS,			/* 128 = rename */
				       NOSYS,			/* 129 = truncate */
				       NOSYS,			/* 130 = ftruncate */
				       NOSYS,			/* 131 = flock */
				       NOSYS,			/* 132 = (unused) */
				       NOSYS,			/* 133 = sendto */
				       NOSYS,			/* 134 = shutdown */
				       NOSYS,			/* 135 = socketpair */
				       NOSYS,			/* 136 = mkdir */
				       NOSYS,			/* 137 = rmdir */
				       NOSYS,			/* 138 = utimes */
				       NOSYS,			/* 139 = internal */
				       NOSYS,			/* 140 = adjtime */
				       NOSYS,			/* 141 = getpeername */
				       NOSYS,			/* 142 = gethostid */
				       NOSYS,			/* 143 = sethostid */
				       NOSYS,			/* 144 = getrlimit */
				       NOSYS,			/* 145 = setrlimit */
				       NOSYS,			/* 146 = killpg */
				       NOSYS,			/* 147 = (unused) */
				       NOSYS,			/* 148 = setquota */
				       NOSYS,			/* 149 = quota */
				       NOSYS,			/* 150 = getsockname */
				       NOSYS,			/* 151 = reserved */
				       NOSYS,			/* 152 = reserved */
				       NOSYS,			/* 153 = reserved */
				       NOSYS,			/* 154 = reserved */
				       NOSYS,			/* 155 = reserved */
				       NOSYS,			/* 156 = reserved */
				       NOSYS,			/* 157 = reserved */
				       NOSYS,			/* 158 = reserved */
				       NOSYS,			/* 159 = reserved */
				       NOSYS,			/* 160 = reserved */
				       NOSYS,			/* 161 = reserved */
				       NOSYS,			/* 162 = reserved */
				       NOSYS,			/* 163 = reserved */
				       NOSYS,			/* 164 = reserved */
				       NOSYS,			/* 165 = reserved */
				       NOSYS,			/* 166 = reserved */
				       NOSYS,			/* 167 = reserved */
				       NOSYS,			/* 168 = reserved */
				       NOSYS,			/* 169 = reserved */
				       NOSYS,			/* 170 = reserved */
				       NOSYS,			/* 171 = reserved */
				       NOSYS,			/* 172 = reserved */
				       NOSYS,			/* 173 = reserved */
				       NOSYS,			/* 174 = reserved */
				       NOSYS,			/* 175 = reserved */
				       NOSYS,			/* 176 = reserved */
				       NOSYS,			/* 177 = reserved */
				       NOSYS,			/* 178 = reserved */
				       NOSYS,			/* 179 = reserved */
				       NOSYS,			/* 180 = reserved */
				       NOSYS,			/* 181 = reserved */
				       NOSYS,			/* 182 = reserved */
				       NOSYS,			/* 183 = reserved */
				       NOSYS,			/* 184 = reserved */
				       NOSYS,			/* 185 = reserved */
				       NOSYS,			/* 186 = reserved */
				       NOSYS,			/* 187 = reserved */
				       NOSYS,			/* 188 = reserved */
				       NOSYS,			/* 189 = reserved */
				       NOSYS,			/* 190 = reserved */
				       NOSYS,			/* 191 = reserved */
				       NOSYS,			/* 192 = reserved */
				       NOSYS,			/* 193 = reserved */
				       NOSYS,			/* 194 = reserved */
				       NOSYS,			/* 195 = reserved */
				       NOSYS,			/* 196 = reserved */
				       NOSYS,			/* 197 = reserved */
				       NOSYS,			/* 198 = reserved */
				       NOSYS,			/* 199 = reserved */
				       NOSYS,			/* 200 = reserved */
				       NOSYS,			/* 201 = reserved */
				       NOSYS,			/* 202 = reserved */
				       NOSYS,			/* 203 = reserved */
				       NOSYS,			/* 204 = reserved */
				       NOSYS,			/* 205 = reserved */
				       NOSYS,			/* 206 = reserved */
				       NOSYS,			/* 207 = reserved */
				       NOSYS,			/* 208 = reserved */
				       NOSYS,			/* 209 = reserved */
				       NOSYS,			/* 210 = reserved */
				       NOSYS,			/* 211 = reserved */
				       NOSYS,			/* 212 = reserved */
				       NOSYS,			/* 213 = reserved */
				       NOSYS,			/* 214 = reserved */
				       NOSYS,			/* 215 = reserved */
				       NOSYS,			/* 216 = reserved */
				       NOSYS,			/* 217 = reserved */
				       NOSYS,			/* 218 = reserved */
				       NOSYS,			/* 219 = reserved */
				       NOSYS,			/* 220 = reserved */
				       NOSYS,			/* 221 = reserved */
				       NOSYS,			/* 222 = reserved */
				       NOSYS,			/* 223 = reserved */
				       NOSYS,			/* 224 = reserved */
				       NOSYS,			/* 225 = reserved */
				       NOSYS,			/* 226 = reserved */
				       NOSYS,			/* 227 = reserved */
				       NOSYS,			/* 228 = reserved */
				       NOSYS,			/* 229 = reserved */
				       NOSYS,			/* 230 = reserved */
				       NOSYS,			/* 231 = reserved */
				       NOSYS,			/* 232 = reserved */
				       NOSYS,			/* 233 = reserved */
				       NOSYS,			/* 234 = reserved */
				       NOSYS,			/* 235 = reserved */
				       NOSYS,			/* 236 = reserved */
				       NOSYS,			/* 237 = reserved */
				       NOSYS,			/* 238 = reserved */
				       NOSYS,			/* 239 = reserved */
				       NOSYS,			/* 240 = reserved */
				       NOSYS,			/* 241 = reserved */
				       NOSYS,			/* 242 = reserved */
				       NOSYS,			/* 243 = reserved */
				       NOSYS,			/* 244 = reserved */
				       NOSYS,			/* 245 = reserved */
				       NOSYS,			/* 246 = reserved */
				       NOSYS,			/* 247 = reserved */
				       NOSYS,			/* 248 = reserved */
				       NOSYS,			/* 249 = reserved */
				       NOSYS,			/* 250 = reserved */
				       NOSYS,			/* 251 = reserved */
				       NOSYS,			/* 252 = reserved */
				       NOSYS,			/* 253 = reserved */
				       NOSYS,			/* 254 = reserved */
				       NOSYS,			/* 255 = reserved */
				       { 2, 2, sys_cmkrnl },	/* 256 = cmkrnl */
				       NOSYS,			/* 257 = loadavg */
				       NOSYS,			/* 258 = map */
				       NOSYS,			/* 259 = unmap */
				       NOSYS,			/* 260 = dumpmap (unadvertised) */
				       NOSYS,			/* 261 = slock */
				       NOSYS,			/* 262 = sunlock */
				       NOSYS,			/* 263 = secverbose (unadvertised) */
				       NOSYS,			/* 264 = memlock */
				       NOSYS,			/* 265 = memunlock */
				       NOSYS,			/* 266 = map_physical */
				       NOSYS,			/* 267 = onexit */
				       NOSYS,			/* 268 = take_snapshot */
				       NOSYS,			/* 269 = read_snapshot */
				       NOSYS,			/* 270 = nfssvc_create */
				       NOSYS,			/* 271 = nfssvc_link */
				       NOSYS,			/* 272 = nfssvc_lookup */
				       NOSYS,			/* 273 = nfssvc_mkdir */
				       NOSYS,			/* 274 = nfssvc_read */
				       NOSYS,			/* 275 = nfssvc_readlink */
				       NOSYS,			/* 276 = nfssvc_remove */
				       NOSYS,			/* 277 = nfssvc_rename */
				       NOSYS,			/* 278 = nfssvc_rmdir */
				       NOSYS,			/* 279 = nfssvc_setattr */
				       NOSYS,			/* 280 = nfssvc_stat */
				       NOSYS,			/* 281 = nfssvc_statfs */
				       NOSYS,			/* 282 = nfssvc_symlink */
				       NOSYS,			/* 283 = nfssvc_write */
				       /* end of UNIX syscalls, now come the ones */
				       /* peculiar to the slave kernel */
				       { 0, 0, sys_gettime100 }, /* 284 */
				       { 1, 1, sys_getmapplace }, /* 285 */
				       { 0, 0, sys_gettimesch }, /* 286 */
				       { 0, 0, sys_schslippage }, /* 287 */
				       { 2, 2, sys_sendsignal }, /* 288 */
				       { 3, 3, sys_ast100 },	/* 289 */
				       { 3, 3, sys_astsch },	/* 290 */
				       { 1, 1, sys_getmem },	/* 291 */
				       { 0, 0, sys_clearast },	/* 292 */
				       { 0, 0, sys_blockast },	/* 293 */
				       { 2, 2, sys_handlesig },	/* 294 */
				       { 2, 2, sys_badaddr },	/* 295 */
				       { 0, 0, sys_debugger },	/* 296 */
				       { 0, 0, sys_halt },	/* 297 */
				       { 2, 2, sys_exportclock }, /* 298 */
				       { 4, 4, sys_schedast },	/* 299 */
				       { 0, 0, sys_physmem },	/* 300 */
				       };
int nsyscalls = sizeof(syscalltbl) / sizeof(syscalltbl[0]);

syscall(args,regs)
struct {
  int code;
  int pc;
  int psl; } *args;
int regs[];
{
 unsigned int nargs;
 int *uargs;

 if (prober(regs[AP],4))
  {
fault:
    regs[R0] = EFAULT;
    args->psl |= PSL_C;
    return;
  }
 nargs = * (unsigned int *) regs[AP];
 if (prober(regs[AP]+4,4*nargs))
  { goto fault;
  }
 if ((args->code < 0) || (args->code >= nsyscalls))
  { /* what to do for bad syscalls? */
    printf("syscall number (%d) out of range\n",args->code);
    regs[R0] = EINVAL;
    args->psl |= PSL_C;
    return;
  }
 if ( (nargs < syscalltbl[args->code].minargs) ||
      (nargs > syscalltbl[args->code].maxargs) )
  { printf("bad # of args %d to syscall %d (wants %d..%d)\n",
			nargs,
			args->code,
			syscalltbl[args->code].minargs,
			syscalltbl[args->code].maxargs );
    regs[R0] = EINVAL;
    args->psl |= PSL_C;
    return;
  }
 if ((*syscalltbl[args->code].handler)(args->code,nargs,(int *)(regs[AP]+4),regs,&args->pc,&args->psl))
  { args->psl |= PSL_C;
  }
}
