target/arm: Deliver BKPT/BRK exceptions to correct exception level
[qemu.git] / hw / 9pfs / virtio-9p-device.c
bloba3288cbe56adae814ed7b02eafaba37aa40f149b
1 /*
2 * Virtio 9p backend
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "hw/virtio/virtio.h"
16 #include "qemu/sockets.h"
17 #include "virtio-9p.h"
18 #include "fsdev/qemu-fsdev.h"
19 #include "coth.h"
20 #include "hw/virtio/virtio-access.h"
21 #include "qemu/iov.h"
22 #include "qemu/module.h"
24 static void virtio_9p_push_and_notify(V9fsPDU *pdu)
26 V9fsState *s = pdu->s;
27 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
28 VirtQueueElement *elem = v->elems[pdu->idx];
30 /* push onto queue and notify */
31 virtqueue_push(v->vq, elem, pdu->size);
32 g_free(elem);
33 v->elems[pdu->idx] = NULL;
35 /* FIXME: we should batch these completions */
36 virtio_notify(VIRTIO_DEVICE(v), v->vq);
39 static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
41 V9fsVirtioState *v = (V9fsVirtioState *)vdev;
42 V9fsState *s = &v->state;
43 V9fsPDU *pdu;
44 ssize_t len;
45 VirtQueueElement *elem;
47 while ((pdu = pdu_alloc(s))) {
48 P9MsgHeader out;
50 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
51 if (!elem) {
52 goto out_free_pdu;
55 if (iov_size(elem->in_sg, elem->in_num) < 7) {
56 virtio_error(vdev,
57 "The guest sent a VirtFS request without space for "
58 "the reply");
59 goto out_free_req;
62 len = iov_to_buf(elem->out_sg, elem->out_num, 0, &out, 7);
63 if (len != 7) {
64 virtio_error(vdev, "The guest sent a malformed VirtFS request: "
65 "header size is %zd, should be 7", len);
66 goto out_free_req;
69 v->elems[pdu->idx] = elem;
71 pdu_submit(pdu, &out);
74 return;
76 out_free_req:
77 virtqueue_detach_element(vq, elem, 0);
78 g_free(elem);
79 out_free_pdu:
80 pdu_free(pdu);
83 static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features,
84 Error **errp)
86 virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG);
87 return features;
90 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
92 int len;
93 struct virtio_9p_config *cfg;
94 V9fsVirtioState *v = VIRTIO_9P(vdev);
95 V9fsState *s = &v->state;
97 len = strlen(s->tag);
98 cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
99 virtio_stw_p(vdev, &cfg->tag_len, len);
100 /* We don't copy the terminating null to config space */
101 memcpy(cfg->tag, s->tag, len);
102 memcpy(config, cfg, v->config_size);
103 g_free(cfg);
106 static void virtio_9p_reset(VirtIODevice *vdev)
108 V9fsVirtioState *v = (V9fsVirtioState *)vdev;
110 v9fs_reset(&v->state);
113 static ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset,
114 const char *fmt, va_list ap)
116 V9fsState *s = pdu->s;
117 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
118 VirtQueueElement *elem = v->elems[pdu->idx];
119 ssize_t ret;
121 ret = v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap);
122 if (ret < 0) {
123 VirtIODevice *vdev = VIRTIO_DEVICE(v);
125 virtio_error(vdev, "Failed to encode VirtFS reply type %d",
126 pdu->id + 1);
128 return ret;
131 static ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset,
132 const char *fmt, va_list ap)
134 V9fsState *s = pdu->s;
135 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
136 VirtQueueElement *elem = v->elems[pdu->idx];
137 ssize_t ret;
139 ret = v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap);
140 if (ret < 0) {
141 VirtIODevice *vdev = VIRTIO_DEVICE(v);
143 virtio_error(vdev, "Failed to decode VirtFS request type %d", pdu->id);
145 return ret;
148 static void virtio_init_in_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
149 unsigned int *pniov, size_t size)
151 V9fsState *s = pdu->s;
152 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
153 VirtQueueElement *elem = v->elems[pdu->idx];
154 size_t buf_size = iov_size(elem->in_sg, elem->in_num);
156 if (buf_size < size) {
157 VirtIODevice *vdev = VIRTIO_DEVICE(v);
159 virtio_error(vdev,
160 "VirtFS reply type %d needs %zu bytes, buffer has %zu",
161 pdu->id + 1, size, buf_size);
164 *piov = elem->in_sg;
165 *pniov = elem->in_num;
168 static void virtio_init_out_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
169 unsigned int *pniov, size_t size)
171 V9fsState *s = pdu->s;
172 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
173 VirtQueueElement *elem = v->elems[pdu->idx];
174 size_t buf_size = iov_size(elem->out_sg, elem->out_num);
176 if (buf_size < size) {
177 VirtIODevice *vdev = VIRTIO_DEVICE(v);
179 virtio_error(vdev,
180 "VirtFS request type %d needs %zu bytes, buffer has %zu",
181 pdu->id, size, buf_size);
184 *piov = elem->out_sg;
185 *pniov = elem->out_num;
188 static const V9fsTransport virtio_9p_transport = {
189 .pdu_vmarshal = virtio_pdu_vmarshal,
190 .pdu_vunmarshal = virtio_pdu_vunmarshal,
191 .init_in_iov_from_pdu = virtio_init_in_iov_from_pdu,
192 .init_out_iov_from_pdu = virtio_init_out_iov_from_pdu,
193 .push_and_notify = virtio_9p_push_and_notify,
196 static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
198 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
199 V9fsVirtioState *v = VIRTIO_9P(dev);
200 V9fsState *s = &v->state;
202 if (v9fs_device_realize_common(s, &virtio_9p_transport, errp)) {
203 return;
206 v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag);
207 virtio_init(vdev, "virtio-9p", VIRTIO_ID_9P, v->config_size);
208 v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
211 static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp)
213 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
214 V9fsVirtioState *v = VIRTIO_9P(dev);
215 V9fsState *s = &v->state;
217 virtio_cleanup(vdev);
218 v9fs_device_unrealize_common(s, errp);
221 /* virtio-9p device */
223 static const VMStateDescription vmstate_virtio_9p = {
224 .name = "virtio-9p",
225 .minimum_version_id = 1,
226 .version_id = 1,
227 .fields = (VMStateField[]) {
228 VMSTATE_VIRTIO_DEVICE,
229 VMSTATE_END_OF_LIST()
233 static Property virtio_9p_properties[] = {
234 DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag),
235 DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id),
236 DEFINE_PROP_END_OF_LIST(),
239 static void virtio_9p_class_init(ObjectClass *klass, void *data)
241 DeviceClass *dc = DEVICE_CLASS(klass);
242 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
244 dc->props = virtio_9p_properties;
245 dc->vmsd = &vmstate_virtio_9p;
246 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
247 vdc->realize = virtio_9p_device_realize;
248 vdc->unrealize = virtio_9p_device_unrealize;
249 vdc->get_features = virtio_9p_get_features;
250 vdc->get_config = virtio_9p_get_config;
251 vdc->reset = virtio_9p_reset;
254 static const TypeInfo virtio_device_info = {
255 .name = TYPE_VIRTIO_9P,
256 .parent = TYPE_VIRTIO_DEVICE,
257 .instance_size = sizeof(V9fsVirtioState),
258 .class_init = virtio_9p_class_init,
261 static void virtio_9p_register_types(void)
263 type_register_static(&virtio_device_info);
266 type_init(virtio_9p_register_types)