Module Name:    src
Committed By:   martin
Date:           Sun May 12 09:22:52 UTC 2019

Modified Files:
        src/sys/arch/x86/x86 [netbsd-8]: cpu_ucode_intel.c

Log Message:
Pull up following revision(s) (requested by maxv in ticket #1261):

        sys/arch/x86/x86/cpu_ucode_intel.c: revision 1.16
        sys/arch/x86/x86/cpu_ucode_intel.c: revision 1.17

Invalidate the cache before updating the microcode. Some platforms require
this. Seen in Illumos and FreeBSD.

Clean up, and add sanity checks on the microcode lengths.


To generate a diff of this commit:
cvs rdiff -u -r1.12.2.1 -r1.12.2.2 src/sys/arch/x86/x86/cpu_ucode_intel.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu_ucode_intel.c
diff -u src/sys/arch/x86/x86/cpu_ucode_intel.c:1.12.2.1 src/sys/arch/x86/x86/cpu_ucode_intel.c:1.12.2.2
--- src/sys/arch/x86/x86/cpu_ucode_intel.c:1.12.2.1	Thu Jul 26 21:02:29 2018
+++ src/sys/arch/x86/x86/cpu_ucode_intel.c	Sun May 12 09:22:52 2019
@@ -1,10 +1,11 @@
-/* $NetBSD: cpu_ucode_intel.c,v 1.12.2.1 2018/07/26 21:02:29 snj Exp $ */
+/* $NetBSD: cpu_ucode_intel.c,v 1.12.2.2 2019/05/12 09:22:52 martin Exp $ */
+
 /*
- * Copyright (c) 2012 The NetBSD Foundation, Inc.
+ * Copyright (c) 2012, 2019 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
- * by Matthias Drochner.
+ * by Matthias Drochner and Maxime Villard.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -29,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cpu_ucode_intel.c,v 1.12.2.1 2018/07/26 21:02:29 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu_ucode_intel.c,v 1.12.2.2 2019/05/12 09:22:52 martin Exp $");
 
 #include "opt_xen.h"
 #include "opt_cpu_ucode.h"
@@ -86,8 +87,8 @@ cpu_ucode_intel_firmware_open(firmware_h
 {
 	const char *fw_path = "cpu_x86_intel1";
 	uint32_t ucodeversion, cpu_signature;
-	int platformid;
 	char cpuspec[11];
+	int platformid;
 
 	if (fwname != NULL && fwname[0] != '\0')
 		return firmware_open(fw_path, fwname, fwh);
@@ -104,71 +105,68 @@ cpu_ucode_intel_firmware_open(firmware_h
 }
 
 #ifndef XEN
-/* Check header version and checksum */
 static int
-cpu_ucode_intel_verify(struct intel1_ucode_header *buf)
+cpu_ucode_intel_verify(struct cpu_ucode_softc *sc,
+    struct intel1_ucode_header *buf)
 {
-	uint32_t data_size, total_size, payload_size, extended_table_size;
-#if 0 /* not yet */
-	struct intel1_ucode_ext_table *ext_table;
-	struct intel1_ucode_proc_signature *ext_psig;
-#endif
+	uint32_t data_size, total_size, payload_size, ext_size;
 	uint32_t sum;
 	int i;
-	
+
 	if ((buf->uh_header_ver != 1) || (buf->uh_loader_rev != 1))
 		return EINVAL;
 
-	/* Data size */
-	if (buf->uh_data_size == 0)
+	/*
+	 * Data size.
+	 */
+	if (buf->uh_data_size == 0) {
 		data_size = 2000;
-	else
+	} else {
 		data_size = buf->uh_data_size;
-
-	if ((data_size % 4) != 0) {
-		/* Wrong size */
-		return EINVAL;
 	}
+	if ((data_size % 4) != 0)
+		return EINVAL;
+	if (data_size > sc->sc_blobsize)
+		return EINVAL;
 
-	/* Total size */
-	if (buf->uh_total_size == 0)
+	/*
+	 * Total size.
+	 */
+	if (buf->uh_total_size == 0) {
 		total_size = data_size + 48;
-	else
+	} else {
 		total_size = buf->uh_total_size;
-
-	if ((total_size % 1024) != 0) {
-		/* Wrong size */
-		return EINVAL;
 	}
+	if ((total_size % 1024) != 0)
+		return EINVAL;
+	if (total_size > sc->sc_blobsize)
+		return EINVAL;
 
+	/*
+	 * Payload size.
+	 */
 	payload_size = data_size + 48;
-
-	/* Extended table size */
-	extended_table_size = total_size - payload_size;
+	if (payload_size > sc->sc_blobsize)
+		return EINVAL;
 
 	/*
-	 * Verify checksum of update data and header
-	 * (exclude extended signature).
+	 * Verify checksum of update data and header. Exclude extended
+	 * signature.
 	 */
 	sum = 0;
-	for (i = 0; i < (payload_size / sizeof(uint32_t)); i++)
+	for (i = 0; i < (payload_size / sizeof(uint32_t)); i++) {
 		sum += *((uint32_t *)buf + i);
-	if (sum != 0) {
-		/* Checksum mismatch */
-		return EINVAL;
 	}
+	if (sum != 0)
+		return EINVAL;
 
-	if (extended_table_size == 0)
-		return 0;
-
-#if 0
-	/* Verify extended signature's checksum */
-	ext_table = (void *)buf + payload_size;
-	ext_psig = (void *)ext_table + sizeof(struct intel1_ucode_ext_table);
-	printf("ext_table = %p, extsig = %p\n", ext_table, ext_psig);
-#else
-	printf("This image has extended signature table.");
-#endif
+	/*
+	 * Extended table size. Ignored for now.
+	 */
+	ext_size = total_size - payload_size;
+	if (ext_size > 0) {
+		printf("This image has extended signature table.");
+	}
 
 	return 0;
 }
@@ -177,29 +175,28 @@ int
 cpu_ucode_intel_apply(struct cpu_ucode_softc *sc, int cpuno)
 {
 	uint32_t ucodetarget, oucodeversion, nucodeversion;
-	int platformid, cpuid;
 	struct intel1_ucode_header *uh;
-	void *uha;
+	int platformid, cpuid, error;
 	size_t newbufsize = 0;
-	int rv = 0;
+	void *uha;
 
-	if (sc->loader_version != CPU_UCODE_LOADER_INTEL1
-	    || cpuno != CPU_UCODE_CURRENT_CPU)
+	if (sc->loader_version != CPU_UCODE_LOADER_INTEL1 ||
+	    cpuno != CPU_UCODE_CURRENT_CPU)
 		return EINVAL;
 
-	uh = (struct intel1_ucode_header *)(sc->sc_blob);
-	rv = cpu_ucode_intel_verify(uh);
-	if (rv != 0)
-		return EINVAL;
+	uh = (struct intel1_ucode_header *)sc->sc_blob;
+
+	error = cpu_ucode_intel_verify(sc, uh);
+	if (error != 0)
+		return error;
 
 	ucodetarget = uh->uh_rev;
 
-	if ((uintptr_t)(sc->sc_blob) & 15) {
-		/* Make the buffer 16 byte aligned */
+	if (((uintptr_t)sc->sc_blob) & 15) {
+		/* Make the buffer 16 byte aligned. */
 		newbufsize = sc->sc_blobsize + 15;
 		uha = kmem_alloc(newbufsize, KM_SLEEP);
 		uh = (struct intel1_ucode_header *)roundup2((uintptr_t)uha, 16);
-		/* Copy to the new area */
 		memcpy(uh, sc->sc_blob, sc->sc_blobsize);
 	}
 
@@ -208,25 +205,33 @@ cpu_ucode_intel_apply(struct cpu_ucode_s
 	intel_getcurrentucode(&oucodeversion, &platformid);
 	if (oucodeversion >= ucodetarget) {
 		kpreempt_enable();
-		rv = EEXIST; /* ??? */
+		error = EEXIST;
 		goto out;
 	}
+
+	/*
+	 * Perform update. On some platforms a cache invalidation is
+	 * required.
+	 */
+	wbinvd();
 	wrmsr(MSR_BIOS_UPDT_TRIG, (uintptr_t)uh + 48);
+
 	intel_getcurrentucode(&nucodeversion, &platformid);
 	cpuid = curcpu()->ci_index;
 
 	kpreempt_enable();
 
 	if (nucodeversion != ucodetarget) {
-		rv = EIO;
+		error = EIO;
 		goto out;
 	}
 
-	printf("cpu %d: ucode 0x%x->0x%x\n", cpuid,
-	       oucodeversion, nucodeversion);
+	printf("cpu %d: ucode 0x%x->0x%x\n", cpuid, oucodeversion,
+	    nucodeversion);
+
 out:
 	if (newbufsize != 0)
 		kmem_free(uha, newbufsize);
-	return rv;
+	return error;
 }
 #endif /* ! XEN */

Reply via email to