// SPDX-License-Identifier: GPL-2.0 /* * Copyright (C) 2012 Intel, Inc. * Copyright (C) 2013 Intel, Inc. * Copyright (C) 2014 Linaro Limited * Copyright (C) 2011-2016 Google, Inc. * * This software is licensed under the terms of the GNU General Public * License version 2, as published by the Free Software Foundation, and * may be copied, distributed, and modified under those terms. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. *
*/
/* This source file contains the implementation of a special device driver * that intends to provide a *very* fast communication channel between the * guest system and the QEMU emulator. * * Usage from the guest is simply the following (error handling simplified): * * int fd = open("/dev/qemu_pipe",O_RDWR); * .... write() or read() through the pipe. * * This driver doesn't deal with the exact protocol used during the session. * It is intended to be as simple as something like: * * // do this _just_ after opening the fd to connect to a specific * // emulator service. * const char* msg = "<pipename>"; * if (write(fd, msg, strlen(msg)+1) < 0) { * ... could not connect to <pipename> service * close(fd); * } * * // after this, simply read() and write() to communicate with the * // service. Exact protocol details left as an exercise to the reader. * * This driver is very fast because it doesn't copy any data through * intermediate buffers, since the emulator is capable of translating * guest user addresses into host ones. * * Note that we must however ensure that each user page involved in the * exchange is properly mapped during a transfer.
*/
/* * Update this when something changes in the driver's behavior so the host * can benefit from knowing it
*/ enum {
PIPE_DRIVER_VERSION = 2,
PIPE_CURRENT_DEVICE_VERSION = 2
};
/* A per-pipe command structure, shared with the host */ struct goldfish_pipe_command {
s32 cmd; /* PipeCmdCode, guest -> host */
s32 id; /* pipe id, guest -> host */
s32 status; /* command execution status, host -> guest */
s32 reserved; /* to pad to 64-bit boundary */ union { /* Parameters for PIPE_CMD_{READ,WRITE} */ struct { /* number of buffers, guest -> host */
u32 buffers_count; /* number of consumed bytes, host -> guest */
s32 consumed_size; /* buffer pointers, guest -> host */
u64 ptrs[MAX_BUFFERS_PER_COMMAND]; /* buffer sizes, guest -> host */
u32 sizes[MAX_BUFFERS_PER_COMMAND];
} rw_params;
};
};
/* A single signalled pipe information */ struct signalled_pipe_buffer {
u32 id;
u32 flags;
};
/* Parameters for the PIPE_CMD_OPEN command */ struct open_command_param {
u64 command_buffer_ptr;
u32 rw_params_max_count;
};
/* Device-level set of buffers shared with the host */ struct goldfish_pipe_dev_buffers { struct open_command_param open_command_params; struct signalled_pipe_buffer
signalled_pipe_buffers[MAX_SIGNALLED_PIPES];
};
/* This data type models a given pipe instance */ struct goldfish_pipe { /* pipe ID - index into goldfish_pipe_dev::pipes array */
u32 id;
/* The wake flags pipe is waiting for * Note: not protected with any lock, uses atomic operations * and barriers to make it thread-safe.
*/ unsignedlong flags;
/* wake flags host have signalled, * - protected by goldfish_pipe_dev::lock
*/ unsignedlong signalled_flags;
/* A pointer to command buffer */ struct goldfish_pipe_command *command_buffer;
/* doubly linked list of signalled pipes, protected by * goldfish_pipe_dev::lock
*/ struct goldfish_pipe *prev_signalled; struct goldfish_pipe *next_signalled;
/* * A pipe's own lock. Protects the following: * - *command_buffer - makes sure a command can safely write its * parameters to the host and read the results back.
*/ struct mutex lock;
/* A wake queue for sleeping until host signals an event */
wait_queue_head_t wake_queue;
/* Pointer to the parent goldfish_pipe_dev instance */ struct goldfish_pipe_dev *dev;
/* A buffer of pages, too large to fit into a stack frame */ struct page *pages[MAX_BUFFERS_PER_COMMAND];
};
/* The global driver data. Holds a reference to the i/o page used to * communicate with the emulator, and a wake queue for blocked tasks * waiting to be awoken.
*/ struct goldfish_pipe_dev { /* A magic number to check if this is an instance of this struct */ void *magic;
/* * Global device spinlock. Protects the following members: * - pipes, pipes_capacity * - [*pipes, *pipes + pipes_capacity) - array data * - first_signalled_pipe, * goldfish_pipe::prev_signalled, * goldfish_pipe::next_signalled, * goldfish_pipe::signalled_flags - all singnalled-related fields, * in all allocated pipes * - open_command_params - PIPE_CMD_OPEN-related buffers * * It looks like a lot of different fields, but the trick is that * the only operation that happens often is the signalled pipes array * manipulation. That's why it's OK for now to keep the rest of the * fields under the same lock. If we notice too much contention because * of PIPE_CMD_OPEN, then we should add a separate lock there.
*/
spinlock_t lock;
/* * Array of the pipes of |pipes_capacity| elements, * indexed by goldfish_pipe::id
*/ struct goldfish_pipe **pipes;
u32 pipes_capacity;
/* Pointers to the buffers host uses for interaction with this driver */ struct goldfish_pipe_dev_buffers *buffers;
/* Head of a doubly linked list of signalled pipes */ struct goldfish_pipe *first_signalled_pipe;
staticint goldfish_pipe_cmd(struct goldfish_pipe *pipe, enum PipeCmdCode cmd)
{ int status;
if (mutex_lock_interruptible(&pipe->lock)) return PIPE_ERROR_IO;
status = goldfish_pipe_cmd_locked(pipe, cmd);
mutex_unlock(&pipe->lock); return status;
}
/* * This function converts an error code returned by the emulator through * the PIPE_REG_STATUS i/o register into a valid negative errno value.
*/ staticint goldfish_pipe_error_convert(int status)
{ switch (status) { case PIPE_ERROR_AGAIN: return -EAGAIN; case PIPE_ERROR_NOMEM: return -ENOMEM; case PIPE_ERROR_IO: return -EIO; default: return -EINVAL;
}
}
staticint goldfish_pin_pages(unsignedlong first_page, unsignedlong last_page, unsignedint last_page_size, int is_write, struct page *pages[MAX_BUFFERS_PER_COMMAND], unsignedint *iter_last_page_size)
{ int ret; int requested_pages = ((last_page - first_page) >> PAGE_SHIFT) + 1;
/* If the emulator already closed the pipe, no need to go further */ if (unlikely(test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))) return -EIO; /* Null reads or writes succeeds */ if (unlikely(bufflen == 0)) return 0; /* Check the buffer range for access */ if (unlikely(!access_ok(buffer, bufflen))) return -EFAULT;
while (address < address_end) {
s32 consumed_size; int status;
ret = transfer_max_buffers(pipe, address, address_end, is_write,
last_page, last_page_size,
&consumed_size, &status); if (ret < 0) break;
if (consumed_size > 0) { /* No matter what's the status, we've transferred * something.
*/
count += consumed_size;
address += consumed_size;
} if (status > 0) continue; if (status == 0) { /* EOF */
ret = 0; break;
} if (count > 0) { /* * An error occurred, but we already transferred * something on one of the previous iterations. * Just return what we already copied and log this * err.
*/ if (status != PIPE_ERROR_AGAIN)
dev_err_ratelimited(pipe->dev->pdev_dev, "backend error %d on %s\n",
status, is_write ? "write" : "read"); break;
}
/* * If the error is not PIPE_ERROR_AGAIN, or if we are in * non-blocking mode, just return the error code.
*/ if (status != PIPE_ERROR_AGAIN ||
(filp->f_flags & O_NONBLOCK) != 0) {
ret = goldfish_pipe_error_convert(status); break;
}
status = wait_for_host_signal(pipe, is_write); if (status < 0) return status;
}
pipe = dev->first_signalled_pipe; if (pipe) {
*wakes = pipe->signalled_flags;
pipe->signalled_flags = 0; /* * This is an optimized version of * signalled_pipes_remove_locked() * - We want to make it as fast as possible to * wake the sleeping pipe operations faster.
*/
dev->first_signalled_pipe = pipe->next_signalled; if (dev->first_signalled_pipe)
dev->first_signalled_pipe->prev_signalled = NULL;
pipe->next_signalled = NULL;
}
static irqreturn_t goldfish_interrupt_task(int irq, void *dev_addr)
{ /* Iterate over the signalled pipes and wake them one by one */ struct goldfish_pipe_dev *dev = dev_addr; struct goldfish_pipe *pipe; int wakes;
while ((pipe = signalled_pipes_pop_front(dev, &wakes)) != NULL) { if (wakes & PIPE_WAKE_CLOSED) {
pipe->flags = 1 << BIT_CLOSED_ON_HOST;
} else { if (wakes & PIPE_WAKE_READ)
clear_bit(BIT_WAKE_ON_READ, &pipe->flags); if (wakes & PIPE_WAKE_WRITE)
clear_bit(BIT_WAKE_ON_WRITE, &pipe->flags);
} /* * wake_up_interruptible() implies a write barrier, so don't * explicitly add another one here.
*/
wake_up_interruptible(&pipe->wake_queue);
} return IRQ_HANDLED;
}
/* * The general idea of the (threaded) interrupt handling: * * 1. device raises an interrupt if there's at least one signalled pipe * 2. IRQ handler reads the signalled pipes and their count from the device * 3. device writes them into a shared buffer and returns the count * it only resets the IRQ if it has returned all signalled pipes, * otherwise it leaves it raised, so IRQ handler will be called * again for the next chunk * 4. IRQ handler adds all returned pipes to the device's signalled pipes list * 5. IRQ handler defers processing the signalled pipes from the list in a * separate context
*/ static irqreturn_t goldfish_pipe_interrupt(int irq, void *dev_id)
{
u32 count;
u32 i; unsignedlong flags; struct goldfish_pipe_dev *dev = dev_id;
if (dev->magic != &goldfish_pipe_device_deinit) return IRQ_NONE;
/* Request the signalled pipes from the device */
spin_lock_irqsave(&dev->lock, flags);
for (i = 0; i < count; ++i)
signalled_pipes_add_locked(dev,
dev->buffers->signalled_pipe_buffers[i].id,
dev->buffers->signalled_pipe_buffers[i].flags);
spin_unlock_irqrestore(&dev->lock, flags);
return IRQ_WAKE_THREAD;
}
staticint get_free_pipe_id_locked(struct goldfish_pipe_dev *dev)
{ int id;
for (id = 0; id < dev->pipes_capacity; ++id) if (!dev->pipes[id]) return id;
{ /* Reallocate the array. * Since get_free_pipe_id_locked runs with interrupts disabled, * we don't want to make calls that could lead to sleep.
*/
u32 new_capacity = 2 * dev->pipes_capacity; struct goldfish_pipe **pipes =
kcalloc(new_capacity, sizeof(*pipes), GFP_ATOMIC); if (!pipes) return -ENOMEM;
memcpy(pipes, dev->pipes, sizeof(*pipes) * dev->pipes_capacity);
kfree(dev->pipes);
dev->pipes = pipes;
id = dev->pipes_capacity;
dev->pipes_capacity = new_capacity;
} return id;
}
/* A helper function to get the instance of goldfish_pipe_dev from file */ staticstruct goldfish_pipe_dev *to_goldfish_pipe_dev(struct file *file)
{ struct miscdevice *miscdev = file->private_data;
/** * goldfish_pipe_open - open a channel to the AVD * @inode: inode of device * @file: file struct of opener * * Create a new pipe link between the emulator and the use application. * Each new request produces a new pipe. * * Note: we use the pipe ID as a mux. All goldfish emulations are 32bit * right now so this is fine. A move to 64bit will need this addressing
*/ staticint goldfish_pipe_open(struct inode *inode, struct file *file)
{ struct goldfish_pipe_dev *dev = to_goldfish_pipe_dev(file); unsignedlong flags; int id; int status;
/* * Command buffer needs to be allocated on its own page to make sure * it is physically contiguous in host's address space.
*/
BUILD_BUG_ON(sizeof(struct goldfish_pipe_command) > PAGE_SIZE);
pipe->command_buffer =
(struct goldfish_pipe_command *)__get_free_page(GFP_KERNEL); if (!pipe->command_buffer) {
status = -ENOMEM; goto err_pipe;
}
spin_lock_irqsave(&dev->lock, flags);
id = get_free_pipe_id_locked(dev); if (id < 0) {
status = id; goto err_id_locked;
}
/* Now tell the emulator we're opening a new pipe. */
dev->buffers->open_command_params.rw_params_max_count =
MAX_BUFFERS_PER_COMMAND;
dev->buffers->open_command_params.command_buffer_ptr =
(u64)(unsignedlong)__pa(pipe->command_buffer);
status = goldfish_pipe_cmd_locked(pipe, PIPE_CMD_OPEN);
spin_unlock_irqrestore(&dev->lock, flags); if (status < 0) goto err_cmd; /* All is done, save the pipe into the file's private data field */
file->private_data = pipe; return 0;
/* * We're going to pass two buffers, open_command_params and * signalled_pipe_buffers, to the host. This means each of those buffers * needs to be contained in a single physical page. The easiest choice * is to just allocate a page and place the buffers in it.
*/
BUILD_BUG_ON(sizeof(struct goldfish_pipe_dev_buffers) > PAGE_SIZE);
dev->buffers = (struct goldfish_pipe_dev_buffers *)
__get_free_page(GFP_KERNEL); if (!dev->buffers) {
kfree(dev->pipes);
misc_deregister(&dev->miscdev); return -ENOMEM;
}
/* Send the buffer addresses to the host */
write_pa_addr(&dev->buffers->signalled_pipe_buffers,
dev->base + PIPE_REG_SIGNAL_BUFFER,
dev->base + PIPE_REG_SIGNAL_BUFFER_HIGH);
dev->irq = platform_get_irq(pdev, 0); if (dev->irq < 0) return dev->irq;
/* * Exchange the versions with the host device * * Note: v1 driver used to not report its version, so we write it before * reading device version back: this allows the host implementation to * detect the old driver (if there was no version write before read).
*/
writel(PIPE_DRIVER_VERSION, dev->base + PIPE_REG_VERSION);
dev->version = readl(dev->base + PIPE_REG_VERSION); if (WARN_ON(dev->version < PIPE_CURRENT_DEVICE_VERSION)) return -EINVAL;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.