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