Annotation of src/sys/arch/i386/i386/locore.S, Revision 1.123
1.123 ! maxv 1: /* $NetBSD: locore.S,v 1.122 2016/05/14 09:51:56 maxv Exp $ */
1.53 yamt 2:
3: /*
4: * Copyright-o-rama!
5: */
6:
7: /*
1.62 bouyer 8: * Copyright (c) 2006 Manuel Bouyer.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29: *
30: */
31:
32: /*
1.53 yamt 33: * Copyright (c) 2001 Wasabi Systems, Inc.
34: * All rights reserved.
35: *
36: * Written by Frank van der Linden for Wasabi Systems, Inc.
37: *
38: * Redistribution and use in source and binary forms, with or without
39: * modification, are permitted provided that the following conditions
40: * are met:
41: * 1. Redistributions of source code must retain the above copyright
42: * notice, this list of conditions and the following disclaimer.
43: * 2. Redistributions in binary form must reproduce the above copyright
44: * notice, this list of conditions and the following disclaimer in the
45: * documentation and/or other materials provided with the distribution.
46: * 3. All advertising materials mentioning features or use of this software
47: * must display the following acknowledgement:
48: * This product includes software developed for the NetBSD Project by
49: * Wasabi Systems, Inc.
50: * 4. The name of Wasabi Systems, Inc. may not be used to endorse
51: * or promote products derived from this software without specific prior
52: * written permission.
53: *
54: * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
55: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
58: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64: * POSSIBILITY OF SUCH DAMAGE.
65: */
66:
1.1 fvdl 67: /*-
1.84 ad 68: * Copyright (c) 1998, 2000, 2004, 2006, 2007, 2009 The NetBSD Foundation, Inc.
1.1 fvdl 69: * All rights reserved.
70: *
71: * This code is derived from software contributed to The NetBSD Foundation
1.84 ad 72: * by Charles M. Hannum, and by Andrew Doran.
1.1 fvdl 73: *
74: * Redistribution and use in source and binary forms, with or without
75: * modification, are permitted provided that the following conditions
76: * are met:
77: * 1. Redistributions of source code must retain the above copyright
78: * notice, this list of conditions and the following disclaimer.
79: * 2. Redistributions in binary form must reproduce the above copyright
80: * notice, this list of conditions and the following disclaimer in the
81: * documentation and/or other materials provided with the distribution.
82: *
83: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
84: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
85: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
86: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
87: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
88: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
89: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
90: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
91: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
92: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
93: * POSSIBILITY OF SUCH DAMAGE.
94: */
95:
96: /*-
97: * Copyright (c) 1990 The Regents of the University of California.
98: * All rights reserved.
99: *
100: * This code is derived from software contributed to Berkeley by
101: * William Jolitz.
102: *
103: * Redistribution and use in source and binary forms, with or without
104: * modification, are permitted provided that the following conditions
105: * are met:
106: * 1. Redistributions of source code must retain the above copyright
107: * notice, this list of conditions and the following disclaimer.
108: * 2. Redistributions in binary form must reproduce the above copyright
109: * notice, this list of conditions and the following disclaimer in the
110: * documentation and/or other materials provided with the distribution.
1.12 agc 111: * 3. Neither the name of the University nor the names of its contributors
1.1 fvdl 112: * may be used to endorse or promote products derived from this software
113: * without specific prior written permission.
114: *
115: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
116: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
117: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
118: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
119: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
120: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
121: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
122: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
123: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
124: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
125: * SUCH DAMAGE.
126: *
127: * @(#)locore.s 7.3 (Berkeley) 5/13/91
128: */
129:
1.59 lukem 130: #include <machine/asm.h>
1.123 ! maxv 131: __KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.122 2016/05/14 09:51:56 maxv Exp $");
1.59 lukem 132:
1.18 christos 133: #include "opt_compat_oldboot.h"
1.114 uebayasi 134: #include "opt_copy_symtab.h"
1.1 fvdl 135: #include "opt_ddb.h"
1.83 apb 136: #include "opt_modular.h"
1.78 joerg 137: #include "opt_multiboot.h"
1.1 fvdl 138: #include "opt_realmem.h"
1.18 christos 139: #include "opt_vm86.h"
1.62 bouyer 140: #include "opt_xen.h"
1.1 fvdl 141:
142: #include "assym.h"
143: #include "lapic.h"
144: #include "ioapic.h"
1.8 fvdl 145: #include "ksyms.h"
1.1 fvdl 146:
147: #include <sys/errno.h>
148: #include <sys/syscall.h>
149:
150: #include <machine/cputypes.h>
151: #include <machine/segments.h>
152: #include <machine/specialreg.h>
153: #include <machine/trap.h>
154: #include <machine/i82489reg.h>
1.48 yamt 155: #include <machine/frameasm.h>
156: #include <machine/i82489reg.h>
1.62 bouyer 157: #ifndef XEN
158: #include <machine/multiboot.h>
159: #endif
1.1 fvdl 160:
161: /* Get definitions for IOM_BEGIN, IOM_END, and IOM_SIZE */
162: #include <dev/isa/isareg.h>
163:
1.104 uebayasi 164: #ifndef XEN
165: #define _RELOC(x) ((x) - KERNBASE)
166: #else
167: #define _RELOC(x) ((x))
168: #endif /* XEN */
169: #define RELOC(x) _RELOC(_C_LABEL(x))
170:
1.119 maxv 171: /* 32bit version of PG_NX */
172: #define PG_NX32 0x80000000
173:
1.115 maxv 174: #ifndef PAE
175: #define PROC0_PDIR_OFF 0
176: #else
177: #define PROC0_L3_OFF 0
178: #define PROC0_PDIR_OFF 1 * PAGE_SIZE
179: #endif
180:
181: #define PROC0_STK_OFF (PROC0_PDIR_OFF + PDP_SIZE * PAGE_SIZE)
182: #define PROC0_PTP1_OFF (PROC0_STK_OFF + UPAGES * PAGE_SIZE)
183:
184: /*
185: * fillkpt - Fill in a kernel page table
186: * eax = pte (page frame | control | status)
187: * ebx = page table address
188: * ecx = number of pages to map
189: *
190: * For PAE, each entry is 8 bytes long: we must set the 4 upper bytes to 0.
191: * This is done by the first instruction of fillkpt. In the non-PAE case, this
192: * instruction just clears the page table entry.
193: */
194: #define fillkpt \
195: 1: movl $0,(PDE_SIZE-4)(%ebx) ; /* upper 32 bits: 0 */ \
196: movl %eax,(%ebx) ; /* store phys addr */ \
197: addl $PDE_SIZE,%ebx ; /* next PTE/PDE */ \
198: addl $PAGE_SIZE,%eax ; /* next phys page */ \
199: loop 1b ;
200:
1.118 maxv 201: /*
1.120 maxv 202: * fillkpt_nox - Same as fillkpt, but sets the NX/XD bit.
203: */
204: #define fillkpt_nox \
205: pushl %ebp ; \
206: movl RELOC(nox_flag),%ebp ; \
207: 1: movl %ebp,(PDE_SIZE-4)(%ebx) ; /* upper 32 bits: NX */ \
208: movl %eax,(%ebx) ; /* store phys addr */ \
209: addl $PDE_SIZE,%ebx ; /* next PTE/PDE */ \
210: addl $PAGE_SIZE,%eax ; /* next phys page */ \
211: loop 1b ; \
212: popl %ebp ;
213:
214: /*
1.118 maxv 215: * killkpt - Destroy a kernel page table
216: * ebx = page table address
217: * ecx = number of pages to destroy
218: */
219: #define killkpt \
220: 1: movl $0,(PDE_SIZE-4)(%ebx) ; /* upper bits (for PAE) */ \
221: movl $0,(%ebx) ; \
222: addl $PDE_SIZE,%ebx ; \
223: loop 1b ;
224:
1.115 maxv 225:
1.62 bouyer 226: #ifdef XEN
227: /*
228: * Xen guest identifier and loader selection
229: */
230: .section __xen_guest
231: .ascii "GUEST_OS=netbsd,GUEST_VER=3.0,XEN_VER=xen-3.0"
1.106 uebayasi 232: .ascii ",VIRT_BASE=0xc0000000" /* KERNBASE */
233: .ascii ",ELF_PADDR_OFFSET=0xc0000000" /* KERNBASE */
234: .ascii ",VIRT_ENTRY=0xc0100000" /* KERNTEXTOFF */
235: .ascii ",HYPERCALL_PAGE=0x00000101"
1.62 bouyer 236: /* (???+HYPERCALL_PAGE_OFFSET)/PAGE_SIZE) */
1.66 bouyer 237: #ifdef PAE
1.106 uebayasi 238: .ascii ",PAE=yes[extended-cr3]"
1.66 bouyer 239: #endif
1.62 bouyer 240: .ascii ",LOADER=generic"
1.114 uebayasi 241: #if (NKSYMS || defined(DDB) || defined(MODULAR)) && !defined(makeoptions_COPY_SYMTAB)
1.62 bouyer 242: .ascii ",BSD_SYMTAB=yes"
243: #endif
244: .byte 0
1.106 uebayasi 245: #endif /* XEN */
1.62 bouyer 246:
1.1 fvdl 247: /*
248: * Initialization
249: */
250: .data
251:
1.119 maxv 252: .globl _C_LABEL(nox_flag)
1.100 chs 253: .globl _C_LABEL(cputype)
1.73 ad 254: .globl _C_LABEL(cpuid_level)
1.38 jmmv 255: .globl _C_LABEL(esym)
1.71 ad 256: .globl _C_LABEL(eblob)
1.38 jmmv 257: .globl _C_LABEL(atdevbase)
1.104 uebayasi 258: .globl _C_LABEL(lwp0uarea)
259: .globl _C_LABEL(PDPpaddr)
1.1 fvdl 260: .globl _C_LABEL(gdt)
261: .globl _C_LABEL(idt)
1.30 junyoung 262: .globl _C_LABEL(lapic_tpr)
263:
1.1 fvdl 264: #if NLAPIC > 0
265: #ifdef __ELF__
1.7 thorpej 266: .align PAGE_SIZE
1.1 fvdl 267: #else
268: .align 12
269: #endif
270: .globl _C_LABEL(local_apic), _C_LABEL(lapic_id)
1.104 uebayasi 271: .type _C_LABEL(local_apic), @object
272: LABEL(local_apic)
1.1 fvdl 273: .space LAPIC_ID
1.104 uebayasi 274: END(local_apic)
275: .type _C_LABEL(lapic_id), @object
276: LABEL(lapic_id)
1.1 fvdl 277: .long 0x00000000
1.106 uebayasi 278: .space LAPIC_TPRI-(LAPIC_ID+4)
1.104 uebayasi 279: END(lapic_id)
280: .type _C_LABEL(lapic_tpr), @object
281: LABEL(lapic_tpr)
1.106 uebayasi 282: .space LAPIC_PPRI-LAPIC_TPRI
1.104 uebayasi 283: END(lapic_tpr)
284: .type _C_LABEL(lapic_ppr), @object
1.30 junyoung 285: _C_LABEL(lapic_ppr):
1.1 fvdl 286: .space LAPIC_ISR-LAPIC_PPRI
1.104 uebayasi 287: END(lapic_ppr)
288: .type _C_LABEL(lapic_isr), @object
1.1 fvdl 289: _C_LABEL(lapic_isr):
1.7 thorpej 290: .space PAGE_SIZE-LAPIC_ISR
1.104 uebayasi 291: END(lapic_isr)
1.1 fvdl 292: #else
1.104 uebayasi 293: .type _C_LABEL(lapic_tpr), @object
294: LABEL(lapic_tpr)
1.1 fvdl 295: .long 0
1.104 uebayasi 296: END(lapic_tpr)
1.1 fvdl 297: #endif
1.119 maxv 298:
299: .type _C_LABEL(nox_flag), @object
300: LABEL(nox_flag) .long 0 /* 32bit NOX flag, set if supported */
301: END(nox_flag)
1.104 uebayasi 302: .type _C_LABEL(cputype), @object
1.115 maxv 303: LABEL(cputype) .long 0 /* are we 80486, Pentium, or.. */
1.104 uebayasi 304: END(cputype)
305: .type _C_LABEL(cpuid_level), @object
306: LABEL(cpuid_level) .long 0
307: END(cpuid_level)
308: .type _C_LABEL(atdevbase), @object
1.115 maxv 309: LABEL(atdevbase) .long 0 /* location of start of iomem in virt */
1.104 uebayasi 310: END(atdevbase)
311: .type _C_LABEL(lwp0uarea), @object
312: LABEL(lwp0uarea) .long 0
313: END(lwp0uarea)
314: .type _C_LABEL(PDPpaddr), @object
1.115 maxv 315: LABEL(PDPpaddr) .long 0 /* paddr of PDP, for libkvm */
1.104 uebayasi 316: END(PDPpaddr)
317: .type _C_LABEL(tablesize), @object
1.53 yamt 318: _C_LABEL(tablesize): .long 0
1.104 uebayasi 319: END(tablesize)
1.115 maxv 320:
321: /* Space for the temporary stack */
1.104 uebayasi 322: .size tmpstk, tmpstk - .
1.115 maxv 323: .space 512
1.1 fvdl 324: tmpstk:
1.96 cherry 325: #ifdef XEN
1.115 maxv 326: .align PAGE_SIZE, 0x0 /* Align on page boundary */
1.104 uebayasi 327: LABEL(tmpgdt)
1.115 maxv 328: .space PAGE_SIZE /* Xen expects a page */
1.104 uebayasi 329: END(tmpgdt)
1.96 cherry 330: #endif /* XEN */
1.1 fvdl 331:
332: .text
333: .globl _C_LABEL(kernel_text)
334: .set _C_LABEL(kernel_text),KERNTEXTOFF
335:
1.104 uebayasi 336: ENTRY(start)
1.62 bouyer 337: #ifndef XEN
1.115 maxv 338:
339: /* Warm boot */
340: movw $0x1234,0x472
341:
1.37 jmmv 342: #if defined(MULTIBOOT)
343: jmp 1f
344:
345: .align 4
346: .globl Multiboot_Header
347: _C_LABEL(Multiboot_Header):
1.44 jmmv 348: #define MULTIBOOT_HEADER_FLAGS (MULTIBOOT_HEADER_WANT_MEMORY)
1.37 jmmv 349: .long MULTIBOOT_HEADER_MAGIC
350: .long MULTIBOOT_HEADER_FLAGS
351: .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
352:
353: 1:
354: /* Check if we are being executed by a Multiboot-compliant boot
355: * loader. */
356: cmpl $MULTIBOOT_INFO_MAGIC,%eax
357: jne 1f
358:
1.43 mrg 359: /*
360: * Indeed, a multiboot-compliant boot loader executed us. We copy
1.37 jmmv 361: * the received Multiboot information structure into kernel's data
362: * space to process it later -- after we are relocated. It will
1.43 mrg 363: * be safer to run complex C code than doing it at this point.
364: */
1.115 maxv 365: pushl %ebx /* Address of Multiboot information */
1.37 jmmv 366: call _C_LABEL(multiboot_pre_reloc)
367: addl $4,%esp
1.38 jmmv 368: jmp 2f
1.37 jmmv 369: #endif
370:
371: 1:
1.1 fvdl 372: /*
1.38 jmmv 373: * At this point, we know that a NetBSD-specific boot loader
1.117 maxv 374: * booted this kernel.
375: *
376: * Load parameters from the stack (32 bits):
377: * boothowto, [bootdev], bootinfo, esym, biosextmem, biosbasemem
378: * We are not interested in 'bootdev'.
1.1 fvdl 379: */
1.117 maxv 380:
1.115 maxv 381: addl $4,%esp /* Discard return address to boot loader */
1.38 jmmv 382: call _C_LABEL(native_loader)
383: addl $24,%esp
1.1 fvdl 384:
385: 2:
386: /* First, reset the PSL. */
387: pushl $PSL_MBO
388: popfl
389:
390: /* Clear segment registers; always null in proc0. */
391: xorl %eax,%eax
392: movw %ax,%fs
393: movw %ax,%gs
394: decl %eax
1.73 ad 395: movl %eax,RELOC(cpuid_level)
1.1 fvdl 396:
397: /* Find out our CPU type. */
398:
399: try386: /* Try to toggle alignment check flag; does not exist on 386. */
400: pushfl
401: popl %eax
402: movl %eax,%ecx
403: orl $PSL_AC,%eax
404: pushl %eax
405: popfl
406: pushfl
407: popl %eax
408: xorl %ecx,%eax
409: andl $PSL_AC,%eax
410: pushl %ecx
411: popfl
412:
413: testl %eax,%eax
414: jnz try486
415:
416: /*
417: * Try the test of a NexGen CPU -- ZF will not change on a DIV
418: * instruction on a NexGen, it will on an i386. Documented in
419: * Nx586 Processor Recognition Application Note, NexGen, Inc.
420: */
421: movl $0x5555,%eax
422: xorl %edx,%edx
423: movl $2,%ecx
424: divl %ecx
425: jnz is386
426:
427: isnx586:
428: /*
429: * Don't try cpuid, as Nx586s reportedly don't support the
430: * PSL_ID bit.
431: */
1.100 chs 432: movl $CPU_NX586,RELOC(cputype)
1.1 fvdl 433: jmp 2f
434:
435: is386:
1.100 chs 436: movl $CPU_386,RELOC(cputype)
1.1 fvdl 437: jmp 2f
438:
439: try486: /* Try to toggle identification flag; does not exist on early 486s. */
440: pushfl
441: popl %eax
442: movl %eax,%ecx
443: xorl $PSL_ID,%eax
444: pushl %eax
445: popfl
446: pushfl
447: popl %eax
448: xorl %ecx,%eax
449: andl $PSL_ID,%eax
450: pushl %ecx
451: popfl
452:
453: testl %eax,%eax
454: jnz try586
1.100 chs 455: is486: movl $CPU_486,RELOC(cputype)
1.1 fvdl 456: /*
457: * Check Cyrix CPU
458: * Cyrix CPUs do not change the undefined flags following
459: * execution of the divide instruction which divides 5 by 2.
460: *
461: * Note: CPUID is enabled on M2, so it passes another way.
462: */
463: pushfl
464: movl $0x5555, %eax
465: xorl %edx, %edx
466: movl $2, %ecx
467: clc
468: divl %ecx
469: jnc trycyrix486
470: popfl
471: jmp 2f
472: trycyrix486:
1.115 maxv 473: movl $CPU_6x86,RELOC(cputype) /* set CPU type */
1.1 fvdl 474: /*
475: * Check for Cyrix 486 CPU by seeing if the flags change during a
476: * divide. This is documented in the Cx486SLC/e SMM Programmer's
477: * Guide.
478: */
479: xorl %edx,%edx
1.115 maxv 480: cmpl %edx,%edx /* set flags to known state */
1.1 fvdl 481: pushfl
1.115 maxv 482: popl %ecx /* store flags in ecx */
1.1 fvdl 483: movl $-1,%eax
484: movl $4,%ebx
1.115 maxv 485: divl %ebx /* do a long division */
1.1 fvdl 486: pushfl
487: popl %eax
1.115 maxv 488: xorl %ecx,%eax /* are the flags different? */
489: testl $0x8d5,%eax /* only check C|PF|AF|Z|N|V */
490: jne 2f /* yes; must be Cyrix 6x86 CPU */
491: movl $CPU_486DLC,RELOC(cputype) /* set CPU type */
1.1 fvdl 492:
493: #ifndef CYRIX_CACHE_WORKS
494: /* Disable caching of the ISA hole only. */
495: invd
1.115 maxv 496: movb $CCR0,%al /* Configuration Register index (CCR0) */
1.1 fvdl 497: outb %al,$0x22
498: inb $0x23,%al
499: orb $(CCR0_NC1|CCR0_BARB),%al
500: movb %al,%ah
501: movb $CCR0,%al
502: outb %al,$0x22
503: movb %ah,%al
504: outb %al,$0x23
505: invd
506: #else /* CYRIX_CACHE_WORKS */
507: /* Set cache parameters */
1.115 maxv 508: invd /* Start with guaranteed clean cache */
509: movb $CCR0,%al /* Configuration Register index (CCR0) */
1.1 fvdl 510: outb %al,$0x22
511: inb $0x23,%al
512: andb $~CCR0_NC0,%al
513: #ifndef CYRIX_CACHE_REALLY_WORKS
514: orb $(CCR0_NC1|CCR0_BARB),%al
515: #else
516: orb $CCR0_NC1,%al
517: #endif
518: movb %al,%ah
519: movb $CCR0,%al
520: outb %al,$0x22
521: movb %ah,%al
522: outb %al,$0x23
523: /* clear non-cacheable region 1 */
524: movb $(NCR1+2),%al
525: outb %al,$0x22
526: movb $NCR_SIZE_0K,%al
527: outb %al,$0x23
528: /* clear non-cacheable region 2 */
529: movb $(NCR2+2),%al
530: outb %al,$0x22
531: movb $NCR_SIZE_0K,%al
532: outb %al,$0x23
533: /* clear non-cacheable region 3 */
534: movb $(NCR3+2),%al
535: outb %al,$0x22
536: movb $NCR_SIZE_0K,%al
537: outb %al,$0x23
538: /* clear non-cacheable region 4 */
539: movb $(NCR4+2),%al
540: outb %al,$0x22
541: movb $NCR_SIZE_0K,%al
542: outb %al,$0x23
543: /* enable caching in CR0 */
544: movl %cr0,%eax
545: andl $~(CR0_CD|CR0_NW),%eax
546: movl %eax,%cr0
547: invd
548: #endif /* CYRIX_CACHE_WORKS */
549:
550: jmp 2f
551:
552: try586: /* Use the `cpuid' instruction. */
553: xorl %eax,%eax
554: cpuid
1.73 ad 555: movl %eax,RELOC(cpuid_level)
1.1 fvdl 556:
1.122 maxv 557: /*
558: * Retrieve the NX/XD flag. We use the 32bit version of PG_NX.
559: */
560: movl $0x80000001,%eax
561: cpuid
562: andl $CPUID_NOX,%edx
563: jz no_NOX
564: movl $PG_NX32,RELOC(nox_flag)
565: no_NOX:
566:
1.1 fvdl 567: 2:
568: /*
569: * Finished with old stack; load new %esp now instead of later so we
570: * can trace this code without having to worry about the trace trap
571: * clobbering the memory test or the zeroing of the bss+bootstrap page
572: * tables.
573: *
574: * The boot program should check:
575: * text+data <= &stack_variable - more_space_for_stack
576: * text+data+bss+pad+space_for_page_tables <= end_of_memory
1.115 maxv 577: *
578: * XXX: the gdt is in the carcass of the boot program so clearing
1.1 fvdl 579: * the rest of memory is still not possible.
580: */
1.115 maxv 581: movl $_RELOC(tmpstk),%esp
1.1 fvdl 582:
583: /*
1.117 maxv 584: * There are two different layouts possible, depending on whether PAE is
585: * enabled or not.
1.1 fvdl 586: *
1.117 maxv 587: * If PAE is not enabled, there are two levels of pages: PD -> PT. They will
588: * be referred to as: L2 -> L1. L2 is 1 page long. The BOOTSTRAP TABLES have
589: * the following layout:
590: * +-----+------------+----+
591: * | L2 -> PROC0 STK -> L1 |
592: * +-----+------------+----+
593: *
594: * If PAE is enabled, there are three levels of pages: PDP -> PD -> PT. They
595: * will be referred to as: L3 -> L2 -> L1. L3 is 1 page long, L2 is 4 page
596: * long. The BOOTSTRAP TABLES have the following layout:
597: * +-----+-----+------------+----+
598: * | L3 -> L2 -> PROC0 STK -> L1 |
599: * +-----+-----+------------+----+
600: *
601: * Virtual address space of the kernel in both cases:
602: * +------+--------+------+-----+--------+---------------------+-----------
603: * | TEXT | RODATA | DATA | BSS | [SYMS] | [PRELOADED MODULES] | BOOTSTRAP
604: * +------+--------+------+-----+--------+---------------------+-----------
605: * (1) (2) (3)
606: *
607: * -------+------------+
608: * TABLES | ISA IO MEM |
609: * -------+------------+
610: * (4)
1.93 jym 611: *
1.117 maxv 612: * PROC0 STK is obviously not linked as a page level. It just happens to be
613: * caught between L2 and L1.
1.123 ! maxv 614: *
! 615: * Important note: the kernel segments are properly 4k-aligned
! 616: * (see kern.ldscript), so there's no need to enforce alignment.
1.1 fvdl 617: */
618:
1.117 maxv 619: /* Find end of kernel image; brings us on (1). */
1.1 fvdl 620: movl $RELOC(end),%edi
1.71 ad 621:
1.114 uebayasi 622: #if (NKSYMS || defined(DDB) || defined(MODULAR)) && !defined(makeoptions_COPY_SYMTAB)
1.117 maxv 623: /* Save the symbols (if loaded); brinds us on (2). */
1.1 fvdl 624: movl RELOC(esym),%eax
625: testl %eax,%eax
626: jz 1f
1.35 yamt 627: subl $KERNBASE,%eax
1.1 fvdl 628: movl %eax,%edi
629: 1:
630: #endif
631:
1.117 maxv 632: /* Skip over any modules/blobs; brings us on (3). */
1.71 ad 633: movl RELOC(eblob),%eax
634: testl %eax,%eax
635: jz 1f
636: subl $KERNBASE,%eax
637: movl %eax,%edi
638: 1:
1.117 maxv 639:
640: /* We are on (3). Align up for BOOTSTRAP TABLES. */
1.71 ad 641: movl %edi,%esi
1.115 maxv 642: addl $PGOFSET,%esi
1.1 fvdl 643: andl $~PGOFSET,%esi
644:
1.53 yamt 645: /* nkptp[1] = (esi + ~L2_FRAME) >> L2_SHIFT + 1; */
646: movl %esi,%eax
647: addl $~L2_FRAME,%eax
648: shrl $L2_SHIFT,%eax
1.117 maxv 649: incl %eax /* one more PTP for VAs stolen by bootstrap */
1.53 yamt 650: 1: movl %eax,RELOC(nkptp)+1*4
651:
1.93 jym 652: /* tablesize = (PDP_SIZE + UPAGES + nkptp) << PGSHIFT; */
653: addl $(PDP_SIZE+UPAGES),%eax
654: #ifdef PAE
1.117 maxv 655: incl %eax /* one more page for L3 */
1.93 jym 656: shll $PGSHIFT+1,%eax /* PTP tables are twice larger with PAE */
657: #else
1.53 yamt 658: shll $PGSHIFT,%eax
1.93 jym 659: #endif
1.53 yamt 660: movl %eax,RELOC(tablesize)
661:
1.117 maxv 662: /* Ensure that nkptp covers BOOTSTRAP TABLES. */
1.53 yamt 663: addl %esi,%eax
664: addl $~L2_FRAME,%eax
665: shrl $L2_SHIFT,%eax
666: incl %eax
667: cmpl %eax,RELOC(nkptp)+1*4
668: jnz 1b
669:
1.117 maxv 670: /* Now, zero out the BOOTSTRAP TABLES (before filling them in). */
1.105 uebayasi 671: movl %esi,%edi
1.1 fvdl 672: xorl %eax,%eax
673: cld
1.105 uebayasi 674: movl RELOC(tablesize),%ecx
675: shrl $2,%ecx
1.1 fvdl 676: rep
1.117 maxv 677: stosl /* copy eax -> edi */
1.1 fvdl 678:
679: /*
1.117 maxv 680: * Build the page tables and levels. We go from L1 to L2/L3, and link the levels
681: * together. Note: RELOC computes &addr - KERNBASE in 32 bits; the value can't
682: * be > 4G, or we can't deal with it anyway, since we are in 32bit mode.
1.1 fvdl 683: */
1.105 uebayasi 684: /*
1.117 maxv 685: * Build L1.
1.105 uebayasi 686: */
1.117 maxv 687: leal (PROC0_PTP1_OFF)(%esi),%ebx
688:
689: /* Skip the first MB. */
1.118 maxv 690: movl $(KERNTEXTOFF - KERNBASE),%eax
1.1 fvdl 691: movl %eax,%ecx
1.117 maxv 692: shrl $(PGSHIFT-2),%ecx /* ((n >> PGSHIFT) << 2) for # PDEs */
1.93 jym 693: #ifdef PAE
1.117 maxv 694: shll $1,%ecx /* PDEs are twice larger with PAE */
1.93 jym 695: #endif
1.105 uebayasi 696: addl %ecx,%ebx
1.1 fvdl 697:
1.121 maxv 698: /* Map the kernel text RX. */
699: movl $RELOC(__rodata_start),%ecx
1.1 fvdl 700: subl %eax,%ecx
701: shrl $PGSHIFT,%ecx
702: orl $(PG_V|PG_KR),%eax
703: fillkpt
704:
1.121 maxv 705: /* Map the kernel rodata R. */
706: movl $RELOC(__rodata_start),%eax
707: movl $RELOC(__data_start),%ecx
708: subl %eax,%ecx
709: shrl $PGSHIFT,%ecx
710: orl $(PG_V|PG_KR),%eax
711: fillkpt_nox
712:
713: /* Map the kernel data+bss RW. */
714: movl $RELOC(__data_start),%eax
715: movl $RELOC(__kernel_end),%ecx
716: subl %eax,%ecx
1.1 fvdl 717: shrl $PGSHIFT,%ecx
1.121 maxv 718: orl $(PG_V|PG_KW),%eax
719: fillkpt_nox
720:
721: /*
722: * We actually have to be careful here. The memory layout is as
723: * follows:
724: * +----------+---------------------+------------------+
725: * | DATA+BSS < [PRELOADED MODULES] | BOOTSTRAP TABLES >
726: * +----------+---------------------+------------------+
727: * We just map everything from < to > with RWX rights.
728: */
729: movl $RELOC(__kernel_end),%eax
730: movl %esi,%ecx /* start of BOOTSTRAP TABLES */
731: addl RELOC(tablesize),%ecx /* end of BOOTSTRAP TABLES */
732: subl %eax,%ecx /* subtract end of kernel image */
733: shrl $PGSHIFT,%ecx
734: orl $(PG_V|PG_KW),%eax
1.1 fvdl 735: fillkpt
736:
1.123 ! maxv 737: /* We are on (4). Map ISA I/O mem (later atdevbase) RWX. */
1.115 maxv 738: movl $(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax
739: movl $(IOM_SIZE>>PGSHIFT),%ecx
1.1 fvdl 740: fillkpt
741:
1.117 maxv 742: /*
743: * Build L2 for identity mapping. Linked to L1.
744: */
1.105 uebayasi 745: leal (PROC0_PDIR_OFF)(%esi),%ebx
746: leal (PROC0_PTP1_OFF)(%esi),%eax
1.117 maxv 747: orl $(PG_V|PG_KW),%eax
1.105 uebayasi 748: movl RELOC(nkptp)+1*4,%ecx
1.1 fvdl 749: fillkpt
750:
1.117 maxv 751: /* Set up L2 entries for actual kernel mapping */
752: leal (PROC0_PDIR_OFF + L2_SLOT_KERNBASE * PDE_SIZE)(%esi),%ebx
1.105 uebayasi 753: leal (PROC0_PTP1_OFF)(%esi),%eax
1.117 maxv 754: orl $(PG_V|PG_KW),%eax
1.105 uebayasi 755: movl RELOC(nkptp)+1*4,%ecx
1.1 fvdl 756: fillkpt
757:
758: /* Install a PDE recursively mapping page directory as a page table! */
1.117 maxv 759: leal (PROC0_PDIR_OFF + PDIR_SLOT_PTE * PDE_SIZE)(%esi),%ebx
1.105 uebayasi 760: leal (PROC0_PDIR_OFF)(%esi),%eax
761: orl $(PG_V|PG_KW),%eax
1.93 jym 762: movl $PDP_SIZE,%ecx
763: fillkpt
764:
765: #ifdef PAE
1.117 maxv 766: /*
767: * Build L3. Linked to L2.
768: */
1.93 jym 769: leal (PROC0_L3_OFF)(%esi),%ebx
770: leal (PROC0_PDIR_OFF)(%esi),%eax
771: orl $(PG_V),%eax
772: movl $PDP_SIZE,%ecx
773: fillkpt
774:
775: /* Enable PAE mode */
776: movl %cr4,%eax
777: orl $CR4_PAE,%eax
778: movl %eax,%cr4
779: #endif
1.1 fvdl 780:
1.117 maxv 781: /* Save physical address of L2. */
1.93 jym 782: leal (PROC0_PDIR_OFF)(%esi),%eax
783: movl %eax,RELOC(PDPpaddr)
1.1 fvdl 784:
1.93 jym 785: /*
786: * Startup checklist:
1.117 maxv 787: * 1. Load %cr3 with pointer to L2 (or L3 for PAE).
1.93 jym 788: */
1.117 maxv 789: movl %esi,%eax
790: movl %eax,%cr3
1.93 jym 791:
1.105 uebayasi 792: /*
1.119 maxv 793: * 2. Set NOX in EFER, if available.
794: */
795: movl RELOC(nox_flag),%ebx
796: cmpl $0,%ebx
797: je skip_NOX
798: movl $MSR_EFER,%ecx
799: rdmsr
800: xorl %eax,%eax
801: orl $(EFER_NXE),%eax
802: wrmsr
803: skip_NOX:
804:
805: /*
806: * 3. Enable paging and the rest of it.
1.105 uebayasi 807: */
1.115 maxv 808: movl %cr0,%eax
1.111 dsl 809: orl $(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM),%eax
1.115 maxv 810: movl %eax,%cr0
1.1 fvdl 811:
1.115 maxv 812: pushl $begin /* jump to high mem */
1.1 fvdl 813: ret
814:
815: begin:
1.105 uebayasi 816: /*
1.117 maxv 817: * We have arrived. There's no need anymore for the identity mapping in
818: * low memory, remove it.
1.105 uebayasi 819: */
820: movl _C_LABEL(nkptp)+1*4,%ecx
1.115 maxv 821: leal (PROC0_PDIR_OFF)(%esi),%ebx /* old, phys address of PDIR */
822: addl $(KERNBASE), %ebx /* new, virt address of PDIR */
1.118 maxv 823: killkpt
1.1 fvdl 824:
825: /* Relocate atdevbase. */
1.53 yamt 826: movl $KERNBASE,%edx
827: addl _C_LABEL(tablesize),%edx
1.1 fvdl 828: addl %esi,%edx
829: movl %edx,_C_LABEL(atdevbase)
830:
831: /* Set up bootstrap stack. */
1.105 uebayasi 832: leal (PROC0_STK_OFF+KERNBASE)(%esi),%eax
1.91 rmind 833: movl %eax,_C_LABEL(lwp0uarea)
1.112 dsl 834: leal (USPACE-FRAMESIZE)(%eax),%esp
1.115 maxv 835: movl %esi,PCB_CR3(%eax) /* pcb->pcb_cr3 */
836: xorl %ebp,%ebp /* mark end of frames */
1.1 fvdl 837:
1.37 jmmv 838: #if defined(MULTIBOOT)
839: /* It is now safe to parse the Multiboot information structure
840: * we saved before from C code. Note that we cannot delay its
841: * parsing any more because initgdt (called below) needs to make
842: * use of this information. */
843: call _C_LABEL(multiboot_post_reloc)
844: #endif
845:
1.115 maxv 846: subl $NGDT*8, %esp /* space for temporary gdt */
1.1 fvdl 847: pushl %esp
848: call _C_LABEL(initgdt)
849: addl $4,%esp
1.30 junyoung 850:
1.105 uebayasi 851: movl _C_LABEL(tablesize),%eax
1.115 maxv 852: addl %esi,%eax /* skip past stack and page tables */
1.1 fvdl 853:
1.93 jym 854: #ifdef PAE
1.115 maxv 855: pushl $0 /* init386() expects a 64 bits paddr_t with PAE */
1.93 jym 856: #endif
1.1 fvdl 857: pushl %eax
1.115 maxv 858: call _C_LABEL(init386) /* wire 386 chip for unix operation */
859: addl $PDE_SIZE,%esp /* pop paddr_t */
860: addl $NGDT*8,%esp /* pop temporary gdt */
1.1 fvdl 861:
862: #ifdef SAFARI_FIFO_HACK
863: movb $5,%al
864: movw $0x37b,%dx
865: outb %al,%dx
866: movw $0x37f,%dx
867: inb %dx,%al
868: movb %al,%cl
869:
870: orb $1,%cl
871:
872: movb $5,%al
873: movw $0x37b,%dx
874: outb %al,%dx
875: movw $0x37f,%dx
876: movb %cl,%al
877: outb %al,%dx
878: #endif /* SAFARI_FIFO_HACK */
879:
880: call _C_LABEL(main)
1.62 bouyer 881: #else /* XEN */
882: /* First, reset the PSL. */
883: pushl $PSL_MBO
884: popfl
885:
886: cld
1.115 maxv 887: movl %esp, %ebx /* save start of available space */
888: movl $_RELOC(tmpstk),%esp /* bootstrap stack end location */
1.62 bouyer 889:
1.106 uebayasi 890: /* Clear BSS. */
1.62 bouyer 891: xorl %eax,%eax
892: movl $RELOC(__bss_start),%edi
893: movl $RELOC(_end),%ecx
894: subl %edi,%ecx
895: rep stosb
896:
897: /* Copy the necessary stuff from start_info structure. */
1.105 uebayasi 898: /* We need to copy shared_info early, so that sti/cli work */
1.62 bouyer 899: movl $RELOC(start_info_union),%edi
900: movl $128,%ecx
901: rep movsl
902:
903: /* Clear segment registers; always null in proc0. */
904: xorl %eax,%eax
905: movw %ax,%fs
906: movw %ax,%gs
907: decl %eax
1.73 ad 908: movl %eax,RELOC(cpuid_level)
1.62 bouyer 909:
910: xorl %eax,%eax
911: cpuid
1.73 ad 912: movl %eax,RELOC(cpuid_level)
1.62 bouyer 913:
1.96 cherry 914: /*
915: * Use a temp page. We'll re- add it to uvm(9) once we're
916: * done using it.
917: */
918: movl $RELOC(tmpgdt), %eax
1.115 maxv 919: pushl %eax /* start of temporary gdt */
1.96 cherry 920: call _C_LABEL(initgdt)
921: addl $4,%esp
922:
1.62 bouyer 923: call xen_pmap_bootstrap
1.92 jym 924:
1.62 bouyer 925: /*
926: * First avail returned by xen_pmap_bootstrap in %eax
927: */
928: movl %eax, %esi;
1.91 rmind 929: movl %esi, _C_LABEL(lwp0uarea)
1.62 bouyer 930:
931: /* Set up bootstrap stack. */
1.112 dsl 932: leal (USPACE-FRAMESIZE)(%eax),%esp
1.115 maxv 933: xorl %ebp,%ebp /* mark end of frames */
1.62 bouyer 934:
935: addl $USPACE, %esi
1.115 maxv 936: subl $KERNBASE, %esi /* init386 wants a physical address */
1.92 jym 937:
938: #ifdef PAE
1.115 maxv 939: pushl $0 /* init386() expects a 64 bits paddr_t with PAE */
1.92 jym 940: #endif
1.62 bouyer 941: pushl %esi
1.115 maxv 942: call _C_LABEL(init386) /* wire 386 chip for unix operation */
943: addl $PDE_SIZE,%esp /* pop paddr_t */
1.62 bouyer 944: call _C_LABEL(main)
1.106 uebayasi 945: #endif /* XEN */
1.104 uebayasi 946: END(start)
1.62 bouyer 947:
1.101 jym 948: #if defined(XEN)
1.62 bouyer 949: /* space for the hypercall call page */
950: #define HYPERCALL_PAGE_OFFSET 0x1000
951: .org HYPERCALL_PAGE_OFFSET
952: ENTRY(hypercall_page)
953: .skip 0x1000
1.104 uebayasi 954: END(hypercall_page)
1.62 bouyer 955:
956: /*
957: * void lgdt_finish(void);
958: * Finish load a new GDT pointer (do any necessary cleanup).
959: * XXX It's somewhat questionable whether reloading all the segment registers
960: * is necessary, since the actual descriptor data is not changed except by
961: * process creation and exit, both of which clean up via task switches. OTOH,
962: * this only happens at run time when the GDT is resized.
963: */
964: /* LINTSTUB: Func: void lgdt_finish(void) */
965: NENTRY(lgdt_finish)
966: movl $GSEL(GDATA_SEL, SEL_KPL),%eax
967: movw %ax,%ds
968: movw %ax,%es
969: movw %ax,%gs
970: movw %ax,%ss
971: movl $GSEL(GCPU_SEL, SEL_KPL),%eax
972: movw %ax,%fs
973: /* Reload code selector by doing intersegment return. */
974: popl %eax
975: pushl $GSEL(GCODE_SEL, SEL_KPL)
976: pushl %eax
977: lret
1.72 yamt 978: END(lgdt_finish)
1.62 bouyer 979:
980: #endif /* XEN */
1.1 fvdl 981:
982: /*
1.48 yamt 983: * void lwp_trampoline(void);
984: *
1.1 fvdl 985: * This is a trampoline function pushed onto the stack of a newly created
986: * process in order to do some additional setup. The trampoline is entered by
1.109 christos 987: * cpu_switchto()ing to the process, so we abuse the callee-saved
988: * registers used by cpu_switchto() to store the information about the
989: * stub to call.
1.1 fvdl 990: * NOTE: This function does not have a normal calling sequence!
991: */
1.48 yamt 992: NENTRY(lwp_trampoline)
1.68 ad 993: movl %ebp,%edi /* for .Lsyscall_checkast */
1.48 yamt 994: xorl %ebp,%ebp
1.68 ad 995: pushl %edi
1.48 yamt 996: pushl %eax
997: call _C_LABEL(lwp_startup)
998: addl $8,%esp
1.1 fvdl 999: pushl %ebx
1000: call *%esi
1001: addl $4,%esp
1.68 ad 1002: jmp .Lsyscall_checkast
1.1 fvdl 1003: /* NOTREACHED */
1.72 yamt 1004: END(lwp_trampoline)
1.1 fvdl 1005:
1006: /*
1.48 yamt 1007: * sigcode()
1008: *
1009: * Signal trampoline; copied to top of user stack. Used only for
1010: * compatibility with old releases of NetBSD.
1.1 fvdl 1011: */
1012: NENTRY(sigcode)
1013: /*
1014: * Handler has returned here as if we called it. The sigcontext
1015: * is on the stack after the 3 args "we" pushed.
1016: */
1.115 maxv 1017: leal 12(%esp),%eax /* get pointer to sigcontext */
1018: movl %eax,4(%esp) /* put it in the argument slot */
1019: /* fake return address already there */
1.17 christos 1020: movl $SYS_compat_16___sigreturn14,%eax
1.115 maxv 1021: int $0x80 /* enter kernel with args on stack */
1.1 fvdl 1022: movl $SYS_exit,%eax
1.115 maxv 1023: int $0x80 /* exit if sigreturn fails */
1.1 fvdl 1024: .globl _C_LABEL(esigcode)
1025: _C_LABEL(esigcode):
1.72 yamt 1026: END(sigcode)
1.1 fvdl 1027:
1028: /*
1.48 yamt 1029: * int setjmp(label_t *)
1030: *
1031: * Used primarily by DDB.
1.1 fvdl 1032: */
1033: ENTRY(setjmp)
1034: movl 4(%esp),%eax
1.115 maxv 1035: movl %ebx,(%eax) /* save ebx */
1036: movl %esp,4(%eax) /* save esp */
1037: movl %ebp,8(%eax) /* save ebp */
1038: movl %esi,12(%eax) /* save esi */
1039: movl %edi,16(%eax) /* save edi */
1040: movl (%esp),%edx /* get rta */
1041: movl %edx,20(%eax) /* save eip */
1042: xorl %eax,%eax /* return 0 */
1.1 fvdl 1043: ret
1.72 yamt 1044: END(setjmp)
1.1 fvdl 1045:
1.48 yamt 1046: /*
1047: * int longjmp(label_t *)
1048: *
1049: * Used primarily by DDB.
1050: */
1.1 fvdl 1051: ENTRY(longjmp)
1052: movl 4(%esp),%eax
1.115 maxv 1053: movl (%eax),%ebx /* restore ebx */
1054: movl 4(%eax),%esp /* restore esp */
1055: movl 8(%eax),%ebp /* restore ebp */
1056: movl 12(%eax),%esi /* restore esi */
1057: movl 16(%eax),%edi /* restore edi */
1058: movl 20(%eax),%edx /* get rta */
1059: movl %edx,(%esp) /* put in return frame */
1060: movl $1,%eax /* return 1 */
1.1 fvdl 1061: ret
1.72 yamt 1062: END(longjmp)
1.1 fvdl 1063:
1.65 skrll 1064: /*
1065: * void dumpsys(void)
1066: *
1067: * Mimic cpu_switchto() for postmortem debugging.
1068: */
1.64 skrll 1069: ENTRY(dumpsys)
1.115 maxv 1070: pushl %ebx /* set up fake switchframe */
1071: pushl %esi /* and save context */
1072: pushl %edi
1.65 skrll 1073: movl %esp,_C_LABEL(dumppcb)+PCB_ESP
1074: movl %ebp,_C_LABEL(dumppcb)+PCB_EBP
1.115 maxv 1075: call _C_LABEL(dodumpsys) /* dump! */
1076: addl $(3*4), %esp /* unwind switchframe */
1.64 skrll 1077: ret
1.72 yamt 1078: END(dumpsys)
1.64 skrll 1079:
1.1 fvdl 1080: /*
1.106 uebayasi 1081: * struct lwp *cpu_switchto(struct lwp *oldlwp, struct lwp *newlwp,
1.58 ad 1082: * bool returning)
1.30 junyoung 1083: *
1.48 yamt 1084: * 1. if (oldlwp != NULL), save its context.
1085: * 2. then, restore context of newlwp.
1086: *
1087: * Note that the stack frame layout is known to "struct switchframe" in
1088: * <machine/frame.h> and to the code in cpu_lwp_fork() which initializes
1.5 thorpej 1089: * it for a new lwp.
1.1 fvdl 1090: */
1.48 yamt 1091: ENTRY(cpu_switchto)
1.1 fvdl 1092: pushl %ebx
1093: pushl %esi
1094: pushl %edi
1095:
1.70 ad 1096: #if defined(DIAGNOSTIC) && !defined(XEN)
1097: cmpl $IPL_SCHED,CPUVAR(ILEVEL)
1098: jbe 0f
1.74 christos 1099: pushl CPUVAR(ILEVEL)
1.70 ad 1100: pushl $.Lstr
1101: call _C_LABEL(panic)
1.74 christos 1102: addl $8,%esp
1103: .Lstr: .string "cpu_switchto: switching above IPL_SCHED (%d)\0"
1.70 ad 1104: 0:
1105: #endif
1106:
1.115 maxv 1107: movl 16(%esp),%esi /* oldlwp */
1108: movl 20(%esp),%edi /* newlwp */
1109: movl 24(%esp),%edx /* returning */
1.48 yamt 1110: testl %esi,%esi
1111: jz 1f
1.1 fvdl 1112:
1.48 yamt 1113: /* Save old context. */
1.91 rmind 1114: movl L_PCB(%esi),%eax
1.48 yamt 1115: movl %esp,PCB_ESP(%eax)
1116: movl %ebp,PCB_EBP(%eax)
1117:
1118: /* Switch to newlwp's stack. */
1.91 rmind 1119: 1: movl L_PCB(%edi),%ebx
1.48 yamt 1120: movl PCB_EBP(%ebx),%ebp
1121: movl PCB_ESP(%ebx),%esp
1.1 fvdl 1122:
1.61 ad 1123: /*
1124: * Set curlwp. This must be globally visible in order to permit
1125: * non-interlocked mutex release.
1126: */
1127: movl %edi,%ecx
1128: xchgl %ecx,CPUVAR(CURLWP)
1.58 ad 1129:
1130: /* Skip the rest if returning to a pinned LWP. */
1131: testl %edx,%edx
1132: jnz 4f
1133:
1.62 bouyer 1134: #ifdef XEN
1135: pushl %edi
1136: call _C_LABEL(i386_switch_context)
1137: addl $4,%esp
1.97 bouyer 1138: #else /* !XEN */
1.60 yamt 1139: /* Switch ring0 esp */
1140: movl PCB_ESP0(%ebx),%eax
1141: movl %eax,CPUVAR(ESP0)
1.97 bouyer 1142: #endif /* !XEN */
1.1 fvdl 1143:
1144: /* Don't bother with the rest if switching to a system process. */
1.48 yamt 1145: testl $LW_SYSTEM,L_FLAG(%edi)
1146: jnz 4f
1.1 fvdl 1147:
1.97 bouyer 1148: #ifndef XEN
1.54 ad 1149: /* Restore thread-private %fs/%gs descriptors. */
1.60 yamt 1150: movl CPUVAR(GDT),%ecx
1.55 ad 1151: movl PCB_FSD(%ebx), %eax
1152: movl PCB_FSD+4(%ebx), %edx
1.54 ad 1153: movl %eax, (GUFS_SEL*8)(%ecx)
1154: movl %edx, (GUFS_SEL*8+4)(%ecx)
1.55 ad 1155: movl PCB_GSD(%ebx), %eax
1156: movl PCB_GSD+4(%ebx), %edx
1.54 ad 1157: movl %eax, (GUGS_SEL*8)(%ecx)
1158: movl %edx, (GUGS_SEL*8+4)(%ecx)
1.97 bouyer 1159: #endif /* !XEN */
1.54 ad 1160:
1.60 yamt 1161: /* Switch I/O bitmap */
1162: movl PCB_IOMAP(%ebx),%eax
1163: orl %eax,%eax
1.95 joerg 1164: jnz .Lcopy_iobitmap
1.60 yamt 1165: movl $(IOMAP_INVALOFF << 16),CPUVAR(IOBASE)
1166: .Liobitmap_done:
1167:
1.56 ad 1168: /* Is this process using RAS (restartable atomic sequences)? */
1169: movl L_PROC(%edi),%eax
1170: cmpl $0,P_RASLIST(%eax)
1171: jne 5f
1172:
1.48 yamt 1173: /*
1.90 rmind 1174: * Restore cr0 (including FPU state). Raise the IPL to IPL_HIGH.
1.48 yamt 1175: * FPU IPIs can alter the LWP's saved cr0. Dropping the priority
1176: * is deferred until mi_switch(), when cpu_switchto() returns.
1177: */
1.62 bouyer 1178: 2:
1.97 bouyer 1179: #ifdef XEN
1180: pushl %edi
1181: call _C_LABEL(i386_tls_switch)
1182: addl $4,%esp
1183: #else /* !XEN */
1.108 christos 1184: movl $IPL_HIGH,CPUVAR(ILEVEL)
1185: movl PCB_CR0(%ebx),%ecx /* has CR0_TS clear */
1.48 yamt 1186: movl %cr0,%edx
1.1 fvdl 1187:
1.108 christos 1188: /*
1189: * If our floating point registers are on a different CPU,
1190: * set CR0_TS so we'll trap rather than reuse bogus state.
1191: */
1192: cmpl CPUVAR(FPCURLWP),%edi
1193: je 3f
1194: orl $CR0_TS,%ecx
1195:
1.48 yamt 1196: /* Reloading CR0 is very expensive - avoid if possible. */
1.108 christos 1197: 3: cmpl %edx,%ecx
1.48 yamt 1198: je 4f
1.1 fvdl 1199: movl %ecx,%cr0
1.97 bouyer 1200: #endif /* !XEN */
1.1 fvdl 1201:
1.48 yamt 1202: /* Return to the new LWP, returning 'oldlwp' in %eax. */
1203: 4: movl %esi,%eax
1.1 fvdl 1204: popl %edi
1205: popl %esi
1206: popl %ebx
1207: ret
1.20 dsl 1208:
1.48 yamt 1209: /* Check for restartable atomic sequences (RAS). */
1210: 5: movl L_MD_REGS(%edi),%ecx
1211: pushl TF_EIP(%ecx)
1.20 dsl 1212: pushl %eax
1213: call _C_LABEL(ras_lookup)
1214: addl $8,%esp
1215: cmpl $-1,%eax
1.48 yamt 1216: je 2b
1.49 ad 1217: movl L_MD_REGS(%edi),%ecx
1218: movl %eax,TF_EIP(%ecx)
1.48 yamt 1219: jmp 2b
1.1 fvdl 1220:
1.60 yamt 1221: .Lcopy_iobitmap:
1222: /* Copy I/O bitmap. */
1.86 ad 1223: incl _C_LABEL(pmap_iobmp_evcnt)+EV_COUNT
1.60 yamt 1224: movl $(IOMAPSIZE/4),%ecx
1225: pushl %esi
1226: pushl %edi
1227: movl %eax,%esi /* pcb_iomap */
1228: movl CPUVAR(SELF),%edi
1229: leal CPU_INFO_IOMAP(%edi),%edi
1230: rep
1231: movsl
1232: popl %edi
1233: popl %esi
1234: movl $((CPU_INFO_IOMAP - CPU_INFO_TSS) << 16),CPUVAR(IOBASE)
1235: jmp .Liobitmap_done
1.72 yamt 1236: END(cpu_switchto)
1.60 yamt 1237:
1.1 fvdl 1238: /*
1239: * void savectx(struct pcb *pcb);
1.48 yamt 1240: *
1.1 fvdl 1241: * Update pcb, saving current processor state.
1242: */
1243: ENTRY(savectx)
1.115 maxv 1244: movl 4(%esp),%edx /* edx = pcb */
1.1 fvdl 1245: movl %esp,PCB_ESP(%edx)
1246: movl %ebp,PCB_EBP(%edx)
1247: ret
1.72 yamt 1248: END(savectx)
1.1 fvdl 1249:
1250: /*
1.48 yamt 1251: * osyscall()
1252: *
1.1 fvdl 1253: * Old call gate entry for syscall
1254: */
1255: IDTVEC(osyscall)
1.87 ad 1256: #ifndef XEN
1257: /* XXX we are in trouble! interrupts be off here. */
1.115 maxv 1258: cli /* must be first instruction */
1.87 ad 1259: #endif
1.115 maxv 1260: pushfl /* set eflags in trap frame */
1.1 fvdl 1261: popl 8(%esp)
1.115 maxv 1262: orl $PSL_I,8(%esp) /* re-enable ints on return to user */
1263: pushl $7 /* size of instruction for restart */
1.1 fvdl 1264: jmp syscall1
1.72 yamt 1265: IDTVEC_END(osyscall)
1.1 fvdl 1266:
1267: /*
1.48 yamt 1268: * syscall()
1269: *
1.1 fvdl 1270: * Trap gate entry for syscall
1271: */
1272: IDTVEC(syscall)
1.115 maxv 1273: pushl $2 /* size of instruction for restart */
1.1 fvdl 1274: syscall1:
1.115 maxv 1275: pushl $T_ASTFLT /* trap # for doing ASTs */
1.1 fvdl 1276: INTRENTRY
1.85 ad 1277: STI(%eax)
1.1 fvdl 1278: #ifdef DIAGNOSTIC
1279: movl CPUVAR(ILEVEL),%ebx
1280: testl %ebx,%ebx
1281: jz 1f
1282: pushl $5f
1.84 ad 1283: call _C_LABEL(panic)
1.1 fvdl 1284: addl $4,%esp
1285: #ifdef DDB
1286: int $3
1287: #endif
1.30 junyoung 1288: 1:
1.1 fvdl 1289: #endif /* DIAGNOSTIC */
1.115 maxv 1290: addl $1,CPUVAR(NSYSCALL) /* count it atomically */
1291: adcl $0,CPUVAR(NSYSCALL)+4 /* count it atomically */
1.68 ad 1292: movl CPUVAR(CURLWP),%edi
1293: movl L_PROC(%edi),%edx
1.115 maxv 1294: movl %esp,L_MD_REGS(%edi) /* save pointer to frame */
1.15 fvdl 1295: pushl %esp
1.115 maxv 1296: call *P_MD_SYSCALL(%edx) /* get pointer to syscall() function */
1.15 fvdl 1297: addl $4,%esp
1.27 yamt 1298: .Lsyscall_checkast:
1.24 yamt 1299: /* Check for ASTs on exit to user mode. */
1.62 bouyer 1300: CLI(%eax)
1.68 ad 1301: movl L_MD_ASTPENDING(%edi), %eax
1302: orl CPUVAR(WANT_PMAPLOAD), %eax
1.24 yamt 1303: jnz 9f
1.62 bouyer 1304: #ifdef XEN
1305: STIC(%eax)
1306: jz 14f
1307: call _C_LABEL(stipending)
1308: testl %eax,%eax
1309: jz 14f
1310: /* process pending interrupts */
1311: CLI(%eax)
1312: movl CPUVAR(ILEVEL), %ebx
1.115 maxv 1313: movl $.Lsyscall_resume, %esi /* address to resume loop at */
1.62 bouyer 1314: .Lsyscall_resume:
1.115 maxv 1315: movl %ebx,%eax /* get cpl */
1.62 bouyer 1316: movl CPUVAR(IUNMASK)(,%eax,4),%eax
1.115 maxv 1317: andl CPUVAR(IPENDING),%eax /* any non-masked bits left? */
1.62 bouyer 1318: jz 17f
1319: bsrl %eax,%eax
1320: btrl %eax,CPUVAR(IPENDING)
1321: movl CPUVAR(ISOURCES)(,%eax,4),%eax
1322: jmp *IS_RESUME(%eax)
1.115 maxv 1323: 17: movl %ebx, CPUVAR(ILEVEL) /* restore cpl */
1.62 bouyer 1324: jmp .Lsyscall_checkast
1325: 14:
1326: #endif /* XEN */
1.1 fvdl 1327: #ifndef DIAGNOSTIC
1.24 yamt 1328: INTRFASTEXIT
1.1 fvdl 1329: #else /* DIAGNOSTIC */
1.24 yamt 1330: cmpl $IPL_NONE,CPUVAR(ILEVEL)
1.1 fvdl 1331: jne 3f
1332: INTRFASTEXIT
1.62 bouyer 1333: 3: STI(%eax)
1.1 fvdl 1334: pushl $4f
1.84 ad 1335: call _C_LABEL(panic)
1.1 fvdl 1336: addl $4,%esp
1.58 ad 1337: pushl $IPL_NONE
1338: call _C_LABEL(spllower)
1339: addl $4,%esp
1.53 yamt 1340: jmp .Lsyscall_checkast
1.84 ad 1341: 4: .asciz "SPL NOT LOWERED ON SYSCALL EXIT\n"
1342: 5: .asciz "SPL NOT ZERO ON SYSCALL ENTRY\n"
1.1 fvdl 1343: #endif /* DIAGNOSTIC */
1.68 ad 1344: 9:
1345: cmpl $0, CPUVAR(WANT_PMAPLOAD)
1346: jz 10f
1347: STI(%eax)
1.24 yamt 1348: call _C_LABEL(pmap_load)
1.27 yamt 1349: jmp .Lsyscall_checkast /* re-check ASTs */
1.68 ad 1350: 10:
1351: /* Always returning to user mode here. */
1352: movl $0, L_MD_ASTPENDING(%edi)
1353: STI(%eax)
1354: /* Pushed T_ASTFLT into tf_trapno on entry. */
1355: pushl %esp
1356: call _C_LABEL(trap)
1357: addl $4,%esp
1358: jmp .Lsyscall_checkast /* re-check ASTs */
1.72 yamt 1359: IDTVEC_END(syscall)
1.1 fvdl 1360:
1.81 ad 1361: IDTVEC(svr4_fasttrap)
1.115 maxv 1362: pushl $2 /* size of instruction for restart */
1363: pushl $T_ASTFLT /* trap # for doing ASTs */
1.81 ad 1364: INTRENTRY
1.85 ad 1365: STI(%eax)
1.81 ad 1366: pushl $RW_READER
1367: pushl $_C_LABEL(svr4_fasttrap_lock)
1368: call _C_LABEL(rw_enter)
1369: addl $8,%esp
1370: call *_C_LABEL(svr4_fasttrap_vec)
1371: pushl $_C_LABEL(svr4_fasttrap_lock)
1372: call _C_LABEL(rw_exit)
1373: addl $4,%esp
1374: 2: /* Check for ASTs on exit to user mode. */
1375: cli
1.115 maxv 1376: CHECK_ASTPENDING(%eax)
1.81 ad 1377: je 1f
1378: /* Always returning to user mode here. */
1379: CLEAR_ASTPENDING(%eax)
1380: sti
1381: /* Pushed T_ASTFLT into tf_trapno on entry. */
1382: pushl %esp
1383: call _C_LABEL(trap)
1384: addl $4,%esp
1385: jmp 2b
1386: 1: CHECK_DEFERRED_SWITCH
1387: jnz 9f
1388: INTRFASTEXIT
1389: 9: sti
1390: call _C_LABEL(pmap_load)
1391: cli
1392: jmp 2b
1393:
1.48 yamt 1394: /*
1395: * int npx586bug1(int a, int b)
1.110 dsl 1396: * Used when checking for the FDIV bug on first generations pentiums.
1397: * Anything 120MHz or above is fine.
1.48 yamt 1398: */
1.1 fvdl 1399: NENTRY(npx586bug1)
1.115 maxv 1400: fildl 4(%esp) /* x */
1401: fildl 8(%esp) /* y */
1.1 fvdl 1402: fld %st(1)
1.115 maxv 1403: fdiv %st(1),%st /* x/y */
1404: fmulp %st,%st(1) /* (x/y)*y */
1405: fsubrp %st,%st(1) /* x-(x/y)*y */
1.1 fvdl 1406: pushl $0
1407: fistpl (%esp)
1408: popl %eax
1409: ret
1.72 yamt 1410: END(npx586bug1)
1.50 ad 1411:
1412: /*
1.76 ad 1413: * void sse2_idlezero_page(void *pg)
1.50 ad 1414: *
1.76 ad 1415: * Zero a page without polluting the cache. Preemption must be
1416: * disabled by the caller. Abort if a preemption is pending.
1.50 ad 1417: */
1.76 ad 1418: ENTRY(sse2_idlezero_page)
1.50 ad 1419: pushl %ebp
1420: movl %esp,%ebp
1421: movl 8(%esp), %edx
1.76 ad 1422: movl $(PAGE_SIZE/32), %ecx
1.50 ad 1423: xorl %eax, %eax
1424: .align 16
1425: 1:
1.82 ad 1426: testl $RESCHED_KPREEMPT, CPUVAR(RESCHED)
1.76 ad 1427: jnz 2f
1.50 ad 1428: movnti %eax, 0(%edx)
1429: movnti %eax, 4(%edx)
1430: movnti %eax, 8(%edx)
1431: movnti %eax, 12(%edx)
1432: movnti %eax, 16(%edx)
1433: movnti %eax, 20(%edx)
1434: movnti %eax, 24(%edx)
1435: movnti %eax, 28(%edx)
1.76 ad 1436: addl $32, %edx
1.75 ad 1437: decl %ecx
1.50 ad 1438: jnz 1b
1439: sfence
1.76 ad 1440: incl %eax
1.50 ad 1441: pop %ebp
1442: ret
1.76 ad 1443: 2:
1.50 ad 1444: sfence
1445: popl %ebp
1446: ret
1.76 ad 1447: END(sse2_idlezero_page)
CVSweb <webmaster@jp.NetBSD.org>