从STM32-FreeRTOS到linux

2023-05-16

之前买的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,&divide_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年写个汇编还有包装的,中断前保护,调用中断和中断恢复通用包装出乎意料,哈哈

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从STM32-FreeRTOS到linux 的相关文章

随机推荐

  • [转载]Python SMTP发送邮件-smtplib模块

    在进入正题之前 xff0c 我们需要对一些基本内容有所了解 xff1a 常用的电子邮件协议有SMTP POP3 IMAP4 xff0c 它们都隶属于TCP IP协议簇 xff0c 默认状态下 xff0c 分别通过TCP端口25 110和14
  • c语言和c++有什么区别

    差不多是win98跟winXP的关系 C 43 43 是在C的基础上增加了新的理论 xff0c 玩出了新的花样 所以叫C加加 C是一个结构化语言 xff0c 它的重点在于算法和数据结构 C程序的设计首要考虑的是如何通过一个过程 xff0c
  • 梳理LVM逻辑卷管理,

    在Linux操作系统会时不时碰到卷有关的操作 xff0c 以下也是罗列了相关操作内容 xff0c 仅供参考 创建PV VG LV的方法 将各物理磁盘或分区的系统类型设为Linux LVM xff0c 其system ID为8e xff0c
  • 使用sqlyog连接 Mysql 出现1251错误

    使用sqlyog连接 Mysql 出现1251错误 简述 xff1a 1251 client does not support authentication protocol requested by server consider upg
  • 准备给ubuntu18.04安装杀毒软件

    如题 xff0c 电脑最近总出现些奇奇怪怪的小问题 xff0c 还是得装个杀毒软件 xff0c 看是不是中病毒了 输入sudo apt get install clamtk 安装完成后 xff0c 输入clamtk 即可 xff0e 卸载方
  • 使用Nginx代理地址

    DotNetCore在Linux发布时候 xff0c 由于不止一个产品组发布网站 xff0c 不像以前大家都用IIS的80发布网站 那么就存在大家抢80端口的情况 xff0c 为了让大家不比加上端口为此用Nginx代理URL实现网站地址代理
  • CentOS安装Cache数据库

    适用CentOS7 6 CentOS8上安装Intersystem公司的Cache数据库 xff0c 资料基本是空白 xff0c 分享一下 首先安装解压软件unzip和libicu xff0c 最小化安装的缺 xff0c 全安装的不缺 yu
  • Cache数据库之ECP搭建

    Cache作为非关系数据库 xff0c 其强大毋庸置疑 首先其Globle结构 xff0c 直接暴露的表Globel数据 xff0c 以及提供的M语言操作Globle达到的最优查询速度 ECP xff08 企业缓存协议 xff09 更是提供
  • Sebia电泳绘图

    Sebia这仪器真是个奇葩的存在 自己仪器有图不存文件 xff0c LIS要的话还得自己按数据绘制 还有蛋白电泳 固定电泳 画不画参考线等不同要求 xff08 奇葩的很 xff09 按理这种事不属于lis范围 xff0c 无奈国内lis太卷
  • nginx代理与负载均衡

    随着谷歌浏览器不断的改变https调用websocket和非https资源的策略 xff0c 从谷歌大概70以后不允许https调用非https资源和ws的websocket 后面实现了wss解决谷歌这一策略的影响 随着谷歌到90后版本限制
  • FreeRTOS学习第一篇

    之前在STM32Nano开发板开发是基于裸机开发 xff0c 即自己在main方法写死循环 死循环轮流执行各个任务逻辑的方法 这样做直接简单 xff0c 但是不同任务有不同优先级 xff0c 对CPU响应要求不同 逻辑容易某个任务卡住了 x
  • FreeRTOS之heap4

    操作系统离不开内存管理 FreeRTOS提供了5种内存管理方法 实现在portable MemMang里heap1到heap5 每种管理方案策略不同 我采用的是比较有代表性的heap4管理方案 该模式定义了ucHeap全局数组充当堆内存池
  • FreeRTOSMini

    最近在研究实时操作系统FreeRTOS FreeRTOS作为开源的RTOS xff0c 源码规模不大 xff0c 可以供操作系统学习 xff0c 加上我的STM32 Nano开发板正好可以学习OS 借着五一放假宅家里学习 实现的FreeRT
  • 双master节点+keepalived方式部署K8s 1.18.20

    相关部署方式也挺多 xff0c 自己采用双master节点 43 单node节点方式 xff0c 并且采用keepalived部署1 18 20版本 xff0c 中间也出现过相关小问题 xff0c 但都一一处理 xff0c 记录以给需要的同
  • FreeRTOS之TCB

    FreeRTOSMini实现了最小任务调度 现在分开介绍进程调度重要部分 进程调度的基础首先是定义任务调度的数据结构 xff0c 来保存任务堆栈结构和任务状态所在状态列表 xff0c 然后就是任务的优先级唯一号等 最小Mini内核参照 Fr
  • FreeRTOS任务调度主要变量

    之前介绍的和FreeRTOS任务调度相关的数据结构即内存分配实现 xLIST heap 4 TCB结构体 任务调度就是基于这些结构体实现 这次介绍调度相关的主要变量 代码在FreeRTOSMini c文件签名部分 span class to
  • Base64串介绍

    以前写winform时候没接触过Base64 刚开始接触时候还不知道是个啥 最开始接触Base64串时候是仪器出图 很长一段时间我还真以为Base64就是表示图的 xff0c 很多人也是这么认为的 xff0c 这次介绍一下什么是Base64
  • FreeRTOS创建任务

    CPU有这些寄存器 R0 R12为通用寄存器 R13为栈顶指针 xff0c 在OS时候中断函数的R13使用MSP的指针 xff08 内核态 xff09 非中断里面使用PSP指针 xff08 用户态 xff09 正是有双堆栈指针可以保证OS切
  • FreeRTOS任务调度最后篇

    FreeRTOS开启任务调度 一篇说到启动任务调度最后启动Systick定时器 xff0c 通过SVC中断引导第一个任务执行 然后系统就在Systick的定时中断下调度任务执行 xff0c 这次介绍最后的部分 xff0c Systick和P
  • 从STM32-FreeRTOS到linux

    之前买的STM32的开发板学习裸机开发 了解裸机之后学习FreeRTOS来作为小型操作系统学习 xff0c 理解操作系统调度实现 一直想学习一下linux的内核 xff0c 之前下载源码和初步看了下感觉无从下手 有了RTOS的基础后 xff