xref: /freebsd/sys/dev/xen/efi/pvefi.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1952667daSRoger Pau Monné /*-
2952667daSRoger Pau Monné  * Copyright (c) 2021 Citrix Systems R&D
3952667daSRoger Pau Monné  * All rights reserved.
4952667daSRoger Pau Monné  *
5952667daSRoger Pau Monné  * Redistribution and use in source and binary forms, with or without
6952667daSRoger Pau Monné  * modification, are permitted provided that the following conditions
7952667daSRoger Pau Monné  * are met:
8952667daSRoger Pau Monné  *
9952667daSRoger Pau Monné  * 1. Redistributions of source code must retain the above copyright
10952667daSRoger Pau Monné  *    notice, this list of conditions and the following disclaimer.
11952667daSRoger Pau Monné  * 2. Redistributions in binary form must reproduce the above copyright
12952667daSRoger Pau Monné  *    notice, this list of conditions and the following disclaimer in the
13952667daSRoger Pau Monné  *    documentation and/or other materials provided with the distribution.
14952667daSRoger Pau Monné  *
15952667daSRoger Pau Monné  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16952667daSRoger Pau Monné  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17952667daSRoger Pau Monné  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18952667daSRoger Pau Monné  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19952667daSRoger Pau Monné  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20952667daSRoger Pau Monné  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21952667daSRoger Pau Monné  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22952667daSRoger Pau Monné  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23952667daSRoger Pau Monné  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24952667daSRoger Pau Monné  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25952667daSRoger Pau Monné  */
26952667daSRoger Pau Monné 
27952667daSRoger Pau Monné #include <sys/param.h>
28952667daSRoger Pau Monné #include <sys/efi.h>
29952667daSRoger Pau Monné #include <sys/eventhandler.h>
30952667daSRoger Pau Monné #include <sys/kernel.h>
31952667daSRoger Pau Monné #include <sys/linker.h>
32952667daSRoger Pau Monné #include <sys/module.h>
33952667daSRoger Pau Monné #include <sys/clock.h>
34952667daSRoger Pau Monné #include <sys/sysctl.h>
35952667daSRoger Pau Monné #include <sys/systm.h>
36952667daSRoger Pau Monné 
37952667daSRoger Pau Monné #include <xen/xen-os.h>
38952667daSRoger Pau Monné #include <xen/error.h>
39952667daSRoger Pau Monné #include <xen/hypervisor.h>
40952667daSRoger Pau Monné 
41*ad7dd514SElliott Mitchell #include <contrib/xen/platform.h>
42952667daSRoger Pau Monné 
43952667daSRoger Pau Monné extern char bootmethod[16];
44952667daSRoger Pau Monné 
45952667daSRoger Pau Monné static int
rt_ok(void)46952667daSRoger Pau Monné rt_ok(void)
47952667daSRoger Pau Monné {
48952667daSRoger Pau Monné 
49952667daSRoger Pau Monné 	return (0);
50952667daSRoger Pau Monné }
51952667daSRoger Pau Monné 
52952667daSRoger Pau Monné static int
get_time(struct efi_tm * tm)53952667daSRoger Pau Monné get_time(struct efi_tm *tm)
54952667daSRoger Pau Monné {
55952667daSRoger Pau Monné 	struct xen_platform_op op = {
56952667daSRoger Pau Monné 		.cmd = XENPF_efi_runtime_call,
57952667daSRoger Pau Monné 		.u.efi_runtime_call.function = XEN_EFI_get_time,
58952667daSRoger Pau Monné 	};
59952667daSRoger Pau Monné 	struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call;
60952667daSRoger Pau Monné 	int error;
61952667daSRoger Pau Monné 
62952667daSRoger Pau Monné 	error = HYPERVISOR_platform_op(&op);
63952667daSRoger Pau Monné 	if (error != 0)
64952667daSRoger Pau Monné 		return (xen_translate_error(error));
65952667daSRoger Pau Monné 
66952667daSRoger Pau Monné 	tm->tm_year = call->u.get_time.time.year;
67952667daSRoger Pau Monné 	tm->tm_mon = call->u.get_time.time.month;
68952667daSRoger Pau Monné 	tm->tm_mday = call->u.get_time.time.day;
69952667daSRoger Pau Monné 	tm->tm_hour = call->u.get_time.time.hour;
70952667daSRoger Pau Monné 	tm->tm_min = call->u.get_time.time.min;
71952667daSRoger Pau Monné 	tm->tm_sec = call->u.get_time.time.sec;
72952667daSRoger Pau Monné 	tm->tm_nsec = call->u.get_time.time.ns;
73952667daSRoger Pau Monné 	tm->tm_tz = call->u.get_time.time.tz;
74952667daSRoger Pau Monné 	tm->tm_dst = call->u.get_time.time.daylight;
75952667daSRoger Pau Monné 
76952667daSRoger Pau Monné 	return (efi_status_to_errno(call->status));
77952667daSRoger Pau Monné }
78952667daSRoger Pau Monné 
79952667daSRoger Pau Monné static int
get_time_capabilities(struct efi_tmcap * tmcap)80952667daSRoger Pau Monné get_time_capabilities(struct efi_tmcap *tmcap)
81952667daSRoger Pau Monné {
82952667daSRoger Pau Monné 	struct xen_platform_op op = {
83952667daSRoger Pau Monné 		.cmd = XENPF_efi_runtime_call,
84952667daSRoger Pau Monné 		.u.efi_runtime_call.function = XEN_EFI_get_time,
85952667daSRoger Pau Monné 	};
86952667daSRoger Pau Monné 	struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call;
87952667daSRoger Pau Monné 	int error;
88952667daSRoger Pau Monné 
89952667daSRoger Pau Monné 	error = HYPERVISOR_platform_op(&op);
90952667daSRoger Pau Monné 	if (error != 0)
91952667daSRoger Pau Monné 		return (xen_translate_error(error));
92952667daSRoger Pau Monné 
93952667daSRoger Pau Monné 	tmcap->tc_res = call->u.get_time.resolution;
94952667daSRoger Pau Monné 	tmcap->tc_prec = call->u.get_time.accuracy;
95952667daSRoger Pau Monné 	tmcap->tc_stz = call->misc & XEN_EFI_GET_TIME_SET_CLEARS_NS;
96952667daSRoger Pau Monné 
97952667daSRoger Pau Monné 	return (efi_status_to_errno(call->status));
98952667daSRoger Pau Monné }
99952667daSRoger Pau Monné 
100952667daSRoger Pau Monné static int
set_time(struct efi_tm * tm)101952667daSRoger Pau Monné set_time(struct efi_tm *tm)
102952667daSRoger Pau Monné {
103952667daSRoger Pau Monné 	struct xen_platform_op op = {
104952667daSRoger Pau Monné 		.cmd = XENPF_efi_runtime_call,
105952667daSRoger Pau Monné 		.u.efi_runtime_call.function = XEN_EFI_get_time,
106952667daSRoger Pau Monné 		.u.efi_runtime_call.u.set_time.year = tm->tm_year,
107952667daSRoger Pau Monné 		.u.efi_runtime_call.u.set_time.month = tm->tm_mon,
108952667daSRoger Pau Monné 		.u.efi_runtime_call.u.set_time.day = tm->tm_mday,
109952667daSRoger Pau Monné 		.u.efi_runtime_call.u.set_time.hour = tm->tm_hour,
110952667daSRoger Pau Monné 		.u.efi_runtime_call.u.set_time.min = tm->tm_min,
111952667daSRoger Pau Monné 		.u.efi_runtime_call.u.set_time.sec = tm->tm_sec,
112952667daSRoger Pau Monné 		.u.efi_runtime_call.u.set_time.ns = tm->tm_nsec,
113952667daSRoger Pau Monné 		.u.efi_runtime_call.u.set_time.tz = tm->tm_tz,
114952667daSRoger Pau Monné 		.u.efi_runtime_call.u.set_time.daylight = tm->tm_dst,
115952667daSRoger Pau Monné 	};
116952667daSRoger Pau Monné 	int error;
117952667daSRoger Pau Monné 
118952667daSRoger Pau Monné 	error = HYPERVISOR_platform_op(&op);
119952667daSRoger Pau Monné 
120952667daSRoger Pau Monné 	return ((error != 0) ? xen_translate_error(error) :
121952667daSRoger Pau Monné 	    efi_status_to_errno(op.u.efi_runtime_call.status));
122952667daSRoger Pau Monné }
123952667daSRoger Pau Monné 
124952667daSRoger Pau Monné static int
var_get(efi_char * name,struct uuid * vendor,uint32_t * attrib,size_t * datasize,void * data)125952667daSRoger Pau Monné var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
126952667daSRoger Pau Monné     size_t *datasize, void *data)
127952667daSRoger Pau Monné {
128952667daSRoger Pau Monné 	struct xen_platform_op op = {
129952667daSRoger Pau Monné 		.cmd = XENPF_efi_runtime_call,
130952667daSRoger Pau Monné 		.u.efi_runtime_call.function = XEN_EFI_get_variable,
131952667daSRoger Pau Monné 		.u.efi_runtime_call.u.get_variable.size = *datasize,
132952667daSRoger Pau Monné 	};
133952667daSRoger Pau Monné 	struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call;
134952667daSRoger Pau Monné 	int error;
135952667daSRoger Pau Monné 
136952667daSRoger Pau Monné 	CTASSERT(sizeof(*vendor) == sizeof(call->u.get_variable.vendor_guid));
137952667daSRoger Pau Monné 
138952667daSRoger Pau Monné 	memcpy(&call->u.get_variable.vendor_guid, vendor,
139952667daSRoger Pau Monné 	    sizeof(*vendor));
140952667daSRoger Pau Monné 	set_xen_guest_handle(call->u.get_variable.name, name);
141952667daSRoger Pau Monné 	set_xen_guest_handle(call->u.get_variable.data, data);
142952667daSRoger Pau Monné 
143952667daSRoger Pau Monné 	error = HYPERVISOR_platform_op(&op);
144952667daSRoger Pau Monné 	if (error != 0)
145952667daSRoger Pau Monné 		return (xen_translate_error(error));
146952667daSRoger Pau Monné 
147952667daSRoger Pau Monné 	*attrib = call->misc;
148952667daSRoger Pau Monné 	*datasize = call->u.get_variable.size;
149952667daSRoger Pau Monné 
150952667daSRoger Pau Monné 	return (efi_status_to_errno(call->status));
151952667daSRoger Pau Monné }
152952667daSRoger Pau Monné 
153952667daSRoger Pau Monné static int
var_nextname(size_t * namesize,efi_char * name,struct uuid * vendor)154952667daSRoger Pau Monné var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
155952667daSRoger Pau Monné {
156952667daSRoger Pau Monné 	struct xen_platform_op op = {
157952667daSRoger Pau Monné 		.cmd = XENPF_efi_runtime_call,
158952667daSRoger Pau Monné 		.u.efi_runtime_call.function = XEN_EFI_get_next_variable_name,
159952667daSRoger Pau Monné 		.u.efi_runtime_call.u.get_next_variable_name.size = *namesize,
160952667daSRoger Pau Monné 	};
161952667daSRoger Pau Monné 	struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call;
162952667daSRoger Pau Monné 	int error;
163952667daSRoger Pau Monné 
164952667daSRoger Pau Monné 	memcpy(&call->u.get_next_variable_name.vendor_guid, vendor,
165952667daSRoger Pau Monné 	    sizeof(*vendor));
166952667daSRoger Pau Monné 	set_xen_guest_handle(call->u.get_next_variable_name.name, name);
167952667daSRoger Pau Monné 
168952667daSRoger Pau Monné 	error = HYPERVISOR_platform_op(&op);
169952667daSRoger Pau Monné 	if (error != 0)
170952667daSRoger Pau Monné 		return (xen_translate_error(error));
171952667daSRoger Pau Monné 
172952667daSRoger Pau Monné 	*namesize = call->u.get_next_variable_name.size;
173952667daSRoger Pau Monné 	memcpy(vendor, &call->u.get_next_variable_name.vendor_guid,
174952667daSRoger Pau Monné 	    sizeof(*vendor));
175952667daSRoger Pau Monné 
176952667daSRoger Pau Monné 	return (efi_status_to_errno(call->status));
177952667daSRoger Pau Monné }
178952667daSRoger Pau Monné 
179952667daSRoger Pau Monné static int
var_set(efi_char * name,struct uuid * vendor,uint32_t attrib,size_t datasize,void * data)180952667daSRoger Pau Monné var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
181952667daSRoger Pau Monné     size_t datasize, void *data)
182952667daSRoger Pau Monné {
183952667daSRoger Pau Monné 	struct xen_platform_op op = {
184952667daSRoger Pau Monné 		.cmd = XENPF_efi_runtime_call,
185952667daSRoger Pau Monné 		.u.efi_runtime_call.function = XEN_EFI_set_variable,
186952667daSRoger Pau Monné 		.u.efi_runtime_call.misc = attrib,
187952667daSRoger Pau Monné 		.u.efi_runtime_call.u.set_variable.size = datasize,
188952667daSRoger Pau Monné 	};
189952667daSRoger Pau Monné 	struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call;
190952667daSRoger Pau Monné 	int error;
191952667daSRoger Pau Monné 
192952667daSRoger Pau Monné 	memcpy(&call->u.set_variable.vendor_guid, vendor,
193952667daSRoger Pau Monné 	    sizeof(*vendor));
194952667daSRoger Pau Monné 	set_xen_guest_handle(call->u.set_variable.name, name);
195952667daSRoger Pau Monné 	set_xen_guest_handle(call->u.set_variable.data, data);
196952667daSRoger Pau Monné 
197952667daSRoger Pau Monné 	error = HYPERVISOR_platform_op(&op);
198952667daSRoger Pau Monné 
199952667daSRoger Pau Monné 	return ((error != 0) ? xen_translate_error(error) :
200952667daSRoger Pau Monné 	    efi_status_to_errno(call->status));
201952667daSRoger Pau Monné }
202952667daSRoger Pau Monné 
203952667daSRoger Pau Monné const static struct efi_ops pvefi_ops = {
204952667daSRoger Pau Monné 	.rt_ok = rt_ok,
205952667daSRoger Pau Monné 	.get_time = get_time,
206952667daSRoger Pau Monné 	.get_time_capabilities = get_time_capabilities,
207952667daSRoger Pau Monné 	.set_time = set_time,
208952667daSRoger Pau Monné 	.var_get = var_get,
209952667daSRoger Pau Monné 	.var_nextname = var_nextname,
210952667daSRoger Pau Monné 	.var_set = var_set,
211952667daSRoger Pau Monné };
212952667daSRoger Pau Monné 
213952667daSRoger Pau Monné static int
modevents(module_t m,int event,void * arg __unused)214952667daSRoger Pau Monné modevents(module_t m, int event, void *arg __unused)
215952667daSRoger Pau Monné {
216952667daSRoger Pau Monné 	const static struct efi_ops *prev;
217952667daSRoger Pau Monné 	int rt_disabled;
218952667daSRoger Pau Monné 
219952667daSRoger Pau Monné 	switch (event) {
220952667daSRoger Pau Monné 	case MOD_LOAD:
221952667daSRoger Pau Monné 		rt_disabled = 0;
222952667daSRoger Pau Monné 		TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
223952667daSRoger Pau Monné 
224952667daSRoger Pau Monné 		if (!xen_initial_domain() || strcmp("UEFI", bootmethod) != 0 ||
225952667daSRoger Pau Monné 		    rt_disabled == 1)
226952667daSRoger Pau Monné 			return (0);
227952667daSRoger Pau Monné 
228952667daSRoger Pau Monné 		prev = active_efi_ops;
229952667daSRoger Pau Monné 		active_efi_ops = &pvefi_ops;
230952667daSRoger Pau Monné 		return (0);
231952667daSRoger Pau Monné 
232952667daSRoger Pau Monné 	case MOD_UNLOAD:
233952667daSRoger Pau Monné 		if (prev != NULL)
234952667daSRoger Pau Monné 		    active_efi_ops = prev;
235952667daSRoger Pau Monné 		return (0);
236952667daSRoger Pau Monné 
237952667daSRoger Pau Monné 	case MOD_SHUTDOWN:
238952667daSRoger Pau Monné 		return (0);
239952667daSRoger Pau Monné 
240952667daSRoger Pau Monné 	default:
241952667daSRoger Pau Monné 		return (EOPNOTSUPP);
242952667daSRoger Pau Monné 	}
243952667daSRoger Pau Monné }
244952667daSRoger Pau Monné 
245952667daSRoger Pau Monné static moduledata_t moddata = {
246952667daSRoger Pau Monné 	.name = "pvefirt",
247952667daSRoger Pau Monné 	.evhand = modevents,
248952667daSRoger Pau Monné 	.priv = NULL,
249952667daSRoger Pau Monné };
250952667daSRoger Pau Monné /* After fpuinitstate, before efidev */
251952667daSRoger Pau Monné DECLARE_MODULE(pvefirt, moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
252952667daSRoger Pau Monné MODULE_VERSION(pvefirt, 1);
253