From 28fc97ca89be7d052a285f744602be9188e7d6ee Mon Sep 17 00:00:00 2001 From: pooka Date: Sat, 7 Sep 2013 17:58:00 +0000 Subject: [PATCH] Add an initial console device and open fd's 0/1/2 for initproc. This is again useful in standalone-type environments such as Xen, where all printf/etc calls go through the rump kernel. --- sys/rump/librump/rumpkern/Makefile.rumpkern | 4 +- sys/rump/librump/rumpkern/cons.c | 133 ++++++++++++++++++++ sys/rump/librump/rumpkern/rump.c | 18 ++- sys/rump/librump/rumpkern/rump_private.h | 4 +- 4 files changed, 150 insertions(+), 9 deletions(-) create mode 100644 sys/rump/librump/rumpkern/cons.c diff --git a/sys/rump/librump/rumpkern/Makefile.rumpkern b/sys/rump/librump/rumpkern/Makefile.rumpkern index 09484cabbe42..5186c221bd43 100644 --- a/sys/rump/librump/rumpkern/Makefile.rumpkern +++ b/sys/rump/librump/rumpkern/Makefile.rumpkern @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.rumpkern,v 1.131 2013/09/03 21:32:21 pooka Exp $ +# $NetBSD: Makefile.rumpkern,v 1.132 2013/09/07 17:58:00 pooka Exp $ # .include "${RUMPTOP}/Makefile.rump" @@ -19,7 +19,7 @@ LIB= rump # # Source modules, first the ones specifically implemented for librump. # -SRCS+= rump.c rumpcopy.c emul.c intr.c lwproc.c klock.c \ +SRCS+= rump.c rumpcopy.c cons.c emul.c intr.c lwproc.c klock.c \ kobj_rename.c ltsleep.c scheduler.c \ signals.c sleepq.c threads.c vm.c cprng_stub.c diff --git a/sys/rump/librump/rumpkern/cons.c b/sys/rump/librump/rumpkern/cons.c new file mode 100644 index 000000000000..16b99f6c88f7 --- /dev/null +++ b/sys/rump/librump/rumpkern/cons.c @@ -0,0 +1,133 @@ +/* $NetBSD: cons.c,v 1.1 2013/09/07 17:58:00 pooka Exp $ */ + +/* + * Copyright (c) 2013 Antti Kantee. 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 AUTHOR ``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 AUTHOR 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. + */ + +/* + * rumpcons, a (very) simple console-type device which relays output + * to the rumpuser_putchar() hypercall. This is most useful in + * environments where there is no Unix-like host (e.g. Xen DomU). + * It's currently a truly half duplex console since there is support + * only for writing to the console (there is no hypercall for reading + * the host console). + */ + +#include +__KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.1 2013/09/07 17:58:00 pooka Exp $"); + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "rump_private.h" + +static int rumpcons_write(struct file *, off_t *, struct uio *, + kauth_cred_t, int); +static int rumpcons_stat(struct file *, struct stat *); + +static const struct fileops rumpcons_fileops = { + .fo_read = (void *)nullop, + .fo_write = rumpcons_write, + .fo_ioctl = fbadop_ioctl, + .fo_fcntl = fnullop_fcntl, + .fo_poll = fnullop_poll, + .fo_stat = rumpcons_stat, + .fo_close = (void *)nullop, + .fo_kqfilter = fnullop_kqfilter, + .fo_restart = fnullop_restart, +}; + +void +rump_consdev_init(void) +{ + struct file *fp; + int fd, error; + + /* + * We want to open the descriptors for the implicit proc + * so that they get inherited by default to all processes. + */ + KASSERT(curproc->p_pid == 1); + KASSERT(fd_getfile(0) == NULL); + + /* then, map a file descriptor to the device */ + if ((error = fd_allocfile(&fp, &fd)) != 0) + panic("cons fd_allocfile failed: %d", error); + + fp->f_flag = FWRITE; + fp->f_type = DTYPE_MISC; + fp->f_ops = &rumpcons_fileops; + fp->f_data = NULL; + fd_affix(curproc, fp, fd); + + KASSERT(fd == 0); + error += fd_dup2(fp, 1, 0); + error += fd_dup2(fp, 2, 0); + + if (error) + panic("failed to dup fd 0/1/2"); +} + +static int +rumpcons_write(struct file *fp, off_t *off, struct uio *uio, + kauth_cred_t cred, int flags) +{ + char *buf; + size_t len, n; + int error; + + buf = kmem_alloc(PAGE_SIZE, KM_SLEEP); + while (uio->uio_resid > 0) { + len = min(PAGE_SIZE, uio->uio_resid); + error = uiomove(buf, len, uio); + if (error) + break; + + for (n = 0; n < len; n++) { + rumpuser_putchar(*(buf+n)); + } + } + kmem_free(buf, PAGE_SIZE); + + return error; +} + +static int +rumpcons_stat(struct file *fp, struct stat *sb) +{ + + memset(sb, 0, sizeof(*sb)); + sb->st_mode = 0600; + sb->st_atimespec = sb->st_mtimespec = sb->st_ctimespec = boottime; + sb->st_birthtimespec = boottime; + + return 0; +} diff --git a/sys/rump/librump/rumpkern/rump.c b/sys/rump/librump/rumpkern/rump.c index 8a12149d3b48..697fe0bbca63 100644 --- a/sys/rump/librump/rumpkern/rump.c +++ b/sys/rump/librump/rumpkern/rump.c @@ -1,4 +1,4 @@ -/* $NetBSD: rump.c,v 1.273 2013/09/04 17:56:08 pooka Exp $ */ +/* $NetBSD: rump.c,v 1.274 2013/09/07 17:58:00 pooka Exp $ */ /* * Copyright (c) 2007-2011 Antti Kantee. All Rights Reserved. @@ -26,7 +26,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.273 2013/09/04 17:56:08 pooka Exp $"); +__KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.274 2013/09/07 17:58:00 pooka Exp $"); #include #define ELFSIZE ARCH_ELFSIZE @@ -251,7 +251,7 @@ rump_init(void) struct timespec ts; int64_t sec; long nsec; - struct lwp *l; + struct lwp *l, *initlwp; int i, numcpu; /* not reentrant */ @@ -471,10 +471,10 @@ rump_init(void) vmem_rehash_start(); /* - * Create init, used to attach implicit threads in rump. + * Create init (proc 1), used to attach implicit threads in rump. * (note: must be done after vfsinit to get cwdi) */ - (void)rump__lwproc_alloclwp(NULL); /* dummy thread for initproc */ + initlwp = rump__lwproc_alloclwp(NULL); mutex_enter(proc_lock); initproc = proc_find_raw(1); mutex_exit(proc_lock); @@ -542,8 +542,14 @@ rump_init(void) rump_component_init(RUMP_COMPONENT_POSTINIT); - /* release cpu */ + /* component inits done */ bootlwp = NULL; + + /* open 0/1/2 for init */ + rump_lwproc_switch(initlwp); + rump_consdev_init(); + + /* release cpu */ rump_unschedule(); return 0; diff --git a/sys/rump/librump/rumpkern/rump_private.h b/sys/rump/librump/rumpkern/rump_private.h index b703130160b7..276d2073407c 100644 --- a/sys/rump/librump/rumpkern/rump_private.h +++ b/sys/rump/librump/rumpkern/rump_private.h @@ -1,4 +1,4 @@ -/* $NetBSD: rump_private.h,v 1.75 2013/09/03 19:55:13 pooka Exp $ */ +/* $NetBSD: rump_private.h,v 1.76 2013/09/07 17:58:00 pooka Exp $ */ /* * Copyright (c) 2007-2011 Antti Kantee. All Rights Reserved. @@ -145,4 +145,6 @@ void rump_xc_highpri(struct cpu_info *); void rump_thread_init(void); void rump_thread_allow(void); +void rump_consdev_init(void); + #endif /* _SYS_RUMP_PRIVATE_H_ */