From 37da2899f40661e3e9631e497da8dc59b971cbd0 Mon Sep 17 00:00:00 2001 From: "Charles.Forsyth" Date: Fri, 22 Dec 2006 17:07:39 +0000 Subject: 20060303a --- emu/Unixware/asm-386.s | 71 +++++++ emu/Unixware/cmd.c | 205 ++++++++++++++++++++ emu/Unixware/deveia.c | 11 ++ emu/Unixware/devfs.c | 1 + emu/Unixware/emu | 106 +++++++++++ emu/Unixware/ipif.c | 1 + emu/Unixware/mkfile | 45 +++++ emu/Unixware/mkfile-Unixware | 15 ++ emu/Unixware/os.c | 438 +++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 893 insertions(+) create mode 100644 emu/Unixware/asm-386.s create mode 100644 emu/Unixware/cmd.c create mode 100644 emu/Unixware/deveia.c create mode 100644 emu/Unixware/devfs.c create mode 100644 emu/Unixware/emu create mode 100644 emu/Unixware/ipif.c create mode 100644 emu/Unixware/mkfile create mode 100644 emu/Unixware/mkfile-Unixware create mode 100644 emu/Unixware/os.c (limited to 'emu/Unixware') diff --git a/emu/Unixware/asm-386.s b/emu/Unixware/asm-386.s new file mode 100644 index 00000000..e3978dd1 --- /dev/null +++ b/emu/Unixware/asm-386.s @@ -0,0 +1,71 @@ + .section .bss + .align 4 +.L4_.bss: + .align 4 +Unixware_Asm_IntP: / Offset 0 + .type Unixware_Asm_IntP,@object + .size Unixware_Asm_IntP,4 + .set .,.+4 +Unixware_Asm_VoidP: / Offset 4 + .type Unixware_Asm_VoidP,@object + .size Unixware_Asm_VoidP,4 + .set .,.+4 + .section .text + .align 4 +.L1_.text: +/==================== +/ FPsave +/-------------------- + .align 4 + .align 4 + .globl FPsave +FPsave: + pushl %ebp + movl %esp,%ebp + movl 8(%ebp),%eax + movl %eax,Unixware_Asm_VoidP + fstenv (%eax) + leave + ret + .align 4 + .type FPsave,@function + .size FPsave,.-FPsave + +/==================== +/ FPrestore +/-------------------- + .align 4 + .globl FPrestore +FPrestore: + pushl %ebp + movl %esp,%ebp + movl 8(%ebp),%eax + movl %eax,Unixware_Asm_VoidP + fldenv (%eax) + leave + ret + .align 4 + .type FPrestore,@function + .size FPrestore,.-FPrestore + + +/==================== +/ getcallerpc +/-------------------- + .align 4 + .globl getcallerpc +getcallerpc: + movl 4(%ebp),%eax + ret + .align 4 + .type getcallerpc,@function + .size getcallerpc,.-getcallerpc + +/ test-and-set + .align 4 + .globl _tas +_tas: + movl $1, %eax + movl 4(%esp), %ecx + xchgl %eax, 0(%ecx) + ret diff --git a/emu/Unixware/cmd.c b/emu/Unixware/cmd.c new file mode 100644 index 00000000..897490c7 --- /dev/null +++ b/emu/Unixware/cmd.c @@ -0,0 +1,205 @@ +#include +#include +#include +#include +#include +#include + +#include "dat.h" +#include "fns.h" +#include "error.h" + +enum +{ + Debug = 0 +}; + +/* + * os-specific devcmd support. + * this version should be reasonably portable across Unix systems. + */ +typedef struct Targ Targ; +struct Targ +{ + int fd[2]; /* fd[0] is standard input, fd[1] is standard output */ + char** args; + char* dir; + int pid; + int wfd; /* child writes errors that occur after the fork or on exec */ + int uid; + int gid; +}; + +extern int gidnobody; +extern int uidnobody; + +static int +childproc(Targ *t) +{ + int i, nfd; + + if(Debug) + print("devcmd: '%s'", t->args[0]); + + nfd = getdtablesize(); + for(i = 0; i < nfd; i++) + if(i != t->fd[0] && i != t->fd[1] && i != t->wfd) + close(i); + + dup2(t->fd[0], 0); + dup2(t->fd[1], 1); + dup2(t->fd[1], 2); + close(t->fd[0]); + close(t->fd[1]); + + if(t->gid != -1){ + if(setgid(t->gid) < 0 && getegid() == 0){ + fprint(t->wfd, "can't set gid %d: %s", t->gid, strerror(errno)); + _exit(1); + } + } + + if(t->uid != -1){ + if(setuid(t->uid) < 0 && geteuid() == 0){ + fprint(t->wfd, "can't set uid %d: %s", t->uid, strerror(errno)); + _exit(1); + } + } + + if(t->dir != nil && chdir(t->dir) < 0){ + fprint(t->wfd, "can't chdir to %s: %s", t->dir, strerror(errno)); + _exit(1); + } + + signal(SIGPIPE, SIG_DFL); + + execvp(t->args[0], t->args); + if(Debug) + print("execvp: %s\n",strerror(errno)); + fprint(t->wfd, "exec failed: %s", strerror(errno)); + + _exit(1); +} + +void* +oscmd(char **args, int nice, char *dir, int *rfd, int *sfd) +{ + Dir *d; + Targ *t; + int r, fd0[2], fd1[2], wfd[2], n, pid; + + t = mallocz(sizeof(*t), 1); + if(t == nil) + return nil; + + fd0[0] = fd0[1] = -1; + fd1[0] = fd1[1] = -1; + wfd[0] = wfd[1] = -1; + if(pipe(fd0) < 0 || pipe(fd1) < 0 || pipe(wfd) < 0) + goto Error; + if(fcntl(wfd[1], F_SETFD, FD_CLOEXEC) < 0) /* close on exec to give end of file on success */ + goto Error; + + t->fd[0] = fd0[0]; + t->fd[1] = fd1[1]; + t->wfd = wfd[1]; + t->args = args; + t->dir = dir; + t->gid = up->env->gid; + if(t->gid == -1) + t->gid = gidnobody; + t->uid = up->env->uid; + if(t->uid == -1) + t->uid = uidnobody; + + signal(SIGCHLD, SIG_DFL); + switch(pid = fork1()) { + case -1: + goto Error; + case 0: + setpgrp(); + if(nice) + oslopri(); + childproc(t); + _exit(1); + default: + t->pid = pid; + if(Debug) + print("cmd pid %d\n", t->pid); + break; + } + + close(fd0[0]); + close(fd1[1]); + close(wfd[1]); + + n = read(wfd[0], up->genbuf, sizeof(up->genbuf)-1); + close(wfd[0]); + if(n > 0){ + close(fd0[1]); + close(fd1[0]); + free(t); + up->genbuf[n] = 0; + if(Debug) + print("oscmd: bad exec: %q\n", up->genbuf); + error(up->genbuf); + return nil; + } + + *sfd = fd0[1]; + *rfd = fd1[0]; + return t; + +Error: + r = errno; + if(Debug) + print("oscmd: %q\n",strerror(r)); + close(fd0[0]); + close(fd0[1]); + close(fd1[0]); + close(fd1[1]); + close(wfd[0]); + close(wfd[1]); + error(strerror(r)); + return nil; +} + +int +oscmdkill(void *a) +{ + Targ *t = a; + + if(Debug) + print("kill: %d\n", t->pid); + return kill(-t->pid, SIGTERM); +} + +int +oscmdwait(void *a, char *buf, int n) +{ + Targ *t = a; + int s; + + if(waitpid(t->pid, &s, 0) == -1){ + if(Debug) + print("wait error: %d [in %d] %q\n", t->pid, getpid(), strerror(errno)); + return -1; + } + if(WIFEXITED(s)){ + if(WEXITSTATUS(s) == 0) + return snprint(buf, n, "%d 0 0 0 ''", t->pid); + return snprint(buf, n, "%d 0 0 0 'exit: %d'", t->pid, WEXITSTATUS(s)); + } + if(WIFSIGNALED(s)){ + if(WTERMSIG(s) == SIGTERM || WTERMSIG(s) == SIGKILL) + return snprint(buf, n, "%d 0 0 0 killed", t->pid); + return snprint(buf, n, "%d 0 0 0 'signal: %d'", t->pid, WTERMSIG(s)); + } + return snprint(buf, n, "%d 0 0 0 'odd status: 0x%x'", t->pid, s); +} + +void +oscmdfree(void *a) +{ + free(a); +} diff --git a/emu/Unixware/deveia.c b/emu/Unixware/deveia.c new file mode 100644 index 00000000..1e452c00 --- /dev/null +++ b/emu/Unixware/deveia.c @@ -0,0 +1,11 @@ +/* + * Solaris serial port definitions + */ + +static char *sysdev[] = { + "/dev/cua/a", + "/dev/cua/b" +}; + +#include "deveia-posix.c" +#include "deveia-bsd.c" diff --git a/emu/Unixware/devfs.c b/emu/Unixware/devfs.c new file mode 100644 index 00000000..9017c3fc --- /dev/null +++ b/emu/Unixware/devfs.c @@ -0,0 +1 @@ +#include "devfs-posix.c" diff --git a/emu/Unixware/emu b/emu/Unixware/emu new file mode 100644 index 00000000..4c983199 --- /dev/null +++ b/emu/Unixware/emu @@ -0,0 +1,106 @@ +dev + root + cons + env + mnt + pipe + prog + prof + srv + dup + ssl + cap + fs + cmd cmd + indir + + draw + pointer + snarf + + ip ipif ipaux + eia +# audio audio + mem + +lib + interp + tk + freetype + math + draw + + memlayer + memdraw + keyring + sec + mp + + 9 + +link + +mod + sys + draw + + tk + math + srv srv + keyring + loader + freetype + +port + alloc + cache + chan + dev + dial + dis + discall + env + error + errstr + exception + exportfs + inferno + latin1 + main + parse + pgrp + print + proc + qio + random + sysfile + uqid + +code + +init + emuinit + +root + /dev / + /fd / + /prog / + /net / + /net.alt / + /chan / + /nvfs / + /env / +# /chan +# /dev +# /dis +# /env +# /n +# /net +# /nvfs / +# /prog +# /icons +# /osinit.dis +# /dis/emuinit.dis +# /dis/lib/auth.dis +# /dis/lib/ssl.dis +# /n/local / diff --git a/emu/Unixware/ipif.c b/emu/Unixware/ipif.c new file mode 100644 index 00000000..db7edde5 --- /dev/null +++ b/emu/Unixware/ipif.c @@ -0,0 +1 @@ +#include "../port/ipif-posix.c" diff --git a/emu/Unixware/mkfile b/emu/Unixware/mkfile new file mode 100644 index 00000000..1703b426 --- /dev/null +++ b/emu/Unixware/mkfile @@ -0,0 +1,45 @@ +SYSTARG=Unixware +OBJTYPE=386 +<../../mkconfig +SYSTARG=Unixware +OBJTYPE=386 + +#Configurable parameters + +CONF=emu #default configuration +CONFLIST=emu +CLEANCONFLIST= + +INSTALLDIR=$ROOT/$SYSTARG/$OBJTYPE/bin #path of directory where kernel is installed + +#end configurable parameters + +<$ROOT/mkfiles/mkfile-$SYSTARG-$OBJTYPE #set vars based on target system + +<| $SHELLNAME ../port/mkdevlist $CONF #sets $IP, $DEVS, $PORT, $LIBS + +OBJ=\ + asm-$OBJTYPE.$O\ + os.$O\ + win-x11.$O\ + $CONF.root.$O\ + lock.$O\ + $DEVS\ + $PORT\ + +HFILES=\ + +CFLAGS='-DROOT="'$ROOT'"' -DEMU -I. -I../port -I$ROOT/$SYSTARG/$OBJTYPE/include -I$ROOT/include -I$ROOT/libinterp $CTHREADFLAGS $CFLAGS $EMUOPTIONS +SYSLIBS= -lm -lX11 -lsocket -lnsl -lresolv -lxti -Kthread +KERNDATE=`{$NDATE} + +default:V: $O.$CONF + +<../port/portmkfile + +$O.$CONF: $OBJ $CONF.c $CONF.root.h $LIBNAMES + $CC $CFLAGS '-DKERNDATE='$KERNDATE $CONF.c + $LD $LDFLAGS -o $target $OBJ $CONF.$O $LIBFILES $SYSLIBS + +install:V: $O.$CONF + cp $O.$CONF $INSTALLDIR/$CONF diff --git a/emu/Unixware/mkfile-Unixware b/emu/Unixware/mkfile-Unixware new file mode 100644 index 00000000..52a8977d --- /dev/null +++ b/emu/Unixware/mkfile-Unixware @@ -0,0 +1,15 @@ +# +# architecture-dependent files for Unixware +# + +TARGFILES=asm-Unixware-$OBJTYPE.$O\ + devfs-posix.$O\ + devip.$O\ + ipif-posix.$O\ + os-Unixware.$O\ + win-x11.$O\ + deveia-Unixware.$O\ + srv.$O\ + lock.$O\ + +SYSLIBS= -lm -lX11 -lsocket -lnsl -lresolv -lxti -Kthread diff --git a/emu/Unixware/os.c b/emu/Unixware/os.c new file mode 100644 index 00000000..6493aa87 --- /dev/null +++ b/emu/Unixware/os.c @@ -0,0 +1,438 @@ +#include "dat.h" +#include "fns.h" +#include "error.h" +#undef _POSIX_C_SOURCE +#undef getwd +#include +#include +#include +#include +#include +#include +#include +#include + +enum +{ + DELETE = 0x7F +}; +char *hosttype = "Unixware"; + +static thread_key_t prdakey; + +static siginfo_t siginfo; + +extern int dflag; + +Proc* +getup(void) +{ + void *vp; + + if (thr_getspecific(prdakey, &vp)) + return nil; + return vp; +} + +void +pexit(char *msg, int t) +{ + Osenv *e; + + lock(&procs.l); + if(up->prev) + up->prev->next = up->next; + else + procs.head = up->next; + + if(up->next) + up->next->prev = up->prev; + else + procs.tail = up->prev; + unlock(&procs.l); + + /*print("pexit: %s: %s\n", up->text, msg);*/ + e = up->env; + if(e != nil) { + closefgrp(e->fgrp); + closepgrp(e->pgrp); + closeegrp(e->egrp); + closesigs(e->sigs); + } + free(up->prog); + sema_destroy(up->os); + free(up->os); + free(up); + thr_exit(0); +} + +static void * +tramp(void *v) +{ + struct Proc *Up; + + if(thr_setspecific(prdakey, v)) { + print("set specific data failed in tramp\n"); + thr_exit(0); + } + Up = v; + Up->sigid = thr_self(); + Up->func(Up->arg); + pexit("", 0); +} + +int +kproc(char *name, void (*func)(void*), void *arg, int flags) +{ + thread_t thread; + Proc *p; + Pgrp *pg; + Fgrp *fg; + Egrp *eg; + sema_t *sem; + + p = newproc(); + + sem = malloc(sizeof(*sem)); + if(sem == nil) + panic("can't allocate semaphore"); + sema_init(sem, 0, USYNC_THREAD, 0); + p->os = sem; + + if(flags & KPDUPPG) { + pg = up->env->pgrp; + incref(&pg->r); + p->env->pgrp = pg; + } + if(flags & KPDUPFDG) { + fg = up->env->fgrp; + incref(&fg->r); + p->env->fgrp = fg; + } + if(flags & KPDUPENVG) { + eg = up->env->egrp; + incref(&eg->r); + p->env->egrp = eg; + } + + p->env->uid = up->env->uid; + p->env->gid = up->env->gid; + kstrdup(&p->env->user, up->env->user); + + strcpy(p->text, name); + + p->func = func; + p->arg = arg; + + lock(&procs.l); + if(procs.tail != nil) { + p->prev = procs.tail; + procs.tail->next = p; + } + else { + procs.head = p; + p->prev = nil; + } + procs.tail = p; + unlock(&procs.l); + + if(thr_create(0, 0, &tramp, p, THR_BOUND|THR_DETACHED, &thread)) + panic("thr_create failed\n"); + thr_yield(); + return(thread); +} + +/* to get pc on trap use siginfo.si_pc field and define all trap handlers + as printILL - have to set sa_sigaction, sa_flags not sa_handler +*/ + +static void +trapUSR1(void) +{ + int intwait; + + intwait = up->intwait; + up->intwait = 0; /* clear it to let proc continue in osleave */ + + if(up->type != Interp) /* Used to unblock pending I/O */ + return; + + if(intwait == 0) /* Not posted so it's a sync error */ + disfault(nil, Eintr); /* Should never happen */ +} + +static void +trapILL(void) +{ + disfault(nil, "Illegal instruction"); +} + +static void +printILL(int sig, siginfo_t *siginfo, void *v) +{ + panic("Illegal instruction with code=%d at address=%x, opcode=%x.\n", + siginfo->si_code, siginfo->si_addr,*(char*)siginfo->si_addr); +} + +static void +trapBUS(void) +{ + disfault(nil, "Bus error"); +} + +static void +trapSEGV(void) +{ + disfault(nil, "Segmentation violation"); +} + +static void +trapFPE(void) +{ + disfault(nil, "Floating point exception"); +} + +void +oshostintr(Proc *p) +{ + thr_kill(p->sigid, SIGUSR1); +} + +void +osblock(void) +{ + while(sema_wait(up->os)) + ; /* retry on signals */ +} + +void +osready(Proc *p) +{ + sema_post(p->os); +} + +void +oslongjmp(void *regs, osjmpbuf env, int val) +{ + USED(regs); + siglongjmp(env, val); +} + +static struct termios tinit; + +static void +termset(void) +{ + struct termios t; + + tcgetattr(0, &t); + tinit = t; + t.c_lflag &= ~(ICANON|ECHO|ISIG); + t.c_cc[VMIN] = 1; + t.c_cc[VTIME] = 0; + tcsetattr(0, TCSANOW, &t); +} + +static void +termrestore(void) +{ + tcsetattr(0, TCSANOW, &tinit); +} + +void +cleanexit(int x) +{ + USED(x); + + if(up->intwait) { + up->intwait = 0; + return; + } + + if(dflag == 0) + termrestore(); + exit(0); +} + +int gidnobody= -1, uidnobody= -1; + +void +getnobody(void) +{ + struct passwd *pwd; + + if (pwd=getpwnam("nobody")) { + uidnobody = pwd->pw_uid; + gidnobody = pwd->pw_gid; + } +} + +void +osreboot(char *file, char **argv) +{ + if(dflag == 0) + termrestore(); + execvp(file, argv); + panic("reboot failure"); +} + +void +libinit(char *imod) +{ + struct Proc *Up; + struct sigaction act; + struct passwd *pw; + char sys[64]; + + setsid(); + + if(dflag == 0) + termset(); + + gethostname(sys, sizeof(sys)); + kstrdup(&ossysname, sys); + getnobody(); + + memset(&act, 0 , sizeof(act)); + act.sa_handler=trapUSR1; + sigaction(SIGUSR1, &act, nil); + /* + * For the correct functioning of devcmd in the + * face of exiting slaves + */ + signal(SIGPIPE, SIG_IGN); + if(signal(SIGTERM, SIG_IGN) != SIG_IGN) + signal(SIGTERM, cleanexit); + if(sflag == 0) { + act.sa_handler = trapBUS; + sigaction(SIGBUS, &act, nil); + act.sa_handler = trapILL; + sigaction(SIGILL, &act, nil); + act.sa_handler = trapSEGV; + sigaction(SIGSEGV, &act, nil); + act.sa_handler = trapFPE; + sigaction(SIGFPE, &act, nil); + if(signal(SIGINT, SIG_IGN) != SIG_IGN) + signal(SIGINT, cleanexit); + } else{ + act.sa_sigaction = printILL; + act.sa_flags=SA_SIGINFO; + sigaction(SIGILL, &act, nil); + } + + if(thr_keycreate(&prdakey,NULL)) + print("keycreate failed\n"); + + Up = newproc(); + if(thr_setspecific(prdakey,Up)) + panic("set specific thread data failed\n"); + + pw = getpwuid(getuid()); + if(pw != nil) + kstrdup(&eve, pw->pw_name); + else + print("cannot getpwuid\n"); + + up->env->uid = getuid(); + up->env->gid = getgid(); + emuinit(imod); +} + +int +readkbd(void) +{ + int n; + char buf[1]; + + n = read(0, buf, sizeof(buf)); + if(n < 0) + fprint(2, "keyboard read: %s\n", strerror(errno)); + if(n <= 0) + pexit("keyboard thread", 0); + + switch(buf[0]) { + case '\r': + buf[0] = '\n'; + break; + case DELETE: + cleanexit(0); + break; + } + return buf[0]; +} + +/* + * Return an abitrary millisecond clock time + */ +long +osmillisec(void) +{ + static long sec0 = 0, usec0; + struct timeval t; + + if(gettimeofday(&t, NULL)<0) + return(0); + if(sec0==0){ + sec0 = t.tv_sec; + usec0 = t.tv_usec; + } + return((t.tv_sec-sec0)*1000+(t.tv_usec-usec0+500)/1000); +} + +/* + * Return the time since the epoch in microseconds + * The epoch is defined at 1 Jan 1970 + */ +vlong +osnsec(void) +{ + struct timeval t; + + gettimeofday(&t, nil); + return (vlong)t.tv_sec*1000000000L + t.tv_usec*1000; +} + +vlong +osusectime(void) +{ + struct timeval t; + + gettimeofday(&t, nil); + return (vlong)t.tv_sec * 1000000 + t.tv_usec; +} + +int +osmillisleep(ulong milsec) +{ + struct timespec time; + + time.tv_sec = milsec/1000; + time.tv_nsec= (milsec%1000)*1000000; + nanosleep(&time,nil); + return 0; +} + +int +limbosleep(ulong milsec) +{ + return osmillisleep(milsec); +} + +void +osyield(void) +{ + thr_yield(); +} + +void +ospause(void) +{ + for(;;) + pause(); +} + +void +oslopri(void) +{ + setpriority(PRIO_PROCESS, 0, getpriority(PRIO_PROCESS,0)+4); +} -- cgit v1.2.3