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