Home | History | Annotate | Line # | Download | only in x86
cpu_ucode_intel.c revision 1.15
      1 /* $NetBSD: cpu_ucode_intel.c,v 1.15 2019/01/27 02:08:39 pgoyette Exp $ */
      2 /*
      3  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Matthias Drochner.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: cpu_ucode_intel.c,v 1.15 2019/01/27 02:08:39 pgoyette Exp $");
     33 
     34 #ifdef _KERNEL_OPT
     35 #include "opt_xen.h"
     36 #include "opt_cpu_ucode.h"
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/conf.h>
     41 #include <sys/cpuio.h>
     42 #include <sys/cpu.h>
     43 #include <sys/kmem.h>
     44 
     45 #include <machine/cpufunc.h>
     46 #include <machine/specialreg.h>
     47 #include <x86/cpu_ucode.h>
     48 
     49 static void
     50 intel_getcurrentucode(uint32_t *ucodeversion, int *platformid)
     51 {
     52 	unsigned int unneeded_ids[4];
     53 	uint64_t msr;
     54 
     55 	kpreempt_disable();
     56 
     57 	wrmsr(MSR_BIOS_SIGN, 0);
     58 	x86_cpuid(0, unneeded_ids);
     59 	msr = rdmsr(MSR_BIOS_SIGN);
     60 	*ucodeversion = msr >> 32;
     61 
     62 	kpreempt_enable();
     63 
     64 	msr = rdmsr(MSR_IA32_PLATFORM_ID);
     65 	*platformid = ((int)(msr >> 50)) & 7;
     66 }
     67 
     68 int
     69 cpu_ucode_intel_get_version(struct cpu_ucode_version *ucode,
     70     void *ptr, size_t len)
     71 {
     72 	struct cpu_info *ci = curcpu();
     73 	struct cpu_ucode_version_intel1 *data = ptr;
     74 
     75 	if (ucode->loader_version != CPU_UCODE_LOADER_INTEL1 ||
     76 	    CPUID_TO_FAMILY(ci->ci_signature) < 6)
     77 		return EOPNOTSUPP;
     78 
     79 	if (len < sizeof(*data))
     80 		return ENOSPC;
     81 
     82 	intel_getcurrentucode(&data->ucodeversion, &data->platformid);
     83 	return 0;
     84 }
     85 
     86 int
     87 cpu_ucode_intel_firmware_open(firmware_handle_t *fwh, const char *fwname)
     88 {
     89 	const char *fw_path = "cpu_x86_intel1";
     90 	uint32_t ucodeversion, cpu_signature;
     91 	int platformid;
     92 	char cpuspec[11];
     93 
     94 	if (fwname != NULL && fwname[0] != '\0')
     95 		return firmware_open(fw_path, fwname, fwh);
     96 
     97 	cpu_signature = curcpu()->ci_signature;
     98 	if (CPUID_TO_FAMILY(cpu_signature) < 6)
     99 		return EOPNOTSUPP;
    100 
    101 	intel_getcurrentucode(&ucodeversion, &platformid);
    102 	snprintf(cpuspec, sizeof(cpuspec), "%08x-%d", cpu_signature,
    103 	    platformid);
    104 
    105 	return firmware_open(fw_path, cpuspec, fwh);
    106 }
    107 
    108 #ifndef XEN
    109 /* Check header version and checksum */
    110 static int
    111 cpu_ucode_intel_verify(struct intel1_ucode_header *buf)
    112 {
    113 	uint32_t data_size, total_size, payload_size, extended_table_size;
    114 #if 0 /* not yet */
    115 	struct intel1_ucode_ext_table *ext_table;
    116 	struct intel1_ucode_proc_signature *ext_psig;
    117 #endif
    118 	uint32_t sum;
    119 	int i;
    120 
    121 	if ((buf->uh_header_ver != 1) || (buf->uh_loader_rev != 1))
    122 		return EINVAL;
    123 
    124 	/* Data size */
    125 	if (buf->uh_data_size == 0)
    126 		data_size = 2000;
    127 	else
    128 		data_size = buf->uh_data_size;
    129 
    130 	if ((data_size % 4) != 0) {
    131 		/* Wrong size */
    132 		return EINVAL;
    133 	}
    134 
    135 	/* Total size */
    136 	if (buf->uh_total_size == 0)
    137 		total_size = data_size + 48;
    138 	else
    139 		total_size = buf->uh_total_size;
    140 
    141 	if ((total_size % 1024) != 0) {
    142 		/* Wrong size */
    143 		return EINVAL;
    144 	}
    145 
    146 	payload_size = data_size + 48;
    147 
    148 	/* Extended table size */
    149 	extended_table_size = total_size - payload_size;
    150 
    151 	/*
    152 	 * Verify checksum of update data and header
    153 	 * (exclude extended signature).
    154 	 */
    155 	sum = 0;
    156 	for (i = 0; i < (payload_size / sizeof(uint32_t)); i++)
    157 		sum += *((uint32_t *)buf + i);
    158 	if (sum != 0) {
    159 		/* Checksum mismatch */
    160 		return EINVAL;
    161 	}
    162 
    163 	if (extended_table_size == 0)
    164 		return 0;
    165 
    166 #if 0
    167 	/* Verify extended signature's checksum */
    168 	ext_table = (void *)buf + payload_size;
    169 	ext_psig = (void *)ext_table + sizeof(struct intel1_ucode_ext_table);
    170 	printf("ext_table = %p, extsig = %p\n", ext_table, ext_psig);
    171 #else
    172 	printf("This image has extended signature table.");
    173 #endif
    174 
    175 	return 0;
    176 }
    177 
    178 int
    179 cpu_ucode_intel_apply(struct cpu_ucode_softc *sc, int cpuno)
    180 {
    181 	uint32_t ucodetarget, oucodeversion, nucodeversion;
    182 	int platformid, cpuid;
    183 	struct intel1_ucode_header *uh;
    184 	void *uha;
    185 	size_t newbufsize = 0;
    186 	int rv = 0;
    187 
    188 	if (sc->loader_version != CPU_UCODE_LOADER_INTEL1
    189 	    || cpuno != CPU_UCODE_CURRENT_CPU)
    190 		return EINVAL;
    191 
    192 	uh = (struct intel1_ucode_header *)(sc->sc_blob);
    193 	rv = cpu_ucode_intel_verify(uh);
    194 	if (rv != 0)
    195 		return EINVAL;
    196 
    197 	ucodetarget = uh->uh_rev;
    198 
    199 	if ((uintptr_t)(sc->sc_blob) & 15) {
    200 		/* Make the buffer 16 byte aligned */
    201 		newbufsize = sc->sc_blobsize + 15;
    202 		uha = kmem_alloc(newbufsize, KM_SLEEP);
    203 		uh = (struct intel1_ucode_header *)roundup2((uintptr_t)uha, 16);
    204 		/* Copy to the new area */
    205 		memcpy(uh, sc->sc_blob, sc->sc_blobsize);
    206 	}
    207 
    208 	kpreempt_disable();
    209 
    210 	intel_getcurrentucode(&oucodeversion, &platformid);
    211 	if (oucodeversion >= ucodetarget) {
    212 		kpreempt_enable();
    213 		rv = EEXIST; /* ??? */
    214 		goto out;
    215 	}
    216 	wrmsr(MSR_BIOS_UPDT_TRIG, (uintptr_t)uh + 48);
    217 	intel_getcurrentucode(&nucodeversion, &platformid);
    218 	cpuid = curcpu()->ci_index;
    219 
    220 	kpreempt_enable();
    221 
    222 	if (nucodeversion != ucodetarget) {
    223 		rv = EIO;
    224 		goto out;
    225 	}
    226 
    227 	printf("cpu %d: ucode 0x%x->0x%x\n", cpuid,
    228 	       oucodeversion, nucodeversion);
    229 out:
    230 	if (newbufsize != 0)
    231 		kmem_free(uha, newbufsize);
    232 	return rv;
    233 }
    234 #endif /* ! XEN */
    235