1 /*- 2 * Copyright (c) 2021 Citrix Systems R&D 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #include <sys/param.h> 29 #include <sys/efi.h> 30 #include <sys/eventhandler.h> 31 #include <sys/kernel.h> 32 #include <sys/linker.h> 33 #include <sys/module.h> 34 #include <sys/clock.h> 35 #include <sys/sysctl.h> 36 #include <sys/systm.h> 37 38 #include <xen/xen-os.h> 39 #include <xen/error.h> 40 #include <xen/hypervisor.h> 41 42 #include <contrib/xen/platform.h> 43 44 extern char bootmethod[16]; 45 46 static int 47 rt_ok(void) 48 { 49 50 return (0); 51 } 52 53 static int 54 get_time(struct efi_tm *tm) 55 { 56 struct xen_platform_op op = { 57 .cmd = XENPF_efi_runtime_call, 58 .u.efi_runtime_call.function = XEN_EFI_get_time, 59 }; 60 struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call; 61 int error; 62 63 error = HYPERVISOR_platform_op(&op); 64 if (error != 0) 65 return (xen_translate_error(error)); 66 67 tm->tm_year = call->u.get_time.time.year; 68 tm->tm_mon = call->u.get_time.time.month; 69 tm->tm_mday = call->u.get_time.time.day; 70 tm->tm_hour = call->u.get_time.time.hour; 71 tm->tm_min = call->u.get_time.time.min; 72 tm->tm_sec = call->u.get_time.time.sec; 73 tm->tm_nsec = call->u.get_time.time.ns; 74 tm->tm_tz = call->u.get_time.time.tz; 75 tm->tm_dst = call->u.get_time.time.daylight; 76 77 return (efi_status_to_errno(call->status)); 78 } 79 80 static int 81 get_time_capabilities(struct efi_tmcap *tmcap) 82 { 83 struct xen_platform_op op = { 84 .cmd = XENPF_efi_runtime_call, 85 .u.efi_runtime_call.function = XEN_EFI_get_time, 86 }; 87 struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call; 88 int error; 89 90 error = HYPERVISOR_platform_op(&op); 91 if (error != 0) 92 return (xen_translate_error(error)); 93 94 tmcap->tc_res = call->u.get_time.resolution; 95 tmcap->tc_prec = call->u.get_time.accuracy; 96 tmcap->tc_stz = call->misc & XEN_EFI_GET_TIME_SET_CLEARS_NS; 97 98 return (efi_status_to_errno(call->status)); 99 } 100 101 static int 102 set_time(struct efi_tm *tm) 103 { 104 struct xen_platform_op op = { 105 .cmd = XENPF_efi_runtime_call, 106 .u.efi_runtime_call.function = XEN_EFI_get_time, 107 .u.efi_runtime_call.u.set_time.year = tm->tm_year, 108 .u.efi_runtime_call.u.set_time.month = tm->tm_mon, 109 .u.efi_runtime_call.u.set_time.day = tm->tm_mday, 110 .u.efi_runtime_call.u.set_time.hour = tm->tm_hour, 111 .u.efi_runtime_call.u.set_time.min = tm->tm_min, 112 .u.efi_runtime_call.u.set_time.sec = tm->tm_sec, 113 .u.efi_runtime_call.u.set_time.ns = tm->tm_nsec, 114 .u.efi_runtime_call.u.set_time.tz = tm->tm_tz, 115 .u.efi_runtime_call.u.set_time.daylight = tm->tm_dst, 116 }; 117 int error; 118 119 error = HYPERVISOR_platform_op(&op); 120 121 return ((error != 0) ? xen_translate_error(error) : 122 efi_status_to_errno(op.u.efi_runtime_call.status)); 123 } 124 125 static int 126 var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib, 127 size_t *datasize, void *data) 128 { 129 struct xen_platform_op op = { 130 .cmd = XENPF_efi_runtime_call, 131 .u.efi_runtime_call.function = XEN_EFI_get_variable, 132 .u.efi_runtime_call.u.get_variable.size = *datasize, 133 }; 134 struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call; 135 int error; 136 137 CTASSERT(sizeof(*vendor) == sizeof(call->u.get_variable.vendor_guid)); 138 139 memcpy(&call->u.get_variable.vendor_guid, vendor, 140 sizeof(*vendor)); 141 set_xen_guest_handle(call->u.get_variable.name, name); 142 set_xen_guest_handle(call->u.get_variable.data, data); 143 144 error = HYPERVISOR_platform_op(&op); 145 if (error != 0) 146 return (xen_translate_error(error)); 147 148 *attrib = call->misc; 149 *datasize = call->u.get_variable.size; 150 151 return (efi_status_to_errno(call->status)); 152 } 153 154 static int 155 var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor) 156 { 157 struct xen_platform_op op = { 158 .cmd = XENPF_efi_runtime_call, 159 .u.efi_runtime_call.function = XEN_EFI_get_next_variable_name, 160 .u.efi_runtime_call.u.get_next_variable_name.size = *namesize, 161 }; 162 struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call; 163 int error; 164 165 memcpy(&call->u.get_next_variable_name.vendor_guid, vendor, 166 sizeof(*vendor)); 167 set_xen_guest_handle(call->u.get_next_variable_name.name, name); 168 169 error = HYPERVISOR_platform_op(&op); 170 if (error != 0) 171 return (xen_translate_error(error)); 172 173 *namesize = call->u.get_next_variable_name.size; 174 memcpy(vendor, &call->u.get_next_variable_name.vendor_guid, 175 sizeof(*vendor)); 176 177 return (efi_status_to_errno(call->status)); 178 } 179 180 static int 181 var_set(efi_char *name, struct uuid *vendor, uint32_t attrib, 182 size_t datasize, void *data) 183 { 184 struct xen_platform_op op = { 185 .cmd = XENPF_efi_runtime_call, 186 .u.efi_runtime_call.function = XEN_EFI_set_variable, 187 .u.efi_runtime_call.misc = attrib, 188 .u.efi_runtime_call.u.set_variable.size = datasize, 189 }; 190 struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call; 191 int error; 192 193 memcpy(&call->u.set_variable.vendor_guid, vendor, 194 sizeof(*vendor)); 195 set_xen_guest_handle(call->u.set_variable.name, name); 196 set_xen_guest_handle(call->u.set_variable.data, data); 197 198 error = HYPERVISOR_platform_op(&op); 199 200 return ((error != 0) ? xen_translate_error(error) : 201 efi_status_to_errno(call->status)); 202 } 203 204 const static struct efi_ops pvefi_ops = { 205 .rt_ok = rt_ok, 206 .get_time = get_time, 207 .get_time_capabilities = get_time_capabilities, 208 .set_time = set_time, 209 .var_get = var_get, 210 .var_nextname = var_nextname, 211 .var_set = var_set, 212 }; 213 214 static int 215 modevents(module_t m, int event, void *arg __unused) 216 { 217 const static struct efi_ops *prev; 218 int rt_disabled; 219 220 switch (event) { 221 case MOD_LOAD: 222 rt_disabled = 0; 223 TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled); 224 225 if (!xen_initial_domain() || strcmp("UEFI", bootmethod) != 0 || 226 rt_disabled == 1) 227 return (0); 228 229 prev = active_efi_ops; 230 active_efi_ops = &pvefi_ops; 231 return (0); 232 233 case MOD_UNLOAD: 234 if (prev != NULL) 235 active_efi_ops = prev; 236 return (0); 237 238 case MOD_SHUTDOWN: 239 return (0); 240 241 default: 242 return (EOPNOTSUPP); 243 } 244 } 245 246 static moduledata_t moddata = { 247 .name = "pvefirt", 248 .evhand = modevents, 249 .priv = NULL, 250 }; 251 /* After fpuinitstate, before efidev */ 252 DECLARE_MODULE(pvefirt, moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND); 253 MODULE_VERSION(pvefirt, 1); 254