Home | History | Annotate | Line # | Download | only in x86
cpu_ucode_intel.c revision 1.19
      1 /* $NetBSD: cpu_ucode_intel.c,v 1.19 2022/09/15 01:30:56 msaitoh 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.19 2022/09/15 01:30:56 msaitoh 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 XENPV
    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 	/* Data size. */
    122 	if (buf->uh_data_size == 0)
    123 		data_size = 2000;
    124 	else
    125 		data_size = buf->uh_data_size;
    126 	if ((data_size % 4) != 0)
    127 		return EINVAL;
    128 	if (data_size > sc->sc_blobsize)
    129 		return EINVAL;
    130 
    131 	/* Total size. */
    132 	if (buf->uh_total_size == 0)
    133 		total_size = data_size + 48;
    134 	else
    135 		total_size = buf->uh_total_size;
    136 	if ((total_size % 1024) != 0)
    137 		return EINVAL;
    138 	if (total_size > sc->sc_blobsize)
    139 		return EINVAL;
    140 
    141 	/* Payload size. */
    142 	payload_size = data_size + 48;
    143 	if (payload_size > sc->sc_blobsize)
    144 		return EINVAL;
    145 
    146 	/*
    147 	 * Verify checksum of update data and header. Exclude extended
    148 	 * signature.
    149 	 */
    150 	sum = 0;
    151 	for (i = 0; i < (payload_size / sizeof(uint32_t)); i++) {
    152 		sum += *((uint32_t *)buf + i);
    153 	}
    154 	if (sum != 0)
    155 		return EINVAL;
    156 
    157 	/* Extended table size. Ignored for now. */
    158 	ext_size = total_size - payload_size;
    159 	if (ext_size > 0)
    160 		printf("This image has extended signature table.\n");
    161 
    162 	return 0;
    163 }
    164 
    165 int
    166 cpu_ucode_intel_apply(struct cpu_ucode_softc *sc, int cpuno)
    167 {
    168 	uint32_t ucodetarget, oucodeversion, nucodeversion;
    169 	struct intel1_ucode_header *uh;
    170 	int platformid, cpuid, error;
    171 	size_t newbufsize = 0;
    172 	void *uha;
    173 
    174 	if (sc->loader_version != CPU_UCODE_LOADER_INTEL1 ||
    175 	    cpuno != CPU_UCODE_CURRENT_CPU)
    176 		return EINVAL;
    177 
    178 	uh = (struct intel1_ucode_header *)sc->sc_blob;
    179 
    180 	error = cpu_ucode_intel_verify(sc, uh);
    181 	if (error != 0)
    182 		return error;
    183 
    184 	ucodetarget = uh->uh_rev;
    185 
    186 	if (((uintptr_t)sc->sc_blob) & 15) {
    187 		/* Make the buffer 16 byte aligned. */
    188 		newbufsize = sc->sc_blobsize + 15;
    189 		uha = kmem_alloc(newbufsize, KM_SLEEP);
    190 		uh =
    191 		    (struct intel1_ucode_header *)roundup2((uintptr_t)uha, 16);
    192 		memcpy(uh, sc->sc_blob, sc->sc_blobsize);
    193 	}
    194 
    195 	kpreempt_disable();
    196 
    197 	intel_getcurrentucode(&oucodeversion, &platformid);
    198 	if (oucodeversion >= ucodetarget) {
    199 		kpreempt_enable();
    200 		error = EEXIST;
    201 		goto out;
    202 	}
    203 
    204 	/*
    205 	 * Perform update. On some platforms a cache invalidation is
    206 	 * required.
    207 	 */
    208 	wbinvd();
    209 	wrmsr(MSR_BIOS_UPDT_TRIG, (uintptr_t)uh + 48);
    210 
    211 	intel_getcurrentucode(&nucodeversion, &platformid);
    212 	cpuid = curcpu()->ci_index;
    213 
    214 	kpreempt_enable();
    215 
    216 	if (nucodeversion != ucodetarget) {
    217 		error = EIO;
    218 		goto out;
    219 	}
    220 
    221 	printf("cpu %d: ucode 0x%x->0x%x\n", cpuid, oucodeversion,
    222 	    nucodeversion);
    223 
    224 out:
    225 	if (newbufsize != 0)
    226 		kmem_free(uha, newbufsize);
    227 	return error;
    228 }
    229 #endif /* ! XEN */
    230