Revert "tty: hvc: Fix data abort due to race in hvc_open"
[linux/fpc-iii.git] / arch / x86 / kernel / msr.c
blob1547be359d7f5a539ab470381654b6b02c4bf102
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* ----------------------------------------------------------------------- *
4 * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
5 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
7 * ----------------------------------------------------------------------- */
9 /*
10 * x86 MSR access device
12 * This device is accessed by lseek() to the appropriate register number
13 * and then read/write in chunks of 8 bytes. A larger size means multiple
14 * reads or writes of the same register.
16 * This driver uses /dev/cpu/%d/msr where %d is the minor number, and on
17 * an SMP box will direct the access to CPU %d.
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/fcntl.h>
27 #include <linux/init.h>
28 #include <linux/poll.h>
29 #include <linux/smp.h>
30 #include <linux/major.h>
31 #include <linux/fs.h>
32 #include <linux/device.h>
33 #include <linux/cpu.h>
34 #include <linux/notifier.h>
35 #include <linux/uaccess.h>
36 #include <linux/gfp.h>
37 #include <linux/security.h>
39 #include <asm/cpufeature.h>
40 #include <asm/msr.h>
42 static struct class *msr_class;
43 static enum cpuhp_state cpuhp_msr_state;
45 static ssize_t msr_read(struct file *file, char __user *buf,
46 size_t count, loff_t *ppos)
48 u32 __user *tmp = (u32 __user *) buf;
49 u32 data[2];
50 u32 reg = *ppos;
51 int cpu = iminor(file_inode(file));
52 int err = 0;
53 ssize_t bytes = 0;
55 if (count % 8)
56 return -EINVAL; /* Invalid chunk size */
58 for (; count; count -= 8) {
59 err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
60 if (err)
61 break;
62 if (copy_to_user(tmp, &data, 8)) {
63 err = -EFAULT;
64 break;
66 tmp += 2;
67 bytes += 8;
70 return bytes ? bytes : err;
73 static ssize_t msr_write(struct file *file, const char __user *buf,
74 size_t count, loff_t *ppos)
76 const u32 __user *tmp = (const u32 __user *)buf;
77 u32 data[2];
78 u32 reg = *ppos;
79 int cpu = iminor(file_inode(file));
80 int err = 0;
81 ssize_t bytes = 0;
83 err = security_locked_down(LOCKDOWN_MSR);
84 if (err)
85 return err;
87 if (count % 8)
88 return -EINVAL; /* Invalid chunk size */
90 for (; count; count -= 8) {
91 if (copy_from_user(&data, tmp, 8)) {
92 err = -EFAULT;
93 break;
95 err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
96 if (err)
97 break;
98 tmp += 2;
99 bytes += 8;
102 return bytes ? bytes : err;
105 static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
107 u32 __user *uregs = (u32 __user *)arg;
108 u32 regs[8];
109 int cpu = iminor(file_inode(file));
110 int err;
112 switch (ioc) {
113 case X86_IOC_RDMSR_REGS:
114 if (!(file->f_mode & FMODE_READ)) {
115 err = -EBADF;
116 break;
118 if (copy_from_user(&regs, uregs, sizeof(regs))) {
119 err = -EFAULT;
120 break;
122 err = rdmsr_safe_regs_on_cpu(cpu, regs);
123 if (err)
124 break;
125 if (copy_to_user(uregs, &regs, sizeof(regs)))
126 err = -EFAULT;
127 break;
129 case X86_IOC_WRMSR_REGS:
130 if (!(file->f_mode & FMODE_WRITE)) {
131 err = -EBADF;
132 break;
134 if (copy_from_user(&regs, uregs, sizeof(regs))) {
135 err = -EFAULT;
136 break;
138 err = security_locked_down(LOCKDOWN_MSR);
139 if (err)
140 break;
141 err = wrmsr_safe_regs_on_cpu(cpu, regs);
142 if (err)
143 break;
144 if (copy_to_user(uregs, &regs, sizeof(regs)))
145 err = -EFAULT;
146 break;
148 default:
149 err = -ENOTTY;
150 break;
153 return err;
156 static int msr_open(struct inode *inode, struct file *file)
158 unsigned int cpu = iminor(file_inode(file));
159 struct cpuinfo_x86 *c;
161 if (!capable(CAP_SYS_RAWIO))
162 return -EPERM;
164 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
165 return -ENXIO; /* No such CPU */
167 c = &cpu_data(cpu);
168 if (!cpu_has(c, X86_FEATURE_MSR))
169 return -EIO; /* MSR not supported */
171 return 0;
175 * File operations we support
177 static const struct file_operations msr_fops = {
178 .owner = THIS_MODULE,
179 .llseek = no_seek_end_llseek,
180 .read = msr_read,
181 .write = msr_write,
182 .open = msr_open,
183 .unlocked_ioctl = msr_ioctl,
184 .compat_ioctl = msr_ioctl,
187 static int msr_device_create(unsigned int cpu)
189 struct device *dev;
191 dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
192 "msr%d", cpu);
193 return PTR_ERR_OR_ZERO(dev);
196 static int msr_device_destroy(unsigned int cpu)
198 device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
199 return 0;
202 static char *msr_devnode(struct device *dev, umode_t *mode)
204 return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
207 static int __init msr_init(void)
209 int err;
211 if (__register_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr", &msr_fops)) {
212 pr_err("unable to get major %d for msr\n", MSR_MAJOR);
213 return -EBUSY;
215 msr_class = class_create(THIS_MODULE, "msr");
216 if (IS_ERR(msr_class)) {
217 err = PTR_ERR(msr_class);
218 goto out_chrdev;
220 msr_class->devnode = msr_devnode;
222 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/msr:online",
223 msr_device_create, msr_device_destroy);
224 if (err < 0)
225 goto out_class;
226 cpuhp_msr_state = err;
227 return 0;
229 out_class:
230 class_destroy(msr_class);
231 out_chrdev:
232 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
233 return err;
235 module_init(msr_init);
237 static void __exit msr_exit(void)
239 cpuhp_remove_state(cpuhp_msr_state);
240 class_destroy(msr_class);
241 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
243 module_exit(msr_exit)
245 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
246 MODULE_DESCRIPTION("x86 generic MSR driver");
247 MODULE_LICENSE("GPL");