编程知识 cdmana.com

linux 0.0.1

Linux内核始于0.0.1, 代码行数分布如下:

wc -l `find -type f` | sort -n | nl  (序号 文件行数文件名)

                     1 ./lib/errno.c
                     3 ./fs/file_table.c
                     4 ./lib/close.c
                     4 ./lib/dup.c
                     4 ./lib/execve.c
                     4 ./lib/setsid.c
                     4 ./lib/write.c
                     7 ./lib/_exit.c
                     8 ./include/linux/kernel.h
      10              8 ./lib/string.c
      11            10 ./include/linux/mm.h
      12            10 ./lib/wait.c
      13            11 ./kernel/panic.c
      14            13 ./include/utime.h
      15            14 ./include/asm/memory.h
      16            15 ./include/const.h
      17            15 ./include/sys/times.h
      18            16 ./include/sys/utsname.h
      19            19 ./include/stddef.h
      20            19 ./lib/open.c
      21            20 ./include/linux/head.h
      22            23 ./include/sys/wait.h
      23            24 ./include/asm/io.h
      24            28 ./include/stdarg.h
      25            29 ./lib/ctype.c
      26            33 ./kernel/printk.c
      27            34 ./include/ctype.h
      28            34 ./mm/page.s
      29            37 ./mm/Makefile
      30            38 ./include/asm/segment.h
      31            40 ./fs/ioctl.c
      32            42 ./include/time.h
      33            44 ./lib/Makefile
      34            46 ./include/sys/types.h
      35            50 ./fs/char_dev.c
      36            51 ./fs/stat.c
      37            52 ./kernel/mktime.c
      38            53 ./include/linux/config.h
      39            53 ./kernel/serial.c
      40            55 ./include/fcntl.h
      41            58 ./include/sys/stat.h
      42            59 ./fs/truncate.c
      43            60 ./include/errno.h
      44            65 ./include/signal.h
      45            66 ./include/asm/system.h
      46            68 ./tools/build.c
      47            69 ./fs/fcntl.c
      48            74 ./include/linux/tty.h
      49            80 ./include/linux/sys.h
      50            84 ./fs/file_dev.c
      51            86 ./fs/block_dev.c
      52            90 ./kernel/Makefile
      53            92 ./fs/pipe.c
      54            95 ./fs/Makefile
      55            96 ./Makefile
      56            97 ./fs/read_write.c
      57            99 ./include/linux/hdreg.h
      58          102 ./fs/super.c
      59          135 ./kernel/exit.c
      60          136 ./kernel/fork.c
      61          141 ./kernel/rs_io.s
      62          147 ./init/main.c
      63          157 ./kernel/asm.s
      64          158 ./fs/bitmap.c
      65          166 ./fs/tty_ioctl.c
      66          175 ./boot/head.s
      67          185 ./include/linux/fs.h
      68          188 ./fs/open.c
      69          199 ./kernel/traps.c
      70          216 ./kernel/sys.c
      71          219 ./kernel/system_call.s
      72          220 ./include/a.out.h
      73          222 ./include/termios.h
      74          227 ./kernel/vsprintf.c
      75          230 ./include/linux/sched.h
      76          247 ./include/unistd.h
      77          254 ./fs/buffer.c
      78          254 ./kernel/sched.c
      79          264 ./mm/memory.c
      80          288 ./fs/inode.c
      81          306 ./fs/exec.c
      82          306 ./kernel/tty_io.c
      83          329 ./boot/boot.s
      84          405 ./include/string.h
      85          409 ./kernel/keyboard.s
      86          413 ./kernel/hd.c
      87          550 ./kernel/console.c
      88          678 ./fs/namei.c

10239 总用量

共88个文件, 10239行代码. 最大的文件namei.c也只有678行代码, 但这些可以组成一个正常运行的系统, 正所谓麻雀虽小, 五脏俱全.

系统的构建由96行的Makefile决定, 可以看到系统由boot, system, 和build tool 三部分组成.

boot 部分位于boot/boot.s, 从Makefile看出它由as86编译, ld86链接. 鉴于as86编译器的使
用普遍性较差, 可以考虑将代码稍作修改使其用nasm编译.
主要的改动如下:
1. 常量定义: a=b 换为 a equ b.
2. 注释: 由 "|" 换为 ";"
3. 段名: 由.text 改为 SECTION .text, 注释掉.data, .bss等暂且不用的段.
4. jmpi指令: 换为 jmp SEG:OFFSET 格式.
5. .word换为dw, .byte换为db, .string换为.db
6. inc xxx 换为 inc word [xxx]
7. lidt xxx, lgdt xxx 换为 lidt [xxx], lgdt [xxx]
8. mov mem,reg 换为 mov [mem],reg

然后用nasm -f bin boot/boot.s编译生成bin文件即可.

boot部分行使boot loader的功能, 从软盘装载system部分到内存0x10000, 然后再拷贝到0x00000000, 初始化为保护
模式, 跳转到system部分执行.

System部分由boot/head.s和所有其他部分组成. head.s被loader移动到物理地址0, 然后初始化页表, 1:1影射前8MB内存, 设置中断向量表, 启动分页机制, 然后跳转到main().

版权声明
本文为[shzwork]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4000302/blog/4713239

Tags linux asm nasm
Scroll to Top