2 * Intel CPU Microcode Update Driver for Linux
4 * Copyright (C) 2000-2004 Tigran Aivazian
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.
10 * Reference: Section 8.10 of Volume III, Intel Pentium 4 Manual,
11 * Order Number 245472 or free download from:
13 * http://developer.intel.com/design/pentium4/manuals/245472.htm
15 * For more information, go to http://www.urbanmyth.org/microcode
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
23 //#define DEBUG /* pr_debug */
24 #include <linux/capability.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/sched.h>
28 #include <linux/cpumask.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/vmalloc.h>
32 #include <linux/miscdevice.h>
33 #include <linux/spinlock.h>
35 #include <linux/mutex.h>
36 #include <linux/syscalls.h>
39 #include <asm/uaccess.h>
40 #include <asm/processor.h>
42 MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
43 MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
44 MODULE_LICENSE("GPL");
46 #define MICROCODE_VERSION "1.14-xen"
48 #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
49 #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
50 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
52 /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
53 static DEFINE_MUTEX(microcode_mutex);
55 static void __user *user_buffer; /* user area microcode data buffer */
56 static unsigned int user_buffer_size; /* it's size */
58 static int microcode_open (struct inode *unused1, struct file *unused2)
60 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
64 static int do_microcode_update (void)
69 err = sys_mlock((unsigned long)user_buffer, user_buffer_size);
73 op.cmd = DOM0_MICROCODE;
74 set_xen_guest_handle(op.u.microcode.data, user_buffer);
75 op.u.microcode.length = user_buffer_size;
76 err = HYPERVISOR_dom0_op(&op);
78 (void)sys_munlock((unsigned long)user_buffer, user_buffer_size);
83 static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
87 if ((len >> PAGE_SHIFT) > num_physpages) {
88 printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
92 mutex_lock(µcode_mutex);
94 user_buffer = (void __user *) buf;
95 user_buffer_size = (int) len;
97 ret = do_microcode_update();
101 mutex_unlock(µcode_mutex);
106 static struct file_operations microcode_fops = {
107 .owner = THIS_MODULE,
108 .write = microcode_write,
109 .open = microcode_open,
112 static struct miscdevice microcode_dev = {
113 .minor = MICROCODE_MINOR,
115 .fops = µcode_fops,
118 static int __init microcode_init (void)
122 error = misc_register(µcode_dev);
125 "microcode: can't misc_register on minor=%d\n",
131 "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@veritas.com>\n");
135 static void __exit microcode_exit (void)
137 misc_deregister(µcode_dev);
140 module_init(microcode_init)
141 module_exit(microcode_exit)
142 MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);