setup.S 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*
  2. * setup.S Copyright (C) 1991, 1992 Linus Torvalds
  3. *
  4. * setup.s is responsible for getting the system data from the BIOS,
  5. * and putting them into the appropriate places in system memory.
  6. * both setup.s and system has been loaded by the bootblock.
  7. *
  8. * This code asks the bios for memory/disk/other parameters, and
  9. * puts them in a "safe" place: 0x90000-0x901FF, ie where the
  10. * boot-block used to be. It is then up to the protected mode
  11. * system to read them from there before the area is overwritten
  12. * for buffer-blocks.
  13. *
  14. * Move PS/2 aux init code to psaux.c
  15. * (troyer@saifr00.cfsat.Honeywell.COM) 03Oct92
  16. *
  17. * some changes and additional features by Christoph Niemann,
  18. * March 1993/June 1994 (Christoph.Niemann@linux.org)
  19. *
  20. * add APM BIOS checking by Stephen Rothwell, May 1994
  21. * (sfr@canb.auug.org.au)
  22. *
  23. * High load stuff, initrd support and position independency
  24. * by Hans Lermen & Werner Almesberger, February 1996
  25. * <lermen@elserv.ffm.fgan.de>, <almesber@lrc.epfl.ch>
  26. *
  27. * Video handling moved to video.S by Martin Mares, March 1996
  28. * <mj@k332.feld.cvut.cz>
  29. *
  30. * Extended memory detection scheme retwiddled by orc@pell.chi.il.us (david
  31. * parsons) to avoid loadlin confusion, July 1997
  32. *
  33. * Transcribed from Intel (as86) -> AT&T (gas) by Chris Noe, May 1999.
  34. * <stiker@northlink.com>
  35. *
  36. * Fix to work around buggy BIOSes which don't use carry bit correctly
  37. * and/or report extended memory in CX/DX for e801h memory size detection
  38. * call. As a result the kernel got wrong figures. The int15/e801h docs
  39. * from Ralf Brown interrupt list seem to indicate AX/BX should be used
  40. * anyway. So to avoid breaking many machines (presumably there was a reason
  41. * to orginally use CX/DX instead of AX/BX), we do a kludge to see
  42. * if CX/DX have been changed in the e801 call and if so use AX/BX .
  43. * Michael Miller, April 2001 <michaelm@mjmm.org>
  44. *
  45. * Added long mode checking and SSE force. March 2003, Andi Kleen.
  46. */
  47. #include <linux/config.h>
  48. #include <asm/segment.h>
  49. #include <linux/version.h>
  50. #include <linux/compile.h>
  51. #include <asm/boot.h>
  52. #include <asm/e820.h>
  53. #include <asm/page.h>
  54. /* Signature words to ensure LILO loaded us right */
  55. #define SIG1 0xAA55
  56. #define SIG2 0x5A5A
  57. INITSEG = DEF_INITSEG # 0x9000, we move boot here, out of the way
  58. SYSSEG = DEF_SYSSEG # 0x1000, system loaded at 0x10000 (65536).
  59. SETUPSEG = DEF_SETUPSEG # 0x9020, this is the current segment
  60. # ... and the former contents of CS
  61. DELTA_INITSEG = SETUPSEG - INITSEG # 0x0020
  62. .code16
  63. .globl begtext, begdata, begbss, endtext, enddata, endbss
  64. .text
  65. begtext:
  66. .data
  67. begdata:
  68. .bss
  69. begbss:
  70. .text
  71. start:
  72. jmp trampoline
  73. # This is the setup header, and it must start at %cs:2 (old 0x9020:2)
  74. .ascii "HdrS" # header signature
  75. .word 0x0204 # header version number (>= 0x0105)
  76. # or else old loadlin-1.5 will fail)
  77. realmode_swtch: .word 0, 0 # default_switch, SETUPSEG
  78. start_sys_seg: .word SYSSEG
  79. .word kernel_version # pointing to kernel version string
  80. # above section of header is compatible
  81. # with loadlin-1.5 (header v1.5). Don't
  82. # change it.
  83. type_of_loader: .byte 0 # = 0, old one (LILO, Loadlin,
  84. # Bootlin, SYSLX, bootsect...)
  85. # See Documentation/i386/boot.txt for
  86. # assigned ids
  87. # flags, unused bits must be zero (RFU) bit within loadflags
  88. loadflags:
  89. LOADED_HIGH = 1 # If set, the kernel is loaded high
  90. CAN_USE_HEAP = 0x80 # If set, the loader also has set
  91. # heap_end_ptr to tell how much
  92. # space behind setup.S can be used for
  93. # heap purposes.
  94. # Only the loader knows what is free
  95. #ifndef __BIG_KERNEL__
  96. .byte 0
  97. #else
  98. .byte LOADED_HIGH
  99. #endif
  100. setup_move_size: .word 0x8000 # size to move, when setup is not
  101. # loaded at 0x90000. We will move setup
  102. # to 0x90000 then just before jumping
  103. # into the kernel. However, only the
  104. # loader knows how much data behind
  105. # us also needs to be loaded.
  106. code32_start: # here loaders can put a different
  107. # start address for 32-bit code.
  108. #ifndef __BIG_KERNEL__
  109. .long 0x1000 # 0x1000 = default for zImage
  110. #else
  111. .long 0x100000 # 0x100000 = default for big kernel
  112. #endif
  113. ramdisk_image: .long 0 # address of loaded ramdisk image
  114. # Here the loader puts the 32-bit
  115. # address where it loaded the image.
  116. # This only will be read by the kernel.
  117. ramdisk_size: .long 0 # its size in bytes
  118. bootsect_kludge:
  119. .long 0 # obsolete
  120. heap_end_ptr: .word modelist+1024 # (Header version 0x0201 or later)
  121. # space from here (exclusive) down to
  122. # end of setup code can be used by setup
  123. # for local heap purposes.
  124. pad1: .word 0
  125. cmd_line_ptr: .long 0 # (Header version 0x0202 or later)
  126. # If nonzero, a 32-bit pointer
  127. # to the kernel command line.
  128. # The command line should be
  129. # located between the start of
  130. # setup and the end of low
  131. # memory (0xa0000), or it may
  132. # get overwritten before it
  133. # gets read. If this field is
  134. # used, there is no longer
  135. # anything magical about the
  136. # 0x90000 segment; the setup
  137. # can be located anywhere in
  138. # low memory 0x10000 or higher.
  139. ramdisk_max: .long 0xffffffff
  140. trampoline: call start_of_setup
  141. .align 16
  142. # The offset at this point is 0x240
  143. .space (0xeff-0x240+1) # E820 & EDD space (ending at 0xeff)
  144. # End of setup header #####################################################
  145. start_of_setup:
  146. # Bootlin depends on this being done early
  147. movw $0x01500, %ax
  148. movb $0x81, %dl
  149. int $0x13
  150. #ifdef SAFE_RESET_DISK_CONTROLLER
  151. # Reset the disk controller.
  152. movw $0x0000, %ax
  153. movb $0x80, %dl
  154. int $0x13
  155. #endif
  156. # Set %ds = %cs, we know that SETUPSEG = %cs at this point
  157. movw %cs, %ax # aka SETUPSEG
  158. movw %ax, %ds
  159. # Check signature at end of setup
  160. cmpw $SIG1, setup_sig1
  161. jne bad_sig
  162. cmpw $SIG2, setup_sig2
  163. jne bad_sig
  164. jmp good_sig1
  165. # Routine to print asciiz string at ds:si
  166. prtstr:
  167. lodsb
  168. andb %al, %al
  169. jz fin
  170. call prtchr
  171. jmp prtstr
  172. fin: ret
  173. # Space printing
  174. prtsp2: call prtspc # Print double space
  175. prtspc: movb $0x20, %al # Print single space (note: fall-thru)
  176. prtchr:
  177. pushw %ax
  178. pushw %cx
  179. movw $0007,%bx
  180. movw $0x01, %cx
  181. movb $0x0e, %ah
  182. int $0x10
  183. popw %cx
  184. popw %ax
  185. ret
  186. beep: movb $0x07, %al
  187. jmp prtchr
  188. no_sig_mess: .string "No setup signature found ..."
  189. good_sig1:
  190. jmp good_sig
  191. # We now have to find the rest of the setup code/data
  192. bad_sig:
  193. movw %cs, %ax # SETUPSEG
  194. subw $DELTA_INITSEG, %ax # INITSEG
  195. movw %ax, %ds
  196. xorb %bh, %bh
  197. movb (497), %bl # get setup sect from bootsect
  198. subw $4, %bx # LILO loads 4 sectors of setup
  199. shlw $8, %bx # convert to words (1sect=2^8 words)
  200. movw %bx, %cx
  201. shrw $3, %bx # convert to segment
  202. addw $SYSSEG, %bx
  203. movw %bx, %cs:start_sys_seg
  204. # Move rest of setup code/data to here
  205. movw $2048, %di # four sectors loaded by LILO
  206. subw %si, %si
  207. movw %cs, %ax # aka SETUPSEG
  208. movw %ax, %es
  209. movw $SYSSEG, %ax
  210. movw %ax, %ds
  211. rep
  212. movsw
  213. movw %cs, %ax # aka SETUPSEG
  214. movw %ax, %ds
  215. cmpw $SIG1, setup_sig1
  216. jne no_sig
  217. cmpw $SIG2, setup_sig2
  218. jne no_sig
  219. jmp good_sig
  220. no_sig:
  221. lea no_sig_mess, %si
  222. call prtstr
  223. no_sig_loop:
  224. jmp no_sig_loop
  225. good_sig:
  226. movw %cs, %ax # aka SETUPSEG
  227. subw $DELTA_INITSEG, %ax # aka INITSEG
  228. movw %ax, %ds
  229. # Check if an old loader tries to load a big-kernel
  230. testb $LOADED_HIGH, %cs:loadflags # Do we have a big kernel?
  231. jz loader_ok # No, no danger for old loaders.
  232. cmpb $0, %cs:type_of_loader # Do we have a loader that
  233. # can deal with us?
  234. jnz loader_ok # Yes, continue.
  235. pushw %cs # No, we have an old loader,
  236. popw %ds # die.
  237. lea loader_panic_mess, %si
  238. call prtstr
  239. jmp no_sig_loop
  240. loader_panic_mess: .string "Wrong loader, giving up..."
  241. loader_ok:
  242. /* check for long mode. */
  243. /* we have to do this before the VESA setup, otherwise the user
  244. can't see the error message. */
  245. pushw %ds
  246. movw %cs,%ax
  247. movw %ax,%ds
  248. /* minimum CPUID flags for x86-64 */
  249. /* see http://www.x86-64.org/lists/discuss/msg02971.html */
  250. #define SSE_MASK ((1<<25)|(1<<26))
  251. #define REQUIRED_MASK1 ((1<<0)|(1<<3)|(1<<4)|(1<<5)|(1<<6)|(1<<8)|\
  252. (1<<13)|(1<<15)|(1<<24))
  253. #define REQUIRED_MASK2 (1<<29)
  254. pushfl /* standard way to check for cpuid */
  255. popl %eax
  256. movl %eax,%ebx
  257. xorl $0x200000,%eax
  258. pushl %eax
  259. popfl
  260. pushfl
  261. popl %eax
  262. cmpl %eax,%ebx
  263. jz no_longmode /* cpu has no cpuid */
  264. movl $0x0,%eax
  265. cpuid
  266. cmpl $0x1,%eax
  267. jb no_longmode /* no cpuid 1 */
  268. xor %di,%di
  269. cmpl $0x68747541,%ebx /* AuthenticAMD */
  270. jnz noamd
  271. cmpl $0x69746e65,%edx
  272. jnz noamd
  273. cmpl $0x444d4163,%ecx
  274. jnz noamd
  275. mov $1,%di /* cpu is from AMD */
  276. noamd:
  277. movl $0x1,%eax
  278. cpuid
  279. andl $REQUIRED_MASK1,%edx
  280. xorl $REQUIRED_MASK1,%edx
  281. jnz no_longmode
  282. movl $0x80000000,%eax
  283. cpuid
  284. cmpl $0x80000001,%eax
  285. jb no_longmode /* no extended cpuid */
  286. movl $0x80000001,%eax
  287. cpuid
  288. andl $REQUIRED_MASK2,%edx
  289. xorl $REQUIRED_MASK2,%edx
  290. jnz no_longmode
  291. sse_test:
  292. movl $1,%eax
  293. cpuid
  294. andl $SSE_MASK,%edx
  295. cmpl $SSE_MASK,%edx
  296. je sse_ok
  297. test %di,%di
  298. jz no_longmode /* only try to force SSE on AMD */
  299. movl $0xc0010015,%ecx /* HWCR */
  300. rdmsr
  301. btr $15,%eax /* enable SSE */
  302. wrmsr
  303. xor %di,%di /* don't loop */
  304. jmp sse_test /* try again */
  305. no_longmode:
  306. call beep
  307. lea long_mode_panic,%si
  308. call prtstr
  309. no_longmode_loop:
  310. jmp no_longmode_loop
  311. long_mode_panic:
  312. .string "Your CPU does not support long mode. Use a 32bit distribution."
  313. .byte 0
  314. sse_ok:
  315. popw %ds
  316. # tell BIOS we want to go to long mode
  317. movl $0xec00,%eax # declare target operating mode
  318. movl $2,%ebx # long mode
  319. int $0x15
  320. # Get memory size (extended mem, kB)
  321. xorl %eax, %eax
  322. movl %eax, (0x1e0)
  323. #ifndef STANDARD_MEMORY_BIOS_CALL
  324. movb %al, (E820NR)
  325. # Try three different memory detection schemes. First, try
  326. # e820h, which lets us assemble a memory map, then try e801h,
  327. # which returns a 32-bit memory size, and finally 88h, which
  328. # returns 0-64m
  329. # method E820H:
  330. # the memory map from hell. e820h returns memory classified into
  331. # a whole bunch of different types, and allows memory holes and
  332. # everything. We scan through this memory map and build a list
  333. # of the first 32 memory areas, which we return at [E820MAP].
  334. # This is documented at http://www.acpi.info/, in the ACPI 2.0 specification.
  335. #define SMAP 0x534d4150
  336. meme820:
  337. xorl %ebx, %ebx # continuation counter
  338. movw $E820MAP, %di # point into the whitelist
  339. # so we can have the bios
  340. # directly write into it.
  341. jmpe820:
  342. movl $0x0000e820, %eax # e820, upper word zeroed
  343. movl $SMAP, %edx # ascii 'SMAP'
  344. movl $20, %ecx # size of the e820rec
  345. pushw %ds # data record.
  346. popw %es
  347. int $0x15 # make the call
  348. jc bail820 # fall to e801 if it fails
  349. cmpl $SMAP, %eax # check the return is `SMAP'
  350. jne bail820 # fall to e801 if it fails
  351. # cmpl $1, 16(%di) # is this usable memory?
  352. # jne again820
  353. # If this is usable memory, we save it by simply advancing %di by
  354. # sizeof(e820rec).
  355. #
  356. good820:
  357. movb (E820NR), %al # up to 128 entries
  358. cmpb $E820MAX, %al
  359. jae bail820
  360. incb (E820NR)
  361. movw %di, %ax
  362. addw $20, %ax
  363. movw %ax, %di
  364. again820:
  365. cmpl $0, %ebx # check to see if
  366. jne jmpe820 # %ebx is set to EOF
  367. bail820:
  368. # method E801H:
  369. # memory size is in 1k chunksizes, to avoid confusing loadlin.
  370. # we store the 0xe801 memory size in a completely different place,
  371. # because it will most likely be longer than 16 bits.
  372. # (use 1e0 because that's what Larry Augustine uses in his
  373. # alternative new memory detection scheme, and it's sensible
  374. # to write everything into the same place.)
  375. meme801:
  376. stc # fix to work around buggy
  377. xorw %cx,%cx # BIOSes which don't clear/set
  378. xorw %dx,%dx # carry on pass/error of
  379. # e801h memory size call
  380. # or merely pass cx,dx though
  381. # without changing them.
  382. movw $0xe801, %ax
  383. int $0x15
  384. jc mem88
  385. cmpw $0x0, %cx # Kludge to handle BIOSes
  386. jne e801usecxdx # which report their extended
  387. cmpw $0x0, %dx # memory in AX/BX rather than
  388. jne e801usecxdx # CX/DX. The spec I have read
  389. movw %ax, %cx # seems to indicate AX/BX
  390. movw %bx, %dx # are more reasonable anyway...
  391. e801usecxdx:
  392. andl $0xffff, %edx # clear sign extend
  393. shll $6, %edx # and go from 64k to 1k chunks
  394. movl %edx, (0x1e0) # store extended memory size
  395. andl $0xffff, %ecx # clear sign extend
  396. addl %ecx, (0x1e0) # and add lower memory into
  397. # total size.
  398. # Ye Olde Traditional Methode. Returns the memory size (up to 16mb or
  399. # 64mb, depending on the bios) in ax.
  400. mem88:
  401. #endif
  402. movb $0x88, %ah
  403. int $0x15
  404. movw %ax, (2)
  405. # Set the keyboard repeat rate to the max
  406. movw $0x0305, %ax
  407. xorw %bx, %bx
  408. int $0x16
  409. # Check for video adapter and its parameters and allow the
  410. # user to browse video modes.
  411. call video # NOTE: we need %ds pointing
  412. # to bootsector
  413. # Get hd0 data...
  414. xorw %ax, %ax
  415. movw %ax, %ds
  416. ldsw (4 * 0x41), %si
  417. movw %cs, %ax # aka SETUPSEG
  418. subw $DELTA_INITSEG, %ax # aka INITSEG
  419. pushw %ax
  420. movw %ax, %es
  421. movw $0x0080, %di
  422. movw $0x10, %cx
  423. pushw %cx
  424. cld
  425. rep
  426. movsb
  427. # Get hd1 data...
  428. xorw %ax, %ax
  429. movw %ax, %ds
  430. ldsw (4 * 0x46), %si
  431. popw %cx
  432. popw %es
  433. movw $0x0090, %di
  434. rep
  435. movsb
  436. # Check that there IS a hd1 :-)
  437. movw $0x01500, %ax
  438. movb $0x81, %dl
  439. int $0x13
  440. jc no_disk1
  441. cmpb $3, %ah
  442. je is_disk1
  443. no_disk1:
  444. movw %cs, %ax # aka SETUPSEG
  445. subw $DELTA_INITSEG, %ax # aka INITSEG
  446. movw %ax, %es
  447. movw $0x0090, %di
  448. movw $0x10, %cx
  449. xorw %ax, %ax
  450. cld
  451. rep
  452. stosb
  453. is_disk1:
  454. # Check for PS/2 pointing device
  455. movw %cs, %ax # aka SETUPSEG
  456. subw $DELTA_INITSEG, %ax # aka INITSEG
  457. movw %ax, %ds
  458. movw $0, (0x1ff) # default is no pointing device
  459. int $0x11 # int 0x11: equipment list
  460. testb $0x04, %al # check if mouse installed
  461. jz no_psmouse
  462. movw $0xAA, (0x1ff) # device present
  463. no_psmouse:
  464. #include "../../i386/boot/edd.S"
  465. # Now we want to move to protected mode ...
  466. cmpw $0, %cs:realmode_swtch
  467. jz rmodeswtch_normal
  468. lcall *%cs:realmode_swtch
  469. jmp rmodeswtch_end
  470. rmodeswtch_normal:
  471. pushw %cs
  472. call default_switch
  473. rmodeswtch_end:
  474. # we get the code32 start address and modify the below 'jmpi'
  475. # (loader may have changed it)
  476. movl %cs:code32_start, %eax
  477. movl %eax, %cs:code32
  478. # Now we move the system to its rightful place ... but we check if we have a
  479. # big-kernel. In that case we *must* not move it ...
  480. testb $LOADED_HIGH, %cs:loadflags
  481. jz do_move0 # .. then we have a normal low
  482. # loaded zImage
  483. # .. or else we have a high
  484. # loaded bzImage
  485. jmp end_move # ... and we skip moving
  486. do_move0:
  487. movw $0x100, %ax # start of destination segment
  488. movw %cs, %bp # aka SETUPSEG
  489. subw $DELTA_INITSEG, %bp # aka INITSEG
  490. movw %cs:start_sys_seg, %bx # start of source segment
  491. cld
  492. do_move:
  493. movw %ax, %es # destination segment
  494. incb %ah # instead of add ax,#0x100
  495. movw %bx, %ds # source segment
  496. addw $0x100, %bx
  497. subw %di, %di
  498. subw %si, %si
  499. movw $0x800, %cx
  500. rep
  501. movsw
  502. cmpw %bp, %bx # assume start_sys_seg > 0x200,
  503. # so we will perhaps read one
  504. # page more than needed, but
  505. # never overwrite INITSEG
  506. # because destination is a
  507. # minimum one page below source
  508. jb do_move
  509. end_move:
  510. # then we load the segment descriptors
  511. movw %cs, %ax # aka SETUPSEG
  512. movw %ax, %ds
  513. # Check whether we need to be downward compatible with version <=201
  514. cmpl $0, cmd_line_ptr
  515. jne end_move_self # loader uses version >=202 features
  516. cmpb $0x20, type_of_loader
  517. je end_move_self # bootsect loader, we know of it
  518. # Boot loader doesnt support boot protocol version 2.02.
  519. # If we have our code not at 0x90000, we need to move it there now.
  520. # We also then need to move the params behind it (commandline)
  521. # Because we would overwrite the code on the current IP, we move
  522. # it in two steps, jumping high after the first one.
  523. movw %cs, %ax
  524. cmpw $SETUPSEG, %ax
  525. je end_move_self
  526. cli # make sure we really have
  527. # interrupts disabled !
  528. # because after this the stack
  529. # should not be used
  530. subw $DELTA_INITSEG, %ax # aka INITSEG
  531. movw %ss, %dx
  532. cmpw %ax, %dx
  533. jb move_self_1
  534. addw $INITSEG, %dx
  535. subw %ax, %dx # this will go into %ss after
  536. # the move
  537. move_self_1:
  538. movw %ax, %ds
  539. movw $INITSEG, %ax # real INITSEG
  540. movw %ax, %es
  541. movw %cs:setup_move_size, %cx
  542. std # we have to move up, so we use
  543. # direction down because the
  544. # areas may overlap
  545. movw %cx, %di
  546. decw %di
  547. movw %di, %si
  548. subw $move_self_here+0x200, %cx
  549. rep
  550. movsb
  551. ljmp $SETUPSEG, $move_self_here
  552. move_self_here:
  553. movw $move_self_here+0x200, %cx
  554. rep
  555. movsb
  556. movw $SETUPSEG, %ax
  557. movw %ax, %ds
  558. movw %dx, %ss
  559. end_move_self: # now we are at the right place
  560. lidt idt_48 # load idt with 0,0
  561. xorl %eax, %eax # Compute gdt_base
  562. movw %ds, %ax # (Convert %ds:gdt to a linear ptr)
  563. shll $4, %eax
  564. addl $gdt, %eax
  565. movl %eax, (gdt_48+2)
  566. lgdt gdt_48 # load gdt with whatever is
  567. # appropriate
  568. # that was painless, now we enable a20
  569. call empty_8042
  570. movb $0xD1, %al # command write
  571. outb %al, $0x64
  572. call empty_8042
  573. movb $0xDF, %al # A20 on
  574. outb %al, $0x60
  575. call empty_8042
  576. #
  577. # You must preserve the other bits here. Otherwise embarrasing things
  578. # like laptops powering off on boot happen. Corrected version by Kira
  579. # Brown from Linux 2.2
  580. #
  581. inb $0x92, %al #
  582. orb $02, %al # "fast A20" version
  583. outb %al, $0x92 # some chips have only this
  584. # wait until a20 really *is* enabled; it can take a fair amount of
  585. # time on certain systems; Toshiba Tecras are known to have this
  586. # problem. The memory location used here (0x200) is the int 0x80
  587. # vector, which should be safe to use.
  588. xorw %ax, %ax # segment 0x0000
  589. movw %ax, %fs
  590. decw %ax # segment 0xffff (HMA)
  591. movw %ax, %gs
  592. a20_wait:
  593. incw %ax # unused memory location <0xfff0
  594. movw %ax, %fs:(0x200) # we use the "int 0x80" vector
  595. cmpw %gs:(0x210), %ax # and its corresponding HMA addr
  596. je a20_wait # loop until no longer aliased
  597. # make sure any possible coprocessor is properly reset..
  598. xorw %ax, %ax
  599. outb %al, $0xf0
  600. call delay
  601. outb %al, $0xf1
  602. call delay
  603. # well, that went ok, I hope. Now we mask all interrupts - the rest
  604. # is done in init_IRQ().
  605. movb $0xFF, %al # mask all interrupts for now
  606. outb %al, $0xA1
  607. call delay
  608. movb $0xFB, %al # mask all irq's but irq2 which
  609. outb %al, $0x21 # is cascaded
  610. # Well, that certainly wasn't fun :-(. Hopefully it works, and we don't
  611. # need no steenking BIOS anyway (except for the initial loading :-).
  612. # The BIOS-routine wants lots of unnecessary data, and it's less
  613. # "interesting" anyway. This is how REAL programmers do it.
  614. #
  615. # Well, now's the time to actually move into protected mode. To make
  616. # things as simple as possible, we do no register set-up or anything,
  617. # we let the gnu-compiled 32-bit programs do that. We just jump to
  618. # absolute address 0x1000 (or the loader supplied one),
  619. # in 32-bit protected mode.
  620. #
  621. # Note that the short jump isn't strictly needed, although there are
  622. # reasons why it might be a good idea. It won't hurt in any case.
  623. movw $1, %ax # protected mode (PE) bit
  624. lmsw %ax # This is it!
  625. jmp flush_instr
  626. flush_instr:
  627. xorw %bx, %bx # Flag to indicate a boot
  628. xorl %esi, %esi # Pointer to real-mode code
  629. movw %cs, %si
  630. subw $DELTA_INITSEG, %si
  631. shll $4, %esi # Convert to 32-bit pointer
  632. # NOTE: For high loaded big kernels we need a
  633. # jmpi 0x100000,__KERNEL_CS
  634. #
  635. # but we yet haven't reloaded the CS register, so the default size
  636. # of the target offset still is 16 bit.
  637. # However, using an operand prefix (0x66), the CPU will properly
  638. # take our 48 bit far pointer. (INTeL 80386 Programmer's Reference
  639. # Manual, Mixing 16-bit and 32-bit code, page 16-6)
  640. .byte 0x66, 0xea # prefix + jmpi-opcode
  641. code32: .long 0x1000 # will be set to 0x100000
  642. # for big kernels
  643. .word __KERNEL_CS
  644. # Here's a bunch of information about your current kernel..
  645. kernel_version: .ascii UTS_RELEASE
  646. .ascii " ("
  647. .ascii LINUX_COMPILE_BY
  648. .ascii "@"
  649. .ascii LINUX_COMPILE_HOST
  650. .ascii ") "
  651. .ascii UTS_VERSION
  652. .byte 0
  653. # This is the default real mode switch routine.
  654. # to be called just before protected mode transition
  655. default_switch:
  656. cli # no interrupts allowed !
  657. movb $0x80, %al # disable NMI for bootup
  658. # sequence
  659. outb %al, $0x70
  660. lret
  661. # This routine checks that the keyboard command queue is empty
  662. # (after emptying the output buffers)
  663. #
  664. # Some machines have delusions that the keyboard buffer is always full
  665. # with no keyboard attached...
  666. #
  667. # If there is no keyboard controller, we will usually get 0xff
  668. # to all the reads. With each IO taking a microsecond and
  669. # a timeout of 100,000 iterations, this can take about half a
  670. # second ("delay" == outb to port 0x80). That should be ok,
  671. # and should also be plenty of time for a real keyboard controller
  672. # to empty.
  673. #
  674. empty_8042:
  675. pushl %ecx
  676. movl $100000, %ecx
  677. empty_8042_loop:
  678. decl %ecx
  679. jz empty_8042_end_loop
  680. call delay
  681. inb $0x64, %al # 8042 status port
  682. testb $1, %al # output buffer?
  683. jz no_output
  684. call delay
  685. inb $0x60, %al # read it
  686. jmp empty_8042_loop
  687. no_output:
  688. testb $2, %al # is input buffer full?
  689. jnz empty_8042_loop # yes - loop
  690. empty_8042_end_loop:
  691. popl %ecx
  692. ret
  693. # Read the cmos clock. Return the seconds in al
  694. gettime:
  695. pushw %cx
  696. movb $0x02, %ah
  697. int $0x1a
  698. movb %dh, %al # %dh contains the seconds
  699. andb $0x0f, %al
  700. movb %dh, %ah
  701. movb $0x04, %cl
  702. shrb %cl, %ah
  703. aad
  704. popw %cx
  705. ret
  706. # Delay is needed after doing I/O
  707. delay:
  708. outb %al,$0x80
  709. ret
  710. # Descriptor tables
  711. gdt:
  712. .word 0, 0, 0, 0 # dummy
  713. .word 0, 0, 0, 0 # unused
  714. .word 0xFFFF # 4Gb - (0x100000*0x1000 = 4Gb)
  715. .word 0 # base address = 0
  716. .word 0x9A00 # code read/exec
  717. .word 0x00CF # granularity = 4096, 386
  718. # (+5th nibble of limit)
  719. .word 0xFFFF # 4Gb - (0x100000*0x1000 = 4Gb)
  720. .word 0 # base address = 0
  721. .word 0x9200 # data read/write
  722. .word 0x00CF # granularity = 4096, 386
  723. # (+5th nibble of limit)
  724. idt_48:
  725. .word 0 # idt limit = 0
  726. .word 0, 0 # idt base = 0L
  727. gdt_48:
  728. .word 0x8000 # gdt limit=2048,
  729. # 256 GDT entries
  730. .word 0, 0 # gdt base (filled in later)
  731. # Include video setup & detection code
  732. #include "video.S"
  733. # Setup signature -- must be last
  734. setup_sig1: .word SIG1
  735. setup_sig2: .word SIG2
  736. # After this point, there is some free space which is used by the video mode
  737. # handling code to store the temporary mode table (not used by the kernel).
  738. modelist:
  739. .text
  740. endtext:
  741. .data
  742. enddata:
  743. .bss
  744. endbss: