热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

新手学linux之trap.c

2019独角兽企业重金招聘Python工程师标准**linuxkerneltraps.c**(C)1991LinusTorvalds***Traps.chandleshard

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

/**  linux/kernel/traps.c**  (C) 1991  Linus Torvalds*//** 'Traps.c' handles hardware traps and faults after we have saved some* state in 'asm.s'. Currently mostly a debugging-aid, will be extended* to mainly kill the offending process (probably by giving it a signal,* but possibly by killing it outright if necessary).*/#include 
#include 
#include 
#include 
#include 
#include 
#include //声明:以后的博客遇到简单的问题我只会简单的描述 对于有疑难点的会详细说明
//取seg:addr处1个字节
#define get_seg_byte(seg,addr) ({ \
register char __res; \
__asm__("push %%fs;mov %%ax,%%fs;movb %%fs:%2,%%al;pop %%fs" \:"=a" (__res):"0" (seg),"m" (*(addr))); \
__res;})//取seg:addr处的4个字节
#define get_seg_long(seg,addr) ({ \
register unsigned long __res; \
__asm__("push %%fs;mov %%ax,%%fs;movl %%fs:%2,%%eax;pop %%fs" \:"=a" (__res):"0" (seg),"m" (*(addr))); \
__res;})//取fs中的值
#define _fs() ({ \
register unsigned short __res; \
__asm__("mov %%fs,%%ax":"=a" (__res):); \
__res;})int do_exit(long code);void page_exception(void);void divide_error(void);
void debug(void);
void nmi(void);
void int3(void);
void overflow(void);
void bounds(void);
void invalid_op(void);
void device_not_available(void);
void double_fault(void);
void coprocessor_segment_overrun(void);
void invalid_TSS(void);
void segment_not_present(void);
void stack_segment(void);
void general_protection(void);
void page_fault(void);
void coprocessor_error(void);
void reserved(void);
void parallel_interrupt(void);
void irq13(void);//printk内核打印函数 
static void die(char * str,long esp_ptr,long nr)
{long * esp &#61; (long *) esp_ptr;int i;printk("%s: %04x\n\r",str,nr&0xffff);printk("EIP:\t%04x:%p\nEFLAGS:\t%p\nESP:\t%04x:%p\n",esp[1],esp[0],esp[2],esp[4],esp[3]);printk("fs: %04x\n",_fs());printk("base: %p, limit: %p\n",get_base(current->ldt[1]),get_limit(0x17));if (esp[4] &#61;&#61; 0x17) {    //若原ss的值为0x17 则还打印出 用户栈的4个长字//这里为什么是0x17//head.s 23行代码 lss _stack_start&#xff0c;%esp就是设置内核堆栈的//lss的意思是将_stack_start中的数据加载到SS&#xff1a;ESP中//_stack_start数组的2个元素为&user_stack[1024],0x10    //也就是将0X10赋值给SS&#xff0c;&user_stack[1024]复制给esp//move_to_user_mode程序看看为什么0x10编程0x17了    也就是讲段选择子的bit0&#xff0c;bit1&#xff0c;bit2置1//参考至&#xff1a;http://blog.csdn.net/u012108676/article/details/12837771//------通俗的讲就是有cs段寄存器变为cs段选择子 特权级相关位置为用户模式------------printk("Stack: ");for (i&#61;0;i<4;i&#43;&#43;)printk("%p ",get_seg_long(0x17,i&#43;(long *)esp[3]));printk("\n");}str(i);printk("Pid: %d, process nr: %d\n\r",current->pid,0xffff & i);for(i&#61;0;i<10;i&#43;&#43;)printk("%02x ",0xff & get_seg_byte(esp[1],(i&#43;(char *)esp[0])));printk("\n\r");do_exit(11);        /* play segment exception */
}void do_double_fault(long esp, long error_code)
{die("double fault",esp,error_code);
}void do_general_protection(long esp, long error_code)
{die("general protection",esp,error_code);
}void do_divide_error(long esp, long error_code)
{die("divide error",esp,error_code);
}//int3 断点引发的指令 与硬件无关
void do_int3(long * esp, long error_code,long fs,long es,long ds,long ebp,long esi,long edi,long edx,long ecx,long ebx,long eax)
{int tr;__asm__("str %%ax":"&#61;a" (tr):"0" (0));printk("eax\t\tebx\t\tecx\t\tedx\n\r%8x\t%8x\t%8x\t%8x\n\r",eax,ebx,ecx,edx);printk("esi\t\tedi\t\tebp\t\tesp\n\r%8x\t%8x\t%8x\t%8x\n\r",esi,edi,ebp,(long) esp);printk("\n\rds\tes\tfs\ttr\n\r%4x\t%4x\t%4x\t%4x\n\r",ds,es,fs,tr);printk("EIP: %8x   CS: %4x  EFLAGS: %8x\n\r",esp[0],esp[1],esp[2]);
}void do_nmi(long esp, long error_code)
{die("nmi",esp,error_code);
}void do_debug(long esp, long error_code)
{die("debug",esp,error_code);
}void do_overflow(long esp, long error_code)
{die("overflow",esp,error_code);
}void do_bounds(long esp, long error_code)
{die("bounds",esp,error_code);
}void do_invalid_op(long esp, long error_code)
{die("invalid operand",esp,error_code);
}void do_device_not_available(long esp, long error_code)
{die("device not available",esp,error_code);
}void do_coprocessor_segment_overrun(long esp, long error_code)
{die("coprocessor segment overrun",esp,error_code);
}void do_invalid_TSS(long esp,long error_code)
{die("invalid TSS",esp,error_code);
}void do_segment_not_present(long esp,long error_code)
{die("segment not present",esp,error_code);
}void do_stack_segment(long esp,long error_code)
{die("stack segment",esp,error_code);
}void do_coprocessor_error(long esp, long error_code)
{if (last_task_used_math !&#61; current)return;die("coprocessor error",esp,error_code);
}void do_reserved(long esp, long error_code)
{die("reserved (15,17-47) error",esp,error_code);
}//在main.c中调用
/*
set_trap_gate 在system.h中定义
设置其他门的函数也大致相同
set_trap_gate参数的含义:n中断号 addr中断程序偏移位置
_set_gate参数含义:&idt[n]中断描述表中中断号n对应项的偏移值 陷阱描述型的类型15 特权级0(内核)
TYPE字段 14 32-bit Interrupt gate 32位中断门 
下面简要的介绍一下关于IDT表的设置
type的值表示描述符的类型 有任务门(linux中没有使用任务门用一种更简单的方法) 中断门 陷阱门 
15 32-bit trap gate 32位陷阱门 这里主要用到的是这两个
-------
关于gdt是如何工作的参考
http://blog.csdn.net/zhw888888/article/details/6599058 (这篇博客完全copy深入理解linux内核)
结合下面这个看效果更好
http://oss.org.cn/kernel-book/ch03/3.1.4.htm(有个小错误 中断描述符的类型为1110)
Linux又将IDT中的门细分为以下几类:
中断门: intel的中断门, DPL&#61;0, 所有linux的中断处理程序都通过中断门激活, 且限制在内核态
系统中断门: intel中断门, DPL&#61;3, 用于向量3, 因此用户态进程可以使用int $3指令
陷阱门: intl的陷阱门, DPL&#61;0, 大部分的linux异常处理程序由陷阱门激活
系统门: intel的陷阱门, DPL&#61;3, 用于向量4, 5, 128, 因此用户进程可以调用into, bound, int  $128指令
任务门: intel的任务门, DPL&#61;0,  linux中对DOUBLE FAULT异常处理使用任务门激活
-----#define set_trap_gate(n,addr) \_set_gate(&idt[n],15,0,addr)//这个宏的作用就是组拼一个64位的中断描述表符
#define _set_gate(gate_addr,type,dpl,addr) \
__asm__ ("movw %%dx,%%ax\n\t" \"movw %0,%%dx\n\t" \"movl %%eax,%1\n\t" \"movl %%edx,%2" \: \: "i" ((short) (0x8000&#43;(dpl<<13)&#43;(type<<8))), \        "o" (*((char *) (gate_addr))), \        ;中断门描述符的低四位 "o" (*(4&#43;(char *) (gate_addr))), \        ;高四位"d" ((char *) (addr)),"a" (0x00080000))  ;段选择符设置为0x08为什么要设置为0x08一个哥们是这么说的_start from src/cpu/x86/16bit/entry16.inc, invalidates the TLBs, sets up a GDT for selector 0x08 (code) and 0x10 (data), switches to protected mode, and jumps to __protected_start (setting the CSto the new selector 0x08). The selectors provide full flat access to the entire physical memory map. 来自:http://bbs.21ic.com/blog-44442-52917.html
*/
void trap_init(void)
{int i;set_trap_gate(0,÷_error);set_trap_gate(1,&debug);set_trap_gate(2,&nmi);set_system_gate(3,&int3);    /* int3-5 can be called from all */set_system_gate(4,&overflow);set_system_gate(5,&bounds);set_trap_gate(6,&invalid_op);set_trap_gate(7,&device_not_available);set_trap_gate(8,&double_fault);set_trap_gate(9,&coprocessor_segment_overrun);set_trap_gate(10,&invalid_TSS);set_trap_gate(11,&segment_not_present);set_trap_gate(12,&stack_segment);set_trap_gate(13,&general_protection);set_trap_gate(14,&page_fault);set_trap_gate(15,&reserved);set_trap_gate(16,&coprocessor_error);//Intel保留中断for (i&#61;17;i<48;i&#43;&#43;)set_trap_gate(i,&reserved);set_trap_gate(45,&irq13);outb_p(inb_p(0x21)&0xfb,0x21);    //inb_p是延时函数 允许主片IRQ2的中断请求outb(inb_p(0xA1)&0xdf,0xA1);    //允许从片IRQ13中断请求set_trap_gate(39,¶llel_interrupt);
}



转:https://my.oschina.net/lngligelang/blog/263360



推荐阅读
author-avatar
jiazi---
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有