Module Name: src Committed By: jmcneill Date: Sat Sep 3 15:00:28 UTC 2011
Modified Files: src/sys/arch/usermode/conf: Makefile.usermode src/sys/arch/usermode/dev: cpu.c ld_thunkbus.c src/sys/arch/usermode/include: thunk.h src/sys/arch/usermode/usermode: machdep.c thunk.c trap.c urkelvisor.c Log Message: Get this compiling (but not quite linking yet) on Linux To generate a diff of this commit: cvs rdiff -u -r1.16 -r1.17 src/sys/arch/usermode/conf/Makefile.usermode cvs rdiff -u -r1.25 -r1.26 src/sys/arch/usermode/dev/cpu.c cvs rdiff -u -r1.6 -r1.7 src/sys/arch/usermode/dev/ld_thunkbus.c cvs rdiff -u -r1.23 -r1.24 src/sys/arch/usermode/include/thunk.h cvs rdiff -u -r1.21 -r1.22 src/sys/arch/usermode/usermode/machdep.c cvs rdiff -u -r1.26 -r1.27 src/sys/arch/usermode/usermode/thunk.c cvs rdiff -u -r1.18 -r1.19 src/sys/arch/usermode/usermode/trap.c cvs rdiff -u -r1.2 -r1.3 src/sys/arch/usermode/usermode/urkelvisor.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/arch/usermode/conf/Makefile.usermode diff -u src/sys/arch/usermode/conf/Makefile.usermode:1.16 src/sys/arch/usermode/conf/Makefile.usermode:1.17 --- src/sys/arch/usermode/conf/Makefile.usermode:1.16 Sat Sep 3 12:28:45 2011 +++ src/sys/arch/usermode/conf/Makefile.usermode Sat Sep 3 15:00:27 2011 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.usermode,v 1.16 2011/09/03 12:28:45 jmcneill Exp $ +# $NetBSD: Makefile.usermode,v 1.17 2011/09/03 15:00:27 jmcneill Exp $ MACHINE_ARCH= usermode USETOOLS?= no @@ -14,12 +14,16 @@ ## ## (2) compile settings ## +USERMODE_HOSTOS!=uname -s USERMODE_LIBS= -lrt +.if ${USERMODE_HOSTOS} == "Linux" +USERMODE_LIBS+= -lpthread +.endif USERMODE_CPPFLAGS=-U_KERNEL -I/usr/include USERMODE_CPPFLAGS+=${CWARNFLAGS} ${NOGCCERROR:D:U-Werror} DEFCOPTS= -fno-omit-frame-pointer -CPPFLAGS+= -Dusermode +CPPFLAGS+= -Dusermode -D__NetBSD__ -Wno-unused-but-set-variable CPPFLAGS.init_main.c+= -Dmain=kernmain OPT_SDL= %SDL% @@ -63,8 +67,8 @@ ## SYSTEM_LD= @${_MKSHMSG} " link ${.CURDIR:T}/${.TARGET}"; \ ${_MKSHECHO}\ - ${CC} ${COPTS} -Wl,-Map,$@.map,-Bstatic -o $@ '$${SYSTEM_OBJ}' '$${EXTRA_OBJ}' vers.o ${USERMODE_LIBS}; \ - ${CC} ${COPTS} -Wl,-Map,$@.map,-Bstatic -o $@ ${SYSTEM_OBJ} ${EXTRA_OBJ} vers.o ${USERMODE_LIBS} + ${CC} -static ${COPTS} -Wl,-Map,$@.map -o $@ '$${SYSTEM_OBJ}' '$${EXTRA_OBJ}' vers.o ${USERMODE_LIBS}; \ + ${CC} -static ${COPTS} -Wl,-Map,$@.map -o $@ ${SYSTEM_OBJ} ${EXTRA_OBJ} vers.o ${USERMODE_LIBS} NVFLAGS= -n ## Index: src/sys/arch/usermode/dev/cpu.c diff -u src/sys/arch/usermode/dev/cpu.c:1.25 src/sys/arch/usermode/dev/cpu.c:1.26 --- src/sys/arch/usermode/dev/cpu.c:1.25 Sat Sep 3 12:25:31 2011 +++ src/sys/arch/usermode/dev/cpu.c Sat Sep 3 15:00:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: cpu.c,v 1.25 2011/09/03 12:25:31 reinoud Exp $ */ +/* $NetBSD: cpu.c,v 1.26 2011/09/03 15:00:28 jmcneill Exp $ */ /*- * Copyright (c) 2007 Jared D. McNeill <jmcne...@invisible.ca> @@ -29,7 +29,7 @@ #include "opt_cpu.h" #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.25 2011/09/03 12:25:31 reinoud Exp $"); +__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.26 2011/09/03 15:00:28 jmcneill Exp $"); #include <sys/param.h> #include <sys/conf.h> @@ -133,6 +133,7 @@ usermode_reboot(); /* NOTREACHED */ + __builtin_unreachable(); } void @@ -149,7 +150,6 @@ lwp_t * cpu_switchto(lwp_t *oldlwp, lwp_t *newlwp, bool returning) { - extern int errno; struct pcb *oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL; struct pcb *newpcb = lwp_getpcb(newlwp); struct cpu_info *ci = curcpu(); @@ -182,10 +182,10 @@ curlwp = newlwp; if (oldpcb) { if (thunk_swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp)) - panic("swapcontext failed: %d", errno); + panic("swapcontext failed"); } else { if (thunk_setcontext(&newpcb->pcb_ucp)) - panic("setcontext failed: %d", errno); + panic("setcontext failed"); } #ifdef CPU_DEBUG @@ -290,7 +290,6 @@ cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize, void (*func)(void *), void *arg) { - extern int errno; struct pcb *pcb1 = lwp_getpcb(l1); struct pcb *pcb2 = lwp_getpcb(l2); @@ -313,7 +312,7 @@ pcb2->pcb_needfree = false; if (thunk_getcontext(&pcb2->pcb_ucp)) - panic("getcontext failed: %d", errno); + panic("getcontext failed"); pcb2->pcb_ucp.uc_stack.ss_sp = stack; pcb2->pcb_ucp.uc_stack.ss_size = stacksize; Index: src/sys/arch/usermode/dev/ld_thunkbus.c diff -u src/sys/arch/usermode/dev/ld_thunkbus.c:1.6 src/sys/arch/usermode/dev/ld_thunkbus.c:1.7 --- src/sys/arch/usermode/dev/ld_thunkbus.c:1.6 Thu Aug 25 19:08:35 2011 +++ src/sys/arch/usermode/dev/ld_thunkbus.c Sat Sep 3 15:00:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: ld_thunkbus.c,v 1.6 2011/08/25 19:08:35 reinoud Exp $ */ +/* $NetBSD: ld_thunkbus.c,v 1.7 2011/09/03 15:00:28 jmcneill Exp $ */ /*- * Copyright (c) 2011 Jared D. McNeill <jmcne...@invisible.ca> @@ -27,7 +27,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: ld_thunkbus.c,v 1.6 2011/08/25 19:08:35 reinoud Exp $"); +__KERNEL_RCSID(0, "$NetBSD: ld_thunkbus.c,v 1.7 2011/09/03 15:00:28 jmcneill Exp $"); #include <sys/param.h> #include <sys/proc.h> @@ -72,8 +72,6 @@ CFATTACH_DECL_NEW(ld_thunkbus, sizeof(struct ld_thunkbus_softc), ld_thunkbus_match, ld_thunkbus_attach, NULL, NULL); -extern int errno; - static int ld_thunkbus_match(device_t parent, cfdata_t match, void *opaque) { @@ -99,11 +97,11 @@ sc->sc_fd = thunk_open(path, O_RDWR, 0); if (sc->sc_fd == -1) { - aprint_error(": couldn't open %s: %d\n", path, errno); + aprint_error(": couldn't open %s: %d\n", path, thunk_geterrno()); return; } if (thunk_fstat_getsize(sc->sc_fd, &size, &blksize) == -1) { - aprint_error(": couldn't stat %s: %d\n", path, errno); + aprint_error(": couldn't stat %s: %d\n", path, thunk_geterrno()); return; } @@ -126,7 +124,7 @@ sa.sa_flags = SA_RESTART|SA_SIGINFO; sa.sa_sigaction = ld_thunkbus_sig; if (thunk_sigaction(SIGIO, &sa, NULL) == -1) - panic("couldn't register SIGIO handler: %d", errno); + panic("couldn't register SIGIO handler: %d", thunk_geterrno()); ldattach(ld); } @@ -159,7 +157,7 @@ thunk_aio_return(&tt->tt_aio) != -1) { bp->b_resid = 0; } else { - bp->b_error = errno; + bp->b_error = thunk_geterrno(); bp->b_resid = bp->b_bcount; } @@ -201,7 +199,7 @@ else error = thunk_aio_write(&tt->tt_aio); - return error == -1 ? errno : 0; + return error == -1 ? thunk_geterrno() : 0; } static int @@ -212,7 +210,7 @@ len = thunk_pwrite(sc->sc_fd, data, blkcnt, blkno); if (len == -1) - return errno; + return thunk_geterrno(); else if (len != blkcnt) { device_printf(ld->sc_dv, "%s failed (short xfer)\n", __func__); return EIO; @@ -227,7 +225,7 @@ struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld; if (thunk_fsync(sc->sc_fd) == -1) - return errno; + return thunk_geterrno(); return 0; } Index: src/sys/arch/usermode/include/thunk.h diff -u src/sys/arch/usermode/include/thunk.h:1.23 src/sys/arch/usermode/include/thunk.h:1.24 --- src/sys/arch/usermode/include/thunk.h:1.23 Fri Sep 2 16:09:01 2011 +++ src/sys/arch/usermode/include/thunk.h Sat Sep 3 15:00:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: thunk.h,v 1.23 2011/09/02 16:09:01 reinoud Exp $ */ +/* $NetBSD: thunk.h,v 1.24 2011/09/03 15:00:28 jmcneill Exp $ */ /*- * Copyright (c) 2011 Jared D. McNeill <jmcne...@invisible.ca> @@ -35,7 +35,6 @@ #include <sys/fcntl.h> #include <sys/ucontext.h> #include <sys/signal.h> -#include <sys/aio.h> #include <sys/mman.h> struct thunk_timeval { @@ -58,6 +57,8 @@ int32_t c_ospeed; }; +struct aiocb; + int thunk_setitimer(int, const struct thunk_itimerval *, struct thunk_itimerval *); int thunk_gettimeofday(struct thunk_timeval *, void *); unsigned int thunk_getcounter(void); @@ -67,6 +68,8 @@ void thunk_exit(int); void thunk_abort(void); +int thunk_geterrno(void); + int thunk_getcontext(ucontext_t *); int thunk_setcontext(const ucontext_t *); void thunk_makecontext(ucontext_t *, void (*)(void), int, void (*)(void *), void *); Index: src/sys/arch/usermode/usermode/machdep.c diff -u src/sys/arch/usermode/usermode/machdep.c:1.21 src/sys/arch/usermode/usermode/machdep.c:1.22 --- src/sys/arch/usermode/usermode/machdep.c:1.21 Sat Sep 3 12:33:02 2011 +++ src/sys/arch/usermode/usermode/machdep.c Sat Sep 3 15:00:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: machdep.c,v 1.21 2011/09/03 12:33:02 jmcneill Exp $ */ +/* $NetBSD: machdep.c,v 1.22 2011/09/03 15:00:28 jmcneill Exp $ */ /*- * Copyright (c) 2007 Jared D. McNeill <jmcne...@invisible.ca> @@ -31,7 +31,7 @@ #include "opt_urkelvisor.h" #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.21 2011/09/03 12:33:02 jmcneill Exp $"); +__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.22 2011/09/03 15:00:28 jmcneill Exp $"); #include <sys/types.h> #include <sys/param.h> @@ -177,7 +177,7 @@ printf("reg[%02d] (%6s) = %"PRIx32"\n", i, name[i], reg[i]); #endif #else -# error setregs() not yet `ported'/tweaked to this architecture +# error setregs() not yet ported to this architecture #endif printf("updated pcb %p\n", pcb); Index: src/sys/arch/usermode/usermode/thunk.c diff -u src/sys/arch/usermode/usermode/thunk.c:1.26 src/sys/arch/usermode/usermode/thunk.c:1.27 --- src/sys/arch/usermode/usermode/thunk.c:1.26 Fri Sep 2 16:09:01 2011 +++ src/sys/arch/usermode/usermode/thunk.c Sat Sep 3 15:00:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: thunk.c,v 1.26 2011/09/02 16:09:01 reinoud Exp $ */ +/* $NetBSD: thunk.c,v 1.27 2011/09/03 15:00:28 jmcneill Exp $ */ /*- * Copyright (c) 2011 Jared D. McNeill <jmcne...@invisible.ca> @@ -27,13 +27,15 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: thunk.c,v 1.26 2011/09/02 16:09:01 reinoud Exp $"); +#ifdef __NetBSD__ +__RCSID("$NetBSD: thunk.c,v 1.27 2011/09/03 15:00:28 jmcneill Exp $"); +#endif #include <sys/types.h> -#include <sys/ansi.h> #include <aio.h> #include <assert.h> +#include <errno.h> #include <fcntl.h> #include <stdarg.h> #include <stdint.h> @@ -47,6 +49,10 @@ #include "../include/thunk.h" +#ifndef __arraycount +#define __arraycount(x) (sizeof((x)) / sizeof((x)[0])) +#endif + static void thunk_to_timeval(const struct thunk_timeval *ttv, struct timeval *tv) { @@ -184,6 +190,12 @@ } int +thunk_geterrno(void) +{ + return errno; +} + +int thunk_getcontext(ucontext_t *ucp) { return getcontext(ucp); @@ -377,24 +389,9 @@ return sbrk(len); } -/* exposed to signal handler */ -extern vaddr_t kmem_k_start, kmem_k_end; -extern vaddr_t kmem_ext_start, kmem_ext_end; -extern vaddr_t kmem_user_start, kmem_user_end; -extern vaddr_t kmem_ext_cur_start, kmem_ext_cur_end; - void * thunk_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) { -#ifdef DIAGNOSTIC - if (kmem_ext_end && (len <= 4096)) { - if (((vaddr_t) addr < kmem_user_start) || ((vaddr_t) addr >= kmem_ext_end)) { - printf("thunk mmap outside the box\n"); - exit(1); - } - } -#endif - return mmap(addr, len, prot, flags, fd, offset); } Index: src/sys/arch/usermode/usermode/trap.c diff -u src/sys/arch/usermode/usermode/trap.c:1.18 src/sys/arch/usermode/usermode/trap.c:1.19 --- src/sys/arch/usermode/usermode/trap.c:1.18 Fri Sep 2 14:54:41 2011 +++ src/sys/arch/usermode/usermode/trap.c Sat Sep 3 15:00:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: trap.c,v 1.18 2011/09/02 14:54:41 reinoud Exp $ */ +/* $NetBSD: trap.c,v 1.19 2011/09/03 15:00:28 jmcneill Exp $ */ /*- * Copyright (c) 2011 Reinoud Zandijk <rein...@netbsd.org> @@ -27,7 +27,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.18 2011/09/02 14:54:41 reinoud Exp $"); +__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.19 2011/09/03 15:00:28 jmcneill Exp $"); #include <sys/types.h> #include <sys/param.h> @@ -52,7 +52,6 @@ /* forwards and externals */ void setup_signal_handlers(void); static void mem_access_handler(int sig, siginfo_t *info, void *ctx); -extern int errno; bool pmap_fault(pmap_t pmap, vaddr_t va, vm_prot_t *atype); @@ -73,15 +72,18 @@ sa.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; sa.sa_sigaction = mem_access_handler; if (thunk_sigaction(SIGSEGV, &sa, NULL) == -1) - panic("couldn't register SIGSEGV handler : %d", errno); + panic("couldn't register SIGSEGV handler : %d", + thunk_geterrno()); if (thunk_sigaction(SIGBUS, &sa, NULL) == -1) - panic("couldn't register SIGBUS handler : %d", errno); + panic("couldn't register SIGBUS handler : %d", + thunk_geterrno()); if ((sigstk.ss_sp = thunk_malloc(SIGSTKSZ)) == NULL) panic("can't allocate signal stack space\n"); sigstk.ss_size = SIGSTKSZ; sigstk.ss_flags = 0; if (thunk_sigaltstack(&sigstk, 0) < 0) - panic("can't set alternate stacksize : %d", errno); + panic("can't set alternate stacksize : %d", + thunk_geterrno()); // debug_fh = thunk_open("/usr/sources/debug", O_RDWR | O_TRUNC | O_CREAT, 0666); } Index: src/sys/arch/usermode/usermode/urkelvisor.c diff -u src/sys/arch/usermode/usermode/urkelvisor.c:1.2 src/sys/arch/usermode/usermode/urkelvisor.c:1.3 --- src/sys/arch/usermode/usermode/urkelvisor.c:1.2 Sat Sep 3 12:48:22 2011 +++ src/sys/arch/usermode/usermode/urkelvisor.c Sat Sep 3 15:00:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: urkelvisor.c,v 1.2 2011/09/03 12:48:22 jmcneill Exp $ */ +/* $NetBSD: urkelvisor.c,v 1.3 2011/09/03 15:00:28 jmcneill Exp $ */ /*- * Copyright (c) 2011 Jared D. McNeill <jmcne...@invisible.ca> @@ -32,14 +32,19 @@ #include <sys/cdefs.h> #ifdef __NetBSD__ -__RCSID("$NetBSD: urkelvisor.c,v 1.2 2011/09/03 12:48:22 jmcneill Exp $"); +__RCSID("$NetBSD: urkelvisor.c,v 1.3 2011/09/03 15:00:28 jmcneill Exp $"); #endif #include <sys/types.h> #include <sys/param.h> #include <sys/ptrace.h> #include <sys/wait.h> + +#ifdef __linux__ +#include <sys/user.h> +#else #include <machine/reg.h> +#endif #include <err.h> #include <errno.h> @@ -49,16 +54,29 @@ #include "../include/urkelvisor.h" +#if defined(__linux__) +#include <linux/ptrace.h> +#define PT_SYSCALLEMU PTRACE_SYSEMU +typedef intptr_t vaddr_t; +#endif + extern vaddr_t kmem_user_start, kmem_user_end; /* usermode/pmap.c */ -#if defined(__i386__) -#define R_SYSCALL(_regs) ((_regs)->r_eax) -#define R_PC(_regs) ((_regs)->r_eip) -#elif defined(__x86_64__) -#define R_SYSCALL(_regs) ((_regs)->regs[_REG_RAX]) -#define R_PC(_regs) ((_regs)->regs[_REG_RIP]) +#if defined(__linux__) +# define reg_struct user_regs_struct +# define R_SYSCALL(_regs) ((_regs)->orig_eax) +# define R_PC(_regs) ((_regs)->eip) #else -#error port me +# define reg_struct reg +# if defined(__i386__) +# define R_SYSCALL(_regs) ((_regs)->r_eax) +# define R_PC(_regs) ((_regs)->r_eip) +# elif defined(__x86_64__) +# define R_SYSCALL(_regs) ((_regs)->regs[_REG_RAX]) +# define R_PC(_regs) ((_regs)->regs[_REG_RIP]) +# else +# error port me +# endif #endif static int @@ -77,20 +95,30 @@ return status; } +static void +ptrace_getregs(pid_t urkel_pid, struct reg_struct *puregs) +{ +#ifdef __linux__ + ptrace(PT_GETREGS, urkel_pid, NULL, puregs); +#else + ptrace(PT_GETREGS, urkel_pid, puregs, 0); +#endif +} + static int handle_syscall(pid_t urkel_pid) { - struct reg uregs; + struct reg_struct uregs; int sig = 0; errno = 0; - ptrace(PT_GETREGS, urkel_pid, &uregs, 0); + ptrace_getregs(urkel_pid, &uregs); if (errno) err(EXIT_FAILURE, "ptrace(PT_GETREGS, %d, &uregs, 0) failed", urkel_pid); if (R_PC(&uregs) >= kmem_user_start && R_PC(&uregs) < kmem_user_end) { - fprintf(stderr, "caught syscall %d\n", R_SYSCALL(&uregs)); + fprintf(stderr, "caught syscall %d\n", (int)R_SYSCALL(&uregs)); errno = 0; ptrace(PT_SYSCALLEMU, urkel_pid, NULL, 0); if (errno)