]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - arch/x86/kernel/msr.c
UBUNTU: SAUCE: UEFI: x86: Restrict MSR access when module loading is restricted
[mirror_ubuntu-zesty-kernel.git] / arch / x86 / kernel / msr.c
CommitLineData
1da177e4 1/* ----------------------------------------------------------------------- *
2b06ac86
PA
2 *
3 * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
ff55df53 4 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
1da177e4
LT
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
9 * USA; either version 2 of the License, or (at your option) any later
10 * version; incorporated herein by reference.
11 *
12 * ----------------------------------------------------------------------- */
13
14/*
1da177e4
LT
15 * x86 MSR access device
16 *
17 * This device is accessed by lseek() to the appropriate register number
18 * and then read/write in chunks of 8 bytes. A larger size means multiple
19 * reads or writes of the same register.
20 *
21 * This driver uses /dev/cpu/%d/msr where %d is the minor number, and on
22 * an SMP box will direct the access to CPU %d.
23 */
24
951a18c6
FF
25#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
1da177e4 27#include <linux/module.h>
1da177e4
LT
28
29#include <linux/types.h>
30#include <linux/errno.h>
31#include <linux/fcntl.h>
32#include <linux/init.h>
33#include <linux/poll.h>
34#include <linux/smp.h>
1da177e4
LT
35#include <linux/major.h>
36#include <linux/fs.h>
37#include <linux/device.h>
38#include <linux/cpu.h>
39#include <linux/notifier.h>
448dd2fa 40#include <linux/uaccess.h>
5a0e3ad6 41#include <linux/gfp.h>
1da177e4 42
cd4d09ec 43#include <asm/cpufeature.h>
1da177e4 44#include <asm/msr.h>
1da177e4 45
8874b414 46static struct class *msr_class;
8fba38c9 47static enum cpuhp_state cpuhp_msr_state;
1da177e4 48
94a9fa41
PC
49static ssize_t msr_read(struct file *file, char __user *buf,
50 size_t count, loff_t *ppos)
1da177e4
LT
51{
52 u32 __user *tmp = (u32 __user *) buf;
53 u32 data[2];
1da177e4 54 u32 reg = *ppos;
6131ffaa 55 int cpu = iminor(file_inode(file));
85f1cb60
PA
56 int err = 0;
57 ssize_t bytes = 0;
1da177e4
LT
58
59 if (count % 8)
60 return -EINVAL; /* Invalid chunk size */
61
6926d570 62 for (; count; count -= 8) {
78a62d2c 63 err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
0cc0213e 64 if (err)
85f1cb60 65 break;
85f1cb60
PA
66 if (copy_to_user(tmp, &data, 8)) {
67 err = -EFAULT;
68 break;
c6f31932 69 }
1da177e4 70 tmp += 2;
85f1cb60 71 bytes += 8;
1da177e4
LT
72 }
73
85f1cb60 74 return bytes ? bytes : err;
1da177e4
LT
75}
76
77static ssize_t msr_write(struct file *file, const char __user *buf,
78 size_t count, loff_t *ppos)
79{
80 const u32 __user *tmp = (const u32 __user *)buf;
81 u32 data[2];
1da177e4 82 u32 reg = *ppos;
6131ffaa 83 int cpu = iminor(file_inode(file));
85f1cb60
PA
84 int err = 0;
85 ssize_t bytes = 0;
1da177e4 86
34c83a05
MG
87 if (secure_modules())
88 return -EPERM;
89
1da177e4
LT
90 if (count % 8)
91 return -EINVAL; /* Invalid chunk size */
92
f475ff35 93 for (; count; count -= 8) {
85f1cb60
PA
94 if (copy_from_user(&data, tmp, 8)) {
95 err = -EFAULT;
96 break;
97 }
78a62d2c 98 err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
0cc0213e 99 if (err)
85f1cb60 100 break;
1da177e4 101 tmp += 2;
85f1cb60 102 bytes += 8;
1da177e4
LT
103 }
104
85f1cb60 105 return bytes ? bytes : err;
1da177e4
LT
106}
107
ff55df53
PA
108static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
109{
110 u32 __user *uregs = (u32 __user *)arg;
111 u32 regs[8];
6131ffaa 112 int cpu = iminor(file_inode(file));
ff55df53
PA
113 int err;
114
115 switch (ioc) {
116 case X86_IOC_RDMSR_REGS:
117 if (!(file->f_mode & FMODE_READ)) {
118 err = -EBADF;
119 break;
120 }
121 if (copy_from_user(&regs, uregs, sizeof regs)) {
122 err = -EFAULT;
123 break;
124 }
125 err = rdmsr_safe_regs_on_cpu(cpu, regs);
126 if (err)
127 break;
128 if (copy_to_user(uregs, &regs, sizeof regs))
129 err = -EFAULT;
130 break;
131
132 case X86_IOC_WRMSR_REGS:
133 if (!(file->f_mode & FMODE_WRITE)) {
134 err = -EBADF;
135 break;
136 }
34c83a05
MG
137 if (secure_modules()) {
138 err = -EPERM;
139 break;
140 }
ff55df53
PA
141 if (copy_from_user(&regs, uregs, sizeof regs)) {
142 err = -EFAULT;
143 break;
144 }
145 err = wrmsr_safe_regs_on_cpu(cpu, regs);
146 if (err)
147 break;
148 if (copy_to_user(uregs, &regs, sizeof regs))
149 err = -EFAULT;
150 break;
151
152 default:
153 err = -ENOTTY;
154 break;
155 }
156
157 return err;
158}
159
1da177e4
LT
160static int msr_open(struct inode *inode, struct file *file)
161{
6131ffaa 162 unsigned int cpu = iminor(file_inode(file));
494c2ebf 163 struct cpuinfo_x86 *c;
1da177e4 164
c903f045
AC
165 if (!capable(CAP_SYS_RAWIO))
166 return -EPERM;
167
d6c30405
FW
168 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
169 return -ENXIO; /* No such CPU */
170
5119e92e
JC
171 c = &cpu_data(cpu);
172 if (!cpu_has(c, X86_FEATURE_MSR))
d6c30405
FW
173 return -EIO; /* MSR not supported */
174
175 return 0;
1da177e4
LT
176}
177
178/*
179 * File operations we support
180 */
5dfe4c96 181static const struct file_operations msr_fops = {
1da177e4 182 .owner = THIS_MODULE,
b25472f9 183 .llseek = no_seek_end_llseek,
1da177e4
LT
184 .read = msr_read,
185 .write = msr_write,
186 .open = msr_open,
ff55df53
PA
187 .unlocked_ioctl = msr_ioctl,
188 .compat_ioctl = msr_ioctl,
1da177e4
LT
189};
190
8fba38c9 191static int msr_device_create(unsigned int cpu)
1da177e4 192{
a271aaf1 193 struct device *dev;
1da177e4 194
a9b12619
GKH
195 dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
196 "msr%d", cpu);
cba0fdbc 197 return PTR_ERR_OR_ZERO(dev);
881a841f
AM
198}
199
8fba38c9 200static int msr_device_destroy(unsigned int cpu)
881a841f
AM
201{
202 device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
8fba38c9 203 return 0;
1da177e4
LT
204}
205
2c9ede55 206static char *msr_devnode(struct device *dev, umode_t *mode)
07e9bb8e
KS
207{
208 return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
209}
210
1da177e4
LT
211static int __init msr_init(void)
212{
8fba38c9 213 int err;
1da177e4 214
0b962d47 215 if (__register_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr", &msr_fops)) {
951a18c6 216 pr_err("unable to get major %d for msr\n", MSR_MAJOR);
8fba38c9 217 return -EBUSY;
1da177e4 218 }
8874b414 219 msr_class = class_create(THIS_MODULE, "msr");
1da177e4
LT
220 if (IS_ERR(msr_class)) {
221 err = PTR_ERR(msr_class);
222 goto out_chrdev;
223 }
e454cea2 224 msr_class->devnode = msr_devnode;
de82a01b 225
8fba38c9
SAS
226 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/msr:online",
227 msr_device_create, msr_device_destroy);
228 if (err < 0)
229 goto out_class;
230 cpuhp_msr_state = err;
231 return 0;
1da177e4
LT
232
233out_class:
8874b414 234 class_destroy(msr_class);
1da177e4 235out_chrdev:
0b962d47 236 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
1da177e4
LT
237 return err;
238}
8fba38c9 239module_init(msr_init);
1da177e4
LT
240
241static void __exit msr_exit(void)
242{
8fba38c9 243 cpuhp_remove_state(cpuhp_msr_state);
8874b414 244 class_destroy(msr_class);
da482474 245 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
1da177e4 246}
1da177e4
LT
247module_exit(msr_exit)
248
249MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
250MODULE_DESCRIPTION("x86 generic MSR driver");
251MODULE_LICENSE("GPL");