Advertisement
micromike

linux kernel project 1

Jan 20th, 2013
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. /*
  2.  *Introuduction:
  3.  *  This system call will take a pid as parameter, and get the process virtual address,
  4.  *and physical address which match the pid.
  5.  */
  6. #ifndef __LINUX_PROJECT
  7. #define __LINUX_PROJECT
  8.  
  9. #include <linux/linkage.h>
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <asm-i386/page.h>
  14. #include <asm-i386/pgtable.h>
  15.  
  16.  
  17. asmlinkage long sys_project(unsigned long pid1, unsigned long pid2){
  18.     struct vm_area_struct *vm;  /* loop counter to loop through all vm section */
  19.     struct page *page;
  20.     unsigned long vm_address;   /* the virtual address */
  21.     struct task_struct *p;      /* the process descriptor */
  22.     unsigned long pfn;
  23.     unsigned short flag = 0;   
  24.     /*the total page frame is the current system. Do not modified this value. */   
  25.     extern unsigned long max_low_pfn;  
  26.     printk("total page frame : %lx \n", max_low_pfn);
  27.     printk("host memory size is about\t%lu MB\n", pages_to_mb(max_low_pfn));
  28.     /*
  29.      * loop through the whole process list.
  30.      * I think it is the same as the find_task_by_pid().
  31.      */
  32.     for_each_process(p){       
  33.         if (p->pid == pid1){
  34.             flag =1;   
  35.             break;
  36.         }      
  37.     }  
  38.     /* it's time to get the virtual address and physical address of the process */
  39.     if (flag){
  40.         /* check if NULL pointer */
  41.         if(p->mm == NULL || p->mm->pgd == NULL || p->mm->mmap == NULL){
  42.             printk("ptr is NULL\n");
  43.             goto out;
  44.         }
  45.         printk("vm section for process1 pid:%u:\n",pid1);
  46.         /* loop through vm interval */
  47.         for ( vm = p->mm->mmap; vm != NULL; vm = vm->vm_next){
  48.             /* print vm section start and end */
  49.             printk("from 0x%08lx~0x%08lx\n",vm->vm_start,vm->vm_end);
  50.             printk("page frame of the interval : \n");
  51.             for (   vm_address = vm->vm_start;
  52.                 vm_address < vm->vm_end;
  53.                 vm_address += 0x1000)
  54.             {
  55.                 page = follow_page(vm, vm_address, 0);
  56.                 if ( page == NULL) continue;
  57.                 pfn = page_to_pfn(page);
  58.                 printk("0x%x ",pfn);
  59.             }
  60.             printk("\n");
  61.         }      
  62.     }  
  63. /*if some error happened just jumps to here*/  
  64. out:   
  65.     return 0;
  66.  
  67. }
  68.  
  69. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement