Annotation of src/sys/arch/i386/i386/locore.S, Revision 1.23
1.23 ! wiz 1: /* $NetBSD: locore.S,v 1.22 2004/02/13 11:36:13 wiz Exp $ */
1.1 fvdl 2:
3: /*-
4: * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
8: * by Charles M. Hannum.
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: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the NetBSD
21: * Foundation, Inc. and its contributors.
22: * 4. Neither the name of The NetBSD Foundation nor the names of its
23: * contributors may be used to endorse or promote products derived
24: * from this software without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36: * POSSIBILITY OF SUCH DAMAGE.
37: */
38:
39: /*-
40: * Copyright (c) 1990 The Regents of the University of California.
41: * All rights reserved.
42: *
43: * This code is derived from software contributed to Berkeley by
44: * William Jolitz.
45: *
46: * Redistribution and use in source and binary forms, with or without
47: * modification, are permitted provided that the following conditions
48: * are met:
49: * 1. Redistributions of source code must retain the above copyright
50: * notice, this list of conditions and the following disclaimer.
51: * 2. Redistributions in binary form must reproduce the above copyright
52: * notice, this list of conditions and the following disclaimer in the
53: * documentation and/or other materials provided with the distribution.
1.12 agc 54: * 3. Neither the name of the University nor the names of its contributors
1.1 fvdl 55: * may be used to endorse or promote products derived from this software
56: * without specific prior written permission.
57: *
58: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68: * SUCH DAMAGE.
69: *
70: * @(#)locore.s 7.3 (Berkeley) 5/13/91
71: */
72:
1.18 christos 73: #include "opt_compat_netbsd.h"
74: #include "opt_compat_oldboot.h"
1.1 fvdl 75: #include "opt_cputype.h"
76: #include "opt_ddb.h"
77: #include "opt_ipkdb.h"
1.18 christos 78: #include "opt_lockdebug.h"
1.1 fvdl 79: #include "opt_multiprocessor.h"
80: #include "opt_realmem.h"
1.18 christos 81: #include "opt_user_ldt.h"
82: #include "opt_vm86.h"
1.1 fvdl 83:
84: #include "npx.h"
85: #include "assym.h"
86: #include "apm.h"
87: #include "lapic.h"
88: #include "ioapic.h"
1.8 fvdl 89: #include "ksyms.h"
1.1 fvdl 90:
91: #include <sys/errno.h>
92: #include <sys/syscall.h>
93:
94: #include <machine/cputypes.h>
95: #include <machine/param.h>
96: #include <machine/pte.h>
97: #include <machine/segments.h>
98: #include <machine/specialreg.h>
99: #include <machine/trap.h>
100: #include <machine/bootinfo.h>
101:
102: #if NLAPIC > 0
103: #include <machine/i82489reg.h>
104: #endif
105:
106: /* LINTSTUB: include <sys/types.h> */
107: /* LINTSTUB: include <machine/cpu.h> */
108: /* LINTSTUB: include <sys/systm.h> */
109:
110: #include <machine/asm.h>
111:
112: #if defined(MULTIPROCESSOR)
113:
1.5 thorpej 114: #define SET_CURLWP(lwp,cpu) \
1.1 fvdl 115: movl CPUVAR(SELF),cpu ; \
1.5 thorpej 116: movl lwp,CPUVAR(CURLWP) ; \
117: movl cpu,L_CPU(lwp)
1.1 fvdl 118:
119: #else
120:
1.5 thorpej 121: #define SET_CURLWP(lwp,tcpu) movl lwp,CPUVAR(CURLWP)
122: #define GET_CURLWP(reg) movl CPUVAR(CURLWP),reg
1.1 fvdl 123:
124: #endif
125:
126: #define GET_CURPCB(reg) movl CPUVAR(CURPCB),reg
127: #define SET_CURPCB(reg) movl reg,CPUVAR(CURPCB)
128:
129: #define CLEAR_RESCHED(reg) movl reg,CPUVAR(RESCHED)
130:
131: /* XXX temporary kluge; these should not be here */
132: /* Get definitions for IOM_BEGIN, IOM_END, and IOM_SIZE */
133: #include <dev/isa/isareg.h>
134:
135:
136: /* Disallow old names for REALBASEMEM */
137: #ifdef BIOSBASEMEM
138: #error BIOSBASEMEM option deprecated; use REALBASEMEM only if memory size reported by latest boot block is incorrect
139: #endif
140:
141: /* Disallow old names for REALEXTMEM */
142: #ifdef EXTMEM_SIZE
143: #error EXTMEM_SIZE option deprecated; use REALEXTMEM only if memory size reported by latest boot block is incorrect
144: #endif
145: #ifdef BIOSEXTMEM
146: #error BIOSEXTMEM option deprecated; use REALEXTMEM only if memory size reported by latest boot block is incorrect
147: #endif
148:
149: #include <machine/frameasm.h>
150:
151:
152: #ifdef MULTIPROCESSOR
153: #include <machine/i82489reg.h>
154: #endif
155:
156: /*
157: * PTmap is recursive pagemap at top of virtual address space.
158: * Within PTmap, the page directory can be found (third indirection).
159: *
160: * XXX 4 == sizeof pde
161: */
162: .set _C_LABEL(PTmap),(PDSLOT_PTE << PDSHIFT)
1.7 thorpej 163: .set _C_LABEL(PTD),(_C_LABEL(PTmap) + PDSLOT_PTE * PAGE_SIZE)
1.1 fvdl 164: .set _C_LABEL(PTDpde),(_C_LABEL(PTD) + PDSLOT_PTE * 4)
165:
166: /*
167: * APTmap, APTD is the alternate recursive pagemap.
168: * It's used when modifying another process's page tables.
169: *
170: * XXX 4 == sizeof pde
171: */
172: .set _C_LABEL(APTmap),(PDSLOT_APTE << PDSHIFT)
1.7 thorpej 173: .set _C_LABEL(APTD),(_C_LABEL(APTmap) + PDSLOT_APTE * PAGE_SIZE)
1.1 fvdl 174: .set _C_LABEL(APTDpde),(_C_LABEL(PTD) + PDSLOT_APTE * 4)
175:
176:
177: /*
178: * Initialization
179: */
180: .data
181:
182: .globl _C_LABEL(cpu)
183: .globl _C_LABEL(esym),_C_LABEL(boothowto)
184: .globl _C_LABEL(bootinfo),_C_LABEL(atdevbase)
185: #ifdef COMPAT_OLDBOOT
186: .globl _C_LABEL(bootdev)
187: #endif
188: .globl _C_LABEL(proc0paddr),_C_LABEL(PTDpaddr)
189: .globl _C_LABEL(biosbasemem),_C_LABEL(biosextmem)
190: .globl _C_LABEL(gdt)
191: #ifdef I586_CPU
192: .globl _C_LABEL(idt)
193: #endif
194: .globl _C_LABEL(lapic_tpr)
195:
196: #if NLAPIC > 0
197: #ifdef __ELF__
1.7 thorpej 198: .align PAGE_SIZE
1.1 fvdl 199: #else
200: .align 12
201: #endif
202: .globl _C_LABEL(local_apic), _C_LABEL(lapic_id)
203: _C_LABEL(local_apic):
204: .space LAPIC_ID
205: _C_LABEL(lapic_id):
206: .long 0x00000000
207: .space LAPIC_TPRI-(LAPIC_ID+4)
208: _C_LABEL(lapic_tpr):
209: .space LAPIC_PPRI-LAPIC_TPRI
210: _C_LABEL(lapic_ppr):
211: .space LAPIC_ISR-LAPIC_PPRI
212: _C_LABEL(lapic_isr):
1.7 thorpej 213: .space PAGE_SIZE-LAPIC_ISR
1.1 fvdl 214: #else
215: _C_LABEL(lapic_tpr):
216: .long 0
217: #endif
218:
219:
220: _C_LABEL(cpu): .long 0 # are we 386, 386sx, or 486,
221: # or Pentium, or..
222: _C_LABEL(esym): .long 0 # ptr to end of syms
223: _C_LABEL(atdevbase): .long 0 # location of start of iomem in virtual
224: _C_LABEL(proc0paddr): .long 0
225: _C_LABEL(PTDpaddr): .long 0 # paddr of PTD, for libkvm
226: #ifndef REALBASEMEM
227: _C_LABEL(biosbasemem): .long 0 # base memory reported by BIOS
228: #else
229: _C_LABEL(biosbasemem): .long REALBASEMEM
230: #endif
231: #ifndef REALEXTMEM
232: _C_LABEL(biosextmem): .long 0 # extended memory reported by BIOS
233: #else
234: _C_LABEL(biosextmem): .long REALEXTMEM
235: #endif
236:
237: .space 512
238: tmpstk:
239:
240:
1.13 christos 241: #define _RELOC(x) ((x) - KERNBASE_LOCORE)
1.1 fvdl 242: #define RELOC(x) _RELOC(_C_LABEL(x))
243:
244: .text
245: .globl _C_LABEL(kernel_text)
246: .set _C_LABEL(kernel_text),KERNTEXTOFF
247:
248: .globl start
249: start: movw $0x1234,0x472 # warm boot
250:
251: /*
252: * Load parameters from stack
253: * (howto, [bootdev], bootinfo, esym, basemem, extmem).
254: */
255: movl 4(%esp),%eax
256: movl %eax,RELOC(boothowto)
257: #ifdef COMPAT_OLDBOOT
258: movl 8(%esp),%eax
259: movl %eax,RELOC(bootdev)
260: #endif
261: movl 12(%esp),%eax
262:
263: testl %eax, %eax
264: jz 1f
265: movl (%eax), %ebx /* number of entries */
266: movl $RELOC(bootinfo), %edi
267: movl %ebx, (%edi)
268: addl $4, %edi
269: 2:
270: testl %ebx, %ebx
271: jz 1f
272: addl $4, %eax
273: movl (%eax), %ecx /* address of entry */
274: pushl %eax
275: pushl (%ecx) /* len */
276: pushl %ecx
277: pushl %edi
278: addl (%ecx), %edi /* update dest pointer */
279: cmpl $_RELOC(_C_LABEL(bootinfo) + BOOTINFO_MAXSIZE), %edi
280: jg 2f
281: call _C_LABEL(memcpy)
282: addl $12, %esp
283: popl %eax
284: subl $1, %ebx
285: jmp 2b
286: 2: /* cleanup for overflow case */
287: addl $16, %esp
288: movl $RELOC(bootinfo), %edi
289: subl %ebx, (%edi) /* correct number of entries */
290: 1:
291:
292: movl 16(%esp),%eax
293: testl %eax,%eax
294: jz 1f
1.13 christos 295: addl $KERNBASE_LOCORE,%eax
1.1 fvdl 296: 1: movl %eax,RELOC(esym)
297:
298: movl RELOC(biosextmem),%eax
299: testl %eax,%eax
300: jnz 1f
301: movl 20(%esp),%eax
302: movl %eax,RELOC(biosextmem)
303: 1:
304: movl RELOC(biosbasemem),%eax
305: testl %eax,%eax
306: jnz 1f
307: movl 24(%esp),%eax
308: movl %eax,RELOC(biosbasemem)
309: 1:
310:
311: /* First, reset the PSL. */
312: pushl $PSL_MBO
313: popfl
314:
315: /* Clear segment registers; always null in proc0. */
316: xorl %eax,%eax
317: movw %ax,%fs
318: movw %ax,%gs
319: decl %eax
320: movl %eax,RELOC(cpu_info_primary)+CPU_INFO_LEVEL
321:
322: /* Find out our CPU type. */
323:
324: try386: /* Try to toggle alignment check flag; does not exist on 386. */
325: pushfl
326: popl %eax
327: movl %eax,%ecx
328: orl $PSL_AC,%eax
329: pushl %eax
330: popfl
331: pushfl
332: popl %eax
333: xorl %ecx,%eax
334: andl $PSL_AC,%eax
335: pushl %ecx
336: popfl
337:
338: testl %eax,%eax
339: jnz try486
340:
341: /*
342: * Try the test of a NexGen CPU -- ZF will not change on a DIV
343: * instruction on a NexGen, it will on an i386. Documented in
344: * Nx586 Processor Recognition Application Note, NexGen, Inc.
345: */
346: movl $0x5555,%eax
347: xorl %edx,%edx
348: movl $2,%ecx
349: divl %ecx
350: jnz is386
351:
352: isnx586:
353: /*
354: * Don't try cpuid, as Nx586s reportedly don't support the
355: * PSL_ID bit.
356: */
357: movl $CPU_NX586,RELOC(cpu)
358: jmp 2f
359:
360: is386:
361: movl $CPU_386,RELOC(cpu)
362: jmp 2f
363:
364: try486: /* Try to toggle identification flag; does not exist on early 486s. */
365: pushfl
366: popl %eax
367: movl %eax,%ecx
368: xorl $PSL_ID,%eax
369: pushl %eax
370: popfl
371: pushfl
372: popl %eax
373: xorl %ecx,%eax
374: andl $PSL_ID,%eax
375: pushl %ecx
376: popfl
377:
378: testl %eax,%eax
379: jnz try586
380: is486: movl $CPU_486,RELOC(cpu)
381: /*
382: * Check Cyrix CPU
383: * Cyrix CPUs do not change the undefined flags following
384: * execution of the divide instruction which divides 5 by 2.
385: *
386: * Note: CPUID is enabled on M2, so it passes another way.
387: */
388: pushfl
389: movl $0x5555, %eax
390: xorl %edx, %edx
391: movl $2, %ecx
392: clc
393: divl %ecx
394: jnc trycyrix486
395: popfl
396: jmp 2f
397: trycyrix486:
398: movl $CPU_6x86,RELOC(cpu) # set CPU type
399: /*
400: * Check for Cyrix 486 CPU by seeing if the flags change during a
401: * divide. This is documented in the Cx486SLC/e SMM Programmer's
402: * Guide.
403: */
404: xorl %edx,%edx
405: cmpl %edx,%edx # set flags to known state
406: pushfl
407: popl %ecx # store flags in ecx
408: movl $-1,%eax
409: movl $4,%ebx
410: divl %ebx # do a long division
411: pushfl
412: popl %eax
413: xorl %ecx,%eax # are the flags different?
414: testl $0x8d5,%eax # only check C|PF|AF|Z|N|V
415: jne 2f # yes; must be Cyrix 6x86 CPU
416: movl $CPU_486DLC,RELOC(cpu) # set CPU type
417:
418: #ifndef CYRIX_CACHE_WORKS
419: /* Disable caching of the ISA hole only. */
420: invd
421: movb $CCR0,%al # Configuration Register index (CCR0)
422: outb %al,$0x22
423: inb $0x23,%al
424: orb $(CCR0_NC1|CCR0_BARB),%al
425: movb %al,%ah
426: movb $CCR0,%al
427: outb %al,$0x22
428: movb %ah,%al
429: outb %al,$0x23
430: invd
431: #else /* CYRIX_CACHE_WORKS */
432: /* Set cache parameters */
433: invd # Start with guaranteed clean cache
434: movb $CCR0,%al # Configuration Register index (CCR0)
435: outb %al,$0x22
436: inb $0x23,%al
437: andb $~CCR0_NC0,%al
438: #ifndef CYRIX_CACHE_REALLY_WORKS
439: orb $(CCR0_NC1|CCR0_BARB),%al
440: #else
441: orb $CCR0_NC1,%al
442: #endif
443: movb %al,%ah
444: movb $CCR0,%al
445: outb %al,$0x22
446: movb %ah,%al
447: outb %al,$0x23
448: /* clear non-cacheable region 1 */
449: movb $(NCR1+2),%al
450: outb %al,$0x22
451: movb $NCR_SIZE_0K,%al
452: outb %al,$0x23
453: /* clear non-cacheable region 2 */
454: movb $(NCR2+2),%al
455: outb %al,$0x22
456: movb $NCR_SIZE_0K,%al
457: outb %al,$0x23
458: /* clear non-cacheable region 3 */
459: movb $(NCR3+2),%al
460: outb %al,$0x22
461: movb $NCR_SIZE_0K,%al
462: outb %al,$0x23
463: /* clear non-cacheable region 4 */
464: movb $(NCR4+2),%al
465: outb %al,$0x22
466: movb $NCR_SIZE_0K,%al
467: outb %al,$0x23
468: /* enable caching in CR0 */
469: movl %cr0,%eax
470: andl $~(CR0_CD|CR0_NW),%eax
471: movl %eax,%cr0
472: invd
473: #endif /* CYRIX_CACHE_WORKS */
474:
475: jmp 2f
476:
477: try586: /* Use the `cpuid' instruction. */
478: xorl %eax,%eax
479: cpuid
480: movl %eax,RELOC(cpu_info_primary)+CPU_INFO_LEVEL
481:
482: 2:
483: /*
484: * Finished with old stack; load new %esp now instead of later so we
485: * can trace this code without having to worry about the trace trap
486: * clobbering the memory test or the zeroing of the bss+bootstrap page
487: * tables.
488: *
489: * The boot program should check:
490: * text+data <= &stack_variable - more_space_for_stack
491: * text+data+bss+pad+space_for_page_tables <= end_of_memory
492: * Oops, the gdt is in the carcass of the boot program so clearing
493: * the rest of memory is still not possible.
494: */
495: movl $_RELOC(tmpstk),%esp # bootstrap stack end location
496:
497: /*
498: * Virtual address space of kernel:
499: *
500: * text | data | bss | [syms] | page dir | proc0 kstack
501: * 0 1 2 3
502: */
1.7 thorpej 503: #define PROC0PDIR ((0) * PAGE_SIZE)
504: #define PROC0STACK ((1) * PAGE_SIZE)
505: #define SYSMAP ((1+UPAGES) * PAGE_SIZE)
506: #define TABLESIZE ((1+UPAGES) * PAGE_SIZE) /* + nkpde * PAGE_SIZE */
1.1 fvdl 507:
508: /* Find end of kernel image. */
509: movl $RELOC(end),%edi
1.8 fvdl 510: #if (NKSYMS || defined(DDB) || defined(LKM)) && !defined(SYMTAB_SPACE)
1.1 fvdl 511: /* Save the symbols (if loaded). */
512: movl RELOC(esym),%eax
513: testl %eax,%eax
514: jz 1f
1.13 christos 515: subl $KERNBASE_LOCORE,%eax
1.1 fvdl 516: movl %eax,%edi
517: 1:
518: #endif
519:
520: /* Calculate where to start the bootstrap tables. */
521: movl %edi,%esi # edi = esym ? esym : end
522: addl $PGOFSET,%esi # page align up
523: andl $~PGOFSET,%esi
524:
525: /*
526: * Calculate the size of the kernel page table directory, and
527: * how many entries it will have.
528: */
529: movl RELOC(nkpde),%ecx # get nkpde
530: cmpl $NKPTP_MIN,%ecx # larger than min?
531: jge 1f
532: movl $NKPTP_MIN,%ecx # set at min
533: jmp 2f
534: 1: cmpl $NKPTP_MAX,%ecx # larger than max?
535: jle 2f
536: movl $NKPTP_MAX,%ecx
537: 2:
538:
539: /* Clear memory for bootstrap tables. */
540: shll $PGSHIFT,%ecx
541: addl $TABLESIZE,%ecx
542: addl %esi,%ecx # end of tables
543: subl %edi,%ecx # size of tables
544: shrl $2,%ecx
545: xorl %eax,%eax
546: cld
547: rep
548: stosl
549:
550: /*
551: * fillkpt
552: * eax = pte (page frame | control | status)
553: * ebx = page table address
554: * ecx = number of pages to map
555: */
556: #define fillkpt \
557: 1: movl %eax,(%ebx) ; \
1.7 thorpej 558: addl $PAGE_SIZE,%eax ; /* increment physical address */ \
1.1 fvdl 559: addl $4,%ebx ; /* next pte */ \
560: loop 1b ;
561:
562: /*
563: * Build initial page tables.
564: */
565: /* Calculate end of text segment, rounded to a page. */
566: leal (RELOC(etext)+PGOFSET),%edx
567: andl $~PGOFSET,%edx
568:
569: /* Skip over the first 1MB. */
570: movl $_RELOC(KERNTEXTOFF),%eax
571: movl %eax,%ecx
572: shrl $PGSHIFT,%ecx
573: leal (SYSMAP)(%esi,%ecx,4),%ebx
574:
575: /* Map the kernel text read-only. */
576: movl %edx,%ecx
577: subl %eax,%ecx
578: shrl $PGSHIFT,%ecx
579: orl $(PG_V|PG_KR),%eax
580: fillkpt
581:
582: /* Map the data, BSS, and bootstrap tables read-write. */
583: leal (PG_V|PG_KW)(%edx),%eax
584: movl RELOC(nkpde),%ecx
585: shll $PGSHIFT,%ecx
586: addl $TABLESIZE,%ecx
587: addl %esi,%ecx # end of tables
588: subl %edx,%ecx # subtract end of text
589: shrl $PGSHIFT,%ecx
590: fillkpt
591:
592: /* Map ISA I/O memory. */
593: movl $(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax # having these bits set
594: movl $(IOM_SIZE>>PGSHIFT),%ecx # for this many pte s,
595: fillkpt
596:
597: /*
598: * Construct a page table directory.
599: */
600: /* Install PDEs for temporary double map of kernel. */
601: movl RELOC(nkpde),%ecx # for this many pde s,
602: leal (PROC0PDIR+0*4)(%esi),%ebx # which is where temp maps!
603: leal (SYSMAP+PG_V|PG_KW)(%esi),%eax # pte for KPT in proc 0,
604: fillkpt
605:
606: /* Map kernel PDEs. */
607: movl RELOC(nkpde),%ecx # for this many pde s,
608: leal (PROC0PDIR+PDSLOT_KERN*4)(%esi),%ebx # kernel pde offset
609: leal (SYSMAP+PG_V|PG_KW)(%esi),%eax # pte for KPT in proc 0,
610: fillkpt
611:
612: /* Install a PDE recursively mapping page directory as a page table! */
613: leal (PROC0PDIR+PG_V|PG_KW)(%esi),%eax # pte for ptd
614: movl %eax,(PROC0PDIR+PDSLOT_PTE*4)(%esi) # recursive PD slot
615:
616: /* Save phys. addr of PTD, for libkvm. */
617: movl %esi,RELOC(PTDpaddr)
618:
619: /* Load base of page directory and enable mapping. */
620: movl %esi,%eax # phys address of ptd in proc 0
621: movl %eax,%cr3 # load ptd addr into mmu
622: movl %cr0,%eax # get control word
623: # enable paging & NPX emulation
624: orl $(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_EM|CR0_MP),%eax
625: movl %eax,%cr0 # and let's page NOW!
626:
627: pushl $begin # jump to high mem
628: ret
629:
630: begin:
1.13 christos 631: /* Now running relocated at KERNBASE_LOCORE. Remove double mapping. */
1.1 fvdl 632: movl _C_LABEL(nkpde),%ecx # for this many pde s,
633: leal (PROC0PDIR+0*4)(%esi),%ebx # which is where temp maps!
1.13 christos 634: addl $(KERNBASE_LOCORE), %ebx # now use relocated address
1.1 fvdl 635: 1: movl $0,(%ebx)
636: addl $4,%ebx # next pde
637: loop 1b
638:
639: /* Relocate atdevbase. */
640: movl _C_LABEL(nkpde),%edx
641: shll $PGSHIFT,%edx
1.13 christos 642: addl $(TABLESIZE+KERNBASE_LOCORE),%edx
1.1 fvdl 643: addl %esi,%edx
644: movl %edx,_C_LABEL(atdevbase)
645:
646: /* Set up bootstrap stack. */
1.13 christos 647: leal (PROC0STACK+KERNBASE_LOCORE)(%esi),%eax
1.1 fvdl 648: movl %eax,_C_LABEL(proc0paddr)
649: leal (USPACE-FRAMESIZE)(%eax),%esp
650: movl %esi,PCB_CR3(%eax) # pcb->pcb_cr3
651: xorl %ebp,%ebp # mark end of frames
652:
653: subl $NGDT*8, %esp # space for temporary gdt
654: pushl %esp
655: call _C_LABEL(initgdt)
656: addl $4,%esp
657:
658: movl _C_LABEL(nkpde),%eax
659: shll $PGSHIFT,%eax
660: addl $TABLESIZE,%eax
661: addl %esi,%eax # skip past stack and page tables
662:
663: pushl %eax
664: call _C_LABEL(init386) # wire 386 chip for unix operation
665: addl $4+NGDT*8,%esp # pop temporary gdt
666:
667: #ifdef SAFARI_FIFO_HACK
668: movb $5,%al
669: movw $0x37b,%dx
670: outb %al,%dx
671: movw $0x37f,%dx
672: inb %dx,%al
673: movb %al,%cl
674:
675: orb $1,%cl
676:
677: movb $5,%al
678: movw $0x37b,%dx
679: outb %al,%dx
680: movw $0x37f,%dx
681: movb %cl,%al
682: outb %al,%dx
683: #endif /* SAFARI_FIFO_HACK */
684:
685: call _C_LABEL(main)
686:
687: /*
688: * void proc_trampoline(void);
689: * This is a trampoline function pushed onto the stack of a newly created
690: * process in order to do some additional setup. The trampoline is entered by
691: * cpu_switch()ing to the process, so we abuse the callee-saved registers used
692: * by cpu_switch() to store the information about the stub to call.
693: * NOTE: This function does not have a normal calling sequence!
694: */
695: /* LINTSTUB: Func: void proc_trampoline(void) */
696: NENTRY(proc_trampoline)
697: #ifdef MULTIPROCESSOR
698: call _C_LABEL(proc_trampoline_mp)
699: #endif
700: movl $IPL_NONE,CPUVAR(ILEVEL)
701: pushl %ebx
702: call *%esi
703: addl $4,%esp
704: INTRFASTEXIT
705: /* NOTREACHED */
706:
707: /*****************************************************************************/
1.16 christos 708: #ifdef COMPAT_16
1.1 fvdl 709: /*
710: * Signal trampoline; copied to top of user stack.
711: */
712: /* LINTSTUB: Var: char sigcode[1], esigcode[1]; */
713: NENTRY(sigcode)
714: /*
715: * Handler has returned here as if we called it. The sigcontext
716: * is on the stack after the 3 args "we" pushed.
717: */
718: leal 12(%esp),%eax # get pointer to sigcontext
719: movl %eax,4(%esp) # put it in the argument slot
720: # fake return address already there
1.19 christos 721: #if defined(SYS_compat_16___sigreturn14)
1.17 christos 722: movl $SYS_compat_16___sigreturn14,%eax
1.19 christos 723: #elif defined(SYS___sigreturn14)
724: movl $SYS___sigreturn14,%eax
725: #else
726: #error "no sigreturn14 syscall"
727: #endif
1.1 fvdl 728: int $0x80 # enter kernel with args on stack
729: movl $SYS_exit,%eax
730: int $0x80 # exit if sigreturn fails
731: .globl _C_LABEL(esigcode)
732: _C_LABEL(esigcode):
1.16 christos 733: #endif
1.1 fvdl 734:
735: /*****************************************************************************/
736:
737: /*
738: * The following primitives are used to fill and copy regions of memory.
739: */
740:
741: /*
742: * XXX No section 9 man page for fillw.
743: * fillw seems to be very sparsely used (only in pccons it seems.)
744: * One wonders if it couldn't be done without.
745: * -- Perry Metzger, May 7, 2001
746: */
747: /*
748: * void fillw(short pattern, void *addr, size_t len);
749: * Write len copies of pattern at addr.
750: */
751: /* LINTSTUB: Func: void fillw(short pattern, void *addr, size_t len) */
752: ENTRY(fillw)
753: pushl %edi
754: movl 8(%esp),%eax
755: movl 12(%esp),%edi
756: movw %ax,%cx
757: rorl $16,%eax
758: movw %cx,%ax
759: cld
760: movl 16(%esp),%ecx
761: shrl %ecx # do longwords
762: rep
763: stosl
764: movl 16(%esp),%ecx
765: andl $1,%ecx # do remainder
766: rep
767: stosw
768: popl %edi
769: ret
770:
771: /*
772: * int kcopy(const void *from, void *to, size_t len);
773: * Copy len bytes, abort on fault.
774: */
775: /* LINTSTUB: Func: int kcopy(const void *from, void *to, size_t len) */
776: ENTRY(kcopy)
777: pushl %esi
778: pushl %edi
779: GET_CURPCB(%eax) # load curpcb into eax and set on-fault
780: pushl PCB_ONFAULT(%eax)
781: movl $_C_LABEL(copy_fault), PCB_ONFAULT(%eax)
782:
783: movl 16(%esp),%esi
784: movl 20(%esp),%edi
785: movl 24(%esp),%ecx
786: movl %edi,%eax
787: subl %esi,%eax
788: cmpl %ecx,%eax # overlapping?
789: jb 1f
790: cld # nope, copy forward
791: shrl $2,%ecx # copy by 32-bit words
792: rep
793: movsl
794: movl 24(%esp),%ecx
795: andl $3,%ecx # any bytes left?
796: rep
797: movsb
798:
799: GET_CURPCB(%edx) # XXX save curpcb?
800: popl PCB_ONFAULT(%edx)
801: popl %edi
802: popl %esi
803: xorl %eax,%eax
804: ret
805:
806: ALIGN_TEXT
807: 1: addl %ecx,%edi # copy backward
808: addl %ecx,%esi
809: std
810: andl $3,%ecx # any fractional bytes?
811: decl %edi
812: decl %esi
813: rep
814: movsb
815: movl 24(%esp),%ecx # copy remainder by 32-bit words
816: shrl $2,%ecx
817: subl $3,%esi
818: subl $3,%edi
819: rep
820: movsl
821: cld
822:
823: GET_CURPCB(%edx)
824: popl PCB_ONFAULT(%edx)
825: popl %edi
826: popl %esi
827: xorl %eax,%eax
828: ret
829:
830: /*****************************************************************************/
831:
832: /*
833: * The following primitives are used to copy data in and out of the user's
834: * address space.
835: */
836:
837: /*
838: * Default to the lowest-common-denominator. We will improve it
839: * later.
840: */
841: #if defined(I386_CPU)
842: #define DEFAULT_COPYOUT _C_LABEL(i386_copyout)
843: #define DEFAULT_COPYIN _C_LABEL(i386_copyin)
844: #elif defined(I486_CPU)
845: #define DEFAULT_COPYOUT _C_LABEL(i486_copyout)
846: #define DEFAULT_COPYIN _C_LABEL(i386_copyin)
847: #elif defined(I586_CPU)
848: #define DEFAULT_COPYOUT _C_LABEL(i486_copyout) /* XXX */
849: #define DEFAULT_COPYIN _C_LABEL(i386_copyin) /* XXX */
850: #elif defined(I686_CPU)
851: #define DEFAULT_COPYOUT _C_LABEL(i486_copyout) /* XXX */
852: #define DEFAULT_COPYIN _C_LABEL(i386_copyin) /* XXX */
853: #endif
854:
855: .data
856:
857: .globl _C_LABEL(copyout_func)
858: _C_LABEL(copyout_func):
859: .long DEFAULT_COPYOUT
860:
861: .globl _C_LABEL(copyin_func)
862: _C_LABEL(copyin_func):
863: .long DEFAULT_COPYIN
864:
865: .text
866:
867: /*
868: * int copyout(const void *from, void *to, size_t len);
869: * Copy len bytes into the user's address space.
870: * see copyout(9)
871: */
872: /* LINTSTUB: Func: int copyout(const void *kaddr, void *uaddr, size_t len) */
873: ENTRY(copyout)
874: jmp *_C_LABEL(copyout_func)
875:
876: #if defined(I386_CPU)
877: /* LINTSTUB: Func: int i386_copyout(const void *kaddr, void *uaddr, size_t len) */
878: ENTRY(i386_copyout)
879: pushl %esi
880: pushl %edi
881: pushl $0
882:
883: movl 16(%esp),%esi
884: movl 20(%esp),%edi
885: movl 24(%esp),%eax
886:
887: /*
888: * We check that the end of the destination buffer is not past the end
889: * of the user's address space. If it's not, then we only need to
890: * check that each page is writable. The 486 will do this for us; the
891: * 386 will not. (We assume that pages in user space that are not
892: * writable by the user are not writable by the kernel either.)
893: */
894: movl %edi,%edx
895: addl %eax,%edx
896: jc _C_LABEL(copy_efault)
897: cmpl $VM_MAXUSER_ADDRESS,%edx
898: ja _C_LABEL(copy_efault)
899:
900: testl %eax,%eax # anything to do?
901: jz 3f
902:
903: /*
904: * We have to check each PTE for (write) permission, since the CPU
905: * doesn't do it for us.
906: */
907:
908: /* Compute number of pages. */
909: movl %edi,%ecx
910: andl $PGOFSET,%ecx
911: addl %eax,%ecx
912: decl %ecx
913: shrl $PGSHIFT,%ecx
914:
915: /* Compute PTE offset for start address. */
916: shrl $PGSHIFT,%edi
917:
918: GET_CURPCB(%edx)
919: movl $2f,PCB_ONFAULT(%edx)
920:
921: 1: /* Check PTE for each page. */
922: testb $PG_RW,_C_LABEL(PTmap)(,%edi,4)
923: jz 2f
924:
925: 4: incl %edi
926: decl %ecx
927: jns 1b
928:
929: movl 20(%esp),%edi
930: movl 24(%esp),%eax
931: jmp 3f
932:
933: 2: /* Simulate a trap. */
934: pushl %ecx
935: movl %edi,%eax
936: shll $PGSHIFT,%eax
937: pushl %eax
938: call _C_LABEL(trapwrite) # trapwrite(addr)
939: addl $4,%esp # pop argument
940: popl %ecx
941: testl %eax,%eax # if not ok, return EFAULT
942: jz 4b
943: jmp _C_LABEL(copy_efault)
944:
945: 3: GET_CURPCB(%edx)
946: movl $_C_LABEL(copy_fault),PCB_ONFAULT(%edx)
947:
948: /* bcopy(%esi, %edi, %eax); */
949: cld
950: movl %eax,%ecx
951: shrl $2,%ecx
952: rep
953: movsl
954: movl %eax,%ecx
955: andl $3,%ecx
956: rep
957: movsb
958:
959: popl PCB_ONFAULT(%edx)
960: popl %edi
961: popl %esi
962: xorl %eax,%eax
963: ret
964: #endif /* I386_CPU */
965:
966: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
967: /* LINTSTUB: Func: int i486_copyout(const void *kaddr, void *uaddr, size_t len) */
968: ENTRY(i486_copyout)
969: pushl %esi
970: pushl %edi
971: pushl $0
972:
973: movl 16(%esp),%esi
974: movl 20(%esp),%edi
975: movl 24(%esp),%eax
976:
977: /*
978: * We check that the end of the destination buffer is not past the end
979: * of the user's address space.
980: */
981: movl %edi,%edx
982: addl %eax,%edx
983: jc _C_LABEL(copy_efault)
984: cmpl $VM_MAXUSER_ADDRESS,%edx
985: ja _C_LABEL(copy_efault)
986:
987: GET_CURPCB(%edx)
988: movl $_C_LABEL(copy_fault),PCB_ONFAULT(%edx)
989:
990: /* bcopy(%esi, %edi, %eax); */
991: cld
992: movl %eax,%ecx
993: shrl $2,%ecx
994: rep
995: movsl
996: movl %eax,%ecx
997: andl $3,%ecx
998: rep
999: movsb
1000:
1001: popl PCB_ONFAULT(%edx)
1002: popl %edi
1003: popl %esi
1004: xorl %eax,%eax
1005: ret
1006: #endif /* I486_CPU || I586_CPU || I686_CPU */
1007:
1008: /*
1009: * int copyin(const void *from, void *to, size_t len);
1010: * Copy len bytes from the user's address space.
1011: * see copyin(9)
1012: */
1013: /* LINTSTUB: Func: int copyin(const void *uaddr, void *kaddr, size_t len) */
1014: ENTRY(copyin)
1015: jmp *_C_LABEL(copyin_func)
1016:
1017: #if defined(I386_CPU) || defined(I486_CPU) || defined(I586_CPU) || \
1018: defined(I686_CPU)
1019: /* LINTSTUB: Func: int i386_copyin(const void *uaddr, void *kaddr, size_t len) */
1020: ENTRY(i386_copyin)
1021: pushl %esi
1022: pushl %edi
1023: GET_CURPCB(%eax)
1024: pushl $0
1025: movl $_C_LABEL(copy_fault),PCB_ONFAULT(%eax)
1026:
1027: movl 16(%esp),%esi
1028: movl 20(%esp),%edi
1029: movl 24(%esp),%eax
1030:
1031: /*
1032: * We check that the end of the destination buffer is not past the end
1033: * of the user's address space. If it's not, then we only need to
1034: * check that each page is readable, and the CPU will do that for us.
1035: */
1036: movl %esi,%edx
1037: addl %eax,%edx
1038: jc _C_LABEL(copy_efault)
1039: cmpl $VM_MAXUSER_ADDRESS,%edx
1040: ja _C_LABEL(copy_efault)
1041:
1042: /* bcopy(%esi, %edi, %eax); */
1043: cld
1044: movl %eax,%ecx
1045: shrl $2,%ecx
1046: rep
1047: movsl
1048: movl %eax,%ecx
1049: andl $3,%ecx
1050: rep
1051: movsb
1052:
1053: GET_CURPCB(%edx)
1054: popl PCB_ONFAULT(%edx)
1055: popl %edi
1056: popl %esi
1057: xorl %eax,%eax
1058: ret
1059: #endif /* I386_CPU || I486_CPU || I586_CPU || I686_CPU */
1060:
1061: /* LINTSTUB: Ignore */
1062: NENTRY(copy_efault)
1063: movl $EFAULT,%eax
1064:
1065: /* LINTSTUB: Ignore */
1066: NENTRY(copy_fault)
1067: GET_CURPCB(%edx)
1068: popl PCB_ONFAULT(%edx)
1069: popl %edi
1070: popl %esi
1071: ret
1072:
1073: /*
1074: * int copyoutstr(const void *from, void *to, size_t maxlen, size_t *lencopied);
1075: * Copy a NUL-terminated string, at most maxlen characters long, into the
1076: * user's address space. Return the number of characters copied (including the
1077: * NUL) in *lencopied. If the string is too long, return ENAMETOOLONG; else
1078: * return 0 or EFAULT.
1079: * see copyoutstr(9)
1080: */
1081: /* LINTSTUB: Func: int copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done) */
1082: ENTRY(copyoutstr)
1083: pushl %esi
1084: pushl %edi
1085:
1086: movl 12(%esp),%esi # esi = from
1087: movl 16(%esp),%edi # edi = to
1088: movl 20(%esp),%edx # edx = maxlen
1089:
1090: #if defined(I386_CPU)
1091: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
1092: cmpl $CPUCLASS_386,_C_LABEL(cpu_class)
1093: jne 5f
1094: #endif /* I486_CPU || I586_CPU || I686_CPU */
1095:
1096: /* Compute number of bytes in first page. */
1097: movl %edi,%eax
1098: andl $PGOFSET,%eax
1.7 thorpej 1099: movl $PAGE_SIZE,%ecx
1100: subl %eax,%ecx # ecx = PAGE_SIZE - (src % PAGE_SIZE)
1.1 fvdl 1101:
1102: GET_CURPCB(%eax)
1103: movl $6f,PCB_ONFAULT(%eax)
1104:
1105: 1: /*
1106: * Once per page, check that we are still within the bounds of user
1107: * space, and check for a write fault.
1108: */
1109: cmpl $VM_MAXUSER_ADDRESS,%edi
1110: jae _C_LABEL(copystr_efault)
1111:
1112: /* Compute PTE offset. */
1113: movl %edi,%eax
1114: shrl $PGSHIFT,%eax # calculate pte address
1115:
1116: testb $PG_RW,_C_LABEL(PTmap)(,%eax,4)
1117: jnz 2f
1118:
1119: 6: /* Simulate a trap. */
1120: pushl %edx
1121: pushl %edi
1122: call _C_LABEL(trapwrite) # trapwrite(addr)
1123: addl $4,%esp # clear argument from stack
1124: popl %edx
1125: testl %eax,%eax
1126: jnz _C_LABEL(copystr_efault)
1127:
1128: 2: /* Copy up to end of this page. */
1129: subl %ecx,%edx # predecrement total count
1130: jnc 3f
1131: addl %edx,%ecx # ecx += (edx - ecx) = edx
1132: xorl %edx,%edx
1133:
1134: 3: decl %ecx
1135: js 4f
1136: lodsb
1137: stosb
1138: testb %al,%al
1139: jnz 3b
1140:
1141: /* Success -- 0 byte reached. */
1142: addl %ecx,%edx # add back residual for this page
1143: xorl %eax,%eax
1144: jmp copystr_return
1145:
1146: 4: /* Go to next page, if any. */
1.7 thorpej 1147: movl $PAGE_SIZE,%ecx
1.1 fvdl 1148: testl %edx,%edx
1149: jnz 1b
1150:
1151: /* edx is zero -- return ENAMETOOLONG. */
1152: movl $ENAMETOOLONG,%eax
1153: jmp copystr_return
1154: #endif /* I386_CPU */
1155:
1156: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
1157: 5: GET_CURPCB(%eax)
1158: movl $_C_LABEL(copystr_fault),PCB_ONFAULT(%eax)
1159: /*
1160: * Get min(%edx, VM_MAXUSER_ADDRESS-%edi).
1161: */
1162: movl $VM_MAXUSER_ADDRESS,%eax
1163: subl %edi,%eax
1164: cmpl %edx,%eax
1165: jae 1f
1166: movl %eax,%edx
1167: movl %eax,20(%esp)
1168:
1169: 1: incl %edx
1170: cld
1171:
1172: 1: decl %edx
1173: jz 2f
1174: lodsb
1175: stosb
1176: testb %al,%al
1177: jnz 1b
1178:
1179: /* Success -- 0 byte reached. */
1180: decl %edx
1181: xorl %eax,%eax
1182: jmp copystr_return
1183:
1184: 2: /* edx is zero -- return EFAULT or ENAMETOOLONG. */
1185: cmpl $VM_MAXUSER_ADDRESS,%edi
1186: jae _C_LABEL(copystr_efault)
1187: movl $ENAMETOOLONG,%eax
1188: jmp copystr_return
1189: #endif /* I486_CPU || I586_CPU || I686_CPU */
1190:
1191: /*
1192: * int copyinstr(const void *from, void *to, size_t maxlen, size_t *lencopied);
1193: * Copy a NUL-terminated string, at most maxlen characters long, from the
1194: * user's address space. Return the number of characters copied (including the
1195: * NUL) in *lencopied. If the string is too long, return ENAMETOOLONG; else
1196: * return 0 or EFAULT.
1197: * see copyinstr(9)
1198: */
1199: /* LINTSTUB: Func: int copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done) */
1200: ENTRY(copyinstr)
1201: pushl %esi
1202: pushl %edi
1203: GET_CURPCB(%ecx)
1204: movl $_C_LABEL(copystr_fault),PCB_ONFAULT(%ecx)
1205:
1206: movl 12(%esp),%esi # %esi = from
1207: movl 16(%esp),%edi # %edi = to
1208: movl 20(%esp),%edx # %edx = maxlen
1209:
1210: /*
1211: * Get min(%edx, VM_MAXUSER_ADDRESS-%esi).
1212: */
1213: movl $VM_MAXUSER_ADDRESS,%eax
1214: subl %esi,%eax
1215: cmpl %edx,%eax
1216: jae 1f
1217: movl %eax,%edx
1218: movl %eax,20(%esp)
1219:
1220: 1: incl %edx
1221: cld
1222:
1223: 1: decl %edx
1224: jz 2f
1225: lodsb
1226: stosb
1227: testb %al,%al
1228: jnz 1b
1229:
1230: /* Success -- 0 byte reached. */
1231: decl %edx
1232: xorl %eax,%eax
1233: jmp copystr_return
1234:
1235: 2: /* edx is zero -- return EFAULT or ENAMETOOLONG. */
1236: cmpl $VM_MAXUSER_ADDRESS,%esi
1237: jae _C_LABEL(copystr_efault)
1238: movl $ENAMETOOLONG,%eax
1239: jmp copystr_return
1240:
1241: /* LINTSTUB: Ignore */
1242: NENTRY(copystr_efault)
1243: movl $EFAULT,%eax
1244:
1245: /* LINTSTUB: Ignore */
1246: NENTRY(copystr_fault)
1247: copystr_return:
1248: /* Set *lencopied and return %eax. */
1249: GET_CURPCB(%ecx)
1250: movl $0,PCB_ONFAULT(%ecx)
1251: movl 20(%esp),%ecx
1252: subl %edx,%ecx
1253: movl 24(%esp),%edx
1254: testl %edx,%edx
1255: jz 8f
1256: movl %ecx,(%edx)
1257:
1258: 8: popl %edi
1259: popl %esi
1260: ret
1261:
1262: /*
1263: * int copystr(const void *from, void *to, size_t maxlen, size_t *lencopied);
1264: * Copy a NUL-terminated string, at most maxlen characters long. Return the
1265: * number of characters copied (including the NUL) in *lencopied. If the
1266: * string is too long, return ENAMETOOLONG; else return 0.
1267: * see copystr(9)
1268: */
1269: /* LINTSTUB: Func: int copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done) */
1270: ENTRY(copystr)
1271: pushl %esi
1272: pushl %edi
1273:
1274: movl 12(%esp),%esi # esi = from
1275: movl 16(%esp),%edi # edi = to
1276: movl 20(%esp),%edx # edx = maxlen
1277: incl %edx
1278: cld
1279:
1280: 1: decl %edx
1281: jz 4f
1282: lodsb
1283: stosb
1284: testb %al,%al
1285: jnz 1b
1286:
1287: /* Success -- 0 byte reached. */
1288: decl %edx
1289: xorl %eax,%eax
1290: jmp 6f
1291:
1292: 4: /* edx is zero -- return ENAMETOOLONG. */
1293: movl $ENAMETOOLONG,%eax
1294:
1295: 6: /* Set *lencopied and return %eax. */
1296: movl 20(%esp),%ecx
1297: subl %edx,%ecx
1298: movl 24(%esp),%edx
1299: testl %edx,%edx
1300: jz 7f
1301: movl %ecx,(%edx)
1302:
1303: 7: popl %edi
1304: popl %esi
1305: ret
1306:
1307: /*
1308: * long fuword(const void *uaddr);
1309: * Fetch an int from the user's address space.
1310: * see fuword(9)
1311: */
1312: /* LINTSTUB: Func: long fuword(const void *base) */
1313: ENTRY(fuword)
1314: movl 4(%esp),%edx
1315: cmpl $VM_MAXUSER_ADDRESS-4,%edx
1316: ja _C_LABEL(fusuaddrfault)
1317: GET_CURPCB(%ecx)
1318: movl $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
1319: movl (%edx),%eax
1320: movl $0,PCB_ONFAULT(%ecx)
1321: ret
1322:
1323: /*
1324: * int fusword(const void *uaddr);
1325: * Fetch a short from the user's address space.
1326: * see fusword(9)
1327: */
1328: /* LINTSTUB: Func: int fusword(const void *base) */
1329: ENTRY(fusword)
1330: movl 4(%esp),%edx
1331: cmpl $VM_MAXUSER_ADDRESS-2,%edx
1332: ja _C_LABEL(fusuaddrfault)
1333: GET_CURPCB(%ecx)
1334: movl $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
1335: movzwl (%edx),%eax
1336: movl $0,PCB_ONFAULT(%ecx)
1337: ret
1338:
1339: /*
1340: * int fuswintr(const void *uaddr);
1341: * Fetch a short from the user's address space. Can be called during an
1342: * interrupt.
1343: * see fuswintr(9)
1344: */
1345: /* LINTSTUB: Func: int fuswintr(const void *base) */
1346: ENTRY(fuswintr)
1347: movl 4(%esp),%edx
1348: cmpl $VM_MAXUSER_ADDRESS-2,%edx
1349: ja _C_LABEL(fusuaddrfault)
1.5 thorpej 1350: movl CPUVAR(CURLWP),%ecx
1351: movl L_ADDR(%ecx),%ecx
1.1 fvdl 1352: movl $_C_LABEL(fusubail),PCB_ONFAULT(%ecx)
1353: movzwl (%edx),%eax
1354: movl $0,PCB_ONFAULT(%ecx)
1355: ret
1356:
1357: /*
1358: * int fubyte(const void *uaddr);
1359: * Fetch a byte from the user's address space.
1360: * see fubyte(9)
1361: */
1362: /* LINTSTUB: Func: int fubyte(const void *base) */
1363: ENTRY(fubyte)
1364: movl 4(%esp),%edx
1365: cmpl $VM_MAXUSER_ADDRESS-1,%edx
1366: ja _C_LABEL(fusuaddrfault)
1367: GET_CURPCB(%ecx)
1368: movl $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
1369: movzbl (%edx),%eax
1370: movl $0,PCB_ONFAULT(%ecx)
1371: ret
1372:
1373: /*
1374: * Handle faults from [fs]u*(). Clean up and return -1.
1375: */
1376: /* LINTSTUB: Ignore */
1377: NENTRY(fusufault)
1378: movl $0,PCB_ONFAULT(%ecx)
1379: movl $-1,%eax
1380: ret
1381:
1382: /*
1383: * Handle faults from [fs]u*(). Clean up and return -1. This differs from
1384: * fusufault() in that trap() will recognize it and return immediately rather
1385: * than trying to page fault.
1386: */
1387: /* LINTSTUB: Ignore */
1388: NENTRY(fusubail)
1389: movl $0,PCB_ONFAULT(%ecx)
1390: movl $-1,%eax
1391: ret
1392:
1393: /*
1394: * Handle earlier faults from [fs]u*(), due to our of range addresses.
1395: */
1396: /* LINTSTUB: Ignore */
1397: NENTRY(fusuaddrfault)
1398: movl $-1,%eax
1399: ret
1400:
1401: /*
1402: * int suword(void *uaddr, long x);
1403: * Store an int in the user's address space.
1404: * see suword(9)
1405: */
1406: /* LINTSTUB: Func: int suword(void *base, long c) */
1407: ENTRY(suword)
1408: movl 4(%esp),%edx
1409: cmpl $VM_MAXUSER_ADDRESS-4,%edx
1410: ja _C_LABEL(fusuaddrfault)
1411:
1412: #if defined(I386_CPU)
1413: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
1414: cmpl $CPUCLASS_386,_C_LABEL(cpu_class)
1415: jne 2f
1416: #endif /* I486_CPU || I586_CPU || I686_CPU */
1417:
1418: GET_CURPCB(%eax)
1419: movl $3f,PCB_ONFAULT(%eax)
1420:
1421: movl %edx,%eax
1422: shrl $PGSHIFT,%eax # calculate pte address
1423: testb $PG_RW,_C_LABEL(PTmap)(,%eax,4)
1424: jnz 1f
1425:
1426: 3: /* Simulate a trap. */
1427: pushl %edx
1428: pushl %edx
1429: call _C_LABEL(trapwrite) # trapwrite(addr)
1430: addl $4,%esp # clear parameter from the stack
1431: popl %edx
1432: GET_CURPCB(%ecx)
1433: testl %eax,%eax
1434: jnz _C_LABEL(fusufault)
1435:
1436: 1: /* XXX also need to check the following 3 bytes for validity! */
1437: #endif
1438:
1439: 2: GET_CURPCB(%ecx)
1440: movl $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
1441:
1442: movl 8(%esp),%eax
1443: movl %eax,(%edx)
1444: xorl %eax,%eax
1445: movl %eax,PCB_ONFAULT(%ecx)
1446: ret
1447:
1448: /*
1449: * int susword(void *uaddr, short x);
1450: * Store a short in the user's address space.
1451: * see susword(9)
1452: */
1453: /* LINTSTUB: Func: int susword(void *base, short c) */
1454: ENTRY(susword)
1455: movl 4(%esp),%edx
1456: cmpl $VM_MAXUSER_ADDRESS-2,%edx
1457: ja _C_LABEL(fusuaddrfault)
1458:
1459: #if defined(I386_CPU)
1460: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
1461: cmpl $CPUCLASS_386,_C_LABEL(cpu_class)
1462: jne 2f
1463: #endif /* I486_CPU || I586_CPU || I686_CPU */
1464:
1465: GET_CURPCB(%eax)
1466: movl $3f,PCB_ONFAULT(%eax)
1467:
1468: movl %edx,%eax
1469: shrl $PGSHIFT,%eax # calculate pte address
1470: testb $PG_RW,_C_LABEL(PTmap)(,%eax,4)
1471: jnz 1f
1472:
1473: 3: /* Simulate a trap. */
1474: pushl %edx
1475: pushl %edx
1476: call _C_LABEL(trapwrite) # trapwrite(addr)
1477: addl $4,%esp # clear parameter from the stack
1478: popl %edx
1479: GET_CURPCB(%ecx)
1480: testl %eax,%eax
1481: jnz _C_LABEL(fusufault)
1482:
1483: 1: /* XXX also need to check the following byte for validity! */
1484: #endif
1485:
1486: 2: GET_CURPCB(%ecx)
1487: movl $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
1488:
1489: movl 8(%esp),%eax
1490: movw %ax,(%edx)
1491: xorl %eax,%eax
1492: movl %eax,PCB_ONFAULT(%ecx)
1493: ret
1494:
1495: /*
1496: * int suswintr(void *uaddr, short x);
1497: * Store a short in the user's address space. Can be called during an
1498: * interrupt.
1499: * see suswintr(9)
1500: */
1501: /* LINTSTUB: Func: int suswintr(void *base, short c) */
1502: ENTRY(suswintr)
1503: movl 4(%esp),%edx
1504: cmpl $VM_MAXUSER_ADDRESS-2,%edx
1505: ja _C_LABEL(fusuaddrfault)
1.5 thorpej 1506: movl CPUVAR(CURLWP),%ecx
1507: movl L_ADDR(%ecx),%ecx
1.1 fvdl 1508: movl $_C_LABEL(fusubail),PCB_ONFAULT(%ecx)
1509:
1510: #if defined(I386_CPU)
1511: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
1512: cmpl $CPUCLASS_386,_C_LABEL(cpu_class)
1513: jne 2f
1514: #endif /* I486_CPU || I586_CPU || I686_CPU */
1515:
1516: movl %edx,%eax
1517: shrl $PGSHIFT,%eax # calculate pte address
1518: testb $PG_RW,_C_LABEL(PTmap)(,%eax,4)
1519: jnz 1f
1520:
1521: /* Simulate a trap. */
1522: jmp _C_LABEL(fusubail)
1523:
1524: 1: /* XXX also need to check the following byte for validity! */
1525: #endif
1526:
1527: 2: movl 8(%esp),%eax
1528: movw %ax,(%edx)
1529: xorl %eax,%eax
1530: movl %eax,PCB_ONFAULT(%ecx)
1531: ret
1532:
1533: /*
1534: * int subyte(void *uaddr, char x);
1535: * Store a byte in the user's address space.
1536: * see subyte(9)
1537: */
1538: /* LINTSTUB: Func: int subyte(void *base, int c) */
1539: ENTRY(subyte)
1540: movl 4(%esp),%edx
1541: cmpl $VM_MAXUSER_ADDRESS-1,%edx
1542: ja _C_LABEL(fusuaddrfault)
1543:
1544: #if defined(I386_CPU)
1545: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
1546: cmpl $CPUCLASS_386,_C_LABEL(cpu_class)
1547: jne 2f
1548: #endif /* I486_CPU || I586_CPU || I686_CPU */
1549:
1550: GET_CURPCB(%eax)
1551: movl $3f,PCB_ONFAULT(%eax)
1552:
1553: movl %edx,%eax
1554: shrl $PGSHIFT,%eax # calculate pte address
1555: testb $PG_RW,_C_LABEL(PTmap)(,%eax,4)
1556: jnz 1f
1557:
1558: 3: /* Simulate a trap. */
1559: pushl %edx
1560: pushl %edx
1561: call _C_LABEL(trapwrite) # trapwrite(addr)
1562: addl $4,%esp # clear parameter from the stack
1563: popl %edx
1564: GET_CURPCB(%ecx)
1565: testl %eax,%eax
1566: jnz _C_LABEL(fusufault)
1567:
1568: 1:
1569: #endif
1570:
1571: 2: GET_CURPCB(%ecx)
1572: movl $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
1573:
1574: movb 8(%esp),%al
1575: movb %al,(%edx)
1576: xorl %eax,%eax
1577: movl %eax,PCB_ONFAULT(%ecx)
1578: ret
1579:
1580: /*****************************************************************************/
1581:
1582: /*
1583: * The following is i386-specific nonsense.
1584: */
1585:
1586: /*
1587: * void lgdt(struct region_descriptor *rdp);
1588: * Load a new GDT pointer (and do any necessary cleanup).
1589: * XXX It's somewhat questionable whether reloading all the segment registers
1590: * is necessary, since the actual descriptor data is not changed except by
1591: * process creation and exit, both of which clean up via task switches. OTOH,
1592: * this only happens at run time when the GDT is resized.
1593: */
1594: /* LINTSTUB: Func: void lgdt(struct region_descriptor *rdp) */
1595: NENTRY(lgdt)
1596: /* Reload the descriptor table. */
1597: movl 4(%esp),%eax
1598: lgdt (%eax)
1599: /* Flush the prefetch queue. */
1600: jmp 1f
1601: nop
1602: 1: /* Reload "stale" selectors. */
1603: movl $GSEL(GDATA_SEL, SEL_KPL),%eax
1604: movw %ax,%ds
1605: movw %ax,%es
1606: movw %ax,%gs
1607: movw %ax,%ss
1608: movl $GSEL(GCPU_SEL, SEL_KPL),%eax
1609: movw %ax,%fs
1610: /* Reload code selector by doing intersegment return. */
1611: popl %eax
1612: pushl $GSEL(GCODE_SEL, SEL_KPL)
1613: pushl %eax
1614: lret
1615:
1616: /*****************************************************************************/
1617:
1618: /*
1619: * These functions are primarily used by DDB.
1620: */
1621:
1622: /* LINTSTUB: Func: int setjmp (label_t *l) */
1623: ENTRY(setjmp)
1624: movl 4(%esp),%eax
1625: movl %ebx,(%eax) # save ebx
1626: movl %esp,4(%eax) # save esp
1627: movl %ebp,8(%eax) # save ebp
1628: movl %esi,12(%eax) # save esi
1629: movl %edi,16(%eax) # save edi
1630: movl (%esp),%edx # get rta
1631: movl %edx,20(%eax) # save eip
1632: xorl %eax,%eax # return (0);
1633: ret
1634:
1635: /* LINTSTUB: Func: void longjmp (label_t *l) */
1636: ENTRY(longjmp)
1637: movl 4(%esp),%eax
1638: movl (%eax),%ebx # restore ebx
1639: movl 4(%eax),%esp # restore esp
1640: movl 8(%eax),%ebp # restore ebp
1641: movl 12(%eax),%esi # restore esi
1642: movl 16(%eax),%edi # restore edi
1643: movl 20(%eax),%edx # get rta
1644: movl %edx,(%esp) # put in return frame
1645: xorl %eax,%eax # return (1);
1646: incl %eax
1647: ret
1648:
1649: /*****************************************************************************/
1650:
1651: .globl _C_LABEL(sched_whichqs),_C_LABEL(sched_qs)
1652: .globl _C_LABEL(uvmexp),_C_LABEL(panic)
1653:
1654: #ifdef DIAGNOSTIC
1655: NENTRY(switch_error)
1656: pushl $1f
1657: call _C_LABEL(panic)
1658: /* NOTREACHED */
1659: 1: .asciz "cpu_switch"
1660: #endif /* DIAGNOSTIC */
1661:
1662: /*
1.5 thorpej 1663: * void cpu_switch(struct lwp *)
1.1 fvdl 1664: * Find a runnable process and switch to it. Wait if necessary. If the new
1665: * process is the same as the old one, we short-circuit the context save and
1666: * restore.
1667: *
1668: * Note that the stack frame layout is known to "struct switchframe"
1669: * in <machine/frame.h> and to the code in cpu_fork() which initializes
1.5 thorpej 1670: * it for a new lwp.
1.1 fvdl 1671: */
1672: ENTRY(cpu_switch)
1673: pushl %ebx
1674: pushl %esi
1675: pushl %edi
1676:
1677: #ifdef DEBUG
1678: cmpl $IPL_SCHED,CPUVAR(ILEVEL)
1679: jae 1f
1.5 thorpej 1680: pushl $2f
1.1 fvdl 1681: call _C_LABEL(panic)
1682: /* NOTREACHED */
1.5 thorpej 1683: 2: .asciz "not splsched() in cpu_switch!"
1.1 fvdl 1684: 1:
1685: #endif /* DEBUG */
1686:
1.5 thorpej 1687: movl 16(%esp),%esi # current
1.1 fvdl 1688:
1689: /*
1.5 thorpej 1690: * Clear curlwp so that we don't accumulate system time while idle.
1691: * This also insures that schedcpu() will move the old lwp to
1.1 fvdl 1692: * the correct queue if it happens to get called from the spllower()
1693: * below and changes the priority. (See corresponding comment in
1694: * userret()).
1695: */
1.5 thorpej 1696: movl $0,CPUVAR(CURLWP)
1.1 fvdl 1697: /*
1.5 thorpej 1698: * First phase: find new lwp.
1.1 fvdl 1699: *
1700: * Registers:
1701: * %eax - queue head, scratch, then zero
1702: * %ebx - queue number
1703: * %ecx - cached value of whichqs
1.5 thorpej 1704: * %edx - next lwp in queue
1705: * %esi - old lwp
1706: * %edi - new lwp
1.1 fvdl 1707: */
1708:
1.5 thorpej 1709: /* Look for new lwp. */
1.1 fvdl 1710: cli # splhigh doesn't do a cli
1711: movl _C_LABEL(sched_whichqs),%ecx
1712: bsfl %ecx,%ebx # find a full q
1713: jnz switch_dequeue
1714:
1715: /*
1716: * idling: save old context.
1717: *
1718: * Registers:
1719: * %eax, %ecx - scratch
1.5 thorpej 1720: * %esi - old lwp, then old pcb
1.1 fvdl 1721: * %edi - idle pcb
1722: */
1723:
1724: pushl %esi
1725: call _C_LABEL(pmap_deactivate) # pmap_deactivate(oldproc)
1726: addl $4,%esp
1727:
1.5 thorpej 1728: movl L_ADDR(%esi),%esi
1.1 fvdl 1729:
1730: /* Save stack pointers. */
1731: movl %esp,PCB_ESP(%esi)
1732: movl %ebp,PCB_EBP(%esi)
1733:
1734: /* Find idle PCB for this CPU */
1735: #ifndef MULTIPROCESSOR
1.5 thorpej 1736: movl $_C_LABEL(lwp0),%ebx
1737: movl L_ADDR(%ebx),%edi
1738: movl L_MD_TSS_SEL(%ebx),%edx
1.1 fvdl 1739: #else
1740: movl CPUVAR(IDLE_PCB),%edi
1741: movl CPUVAR(IDLE_TSS_SEL),%edx
1742: #endif
1.5 thorpej 1743: movl $0,CPUVAR(CURLWP) /* In case we fault... */
1.1 fvdl 1744:
1745: /* Restore the idle context (avoid interrupts) */
1746: cli
1747:
1748: /* Restore stack pointers. */
1749: movl PCB_ESP(%edi),%esp
1750: movl PCB_EBP(%edi),%ebp
1751:
1752:
1753: /* Switch address space. */
1754: movl PCB_CR3(%edi),%ecx
1755: movl %ecx,%cr3
1756:
1757: /* Switch TSS. Reset "task busy" flag before loading. */
1758: #ifdef MULTIPROCESSOR
1759: movl CPUVAR(GDT),%eax
1760: #else
1761: movl _C_LABEL(gdt),%eax
1762: #endif
1763: andl $~0x0200,4-SEL_KPL(%eax,%edx,1)
1764: ltr %dx
1765:
1766: /* We're always in the kernel, so we don't need the LDT. */
1767:
1768: /* Restore cr0 (including FPU state). */
1769: movl PCB_CR0(%edi),%ecx
1770: movl %ecx,%cr0
1771:
1772: /* Record new pcb. */
1773: SET_CURPCB(%edi)
1774:
1775: xorl %esi,%esi
1776: sti
1777: idle_unlock:
1778: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1779: call _C_LABEL(sched_unlock_idle)
1780: #endif
1781: /* Interrupts are okay again. */
1.2 fvdl 1782: pushl $IPL_NONE # spl0()
1.1 fvdl 1783: call _C_LABEL(Xspllower) # process pending interrupts
1.2 fvdl 1784: addl $4,%esp
1.1 fvdl 1785: jmp idle_start
1786: idle_zero:
1787: sti
1788: call _C_LABEL(uvm_pageidlezero)
1789: cli
1790: cmpl $0,_C_LABEL(sched_whichqs)
1791: jnz idle_exit
1792: idle_loop:
1793: /* Try to zero some pages. */
1794: movl _C_LABEL(uvm)+UVM_PAGE_IDLE_ZERO,%ecx
1795: testl %ecx,%ecx
1796: jnz idle_zero
1797: sti
1798: hlt
1799: NENTRY(mpidle)
1800: idle_start:
1801: cli
1802: cmpl $0,_C_LABEL(sched_whichqs)
1803: jz idle_loop
1804: idle_exit:
1805: movl $IPL_HIGH,CPUVAR(ILEVEL) # splhigh
1.14 fvdl 1806: sti
1.1 fvdl 1807: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1808: call _C_LABEL(sched_lock_idle)
1809: #endif
1810: movl _C_LABEL(sched_whichqs),%ecx
1811: bsfl %ecx,%ebx
1812: jz idle_unlock
1813:
1814: switch_dequeue:
1815: /*
1816: * we're running at splhigh(), but it's otherwise okay to take
1817: * interrupts here.
1818: */
1819: sti
1820: leal _C_LABEL(sched_qs)(,%ebx,8),%eax # select q
1821:
1.5 thorpej 1822: movl L_FORW(%eax),%edi # unlink from front of process q
1.1 fvdl 1823: #ifdef DIAGNOSTIC
1824: cmpl %edi,%eax # linked to self (i.e. nothing queued)?
1825: je _C_LABEL(switch_error) # not possible
1826: #endif /* DIAGNOSTIC */
1.5 thorpej 1827: movl L_FORW(%edi),%edx
1828: movl %edx,L_FORW(%eax)
1829: movl %eax,L_BACK(%edx)
1.1 fvdl 1830:
1831: cmpl %edx,%eax # q empty?
1832: jne 3f
1833:
1834: btrl %ebx,%ecx # yes, clear to indicate empty
1835: movl %ecx,_C_LABEL(sched_whichqs) # update q status
1836:
1837: 3: /* We just did it. */
1838: xorl %eax,%eax
1839: CLEAR_RESCHED(%eax)
1840:
1.5 thorpej 1841: switch_resume:
1.1 fvdl 1842: #ifdef DIAGNOSTIC
1.5 thorpej 1843: cmpl %eax,L_WCHAN(%edi) # Waiting for something?
1.1 fvdl 1844: jne _C_LABEL(switch_error) # Yes; shouldn't be queued.
1.5 thorpej 1845: cmpb $LSRUN,L_STAT(%edi) # In run state?
1.1 fvdl 1846: jne _C_LABEL(switch_error) # No; shouldn't be queued.
1847: #endif /* DIAGNOSTIC */
1848:
1.5 thorpej 1849: /* Isolate lwp. XXX Is this necessary? */
1850: movl %eax,L_BACK(%edi)
1.1 fvdl 1851:
1.5 thorpej 1852: /* Record new lwp. */
1853: movb $LSONPROC,L_STAT(%edi) # l->l_stat = LSONPROC
1854: SET_CURLWP(%edi,%ecx)
1.1 fvdl 1855:
1.5 thorpej 1856: /* Skip context switch if same lwp. */
1.10 fvdl 1857: xorl %ebx,%ebx
1.1 fvdl 1858: cmpl %edi,%esi
1859: je switch_return
1860:
1.5 thorpej 1861: /* If old lwp exited, don't bother. */
1.1 fvdl 1862: testl %esi,%esi
1863: jz switch_exited
1864:
1865: /*
1866: * Second phase: save old context.
1867: *
1868: * Registers:
1869: * %eax, %ecx - scratch
1.5 thorpej 1870: * %esi - old lwp, then old pcb
1871: * %edi - new lwp
1.1 fvdl 1872: */
1873:
1874: pushl %esi
1875: call _C_LABEL(pmap_deactivate) # pmap_deactivate(oldproc)
1876: addl $4,%esp
1877:
1.5 thorpej 1878: movl L_ADDR(%esi),%esi
1.1 fvdl 1879:
1880: /* Save stack pointers. */
1881: movl %esp,PCB_ESP(%esi)
1882: movl %ebp,PCB_EBP(%esi)
1883:
1884: switch_exited:
1885: /*
1886: * Third phase: restore saved context.
1887: *
1888: * Registers:
1889: * %eax, %ebx, %ecx, %edx - scratch
1890: * %esi - new pcb
1.5 thorpej 1891: * %edi - new lwp
1.1 fvdl 1892: */
1893:
1894: /* No interrupts while loading new state. */
1895: cli
1.5 thorpej 1896: movl L_ADDR(%edi),%esi
1.1 fvdl 1897:
1898: /* Restore stack pointers. */
1899: movl PCB_ESP(%esi),%esp
1900: movl PCB_EBP(%esi),%ebp
1901:
1902: #if 0
1903: /* Don't bother with the rest if switching to a system process. */
1.5 thorpej 1904: testl $P_SYSTEM,L_FLAG(%edi); XXX NJWLWP lwp's don't have P_SYSTEM!
1.1 fvdl 1905: jnz switch_restored
1906: #endif
1907:
1908: #ifdef MULTIPROCESSOR
1909: movl CPUVAR(GDT),%eax
1910: #else
1911: /* Load TSS info. */
1912: movl _C_LABEL(gdt),%eax
1913: #endif
1.5 thorpej 1914: movl L_MD_TSS_SEL(%edi),%edx
1.1 fvdl 1915:
1916: /* Switch TSS. Reset "task busy" flag before loading. */
1917: andl $~0x0200,4(%eax,%edx, 1)
1918: ltr %dx
1919:
1920: pushl %edi
1921: call _C_LABEL(pmap_activate) # pmap_activate(p)
1922: addl $4,%esp
1923:
1924: #if 0
1925: switch_restored:
1926: #endif
1927: /* Restore cr0 (including FPU state). */
1928: movl PCB_CR0(%esi),%ecx
1929: #ifdef MULTIPROCESSOR
1930: /*
1.22 wiz 1931: * If our floating point registers are on a different CPU,
1.1 fvdl 1932: * clear CR0_TS so we'll trap rather than reuse bogus state.
1933: */
1934: movl PCB_FPCPU(%esi),%ebx
1935: cmpl CPUVAR(SELF),%ebx
1936: jz 1f
1937: orl $CR0_TS,%ecx
1938: 1:
1939: #endif
1940: movl %ecx,%cr0
1941:
1942: /* Record new pcb. */
1943: SET_CURPCB(%esi)
1944:
1945: /* Interrupts are okay again. */
1946: sti
1947:
1948: /*
1949: * Check for restartable atomic sequences (RAS)
1950: */
1.5 thorpej 1951: movl CPUVAR(CURLWP),%edi
1952: movl L_PROC(%edi),%esi
1.20 dsl 1953: cmpl $0,P_RASLIST(%esi)
1954: jne 2f
1.1 fvdl 1955: 1:
1.10 fvdl 1956: movl $1,%ebx
1.1 fvdl 1957:
1958: switch_return:
1959: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1960: call _C_LABEL(sched_unlock_idle)
1961: #endif
1.2 fvdl 1962: pushl $IPL_NONE # spl0()
1.1 fvdl 1963: call _C_LABEL(Xspllower) # process pending interrupts
1.2 fvdl 1964: addl $4,%esp
1.1 fvdl 1965: movl $IPL_HIGH,CPUVAR(ILEVEL) # splhigh()
1.9 fvdl 1966:
1967: movl %ebx,%eax
1.1 fvdl 1968:
1969: popl %edi
1970: popl %esi
1971: popl %ebx
1972: ret
1.20 dsl 1973:
1974: 2: # check RAS list
1975: movl L_MD_REGS(%edi),%ebx
1976: movl TF_EIP(%ebx),%eax
1977: pushl %eax
1978: pushl %esi
1979: call _C_LABEL(ras_lookup)
1980: addl $8,%esp
1981: cmpl $-1,%eax
1982: je 1b
1983: movl %eax,TF_EIP(%ebx)
1984: jmp 1b
1.1 fvdl 1985:
1986: /*
1.5 thorpej 1987: * void cpu_switchto(struct lwp *current, struct lwp *next)
1988: * Switch to the specified next LWP.
1989: */
1990: ENTRY(cpu_switchto)
1991: pushl %ebx
1992: pushl %esi
1993: pushl %edi
1994:
1995: #ifdef DEBUG
1996: cmpl $IPL_SCHED,CPUVAR(ILEVEL)
1997: jae 1f
1998: pushl $2f
1999: call _C_LABEL(panic)
2000: /* NOTREACHED */
2001: 2: .asciz "not splsched() in cpu_switchto!"
2002: 1:
2003: #endif /* DEBUG */
2004:
2005: movl 16(%esp),%esi # current
2006: movl 20(%esp),%edi # next
2007:
2008: /*
2009: * Clear curlwp so that we don't accumulate system time while idle.
2010: * This also insures that schedcpu() will move the old process to
2011: * the correct queue if it happens to get called from the spllower()
2012: * below and changes the priority. (See corresponding comment in
2013: * usrret()).
2014: *
2015: * XXX Is this necessary? We know we won't go idle.
2016: */
2017: movl $0,CPUVAR(CURLWP)
2018:
2019: /*
2020: * We're running at splhigh(), but it's otherwise okay to take
2021: * interrupts here.
2022: */
2023: sti
2024:
2025: /* Jump into the middle of cpu_switch */
2026: xorl %eax,%eax
2027: jmp switch_resume
2028:
2029: /*
1.21 jdolecek 2030: * void cpu_exit(struct lwp *l)
1.22 wiz 2031: * Switch to the appropriate idle context (lwp0's if uniprocessor; the CPU's
1.1 fvdl 2032: * if multiprocessor) and deallocate the address space and kernel stack for p.
2033: * Then jump into cpu_switch(), as if we were in the idle proc all along.
2034: */
2035: #ifndef MULTIPROCESSOR
1.5 thorpej 2036: .globl _C_LABEL(lwp0)
1.1 fvdl 2037: #endif
2038: .globl _C_LABEL(uvmspace_free),_C_LABEL(kernel_map)
2039: .globl _C_LABEL(uvm_km_free),_C_LABEL(tss_free)
1.21 jdolecek 2040: /* LINTSTUB: Func: void cpu_exit(struct lwp *l) */
2041: ENTRY(cpu_exit)
1.1 fvdl 2042: movl 4(%esp),%edi # old process
2043: #ifndef MULTIPROCESSOR
1.5 thorpej 2044: movl $_C_LABEL(lwp0),%ebx
2045: movl L_ADDR(%ebx),%esi
2046: movl L_MD_TSS_SEL(%ebx),%edx
1.1 fvdl 2047: #else
2048: movl CPUVAR(IDLE_PCB),%esi
2049: movl CPUVAR(IDLE_TSS_SEL),%edx
2050: #endif
2051: /* In case we fault... */
1.5 thorpej 2052: movl $0,CPUVAR(CURLWP)
1.1 fvdl 2053:
2054: /* Restore the idle context. */
2055: cli
2056:
2057: /* Restore stack pointers. */
2058: movl PCB_ESP(%esi),%esp
2059: movl PCB_EBP(%esi),%ebp
2060:
2061: /* Load TSS info. */
2062: #ifdef MULTIPROCESSOR
2063: movl CPUVAR(GDT),%eax
2064: #else
2065: /* Load TSS info. */
2066: movl _C_LABEL(gdt),%eax
2067: #endif
2068:
2069: /* Switch address space. */
2070: movl PCB_CR3(%esi),%ecx
2071: movl %ecx,%cr3
2072:
2073: /* Switch TSS. */
2074: andl $~0x0200,4-SEL_KPL(%eax,%edx,1)
2075: ltr %dx
2076:
2077: /* We're always in the kernel, so we don't need the LDT. */
2078:
2079: /* Restore cr0 (including FPU state). */
2080: movl PCB_CR0(%esi),%ecx
2081: movl %ecx,%cr0
2082:
2083: /* Record new pcb. */
2084: SET_CURPCB(%esi)
2085:
2086: /* Interrupts are okay again. */
2087: sti
2088:
2089: /*
1.21 jdolecek 2090: * Schedule the dead LWP's stack to be freed.
1.1 fvdl 2091: */
1.21 jdolecek 2092: pushl %edi
2093: call _C_LABEL(lwp_exit2)
1.1 fvdl 2094: addl $4,%esp
2095:
2096: /* Jump into cpu_switch() with the right state. */
2097: xorl %esi,%esi
1.5 thorpej 2098: movl %esi,CPUVAR(CURLWP)
1.1 fvdl 2099: jmp idle_start
2100:
2101: /*
2102: * void savectx(struct pcb *pcb);
2103: * Update pcb, saving current processor state.
2104: */
2105: /* LINTSTUB: Func: void savectx(struct pcb *pcb) */
2106: ENTRY(savectx)
2107: movl 4(%esp),%edx # edx = p->p_addr
2108:
2109: /* Save stack pointers. */
2110: movl %esp,PCB_ESP(%edx)
2111: movl %ebp,PCB_EBP(%edx)
2112:
2113: ret
2114:
2115: /*
2116: * Old call gate entry for syscall
2117: */
2118: /* LINTSTUB: Var: char Xosyscall[1]; */
2119: IDTVEC(osyscall)
2120: /* Set eflags in trap frame. */
2121: pushfl
2122: popl 8(%esp)
2123: pushl $7 # size of instruction for restart
2124: jmp syscall1
2125:
2126: /*
2127: * Trap gate entry for syscall
2128: */
2129: /* LINTSTUB: Var: char Xsyscall[1]; */
2130: IDTVEC(syscall)
2131: pushl $2 # size of instruction for restart
2132: syscall1:
2133: pushl $T_ASTFLT # trap # for doing ASTs
2134: INTRENTRY
2135:
2136: #ifdef DIAGNOSTIC
2137: movl CPUVAR(ILEVEL),%ebx
2138: testl %ebx,%ebx
2139: jz 1f
2140: pushl $5f
2141: call _C_LABEL(printf)
2142: addl $4,%esp
2143: #ifdef DDB
2144: int $3
2145: #endif
2146: 1:
2147: #endif /* DIAGNOSTIC */
1.5 thorpej 2148: movl CPUVAR(CURLWP),%edx
2149: movl %esp,L_MD_REGS(%edx) # save pointer to frame
2150: movl L_PROC(%edx),%edx
1.15 fvdl 2151: pushl %esp
1.1 fvdl 2152: call *P_MD_SYSCALL(%edx) # get pointer to syscall() function
1.15 fvdl 2153: addl $4,%esp
1.1 fvdl 2154: 2: /* Check for ASTs on exit to user mode. */
2155: cli
1.5 thorpej 2156: CHECK_ASTPENDING(%eax)
1.1 fvdl 2157: je 1f
2158: /* Always returning to user mode here. */
1.5 thorpej 2159: CLEAR_ASTPENDING(%eax)
1.1 fvdl 2160: sti
2161: /* Pushed T_ASTFLT into tf_trapno on entry. */
1.15 fvdl 2162: pushl %esp
1.1 fvdl 2163: call _C_LABEL(trap)
1.15 fvdl 2164: addl $4,%esp
1.1 fvdl 2165: jmp 2b
2166: #ifndef DIAGNOSTIC
2167: 1: INTRFASTEXIT
2168: #else /* DIAGNOSTIC */
2169: 1: cmpl $IPL_NONE,CPUVAR(ILEVEL)
2170: jne 3f
2171: INTRFASTEXIT
2172: 3: sti
2173: pushl $4f
2174: call _C_LABEL(printf)
2175: addl $4,%esp
2176: #ifdef DDB
2177: int $3
2178: #endif /* DDB */
2179: movl $IPL_NONE,CPUVAR(ILEVEL)
2180: jmp 2b
2181: 4: .asciz "WARNING: SPL NOT LOWERED ON SYSCALL EXIT\n"
2182: 5: .asciz "WARNING: SPL NOT ZERO ON SYSCALL ENTRY\n"
2183: #endif /* DIAGNOSTIC */
2184:
2185: #if NNPX > 0
2186: /*
2187: * Special interrupt handlers. Someday intr0-intr15 will be used to count
2188: * interrupts. We'll still need a special exception 16 handler. The busy
2189: * latch stuff in probintr() can be moved to npxprobe().
2190: */
2191:
2192: /* LINTSTUB: Func: void probeintr(void) */
2193: NENTRY(probeintr)
2194: ss
2195: incl _C_LABEL(npx_intrs_while_probing)
2196: pushl %eax
2197: movb $0x20,%al # EOI (asm in strings loses cpp features)
2198: outb %al,$0xa0 # IO_ICU2
2199: outb %al,$0x20 # IO_ICU1
2200: movb $0,%al
2201: outb %al,$0xf0 # clear BUSY# latch
2202: popl %eax
2203: iret
2204:
2205: /* LINTSTUB: Func: void probetrap(void) */
2206: NENTRY(probetrap)
2207: ss
2208: incl _C_LABEL(npx_traps_while_probing)
2209: fnclex
2210: iret
2211:
2212: /* LINTSTUB: Func: int npx586bug1(int a, int b) */
2213: NENTRY(npx586bug1)
2214: fildl 4(%esp) # x
2215: fildl 8(%esp) # y
2216: fld %st(1)
2217: fdiv %st(1),%st # x/y
2218: fmulp %st,%st(1) # (x/y)*y
2219: fsubrp %st,%st(1) # x-(x/y)*y
2220: pushl $0
2221: fistpl (%esp)
2222: popl %eax
2223: ret
2224: #endif /* NNPX > 0 */
CVSweb <webmaster@jp.NetBSD.org>