Centos5.5 IDT HOOK

HOOK PageFault

    #include <linux/kernel.h>
    #include <linux/module.h>
    #include <asm/desc.h>

    typedef struct desc_struct gate_desc;

    struct desc_easy {
        unsigned short a;
        unsigned short b;
        unsigned short c;
        unsigned short d;
    } __attribute__((packed)) ;

    struct desc_ptr {
            unsigned short size;
            unsigned long address;
    } __attribute__((packed)) ;

    #define PGFAULT_INT 0x0E

    static unsigned long isr_orig;
    static unsigned long isr_new;
    static gate_desc *PF_gate;

    asmlinkage void my_function(void);

    void stub(void)
    {
        __asm__
        (
        "       pushal              \n"
        "       pushl %es           \n"
        "       pushl %ds           \n"
        "       call my_function    \n"
        "       popl %ds            \n"
        "       popl %es            \n"
        "       popal               \n"
        "       jmp  *isr_orig      \n"
        );
    }

    asmlinkage void my_function(void)
    {
        unsigned long add;
        asm("movl %%cr2,%0":"=r"(add));
        printk("PID: %d >> %08x\n",current->tgid,(unsigned int )add);
    }

    int pgfault_init( void )
    {
        struct desc_ptr idtr;
        gate_desc *idt_table;

        printk("+z+ pgfault_init\n");
        //获取IDT指针
        asm ("sidt %0" : "=m" (idtr));

        //page_fault_gate地址
        idt_table = ((gate_desc *) idtr.address);
        PF_gate = &idt_table[PGFAULT_INT];

        //保存原始的page_fault()地址
        isr_orig = (PF_gate->a & 0xffff) | (PF_gate->b & 0xffff0000);

        //把新的处理函数地址填充进去
        isr_new = (unsigned long)stub;
        ((struct desc_easy *) PF_gate)->a = (unsigned short) (isr_new & 0x0000FFFF);
        ((struct desc_easy *) PF_gate)->d = (unsigned short) (isr_new >> 16);

        return 0;
    }


    void pgfault_exit( void )
    {
        printk("+z+ pgfault_exit\n");
        //还原以前的page_fault地址
        ((struct desc_easy *) PF_gate)->a = (unsigned short) (isr_orig & 0x0000FFFF);
        ((struct desc_easy *) PF_gate)->d = (unsigned short) (isr_orig >> 16);
    }

    MODULE_LICENSE("GPL");
    module_init( pgfault_init);
    module_exit( pgfault_exit);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值