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