/* $NetBSD: xenbus_dev.c,v 1.19 2024/02/09 22:08:34 andvar Exp $ */
/*
* xenbus_dev.c
*
* Driver giving user-space access to the kernel's xenbus connection
* to xenstore.
*
* Copyright (c) 2005, Christian Limpach
* Copyright (c) 2005, Rusty Russell, IBM Corporation
*
* This file may be distributed separately from the Linux kernel, or
* incorporated into other software packages, subject to the following license:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this source file (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
static int xenbus_dev_read(void *);
static int xenbus_dev_write(void *);
static int xenbus_dev_open(void *);
static int xenbus_dev_close(void *);
static int xsd_port_read(void *);
/*
* several process may open /kern/xen/xenbus in parallel.
* In a transaction one or more write is followed by one or more read.
* Unfortunately we don't get a file descriptor identifier down there,
* which we could use to link a read() to a transaction started in a write().
* To work around this we keep a list of lwp that opended the xenbus file.
* This assumes that a single lwp won't open /kern/xen/xenbus more
* than once, and that a transaction started by one lwp won't be ended
* by another.
* because of this, we also assume that we always got the data before
* the read() syscall.
*/
if (xen_start_info.store_evtchn == 0)
return ENOENT;
mutex_enter(&xenbus_dev_open_mtx);
u = kfs->kfs_v;
if (u == NULL) {
mutex_exit(&xenbus_dev_open_mtx);
u = kmem_zalloc(sizeof(*u), KM_SLEEP);
SLIST_INIT(&u->lwps);
mutex_init(&u->mtx, MUTEX_DEFAULT, IPL_NONE);
mutex_enter(&xenbus_dev_open_mtx);
/*
* Must re-check if filled while waiting in alloc
* by some other lwp.
*/
if (kfs->kfs_v) {
kmem_free(u, sizeof(*u));
u = kfs->kfs_v;
} else {
kfs->kfs_v = u;
}
};
mutex_exit(&xenbus_dev_open_mtx);
mutex_enter(&u->mtx);
SLIST_FOREACH(xlwp, &u->lwps, lwp_next) {
if (xlwp->lwp == curlwp) {
break;
}
}
if (xlwp == NULL) {
mutex_exit(&u->mtx);
mutex_enter(&u->mtx);
/*
* While alloc can block, this can't be re-entered with
* curlwp, so no need to re-check. Also the node can't
* be closed while we are blocked here.
*/
SLIST_INSERT_HEAD(&u->lwps, xlwp, lwp_next);
}
mutex_exit(&u->mtx);