xref: /openbmc/qemu/hw/s390x/s390-skeys-kvm.c (revision b45c03f5)
1 /*
2  * s390 storage key device
3  *
4  * Copyright 2015 IBM Corp.
5  * Author(s): Jason J. Herne <jjherne@linux.vnet.ibm.com>
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2 or (at
8  * your option) any later version. See the COPYING file in the top-level
9  * directory.
10  */
11 
12 #include "hw/s390x/storage-keys.h"
13 #include "sysemu/kvm.h"
14 #include "qemu/error-report.h"
15 
16 static int kvm_s390_skeys_enabled(S390SKeysState *ss)
17 {
18     S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
19     uint8_t single_key;
20     int r;
21 
22     r = skeyclass->get_skeys(ss, 0, 1, &single_key);
23     if (r != 0 && r != KVM_S390_GET_SKEYS_NONE) {
24         error_report("S390_GET_KEYS error %d\n", r);
25     }
26     return (r == 0);
27 }
28 
29 static int kvm_s390_skeys_get(S390SKeysState *ss, uint64_t start_gfn,
30                               uint64_t count, uint8_t *keys)
31 {
32     struct kvm_s390_skeys args = {
33         .start_gfn = start_gfn,
34         .count = count,
35         .skeydata_addr = (__u64)keys
36     };
37 
38     return kvm_vm_ioctl(kvm_state, KVM_S390_GET_SKEYS, &args);
39 }
40 
41 static int kvm_s390_skeys_set(S390SKeysState *ss, uint64_t start_gfn,
42                               uint64_t count, uint8_t *keys)
43 {
44     struct kvm_s390_skeys args = {
45         .start_gfn = start_gfn,
46         .count = count,
47         .skeydata_addr = (__u64)keys
48     };
49 
50     return kvm_vm_ioctl(kvm_state, KVM_S390_SET_SKEYS, &args);
51 }
52 
53 static void kvm_s390_skeys_class_init(ObjectClass *oc, void *data)
54 {
55     S390SKeysClass *skeyclass = S390_SKEYS_CLASS(oc);
56 
57     skeyclass->skeys_enabled = kvm_s390_skeys_enabled;
58     skeyclass->get_skeys = kvm_s390_skeys_get;
59     skeyclass->set_skeys = kvm_s390_skeys_set;
60 }
61 
62 static const TypeInfo kvm_s390_skeys_info = {
63     .name          = TYPE_KVM_S390_SKEYS,
64     .parent        = TYPE_S390_SKEYS,
65     .instance_size = sizeof(S390SKeysState),
66     .class_init    = kvm_s390_skeys_class_init,
67     .class_size    = sizeof(S390SKeysClass),
68 };
69 
70 static void kvm_s390_skeys_register_types(void)
71 {
72     type_register_static(&kvm_s390_skeys_info);
73 }
74 
75 type_init(kvm_s390_skeys_register_types)
76