Chinaunix首页 | 论坛 | 博客
  • 博客访问: 693358
  • 博文数量: 192
  • 博客积分: 1875
  • 博客等级: 上尉
  • 技术积分: 2177
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-23 23:21
个人简介

有时候,就是想窥视一下不知道的东东,因为好奇!

文章分类

全部博文(192)

文章存档

2024年(8)

2023年(3)

2020年(1)

2019年(1)

2018年(1)

2017年(2)

2016年(69)

2015年(53)

2014年(14)

2013年(1)

2012年(5)

2011年(25)

2010年(9)

分类: LINUX

2011-04-21 22:49:27

通过for_each_process(p)可以扫描整个进程链表。

其中p->mm值为0的任务就是所谓的内核线程。

从内核态的sp可以得到当前任务的指针: (sp & 0xffffe000)->task

对于s3c2440,外接的64MB的RAM的物理地址空间是0x30000000-0x34000000,
映射到0xc0000000-0xc4000000, 虚拟地址与物理地址相差0x90000000,与ttb和current->mm->pgd
之间相差0x90000000相符(在x86的cpu对应是0xc0000000)。

软/硬件环境:linux-2.6.36/s3c2440

test.c
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/proc_fs.h>
  4. #include <linux/sched.h>
  5. #include <linux/string.h>
  6. #include <linux/vmalloc.h>
  7. #include <asm/uaccess.h>

  8. MODULE_LICENSE("GPL");
  9. MODULE_AUTHOR("zhanglong");
  10. #define MAX_COOKIE_LENGTH PAGE_SIZE

  11. static struct proc_dir_entry *proc_entry;
  12. static char *cookie_pot; // Space for procfile strings

  13. ssize_t proc_write(struct file *filp, const char __user *buff,
  14.         unsigned long len, void *data)
  15. {
  16.    return 1; //如果返回0, 写数据时不能返回
  17. }

  18. int proc_read(char *page, char **start, off_t off,
  19.         int count, int *eof, void *data)
  20. {
  21.     int len = 0;
  22.     int num = 0;
  23.     unsigned long ttb = 0;
  24.     unsigned long sp = 0;
  25.     struct task_struct *p;
  26.     struct vm_area_struct *vm = current->mm->mmap;
  27.     struct thread_info *thread;

  28.     memset(page, 0, MAX_COOKIE_LENGTH);
  29. //从c2读ttb
  30.     __asm__ __volatile__ (
  31.             "mrc p15, 0, %0, c2, c0, 0\n"
  32.             "mov %1, r13\n"
  33.             :"=r" (ttb), "=r" (sp)
  34.     );

  35.     thread = (struct thread_info *)(sp & 0xffffe000);
  36.            
  37.     sprintf(page + len, "***********************************************\n");
  38.     len = strlen(page);
  39.     sprintf(page + len, "current->comm: %s\n", current->comm); //执行当前进程的命令的名字
  40.     len = strlen(page);
  41.     sprintf(page + len, "current->state: %d\n", current->state); //运行状态
  42.     len = strlen(page);
  43.     sprintf(page + len, "currentr->mm = 0x%08x\n", current->mm);
  44.     len = strlen(page);
  45.     sprintf(page + len, "current = 0x%08x\n", current);
  46.     len = strlen(page);
  47.     sprintf(page + len, "thread->task = 0x%08x\n", thread->task);
  48.     len = strlen(page);

  49.     sprintf(page + len, "ttb = 0x%08x\n", ttb);
  50.     len = strlen(page);
  51.     sprintf(page + len, "currentr->mm->pgd = 0x%08x\n", current->mm->pgd); //页目录表的地址(好象与ttb相差0x90000000)
  52.     len = strlen(page);

  53.     for_each_process(p){
  54.         num++;
  55.         sprintf(page + len, "%03d-[pid = %04d]-[mm = 0x%08x] comm: %s\n", num, p->pid, p->mm, p->comm); //
  56.         len = strlen(page);
  57.     }

  58.     sprintf(page + len, "len = %d\n", len); //环境变量的起始地址
  59.     len = strlen(page);

  60.     return len;
  61. }


  62. int init_proc_module(void)
  63. {
  64.     int ret = 0;
  65.     cookie_pot = (char *)vmalloc(MAX_COOKIE_LENGTH);
  66.     if (!cookie_pot)
  67.     {
  68.         ret = -ENOMEM;
  69.     }
  70.     else
  71.     {
  72.         memset(cookie_pot, 0, MAX_COOKIE_LENGTH);
  73.         proc_entry = create_proc_entry("procfile", 0644, NULL);
  74.         if (proc_entry == NULL)
  75.         {
  76.             ret = -ENOMEM;
  77.             vfree(cookie_pot);
  78.             printk(KERN_INFO "proc: Couldn't create proc entry\n");
  79.         }
  80.         else
  81.         {
  82.             proc_entry->read_proc = proc_read;
  83.             proc_entry->write_proc = proc_write;
  84.             //proc_entry->owner = THIS_MODULE;
  85.             printk(KERN_INFO "proc: Module loaded.\n");
  86.         }
  87.     }
  88.     return ret;
  89. }
  90. void cleanup_proc_module(void)
  91. {
  92.     remove_proc_entry("proc", NULL);
  93.     vfree(cookie_pot);
  94.     printk(KERN_INFO "proc: Module unloaded.\n");
  95. }
  96. module_init(init_proc_module);
  97. module_exit(cleanup_proc_module);
Makefile
  1. obj-m := test.o

  2. KERNELS = /media/STUDY/linux/kernel/my2440-2.6.36
  3. # KERNELS = /lib/modules/$(shell uname -r)/build/

  4. default:
  5. make -C $(KERNELS) M=$(shell pwd) modules

  6. .PHONY:clean
  7. clean:
  8. make -C $(KERNELS) M=$(shell pwd) clean
执行make 命令后插入生成的test.ko模块。再执行命令cat /proc/procfile有如下输出:

[root@zhanglong proc]# cat /proc/procfile
***********************************************
current->comm: cat
current->state: 0
currentr->mm = 0xc3af8300
current = 0xc3893420
thread->task = 0xc3893420
ttb = 0x33a14000
currentr->mm->pgd = 0xc3a14000
001-[pid = 0001]-[mm = 0xc3a0f000] comm: init
002-[pid = 0002]-[mm = 0x00000000] comm: kthreadd
003-[pid = 0003]-[mm = 0x00000000] comm: ksoftirqd/0
004-[pid = 0004]-[mm = 0x00000000] comm: kworker/0:0
005-[pid = 0005]-[mm = 0x00000000] comm: kworker/u:0
006-[pid = 0006]-[mm = 0x00000000] comm: khelper
007-[pid = 0155]-[mm = 0x00000000] comm: sync_supers
008-[pid = 0157]-[mm = 0x00000000] comm: bdi-default
009-[pid = 0158]-[mm = 0x00000000] comm: kintegrityd
010-[pid = 0160]-[mm = 0x00000000] comm: kblockd
011-[pid = 0169]-[mm = 0x00000000] comm: khubd
012-[pid = 0172]-[mm = 0x00000000] comm: kseriod
013-[pid = 0176]-[mm = 0x00000000] comm: kmmcd
014-[pid = 0265]-[mm = 0x00000000] comm: rpciod
015-[pid = 0267]-[mm = 0x00000000] comm: kworker/0:1
016-[pid = 0273]-[mm = 0x00000000] comm: kswapd0
017-[pid = 0325]-[mm = 0x00000000] comm: fsnotify_mark
018-[pid = 0327]-[mm = 0x00000000] comm: aio
019-[pid = 0334]-[mm = 0x00000000] comm: nfsiod
020-[pid = 0342]-[mm = 0x00000000] comm: crypto
021-[pid = 0615]-[mm = 0x00000000] comm: kapmd
022-[pid = 0662]-[mm = 0x00000000] comm: mtdblock0
023-[pid = 0667]-[mm = 0x00000000] comm: mtdblock1
024-[pid = 0672]-[mm = 0x00000000] comm: mtdblock2
025-[pid = 0693]-[mm = 0x00000000] comm: kpsmoused
026-[pid = 0729]-[mm = 0x00000000] comm: usbhid_resumer
027-[pid = 0774]-[mm = 0xc3a0f480] comm: syslogd
028-[pid = 0777]-[mm = 0xc3a0f300] comm: inetd
029-[pid = 0781]-[mm = 0xc3a0f780] comm: boa
030-[pid = 0789]-[mm = 0xc3af8900] comm: qpe
031-[pid = 0790]-[mm = 0xc3af8480] comm: sh
032-[pid = 0791]-[mm = 0xc3a0f900] comm: init
033-[pid = 0793]-[mm = 0xc3a0f600] comm: init
034-[pid = 0795]-[mm = 0xc3a0fd80] comm: init
035-[pid = 0797]-[mm = 0x00000000] comm: kworker/u:2
036-[pid = 0801]-[mm = 0x00000000] comm: flush-0:14
037-[pid = 0808]-[mm = 0xc3af8000] comm: qss
038-[pid = 0809]-[mm = 0xc3af8600] comm: quicklauncher
039-[pid = 0815]-[mm = 0xc3af8300] comm: cat
len = 3130
[root@zhanglong proc]#



阅读(2199) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~