2018年3月6日 星期二

linux 0.11 trace boot/setup.s

  1. .code16
  2. # rewrite with AT&T syntax by falcon at 081012
  3. #
  4. # setup.s (C) 1991 Linus Torvalds
  5. #
  6. # setup.s is responsible for getting the system data from the BIOS,
  7. # and putting them into the appropriate places in system memory.
  8. # both setup.s and system has been loaded by the bootblock.
  9. #
  10. # This code asks the bios for memory/disk/other parameters, and
  11. # puts them in a "safe" place: 0x90000-0x901FF, ie where the
  12. # boot-block used to be. It is then up to the protected mode
  13. # system to read them from there before the area is overwritten
  14. # for buffer-blocks.
  15. #
  16. # NOTE! These had better be the same as in bootsect.s!
  17. .equ INITSEG, 0x9000 # we move boot here - out of the way
  18. .equ SYSSEG, 0x1000 # system loaded at 0x10000 (65536).
  19. .equ SETUPSEG, 0x9020 # this is the current segment
  20. .global _start, begtext, begdata, begbss, endtext, enddata, endbss
  21. .text
  22. begtext:
  23. .data
  24. begdata:
  25. .bss
  26. begbss:
  27. .text
  28. ljmp $SETUPSEG, $_start #boot/bootsetup.s中,會透過ljmp $SETUPSEG, $0跳到這一行
  29. _start:
  30. # ok, the read went well so we get current cursor position and save it for
  31. # posterity.
  32. mov $INITSEG, %ax # this is done in bootsect already, but...,這裡原先是放用來執行bootsect的code,
  33. #但現在已經用不到了,改為放從bios讀到的資訊(cursor pos,memory size,...)
  34. mov %ax, %ds
  35. mov $0x03, %ah # read cursor pos
  36. xor %bh, %bh
  37. int $0x10 # save it in known place, con_init fetches
  38. mov %dx, %ds:0 # it from 0x90000.
  39. # Get memory size (extended mem, kB)
  40. mov $0x88, %ah
  41. int $0x15
  42. mov %ax, %ds:2
  43. # Get video-card data:
  44. mov $0x0f, %ah
  45. int $0x10
  46. mov %bx, %ds:4 # bh = display page
  47. mov %ax, %ds:6 # al = video mode, ah = window width
  48. # check for EGA/VGA and some config parameters
  49. mov $0x12, %ah
  50. mov $0x10, %bl
  51. int $0x10
  52. mov %ax, %ds:8
  53. mov %bx, %ds:10
  54. mov %cx, %ds:12
  55. # Get hd0 data
  56. mov $0x0000, %ax
  57. mov %ax, %ds
  58. lds %ds:4*0x41, %si
  59. mov $INITSEG, %ax
  60. mov %ax, %es
  61. mov $0x0080, %di
  62. mov $0x10, %cx
  63. rep
  64. movsb
  65. # Get hd1 data
  66. mov $0x0000, %ax
  67. mov %ax, %ds
  68. lds %ds:4*0x46, %si
  69. mov $INITSEG, %ax
  70. mov %ax, %es
  71. mov $0x0090, %di
  72. mov $0x10, %cx
  73. rep
  74. movsb
  75. # Check that there IS a hd1 :-)
  76. mov $0x01500, %ax
  77. mov $0x81, %dl
  78. int $0x13
  79. jc no_disk1
  80. cmp $3, %ah
  81. je is_disk1
  82. no_disk1:
  83. mov $INITSEG, %ax
  84. mov %ax, %es
  85. mov $0x0090, %di
  86. mov $0x10, %cx
  87. mov $0x00, %ax
  88. rep
  89. stosb
  90. is_disk1:
  91. # now we want to move to protected mode ...
  92. cli # no interrupts allowed !
  93. # first we move the system to it's rightful place,system大小=tools/kernel=121508bytes
  94. mov $0x0000, %ax
  95. cld # 'direction'=0, movs moves forward,SI DI遞增
  96. do_move: #第一次為將資料從source 0x1000 移到destination 0x0000,移65536bytes,
  97. #之後為source與dest分別加0x1000再做相同的動作,直到src=0x9000時,則跳出迴圈
  98. mov %ax, %es # destination segment
  99. add $0x1000, %ax
  100. cmp $0x9000, %ax #因為這邊system大小為121508bytes,故最少只須搬兩次(65536*2),即最少只須cmp到$0x3000
  101. jz end_move
  102. mov %ax, %ds # source segment
  103. sub %di, %di
  104. sub %si, %si
  105. mov $0x8000, %cx
  106. rep
  107. movsw
  108. jmp do_move
  109. # then we load the segment descriptors
  110. end_move:
  111. mov $SETUPSEG, %ax # right, forgot this at first. didn't work :-)
  112. mov %ax, %ds
  113. lidt idt_48 # load idt with 0,0
  114. lgdt gdt_48 # load gdt with whatever appropriate
  115. # that was painless, now we enable A20
  116. #call empty_8042 # 8042 is the keyboard controller
  117. #mov $0xD1, %al # command write
  118. #out %al, $0x64
  119. #call empty_8042
  120. #mov $0xDF, %al # A20 on
  121. #out %al, $0x60
  122. #call empty_8042
  123. inb $0x92, %al # open A20 line(Fast Gate A20).
  124. orb $0b00000010, %al
  125. outb %al, $0x92
  126. # well, that went ok, I hope. Now we have to reprogram the interrupts :-(
  127. # we put them right after the intel-reserved hardware interrupts, at
  128. # int 0x20-0x2F. There they won't mess up anything. Sadly IBM really
  129. # messed this up with the original PC, and they haven't been able to
  130. # rectify it afterwards. Thus the bios puts interrupts at 0x08-0x0f,
  131. # which is used for the internal hardware interrupts as well. We just
  132. # have to reprogram the 8259's, and it isn't fun.
  133. mov $0x11, %al # initialization sequence(ICW1)
  134. # ICW4 needed(1),CASCADE mode,Level-triggered
  135. out %al, $0x20 # send it to 8259A-1
  136. .word 0x00eb,0x00eb # jmp $+2, jmp $+2
  137. out %al, $0xA0 # and to 8259A-2
  138. .word 0x00eb,0x00eb
  139. mov $0x20, %al # start of hardware int's (0x20)(ICW2)
  140. out %al, $0x21 # from 0x20-0x27
  141. .word 0x00eb,0x00eb
  142. mov $0x28, %al # start of hardware int's 2 (0x28)
  143. out %al, $0xA1 # from 0x28-0x2F
  144. .word 0x00eb,0x00eb # IR 7654 3210
  145. mov $0x04, %al # 8259-1 is master(0000 0100) --\
  146. out %al, $0x21 # |
  147. .word 0x00eb,0x00eb # INT /
  148. mov $0x02, %al # 8259-2 is slave( 010 --> 2)
  149. out %al, $0xA1
  150. .word 0x00eb,0x00eb
  151. mov $0x01, %al # 8086 mode for both
  152. out %al, $0x21
  153. .word 0x00eb,0x00eb
  154. out %al, $0xA1
  155. .word 0x00eb,0x00eb
  156. mov $0xFF, %al # mask off all interrupts for now
  157. out %al, $0x21
  158. .word 0x00eb,0x00eb
  159. out %al, $0xA1
  160. # well, that certainly wasn't fun :-(. Hopefully it works, and we don't
  161. # need no steenking BIOS anyway (except for the initial loading :-).
  162. # The BIOS-routine wants lots of unnecessary data, and it's less
  163. # "interesting" anyway. This is how REAL programmers do it.
  164. #
  165. # Well, now's the time to actually move into protected mode. To make
  166. # things as simple as possible, we do no register set-up or anything,
  167. # we let the gnu-compiled 32-bit programs do that. We just jump to
  168. # absolute address 0x00000, in 32-bit protected mode.
  169. #mov $0x0001, %ax # protected mode (PE) bit
  170. #lmsw %ax # This is it!
  171. mov %cr0, %eax # get machine status(cr0|MSW)
  172. bts $0, %eax # turn on the PE-bit
  173. mov %eax, %cr0 # protection enabled
  174. # segment-descriptor (INDEX:TI:RPL)
  175. .equ sel_cs0, 0x0008 # select for code segment 0 ( 001:0 :00)
  176. ljmp $sel_cs0, $0 # jmp offset 0 of code segment 0 in gdt
  177. # This routine checks that the keyboard command queue is empty
  178. # No timeout is used - if this hangs there is something wrong with
  179. # the machine, and we probably couldn't proceed anyway.
  180. empty_8042:
  181. .word 0x00eb,0x00eb
  182. in $0x64, %al # 8042 status port
  183. test $2, %al # is input buffer full?
  184. jnz empty_8042 # yes - loop
  185. ret
  186. gdt:
  187. .word 0,0,0,0 # dummy
  188. .word 0x07FF # 8Mb - limit=2047 (2048*4096=8Mb)
  189. .word 0x0000 # base address=0
  190. .word 0x9A00 # code read/exec
  191. .word 0x00C0 # granularity=4096, 386
  192. .word 0x07FF # 8Mb - limit=2047 (2048*4096=8Mb)
  193. .word 0x0000 # base address=0
  194. .word 0x9200 # data read/write
  195. .word 0x00C0 # granularity=4096, 386
  196. idt_48:
  197. .word 0 # idt limit=0
  198. .word 0,0 # idt base=0L
  199. gdt_48:
  200. .word 0x800 # gdt limit=2048, 256 GDT entries
  201. .word 512+gdt, 0x9 # gdt base = 0X9xxxx,
  202. # 512+gdt is the real gdt after setup is moved to 0x9020 * 0x10
  203. .text
  204. endtext:
  205. .data
  206. enddata:
  207. .bss
  208. endbss:

linux 0.11 trace boot/bootsect.s

  1. .code16
  2. # rewrite with AT&T syntax by falcon at 081012
  3. #
  4. # SYS_SIZE is the number of clicks (16 bytes) to be loaded.
  5. # 0x3000 is 0x30000 bytes = 196kB, more than enough for current
  6. # versions of linux
  7. #
  8. .equ SYSSIZE, 0x3000
  9. #
  10. # bootsect.s (C) 1991 Linus Torvalds
  11. #
  12. # bootsect.s is loaded at 0x7c00 by the bios-startup routines, and moves
  13. # iself out of the way to address 0x90000, and jumps there.
  14. #
  15. # It then loads 'setup' directly after itself (0x90200), and the system
  16. # at 0x10000, using BIOS interrupts.
  17. #
  18. # NOTE! currently system is at most 8*65536 bytes long. This should be no
  19. # problem, even in the future. I want to keep it simple. This 512 kB
  20. # kernel size should be enough, especially as this doesn't contain the
  21. # buffer cache as in minix
  22. #
  23. # The loader has been made as simple as possible, and continuos
  24. # read errors will result in a unbreakable loop. Reboot by hand. It
  25. # loads pretty fast by getting whole sectors at a time whenever possible.
  26. .global _start, begtext, begdata, begbss, endtext, enddata, endbss
  27. .text
  28. begtext:
  29. .data
  30. begdata:
  31. .bss
  32. begbss:
  33. .text
  34. .equ SETUPLEN, 4 # nr of setup-sectors
  35. .equ BOOTSEG, 0x07c0 # original address of boot-sector
  36. .equ INITSEG, 0x9000 # we move boot here - out of the way
  37. .equ SETUPSEG, 0x9020 # setup starts here
  38. .equ SYSSEG, 0x1000 # system loaded at 0x10000 (65536).
  39. .equ ENDSEG, SYSSEG + SYSSIZE # where to stop loading
  40. # ROOT_DEV: 0x000 - same type of floppy as boot.
  41. # 0x301 - first partition on first drive etc
  42. .equ ROOT_DEV, 0x301
  43. ljmp $BOOTSEG, $_start #跳至cs:eip,這樣一行可以不用加,因為現在的cs即為BOOTSEG(0x07c0)
  44. _start: #將BOOTSEG上的資料依序往後複製至INITSEG,共複製256回合,每回合複製兩個byte(即一個word),即從BOOTSEG複製512bytes到INITSEG上
  45. mov $BOOTSEG, %ax
  46. mov %ax, %ds
  47. mov $INITSEG, %ax
  48. mov %ax, %es #es設定為INITSEG,之後ljmp會用到
  49. mov $256, %cx
  50. sub %si, %si
  51. sub %di, %di
  52. rep
  53. movsw
  54. ljmp $INITSEG, $go #跳至INITSEG seg(跳過去後,會執行複製後的bootsect code)
  55. go: mov %cs, %ax #ax=cs=INITSEG
  56. mov %ax, %ds #ds=ax=INITSEG
  57. mov %ax, %es #es=ax=INITSEG
  58. # put stack at 0x9ff00.
  59. mov %ax, %ss #ss=ax=INITSEG=0x9000
  60. mov $0xFF00, %sp # arbitrary value >>512
  61. # load the setup-sectors directly after the bootblock.
  62. # Note that 'es' is already set up.
  63. load_setup:
  64. mov $0x0000, %dx # drive 0, head 0
  65. mov $0x0002, %cx # sector 2, track 0 #從sector2開始讀(sector2,3放得是setup部分的code)
  66. mov $0x0200, %bx # address = 512, in INITSEG
  67. .equ AX, 0x0200+SETUPLEN
  68. mov $AX, %ax # service 2, nr of sectors #ax=0x0204(AH=02h: Read Sectors From Drive AL=Sectors To Read Count)
  69. int $0x13 # read it
  70. jnc ok_load_setup # ok - continue #會把讀到的兩個sector放在es(INITSEG):bx(0x0200)上,即為SETUPSEG
  71. mov $0x0000, %dx
  72. mov $0x0000, %ax # reset the diskette
  73. int $0x13
  74. jmp load_setup
  75. ok_load_setup:
  76. # Get disk drive parameters, specifically nr of sectors/track
  77. mov $0x00, %dl
  78. mov $0x0800, %ax # AH=8 is get drive parameters
  79. int $0x13
  80. mov $0x00, %ch
  81. #seg cs
  82. mov %cx, %cs:sectors+0 # %cs means sectors is in %cs
  83. mov $INITSEG, %ax
  84. mov %ax, %es
  85. # Print some inane message
  86. mov $0x03, %ah # read cursor pos
  87. xor %bh, %bh
  88. int $0x10
  89. mov $24, %cx
  90. mov $0x0007, %bx # page 0, attribute 7 (normal)
  91. #lea msg1, %bp
  92. mov $msg1, %bp
  93. mov $0x1301, %ax # write string, move cursor
  94. int $0x10
  95. # ok, we've written the message, now
  96. # we want to load the system (at 0x10000)
  97. mov $SYSSEG, %ax
  98. mov %ax, %es # segment of 0x010000
  99. call read_it
  100. call kill_motor
  101. # After that we check which root-device to use. If the device is
  102. # defined (#= 0), nothing is done and the given device is used.
  103. # Otherwise, either /dev/PS0 (2,28) or /dev/at0 (2,8), depending
  104. # on the number of sectors that the BIOS reports currently.
  105. #seg cs
  106. mov %cs:root_dev+0, %ax
  107. cmp $0, %ax
  108. jne root_defined
  109. #seg cs
  110. mov %cs:sectors+0, %bx
  111. mov $0x0208, %ax # /dev/ps0 - 1.2Mb
  112. cmp $15, %bx
  113. je root_defined
  114. mov $0x021c, %ax # /dev/PS0 - 1.44Mb
  115. cmp $18, %bx
  116. je root_defined
  117. undef_root:
  118. jmp undef_root
  119. root_defined:
  120. #seg cs
  121. mov %ax, %cs:root_dev+0
  122. # after that (everyting loaded), we jump to
  123. # the setup-routine loaded directly after
  124. # the bootblock:
  125. ljmp $SETUPSEG, $0 #跳至SETUPSEG,執行setup code ,需看boot/setup.s
  126. # This routine loads the system at address 0x10000, making sure
  127. # no 64kB boundaries are crossed. We try to load it as fast as
  128. # possible, loading whole tracks whenever we can.
  129. #
  130. # in: es - starting address segment (normally 0x1000)
  131. #
  132. sread: .word 1+ SETUPLEN # sectors read of current track
  133. head: .word 0 # current head
  134. track: .word 0 # current track
  135. read_it:
  136. mov %es, %ax
  137. test $0x0fff, %ax
  138. die: jne die # es must be at 64kB boundary
  139. xor %bx, %bx # bx is starting address within segment
  140. rp_read:
  141. mov %es, %ax
  142. cmp $ENDSEG, %ax # have we loaded all yet?
  143. jb ok1_read
  144. ret
  145. ok1_read:
  146. #seg cs
  147. mov %cs:sectors+0, %ax
  148. sub sread, %ax
  149. mov %ax, %cx
  150. shl $9, %cx
  151. add %bx, %cx
  152. jnc ok2_read
  153. je ok2_read
  154. xor %ax, %ax
  155. sub %bx, %ax
  156. shr $9, %ax
  157. ok2_read:
  158. call read_track
  159. mov %ax, %cx
  160. add sread, %ax
  161. #seg cs
  162. cmp %cs:sectors+0, %ax
  163. jne ok3_read
  164. mov $1, %ax
  165. sub head, %ax
  166. jne ok4_read
  167. incw track
  168. ok4_read:
  169. mov %ax, head
  170. xor %ax, %ax
  171. ok3_read:
  172. mov %ax, sread
  173. shl $9, %cx
  174. add %cx, %bx
  175. jnc rp_read
  176. mov %es, %ax
  177. add $0x1000, %ax
  178. mov %ax, %es
  179. xor %bx, %bx
  180. jmp rp_read
  181. read_track:
  182. push %ax
  183. push %bx
  184. push %cx
  185. push %dx
  186. mov track, %dx
  187. mov sread, %cx
  188. inc %cx
  189. mov %dl, %ch
  190. mov head, %dx
  191. mov %dl, %dh
  192. mov $0, %dl
  193. and $0x0100, %dx
  194. mov $2, %ah
  195. int $0x13
  196. jc bad_rt
  197. pop %dx
  198. pop %cx
  199. pop %bx
  200. pop %ax
  201. ret
  202. bad_rt: mov $0, %ax
  203. mov $0, %dx
  204. int $0x13
  205. pop %dx
  206. pop %cx
  207. pop %bx
  208. pop %ax
  209. jmp read_track
  210. #/*
  211. # * This procedure turns off the floppy drive motor, so
  212. # * that we enter the kernel in a known state, and
  213. # * don't have to worry about it later.
  214. # */
  215. kill_motor:
  216. push %dx
  217. mov $0x3f2, %dx
  218. mov $0, %al
  219. outsb
  220. pop %dx
  221. ret
  222. sectors:
  223. .word 0
  224. msg1:
  225. .byte 13,10
  226. .ascii "Loading system ..."
  227. .byte 13,10,13,10
  228. .org 508
  229. root_dev:
  230. .word ROOT_DEV
  231. boot_flag:
  232. .word 0xAA55
  233. .text
  234. endtext:
  235. .data
  236. enddata:
  237. .bss
  238. endbss: