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