2 * setup.S Copyright (C) 1991, 1992 Linus Torvalds
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.
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
14 * Move PS/2 aux init code to psaux.c
15 * (troyer@saifr00.cfsat.Honeywell.COM) 03Oct92
17 * some changes and additional features by Christoph Niemann,
18 * March 1993/June 1994 (Christoph.Niemann@linux.org)
20 * add APM BIOS checking by Stephen Rothwell, May 1994
21 * (sfr@canb.auug.org.au)
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>
27 * Video handling moved to video.S by Martin Mares, March 1996
28 * <mj@k332.feld.cvut.cz>
30 * Extended memory detection scheme retwiddled by orc@pell.chi.il.us (david
31 * parsons) to avoid loadlin confusion, July 1997
33 * Transcribed from Intel (as86) -> AT&T (gas) by Chris Noe, May 1999.
34 * <stiker@northlink.com>
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>
45 * Added long mode checking and SSE force. March 2003, Andi Kleen.
48 #include <asm/segment.h>
49 #include <linux/utsrelease.h>
50 #include <linux/compile.h>
54 #include <asm/setup.h>
56 /* Signature words to ensure LILO loaded us right */
60 INITSEG = DEF_INITSEG # 0x9000, we move boot here, out of the way
61 SYSSEG = DEF_SYSSEG # 0x1000, system loaded at 0x10000 (65536).
62 SETUPSEG = DEF_SETUPSEG # 0x9020, this is the current segment
63 # ... and the former contents of CS
65 DELTA_INITSEG = SETUPSEG - INITSEG # 0x0020
68 .globl begtext, begdata, begbss, endtext, enddata, endbss
81 # This is the setup header, and it must start at %cs:2 (old 0x9020:2)
83 .ascii "HdrS" # header signature
84 .word 0x0206 # header version number (>= 0x0105)
85 # or else old loadlin-1.5 will fail)
86 realmode_swtch: .word 0, 0 # default_switch, SETUPSEG
87 start_sys_seg: .word SYSSEG
88 .word kernel_version # pointing to kernel version string
89 # above section of header is compatible
90 # with loadlin-1.5 (header v1.5). Don't
93 type_of_loader: .byte 0 # = 0, old one (LILO, Loadlin,
94 # Bootlin, SYSLX, bootsect...)
95 # See Documentation/i386/boot.txt for
98 # flags, unused bits must be zero (RFU) bit within loadflags
100 LOADED_HIGH = 1 # If set, the kernel is loaded high
101 CAN_USE_HEAP = 0x80 # If set, the loader also has set
102 # heap_end_ptr to tell how much
103 # space behind setup.S can be used for
105 # Only the loader knows what is free
106 #ifndef __BIG_KERNEL__
112 setup_move_size: .word 0x8000 # size to move, when setup is not
113 # loaded at 0x90000. We will move setup
114 # to 0x90000 then just before jumping
115 # into the kernel. However, only the
116 # loader knows how much data behind
117 # us also needs to be loaded.
119 code32_start: # here loaders can put a different
120 # start address for 32-bit code.
121 #ifndef __BIG_KERNEL__
122 .long 0x1000 # 0x1000 = default for zImage
124 .long 0x100000 # 0x100000 = default for big kernel
127 ramdisk_image: .long 0 # address of loaded ramdisk image
128 # Here the loader puts the 32-bit
129 # address where it loaded the image.
130 # This only will be read by the kernel.
132 ramdisk_size: .long 0 # its size in bytes
137 heap_end_ptr: .word modelist+1024 # (Header version 0x0201 or later)
138 # space from here (exclusive) down to
139 # end of setup code can be used by setup
140 # for local heap purposes.
143 cmd_line_ptr: .long 0 # (Header version 0x0202 or later)
144 # If nonzero, a 32-bit pointer
145 # to the kernel command line.
146 # The command line should be
147 # located between the start of
148 # setup and the end of low
149 # memory (0xa0000), or it may
150 # get overwritten before it
151 # gets read. If this field is
152 # used, there is no longer
153 # anything magical about the
154 # 0x90000 segment; the setup
155 # can be located anywhere in
156 # low memory 0x10000 or higher.
158 ramdisk_max: .long 0xffffffff
159 kernel_alignment: .long 0x200000 # physical addr alignment required for
160 # protected mode relocatable kernel
161 #ifdef CONFIG_RELOCATABLE
162 relocatable_kernel: .byte 1
164 relocatable_kernel: .byte 0
169 cmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line,
170 #added with boot protocol
173 trampoline: call start_of_setup
175 # The offset at this point is 0x240
176 .space (0xeff-0x240+1) # E820 & EDD space (ending at 0xeff)
177 # End of setup header #####################################################
180 # Bootlin depends on this being done early
185 #ifdef SAFE_RESET_DISK_CONTROLLER
186 # Reset the disk controller.
192 # Set %ds = %cs, we know that SETUPSEG = %cs at this point
193 movw %cs, %ax # aka SETUPSEG
195 # Check signature at end of setup
196 cmpw $SIG1, setup_sig1
199 cmpw $SIG2, setup_sig2
204 # Routine to print asciiz string at ds:si
216 prtsp2: call prtspc # Print double space
217 prtspc: movb $0x20, %al # Print single space (note: fall-thru)
230 beep: movb $0x07, %al
233 no_sig_mess: .string "No setup signature found ..."
238 # We now have to find the rest of the setup code/data
240 movw %cs, %ax # SETUPSEG
241 subw $DELTA_INITSEG, %ax # INITSEG
244 movb (497), %bl # get setup sect from bootsect
245 subw $4, %bx # LILO loads 4 sectors of setup
246 shlw $8, %bx # convert to words (1sect=2^8 words)
248 shrw $3, %bx # convert to segment
250 movw %bx, %cs:start_sys_seg
251 # Move rest of setup code/data to here
252 movw $2048, %di # four sectors loaded by LILO
254 movw %cs, %ax # aka SETUPSEG
260 movw %cs, %ax # aka SETUPSEG
262 cmpw $SIG1, setup_sig1
265 cmpw $SIG2, setup_sig2
278 movw %cs, %ax # aka SETUPSEG
279 subw $DELTA_INITSEG, %ax # aka INITSEG
281 # Check if an old loader tries to load a big-kernel
282 testb $LOADED_HIGH, %cs:loadflags # Do we have a big kernel?
283 jz loader_ok # No, no danger for old loaders.
285 cmpb $0, %cs:type_of_loader # Do we have a loader that
287 jnz loader_ok # Yes, continue.
289 pushw %cs # No, we have an old loader,
291 lea loader_panic_mess, %si
296 loader_panic_mess: .string "Wrong loader, giving up..."
299 /* check for long mode. */
300 /* we have to do this before the VESA setup, otherwise the user
301 can't see the error message. */
313 lea long_mode_panic,%si
318 .string "Your CPU does not support long mode. Use a 32bit distribution."
321 #include "../kernel/verify_cpu.S"
325 # tell BIOS we want to go to long mode
326 movl $0xec00,%eax # declare target operating mode
327 movl $2,%ebx # long mode
330 # Get memory size (extended mem, kB)
334 #ifndef STANDARD_MEMORY_BIOS_CALL
336 # Try three different memory detection schemes. First, try
337 # e820h, which lets us assemble a memory map, then try e801h,
338 # which returns a 32-bit memory size, and finally 88h, which
342 # the memory map from hell. e820h returns memory classified into
343 # a whole bunch of different types, and allows memory holes and
344 # everything. We scan through this memory map and build a list
345 # of the first 32 memory areas, which we return at [E820MAP].
346 # This is documented at http://www.acpi.info/, in the ACPI 2.0 specification.
348 #define SMAP 0x534d4150
351 xorl %ebx, %ebx # continuation counter
352 movw $E820MAP, %di # point into the whitelist
353 # so we can have the bios
354 # directly write into it.
357 movl $0x0000e820, %eax # e820, upper word zeroed
358 movl $SMAP, %edx # ascii 'SMAP'
359 movl $20, %ecx # size of the e820rec
360 pushw %ds # data record.
362 int $0x15 # make the call
363 jc bail820 # fall to e801 if it fails
365 cmpl $SMAP, %eax # check the return is `SMAP'
366 jne bail820 # fall to e801 if it fails
368 # cmpl $1, 16(%di) # is this usable memory?
371 # If this is usable memory, we save it by simply advancing %di by
375 movb (E820NR), %al # up to 128 entries
384 cmpl $0, %ebx # check to see if
385 jne jmpe820 # %ebx is set to EOF
390 # memory size is in 1k chunksizes, to avoid confusing loadlin.
391 # we store the 0xe801 memory size in a completely different place,
392 # because it will most likely be longer than 16 bits.
393 # (use 1e0 because that's what Larry Augustine uses in his
394 # alternative new memory detection scheme, and it's sensible
395 # to write everything into the same place.)
398 stc # fix to work around buggy
399 xorw %cx,%cx # BIOSes which don't clear/set
400 xorw %dx,%dx # carry on pass/error of
401 # e801h memory size call
402 # or merely pass cx,dx though
403 # without changing them.
408 cmpw $0x0, %cx # Kludge to handle BIOSes
409 jne e801usecxdx # which report their extended
410 cmpw $0x0, %dx # memory in AX/BX rather than
411 jne e801usecxdx # CX/DX. The spec I have read
412 movw %ax, %cx # seems to indicate AX/BX
413 movw %bx, %dx # are more reasonable anyway...
416 andl $0xffff, %edx # clear sign extend
417 shll $6, %edx # and go from 64k to 1k chunks
418 movl %edx, (0x1e0) # store extended memory size
419 andl $0xffff, %ecx # clear sign extend
420 addl %ecx, (0x1e0) # and add lower memory into
423 # Ye Olde Traditional Methode. Returns the memory size (up to 16mb or
424 # 64mb, depending on the bios) in ax.
432 # Set the keyboard repeat rate to the max
437 # Check for video adapter and its parameters and allow the
438 # user to browse video modes.
439 call video # NOTE: we need %ds pointing
446 movw %cs, %ax # aka SETUPSEG
447 subw $DELTA_INITSEG, %ax # aka INITSEG
465 # Check that there IS a hd1 :-)
475 movw %cs, %ax # aka SETUPSEG
476 subw $DELTA_INITSEG, %ax # aka INITSEG
486 # Check for PS/2 pointing device
487 movw %cs, %ax # aka SETUPSEG
488 subw $DELTA_INITSEG, %ax # aka INITSEG
490 movb $0, (0x1ff) # default is no pointing device
491 int $0x11 # int 0x11: equipment list
492 testb $0x04, %al # check if mouse installed
495 movb $0xAA, (0x1ff) # device present
498 #include "../../i386/boot/edd.S"
500 # Now we want to move to protected mode ...
501 cmpw $0, %cs:realmode_swtch
504 lcall *%cs:realmode_swtch
513 # we get the code32 start address and modify the below 'jmpi'
514 # (loader may have changed it)
515 movl %cs:code32_start, %eax
516 movl %eax, %cs:code32
518 # Now we move the system to its rightful place ... but we check if we have a
519 # big-kernel. In that case we *must* not move it ...
520 testb $LOADED_HIGH, %cs:loadflags
521 jz do_move0 # .. then we have a normal low
523 # .. or else we have a high
525 jmp end_move # ... and we skip moving
528 movw $0x100, %ax # start of destination segment
529 movw %cs, %bp # aka SETUPSEG
530 subw $DELTA_INITSEG, %bp # aka INITSEG
531 movw %cs:start_sys_seg, %bx # start of source segment
534 movw %ax, %es # destination segment
535 incb %ah # instead of add ax,#0x100
536 movw %bx, %ds # source segment
543 cmpw %bp, %bx # assume start_sys_seg > 0x200,
544 # so we will perhaps read one
545 # page more than needed, but
546 # never overwrite INITSEG
547 # because destination is a
548 # minimum one page below source
552 # then we load the segment descriptors
553 movw %cs, %ax # aka SETUPSEG
556 # Check whether we need to be downward compatible with version <=201
557 cmpl $0, cmd_line_ptr
558 jne end_move_self # loader uses version >=202 features
559 cmpb $0x20, type_of_loader
560 je end_move_self # bootsect loader, we know of it
562 # Boot loader doesnt support boot protocol version 2.02.
563 # If we have our code not at 0x90000, we need to move it there now.
564 # We also then need to move the params behind it (commandline)
565 # Because we would overwrite the code on the current IP, we move
566 # it in two steps, jumping high after the first one.
571 cli # make sure we really have
572 # interrupts disabled !
573 # because after this the stack
575 subw $DELTA_INITSEG, %ax # aka INITSEG
581 subw %ax, %dx # this will go into %ss after
585 movw $INITSEG, %ax # real INITSEG
587 movw %cs:setup_move_size, %cx
588 std # we have to move up, so we use
589 # direction down because the
594 subw $move_self_here+0x200, %cx
597 ljmp $SETUPSEG, $move_self_here
600 movw $move_self_here+0x200, %cx
606 end_move_self: # now we are at the right place
607 lidt idt_48 # load idt with 0,0
608 xorl %eax, %eax # Compute gdt_base
609 movw %ds, %ax # (Convert %ds:gdt to a linear ptr)
612 movl %eax, (gdt_48+2)
613 lgdt gdt_48 # load gdt with whatever is
616 # that was painless, now we enable a20
619 movb $0xD1, %al # command write
623 movb $0xDF, %al # A20 on
628 # You must preserve the other bits here. Otherwise embarrasing things
629 # like laptops powering off on boot happen. Corrected version by Kira
630 # Brown from Linux 2.2
633 orb $02, %al # "fast A20" version
634 outb %al, $0x92 # some chips have only this
636 # wait until a20 really *is* enabled; it can take a fair amount of
637 # time on certain systems; Toshiba Tecras are known to have this
638 # problem. The memory location used here (0x200) is the int 0x80
639 # vector, which should be safe to use.
641 xorw %ax, %ax # segment 0x0000
643 decw %ax # segment 0xffff (HMA)
646 incw %ax # unused memory location <0xfff0
647 movw %ax, %fs:(0x200) # we use the "int 0x80" vector
648 cmpw %gs:(0x210), %ax # and its corresponding HMA addr
649 je a20_wait # loop until no longer aliased
651 # make sure any possible coprocessor is properly reset..
659 # well, that went ok, I hope. Now we mask all interrupts - the rest
660 # is done in init_IRQ().
661 movb $0xFF, %al # mask all interrupts for now
665 movb $0xFB, %al # mask all irq's but irq2 which
666 outb %al, $0x21 # is cascaded
668 # Well, that certainly wasn't fun :-(. Hopefully it works, and we don't
669 # need no steenking BIOS anyway (except for the initial loading :-).
670 # The BIOS-routine wants lots of unnecessary data, and it's less
671 # "interesting" anyway. This is how REAL programmers do it.
673 # Well, now's the time to actually move into protected mode. To make
674 # things as simple as possible, we do no register set-up or anything,
675 # we let the gnu-compiled 32-bit programs do that. We just jump to
676 # absolute address 0x1000 (or the loader supplied one),
677 # in 32-bit protected mode.
679 # Note that the short jump isn't strictly needed, although there are
680 # reasons why it might be a good idea. It won't hurt in any case.
681 movw $1, %ax # protected mode (PE) bit
682 lmsw %ax # This is it!
686 xorw %bx, %bx # Flag to indicate a boot
687 xorl %esi, %esi # Pointer to real-mode code
689 subw $DELTA_INITSEG, %si
690 shll $4, %esi # Convert to 32-bit pointer
691 # NOTE: For high loaded big kernels we need a
692 # jmpi 0x100000,__KERNEL_CS
694 # but we yet haven't reloaded the CS register, so the default size
695 # of the target offset still is 16 bit.
696 # However, using an operand prefix (0x66), the CPU will properly
697 # take our 48 bit far pointer. (INTeL 80386 Programmer's Reference
698 # Manual, Mixing 16-bit and 32-bit code, page 16-6)
700 .byte 0x66, 0xea # prefix + jmpi-opcode
701 code32: .long 0x1000 # will be set to 0x100000
705 # Here's a bunch of information about your current kernel..
706 kernel_version: .ascii UTS_RELEASE
708 .ascii LINUX_COMPILE_BY
710 .ascii LINUX_COMPILE_HOST
715 # This is the default real mode switch routine.
716 # to be called just before protected mode transition
718 cli # no interrupts allowed !
719 movb $0x80, %al # disable NMI for bootup
725 # This routine checks that the keyboard command queue is empty
726 # (after emptying the output buffers)
728 # Some machines have delusions that the keyboard buffer is always full
729 # with no keyboard attached...
731 # If there is no keyboard controller, we will usually get 0xff
732 # to all the reads. With each IO taking a microsecond and
733 # a timeout of 100,000 iterations, this can take about half a
734 # second ("delay" == outb to port 0x80). That should be ok,
735 # and should also be plenty of time for a real keyboard controller
745 jz empty_8042_end_loop
749 inb $0x64, %al # 8042 status port
750 testb $1, %al # output buffer?
754 inb $0x60, %al # read it
758 testb $2, %al # is input buffer full?
759 jnz empty_8042_loop # yes - loop
764 # Read the cmos clock. Return the seconds in al
769 movb %dh, %al # %dh contains the seconds
778 # Delay is needed after doing I/O
785 .word 0, 0, 0, 0 # dummy
787 .word 0, 0, 0, 0 # unused
789 .word 0xFFFF # 4Gb - (0x100000*0x1000 = 4Gb)
790 .word 0 # base address = 0
791 .word 0x9A00 # code read/exec
792 .word 0x00CF # granularity = 4096, 386
793 # (+5th nibble of limit)
795 .word 0xFFFF # 4Gb - (0x100000*0x1000 = 4Gb)
796 .word 0 # base address = 0
797 .word 0x9200 # data read/write
798 .word 0x00CF # granularity = 4096, 386
799 # (+5th nibble of limit)
802 .word 0 # idt limit = 0
803 .word 0, 0 # idt base = 0L
805 .word gdt_end-gdt-1 # gdt limit
806 .word 0, 0 # gdt base (filled in later)
808 # Include video setup & detection code
812 # Setup signature -- must be last
813 setup_sig1: .word SIG1
814 setup_sig2: .word SIG2
816 # After this point, there is some free space which is used by the video mode
817 # handling code to store the temporary mode table (not used by the kernel).