summaryrefslogtreecommitdiff
path: root/usr.sbin/vmd/psp.c
blob: 2de8cf4ac0b6451bd9e6604e5a93e444f5c3bb9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*	$OpenBSD: psp.c,v 1.3 2024/11/05 23:16:46 bluhm Exp $	*/

/*
 * Copyright (c) 2023, 2024 Hans-Joerg Hoexer <hshoexer@genua.de>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/ioctl.h>

#include <dev/ic/pspvar.h>

#include <errno.h>
#include <fcntl.h>
#include <string.h>

#include "vmd.h"

extern struct vmd	*env;

/* Guest policy */
#define GPOL_NODBG	(1ULL << 0)	/* no debuggin */
#define GPOL_NOKS	(1ULL << 1)	/* no key sharing */
#define GPOL_ES		(1ULL << 2)	/* SEV-ES required */
#define GPOL_NOSEND	(1ULL << 3)	/* no guest migration */
#define GPOL_DOMAIN	(1ULL << 4)	/* no migration to other domain */
#define GPOL_SEV	(1ULL << 5)	/* no migration to non-SEV platform */


/*
 * Retrieve platform state.
 */
int
psp_get_pstate(uint16_t *state)
{
	struct psp_platform_status pst;

	if (ioctl(env->vmd_psp_fd, PSP_IOC_GET_PSTATUS, &pst) < 0) {
		log_warn("%s: ioctl", __func__);
		return (-1);
	}

	if (state)
		*state = pst.state;

	return (0);
}


/*
 * Flush data fabrics of all cores.
 *
 * This ensures all data of a SEV enabled guest is committed to
 * memory.  This needs to be done before an ASID is assigend to
 * guest using psp_activate().
 */
int
psp_df_flush(void)
{
	if (ioctl(env->vmd_psp_fd, PSP_IOC_DF_FLUSH) < 0) {
		log_warn("%s: ioctl", __func__);
		return (-1);
	}

	return (0);
}


/*
 * Retrieve guest state.
 */
int
psp_get_gstate(uint32_t handle, uint32_t *policy, uint32_t *asid,
    uint8_t *state)
{
	struct psp_guest_status gst;

	memset(&gst, 0, sizeof(gst));
	gst.handle = handle;

	if (ioctl(env->vmd_psp_fd, PSP_IOC_GET_GSTATUS, &gst) < 0) {
		log_warn("%s: ioctl", __func__);
		return (-1);
	}

	if (policy)
		*policy = gst.policy;
	if (asid)
		*asid = gst.asid;
	if (state)
		*state = gst.state;

	return (0);
}


/*
 * Start the launch sequence of a guest.
 */
int
psp_launch_start(uint32_t *handle)
{
	struct psp_launch_start ls;

	memset(&ls, 0, sizeof(ls));

	/* Set guest policy. */
	ls.policy = (GPOL_NODBG | GPOL_NOKS | GPOL_NOSEND | GPOL_DOMAIN |
	    GPOL_SEV);

	if (ioctl(env->vmd_psp_fd, PSP_IOC_LAUNCH_START, &ls) < 0) {
		log_warn("%s: ioctl", __func__);
		return (-1);
	}

	if (handle)
		*handle = ls.handle;

	return (0);
}


/*
 * Encrypt and measure a memory range.
 */
int
psp_launch_update(uint32_t handle, vaddr_t v, size_t len)
{
	struct psp_launch_update_data lud;

	memset(&lud, 0, sizeof(lud));
	lud.handle = handle;
	lud.paddr = v;			/* will be converted to paddr */
	lud.length = len;

	if (ioctl(env->vmd_psp_fd, PSP_IOC_LAUNCH_UPDATE_DATA, &lud) < 0) {
		log_warn("%s: ioctl", __func__);
		return (-1);
	}

	return (0);
}


/*
 * Finalize and return memory measurement.
 *
 * We ask the PSP to provide a measurement (HMAC) over the encrypted
 * memory.  As we do not yet negotiate a shared integrity key with
 * the PSP, the measurement is not really meaningful.  Thus we just
 * log it for now.
 */
int
psp_launch_measure(uint32_t handle)
{
	struct psp_launch_measure lm;
	char *p, buf[256];
	size_t len;
	unsigned int i;

	memset(&lm, 0, sizeof(lm));
	lm.handle = handle;
	lm.measure_len = sizeof(lm.psp_measure);
	memset(lm.measure, 0, sizeof(lm.measure));
	memset(lm.measure_nonce, 0, sizeof(lm.measure_nonce));

	if (ioctl(env->vmd_psp_fd, PSP_IOC_LAUNCH_MEASURE, &lm) < 0) {
		log_warn("%s: ioctl", __func__);
		return (-1);
	}

	/*
	 * We can not verify the measurement, yet. Therefore just
	 * log it.
	 */
	len = sizeof(buf);
	memset(buf, 0, len);
	p = buf;
	for (i = 0; i < sizeof(lm.measure) && len >= 2;
	    i++, p += 2, len -= 2) {
		snprintf(p, len, "%02x", lm.measure[i]);
	}
	log_info("%s: measurement\t0x%s", __func__, buf);

	len = sizeof(buf);
	memset(buf, 0, len);
	p = buf;
	for (i = 0; i < sizeof(lm.measure_nonce) && len >= 2;
	    i++, p += 2, len -= 2) {
		snprintf(p, len, "%02x", lm.measure_nonce[i]);
	}
	log_info("%s: nonce\t0x%s", __func__, buf);

	return (0);
}


/*
 * Finalize launch sequence.
 */
int
psp_launch_finish(uint32_t handle)
{
	struct psp_launch_finish lf;

	lf.handle = handle;

	if (ioctl(env->vmd_psp_fd, PSP_IOC_LAUNCH_FINISH, &lf) < 0) {
		log_warn("%s: ioctl", __func__);
		return (-1);
	}

	return (0);
}


/*
 * Activate a guest.
 *
 * This associates the guest's ASID with the handle used to identify
 * crypto contexts managed by the PSP.
 */
int
psp_activate(uint32_t handle, uint32_t asid)
{
	struct psp_activate act;

	act.handle = handle;
	act.asid = asid;

	if (ioctl(env->vmd_psp_fd, PSP_IOC_ACTIVATE, &act) < 0) {
		log_warn("%s: ioctl", __func__);
		return (-1);
	}

	return (0);
}


/*
 * Deactivate and decommission a guest.
 *
 * This deassociates the guest's ASID from the crypto contexts in
 * the PSP.  Then the PSP releases the crypto contexts (i.e. deletes
 * keys).
 */
int
psp_guest_shutdown(uint32_t handle)
{
	struct psp_guest_shutdown gshutdown;

	gshutdown.handle = handle;

	if (ioctl(env->vmd_psp_fd, PSP_IOC_GUEST_SHUTDOWN, &gshutdown) < 0) {
		log_warn("%s: ioctl", __func__);
		return (-1);
	}

	return (0);
}

/*
 * Initialize PSP.
 */
static int
psp_init(void)
{
	if (ioctl(env->vmd_psp_fd, PSP_IOC_INIT) < 0) {
		log_warn("%s: ioctl", __func__);
		return (-1);
	}

	return (0);
}

/*
 * Shutdown PSP.
 */
static int
psp_shutdown(void)
{
	if (ioctl(env->vmd_psp_fd, PSP_IOC_SHUTDOWN) < 0) {
		log_warn("%s: ioctl", __func__);
		return (-1);
	}

	return (0);
}

/*
 * Reset PSP.
 *
 * Shut PSP down, then re-initialize it.  This clears and resets
 * all active contexts.
 */
static int
psp_reset(void)
{
	int	ret;

	if ((ret = psp_shutdown()) < 0 || (ret = psp_init()) < 0)
		return (ret);

	return (0);
}

void
psp_setup(void)
{
	env->vmd_psp_fd = open(PSP_NODE, O_RDWR);
	if (env->vmd_psp_fd == -1) {
		if (errno != ENXIO)
			log_debug("%s: failed to open %s", __func__, PSP_NODE);
		return;
	}

	if (psp_reset() < 0)
		fatalx("%s: failed to reset PSP", __func__);
}