Annotation of src/sys/arch/i386/i386/locore.S, Revision 1.124
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:
1.124 ! maxv 721: /* Map [SYMS]+[PRELOADED MODULES] RWX. */
1.121 maxv 722: movl $RELOC(__kernel_end),%eax
723: movl %esi,%ecx /* start of BOOTSTRAP TABLES */
1.124 ! maxv 724: subl %eax,%ecx
1.121 maxv 725: shrl $PGSHIFT,%ecx
726: orl $(PG_V|PG_KW),%eax
1.1 fvdl 727: fillkpt
728:
1.124 ! maxv 729: /* Map the BOOTSTRAP TABLES RW. */
! 730: movl %esi,%eax /* start of BOOTSTRAP TABLES */
! 731: movl RELOC(tablesize),%ecx /* length of BOOTSTRAP TABLES */
! 732: shrl $PGSHIFT,%ecx
! 733: orl $(PG_V|PG_KW),%eax
! 734: fillkpt_nox
! 735:
1.123 maxv 736: /* We are on (4). Map ISA I/O mem (later atdevbase) RWX. */
1.115 maxv 737: movl $(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax
738: movl $(IOM_SIZE>>PGSHIFT),%ecx
1.1 fvdl 739: fillkpt
740:
1.117 maxv 741: /*
742: * Build L2 for identity mapping. Linked to L1.
743: */
1.105 uebayasi 744: leal (PROC0_PDIR_OFF)(%esi),%ebx
745: leal (PROC0_PTP1_OFF)(%esi),%eax
1.117 maxv 746: orl $(PG_V|PG_KW),%eax
1.105 uebayasi 747: movl RELOC(nkptp)+1*4,%ecx
1.1 fvdl 748: fillkpt
749:
1.117 maxv 750: /* Set up L2 entries for actual kernel mapping */
751: leal (PROC0_PDIR_OFF + L2_SLOT_KERNBASE * PDE_SIZE)(%esi),%ebx
1.105 uebayasi 752: leal (PROC0_PTP1_OFF)(%esi),%eax
1.117 maxv 753: orl $(PG_V|PG_KW),%eax
1.105 uebayasi 754: movl RELOC(nkptp)+1*4,%ecx
1.1 fvdl 755: fillkpt
756:
757: /* Install a PDE recursively mapping page directory as a page table! */
1.117 maxv 758: leal (PROC0_PDIR_OFF + PDIR_SLOT_PTE * PDE_SIZE)(%esi),%ebx
1.105 uebayasi 759: leal (PROC0_PDIR_OFF)(%esi),%eax
760: orl $(PG_V|PG_KW),%eax
1.93 jym 761: movl $PDP_SIZE,%ecx
762: fillkpt
763:
764: #ifdef PAE
1.117 maxv 765: /*
766: * Build L3. Linked to L2.
767: */
1.93 jym 768: leal (PROC0_L3_OFF)(%esi),%ebx
769: leal (PROC0_PDIR_OFF)(%esi),%eax
770: orl $(PG_V),%eax
771: movl $PDP_SIZE,%ecx
772: fillkpt
773:
774: /* Enable PAE mode */
775: movl %cr4,%eax
776: orl $CR4_PAE,%eax
777: movl %eax,%cr4
778: #endif
1.1 fvdl 779:
1.117 maxv 780: /* Save physical address of L2. */
1.93 jym 781: leal (PROC0_PDIR_OFF)(%esi),%eax
782: movl %eax,RELOC(PDPpaddr)
1.1 fvdl 783:
1.93 jym 784: /*
785: * Startup checklist:
1.117 maxv 786: * 1. Load %cr3 with pointer to L2 (or L3 for PAE).
1.93 jym 787: */
1.117 maxv 788: movl %esi,%eax
789: movl %eax,%cr3
1.93 jym 790:
1.105 uebayasi 791: /*
1.119 maxv 792: * 2. Set NOX in EFER, if available.
793: */
794: movl RELOC(nox_flag),%ebx
795: cmpl $0,%ebx
796: je skip_NOX
797: movl $MSR_EFER,%ecx
798: rdmsr
799: xorl %eax,%eax
800: orl $(EFER_NXE),%eax
801: wrmsr
802: skip_NOX:
803:
804: /*
805: * 3. Enable paging and the rest of it.
1.105 uebayasi 806: */
1.115 maxv 807: movl %cr0,%eax
1.111 dsl 808: orl $(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM),%eax
1.115 maxv 809: movl %eax,%cr0
1.1 fvdl 810:
1.115 maxv 811: pushl $begin /* jump to high mem */
1.1 fvdl 812: ret
813:
814: begin:
1.105 uebayasi 815: /*
1.117 maxv 816: * We have arrived. There's no need anymore for the identity mapping in
817: * low memory, remove it.
1.105 uebayasi 818: */
819: movl _C_LABEL(nkptp)+1*4,%ecx
1.115 maxv 820: leal (PROC0_PDIR_OFF)(%esi),%ebx /* old, phys address of PDIR */
821: addl $(KERNBASE), %ebx /* new, virt address of PDIR */
1.118 maxv 822: killkpt
1.1 fvdl 823:
824: /* Relocate atdevbase. */
1.53 yamt 825: movl $KERNBASE,%edx
826: addl _C_LABEL(tablesize),%edx
1.1 fvdl 827: addl %esi,%edx
828: movl %edx,_C_LABEL(atdevbase)
829:
830: /* Set up bootstrap stack. */
1.105 uebayasi 831: leal (PROC0_STK_OFF+KERNBASE)(%esi),%eax
1.91 rmind 832: movl %eax,_C_LABEL(lwp0uarea)
1.112 dsl 833: leal (USPACE-FRAMESIZE)(%eax),%esp
1.115 maxv 834: movl %esi,PCB_CR3(%eax) /* pcb->pcb_cr3 */
835: xorl %ebp,%ebp /* mark end of frames */
1.1 fvdl 836:
1.37 jmmv 837: #if defined(MULTIBOOT)
838: /* It is now safe to parse the Multiboot information structure
839: * we saved before from C code. Note that we cannot delay its
840: * parsing any more because initgdt (called below) needs to make
841: * use of this information. */
842: call _C_LABEL(multiboot_post_reloc)
843: #endif
844:
1.115 maxv 845: subl $NGDT*8, %esp /* space for temporary gdt */
1.1 fvdl 846: pushl %esp
847: call _C_LABEL(initgdt)
848: addl $4,%esp
1.30 junyoung 849:
1.105 uebayasi 850: movl _C_LABEL(tablesize),%eax
1.115 maxv 851: addl %esi,%eax /* skip past stack and page tables */
1.1 fvdl 852:
1.93 jym 853: #ifdef PAE
1.115 maxv 854: pushl $0 /* init386() expects a 64 bits paddr_t with PAE */
1.93 jym 855: #endif
1.1 fvdl 856: pushl %eax
1.115 maxv 857: call _C_LABEL(init386) /* wire 386 chip for unix operation */
858: addl $PDE_SIZE,%esp /* pop paddr_t */
859: addl $NGDT*8,%esp /* pop temporary gdt */
1.1 fvdl 860:
861: #ifdef SAFARI_FIFO_HACK
862: movb $5,%al
863: movw $0x37b,%dx
864: outb %al,%dx
865: movw $0x37f,%dx
866: inb %dx,%al
867: movb %al,%cl
868:
869: orb $1,%cl
870:
871: movb $5,%al
872: movw $0x37b,%dx
873: outb %al,%dx
874: movw $0x37f,%dx
875: movb %cl,%al
876: outb %al,%dx
877: #endif /* SAFARI_FIFO_HACK */
878:
879: call _C_LABEL(main)
1.62 bouyer 880: #else /* XEN */
881: /* First, reset the PSL. */
882: pushl $PSL_MBO
883: popfl
884:
885: cld
1.115 maxv 886: movl %esp, %ebx /* save start of available space */
887: movl $_RELOC(tmpstk),%esp /* bootstrap stack end location */
1.62 bouyer 888:
1.106 uebayasi 889: /* Clear BSS. */
1.62 bouyer 890: xorl %eax,%eax
891: movl $RELOC(__bss_start),%edi
892: movl $RELOC(_end),%ecx
893: subl %edi,%ecx
894: rep stosb
895:
896: /* Copy the necessary stuff from start_info structure. */
1.105 uebayasi 897: /* We need to copy shared_info early, so that sti/cli work */
1.62 bouyer 898: movl $RELOC(start_info_union),%edi
899: movl $128,%ecx
900: rep movsl
901:
902: /* Clear segment registers; always null in proc0. */
903: xorl %eax,%eax
904: movw %ax,%fs
905: movw %ax,%gs
906: decl %eax
1.73 ad 907: movl %eax,RELOC(cpuid_level)
1.62 bouyer 908:
909: xorl %eax,%eax
910: cpuid
1.73 ad 911: movl %eax,RELOC(cpuid_level)
1.62 bouyer 912:
1.96 cherry 913: /*
914: * Use a temp page. We'll re- add it to uvm(9) once we're
915: * done using it.
916: */
917: movl $RELOC(tmpgdt), %eax
1.115 maxv 918: pushl %eax /* start of temporary gdt */
1.96 cherry 919: call _C_LABEL(initgdt)
920: addl $4,%esp
921:
1.62 bouyer 922: call xen_pmap_bootstrap
1.92 jym 923:
1.62 bouyer 924: /*
925: * First avail returned by xen_pmap_bootstrap in %eax
926: */
927: movl %eax, %esi;
1.91 rmind 928: movl %esi, _C_LABEL(lwp0uarea)
1.62 bouyer 929:
930: /* Set up bootstrap stack. */
1.112 dsl 931: leal (USPACE-FRAMESIZE)(%eax),%esp
1.115 maxv 932: xorl %ebp,%ebp /* mark end of frames */
1.62 bouyer 933:
934: addl $USPACE, %esi
1.115 maxv 935: subl $KERNBASE, %esi /* init386 wants a physical address */
1.92 jym 936:
937: #ifdef PAE
1.115 maxv 938: pushl $0 /* init386() expects a 64 bits paddr_t with PAE */
1.92 jym 939: #endif
1.62 bouyer 940: pushl %esi
1.115 maxv 941: call _C_LABEL(init386) /* wire 386 chip for unix operation */
942: addl $PDE_SIZE,%esp /* pop paddr_t */
1.62 bouyer 943: call _C_LABEL(main)
1.106 uebayasi 944: #endif /* XEN */
1.104 uebayasi 945: END(start)
1.62 bouyer 946:
1.101 jym 947: #if defined(XEN)
1.62 bouyer 948: /* space for the hypercall call page */
949: #define HYPERCALL_PAGE_OFFSET 0x1000
950: .org HYPERCALL_PAGE_OFFSET
951: ENTRY(hypercall_page)
952: .skip 0x1000
1.104 uebayasi 953: END(hypercall_page)
1.62 bouyer 954:
955: /*
956: * void lgdt_finish(void);
957: * Finish load a new GDT pointer (do any necessary cleanup).
958: * XXX It's somewhat questionable whether reloading all the segment registers
959: * is necessary, since the actual descriptor data is not changed except by
960: * process creation and exit, both of which clean up via task switches. OTOH,
961: * this only happens at run time when the GDT is resized.
962: */
963: /* LINTSTUB: Func: void lgdt_finish(void) */
964: NENTRY(lgdt_finish)
965: movl $GSEL(GDATA_SEL, SEL_KPL),%eax
966: movw %ax,%ds
967: movw %ax,%es
968: movw %ax,%gs
969: movw %ax,%ss
970: movl $GSEL(GCPU_SEL, SEL_KPL),%eax
971: movw %ax,%fs
972: /* Reload code selector by doing intersegment return. */
973: popl %eax
974: pushl $GSEL(GCODE_SEL, SEL_KPL)
975: pushl %eax
976: lret
1.72 yamt 977: END(lgdt_finish)
1.62 bouyer 978:
979: #endif /* XEN */
1.1 fvdl 980:
981: /*
1.48 yamt 982: * void lwp_trampoline(void);
983: *
1.1 fvdl 984: * This is a trampoline function pushed onto the stack of a newly created
985: * process in order to do some additional setup. The trampoline is entered by
1.109 christos 986: * cpu_switchto()ing to the process, so we abuse the callee-saved
987: * registers used by cpu_switchto() to store the information about the
988: * stub to call.
1.1 fvdl 989: * NOTE: This function does not have a normal calling sequence!
990: */
1.48 yamt 991: NENTRY(lwp_trampoline)
1.68 ad 992: movl %ebp,%edi /* for .Lsyscall_checkast */
1.48 yamt 993: xorl %ebp,%ebp
1.68 ad 994: pushl %edi
1.48 yamt 995: pushl %eax
996: call _C_LABEL(lwp_startup)
997: addl $8,%esp
1.1 fvdl 998: pushl %ebx
999: call *%esi
1000: addl $4,%esp
1.68 ad 1001: jmp .Lsyscall_checkast
1.1 fvdl 1002: /* NOTREACHED */
1.72 yamt 1003: END(lwp_trampoline)
1.1 fvdl 1004:
1005: /*
1.48 yamt 1006: * sigcode()
1007: *
1008: * Signal trampoline; copied to top of user stack. Used only for
1009: * compatibility with old releases of NetBSD.
1.1 fvdl 1010: */
1011: NENTRY(sigcode)
1012: /*
1013: * Handler has returned here as if we called it. The sigcontext
1014: * is on the stack after the 3 args "we" pushed.
1015: */
1.115 maxv 1016: leal 12(%esp),%eax /* get pointer to sigcontext */
1017: movl %eax,4(%esp) /* put it in the argument slot */
1018: /* fake return address already there */
1.17 christos 1019: movl $SYS_compat_16___sigreturn14,%eax
1.115 maxv 1020: int $0x80 /* enter kernel with args on stack */
1.1 fvdl 1021: movl $SYS_exit,%eax
1.115 maxv 1022: int $0x80 /* exit if sigreturn fails */
1.1 fvdl 1023: .globl _C_LABEL(esigcode)
1024: _C_LABEL(esigcode):
1.72 yamt 1025: END(sigcode)
1.1 fvdl 1026:
1027: /*
1.48 yamt 1028: * int setjmp(label_t *)
1029: *
1030: * Used primarily by DDB.
1.1 fvdl 1031: */
1032: ENTRY(setjmp)
1033: movl 4(%esp),%eax
1.115 maxv 1034: movl %ebx,(%eax) /* save ebx */
1035: movl %esp,4(%eax) /* save esp */
1036: movl %ebp,8(%eax) /* save ebp */
1037: movl %esi,12(%eax) /* save esi */
1038: movl %edi,16(%eax) /* save edi */
1039: movl (%esp),%edx /* get rta */
1040: movl %edx,20(%eax) /* save eip */
1041: xorl %eax,%eax /* return 0 */
1.1 fvdl 1042: ret
1.72 yamt 1043: END(setjmp)
1.1 fvdl 1044:
1.48 yamt 1045: /*
1046: * int longjmp(label_t *)
1047: *
1048: * Used primarily by DDB.
1049: */
1.1 fvdl 1050: ENTRY(longjmp)
1051: movl 4(%esp),%eax
1.115 maxv 1052: movl (%eax),%ebx /* restore ebx */
1053: movl 4(%eax),%esp /* restore esp */
1054: movl 8(%eax),%ebp /* restore ebp */
1055: movl 12(%eax),%esi /* restore esi */
1056: movl 16(%eax),%edi /* restore edi */
1057: movl 20(%eax),%edx /* get rta */
1058: movl %edx,(%esp) /* put in return frame */
1059: movl $1,%eax /* return 1 */
1.1 fvdl 1060: ret
1.72 yamt 1061: END(longjmp)
1.1 fvdl 1062:
1.65 skrll 1063: /*
1064: * void dumpsys(void)
1065: *
1066: * Mimic cpu_switchto() for postmortem debugging.
1067: */
1.64 skrll 1068: ENTRY(dumpsys)
1.115 maxv 1069: pushl %ebx /* set up fake switchframe */
1070: pushl %esi /* and save context */
1071: pushl %edi
1.65 skrll 1072: movl %esp,_C_LABEL(dumppcb)+PCB_ESP
1073: movl %ebp,_C_LABEL(dumppcb)+PCB_EBP
1.115 maxv 1074: call _C_LABEL(dodumpsys) /* dump! */
1075: addl $(3*4), %esp /* unwind switchframe */
1.64 skrll 1076: ret
1.72 yamt 1077: END(dumpsys)
1.64 skrll 1078:
1.1 fvdl 1079: /*
1.106 uebayasi 1080: * struct lwp *cpu_switchto(struct lwp *oldlwp, struct lwp *newlwp,
1.58 ad 1081: * bool returning)
1.30 junyoung 1082: *
1.48 yamt 1083: * 1. if (oldlwp != NULL), save its context.
1084: * 2. then, restore context of newlwp.
1085: *
1086: * Note that the stack frame layout is known to "struct switchframe" in
1087: * <machine/frame.h> and to the code in cpu_lwp_fork() which initializes
1.5 thorpej 1088: * it for a new lwp.
1.1 fvdl 1089: */
1.48 yamt 1090: ENTRY(cpu_switchto)
1.1 fvdl 1091: pushl %ebx
1092: pushl %esi
1093: pushl %edi
1094:
1.70 ad 1095: #if defined(DIAGNOSTIC) && !defined(XEN)
1096: cmpl $IPL_SCHED,CPUVAR(ILEVEL)
1097: jbe 0f
1.74 christos 1098: pushl CPUVAR(ILEVEL)
1.70 ad 1099: pushl $.Lstr
1100: call _C_LABEL(panic)
1.74 christos 1101: addl $8,%esp
1102: .Lstr: .string "cpu_switchto: switching above IPL_SCHED (%d)\0"
1.70 ad 1103: 0:
1104: #endif
1105:
1.115 maxv 1106: movl 16(%esp),%esi /* oldlwp */
1107: movl 20(%esp),%edi /* newlwp */
1108: movl 24(%esp),%edx /* returning */
1.48 yamt 1109: testl %esi,%esi
1110: jz 1f
1.1 fvdl 1111:
1.48 yamt 1112: /* Save old context. */
1.91 rmind 1113: movl L_PCB(%esi),%eax
1.48 yamt 1114: movl %esp,PCB_ESP(%eax)
1115: movl %ebp,PCB_EBP(%eax)
1116:
1117: /* Switch to newlwp's stack. */
1.91 rmind 1118: 1: movl L_PCB(%edi),%ebx
1.48 yamt 1119: movl PCB_EBP(%ebx),%ebp
1120: movl PCB_ESP(%ebx),%esp
1.1 fvdl 1121:
1.61 ad 1122: /*
1123: * Set curlwp. This must be globally visible in order to permit
1124: * non-interlocked mutex release.
1125: */
1126: movl %edi,%ecx
1127: xchgl %ecx,CPUVAR(CURLWP)
1.58 ad 1128:
1129: /* Skip the rest if returning to a pinned LWP. */
1130: testl %edx,%edx
1131: jnz 4f
1132:
1.62 bouyer 1133: #ifdef XEN
1134: pushl %edi
1135: call _C_LABEL(i386_switch_context)
1136: addl $4,%esp
1.97 bouyer 1137: #else /* !XEN */
1.60 yamt 1138: /* Switch ring0 esp */
1139: movl PCB_ESP0(%ebx),%eax
1140: movl %eax,CPUVAR(ESP0)
1.97 bouyer 1141: #endif /* !XEN */
1.1 fvdl 1142:
1143: /* Don't bother with the rest if switching to a system process. */
1.48 yamt 1144: testl $LW_SYSTEM,L_FLAG(%edi)
1145: jnz 4f
1.1 fvdl 1146:
1.97 bouyer 1147: #ifndef XEN
1.54 ad 1148: /* Restore thread-private %fs/%gs descriptors. */
1.60 yamt 1149: movl CPUVAR(GDT),%ecx
1.55 ad 1150: movl PCB_FSD(%ebx), %eax
1151: movl PCB_FSD+4(%ebx), %edx
1.54 ad 1152: movl %eax, (GUFS_SEL*8)(%ecx)
1153: movl %edx, (GUFS_SEL*8+4)(%ecx)
1.55 ad 1154: movl PCB_GSD(%ebx), %eax
1155: movl PCB_GSD+4(%ebx), %edx
1.54 ad 1156: movl %eax, (GUGS_SEL*8)(%ecx)
1157: movl %edx, (GUGS_SEL*8+4)(%ecx)
1.97 bouyer 1158: #endif /* !XEN */
1.54 ad 1159:
1.60 yamt 1160: /* Switch I/O bitmap */
1161: movl PCB_IOMAP(%ebx),%eax
1162: orl %eax,%eax
1.95 joerg 1163: jnz .Lcopy_iobitmap
1.60 yamt 1164: movl $(IOMAP_INVALOFF << 16),CPUVAR(IOBASE)
1165: .Liobitmap_done:
1166:
1.56 ad 1167: /* Is this process using RAS (restartable atomic sequences)? */
1168: movl L_PROC(%edi),%eax
1169: cmpl $0,P_RASLIST(%eax)
1170: jne 5f
1171:
1.48 yamt 1172: /*
1.90 rmind 1173: * Restore cr0 (including FPU state). Raise the IPL to IPL_HIGH.
1.48 yamt 1174: * FPU IPIs can alter the LWP's saved cr0. Dropping the priority
1175: * is deferred until mi_switch(), when cpu_switchto() returns.
1176: */
1.62 bouyer 1177: 2:
1.97 bouyer 1178: #ifdef XEN
1179: pushl %edi
1180: call _C_LABEL(i386_tls_switch)
1181: addl $4,%esp
1182: #else /* !XEN */
1.108 christos 1183: movl $IPL_HIGH,CPUVAR(ILEVEL)
1184: movl PCB_CR0(%ebx),%ecx /* has CR0_TS clear */
1.48 yamt 1185: movl %cr0,%edx
1.1 fvdl 1186:
1.108 christos 1187: /*
1188: * If our floating point registers are on a different CPU,
1189: * set CR0_TS so we'll trap rather than reuse bogus state.
1190: */
1191: cmpl CPUVAR(FPCURLWP),%edi
1192: je 3f
1193: orl $CR0_TS,%ecx
1194:
1.48 yamt 1195: /* Reloading CR0 is very expensive - avoid if possible. */
1.108 christos 1196: 3: cmpl %edx,%ecx
1.48 yamt 1197: je 4f
1.1 fvdl 1198: movl %ecx,%cr0
1.97 bouyer 1199: #endif /* !XEN */
1.1 fvdl 1200:
1.48 yamt 1201: /* Return to the new LWP, returning 'oldlwp' in %eax. */
1202: 4: movl %esi,%eax
1.1 fvdl 1203: popl %edi
1204: popl %esi
1205: popl %ebx
1206: ret
1.20 dsl 1207:
1.48 yamt 1208: /* Check for restartable atomic sequences (RAS). */
1209: 5: movl L_MD_REGS(%edi),%ecx
1210: pushl TF_EIP(%ecx)
1.20 dsl 1211: pushl %eax
1212: call _C_LABEL(ras_lookup)
1213: addl $8,%esp
1214: cmpl $-1,%eax
1.48 yamt 1215: je 2b
1.49 ad 1216: movl L_MD_REGS(%edi),%ecx
1217: movl %eax,TF_EIP(%ecx)
1.48 yamt 1218: jmp 2b
1.1 fvdl 1219:
1.60 yamt 1220: .Lcopy_iobitmap:
1221: /* Copy I/O bitmap. */
1.86 ad 1222: incl _C_LABEL(pmap_iobmp_evcnt)+EV_COUNT
1.60 yamt 1223: movl $(IOMAPSIZE/4),%ecx
1224: pushl %esi
1225: pushl %edi
1226: movl %eax,%esi /* pcb_iomap */
1227: movl CPUVAR(SELF),%edi
1228: leal CPU_INFO_IOMAP(%edi),%edi
1229: rep
1230: movsl
1231: popl %edi
1232: popl %esi
1233: movl $((CPU_INFO_IOMAP - CPU_INFO_TSS) << 16),CPUVAR(IOBASE)
1234: jmp .Liobitmap_done
1.72 yamt 1235: END(cpu_switchto)
1.60 yamt 1236:
1.1 fvdl 1237: /*
1238: * void savectx(struct pcb *pcb);
1.48 yamt 1239: *
1.1 fvdl 1240: * Update pcb, saving current processor state.
1241: */
1242: ENTRY(savectx)
1.115 maxv 1243: movl 4(%esp),%edx /* edx = pcb */
1.1 fvdl 1244: movl %esp,PCB_ESP(%edx)
1245: movl %ebp,PCB_EBP(%edx)
1246: ret
1.72 yamt 1247: END(savectx)
1.1 fvdl 1248:
1249: /*
1.48 yamt 1250: * osyscall()
1251: *
1.1 fvdl 1252: * Old call gate entry for syscall
1253: */
1254: IDTVEC(osyscall)
1.87 ad 1255: #ifndef XEN
1256: /* XXX we are in trouble! interrupts be off here. */
1.115 maxv 1257: cli /* must be first instruction */
1.87 ad 1258: #endif
1.115 maxv 1259: pushfl /* set eflags in trap frame */
1.1 fvdl 1260: popl 8(%esp)
1.115 maxv 1261: orl $PSL_I,8(%esp) /* re-enable ints on return to user */
1262: pushl $7 /* size of instruction for restart */
1.1 fvdl 1263: jmp syscall1
1.72 yamt 1264: IDTVEC_END(osyscall)
1.1 fvdl 1265:
1266: /*
1.48 yamt 1267: * syscall()
1268: *
1.1 fvdl 1269: * Trap gate entry for syscall
1270: */
1271: IDTVEC(syscall)
1.115 maxv 1272: pushl $2 /* size of instruction for restart */
1.1 fvdl 1273: syscall1:
1.115 maxv 1274: pushl $T_ASTFLT /* trap # for doing ASTs */
1.1 fvdl 1275: INTRENTRY
1.85 ad 1276: STI(%eax)
1.1 fvdl 1277: #ifdef DIAGNOSTIC
1278: movl CPUVAR(ILEVEL),%ebx
1279: testl %ebx,%ebx
1280: jz 1f
1281: pushl $5f
1.84 ad 1282: call _C_LABEL(panic)
1.1 fvdl 1283: addl $4,%esp
1284: #ifdef DDB
1285: int $3
1286: #endif
1.30 junyoung 1287: 1:
1.1 fvdl 1288: #endif /* DIAGNOSTIC */
1.115 maxv 1289: addl $1,CPUVAR(NSYSCALL) /* count it atomically */
1290: adcl $0,CPUVAR(NSYSCALL)+4 /* count it atomically */
1.68 ad 1291: movl CPUVAR(CURLWP),%edi
1292: movl L_PROC(%edi),%edx
1.115 maxv 1293: movl %esp,L_MD_REGS(%edi) /* save pointer to frame */
1.15 fvdl 1294: pushl %esp
1.115 maxv 1295: call *P_MD_SYSCALL(%edx) /* get pointer to syscall() function */
1.15 fvdl 1296: addl $4,%esp
1.27 yamt 1297: .Lsyscall_checkast:
1.24 yamt 1298: /* Check for ASTs on exit to user mode. */
1.62 bouyer 1299: CLI(%eax)
1.68 ad 1300: movl L_MD_ASTPENDING(%edi), %eax
1301: orl CPUVAR(WANT_PMAPLOAD), %eax
1.24 yamt 1302: jnz 9f
1.62 bouyer 1303: #ifdef XEN
1304: STIC(%eax)
1305: jz 14f
1306: call _C_LABEL(stipending)
1307: testl %eax,%eax
1308: jz 14f
1309: /* process pending interrupts */
1310: CLI(%eax)
1311: movl CPUVAR(ILEVEL), %ebx
1.115 maxv 1312: movl $.Lsyscall_resume, %esi /* address to resume loop at */
1.62 bouyer 1313: .Lsyscall_resume:
1.115 maxv 1314: movl %ebx,%eax /* get cpl */
1.62 bouyer 1315: movl CPUVAR(IUNMASK)(,%eax,4),%eax
1.115 maxv 1316: andl CPUVAR(IPENDING),%eax /* any non-masked bits left? */
1.62 bouyer 1317: jz 17f
1318: bsrl %eax,%eax
1319: btrl %eax,CPUVAR(IPENDING)
1320: movl CPUVAR(ISOURCES)(,%eax,4),%eax
1321: jmp *IS_RESUME(%eax)
1.115 maxv 1322: 17: movl %ebx, CPUVAR(ILEVEL) /* restore cpl */
1.62 bouyer 1323: jmp .Lsyscall_checkast
1324: 14:
1325: #endif /* XEN */
1.1 fvdl 1326: #ifndef DIAGNOSTIC
1.24 yamt 1327: INTRFASTEXIT
1.1 fvdl 1328: #else /* DIAGNOSTIC */
1.24 yamt 1329: cmpl $IPL_NONE,CPUVAR(ILEVEL)
1.1 fvdl 1330: jne 3f
1331: INTRFASTEXIT
1.62 bouyer 1332: 3: STI(%eax)
1.1 fvdl 1333: pushl $4f
1.84 ad 1334: call _C_LABEL(panic)
1.1 fvdl 1335: addl $4,%esp
1.58 ad 1336: pushl $IPL_NONE
1337: call _C_LABEL(spllower)
1338: addl $4,%esp
1.53 yamt 1339: jmp .Lsyscall_checkast
1.84 ad 1340: 4: .asciz "SPL NOT LOWERED ON SYSCALL EXIT\n"
1341: 5: .asciz "SPL NOT ZERO ON SYSCALL ENTRY\n"
1.1 fvdl 1342: #endif /* DIAGNOSTIC */
1.68 ad 1343: 9:
1344: cmpl $0, CPUVAR(WANT_PMAPLOAD)
1345: jz 10f
1346: STI(%eax)
1.24 yamt 1347: call _C_LABEL(pmap_load)
1.27 yamt 1348: jmp .Lsyscall_checkast /* re-check ASTs */
1.68 ad 1349: 10:
1350: /* Always returning to user mode here. */
1351: movl $0, L_MD_ASTPENDING(%edi)
1352: STI(%eax)
1353: /* Pushed T_ASTFLT into tf_trapno on entry. */
1354: pushl %esp
1355: call _C_LABEL(trap)
1356: addl $4,%esp
1357: jmp .Lsyscall_checkast /* re-check ASTs */
1.72 yamt 1358: IDTVEC_END(syscall)
1.1 fvdl 1359:
1.81 ad 1360: IDTVEC(svr4_fasttrap)
1.115 maxv 1361: pushl $2 /* size of instruction for restart */
1362: pushl $T_ASTFLT /* trap # for doing ASTs */
1.81 ad 1363: INTRENTRY
1.85 ad 1364: STI(%eax)
1.81 ad 1365: pushl $RW_READER
1366: pushl $_C_LABEL(svr4_fasttrap_lock)
1367: call _C_LABEL(rw_enter)
1368: addl $8,%esp
1369: call *_C_LABEL(svr4_fasttrap_vec)
1370: pushl $_C_LABEL(svr4_fasttrap_lock)
1371: call _C_LABEL(rw_exit)
1372: addl $4,%esp
1373: 2: /* Check for ASTs on exit to user mode. */
1374: cli
1.115 maxv 1375: CHECK_ASTPENDING(%eax)
1.81 ad 1376: je 1f
1377: /* Always returning to user mode here. */
1378: CLEAR_ASTPENDING(%eax)
1379: sti
1380: /* Pushed T_ASTFLT into tf_trapno on entry. */
1381: pushl %esp
1382: call _C_LABEL(trap)
1383: addl $4,%esp
1384: jmp 2b
1385: 1: CHECK_DEFERRED_SWITCH
1386: jnz 9f
1387: INTRFASTEXIT
1388: 9: sti
1389: call _C_LABEL(pmap_load)
1390: cli
1391: jmp 2b
1392:
1.48 yamt 1393: /*
1394: * int npx586bug1(int a, int b)
1.110 dsl 1395: * Used when checking for the FDIV bug on first generations pentiums.
1396: * Anything 120MHz or above is fine.
1.48 yamt 1397: */
1.1 fvdl 1398: NENTRY(npx586bug1)
1.115 maxv 1399: fildl 4(%esp) /* x */
1400: fildl 8(%esp) /* y */
1.1 fvdl 1401: fld %st(1)
1.115 maxv 1402: fdiv %st(1),%st /* x/y */
1403: fmulp %st,%st(1) /* (x/y)*y */
1404: fsubrp %st,%st(1) /* x-(x/y)*y */
1.1 fvdl 1405: pushl $0
1406: fistpl (%esp)
1407: popl %eax
1408: ret
1.72 yamt 1409: END(npx586bug1)
1.50 ad 1410:
1411: /*
1.76 ad 1412: * void sse2_idlezero_page(void *pg)
1.50 ad 1413: *
1.76 ad 1414: * Zero a page without polluting the cache. Preemption must be
1415: * disabled by the caller. Abort if a preemption is pending.
1.50 ad 1416: */
1.76 ad 1417: ENTRY(sse2_idlezero_page)
1.50 ad 1418: pushl %ebp
1419: movl %esp,%ebp
1420: movl 8(%esp), %edx
1.76 ad 1421: movl $(PAGE_SIZE/32), %ecx
1.50 ad 1422: xorl %eax, %eax
1423: .align 16
1424: 1:
1.82 ad 1425: testl $RESCHED_KPREEMPT, CPUVAR(RESCHED)
1.76 ad 1426: jnz 2f
1.50 ad 1427: movnti %eax, 0(%edx)
1428: movnti %eax, 4(%edx)
1429: movnti %eax, 8(%edx)
1430: movnti %eax, 12(%edx)
1431: movnti %eax, 16(%edx)
1432: movnti %eax, 20(%edx)
1433: movnti %eax, 24(%edx)
1434: movnti %eax, 28(%edx)
1.76 ad 1435: addl $32, %edx
1.75 ad 1436: decl %ecx
1.50 ad 1437: jnz 1b
1438: sfence
1.76 ad 1439: incl %eax
1.50 ad 1440: pop %ebp
1441: ret
1.76 ad 1442: 2:
1.50 ad 1443: sfence
1444: popl %ebp
1445: ret
1.76 ad 1446: END(sse2_idlezero_page)
CVSweb <webmaster@jp.NetBSD.org>