logo资料库

Linux0.01内核源代码及注释.pdf

第1页 / 共480页
第2页 / 共480页
第3页 / 共480页
第4页 / 共480页
第5页 / 共480页
第6页 / 共480页
第7页 / 共480页
第8页 / 共480页
资料共480页,剩余部分请下载后查看
Linux0.01 源代码及注释 Bootsect.s ..........................................................................................................................................4 Head.s..............................................................................................................................................12 Setup.s.............................................................................................................................................21 Bitmap.c..........................................................................................................................................29 Block_dev.c.....................................................................................................................................34 Buffer.c............................................................................................................................................37 Char_dev.c ......................................................................................................................................51 Exec.c..............................................................................................................................................54 Fcntl.c..............................................................................................................................................68 File_dev.c........................................................................................................................................70 File_table.c......................................................................................................................................74 Inode.c.............................................................................................................................................74 Ioctl.c ..............................................................................................................................................86 Namei.c ...........................................................................................................................................88 Open.c ...........................................................................................................................................117 Pipe.c.............................................................................................................................................125 Read_write.c .................................................................................................................................129 Stat.c..............................................................................................................................................133 Super.c...........................................................................................................................................135 Truncate.c......................................................................................................................................146 Include...........................................................................................................................................148 Asm...............................................................................................................................................148 io.h ................................................................................................................................................148 Memory.h......................................................................................................................................149 Segment.h......................................................................................................................................150 System.h........................................................................................................................................153 Fs.h................................................................................................................................................155 Hdreg.h..........................................................................................................................................161 Head.h ...........................................................................................................................................163 Kernel.h.........................................................................................................................................164 Mm.h.............................................................................................................................................165 Sched.h..........................................................................................................................................165 Sys.h..............................................................................................................................................174 Tty.h ..............................................................................................................................................176 Config.h ........................................................................................................................................178 Stat.h .............................................................................................................................................180 Times.h..........................................................................................................................................182 Types.h..........................................................................................................................................183 Utsname.h .....................................................................................................................................184 Wait.h............................................................................................................................................184 Out.h..............................................................................................................................................185 Const.h ..........................................................................................................................................192 1
Stype.h ..........................................................................................................................................193 Errno.h ..........................................................................................................................................194 Fcntl.h ...........................................................................................................................................196 Singnal.h .......................................................................................................................................198 Stdarg.h .........................................................................................................................................200 Stddef.h .........................................................................................................................................201 String.h..........................................................................................................................................202 Termios.h ......................................................................................................................................213 Time.h ...........................................................................................................................................221 Unistd.h.........................................................................................................................................222 Utime.h..........................................................................................................................................229 Main.c ...........................................................................................................................................229 Blk.h..............................................................................................................................................237 Floppy.c.........................................................................................................................................241 Hd.c...............................................................................................................................................258 Ll_rw_blk.c...................................................................................................................................269 Ramdisk.c......................................................................................................................................275 Makefile ........................................................................................................................................280 Console.c.......................................................................................................................................282 Keyboard.s ....................................................................................................................................306 Rs_io.s...........................................................................................................................................322 Serial.c ..........................................................................................................................................338 Tty_io.c .........................................................................................................................................340 Tty_ioctl.c .....................................................................................................................................354 Makefile ........................................................................................................................................361 Math_emulate.c.............................................................................................................................364 Makefile ........................................................................................................................................366 Asm.s ............................................................................................................................................368 Exit.c.............................................................................................................................................373 Fork.c ............................................................................................................................................379 Mktime.c .......................................................................................................................................384 Panic.c...........................................................................................................................................385 Printk.c..........................................................................................................................................386 Sched.c..........................................................................................................................................388 Signal.c..........................................................................................................................................402 Sys.c..............................................................................................................................................406 System_call.s ................................................................................................................................414 Vsprintf.c ......................................................................................................................................423 _exit.c............................................................................................................................................431 Close.c...........................................................................................................................................432 Ctype.c ..........................................................................................................................................432 Dup.c.............................................................................................................................................433 Execve.c........................................................................................................................................434 Malloc.c ........................................................................................................................................435 2
Open.c ...........................................................................................................................................444 String.c..........................................................................................................................................445 Setsid.c..........................................................................................................................................445 Wait.c ............................................................................................................................................446 Write.c...........................................................................................................................................446 Makefile ........................................................................................................................................447 Memory.c ......................................................................................................................................450 page.s ............................................................................................................................................466 Makefile ........................................................................................................................................467 Build.c...........................................................................................................................................469 Makefile(总)...............................................................................................................................475 3
Bootsect.s ! ! SYS_SIZE is the number of clicks (16 bytes) to be loaded. ! 0x3000 is 0x30000 bytes = 196kB, more than enough for current ! versions of linux ! SYS_SIZE 是要加载的节数(16 字节为 1 节)。0x3000 共为 1 2 3 4 5 6 0x7c00 0x0000 0x90000 0x10000 0xA0000 system 模块 代码执行位置线路 0x90200 ! 0x30000 字节=192 kB(上面 Linus 估算错了),对于当前的版本空间已足够了。 ! SYSSIZE = 0x3000 ! 指编译连接后 system 模块的大小。参见列表 1.2 中第 92 的说明。 ! 这里给出了一个最大默认值。 ! ! bootsect.s (C) 1991 Linus Torvalds ! ! bootsect.s is loaded at 0x7c00 by the bios-startup routines, and moves ! iself out of the way to address 0x90000, and jumps there. ! ! It then loads 'setup' directly after itself (0x90200), and the system ! at 0x10000, using BIOS interrupts. ! ! NOTE! currently system is at most 8*65536 bytes long. This should be no ! problem, even in the future. I want to keep it simple. This 512 kB ! kernel size should be enough, especially as this doesn't contain the ! buffer cache as in minix ! ! The loader has been made as simple as possible, and continuos ! read errors will result in a unbreakable loop. Reboot by hand. It ! loads pretty fast by getting whole sectors at a time whenever possible. ! ! 以下是前面这些文字的翻译: ! bootsect.s (C) 1991 Linus Torvalds 版权所有 ! ! bootsect.s 被 bios-启动子程序加载至 0x7c00 (31k)处,并将自己 ! 移到了地址 0x90000 (576k)处,并跳转至那里。 ! 4
! 它然后使用 BIOS 中断将'setup'直接加载到自己的后面(0x90200)(576.5k), ! 并将 system 加载到地址 0x10000 处。 ! ! 注意! 目前的内核系统最大长度限制为(8*65536)(512k)字节,即使是在 ! 将来这也应该没有问题的。我想让它保持简单明了。这样 512k 的最大内核长度应该 ! 足够了,尤其是这里没有象 minix 中一样包含缓冲区高速缓冲。 ! ! 加载程序已经做的够简单了,所以持续的读出错将导致死循环。只能手工重启。 ! 只要可能,通过一次取取所有的扇区,加载过程可以做的很快的。 .globl begtext, begdata, begbss, endtext, enddata, endbss ! 定义了 6 个全局标识符; .text ! 文本段; begtext: .data ! 数据段; begdata: .bss ! 堆栈段; begbss: .text ! 文本段; SETUPLEN = 4 ! nr of setup-sectors ! setup 程序的扇区数(setup-sectors)值; BOOTSEG = 0x07c0 ! original address of boot-sector ! bootsect 的原始地址(是段地址,以下同); INITSEG = 0x9000 ! we move boot here - out of the way ! 将 bootsect 移到这里 -- 避开; SETUPSEG = 0x9020 ! setup starts here ! setup 程序从这里开始; SYSSEG = 0x1000 ! system loaded at 0x10000 (65536). ! system 模块加载到 0x10000(64 kB)处; ENDSEG = SYSSEG + SYSSIZE ! where to stop loading ! 停止加载的段地址; ! ROOT_DEV: 0x000 - same type of floppy as boot. ! 根文件系统设备使用与引导时同样的软驱设备; ! 0x301 - first partition on first drive etc ! 根文件系统设备在第一个硬盘的第一个分区上,等等; ROOT_DEV = 0x306 ! 指定根文件系统设备是第 2 个硬盘的第 1 个分区。这是 Linux 老式 的硬盘命名 ! 方式,具体值的含义如下: ! 设备号=主设备号*256 + 次设备号(也即 dev_no = (major<<8) + minor ) ! (主设备号:1-内存,2-磁盘,3-硬盘,4-ttyx,5-tty,6-并行口,7-非命名管道) ! 0x300 - /dev/hd0 - 代表整个第 1 个硬盘; ! 0x301 - /dev/hd1 - 第 1 个盘的第 1 个分区; ! … 5
! 0x304 - /dev/hd4 - 第 1 个盘的第 4 个分区; ! 0x305 - /dev/hd5 - 代表整个第 2 个硬盘盘; ! 0x306 - /dev/hd6 - 第 2 个盘的第 1 个分区; ! … ! 0x309 - /dev/hd9 - 第 2 个盘的第 4 个分区; ! 从 linux 内核 0.95 版后已经使用与现在相同的命名方法了。 entry start ! 告知连接程序,程序从 start 标号开始执行。 start: ! 47--56 行作用是将自身(bootsect)从目前段位置 0x07c0(31k) ! 移动到 0x9000(576k)处,共 256 字(512 字节),然后跳转到 ! 移动后代码的 go 标号处,也即本程序的下一语句处。 mov ax,#BOOTSEG ! 将 ds 段寄存器置为 0x7C0; mov ds,ax mov ax,#INITSEG ! 将 es 段寄存器置为 0x9000; mov es,ax mov cx,#256 ! 移动计数值=256 字; sub si,si ! 源地址 ds:si = 0x07C0:0x0000 sub di,di ! 目的地址 es:di = 0x9000:0x0000 rep ! 重复执行,直到 cx = 0 movw ! 移动 1 个字; jmpi go,INITSEG ! 间接跳转。这里 INITSEG 指出跳转到的段地址。 go: mov ax,cs ! 将 ds、es 和 ss 都置成移动后代码所在的段处(0x9000)。 mov ds,ax !由于程序中有堆栈操作(push,pop,call),因此必须设置堆栈。 mov es,ax ! put stack at 0x9ff00. ! 将堆栈指针 sp 指向 0x9ff00(即 0x9000:0xff00)处 mov ss,ax mov sp,#0xFF00 ! arbitrary value >>512 ! 由于代码段移动过了,所以要重新设置堆栈段的位置。 ! sp 只要指向远大于 512 偏移(即地址 0x90200)处 ! 都可以。因为从 0x90200 地址开始处还要放置 setup 程序, ! 而此时 setup 程序大约为 4 个扇区,因此 sp 要指向大 ! 于(0x200 + 0x200 * 4 + 堆栈大小)处。 ! load the setup-sectors directly after the bootblock. ! Note that 'es' is already set up. ! 在 bootsect 程序块后紧根着加载 setup 模块的代码数据。 ! 注意 es 已经设置好了。(在移动代码时 es 已经指向目的段地址处 0x9000)。 load_setup: ! 68--77 行的用途是利用 BIOS 中断 INT 0x13 将 setup 模块从磁盘第 2 个扇区 ! 开始读到 0x90200 开始处,共读 4 个扇区。如果读出错,则复位驱动器,并 ! 重试,没有退路。INT 0x13 的使用方法如下: ! 读扇区: ! ah = 0x02 - 读磁盘扇区到内存;al = 需要读出的扇区数量; 6
! ch = 磁道(柱面)号的低 8 位; cl = 开始扇区(0-5 位),磁道号高 2 位(6-7); ! dh = 磁头号; dl = 驱动器号(如果是硬盘则要置位 7); ! es:bx ??指向数据缓冲区; 如果出错则 CF 标志置位。 mov dx,#0x0000 ! drive 0, head 0 mov cx,#0x0002 ! sector 2, track 0 mov bx,#0x0200 ! address = 512, in INITSEG mov ax,#0x0200+SETUPLEN ! service 2, nr of sectors int 0x13 ! read it jnc ok_load_setup ! ok - continue mov dx,#0x0000 mov ax,#0x0000 ! reset the diskette int 0x13 j load_setup ok_load_setup: ! Get disk drive parameters, specifically nr of sectors/track ! 取磁盘驱动器的参数,特别是每道的扇区数量。 ! 取磁盘驱动器参数 INT 0x13 调用格式和返回信息如下: ! ah = 0x08 dl = 驱动器号(如果是硬盘则要置位 7 为 1)。 ! 返回信息: ! 如果出错则 CF 置位,并且 ah = 状态码。 ! ah = 0, al = 0, bl = 驱动器类型(AT/PS2) ! ch = 最大磁道号的低 8 位,cl = 每磁道最大扇区数(位 0-5),最大磁道号高 2 位(位 6-7) ! dh = 最大磁头数, dl = 驱动器数量, ! es:di -?? 软驱磁盘参数表。 mov dl,#0x00 mov ax,#0x0800 ! AH=8 is get drive parameters int 0x13 mov ch,#0x00 seg cs ! 表示下一条语句的操作数在 cs 段寄存器所指的段中。 mov sectors,cx ! 保存每磁道扇区数。 mov ax,#INITSEG mov es,ax ! 因为上面取磁盘参数中断改掉了 es 的值,这里重新改回。 ! Print some inane message ! 在显示一些信息('Loading system ...'回车换行,共 24 个字符)。 mov ah,#0x03 ! read cursor pos xor bh,bh ! 读光标位置。 int 0x10 mov cx,#24 ! 共 24 个字符。 mov bx,#0x0007 ! page 0, attribute 7 (normal) 7
mov bp,#msg1 ! 指向要显示的字符串。 mov ax,#0x1301 ! write string, move cursor int 0x10 ! 写字符串并移动光标。 ! ok, we've written the message, now ! we want to load the system (at 0x10000) ! 现在开始将 system 模块加载到 0x10000(64k)处。 mov ax,#SYSSEG mov es,ax ! segment of 0x010000 ! es = 存放 system 的段地址。 call read_it ! 读磁盘上 system 模块,es 为输入参数。 call kill_motor ! 关闭驱动器马达,这样就可以知道驱动器的状态了。 ! After that we check which root-device to use. If the device is ! defined (!= 0), nothing is done and the given device is used. ! Otherwise, either /dev/PS0 (2,28) or /dev/at0 (2,8), depending ! on the number of sectors that the BIOS reports currently. ! 此后,我们检查要使用哪个根文件系统设备(简称根设备)。如果已经指定了设备(!=0) ! 就直接使用给定的设备。否则就需要根据 BIOS 报告的每磁道扇区数来 ! 确定到底使用/dev/PS0 (2,28) 还是 /dev/at0 (2,8)。 ! 上面一行中两个设备文件的含义: ! 在 Linux 中软驱的主设备号是 2(参见第 43 行的注释),次设备号 = type*4 + nr,其中 ! nr 为 0-3 分别对应软驱 A、B、C 或 D;type 是软驱的类型(2??1.2M 或 7??1.44M 等)。 ! 因为 7*4 + 0 = 28,所以 /dev/PS0 (2,28)指的是 1.44M A 驱动器,其设备号是 0x021c ! 同理 /dev/at0 (2,8)指的是 1.2M A 驱动器,其设备号是 0x0208。 seg cs mov ax,root_dev ! 将根设备号 cmp ax,#0 jne root_defined seg cs mov bx,sectors ! 取上面第 88 行保存的每磁道扇区数。如果 sectors=15 ! 则说明是 1.2Mb 的驱动器;如果 sectors=18,则说明是 ! 1.44Mb 软驱。因为是可引导的驱动器,所以肯定是 A 驱。 mov ax,#0x0208 ! /dev/ps0 - 1.2Mb cmp bx,#15 ! 判断每磁道扇区数是否=15 je root_defined ! 如果等于,则 ax 中就是引导驱动器的设备号。 mov ax,#0x021c ! /dev/PS0 - 1.44Mb cmp bx,#18 je root_defined undef_root: ! 如果都不一样,则死循环(死机)。 jmp undef_root root_defined: seg cs mov root_dev,ax ! 将检查过的设备号保存起来。 8
分享到:
收藏