[自制操作系统] 第13回 磨刀不误砍柴工


目录
一、前景回顾
二、编写makefile
三、实现Assert断言
四、实现字符串操作函数
五、测试

 

一、前景回顾

  上一回我们详细地讲解了整个系统的中断工作流程,整个中断系统比较难的地方在于中断的执行流程,我开始学的时候对这一块也是比较模糊的,感觉不知从何入手。现在已经很清楚整个流程了,这里可以给读者一个建议,想象自己是CPU,当接收到中断信号后,根据中断的处理流程去看代码,应该很快就能看懂代码,不要单独去看某一块代码,这样代入性不强。这一回先暂停主线任务,先腾出手来把一些准备工作给完善了。

二、编写makefile

  这里为什么要插入makefile呢?在前面的代码中,如果读者都编译运行过的话,会发现实在是太太太麻烦了!每一个文件都要去编译,最后再链接。所以这里我们写一个自己的makefile,只需要一键make就可以。直接上代码:

 1 BUILD_DIR = ./build  2 PATH1 = project/kernel  3 PATH2 = project/lib/kernel  4 PATH3 = project/lib/user  5 PATH4 = project/userprog  6 PATH5 = project/lib  7 INCLUDE = -I $(PATH1) -I $(PATH2) -I $(PATH3) -I $(PATH4) -I $(PATH5)  8 SRC = $(wildcard $(PATH1)/*.c $(PATH2)/*.c $(PATH3)/*.c $(PATH4)/*.c $(PATH5)/*.c)  9 OBJ = $(patsubst %.c, $(BUILD_DIR)/%.o, $(notdir $(SRC))) $(BUILD_DIR)/print.o $(BUILD_DIR)/kernel.o 10 
11 kernel.bin: $(OBJ) 12  ld -m elf_i386 -Ttext 0xc0001500 -e main -o ./kernel.bin ./build/main.o ./build/print.o ./build/interrupt.o \ 13  ./build/kernel.o ./build/timer.o ./build/init.o ./build/debug.o ./build/string.o 14 
15 mbr.bin: mbr.S 16  nasm -I include/ mbr.S -o mbr.bin 17 
18 loader.bin: loader.S 19  nasm -I include/ loader.S -o loader.bin 20 
21 install: mbr.bin loader.bin 22  dd if=./mbr.bin of=./hd60M.img bs=512 count=1 conv=notrunc 23  dd if=./loader.bin of=./hd60M.img bs=512 count=4 seek=2 conv=notrunc 24  dd if=./kernel.bin of=./hd60M.img bs=512 count=200 seek=9 conv=notrunc 25  ./bin/bochs -f bochsrc.disk 26 
27 #编译print.S 28 $(BUILD_DIR)/print.o : ./project/lib/kernel/print.S 29  nasm -f elf -o $(BUILD_DIR)/print.o ./project/lib/kernel/print.S 30 
31 #编译kernel.S 32 $(BUILD_DIR)/kernel.o : ./project/kernel/kernel.S 33  nasm -f elf -o $(BUILD_DIR)/kernel.o ./project/kernel/kernel.S 34 
35 #编译四个目录下的.c文件为对应的.o文件 36 $(BUILD_DIR)/%.o : $(PATH1)/%.c 37  gcc -m32 $(INCLUDE) -c -fno-builtin $< -o $@ 38 
39 $(BUILD_DIR)/%.o : $(PATH2)/%.c 40  gcc -m32 $(INCLUDE) -c -fno-builtin $< -o $@ 41 
42 $(BUILD_DIR)/%.o : $(PATH3)/%.c 43  gcc -m32 $(INCLUDE) -c -fno-builtin $< -o $@ 44 
45 $(BUILD_DIR)/%.o : $(PATH4)/%.c 46  gcc -m32 $(INCLUDE) -c -fno-builtin $< -o $@ 47 
48 $(BUILD_DIR)/%.o : $(PATH5)/%.c 49  gcc -m32 $(INCLUDE) -c -fno-stack-protector -fno-builtin $< -o $@ 50 
51 .PHONY:clean #防止 外面有clean文件 阻止执行clean 52 clean: 53  -rm -rf $(BUILD_DIR)/*.o
makefile

  我们新建了一个文件夹build,这个文件以后专门用于存放编译生成的.o文件。这里需要注意一个地方,因为考虑到ld链接的顺序,被依赖的文件应该放在前面。所以这里需要手动添加链接的文件。以后每新增一个.o文件,我们都需要自己手动修改一下makefile。这也是无奈之举。除了这个以外,我们以后只需要通过make就可以编译链接所有文件,通过make install命令就可以自动将生成的bin文件拷贝进硬盘并且启动系统。这个makefile我没有抄书上的,是根据自己的理解来写的。所以可能有些地方看起来很丑,不过能用就行了。

三、实现Assert断言

  Assert断言是什么意思呢?我以前学习stm32的时候,有些时候看源代码会有这种代码出现:

  

  它就是一种Assert断言,什么意思呢?就是对传进来的表达式进行判断,如果为真就跳过,如果为假就报错。就是起到一种debug的作用,好让你知道当程序出错后,是错在哪个地方。在此之前,还需要完善一下interrupt.c和interrupt.h文件,然后在project/kernel目录下新建debug.c和debug.h文件。一并如下:

 1 #include "interrupt.h"
 2 #include "stdint.h"
 3 #include "global.h"
 4 #include "io.h"
 5 #include "print.h"
 6 
 7 #define IDT_DESC_CNT 0x81               //目前支持的中断数
 8 
 9 #define PIC_M_CTRL  0x20                //主片的控制端口是0x20
 10 #define PIC_M_DATA  0x21                //主片的数据端口是0x21
 11 #define PIC_S_CTRL  0xa0                //从片的控制端口是0xa0
 12 #define PIC_S_DATA  0xa1                //从片的数据端口是0xa1
 13 
 14 #define EFLAGS_IF 0x00000200           //eflags寄存器的if位为1
 15 #define GET_EFLAGS(EFLAGS_VAR)    asm volatile("pushfl;  popl %0" : "=g"(EFLAGS_VAR))
 16 
 17 /*中断门描述符结构体*/
 18 struct gate_desc {  19  uint16_t func_offet_low_word;  20  uint16_t selector;  21     uint8_t dcount;                 //此项为双字计数字段,是门描述符中的第4字节
 22  uint8_t attribute;  23  uint16_t func_offet_high_word;  24 };  25 
 26 /*定义IDT表*/
 27 static struct gate_desc idt[IDT_DESC_CNT];  28 extern intr_handler intr_entry_table[IDT_DESC_CNT];  29 
 30 char *intr_name[IDT_DESC_CNT];                //用于保存异常的名字
 31 intr_handler idt_table[IDT_DESC_CNT];         //定义中断处理程序数组
 32 
 33 /*通用的中断处理函数,一般用在异常出现时的处理*/
 34 static void general_intr_handler(uint8_t vec_nr)  35 {  36     if (vec_nr == 0x27 || vec_nr == 0x2f) {  37         return ;  38  }  39 
 40     /*将光标置为0,从屏幕左上角清出一片打印异常信息的区域,方便阅读*/
 41     set_cursor(0);  42     int cursor_pos = 0;  43     while (cursor_pos < 320) {  44         put_char(' ');  45         cursor_pos++;  46  }  47 
 48     set_cursor(0);  49     put_str("!!!!!!!!!!!exception message begin!!!!!!!!!");  50     set_cursor(88);  51  put_str(intr_name[vec_nr]);  52     
 53     //如果为pagefault,将缺失的地址打印出来并且悬停
 54     if (vec_nr == 14) {  55         int page_fault_vaddr = 0;  56         asm volatile ("movl %%cr2, %0": "=r" (page_fault_vaddr)); //cr2存放造成pagefault的虚拟地址
 57         put_str("\npage fault addr is: ");put_int(page_fault_vaddr);  58  }  59     put_str("!!!!!!!!!!!exception message end!!!!!!!!!!");  60     //能进入中断处理程序就表示已经处于关中断的情况下,不会出现进程调度的情况,因此下面的死循环可以一直执行
 61     while (1);  62 }  63 
 64 /*完成一般中断处理函数注册及异常名称注册*/
 65 static void exception_init(void)  66 {  67     int i;  68     for (i = 0; i < IDT_DESC_CNT; i++) {  69         idt_table[i] = general_intr_handler;  70         intr_name[i] = "unknow";  71  }  72     intr_name[0] = "#DE Divide Error";  73     intr_name[1] = "#DB Debug Exception";  74     intr_name[2] = "NMI Interrupt";  75     intr_name[3] = "#BP Breakpoint Exception";  76     intr_name[4] = "#OF Overflow Exception";  77     intr_name[5] = "#BR BOUND Range Exceeded Exception";  78     intr_name[6] = "#UD Invalid Opcode Exception";  79     intr_name[7] = "#NM Device Not Available Exception";  80     intr_name[8] = "#DF Double Fault Exception";  81     intr_name[9] = "Coprocessor Segment Overrun";  82     intr_name[10] = "#TS Invalid TSS Exception";  83     intr_name[11] = "#NP Segment Not Present";  84     intr_name[12] = "#SS Stack Fault Exception";  85     intr_name[13] = "#GP General Protection Exception";  86     intr_name[14] = "#PF Page-Fault Exception";  87     // intr_name[15] 第15项是intel保留项,未使用
 88     intr_name[16] = "#MF x87 FPU Floating-Point Error";  89     intr_name[17] = "#AC Alignment Check Exception";  90     intr_name[18] = "#MC Machine-Check Exception";  91     intr_name[19] = "#XF SIMD Floating-Point Exception";  92 }  93 
 94 /* 初始化可编程中断控制器8259A */
 95 static void pic_init(void) {  96    /* 初始化主片 */
 97    outb(PIC_M_CTRL, 0x11);   // ICW1: 边沿触发,级联8259, 需要ICW4.
 98    outb(PIC_M_DATA, 0x20);   // ICW2: 起始中断向量号为0x20,也就是IR[0-7] 为 0x20 ~ 0x27.
 99    outb(PIC_M_DATA, 0x04);   // ICW3: IR2接从片. 
100    outb(PIC_M_DATA, 0x01);   // ICW4: 8086模式, 正常EOI
101 
102    /* 初始化从片 */
103    outb(PIC_S_CTRL, 0x11);    // ICW1: 边沿触发,级联8259, 需要ICW4.
104    outb(PIC_S_DATA, 0x28);    // ICW2: 起始中断向量号为0x28,也就是IR[8-15] 为 0x28 ~ 0x2F.
105    outb(PIC_S_DATA, 0x02);    // ICW3: 设置从片连接到主片的IR2引脚
106    outb(PIC_S_DATA, 0x01);    // ICW4: 8086模式, 正常EOI
107    
108    /*打开键盘和时钟中断*/
109    outb(PIC_M_DATA, 0xfc); 110    outb(PIC_S_DATA, 0xff); 111 
112    put_str("pic_init done\n"); 113 } 114 
115 
116 /*创建中断门描述符*/
117 static void make_idt_desc(struct gate_desc *p_gdesc, uint8_t attr, intr_handler function) 118 { 119     p_gdesc->func_offet_low_word = (uint32_t)function & 0x0000FFFF; 120     p_gdesc->selector = SELECTOR_K_CODE; 121     p_gdesc->dcount = 0; 122     p_gdesc->attribute = attr; 123     p_gdesc->func_offet_high_word = ((uint32_t)function & 0xFFFF0000) >> 16; 124 } 125 
126 /*初始化中断描述符表*/
127 static void idt_desc_init(void) 128 { 129     int i = 0; 130     for (i = 0; i <IDT_DESC_CNT; i++) { 131         make_idt_desc(&idt[i], IDT_DESC_ATTR_DPL0, intr_entry_table[i]); 132  } 133 
134     /* 单独处理系统调用,系统调用对应的中断门dpl为3 135  中断处理程序为单独的syscall_handler */
136     //make_idt_desc(&idt[0x80], IDT_DESC_ATTR_DPL3, syscall_handler);
137     put_str("ide_desc_init done\n"); 138 } 139 
140 /*完成中断有关的所有初始化工作*/
141 void idt_init(void) 142 { 143     put_str("idt_init start\n"); 144  idt_desc_init(); 145  exception_init(); 146  pic_init(); 147 
148     /*加载idt*/
149     uint64_t idt_operand = (sizeof(idt) - 1) | ((uint64_t)(uint32_t)idt << 16); 150     asm volatile("lidt %0" : : "m"(idt_operand)); 151     put_str("idt_init done\n"); 152 } 153 
154 
155 /*在中断处理程序数组第vector_no个元素中注册安装中断处理程序*/
156 void register_handler(uint8_t vector_no, intr_handler function) 157 { 158     idt_table[vector_no] = function; 159 } 160 
161 
162 /*开中断,并且返回开中断前的状态*/
163 enum intr_status intr_enable(void) 164 { 165     enum intr_status old_status; 166     if (INTR_ON == intr_get_status()) { 167         old_status = INTR_ON; 168         return old_status; 169     } else { 170         old_status = INTR_OFF; 171         asm volatile("sti");  //开中断
172         return old_status; 173  } 174 } 175 
176 /*关中断,并且返回关中断前的状态*/
177 enum intr_status intr_disable(void) 178 { 179     enum intr_status old_status; 180     if (INTR_ON == intr_get_status()) { 181         old_status = INTR_ON; 182         asm volatile("cli": : : "memory"); 183         return old_status; 184     } else { 185         old_status = INTR_OFF; 186         return old_status; 187  } 188 } 189 
190 /*将中断状态设置为status*/
191 enum intr_status intr_set_status(enum intr_status status) 192 { 193     return status & INTR_ON ? intr_enable() : intr_disable(); 194 } 195 
196 /*获取当前中断状态*/
197 enum intr_status intr_get_status(void) 198 { 199     uint32_t eflags = 0; 200  GET_EFLAGS(eflags); 201     return (EFLAGS_IF & eflags) ? INTR_ON : INTR_OFF; 202 }
interrupt.c
#ifndef __KERNEL_INTERRUPT_H #define  __KERNEL_INTERRUPT_H #include "stdint.h"
/*定义中断的两种状态 *INTR_OFF为0,表示关中断 *INTR_ON为1,表示开中断 */
enum intr_status { INTR_OFF, //中断关闭
        INTR_ON       //中断打开
}; typedef void* intr_handler; void register_handler(uint8_t vector_no, intr_handler function); enum intr_status intr_enable(void); enum intr_status intr_disable(void); enum intr_status intr_set_status(enum intr_status status); enum intr_status intr_get_status(void); void idt_init(void); #endif
interrupt.h
#include "debug.h" #include "print.h" #include "interrupt.h"
enum intr_status intr_disable(void); void panic_spin(char *filename, int line, const char *func, const char *condition) { intr_disable(); put_str("\n\n\n!!!!! error !!!!!\n"); put_str("filename:");put_str(filename);put_str("\n"); put_str("line:0x");put_int(line);put_str("\n"); put_str("function:");put_str((char *)func);put_str("\n"); put_str("condition:");put_str((char *)condition);put_str("\n"); while(1); }
debug.c
#ifndef __KERNEL_DEBUG_H #define  __KERNEL_DEBUG_H
void panic_spin(char *filename, int line, const char *func, const char *condition); #define PANIC(...) panic_spin(__FILE__, __LINE__, __func__, __VA_ARGS__) #ifdef NDEBUG #define ASSERT(...) ((void)0)
#else
#define ASSERT(CONDITION) \
if (CONDITION) {} else { \ PANIC(#CONDITION); \ } #endif
#endif
debug.h

四、实现字符串操作函数

  这个没什么好说的,就是一些基本的字符串操作函数,为方便后面的使用。在project/lib/kernel目录下新建string.c和string.h文件,代码如下:

 1 #include "string.h"
 2 #include "global.h"
 3 #include "debug.h"
 4 
 5 /*将dst_起始的size个字节置为value*/
 6 void memset(void *dst_, uint8_t value, uint32_t size)  7 {  8     ASSERT(dst_ != NULL);  9     uint8_t *dst = (uint8_t *)dst_; 10     while (size-- > 0) 11         *dst++ = value; 12 } 13 
14 /*将src_起始的size个字节复制到dst_*/
15 void memcpy(void *dst_, const void *src_, uint32_t size) 16 { 17     ASSERT((dst_ != NULL) && (src_ != NULL)); 18     uint8_t *dst = (uint8_t *)dst_; 19     const uint8_t *src = (const uint8_t *)src_; 20     while (size-- > 0) 21         *dst++ = *src++; 22 } 23 
24 /*连续比较以地址a_和地址b_开头的size个字节,若相等则返回0,若a_大于b_,返回+1,否则返回-1*/
25 int memcmp(const void *a_, const void *b_, uint32_t size) 26 { 27     ASSERT((a_ != NULL) && (b_ != NULL)); 28     const char *a = (const char *)a_; 29     const char *b = (const char *)b_; 30     while (size-- > 0) { 31         if (*a != *b) 32             return (*a > *b) ? 1 : -1; 33         a++; 34         b++; 35  } 36     return 0; 37 } 38 
39 /*将字符串从src_复制到dst_*/
40 char *strcpy(char *dst_, const char *src_) 41 { 42     ASSERT((dst_ != NULL) && (src_ != NULL)); 43     uint8_t *dst = (uint8_t *)dst_; 44     const uint8_t *src = (const uint8_t *)src_; 45     while((*dst++ = *src++)); //先将*src++赋值给*dst++,再判断*dst++是否为0
46     return dst; 47 } 48 
49 /*返回字符串长度*/
50 uint32_t strlen(const char *str) 51 { 52     const char *p = str; 53     while (*p++); 54     return (p - str - 1); 55 } 56 
57 /*比较两个字符串,若a_中的字符大于b_中的字符返回1,相等返回0,否则返回-1*/
58 int8_t strcmp(const void *a_, const void *b_) 59 { 60     ASSERT((a_ != NULL) && (b_ != NULL)); 61     const char *a = (const char *)a_; 62     const char *b = (const char *)b_; 63     if ((*a != 0) && (*a == *b)) { 64         a++; 65         b++; 66  } 67     return (*a < *b) ? -1 : *a > *b;    //这里的*a > *b,如果满足就是1,否则就是0,很巧妙
68 } 69 
70 /*从左往右查找字符串str首次出现字符ch的地址*/
71 char *strchr(const char *str, const uint8_t ch) 72 { 73     ASSERT(str != NULL); 74     while (*str != 0) { 75         if (*str == ch) 76             return (char *)str; 77         str++; 78  } 79     return NULL; 80 }
string.c
#ifndef __LIB_STRING_H #define  __LIB_STRING_H #include "stdint.h"

void memset(void *dst_, uint8_t value, uint32_t size); void memcpy(void *dst_, const void *src_, uint32_t size); int memcmp(const void *a_, const void *b_, uint32_t size); char *strcpy(char *dst_, const char *src_); uint32_t strlen(const char *str); int8_t strcmp(const void *a_, const void *b_); char *strchr(const char *str, const uint8_t ch); #endif
string.h

五、测试

  最后我们来测试一下前面的ASSERT函数的功能。修改main函数如下,不要忘记还要在makefile中增加debug.o和string.o。

#include "print.h" #include "init.h" #include "debug.h"

int main(void) { put_str("HELLO KERNEL\n"); init_all(); ASSERT(1==2); while(1); }
main.c

  最终运行结果如下,也就说明我们的ASSERT函数成功。

  

  本回的内容就到此结束了,下一回合我们开始步入内存管理系统。欲知后事如何,请看下回分解。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM