]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_ovec.c
hw/s390x/3270-ccw: avoid taking address of fields in packed struct
[mirror_qemu.git] / hw / ppc / spapr_ovec.c
CommitLineData
b20b7b7a
MR
1/*
2 * QEMU SPAPR Architecture Option Vector Helper Functions
3 *
4 * Copyright IBM Corp. 2016
5 *
6 * Authors:
7 * Bharata B Rao <bharata@linux.vnet.ibm.com>
8 * Michael Roth <mdroth@linux.vnet.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
12 */
13
14#include "qemu/osdep.h"
15#include "hw/ppc/spapr_ovec.h"
16#include "qemu/bitmap.h"
17#include "exec/address-spaces.h"
18#include "qemu/error-report.h"
5b929608 19#include "trace.h"
b20b7b7a
MR
20#include <libfdt.h>
21
b20b7b7a
MR
22#define OV_MAXBYTES 256 /* not including length byte */
23#define OV_MAXBITS (OV_MAXBYTES * BITS_PER_BYTE)
24
25/* we *could* work with bitmaps directly, but handling the bitmap privately
26 * allows us to more safely make assumptions about the bitmap size and
27 * simplify the calling code somewhat
28 */
ce2918cb 29struct SpaprOptionVector {
b20b7b7a 30 unsigned long *bitmap;
62ef3760
MR
31 int32_t bitmap_size; /* only used for migration */
32};
33
34const VMStateDescription vmstate_spapr_ovec = {
35 .name = "spapr_option_vector",
36 .version_id = 1,
37 .minimum_version_id = 1,
38 .fields = (VMStateField[]) {
ce2918cb 39 VMSTATE_BITMAP(bitmap, SpaprOptionVector, 1, bitmap_size),
62ef3760
MR
40 VMSTATE_END_OF_LIST()
41 }
b20b7b7a
MR
42};
43
ce2918cb 44SpaprOptionVector *spapr_ovec_new(void)
b20b7b7a 45{
ce2918cb 46 SpaprOptionVector *ov;
b20b7b7a 47
ce2918cb 48 ov = g_new0(SpaprOptionVector, 1);
b20b7b7a 49 ov->bitmap = bitmap_new(OV_MAXBITS);
62ef3760 50 ov->bitmap_size = OV_MAXBITS;
b20b7b7a
MR
51
52 return ov;
53}
54
ce2918cb 55SpaprOptionVector *spapr_ovec_clone(SpaprOptionVector *ov_orig)
b20b7b7a 56{
ce2918cb 57 SpaprOptionVector *ov;
b20b7b7a
MR
58
59 g_assert(ov_orig);
60
61 ov = spapr_ovec_new();
62 bitmap_copy(ov->bitmap, ov_orig->bitmap, OV_MAXBITS);
63
64 return ov;
65}
66
ce2918cb
DG
67void spapr_ovec_intersect(SpaprOptionVector *ov,
68 SpaprOptionVector *ov1,
69 SpaprOptionVector *ov2)
b20b7b7a
MR
70{
71 g_assert(ov);
72 g_assert(ov1);
73 g_assert(ov2);
74
75 bitmap_and(ov->bitmap, ov1->bitmap, ov2->bitmap, OV_MAXBITS);
76}
77
78/* returns true if options bits were removed, false otherwise */
ce2918cb
DG
79bool spapr_ovec_diff(SpaprOptionVector *ov,
80 SpaprOptionVector *ov_old,
81 SpaprOptionVector *ov_new)
b20b7b7a
MR
82{
83 unsigned long *change_mask = bitmap_new(OV_MAXBITS);
84 unsigned long *removed_bits = bitmap_new(OV_MAXBITS);
85 bool bits_were_removed = false;
86
87 g_assert(ov);
88 g_assert(ov_old);
89 g_assert(ov_new);
90
91 bitmap_xor(change_mask, ov_old->bitmap, ov_new->bitmap, OV_MAXBITS);
92 bitmap_and(ov->bitmap, ov_new->bitmap, change_mask, OV_MAXBITS);
93 bitmap_and(removed_bits, ov_old->bitmap, change_mask, OV_MAXBITS);
94
95 if (!bitmap_empty(removed_bits, OV_MAXBITS)) {
96 bits_were_removed = true;
97 }
98
99 g_free(change_mask);
100 g_free(removed_bits);
101
102 return bits_were_removed;
103}
104
ce2918cb 105void spapr_ovec_cleanup(SpaprOptionVector *ov)
b20b7b7a
MR
106{
107 if (ov) {
108 g_free(ov->bitmap);
109 g_free(ov);
110 }
111}
112
ce2918cb 113void spapr_ovec_set(SpaprOptionVector *ov, long bitnr)
b20b7b7a
MR
114{
115 g_assert(ov);
719a3077 116 g_assert(bitnr < OV_MAXBITS);
b20b7b7a
MR
117
118 set_bit(bitnr, ov->bitmap);
119}
120
ce2918cb 121void spapr_ovec_clear(SpaprOptionVector *ov, long bitnr)
b20b7b7a
MR
122{
123 g_assert(ov);
719a3077 124 g_assert(bitnr < OV_MAXBITS);
b20b7b7a
MR
125
126 clear_bit(bitnr, ov->bitmap);
127}
128
ce2918cb 129bool spapr_ovec_test(SpaprOptionVector *ov, long bitnr)
b20b7b7a
MR
130{
131 g_assert(ov);
719a3077 132 g_assert(bitnr < OV_MAXBITS);
b20b7b7a
MR
133
134 return test_bit(bitnr, ov->bitmap) ? true : false;
135}
136
137static void guest_byte_to_bitmap(uint8_t entry, unsigned long *bitmap,
138 long bitmap_offset)
139{
140 int i;
141
142 for (i = 0; i < BITS_PER_BYTE; i++) {
143 if (entry & (1 << (BITS_PER_BYTE - 1 - i))) {
144 bitmap_set(bitmap, bitmap_offset + i, 1);
145 }
146 }
147}
148
149static uint8_t guest_byte_from_bitmap(unsigned long *bitmap, long bitmap_offset)
150{
151 uint8_t entry = 0;
152 int i;
153
154 for (i = 0; i < BITS_PER_BYTE; i++) {
155 if (test_bit(bitmap_offset + i, bitmap)) {
156 entry |= (1 << (BITS_PER_BYTE - 1 - i));
157 }
158 }
159
160 return entry;
161}
162
163static target_ulong vector_addr(target_ulong table_addr, int vector)
164{
165 uint16_t vector_count, vector_len;
166 int i;
167
168 vector_count = ldub_phys(&address_space_memory, table_addr) + 1;
169 if (vector > vector_count) {
170 return 0;
171 }
172 table_addr++; /* skip nr option vectors */
173
174 for (i = 0; i < vector - 1; i++) {
175 vector_len = ldub_phys(&address_space_memory, table_addr) + 1;
176 table_addr += vector_len + 1; /* bit-vector + length byte */
177 }
178 return table_addr;
179}
180
ce2918cb 181SpaprOptionVector *spapr_ovec_parse_vector(target_ulong table_addr, int vector)
b20b7b7a 182{
ce2918cb 183 SpaprOptionVector *ov;
b20b7b7a
MR
184 target_ulong addr;
185 uint16_t vector_len;
186 int i;
187
188 g_assert(table_addr);
719a3077 189 g_assert(vector >= 1); /* vector numbering starts at 1 */
b20b7b7a
MR
190
191 addr = vector_addr(table_addr, vector);
192 if (!addr) {
193 /* specified vector isn't present */
194 return NULL;
195 }
196
197 vector_len = ldub_phys(&address_space_memory, addr++) + 1;
719a3077 198 g_assert(vector_len <= OV_MAXBYTES);
b20b7b7a
MR
199 ov = spapr_ovec_new();
200
201 for (i = 0; i < vector_len; i++) {
202 uint8_t entry = ldub_phys(&address_space_memory, addr + i);
203 if (entry) {
5b929608 204 trace_spapr_ovec_parse_vector(vector, i + 1, vector_len, entry);
b20b7b7a
MR
205 guest_byte_to_bitmap(entry, ov->bitmap, i * BITS_PER_BYTE);
206 }
207 }
208
209 return ov;
210}
211
212int spapr_ovec_populate_dt(void *fdt, int fdt_offset,
ce2918cb 213 SpaprOptionVector *ov, const char *name)
b20b7b7a
MR
214{
215 uint8_t vec[OV_MAXBYTES + 1];
216 uint16_t vec_len;
217 unsigned long lastbit;
218 int i;
219
220 g_assert(ov);
221
222 lastbit = find_last_bit(ov->bitmap, OV_MAXBITS);
223 /* if no bits are set, include at least 1 byte of the vector so we can
224 * still encoded this in the device tree while abiding by the same
225 * encoding/sizing expected in ibm,client-architecture-support
226 */
227 vec_len = (lastbit == OV_MAXBITS) ? 1 : lastbit / BITS_PER_BYTE + 1;
719a3077 228 g_assert(vec_len <= OV_MAXBYTES);
b20b7b7a
MR
229 /* guest expects vector len encoded as vec_len - 1, since the length byte
230 * is assumed and not included, and the first byte of the vector
231 * is assumed as well
232 */
233 vec[0] = vec_len - 1;
234
235 for (i = 1; i < vec_len + 1; i++) {
236 vec[i] = guest_byte_from_bitmap(ov->bitmap, (i - 1) * BITS_PER_BYTE);
237 if (vec[i]) {
5b929608 238 trace_spapr_ovec_populate_dt(i, vec_len, vec[i]);
b20b7b7a
MR
239 }
240 }
241
fe93e3e6 242 return fdt_setprop(fdt, fdt_offset, name, vec, vec_len + 1);
b20b7b7a 243}