xref: /qemu/include/hw/s390x/storage-keys.h (revision 55f5bf716a65f67663d0769bcb8c017764b3e53a)
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 #ifndef S390_STORAGE_KEYS_H
13 #define S390_STORAGE_KEYS_H
14 
15 #include "hw/qdev-core.h"
16 #include "monitor/monitor.h"
17 #include "qom/object.h"
18 
19 #define TYPE_S390_SKEYS "s390-skeys"
20 OBJECT_DECLARE_TYPE(S390SKeysState, S390SKeysClass, S390_SKEYS)
21 
22 struct S390SKeysState {
23     DeviceState parent_obj;
24 };
25 
26 
27 struct S390SKeysClass {
28     DeviceClass parent_class;
29 
30     /**
31      * @skeys_are_enabled:
32      *
33      * Check whether storage keys are enabled. If not enabled, they were not
34      * enabled lazily either by the guest via a storage key instruction or
35      * by the host during migration.
36      *
37      * If disabled, everything not explicitly triggered by the guest,
38      * such as outgoing migration or dirty/change tracking, should not touch
39      * storage keys and should not lazily enable it.
40      *
41      * @ks: the #S390SKeysState
42      *
43      * Returns false if not enabled and true if enabled.
44      */
45     bool (*skeys_are_enabled)(S390SKeysState *ks);
46 
47     /**
48      * @enable_skeys:
49      *
50      * Lazily enable storage keys. If this function is not implemented,
51      * setting a storage key will lazily enable storage keys implicitly
52      * instead. TCG guests have to make sure to flush the TLB of all CPUs
53      * if storage keys were not enabled before this call.
54      *
55      * @ks: the #S390SKeysState
56      *
57      * Returns false if not enabled before this call, and true if already
58      * enabled.
59      */
60     bool (*enable_skeys)(S390SKeysState *ks);
61 
62     /**
63      * @get_skeys:
64      *
65      * Get storage keys for the given PFN range. This call will fail if
66      * storage keys have not been lazily enabled yet.
67      *
68      * Callers have to validate that a GFN is valid before this call.
69      *
70      * @ks: the #S390SKeysState
71      * @start_gfn: the start GFN to get storage keys for
72      * @count: the number of storage keys to get
73      * @keys: the byte array where storage keys will be stored to
74      *
75      * Returns 0 on success, returns an error if getting a storage key failed.
76      */
77     int (*get_skeys)(S390SKeysState *ks, uint64_t start_gfn, uint64_t count,
78                      uint8_t *keys);
79     /**
80      * @set_skeys:
81      *
82      * Set storage keys for the given PFN range. This call will fail if
83      * storage keys have not been lazily enabled yet and implicit
84      * enablement is not supported.
85      *
86      * Callers have to validate that a GFN is valid before this call.
87      *
88      * @ks: the #S390SKeysState
89      * @start_gfn: the start GFN to set storage keys for
90      * @count: the number of storage keys to set
91      * @keys: the byte array where storage keys will be read from
92      *
93      * Returns 0 on success, returns an error if setting a storage key failed.
94      */
95     int (*set_skeys)(S390SKeysState *ks, uint64_t start_gfn, uint64_t count,
96                      uint8_t *keys);
97 };
98 
99 #define TYPE_KVM_S390_SKEYS "s390-skeys-kvm"
100 #define TYPE_QEMU_S390_SKEYS "s390-skeys-qemu"
101 typedef struct QEMUS390SKeysState QEMUS390SKeysState;
102 DECLARE_INSTANCE_CHECKER(QEMUS390SKeysState, QEMU_S390_SKEYS,
103                          TYPE_QEMU_S390_SKEYS)
104 
105 struct QEMUS390SKeysState {
106     S390SKeysState parent_obj;
107     uint8_t *keydata;
108     uint32_t key_count;
109 };
110 
111 void s390_skeys_init(void);
112 /**
113  * @s390_skeys_get: See S390SKeysClass::get_skeys()
114  */
115 int s390_skeys_get(S390SKeysState *ks, uint64_t start_gfn,
116                    uint64_t count, uint8_t *keys);
117 /**
118  * @s390_skeys_set: See S390SKeysClass::set_skeys()
119  */
120 int s390_skeys_set(S390SKeysState *ks, uint64_t start_gfn,
121                    uint64_t count, uint8_t *keys);
122 
123 S390SKeysState *s390_get_skeys_device(void);
124 
125 void s390_qmp_dump_skeys(const char *filename, Error **errp);
126 void hmp_dump_skeys(Monitor *mon, const QDict *qdict);
127 void hmp_info_skeys(Monitor *mon, const QDict *qdict);
128 
129 #define TYPE_DUMP_SKEYS_INTERFACE "dump-skeys-interface"
130 
131 typedef struct DumpSKeysInterface DumpSKeysInterface;
132 DECLARE_CLASS_CHECKERS(DumpSKeysInterface, DUMP_SKEYS_INTERFACE,
133                        TYPE_DUMP_SKEYS_INTERFACE)
134 
135 struct DumpSKeysInterface {
136     InterfaceClass parent_class;
137 
138     /**
139      * @qmp_dump_skeys: Callback to dump guest's storage keys to @filename.
140      */
141     void (*qmp_dump_skeys)(const char *filename, Error **errp);
142 };
143 
144 #endif /* S390_STORAGE_KEYS_H */
145