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
16 #define IA32_BIOS_UPDT_TRIG (0x79) /* microcode update trigger MSR */
18 struct intel_ucupdate
*global_update
= NULL
;
20 /* Exceute the actual update! */
22 update_microcode(void)
24 /* SDM Example 9-8 code shows that we load the
25 * address of the UpdateData within the microcode blob,
26 * not the address of the header.
28 wrmsr64(IA32_BIOS_UPDT_TRIG
, (uint64_t)(uintptr_t)&global_update
->data
);
32 static lck_grp_attr_t
*ucode_slock_grp_attr
= NULL
;
33 static lck_grp_t
*ucode_slock_grp
= NULL
;
34 static lck_attr_t
*ucode_slock_attr
= NULL
;
35 static lck_spin_t
*ucode_slock
= NULL
;
40 /* already allocated? */
41 if (ucode_slock_grp_attr
&& ucode_slock_grp
&& ucode_slock_attr
&& ucode_slock
)
44 /* allocate lock group attribute and group */
45 if (!(ucode_slock_grp_attr
= lck_grp_attr_alloc_init()))
48 lck_grp_attr_setstat(ucode_slock_grp_attr
);
50 if (!(ucode_slock_grp
= lck_grp_alloc_init("uccode_lock", ucode_slock_grp_attr
)))
53 /* Allocate lock attribute */
54 if (!(ucode_slock_attr
= lck_attr_alloc_init()))
57 /* Allocate the spin lock */
58 /* We keep one global spin-lock. We could have one per update
59 * request... but srsly, why would you update microcode like that?
61 if (!(ucode_slock
= lck_spin_alloc_init(ucode_slock_grp
, ucode_slock_attr
)))
69 lck_spin_free(ucode_slock
, ucode_slock_grp
);
71 lck_attr_free(ucode_slock_attr
);
73 lck_grp_free(ucode_slock_grp
);
74 if (ucode_slock_grp_attr
)
75 lck_grp_attr_free(ucode_slock_grp_attr
);
80 /* Copy in an update */
82 copyin_update(uint64_t inaddr
)
84 struct intel_ucupdate update_header
;
85 struct intel_ucupdate
*update
;
90 /* Copy in enough header to peek at the size */
91 error
= copyin((user_addr_t
)inaddr
, (void *)&update_header
, sizeof(update_header
));
95 /* Get the actual, alleged size */
96 size
= update_header
.total_size
;
98 /* huge bogus piece of data that somehow made it through? */
99 if (size
>= 1024 * 1024)
102 /* Old microcodes? */
104 size
= 2048; /* default update size; see SDM */
107 * create the buffer for the update
108 * It need only be aligned to 16-bytes, according to the SDM.
109 * This also wires it down
111 ret
= kmem_alloc_kobject(kernel_map
, (vm_offset_t
*)&update
, size
);
112 if (ret
!= KERN_SUCCESS
)
116 error
= copyin((user_addr_t
)inaddr
, (void*)update
, size
);
118 kmem_free(kernel_map
, (vm_offset_t
)update
, size
);
122 global_update
= update
;
127 * This is called once by every CPU on a wake from sleep/hibernate
128 * and is meant to re-apply a microcode update that got lost
135 kprintf("ucode: Re-applying update after wake (CPU #%d)\n", cpu_number());
139 kprintf("ucode: No update to apply (CPU #%d)\n", cpu_number());
145 cpu_update(__unused
void *arg
)
148 lck_spin_lock(ucode_slock
);
150 /* execute the update */
153 /* if CPU #0, update global CPU information */
157 /* release the lock */
158 lck_spin_unlock(ucode_slock
);
161 /* Farm an update out to all CPUs */
165 if (register_locks() != KERN_SUCCESS
)
168 /* Get all CPUs to perform the update */
169 mp_broadcast(cpu_update
, NULL
);
177 ucode_interface(uint64_t addr
)
183 * Userland may only call this once per boot. Anything else
184 * would not make sense (all updates are cumulative), and also
185 * leak memory, because we don't free previous updates.
191 /* Get the whole microcode */
192 error
= copyin_update(addr
);
197 /* Farm out the updates */