Module Name: src Committed By: jmcneill Date: Fri Aug 12 00:57:25 UTC 2011
Modified Files: src/sys/arch/usermode/conf: Makefile.usermode src/sys/arch/usermode/dev: clock.c cpu.c ttycons.c src/sys/arch/usermode/include: cpu.h pcb.h Added Files: src/sys/arch/usermode/include: thunk.h src/sys/arch/usermode/usermode: thunk.c Log Message: Clean up extern mess by adding an API for kernel components to call libc functions. thunk.c is built with special cflags that makes it compile against standard system headers instead of kernel ones. To generate a diff of this commit: cvs rdiff -u -r1.7 -r1.8 src/sys/arch/usermode/conf/Makefile.usermode cvs rdiff -u -r1.5 -r1.6 src/sys/arch/usermode/dev/clock.c cvs rdiff -u -r1.9 -r1.10 src/sys/arch/usermode/dev/cpu.c cvs rdiff -u -r1.3 -r1.4 src/sys/arch/usermode/dev/ttycons.c cvs rdiff -u -r1.4 -r1.5 src/sys/arch/usermode/include/cpu.h cvs rdiff -u -r1.2 -r1.3 src/sys/arch/usermode/include/pcb.h cvs rdiff -u -r0 -r1.1 src/sys/arch/usermode/include/thunk.h cvs rdiff -u -r0 -r1.1 src/sys/arch/usermode/usermode/thunk.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.7 src/sys/arch/usermode/conf/Makefile.usermode:1.8 --- src/sys/arch/usermode/conf/Makefile.usermode:1.7 Thu Aug 11 22:38:25 2011 +++ src/sys/arch/usermode/conf/Makefile.usermode Fri Aug 12 00:57:23 2011 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.usermode,v 1.7 2011/08/11 22:38:25 jmcneill Exp $ +# $NetBSD: Makefile.usermode,v 1.8 2011/08/12 00:57:23 jmcneill Exp $ MACHINE_ARCH= usermode USETOOLS?= no @@ -18,6 +18,8 @@ CPPFLAGS+= -Dusermode CPPFLAGS.init_main.c+= -Dmain=kernmain +CPPFLAGS.thunk.c+= -U_KERNEL -I/usr/include + ## ## (3) libkern and compat ## @@ -27,14 +29,14 @@ ## ## (4) local objects, compile rules, and dependencies ## -MD_OBJS= -MD_CFILES= +MD_OBJS= thunk.o +MD_CFILES= ${USERMODE}/usermode/thunk.c MD_SFILES= ## ## (5) link settings ## -SYSTEM_LD= @${_MKSHMSG} " link ${.CUTDIR:T}/${.TARGET}"; \ +SYSTEM_LD= @${_MKSHMSG} " link ${.CURDIR:T}/${.TARGET}"; \ ${_MKSHECHO}\ ${CC} ${COPTS} -Wl,-Map,$@.map -o $@ '$${SYSTEM_OBJ}' '$${EXTRA_OBJ}' vers.o; \ ${CC} ${COPTS} -Wl,-Map,$@.map -o $@ ${SYSTEM_OBJ} ${EXTRA_OBJ} vers.o @@ -44,6 +46,9 @@ ## (6) port specific target dependencies ## +thunk.o: ${USERMODE}/usermode/thunk.c + ${CC} ${COPTS} -I${.CURDIR} -c -o $@ ${USERMODE}/usermode/thunk.c + ## ## (7) misc settings ## Index: src/sys/arch/usermode/dev/clock.c diff -u src/sys/arch/usermode/dev/clock.c:1.5 src/sys/arch/usermode/dev/clock.c:1.6 --- src/sys/arch/usermode/dev/clock.c:1.5 Wed Aug 10 01:32:44 2011 +++ src/sys/arch/usermode/dev/clock.c Fri Aug 12 00:57:24 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: clock.c,v 1.5 2011/08/10 01:32:44 jmcneill Exp $ */ +/* $NetBSD: clock.c,v 1.6 2011/08/12 00:57:24 jmcneill Exp $ */ /*- * Copyright (c) 2007 Jared D. McNeill <jmcne...@invisible.ca> @@ -27,7 +27,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.5 2011/08/10 01:32:44 jmcneill Exp $"); +__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.6 2011/08/12 00:57:24 jmcneill Exp $"); #include <sys/param.h> #include <sys/proc.h> @@ -37,6 +37,7 @@ #include <sys/time.h> #include <machine/mainbus.h> +#include <machine/thunk.h> static int clock_match(device_t, cfdata_t, void *); static void clock_attach(device_t, device_t, void *); @@ -48,8 +49,6 @@ device_t sc_dev; } clock_softc_t; -extern int setitimer(int, const struct itimerval *, struct itimerval *); - static struct timecounter clock_timecounter = { clock_getcounter, /* get_timecount */ 0, /* no poll_pps */ @@ -91,7 +90,7 @@ itimer.it_interval.tv_sec = 0; itimer.it_interval.tv_usec = 10000; itimer.it_value = itimer.it_interval; - (void)setitimer(ITIMER_REAL, &itimer, NULL); + thunk_setitimer(ITIMER_REAL, &itimer, NULL); tc_init(&clock_timecounter); } @@ -114,9 +113,8 @@ static u_int clock_getcounter(struct timecounter *tc) { - extern int gettimeofday(struct timeval *, void *); struct timeval tv; - gettimeofday(&tv, NULL); + thunk_gettimeofday(&tv, NULL); return tv.tv_sec * 1000000 + tv.tv_usec; } Index: src/sys/arch/usermode/dev/cpu.c diff -u src/sys/arch/usermode/dev/cpu.c:1.9 src/sys/arch/usermode/dev/cpu.c:1.10 --- src/sys/arch/usermode/dev/cpu.c:1.9 Thu Aug 11 23:04:44 2011 +++ src/sys/arch/usermode/dev/cpu.c Fri Aug 12 00:57:24 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: cpu.c,v 1.9 2011/08/11 23:04:44 jmcneill Exp $ */ +/* $NetBSD: cpu.c,v 1.10 2011/08/12 00:57:24 jmcneill Exp $ */ /*- * Copyright (c) 2007 Jared D. McNeill <jmcne...@invisible.ca> @@ -27,7 +27,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.9 2011/08/11 23:04:44 jmcneill Exp $"); +__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.10 2011/08/12 00:57:24 jmcneill Exp $"); #include <sys/param.h> #include <sys/conf.h> @@ -44,6 +44,7 @@ #include <machine/cpu.h> #include <machine/mainbus.h> #include <machine/pcb.h> +#include <machine/thunk.h> #include <uvm/uvm_extern.h> #include <uvm/uvm_page.h> @@ -90,7 +91,7 @@ sc->sc_ci->ci_self = &cpu_info_primary; sc->sc_ci->ci_curlwp = &lwp0; - if (getcontext(&lwp0pcb)) + if (thunk_getcontext(&lwp0pcb)) panic("getcontext failed"); uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb); } @@ -107,13 +108,10 @@ void cpu_reboot(int howto, char *bootstr) { - extern void exit(int); - extern void abort(void); - splhigh(); if ((howto & RB_POWERDOWN) == RB_POWERDOWN) - exit(0); + thunk_exit(0); if (howto & RB_HALT) { printf("\n"); @@ -129,7 +127,7 @@ /* * XXXJDM If we've panic'd, make sure we dump a core */ - abort(); + thunk_abort(); /* NOTREACHED */ } @@ -176,10 +174,10 @@ ci->ci_stash = oldlwp; curlwp = newlwp; if (oldpcb) { - if (swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp)) + if (thunk_swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp)) panic("swapcontext failed: %d", errno); } else { - if (setcontext(&newpcb->pcb_ucp)) + if (thunk_setcontext(&newpcb->pcb_ucp)) panic("setcontext failed: %d", errno); } @@ -225,14 +223,13 @@ void cpu_idle(void) { - extern int usleep(useconds_t); struct cpu_info *ci = curcpu(); if (ci->ci_want_resched) return; #if notyet - usleep(10000); + thunk_usleep(10000); #endif } @@ -294,13 +291,13 @@ } else pcb->pcb_needfree = false; - if (getcontext(&pcb->pcb_ucp)) + if (thunk_getcontext(&pcb->pcb_ucp)) panic("getcontext failed: %d", errno); pcb->pcb_ucp.uc_stack.ss_sp = stack; pcb->pcb_ucp.uc_stack.ss_size = stacksize; pcb->pcb_ucp.uc_link = NULL; pcb->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU; - makecontext(&pcb->pcb_ucp, (void (*)(void))cpu_lwp_trampoline, + thunk_makecontext(&pcb->pcb_ucp, (void (*)(void))cpu_lwp_trampoline, 2, func, arg); } Index: src/sys/arch/usermode/dev/ttycons.c diff -u src/sys/arch/usermode/dev/ttycons.c:1.3 src/sys/arch/usermode/dev/ttycons.c:1.4 --- src/sys/arch/usermode/dev/ttycons.c:1.3 Fri Nov 27 03:23:14 2009 +++ src/sys/arch/usermode/dev/ttycons.c Fri Aug 12 00:57:24 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: ttycons.c,v 1.3 2009/11/27 03:23:14 rmind Exp $ */ +/* $NetBSD: ttycons.c,v 1.4 2011/08/12 00:57:24 jmcneill Exp $ */ /*- * Copyright (c) 2007 Jared D. McNeill <jmcne...@invisible.ca> @@ -27,7 +27,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: ttycons.c,v 1.3 2009/11/27 03:23:14 rmind Exp $"); +__KERNEL_RCSID(0, "$NetBSD: ttycons.c,v 1.4 2011/08/12 00:57:24 jmcneill Exp $"); #include <sys/param.h> #include <sys/proc.h> @@ -37,6 +37,7 @@ #include <dev/cons.h> #include <machine/mainbus.h> +#include <machine/thunk.h> static int ttycons_match(device_t, cfdata_t, void *); static void ttycons_attach(device_t, device_t, void *); @@ -94,15 +95,13 @@ int ttycons_cngetc(dev_t dev) { - extern int getchar(void); - return getchar(); + return thunk_getchar(); } void ttycons_cnputc(dev_t dev, int c) { - extern void putchar(int); - putchar(c); + thunk_putchar(c); } void Index: src/sys/arch/usermode/include/cpu.h diff -u src/sys/arch/usermode/include/cpu.h:1.4 src/sys/arch/usermode/include/cpu.h:1.5 --- src/sys/arch/usermode/include/cpu.h:1.4 Thu Aug 11 23:04:44 2011 +++ src/sys/arch/usermode/include/cpu.h Fri Aug 12 00:57:24 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: cpu.h,v 1.4 2011/08/11 23:04:44 jmcneill Exp $ */ +/* $NetBSD: cpu.h,v 1.5 2011/08/12 00:57:24 jmcneill Exp $ */ /*- * Copyright (c) 2007 Jared D. McNeill <jmcne...@invisible.ca> @@ -33,6 +33,7 @@ #include <sys/cpu_data.h> #include <machine/intrdefs.h> +#include <machine/thunk.h> extern void cpu_signotify(struct lwp *); extern void cpu_need_proftick(struct lwp *); @@ -61,9 +62,7 @@ __inline static void usermode_delay(unsigned int ms) { - extern int usleep(useconds_t); - - usleep(ms); + thunk_usleep(ms); } #define curcpu() usermode_curcpu() Index: src/sys/arch/usermode/include/pcb.h diff -u src/sys/arch/usermode/include/pcb.h:1.2 src/sys/arch/usermode/include/pcb.h:1.3 --- src/sys/arch/usermode/include/pcb.h:1.2 Wed Oct 21 16:06:59 2009 +++ src/sys/arch/usermode/include/pcb.h Fri Aug 12 00:57:24 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: pcb.h,v 1.2 2009/10/21 16:06:59 snj Exp $ */ +/* $NetBSD: pcb.h,v 1.3 2011/08/12 00:57:24 jmcneill Exp $ */ /*- * Copyright (c) 2007 Jared D. McNeill <jmcne...@invisible.ca> @@ -32,11 +32,6 @@ #include <sys/cdefs.h> #include <sys/ucontext.h> -extern int getcontext(ucontext_t *); -extern int setcontext(const ucontext_t *); -extern void makecontext(ucontext_t *, void (*)(void), int, ...); -extern int swapcontext(ucontext_t *, const ucontext_t *); - struct pcb { ucontext_t pcb_ucp; bool pcb_needfree; Added files: Index: src/sys/arch/usermode/include/thunk.h diff -u /dev/null src/sys/arch/usermode/include/thunk.h:1.1 --- /dev/null Fri Aug 12 00:57:25 2011 +++ src/sys/arch/usermode/include/thunk.h Fri Aug 12 00:57:24 2011 @@ -0,0 +1,51 @@ +/* $NetBSD: thunk.h,v 1.1 2011/08/12 00:57:24 jmcneill Exp $ */ + +/*- + * Copyright (c) 2011 Jared D. McNeill <jmcne...@invisible.ca> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ARCH_USERMODE_INCLUDE_THUNK_H +#define _ARCH_USERMODE_INCLUDE_THUNK_H + +#include <sys/types.h> +#include <sys/time.h> +#include <sys/ucontext.h> + +int thunk_setitimer(int, const struct itimerval *, struct itimerval *); +int thunk_gettimeofday(struct timeval *, void *); +int thunk_usleep(useconds_t); + +void thunk_exit(int); +void thunk_abort(void); + +int thunk_getcontext(ucontext_t *); +int thunk_setcontext(const ucontext_t *); +void thunk_makecontext(ucontext_t *, void (*)(void), int, void (*)(void *), void *); +int thunk_swapcontext(ucontext_t *, ucontext_t *); + +int thunk_getchar(void); +void thunk_putchar(int); + +#endif /* !_ARCH_USERMODE_INCLUDE_THUNK_H */ Index: src/sys/arch/usermode/usermode/thunk.c diff -u /dev/null src/sys/arch/usermode/usermode/thunk.c:1.1 --- /dev/null Fri Aug 12 00:57:25 2011 +++ src/sys/arch/usermode/usermode/thunk.c Fri Aug 12 00:57:24 2011 @@ -0,0 +1,110 @@ +/* $NetBSD: thunk.c,v 1.1 2011/08/12 00:57:24 jmcneill Exp $ */ + +/*- + * Copyright (c) 2011 Jared D. McNeill <jmcne...@invisible.ca> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__RCSID("$NetBSD: thunk.c,v 1.1 2011/08/12 00:57:24 jmcneill Exp $"); + +#include <machine/thunk.h> + +#include <assert.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <ucontext.h> + +int +thunk_setitimer(int which, const struct itimerval *value, + struct itimerval *ovalue) +{ + return setitimer(which, value, ovalue); +} + +int +thunk_gettimeofday(struct timeval *tp, void *tzp) +{ + return gettimeofday(tp, tzp); +} + +int +thunk_usleep(useconds_t microseconds) +{ + return usleep(microseconds); +} + +void +thunk_exit(int status) +{ + return exit(status); +} + +void +thunk_abort(void) +{ + abort(); +} + +int +thunk_getcontext(ucontext_t *ucp) +{ + return getcontext(ucp); +} + +int +thunk_setcontext(const ucontext_t *ucp) +{ + return setcontext(ucp); +} + +void +thunk_makecontext(ucontext_t *ucp, void (*func)(), int argc, + void (*arg1)(void *), void *arg2) +{ + assert(argc == 2); + + makecontext(ucp, func, argc, arg1, arg2); +} + +int +thunk_swapcontext(ucontext_t *oucp, ucontext_t *ucp) +{ + return swapcontext(oucp, ucp); +} + +int +thunk_getchar(void) +{ + return getchar(); +} + +void +thunk_putchar(int c) +{ + putchar(c); + fflush(stdout); +}