Advertisement
Guest User

Checkpoint4

a guest
Nov 14th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. // cpuinfo.c
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/fs.h>
  5. #include <asm/uaccess.h>
  6.  
  7. /*
  8. static inline void native_cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx) {
  9. // ecx is often an input as well as an output.
  10. asm volatile("cpuid"
  11. : "=a" (*eax),
  12. "=b" (*ebx),
  13. "=c" (*ecx),
  14. "=d" (*edx)
  15. : "0" (*eax), "2" (*ecx)
  16. : "memory");
  17. }
  18. */
  19.  
  20. MODULE_LICENSE("GPL");
  21. MODULE_AUTHOR("EIEI");
  22. MODULE_DESCRIPTION("\"cpuinfo\" Character Device");
  23.  
  24. #define DEVICENAME "cpuinfo"
  25. static int dev_major;
  26. static int dev_open =0;
  27. static char *f_ptr;
  28.  
  29. static int device_open(struct inode *, struct file *);
  30. static int device_release(struct inode *inode, struct file *file);
  31. static ssize_t device_read(struct file *, char *, size_t, loff_t *);
  32. // File operations structor
  33. // Only implement those that will be used.
  34. static struct file_operations dev_fops = {
  35. .read = device_read,
  36. .open = device_open,
  37. .release = device_release
  38. };
  39.  
  40. int init_module(void) {
  41. printk(KERN_INFO "CPCHAR: dev osinfo init\n");
  42. dev_major=register_chrdev(0, DEVICENAME, &dev_fops);
  43. if (dev_major < 0) {
  44. printk(KERN_ALERT "Fail register_chrdev osinfo with %d\n",dev_major);
  45. return dev_major;
  46. }
  47. printk(KERN_INFO "Device MajorNumber %d.\n", dev_major);
  48. printk(KERN_INFO "To create a device file:\n");
  49. printk(KERN_INFO "\t'mknod /dev/%s c %d 0'.\n", DEVICENAME, dev_major);
  50. printk(KERN_INFO "Try varying minor numbers.\n");
  51. printk(KERN_INFO "Please remove the device file and module when done.\n");
  52. /* * non 0 - means init_module failed */
  53. return 0;
  54. }
  55.  
  56. void cleanup_module(void) {
  57. printk(KERN_INFO "CPCHAR: dev osinfo cleanup\n");
  58. unregister_chrdev(dev_major, DEVICENAME);
  59. }
  60.  
  61. static int device_open(struct inode *inode, struct file *file) {
  62. if (dev_open)
  63. return -EBUSY; dev_open++;
  64. printk(KERN_INFO "dev minor %d\n", MINOR(inode->i_rdev));
  65.  
  66. // Code snippet
  67. unsigned eax, ebx, ecx, edx;
  68. // for obtaining the features
  69. eax = 0; /* processor info and feature bits */
  70. native_cpuid(&eax, &ebx, &ecx, &edx);
  71.  
  72. char str1[100], f_data0[1000];
  73. snprintf(str1, 100, "Vendor ID ");
  74. strncat (f_data0, str1, 1000);
  75. snprintf(str1, 100, "%c%c%c%c", (ebx) & 0xFF, (ebx>>8) & 0xFF, (ebx>>16) & 0xFF, (ebx>>24) & 0xFF);
  76. strncat (f_data0, str1, 1000);
  77. snprintf(str1, 100, "%c%c%c%c", (edx) & 0xFF, (edx>>8) & 0xFF, (edx>>16) & 0xFF, (edx>>24) & 0xFF);
  78. strncat (f_data0, str1, 1000);
  79. snprintf(str1, 100, "%c%c%c%c\n", (ecx) & 0xFF, (ecx>>8) & 0xFF, (ecx>>16) & 0xFF, (ecx>>24) & 0xFF);
  80. strncat (f_data0, str1, 1000);
  81.  
  82. eax = 1;
  83. native_cpuid(&eax, &ebx, &ecx, &edx);
  84. snprintf(str1, 100, "Stepping %d\n", eax & 0xF);
  85. strncat (f_data0, str1, 1000);
  86. snprintf(str1, 100, "Model %d\n", (eax >> 4) & 0xF);
  87. strncat (f_data0, str1, 1000);
  88. snprintf(str1, 100, "Family %d\n", (eax >> 8) & 0xF);
  89. strncat (f_data0, str1, 1000);
  90. snprintf(str1, 100, "Processor %d\n", (eax >> 12) & 0x3);
  91. strncat (f_data0, str1, 1000);
  92. snprintf(str1, 100, "Extended Model %d\n", (eax >> 16) & 0xF);
  93. strncat (f_data0, str1, 1000);
  94. snprintf(str1, 100, "Extended Family %d\n", (eax >> 20) & 0xFF);
  95. strncat (f_data0, str1, 1000);
  96.  
  97. eax = 3;
  98. native_cpuid(&eax, &ebx, &ecx, &edx);
  99. snprintf(str1, 100, "Serial Number 0x%08x%08x\n", edx, ecx);
  100. strncat (f_data0, str1, 1000);
  101.  
  102.  
  103. f_ptr=(char * )f_data0;
  104.  
  105. // lock module
  106. try_module_get(THIS_MODULE);
  107. return 0;
  108. }
  109.  
  110. static int device_release(struct inode *inode, struct file *file) {
  111. dev_open--; /* We're now ready for our next caller */
  112. // release module
  113. module_put(THIS_MODULE);
  114. return 0;
  115. }
  116.  
  117. static ssize_t device_read(struct file *filp,
  118. char *buffer,
  119. /* see include/linux/fs.h */
  120. /* buffer to fill with data */
  121. /* length of the buffer */
  122. size_t length,
  123. loff_t * offset) {
  124. int bytes_read=0;
  125. if (*f_ptr ==0) {
  126. return 0;
  127. }
  128. while (length && *f_ptr) {
  129. put_user(*(f_ptr++), buffer++); length--;
  130. bytes_read++;
  131. }
  132. return bytes_read;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement