Merge from vendor branch HEIMDAL:
[dragonfly.git] / sys / platform / pc32 / i386 / locore.s
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  * $FreeBSD: src/sys/i386/i386/locore.s,v 1.132.2.10 2003/02/03 20:54:49 jhb Exp $
38  * $DragonFly: src/sys/platform/pc32/i386/locore.s,v 1.10 2005/02/27 10:57:24 swildner Exp $
39  *
40  *              originally from: locore.s, by William F. Jolitz
41  *
42  *              Substantially rewritten by David Greenman, Rod Grimes,
43  *                      Bruce Evans, Wolfgang Solfrank, Poul-Henning Kamp
44  *                      and many others.
45  */
46
47 #include "opt_bootp.h"
48 #include "opt_nfsroot.h"
49
50 #include <sys/syscall.h>
51 #include <sys/reboot.h>
52
53 #include <machine/asmacros.h>
54 #include <machine/cputypes.h>
55 #include <machine/psl.h>
56 #include <machine/pmap.h>
57 #include <machine/specialreg.h>
58
59 #include "assym.s"
60
61 /*
62  *      XXX
63  *
64  * Note: This version greatly munged to avoid various assembler errors
65  * that may be fixed in newer versions of gas. Perhaps newer versions
66  * will have more pleasant appearance.
67  */
68
69 /*
70  * PTmap is recursive pagemap at top of virtual address space.
71  * Within PTmap, the page directory can be found (third indirection).
72  */
73         .globl  PTmap,PTD,PTDpde
74         .set    PTmap,(PTDPTDI << PDRSHIFT)
75         .set    PTD,PTmap + (PTDPTDI * PAGE_SIZE)
76         .set    PTDpde,PTD + (PTDPTDI * PDESIZE)
77
78 /*
79  * APTmap, APTD is the alternate recursive pagemap.
80  * It's used when modifying another process's page tables.
81  */
82         .globl  APTmap,APTD,APTDpde
83         .set    APTmap,APTDPTDI << PDRSHIFT
84         .set    APTD,APTmap + (APTDPTDI * PAGE_SIZE)
85         .set    APTDpde,PTD + (APTDPTDI * PDESIZE)
86
87 /*
88  * Compiled KERNBASE location
89  */
90         .globl  kernbase
91         .set    kernbase,KERNBASE
92
93 /*
94  * Globals
95  */
96         .data
97         ALIGN_DATA              /* just to be sure */
98
99         .globl  .tmpstk
100         .space  0x2000          /* space for tmpstk - temporary stack */
101 .tmpstk:
102
103         .globl  boothowto,bootdev
104
105         .globl  cpu,cpu_vendor,cpu_id,bootinfo
106         .globl  cpu_high, cpu_feature, cpu_procinfo
107
108 cpu:            .long   0                       /* are we 386, 386sx, or 486 */
109 cpu_id:         .long   0                       /* stepping ID */
110 cpu_high:       .long   0                       /* highest arg to CPUID */
111 cpu_feature:    .long   0                       /* features */
112 cpu_procinfo:   .long   0                       /* brand index / HTT info */
113 cpu_vendor:     .space  20                      /* CPU origin code */
114 bootinfo:       .space  BOOTINFO_SIZE           /* bootinfo buffer space */
115
116 KERNend:        .long   0                       /* phys addr end of kernel (just after bss) */
117 physfree:       .long   0                       /* phys addr of next free page */
118
119                 .globl  cpu0prvpage
120 cpu0pp:         .long   0                       /* phys addr cpu0 private pg */
121 cpu0prvpage:    .long   0                       /* relocated version */
122 cpu0idlestk:    .long   0                       /* stack for the idle thread */
123
124                 .globl  SMPpt
125 SMPptpa:        .long   0                       /* phys addr SMP page table */
126 SMPpt:          .long   0                       /* relocated version */
127
128         .globl  IdlePTD
129 IdlePTD:        .long   0                       /* phys addr of kernel PTD */
130
131         .globl  KPTphys
132 KPTphys:        .long   0                       /* PA of kernel page tables */
133
134         .globl  proc0paddr
135 proc0paddr:     .long   0                       /* VA of proc 0 address space */
136 p0upa:          .long   0                       /* PA of proc0's UPAGES */
137
138 vm86phystk:     .long   0                       /* PA of vm86/bios stack */
139
140         .globl  vm86paddr, vm86pa
141 vm86paddr:      .long   0                       /* address of vm86 region */
142 vm86pa:         .long   0                       /* phys addr of vm86 region */
143
144 #ifdef BDE_DEBUGGER
145         .globl  bdb_exists                      /* BDE debugger is present */
146 bdb_exists:     .long   0
147 #endif
148
149 /**********************************************************************
150  *
151  * Some handy macros
152  *
153  */
154
155 #define R(foo) ((foo)-KERNBASE)
156
157 #define ALLOCPAGES(foo)                                 \
158         movl    R(physfree), %esi ;                     \
159         movl    $((foo)*PAGE_SIZE), %eax ;              \
160         addl    %esi, %eax ;                            \
161         movl    %eax, R(physfree) ;                     \
162         movl    %esi, %edi ;                            \
163         movl    $((foo)*PAGE_SIZE),%ecx ;               \
164         xorl    %eax,%eax ;                             \
165         cld ;                                           \
166         rep ;                                           \
167         stosb
168
169 /*
170  * fillkpt
171  *      eax = page frame address
172  *      ebx = index into page table
173  *      ecx = how many pages to map
174  *      base = base address of page dir/table
175  *      prot = protection bits
176  */
177 #define fillkpt(base, prot)               \
178         shll    $2,%ebx                 ; \
179         addl    base,%ebx               ; \
180         orl     $PG_V,%eax              ; \
181         orl     prot,%eax               ; \
182 1:      movl    %eax,(%ebx)             ; \
183         addl    $PAGE_SIZE,%eax         ; /* increment physical address */ \
184         addl    $4,%ebx                 ; /* next pte */ \
185         loop    1b
186
187 /*
188  * fillkptphys(prot)
189  *      eax = physical address
190  *      ecx = how many pages to map
191  *      prot = protection bits
192  */
193 #define fillkptphys(prot)                 \
194         movl    %eax, %ebx              ; \
195         shrl    $PAGE_SHIFT, %ebx       ; \
196         fillkpt(R(KPTphys), prot)
197
198         .text
199 /**********************************************************************
200  *
201  * This is where the bootblocks start us, set the ball rolling...
202  *
203  */
204 NON_GPROF_ENTRY(btext)
205
206 #ifdef BDE_DEBUGGER
207 #ifdef BIOS_STEALS_3K
208         cmpl    $0x0375c339,0x95504
209 #else
210         cmpl    $0x0375c339,0x96104     /* XXX - debugger signature */
211 #endif
212         jne     1f
213         movb    $1,R(bdb_exists)
214 1:
215 #endif
216 /* Tell the bios to warmboot next time */
217         movw    $0x1234,0x472
218
219 /* Set up a real frame in case the double return in newboot is executed. */
220         pushl   %ebp
221         movl    %esp, %ebp
222
223 /* Don't trust what the BIOS gives for eflags. */
224         pushl   $PSL_KERNEL
225         popfl
226
227 /*
228  * Don't trust what the BIOS gives for %fs and %gs.  Trust the bootstrap
229  * to set %cs, %ds, %es and %ss.
230  */
231         mov     %ds, %ax
232         mov     %ax, %fs
233         mov     %ax, %gs
234
235 /*
236  * Clear the bss.  Not all boot programs do it, and it is our job anyway.
237  * 
238  * XXX we don't check that there is memory for our bss and page tables   
239  * before using it.
240  * 
241  * Note: we must be careful to not overwrite an active gdt or idt.  They
242  * inactive from now until we switch to new ones, since we don't load any
243  * more segment registers or permit interrupts until after the switch.
244  */
245         movl    $R(_end),%ecx
246         movl    $R(_edata),%edi
247         subl    %edi,%ecx
248         xorl    %eax,%eax
249         cld
250         rep
251         stosb
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(.tmpstk),%esp
262
263         call    identify_cpu
264
265         call    create_pagetables
266
267 /*
268  * If the CPU has support for VME, turn it on.
269  */ 
270         testl   $CPUID_VME, R(cpu_feature)
271         jz      1f
272         movl    %cr4, %eax
273         orl     $CR4_VME, %eax
274         movl    %eax, %cr4
275 1:
276
277 #ifdef BDE_DEBUGGER
278 /*
279  * Adjust as much as possible for paging before enabling paging so that the
280  * adjustments can be traced.
281  */
282         call    bdb_prepare_paging
283 #endif
284
285 /* Now enable paging */
286         movl    R(IdlePTD), %eax
287         movl    %eax,%cr3                       /* load ptd addr into mmu */
288         movl    %cr0,%eax                       /* get control word */
289         orl     $CR0_PE|CR0_PG,%eax             /* enable paging */
290         movl    %eax,%cr0                       /* and let's page NOW! */
291
292
293 #ifdef BDE_DEBUGGER
294 /*
295  * Complete the adjustments for paging so that we can keep tracing through
296  * initi386() after the low (physical) addresses for the gdt and idt become
297  * invalid.
298  */
299         call    bdb_commit_paging
300 #endif
301
302         pushl   $begin                          /* jump to high virtualized address */
303         ret
304
305 /* now running relocated at KERNBASE where the system is linked to run */
306 begin:
307
308         /*
309          * set up the bootstrap stack.  The pcb sits at the end of the
310          * bootstrap stack.
311          */
312         /* set up bootstrap stack */
313         movl    proc0paddr,%esp /* location of in-kernel pages */
314         addl    $UPAGES*PAGE_SIZE-PCB_SIZE,%esp 
315         xorl    %eax,%eax               /* mark end of frames */
316         movl    %eax,%ebp
317         /*movl  proc0paddr,%eax*/
318         movl    IdlePTD, %esi
319         movl    %esi,PCB_CR3(%esp)
320
321         testl   $CPUID_PGE, R(cpu_feature)
322         jz      1f
323         movl    %cr4, %eax
324         orl     $CR4_PGE, %eax
325         movl    %eax, %cr4
326 1:
327
328         movl    physfree, %esi
329         pushl   %esi                    /* value of first for init386(first) */
330
331         call    init386                 /* wire 386 chip for unix operation */
332         popl    %esi
333
334         call    mi_startup              /* autoconfiguration, mountroot etc */
335
336         hlt             /* never returns to here */
337
338 /*
339  * Signal trampoline, copied to top of user stack
340  */
341 NON_GPROF_ENTRY(sigcode)
342         call    *SIGF_HANDLER(%esp)             /* call signal handler */
343         lea     SIGF_UC(%esp),%eax              /* get ucontext_t */
344         pushl   %eax
345         testl   $PSL_VM,UC_EFLAGS(%eax)
346         jne     9f
347         movl    UC_GS(%eax),%gs                 /* restore %gs */
348 9:
349         movl    $SYS_sigreturn,%eax
350         pushl   %eax                            /* junk to fake return addr. */
351         int     $0x80                           /* enter kernel with args */
352 0:      jmp     0b
353
354         ALIGN_TEXT
355 esigcode:
356
357         .data
358         .globl  szsigcode
359 szsigcode:
360         .long   esigcode - sigcode
361         .text
362
363 /**********************************************************************
364  *
365  * Recover the bootinfo passed to us from the boot program
366  *
367  */
368 recover_bootinfo:
369         /*
370          * This code is called in different ways depending on what loaded
371          * and started the kernel.  This is used to detect how we get the
372          * arguments from the other code and what we do with them.
373          *
374          * Old disk boot blocks:
375          *      (*btext)(howto, bootdev, cyloffset, esym);
376          *      [return address == 0, and can NOT be returned to]
377          *      [cyloffset was not supported by the FreeBSD boot code
378          *       and always passed in as 0]
379          *      [esym is also known as total in the boot code, and
380          *       was never properly supported by the FreeBSD boot code]
381          *
382          * Old diskless netboot code:
383          *      (*btext)(0,0,0,0,&nfsdiskless,0,0,0);
384          *      [return address != 0, and can NOT be returned to]
385          *      If we are being booted by this code it will NOT work,
386          *      so we are just going to halt if we find this case.
387          *
388          * New uniform boot code:
389          *      (*btext)(howto, bootdev, 0, 0, 0, &bootinfo)
390          *      [return address != 0, and can be returned to]
391          *
392          * There may seem to be a lot of wasted arguments in here, but
393          * that is so the newer boot code can still load very old kernels
394          * and old boot code can load new kernels.
395          */
396
397         /*
398          * The old style disk boot blocks fake a frame on the stack and
399          * did an lret to get here.  The frame on the stack has a return
400          * address of 0.
401          */
402         cmpl    $0,4(%ebp)
403         je      olddiskboot
404
405         /*
406          * We have some form of return address, so this is either the
407          * old diskless netboot code, or the new uniform code.  That can
408          * be detected by looking at the 5th argument, if it is 0
409          * we are being booted by the new uniform boot code.
410          */
411         cmpl    $0,24(%ebp)
412         je      newboot
413
414         /*
415          * Seems we have been loaded by the old diskless boot code, we
416          * don't stand a chance of running as the diskless structure
417          * changed considerably between the two, so just halt.
418          */
419          hlt
420
421         /*
422          * We have been loaded by the new uniform boot code.
423          * Let's check the bootinfo version, and if we do not understand
424          * it we return to the loader with a status of 1 to indicate this error
425          */
426 newboot:
427         movl    28(%ebp),%ebx           /* &bootinfo.version */
428         movl    BI_VERSION(%ebx),%eax
429         cmpl    $1,%eax                 /* We only understand version 1 */
430         je      1f
431         movl    $1,%eax                 /* Return status */
432         leave
433         /*
434          * XXX this returns to our caller's caller (as is required) since
435          * we didn't set up a frame and our caller did.
436          */
437         ret
438
439 1:
440         /*
441          * If we have a kernelname copy it in
442          */
443         movl    BI_KERNELNAME(%ebx),%esi
444         cmpl    $0,%esi
445         je      2f                      /* No kernelname */
446         movl    $MAXPATHLEN,%ecx        /* Brute force!!! */
447         movl    $R(kernelname),%edi
448         cmpb    $'/',(%esi)             /* Make sure it starts with a slash */
449         je      1f
450         movb    $'/',(%edi)
451         incl    %edi
452         decl    %ecx
453 1:
454         cld
455         rep
456         movsb
457
458 2:
459         /*
460          * Determine the size of the boot loader's copy of the bootinfo
461          * struct.  This is impossible to do properly because old versions
462          * of the struct don't contain a size field and there are 2 old
463          * versions with the same version number.
464          */
465         movl    $BI_ENDCOMMON,%ecx      /* prepare for sizeless version */
466         testl   $RB_BOOTINFO,8(%ebp)    /* bi_size (and bootinfo) valid? */
467         je      got_bi_size             /* no, sizeless version */
468         movl    BI_SIZE(%ebx),%ecx
469 got_bi_size:
470
471         /*
472          * Copy the common part of the bootinfo struct
473          */
474         movl    %ebx,%esi
475         movl    $R(bootinfo),%edi
476         cmpl    $BOOTINFO_SIZE,%ecx
477         jbe     got_common_bi_size
478         movl    $BOOTINFO_SIZE,%ecx
479 got_common_bi_size:
480         cld
481         rep
482         movsb
483
484 #ifdef NFS_ROOT
485 #ifndef BOOTP_NFSV3
486         /*
487          * If we have a nfs_diskless structure copy it in
488          */
489         movl    BI_NFS_DISKLESS(%ebx),%esi
490         cmpl    $0,%esi
491         je      olddiskboot
492         movl    $R(nfs_diskless),%edi
493         movl    $NFSDISKLESS_SIZE,%ecx
494         cld
495         rep
496         movsb
497         movl    $R(nfs_diskless_valid),%edi
498         movl    $1,(%edi)
499 #endif
500 #endif
501
502         /*
503          * The old style disk boot.
504          *      (*btext)(howto, bootdev, cyloffset, esym);
505          * Note that the newer boot code just falls into here to pick
506          * up howto and bootdev, cyloffset and esym are no longer used
507          */
508 olddiskboot:
509         movl    8(%ebp),%eax
510         movl    %eax,R(boothowto)
511         movl    12(%ebp),%eax
512         movl    %eax,R(bootdev)
513
514         ret
515
516
517 /**********************************************************************
518  *
519  * Identify the CPU and initialize anything special about it
520  *
521  */
522 identify_cpu:
523
524         /* Try to toggle alignment check flag; does not exist on 386. */
525         pushfl
526         popl    %eax
527         movl    %eax,%ecx
528         orl     $PSL_AC,%eax
529         pushl   %eax
530         popfl
531         pushfl
532         popl    %eax
533         xorl    %ecx,%eax
534         andl    $PSL_AC,%eax
535         pushl   %ecx
536         popfl
537
538         testl   %eax,%eax
539         jnz     try486
540
541         /* NexGen CPU does not have aligment check flag. */
542         pushfl
543         movl    $0x5555, %eax
544         xorl    %edx, %edx
545         movl    $2, %ecx
546         clc
547         divl    %ecx
548         jz      trynexgen
549         popfl
550         movl    $CPU_386,R(cpu)
551         jmp     3f
552
553 trynexgen:
554         popfl
555         movl    $CPU_NX586,R(cpu)
556         movl    $0x4778654e,R(cpu_vendor)       # store vendor string
557         movl    $0x72446e65,R(cpu_vendor+4)
558         movl    $0x6e657669,R(cpu_vendor+8)
559         movl    $0,R(cpu_vendor+12)
560         jmp     3f
561
562 try486: /* Try to toggle identification flag; does not exist on early 486s. */
563         pushfl
564         popl    %eax
565         movl    %eax,%ecx
566         xorl    $PSL_ID,%eax
567         pushl   %eax
568         popfl
569         pushfl
570         popl    %eax
571         xorl    %ecx,%eax
572         andl    $PSL_ID,%eax
573         pushl   %ecx
574         popfl
575
576         testl   %eax,%eax
577         jnz     trycpuid
578         movl    $CPU_486,R(cpu)
579
580         /*
581          * Check Cyrix CPU
582          * Cyrix CPUs do not change the undefined flags following
583          * execution of the divide instruction which divides 5 by 2.
584          *
585          * Note: CPUID is enabled on M2, so it passes another way.
586          */
587         pushfl
588         movl    $0x5555, %eax
589         xorl    %edx, %edx
590         movl    $2, %ecx
591         clc
592         divl    %ecx
593         jnc     trycyrix
594         popfl
595         jmp     3f              /* You may use Intel CPU. */
596
597 trycyrix:
598         popfl
599         /*
600          * IBM Bluelighting CPU also doesn't change the undefined flags.
601          * Because IBM doesn't disclose the information for Bluelighting
602          * CPU, we couldn't distinguish it from Cyrix's (including IBM
603          * brand of Cyrix CPUs).
604          */
605         movl    $0x69727943,R(cpu_vendor)       # store vendor string
606         movl    $0x736e4978,R(cpu_vendor+4)
607         movl    $0x64616574,R(cpu_vendor+8)
608         jmp     3f
609
610 trycpuid:       /* Use the `cpuid' instruction. */
611         xorl    %eax,%eax
612         cpuid                                   # cpuid 0
613         movl    %eax,R(cpu_high)                # highest capability
614         movl    %ebx,R(cpu_vendor)              # store vendor string
615         movl    %edx,R(cpu_vendor+4)
616         movl    %ecx,R(cpu_vendor+8)
617         movb    $0,R(cpu_vendor+12)
618
619         movl    $1,%eax
620         cpuid                                   # cpuid 1
621         movl    %eax,R(cpu_id)                  # store cpu_id
622         movl    %ebx,R(cpu_procinfo)            # store cpu_procinfo
623         movl    %edx,R(cpu_feature)             # store cpu_feature
624         rorl    $8,%eax                         # extract family type
625         andl    $15,%eax
626         cmpl    $5,%eax
627         jae     1f
628
629         /* less than Pentium; must be 486 */
630         movl    $CPU_486,R(cpu)
631         jmp     3f
632 1:
633         /* a Pentium? */
634         cmpl    $5,%eax
635         jne     2f
636         movl    $CPU_586,R(cpu)
637         jmp     3f
638 2:
639         /* Greater than Pentium...call it a Pentium Pro */
640         movl    $CPU_686,R(cpu)
641 3:
642         ret
643
644
645 /**********************************************************************
646  *
647  * Create the first page directory and its page tables.
648  *
649  */
650
651 create_pagetables:
652
653 /* Find end of kernel image (rounded up to a page boundary). */
654         movl    $R(end),%esi
655
656 /* Include symbols, if any. */
657         movl    R(bootinfo+BI_ESYMTAB),%edi
658         testl   %edi,%edi
659         je      over_symalloc
660         movl    %edi,%esi
661         movl    $KERNBASE,%edi
662         addl    %edi,R(bootinfo+BI_SYMTAB)
663         addl    %edi,R(bootinfo+BI_ESYMTAB)
664 over_symalloc:
665
666 /* If we are told where the end of the kernel space is, believe it. */
667         movl    R(bootinfo+BI_KERNEND),%edi
668         testl   %edi,%edi
669         je      no_kernend
670         movl    %edi,%esi
671 no_kernend:
672         
673         addl    $PAGE_MASK,%esi
674         andl    $~PAGE_MASK,%esi
675         movl    %esi,R(KERNend)         /* save end of kernel */
676         movl    %esi,R(physfree)        /* next free page is at end of kernel */
677
678
679 /* Allocate Kernel Page Tables */
680         ALLOCPAGES(NKPT)
681         movl    %esi,R(KPTphys)
682
683 /* Allocate Page Table Directory */
684         ALLOCPAGES(1)
685         movl    %esi,R(IdlePTD)
686
687 /* Allocate UPAGES */
688         ALLOCPAGES(UPAGES)
689         movl    %esi,R(p0upa)
690         addl    $KERNBASE, %esi
691         movl    %esi, R(proc0paddr)
692
693         ALLOCPAGES(1)                   /* vm86/bios stack */
694         movl    %esi,R(vm86phystk)
695
696         ALLOCPAGES(3)                   /* pgtable + ext + IOPAGES */
697         movl    %esi,R(vm86pa)
698         addl    $KERNBASE, %esi
699         movl    %esi, R(vm86paddr)
700
701 /* Allocate cpu0's private data page */
702         ALLOCPAGES(1)
703         movl    %esi,R(cpu0pp)
704         addl    $KERNBASE, %esi
705         movl    %esi, R(cpu0prvpage)    /* relocated to KVM space */
706
707 /* Allocate cpu0's idle stack */
708         ALLOCPAGES(UPAGES)
709         movl    %esi,R(cpu0idlestk)
710
711 /* Allocate SMP page table page */
712         ALLOCPAGES(1)
713         movl    %esi,R(SMPptpa)
714         addl    $KERNBASE, %esi
715         movl    %esi, R(SMPpt)          /* relocated to KVM space */
716
717 /* Map read-only from zero to the end of the kernel text section */
718         xorl    %eax, %eax
719 #ifdef BDE_DEBUGGER
720 /* If the debugger is present, actually map everything read-write. */
721         cmpl    $0,R(bdb_exists)
722         jne     map_read_write
723 #endif
724         xorl    %edx,%edx
725
726 #if !defined(SMP)
727         testl   $CPUID_PGE, R(cpu_feature)
728         jz      2f
729         orl     $PG_G,%edx
730 #endif
731         
732 2:      movl    $R(etext),%ecx
733         addl    $PAGE_MASK,%ecx
734         shrl    $PAGE_SHIFT,%ecx
735         fillkptphys(%edx)
736
737 /* Map read-write, data, bss and symbols */
738         movl    $R(etext),%eax
739         addl    $PAGE_MASK, %eax
740         andl    $~PAGE_MASK, %eax
741 map_read_write:
742         movl    $PG_RW,%edx
743 #if !defined(SMP)
744         testl   $CPUID_PGE, R(cpu_feature)
745         jz      1f
746         orl     $PG_G,%edx
747 #endif
748         
749 1:      movl    R(KERNend),%ecx
750         subl    %eax,%ecx
751         shrl    $PAGE_SHIFT,%ecx
752         fillkptphys(%edx)
753
754 /* Map page directory. */
755         movl    R(IdlePTD), %eax
756         movl    $1, %ecx
757         fillkptphys($PG_RW)
758
759 /* Map proc0's UPAGES in the physical way ... */
760         movl    R(p0upa), %eax
761         movl    $UPAGES, %ecx
762         fillkptphys($PG_RW)
763
764 /* Map ISA hole */
765         movl    $ISA_HOLE_START, %eax
766         movl    $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
767         fillkptphys($PG_RW)
768
769 /* Map space for the vm86 region */
770         movl    R(vm86phystk), %eax
771         movl    $4, %ecx
772         fillkptphys($PG_RW)
773
774 /* Map page 0 into the vm86 page table */
775         movl    $0, %eax
776         movl    $0, %ebx
777         movl    $1, %ecx
778         fillkpt(R(vm86pa), $PG_RW|PG_U)
779
780 /* ...likewise for the ISA hole */
781         movl    $ISA_HOLE_START, %eax
782         movl    $ISA_HOLE_START>>PAGE_SHIFT, %ebx
783         movl    $ISA_HOLE_LENGTH>>PAGE_SHIFT, %ecx
784         fillkpt(R(vm86pa), $PG_RW|PG_U)
785
786 /* Map cpu0's private page into global kmem (4K @ cpu0prvpage) */
787         movl    R(cpu0pp), %eax
788         movl    $1, %ecx
789         fillkptphys($PG_RW)
790
791 /* Map SMP page table page into global kmem FWIW */
792         movl    R(SMPptpa), %eax
793         movl    $1, %ecx
794         fillkptphys($PG_RW)
795
796 /* Map the private page into the SMP page table */
797         movl    R(cpu0pp), %eax
798         movl    $0, %ebx                /* pte offset = 0 */
799         movl    $1, %ecx                /* one private page coming right up */
800         fillkpt(R(SMPptpa), $PG_RW)
801
802 /* Map the cpu0's idle thread stack */
803         movl    R(cpu0idlestk), %eax
804         movl    $PS_IDLESTACK_PAGE, %ebx
805         movl    $UPAGES, %ecx
806         fillkpt(R(SMPptpa), $PG_RW)
807
808 /* ... and put the page table table in the pde. */
809         movl    R(SMPptpa), %eax
810         movl    $MPPTDI, %ebx
811         movl    $1, %ecx
812         fillkpt(R(IdlePTD), $PG_RW)
813
814 /* Fakeup VA for the local apic to allow early traps. */
815         ALLOCPAGES(1)
816         movl    %esi, %eax
817         movl    $(NPTEPG-1), %ebx       /* pte offset = NTEPG-1 */
818         movl    $1, %ecx                /* one private pt coming right up */
819         fillkpt(R(SMPptpa), $PG_RW)
820
821 #ifdef SMP
822 /* Initialize mp lock to allow early traps */
823         movl    $0, R(mp_lock)
824 #endif  /* SMP */
825
826 /* install a pde for temporary double map of bottom of VA */
827         movl    R(KPTphys), %eax
828         xorl    %ebx, %ebx
829         movl    $NKPT, %ecx
830         fillkpt(R(IdlePTD), $PG_RW)
831
832 /* install pde's for pt's */
833         movl    R(KPTphys), %eax
834         movl    $KPTDI, %ebx
835         movl    $NKPT, %ecx
836         fillkpt(R(IdlePTD), $PG_RW)
837
838 /* install a pde recursively mapping page directory as a page table */
839         movl    R(IdlePTD), %eax
840         movl    $PTDPTDI, %ebx
841         movl    $1,%ecx
842         fillkpt(R(IdlePTD), $PG_RW)
843
844         ret
845
846 #ifdef BDE_DEBUGGER
847 bdb_prepare_paging:
848         cmpl    $0,R(bdb_exists)
849         je      bdb_prepare_paging_exit
850
851         subl    $6,%esp
852
853         /*
854          * Copy and convert debugger entries from the bootstrap gdt and idt
855          * to the kernel gdt and idt.  Everything is still in low memory.
856          * Tracing continues to work after paging is enabled because the
857          * low memory addresses remain valid until everything is relocated.
858          * However, tracing through the setidt() that initializes the trace
859          * trap will crash.
860          */
861         sgdt    (%esp)
862         movl    2(%esp),%esi            /* base address of bootstrap gdt */
863         movl    $R(gdt),%edi
864         movl    %edi,2(%esp)            /* prepare to load kernel gdt */
865         movl    $8*18/4,%ecx
866         cld
867         rep                             /* copy gdt */
868         movsl
869         movl    $R(gdt),-8+2(%edi)      /* adjust gdt self-ptr */
870         movb    $0x92,-8+5(%edi)
871         lgdt    (%esp)
872
873         sidt    (%esp)
874         movl    2(%esp),%esi            /* base address of current idt */
875         movl    8+4(%esi),%eax          /* convert dbg descriptor to ... */
876         movw    8(%esi),%ax
877         movl    %eax,R(bdb_dbg_ljmp+1)  /* ... immediate offset ... */
878         movl    8+2(%esi),%eax
879         movw    %ax,R(bdb_dbg_ljmp+5)   /* ... and selector for ljmp */
880         movl    24+4(%esi),%eax         /* same for bpt descriptor */
881         movw    24(%esi),%ax
882         movl    %eax,R(bdb_bpt_ljmp+1)
883         movl    24+2(%esi),%eax
884         movw    %ax,R(bdb_bpt_ljmp+5)
885         movl    R(idt),%edi
886         movl    %edi,2(%esp)            /* prepare to load kernel idt */
887         movl    $8*4/4,%ecx
888         cld
889         rep                             /* copy idt */
890         movsl
891         lidt    (%esp)
892
893         addl    $6,%esp
894
895 bdb_prepare_paging_exit:
896         ret
897
898 /* Relocate debugger gdt entries and gdt and idt pointers. */
899 bdb_commit_paging:
900         cmpl    $0,_bdb_exists
901         je      bdb_commit_paging_exit
902
903         movl    $_gdt+8*9,%eax          /* adjust slots 9-17 */
904         movl    $9,%ecx
905 reloc_gdt:
906         movb    $KERNBASE>>24,7(%eax)   /* top byte of base addresses, was 0, */
907         addl    $8,%eax                 /* now KERNBASE>>24 */
908         loop    reloc_gdt
909
910         subl    $6,%esp
911         sgdt    (%esp)
912         addl    $KERNBASE,2(%esp)
913         lgdt    (%esp)
914         sidt    (%esp)
915         addl    $KERNBASE,2(%esp)
916         lidt    (%esp)
917         addl    $6,%esp
918
919         int     $3
920
921 bdb_commit_paging_exit:
922         ret
923
924 #endif /* BDE_DEBUGGER */