提交 209b6c8f 编写于 作者: L Linus Torvalds

Merge branch 'x86-microcode-for-linus' of...

Merge branch 'x86-microcode-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip

* 'x86-microcode-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip:
  x86, microcode, AMD: Fix signedness bug in generic_load_microcode()
  x86, microcode, AMD: Extend ucode size verification
  x86, microcode, AMD: Cleanup dmesg output
  x86, microcode, AMD: Remove unneeded memset call
  x86, microcode, AMD: Simplify get_next_ucode
  x86, microcode, AMD: Simplify install_equiv_cpu_table
  x86, microcode, AMD: Release firmware on error
  x86, microcode: Correct sysdev_add error path
...@@ -66,7 +66,6 @@ struct microcode_amd { ...@@ -66,7 +66,6 @@ struct microcode_amd {
unsigned int mpb[0]; unsigned int mpb[0];
}; };
#define UCODE_MAX_SIZE 2048
#define UCODE_CONTAINER_SECTION_HDR 8 #define UCODE_CONTAINER_SECTION_HDR 8
#define UCODE_CONTAINER_HEADER_SIZE 12 #define UCODE_CONTAINER_HEADER_SIZE 12
...@@ -77,20 +76,20 @@ static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig) ...@@ -77,20 +76,20 @@ static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
struct cpuinfo_x86 *c = &cpu_data(cpu); struct cpuinfo_x86 *c = &cpu_data(cpu);
u32 dummy; u32 dummy;
memset(csig, 0, sizeof(*csig));
if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) { if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
pr_warning("microcode: CPU%d: AMD CPU family 0x%x not " pr_warning("CPU%d: family %d not supported\n", cpu, c->x86);
"supported\n", cpu, c->x86);
return -1; return -1;
} }
rdmsr(MSR_AMD64_PATCH_LEVEL, csig->rev, dummy); rdmsr(MSR_AMD64_PATCH_LEVEL, csig->rev, dummy);
pr_info("CPU%d: patch_level=0x%x\n", cpu, csig->rev); pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
return 0; return 0;
} }
static int get_matching_microcode(int cpu, void *mc, int rev) static int get_matching_microcode(int cpu, struct microcode_header_amd *mc_hdr,
int rev)
{ {
struct microcode_header_amd *mc_header = mc;
unsigned int current_cpu_id; unsigned int current_cpu_id;
u16 equiv_cpu_id = 0; u16 equiv_cpu_id = 0;
unsigned int i = 0; unsigned int i = 0;
...@@ -109,17 +108,17 @@ static int get_matching_microcode(int cpu, void *mc, int rev) ...@@ -109,17 +108,17 @@ static int get_matching_microcode(int cpu, void *mc, int rev)
if (!equiv_cpu_id) if (!equiv_cpu_id)
return 0; return 0;
if (mc_header->processor_rev_id != equiv_cpu_id) if (mc_hdr->processor_rev_id != equiv_cpu_id)
return 0; return 0;
/* ucode might be chipset specific -- currently we don't support this */ /* ucode might be chipset specific -- currently we don't support this */
if (mc_header->nb_dev_id || mc_header->sb_dev_id) { if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
pr_err("CPU%d: loading of chipset specific code not yet supported\n", pr_err("CPU%d: chipset specific code not yet supported\n",
cpu); cpu);
return 0; return 0;
} }
if (mc_header->patch_id <= rev) if (mc_hdr->patch_id <= rev)
return 0; return 0;
return 1; return 1;
...@@ -144,71 +143,93 @@ static int apply_microcode_amd(int cpu) ...@@ -144,71 +143,93 @@ static int apply_microcode_amd(int cpu)
/* check current patch id and patch's id for match */ /* check current patch id and patch's id for match */
if (rev != mc_amd->hdr.patch_id) { if (rev != mc_amd->hdr.patch_id) {
pr_err("CPU%d: update failed (for patch_level=0x%x)\n", pr_err("CPU%d: update failed for patch_level=0x%08x\n",
cpu, mc_amd->hdr.patch_id); cpu, mc_amd->hdr.patch_id);
return -1; return -1;
} }
pr_info("CPU%d: updated (new patch_level=0x%x)\n", cpu, rev); pr_info("CPU%d: new patch_level=0x%08x\n", cpu, rev);
uci->cpu_sig.rev = rev; uci->cpu_sig.rev = rev;
return 0; return 0;
} }
static void * static unsigned int verify_ucode_size(int cpu, const u8 *buf, unsigned int size)
get_next_ucode(const u8 *buf, unsigned int size, unsigned int *mc_size)
{ {
unsigned int total_size; struct cpuinfo_x86 *c = &cpu_data(cpu);
u8 section_hdr[UCODE_CONTAINER_SECTION_HDR]; unsigned int max_size, actual_size;
void *mc;
#define F1XH_MPB_MAX_SIZE 2048
#define F14H_MPB_MAX_SIZE 1824
#define F15H_MPB_MAX_SIZE 4096
switch (c->x86) {
case 0x14:
max_size = F14H_MPB_MAX_SIZE;
break;
case 0x15:
max_size = F15H_MPB_MAX_SIZE;
break;
default:
max_size = F1XH_MPB_MAX_SIZE;
break;
}
get_ucode_data(section_hdr, buf, UCODE_CONTAINER_SECTION_HDR); actual_size = buf[4] + (buf[5] << 8);
if (section_hdr[0] != UCODE_UCODE_TYPE) { if (actual_size > size || actual_size > max_size) {
pr_err("error: invalid type field in container file section header\n"); pr_err("section size mismatch\n");
return NULL; return 0;
} }
total_size = (unsigned long) (section_hdr[4] + (section_hdr[5] << 8)); return actual_size;
}
if (total_size > size || total_size > UCODE_MAX_SIZE) { static struct microcode_header_amd *
pr_err("error: size mismatch\n"); get_next_ucode(int cpu, const u8 *buf, unsigned int size, unsigned int *mc_size)
return NULL; {
struct microcode_header_amd *mc = NULL;
unsigned int actual_size = 0;
if (buf[0] != UCODE_UCODE_TYPE) {
pr_err("invalid type field in container file section header\n");
goto out;
} }
mc = vzalloc(UCODE_MAX_SIZE); actual_size = verify_ucode_size(cpu, buf, size);
if (!actual_size)
goto out;
mc = vzalloc(actual_size);
if (!mc) if (!mc)
return NULL; goto out;
get_ucode_data(mc, buf + UCODE_CONTAINER_SECTION_HDR, total_size); get_ucode_data(mc, buf + UCODE_CONTAINER_SECTION_HDR, actual_size);
*mc_size = total_size + UCODE_CONTAINER_SECTION_HDR; *mc_size = actual_size + UCODE_CONTAINER_SECTION_HDR;
out:
return mc; return mc;
} }
static int install_equiv_cpu_table(const u8 *buf) static int install_equiv_cpu_table(const u8 *buf)
{ {
u8 *container_hdr[UCODE_CONTAINER_HEADER_SIZE]; unsigned int *ibuf = (unsigned int *)buf;
unsigned int *buf_pos = (unsigned int *)container_hdr; unsigned int type = ibuf[1];
unsigned long size; unsigned int size = ibuf[2];
get_ucode_data(&container_hdr, buf, UCODE_CONTAINER_HEADER_SIZE); if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
pr_err("empty section/"
size = buf_pos[2]; "invalid type field in container file section header\n");
return -EINVAL;
if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
pr_err("error: invalid type field in container file section header\n");
return 0;
} }
equiv_cpu_table = vmalloc(size); equiv_cpu_table = vmalloc(size);
if (!equiv_cpu_table) { if (!equiv_cpu_table) {
pr_err("failed to allocate equivalent CPU table\n"); pr_err("failed to allocate equivalent CPU table\n");
return 0; return -ENOMEM;
} }
buf += UCODE_CONTAINER_HEADER_SIZE; get_ucode_data(equiv_cpu_table, buf + UCODE_CONTAINER_HEADER_SIZE, size);
get_ucode_data(equiv_cpu_table, buf, size);
return size + UCODE_CONTAINER_HEADER_SIZE; /* add header length */ return size + UCODE_CONTAINER_HEADER_SIZE; /* add header length */
} }
...@@ -223,16 +244,16 @@ static enum ucode_state ...@@ -223,16 +244,16 @@ static enum ucode_state
generic_load_microcode(int cpu, const u8 *data, size_t size) generic_load_microcode(int cpu, const u8 *data, size_t size)
{ {
struct ucode_cpu_info *uci = ucode_cpu_info + cpu; struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
struct microcode_header_amd *mc_hdr = NULL;
unsigned int mc_size, leftover;
int offset;
const u8 *ucode_ptr = data; const u8 *ucode_ptr = data;
void *new_mc = NULL; void *new_mc = NULL;
void *mc; unsigned int new_rev = uci->cpu_sig.rev;
int new_rev = uci->cpu_sig.rev;
unsigned int leftover;
unsigned long offset;
enum ucode_state state = UCODE_OK; enum ucode_state state = UCODE_OK;
offset = install_equiv_cpu_table(ucode_ptr); offset = install_equiv_cpu_table(ucode_ptr);
if (!offset) { if (offset < 0) {
pr_err("failed to create equivalent cpu table\n"); pr_err("failed to create equivalent cpu table\n");
return UCODE_ERROR; return UCODE_ERROR;
} }
...@@ -241,64 +262,65 @@ generic_load_microcode(int cpu, const u8 *data, size_t size) ...@@ -241,64 +262,65 @@ generic_load_microcode(int cpu, const u8 *data, size_t size)
leftover = size - offset; leftover = size - offset;
while (leftover) { while (leftover) {
unsigned int uninitialized_var(mc_size); mc_hdr = get_next_ucode(cpu, ucode_ptr, leftover, &mc_size);
struct microcode_header_amd *mc_header; if (!mc_hdr)
mc = get_next_ucode(ucode_ptr, leftover, &mc_size);
if (!mc)
break; break;
mc_header = (struct microcode_header_amd *)mc; if (get_matching_microcode(cpu, mc_hdr, new_rev)) {
if (get_matching_microcode(cpu, mc, new_rev)) {
vfree(new_mc); vfree(new_mc);
new_rev = mc_header->patch_id; new_rev = mc_hdr->patch_id;
new_mc = mc; new_mc = mc_hdr;
} else } else
vfree(mc); vfree(mc_hdr);
ucode_ptr += mc_size; ucode_ptr += mc_size;
leftover -= mc_size; leftover -= mc_size;
} }
if (new_mc) { if (!new_mc) {
if (!leftover) {
vfree(uci->mc);
uci->mc = new_mc;
pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
cpu, new_rev, uci->cpu_sig.rev);
} else {
vfree(new_mc);
state = UCODE_ERROR;
}
} else
state = UCODE_NFOUND; state = UCODE_NFOUND;
goto free_table;
}
if (!leftover) {
vfree(uci->mc);
uci->mc = new_mc;
pr_debug("CPU%d update ucode (0x%08x -> 0x%08x)\n",
cpu, uci->cpu_sig.rev, new_rev);
} else {
vfree(new_mc);
state = UCODE_ERROR;
}
free_table:
free_equiv_cpu_table(); free_equiv_cpu_table();
return state; return state;
} }
static enum ucode_state request_microcode_fw(int cpu, struct device *device) static enum ucode_state request_microcode_amd(int cpu, struct device *device)
{ {
const char *fw_name = "amd-ucode/microcode_amd.bin"; const char *fw_name = "amd-ucode/microcode_amd.bin";
const struct firmware *firmware; const struct firmware *fw;
enum ucode_state ret; enum ucode_state ret = UCODE_NFOUND;
if (request_firmware(&firmware, fw_name, device)) { if (request_firmware(&fw, fw_name, device)) {
printk(KERN_ERR "microcode: failed to load file %s\n", fw_name); pr_err("failed to load file %s\n", fw_name);
return UCODE_NFOUND; goto out;
} }
if (*(u32 *)firmware->data != UCODE_MAGIC) { ret = UCODE_ERROR;
pr_err("invalid UCODE_MAGIC (0x%08x)\n", if (*(u32 *)fw->data != UCODE_MAGIC) {
*(u32 *)firmware->data); pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
return UCODE_ERROR; goto fw_release;
} }
ret = generic_load_microcode(cpu, firmware->data, firmware->size); ret = generic_load_microcode(cpu, fw->data, fw->size);
release_firmware(firmware); fw_release:
release_firmware(fw);
out:
return ret; return ret;
} }
...@@ -319,7 +341,7 @@ static void microcode_fini_cpu_amd(int cpu) ...@@ -319,7 +341,7 @@ static void microcode_fini_cpu_amd(int cpu)
static struct microcode_ops microcode_amd_ops = { static struct microcode_ops microcode_amd_ops = {
.request_microcode_user = request_microcode_user, .request_microcode_user = request_microcode_user,
.request_microcode_fw = request_microcode_fw, .request_microcode_fw = request_microcode_amd,
.collect_cpu_info = collect_cpu_info_amd, .collect_cpu_info = collect_cpu_info_amd,
.apply_microcode = apply_microcode_amd, .apply_microcode = apply_microcode_amd,
.microcode_fini_cpu = microcode_fini_cpu_amd, .microcode_fini_cpu = microcode_fini_cpu_amd,
......
...@@ -417,8 +417,10 @@ static int mc_sysdev_add(struct sys_device *sys_dev) ...@@ -417,8 +417,10 @@ static int mc_sysdev_add(struct sys_device *sys_dev)
if (err) if (err)
return err; return err;
if (microcode_init_cpu(cpu) == UCODE_ERROR) if (microcode_init_cpu(cpu) == UCODE_ERROR) {
err = -EINVAL; sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
return -EINVAL;
}
return err; return err;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册