locore.s revision 25837
1/*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * William Jolitz. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)locore.s 7.3 (Berkeley) 5/13/91 37 * $Id: locore.s,v 1.86 1997/04/26 11:45:08 peter Exp $ 38 * 39 * originally from: locore.s, by William F. Jolitz 40 * 41 * Substantially rewritten by David Greenman, Rod Grimes, 42 * Bruce Evans, Wolfgang Solfrank, Poul-Henning Kamp 43 * and many others. 44 */ 45 46#include "apm.h" 47#include "opt_cpu.h" 48#include "opt_ddb.h" 49#include "opt_userconfig.h" 50#include "opt_smp.h" 51#include "opt_smp_privpages.h" 52#include "opt_serial.h" 53 54#include <sys/errno.h> 55#include <sys/syscall.h> 56#include <sys/reboot.h> 57 58#include <machine/asmacros.h> 59#include <machine/cputypes.h> 60#include <machine/psl.h> 61#include <machine/pmap.h> 62#include <machine/specialreg.h> 63 64#if defined(SMP) && defined(SMP_PRIVPAGES) 65#include <machine/apic.h> 66#endif /* SMP && PRIVPAGES */ 67 68#include "assym.s" 69 70/* 71 * XXX 72 * 73 * Note: This version greatly munged to avoid various assembler errors 74 * that may be fixed in newer versions of gas. Perhaps newer versions 75 * will have more pleasant appearance. 76 */ 77 78/* 79 * PTmap is recursive pagemap at top of virtual address space. 80 * Within PTmap, the page directory can be found (third indirection). 81 */ 82 .globl _PTmap,_PTD,_PTDpde 83 .set _PTmap,(PTDPTDI << PDRSHIFT) 84 .set _PTD,_PTmap + (PTDPTDI * PAGE_SIZE) 85 .set _PTDpde,_PTD + (PTDPTDI * PDESIZE) 86 87/* 88 * APTmap, APTD is the alternate recursive pagemap. 89 * It's used when modifying another process's page tables. 90 */ 91 .globl _APTmap,_APTD,_APTDpde 92 .set _APTmap,APTDPTDI << PDRSHIFT 93 .set _APTD,_APTmap + (APTDPTDI * PAGE_SIZE) 94 .set _APTDpde,_PTD + (APTDPTDI * PDESIZE) 95 96/* 97 * Globals 98 */ 99 .data 100 ALIGN_DATA /* just to be sure */ 101 102 .globl HIDENAME(tmpstk) 103 .space 0x2000 /* space for tmpstk - temporary stack */ 104HIDENAME(tmpstk): 105 106 .globl _boothowto,_bootdev 107 108 .globl _cpu,_cpu_vendor,_cpu_id,_bootinfo 109 .globl _cpu_high, _cpu_feature 110 111_cpu: .long 0 /* are we 386, 386sx, or 486 */ 112_cpu_id: .long 0 /* stepping ID */ 113_cpu_high: .long 0 /* highest arg to CPUID */ 114_cpu_feature: .long 0 /* features */ 115_cpu_vendor: .space 20 /* CPU origin code */ 116_bootinfo: .space BOOTINFO_SIZE /* bootinfo that we can handle */ 117 118_KERNend: .long 0 /* phys addr end of kernel (just after bss) */ 119physfree: .long 0 /* phys addr of next free page */ 120 121#if defined(SMP) && defined(SMP_PRIVPAGES) 122cpu0pp: .long 0 /* phys addr cpu0 private pg */ 123cpu0pt: .long 0 /* phys addr cpu0 private pt */ 124 125 .globl _cpu0prvpage,_cpu0prvpt 126_cpu0prvpage: .long 0 /* relocated version */ 127_cpu0prvpt: .long 0 /* relocated version */ 128#endif /* SMP && PRIVPAGES */ 129 130 .globl _IdlePTD 131_IdlePTD: .long 0 /* phys addr of kernel PTD */ 132 133_KPTphys: .long 0 /* phys addr of kernel page tables */ 134 135 .globl _proc0paddr 136_proc0paddr: .long 0 /* address of proc 0 address space */ 137p0upa: .long 0 /* phys addr of proc0's UPAGES */ 138 139#ifdef BDE_DEBUGGER 140 .globl _bdb_exists /* flag to indicate BDE debugger is present */ 141_bdb_exists: .long 0 142#endif 143 144 145/********************************************************************** 146 * 147 * Some handy macros 148 * 149 */ 150 151#define R(foo) ((foo)-KERNBASE) 152 153#define ALLOCPAGES(foo) \ 154 movl R(physfree), %esi ; \ 155 movl $((foo)*PAGE_SIZE), %eax ; \ 156 addl %esi, %eax ; \ 157 movl %eax, R(physfree) ; \ 158 movl %esi, %edi ; \ 159 movl $((foo)*PAGE_SIZE),%ecx ; \ 160 xorl %eax,%eax ; \ 161 cld ; \ 162 rep ; \ 163 stosb 164 165/* 166 * fillkpt 167 * eax = page frame address 168 * ebx = index into page table 169 * ecx = how many pages to map 170 * base = base address of page dir/table 171 * prot = protection bits 172 */ 173#define fillkpt(base, prot) \ 174 shll $2,%ebx ; \ 175 addl base,%ebx ; \ 176 orl $PG_V,%eax ; \ 177 orl prot,%eax ; \ 1781: movl %eax,(%ebx) ; \ 179 addl $PAGE_SIZE,%eax ; /* increment physical address */ \ 180 addl $4,%ebx ; /* next pte */ \ 181 loop 1b 182 183/* 184 * fillkptphys(prot) 185 * eax = physical address 186 * ecx = how many pages to map 187 * prot = protection bits 188 */ 189#define fillkptphys(prot) \ 190 movl %eax, %ebx ; \ 191 shrl $PAGE_SHIFT, %ebx ; \ 192 fillkpt(R(_KPTphys), prot) 193 194 .text 195/********************************************************************** 196 * 197 * This is where the bootblocks start us, set the ball rolling... 198 * 199 */ 200NON_GPROF_ENTRY(btext) 201 202#ifdef PC98 203 jmp 1f 204 .globl _pc98_system_parameter 205 .org 0x400 206_pc98_system_parameter: 207 .space 0x240 /* BIOS parameter block */ 2081: 209 /* save SYSTEM PARAMETER for resume (NS/T or other) */ 210 movl $0xa1000,%esi 211 movl $0x100000,%edi 212 movl $0x0630,%ecx 213 cld 214 rep 215 movsb 216#else /* IBM-PC */ 217#ifdef BDE_DEBUGGER 218#ifdef BIOS_STEALS_3K 219 cmpl $0x0375c339,0x95504 220#else 221 cmpl $0x0375c339,0x96104 /* XXX - debugger signature */ 222#endif 223 jne 1f 224 movb $1,R(_bdb_exists) 2251: 226#endif 227 228/* Tell the bios to warmboot next time */ 229 movw $0x1234,0x472 230#endif /* PC98 */ 231 232/* Set up a real frame in case the double return in newboot is executed. */ 233 pushl %ebp 234 movl %esp, %ebp 235 236/* Don't trust what the BIOS gives for eflags. */ 237 pushl $PSL_KERNEL 238 popfl 239 240/* 241 * Don't trust what the BIOS gives for %fs and %gs. Trust the bootstrap 242 * to set %cs, %ds, %es and %ss. 243 */ 244 mov %ds, %ax 245 mov %ax, %fs 246 mov %ax, %gs 247 248 call recover_bootinfo 249 250/* Get onto a stack that we can trust. */ 251/* 252 * XXX this step is delayed in case recover_bootinfo needs to return via 253 * the old stack, but it need not be, since recover_bootinfo actually 254 * returns via the old frame. 255 */ 256 movl $R(HIDENAME(tmpstk)),%esp 257 258#ifdef PC98 259 testb $0x02,0x100620 /* pc98_machine_type & M_EPSON_PC98 */ 260 jz 3f 261 cmpb $0x0b,0x100624 /* epson_machine_id <= 0x0b */ 262 ja 3f 263 264 /* count up memory */ 265 movl $0x100000,%eax /* next, talley remaining memory */ 266 movl $0xFFF-0x100,%ecx 2671: movl 0(%eax),%ebx /* save location to check */ 268 movl $0xa55a5aa5,0(%eax) /* write test pattern */ 269 cmpl $0xa55a5aa5,0(%eax) /* does not check yet for rollover */ 270 jne 2f 271 movl %ebx,0(%eax) /* restore memory */ 272 addl $PAGE_SIZE,%eax 273 loop 1b 2742: subl $0x100000,%eax 275 shrl $17,%eax 276 movb %al,0x100401 2773: 278#endif 279 280 call identify_cpu 281 282/* clear bss */ 283/* 284 * XXX this should be done a little earlier. 285 * 286 * XXX we don't check that there is memory for our bss and page tables 287 * before using it. 288 * 289 * XXX the boot program somewhat bogusly clears the bss. We still have 290 * to do it in case we were unzipped by kzipboot. Then the boot program 291 * only clears kzipboot's bss. 292 * 293 * XXX the gdt and idt are still somewhere in the boot program. We 294 * depend on the convention that the boot program is below 1MB and we 295 * are above 1MB to keep the gdt and idt away from the bss and page 296 * tables. The idt is only used if BDE_DEBUGGER is enabled. 297 */ 298 movl $R(_end),%ecx 299 movl $R(_edata),%edi 300 subl %edi,%ecx 301 xorl %eax,%eax 302 cld 303 rep 304 stosb 305 306#if NAPM > 0 307/* 308 * XXX it's not clear that APM can live in the current environonment. 309 * Only pc-relative addressing works. 310 */ 311 call _apm_setup 312#endif 313 314 call create_pagetables 315 316#ifdef BDE_DEBUGGER 317/* 318 * Adjust as much as possible for paging before enabling paging so that the 319 * adjustments can be traced. 320 */ 321 call bdb_prepare_paging 322#endif 323 324/* Now enable paging */ 325 movl R(_IdlePTD), %eax 326 movl %eax,%cr3 /* load ptd addr into mmu */ 327 movl %cr0,%eax /* get control word */ 328 orl $CR0_PE|CR0_PG,%eax /* enable paging */ 329 movl %eax,%cr0 /* and let's page NOW! */ 330 331#ifdef BDE_DEBUGGER 332/* 333 * Complete the adjustments for paging so that we can keep tracing through 334 * initi386() after the low (physical) addresses for the gdt and idt become 335 * invalid. 336 */ 337 call bdb_commit_paging 338#endif 339 340 pushl $begin /* jump to high virtualized address */ 341 ret 342 343/* now running relocated at KERNBASE where the system is linked to run */ 344begin: 345 /* set up bootstrap stack */ 346 movl _proc0paddr,%esp /* location of in-kernel pages */ 347 addl $UPAGES*PAGE_SIZE,%esp /* bootstrap stack end location */ 348 xorl %eax,%eax /* mark end of frames */ 349 movl %eax,%ebp 350 movl _proc0paddr,%eax 351 movl _IdlePTD, %esi 352 movl %esi,PCB_CR3(%eax) 353 354 movl physfree, %esi 355 pushl %esi /* value of first for init386(first) */ 356 call _init386 /* wire 386 chip for unix operation */ 357 popl %esi 358 359 .globl __ucodesel,__udatasel 360 361 pushl $0 /* unused */ 362 pushl __udatasel /* ss */ 363 pushl $0 /* esp - filled in by execve() */ 364 pushl $PSL_USER /* eflags (IOPL 0, int enab) */ 365 pushl __ucodesel /* cs */ 366 pushl $0 /* eip - filled in by execve() */ 367 subl $(12*4),%esp /* space for rest of registers */ 368 369 pushl %esp /* call main with frame pointer */ 370 call _main /* autoconfiguration, mountroot etc */ 371 372 hlt /* never returns to here */ 373 374/* 375 * When starting init, call this to configure the process for user 376 * mode. This will be inherited by other processes. 377 */ 378NON_GPROF_ENTRY(prepare_usermode) 379 /* 380 * Now we've run main() and determined what cpu-type we are, we can 381 * enable write protection and alignment checking on i486 cpus and 382 * above. 383 */ 384#if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU) 385 cmpl $CPUCLASS_386,_cpu_class 386 je 1f 387 movl %cr0,%eax /* get control word */ 388 orl $CR0_WP|CR0_AM,%eax /* enable i486 features */ 389 movl %eax,%cr0 /* and do it */ 3901: 391#endif 392 /* 393 * on return from main(), we are process 1 394 * set up address space and stack so that we can 'return' to user mode 395 */ 396 movl __ucodesel,%eax 397 movl __udatasel,%ecx 398 399#if 0 400 movl %cx,%ds 401#endif 402 movl %cx,%es 403 movl %ax,%fs /* double map cs to fs */ 404 movl %cx,%gs /* and ds to gs */ 405 ret /* goto user! */ 406 407 408#define LCALL(x,y) .byte 0x9a ; .long y ; .word x 409 410/* 411 * Signal trampoline, copied to top of user stack 412 */ 413NON_GPROF_ENTRY(sigcode) 414 call SIGF_HANDLER(%esp) 415 lea SIGF_SC(%esp),%eax /* scp (the call may have clobbered the */ 416 /* copy at 8(%esp)) */ 417 pushl %eax 418 pushl %eax /* junk to fake return address */ 419 movl $SYS_sigreturn,%eax /* sigreturn() */ 420 LCALL(0x7,0) /* enter kernel with args on stack */ 421 hlt /* never gets here */ 422 ALIGN_TEXT 423_esigcode: 424 425 .data 426 .globl _szsigcode 427_szsigcode: 428 .long _esigcode-_sigcode 429 .text 430 431/********************************************************************** 432 * 433 * Recover the bootinfo passed to us from the boot program 434 * 435 */ 436recover_bootinfo: 437 /* 438 * This code is called in different ways depending on what loaded 439 * and started the kernel. This is used to detect how we get the 440 * arguments from the other code and what we do with them. 441 * 442 * Old disk boot blocks: 443 * (*btext)(howto, bootdev, cyloffset, esym); 444 * [return address == 0, and can NOT be returned to] 445 * [cyloffset was not supported by the FreeBSD boot code 446 * and always passed in as 0] 447 * [esym is also known as total in the boot code, and 448 * was never properly supported by the FreeBSD boot code] 449 * 450 * Old diskless netboot code: 451 * (*btext)(0,0,0,0,&nfsdiskless,0,0,0); 452 * [return address != 0, and can NOT be returned to] 453 * If we are being booted by this code it will NOT work, 454 * so we are just going to halt if we find this case. 455 * 456 * New uniform boot code: 457 * (*btext)(howto, bootdev, 0, 0, 0, &bootinfo) 458 * [return address != 0, and can be returned to] 459 * 460 * There may seem to be a lot of wasted arguments in here, but 461 * that is so the newer boot code can still load very old kernels 462 * and old boot code can load new kernels. 463 */ 464 465 /* 466 * The old style disk boot blocks fake a frame on the stack and 467 * did an lret to get here. The frame on the stack has a return 468 * address of 0. 469 */ 470 cmpl $0,4(%ebp) 471 je olddiskboot 472 473 /* 474 * We have some form of return address, so this is either the 475 * old diskless netboot code, or the new uniform code. That can 476 * be detected by looking at the 5th argument, if it is 0 477 * we are being booted by the new uniform boot code. 478 */ 479 cmpl $0,24(%ebp) 480 je newboot 481 482 /* 483 * Seems we have been loaded by the old diskless boot code, we 484 * don't stand a chance of running as the diskless structure 485 * changed considerably between the two, so just halt. 486 */ 487 hlt 488 489 /* 490 * We have been loaded by the new uniform boot code. 491 * Let's check the bootinfo version, and if we do not understand 492 * it we return to the loader with a status of 1 to indicate this error 493 */ 494newboot: 495 movl 28(%ebp),%ebx /* &bootinfo.version */ 496 movl BI_VERSION(%ebx),%eax 497 cmpl $1,%eax /* We only understand version 1 */ 498 je 1f 499 movl $1,%eax /* Return status */ 500 leave 501 /* 502 * XXX this returns to our caller's caller (as is required) since 503 * we didn't set up a frame and our caller did. 504 */ 505 ret 506 5071: 508 /* 509 * If we have a kernelname copy it in 510 */ 511 movl BI_KERNELNAME(%ebx),%esi 512 cmpl $0,%esi 513 je 2f /* No kernelname */ 514 movl $MAXPATHLEN,%ecx /* Brute force!!! */ 515 movl $R(_kernelname),%edi 516 cmpb $'/',(%esi) /* Make sure it starts with a slash */ 517 je 1f 518 movb $'/',(%edi) 519 incl %edi 520 decl %ecx 5211: 522 cld 523 rep 524 movsb 525 5262: 527 /* 528 * Determine the size of the boot loader's copy of the bootinfo 529 * struct. This is impossible to do properly because old versions 530 * of the struct don't contain a size field and there are 2 old 531 * versions with the same version number. 532 */ 533 movl $BI_ENDCOMMON,%ecx /* prepare for sizeless version */ 534 testl $RB_BOOTINFO,8(%ebp) /* bi_size (and bootinfo) valid? */ 535 je got_bi_size /* no, sizeless version */ 536 movl BI_SIZE(%ebx),%ecx 537got_bi_size: 538 539 /* 540 * Copy the common part of the bootinfo struct 541 */ 542 movl %ebx,%esi 543 movl $R(_bootinfo),%edi 544 cmpl $BOOTINFO_SIZE,%ecx 545 jbe got_common_bi_size 546 movl $BOOTINFO_SIZE,%ecx 547got_common_bi_size: 548 cld 549 rep 550 movsb 551 552#ifdef NFS 553#ifndef BOOTP_NFSV3 554 /* 555 * If we have a nfs_diskless structure copy it in 556 */ 557 movl BI_NFS_DISKLESS(%ebx),%esi 558 cmpl $0,%esi 559 je olddiskboot 560 movl $R(_nfs_diskless),%edi 561 movl $NFSDISKLESS_SIZE,%ecx 562 cld 563 rep 564 movsb 565 movl $R(_nfs_diskless_valid),%edi 566 movl $1,(%edi) 567#endif 568#endif 569 570 /* 571 * The old style disk boot. 572 * (*btext)(howto, bootdev, cyloffset, esym); 573 * Note that the newer boot code just falls into here to pick 574 * up howto and bootdev, cyloffset and esym are no longer used 575 */ 576olddiskboot: 577 movl 8(%ebp),%eax 578 movl %eax,R(_boothowto) 579 movl 12(%ebp),%eax 580 movl %eax,R(_bootdev) 581 582#if defined(USERCONFIG_BOOT) && defined(USERCONFIG) 583#ifdef PC98 584 movl $0x90200, %esi 585#else 586 movl $0x10200, %esi 587#endif 588 movl $R(_userconfig_from_boot),%edi 589 movl $512,%ecx 590 cld 591 rep 592 movsb 593#endif /* USERCONFIG_BOOT */ 594 595 ret 596 597 598/********************************************************************** 599 * 600 * Identify the CPU and initialize anything special about it 601 * 602 */ 603identify_cpu: 604 605 /* Try to toggle alignment check flag; does not exist on 386. */ 606 pushfl 607 popl %eax 608 movl %eax,%ecx 609 orl $PSL_AC,%eax 610 pushl %eax 611 popfl 612 pushfl 613 popl %eax 614 xorl %ecx,%eax 615 andl $PSL_AC,%eax 616 pushl %ecx 617 popfl 618 619 testl %eax,%eax 620 jnz try486 621 622 /* NexGen CPU does not have aligment check flag. */ 623 pushfl 624 movl $0x5555, %eax 625 xorl %edx, %edx 626 movl $2, %ecx 627 clc 628 divl %ecx 629 jz trynexgen 630 popfl 631 movl $CPU_386,R(_cpu) 632 jmp 3f 633 634trynexgen: 635 movl $CPU_NX586,R(_cpu) 636 movl $0x4778654e,R(_cpu_vendor) # store vendor string 637 movl $0x72446e65,R(_cpu_vendor+4) 638 movl $0x6e657669,R(_cpu_vendor+8) 639 movl $0,R(_cpu_vendor+12) 640 jmp 3f 641 642try486: /* Try to toggle identification flag; does not exist on early 486s. */ 643 pushfl 644 popl %eax 645 movl %eax,%ecx 646 xorl $PSL_ID,%eax 647 pushl %eax 648 popfl 649 pushfl 650 popl %eax 651 xorl %ecx,%eax 652 andl $PSL_ID,%eax 653 pushl %ecx 654 popfl 655 656 testl %eax,%eax 657 jnz trycpuid 658 movl $CPU_486,R(_cpu) 659 660 /* 661 * Check Cyrix CPU 662 * Cyrix CPUs do not change the undefined flags following 663 * execution of the divide instruction which divides 5 by 2. 664 * 665 * Note: CPUID is enabled on M2, so it passes another way. 666 */ 667 pushfl 668 movl $0x5555, %eax 669 xorl %edx, %edx 670 movl $2, %ecx 671 clc 672 divl %ecx 673 jnc trycyrix 674 popfl 675 jmp 3f /* You may use Intel CPU. */ 676 677trycyrix: 678 popfl 679 /* 680 * IBM Bluelighting CPU also doesn't change the undefined flags. 681 * Because IBM doesn't disclose the information for Bluelighting 682 * CPU, we couldn't distinguish it from Cyrix's (including IBM 683 * brand of Cyrix CPUs). 684 */ 685 movl $0x69727943,R(_cpu_vendor) # store vendor string 686 movl $0x736e4978,R(_cpu_vendor+4) 687 movl $0x64616574,R(_cpu_vendor+8) 688 jmp 3f 689 690trycpuid: /* Use the `cpuid' instruction. */ 691 xorl %eax,%eax 692 .byte 0x0f,0xa2 # cpuid 0 693 movl %eax,R(_cpu_high) # highest capability 694 movl %ebx,R(_cpu_vendor) # store vendor string 695 movl %edx,R(_cpu_vendor+4) 696 movl %ecx,R(_cpu_vendor+8) 697 movb $0,R(_cpu_vendor+12) 698 699 movl $1,%eax 700 .byte 0x0f,0xa2 # cpuid 1 701 movl %eax,R(_cpu_id) # store cpu_id 702 movl %edx,R(_cpu_feature) # store cpu_feature 703 rorl $8,%eax # extract family type 704 andl $15,%eax 705 cmpl $5,%eax 706 jae 1f 707 708 /* less than Pentium; must be 486 */ 709 movl $CPU_486,R(_cpu) 710 jmp 3f 7111: 712 /* a Pentium? */ 713 cmpl $5,%eax 714 jne 2f 715 movl $CPU_586,R(_cpu) 716 jmp 3f 7172: 718 /* Greater than Pentium...call it a Pentium Pro */ 719 movl $CPU_686,R(_cpu) 7203: 721 ret 722 723 724/********************************************************************** 725 * 726 * Create the first page directory and its page tables. 727 * 728 */ 729 730create_pagetables: 731 732 testl $CPUID_PGE, R(_cpu_feature) 733 jz 1f 734 movl %cr4, %eax 735 orl $CR4_PGE, %eax 736 movl %eax, %cr4 7371: 738 739/* Find end of kernel image (rounded up to a page boundary). */ 740 movl $R(_end),%esi 741 742/* include symbols in "kernel image" if they are loaded and useful */ 743#ifdef DDB 744 movl R(_bootinfo+BI_ESYMTAB),%edi 745 testl %edi,%edi 746 je over_symalloc 747 movl %edi,%esi 748 movl $KERNBASE,%edi 749 addl %edi,R(_bootinfo+BI_SYMTAB) 750 addl %edi,R(_bootinfo+BI_ESYMTAB) 751over_symalloc: 752#endif 753 754 addl $PAGE_MASK,%esi 755 andl $~PAGE_MASK,%esi 756 movl %esi,R(_KERNend) /* save end of kernel */ 757 movl %esi,R(physfree) /* next free page is at end of kernel */ 758 759/* Allocate Kernel Page Tables */ 760 ALLOCPAGES(NKPT) 761 movl %esi,R(_KPTphys) 762 763/* Allocate Page Table Directory */ 764 ALLOCPAGES(1) 765 movl %esi,R(_IdlePTD) 766 767/* Allocate UPAGES */ 768 ALLOCPAGES(UPAGES) 769 movl %esi,R(p0upa) 770 addl $KERNBASE, %esi 771 movl %esi, R(_proc0paddr) 772 773#if defined(SMP) && defined(SMP_PRIVPAGES) 774/* Allocate cpu0's private data page */ 775 ALLOCPAGES(1) 776 movl %esi,R(cpu0pp) 777 addl $KERNBASE, %esi 778 movl %esi, R(_cpu0prvpage) /* relocated to KVM space */ 779 780/* Allocate cpu0's private page table for mapping priv page, apic, etc */ 781 ALLOCPAGES(1) 782 movl %esi,R(cpu0pt) 783 addl $KERNBASE, %esi 784 movl %esi, R(_cpu0prvpt) /* relocated to KVM space */ 785#endif /* SMP && SMP_PRIVPAGES */ 786 787/* Map read-only from zero to the end of the kernel text section */ 788 xorl %eax, %eax 789#ifdef BDE_DEBUGGER 790/* If the debugger is present, actually map everything read-write. */ 791 cmpl $0,R(_bdb_exists) 792 jne map_read_write 793#endif 794 xorl %edx,%edx 795 testl $CPUID_PGE, R(_cpu_feature) 796 jz 2f 797 orl $PG_G,%edx 798 7992: movl $R(_etext),%ecx 800 addl $PAGE_MASK,%ecx 801 shrl $PAGE_SHIFT,%ecx 802 fillkptphys(%edx) 803 804/* Map read-write, data, bss and symbols */ 805 movl $R(_etext),%eax 806 addl $PAGE_MASK, %eax 807 andl $~PAGE_MASK, %eax 808map_read_write: 809 movl $PG_RW,%edx 810 testl $CPUID_PGE, R(_cpu_feature) 811 jz 1f 812 orl $PG_G,%edx 813 8141: movl R(_KERNend),%ecx 815 subl %eax,%ecx 816 shrl $PAGE_SHIFT,%ecx 817 fillkptphys(%edx) 818 819/* Map page directory. */ 820 movl R(_IdlePTD), %eax 821 movl $1, %ecx 822 fillkptphys($PG_RW) 823 824/* Map proc0's UPAGES in the physical way ... */ 825 movl R(p0upa), %eax 826 movl $UPAGES, %ecx 827 fillkptphys($PG_RW) 828 829/* Map ISA hole */ 830 movl $ISA_HOLE_START, %eax 831 movl $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx 832 fillkptphys($PG_RW) 833 834#if defined(SMP) && defined(SMP_PRIVPAGES) 835/* Map cpu0's private page into global KVM */ 836 movl R(cpu0pp), %eax 837 movl $1, %ecx 838 fillkptphys($PG_RW) 839 840/* Map cpu0's private page table into global KVM */ 841 movl R(cpu0pt), %eax 842 movl $1, %ecx 843 fillkptphys($PG_RW) 844 845/* Map the private page into the private page table (4K @ 0xff80000) */ 846 movl R(cpu0pp), %eax 847 movl $0, %ebx /* pte offset = 0 */ 848 movl $1, %ecx /* one private page coming right up */ 849 fillkpt(R(cpu0pt), $PG_RW) 850 851/* Map the default Local APIC address (4K @ 0xff801000) */ 852 movl $DEFAULT_APIC_BASE, %eax /* XXX just testing.. */ 853 movl $1, %ebx /* pte offset = 1 */ 854 movl $1, %ecx /* Bing! Local APIC appears */ 855 fillkpt(R(cpu0pt), $PG_RW|PG_N) 856 857/* Map the default IO APIC address (4K @ 0xff802000) */ 858 movl $DEFAULT_IO_APIC_BASE, %eax /* XXX just testing.. */ 859 movl $2, %ebx /* pte offset = 2 */ 860 movl $1, %ecx /* Bing! Local APIC appears */ 861 fillkpt(R(cpu0pt), $PG_RW|PG_N) 862 863/* ... and put the page table in the pde. */ 864 movl R(cpu0pt), %eax 865 movl $MPPTDI, %ebx 866 movl $1, %ecx 867 fillkpt(R(_IdlePTD), $PG_RW) 868#endif /* SMP && SMP_PRIVPAGES */ 869 870/* install a pde for temporary double map of bottom of VA */ 871 movl R(_KPTphys), %eax 872 xorl %ebx, %ebx 873 movl $1, %ecx 874 fillkpt(R(_IdlePTD), $PG_RW) 875 876/* install pde's for pt's */ 877 movl R(_KPTphys), %eax 878 movl $KPTDI, %ebx 879 movl $NKPT, %ecx 880 fillkpt(R(_IdlePTD), $PG_RW) 881 882/* install a pde recursively mapping page directory as a page table */ 883 movl R(_IdlePTD), %eax 884 movl $PTDPTDI, %ebx 885 movl $1,%ecx 886 fillkpt(R(_IdlePTD), $PG_RW) 887 888 ret 889 890#ifdef BDE_DEBUGGER 891bdb_prepare_paging: 892 cmpl $0,R(_bdb_exists) 893 je bdb_prepare_paging_exit 894 895 subl $6,%esp 896 897 /* 898 * Copy and convert debugger entries from the bootstrap gdt and idt 899 * to the kernel gdt and idt. Everything is still in low memory. 900 * Tracing continues to work after paging is enabled because the 901 * low memory addresses remain valid until everything is relocated. 902 * However, tracing through the setidt() that initializes the trace 903 * trap will crash. 904 */ 905 sgdt (%esp) 906 movl 2(%esp),%esi /* base address of bootstrap gdt */ 907 movl $R(_gdt),%edi 908 movl %edi,2(%esp) /* prepare to load kernel gdt */ 909 movl $8*18/4,%ecx 910 cld 911 rep /* copy gdt */ 912 movsl 913 movl $R(_gdt),-8+2(%edi) /* adjust gdt self-ptr */ 914 movb $0x92,-8+5(%edi) 915 lgdt (%esp) 916 917 sidt (%esp) 918 movl 2(%esp),%esi /* base address of current idt */ 919 movl 8+4(%esi),%eax /* convert dbg descriptor to ... */ 920 movw 8(%esi),%ax 921 movl %eax,R(bdb_dbg_ljmp+1) /* ... immediate offset ... */ 922 movl 8+2(%esi),%eax 923 movw %ax,R(bdb_dbg_ljmp+5) /* ... and selector for ljmp */ 924 movl 24+4(%esi),%eax /* same for bpt descriptor */ 925 movw 24(%esi),%ax 926 movl %eax,R(bdb_bpt_ljmp+1) 927 movl 24+2(%esi),%eax 928 movw %ax,R(bdb_bpt_ljmp+5) 929 movl $R(_idt),%edi 930 movl %edi,2(%esp) /* prepare to load kernel idt */ 931 movl $8*4/4,%ecx 932 cld 933 rep /* copy idt */ 934 movsl 935 lidt (%esp) 936 937 addl $6,%esp 938 939bdb_prepare_paging_exit: 940 ret 941 942/* Relocate debugger gdt entries and gdt and idt pointers. */ 943bdb_commit_paging: 944 cmpl $0,_bdb_exists 945 je bdb_commit_paging_exit 946 947 movl $_gdt+8*9,%eax /* adjust slots 9-17 */ 948 movl $9,%ecx 949reloc_gdt: 950 movb $KERNBASE>>24,7(%eax) /* top byte of base addresses, was 0, */ 951 addl $8,%eax /* now KERNBASE>>24 */ 952 loop reloc_gdt 953 954 subl $6,%esp 955 sgdt (%esp) 956 addl $KERNBASE,2(%esp) 957 lgdt (%esp) 958 sidt (%esp) 959 addl $KERNBASE,2(%esp) 960 lidt (%esp) 961 addl $6,%esp 962 963 int $3 964 965bdb_commit_paging_exit: 966 ret 967 968#endif /* BDE_DEBUGGER */ 969