Linux-0.0.1内核阅读连载笔记-2013.08.20

来源:互联网 发布:usb3.0根集线器 ubuntu 编辑:程序博客网 时间:2024/05/17 23:54

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

../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() */
}
原创粉丝点击