diff options
author | Michael S. Tsirkin <mst@redhat.com> | 2014-10-22 08:35:56 -0400 |
---|---|---|
committer | Michael S. Tsirkin <mst@redhat.com> | 2014-12-09 05:05:24 -0500 |
commit | eef960a04354d13426c43a4e3750a5e2b383040c (patch) | |
tree | a5d6e327dbb22c85b2f6592077a544bcc0153471 | |
parent | 4ec22faeb2a165b6ce7009f91309046010b3f282 (diff) |
virtio: memory access APIs
virtio 1.0 makes all memory structures LE, so
we need APIs to conditionally do a byteswap on BE
architectures.
To make it easier to check code statically,
add virtio specific types for multi-byte integers
in memory.
Add low level wrappers that do a byteswap conditionally, these will be
useful e.g. for vhost. Add high level wrappers that
query device endian-ness and act accordingly.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Cornelia Huck <cornelia.huck@de.ibm.com>
-rw-r--r-- | include/linux/virtio_byteorder.h | 59 | ||||
-rw-r--r-- | include/linux/virtio_config.h | 32 | ||||
-rw-r--r-- | include/uapi/linux/Kbuild | 1 | ||||
-rw-r--r-- | include/uapi/linux/virtio_ring.h | 45 | ||||
-rw-r--r-- | include/uapi/linux/virtio_types.h | 46 |
5 files changed, 161 insertions, 22 deletions
diff --git a/include/linux/virtio_byteorder.h b/include/linux/virtio_byteorder.h new file mode 100644 index 000000000000..51865d05b267 --- /dev/null +++ b/include/linux/virtio_byteorder.h | |||
@@ -0,0 +1,59 @@ | |||
1 | #ifndef _LINUX_VIRTIO_BYTEORDER_H | ||
2 | #define _LINUX_VIRTIO_BYTEORDER_H | ||
3 | #include <linux/types.h> | ||
4 | #include <uapi/linux/virtio_types.h> | ||
5 | |||
6 | /* | ||
7 | * Low-level memory accessors for handling virtio in modern little endian and in | ||
8 | * compatibility native endian format. | ||
9 | */ | ||
10 | |||
11 | static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val) | ||
12 | { | ||
13 | if (little_endian) | ||
14 | return le16_to_cpu((__force __le16)val); | ||
15 | else | ||
16 | return (__force u16)val; | ||
17 | } | ||
18 | |||
19 | static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val) | ||
20 | { | ||
21 | if (little_endian) | ||
22 | return (__force __virtio16)cpu_to_le16(val); | ||
23 | else | ||
24 | return (__force __virtio16)val; | ||
25 | } | ||
26 | |||
27 | static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val) | ||
28 | { | ||
29 | if (little_endian) | ||
30 | return le32_to_cpu((__force __le32)val); | ||
31 | else | ||
32 | return (__force u32)val; | ||
33 | } | ||
34 | |||
35 | static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val) | ||
36 | { | ||
37 | if (little_endian) | ||
38 | return (__force __virtio32)cpu_to_le32(val); | ||
39 | else | ||
40 | return (__force __virtio32)val; | ||
41 | } | ||
42 | |||
43 | static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val) | ||
44 | { | ||
45 | if (little_endian) | ||
46 | return le64_to_cpu((__force __le64)val); | ||
47 | else | ||
48 | return (__force u64)val; | ||
49 | } | ||
50 | |||
51 | static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val) | ||
52 | { | ||
53 | if (little_endian) | ||
54 | return (__force __virtio64)cpu_to_le64(val); | ||
55 | else | ||
56 | return (__force __virtio64)val; | ||
57 | } | ||
58 | |||
59 | #endif /* _LINUX_VIRTIO_BYTEORDER */ | ||
diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h index f51788439574..02f0acb549d6 100644 --- a/include/linux/virtio_config.h +++ b/include/linux/virtio_config.h | |||
@@ -4,6 +4,7 @@ | |||
4 | #include <linux/err.h> | 4 | #include <linux/err.h> |
5 | #include <linux/bug.h> | 5 | #include <linux/bug.h> |
6 | #include <linux/virtio.h> | 6 | #include <linux/virtio.h> |
7 | #include <linux/virtio_byteorder.h> | ||
7 | #include <uapi/linux/virtio_config.h> | 8 | #include <uapi/linux/virtio_config.h> |
8 | 9 | ||
9 | /** | 10 | /** |
@@ -199,6 +200,37 @@ int virtqueue_set_affinity(struct virtqueue *vq, int cpu) | |||
199 | return 0; | 200 | return 0; |
200 | } | 201 | } |
201 | 202 | ||
203 | /* Memory accessors */ | ||
204 | static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val) | ||
205 | { | ||
206 | return __virtio16_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val); | ||
207 | } | ||
208 | |||
209 | static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val) | ||
210 | { | ||
211 | return __cpu_to_virtio16(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val); | ||
212 | } | ||
213 | |||
214 | static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val) | ||
215 | { | ||
216 | return __virtio32_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val); | ||
217 | } | ||
218 | |||
219 | static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val) | ||
220 | { | ||
221 | return __cpu_to_virtio32(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val); | ||
222 | } | ||
223 | |||
224 | static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val) | ||
225 | { | ||
226 | return __virtio64_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val); | ||
227 | } | ||
228 | |||
229 | static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val) | ||
230 | { | ||
231 | return __cpu_to_virtio64(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val); | ||
232 | } | ||
233 | |||
202 | /* Config space accessors. */ | 234 | /* Config space accessors. */ |
203 | #define virtio_cread(vdev, structname, member, ptr) \ | 235 | #define virtio_cread(vdev, structname, member, ptr) \ |
204 | do { \ | 236 | do { \ |
diff --git a/include/uapi/linux/Kbuild b/include/uapi/linux/Kbuild index 8523f9bb72f2..615f96a6022a 100644 --- a/include/uapi/linux/Kbuild +++ b/include/uapi/linux/Kbuild | |||
@@ -423,6 +423,7 @@ header-y += virtio_blk.h | |||
423 | header-y += virtio_config.h | 423 | header-y += virtio_config.h |
424 | header-y += virtio_console.h | 424 | header-y += virtio_console.h |
425 | header-y += virtio_ids.h | 425 | header-y += virtio_ids.h |
426 | header-y += virtio_types.h | ||
426 | header-y += virtio_net.h | 427 | header-y += virtio_net.h |
427 | header-y += virtio_pci.h | 428 | header-y += virtio_pci.h |
428 | header-y += virtio_ring.h | 429 | header-y += virtio_ring.h |
diff --git a/include/uapi/linux/virtio_ring.h b/include/uapi/linux/virtio_ring.h index a99f9b7caa67..61c818a7fe70 100644 --- a/include/uapi/linux/virtio_ring.h +++ b/include/uapi/linux/virtio_ring.h | |||
@@ -32,6 +32,7 @@ | |||
32 | * | 32 | * |
33 | * Copyright Rusty Russell IBM Corporation 2007. */ | 33 | * Copyright Rusty Russell IBM Corporation 2007. */ |
34 | #include <linux/types.h> | 34 | #include <linux/types.h> |
35 | #include <linux/virtio_types.h> | ||
35 | 36 | ||
36 | /* This marks a buffer as continuing via the next field. */ | 37 | /* This marks a buffer as continuing via the next field. */ |
37 | #define VRING_DESC_F_NEXT 1 | 38 | #define VRING_DESC_F_NEXT 1 |
@@ -61,32 +62,32 @@ | |||
61 | /* Virtio ring descriptors: 16 bytes. These can chain together via "next". */ | 62 | /* Virtio ring descriptors: 16 bytes. These can chain together via "next". */ |
62 | struct vring_desc { | 63 | struct vring_desc { |
63 | /* Address (guest-physical). */ | 64 | /* Address (guest-physical). */ |
64 | __u64 addr; | 65 | __virtio64 addr; |
65 | /* Length. */ | 66 | /* Length. */ |
66 | __u32 len; | 67 | __virtio32 len; |
67 | /* The flags as indicated above. */ | 68 | /* The flags as indicated above. */ |
68 | __u16 flags; | 69 | __virtio16 flags; |
69 | /* We chain unused descriptors via this, too */ | 70 | /* We chain unused descriptors via this, too */ |
70 | __u16 next; | 71 | __virtio16 next; |
71 | }; | 72 | }; |
72 | 73 | ||
73 | struct vring_avail { | 74 | struct vring_avail { |
74 | __u16 flags; | 75 | __virtio16 flags; |
75 | __u16 idx; | 76 | __virtio16 idx; |
76 | __u16 ring[]; | 77 | __virtio16 ring[]; |
77 | }; | 78 | }; |
78 | 79 | ||
79 | /* u32 is used here for ids for padding reasons. */ | 80 | /* u32 is used here for ids for padding reasons. */ |
80 | struct vring_used_elem { | 81 | struct vring_used_elem { |
81 | /* Index of start of used descriptor chain. */ | 82 | /* Index of start of used descriptor chain. */ |
82 | __u32 id; | 83 | __virtio32 id; |
83 | /* Total length of the descriptor chain which was used (written to) */ | 84 | /* Total length of the descriptor chain which was used (written to) */ |
84 | __u32 len; | 85 | __virtio32 len; |
85 | }; | 86 | }; |
86 | 87 | ||
87 | struct vring_used { | 88 | struct vring_used { |
88 | __u16 flags; | 89 | __virtio16 flags; |
89 | __u16 idx; | 90 | __virtio16 idx; |
90 | struct vring_used_elem ring[]; | 91 | struct vring_used_elem ring[]; |
91 | }; | 92 | }; |
92 | 93 | ||
@@ -109,25 +110,25 @@ struct vring { | |||
109 | * struct vring_desc desc[num]; | 110 | * struct vring_desc desc[num]; |
110 | * | 111 | * |
111 | * // A ring of available descriptor heads with free-running index. | 112 | * // A ring of available descriptor heads with free-running index. |
112 | * __u16 avail_flags; | 113 | * __virtio16 avail_flags; |
113 | * __u16 avail_idx; | 114 | * __virtio16 avail_idx; |
114 | * __u16 available[num]; | 115 | * __virtio16 available[num]; |
115 | * __u16 used_event_idx; | 116 | * __virtio16 used_event_idx; |
116 | * | 117 | * |
117 | * // Padding to the next align boundary. | 118 | * // Padding to the next align boundary. |
118 | * char pad[]; | 119 | * char pad[]; |
119 | * | 120 | * |
120 | * // A ring of used descriptor heads with free-running index. | 121 | * // A ring of used descriptor heads with free-running index. |
121 | * __u16 used_flags; | 122 | * __virtio16 used_flags; |
122 | * __u16 used_idx; | 123 | * __virtio16 used_idx; |
123 | * struct vring_used_elem used[num]; | 124 | * struct vring_used_elem used[num]; |
124 | * __u16 avail_event_idx; | 125 | * __virtio16 avail_event_idx; |
125 | * }; | 126 | * }; |
126 | */ | 127 | */ |
127 | /* We publish the used event index at the end of the available ring, and vice | 128 | /* We publish the used event index at the end of the available ring, and vice |
128 | * versa. They are at the end for backwards compatibility. */ | 129 | * versa. They are at the end for backwards compatibility. */ |
129 | #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num]) | 130 | #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num]) |
130 | #define vring_avail_event(vr) (*(__u16 *)&(vr)->used->ring[(vr)->num]) | 131 | #define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num]) |
131 | 132 | ||
132 | static inline void vring_init(struct vring *vr, unsigned int num, void *p, | 133 | static inline void vring_init(struct vring *vr, unsigned int num, void *p, |
133 | unsigned long align) | 134 | unsigned long align) |
@@ -135,15 +136,15 @@ static inline void vring_init(struct vring *vr, unsigned int num, void *p, | |||
135 | vr->num = num; | 136 | vr->num = num; |
136 | vr->desc = p; | 137 | vr->desc = p; |
137 | vr->avail = p + num*sizeof(struct vring_desc); | 138 | vr->avail = p + num*sizeof(struct vring_desc); |
138 | vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + sizeof(__u16) | 139 | vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + sizeof(__virtio16) |
139 | + align-1) & ~(align - 1)); | 140 | + align-1) & ~(align - 1)); |
140 | } | 141 | } |
141 | 142 | ||
142 | static inline unsigned vring_size(unsigned int num, unsigned long align) | 143 | static inline unsigned vring_size(unsigned int num, unsigned long align) |
143 | { | 144 | { |
144 | return ((sizeof(struct vring_desc) * num + sizeof(__u16) * (3 + num) | 145 | return ((sizeof(struct vring_desc) * num + sizeof(__virtio16) * (3 + num) |
145 | + align - 1) & ~(align - 1)) | 146 | + align - 1) & ~(align - 1)) |
146 | + sizeof(__u16) * 3 + sizeof(struct vring_used_elem) * num; | 147 | + sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num; |
147 | } | 148 | } |
148 | 149 | ||
149 | /* The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX */ | 150 | /* The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX */ |
diff --git a/include/uapi/linux/virtio_types.h b/include/uapi/linux/virtio_types.h new file mode 100644 index 000000000000..e845e8c4cbee --- /dev/null +++ b/include/uapi/linux/virtio_types.h | |||
@@ -0,0 +1,46 @@ | |||
1 | #ifndef _UAPI_LINUX_VIRTIO_TYPES_H | ||
2 | #define _UAPI_LINUX_VIRTIO_TYPES_H | ||
3 | /* Type definitions for virtio implementations. | ||
4 | * | ||
5 | * This header is BSD licensed so anyone can use the definitions to implement | ||
6 | * compatible drivers/servers. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * 3. Neither the name of IBM nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | * | ||
31 | * Copyright (C) 2014 Red Hat, Inc. | ||
32 | * Author: Michael S. Tsirkin <mst@redhat.com> | ||
33 | */ | ||
34 | #include <linux/types.h> | ||
35 | |||
36 | /* | ||
37 | * __virtio{16,32,64} have the following meaning: | ||
38 | * - __u{16,32,64} for virtio devices in legacy mode, accessed in native endian | ||
39 | * - __le{16,32,64} for standard-compliant virtio devices | ||
40 | */ | ||
41 | |||
42 | typedef __u16 __bitwise__ __virtio16; | ||
43 | typedef __u32 __bitwise__ __virtio32; | ||
44 | typedef __u64 __bitwise__ __virtio64; | ||
45 | |||
46 | #endif /* _UAPI_LINUX_VIRTIO_TYPES_H */ | ||