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