Main函数代码
/* * GeekOS C code entry point * Copyright (c) 2001,2003,2004 David H. Hovemeyer <[email protected]> * Copyright (c) 2003, Jeffrey K. Hollingsworth <[email protected]> * Copyright (c) 2004, Iulian Neamtiu <[email protected]> * $Revision: 1.51 $ * * This is free software. You are permitted to use, * redistribute, and modify it as specified in the file "COPYING". */ #include <geekos/bootinfo.h> #include <geekos/string.h> #include <geekos/screen.h> #include <geekos/mem.h> #include <geekos/crc32.h> #include <geekos/tss.h> #include <geekos/int.h> #include <geekos/kthread.h> #include <geekos/trap.h> #include <geekos/timer.h> #include <geekos/keyboard.h> /* * Kernel C code entry point. * Initializes kernel subsystems, mounts filesystems, * and spawns init process. */ void Main(struct Boot_Info* bootInfo) { Init_BSS(); Init_Screen(); Init_Mem(bootInfo); Init_CRC32(); Init_TSS(); Init_Interrupts(); Init_Scheduler(); Init_Traps(); Init_Timer(); Init_Keyboard(); Set_Current_Attr(ATTRIB(BLACK, GREEN|BRIGHT)); Print("Welcome to GeekOS!\n"); Set_Current_Attr(ATTRIB(BLACK, GRAY)); TODO("Start a kernel thread to echo pressed keys and print counts"); /* Now this thread is done. */ Exit(0); }
先来直接的看一下Set_Current_Attr(),这个函数用于控制终端显示属性。
位于/src/geekos/screen.c
/* * Set the current character attribute. */ void Set_Current_Attr(uchar_t attrib) { bool iflag = Begin_Int_Atomic(); s_cons.currentAttr = attrib; End_Int_Atomic(iflag); }
这里有成对出现的Begin_Int_Atomic()和End_Int_Atomic();
用于控制临界区的原子执行,就是在这两个函数之间的代码,运行时,任何其他代码都不能打断它。
更本质的说,就是CPU计数器EIP只能连续的加,不能跳到其他地方。
当然,你要是关机了,那谁也拦不住你。
因为终端属性是一个全局的变量,存储于内存的一个固定空间内,任何内核进程都可以访问它,所以要对它进行保护。
这两个函数会在后面详细研究。
Set_Current_Attr函数中的s_cons是struct Console_State结构,如下
位于./src/geekos/screen.c
struct Console_State { /* Current state information */ int row, col; int saveRow, saveCol; uchar_t currentAttr; /* Working variables for processing escape sequences. */ enum State state; int argList[MAXARGS]; int numArgs; }; static struct Console_State s_cons;
位于./include/geekos/kassert.h
#define TODO(message) \ do { \ Set_Current_Attr(ATTRIB(BLUE, GRAY|BRIGHT)); \ Print("Unimplemented feature: %s\n", (message)); \ while (1) \ ; \ } while (0)
这个宏的实现使用了do{something}while(0)的手法,很好的降低了宏和外部代码的耦合度。
好,先把最简单的讲完,接下来就是各个初始化函数了,还有最后一个的Exit(0),我在下面分别详细阐述。