4 * Microcode updater interface sysctl
7 #include <kern/locks.h>
8 #include <i386/ucode.h>
10 #include <i386/proc_reg.h>
11 #include <i386/cpuid.h>
12 #include <vm/vm_kern.h>
13 #include <i386/mp.h> // mp_broadcast
14 #include <machine/cpu_number.h> // cpu_number
15 #include <pexpert/pexpert.h> // boot-args
17 #define IA32_BIOS_UPDT_TRIG (0x79) /* microcode update trigger MSR */
19 struct intel_ucupdate
*global_update
= NULL
;
21 /* Exceute the actual update! */
23 update_microcode(void)
25 /* SDM Example 9-8 code shows that we load the
26 * address of the UpdateData within the microcode blob,
27 * not the address of the header.
29 wrmsr64(IA32_BIOS_UPDT_TRIG
, (uint64_t)(uintptr_t)&global_update
->data
);
33 static lck_grp_attr_t
*ucode_slock_grp_attr
= NULL
;
34 static lck_grp_t
*ucode_slock_grp
= NULL
;
35 static lck_attr_t
*ucode_slock_attr
= NULL
;
36 static lck_spin_t
*ucode_slock
= NULL
;
41 /* already allocated? */
42 if (ucode_slock_grp_attr
&& ucode_slock_grp
&& ucode_slock_attr
&& ucode_slock
)
45 /* allocate lock group attribute and group */
46 if (!(ucode_slock_grp_attr
= lck_grp_attr_alloc_init()))
49 lck_grp_attr_setstat(ucode_slock_grp_attr
);
51 if (!(ucode_slock_grp
= lck_grp_alloc_init("uccode_lock", ucode_slock_grp_attr
)))
54 /* Allocate lock attribute */
55 if (!(ucode_slock_attr
= lck_attr_alloc_init()))
58 /* Allocate the spin lock */
59 /* We keep one global spin-lock. We could have one per update
60 * request... but srsly, why would you update microcode like that?
62 if (!(ucode_slock
= lck_spin_alloc_init(ucode_slock_grp
, ucode_slock_attr
)))
70 lck_spin_free(ucode_slock
, ucode_slock_grp
);
72 lck_attr_free(ucode_slock_attr
);
74 lck_grp_free(ucode_slock_grp
);
75 if (ucode_slock_grp_attr
)
76 lck_grp_attr_free(ucode_slock_grp_attr
);
81 /* Copy in an update */
83 copyin_update(uint64_t inaddr
)
85 struct intel_ucupdate update_header
;
86 struct intel_ucupdate
*update
;
91 /* Copy in enough header to peek at the size */
92 error
= copyin((user_addr_t
)inaddr
, (void *)&update_header
, sizeof(update_header
));
96 /* Get the actual, alleged size */
97 size
= update_header
.total_size
;
99 /* huge bogus piece of data that somehow made it through? */
100 if (size
>= 1024 * 1024)
103 /* Old microcodes? */
105 size
= 2048; /* default update size; see SDM */
108 * create the buffer for the update
109 * It need only be aligned to 16-bytes, according to the SDM.
110 * This also wires it down
112 ret
= kmem_alloc_kobject(kernel_map
, (vm_offset_t
*)&update
, size
, VM_KERN_MEMORY_OSFMK
);
113 if (ret
!= KERN_SUCCESS
)
117 error
= copyin((user_addr_t
)inaddr
, (void*)update
, size
);
119 kmem_free(kernel_map
, (vm_offset_t
)update
, size
);
123 global_update
= update
;
128 * This is called once by every CPU on a wake from sleep/hibernate
129 * and is meant to re-apply a microcode update that got lost
136 kprintf("ucode: Re-applying update after wake (CPU #%d)\n", cpu_number());
140 kprintf("ucode: No update to apply (CPU #%d)\n", cpu_number());
146 cpu_update(__unused
void *arg
)
149 lck_spin_lock(ucode_slock
);
151 /* execute the update */
154 /* release the lock */
155 lck_spin_unlock(ucode_slock
);
158 /* Farm an update out to all CPUs */
162 if (register_locks() != KERN_SUCCESS
)
165 /* Get all CPUs to perform the update */
166 mp_broadcast(cpu_update
, NULL
);
168 /* Update the cpuid info */
178 ucode_interface(uint64_t addr
)
183 if (PE_parse_boot_argn("-x", arg
, sizeof (arg
))) {
184 printf("ucode: no updates in safe mode\n");
190 * Userland may only call this once per boot. Anything else
191 * would not make sense (all updates are cumulative), and also
192 * leak memory, because we don't free previous updates.
198 /* Get the whole microcode */
199 error
= copyin_update(addr
);
204 /* Farm out the updates */