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