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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
#include "efi.h"
#include "runtime.h"
#include <xen/cache.h>
#include <xen/errno.h>
#include <xen/guest_access.h>
#include <xen/irq.h>
#include <xen/time.h>
DEFINE_XEN_GUEST_HANDLE(CHAR16);
struct efi_rs_state {
#ifdef CONFIG_X86
/*
* The way stacks get set up leads to them always being on an 8-byte
* boundary not evenly divisible by 16 (see asm/current.h). The EFI ABI,
* just like the CPU one, however requires stacks to be 16-byte aligned
* before every function call. Since the compiler assumes this (unless
* passing it -mpreferred-stack-boundary=3), it wouldn't generate code to
* align the stack to 16 bytes even if putting a 16-byte aligned object
* there. Hence we need to force larger than 16-byte alignment, even if we
* don't strictly need that.
*/
unsigned long __aligned(32) cr3;
unsigned long msr_s_cet;
#endif
};
struct efi_rs_state efi_rs_enter(void);
void efi_rs_leave(struct efi_rs_state *);
#ifndef COMPAT
#ifndef CONFIG_ARM
# include <asm/i387.h>
# include <asm/xstate.h>
# include <public/platform.h>
#endif
unsigned int __read_mostly efi_num_ct;
const EFI_CONFIGURATION_TABLE *__read_mostly efi_ct;
unsigned int __read_mostly efi_version;
unsigned int __read_mostly efi_fw_revision;
const CHAR16 *__read_mostly efi_fw_vendor;
const EFI_RUNTIME_SERVICES *__read_mostly efi_rs;
#ifndef CONFIG_ARM /* TODO - disabled until implemented on ARM */
static DEFINE_SPINLOCK(efi_rs_lock);
static unsigned int efi_rs_on_cpu = NR_CPUS;
#endif
UINTN __read_mostly efi_memmap_size;
UINTN __read_mostly efi_mdesc_size;
void *__read_mostly efi_memmap;
UINT64 __read_mostly efi_boot_max_var_store_size;
UINT64 __read_mostly efi_boot_remain_var_store_size;
UINT64 __read_mostly efi_boot_max_var_size;
UINT64 __read_mostly efi_apple_properties_addr;
UINTN __read_mostly efi_apple_properties_len;
/* Bit field representing available EFI features/properties. */
unsigned int efi_flags;
struct efi __read_mostly efi = {
.acpi = EFI_INVALID_TABLE_ADDR,
.acpi20 = EFI_INVALID_TABLE_ADDR,
.mps = EFI_INVALID_TABLE_ADDR,
.smbios = EFI_INVALID_TABLE_ADDR,
.smbios3 = EFI_INVALID_TABLE_ADDR,
};
const struct efi_pci_rom *__read_mostly efi_pci_roms;
bool efi_enabled(unsigned int feature)
{
return test_bit(feature, &efi_flags);
}
#ifndef CONFIG_ARM /* TODO - disabled until implemented on ARM */
struct efi_rs_state efi_rs_enter(void)
{
static const u16 fcw = FCW_DEFAULT;
static const u32 mxcsr = MXCSR_DEFAULT;
struct efi_rs_state state = { .cr3 = 0 };
if ( mfn_eq(efi_l4_mfn, INVALID_MFN) )
return state;
state.cr3 = read_cr3();
save_fpu_enable();
asm volatile ( "fnclex; fldcw %0" :: "m" (fcw) );
asm volatile ( "ldmxcsr %0" :: "m" (mxcsr) );
spin_lock(&efi_rs_lock);
efi_rs_on_cpu = smp_processor_id();
/* prevent fixup_page_fault() from doing anything */
irq_enter();
if ( is_pv_vcpu(current) && !is_idle_vcpu(current) )
{
struct desc_ptr gdt_desc = {
.limit = LAST_RESERVED_GDT_BYTE,
.base = (unsigned long)(per_cpu(gdt, smp_processor_id()) -
FIRST_RESERVED_GDT_ENTRY)
};
lgdt(&gdt_desc);
}
switch_cr3_cr4(mfn_to_maddr(efi_l4_mfn), read_cr4());
/*
* At the time of writing (2022), no UEFI firwmare is CET-IBT compatible.
* Work is under way to remedy this.
*
* Stash MSR_S_CET and clobber ENDBR_EN. This is necessary because
* SHSTK_EN isn't configured until very late on the BSP.
*/
if ( cpu_has_xen_ibt )
{
rdmsrl(MSR_S_CET, state.msr_s_cet);
wrmsrl(MSR_S_CET, state.msr_s_cet & ~CET_ENDBR_EN);
}
return state;
}
void efi_rs_leave(struct efi_rs_state *state)
{
struct vcpu *curr = current;
if ( !state->cr3 )
return;
if ( state->msr_s_cet )
wrmsrl(MSR_S_CET, state->msr_s_cet);
switch_cr3_cr4(state->cr3, read_cr4());
if ( is_pv_vcpu(curr) && !is_idle_vcpu(curr) )
{
struct desc_ptr gdt_desc = {
.limit = LAST_RESERVED_GDT_BYTE,
.base = GDT_VIRT_START(curr)
};
lgdt(&gdt_desc);
}
irq_exit();
efi_rs_on_cpu = NR_CPUS;
spin_unlock(&efi_rs_lock);
vcpu_restore_fpu_nonlazy(curr, true);
}
bool efi_rs_using_pgtables(void)
{
return !mfn_eq(efi_l4_mfn, INVALID_MFN) &&
(smp_processor_id() == efi_rs_on_cpu) &&
(read_cr3() == mfn_to_maddr(efi_l4_mfn));
}
unsigned long efi_get_time(void)
{
EFI_TIME time;
EFI_STATUS status;
struct efi_rs_state state = efi_rs_enter();
unsigned long flags;
if ( !state.cr3 )
return 0;
spin_lock_irqsave(&rtc_lock, flags);
status = efi_rs->GetTime(&time, NULL);
spin_unlock_irqrestore(&rtc_lock, flags);
efi_rs_leave(&state);
if ( EFI_ERROR(status) )
return 0;
return mktime(time.Year, time.Month, time.Day,
time.Hour, time.Minute, time.Second);
}
void efi_halt_system(void)
{
EFI_STATUS status;
struct efi_rs_state state = efi_rs_enter();
if ( !state.cr3 )
return;
status = efi_rs->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
efi_rs_leave(&state);
printk(XENLOG_WARNING "EFI: could not halt system (%#lx)\n", status);
}
void efi_reset_system(bool warm)
{
EFI_STATUS status;
struct efi_rs_state state = efi_rs_enter();
if ( !state.cr3 )
return;
status = efi_rs->ResetSystem(warm ? EfiResetWarm : EfiResetCold,
EFI_SUCCESS, 0, NULL);
efi_rs_leave(&state);
printk(XENLOG_WARNING "EFI: could not reset system (%#lx)\n", status);
}
#endif /* CONFIG_ARM */
const CHAR16 *wmemchr(const CHAR16 *s, CHAR16 c, UINTN n)
{
while ( n && *s != c )
{
--n;
++s;
}
return n ? s : NULL;
}
#endif /* COMPAT */
#ifndef CONFIG_ARM /* TODO - disabled until implemented on ARM */
int efi_get_info(uint32_t idx, union xenpf_efi_info *info)
{
unsigned int i, n;
if ( !efi_enabled(EFI_BOOT) )
return -ENOSYS;
switch ( idx )
{
case XEN_FW_EFI_VERSION:
info->version = efi_version;
break;
case XEN_FW_EFI_RT_VERSION:
{
if ( !efi_enabled(EFI_RS) )
return -EOPNOTSUPP;
info->version = efi_rs->Hdr.Revision;
break;
}
case XEN_FW_EFI_CONFIG_TABLE:
info->cfg.addr = __pa(efi_ct);
info->cfg.nent = efi_num_ct;
break;
case XEN_FW_EFI_VENDOR:
if ( !efi_fw_vendor )
return -EOPNOTSUPP;
info->vendor.revision = efi_fw_revision;
n = info->vendor.bufsz / sizeof(*efi_fw_vendor);
if ( !guest_handle_okay(guest_handle_cast(info->vendor.name,
CHAR16), n) )
return -EFAULT;
for ( i = 0; i < n; ++i )
{
if ( __copy_to_guest_offset(info->vendor.name, i,
efi_fw_vendor + i, 1) )
return -EFAULT;
if ( !efi_fw_vendor[i] )
break;
}
break;
case XEN_FW_EFI_MEM_INFO:
for ( i = 0; i < efi_memmap_size; i += efi_mdesc_size )
{
EFI_MEMORY_DESCRIPTOR *desc = efi_memmap + i;
u64 len = desc->NumberOfPages << EFI_PAGE_SHIFT;
if ( info->mem.addr >= desc->PhysicalStart &&
info->mem.addr < desc->PhysicalStart + len )
{
info->mem.type = desc->Type;
info->mem.attr = desc->Attribute;
if ( info->mem.addr + info->mem.size < info->mem.addr ||
info->mem.addr + info->mem.size >
desc->PhysicalStart + len )
info->mem.size = desc->PhysicalStart + len -
info->mem.addr;
return 0;
}
}
return -ESRCH;
case XEN_FW_EFI_PCI_ROM: {
const struct efi_pci_rom *ent;
for ( ent = efi_pci_roms; ent; ent = ent->next )
if ( info->pci_rom.segment == ent->segment &&
info->pci_rom.bus == ent->bus &&
info->pci_rom.devfn == ent->devfn &&
info->pci_rom.vendor == ent->vendor &&
info->pci_rom.devid == ent->devid )
{
info->pci_rom.address = __pa(ent->data);
info->pci_rom.size = ent->size;
return 0;
}
return -ESRCH;
}
case XEN_FW_EFI_APPLE_PROPERTIES:
if ( !efi_apple_properties_len )
return -ENODATA;
info->apple_properties.address = efi_apple_properties_addr;
info->apple_properties.size = efi_apple_properties_len;
break;
default:
return -EINVAL;
}
return 0;
}
static long gwstrlen(XEN_GUEST_HANDLE_PARAM(CHAR16) str)
{
unsigned long len;
for ( len = 0; ; ++len )
{
CHAR16 c;
if ( copy_from_guest_offset(&c, str, len, 1) )
return -EFAULT;
if ( !c )
break;
}
return len;
}
static inline EFI_TIME *cast_time(struct xenpf_efi_time *time)
{
#define chk_fld(F, f) \
BUILD_BUG_ON(sizeof(cast_time(NULL)->F) != sizeof(time->f) || \
offsetof(EFI_TIME, F) != offsetof(struct xenpf_efi_time, f))
chk_fld(Year, year);
chk_fld(Month, month);
chk_fld(Day, day);
chk_fld(Hour, hour);
chk_fld(Minute, min);
chk_fld(Second, sec);
chk_fld(Nanosecond, ns);
chk_fld(TimeZone, tz);
chk_fld(Daylight, daylight);
#undef chk_fld
return (void *)time;
}
static inline EFI_GUID *cast_guid(struct xenpf_efi_guid *guid)
{
#define chk_fld(n) \
BUILD_BUG_ON(sizeof(cast_guid(NULL)->Data##n) != sizeof(guid->data##n) || \
offsetof(EFI_GUID, Data##n) != \
offsetof(struct xenpf_efi_guid, data##n))
chk_fld(1);
chk_fld(2);
chk_fld(3);
chk_fld(4);
#undef chk_fld
return (void *)guid;
}
int efi_runtime_call(struct xenpf_efi_runtime_call *op)
{
struct efi_rs_state state;
unsigned long flags;
EFI_STATUS status = EFI_NOT_STARTED;
int rc = 0;
if ( !efi_enabled(EFI_BOOT) )
return -ENOSYS;
if ( !efi_enabled(EFI_RS) )
return -EOPNOTSUPP;
switch ( op->function )
{
case XEN_EFI_get_time:
{
EFI_TIME_CAPABILITIES caps;
if ( op->misc )
return -EINVAL;
state = efi_rs_enter();
if ( !state.cr3 )
return -EOPNOTSUPP;
spin_lock_irqsave(&rtc_lock, flags);
status = efi_rs->GetTime(cast_time(&op->u.get_time.time), &caps);
spin_unlock_irqrestore(&rtc_lock, flags);
efi_rs_leave(&state);
if ( !EFI_ERROR(status) )
{
op->u.get_time.resolution = caps.Resolution;
op->u.get_time.accuracy = caps.Accuracy;
if ( caps.SetsToZero )
op->misc = XEN_EFI_GET_TIME_SET_CLEARS_NS;
}
}
break;
case XEN_EFI_set_time:
if ( op->misc )
return -EINVAL;
state = efi_rs_enter();
if ( !state.cr3 )
return -EOPNOTSUPP;
spin_lock_irqsave(&rtc_lock, flags);
status = efi_rs->SetTime(cast_time(&op->u.set_time));
spin_unlock_irqrestore(&rtc_lock, flags);
efi_rs_leave(&state);
break;
case XEN_EFI_get_wakeup_time:
{
BOOLEAN enabled, pending;
if ( op->misc )
return -EINVAL;
state = efi_rs_enter();
if ( !state.cr3 )
return -EOPNOTSUPP;
spin_lock_irqsave(&rtc_lock, flags);
status = efi_rs->GetWakeupTime(&enabled, &pending,
cast_time(&op->u.get_wakeup_time));
spin_unlock_irqrestore(&rtc_lock, flags);
efi_rs_leave(&state);
if ( !EFI_ERROR(status) )
{
if ( enabled )
op->misc |= XEN_EFI_GET_WAKEUP_TIME_ENABLED;
if ( pending )
op->misc |= XEN_EFI_GET_WAKEUP_TIME_PENDING;
}
}
break;
case XEN_EFI_set_wakeup_time:
if ( op->misc & ~(XEN_EFI_SET_WAKEUP_TIME_ENABLE |
XEN_EFI_SET_WAKEUP_TIME_ENABLE_ONLY) )
return -EINVAL;
state = efi_rs_enter();
if ( !state.cr3 )
return -EOPNOTSUPP;
spin_lock_irqsave(&rtc_lock, flags);
status = efi_rs->SetWakeupTime(!!(op->misc &
XEN_EFI_SET_WAKEUP_TIME_ENABLE),
(op->misc &
XEN_EFI_SET_WAKEUP_TIME_ENABLE_ONLY) ?
NULL :
cast_time(&op->u.set_wakeup_time));
spin_unlock_irqrestore(&rtc_lock, flags);
efi_rs_leave(&state);
op->misc = 0;
break;
case XEN_EFI_get_next_high_monotonic_count:
if ( op->misc )
return -EINVAL;
state = efi_rs_enter();
if ( state.cr3 )
status = efi_rs->GetNextHighMonotonicCount(&op->misc);
else
rc = -EOPNOTSUPP;
efi_rs_leave(&state);
break;
case XEN_EFI_get_variable:
{
CHAR16 *name;
long len;
unsigned char *data;
UINTN size;
if ( op->misc )
return -EINVAL;
len = gwstrlen(guest_handle_cast(op->u.get_variable.name, CHAR16));
if ( len < 0 )
return len;
name = xmalloc_array(CHAR16, ++len);
if ( !name )
return -ENOMEM;
if ( __copy_from_guest(name, op->u.get_variable.name, len) ||
wmemchr(name, 0, len) != name + len - 1 )
{
xfree(name);
return -EIO;
}
size = op->u.get_variable.size;
if ( size )
{
data = xmalloc_bytes(size);
if ( !data )
{
xfree(name);
return -ENOMEM;
}
}
else
data = NULL;
state = efi_rs_enter();
if ( state.cr3 )
{
status = efi_rs->GetVariable(
name, cast_guid(&op->u.get_variable.vendor_guid),
&op->misc, &size, data);
efi_rs_leave(&state);
if ( !EFI_ERROR(status) &&
copy_to_guest(op->u.get_variable.data, data, size) )
rc = -EFAULT;
op->u.get_variable.size = size;
}
else
rc = -EOPNOTSUPP;
xfree(data);
xfree(name);
}
break;
case XEN_EFI_set_variable:
{
CHAR16 *name;
long len;
unsigned char *data;
len = gwstrlen(guest_handle_cast(op->u.set_variable.name, CHAR16));
if ( len < 0 )
return len;
name = xmalloc_array(CHAR16, ++len);
if ( !name )
return -ENOMEM;
if ( __copy_from_guest(name, op->u.set_variable.name, len) ||
wmemchr(name, 0, len) != name + len - 1 )
{
xfree(name);
return -EIO;
}
data = xmalloc_bytes(op->u.set_variable.size);
if ( !data )
rc = -ENOMEM;
else if ( copy_from_guest(data, op->u.set_variable.data,
op->u.set_variable.size) )
rc = -EFAULT;
else
{
state = efi_rs_enter();
if ( state.cr3 )
status = efi_rs->SetVariable(
name, cast_guid(&op->u.set_variable.vendor_guid),
op->misc, op->u.set_variable.size, data);
else
rc = -EOPNOTSUPP;
efi_rs_leave(&state);
}
xfree(data);
xfree(name);
}
break;
case XEN_EFI_get_next_variable_name:
{
union {
CHAR16 *str;
unsigned char *raw;
} name;
UINTN size;
if ( op->misc )
return -EINVAL;
size = op->u.get_next_variable_name.size;
name.raw = xzalloc_bytes(size);
if ( !name.raw )
return -ENOMEM;
if ( copy_from_guest(name.raw, op->u.get_next_variable_name.name,
size) )
{
xfree(name.raw);
return -EFAULT;
}
state = efi_rs_enter();
if ( state.cr3 )
{
status = efi_rs->GetNextVariableName(
&size, name.str,
cast_guid(&op->u.get_next_variable_name.vendor_guid));
efi_rs_leave(&state);
/*
* Copy the variable name if necessary. The caller provided size
* is used because some firmwares update size when they shouldn't.
* */
if ( !EFI_ERROR(status) &&
__copy_to_guest(op->u.get_next_variable_name.name,
name.raw, op->u.get_next_variable_name.size) )
rc = -EFAULT;
op->u.get_next_variable_name.size = size;
}
else
rc = -EOPNOTSUPP;
xfree(name.raw);
}
break;
case XEN_EFI_query_variable_info:
{
/*
* Put OUT variables on the stack to make them 8 byte aligned when
* called from the compat handler, as their placement in
* compat_pf_efi_runtime_call will make them 4 byte aligned instead
* and compilers may validly complain. This is done regardless of
* whether called from the compat handler or not, as it's not worth
* the extra logic to differentiate.
*/
uint64_t max_store_size = 0, remain_store_size = 0, max_size = 0;
if ( op->misc & ~XEN_EFI_VARINFO_BOOT_SNAPSHOT )
return -EINVAL;
if ( op->misc & XEN_EFI_VARINFO_BOOT_SNAPSHOT )
{
if ( (op->u.query_variable_info.attr
& ~EFI_VARIABLE_APPEND_WRITE) !=
(EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS |
EFI_VARIABLE_RUNTIME_ACCESS) )
return -EINVAL;
op->u.query_variable_info.max_store_size =
efi_boot_max_var_store_size;
op->u.query_variable_info.remain_store_size =
efi_boot_remain_var_store_size;
if ( efi_boot_max_var_store_size )
{
op->u.query_variable_info.max_size = efi_boot_max_var_size;
status = EFI_SUCCESS;
}
else
{
op->u.query_variable_info.max_size = 0;
status = efi_boot_max_var_size;
}
break;
}
if ( !efi_enabled(EFI_RS) || (efi_rs->Hdr.Revision >> 16) < 2 )
return -EOPNOTSUPP;
state = efi_rs_enter();
if ( !state.cr3 )
return -EOPNOTSUPP;
status = efi_rs->QueryVariableInfo(
op->u.query_variable_info.attr, &max_store_size, &remain_store_size,
&max_size);
efi_rs_leave(&state);
op->u.query_variable_info.max_store_size = max_store_size;
op->u.query_variable_info.remain_store_size = remain_store_size;
op->u.query_variable_info.max_size = max_size;
break;
}
case XEN_EFI_query_capsule_capabilities:
case XEN_EFI_update_capsule:
if ( op->misc )
return -EINVAL;
if ( !efi_enabled(EFI_RS) || (efi_rs->Hdr.Revision >> 16) < 2 )
return -EOPNOTSUPP;
/* XXX fall through for now */
default:
return -ENOSYS;
}
#ifndef COMPAT
op->status = status;
#else
op->status = (status & 0x3fffffff) | ((status >> 32) & 0xc0000000);
#endif
return rc;
}
#endif
|