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