Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * runtime-wrappers.c - Runtime Services function call wrappers
0004  *
0005  * Implementation summary:
0006  * -----------------------
0007  * 1. When user/kernel thread requests to execute efi_runtime_service(),
0008  * enqueue work to efi_rts_wq.
0009  * 2. Caller thread waits for completion until the work is finished
0010  * because it's dependent on the return status and execution of
0011  * efi_runtime_service().
0012  * For instance, get_variable() and get_next_variable().
0013  *
0014  * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
0015  *
0016  * Split off from arch/x86/platform/efi/efi.c
0017  *
0018  * Copyright (C) 1999 VA Linux Systems
0019  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
0020  * Copyright (C) 1999-2002 Hewlett-Packard Co.
0021  * Copyright (C) 2005-2008 Intel Co.
0022  * Copyright (C) 2013 SuSE Labs
0023  */
0024 
0025 #define pr_fmt(fmt) "efi: " fmt
0026 
0027 #include <linux/bug.h>
0028 #include <linux/efi.h>
0029 #include <linux/irqflags.h>
0030 #include <linux/mutex.h>
0031 #include <linux/semaphore.h>
0032 #include <linux/stringify.h>
0033 #include <linux/workqueue.h>
0034 #include <linux/completion.h>
0035 
0036 #include <asm/efi.h>
0037 
0038 /*
0039  * Wrap around the new efi_call_virt_generic() macros so that the
0040  * code doesn't get too cluttered:
0041  */
0042 #define efi_call_virt(f, args...)   \
0043     efi_call_virt_pointer(efi.runtime, f, args)
0044 #define __efi_call_virt(f, args...) \
0045     __efi_call_virt_pointer(efi.runtime, f, args)
0046 
0047 struct efi_runtime_work efi_rts_work;
0048 
0049 /*
0050  * efi_queue_work:  Queue efi_runtime_service() and wait until it's done
0051  * @rts:        efi_runtime_service() function identifier
0052  * @rts_arg<1-5>:   efi_runtime_service() function arguments
0053  *
0054  * Accesses to efi_runtime_services() are serialized by a binary
0055  * semaphore (efi_runtime_lock) and caller waits until the work is
0056  * finished, hence _only_ one work is queued at a time and the caller
0057  * thread waits for completion.
0058  */
0059 #define efi_queue_work(_rts, _arg1, _arg2, _arg3, _arg4, _arg5)     \
0060 ({                                  \
0061     efi_rts_work.status = EFI_ABORTED;              \
0062                                     \
0063     if (!efi_enabled(EFI_RUNTIME_SERVICES)) {           \
0064         pr_warn_once("EFI Runtime Services are disabled!\n");   \
0065         goto exit;                      \
0066     }                               \
0067                                     \
0068     init_completion(&efi_rts_work.efi_rts_comp);            \
0069     INIT_WORK(&efi_rts_work.work, efi_call_rts);            \
0070     efi_rts_work.arg1 = _arg1;                  \
0071     efi_rts_work.arg2 = _arg2;                  \
0072     efi_rts_work.arg3 = _arg3;                  \
0073     efi_rts_work.arg4 = _arg4;                  \
0074     efi_rts_work.arg5 = _arg5;                  \
0075     efi_rts_work.efi_rts_id = _rts;                 \
0076                                     \
0077     /*                              \
0078      * queue_work() returns 0 if work was already on queue,         \
0079      * _ideally_ this should never happen.                          \
0080      */                             \
0081     if (queue_work(efi_rts_wq, &efi_rts_work.work))         \
0082         wait_for_completion(&efi_rts_work.efi_rts_comp);    \
0083     else                                \
0084         pr_err("Failed to queue work to efi_rts_wq.\n");    \
0085                                     \
0086 exit:                                   \
0087     efi_rts_work.efi_rts_id = EFI_NONE;             \
0088     efi_rts_work.status;                        \
0089 })
0090 
0091 #ifndef arch_efi_save_flags
0092 #define arch_efi_save_flags(state_flags)    local_save_flags(state_flags)
0093 #define arch_efi_restore_flags(state_flags) local_irq_restore(state_flags)
0094 #endif
0095 
0096 unsigned long efi_call_virt_save_flags(void)
0097 {
0098     unsigned long flags;
0099 
0100     arch_efi_save_flags(flags);
0101     return flags;
0102 }
0103 
0104 void efi_call_virt_check_flags(unsigned long flags, const char *call)
0105 {
0106     unsigned long cur_flags, mismatch;
0107 
0108     cur_flags = efi_call_virt_save_flags();
0109 
0110     mismatch = flags ^ cur_flags;
0111     if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
0112         return;
0113 
0114     add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
0115     pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
0116                flags, cur_flags, call);
0117     arch_efi_restore_flags(flags);
0118 }
0119 
0120 /*
0121  * According to section 7.1 of the UEFI spec, Runtime Services are not fully
0122  * reentrant, and there are particular combinations of calls that need to be
0123  * serialized. (source: UEFI Specification v2.4A)
0124  *
0125  * Table 31. Rules for Reentry Into Runtime Services
0126  * +------------------------------------+-------------------------------+
0127  * | If previous call is busy in    | Forbidden to call     |
0128  * +------------------------------------+-------------------------------+
0129  * | Any                | SetVirtualAddressMap()    |
0130  * +------------------------------------+-------------------------------+
0131  * | ConvertPointer()           | ConvertPointer()      |
0132  * +------------------------------------+-------------------------------+
0133  * | SetVariable()          | ResetSystem()         |
0134  * | UpdateCapsule()            |               |
0135  * | SetTime()              |               |
0136  * | SetWakeupTime()            |               |
0137  * | GetNextHighMonotonicCount()    |               |
0138  * +------------------------------------+-------------------------------+
0139  * | GetVariable()          | GetVariable()         |
0140  * | GetNextVariableName()      | GetNextVariableName()     |
0141  * | SetVariable()          | SetVariable()         |
0142  * | QueryVariableInfo()        | QueryVariableInfo()       |
0143  * | UpdateCapsule()            | UpdateCapsule()       |
0144  * | QueryCapsuleCapabilities()     | QueryCapsuleCapabilities()    |
0145  * | GetNextHighMonotonicCount()    | GetNextHighMonotonicCount()   |
0146  * +------------------------------------+-------------------------------+
0147  * | GetTime()              | GetTime()         |
0148  * | SetTime()              | SetTime()         |
0149  * | GetWakeupTime()            | GetWakeupTime()       |
0150  * | SetWakeupTime()            | SetWakeupTime()       |
0151  * +------------------------------------+-------------------------------+
0152  *
0153  * Due to the fact that the EFI pstore may write to the variable store in
0154  * interrupt context, we need to use a lock for at least the groups that
0155  * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
0156  * none of the remaining functions are actually ever called at runtime.
0157  * So let's just use a single lock to serialize all Runtime Services calls.
0158  */
0159 static DEFINE_SEMAPHORE(efi_runtime_lock);
0160 
0161 /*
0162  * Expose the EFI runtime lock to the UV platform
0163  */
0164 #ifdef CONFIG_X86_UV
0165 extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
0166 #endif
0167 
0168 /*
0169  * Calls the appropriate efi_runtime_service() with the appropriate
0170  * arguments.
0171  *
0172  * Semantics followed by efi_call_rts() to understand efi_runtime_work:
0173  * 1. If argument was a pointer, recast it from void pointer to original
0174  * pointer type.
0175  * 2. If argument was a value, recast it from void pointer to original
0176  * pointer type and dereference it.
0177  */
0178 static void efi_call_rts(struct work_struct *work)
0179 {
0180     void *arg1, *arg2, *arg3, *arg4, *arg5;
0181     efi_status_t status = EFI_NOT_FOUND;
0182 
0183     arg1 = efi_rts_work.arg1;
0184     arg2 = efi_rts_work.arg2;
0185     arg3 = efi_rts_work.arg3;
0186     arg4 = efi_rts_work.arg4;
0187     arg5 = efi_rts_work.arg5;
0188 
0189     switch (efi_rts_work.efi_rts_id) {
0190     case EFI_GET_TIME:
0191         status = efi_call_virt(get_time, (efi_time_t *)arg1,
0192                        (efi_time_cap_t *)arg2);
0193         break;
0194     case EFI_SET_TIME:
0195         status = efi_call_virt(set_time, (efi_time_t *)arg1);
0196         break;
0197     case EFI_GET_WAKEUP_TIME:
0198         status = efi_call_virt(get_wakeup_time, (efi_bool_t *)arg1,
0199                        (efi_bool_t *)arg2, (efi_time_t *)arg3);
0200         break;
0201     case EFI_SET_WAKEUP_TIME:
0202         status = efi_call_virt(set_wakeup_time, *(efi_bool_t *)arg1,
0203                        (efi_time_t *)arg2);
0204         break;
0205     case EFI_GET_VARIABLE:
0206         status = efi_call_virt(get_variable, (efi_char16_t *)arg1,
0207                        (efi_guid_t *)arg2, (u32 *)arg3,
0208                        (unsigned long *)arg4, (void *)arg5);
0209         break;
0210     case EFI_GET_NEXT_VARIABLE:
0211         status = efi_call_virt(get_next_variable, (unsigned long *)arg1,
0212                        (efi_char16_t *)arg2,
0213                        (efi_guid_t *)arg3);
0214         break;
0215     case EFI_SET_VARIABLE:
0216         status = efi_call_virt(set_variable, (efi_char16_t *)arg1,
0217                        (efi_guid_t *)arg2, *(u32 *)arg3,
0218                        *(unsigned long *)arg4, (void *)arg5);
0219         break;
0220     case EFI_QUERY_VARIABLE_INFO:
0221         status = efi_call_virt(query_variable_info, *(u32 *)arg1,
0222                        (u64 *)arg2, (u64 *)arg3, (u64 *)arg4);
0223         break;
0224     case EFI_GET_NEXT_HIGH_MONO_COUNT:
0225         status = efi_call_virt(get_next_high_mono_count, (u32 *)arg1);
0226         break;
0227     case EFI_UPDATE_CAPSULE:
0228         status = efi_call_virt(update_capsule,
0229                        (efi_capsule_header_t **)arg1,
0230                        *(unsigned long *)arg2,
0231                        *(unsigned long *)arg3);
0232         break;
0233     case EFI_QUERY_CAPSULE_CAPS:
0234         status = efi_call_virt(query_capsule_caps,
0235                        (efi_capsule_header_t **)arg1,
0236                        *(unsigned long *)arg2, (u64 *)arg3,
0237                        (int *)arg4);
0238         break;
0239     default:
0240         /*
0241          * Ideally, we should never reach here because a caller of this
0242          * function should have put the right efi_runtime_service()
0243          * function identifier into efi_rts_work->efi_rts_id
0244          */
0245         pr_err("Requested executing invalid EFI Runtime Service.\n");
0246     }
0247     efi_rts_work.status = status;
0248     complete(&efi_rts_work.efi_rts_comp);
0249 }
0250 
0251 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
0252 {
0253     efi_status_t status;
0254 
0255     if (down_interruptible(&efi_runtime_lock))
0256         return EFI_ABORTED;
0257     status = efi_queue_work(EFI_GET_TIME, tm, tc, NULL, NULL, NULL);
0258     up(&efi_runtime_lock);
0259     return status;
0260 }
0261 
0262 static efi_status_t virt_efi_set_time(efi_time_t *tm)
0263 {
0264     efi_status_t status;
0265 
0266     if (down_interruptible(&efi_runtime_lock))
0267         return EFI_ABORTED;
0268     status = efi_queue_work(EFI_SET_TIME, tm, NULL, NULL, NULL, NULL);
0269     up(&efi_runtime_lock);
0270     return status;
0271 }
0272 
0273 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
0274                          efi_bool_t *pending,
0275                          efi_time_t *tm)
0276 {
0277     efi_status_t status;
0278 
0279     if (down_interruptible(&efi_runtime_lock))
0280         return EFI_ABORTED;
0281     status = efi_queue_work(EFI_GET_WAKEUP_TIME, enabled, pending, tm, NULL,
0282                 NULL);
0283     up(&efi_runtime_lock);
0284     return status;
0285 }
0286 
0287 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
0288 {
0289     efi_status_t status;
0290 
0291     if (down_interruptible(&efi_runtime_lock))
0292         return EFI_ABORTED;
0293     status = efi_queue_work(EFI_SET_WAKEUP_TIME, &enabled, tm, NULL, NULL,
0294                 NULL);
0295     up(&efi_runtime_lock);
0296     return status;
0297 }
0298 
0299 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
0300                       efi_guid_t *vendor,
0301                       u32 *attr,
0302                       unsigned long *data_size,
0303                       void *data)
0304 {
0305     efi_status_t status;
0306 
0307     if (down_interruptible(&efi_runtime_lock))
0308         return EFI_ABORTED;
0309     status = efi_queue_work(EFI_GET_VARIABLE, name, vendor, attr, data_size,
0310                 data);
0311     up(&efi_runtime_lock);
0312     return status;
0313 }
0314 
0315 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
0316                            efi_char16_t *name,
0317                            efi_guid_t *vendor)
0318 {
0319     efi_status_t status;
0320 
0321     if (down_interruptible(&efi_runtime_lock))
0322         return EFI_ABORTED;
0323     status = efi_queue_work(EFI_GET_NEXT_VARIABLE, name_size, name, vendor,
0324                 NULL, NULL);
0325     up(&efi_runtime_lock);
0326     return status;
0327 }
0328 
0329 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
0330                       efi_guid_t *vendor,
0331                       u32 attr,
0332                       unsigned long data_size,
0333                       void *data)
0334 {
0335     efi_status_t status;
0336 
0337     if (down_interruptible(&efi_runtime_lock))
0338         return EFI_ABORTED;
0339     status = efi_queue_work(EFI_SET_VARIABLE, name, vendor, &attr, &data_size,
0340                 data);
0341     up(&efi_runtime_lock);
0342     return status;
0343 }
0344 
0345 static efi_status_t
0346 virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
0347                   u32 attr, unsigned long data_size,
0348                   void *data)
0349 {
0350     efi_status_t status;
0351 
0352     if (down_trylock(&efi_runtime_lock))
0353         return EFI_NOT_READY;
0354 
0355     status = efi_call_virt(set_variable, name, vendor, attr, data_size,
0356                    data);
0357     up(&efi_runtime_lock);
0358     return status;
0359 }
0360 
0361 
0362 static efi_status_t virt_efi_query_variable_info(u32 attr,
0363                          u64 *storage_space,
0364                          u64 *remaining_space,
0365                          u64 *max_variable_size)
0366 {
0367     efi_status_t status;
0368 
0369     if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
0370         return EFI_UNSUPPORTED;
0371 
0372     if (down_interruptible(&efi_runtime_lock))
0373         return EFI_ABORTED;
0374     status = efi_queue_work(EFI_QUERY_VARIABLE_INFO, &attr, storage_space,
0375                 remaining_space, max_variable_size, NULL);
0376     up(&efi_runtime_lock);
0377     return status;
0378 }
0379 
0380 static efi_status_t
0381 virt_efi_query_variable_info_nonblocking(u32 attr,
0382                      u64 *storage_space,
0383                      u64 *remaining_space,
0384                      u64 *max_variable_size)
0385 {
0386     efi_status_t status;
0387 
0388     if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
0389         return EFI_UNSUPPORTED;
0390 
0391     if (down_trylock(&efi_runtime_lock))
0392         return EFI_NOT_READY;
0393 
0394     status = efi_call_virt(query_variable_info, attr, storage_space,
0395                    remaining_space, max_variable_size);
0396     up(&efi_runtime_lock);
0397     return status;
0398 }
0399 
0400 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
0401 {
0402     efi_status_t status;
0403 
0404     if (down_interruptible(&efi_runtime_lock))
0405         return EFI_ABORTED;
0406     status = efi_queue_work(EFI_GET_NEXT_HIGH_MONO_COUNT, count, NULL, NULL,
0407                 NULL, NULL);
0408     up(&efi_runtime_lock);
0409     return status;
0410 }
0411 
0412 static void virt_efi_reset_system(int reset_type,
0413                   efi_status_t status,
0414                   unsigned long data_size,
0415                   efi_char16_t *data)
0416 {
0417     if (down_trylock(&efi_runtime_lock)) {
0418         pr_warn("failed to invoke the reset_system() runtime service:\n"
0419             "could not get exclusive access to the firmware\n");
0420         return;
0421     }
0422     efi_rts_work.efi_rts_id = EFI_RESET_SYSTEM;
0423     __efi_call_virt(reset_system, reset_type, status, data_size, data);
0424     up(&efi_runtime_lock);
0425 }
0426 
0427 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
0428                         unsigned long count,
0429                         unsigned long sg_list)
0430 {
0431     efi_status_t status;
0432 
0433     if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
0434         return EFI_UNSUPPORTED;
0435 
0436     if (down_interruptible(&efi_runtime_lock))
0437         return EFI_ABORTED;
0438     status = efi_queue_work(EFI_UPDATE_CAPSULE, capsules, &count, &sg_list,
0439                 NULL, NULL);
0440     up(&efi_runtime_lock);
0441     return status;
0442 }
0443 
0444 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
0445                         unsigned long count,
0446                         u64 *max_size,
0447                         int *reset_type)
0448 {
0449     efi_status_t status;
0450 
0451     if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
0452         return EFI_UNSUPPORTED;
0453 
0454     if (down_interruptible(&efi_runtime_lock))
0455         return EFI_ABORTED;
0456     status = efi_queue_work(EFI_QUERY_CAPSULE_CAPS, capsules, &count,
0457                 max_size, reset_type, NULL);
0458     up(&efi_runtime_lock);
0459     return status;
0460 }
0461 
0462 void efi_native_runtime_setup(void)
0463 {
0464     efi.get_time = virt_efi_get_time;
0465     efi.set_time = virt_efi_set_time;
0466     efi.get_wakeup_time = virt_efi_get_wakeup_time;
0467     efi.set_wakeup_time = virt_efi_set_wakeup_time;
0468     efi.get_variable = virt_efi_get_variable;
0469     efi.get_next_variable = virt_efi_get_next_variable;
0470     efi.set_variable = virt_efi_set_variable;
0471     efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
0472     efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
0473     efi.reset_system = virt_efi_reset_system;
0474     efi.query_variable_info = virt_efi_query_variable_info;
0475     efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
0476     efi.update_capsule = virt_efi_update_capsule;
0477     efi.query_capsule_caps = virt_efi_query_capsule_caps;
0478 }