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