1 /* $NetBSD: cpu_ucode_intel.c,v 1.20 2022/09/15 14:34:22 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.20 2022/09/15 14:34:22 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
intel_getcurrentucode(uint32_t * ucodeversion,int * platformid)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
cpu_ucode_intel_get_version(struct cpu_ucode_version * ucode,void * ptr,size_t len)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
cpu_ucode_intel_firmware_open(firmware_handle_t * fwh,const char * fwname)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
cpu_ucode_intel_verify(struct cpu_ucode_softc * sc,struct intel1_ucode_header * buf)111 cpu_ucode_intel_verify(struct cpu_ucode_softc *sc,
112 struct intel1_ucode_header *buf)
113 {
114 struct intel1_ucode_ext_table *ehdr;
115 uint32_t data_size, total_size, payload_size, ext_size;
116 uint32_t sum;
117 uint32_t *p;
118 int i;
119
120 if ((buf->uh_header_ver != 1) || (buf->uh_loader_rev != 1))
121 return EINVAL;
122
123 /* Data size. */
124 if (buf->uh_data_size == 0)
125 data_size = 2000;
126 else
127 data_size = buf->uh_data_size;
128 if ((data_size % 4) != 0)
129 return EINVAL;
130 if (data_size > sc->sc_blobsize)
131 return EINVAL;
132
133 /* Total size. */
134 if (buf->uh_total_size == 0)
135 total_size = data_size + 48;
136 else
137 total_size = buf->uh_total_size;
138 if ((total_size % 1024) != 0)
139 return EINVAL;
140 if (total_size > sc->sc_blobsize)
141 return EINVAL;
142
143 /* Payload size. */
144 payload_size = data_size + 48;
145 if (payload_size > sc->sc_blobsize)
146 return EINVAL;
147
148 /* Verify checksum of update data and header(s). */
149 sum = 0;
150 p = (uint32_t *)buf;
151 for (i = 0; i < (payload_size / sizeof(uint32_t)); i++)
152 sum += p[i];
153 if (sum != 0)
154 return EINVAL;
155
156 ext_size = total_size - payload_size;
157 if (ext_size > 0) {
158 /* This image has extended signature table. */
159 ehdr = (struct intel1_ucode_ext_table *)
160 ((uint8_t *)buf + sizeof(struct intel1_ucode_header) +
161 data_size);
162 payload_size =
163 sizeof(struct intel1_ucode_ext_table) +
164 sizeof(struct intel1_ucode_proc_signature) *
165 ehdr->uet_count;
166
167 sum = 0;
168 p = (uint32_t *)ehdr;
169 for (i = 0; i < (payload_size / sizeof(uint32_t)); i++)
170 sum += p[i];
171 if (sum != 0)
172 return EINVAL;
173 }
174
175 return 0;
176 }
177
178 int
cpu_ucode_intel_apply(struct cpu_ucode_softc * sc,int cpuno)179 cpu_ucode_intel_apply(struct cpu_ucode_softc *sc, int cpuno)
180 {
181 uint32_t ucodetarget, oucodeversion, nucodeversion;
182 struct intel1_ucode_header *uh;
183 int platformid, cpuid, error;
184 size_t newbufsize = 0;
185 void *uha;
186
187 if (sc->loader_version != CPU_UCODE_LOADER_INTEL1 ||
188 cpuno != CPU_UCODE_CURRENT_CPU)
189 return EINVAL;
190
191 uh = (struct intel1_ucode_header *)sc->sc_blob;
192
193 error = cpu_ucode_intel_verify(sc, uh);
194 if (error != 0)
195 return error;
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 =
204 (struct intel1_ucode_header *)roundup2((uintptr_t)uha, 16);
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 error = EEXIST;
214 goto out;
215 }
216
217 /*
218 * Perform update. On some platforms a cache invalidation is
219 * required.
220 */
221 wbinvd();
222 wrmsr(MSR_BIOS_UPDT_TRIG, (uintptr_t)uh + 48);
223
224 intel_getcurrentucode(&nucodeversion, &platformid);
225 cpuid = curcpu()->ci_index;
226
227 kpreempt_enable();
228
229 if (nucodeversion != ucodetarget) {
230 error = EIO;
231 goto out;
232 }
233
234 printf("cpu %d: ucode 0x%x->0x%x\n", cpuid, oucodeversion,
235 nucodeversion);
236
237 out:
238 if (newbufsize != 0)
239 kmem_free(uha, newbufsize);
240 return error;
241 }
242 #endif /* ! XEN */
243