[PATCH] x86: PARAVIRT: Jeremy Fitzhardinge <jeremy@goop.org>
[safe/jmp/linux-2.6] / arch / i386 / boot / setup.S
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  * New A20 code ported from SYSLINUX by H. Peter Anvin. AMD Elan bugfixes
46  * by Robert Schwebel, December 2001 <robert@schwebel.de>
47  */
48
49 #include <asm/segment.h>
50 #include <linux/utsrelease.h>
51 #include <linux/compile.h>
52 #include <asm/boot.h>
53 #include <asm/e820.h>
54 #include <asm/page.h>
55 #include <asm/setup.h>
56         
57 /* Signature words to ensure LILO loaded us right */
58 #define SIG1    0xAA55
59 #define SIG2    0x5A5A
60
61 INITSEG  = DEF_INITSEG          # 0x9000, we move boot here, out of the way
62 SYSSEG   = DEF_SYSSEG           # 0x1000, system loaded at 0x10000 (65536).
63 SETUPSEG = DEF_SETUPSEG         # 0x9020, this is the current segment
64                                 # ... and the former contents of CS
65
66 DELTA_INITSEG = SETUPSEG - INITSEG      # 0x0020
67
68 .code16
69 .globl begtext, begdata, begbss, endtext, enddata, endbss
70
71 .text
72 begtext:
73 .data
74 begdata:
75 .bss
76 begbss:
77 .text
78
79 start:
80         jmp     trampoline
81
82 # This is the setup header, and it must start at %cs:2 (old 0x9020:2)
83
84                 .ascii  "HdrS"          # header signature
85                 .word   0x0206          # header version number (>= 0x0105)
86                                         # or else old loadlin-1.5 will fail)
87 realmode_swtch: .word   0, 0            # default_switch, SETUPSEG
88 start_sys_seg:  .word   SYSSEG
89                 .word   kernel_version  # pointing to kernel version string
90                                         # above section of header is compatible
91                                         # with loadlin-1.5 (header v1.5). Don't
92                                         # change it.
93
94 type_of_loader: .byte   0               # = 0, old one (LILO, Loadlin,
95                                         #      Bootlin, SYSLX, bootsect...)
96                                         # See Documentation/i386/boot.txt for
97                                         # assigned ids
98         
99 # flags, unused bits must be zero (RFU) bit within loadflags
100 loadflags:
101 LOADED_HIGH     = 1                     # If set, the kernel is loaded high
102 CAN_USE_HEAP    = 0x80                  # If set, the loader also has set
103                                         # heap_end_ptr to tell how much
104                                         # space behind setup.S can be used for
105                                         # heap purposes.
106                                         # Only the loader knows what is free
107 #ifndef __BIG_KERNEL__
108                 .byte   0
109 #else
110                 .byte   LOADED_HIGH
111 #endif
112
113 setup_move_size: .word  0x8000          # size to move, when setup is not
114                                         # loaded at 0x90000. We will move setup 
115                                         # to 0x90000 then just before jumping
116                                         # into the kernel. However, only the
117                                         # loader knows how much data behind
118                                         # us also needs to be loaded.
119
120 code32_start:                           # here loaders can put a different
121                                         # start address for 32-bit code.
122 #ifndef __BIG_KERNEL__
123                 .long   0x1000          #   0x1000 = default for zImage
124 #else
125                 .long   0x100000        # 0x100000 = default for big kernel
126 #endif
127
128 ramdisk_image:  .long   0               # address of loaded ramdisk image
129                                         # Here the loader puts the 32-bit
130                                         # address where it loaded the image.
131                                         # This only will be read by the kernel.
132
133 ramdisk_size:   .long   0               # its size in bytes
134
135 bootsect_kludge:
136                 .long   0               # obsolete
137
138 heap_end_ptr:   .word   modelist+1024   # (Header version 0x0201 or later)
139                                         # space from here (exclusive) down to
140                                         # end of setup code can be used by setup
141                                         # for local heap purposes.
142
143 pad1:           .word   0
144 cmd_line_ptr:   .long 0                 # (Header version 0x0202 or later)
145                                         # If nonzero, a 32-bit pointer
146                                         # to the kernel command line.
147                                         # The command line should be
148                                         # located between the start of
149                                         # setup and the end of low
150                                         # memory (0xa0000), or it may
151                                         # get overwritten before it
152                                         # gets read.  If this field is
153                                         # used, there is no longer
154                                         # anything magical about the
155                                         # 0x90000 segment; the setup
156                                         # can be located anywhere in
157                                         # low memory 0x10000 or higher.
158
159 ramdisk_max:    .long (-__PAGE_OFFSET-(512 << 20)-1) & 0x7fffffff
160                                         # (Header version 0x0203 or later)
161                                         # The highest safe address for
162                                         # the contents of an initrd
163
164 kernel_alignment:  .long CONFIG_PHYSICAL_ALIGN  #physical addr alignment
165                                                 #required for protected mode
166                                                 #kernel
167 #ifdef CONFIG_RELOCATABLE
168 relocatable_kernel:    .byte 1
169 #else
170 relocatable_kernel:    .byte 0
171 #endif
172 pad2:                   .byte 0
173 pad3:                   .word 0
174
175 cmdline_size:   .long   COMMAND_LINE_SIZE-1     #length of the command line,
176                                                 #added with boot protocol
177                                                 #version 2.06
178
179 trampoline:     call    start_of_setup
180                 .align 16
181                                         # The offset at this point is 0x240
182                 .space  (0xeff-0x240+1) # E820 & EDD space (ending at 0xeff)
183 # End of setup header #####################################################
184
185 start_of_setup:
186 # Bootlin depends on this being done early
187         movw    $0x01500, %ax
188         movb    $0x81, %dl
189         int     $0x13
190
191 #ifdef SAFE_RESET_DISK_CONTROLLER
192 # Reset the disk controller.
193         movw    $0x0000, %ax
194         movb    $0x80, %dl
195         int     $0x13
196 #endif
197
198 # Set %ds = %cs, we know that SETUPSEG = %cs at this point
199         movw    %cs, %ax                # aka SETUPSEG
200         movw    %ax, %ds
201 # Check signature at end of setup
202         cmpw    $SIG1, setup_sig1
203         jne     bad_sig
204
205         cmpw    $SIG2, setup_sig2
206         jne     bad_sig
207
208         jmp     good_sig1
209
210 # Routine to print asciiz string at ds:si
211 prtstr:
212         lodsb
213         andb    %al, %al
214         jz      fin
215
216         call    prtchr
217         jmp     prtstr
218
219 fin:    ret
220
221 # Space printing
222 prtsp2: call    prtspc          # Print double space
223 prtspc: movb    $0x20, %al      # Print single space (note: fall-thru)
224
225 # Part of above routine, this one just prints ascii al
226 prtchr: pushw   %ax
227         pushw   %cx
228         movw    $7,%bx
229         movw    $0x01, %cx
230         movb    $0x0e, %ah
231         int     $0x10
232         popw    %cx
233         popw    %ax
234         ret
235
236 beep:   movb    $0x07, %al
237         jmp     prtchr
238         
239 no_sig_mess: .string    "No setup signature found ..."
240
241 good_sig1:
242         jmp     good_sig
243
244 # We now have to find the rest of the setup code/data
245 bad_sig:
246         movw    %cs, %ax                        # SETUPSEG
247         subw    $DELTA_INITSEG, %ax             # INITSEG
248         movw    %ax, %ds
249         xorb    %bh, %bh
250         movb    (497), %bl                      # get setup sect from bootsect
251         subw    $4, %bx                         # LILO loads 4 sectors of setup
252         shlw    $8, %bx                         # convert to words (1sect=2^8 words)
253         movw    %bx, %cx
254         shrw    $3, %bx                         # convert to segment
255         addw    $SYSSEG, %bx
256         movw    %bx, %cs:start_sys_seg
257 # Move rest of setup code/data to here
258         movw    $2048, %di                      # four sectors loaded by LILO
259         subw    %si, %si
260         pushw   %cs
261         popw    %es
262         movw    $SYSSEG, %ax
263         movw    %ax, %ds
264         rep
265         movsw
266         movw    %cs, %ax                        # aka SETUPSEG
267         movw    %ax, %ds
268         cmpw    $SIG1, setup_sig1
269         jne     no_sig
270
271         cmpw    $SIG2, setup_sig2
272         jne     no_sig
273
274         jmp     good_sig
275
276 no_sig:
277         lea     no_sig_mess, %si
278         call    prtstr
279
280 no_sig_loop:
281         hlt
282         jmp     no_sig_loop
283
284 good_sig:
285         movw    %cs, %ax                        # aka SETUPSEG
286         subw    $DELTA_INITSEG, %ax             # aka INITSEG
287         movw    %ax, %ds
288 # Check if an old loader tries to load a big-kernel
289         testb   $LOADED_HIGH, %cs:loadflags     # Do we have a big kernel?
290         jz      loader_ok                       # No, no danger for old loaders.
291
292         cmpb    $0, %cs:type_of_loader          # Do we have a loader that
293                                                 # can deal with us?
294         jnz     loader_ok                       # Yes, continue.
295
296         pushw   %cs                             # No, we have an old loader,
297         popw    %ds                             # die. 
298         lea     loader_panic_mess, %si
299         call    prtstr
300
301         jmp     no_sig_loop
302
303 loader_panic_mess: .string "Wrong loader, giving up..."
304
305 loader_ok:
306 # Get memory size (extended mem, kB)
307
308         xorl    %eax, %eax
309         movl    %eax, (0x1e0)
310 #ifndef STANDARD_MEMORY_BIOS_CALL
311         movb    %al, (E820NR)
312 # Try three different memory detection schemes.  First, try
313 # e820h, which lets us assemble a memory map, then try e801h,
314 # which returns a 32-bit memory size, and finally 88h, which
315 # returns 0-64m
316
317 # method E820H:
318 # the memory map from hell.  e820h returns memory classified into
319 # a whole bunch of different types, and allows memory holes and
320 # everything.  We scan through this memory map and build a list
321 # of the first 32 memory areas, which we return at [E820MAP].
322 # This is documented at http://www.acpi.info/, in the ACPI 2.0 specification.
323
324 #define SMAP  0x534d4150
325
326 meme820:
327         xorl    %ebx, %ebx                      # continuation counter
328         movw    $E820MAP, %di                   # point into the whitelist
329                                                 # so we can have the bios
330                                                 # directly write into it.
331
332 jmpe820:
333         movl    $0x0000e820, %eax               # e820, upper word zeroed
334         movl    $SMAP, %edx                     # ascii 'SMAP'
335         movl    $20, %ecx                       # size of the e820rec
336         pushw   %ds                             # data record.
337         popw    %es
338         int     $0x15                           # make the call
339         jc      bail820                         # fall to e801 if it fails
340
341         cmpl    $SMAP, %eax                     # check the return is `SMAP'
342         jne     bail820                         # fall to e801 if it fails
343
344 #       cmpl    $1, 16(%di)                     # is this usable memory?
345 #       jne     again820
346
347         # If this is usable memory, we save it by simply advancing %di by
348         # sizeof(e820rec).
349         #
350 good820:
351         movb    (E820NR), %al                   # up to 128 entries
352         cmpb    $E820MAX, %al
353         jae     bail820
354
355         incb    (E820NR)
356         movw    %di, %ax
357         addw    $20, %ax
358         movw    %ax, %di
359 again820:
360         cmpl    $0, %ebx                        # check to see if
361         jne     jmpe820                         # %ebx is set to EOF
362 bail820:
363
364
365 # method E801H:
366 # memory size is in 1k chunksizes, to avoid confusing loadlin.
367 # we store the 0xe801 memory size in a completely different place,
368 # because it will most likely be longer than 16 bits.
369 # (use 1e0 because that's what Larry Augustine uses in his
370 # alternative new memory detection scheme, and it's sensible
371 # to write everything into the same place.)
372
373 meme801:
374         stc                                     # fix to work around buggy
375         xorw    %cx,%cx                         # BIOSes which don't clear/set
376         xorw    %dx,%dx                         # carry on pass/error of
377                                                 # e801h memory size call
378                                                 # or merely pass cx,dx though
379                                                 # without changing them.
380         movw    $0xe801, %ax
381         int     $0x15
382         jc      mem88
383
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
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
399 # Ye Olde Traditional Methode.  Returns the memory size (up to 16mb or
400 # 64mb, depending on the bios) in ax.
401 mem88:
402
403 #endif
404         movb    $0x88, %ah
405         int     $0x15
406         movw    %ax, (2)
407
408 # Set the keyboard repeat rate to the max
409         movw    $0x0305, %ax
410         xorw    %bx, %bx
411         int     $0x16
412
413 # Check for video adapter and its parameters and allow the
414 # user to browse video modes.
415         call    video                           # NOTE: we need %ds pointing
416                                                 # to bootsector
417
418 # Get hd0 data...
419         xorw    %ax, %ax
420         movw    %ax, %ds
421         ldsw    (4 * 0x41), %si
422         movw    %cs, %ax                        # aka SETUPSEG
423         subw    $DELTA_INITSEG, %ax             # aka INITSEG
424         pushw   %ax
425         movw    %ax, %es
426         movw    $0x0080, %di
427         movw    $0x10, %cx
428         pushw   %cx
429         cld
430         rep
431         movsb
432 # Get hd1 data...
433         xorw    %ax, %ax
434         movw    %ax, %ds
435         ldsw    (4 * 0x46), %si
436         popw    %cx
437         popw    %es
438         movw    $0x0090, %di
439         rep
440         movsb
441 # Check that there IS a hd1 :-)
442         movw    $0x01500, %ax
443         movb    $0x81, %dl
444         int     $0x13
445         jc      no_disk1
446         
447         cmpb    $3, %ah
448         je      is_disk1
449
450 no_disk1:
451         movw    %cs, %ax                        # aka SETUPSEG
452         subw    $DELTA_INITSEG, %ax             # aka INITSEG
453         movw    %ax, %es
454         movw    $0x0090, %di
455         movw    $0x10, %cx
456         xorw    %ax, %ax
457         cld
458         rep
459         stosb
460 is_disk1:
461 # check for Micro Channel (MCA) bus
462         movw    %cs, %ax                        # aka SETUPSEG
463         subw    $DELTA_INITSEG, %ax             # aka INITSEG
464         movw    %ax, %ds
465         xorw    %ax, %ax
466         movw    %ax, (0xa0)                     # set table length to 0
467         movb    $0xc0, %ah
468         stc
469         int     $0x15                           # moves feature table to es:bx
470         jc      no_mca
471
472         pushw   %ds
473         movw    %es, %ax
474         movw    %ax, %ds
475         movw    %cs, %ax                        # aka SETUPSEG
476         subw    $DELTA_INITSEG, %ax             # aka INITSEG
477         movw    %ax, %es
478         movw    %bx, %si
479         movw    $0xa0, %di
480         movw    (%si), %cx
481         addw    $2, %cx                         # table length is a short
482         cmpw    $0x10, %cx
483         jc      sysdesc_ok
484
485         movw    $0x10, %cx                      # we keep only first 16 bytes
486 sysdesc_ok:
487         rep
488         movsb
489         popw    %ds
490 no_mca:
491 #ifdef CONFIG_X86_VOYAGER
492         movb    $0xff, 0x40     # flag on config found
493         movb    $0xc0, %al
494         mov     $0xff, %ah
495         int     $0x15           # put voyager config info at es:di
496         jc      no_voyager
497         movw    $0x40, %si      # place voyager info in apm table
498         cld
499         movw    $7, %cx
500 voyager_rep:
501         movb    %es:(%di), %al
502         movb    %al,(%si)
503         incw    %di
504         incw    %si
505         decw    %cx
506         jnz     voyager_rep
507 no_voyager:     
508 #endif
509 # Check for PS/2 pointing device
510         movw    %cs, %ax                        # aka SETUPSEG
511         subw    $DELTA_INITSEG, %ax             # aka INITSEG
512         movw    %ax, %ds
513         movb    $0, (0x1ff)                     # default is no pointing device
514         int     $0x11                           # int 0x11: equipment list
515         testb   $0x04, %al                      # check if mouse installed
516         jz      no_psmouse
517
518         movb    $0xAA, (0x1ff)                  # device present
519 no_psmouse:
520
521 #if defined(CONFIG_X86_SPEEDSTEP_SMI) || defined(CONFIG_X86_SPEEDSTEP_SMI_MODULE)
522         movl    $0x0000E980, %eax               # IST Support 
523         movl    $0x47534943, %edx               # Request value
524         int     $0x15
525
526         movl    %eax, (96)
527         movl    %ebx, (100)
528         movl    %ecx, (104)
529         movl    %edx, (108)
530 #endif
531
532 #if defined(CONFIG_APM) || defined(CONFIG_APM_MODULE)
533 # Then check for an APM BIOS...
534                                                 # %ds points to the bootsector
535         movw    $0, 0x40                        # version = 0 means no APM BIOS
536         movw    $0x05300, %ax                   # APM BIOS installation check
537         xorw    %bx, %bx
538         int     $0x15
539         jc      done_apm_bios                   # Nope, no APM BIOS
540         
541         cmpw    $0x0504d, %bx                   # Check for "PM" signature
542         jne     done_apm_bios                   # No signature, no APM BIOS
543
544         andw    $0x02, %cx                      # Is 32 bit supported?
545         je      done_apm_bios                   # No 32-bit, no (good) APM BIOS
546
547         movw    $0x05304, %ax                   # Disconnect first just in case
548         xorw    %bx, %bx
549         int     $0x15                           # ignore return code
550         movw    $0x05303, %ax                   # 32 bit connect
551         xorl    %ebx, %ebx
552         xorw    %cx, %cx                        # paranoia :-)
553         xorw    %dx, %dx                        #   ...
554         xorl    %esi, %esi                      #   ...
555         xorw    %di, %di                        #   ...
556         int     $0x15
557         jc      no_32_apm_bios                  # Ack, error. 
558
559         movw    %ax,  (66)                      # BIOS code segment
560         movl    %ebx, (68)                      # BIOS entry point offset
561         movw    %cx,  (72)                      # BIOS 16 bit code segment
562         movw    %dx,  (74)                      # BIOS data segment
563         movl    %esi, (78)                      # BIOS code segment lengths
564         movw    %di,  (82)                      # BIOS data segment length
565 # Redo the installation check as the 32 bit connect
566 # modifies the flags returned on some BIOSs
567         movw    $0x05300, %ax                   # APM BIOS installation check
568         xorw    %bx, %bx
569         xorw    %cx, %cx                        # paranoia
570         int     $0x15
571         jc      apm_disconnect                  # error -> shouldn't happen
572
573         cmpw    $0x0504d, %bx                   # check for "PM" signature
574         jne     apm_disconnect                  # no sig -> shouldn't happen
575
576         movw    %ax, (64)                       # record the APM BIOS version
577         movw    %cx, (76)                       # and flags
578         jmp     done_apm_bios
579
580 apm_disconnect:                                 # Tidy up
581         movw    $0x05304, %ax                   # Disconnect
582         xorw    %bx, %bx
583         int     $0x15                           # ignore return code
584
585         jmp     done_apm_bios
586
587 no_32_apm_bios:
588         andw    $0xfffd, (76)                   # remove 32 bit support bit
589 done_apm_bios:
590 #endif
591
592 #include "edd.S"
593
594 # Now we want to move to protected mode ...
595         cmpw    $0, %cs:realmode_swtch
596         jz      rmodeswtch_normal
597
598         lcall   *%cs:realmode_swtch
599
600         jmp     rmodeswtch_end
601
602 rmodeswtch_normal:
603         pushw   %cs
604         call    default_switch
605
606 rmodeswtch_end:
607 # Now we move the system to its rightful place ... but we check if we have a
608 # big-kernel. In that case we *must* not move it ...
609         testb   $LOADED_HIGH, %cs:loadflags
610         jz      do_move0                        # .. then we have a normal low
611                                                 # loaded zImage
612                                                 # .. or else we have a high
613                                                 # loaded bzImage
614         jmp     end_move                        # ... and we skip moving
615
616 do_move0:
617         movw    $0x100, %ax                     # start of destination segment
618         movw    %cs, %bp                        # aka SETUPSEG
619         subw    $DELTA_INITSEG, %bp             # aka INITSEG
620         movw    %cs:start_sys_seg, %bx          # start of source segment
621         cld
622 do_move:
623         movw    %ax, %es                        # destination segment
624         incb    %ah                             # instead of add ax,#0x100
625         movw    %bx, %ds                        # source segment
626         addw    $0x100, %bx
627         subw    %di, %di
628         subw    %si, %si
629         movw    $0x800, %cx
630         rep
631         movsw
632         cmpw    %bp, %bx                        # assume start_sys_seg > 0x200,
633                                                 # so we will perhaps read one
634                                                 # page more than needed, but
635                                                 # never overwrite INITSEG
636                                                 # because destination is a
637                                                 # minimum one page below source
638         jb      do_move
639
640 end_move:
641 # then we load the segment descriptors
642         movw    %cs, %ax                        # aka SETUPSEG
643         movw    %ax, %ds
644                 
645 # Check whether we need to be downward compatible with version <=201
646         cmpl    $0, cmd_line_ptr
647         jne     end_move_self           # loader uses version >=202 features
648         cmpb    $0x20, type_of_loader
649         je      end_move_self           # bootsect loader, we know of it
650
651 # Boot loader doesnt support boot protocol version 2.02.
652 # If we have our code not at 0x90000, we need to move it there now.
653 # We also then need to move the params behind it (commandline)
654 # Because we would overwrite the code on the current IP, we move
655 # it in two steps, jumping high after the first one.
656         movw    %cs, %ax
657         cmpw    $SETUPSEG, %ax
658         je      end_move_self
659
660         cli                                     # make sure we really have
661                                                 # interrupts disabled !
662                                                 # because after this the stack
663                                                 # should not be used
664         subw    $DELTA_INITSEG, %ax             # aka INITSEG
665         movw    %ss, %dx
666         cmpw    %ax, %dx
667         jb      move_self_1
668
669         addw    $INITSEG, %dx
670         subw    %ax, %dx                        # this will go into %ss after
671                                                 # the move
672 move_self_1:
673         movw    %ax, %ds
674         movw    $INITSEG, %ax                   # real INITSEG
675         movw    %ax, %es
676         movw    %cs:setup_move_size, %cx
677         std                                     # we have to move up, so we use
678                                                 # direction down because the
679                                                 # areas may overlap
680         movw    %cx, %di
681         decw    %di
682         movw    %di, %si
683         subw    $move_self_here+0x200, %cx
684         rep
685         movsb
686         ljmp    $SETUPSEG, $move_self_here
687
688 move_self_here:
689         movw    $move_self_here+0x200, %cx
690         rep
691         movsb
692         movw    $SETUPSEG, %ax
693         movw    %ax, %ds
694         movw    %dx, %ss
695 end_move_self:                                  # now we are at the right place
696
697 #
698 # Enable A20.  This is at the very best an annoying procedure.
699 # A20 code ported from SYSLINUX 1.52-1.63 by H. Peter Anvin.
700 # AMD Elan bug fix by Robert Schwebel.
701 #
702
703 #if defined(CONFIG_X86_ELAN)
704         movb $0x02, %al                 # alternate A20 gate
705         outb %al, $0x92                 # this works on SC410/SC520
706 a20_elan_wait:
707         call a20_test
708         jz a20_elan_wait
709         jmp a20_done
710 #endif
711
712
713 A20_TEST_LOOPS          =  32           # Iterations per wait
714 A20_ENABLE_LOOPS        = 255           # Total loops to try            
715
716
717 #ifndef CONFIG_X86_VOYAGER
718 a20_try_loop:
719
720         # First, see if we are on a system with no A20 gate.
721 a20_none:
722         call    a20_test
723         jnz     a20_done
724
725         # Next, try the BIOS (INT 0x15, AX=0x2401)
726 a20_bios:
727         movw    $0x2401, %ax
728         pushfl                                  # Be paranoid about flags
729         int     $0x15
730         popfl
731
732         call    a20_test
733         jnz     a20_done
734
735         # Try enabling A20 through the keyboard controller
736 #endif /* CONFIG_X86_VOYAGER */
737 a20_kbc:
738         call    empty_8042
739
740 #ifndef CONFIG_X86_VOYAGER
741         call    a20_test                        # Just in case the BIOS worked
742         jnz     a20_done                        # but had a delayed reaction.
743 #endif
744
745         movb    $0xD1, %al                      # command write
746         outb    %al, $0x64
747         call    empty_8042
748
749         movb    $0xDF, %al                      # A20 on
750         outb    %al, $0x60
751         call    empty_8042
752
753 #ifndef CONFIG_X86_VOYAGER
754         # Wait until a20 really *is* enabled; it can take a fair amount of
755         # time on certain systems; Toshiba Tecras are known to have this
756         # problem.
757 a20_kbc_wait:
758         xorw    %cx, %cx
759 a20_kbc_wait_loop:
760         call    a20_test
761         jnz     a20_done
762         loop    a20_kbc_wait_loop
763
764         # Final attempt: use "configuration port A"
765 a20_fast:
766         inb     $0x92, %al                      # Configuration Port A
767         orb     $0x02, %al                      # "fast A20" version
768         andb    $0xFE, %al                      # don't accidentally reset
769         outb    %al, $0x92
770
771         # Wait for configuration port A to take effect
772 a20_fast_wait:
773         xorw    %cx, %cx
774 a20_fast_wait_loop:
775         call    a20_test
776         jnz     a20_done
777         loop    a20_fast_wait_loop
778
779         # A20 is still not responding.  Try frobbing it again.
780         # 
781         decb    (a20_tries)
782         jnz     a20_try_loop
783         
784         movw    $a20_err_msg, %si
785         call    prtstr
786
787 a20_die:
788         hlt
789         jmp     a20_die
790
791 a20_tries:
792         .byte   A20_ENABLE_LOOPS
793
794 a20_err_msg:
795         .ascii  "linux: fatal error: A20 gate not responding!"
796         .byte   13, 10, 0
797
798         # If we get here, all is good
799 a20_done:
800
801 #endif /* CONFIG_X86_VOYAGER */
802 # set up gdt and idt and 32bit start address
803         lidt    idt_48                          # load idt with 0,0
804         xorl    %eax, %eax                      # Compute gdt_base
805         movw    %ds, %ax                        # (Convert %ds:gdt to a linear ptr)
806         shll    $4, %eax
807         addl    %eax, code32
808         addl    $gdt, %eax
809         movl    %eax, (gdt_48+2)
810         lgdt    gdt_48                          # load gdt with whatever is
811                                                 # appropriate
812
813 # make sure any possible coprocessor is properly reset..
814         xorw    %ax, %ax
815         outb    %al, $0xf0
816         call    delay
817
818         outb    %al, $0xf1
819         call    delay
820
821 # well, that went ok, I hope. Now we mask all interrupts - the rest
822 # is done in init_IRQ().
823         movb    $0xFF, %al                      # mask all interrupts for now
824         outb    %al, $0xA1
825         call    delay
826         
827         movb    $0xFB, %al                      # mask all irq's but irq2 which
828         outb    %al, $0x21                      # is cascaded
829
830 # Well, that certainly wasn't fun :-(. Hopefully it works, and we don't
831 # need no steenking BIOS anyway (except for the initial loading :-).
832 # The BIOS-routine wants lots of unnecessary data, and it's less
833 # "interesting" anyway. This is how REAL programmers do it.
834 #
835 # Well, now's the time to actually move into protected mode. To make
836 # things as simple as possible, we do no register set-up or anything,
837 # we let the gnu-compiled 32-bit programs do that. We just jump to
838 # absolute address 0x1000 (or the loader supplied one),
839 # in 32-bit protected mode.
840 #
841 # Note that the short jump isn't strictly needed, although there are
842 # reasons why it might be a good idea. It won't hurt in any case.
843         movw    $1, %ax                         # protected mode (PE) bit
844         lmsw    %ax                             # This is it!
845         jmp     flush_instr
846
847 flush_instr:
848         xorw    %bx, %bx                        # Flag to indicate a boot
849         xorl    %esi, %esi                      # Pointer to real-mode code
850         movw    %cs, %si
851         subw    $DELTA_INITSEG, %si
852         shll    $4, %esi                        # Convert to 32-bit pointer
853
854 # jump to startup_32 in arch/i386/boot/compressed/head.S
855 #       
856 # NOTE: For high loaded big kernels we need a
857 #       jmpi    0x100000,__BOOT_CS
858 #
859 #       but we yet haven't reloaded the CS register, so the default size 
860 #       of the target offset still is 16 bit.
861 #       However, using an operand prefix (0x66), the CPU will properly
862 #       take our 48 bit far pointer. (INTeL 80386 Programmer's Reference
863 #       Manual, Mixing 16-bit and 32-bit code, page 16-6)
864
865         .byte 0x66, 0xea                        # prefix + jmpi-opcode
866 code32: .long   startup_32                      # will be set to %cs+startup_32
867         .word   __BOOT_CS
868 .code32
869 startup_32:
870         movl $(__BOOT_DS), %eax
871         movl %eax, %ds
872         movl %eax, %es
873         movl %eax, %fs
874         movl %eax, %gs
875         movl %eax, %ss
876
877         xorl %eax, %eax
878 1:      incl %eax                               # check that A20 really IS enabled
879         movl %eax, 0x00000000                   # loop forever if it isn't
880         cmpl %eax, 0x00100000
881         je 1b
882
883         # Jump to the 32bit entry point
884         jmpl *(code32_start - start + (DELTA_INITSEG << 4))(%esi)
885 .code16
886
887 # Here's a bunch of information about your current kernel..
888 kernel_version: .ascii  UTS_RELEASE
889                 .ascii  " ("
890                 .ascii  LINUX_COMPILE_BY
891                 .ascii  "@"
892                 .ascii  LINUX_COMPILE_HOST
893                 .ascii  ") "
894                 .ascii  UTS_VERSION
895                 .byte   0
896
897 # This is the default real mode switch routine.
898 # to be called just before protected mode transition
899 default_switch:
900         cli                                     # no interrupts allowed !
901         movb    $0x80, %al                      # disable NMI for bootup
902                                                 # sequence
903         outb    %al, $0x70
904         lret
905
906
907 #ifndef CONFIG_X86_VOYAGER
908 # This routine tests whether or not A20 is enabled.  If so, it
909 # exits with zf = 0.
910 #
911 # The memory address used, 0x200, is the int $0x80 vector, which
912 # should be safe.
913
914 A20_TEST_ADDR = 4*0x80
915
916 a20_test:
917         pushw   %cx
918         pushw   %ax
919         xorw    %cx, %cx
920         movw    %cx, %fs                        # Low memory
921         decw    %cx
922         movw    %cx, %gs                        # High memory area
923         movw    $A20_TEST_LOOPS, %cx
924         movw    %fs:(A20_TEST_ADDR), %ax
925         pushw   %ax
926 a20_test_wait:
927         incw    %ax
928         movw    %ax, %fs:(A20_TEST_ADDR)
929         call    delay                           # Serialize and make delay constant
930         cmpw    %gs:(A20_TEST_ADDR+0x10), %ax
931         loope   a20_test_wait
932
933         popw    %fs:(A20_TEST_ADDR)
934         popw    %ax
935         popw    %cx
936         ret     
937
938 #endif /* CONFIG_X86_VOYAGER */
939
940 # This routine checks that the keyboard command queue is empty
941 # (after emptying the output buffers)
942 #
943 # Some machines have delusions that the keyboard buffer is always full
944 # with no keyboard attached...
945 #
946 # If there is no keyboard controller, we will usually get 0xff
947 # to all the reads.  With each IO taking a microsecond and
948 # a timeout of 100,000 iterations, this can take about half a
949 # second ("delay" == outb to port 0x80). That should be ok,
950 # and should also be plenty of time for a real keyboard controller
951 # to empty.
952 #
953
954 empty_8042:
955         pushl   %ecx
956         movl    $100000, %ecx
957
958 empty_8042_loop:
959         decl    %ecx
960         jz      empty_8042_end_loop
961
962         call    delay
963
964         inb     $0x64, %al                      # 8042 status port
965         testb   $1, %al                         # output buffer?
966         jz      no_output
967
968         call    delay
969         inb     $0x60, %al                      # read it
970         jmp     empty_8042_loop
971
972 no_output:
973         testb   $2, %al                         # is input buffer full?
974         jnz     empty_8042_loop                 # yes - loop
975 empty_8042_end_loop:
976         popl    %ecx
977         ret
978
979 # Read the cmos clock. Return the seconds in al
980 gettime:
981         pushw   %cx
982         movb    $0x02, %ah
983         int     $0x1a
984         movb    %dh, %al                        # %dh contains the seconds
985         andb    $0x0f, %al
986         movb    %dh, %ah
987         movb    $0x04, %cl
988         shrb    %cl, %ah
989         aad
990         popw    %cx
991         ret
992
993 # Delay is needed after doing I/O
994 delay:
995         outb    %al,$0x80
996         ret
997
998 # Descriptor tables
999 #
1000 # NOTE: The intel manual says gdt should be sixteen bytes aligned for
1001 # efficiency reasons.  However, there are machines which are known not
1002 # to boot with misaligned GDTs, so alter this at your peril!  If you alter
1003 # GDT_ENTRY_BOOT_CS (in asm/segment.h) remember to leave at least two
1004 # empty GDT entries (one for NULL and one reserved).
1005 #
1006 # NOTE: On some CPUs, the GDT must be 8 byte aligned.  This is
1007 # true for the Voyager Quad CPU card which will not boot without
1008 # This directive.  16 byte aligment is recommended by intel.
1009 #
1010         .align 16
1011 gdt:
1012         .fill GDT_ENTRY_BOOT_CS,8,0
1013
1014         .word   0xFFFF                          # 4Gb - (0x100000*0x1000 = 4Gb)
1015         .word   0                               # base address = 0
1016         .word   0x9A00                          # code read/exec
1017         .word   0x00CF                          # granularity = 4096, 386
1018                                                 #  (+5th nibble of limit)
1019
1020         .word   0xFFFF                          # 4Gb - (0x100000*0x1000 = 4Gb)
1021         .word   0                               # base address = 0
1022         .word   0x9200                          # data read/write
1023         .word   0x00CF                          # granularity = 4096, 386
1024                                                 #  (+5th nibble of limit)
1025 gdt_end:
1026         .align  4
1027         
1028         .word   0                               # alignment byte
1029 idt_48:
1030         .word   0                               # idt limit = 0
1031         .word   0, 0                            # idt base = 0L
1032
1033         .word   0                               # alignment byte
1034 gdt_48:
1035         .word   gdt_end - gdt - 1               # gdt limit
1036         .word   0, 0                            # gdt base (filled in later)
1037
1038 # Include video setup & detection code
1039
1040 #include "video.S"
1041
1042 # Setup signature -- must be last
1043 setup_sig1:     .word   SIG1
1044 setup_sig2:     .word   SIG2
1045
1046 # After this point, there is some free space which is used by the video mode
1047 # handling code to store the temporary mode table (not used by the kernel).
1048
1049 modelist:
1050
1051 .text
1052 endtext:
1053 .data
1054 enddata:
1055 .bss
1056 endbss: