xref: /linux/drivers/virt/coco/sev-guest/sev-guest.c (revision 3526d7462355c4df269d4a87eed53dbd48a1df02)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AMD Secure Encrypted Virtualization (SEV) guest driver interface
4  *
5  * Copyright (C) 2021-2024 Advanced Micro Devices, Inc.
6  *
7  * Author: Brijesh Singh <brijesh.singh@amd.com>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/mutex.h>
14 #include <linux/io.h>
15 #include <linux/platform_device.h>
16 #include <linux/miscdevice.h>
17 #include <linux/set_memory.h>
18 #include <linux/fs.h>
19 #include <linux/tsm.h>
20 #include <crypto/gcm.h>
21 #include <linux/psp-sev.h>
22 #include <linux/sockptr.h>
23 #include <linux/cleanup.h>
24 #include <linux/uuid.h>
25 #include <linux/configfs.h>
26 #include <linux/mm.h>
27 #include <uapi/linux/sev-guest.h>
28 #include <uapi/linux/psp-sev.h>
29 
30 #include <asm/svm.h>
31 #include <asm/sev.h>
32 
33 #define DEVICE_NAME	"sev-guest"
34 
35 #define SVSM_MAX_RETRIES		3
36 
37 struct snp_guest_dev {
38 	struct device *dev;
39 	struct miscdevice misc;
40 
41 	struct snp_msg_desc *msg_desc;
42 };
43 
44 /*
45  * The VMPCK ID represents the key used by the SNP guest to communicate with the
46  * SEV firmware in the AMD Secure Processor (ASP, aka PSP). By default, the key
47  * used will be the key associated with the VMPL at which the guest is running.
48  * Should the default key be wiped (see snp_disable_vmpck()), this parameter
49  * allows for using one of the remaining VMPCKs.
50  */
51 static int vmpck_id = -1;
52 module_param(vmpck_id, int, 0444);
53 MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP.");
54 
to_snp_dev(struct file * file)55 static inline struct snp_guest_dev *to_snp_dev(struct file *file)
56 {
57 	struct miscdevice *dev = file->private_data;
58 
59 	return container_of(dev, struct snp_guest_dev, misc);
60 }
61 
62 struct snp_req_resp {
63 	sockptr_t req_data;
64 	sockptr_t resp_data;
65 };
66 
get_report(struct snp_guest_dev * snp_dev,struct snp_guest_request_ioctl * arg)67 static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg)
68 {
69 	struct snp_report_req *report_req __free(kfree) = NULL;
70 	struct snp_msg_desc *mdesc = snp_dev->msg_desc;
71 	struct snp_report_resp *report_resp;
72 	struct snp_guest_req req = {};
73 	int rc, resp_len;
74 
75 	if (!arg->req_data || !arg->resp_data)
76 		return -EINVAL;
77 
78 	report_req = kzalloc_obj(*report_req, GFP_KERNEL_ACCOUNT);
79 	if (!report_req)
80 		return -ENOMEM;
81 
82 	if (copy_from_user(report_req, (void __user *)arg->req_data, sizeof(*report_req)))
83 		return -EFAULT;
84 
85 	/*
86 	 * The intermediate response buffer is used while decrypting the
87 	 * response payload. Make sure that it has enough space to cover the
88 	 * authtag.
89 	 */
90 	resp_len = sizeof(report_resp->data) + mdesc->ctx->authsize;
91 	report_resp = kzalloc(resp_len, GFP_KERNEL_ACCOUNT);
92 	if (!report_resp)
93 		return -ENOMEM;
94 
95 	req.msg_version = arg->msg_version;
96 	req.msg_type = SNP_MSG_REPORT_REQ;
97 	req.vmpck_id = mdesc->vmpck_id;
98 	req.req_buf = report_req;
99 	req.req_sz = sizeof(*report_req);
100 	req.resp_buf = report_resp->data;
101 	req.resp_sz = resp_len;
102 	req.exit_code = SVM_VMGEXIT_GUEST_REQUEST;
103 
104 	rc = snp_send_guest_request(mdesc, &req);
105 	arg->exitinfo2 = req.exitinfo2;
106 	if (rc)
107 		goto e_free;
108 
109 	if (copy_to_user((void __user *)arg->resp_data, report_resp, sizeof(*report_resp)))
110 		rc = -EFAULT;
111 
112 e_free:
113 	kfree(report_resp);
114 	return rc;
115 }
116 
get_derived_key(struct snp_guest_dev * snp_dev,struct snp_guest_request_ioctl * arg)117 static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg)
118 {
119 	struct snp_derived_key_resp *derived_key_resp __free(kfree) = NULL;
120 	struct snp_derived_key_req *derived_key_req __free(kfree) = NULL;
121 	struct snp_msg_desc *mdesc = snp_dev->msg_desc;
122 	struct snp_guest_req req = {};
123 	int rc, resp_len;
124 
125 	if (!arg->req_data || !arg->resp_data)
126 		return -EINVAL;
127 
128 	/*
129 	 * The intermediate response buffer is used while decrypting the
130 	 * response payload. Make sure that it has enough space to cover the
131 	 * authtag.
132 	 */
133 	resp_len = sizeof(derived_key_resp->data) + mdesc->ctx->authsize;
134 	derived_key_resp = kzalloc(resp_len, GFP_KERNEL_ACCOUNT);
135 	if (!derived_key_resp)
136 		return -ENOMEM;
137 
138 	derived_key_req = kzalloc_obj(*derived_key_req, GFP_KERNEL_ACCOUNT);
139 	if (!derived_key_req)
140 		return -ENOMEM;
141 
142 	if (copy_from_user(derived_key_req, (void __user *)arg->req_data,
143 			   sizeof(*derived_key_req)))
144 		return -EFAULT;
145 
146 	req.msg_version = arg->msg_version;
147 	req.msg_type = SNP_MSG_KEY_REQ;
148 	req.vmpck_id = mdesc->vmpck_id;
149 	req.req_buf = derived_key_req;
150 	req.req_sz = sizeof(*derived_key_req);
151 	req.resp_buf = derived_key_resp;
152 	req.resp_sz = resp_len;
153 	req.exit_code = SVM_VMGEXIT_GUEST_REQUEST;
154 
155 	rc = snp_send_guest_request(mdesc, &req);
156 	arg->exitinfo2 = req.exitinfo2;
157 	if (!rc) {
158 		if (copy_to_user((void __user *)arg->resp_data, derived_key_resp,
159 				 sizeof(derived_key_resp->data)))
160 			rc = -EFAULT;
161 	}
162 
163 	/* The response buffer contains the sensitive data, explicitly clear it. */
164 	memzero_explicit(derived_key_resp, sizeof(*derived_key_resp));
165 
166 	return rc;
167 }
168 
get_ext_report(struct snp_guest_dev * snp_dev,struct snp_guest_request_ioctl * arg,struct snp_req_resp * io)169 static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_ioctl *arg,
170 			  struct snp_req_resp *io)
171 
172 {
173 	struct snp_ext_report_req *report_req __free(kfree) = NULL;
174 	struct snp_msg_desc *mdesc = snp_dev->msg_desc;
175 	struct snp_report_resp *report_resp;
176 	struct snp_guest_req req = {};
177 	int ret, npages = 0, resp_len;
178 	sockptr_t certs_address;
179 	u64 pfn;
180 
181 	if (sockptr_is_null(io->req_data) || sockptr_is_null(io->resp_data))
182 		return -EINVAL;
183 
184 	report_req = kzalloc_obj(*report_req, GFP_KERNEL_ACCOUNT);
185 	if (!report_req)
186 		return -ENOMEM;
187 
188 	if (copy_from_sockptr(report_req, io->req_data, sizeof(*report_req)))
189 		return -EFAULT;
190 
191 	/* caller does not want certificate data */
192 	if (!report_req->certs_len || !report_req->certs_address)
193 		goto cmd;
194 
195 	if (report_req->certs_len > SEV_FW_BLOB_MAX_SIZE ||
196 	    !IS_ALIGNED(report_req->certs_len, PAGE_SIZE))
197 		return -EINVAL;
198 
199 	if (sockptr_is_kernel(io->resp_data)) {
200 		certs_address = KERNEL_SOCKPTR((void *)report_req->certs_address);
201 	} else {
202 		certs_address = USER_SOCKPTR((void __user *)report_req->certs_address);
203 		if (!access_ok(certs_address.user, report_req->certs_len))
204 			return -EFAULT;
205 	}
206 
207 	/*
208 	 * Initialize the intermediate buffer with all zeros. This buffer
209 	 * is used in the guest request message to get the certs blob from
210 	 * the host. If host does not supply any certs in it, then copy
211 	 * zeros to indicate that certificate data was not provided.
212 	 */
213 	npages = report_req->certs_len >> PAGE_SHIFT;
214 	req.certs_data = alloc_pages_exact(npages << PAGE_SHIFT,
215 					   GFP_KERNEL_ACCOUNT | __GFP_ZERO);
216 	if (!req.certs_data)
217 		return -ENOMEM;
218 
219 	pfn = PHYS_PFN(virt_to_phys(req.certs_data));
220 	ret = set_memory_decrypted((unsigned long)req.certs_data, npages);
221 	if (ret) {
222 		pr_err("failed to mark page shared, ret=%d\n", ret);
223 		snp_leak_pages(pfn, npages);
224 		return -EFAULT;
225 	}
226 
227 cmd:
228 	/*
229 	 * The intermediate response buffer is used while decrypting the
230 	 * response payload. Make sure that it has enough space to cover the
231 	 * authtag.
232 	 */
233 	resp_len = sizeof(report_resp->data) + mdesc->ctx->authsize;
234 	report_resp = kzalloc(resp_len, GFP_KERNEL_ACCOUNT);
235 	if (!report_resp) {
236 		ret = -ENOMEM;
237 		goto e_free_data;
238 	}
239 
240 	req.input.data_npages = npages;
241 
242 	req.msg_version = arg->msg_version;
243 	req.msg_type = SNP_MSG_REPORT_REQ;
244 	req.vmpck_id = mdesc->vmpck_id;
245 	req.req_buf = &report_req->data;
246 	req.req_sz = sizeof(report_req->data);
247 	req.resp_buf = report_resp->data;
248 	req.resp_sz = resp_len;
249 	req.exit_code = SVM_VMGEXIT_EXT_GUEST_REQUEST;
250 
251 	ret = snp_send_guest_request(mdesc, &req);
252 	arg->exitinfo2 = req.exitinfo2;
253 
254 	/* If certs length is invalid then copy the returned length */
255 	if (arg->vmm_error == SNP_GUEST_VMM_ERR_INVALID_LEN) {
256 		report_req->certs_len = req.input.data_npages << PAGE_SHIFT;
257 
258 		if (copy_to_sockptr(io->req_data, report_req, sizeof(*report_req)))
259 			ret = -EFAULT;
260 	}
261 
262 	if (ret)
263 		goto e_free;
264 
265 	if (npages && copy_to_sockptr(certs_address, req.certs_data, report_req->certs_len)) {
266 		ret = -EFAULT;
267 		goto e_free;
268 	}
269 
270 	if (copy_to_sockptr(io->resp_data, report_resp, sizeof(*report_resp)))
271 		ret = -EFAULT;
272 
273 e_free:
274 	kfree(report_resp);
275 e_free_data:
276 	if (npages) {
277 		if (set_memory_encrypted((unsigned long)req.certs_data, npages)) {
278 			WARN_ONCE(ret, "failed to restore encryption mask (leak it)\n");
279 			snp_leak_pages(pfn, npages);
280 		} else {
281 			free_pages_exact(req.certs_data, npages << PAGE_SHIFT);
282 		}
283 	}
284 	return ret;
285 }
286 
snp_guest_ioctl(struct file * file,unsigned int ioctl,unsigned long arg)287 static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
288 {
289 	struct snp_guest_dev *snp_dev = to_snp_dev(file);
290 	void __user *argp = (void __user *)arg;
291 	struct snp_guest_request_ioctl input;
292 	struct snp_req_resp io;
293 	int ret = -ENOTTY;
294 
295 	if (copy_from_user(&input, argp, sizeof(input)))
296 		return -EFAULT;
297 
298 	input.exitinfo2 = 0xff;
299 
300 	/* Message version must be non-zero */
301 	if (!input.msg_version)
302 		return -EINVAL;
303 
304 	switch (ioctl) {
305 	case SNP_GET_REPORT:
306 		ret = get_report(snp_dev, &input);
307 		break;
308 	case SNP_GET_DERIVED_KEY:
309 		ret = get_derived_key(snp_dev, &input);
310 		break;
311 	case SNP_GET_EXT_REPORT:
312 		/*
313 		 * As get_ext_report() may be called from the ioctl() path and a
314 		 * kernel internal path (configfs-tsm), decorate the passed
315 		 * buffers as user pointers.
316 		 */
317 		io.req_data = USER_SOCKPTR((void __user *)input.req_data);
318 		io.resp_data = USER_SOCKPTR((void __user *)input.resp_data);
319 		ret = get_ext_report(snp_dev, &input, &io);
320 		break;
321 	default:
322 		break;
323 	}
324 
325 	if (input.exitinfo2 && copy_to_user(argp, &input, sizeof(input)))
326 		return -EFAULT;
327 
328 	return ret;
329 }
330 
331 static const struct file_operations snp_guest_fops = {
332 	.owner	= THIS_MODULE,
333 	.unlocked_ioctl = snp_guest_ioctl,
334 };
335 
336 struct snp_msg_report_resp_hdr {
337 	u32 status;
338 	u32 report_size;
339 	u8 rsvd[24];
340 };
341 
342 struct snp_msg_cert_entry {
343 	guid_t guid;
344 	u32 offset;
345 	u32 length;
346 };
347 
sev_svsm_report_new(struct tsm_report * report,void * data)348 static int sev_svsm_report_new(struct tsm_report *report, void *data)
349 {
350 	unsigned int rep_len, man_len, certs_len;
351 	struct tsm_report_desc *desc = &report->desc;
352 	struct svsm_attest_call ac = {};
353 	unsigned int retry_count;
354 	void *rep, *man, *certs;
355 	struct svsm_call call;
356 	unsigned int size;
357 	bool try_again;
358 	void *buffer;
359 	u64 call_id;
360 	int ret;
361 
362 	/*
363 	 * Allocate pages for the request:
364 	 * - Report blob (4K)
365 	 * - Manifest blob (4K)
366 	 * - Certificate blob (16K)
367 	 *
368 	 * Above addresses must be 4K aligned
369 	 */
370 	rep_len = SZ_4K;
371 	man_len = SZ_4K;
372 	certs_len = SEV_FW_BLOB_MAX_SIZE;
373 
374 	if (guid_is_null(&desc->service_guid)) {
375 		call_id = SVSM_ATTEST_CALL(SVSM_ATTEST_SERVICES);
376 	} else {
377 		export_guid(ac.service_guid, &desc->service_guid);
378 		ac.service_manifest_ver = desc->service_manifest_version;
379 
380 		call_id = SVSM_ATTEST_CALL(SVSM_ATTEST_SINGLE_SERVICE);
381 	}
382 
383 	retry_count = 0;
384 
385 retry:
386 	memset(&call, 0, sizeof(call));
387 
388 	size = rep_len + man_len + certs_len;
389 	buffer = alloc_pages_exact(size, __GFP_ZERO);
390 	if (!buffer)
391 		return -ENOMEM;
392 
393 	rep = buffer;
394 	ac.report_buf.pa = __pa(rep);
395 	ac.report_buf.len = rep_len;
396 
397 	man = rep + rep_len;
398 	ac.manifest_buf.pa = __pa(man);
399 	ac.manifest_buf.len = man_len;
400 
401 	certs = man + man_len;
402 	ac.certificates_buf.pa = __pa(certs);
403 	ac.certificates_buf.len = certs_len;
404 
405 	ac.nonce.pa = __pa(desc->inblob);
406 	ac.nonce.len = desc->inblob_len;
407 
408 	ret = snp_issue_svsm_attest_req(call_id, &call, &ac);
409 	if (ret) {
410 		free_pages_exact(buffer, size);
411 
412 		switch (call.rax_out) {
413 		case SVSM_ERR_INVALID_PARAMETER:
414 			try_again = false;
415 
416 			if (ac.report_buf.len > rep_len) {
417 				rep_len = PAGE_ALIGN(ac.report_buf.len);
418 				try_again = true;
419 			}
420 
421 			if (ac.manifest_buf.len > man_len) {
422 				man_len = PAGE_ALIGN(ac.manifest_buf.len);
423 				try_again = true;
424 			}
425 
426 			if (ac.certificates_buf.len > certs_len) {
427 				certs_len = PAGE_ALIGN(ac.certificates_buf.len);
428 				try_again = true;
429 			}
430 
431 			/* If one of the buffers wasn't large enough, retry the request */
432 			if (try_again && retry_count < SVSM_MAX_RETRIES) {
433 				retry_count++;
434 				goto retry;
435 			}
436 
437 			return -EINVAL;
438 		default:
439 			pr_err_ratelimited("SVSM attestation request failed (%d / 0x%llx)\n",
440 					   ret, call.rax_out);
441 			return -EINVAL;
442 		}
443 	}
444 
445 	/*
446 	 * Allocate all the blob memory buffers at once so that the cleanup is
447 	 * done for errors that occur after the first allocation (i.e. before
448 	 * using no_free_ptr()).
449 	 */
450 	rep_len = ac.report_buf.len;
451 	void *rbuf __free(kvfree) = kvzalloc(rep_len, GFP_KERNEL);
452 
453 	man_len = ac.manifest_buf.len;
454 	void *mbuf __free(kvfree) = kvzalloc(man_len, GFP_KERNEL);
455 
456 	certs_len = ac.certificates_buf.len;
457 	void *cbuf __free(kvfree) = certs_len ? kvzalloc(certs_len, GFP_KERNEL) : NULL;
458 
459 	if (!rbuf || !mbuf || (certs_len && !cbuf)) {
460 		free_pages_exact(buffer, size);
461 		return -ENOMEM;
462 	}
463 
464 	memcpy(rbuf, rep, rep_len);
465 	report->outblob = no_free_ptr(rbuf);
466 	report->outblob_len = rep_len;
467 
468 	memcpy(mbuf, man, man_len);
469 	report->manifestblob = no_free_ptr(mbuf);
470 	report->manifestblob_len = man_len;
471 
472 	if (certs_len) {
473 		memcpy(cbuf, certs, certs_len);
474 		report->auxblob = no_free_ptr(cbuf);
475 		report->auxblob_len = certs_len;
476 	}
477 
478 	free_pages_exact(buffer, size);
479 
480 	return 0;
481 }
482 
sev_report_new(struct tsm_report * report,void * data)483 static int sev_report_new(struct tsm_report *report, void *data)
484 {
485 	struct snp_msg_cert_entry *cert_table;
486 	struct tsm_report_desc *desc = &report->desc;
487 	struct snp_guest_dev *snp_dev = data;
488 	struct snp_msg_report_resp_hdr hdr;
489 	const u32 report_size = SZ_4K;
490 	const u32 ext_size = SEV_FW_BLOB_MAX_SIZE;
491 	u32 certs_size, i, size = report_size + ext_size;
492 	int ret;
493 
494 	if (desc->inblob_len != SNP_REPORT_USER_DATA_SIZE)
495 		return -EINVAL;
496 
497 	if (desc->service_provider) {
498 		if (strcmp(desc->service_provider, "svsm"))
499 			return -EINVAL;
500 
501 		return sev_svsm_report_new(report, data);
502 	}
503 
504 	void *buf __free(kvfree) = kvzalloc(size, GFP_KERNEL);
505 	if (!buf)
506 		return -ENOMEM;
507 
508 	cert_table = buf + report_size;
509 	struct snp_ext_report_req ext_req = {
510 		.data = { .vmpl = desc->privlevel },
511 		.certs_address = (__u64)cert_table,
512 		.certs_len = ext_size,
513 	};
514 	memcpy(&ext_req.data.user_data, desc->inblob, desc->inblob_len);
515 
516 	struct snp_guest_request_ioctl input = {
517 		.msg_version = 1,
518 		.req_data = (__u64)&ext_req,
519 		.resp_data = (__u64)buf,
520 		.exitinfo2 = 0xff,
521 	};
522 	struct snp_req_resp io = {
523 		.req_data = KERNEL_SOCKPTR(&ext_req),
524 		.resp_data = KERNEL_SOCKPTR(buf),
525 	};
526 
527 	ret = get_ext_report(snp_dev, &input, &io);
528 	if (ret)
529 		return ret;
530 
531 	memcpy(&hdr, buf, sizeof(hdr));
532 	if (hdr.status == SEV_RET_INVALID_PARAM)
533 		return -EINVAL;
534 	if (hdr.status == SEV_RET_INVALID_KEY)
535 		return -EINVAL;
536 	if (hdr.status)
537 		return -ENXIO;
538 	if ((hdr.report_size + sizeof(hdr)) > report_size)
539 		return -ENOMEM;
540 
541 	void *rbuf __free(kvfree) = kvzalloc(hdr.report_size, GFP_KERNEL);
542 	if (!rbuf)
543 		return -ENOMEM;
544 
545 	memcpy(rbuf, buf + sizeof(hdr), hdr.report_size);
546 	report->outblob = no_free_ptr(rbuf);
547 	report->outblob_len = hdr.report_size;
548 
549 	certs_size = 0;
550 	for (i = 0; i < ext_size / sizeof(struct snp_msg_cert_entry); i++) {
551 		struct snp_msg_cert_entry *ent = &cert_table[i];
552 
553 		if (guid_is_null(&ent->guid) && !ent->offset && !ent->length)
554 			break;
555 		certs_size = max(certs_size, ent->offset + ent->length);
556 	}
557 
558 	/* Suspicious that the response populated entries without populating size */
559 	if (!certs_size && i)
560 		dev_warn_ratelimited(snp_dev->dev, "certificate slots conveyed without size\n");
561 
562 	/* No certs to report */
563 	if (!certs_size)
564 		return 0;
565 
566 	/* Suspicious that the certificate blob size contract was violated
567 	 */
568 	if (certs_size > ext_size) {
569 		dev_warn_ratelimited(snp_dev->dev, "certificate data truncated\n");
570 		certs_size = ext_size;
571 	}
572 
573 	void *cbuf __free(kvfree) = kvzalloc(certs_size, GFP_KERNEL);
574 	if (!cbuf)
575 		return -ENOMEM;
576 
577 	memcpy(cbuf, cert_table, certs_size);
578 	report->auxblob = no_free_ptr(cbuf);
579 	report->auxblob_len = certs_size;
580 
581 	return 0;
582 }
583 
sev_report_attr_visible(int n)584 static bool sev_report_attr_visible(int n)
585 {
586 	switch (n) {
587 	case TSM_REPORT_GENERATION:
588 	case TSM_REPORT_PROVIDER:
589 	case TSM_REPORT_PRIVLEVEL:
590 	case TSM_REPORT_PRIVLEVEL_FLOOR:
591 		return true;
592 	case TSM_REPORT_SERVICE_PROVIDER:
593 	case TSM_REPORT_SERVICE_GUID:
594 	case TSM_REPORT_SERVICE_MANIFEST_VER:
595 		return snp_vmpl;
596 	}
597 
598 	return false;
599 }
600 
sev_report_bin_attr_visible(int n)601 static bool sev_report_bin_attr_visible(int n)
602 {
603 	switch (n) {
604 	case TSM_REPORT_INBLOB:
605 	case TSM_REPORT_OUTBLOB:
606 	case TSM_REPORT_AUXBLOB:
607 		return true;
608 	case TSM_REPORT_MANIFESTBLOB:
609 		return snp_vmpl;
610 	}
611 
612 	return false;
613 }
614 
615 static struct tsm_report_ops sev_tsm_report_ops = {
616 	.name = KBUILD_MODNAME,
617 	.report_new = sev_report_new,
618 	.report_attr_visible = sev_report_attr_visible,
619 	.report_bin_attr_visible = sev_report_bin_attr_visible,
620 };
621 
unregister_sev_tsm(void * data)622 static void unregister_sev_tsm(void *data)
623 {
624 	tsm_report_unregister(&sev_tsm_report_ops);
625 }
626 
sev_guest_probe(struct platform_device * pdev)627 static int __init sev_guest_probe(struct platform_device *pdev)
628 {
629 	struct device *dev = &pdev->dev;
630 	struct snp_guest_dev *snp_dev;
631 	struct snp_msg_desc *mdesc;
632 	struct miscdevice *misc;
633 	int ret;
634 
635 	BUILD_BUG_ON(sizeof(struct snp_guest_msg) > PAGE_SIZE);
636 
637 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
638 		return -ENODEV;
639 
640 	snp_dev = devm_kzalloc(&pdev->dev, sizeof(struct snp_guest_dev), GFP_KERNEL);
641 	if (!snp_dev)
642 		return -ENOMEM;
643 
644 	mdesc = snp_msg_alloc();
645 	if (IS_ERR_OR_NULL(mdesc))
646 		return -ENOMEM;
647 
648 	ret = snp_msg_init(mdesc, vmpck_id);
649 	if (ret)
650 		goto e_msg_init;
651 
652 	platform_set_drvdata(pdev, snp_dev);
653 	snp_dev->dev = dev;
654 
655 	misc = &snp_dev->misc;
656 	misc->minor = MISC_DYNAMIC_MINOR;
657 	misc->name = DEVICE_NAME;
658 	misc->fops = &snp_guest_fops;
659 
660 	/* Set the privlevel_floor attribute based on the vmpck_id */
661 	sev_tsm_report_ops.privlevel_floor = mdesc->vmpck_id;
662 
663 	ret = tsm_report_register(&sev_tsm_report_ops, snp_dev);
664 	if (ret)
665 		goto e_msg_init;
666 
667 	ret = devm_add_action_or_reset(&pdev->dev, unregister_sev_tsm, NULL);
668 	if (ret)
669 		goto e_msg_init;
670 
671 	ret =  misc_register(misc);
672 	if (ret)
673 		goto e_msg_init;
674 
675 	snp_dev->msg_desc = mdesc;
676 	dev_info(dev, "Initialized SEV guest driver (using VMPCK%d communication key)\n",
677 		 mdesc->vmpck_id);
678 	return 0;
679 
680 e_msg_init:
681 	snp_msg_free(mdesc);
682 
683 	return ret;
684 }
685 
sev_guest_remove(struct platform_device * pdev)686 static void __exit sev_guest_remove(struct platform_device *pdev)
687 {
688 	struct snp_guest_dev *snp_dev = platform_get_drvdata(pdev);
689 
690 	snp_msg_free(snp_dev->msg_desc);
691 	misc_deregister(&snp_dev->misc);
692 }
693 
694 /*
695  * This driver is meant to be a common SEV guest interface driver and to
696  * support any SEV guest API. As such, even though it has been introduced
697  * with the SEV-SNP support, it is named "sev-guest".
698  *
699  * sev_guest_remove() lives in .exit.text. For drivers registered via
700  * module_platform_driver_probe() this is ok because they cannot get unbound
701  * at runtime. So mark the driver struct with __refdata to prevent modpost
702  * triggering a section mismatch warning.
703  */
704 static struct platform_driver sev_guest_driver __refdata = {
705 	.remove		= __exit_p(sev_guest_remove),
706 	.driver		= {
707 		.name = "sev-guest",
708 	},
709 };
710 
711 module_platform_driver_probe(sev_guest_driver, sev_guest_probe);
712 
713 MODULE_AUTHOR("Brijesh Singh <brijesh.singh@amd.com>");
714 MODULE_LICENSE("GPL");
715 MODULE_VERSION("1.0.0");
716 MODULE_DESCRIPTION("AMD SEV Guest Driver");
717 MODULE_ALIAS("platform:sev-guest");
718