Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * runtime-wrappers.c - Runtime Services function call wrappers
4 *
5 * Implementation summary:
6 * -----------------------
7 * 1. When user/kernel thread requests to execute efi_runtime_service(),
8 * enqueue work to efi_rts_wq.
9 * 2. Caller thread waits for completion until the work is finished
10 * because it's dependent on the return status and execution of
11 * efi_runtime_service().
12 * For instance, get_variable() and get_next_variable().
13 *
14 * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
15 *
16 * Split off from arch/x86/platform/efi/efi.c
17 *
18 * Copyright (C) 1999 VA Linux Systems
19 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
20 * Copyright (C) 1999-2002 Hewlett-Packard Co.
21 * Copyright (C) 2005-2008 Intel Co.
22 * Copyright (C) 2013 SuSE Labs
23 */
24
25#define pr_fmt(fmt) "efi: " fmt
26
27#include <linux/bug.h>
28#include <linux/efi.h>
29#include <linux/irqflags.h>
30#include <linux/mutex.h>
31#include <linux/semaphore.h>
32#include <linux/stringify.h>
33#include <linux/workqueue.h>
34#include <linux/completion.h>
35
36#include <asm/efi.h>
37
38/*
39 * Wrap around the new efi_call_virt_generic() macros so that the
40 * code doesn't get too cluttered:
41 */
42#define efi_call_virt(f, args...) \
43 arch_efi_call_virt(efi.runtime, f, args)
44
45union efi_rts_args {
46 struct {
47 efi_time_t *time;
48 efi_time_cap_t *capabilities;
49 } GET_TIME;
50
51 struct {
52 efi_time_t *time;
53 } SET_TIME;
54
55 struct {
56 efi_bool_t *enabled;
57 efi_bool_t *pending;
58 efi_time_t *time;
59 } GET_WAKEUP_TIME;
60
61 struct {
62 efi_bool_t enable;
63 efi_time_t *time;
64 } SET_WAKEUP_TIME;
65
66 struct {
67 efi_char16_t *name;
68 efi_guid_t *vendor;
69 u32 *attr;
70 unsigned long *data_size;
71 void *data;
72 } GET_VARIABLE;
73
74 struct {
75 unsigned long *name_size;
76 efi_char16_t *name;
77 efi_guid_t *vendor;
78 } GET_NEXT_VARIABLE;
79
80 struct {
81 efi_char16_t *name;
82 efi_guid_t *vendor;
83 u32 attr;
84 unsigned long data_size;
85 void *data;
86 } SET_VARIABLE;
87
88 struct {
89 u32 attr;
90 u64 *storage_space;
91 u64 *remaining_space;
92 u64 *max_variable_size;
93 } QUERY_VARIABLE_INFO;
94
95 struct {
96 u32 *high_count;
97 } GET_NEXT_HIGH_MONO_COUNT;
98
99 struct {
100 efi_capsule_header_t **capsules;
101 unsigned long count;
102 unsigned long sg_list;
103 } UPDATE_CAPSULE;
104
105 struct {
106 efi_capsule_header_t **capsules;
107 unsigned long count;
108 u64 *max_size;
109 int *reset_type;
110 } QUERY_CAPSULE_CAPS;
111
112 struct {
113 efi_status_t (__efiapi *acpi_prm_handler)(u64, void *);
114 u64 param_buffer_addr;
115 void *context;
116 } ACPI_PRM_HANDLER;
117};
118
119struct efi_runtime_work efi_rts_work;
120
121/*
122 * efi_queue_work: Queue EFI runtime service call and wait for completion
123 * @_rts: EFI runtime service function identifier
124 * @_args: Arguments to pass to the EFI runtime service
125 *
126 * Accesses to efi_runtime_services() are serialized by a binary
127 * semaphore (efi_runtime_lock) and caller waits until the work is
128 * finished, hence _only_ one work is queued at a time and the caller
129 * thread waits for completion.
130 */
131#define efi_queue_work(_rts, _args...) \
132 __efi_queue_work(EFI_ ## _rts, \
133 &(union efi_rts_args){ ._rts = { _args }})
134
135#ifndef arch_efi_save_flags
136#define arch_efi_save_flags(state_flags) local_save_flags(state_flags)
137#define arch_efi_restore_flags(state_flags) local_irq_restore(state_flags)
138#endif
139
140unsigned long efi_call_virt_save_flags(void)
141{
142 unsigned long flags;
143
144 arch_efi_save_flags(flags);
145 return flags;
146}
147
148void efi_call_virt_check_flags(unsigned long flags, const void *caller)
149{
150 unsigned long cur_flags, mismatch;
151
152 cur_flags = efi_call_virt_save_flags();
153
154 mismatch = flags ^ cur_flags;
155 if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
156 return;
157
158 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
159 pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI call from %pS\n",
160 flags, cur_flags, caller ?: __builtin_return_address(0));
161 arch_efi_restore_flags(flags);
162}
163
164/*
165 * According to section 7.1 of the UEFI spec, Runtime Services are not fully
166 * reentrant, and there are particular combinations of calls that need to be
167 * serialized. (source: UEFI Specification v2.4A)
168 *
169 * Table 31. Rules for Reentry Into Runtime Services
170 * +------------------------------------+-------------------------------+
171 * | If previous call is busy in | Forbidden to call |
172 * +------------------------------------+-------------------------------+
173 * | Any | SetVirtualAddressMap() |
174 * +------------------------------------+-------------------------------+
175 * | ConvertPointer() | ConvertPointer() |
176 * +------------------------------------+-------------------------------+
177 * | SetVariable() | ResetSystem() |
178 * | UpdateCapsule() | |
179 * | SetTime() | |
180 * | SetWakeupTime() | |
181 * | GetNextHighMonotonicCount() | |
182 * +------------------------------------+-------------------------------+
183 * | GetVariable() | GetVariable() |
184 * | GetNextVariableName() | GetNextVariableName() |
185 * | SetVariable() | SetVariable() |
186 * | QueryVariableInfo() | QueryVariableInfo() |
187 * | UpdateCapsule() | UpdateCapsule() |
188 * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
189 * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
190 * +------------------------------------+-------------------------------+
191 * | GetTime() | GetTime() |
192 * | SetTime() | SetTime() |
193 * | GetWakeupTime() | GetWakeupTime() |
194 * | SetWakeupTime() | SetWakeupTime() |
195 * +------------------------------------+-------------------------------+
196 *
197 * Due to the fact that the EFI pstore may write to the variable store in
198 * interrupt context, we need to use a lock for at least the groups that
199 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
200 * none of the remaining functions are actually ever called at runtime.
201 * So let's just use a single lock to serialize all Runtime Services calls.
202 */
203static DEFINE_SEMAPHORE(efi_runtime_lock, 1);
204
205static struct task_struct *efi_runtime_lock_owner;
206
207/*
208 * Expose the EFI runtime lock to the UV platform
209 */
210#ifdef CONFIG_X86_UV
211extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
212#endif
213
214/*
215 * Calls the appropriate efi_runtime_service() with the appropriate
216 * arguments.
217 */
218static void __nocfi efi_call_rts(struct work_struct *work)
219{
220 const union efi_rts_args *args = efi_rts_work.args;
221 efi_status_t status = EFI_NOT_FOUND;
222 unsigned long flags;
223
224 efi_runtime_lock_owner = current;
225
226 arch_efi_call_virt_setup();
227 flags = efi_call_virt_save_flags();
228
229 switch (efi_rts_work.efi_rts_id) {
230 case EFI_GET_TIME:
231 status = efi_call_virt(get_time,
232 args->GET_TIME.time,
233 args->GET_TIME.capabilities);
234 break;
235 case EFI_SET_TIME:
236 status = efi_call_virt(set_time,
237 args->SET_TIME.time);
238 break;
239 case EFI_GET_WAKEUP_TIME:
240 status = efi_call_virt(get_wakeup_time,
241 args->GET_WAKEUP_TIME.enabled,
242 args->GET_WAKEUP_TIME.pending,
243 args->GET_WAKEUP_TIME.time);
244 break;
245 case EFI_SET_WAKEUP_TIME:
246 status = efi_call_virt(set_wakeup_time,
247 args->SET_WAKEUP_TIME.enable,
248 args->SET_WAKEUP_TIME.time);
249 break;
250 case EFI_GET_VARIABLE:
251 status = efi_call_virt(get_variable,
252 args->GET_VARIABLE.name,
253 args->GET_VARIABLE.vendor,
254 args->GET_VARIABLE.attr,
255 args->GET_VARIABLE.data_size,
256 args->GET_VARIABLE.data);
257 break;
258 case EFI_GET_NEXT_VARIABLE:
259 status = efi_call_virt(get_next_variable,
260 args->GET_NEXT_VARIABLE.name_size,
261 args->GET_NEXT_VARIABLE.name,
262 args->GET_NEXT_VARIABLE.vendor);
263 break;
264 case EFI_SET_VARIABLE:
265 status = efi_call_virt(set_variable,
266 args->SET_VARIABLE.name,
267 args->SET_VARIABLE.vendor,
268 args->SET_VARIABLE.attr,
269 args->SET_VARIABLE.data_size,
270 args->SET_VARIABLE.data);
271 break;
272 case EFI_QUERY_VARIABLE_INFO:
273 status = efi_call_virt(query_variable_info,
274 args->QUERY_VARIABLE_INFO.attr,
275 args->QUERY_VARIABLE_INFO.storage_space,
276 args->QUERY_VARIABLE_INFO.remaining_space,
277 args->QUERY_VARIABLE_INFO.max_variable_size);
278 break;
279 case EFI_GET_NEXT_HIGH_MONO_COUNT:
280 status = efi_call_virt(get_next_high_mono_count,
281 args->GET_NEXT_HIGH_MONO_COUNT.high_count);
282 break;
283 case EFI_UPDATE_CAPSULE:
284 status = efi_call_virt(update_capsule,
285 args->UPDATE_CAPSULE.capsules,
286 args->UPDATE_CAPSULE.count,
287 args->UPDATE_CAPSULE.sg_list);
288 break;
289 case EFI_QUERY_CAPSULE_CAPS:
290 status = efi_call_virt(query_capsule_caps,
291 args->QUERY_CAPSULE_CAPS.capsules,
292 args->QUERY_CAPSULE_CAPS.count,
293 args->QUERY_CAPSULE_CAPS.max_size,
294 args->QUERY_CAPSULE_CAPS.reset_type);
295 break;
296 case EFI_ACPI_PRM_HANDLER:
297#ifdef CONFIG_ACPI_PRMT
298 status = arch_efi_call_virt(args, ACPI_PRM_HANDLER.acpi_prm_handler,
299 args->ACPI_PRM_HANDLER.param_buffer_addr,
300 args->ACPI_PRM_HANDLER.context);
301 break;
302#endif
303 default:
304 /*
305 * Ideally, we should never reach here because a caller of this
306 * function should have put the right efi_runtime_service()
307 * function identifier into efi_rts_work->efi_rts_id
308 */
309 pr_err("Requested executing invalid EFI Runtime Service.\n");
310 }
311
312 efi_call_virt_check_flags(flags, efi_rts_work.caller);
313 arch_efi_call_virt_teardown();
314
315 efi_rts_work.status = status;
316 complete(&efi_rts_work.efi_rts_comp);
317 efi_runtime_lock_owner = NULL;
318}
319
320static efi_status_t __efi_queue_work(enum efi_rts_ids id,
321 union efi_rts_args *args)
322{
323 efi_rts_work.efi_rts_id = id;
324 efi_rts_work.args = args;
325 efi_rts_work.caller = __builtin_return_address(0);
326 efi_rts_work.status = EFI_ABORTED;
327
328 if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
329 pr_warn_once("EFI Runtime Services are disabled!\n");
330 efi_rts_work.status = EFI_DEVICE_ERROR;
331 goto exit;
332 }
333
334 init_completion(&efi_rts_work.efi_rts_comp);
335 INIT_WORK(&efi_rts_work.work, efi_call_rts);
336
337 /*
338 * queue_work() returns 0 if work was already on queue,
339 * _ideally_ this should never happen.
340 */
341 if (queue_work(efi_rts_wq, &efi_rts_work.work))
342 wait_for_completion(&efi_rts_work.efi_rts_comp);
343 else
344 pr_err("Failed to queue work to efi_rts_wq.\n");
345
346 WARN_ON_ONCE(efi_rts_work.status == EFI_ABORTED);
347exit:
348 efi_rts_work.efi_rts_id = EFI_NONE;
349 return efi_rts_work.status;
350}
351
352static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
353{
354 efi_status_t status;
355
356 if (down_interruptible(&efi_runtime_lock))
357 return EFI_ABORTED;
358 status = efi_queue_work(GET_TIME, tm, tc);
359 up(&efi_runtime_lock);
360 return status;
361}
362
363static efi_status_t virt_efi_set_time(efi_time_t *tm)
364{
365 efi_status_t status;
366
367 if (down_interruptible(&efi_runtime_lock))
368 return EFI_ABORTED;
369 status = efi_queue_work(SET_TIME, tm);
370 up(&efi_runtime_lock);
371 return status;
372}
373
374static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
375 efi_bool_t *pending,
376 efi_time_t *tm)
377{
378 efi_status_t status;
379
380 if (down_interruptible(&efi_runtime_lock))
381 return EFI_ABORTED;
382 status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm);
383 up(&efi_runtime_lock);
384 return status;
385}
386
387static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
388{
389 efi_status_t status;
390
391 if (down_interruptible(&efi_runtime_lock))
392 return EFI_ABORTED;
393 status = efi_queue_work(SET_WAKEUP_TIME, enabled, tm);
394 up(&efi_runtime_lock);
395 return status;
396}
397
398static efi_status_t virt_efi_get_variable(efi_char16_t *name,
399 efi_guid_t *vendor,
400 u32 *attr,
401 unsigned long *data_size,
402 void *data)
403{
404 efi_status_t status;
405
406 if (down_interruptible(&efi_runtime_lock))
407 return EFI_ABORTED;
408 status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size,
409 data);
410 up(&efi_runtime_lock);
411 return status;
412}
413
414static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
415 efi_char16_t *name,
416 efi_guid_t *vendor)
417{
418 efi_status_t status;
419
420 if (down_interruptible(&efi_runtime_lock))
421 return EFI_ABORTED;
422 status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor);
423 up(&efi_runtime_lock);
424 return status;
425}
426
427static efi_status_t virt_efi_set_variable(efi_char16_t *name,
428 efi_guid_t *vendor,
429 u32 attr,
430 unsigned long data_size,
431 void *data)
432{
433 efi_status_t status;
434
435 if (down_interruptible(&efi_runtime_lock))
436 return EFI_ABORTED;
437 status = efi_queue_work(SET_VARIABLE, name, vendor, attr, data_size,
438 data);
439 up(&efi_runtime_lock);
440 return status;
441}
442
443static efi_status_t __nocfi
444virt_efi_set_variable_nb(efi_char16_t *name, efi_guid_t *vendor, u32 attr,
445 unsigned long data_size, void *data)
446{
447 efi_status_t status;
448
449 if (down_trylock(&efi_runtime_lock))
450 return EFI_NOT_READY;
451
452 efi_runtime_lock_owner = current;
453 status = efi_call_virt_pointer(efi.runtime, set_variable, name, vendor,
454 attr, data_size, data);
455 efi_runtime_lock_owner = NULL;
456 up(&efi_runtime_lock);
457 return status;
458}
459
460
461static efi_status_t virt_efi_query_variable_info(u32 attr,
462 u64 *storage_space,
463 u64 *remaining_space,
464 u64 *max_variable_size)
465{
466 efi_status_t status;
467
468 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
469 return EFI_UNSUPPORTED;
470
471 if (down_interruptible(&efi_runtime_lock))
472 return EFI_ABORTED;
473 status = efi_queue_work(QUERY_VARIABLE_INFO, attr, storage_space,
474 remaining_space, max_variable_size);
475 up(&efi_runtime_lock);
476 return status;
477}
478
479static efi_status_t __nocfi
480virt_efi_query_variable_info_nb(u32 attr, u64 *storage_space,
481 u64 *remaining_space, u64 *max_variable_size)
482{
483 efi_status_t status;
484
485 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
486 return EFI_UNSUPPORTED;
487
488 if (down_trylock(&efi_runtime_lock))
489 return EFI_NOT_READY;
490
491 efi_runtime_lock_owner = current;
492 status = efi_call_virt_pointer(efi.runtime, query_variable_info, attr,
493 storage_space, remaining_space,
494 max_variable_size);
495 efi_runtime_lock_owner = NULL;
496 up(&efi_runtime_lock);
497 return status;
498}
499
500static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
501{
502 efi_status_t status;
503
504 if (down_interruptible(&efi_runtime_lock))
505 return EFI_ABORTED;
506 status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count);
507 up(&efi_runtime_lock);
508 return status;
509}
510
511static void __nocfi
512virt_efi_reset_system(int reset_type, efi_status_t status,
513 unsigned long data_size, efi_char16_t *data)
514{
515 if (down_trylock(&efi_runtime_lock)) {
516 pr_warn("failed to invoke the reset_system() runtime service:\n"
517 "could not get exclusive access to the firmware\n");
518 return;
519 }
520
521 efi_runtime_lock_owner = current;
522 arch_efi_call_virt_setup();
523 efi_rts_work.efi_rts_id = EFI_RESET_SYSTEM;
524 arch_efi_call_virt(efi.runtime, reset_system, reset_type, status,
525 data_size, data);
526 arch_efi_call_virt_teardown();
527 efi_runtime_lock_owner = NULL;
528 up(&efi_runtime_lock);
529}
530
531static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
532 unsigned long count,
533 unsigned long sg_list)
534{
535 efi_status_t status;
536
537 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
538 return EFI_UNSUPPORTED;
539
540 if (down_interruptible(&efi_runtime_lock))
541 return EFI_ABORTED;
542 status = efi_queue_work(UPDATE_CAPSULE, capsules, count, sg_list);
543 up(&efi_runtime_lock);
544 return status;
545}
546
547static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
548 unsigned long count,
549 u64 *max_size,
550 int *reset_type)
551{
552 efi_status_t status;
553
554 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
555 return EFI_UNSUPPORTED;
556
557 if (down_interruptible(&efi_runtime_lock))
558 return EFI_ABORTED;
559 status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, count,
560 max_size, reset_type);
561 up(&efi_runtime_lock);
562 return status;
563}
564
565void __init efi_native_runtime_setup(void)
566{
567 efi.get_time = virt_efi_get_time;
568 efi.set_time = virt_efi_set_time;
569 efi.get_wakeup_time = virt_efi_get_wakeup_time;
570 efi.set_wakeup_time = virt_efi_set_wakeup_time;
571 efi.get_variable = virt_efi_get_variable;
572 efi.get_next_variable = virt_efi_get_next_variable;
573 efi.set_variable = virt_efi_set_variable;
574 efi.set_variable_nonblocking = virt_efi_set_variable_nb;
575 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
576 efi.reset_system = virt_efi_reset_system;
577 efi.query_variable_info = virt_efi_query_variable_info;
578 efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nb;
579 efi.update_capsule = virt_efi_update_capsule;
580 efi.query_capsule_caps = virt_efi_query_capsule_caps;
581}
582
583#ifdef CONFIG_ACPI_PRMT
584
585efi_status_t
586efi_call_acpi_prm_handler(efi_status_t (__efiapi *handler_addr)(u64, void *),
587 u64 param_buffer_addr, void *context)
588{
589 efi_status_t status;
590
591 if (down_interruptible(&efi_runtime_lock))
592 return EFI_ABORTED;
593 status = efi_queue_work(ACPI_PRM_HANDLER, handler_addr,
594 param_buffer_addr, context);
595 up(&efi_runtime_lock);
596 return status;
597}
598
599#endif
600
601void efi_runtime_assert_lock_held(void)
602{
603 WARN_ON(efi_runtime_lock_owner != current);
604}