之前买的STM32的开发板学习裸机开发。了解裸机之后学习FreeRTOS来作为小型操作系统学习,理解操作系统调度实现。一直想学习一下linux的内核,之前下载源码和初步看了下感觉无从下手。有了RTOS的基础后,现在在学linux早期内核。瞻仰一下老前辈的光辉。
之前结束FreeRTOS的时候说过,OS调度的基础就是中断,早期linux应该不比FreeRTOS复杂太多,起码我那个STM32开发版应该赶的上91年的PC把(哈哈)
所以最开始涉及到的就是kernel目录下的asm.s和traps.c代码文件。asm.s汇编代码实现了中断函数和中断处理流程。
和ARM一样,执行中断处理经过以下步骤:
1.压栈各寄存器值
2.压栈程序计数器、PSP等
#################上面为现场保护###################
3.调用中断处理函数
#################下面为现场恢复###################
4.出栈恢复寄存器和PSP等
asm.s里的no_error_code和error_code就是对不带错误码和带错误码的中断流程包装。遵循中断保护和恢复步骤,保护的寄存器和硬件平台有关。trap_init设置中断向量表。
对应在STM32启动汇编设置中断向量表设置中断函数入口和PendSV切换上下文保护和恢复寄存器值部分。
中断处理栈分布图:
asm.s
/*
* asm.s contains the low-level code for most hardware faults.
* page_exception is handled by the mm, so that isn't here. This
* file also handles (hopefully) fpu-exceptions due to TS-bit, as
* the fpu must be properly saved/resored. This hasn't been tested.
*/
/*
这个汇编作为大部分硬件中断的低级别代码,对应的处理函数C代码在traps.c里
这里实现硬件中断处理通用模式,中断前保存寄存器值,然后调用压栈的C方法,
调用完成后出栈恢复寄存器值。这里主要抽取了中断处理模式no_error_code和
error_code来统一中断处理流程。其他中断先压栈处理方法然后跳转统一处理流程
*/
#引入外部c方法,实现在traps.c里
.globl _divide_error,_debug,_nmi,_int3,_overflow,_bounds,_invalid_op
.globl _device_not_available,_double_fault,_coprocessor_segment_overrun
.globl _invalid_TSS,_segment_not_present,_stack_segment
.globl _general_protection,_coprocessor_error,_reserved
#处理除数错误,压栈divide_error方法地址,这里没jmp应该就是接着往下执行
_divide_error:
pushl $_do_divide_error
#没有错误码的中断处理流程方法
no_error_code:
#将%eax的值保存在栈上,将中断处理函数的地址保存在%eax寄存器中
xchgl %eax,(%esp)
#压栈各寄存器值
pushl %ebx
pushl %ecx
pushl %edx
pushl %edi
pushl %esi
pushl %ebp
push %ds
push %es
push %fs
#压入0在错误码位置
pushl $0 # "error code"
#44(%esp)表示44+esp为引发该中断的指令的地址压入堆栈时的esp0指针的位置(函数返回值放入%edx)
lea 44(%esp),%edx
#把%edx里的函数返回值入栈
pushl %edx
#将内核代码段和数据段的选择符放入edx寄存器中
movl $0x10,%edx
#重新加载各个段寄存器
mov %dx,%ds
mov %dx,%es
mov %dx,%fs
#调用先压好的处理函数,给调用方法的参数就是这个代码前压栈的数据,压的顺序和方法参数相反(pushl $0 lea 44(%esp),%edx pushl %edx)
call *%eax
#出栈各寄存器值
addl $8,%esp
pop %fs
pop %es
pop %ds
popl %ebp
popl %esi
popl %edi
popl %edx
popl %ecx
popl %ebx
popl %eax
#返回
iret
#处理调试,压栈int3方法地址,然后跳转到没错误码的中断执行流程
_debug:
pushl $_do_int3 # _do_debug
jmp no_error_code
#处理nmi,压栈nmi方法地址,然后跳转到没错误码的中断执行流程
_nmi:
pushl $_do_nmi
jmp no_error_code
#处理int3,压栈int3方法地址,然后跳转到没有错误码的中断执行流程
_int3:
pushl $_do_int3
jmp no_error_code
#处理溢出,压栈overflow方法地址,然后跳转到没有错误码的中断执行流程
_overflow:
pushl $_do_overflow
jmp no_error_code
#处理bounds,压栈bounds方法地址,然后跳转到没有错误码的中断执行流程
_bounds:
pushl $_do_bounds
jmp no_error_code
#处理无效操作,压栈invalid_op方法地址,然后跳转到没有错误码的中断执行流程
_invalid_op:
pushl $_do_invalid_op
jmp no_error_code
#处理数学仿真,弹出到%eax寄存器压入device_not_available方法地址,然后跳转到没有错误码的中断执行流程
math_emulate:
popl %eax
pushl $_do_device_not_available
jmp no_error_code
#设备不可用
_device_not_available:
pushl %eax
movl %cr0,%eax
bt $2,%eax # EM (math emulation bit)
jc math_emulate
clts # clear TS so that we can use math
movl _current,%eax
cmpl _last_task_used_math,%eax
je 1f # shouldn't happen really ...
pushl %ecx
pushl %edx
push %ds
movl $0x10,%eax
mov %ax,%ds
call _math_state_restore
pop %ds
popl %edx
popl %ecx
1: popl %eax
iret
#协处理器段溢出,压栈coprocessor_segment_overrun方法地址,然后跳转到没有错误码的中断执行流程
_coprocessor_segment_overrun:
pushl $_do_coprocessor_segment_overrun
jmp no_error_code
#预留,压栈reserved地址,然后跳转到没有错误码的中断执行流程
_reserved:
pushl $_do_reserved
jmp no_error_code
#协处理器错误,压栈coprocessor_error方法地址,然后跳转到没有错误码的中断执行流程
_coprocessor_error:
pushl $_do_coprocessor_error
jmp no_error_code
#浮点错误,压栈double_fault方法地址
_double_fault:
pushl $_do_double_fault
#带错误码的处理
error_code:
#将%eax的值保存在栈上,将error code存放在%eax中
xchgl %eax,4(%esp) # error code <-> %eax
#将%ebx的值保存在栈上,将中断处理函数的地址保存在%ebx寄存器中
xchgl %ebx,(%esp) # &function <-> %ebx
#压栈各寄存器值
pushl %ecx
pushl %edx
pushl %edi
pushl %esi
pushl %ebp
push %ds
push %es
push %fs
#压入0在错误码位置
pushl %eax # error code
#44(%esp)表示44+esp为引发该中断的指令的地址压入堆栈时的esp0指针的位置(函数返回值放入%eax)
lea 44(%esp),%eax # offset
#把%eax里的函数返回值入栈
pushl %eax
#用内核代码段和数据段选择符初始化%eax
movl $0x10,%eax
#重新加载各个段寄存器
mov %ax,%ds
mov %ax,%es
mov %ax,%fs
#调用先压好的处理函数,给调用方法的参数就是这个代码前压栈的数据,压的顺序和方法参数相反(pushl %eax lea 44(%esp),%eax pushl %eax)
call *%ebx
#出栈各寄存器值
addl $8,%esp
pop %fs
pop %es
pop %ds
popl %ebp
popl %esi
popl %edi
popl %edx
popl %ecx
popl %ebx
#将错误码出栈,保存在eax中
popl %eax
iret
#无效的TSS,压栈invalid_TSS方法地址,然后跳转到有错误码的中断执行流程
_invalid_TSS:
pushl $_do_invalid_TSS
jmp error_code
#段不在当前,压栈segment_not_present方法地址,然后跳转到有错误码的中断执行流程
_segment_not_present:
pushl $_do_segment_not_present
jmp error_code
#堆栈段,压栈stack_segment方法地址,然后跳转到有错误码的中断执行流程
_stack_segment:
pushl $_do_stack_segment
jmp error_code
#通用保护,压栈general_protection方法地址,然后跳转到有错误码的中断执行流程
_general_protection:
pushl $_do_general_protection
jmp error_code
traps.c
/*
* '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).
*/
/*
* 实现硬件中断处理,在asm.s里调入该文件的实现函数
*/
#include <string.h>
#include <linux/head.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <asm/system.h>
#include <asm/segment.h>
//得到段地址的数据
#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;})
//得到段长度
#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;})
#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);
//nmi
void nmi(void);
//int3
void int3(void);
//溢出
void overflow(void);
//bounds
void bounds(void);
//无效操作
void invalid_op(void);
//设备不可用
void device_not_available(void);
//浮点错误
void double_fault(void);
//协处理器段溢出
void coprocessor_segment_overrun(void);
//无效的TSS
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);
//死机
static void die(char * str,long esp_ptr,long nr)
{
long * esp = (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] == 0x17) {
printk("Stack: ");
for (i=0;i<4;i++)
printk("%p ",get_seg_long(0x17,i+(long *)esp[3]));
printk("\n");
}
str(i);
printk("Pid: %d, process nr: %d\n\r",current->pid,0xffff & i);
for(i=0;i<10;i++)
printk("%02x ",0xff & get_seg_byte(esp[1],(i+(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":"=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]);
}
//nmi
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);
}
//bounds
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);
}
//无效的TSS
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)
{
die("coprocessor error",esp,error_code);
}
//预留
void do_reserved(long esp, long error_code)
{
die("reserved (15,17-31) error",esp,error_code);
}
//中断初始化函数
//设置中断向量表
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);
//预留的中断向量号
for (i=17;i<32;i++)
set_trap_gate(i,&reserved);
/* __asm__("movl $0x3ff000,%%eax\n\t"
"movl %%eax,%%db0\n\t"
"movl $0x000d0303,%%eax\n\t"
"movl %%eax,%%db7"
:::"ax");*/
}
原来在91年写个汇编还有包装的,中断前保护,调用中断和中断恢复通用包装出乎意料,哈哈