ia64/xen-unstable

view xen/arch/x86/microcode.c @ 18506:f163138e3340

x86, microcode: Free microcode_info struct at end of hypercall.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Sep 16 13:49:16 2008 +0100 (2008-09-16)
parents 879330497672
children fb0dc8143932
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 struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
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 = ucode_cpu_info + cpu;
56 spin_lock(&microcode_mutex);
57 xfree(uci->mc.valid_mc);
58 uci->mc.valid_mc = NULL;
59 uci->valid = 0;
60 spin_unlock(&microcode_mutex);
61 }
63 static int collect_cpu_info(int cpu)
64 {
65 int err = 0;
66 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
68 memset(uci, 0, sizeof(*uci));
69 err = microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig);
70 if ( !err )
71 uci->valid = 1;
73 return err;
74 }
76 static int microcode_resume_cpu(int cpu)
77 {
78 int err = 0;
79 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
80 struct cpu_signature nsig;
82 gdprintk(XENLOG_INFO, "microcode: CPU%d resumed\n", cpu);
84 if ( !uci->mc.valid_mc )
85 return -EIO;
87 /*
88 * Let's verify that the 'cached' ucode does belong
89 * to this cpu (a bit of paranoia):
90 */
91 err = microcode_ops->collect_cpu_info(cpu, &nsig);
92 if ( err )
93 {
94 microcode_fini_cpu(cpu);
95 return err;
96 }
98 if ( memcmp(&nsig, &uci->cpu_sig, sizeof(nsig)) )
99 {
100 microcode_fini_cpu(cpu);
101 /* Should we look for a new ucode here? */
102 return -EIO;
103 }
105 err = microcode_ops->apply_microcode(cpu);
107 return err;
108 }
110 static int microcode_update_cpu(const void *buf, size_t size)
111 {
112 int err;
113 unsigned int cpu = smp_processor_id();
114 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
116 spin_lock(&microcode_mutex);
118 /*
119 * Check if the system resume is in progress (uci->valid != NULL),
120 * otherwise just request a firmware:
121 */
122 if ( uci->valid )
123 {
124 err = microcode_resume_cpu(cpu);
125 }
126 else
127 {
128 err = collect_cpu_info(cpu);
129 if ( !err && uci->valid )
130 err = microcode_ops->cpu_request_microcode(cpu, buf, size);
131 }
133 spin_unlock(&microcode_mutex);
135 return err;
136 }
138 static long do_microcode_update(void *_info)
139 {
140 struct microcode_info *info = _info;
141 int error;
143 BUG_ON(info->cpu != smp_processor_id());
145 error = microcode_update_cpu(info->buffer, info->buffer_size);
146 if ( error )
147 info->error = error;
149 info->cpu = next_cpu(info->cpu, cpu_online_map);
150 if ( info->cpu < NR_CPUS )
151 return continue_hypercall_on_cpu(info->cpu, do_microcode_update, info);
153 error = info->error;
154 xfree(info);
155 return error;
157 }
159 int microcode_update(XEN_GUEST_HANDLE(const_void) buf, unsigned long len)
160 {
161 int ret;
162 struct microcode_info *info;
164 if ( len != (uint32_t)len )
165 return -E2BIG;
167 if ( microcode_ops == NULL )
168 return -EINVAL;
170 info = xmalloc_bytes(sizeof(*info) + len);
171 if ( info == NULL )
172 return -ENOMEM;
174 ret = copy_from_guest(info->buffer, buf, len);
175 if ( ret != 0 )
176 {
177 xfree(info);
178 return ret;
179 }
181 info->buffer_size = len;
182 info->error = 0;
183 info->cpu = first_cpu(cpu_online_map);
185 return continue_hypercall_on_cpu(info->cpu, do_microcode_update, info);
186 }