]> git.proxmox.com Git - qemu.git/blame - iov.c
msi: Guard msi/msix_write_config with msi_present
[qemu.git] / iov.c
CommitLineData
e4d5639d
AS
1/*
2 * Helpers for getting linearized buffers from iov / filling buffers into iovs
3 *
4 * Copyright IBM, Corp. 2007, 2008
5 * Copyright (C) 2010 Red Hat, Inc.
6 *
7 * Author(s):
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Amit Shah <amit.shah@redhat.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
6b620ca3
PB
13 *
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
e4d5639d
AS
16 */
17
18#include "iov.h"
19
348e7b8d
HR
20size_t iov_from_buf(struct iovec *iov, unsigned int iov_cnt,
21 const void *buf, size_t iov_off, size_t size)
e4d5639d 22{
348e7b8d 23 size_t iovec_off, buf_off;
e4d5639d
AS
24 unsigned int i;
25
348e7b8d
HR
26 iovec_off = 0;
27 buf_off = 0;
28 for (i = 0; i < iov_cnt && size; i++) {
29 if (iov_off < (iovec_off + iov[i].iov_len)) {
30 size_t len = MIN((iovec_off + iov[i].iov_len) - iov_off, size);
e4d5639d 31
348e7b8d 32 memcpy(iov[i].iov_base + (iov_off - iovec_off), buf + buf_off, len);
e4d5639d 33
348e7b8d
HR
34 buf_off += len;
35 iov_off += len;
36 size -= len;
37 }
38 iovec_off += iov[i].iov_len;
e4d5639d 39 }
348e7b8d 40 return buf_off;
e4d5639d 41}
fa6111f2 42
348e7b8d
HR
43size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
44 void *buf, size_t iov_off, size_t size)
fa6111f2
AS
45{
46 uint8_t *ptr;
348e7b8d 47 size_t iovec_off, buf_off;
fa6111f2
AS
48 unsigned int i;
49
50 ptr = buf;
348e7b8d 51 iovec_off = 0;
fa6111f2 52 buf_off = 0;
348e7b8d
HR
53 for (i = 0; i < iov_cnt && size; i++) {
54 if (iov_off < (iovec_off + iov[i].iov_len)) {
55 size_t len = MIN((iovec_off + iov[i].iov_len) - iov_off , size);
fa6111f2 56
348e7b8d 57 memcpy(ptr + buf_off, iov[i].iov_base + (iov_off - iovec_off), len);
fa6111f2
AS
58
59 buf_off += len;
348e7b8d 60 iov_off += len;
fa6111f2
AS
61 size -= len;
62 }
348e7b8d 63 iovec_off += iov[i].iov_len;
8d15028e
GH
64 }
65 return buf_off;
66}
67
68size_t iov_clear(const struct iovec *iov, const unsigned int iov_cnt,
69 size_t iov_off, size_t size)
70{
71 size_t iovec_off, buf_off;
72 unsigned int i;
73
74 iovec_off = 0;
75 buf_off = 0;
76 for (i = 0; i < iov_cnt && size; i++) {
77 if (iov_off < (iovec_off + iov[i].iov_len)) {
78 size_t len = MIN((iovec_off + iov[i].iov_len) - iov_off , size);
79
80 memset(iov[i].iov_base + (iov_off - iovec_off), 0, len);
81
82 buf_off += len;
83 iov_off += len;
84 size -= len;
85 }
86 iovec_off += iov[i].iov_len;
fa6111f2
AS
87 }
88 return buf_off;
89}
90
348e7b8d 91size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
fa6111f2
AS
92{
93 size_t len;
94 unsigned int i;
95
96 len = 0;
348e7b8d 97 for (i = 0; i < iov_cnt; i++) {
fa6111f2
AS
98 len += iov[i].iov_len;
99 }
100 return len;
101}
3a1dca94
GH
102
103void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
104 FILE *fp, const char *prefix, size_t limit)
105{
106 unsigned int i, v, b;
107 uint8_t *c;
108
109 c = iov[0].iov_base;
110 for (i = 0, v = 0, b = 0; b < limit; i++, b++) {
111 if (i == iov[v].iov_len) {
112 i = 0; v++;
113 if (v == iov_cnt) {
114 break;
115 }
116 c = iov[v].iov_base;
117 }
118 if ((b % 16) == 0) {
119 fprintf(fp, "%s: %04x:", prefix, b);
120 }
121 if ((b % 4) == 0) {
122 fprintf(fp, " ");
123 }
124 fprintf(fp, " %02x", c[i]);
125 if ((b % 16) == 15) {
126 fprintf(fp, "\n");
127 }
128 }
129 if ((b % 16) != 0) {
130 fprintf(fp, "\n");
131 }
132}