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