cpuid.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* ----------------------------------------------------------------------- *
  3. *
  4. * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
  5. *
  6. * ----------------------------------------------------------------------- */
  7. /*
  8. * x86 CPUID access device
  9. *
  10. * This device is accessed by lseek() to the appropriate CPUID level
  11. * and then read in chunks of 16 bytes. A larger size means multiple
  12. * reads of consecutive levels.
  13. *
  14. * The lower 32 bits of the file position is used as the incoming %eax,
  15. * and the upper 32 bits of the file position as the incoming %ecx,
  16. * the latter intended for "counting" eax levels like eax=4.
  17. *
  18. * This driver uses /dev/cpu/%d/cpuid where %d is the minor number, and on
  19. * an SMP box will direct the access to CPU %d.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/errno.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/init.h>
  26. #include <linux/poll.h>
  27. #include <linux/smp.h>
  28. #include <linux/major.h>
  29. #include <linux/fs.h>
  30. #include <linux/device.h>
  31. #include <linux/cpu.h>
  32. #include <linux/notifier.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/gfp.h>
  35. #include <linux/completion.h>
  36. #include <asm/processor.h>
  37. #include <asm/msr.h>
  38. static struct class *cpuid_class;
  39. static enum cpuhp_state cpuhp_cpuid_state;
  40. struct cpuid_regs_done {
  41. struct cpuid_regs regs;
  42. struct completion done;
  43. };
  44. static void cpuid_smp_cpuid(void *cmd_block)
  45. {
  46. struct cpuid_regs_done *cmd = cmd_block;
  47. cpuid_count(cmd->regs.eax, cmd->regs.ecx,
  48. &cmd->regs.eax, &cmd->regs.ebx,
  49. &cmd->regs.ecx, &cmd->regs.edx);
  50. complete(&cmd->done);
  51. }
  52. static ssize_t cpuid_read(struct file *file, char __user *buf,
  53. size_t count, loff_t *ppos)
  54. {
  55. char __user *tmp = buf;
  56. struct cpuid_regs_done cmd;
  57. int cpu = iminor(file_inode(file));
  58. u64 pos = *ppos;
  59. ssize_t bytes = 0;
  60. int err = 0;
  61. if (count % 16)
  62. return -EINVAL; /* Invalid chunk size */
  63. init_completion(&cmd.done);
  64. for (; count; count -= 16) {
  65. call_single_data_t csd = {
  66. .func = cpuid_smp_cpuid,
  67. .info = &cmd,
  68. };
  69. cmd.regs.eax = pos;
  70. cmd.regs.ecx = pos >> 32;
  71. err = smp_call_function_single_async(cpu, &csd);
  72. if (err)
  73. break;
  74. wait_for_completion(&cmd.done);
  75. if (copy_to_user(tmp, &cmd.regs, 16)) {
  76. err = -EFAULT;
  77. break;
  78. }
  79. tmp += 16;
  80. bytes += 16;
  81. *ppos = ++pos;
  82. reinit_completion(&cmd.done);
  83. }
  84. return bytes ? bytes : err;
  85. }
  86. static int cpuid_open(struct inode *inode, struct file *file)
  87. {
  88. unsigned int cpu;
  89. struct cpuinfo_x86 *c;
  90. cpu = iminor(file_inode(file));
  91. if (cpu >= nr_cpu_ids || !cpu_online(cpu))
  92. return -ENXIO; /* No such CPU */
  93. c = &cpu_data(cpu);
  94. if (c->cpuid_level < 0)
  95. return -EIO; /* CPUID not supported */
  96. return 0;
  97. }
  98. /*
  99. * File operations we support
  100. */
  101. static const struct file_operations cpuid_fops = {
  102. .owner = THIS_MODULE,
  103. .llseek = no_seek_end_llseek,
  104. .read = cpuid_read,
  105. .open = cpuid_open,
  106. };
  107. static int cpuid_device_create(unsigned int cpu)
  108. {
  109. struct device *dev;
  110. dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL,
  111. "cpu%d", cpu);
  112. return PTR_ERR_OR_ZERO(dev);
  113. }
  114. static int cpuid_device_destroy(unsigned int cpu)
  115. {
  116. device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
  117. return 0;
  118. }
  119. static char *cpuid_devnode(struct device *dev, umode_t *mode)
  120. {
  121. return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt));
  122. }
  123. static int __init cpuid_init(void)
  124. {
  125. int err;
  126. if (__register_chrdev(CPUID_MAJOR, 0, NR_CPUS,
  127. "cpu/cpuid", &cpuid_fops)) {
  128. printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
  129. CPUID_MAJOR);
  130. return -EBUSY;
  131. }
  132. cpuid_class = class_create(THIS_MODULE, "cpuid");
  133. if (IS_ERR(cpuid_class)) {
  134. err = PTR_ERR(cpuid_class);
  135. goto out_chrdev;
  136. }
  137. cpuid_class->devnode = cpuid_devnode;
  138. err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/cpuid:online",
  139. cpuid_device_create, cpuid_device_destroy);
  140. if (err < 0)
  141. goto out_class;
  142. cpuhp_cpuid_state = err;
  143. return 0;
  144. out_class:
  145. class_destroy(cpuid_class);
  146. out_chrdev:
  147. __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
  148. return err;
  149. }
  150. module_init(cpuid_init);
  151. static void __exit cpuid_exit(void)
  152. {
  153. cpuhp_remove_state(cpuhp_cpuid_state);
  154. class_destroy(cpuid_class);
  155. __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
  156. }
  157. module_exit(cpuid_exit);
  158. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  159. MODULE_DESCRIPTION("x86 generic CPUID driver");
  160. MODULE_LICENSE("GPL");