Memory Management
1.listvma
static void Mtest_dump_vma_list (void)
{
struct Task_struct *task = current; Get the task_struct of the current process
struct Mm_struct *mm = task->mm;
struct Vm_area_struct *vma; Get the VMA area of the current process
int count = 0; The number of VMA
Down_read (&mm->mmap_sem);
for (VMA = mm->mmap; vma; VMA = vma->vm_next)
{
count++;
PRINTK ("%d:0x%lx 0x%lx", Count, Vma->vm_start, vma->vm_end);
if (Vma->vm_flags & Vm_read)
PRINTK ("R");
Else
PRINTK ("-");
if (Vma->vm_flags & Vm_write)
PRINTK ("w");
Else
PRINTK ("-");
if (Vma->vm_flags & Vm_write)
PRINTK ("X");
Else
PRINTK ("-");
PRINTK ("\ n");
}
Up_read (&mm->mmap_sem);
}
2.findpage Addr
static struct page *
My_follow_page (struct vm_area_struct *vma, unsigned long addr)
{
pgd_t *PGD;
pmd_t *PMD;
pud_t *pud;
pte_t *pte;
spinlock_t *PTL;
struct page *page = NULL;
struct Mm_struct *mm = vma->vm_mm;
PGD = Pgd_offset (mm, addr); Get PGD
if (Pgd_none (*PGD) | | Unlikely (Pgd_bad (*PGD)))
Goto out;
PUD = Pud_offset (PGD, addr); Get PUD
if (Pud_none (*pud) | | Unlikely (Pmd_bad (*pud)))
Goto out;
PMD = Pmd_offset (pud, addr); Get PMD
if (Pmd_none (*PMD) | | Unlikely (Pmd_bad (*PMD)))
Goto out;
Pte = Pte_offset_map_lock (mm, PMD, addr, &PTL); Get Pte
if (!PTE)
Goto out;
if (!pte_present (*pte))//pte not in memory
goto unlock;
page = Pfn_to_page (PTE_PFN (*pte));
if (!page)
goto unlock;
Get_page (page);
Unlock
Pte_unmap_unlock (Pte, PTL);
Out
return page;
}
static void Mtest_find_page (unsigned long addr)
{
struct Vm_area_struct *vma;
struct Task_struct *task = current;
struct Mm_struct *mm = task->mm;
unsigned long kernel_addr;
struct page *page;
Down_read (&mm->mmap_sem);
VMA = FIND_VMA (mm, addr);
page = My_follow_page (VMA, addr);
if (!page)
{
PRINTK ("translation failed.\n");
Goto out;
}
KERNEL_ADDR = (unsigned long) page_address (page);
Kernel_addr + = (addr & ~page_mask);
PRINTK ("VMA 0X%LX, PMA 0x%lx\n", addr, kernel_addr);
Out
Up_read (&mm->mmap_sem);
}
3.writeval addr Val
static void
Mtest_write_val (unsigned long addr, unsigned long val)
{
struct Vm_area_struct *vma;
struct Task_struct *task = current;
struct Mm_struct *mm = task->mm;
struct page *page;
unsigned long kernel_addr;
Down_read (&mm->mmap_sem);
VMA = FIND_VMA (mm, addr);
Test if it is a legal VMA
if (VMA && addr >= vma->vm_start && (addr + sizeof (val)) < Vma->vm_end)
{
if (! ( Vma->vm_flags & Vm_write)//test If we have rights to WRITE
{
PRINTK ("Cannot write to 0x%lx\n", addr);
Goto out;
}
page = My_follow_page (VMA, addr);
if (!page)
{
PRINTK ("Page not found 0x%lx\n", addr);
Goto out;
}
KERNEL_ADDR = (unsigned long) page_address (page);
Kernel_addr + = (addr &~ page_mask);
PRINTK ("Write 0X%LX to Address 0x%lx\n", Val, kernel_addr);
* (unsigned long *) kernel_addr = val;
Put_page (page);
}
Else
{
PRINTK ("No VMA found for%lx\n", addr);
}
Out
Up_read (&mm->mmap_sem);
}
4.To Build a proc file
Static ssize_t
Mtest_write (struct file *file, const char __user *buffer, size_t count, loff_t *data)
{
Char buf[128];
unsigned long Val, val2;
if (Count > sizeof (BUF))
Return-einval;
if (Copy_from_user (buf, buffer, count))//get the command from Shell
Return-einval;
if (memcmp (buf, "LISTVMA", 7) = = 0)
Mtest_dump_vma_list ();
else if (memcmp (buf, "Findpage", 8) = = 0)
{
if (sscanf (buf+8, "%LX", &val) = = 1)
Mtest_find_page (Val);
}
else if (memcmp (buf, "Writeval", 8) = = 0)
{
if (sscanf (buf+8, "%lx%lx", &val, &val2) = = 2)
{
Mtest_write_val (Val, val2);
}
}
return count;
}
static struct
File_operations proc_mtest_operation = {
Write:mtest_write,
};
static int __init
Mtest_init (void)
{
Proc_create ("Mtest", 0, NULL, &proc_mtest_operation);
PRINTK ("Create mtest...\n");
return 0;
}
static void __exit
Mtest_exit (void)
{
Remove_proc_entry ("Mtest", NULL);
}
Module_license ("GPL");
Module_description ("Memory Management task");
Module_init (Mtest_init);
Module_exit (Mtest_exit);
6.some head files that should be included
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/errno.h>
7. How to test
1) Write a Makefile
2) type ' make ' in shell
3) Type "sudo insmod Mtest.ko"
4) Type "SUDO-SU"
5) Type:echo "LISTVMA" >/proc/mtest
6) TYPE:DMESG (then you'll find a lot of VMA)
7) Choose one of them and Type:echo "findpage 0x ..." >/proc/mtest
Then TYPE:DMESG
Note:you may find, "translation failed" shows up. But that does isn't mean you fail the test. Choose an address between the start and the end of the VMA listed.
e.g. 0x123–0x345
Want to Type:echo "Findpage 0x300" >/proc/mtest
Because you cannot being sure if the beginning of the virtual address is used.
8) Choose an address so you have rights to write.
Then Type:echo "Writeval 0x ... 123 ">/proc/mtest
Type:dmesg
Note:123 can is any unsigned int
You ' d better choose the same address to test in both step 7 and 8
If the physical address is consistent.
The End.