domcaps.c revision 613b28719c10e84c1202c1045df44d77767de21d
843e19887f64dde75055cf8842fc4db2171eff45johnlev/*
843e19887f64dde75055cf8842fc4db2171eff45johnlev * CDDL HEADER START
843e19887f64dde75055cf8842fc4db2171eff45johnlev *
843e19887f64dde75055cf8842fc4db2171eff45johnlev * The contents of this file are subject to the terms of the
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Common Development and Distribution License (the "License").
843e19887f64dde75055cf8842fc4db2171eff45johnlev * You may not use this file except in compliance with the License.
843e19887f64dde75055cf8842fc4db2171eff45johnlev *
843e19887f64dde75055cf8842fc4db2171eff45johnlev * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
843e19887f64dde75055cf8842fc4db2171eff45johnlev * or http://www.opensolaris.org/os/licensing.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * See the License for the specific language governing permissions
843e19887f64dde75055cf8842fc4db2171eff45johnlev * and limitations under the License.
843e19887f64dde75055cf8842fc4db2171eff45johnlev *
843e19887f64dde75055cf8842fc4db2171eff45johnlev * When distributing Covered Code, include this CDDL HEADER in each
843e19887f64dde75055cf8842fc4db2171eff45johnlev * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * If applicable, add the following below this CDDL HEADER, with the
843e19887f64dde75055cf8842fc4db2171eff45johnlev * fields enclosed by brackets "[]" replaced with your own identifying
843e19887f64dde75055cf8842fc4db2171eff45johnlev * information: Portions Copyright [yyyy] [name of copyright owner]
843e19887f64dde75055cf8842fc4db2171eff45johnlev *
843e19887f64dde75055cf8842fc4db2171eff45johnlev * CDDL HEADER END
843e19887f64dde75055cf8842fc4db2171eff45johnlev */
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev/*
613b28719c10e84c1202c1045df44d77767de21dRichard Bean * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Use is subject to license terms.
843e19887f64dde75055cf8842fc4db2171eff45johnlev */
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev/*
843e19887f64dde75055cf8842fc4db2171eff45johnlev * This rather uninspiring device enables userland to discover if
843e19887f64dde75055cf8842fc4db2171eff45johnlev * the current kernel is actually a dom0 or other domain e.g. domU.
843e19887f64dde75055cf8842fc4db2171eff45johnlev */
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev#include <sys/types.h>
843e19887f64dde75055cf8842fc4db2171eff45johnlev#include <sys/file.h>
843e19887f64dde75055cf8842fc4db2171eff45johnlev#include <sys/errno.h>
843e19887f64dde75055cf8842fc4db2171eff45johnlev#include <sys/open.h>
843e19887f64dde75055cf8842fc4db2171eff45johnlev#include <sys/cred.h>
843e19887f64dde75055cf8842fc4db2171eff45johnlev#include <sys/conf.h>
843e19887f64dde75055cf8842fc4db2171eff45johnlev#include <sys/stat.h>
843e19887f64dde75055cf8842fc4db2171eff45johnlev#include <sys/modctl.h>
843e19887f64dde75055cf8842fc4db2171eff45johnlev#include <sys/ddi.h>
843e19887f64dde75055cf8842fc4db2171eff45johnlev#include <sys/sunddi.h>
843e19887f64dde75055cf8842fc4db2171eff45johnlev#include <sys/hypervisor.h>
843e19887f64dde75055cf8842fc4db2171eff45johnlev#include <sys/sysmacros.h>
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev#include <sys/domcaps_impl.h>
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic dev_info_t *domcaps_devi;
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev/*ARGSUSED*/
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic int
843e19887f64dde75055cf8842fc4db2171eff45johnlevdomcaps_getinfo(dev_info_t *devi, ddi_info_cmd_t cmd, void *arg, void **result)
843e19887f64dde75055cf8842fc4db2171eff45johnlev{
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (getminor((dev_t)arg) != DOMCAPS_MINOR)
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (DDI_FAILURE);
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev switch (cmd) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev case DDI_INFO_DEVT2DEVINFO:
843e19887f64dde75055cf8842fc4db2171eff45johnlev *result = domcaps_devi;
843e19887f64dde75055cf8842fc4db2171eff45johnlev break;
843e19887f64dde75055cf8842fc4db2171eff45johnlev case DDI_INFO_DEVT2INSTANCE:
843e19887f64dde75055cf8842fc4db2171eff45johnlev *result = 0;
843e19887f64dde75055cf8842fc4db2171eff45johnlev break;
843e19887f64dde75055cf8842fc4db2171eff45johnlev default:
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (DDI_FAILURE);
843e19887f64dde75055cf8842fc4db2171eff45johnlev }
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (DDI_SUCCESS);
843e19887f64dde75055cf8842fc4db2171eff45johnlev}
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic int
843e19887f64dde75055cf8842fc4db2171eff45johnlevdomcaps_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
843e19887f64dde75055cf8842fc4db2171eff45johnlev{
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (cmd != DDI_ATTACH)
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (DDI_FAILURE);
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (ddi_create_minor_node(devi, ddi_get_name(devi), S_IFCHR,
843e19887f64dde75055cf8842fc4db2171eff45johnlev ddi_get_instance(devi), DDI_PSEUDO, 0) != DDI_SUCCESS)
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (DDI_FAILURE);
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev domcaps_devi = devi;
843e19887f64dde75055cf8842fc4db2171eff45johnlev ddi_report_dev(devi);
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (DDI_SUCCESS);
843e19887f64dde75055cf8842fc4db2171eff45johnlev}
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic int
843e19887f64dde75055cf8842fc4db2171eff45johnlevdomcaps_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
843e19887f64dde75055cf8842fc4db2171eff45johnlev{
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (cmd != DDI_DETACH)
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (DDI_FAILURE);
843e19887f64dde75055cf8842fc4db2171eff45johnlev ddi_remove_minor_node(devi, NULL);
843e19887f64dde75055cf8842fc4db2171eff45johnlev domcaps_devi = NULL;
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (DDI_SUCCESS);
843e19887f64dde75055cf8842fc4db2171eff45johnlev}
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev/*ARGSUSED1*/
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic int
843e19887f64dde75055cf8842fc4db2171eff45johnlevdomcaps_open(dev_t *dev, int flag, int otyp, cred_t *cr)
843e19887f64dde75055cf8842fc4db2171eff45johnlev{
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (getminor(*dev) == DOMCAPS_MINOR ? 0 : ENXIO);
843e19887f64dde75055cf8842fc4db2171eff45johnlev}
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev/*ARGSUSED*/
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic int
843e19887f64dde75055cf8842fc4db2171eff45johnlevdomcaps_read(dev_t dev, uio_t *uio, cred_t *cr)
843e19887f64dde75055cf8842fc4db2171eff45johnlev{
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (DOMAIN_IS_INITDOMAIN(xen_info)) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev static char data[] = "control_d\n";
843e19887f64dde75055cf8842fc4db2171eff45johnlev size_t nbytes;
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (uio->uio_loffset > sizeof (data))
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (0);
843e19887f64dde75055cf8842fc4db2171eff45johnlev nbytes = MIN(uio->uio_resid, sizeof (data) - uio->uio_loffset);
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (uiomove(data + uio->uio_loffset, nbytes,
843e19887f64dde75055cf8842fc4db2171eff45johnlev UIO_READ, uio));
843e19887f64dde75055cf8842fc4db2171eff45johnlev }
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (0);
843e19887f64dde75055cf8842fc4db2171eff45johnlev}
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic struct cb_ops domcaps_cb_ops = {
843e19887f64dde75055cf8842fc4db2171eff45johnlev domcaps_open,
843e19887f64dde75055cf8842fc4db2171eff45johnlev nulldev, /* close */
843e19887f64dde75055cf8842fc4db2171eff45johnlev nodev, /* strategy */
843e19887f64dde75055cf8842fc4db2171eff45johnlev nodev, /* print */
843e19887f64dde75055cf8842fc4db2171eff45johnlev nodev, /* dump */
843e19887f64dde75055cf8842fc4db2171eff45johnlev domcaps_read,
843e19887f64dde75055cf8842fc4db2171eff45johnlev nodev, /* write */
843e19887f64dde75055cf8842fc4db2171eff45johnlev nodev, /* ioctl */
843e19887f64dde75055cf8842fc4db2171eff45johnlev nodev, /* devmap */
843e19887f64dde75055cf8842fc4db2171eff45johnlev nodev, /* mmap */
843e19887f64dde75055cf8842fc4db2171eff45johnlev nodev, /* segmap */
843e19887f64dde75055cf8842fc4db2171eff45johnlev nochpoll, /* poll */
843e19887f64dde75055cf8842fc4db2171eff45johnlev ddi_prop_op,
843e19887f64dde75055cf8842fc4db2171eff45johnlev NULL,
843e19887f64dde75055cf8842fc4db2171eff45johnlev D_64BIT | D_MP,
843e19887f64dde75055cf8842fc4db2171eff45johnlev CB_REV,
843e19887f64dde75055cf8842fc4db2171eff45johnlev NULL,
843e19887f64dde75055cf8842fc4db2171eff45johnlev NULL
843e19887f64dde75055cf8842fc4db2171eff45johnlev};
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic struct dev_ops domcaps_dv_ops = {
843e19887f64dde75055cf8842fc4db2171eff45johnlev DEVO_REV,
843e19887f64dde75055cf8842fc4db2171eff45johnlev 0,
843e19887f64dde75055cf8842fc4db2171eff45johnlev domcaps_getinfo,
843e19887f64dde75055cf8842fc4db2171eff45johnlev nulldev, /* identify */
843e19887f64dde75055cf8842fc4db2171eff45johnlev nulldev, /* probe */
843e19887f64dde75055cf8842fc4db2171eff45johnlev domcaps_attach,
843e19887f64dde75055cf8842fc4db2171eff45johnlev domcaps_detach,
843e19887f64dde75055cf8842fc4db2171eff45johnlev nodev, /* reset */
843e19887f64dde75055cf8842fc4db2171eff45johnlev &domcaps_cb_ops,
843e19887f64dde75055cf8842fc4db2171eff45johnlev NULL, /* struct bus_ops */
843e19887f64dde75055cf8842fc4db2171eff45johnlev NULL /* power */
843e19887f64dde75055cf8842fc4db2171eff45johnlev};
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic struct modldrv modldrv = {
843e19887f64dde75055cf8842fc4db2171eff45johnlev &mod_driverops,
613b28719c10e84c1202c1045df44d77767de21dRichard Bean "hypervisor capabilities driver",
843e19887f64dde75055cf8842fc4db2171eff45johnlev &domcaps_dv_ops
843e19887f64dde75055cf8842fc4db2171eff45johnlev};
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic struct modlinkage modl = {
843e19887f64dde75055cf8842fc4db2171eff45johnlev MODREV_1,
843e19887f64dde75055cf8842fc4db2171eff45johnlev {
843e19887f64dde75055cf8842fc4db2171eff45johnlev (void *)&modldrv,
843e19887f64dde75055cf8842fc4db2171eff45johnlev NULL /* null termination */
843e19887f64dde75055cf8842fc4db2171eff45johnlev }
843e19887f64dde75055cf8842fc4db2171eff45johnlev};
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlevint
843e19887f64dde75055cf8842fc4db2171eff45johnlev_init(void)
843e19887f64dde75055cf8842fc4db2171eff45johnlev{
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (mod_install(&modl));
843e19887f64dde75055cf8842fc4db2171eff45johnlev}
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlevint
843e19887f64dde75055cf8842fc4db2171eff45johnlev_fini(void)
843e19887f64dde75055cf8842fc4db2171eff45johnlev{
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (mod_remove(&modl));
843e19887f64dde75055cf8842fc4db2171eff45johnlev}
843e19887f64dde75055cf8842fc4db2171eff45johnlev
843e19887f64dde75055cf8842fc4db2171eff45johnlevint
843e19887f64dde75055cf8842fc4db2171eff45johnlev_info(struct modinfo *modinfo)
843e19887f64dde75055cf8842fc4db2171eff45johnlev{
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (mod_info(&modl, modinfo));
843e19887f64dde75055cf8842fc4db2171eff45johnlev}