setup.S 22 KB

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