获取系统不识别的内存方法:mmap

来源:互联网 发布:淘宝店叫什么名字好 编辑:程序博客网 时间:2024/04/29 20:53
  

获取系统不使用的内存方法:mmap

要求:
修改/boot/grub/menu.list
将 kernel /vmlinuz-2.6.15-1.2054_FC5 ro root=/dev/VolGroup00/LogVol00 rhgb
quiet 改为
kernel /vmlinuz-2.6.15-1.2054_FC5 ro root=/dev/VolGroup00/LogVol00 rhgb
quiet mem=400M
可通过 cat /proc/meminfo看出已经改了
 
#include <linux/init.h>
#include 
<linux/kernel.h>
#include 
<linux/module.h>
#include 
<linux/config.h>
//#include <linux/malloc.h>   /* kmalloc() */
#include <linux/slab.h>
#include 
<linux/fs.h>       /* everything... */
#include 
<linux/errno.h>    /* error codes */
#include 
<linux/types.h>    /* size_t */
#include 
<linux/proc_fs.h>
#include 
<linux/fcntl.h>    /* O_ACCMODE */
#include 
<asm/system.h>     /* cli(), *_flags */
#include 
<asm/io.h>
#include 
<linux/types.h>
#include 
<linux/cdev.h>
#include 
<linux/mm.h>/*vma*/
 
 
MODULE_LICENSE(
"Dual BSD/GPL");
 
#define DEVNAME "DevDirectIO"
dev_t dev 
= 0;
 
void directIO_vm_open(struct vm_area_struct *vm)
{
        printk(
"vm opened ");
}

void directIO_vm_close(struct vm_area_struct *vm)
{
}

static struct vm_operations_struct directIO_remap_vm_ops = {
        .open 
= directIO_vm_open,
        .close 
= directIO_vm_close,
}
;
int directIO_open(struct inode *inode, struct file *filp)
{
        printk(
"DirectIO: open()  ");
    
return 0;          /* success */
}

int directIO_release(struct inode *inode, struct file *filp)
{
 
// MOD_DEC_USE_COUNT;
    return 0;
}

ssize_t directIO_read(
struct file *filp, char *buf, size_t count,
                loff_t 
*f_pos)
{
    ssize_t ret 
= 0;
    
return ret;
}

ssize_t directIO_write(
struct file *filp, const char *buf, size_t count,
                loff_t 
*f_pos)
{
 
//   ssize_t ret = -ENOMEM; /* value used in "goto out" statements */
    return count;
}

int directIO_mmap(struct file* filep, struct vm_area_struct* vm)
{
//      unsigned long lenth = vm->
        printk("start:%lu length:%lu off:%lu ", vm->vm_start,vm->vm_end - vm->vm_start,vm->vm_pgoff);
        
/*if(remap_pfn_range(vm, vm->vm_start,vm->vm_pgoff,vm->vm_end -
 vm->vm_start,vm->vm_page_prot)){
                return -EAGAIN;
        }
*/

        unsigned 
long ulStart = (VMALLOC_START - PAGE_OFFSET - VMALLOC_OFFSET) >> 12;
/* VMALLOC_START:系统虚拟内存总大小
  PAGE_OFFSET:8M空隙
  VMALLOC_OFFSET:虚拟内存(高端)起始点,linux2.6的为3G
*/

 
        
if(remap_pfn_range(vm, vm->vm_start,ulStart + 1,vm->vm_end -
 vm
->vm_start,vm->vm_page_prot)){
                
return -EAGAIN;
        }

        vm
->vm_ops = &directIO_remap_vm_ops;
        directIO_vm_open(vm);
        
return 0;
}
 
原创粉丝点击