diff options
Diffstat (limited to 'emu/Linux')
| -rw-r--r-- | emu/Linux/asm-386.S | 103 | ||||
| -rw-r--r-- | emu/Linux/asm-arm.S | 132 | ||||
| -rw-r--r-- | emu/Linux/asm-power.S | 72 | ||||
| -rw-r--r-- | emu/Linux/cmd.c | 213 | ||||
| -rw-r--r-- | emu/Linux/deveia.c | 44 | ||||
| -rw-r--r-- | emu/Linux/devfs.c | 1 | ||||
| -rw-r--r-- | emu/Linux/emu | 106 | ||||
| -rw-r--r-- | emu/Linux/emu-g | 94 | ||||
| -rw-r--r-- | emu/Linux/mkfile | 47 | ||||
| -rw-r--r-- | emu/Linux/os.c | 535 | ||||
| -rw-r--r-- | emu/Linux/segflush-power.c | 27 |
11 files changed, 1374 insertions, 0 deletions
diff --git a/emu/Linux/asm-386.S b/emu/Linux/asm-386.S new file mode 100644 index 00000000..31946335 --- /dev/null +++ b/emu/Linux/asm-386.S @@ -0,0 +1,103 @@ + .file "asm-Linux-386.S" +#include "syscall.h" + .text + +/* + * executeonnewstack(void *tos, void (*tramp)(void *arg), void *arg) + */ + + .type executeonnewstack,@function + .global executeonnewstack +executeonnewstack: + pushl %ebp + movl %esp, %ebp + pushl %esi + + movl 8(%ebp), %esi /* get tos */ + subl $4, %esi + movl 16(%ebp), %eax + movl %eax, (%esi) /* stash arg on new stack */ + subl $4, %esi + movl 12(%ebp), %eax + movl %eax, (%esi) /* stash tramp on new stack */ + mov %esi, %esp /* swap stacks pronto */ + popl %eax /* recover the tramp address */ + subl %ebp, %ebp /* stop stack traceback here */ + call *%eax /* and jump to it (ho ho) */ + + /* if we return here, tramp didn't do its job */ + + addl $8, %esp /* clean up for pose value */ + + leal SYS_exit, %eax + int $0x80 + +/* + * unlockandexit(int *key) + * + * NB: the return status may be garbaged if the stack is reused + * between the unlock and the system call, but this should + * not matter since no task is waiting for the result + */ + + .type unlockandexit,@function + .global unlockandexit +unlockandexit: + pushl %ebp + movl %esp, %ebp + + movl 8(%ebp), %esi /* get the key address */ + pushl $0 /* exit status 0 */ + movl $0, %eax /* unlock the stack allocator */ + movl %eax, (%esi) + leal SYS_exit, %eax /* call exit */ + int $0x80 + +/* + * umult(ulong m1, ulong m2, ulong *hi) + */ + + .type umult,@function + .global umult +umult: + pushl %ebp + movl %esp, %ebp + pushl %ebx + + movl 8(%ebp), %eax + movl 12(%ebp), %ebx + mull %ebx + movl 16(%ebp), %ebx + movl %edx, (%ebx) + + popl %ebx + popl %ebp + ret + + .type FPsave,@function + .global FPsave +FPsave: + pushl %ebp + movl %esp, %ebp + movl 8(%ebp), %eax + fstenv (%eax) + popl %ebp + ret + + .type FPrestore,@function + .global FPrestore +FPrestore: + pushl %ebp + movl %esp, %ebp + movl 8(%ebp), %eax + fldenv (%eax) + popl %ebp + ret + + .type _tas,@function + .globl _tas +_tas: + movl $1, %eax + movl 4(%esp), %ecx + xchgl %eax, 0(%ecx) + ret diff --git a/emu/Linux/asm-arm.S b/emu/Linux/asm-arm.S new file mode 100644 index 00000000..d13bcd0e --- /dev/null +++ b/emu/Linux/asm-arm.S @@ -0,0 +1,132 @@ + .file "asm-Linux-arm.S" +#include "syscall.h" + .text + +/* + * void executeonnewstack(void *tos, void (*tramp)(void *arg), void *arg) + */ + + .align 2 + .global executeonnewstack + .type executeonnewstack, %function +executeonnewstack: + @ args = 0, pretend = 0, frame = 12 + @ frame_needed = 1, uses_anonymous_args = 0 + mov ip, sp + stmfd sp!, {fp, ip, lr, pc} + sub fp, ip, #4 + sub sp, sp, #12 + str r0, [fp, #-16] /* store tos */ + str r1, [fp, #-20] /* store tramp */ + str r2, [fp, #-24] /* store arg */ + ldr r0, [fp, #-24] /* get arg */ + ldr r2, [fp, #-16] /* get tos */ + mov sp, r2 /* set new stack */ + mov lr, pc + blx r1 /* call tramp*/ + + /* if we return here, tramp didn't do it's job */ + swi SYS_exit + ldmea fp, {fp, sp, pc} + .size executeonnewstack, .-executeonnewstack + +/* + * void unlockandexit(int *key) + * + * NB: the return status may be garbaged if the stack is reused + * between the unlock and the system call, but this should + * not matter since no task is waiting for the result + */ + + .align 2 + .global unlockandexit + .type unlockandexit, %function +unlockandexit: + @ args = 0, pretend = 0, frame = 4 + @ frame_needed = 1, uses_anonymous_args = 0 + mov ip, sp + stmfd sp!, {fp, ip, lr, pc} + sub fp, ip, #4 + sub sp, sp, #4 + mov r1, #0 + str r1, [r0] + swi SYS_exit + ldmea fp, {fp, sp, pc} + .size unlockandexit, .-unlockandexit + +/* + * ulong umult(ulong m1, ulong m2, ulong *hi) + */ + + .align 2 + .global umult + .type umult, %function +umult: + @ args = 0, pretend = 0, frame = 12 + @ frame_needed = 1, uses_anonymous_args = 0 + mov ip, sp + stmfd sp!, {fp, ip, lr, pc} + sub fp, ip, #4 + sub sp, sp, #12 + str r0, [fp, #-16] + str r1, [fp, #-20] + str r2, [fp, #-24] + ldr r1, [fp, #-16] + ldr r2, [fp, #-20] + umull r0, r3, r1, r2 + ldr r1, [fp, #-24] + str r3, [r1] + ldmea fp, {fp, sp, pc} + .size umult, .-umult + +/* + * void FPsave(void*); + */ + + .align 2 + .global FPsave + .type FPsave, %function +FPsave: + @ args = 0, pretend = 0, frame = 4 + @ frame_needed = 1, uses_anonymous_args = 0 + mov ip, sp + stmfd sp!, {fp, ip, lr, pc} + sub fp, ip, #4 + sub sp, sp, #4 + str r0, [fp, #-16] + ldmea fp, {fp, sp, pc} + .size FPsave, .-FPsave + +/* + * void FPrestore(void*); + */ + .align 2 + .global FPrestore + .type FPrestore, %function +FPrestore: + @ args = 0, pretend = 0, frame = 4 + @ frame_needed = 1, uses_anonymous_args = 0 + mov ip, sp + stmfd sp!, {fp, ip, lr, pc} + sub fp, ip, #4 + sub sp, sp, #4 + str r0, [fp, #-16] + ldmea fp, {fp, sp, pc} + .size FPrestore, .-FPrestore + +/* + * ulong _tas(ulong*); + */ + .align 2 + .global _tas + .type _tas, %function +_tas: + @ args = 0, pretend = 0, frame = 0 + @ frame_needed = 0, uses_anonymous_args = 0 + @ link register save eliminated. + @ lr needed for prologue + mov r3, #1 + mov r1, r0 + swp r0, r3, [r1] + mov pc, lr + .size _tas, .-_tas diff --git a/emu/Linux/asm-power.S b/emu/Linux/asm-power.S new file mode 100644 index 00000000..ff5f97cd --- /dev/null +++ b/emu/Linux/asm-power.S @@ -0,0 +1,72 @@ +#include <asm-ppc/reg.h> + + .file "asm-power.S" + .section ".text" + .align 2 + .globl FPsave + .type FPsave, @function +FPsave: + .set _framesize,0 + mffs f0 + stfd f0,0(r3) + blr + .size FPsave,.-FPsave + + .align 2 + .globl FPrestore +FPrestore: + lfd f0,0(r3) + mtfsf 0xff,f0 + blr + .size FPrestore, .-FPrestore + + .align 2 + .globl _tas +_tas: + sync + mr r4,r3 + addi r5,0,0x1 +1: + lwarx r3,0,r4 + cmpwi r3,0x0 + bne- 2f + stwcx. r5,0,r4 + bne- 1b /* Lost reservation, try again */ +2: + sync + blr + .size _tas,.-_tas + +/* + * void executeonnewstack(void *tos, void (*tramp)(void *arg), void *arg) + */ + .align 2 + .globl executeonnewstack: +executeonnewstack: + mr r1,r3 /* change stacks */ + stwu 1,-16(r1) /* save lr to aid the traceback */ + li r0,0 + stw r0,20(r1) + mr r3,r5 + mtctr r4 + bctrl /* tramp(arg) */ + br . /* failed */ + .size executeonnewstack,.-executeonnewstack + +/* + * void unlockandexit(int *key) + * + * NB: the return status may be garbaged if the stack is reused + * between the unlock and the system call, but this should + * not matter since no task is waiting for the result + */ + .align 2 + .globl unlockandexit +unlockandexit: + li r0,0x0 + stw r0,0(r3) /* unlock */ + li r0,1 /* sys exit; 234 is exit group */ + li r3,0 /* exit status */ + sc + br . /* not reached */ + .size unlockandexit,.-unlockandexit diff --git a/emu/Linux/cmd.c b/emu/Linux/cmd.c new file mode 100644 index 00000000..0b8c960b --- /dev/null +++ b/emu/Linux/cmd.c @@ -0,0 +1,213 @@ +#include <sys/types.h> +#include <signal.h> +#include <pwd.h> +#include <sys/resource.h> +#include <sys/wait.h> +#include <fcntl.h> + +#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[3]; /* fd[0] is standard input, fd[1] is standard output, fd[2] is standard error */ + 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->fd[2] && i != t->wfd) + close(i); + + dup2(t->fd[0], 0); + dup2(t->fd[1], 1); + dup2(t->fd[2], 2); + close(t->fd[0]); + close(t->fd[1]); + close(t->fd[2]); + + /* should have an auth file to do host-specific authorisation? */ + 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 *fd) +{ + Targ *t; + int r, fd0[2], fd1[2], fd2[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; + fd2[0] = fd2[1] = -1; + wfd[0] = wfd[1] = -1; + if(pipe(fd0) < 0 || pipe(fd1) < 0 || pipe(fd2) < 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->fd[2] = fd2[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 = fork()) { + case -1: + goto Error; + case 0: + setpgrp(); + if(nice) + setpriority(PRIO_PROCESS, 0, 19); + 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(fd2[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]); + close(fd2[0]); + free(t); + up->genbuf[n] = 0; + if(Debug) + print("oscmd: bad exec: %q\n", up->genbuf); + error(up->genbuf); + return nil; + } + + fd[0] = fd0[1]; + fd[1] = fd1[0]; + fd[2] = fd2[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(fd2[0]); + close(fd2[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/Linux/deveia.c b/emu/Linux/deveia.c new file mode 100644 index 00000000..a1f0951f --- /dev/null +++ b/emu/Linux/deveia.c @@ -0,0 +1,44 @@ +/* + * Linux serial port definitions + */ + +static char *sysdev[] = { + "/dev/ttyS0", + "/dev/ttyS1", + "/dev/ttyS2", + "/dev/ttyS3", + "/dev/ttyS4", + "/dev/ttyS5", + "/dev/ttyS6", + "/dev/ttyS7", +}; + +#include <sys/ioctl.h> +#include "deveia-posix.c" +#include "deveia-bsd.c" + + +static struct tcdef_t bps[] = { + {0, B0}, + {50, B50}, + {75, B75}, + {110, B110}, + {134, B134}, + {150, B150}, + {200, B200}, + {300, B300}, + {600, B600}, + {1200, B1200}, + {1800, B1800}, + {2400, B2400}, + {4800, B4800}, + {9600, B9600}, + {19200, B19200}, + {38400, B38400}, + {57600, B57600}, + {115200, B115200}, + {230400, B230400}, + {460800, B460800}, + {-1, -1} +}; + diff --git a/emu/Linux/devfs.c b/emu/Linux/devfs.c new file mode 100644 index 00000000..9017c3fc --- /dev/null +++ b/emu/Linux/devfs.c @@ -0,0 +1 @@ +#include "devfs-posix.c" diff --git a/emu/Linux/emu b/emu/Linux/emu new file mode 100644 index 00000000..0e7c17f9 --- /dev/null +++ b/emu/Linux/emu @@ -0,0 +1,106 @@ +dev + root + cons + env + mnt + pipe + prog + prof + srv + dup + ssl + cap + fs + cmd cmd + indir + + draw win-x11a + pointer + snarf + + ip ipif-posix 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/Linux/emu-g b/emu/Linux/emu-g new file mode 100644 index 00000000..31d29125 --- /dev/null +++ b/emu/Linux/emu-g @@ -0,0 +1,94 @@ +dev + root + cons + env + mnt + pipe + prog + prof + srv + dup + ssl + cap + fs + cmd cmd + indir + + ip ipif-posix ipaux + eia +# audio audio + mem + +lib + interp + math + keyring + sec + mp + + 9 + +link + +mod + sys + math + srv srv + keyring + loader + +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 + void setpointer(int x, int y){USED(x); USED(y);} + ulong strtochan(char *s){USED(s); return ~0;} + +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/Linux/mkfile b/emu/Linux/mkfile new file mode 100644 index 00000000..77693e4e --- /dev/null +++ b/emu/Linux/mkfile @@ -0,0 +1,47 @@ +SYSTARG=Linux +<../../mkconfig +SYSTARG=Linux + +#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\ + $CONF.root.$O\ + lock.$O\ + $DEVS\ + $PORT\ + +LIBNAMES=${LIBS:%=lib%.a} +#libs=${LIBS:%=$ROOT/$OBJDIR/lib/lib%.a} + +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 -lXext +KERNDATE=`{$NDATE} + +default:V: $O.$CONF + +$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 + +<../port/portmkfile + +devfs.$O: ../port/devfs-posix.c diff --git a/emu/Linux/os.c b/emu/Linux/os.c new file mode 100644 index 00000000..49aec7e5 --- /dev/null +++ b/emu/Linux/os.c @@ -0,0 +1,535 @@ +#include <sys/types.h> +#include <time.h> +#include <termios.h> +#include <signal.h> +#include <pwd.h> +#include <sched.h> +#include <sys/resource.h> +#include <sys/wait.h> +#include <sys/time.h> + +#include "dat.h" +#include "fns.h" +#include "error.h" + +/* glibc 2.3.3-NTPL messes up getpid() by trying to cache the result, so we'll do it ourselves */ +#include <sys/syscall.h> +#define getpid() syscall(SYS_getpid) + +/* temporarily suppress CLONE_PTRACE so it works on broken Linux kernels */ +#undef CLONE_PTRACE +#define CLONE_PTRACE 0 + +enum +{ + DELETE = 0x7f, + CTRLC = 'C'-'@', + NSTACKSPERALLOC = 16, + X11STACK= 256*1024 +}; +char *hosttype = "Linux"; + +static void *stackalloc(Proc *p, void **tos); +static void stackfreeandexit(void *stack); + +extern int dflag; + +int gidnobody = -1; +int uidnobody = -1; +static struct termios tinit; + +void +pexit(char *msg, int t) +{ + Osenv *e; + void *kstack; + + 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); + + if(0) + 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); + } + kstack = up->kstack; + free(up->prog); + free(up); + if(kstack != nil) + stackfreeandexit(kstack); +} + +void +tramp(void *arg) +{ + Proc *p; + p = arg; + p->pid = p->sigid = getpid(); + (*p->func)(p->arg); + pexit("{Tramp}", 0); +} + +int +kproc(char *name, void (*func)(void*), void *arg, int flags) +{ + int pid; + Proc *p; + Pgrp *pg; + Fgrp *fg; + Egrp *eg; + void *tos; + + p = newproc(); + if(0) + print("start %s:%.8lx\n", name, p); + if(p == nil) { + print("kproc(%s): no memory", name); + return; + } + + 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; + + if(flags & KPX11){ + p->kstack = nil; /* never freed; also up not defined */ + tos = (char*)mallocz(X11STACK, 0) + X11STACK - sizeof(void*); + }else + p->kstack = stackalloc(p, &tos); + + 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 (__clone(tramp, tos, CLONE_PTRACE|CLONE_VM|CLONE_FS|CLONE_FILES|SIGCHLD, p) <= 0) + panic("kproc: clone failed"); + + return 0; +} + +/* + * TO DO: + * To get pc on trap, use sigaction instead of signal and + * examine its siginfo structure + */ + +/* +static void +diserr(char *s, int pc) +{ + char buf[ERRMAX]; + + snprint(buf, sizeof(buf), "%s: pc=0x%lux", s, pc); + disfault(nil, buf); +} +*/ + +static void +trapILL(int signo) +{ + USED(signo); + disfault(nil, "Illegal instruction"); +} + +static void +trapBUS(int signo) +{ + USED(signo); + disfault(nil, "Bus error"); +} + +static void +trapSEGV(int signo) +{ + USED(signo); + disfault(nil, "Segmentation violation"); +} + +#include <fpuctl.h> +static void +trapFPE(int signo) +{ + USED(signo); + print("FPU status=0x%.4lux", getfsr()); + disfault(nil, "Floating exception"); +} + +static void +trapUSR1(int signo) +{ + int intwait; + + USED(signo); + + 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 */ +} + +/* called to wake up kproc blocked on a syscall */ +void +oshostintr(Proc *p) +{ + kill(p->sigid, SIGUSR1); +} + +static void +trapUSR2(int signo) +{ + USED(signo); + /* we've done our work of interrupting sigsuspend */ +} + +void +osblock(void) +{ + sigset_t mask; + + sigprocmask(SIG_SETMASK, NULL, &mask); + sigdelset(&mask, SIGUSR2); + sigsuspend(&mask); +} + +void +osready(Proc *p) +{ + if(kill(p->sigid, SIGUSR2) < 0) + fprint(2, "emu: osready failed: pid %d: %s\n", p->sigid, strerror(errno)); +} + +void +oslongjmp(void *regs, osjmpbuf env, int val) +{ + USED(regs); + siglongjmp(env, val); +} + +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(); + + kill(0, SIGKILL); + exit(0); +} + +void +osreboot(char *file, char **argv) +{ +} + +void +libinit(char *imod) +{ + struct termios t; + struct sigaction act; + sigset_t mask; + struct passwd *pw; + Proc *p; + void *tos; + char sys[64]; + + setsid(); + + gethostname(sys, sizeof(sys)); + kstrdup(&ossysname, sys); + pw = getpwnam("nobody"); + if(pw != nil) { + uidnobody = pw->pw_uid; + gidnobody = pw->pw_gid; + } + + if(dflag == 0) + termset(); + + memset(&act, 0 , sizeof(act)); + act.sa_handler = trapUSR1; + sigaction(SIGUSR1, &act, nil); + + sigemptyset(&mask); + sigaddset(&mask, SIGUSR2); + sigprocmask(SIG_BLOCK, &mask, NULL); + + memset(&act, 0 , sizeof(act)); + act.sa_handler = trapUSR2; + sigaction(SIGUSR2, &act, nil); + + act.sa_handler = SIG_IGN; + sigaction(SIGCHLD, &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(signal(SIGINT, SIG_IGN) != SIG_IGN) + signal(SIGINT, 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); + } + + p = newproc(); + p->kstack = stackalloc(p, &tos); + + pw = getpwuid(getuid()); + if(pw != nil) + kstrdup(&eve, pw->pw_name); + else + print("cannot getpwuid\n"); + + p->env->uid = getuid(); + p->env->gid = getgid(); + + executeonnewstack(tos, emuinit, imod); +} + +int +readkbd(void) +{ + int n; + char buf[1]; + + n = read(0, buf, sizeof(buf)); + if(n < 0) + print("keyboard close (n=%d, %s)\n", n, strerror(errno)); + if(n <= 0) + pexit("keyboard thread", 0); + + switch(buf[0]) { + case '\r': + buf[0] = '\n'; + break; + case DELETE: + buf[0] = 'H' - '@'; + break; + case CTRLC: + 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,(struct timezone*)0)<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 nanoseconds and 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, NULL); + return 0; +} + +int +limbosleep(ulong milsec) +{ + return osmillisleep(milsec); +} + +void +osyield(void) +{ + sched_yield(); +} + +void +ospause(void) +{ + for(;;) + pause(); +} + +void +oslopri(void) +{ + setpriority(PRIO_PROCESS, 0, getpriority(PRIO_PROCESS,0)+4); +} + +static struct { + Lock l; + void *free; +} stacklist; + +static void +_stackfree(void *stack) +{ + *((void **)stack) = stacklist.free; + stacklist.free = stack; +} + +static void +stackfreeandexit(void *stack) +{ + lock(&stacklist.l); + _stackfree(stack); + unlockandexit(&stacklist.l.val); +} + +static void * +stackalloc(Proc *p, void **tos) +{ + void *rv; + lock(&stacklist.l); + if (stacklist.free == 0) { + int x; + /* + * obtain some more by using sbrk() + */ + void *more = sbrk(KSTACK * (NSTACKSPERALLOC + 1)); + if (more == 0) + panic("stackalloc: no more stacks"); + /* + * align to KSTACK + */ + more = (void *)((((unsigned long)more) + (KSTACK - 1)) & ~(KSTACK - 1)); + /* + * free all the new stacks onto the freelist + */ + for (x = 0; x < NSTACKSPERALLOC; x++) + _stackfree((char *)more + KSTACK * x); + } + rv = stacklist.free; + stacklist.free = *(void **)rv; + unlock(&stacklist.l); + *tos = rv + KSTACK - sizeof(void *); + *(Proc **)rv = p; + return rv; +} + +#ifdef LINUX_ARM +#define SYS_cacheflush __ARM_NR_cacheflush + +int +segflush(void *a, ulong n) +{ + if(n) + syscall(SYS_cacheflush, a, (char*)a+n-1, 1); + return 0; +} +#endif diff --git a/emu/Linux/segflush-power.c b/emu/Linux/segflush-power.c new file mode 100644 index 00000000..07f9a3cb --- /dev/null +++ b/emu/Linux/segflush-power.c @@ -0,0 +1,27 @@ +/* + * from geoff collyer's port + * invalidate instruction cache and write back data cache from a to a+n-1, + * at least. + */ +void +segflush(void *a, ulong n) +{ + ulong *p; + + // cache blocks are often eight words (32 bytes) long, sometimes 16 bytes. + // need to determine it dynamically? + for (p = (ulong *)((ulong)a & ~3UL); (char *)p < (char *)a + n; p++) + __asm__("dcbst 0,%0\n\t" // not dcbf, which writes back, then invalidates + "icbi 0,%0\n\t" + : // no output + : "ar" (p) + ); + __asm__("sync\n\t" + : // no output + : + ); + __asm__("isync\n\t" + : // no output + : + ); +} |
