1 // SPDX-License-Identifier: GPL-2.0
3 * pkey base: debug feature, pkey handler registry
5 * Copyright IBM Corp. 2024
8 #define KMSG_COMPONENT "pkey"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/cpufeature.h>
12 #include <linux/init.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/rculist.h>
17 #include "pkey_base.h"
19 MODULE_LICENSE("GPL");
20 MODULE_AUTHOR("IBM Corporation");
21 MODULE_DESCRIPTION("s390 protected key base and api");
26 debug_info_t
*pkey_dbf_info
;
27 EXPORT_SYMBOL(pkey_dbf_info
);
30 * pkey handler registry
33 static DEFINE_SPINLOCK(handler_list_write_lock
);
34 static LIST_HEAD(handler_list
);
36 int pkey_handler_register(struct pkey_handler
*handler
)
38 const struct pkey_handler
*h
;
41 !handler
->is_supported_key
||
42 !handler
->is_supported_keytype
)
45 if (!try_module_get(handler
->module
))
48 spin_lock(&handler_list_write_lock
);
51 list_for_each_entry_rcu(h
, &handler_list
, list
) {
54 spin_unlock(&handler_list_write_lock
);
55 module_put(handler
->module
);
61 list_add_rcu(&handler
->list
, &handler_list
);
62 spin_unlock(&handler_list_write_lock
);
65 module_put(handler
->module
);
67 PKEY_DBF_INFO("%s pkey handler '%s' registered\n", __func__
,
68 handler
->name
?: "<no name>");
72 EXPORT_SYMBOL(pkey_handler_register
);
74 int pkey_handler_unregister(struct pkey_handler
*handler
)
76 spin_lock(&handler_list_write_lock
);
77 list_del_rcu(&handler
->list
);
78 INIT_LIST_HEAD_RCU(&handler
->list
);
79 spin_unlock(&handler_list_write_lock
);
82 PKEY_DBF_INFO("%s pkey handler '%s' unregistered\n", __func__
,
83 handler
->name
?: "<no name>");
87 EXPORT_SYMBOL(pkey_handler_unregister
);
90 * Handler invocation functions.
93 const struct pkey_handler
*pkey_handler_get_keybased(const u8
*key
, u32 keylen
)
95 const struct pkey_handler
*h
;
98 list_for_each_entry_rcu(h
, &handler_list
, list
) {
99 if (!try_module_get(h
->module
))
101 if (h
->is_supported_key(key
, keylen
)) {
105 module_put(h
->module
);
111 EXPORT_SYMBOL(pkey_handler_get_keybased
);
113 const struct pkey_handler
*pkey_handler_get_keytypebased(enum pkey_key_type kt
)
115 const struct pkey_handler
*h
;
118 list_for_each_entry_rcu(h
, &handler_list
, list
) {
119 if (!try_module_get(h
->module
))
121 if (h
->is_supported_keytype(kt
)) {
125 module_put(h
->module
);
131 EXPORT_SYMBOL(pkey_handler_get_keytypebased
);
133 void pkey_handler_put(const struct pkey_handler
*handler
)
135 const struct pkey_handler
*h
;
141 list_for_each_entry_rcu(h
, &handler_list
, list
) {
143 module_put(h
->module
);
149 EXPORT_SYMBOL(pkey_handler_put
);
151 int pkey_handler_key_to_protkey(const struct pkey_apqn
*apqns
, size_t nr_apqns
,
152 const u8
*key
, u32 keylen
,
153 u8
*protkey
, u32
*protkeylen
, u32
*protkeytype
)
155 const struct pkey_handler
*h
;
158 h
= pkey_handler_get_keybased(key
, keylen
);
159 if (h
&& h
->key_to_protkey
) {
160 rc
= h
->key_to_protkey(apqns
, nr_apqns
, key
, keylen
,
168 EXPORT_SYMBOL(pkey_handler_key_to_protkey
);
171 * This handler invocation is special as there may be more than
172 * one handler providing support for the very same key (type).
173 * And the handler may not respond true on is_supported_key(),
174 * so simple try and check return value here.
176 int pkey_handler_slowpath_key_to_protkey(const struct pkey_apqn
*apqns
,
178 const u8
*key
, u32 keylen
,
179 u8
*protkey
, u32
*protkeylen
,
182 const struct pkey_handler
*h
, *htmp
[10];
183 int i
, n
= 0, rc
= -ENODEV
;
186 list_for_each_entry_rcu(h
, &handler_list
, list
) {
187 if (!try_module_get(h
->module
))
189 if (h
->slowpath_key_to_protkey
&& n
< ARRAY_SIZE(htmp
))
192 module_put(h
->module
);
196 for (i
= 0; i
< n
; i
++) {
199 rc
= h
->slowpath_key_to_protkey(apqns
, nr_apqns
,
203 module_put(h
->module
);
208 EXPORT_SYMBOL(pkey_handler_slowpath_key_to_protkey
);
210 int pkey_handler_gen_key(const struct pkey_apqn
*apqns
, size_t nr_apqns
,
211 u32 keytype
, u32 keysubtype
,
212 u32 keybitsize
, u32 flags
,
213 u8
*keybuf
, u32
*keybuflen
, u32
*keyinfo
)
215 const struct pkey_handler
*h
;
218 h
= pkey_handler_get_keytypebased(keysubtype
);
219 if (h
&& h
->gen_key
) {
220 rc
= h
->gen_key(apqns
, nr_apqns
, keytype
, keysubtype
,
222 keybuf
, keybuflen
, keyinfo
);
228 EXPORT_SYMBOL(pkey_handler_gen_key
);
230 int pkey_handler_clr_to_key(const struct pkey_apqn
*apqns
, size_t nr_apqns
,
231 u32 keytype
, u32 keysubtype
,
232 u32 keybitsize
, u32 flags
,
233 const u8
*clrkey
, u32 clrkeylen
,
234 u8
*keybuf
, u32
*keybuflen
, u32
*keyinfo
)
236 const struct pkey_handler
*h
;
239 h
= pkey_handler_get_keytypebased(keysubtype
);
240 if (h
&& h
->clr_to_key
) {
241 rc
= h
->clr_to_key(apqns
, nr_apqns
, keytype
, keysubtype
,
242 keybitsize
, flags
, clrkey
, clrkeylen
,
243 keybuf
, keybuflen
, keyinfo
);
249 EXPORT_SYMBOL(pkey_handler_clr_to_key
);
251 int pkey_handler_verify_key(const u8
*key
, u32 keylen
,
253 u32
*keytype
, u32
*keybitsize
, u32
*flags
)
255 const struct pkey_handler
*h
;
258 h
= pkey_handler_get_keybased(key
, keylen
);
259 if (h
&& h
->verify_key
) {
260 rc
= h
->verify_key(key
, keylen
, card
, dom
,
261 keytype
, keybitsize
, flags
);
267 EXPORT_SYMBOL(pkey_handler_verify_key
);
269 int pkey_handler_apqns_for_key(const u8
*key
, u32 keylen
, u32 flags
,
270 struct pkey_apqn
*apqns
, size_t *nr_apqns
)
272 const struct pkey_handler
*h
;
275 h
= pkey_handler_get_keybased(key
, keylen
);
276 if (h
&& h
->apqns_for_key
)
277 rc
= h
->apqns_for_key(key
, keylen
, flags
, apqns
, nr_apqns
);
282 EXPORT_SYMBOL(pkey_handler_apqns_for_key
);
284 int pkey_handler_apqns_for_keytype(enum pkey_key_type keysubtype
,
285 u8 cur_mkvp
[32], u8 alt_mkvp
[32], u32 flags
,
286 struct pkey_apqn
*apqns
, size_t *nr_apqns
)
288 const struct pkey_handler
*h
;
291 h
= pkey_handler_get_keytypebased(keysubtype
);
292 if (h
&& h
->apqns_for_keytype
) {
293 rc
= h
->apqns_for_keytype(keysubtype
,
294 cur_mkvp
, alt_mkvp
, flags
,
301 EXPORT_SYMBOL(pkey_handler_apqns_for_keytype
);
303 void pkey_handler_request_modules(void)
305 #ifdef CONFIG_MODULES
306 static const char * const pkey_handler_modules
[] = {
307 #if IS_MODULE(CONFIG_PKEY_CCA)
310 #if IS_MODULE(CONFIG_PKEY_EP11)
313 #if IS_MODULE(CONFIG_PKEY_PCKMO)
316 #if IS_MODULE(CONFIG_PKEY_UV)
322 for (i
= 0; i
< ARRAY_SIZE(pkey_handler_modules
); i
++) {
323 const struct pkey_handler
*h
;
327 list_for_each_entry_rcu(h
, &handler_list
, list
) {
329 !strcmp(h
->module
->name
, pkey_handler_modules
[i
])) {
336 pr_debug("request_module(%s)\n", pkey_handler_modules
[i
]);
337 request_module(pkey_handler_modules
[i
]);
342 EXPORT_SYMBOL(pkey_handler_request_modules
);
347 static int __init
pkey_init(void)
351 /* init debug feature */
352 pkey_dbf_info
= debug_register("pkey", 1, 1, 5 * sizeof(long));
353 debug_register_view(pkey_dbf_info
, &debug_sprintf_view
);
354 debug_set_level(pkey_dbf_info
, 4);
356 /* the handler registry does not need any init */
358 rc
= pkey_api_init();
360 debug_unregister(pkey_dbf_info
);
368 static void __exit
pkey_exit(void)
373 module_cpu_feature_match(S390_CPU_FEATURE_MSA
, pkey_init
);
374 module_exit(pkey_exit
);