xref: /openbmc/qemu/hw/s390x/s390-stattrib-kvm.c (revision 88daa112d4eda4e6c29f9f7004be09c13e4785df)
1903fd80bSClaudio Imbrenda /*
2903fd80bSClaudio Imbrenda  * s390 storage attributes device -- KVM object
3903fd80bSClaudio Imbrenda  *
4903fd80bSClaudio Imbrenda  * Copyright 2016 IBM Corp.
5903fd80bSClaudio Imbrenda  * Author(s): Claudio Imbrenda <imbrenda@linux.vnet.ibm.com>
6903fd80bSClaudio Imbrenda  *
7903fd80bSClaudio Imbrenda  * This work is licensed under the terms of the GNU GPL, version 2 or (at
8903fd80bSClaudio Imbrenda  * your option) any later version. See the COPYING file in the top-level
9903fd80bSClaudio Imbrenda  * directory.
10903fd80bSClaudio Imbrenda  */
11903fd80bSClaudio Imbrenda 
12903fd80bSClaudio Imbrenda #include "qemu/osdep.h"
13903fd80bSClaudio Imbrenda #include "hw/boards.h"
14903fd80bSClaudio Imbrenda #include "migration/qemu-file.h"
15903fd80bSClaudio Imbrenda #include "hw/s390x/storage-attributes.h"
16903fd80bSClaudio Imbrenda #include "qemu/error-report.h"
17903fd80bSClaudio Imbrenda #include "sysemu/kvm.h"
18903fd80bSClaudio Imbrenda #include "exec/ram_addr.h"
1967043607SCho, Yu-Chen #include "kvm/kvm_s390x.h"
20*e86f2434SCédric Le Goater #include "qapi/error.h"
21903fd80bSClaudio Imbrenda 
kvm_s390_stattrib_create(void)22903fd80bSClaudio Imbrenda Object *kvm_s390_stattrib_create(void)
23903fd80bSClaudio Imbrenda {
24903fd80bSClaudio Imbrenda     if (kvm_enabled() &&
25903fd80bSClaudio Imbrenda                 kvm_check_extension(kvm_state, KVM_CAP_S390_CMMA_MIGRATION)) {
26903fd80bSClaudio Imbrenda         return object_new(TYPE_KVM_S390_STATTRIB);
27903fd80bSClaudio Imbrenda     }
28903fd80bSClaudio Imbrenda     return NULL;
29903fd80bSClaudio Imbrenda }
30903fd80bSClaudio Imbrenda 
kvm_s390_stattrib_instance_init(Object * obj)31903fd80bSClaudio Imbrenda static void kvm_s390_stattrib_instance_init(Object *obj)
32903fd80bSClaudio Imbrenda {
33903fd80bSClaudio Imbrenda     KVMS390StAttribState *sas = KVM_S390_STATTRIB(obj);
34903fd80bSClaudio Imbrenda 
35903fd80bSClaudio Imbrenda     sas->still_dirty = 0;
36903fd80bSClaudio Imbrenda }
37903fd80bSClaudio Imbrenda 
kvm_s390_stattrib_read_helper(S390StAttribState * sa,uint64_t * start_gfn,uint32_t count,uint8_t * values,uint32_t flags)38903fd80bSClaudio Imbrenda static int kvm_s390_stattrib_read_helper(S390StAttribState *sa,
39903fd80bSClaudio Imbrenda                                          uint64_t *start_gfn,
40903fd80bSClaudio Imbrenda                                          uint32_t count,
41903fd80bSClaudio Imbrenda                                          uint8_t *values,
42903fd80bSClaudio Imbrenda                                          uint32_t flags)
43903fd80bSClaudio Imbrenda {
44903fd80bSClaudio Imbrenda     KVMS390StAttribState *sas = KVM_S390_STATTRIB(sa);
45903fd80bSClaudio Imbrenda     int r;
46903fd80bSClaudio Imbrenda     struct kvm_s390_cmma_log clog = {
47903fd80bSClaudio Imbrenda         .values = (uint64_t)values,
48903fd80bSClaudio Imbrenda         .start_gfn = *start_gfn,
49903fd80bSClaudio Imbrenda         .count = count,
50903fd80bSClaudio Imbrenda         .flags = flags,
51903fd80bSClaudio Imbrenda     };
52903fd80bSClaudio Imbrenda 
53903fd80bSClaudio Imbrenda     r = kvm_vm_ioctl(kvm_state, KVM_S390_GET_CMMA_BITS, &clog);
54903fd80bSClaudio Imbrenda     if (r < 0) {
55903fd80bSClaudio Imbrenda         error_report("KVM_S390_GET_CMMA_BITS failed: %s", strerror(-r));
56903fd80bSClaudio Imbrenda         return r;
57903fd80bSClaudio Imbrenda     }
58903fd80bSClaudio Imbrenda 
59903fd80bSClaudio Imbrenda     *start_gfn = clog.start_gfn;
60903fd80bSClaudio Imbrenda     sas->still_dirty = clog.remaining;
61903fd80bSClaudio Imbrenda     return clog.count;
62903fd80bSClaudio Imbrenda }
63903fd80bSClaudio Imbrenda 
kvm_s390_stattrib_get_stattr(S390StAttribState * sa,uint64_t * start_gfn,uint32_t count,uint8_t * values)64903fd80bSClaudio Imbrenda static int kvm_s390_stattrib_get_stattr(S390StAttribState *sa,
65903fd80bSClaudio Imbrenda                                         uint64_t *start_gfn,
66903fd80bSClaudio Imbrenda                                         uint32_t count,
67903fd80bSClaudio Imbrenda                                         uint8_t *values)
68903fd80bSClaudio Imbrenda {
69903fd80bSClaudio Imbrenda     return kvm_s390_stattrib_read_helper(sa, start_gfn, count, values, 0);
70903fd80bSClaudio Imbrenda }
71903fd80bSClaudio Imbrenda 
kvm_s390_stattrib_peek_stattr(S390StAttribState * sa,uint64_t start_gfn,uint32_t count,uint8_t * values)72903fd80bSClaudio Imbrenda static int kvm_s390_stattrib_peek_stattr(S390StAttribState *sa,
73903fd80bSClaudio Imbrenda                                          uint64_t start_gfn,
74903fd80bSClaudio Imbrenda                                          uint32_t count,
75903fd80bSClaudio Imbrenda                                          uint8_t *values)
76903fd80bSClaudio Imbrenda {
77903fd80bSClaudio Imbrenda     return kvm_s390_stattrib_read_helper(sa, &start_gfn, count, values,
78903fd80bSClaudio Imbrenda                                          KVM_S390_CMMA_PEEK);
79903fd80bSClaudio Imbrenda }
80903fd80bSClaudio Imbrenda 
kvm_s390_stattrib_set_stattr(S390StAttribState * sa,uint64_t start_gfn,uint32_t count,uint8_t * values)81903fd80bSClaudio Imbrenda static int kvm_s390_stattrib_set_stattr(S390StAttribState *sa,
82903fd80bSClaudio Imbrenda                                         uint64_t start_gfn,
83903fd80bSClaudio Imbrenda                                         uint32_t count,
84903fd80bSClaudio Imbrenda                                         uint8_t *values)
85903fd80bSClaudio Imbrenda {
86903fd80bSClaudio Imbrenda     KVMS390StAttribState *sas = KVM_S390_STATTRIB(sa);
87903fd80bSClaudio Imbrenda     MachineState *machine = MACHINE(qdev_get_machine());
885c30ef93SChristian Borntraeger     unsigned long max = machine->ram_size / TARGET_PAGE_SIZE;
89903fd80bSClaudio Imbrenda 
90903fd80bSClaudio Imbrenda     if (start_gfn + count > max) {
91903fd80bSClaudio Imbrenda         error_report("Out of memory bounds when setting storage attributes");
92903fd80bSClaudio Imbrenda         return -1;
93903fd80bSClaudio Imbrenda     }
94903fd80bSClaudio Imbrenda     if (!sas->incoming_buffer) {
95903fd80bSClaudio Imbrenda         sas->incoming_buffer = g_malloc0(max);
96903fd80bSClaudio Imbrenda     }
97903fd80bSClaudio Imbrenda 
98903fd80bSClaudio Imbrenda     memcpy(sas->incoming_buffer + start_gfn, values, count);
99903fd80bSClaudio Imbrenda 
100903fd80bSClaudio Imbrenda     return 0;
101903fd80bSClaudio Imbrenda }
102903fd80bSClaudio Imbrenda 
kvm_s390_stattrib_synchronize(S390StAttribState * sa)103903fd80bSClaudio Imbrenda static void kvm_s390_stattrib_synchronize(S390StAttribState *sa)
104903fd80bSClaudio Imbrenda {
105903fd80bSClaudio Imbrenda     KVMS390StAttribState *sas = KVM_S390_STATTRIB(sa);
106903fd80bSClaudio Imbrenda     MachineState *machine = MACHINE(qdev_get_machine());
1075c30ef93SChristian Borntraeger     unsigned long max = machine->ram_size / TARGET_PAGE_SIZE;
10817f45666SClaudio Imbrenda     /* We do not need to reach the maximum buffer size allowed */
10917f45666SClaudio Imbrenda     unsigned long cx, len = KVM_S390_SKEYS_MAX / 2;
110903fd80bSClaudio Imbrenda     int r;
111903fd80bSClaudio Imbrenda     struct kvm_s390_cmma_log clog = {
112903fd80bSClaudio Imbrenda         .flags = 0,
113903fd80bSClaudio Imbrenda         .mask = ~0ULL,
114903fd80bSClaudio Imbrenda     };
115903fd80bSClaudio Imbrenda 
116903fd80bSClaudio Imbrenda     if (sas->incoming_buffer) {
117903fd80bSClaudio Imbrenda         for (cx = 0; cx + len <= max; cx += len) {
118903fd80bSClaudio Imbrenda             clog.start_gfn = cx;
119903fd80bSClaudio Imbrenda             clog.count = len;
12046fa8933SClaudio Imbrenda             clog.values = (uint64_t)(sas->incoming_buffer + cx);
121903fd80bSClaudio Imbrenda             r = kvm_vm_ioctl(kvm_state, KVM_S390_SET_CMMA_BITS, &clog);
122903fd80bSClaudio Imbrenda             if (r) {
123903fd80bSClaudio Imbrenda                 error_report("KVM_S390_SET_CMMA_BITS failed: %s", strerror(-r));
124903fd80bSClaudio Imbrenda                 return;
125903fd80bSClaudio Imbrenda             }
126903fd80bSClaudio Imbrenda         }
127903fd80bSClaudio Imbrenda         if (cx < max) {
128903fd80bSClaudio Imbrenda             clog.start_gfn = cx;
129903fd80bSClaudio Imbrenda             clog.count = max - cx;
13046fa8933SClaudio Imbrenda             clog.values = (uint64_t)(sas->incoming_buffer + cx);
131903fd80bSClaudio Imbrenda             r = kvm_vm_ioctl(kvm_state, KVM_S390_SET_CMMA_BITS, &clog);
132903fd80bSClaudio Imbrenda             if (r) {
133903fd80bSClaudio Imbrenda                 error_report("KVM_S390_SET_CMMA_BITS failed: %s", strerror(-r));
134903fd80bSClaudio Imbrenda             }
135903fd80bSClaudio Imbrenda         }
136903fd80bSClaudio Imbrenda         g_free(sas->incoming_buffer);
137903fd80bSClaudio Imbrenda         sas->incoming_buffer = NULL;
138903fd80bSClaudio Imbrenda     }
139903fd80bSClaudio Imbrenda }
140903fd80bSClaudio Imbrenda 
kvm_s390_stattrib_set_migrationmode(S390StAttribState * sa,bool val,Error ** errp)141*e86f2434SCédric Le Goater static int kvm_s390_stattrib_set_migrationmode(S390StAttribState *sa, bool val,
142*e86f2434SCédric Le Goater                                                Error **errp)
143903fd80bSClaudio Imbrenda {
144903fd80bSClaudio Imbrenda     struct kvm_device_attr attr = {
145903fd80bSClaudio Imbrenda         .group = KVM_S390_VM_MIGRATION,
146903fd80bSClaudio Imbrenda         .attr = val,
147903fd80bSClaudio Imbrenda         .addr = 0,
148903fd80bSClaudio Imbrenda     };
149*e86f2434SCédric Le Goater     int r;
150*e86f2434SCédric Le Goater 
151*e86f2434SCédric Le Goater     r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
152*e86f2434SCédric Le Goater     if (r) {
153*e86f2434SCédric Le Goater         error_setg_errno(errp, -r, "setting KVM_S390_VM_MIGRATION failed");
154*e86f2434SCédric Le Goater     }
155*e86f2434SCédric Le Goater     return r;
156903fd80bSClaudio Imbrenda }
157903fd80bSClaudio Imbrenda 
kvm_s390_stattrib_get_dirtycount(S390StAttribState * sa)158903fd80bSClaudio Imbrenda static long long kvm_s390_stattrib_get_dirtycount(S390StAttribState *sa)
159903fd80bSClaudio Imbrenda {
160903fd80bSClaudio Imbrenda     KVMS390StAttribState *sas = KVM_S390_STATTRIB(sa);
161903fd80bSClaudio Imbrenda     uint8_t val[8];
162903fd80bSClaudio Imbrenda 
163903fd80bSClaudio Imbrenda     kvm_s390_stattrib_peek_stattr(sa, 0, 1, val);
164903fd80bSClaudio Imbrenda     return sas->still_dirty;
165903fd80bSClaudio Imbrenda }
166903fd80bSClaudio Imbrenda 
kvm_s390_stattrib_get_active(S390StAttribState * sa)167903fd80bSClaudio Imbrenda static int kvm_s390_stattrib_get_active(S390StAttribState *sa)
168903fd80bSClaudio Imbrenda {
169903fd80bSClaudio Imbrenda     return kvm_s390_cmma_active() && sa->migration_enabled;
170903fd80bSClaudio Imbrenda }
171903fd80bSClaudio Imbrenda 
kvm_s390_stattrib_class_init(ObjectClass * oc,void * data)172903fd80bSClaudio Imbrenda static void kvm_s390_stattrib_class_init(ObjectClass *oc, void *data)
173903fd80bSClaudio Imbrenda {
174903fd80bSClaudio Imbrenda     S390StAttribClass *sac = S390_STATTRIB_CLASS(oc);
1753ea6d20eSThomas Huth     DeviceClass *dc = DEVICE_CLASS(oc);
176903fd80bSClaudio Imbrenda 
177903fd80bSClaudio Imbrenda     sac->get_stattr = kvm_s390_stattrib_get_stattr;
178903fd80bSClaudio Imbrenda     sac->peek_stattr = kvm_s390_stattrib_peek_stattr;
179903fd80bSClaudio Imbrenda     sac->set_stattr = kvm_s390_stattrib_set_stattr;
180903fd80bSClaudio Imbrenda     sac->set_migrationmode = kvm_s390_stattrib_set_migrationmode;
181903fd80bSClaudio Imbrenda     sac->get_dirtycount = kvm_s390_stattrib_get_dirtycount;
182903fd80bSClaudio Imbrenda     sac->synchronize = kvm_s390_stattrib_synchronize;
183903fd80bSClaudio Imbrenda     sac->get_active = kvm_s390_stattrib_get_active;
1843ea6d20eSThomas Huth 
1853ea6d20eSThomas Huth     /* Reason: Can only be instantiated one time (internally) */
1863ea6d20eSThomas Huth     dc->user_creatable = false;
187903fd80bSClaudio Imbrenda }
188903fd80bSClaudio Imbrenda 
189903fd80bSClaudio Imbrenda static const TypeInfo kvm_s390_stattrib_info = {
190903fd80bSClaudio Imbrenda     .name          = TYPE_KVM_S390_STATTRIB,
191903fd80bSClaudio Imbrenda     .parent        = TYPE_S390_STATTRIB,
192903fd80bSClaudio Imbrenda     .instance_init = kvm_s390_stattrib_instance_init,
193903fd80bSClaudio Imbrenda     .instance_size = sizeof(KVMS390StAttribState),
194903fd80bSClaudio Imbrenda     .class_init    = kvm_s390_stattrib_class_init,
195903fd80bSClaudio Imbrenda     .class_size    = sizeof(S390StAttribClass),
196903fd80bSClaudio Imbrenda };
197903fd80bSClaudio Imbrenda 
kvm_s390_stattrib_register_types(void)198903fd80bSClaudio Imbrenda static void kvm_s390_stattrib_register_types(void)
199903fd80bSClaudio Imbrenda {
200903fd80bSClaudio Imbrenda     type_register_static(&kvm_s390_stattrib_info);
201903fd80bSClaudio Imbrenda }
202903fd80bSClaudio Imbrenda 
203903fd80bSClaudio Imbrenda type_init(kvm_s390_stattrib_register_types)
204