Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // cpuinfo.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <asm/uaccess.h>
- /*
- static inline void native_cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx) {
- // ecx is often an input as well as an output.
- asm volatile("cpuid"
- : "=a" (*eax),
- "=b" (*ebx),
- "=c" (*ecx),
- "=d" (*edx)
- : "0" (*eax), "2" (*ecx)
- : "memory");
- }
- */
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("EIEI");
- MODULE_DESCRIPTION("\"cpuinfo\" Character Device");
- #define DEVICENAME "cpuinfo"
- static int dev_major;
- static int dev_open =0;
- static char *f_ptr;
- static int device_open(struct inode *, struct file *);
- static int device_release(struct inode *inode, struct file *file);
- static ssize_t device_read(struct file *, char *, size_t, loff_t *);
- // File operations structor
- // Only implement those that will be used.
- static struct file_operations dev_fops = {
- .read = device_read,
- .open = device_open,
- .release = device_release
- };
- int init_module(void) {
- printk(KERN_INFO "CPCHAR: dev osinfo init\n");
- dev_major=register_chrdev(0, DEVICENAME, &dev_fops);
- if (dev_major < 0) {
- printk(KERN_ALERT "Fail register_chrdev osinfo with %d\n",dev_major);
- return dev_major;
- }
- printk(KERN_INFO "Device MajorNumber %d.\n", dev_major);
- printk(KERN_INFO "To create a device file:\n");
- printk(KERN_INFO "\t'mknod /dev/%s c %d 0'.\n", DEVICENAME, dev_major);
- printk(KERN_INFO "Try varying minor numbers.\n");
- printk(KERN_INFO "Please remove the device file and module when done.\n");
- /* * non 0 - means init_module failed */
- return 0;
- }
- void cleanup_module(void) {
- printk(KERN_INFO "CPCHAR: dev osinfo cleanup\n");
- unregister_chrdev(dev_major, DEVICENAME);
- }
- static int device_open(struct inode *inode, struct file *file) {
- if (dev_open)
- return -EBUSY; dev_open++;
- printk(KERN_INFO "dev minor %d\n", MINOR(inode->i_rdev));
- // Code snippet
- unsigned eax, ebx, ecx, edx;
- // for obtaining the features
- eax = 0; /* processor info and feature bits */
- native_cpuid(&eax, &ebx, &ecx, &edx);
- char str1[100], f_data0[1000];
- snprintf(str1, 100, "Vendor ID ");
- strncat (f_data0, str1, 1000);
- snprintf(str1, 100, "%c%c%c%c", (ebx) & 0xFF, (ebx>>8) & 0xFF, (ebx>>16) & 0xFF, (ebx>>24) & 0xFF);
- strncat (f_data0, str1, 1000);
- snprintf(str1, 100, "%c%c%c%c", (edx) & 0xFF, (edx>>8) & 0xFF, (edx>>16) & 0xFF, (edx>>24) & 0xFF);
- strncat (f_data0, str1, 1000);
- snprintf(str1, 100, "%c%c%c%c\n", (ecx) & 0xFF, (ecx>>8) & 0xFF, (ecx>>16) & 0xFF, (ecx>>24) & 0xFF);
- strncat (f_data0, str1, 1000);
- eax = 1;
- native_cpuid(&eax, &ebx, &ecx, &edx);
- snprintf(str1, 100, "Stepping %d\n", eax & 0xF);
- strncat (f_data0, str1, 1000);
- snprintf(str1, 100, "Model %d\n", (eax >> 4) & 0xF);
- strncat (f_data0, str1, 1000);
- snprintf(str1, 100, "Family %d\n", (eax >> 8) & 0xF);
- strncat (f_data0, str1, 1000);
- snprintf(str1, 100, "Processor %d\n", (eax >> 12) & 0x3);
- strncat (f_data0, str1, 1000);
- snprintf(str1, 100, "Extended Model %d\n", (eax >> 16) & 0xF);
- strncat (f_data0, str1, 1000);
- snprintf(str1, 100, "Extended Family %d\n", (eax >> 20) & 0xFF);
- strncat (f_data0, str1, 1000);
- eax = 3;
- native_cpuid(&eax, &ebx, &ecx, &edx);
- snprintf(str1, 100, "Serial Number 0x%08x%08x\n", edx, ecx);
- strncat (f_data0, str1, 1000);
- f_ptr=(char * )f_data0;
- // lock module
- try_module_get(THIS_MODULE);
- return 0;
- }
- static int device_release(struct inode *inode, struct file *file) {
- dev_open--; /* We're now ready for our next caller */
- // release module
- module_put(THIS_MODULE);
- return 0;
- }
- static ssize_t device_read(struct file *filp,
- char *buffer,
- /* see include/linux/fs.h */
- /* buffer to fill with data */
- /* length of the buffer */
- size_t length,
- loff_t * offset) {
- int bytes_read=0;
- if (*f_ptr ==0) {
- return 0;
- }
- while (length && *f_ptr) {
- put_user(*(f_ptr++), buffer++); length--;
- bytes_read++;
- }
- return bytes_read;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement