Skip to content

Commit 4da2131

Browse files
KAGA-KOKObp3tk0v
authored andcommitted
x86/microcode/intel: Move microcode functions out of cpu/intel.c
There is really no point to have that in the CPUID evaluation code. Move it into the Intel-specific microcode handling along with the data structures, defines and helpers required by it. The exports need to stay for IFS. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Link: https://lore.kernel.org/r/20230812195727.719202319@linutronix.de
1 parent e6bcfdd commit 4da2131

File tree

3 files changed

+204
-200
lines changed

3 files changed

+204
-200
lines changed

arch/x86/include/asm/microcode_intel.h

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,15 @@ struct microcode_intel {
2323
unsigned int bits[];
2424
};
2525

26-
/* microcode format is extended from prescott processors */
27-
struct extended_signature {
28-
unsigned int sig;
29-
unsigned int pf;
30-
unsigned int cksum;
31-
};
32-
33-
struct extended_sigtable {
34-
unsigned int count;
35-
unsigned int cksum;
36-
unsigned int reserved[3];
37-
struct extended_signature sigs[];
38-
};
39-
40-
#define DEFAULT_UCODE_DATASIZE (2000)
41-
#define MC_HEADER_SIZE (sizeof(struct microcode_header_intel))
42-
#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
43-
#define EXT_HEADER_SIZE (sizeof(struct extended_sigtable))
44-
#define EXT_SIGNATURE_SIZE (sizeof(struct extended_signature))
26+
#define MC_HEADER_SIZE (sizeof(struct microcode_header_intel))
4527
#define MC_HEADER_TYPE_MICROCODE 1
4628
#define MC_HEADER_TYPE_IFS 2
47-
48-
#define get_totalsize(mc) \
49-
(((struct microcode_intel *)mc)->hdr.datasize ? \
50-
((struct microcode_intel *)mc)->hdr.totalsize : \
51-
DEFAULT_UCODE_TOTALSIZE)
29+
#define DEFAULT_UCODE_DATASIZE (2000)
5230

5331
#define get_datasize(mc) \
5432
(((struct microcode_intel *)mc)->hdr.datasize ? \
5533
((struct microcode_intel *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
5634

57-
#define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
58-
5935
static inline u32 intel_get_microcode_revision(void)
6036
{
6137
u32 rev, dummy;

arch/x86/kernel/cpu/intel.c

Lines changed: 0 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -184,180 +184,6 @@ static bool bad_spectre_microcode(struct cpuinfo_x86 *c)
184184
return false;
185185
}
186186

187-
int intel_cpu_collect_info(struct ucode_cpu_info *uci)
188-
{
189-
unsigned int val[2];
190-
unsigned int family, model;
191-
struct cpu_signature csig = { 0 };
192-
unsigned int eax, ebx, ecx, edx;
193-
194-
memset(uci, 0, sizeof(*uci));
195-
196-
eax = 0x00000001;
197-
ecx = 0;
198-
native_cpuid(&eax, &ebx, &ecx, &edx);
199-
csig.sig = eax;
200-
201-
family = x86_family(eax);
202-
model = x86_model(eax);
203-
204-
if (model >= 5 || family > 6) {
205-
/* get processor flags from MSR 0x17 */
206-
native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
207-
csig.pf = 1 << ((val[1] >> 18) & 7);
208-
}
209-
210-
csig.rev = intel_get_microcode_revision();
211-
212-
uci->cpu_sig = csig;
213-
214-
return 0;
215-
}
216-
EXPORT_SYMBOL_GPL(intel_cpu_collect_info);
217-
218-
/*
219-
* Returns 1 if update has been found, 0 otherwise.
220-
*/
221-
int intel_find_matching_signature(void *mc, unsigned int csig, int cpf)
222-
{
223-
struct microcode_header_intel *mc_hdr = mc;
224-
struct extended_sigtable *ext_hdr;
225-
struct extended_signature *ext_sig;
226-
int i;
227-
228-
if (intel_cpu_signatures_match(csig, cpf, mc_hdr->sig, mc_hdr->pf))
229-
return 1;
230-
231-
/* Look for ext. headers: */
232-
if (get_totalsize(mc_hdr) <= get_datasize(mc_hdr) + MC_HEADER_SIZE)
233-
return 0;
234-
235-
ext_hdr = mc + get_datasize(mc_hdr) + MC_HEADER_SIZE;
236-
ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE;
237-
238-
for (i = 0; i < ext_hdr->count; i++) {
239-
if (intel_cpu_signatures_match(csig, cpf, ext_sig->sig, ext_sig->pf))
240-
return 1;
241-
ext_sig++;
242-
}
243-
return 0;
244-
}
245-
EXPORT_SYMBOL_GPL(intel_find_matching_signature);
246-
247-
/**
248-
* intel_microcode_sanity_check() - Sanity check microcode file.
249-
* @mc: Pointer to the microcode file contents.
250-
* @print_err: Display failure reason if true, silent if false.
251-
* @hdr_type: Type of file, i.e. normal microcode file or In Field Scan file.
252-
* Validate if the microcode header type matches with the type
253-
* specified here.
254-
*
255-
* Validate certain header fields and verify if computed checksum matches
256-
* with the one specified in the header.
257-
*
258-
* Return: 0 if the file passes all the checks, -EINVAL if any of the checks
259-
* fail.
260-
*/
261-
int intel_microcode_sanity_check(void *mc, bool print_err, int hdr_type)
262-
{
263-
unsigned long total_size, data_size, ext_table_size;
264-
struct microcode_header_intel *mc_header = mc;
265-
struct extended_sigtable *ext_header = NULL;
266-
u32 sum, orig_sum, ext_sigcount = 0, i;
267-
struct extended_signature *ext_sig;
268-
269-
total_size = get_totalsize(mc_header);
270-
data_size = get_datasize(mc_header);
271-
272-
if (data_size + MC_HEADER_SIZE > total_size) {
273-
if (print_err)
274-
pr_err("Error: bad microcode data file size.\n");
275-
return -EINVAL;
276-
}
277-
278-
if (mc_header->ldrver != 1 || mc_header->hdrver != hdr_type) {
279-
if (print_err)
280-
pr_err("Error: invalid/unknown microcode update format. Header type %d\n",
281-
mc_header->hdrver);
282-
return -EINVAL;
283-
}
284-
285-
ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
286-
if (ext_table_size) {
287-
u32 ext_table_sum = 0;
288-
u32 *ext_tablep;
289-
290-
if (ext_table_size < EXT_HEADER_SIZE ||
291-
((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
292-
if (print_err)
293-
pr_err("Error: truncated extended signature table.\n");
294-
return -EINVAL;
295-
}
296-
297-
ext_header = mc + MC_HEADER_SIZE + data_size;
298-
if (ext_table_size != exttable_size(ext_header)) {
299-
if (print_err)
300-
pr_err("Error: extended signature table size mismatch.\n");
301-
return -EFAULT;
302-
}
303-
304-
ext_sigcount = ext_header->count;
305-
306-
/*
307-
* Check extended table checksum: the sum of all dwords that
308-
* comprise a valid table must be 0.
309-
*/
310-
ext_tablep = (u32 *)ext_header;
311-
312-
i = ext_table_size / sizeof(u32);
313-
while (i--)
314-
ext_table_sum += ext_tablep[i];
315-
316-
if (ext_table_sum) {
317-
if (print_err)
318-
pr_warn("Bad extended signature table checksum, aborting.\n");
319-
return -EINVAL;
320-
}
321-
}
322-
323-
/*
324-
* Calculate the checksum of update data and header. The checksum of
325-
* valid update data and header including the extended signature table
326-
* must be 0.
327-
*/
328-
orig_sum = 0;
329-
i = (MC_HEADER_SIZE + data_size) / sizeof(u32);
330-
while (i--)
331-
orig_sum += ((u32 *)mc)[i];
332-
333-
if (orig_sum) {
334-
if (print_err)
335-
pr_err("Bad microcode data checksum, aborting.\n");
336-
return -EINVAL;
337-
}
338-
339-
if (!ext_table_size)
340-
return 0;
341-
342-
/*
343-
* Check extended signature checksum: 0 => valid.
344-
*/
345-
for (i = 0; i < ext_sigcount; i++) {
346-
ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
347-
EXT_SIGNATURE_SIZE * i;
348-
349-
sum = (mc_header->sig + mc_header->pf + mc_header->cksum) -
350-
(ext_sig->sig + ext_sig->pf + ext_sig->cksum);
351-
if (sum) {
352-
if (print_err)
353-
pr_err("Bad extended signature checksum, aborting.\n");
354-
return -EINVAL;
355-
}
356-
}
357-
return 0;
358-
}
359-
EXPORT_SYMBOL_GPL(intel_microcode_sanity_check);
360-
361187
static void early_init_intel(struct cpuinfo_x86 *c)
362188
{
363189
u64 misc_enable;

0 commit comments

Comments
 (0)