debuggers.hg

view xen/arch/x86/microcode.c @ 22906:700ac6445812

Now add KDB to the non-kdb tree
author Mukesh Rathor
date Thu Feb 03 15:42:41 2011 -0800 (2011-02-03)
parents 2a16128f17d8
children
line source
1 /*
2 * Intel CPU Microcode Update Driver for Linux
3 *
4 * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
5 * 2006 Shaohua Li <shaohua.li@intel.com> *
6 * This driver allows to upgrade microcode on Intel processors
7 * belonging to IA-32 family - PentiumPro, Pentium II,
8 * Pentium III, Xeon, Pentium 4, etc.
9 *
10 * Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture
11 * Software Developer's Manual
12 * Order Number 253668 or free download from:
13 *
14 * http://developer.intel.com/design/pentium4/manuals/253668.htm
15 *
16 * For more information, go to http://www.urbanmyth.org/microcode
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
22 */
24 #include <xen/config.h>
25 #include <xen/lib.h>
26 #include <xen/kernel.h>
27 #include <xen/init.h>
28 #include <xen/sched.h>
29 #include <xen/smp.h>
30 #include <xen/spinlock.h>
31 #include <xen/guest_access.h>
33 #include <asm/current.h>
34 #include <asm/msr.h>
35 #include <asm/uaccess.h>
36 #include <asm/processor.h>
37 #include <asm/microcode.h>
39 const struct microcode_ops *microcode_ops;
41 static DEFINE_SPINLOCK(microcode_mutex);
43 DEFINE_PER_CPU(struct ucode_cpu_info, ucode_cpu_info);
45 struct microcode_info {
46 unsigned int cpu;
47 uint32_t buffer_size;
48 int error;
49 char buffer[1];
50 };
52 static void __microcode_fini_cpu(int cpu)
53 {
54 struct ucode_cpu_info *uci = &per_cpu(ucode_cpu_info, cpu);
56 xfree(uci->mc.mc_valid);
57 memset(uci, 0, sizeof(*uci));
58 }
60 static void microcode_fini_cpu(int cpu)
61 {
62 spin_lock(&microcode_mutex);
63 __microcode_fini_cpu(cpu);
64 spin_unlock(&microcode_mutex);
65 }
67 int microcode_resume_cpu(int cpu)
68 {
69 int err;
70 struct ucode_cpu_info *uci = &per_cpu(ucode_cpu_info, cpu);
71 struct cpu_signature nsig;
73 if ( !uci->mc.mc_valid )
74 return -EIO;
76 /*
77 * Let's verify that the 'cached' ucode does belong
78 * to this cpu (a bit of paranoia):
79 */
80 err = microcode_ops->collect_cpu_info(cpu, &nsig);
81 if ( err )
82 {
83 microcode_fini_cpu(cpu);
84 return err;
85 }
87 if ( microcode_ops->microcode_resume_match(cpu, &nsig) )
88 {
89 return microcode_ops->apply_microcode(cpu);
90 }
91 else
92 {
93 microcode_fini_cpu(cpu);
94 return -EIO;
95 }
96 }
98 static int microcode_update_cpu(const void *buf, size_t size)
99 {
100 int err;
101 unsigned int cpu = smp_processor_id();
102 struct ucode_cpu_info *uci = &per_cpu(ucode_cpu_info, cpu);
104 spin_lock(&microcode_mutex);
106 err = microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig);
107 if ( likely(!err) )
108 err = microcode_ops->cpu_request_microcode(cpu, buf, size);
109 else
110 __microcode_fini_cpu(cpu);
112 spin_unlock(&microcode_mutex);
114 return err;
115 }
117 static long do_microcode_update(void *_info)
118 {
119 struct microcode_info *info = _info;
120 int error;
122 BUG_ON(info->cpu != smp_processor_id());
124 error = microcode_update_cpu(info->buffer, info->buffer_size);
125 if ( error )
126 info->error = error;
128 info->cpu = next_cpu(info->cpu, cpu_online_map);
129 if ( info->cpu < NR_CPUS )
130 return continue_hypercall_on_cpu(info->cpu, do_microcode_update, info);
132 error = info->error;
133 xfree(info);
134 return error;
135 }
137 int microcode_update(XEN_GUEST_HANDLE(const_void) buf, unsigned long len)
138 {
139 int ret;
140 struct microcode_info *info;
142 if ( len != (uint32_t)len )
143 return -E2BIG;
145 if ( microcode_ops == NULL )
146 return -EINVAL;
148 info = xmalloc_bytes(sizeof(*info) + len);
149 if ( info == NULL )
150 return -ENOMEM;
152 ret = copy_from_guest(info->buffer, buf, len);
153 if ( ret != 0 )
154 {
155 xfree(info);
156 return ret;
157 }
159 info->buffer_size = len;
160 info->error = 0;
161 info->cpu = first_cpu(cpu_online_map);
163 return continue_hypercall_on_cpu(info->cpu, do_microcode_update, info);
164 }