locore.s revision 13081
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.58 1995/12/25 14:40:49 davidg Exp $ 38 */ 39 40/* 41 * locore.s: FreeBSD machine support for the Intel 386 42 * originally from: locore.s, by William F. Jolitz 43 * 44 * Substantially rewritten by David Greenman, Rod Grimes, 45 * Bruce Evans, Wolfgang Solfrank, and many others. 46 */ 47 48#include "assym.s" /* system definitions */ 49#include <machine/psl.h> /* processor status longword defs */ 50#include <machine/pte.h> /* page table entry definitions */ 51#include <sys/errno.h> /* error return codes */ 52#include <machine/specialreg.h> /* x86 special registers */ 53#include <machine/cputypes.h> /* x86 cpu type definitions */ 54#include <sys/syscall.h> /* system call numbers */ 55#include <machine/asmacros.h> /* miscellaneous asm macros */ 56#include <sys/reboot.h> 57#include "apm.h" 58 59/* 60 * XXX 61 * 62 * Note: This version greatly munged to avoid various assembler errors 63 * that may be fixed in newer versions of gas. Perhaps newer versions 64 * will have more pleasant appearance. 65 */ 66 67/* 68 * PTmap is recursive pagemap at top of virtual address space. 69 * Within PTmap, the page directory can be found (third indirection). 70 */ 71 .globl _PTmap,_PTD,_PTDpde 72 .set _PTmap,PTDPTDI << PDRSHIFT 73 .set _PTD,_PTmap + (PTDPTDI * NBPG) 74 .set _PTDpde,_PTD + (PTDPTDI * PDESIZE) 75 76/* 77 * Sysmap is the base address of the kernel page tables. 78 * It is a bogus interface for kgdb and isn't used by the kernel itself. 79 */ 80 .set _Sysmap,_PTmap + (KPTDI * NBPG) 81 82/* 83 * APTmap, APTD is the alternate recursive pagemap. 84 * It's used when modifying another process's page tables. 85 */ 86 .globl _APTmap,_APTD,_APTDpde 87 .set _APTmap,APTDPTDI << PDRSHIFT 88 .set _APTD,_APTmap + (APTDPTDI * NBPG) 89 .set _APTDpde,_PTD + (APTDPTDI * PDESIZE) 90 91/* 92 * Access to each processes kernel stack is via a region of 93 * per-process address space (at the beginning), immediatly above 94 * the user process stack. 95 */ 96 .set _kstack,USRSTACK 97 .globl _kstack 98 99/* 100 * Globals 101 */ 102 .data 103 104 .globl tmpstk 105 .space 0x1000 /* space for tmpstk - temporary stack */ 106tmpstk: 107/* 108 * Dummy frame at top of tmpstk to help debuggers print a nice stack trace. 109 */ 110 .long tmpstk+8 /* caller's %ebp */ 111 .long _cpu_switch /* caller */ 112 .long 0 /* %ebp == 0 should terminate trace */ 113 .long _mvesp /* in case %ebp == 0 doesn't work ... */ 114 .long 0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555 115 116 .globl _boothowto,_bootdev 117 118 .globl _cpu,_cold,_atdevbase,_cpu_vendor,_cpu_id,_bootinfo 119 .globl _cpu_high, _cpu_feature 120 121_cpu: .long 0 /* are we 386, 386sx, or 486 */ 122_cpu_id: .long 0 /* stepping ID */ 123_cpu_high: .long 0 /* highest arg to CPUID */ 124_cpu_feature: .long 0 /* features */ 125_cpu_vendor: .space 20 /* CPU origin code */ 126_bootinfo: .space BOOTINFO_SIZE /* bootinfo that we can handle */ 127_cold: .long 1 /* cold till we are not */ 128_atdevbase: .long 0 /* location of start of iomem in virtual */ 129_atdevphys: .long 0 /* location of device mapping ptes (phys) */ 130 131_KERNend: .long 0 /* phys addr end of kernel (just after bss) */ 132 133 .globl _IdlePTD 134_IdlePTD: .long 0 /* phys addr of kernel PTD */ 135 136_KPTphys: .long 0 /* phys addr of kernel page tables */ 137 138 .globl _proc0paddr 139_proc0paddr: .long 0 /* address of proc 0 address space */ 140 141#ifdef BDE_DEBUGGER 142 .globl _bdb_exists /* flag to indicate BDE debugger is available */ 143_bdb_exists: .long 0 144#endif 145 146/* 147 * System Initialization 148 */ 149 .text 150 151/* 152 * btext: beginning of text section. 153 * Also the entry point (jumped to directly from the boot blocks). 154 */ 155NON_GPROF_ENTRY(btext) 156 movw $0x1234,0x472 /* warm boot */ 157 158 /* Set up a real frame, some day we will be doing returns */ 159 pushl %ebp 160 movl %esp, %ebp 161 162 /* Don't trust what the BIOS gives for eflags. */ 163 pushl $PSL_KERNEL 164 popfl 165 166 /* Don't trust what the BIOS gives for %fs and %gs. */ 167 mov %ds, %ax 168 mov %ax, %fs 169 mov %ax, %gs 170 171 /* 172 * This code is called in different ways depending on what loaded 173 * and started the kernel. This is used to detect how we get the 174 * arguments from the other code and what we do with them. 175 * 176 * Old disk boot blocks: 177 * (*btext)(howto, bootdev, cyloffset, esym); 178 * [return address == 0, and can NOT be returned to] 179 * [cyloffset was not supported by the FreeBSD boot code 180 * and always passed in as 0] 181 * [esym is also known as total in the boot code, and 182 * was never properly supported by the FreeBSD boot code] 183 * 184 * Old diskless netboot code: 185 * (*btext)(0,0,0,0,&nfsdiskless,0,0,0); 186 * [return address != 0, and can NOT be returned to] 187 * If we are being booted by this code it will NOT work, 188 * so we are just going to halt if we find this case. 189 * 190 * New uniform boot code: 191 * (*btext)(howto, bootdev, 0, 0, 0, &bootinfo) 192 * [return address != 0, and can be returned to] 193 * 194 * There may seem to be a lot of wasted arguments in here, but 195 * that is so the newer boot code can still load very old kernels 196 * and old boot code can load new kernels. 197 */ 198 199 /* 200 * The old style disk boot blocks fake a frame on the stack and 201 * did an lret to get here. The frame on the stack has a return 202 * address of 0. 203 */ 204 cmpl $0,4(%ebp) 205 je 2f /* olddiskboot: */ 206 207 /* 208 * We have some form of return address, so this is either the 209 * old diskless netboot code, or the new uniform code. That can 210 * be detected by looking at the 5th argument, it if is 0 we 211 * we are being booted by the new unifrom boot code. 212 */ 213 cmpl $0,24(%ebp) 214 je 1f /* newboot: */ 215 216 /* 217 * Seems we have been loaded by the old diskless boot code, we 218 * don't stand a chance of running as the diskless structure 219 * changed considerably between the two, so just halt. 220 */ 221 hlt 222 223 /* 224 * We have been loaded by the new uniform boot code. 225 * Lets check the bootinfo version, and if we do not understand 226 * it we return to the loader with a status of 1 to indicate this error 227 */ 2281: /* newboot: */ 229 movl 28(%ebp),%ebx /* &bootinfo.version */ 230 movl BI_VERSION(%ebx),%eax 231 cmpl $1,%eax /* We only understand version 1 */ 232 je 1f 233 movl $1,%eax /* Return status */ 234 leave 235 ret 236 2371: 238 /* 239 * If we have a kernelname copy it in 240 */ 241 movl BI_KERNELNAME(%ebx),%esi 242 cmpl $0,%esi 243 je 2f /* No kernelname */ 244 movl $MAXPATHLEN,%ecx /* Brute force!!! */ 245 lea _kernelname-KERNBASE,%edi 246 cmpb $'/',(%esi) /* Make sure it starts with a slash */ 247 je 1f 248 movb $'/',(%edi) 249 incl %edi 250 decl %ecx 2511: 252 cld 253 rep 254 movsb 255 2562: 257 /* 258 * Determine the size of the boot loader's copy of the bootinfo 259 * struct. This is impossible to do properly because old versions 260 * of the struct don't contain a size field and there are 2 old 261 * versions with the same version number. 262 */ 263 movl $BI_ENDCOMMON,%ecx /* prepare for sizeless version */ 264 testl $RB_BOOTINFO,8(%ebp) /* bi_size (and bootinfo) valid? */ 265 je got_bi_size /* no, sizeless version */ 266 movl BI_SIZE(%ebx),%ecx 267got_bi_size: 268 269 /* 270 * Copy the common part of the bootinfo struct 271 */ 272 movl %ebx,%esi 273 movl $_bootinfo-KERNBASE,%edi 274 cmpl $BOOTINFO_SIZE,%ecx 275 jbe got_common_bi_size 276 movl $BOOTINFO_SIZE,%ecx 277got_common_bi_size: 278 cld 279 rep 280 movsb 281 282#ifdef NFS 283 /* 284 * If we have a nfs_diskless structure copy it in 285 */ 286 movl BI_NFS_DISKLESS(%ebx),%esi 287 cmpl $0,%esi 288 je 2f 289 lea _nfs_diskless-KERNBASE,%edi 290 movl $NFSDISKLESS_SIZE,%ecx 291 cld 292 rep 293 movsb 294 lea _nfs_diskless_valid-KERNBASE,%edi 295 movl $1,(%edi) 296#endif 297 298 /* 299 * The old style disk boot. 300 * (*btext)(howto, bootdev, cyloffset, esym); 301 * Note that the newer boot code just falls into here to pick 302 * up howto and bootdev, cyloffset and esym are no longer used 303 */ 3042: /* olddiskboot: */ 305 movl 8(%ebp),%eax 306 movl %eax,_boothowto-KERNBASE 307 movl 12(%ebp),%eax 308 movl %eax,_bootdev-KERNBASE 309 310#if NAPM > 0 311 /* call APM BIOS driver setup (i386/apm/apm_setup.s) */ 312 call _apm_setup 313#endif /* NAPM */ 314 315 /* Find out our CPU type. */ 316 317 /* Try to toggle alignment check flag; does not exist on 386. */ 318 pushfl 319 popl %eax 320 movl %eax,%ecx 321 orl $PSL_AC,%eax 322 pushl %eax 323 popfl 324 pushfl 325 popl %eax 326 xorl %ecx,%eax 327 andl $PSL_AC,%eax 328 pushl %ecx 329 popfl 330 331 testl %eax,%eax 332 jnz 1f 333 movl $CPU_386,_cpu-KERNBASE 334 jmp 3f 335 3361: /* Try to toggle identification flag; does not exist on early 486s. */ 337 pushfl 338 popl %eax 339 movl %eax,%ecx 340 xorl $PSL_ID,%eax 341 pushl %eax 342 popfl 343 pushfl 344 popl %eax 345 xorl %ecx,%eax 346 andl $PSL_ID,%eax 347 pushl %ecx 348 popfl 349 350 testl %eax,%eax 351 jnz 1f 352 movl $CPU_486,_cpu-KERNBASE 353 354 /* check for Cyrix 486DLC -- based on check routine */ 355 /* documented in "Cx486SLC/e SMM Programmer's Guide" */ 356 xorw %dx,%dx 357 cmpw %dx,%dx # set flags to known state 358 pushfw 359 popw %cx # store flags in ecx 360 movw $0xffff,%ax 361 movw $0x0004,%bx 362 divw %bx 363 pushfw 364 popw %ax 365 andw $0x08d5,%ax # mask off important bits 366 andw $0x08d5,%cx 367 cmpw %ax,%cx 368 369 jnz 3f # if flags changed, Intel chip 370 371 movl $CPU_486DLC,_cpu-KERNBASE # set CPU value for Cyrix 372 movl $0x69727943,_cpu_vendor-KERNBASE # store vendor string 373 movw $0x0078,_cpu_vendor-KERNBASE+4 374 375#ifndef CYRIX_CACHE_WORKS 376 /* Disable caching of the ISA hole only. */ 377 invd 378 movb $CCR0,%al # Configuration Register index (CCR0) 379 outb %al,$0x22 380 inb $0x23,%al 381 orb $(CCR0_NC1|CCR0_BARB),%al 382 movb %al,%ah 383 movb $CCR0,%al 384 outb %al,$0x22 385 movb %ah,%al 386 outb %al,$0x23 387 invd 388#else /* CYRIX_CACHE_WORKS */ 389 /* Set cache parameters */ 390 invd # Start with guaranteed clean cache 391 movb $CCR0,%al # Configuration Register index (CCR0) 392 outb %al,$0x22 393 inb $0x23,%al 394 andb $~CCR0_NC0,%al 395#ifndef CYRIX_CACHE_REALLY_WORKS 396 orb $(CCR0_NC1|CCR0_BARB),%al 397#else 398 orb $CCR0_NC1,%al 399#endif 400 movb %al,%ah 401 movb $CCR0,%al 402 outb %al,$0x22 403 movb %ah,%al 404 outb %al,$0x23 405 /* clear non-cacheable region 1 */ 406 movb $(NCR1+2),%al 407 outb %al,$0x22 408 movb $NCR_SIZE_0K,%al 409 outb %al,$0x23 410 /* clear non-cacheable region 2 */ 411 movb $(NCR2+2),%al 412 outb %al,$0x22 413 movb $NCR_SIZE_0K,%al 414 outb %al,$0x23 415 /* clear non-cacheable region 3 */ 416 movb $(NCR3+2),%al 417 outb %al,$0x22 418 movb $NCR_SIZE_0K,%al 419 outb %al,$0x23 420 /* clear non-cacheable region 4 */ 421 movb $(NCR4+2),%al 422 outb %al,$0x22 423 movb $NCR_SIZE_0K,%al 424 outb %al,$0x23 425 /* enable caching in CR0 */ 426 movl %cr0,%eax 427 andl $~(CR0_CD|CR0_NW),%eax 428 movl %eax,%cr0 429 invd 430#endif /* CYRIX_CACHE_WORKS */ 431 jmp 3f 432 4331: /* Use the `cpuid' instruction. */ 434 xorl %eax,%eax 435 .byte 0x0f,0xa2 # cpuid 0 436 movl %eax,_cpu_high-KERNBASE # highest capability 437 movl %ebx,_cpu_vendor-KERNBASE # store vendor string 438 movl %edx,_cpu_vendor+4-KERNBASE 439 movl %ecx,_cpu_vendor+8-KERNBASE 440 movb $0,_cpu_vendor+12-KERNBASE 441 442 movl $1,%eax 443 .byte 0x0f,0xa2 # cpuid 1 444 movl %eax,_cpu_id-KERNBASE # store cpu_id 445 movl %edx,_cpu_feature-KERNBASE # store cpu_feature 446 rorl $8,%eax # extract family type 447 andl $15,%eax 448 cmpl $5,%eax 449 jae 1f 450 451 /* less than Pentium; must be 486 */ 452 movl $CPU_486,_cpu-KERNBASE 453 jmp 3f 4541: 455 /* a Pentium? */ 456 cmpl $5,%eax 457 jne 2f 458 movl $CPU_586,_cpu-KERNBASE 459 jmp 3f 4602: 461 /* Greater than Pentium...call it a Pentium Pro */ 462 movl $CPU_686,_cpu-KERNBASE 4633: 464 465 /* 466 * Finished with old stack; load new %esp now instead of later so 467 * we can trace this code without having to worry about the trace 468 * trap clobbering the memory test or the zeroing of the bss+bootstrap 469 * page tables. 470 * 471 * XXX - wdboot clears the bss after testing that this is safe. 472 * This is too wasteful - memory below 640K is scarce. The boot 473 * program should check: 474 * text+data <= &stack_variable - more_space_for_stack 475 * text+data+bss+pad+space_for_page_tables <= end_of_memory 476 * Oops, the gdt is in the carcass of the boot program so clearing 477 * the rest of memory is still not possible. 478 */ 479 movl $tmpstk-KERNBASE,%esp /* bootstrap stack end location */ 480 481/* 482 * Virtual address space of kernel: 483 * 484 * text | data | bss | [syms] | page dir | proc0 kernel stack | usr stk map | Sysmap 485 * pages: 1 UPAGES (2) 1 NKPT (7) 486 */ 487 488/* find end of kernel image */ 489 movl $_end-KERNBASE,%ecx 490 addl $NBPG-1,%ecx /* page align up */ 491 andl $~(NBPG-1),%ecx 492 movl %ecx,%esi /* esi = start of free memory */ 493 movl %ecx,_KERNend-KERNBASE /* save end of kernel */ 494 495/* clear bss */ 496 movl $_edata-KERNBASE,%edi 497 subl %edi,%ecx /* get amount to clear */ 498 xorl %eax,%eax /* specify zero fill */ 499 cld 500 rep 501 stosb 502 503#ifdef DDB 504/* include symbols in "kernel image" if they are loaded */ 505 movl _bootinfo+BI_ESYMTAB-KERNBASE,%edi 506 testl %edi,%edi 507 je over_symalloc 508 addl $NBPG-1,%edi 509 andl $~(NBPG-1),%edi 510 movl %edi,%esi 511 movl %esi,_KERNend-KERNBASE 512 movl $KERNBASE,%edi 513 addl %edi,_bootinfo+BI_SYMTAB-KERNBASE 514 addl %edi,_bootinfo+BI_ESYMTAB-KERNBASE 515over_symalloc: 516#endif 517 518/* 519 * The value in esi is both the end of the kernel bss and a pointer to 520 * the kernel page directory, and is used by the rest of locore to build 521 * the tables. 522 * esi + 1(page dir) + 2(UPAGES) + 1(p0stack) + NKPT(number of kernel 523 * page table pages) is then passed on the stack to init386(first) as 524 * the value first. esi should ALWAYS be page aligned!! 525 */ 526 movl %esi,%ecx /* Get current first availiable address */ 527 528/* clear pagetables, page directory, stack, etc... */ 529 movl %esi,%edi /* base (page directory) */ 530 movl $((1+UPAGES+1+NKPT)*NBPG),%ecx /* amount to clear */ 531 xorl %eax,%eax /* specify zero fill */ 532 cld 533 rep 534 stosb 535 536/* physical address of Idle proc/kernel page directory */ 537 movl %esi,_IdlePTD-KERNBASE 538 539/* 540 * fillkpt 541 * eax = (page frame address | control | status) == pte 542 * ebx = address of page table 543 * ecx = how many pages to map 544 */ 545#define fillkpt \ 5461: movl %eax,(%ebx) ; \ 547 addl $NBPG,%eax ; /* increment physical address */ \ 548 addl $4,%ebx ; /* next pte */ \ 549 loop 1b ; 550 551/* 552 * Map Kernel 553 * 554 * First step - build page tables 555 */ 556#if defined (KGDB) || defined (BDE_DEBUGGER) 557 movl _KERNend-KERNBASE,%ecx /* this much memory, */ 558 shrl $PGSHIFT,%ecx /* for this many PTEs */ 559#ifdef BDE_DEBUGGER 560 cmpl $0xa0,%ecx /* XXX - cover debugger pages */ 561 jae 1f 562 movl $0xa0,%ecx 5631: 564#endif /* BDE_DEBUGGER */ 565 movl $PG_V|PG_KW,%eax /* kernel R/W, valid */ 566 lea ((1+UPAGES+1)*NBPG)(%esi),%ebx /* phys addr of kernel PT base */ 567 movl %ebx,_KPTphys-KERNBASE /* save in global */ 568 fillkpt 569 570#else /* !KGDB && !BDE_DEBUGGER */ 571 /* write protect kernel text (doesn't do a thing for 386's - only 486's) */ 572 movl $_etext-KERNBASE,%ecx /* get size of text */ 573 addl $NBPG-1,%ecx /* round up to page */ 574 shrl $PGSHIFT,%ecx /* for this many PTEs */ 575 movl $PG_V|PG_KR,%eax /* specify read only */ 576#if 0 577 movl $_etext,%ecx /* get size of text */ 578 subl $_btext,%ecx 579 addl $NBPG-1,%ecx /* round up to page */ 580 shrl $PGSHIFT,%ecx /* for this many PTEs */ 581 movl $_btext-KERNBASE,%eax /* get offset to physical memory */ 582 orl $PG_V|PG_KR,%eax /* specify read only */ 583#endif 584 lea ((1+UPAGES+1)*NBPG)(%esi),%ebx /* phys addr of kernel PT base */ 585 movl %ebx,_KPTphys-KERNBASE /* save in global */ 586 fillkpt 587 588 /* data and bss are r/w */ 589 andl $PG_FRAME,%eax /* strip to just addr of bss */ 590 movl _KERNend-KERNBASE,%ecx /* calculate size */ 591 subl %eax,%ecx 592 shrl $PGSHIFT,%ecx 593 orl $PG_V|PG_KW,%eax /* valid, kernel read/write */ 594 fillkpt 595#endif /* KGDB || BDE_DEBUGGER */ 596 597/* now initialize the page dir, upages, and p0stack PT */ 598 599 movl $(1+UPAGES+1),%ecx /* number of PTEs */ 600 movl %esi,%eax /* phys address of PTD */ 601 andl $PG_FRAME,%eax /* convert to PFN, should be a NOP */ 602 orl $PG_V|PG_KW,%eax /* valid, kernel read/write */ 603 movl %esi,%ebx /* calculate pte offset to ptd */ 604 shrl $PGSHIFT-2,%ebx 605 addl %esi,%ebx /* address of page directory */ 606 addl $((1+UPAGES+1)*NBPG),%ebx /* offset to kernel page tables */ 607 fillkpt 608 609/* map I/O memory map */ 610 611 movl _KPTphys-KERNBASE,%ebx /* base of kernel page tables */ 612 lea (0xa0 * PTESIZE)(%ebx),%ebx /* hardwire ISA hole at KERNBASE + 0xa0000 */ 613 movl $0x100-0xa0,%ecx /* for this many pte s, */ 614 movl $(0xa0000|PG_V|PG_KW|PG_N),%eax /* valid, kernel read/write, non-cacheable */ 615 movl %ebx,_atdevphys-KERNBASE /* save phys addr of ptes */ 616 fillkpt 617 618 /* map proc 0's kernel stack into user page table page */ 619 620 movl $UPAGES,%ecx /* for this many pte s, */ 621 lea (1*NBPG)(%esi),%eax /* physical address in proc 0 */ 622 lea (KERNBASE)(%eax),%edx /* change into virtual addr */ 623 movl %edx,_proc0paddr-KERNBASE /* save VA for proc 0 init */ 624 orl $PG_V|PG_KW,%eax /* valid, kernel read/write */ 625 lea ((1+UPAGES)*NBPG)(%esi),%ebx /* addr of stack page table in proc 0 */ 626 addl $(KSTKPTEOFF * PTESIZE),%ebx /* offset to kernel stack PTE */ 627 fillkpt 628 629/* 630 * Initialize kernel page table directory 631 */ 632 /* install a pde for temporary double map of bottom of VA */ 633 movl _KPTphys-KERNBASE,%eax 634 orl $PG_V|PG_KW,%eax /* valid, kernel read/write */ 635 movl %eax,(%esi) /* which is where temp maps! */ 636 637 /* initialize kernel pde's */ 638 movl $(NKPT),%ecx /* for this many PDEs */ 639 lea (KPTDI*PDESIZE)(%esi),%ebx /* offset of pde for kernel */ 640 fillkpt 641 642 /* install a pde recursively mapping page directory as a page table! */ 643 movl %esi,%eax /* phys address of ptd in proc 0 */ 644 orl $PG_V|PG_KW,%eax /* pde entry is valid */ 645 movl %eax,PTDPTDI*PDESIZE(%esi) /* which is where PTmap maps! */ 646 647 /* install a pde to map kernel stack for proc 0 */ 648 lea ((1+UPAGES)*NBPG)(%esi),%eax /* physical address of pt in proc 0 */ 649 orl $PG_V|PG_KW,%eax /* pde entry is valid */ 650 movl %eax,KSTKPTDI*PDESIZE(%esi) /* which is where kernel stack maps! */ 651 652#ifdef BDE_DEBUGGER 653 /* copy and convert stuff from old gdt and idt for debugger */ 654 655 cmpl $0x0375c339,0x96104 /* XXX - debugger signature */ 656 jne 1f 657 movb $1,_bdb_exists-KERNBASE 6581: 659 pushal 660 subl $2*6,%esp 661 662 sgdt (%esp) 663 movl 2(%esp),%esi /* base address of current gdt */ 664 movl $_gdt-KERNBASE,%edi 665 movl %edi,2(%esp) 666 movl $8*18/4,%ecx 667 cld 668 rep /* copy gdt */ 669 movsl 670 movl $_gdt-KERNBASE,-8+2(%edi) /* adjust gdt self-ptr */ 671 movb $0x92,-8+5(%edi) 672 673 sidt 6(%esp) 674 movl 6+2(%esp),%esi /* base address of current idt */ 675 movl 8+4(%esi),%eax /* convert dbg descriptor to ... */ 676 movw 8(%esi),%ax 677 movl %eax,bdb_dbg_ljmp+1-KERNBASE /* ... immediate offset ... */ 678 movl 8+2(%esi),%eax 679 movw %ax,bdb_dbg_ljmp+5-KERNBASE /* ... and selector for ljmp */ 680 movl 24+4(%esi),%eax /* same for bpt descriptor */ 681 movw 24(%esi),%ax 682 movl %eax,bdb_bpt_ljmp+1-KERNBASE 683 movl 24+2(%esi),%eax 684 movw %ax,bdb_bpt_ljmp+5-KERNBASE 685 686 movl $_idt-KERNBASE,%edi 687 movl %edi,6+2(%esp) 688 movl $8*4/4,%ecx 689 cld 690 rep /* copy idt */ 691 movsl 692 693 lgdt (%esp) 694 lidt 6(%esp) 695 696 addl $2*6,%esp 697 popal 698#endif /* BDE_DEBUGGER */ 699 700 /* load base of page directory and enable mapping */ 701 movl %esi,%eax /* phys address of ptd in proc 0 */ 702 movl %eax,%cr3 /* load ptd addr into mmu */ 703 movl %cr0,%eax /* get control word */ 704 orl $CR0_PE|CR0_PG,%eax /* enable paging */ 705 movl %eax,%cr0 /* and let's page NOW! */ 706 707 pushl $begin /* jump to high mem */ 708 ret 709 710begin: /* now running relocated at KERNBASE where the system is linked to run */ 711 movl _atdevphys,%edx /* get pte PA */ 712 subl _KPTphys,%edx /* remove base of ptes, now have phys offset */ 713 shll $PGSHIFT-2,%edx /* corresponding to virt offset */ 714 addl $KERNBASE,%edx /* add virtual base */ 715 movl %edx,_atdevbase 716 717 /* set up bootstrap stack */ 718 movl $_kstack+UPAGES*NBPG,%esp /* bootstrap stack end location */ 719 xorl %eax,%eax /* mark end of frames */ 720 movl %eax,%ebp 721 movl _proc0paddr,%eax 722 movl %esi,PCB_CR3(%eax) 723 724#ifdef BDE_DEBUGGER 725 /* relocate debugger gdt entries */ 726 727 movl $_gdt+8*9,%eax /* adjust slots 9-17 */ 728 movl $9,%ecx 729reloc_gdt: 730 movb $KERNBASE>>24,7(%eax) /* top byte of base addresses, was 0, */ 731 addl $8,%eax /* now KERNBASE>>24 */ 732 loop reloc_gdt 733 734 cmpl $0,_bdb_exists 735 je 1f 736 int $3 7371: 738#endif /* BDE_DEBUGGER */ 739 740 /* 741 * Prepare "first" - physical address of first available page 742 * after the kernel+pdir+upages+p0stack+page tables 743 */ 744 lea ((1+UPAGES+1+NKPT)*NBPG)(%esi),%esi 745 746 pushl %esi /* value of first for init386(first) */ 747 call _init386 /* wire 386 chip for unix operation */ 748 popl %esi 749 750 .globl __ucodesel,__udatasel 751 752 pushl $0 /* unused */ 753 pushl __udatasel /* ss */ 754 pushl $0 /* esp - filled in by execve() */ 755 pushl $PSL_USER /* eflags (IOPL 0, int enab) */ 756 pushl __ucodesel /* cs */ 757 pushl $0 /* eip - filled in by execve() */ 758 subl $(12*4),%esp /* space for rest of registers */ 759 760 pushl %esp /* call main with frame pointer */ 761 call _main /* autoconfiguration, mountroot etc */ 762 763 addl $(13*4),%esp /* back to a frame we can return with */ 764 765 /* 766 * now we've run main() and determined what cpu-type we are, we can 767 * enable write protection and alignment checking on i486 cpus and 768 * above. 769 */ 770#if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU) 771 cmpl $CPUCLASS_386,_cpu_class 772 je 1f 773 movl %cr0,%eax /* get control word */ 774 orl $CR0_WP|CR0_AM,%eax /* enable i486 features */ 775 movl %eax,%cr0 /* and do it */ 776#endif 777 /* 778 * on return from main(), we are process 1 779 * set up address space and stack so that we can 'return' to user mode 780 */ 7811: 782 movl __ucodesel,%eax 783 movl __udatasel,%ecx 784 785 movl %cx,%ds 786 movl %cx,%es 787 movl %ax,%fs /* double map cs to fs */ 788 movl %cx,%gs /* and ds to gs */ 789 iret /* goto user! */ 790 791#define LCALL(x,y) .byte 0x9a ; .long y ; .word x 792 793NON_GPROF_ENTRY(sigcode) 794 call SIGF_HANDLER(%esp) 795 lea SIGF_SC(%esp),%eax /* scp (the call may have clobbered the */ 796 /* copy at 8(%esp)) */ 797 pushl %eax 798 pushl %eax /* junk to fake return address */ 799 movl $103,%eax /* XXX sigreturn() */ 800 LCALL(0x7,0) /* enter kernel with args on stack */ 801 hlt /* never gets here */ 802 803 .globl _szsigcode 804_szsigcode: 805 .long _szsigcode-_sigcode 806