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