閱讀890 返回首頁    go 微軟 go windows


Linux-0.0.1內核閱讀連載筆記-2013.08.20

----------------------------------------------------------------------------------

../init/main.c

----------------------------------------------------------------------------------


/**

 *本句的作用是打開 unistd.h 中定義的宏變量 _syscall0() 等

 */

#define __LIBRARY__


#include <unistd.h>
#include <time.h>


/**

 * 在unistd.h 中有:

 ------------------------------------------------

#define _syscall0(type,name) \
type name(void) \
{ \
type __res; \
//下麵的內嵌匯編的意思是:
// movl __NR_##name %eax
// int $0x80
// movl %eax _res
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__NR_##name)); \
if (__res >= 0) \
return __res; \
errno = -__res; \
return -1; \
}

 ------------------------------------------------

 */

/**

 *inline 函數

 *起因:

 *使用表達式宏定義的原因,C語言是一個效率很高的語言,這種宏定義在形式及使用上像一個函數,但它使用預處理器實現,沒有了參數壓棧,代碼    生成等一係列的操作,因此,效率很高,這是它在C中被使用的一個主要原因 

 *這種宏定義在形式上類似於一個函數,但在使用它時,僅僅隻是做預處理器符號表中的簡單替換,因此它不能進行參數有效性的檢測,也就不能享受  編譯器嚴格類型檢查的好處,另外它的返回值也不能被強製轉換為可轉換的合適的類型,這樣,它的使用就存在著一係列的隱患和局限性

 *用它替代C中表達式形式的

 *除了宏定義的缺點,同時又很好地繼承了宏定義的優點宏定義

 */

static inline _syscall0(int,fork)
static inline _syscall0(int,pause)
static inline _syscall0(int,setup)
static inline _syscall0(int,sync)


#include <linux/tty.h>
#include <linux/sched.h>
#include <linux/head.h>
#include <asm/system.h>
#include <asm/io.h>

#include <stddef.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>

#include <linux/fs.h>

static char printbuf[1024];

extern int vsprintf();
extern void init(void);
extern void hd_init(void);
extern long kernel_mktime(struct tm * tm);
extern long startup_time;

/**

 *這段宏讀取CMOS 實時時鍾信息

 *0x70 是寫端口號,0x80|addr 是要讀取的CMOS 內存地址

 *0x71 是讀端口號

 */


#define CMOS_READ(addr) ({ \ //禁止NMI(不可屏蔽中斷)
outb_p(0x80|addr,0x70); \
inb_p(0x71); \

})

/**

 *兩位的 BCD 碼轉換為十進製:用 BCD 的低四位 + BCD 的高四位 * 10

 */

#define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)//二進製編碼的十進製表示


static void time_init(void)
{
struct tm time;


do {
time.tm_sec = CMOS_READ(0);
time.tm_min = CMOS_READ(2);
time.tm_hour = CMOS_READ(4);
time.tm_mday = CMOS_READ(7);
time.tm_mon = CMOS_READ(8)-1;
time.tm_year = CMOS_READ(9);
} while (time.tm_sec != CMOS_READ(0));//一定要在1秒之間完成time結構
BCD_TO_BIN(time.tm_sec);
BCD_TO_BIN(time.tm_min);
BCD_TO_BIN(time.tm_hour);
BCD_TO_BIN(time.tm_mday);
BCD_TO_BIN(time.tm_mon);
BCD_TO_BIN(time.tm_year);
startup_time = kernel_mktime(&time);
}


void main(void) /* This really IS void, no error here. */
{ /* The startup routine assumes (well, ...) this */
/*
 * Interrupts are still disabled. Do necessary setups, then
 * enable them
 */
time_init();
tty_init();
trap_init();
sched_init();
buffer_init();
hd_init();
sti();
move_to_user_mode();//轉入用戶模式
if (!fork()) {/* we count on this going ok */
init();

}


for(;;) pause();//暫停task0,循環讓其他進程運行,在這裏其實隻有一個0任務
}


static int printf(const char *fmt, ...)
{
va_list args;
int i;


va_start(args, fmt);
write(1,printbuf,i=vsprintf(printbuf, fmt, args));
va_end(args);
return i;
}


static char * argv[] = { "-",NULL };
static char * envp[] = { "HOME=/usr/root", NULL };


void init(void)
{
int i,j;


setup(); //讀取硬盤信息,並將信息存入start_buffer->b_data中,並保存分區信息,登記根超級塊
if (!fork())
_exit(execve("/bin/update",NULL,NULL));
(void) open("/dev/tty0",O_RDWR,0);
(void) dup(0);//複製一個tty0
(void) dup(0);//再複製一個tty0
printf("%d buffers = %d bytes buffer space\n\r",NR_BUFFERS,
NR_BUFFERS*BLOCK_SIZE);
printf(" Ok.\n\r");
if ((i=fork())<0)//複製當前進程
printf("Fork failed in init\r\n");
else if (!i) {//如果成功
close(0);close(1);close(2);//關閉打開的3個tty0文件
setsid();
(void) open("/dev/tty0",O_RDWR,0);
(void) dup(0);
(void) dup(0);
_exit(execve("/bin/sh",argv,envp));//執行shell
}
j=wait(&i);
printf("child %d died with code %04x\n",j,i);
sync();
_exit(0); /* NOTE! _exit, not exit() */
}

最後更新:2017-04-03 16:48:59

  上一篇:go C# 基礎知識 (一).概念與思想篇
  下一篇:go poj 1144 Network 關節點