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