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