0

Here is the relevant piece of source code: ......

static unsigned long vaddr2paddr(struct seq_file *m, unsigned long vaddr,int pid)
{
    pte_t *pte_tmp = NULL;  
    pmd_t *pmd_tmp = NULL;  
    pud_t *pud_tmp = NULL;  
    pgd_t *pgd_tmp = NULL;  
    struct task_struct *pcb_tmp = NULL;
    unsigned long paddr = 0;

    printk(KERN_INFO"in vaddr2paddr try to find the task %d .\n",pid);  
    pcb_tmp =pid_task(find_get_pid(pid),PIDTYPE_PID); 
    if(!pcb_tmp) {
         printk(KERN_INFO"Can't find the task %d .\n",pid);
         return 0;
    }
    
  pgd_tmp = pgd_offset(pcb_tmp->mm, vaddr);
  if (pgd_none(*pgd_tmp)) {
      printk("not mapped in pgd\n");
      return -1;
  }
    

/73/

pud_tmp = pud_offset(pgd_tmp, vaddr);  
  if (pud_none(*pud_tmp)) {
      printk("not mapped in pud\n");
      return -1;
  }


  pmd_tmp = pmd_offset(pud_tmp, vaddr);
  if (pmd_none(*pmd_tmp)) {
      printk("not mapped in pmd\n");
      return -1;
  }


  //pte = pte_offset_kernel(pmd, vaddr);
  pte_tmp = pte_offset_kernel(pmd_tmp, vaddr);    
  if (pte_none(*pte_tmp)) {
      printk("not mapped in pte\n");
      return -1;
  }
  
 ......
}

error: passing argument 1 of ‘pud_offset’ from incompatible pointer type [-Werror=incompatible-pointer-types]

73 | pud_tmp = pud_offset(pgd_tmp, vaddr);

                 pgd_t * {aka struct <anonymous> *}

expected ‘p4d_t *’ {aka ‘struct *’} but argument is of type ‘pgd_t *’ {aka ‘struct *’}

925 | static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)

I have made the following changes, but there was a segfault later.

static unsigned long vaddr2paddr(struct seq_file *m, unsigned long vaddr,int pid)
{
    pte_t *pte_tmp = NULL;  
    pmd_t *pmd_tmp = NULL;  
    pud_t *pud_tmp = NULL;
    p4d_t* p4d_tmp = NULL; 
    pgd_t *pgd_tmp = NULL;  
    struct task_struct *pcb_tmp = NULL;
    unsigned long paddr = 0;

    printk(KERN_INFO"in vaddr2paddr try to find the task %d .\n",pid);  
    pcb_tmp =pid_task(find_get_pid(pid),PIDTYPE_PID); 
    if(!pcb_tmp) {
         printk(KERN_INFO"Can't find the task %d .\n",pid);
         return 0;
    }
    
  pgd_tmp = pgd_offset(pcb_tmp->mm, vaddr);
  if (pgd_none(*pgd_tmp)) {
      printk("not mapped in pgd\n");
      return -1;
  }


 p4d_tmp = p4d_offset(pgd_tmp, vaddr);
 if (p4d_none(*p4d_tmp))
     return 0;
    
  pud_tmp = pud_offset(p4d_tmp, vaddr);  
  if (pud_none(*pud_tmp)) {
      printk("not mapped in pud\n");
      return -1;
  }
    

  pmd_tmp = pmd_offset(pud_tmp, vaddr);
  if (pmd_none(*pmd_tmp)) {
      printk("not mapped in pmd\n");
      return -1;
  }
    


  //pte = pte_offset_kernel(pmd, vaddr);
  pte_tmp = pte_offset_kernel(pmd_tmp, vaddr);    
  if (pte_none(*pte_tmp)) {
      printk("not mapped in pte\n");
      return -1;
  }
 
}

If you can directly help modify the code, I would be very grateful.

Su.
  • 1
  • 2
  • Why do you want this? Physical addresses are of no use unless you are doing DMA, and in that case there are existing APIs to do that. – Tim Roberts Nov 23 '21 at 05:14
  • https://stackoverflow.com/questions/58743052/getting-error-when-compiling-kernel-for-page-table-walk – Tim Roberts Nov 23 '21 at 05:20
  • I am a student. Out of the need of learning. I made changes according to that article, but there was a segfault later. – Su. Nov 23 '21 at 05:54
  • Certainly if you try to DO anything with a physical address, it's going to explode. There are no CPU instructions that deal with physical addresses, except for loading CR3. I notice that you don't return anything at all from this function. – Tim Roberts Nov 23 '21 at 06:04
  • asm( " mov %%cr3, %%rax \n mov %%rax, %0 " : "=m" (_cr3) :: "ax" );I want to print out the address but there is a segfault – Su. Nov 23 '21 at 06:26
  • User programs can't access the page tables. Kernel code can access the page tables, but user virtual addresses are not mapped into the page tables except while performing a syscall from the user program. You seem to be passing in a pid, so I'm guessing the VA is not mapped to the currently running task. – stark Nov 23 '21 at 19:41

0 Answers0