2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 * Microcode updater interface sysctl
34 #include <kern/locks.h>
35 #include <i386/ucode.h>
36 #include <sys/errno.h>
37 #include <i386/proc_reg.h>
38 #include <i386/cpuid.h>
39 #include <vm/vm_kern.h>
40 #include <i386/mp.h> // mp_broadcast
41 #include <machine/cpu_number.h> // cpu_number
42 #include <pexpert/pexpert.h> // boot-args
44 #define IA32_BIOS_UPDT_TRIG (0x79) /* microcode update trigger MSR */
46 struct intel_ucupdate
*global_update
= NULL
;
48 /* Exceute the actual update! */
50 update_microcode(void)
52 /* SDM Example 9-8 code shows that we load the
53 * address of the UpdateData within the microcode blob,
54 * not the address of the header.
56 wrmsr64(IA32_BIOS_UPDT_TRIG
, (uint64_t)(uintptr_t)&global_update
->data
);
60 static lck_grp_attr_t
*ucode_slock_grp_attr
= NULL
;
61 static lck_grp_t
*ucode_slock_grp
= NULL
;
62 static lck_attr_t
*ucode_slock_attr
= NULL
;
63 static lck_spin_t
*ucode_slock
= NULL
;
68 /* already allocated? */
69 if (ucode_slock_grp_attr
&& ucode_slock_grp
&& ucode_slock_attr
&& ucode_slock
)
72 /* allocate lock group attribute and group */
73 if (!(ucode_slock_grp_attr
= lck_grp_attr_alloc_init()))
76 lck_grp_attr_setstat(ucode_slock_grp_attr
);
78 if (!(ucode_slock_grp
= lck_grp_alloc_init("uccode_lock", ucode_slock_grp_attr
)))
81 /* Allocate lock attribute */
82 if (!(ucode_slock_attr
= lck_attr_alloc_init()))
85 /* Allocate the spin lock */
86 /* We keep one global spin-lock. We could have one per update
87 * request... but srsly, why would you update microcode like that?
89 if (!(ucode_slock
= lck_spin_alloc_init(ucode_slock_grp
, ucode_slock_attr
)))
97 lck_spin_free(ucode_slock
, ucode_slock_grp
);
99 lck_attr_free(ucode_slock_attr
);
101 lck_grp_free(ucode_slock_grp
);
102 if (ucode_slock_grp_attr
)
103 lck_grp_attr_free(ucode_slock_grp_attr
);
105 return KERN_NO_SPACE
;
108 /* Copy in an update */
110 copyin_update(uint64_t inaddr
)
112 struct intel_ucupdate update_header
;
113 struct intel_ucupdate
*update
;
118 /* Copy in enough header to peek at the size */
119 error
= copyin((user_addr_t
)inaddr
, (void *)&update_header
, sizeof(update_header
));
123 /* Get the actual, alleged size */
124 size
= update_header
.total_size
;
126 /* huge bogus piece of data that somehow made it through? */
127 if (size
>= 1024 * 1024)
130 /* Old microcodes? */
132 size
= 2048; /* default update size; see SDM */
135 * create the buffer for the update
136 * It need only be aligned to 16-bytes, according to the SDM.
137 * This also wires it down
139 ret
= kmem_alloc_kobject(kernel_map
, (vm_offset_t
*)&update
, size
, VM_KERN_MEMORY_OSFMK
);
140 if (ret
!= KERN_SUCCESS
)
144 error
= copyin((user_addr_t
)inaddr
, (void*)update
, size
);
146 kmem_free(kernel_map
, (vm_offset_t
)update
, size
);
150 global_update
= update
;
155 * This is called once by every CPU on a wake from sleep/hibernate
156 * and is meant to re-apply a microcode update that got lost
163 kprintf("ucode: Re-applying update after wake (CPU #%d)\n", cpu_number());
167 kprintf("ucode: No update to apply (CPU #%d)\n", cpu_number());
173 cpu_update(__unused
void *arg
)
176 lck_spin_lock(ucode_slock
);
178 /* execute the update */
181 /* release the lock */
182 lck_spin_unlock(ucode_slock
);
185 /* Farm an update out to all CPUs */
189 if (register_locks() != KERN_SUCCESS
)
192 /* Get all CPUs to perform the update */
193 mp_broadcast(cpu_update
, NULL
);
195 /* Update the cpuid info */
205 ucode_interface(uint64_t addr
)
210 if (PE_parse_boot_argn("-x", arg
, sizeof (arg
))) {
211 printf("ucode: no updates in safe mode\n");
217 * Userland may only call this once per boot. Anything else
218 * would not make sense (all updates are cumulative), and also
219 * leak memory, because we don't free previous updates.
225 /* Get the whole microcode */
226 error
= copyin_update(addr
);
231 /* Farm out the updates */