KernelNewbies:
/*
 * tyler@agat.net
 *
 * Virtual Memory Module (intended to Linux Kernel 2.6(.12))
 *
 * This module just explains the concept and the mechanism of the Linux
 * virtual memory system
 */
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>

unsigned long new_page ;
unsigned long temp ;

struct page *new_desc_page ;

int init_module()
{
        new_page = __get_free_page(GFP_KERNEL) ;

        new_desc_page = (struct page *)virt_to_page(new_page) ;

        /*
         * That's how the virtual memory system works.
         * To retrieve the virtual adress corresponding to the page,
         * we get the index in the page frame (new_desc_page-mem_map).
         * We then multiply it by the size of the page. We get the physical adress.
         * And in kernel mode, the differences between virtual and physical adresses
         * is just an offset of PAGE_OFFSET. So we add PAGE_OFFSET
         */
        temp = (new_desc_page-mem_map)*PAGE_SIZE+PAGE_OFFSET ;

        printk("0x%x\n",temp) ;
        printk("0x%x\n",new_page) ;

        /* 
         * In kernel mode, the virtual adresses has just an offset of PAGE_OFFSET
         * (the page tables are properly configured to do this)
         * On i386, the PAGE_OFFSET=0xC0000000
         */
        temp = __pa(new_page) ;

        printk("0x%x\n",new_page-PAGE_OFFSET) ;
        printk("0x%x\n",temp) ;

        return 0 ;
}

void cleanup_module()
{
        free_page(new_page) ;
}

KernelNewbies: MemModuleSource (last edited 2017-12-30 01:30:10 by localhost)