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