Annotation of src/sys/arch/i386/i386/locore.S, Revision 1.33
1.33 ! fvdl 1: /* $NetBSD: locore.S,v 1.32 2005/03/02 12:00:25 mycroft Exp $ */
1.1 fvdl 2:
3: /*-
1.32 mycroft 4: * Copyright (c) 1998, 2000, 2004 The NetBSD Foundation, Inc.
1.1 fvdl 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)
1.30 junyoung 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.30 junyoung 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.30 junyoung 126: #define GET_CURPCB(reg) movl CPUVAR(CURPCB),reg
1.1 fvdl 127: #define SET_CURPCB(reg) movl reg,CPUVAR(CURPCB)
1.24 yamt 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.30 junyoung 155:
1.31 junyoung 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.31 junyoung 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.30 junyoung 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.30 junyoung 187: _C_LABEL(lapic_id):
1.1 fvdl 188: .long 0x00000000
189: .space LAPIC_TPRI-(LAPIC_ID+4)
1.30 junyoung 190: _C_LABEL(lapic_tpr):
1.1 fvdl 191: .space LAPIC_PPRI-LAPIC_TPRI
1.30 junyoung 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.30 junyoung 197: _C_LABEL(lapic_tpr):
1.1 fvdl 198: .long 0
199: #endif
1.30 junyoung 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.31 junyoung 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.30 junyoung 218:
1.1 fvdl 219: .space 512
220: tmpstk:
221:
222:
1.13 christos 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.13 christos 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.30 junyoung 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.13 christos 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.29 mycroft 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.29 mycroft 513: leal TABLESIZE+NBPD+PDOFSET(%edi),%eax
514: shrl $PDSHIFT,%eax
1.1 fvdl 515: movl RELOC(nkpde),%ecx # get nkpde
1.29 mycroft 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.29 mycroft 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.30 junyoung 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.31 junyoung 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.13 christos 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.13 christos 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.13 christos 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.13 christos 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.30 junyoung 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.24 yamt 693: DO_DEFERRED_SWITCH(%eax)
1.1 fvdl 694: INTRFASTEXIT
695: /* NOTREACHED */
696:
697: /*****************************************************************************/
1.16 christos 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.17 christos 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.16 christos 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.24 yamt 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.24 yamt 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.30 junyoung 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.31 junyoung 907: testb $PG_RW,PTE_BASE(,%edi,4)
1.1 fvdl 908: jz 2f
1.30 junyoung 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.30 junyoung 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.30 junyoung 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.24 yamt 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.30 junyoung 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.24 yamt 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.24 yamt 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.31 junyoung 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
1.33 ! fvdl 1165: jc _C_LABEL(copystr_efault)
1.1 fvdl 1166: cmpl %edx,%eax
1167: jae 1f
1168: movl %eax,%edx
1169: movl %eax,20(%esp)
1170:
1171: 1: incl %edx
1172: cld
1173:
1174: 1: decl %edx
1175: jz 2f
1176: lodsb
1177: stosb
1178: testb %al,%al
1179: jnz 1b
1180:
1181: /* Success -- 0 byte reached. */
1182: decl %edx
1183: xorl %eax,%eax
1184: jmp copystr_return
1185:
1186: 2: /* edx is zero -- return EFAULT or ENAMETOOLONG. */
1187: cmpl $VM_MAXUSER_ADDRESS,%edi
1188: jae _C_LABEL(copystr_efault)
1189: movl $ENAMETOOLONG,%eax
1190: jmp copystr_return
1191: #endif /* I486_CPU || I586_CPU || I686_CPU */
1192:
1193: /*
1194: * int copyinstr(const void *from, void *to, size_t maxlen, size_t *lencopied);
1195: * Copy a NUL-terminated string, at most maxlen characters long, from the
1196: * user's address space. Return the number of characters copied (including the
1197: * NUL) in *lencopied. If the string is too long, return ENAMETOOLONG; else
1198: * return 0 or EFAULT.
1199: * see copyinstr(9)
1200: */
1201: /* LINTSTUB: Func: int copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done) */
1202: ENTRY(copyinstr)
1203: pushl %esi
1204: pushl %edi
1.30 junyoung 1205:
1.24 yamt 1206: DO_DEFERRED_SWITCH(%eax)
1207:
1.1 fvdl 1208: GET_CURPCB(%ecx)
1209: movl $_C_LABEL(copystr_fault),PCB_ONFAULT(%ecx)
1210:
1211: movl 12(%esp),%esi # %esi = from
1212: movl 16(%esp),%edi # %edi = to
1213: movl 20(%esp),%edx # %edx = maxlen
1214:
1215: /*
1216: * Get min(%edx, VM_MAXUSER_ADDRESS-%esi).
1217: */
1218: movl $VM_MAXUSER_ADDRESS,%eax
1219: subl %esi,%eax
1.33 ! fvdl 1220: jc _C_LABEL(copystr_efault)
1.1 fvdl 1221: cmpl %edx,%eax
1222: jae 1f
1223: movl %eax,%edx
1224: movl %eax,20(%esp)
1225:
1226: 1: incl %edx
1227: cld
1228:
1229: 1: decl %edx
1230: jz 2f
1231: lodsb
1232: stosb
1233: testb %al,%al
1234: jnz 1b
1235:
1236: /* Success -- 0 byte reached. */
1237: decl %edx
1238: xorl %eax,%eax
1239: jmp copystr_return
1240:
1241: 2: /* edx is zero -- return EFAULT or ENAMETOOLONG. */
1242: cmpl $VM_MAXUSER_ADDRESS,%esi
1243: jae _C_LABEL(copystr_efault)
1244: movl $ENAMETOOLONG,%eax
1245: jmp copystr_return
1246:
1247: /* LINTSTUB: Ignore */
1248: NENTRY(copystr_efault)
1249: movl $EFAULT,%eax
1250:
1251: /* LINTSTUB: Ignore */
1252: NENTRY(copystr_fault)
1253: copystr_return:
1254: /* Set *lencopied and return %eax. */
1255: GET_CURPCB(%ecx)
1256: movl $0,PCB_ONFAULT(%ecx)
1257: movl 20(%esp),%ecx
1258: subl %edx,%ecx
1259: movl 24(%esp),%edx
1260: testl %edx,%edx
1261: jz 8f
1262: movl %ecx,(%edx)
1263:
1264: 8: popl %edi
1265: popl %esi
1266: ret
1267:
1268: /*
1269: * int copystr(const void *from, void *to, size_t maxlen, size_t *lencopied);
1270: * Copy a NUL-terminated string, at most maxlen characters long. Return the
1271: * number of characters copied (including the NUL) in *lencopied. If the
1272: * string is too long, return ENAMETOOLONG; else return 0.
1273: * see copystr(9)
1274: */
1275: /* LINTSTUB: Func: int copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done) */
1276: ENTRY(copystr)
1277: pushl %esi
1278: pushl %edi
1279:
1280: movl 12(%esp),%esi # esi = from
1281: movl 16(%esp),%edi # edi = to
1282: movl 20(%esp),%edx # edx = maxlen
1283: incl %edx
1284: cld
1285:
1286: 1: decl %edx
1287: jz 4f
1288: lodsb
1289: stosb
1290: testb %al,%al
1291: jnz 1b
1292:
1293: /* Success -- 0 byte reached. */
1294: decl %edx
1295: xorl %eax,%eax
1296: jmp 6f
1297:
1298: 4: /* edx is zero -- return ENAMETOOLONG. */
1299: movl $ENAMETOOLONG,%eax
1300:
1301: 6: /* Set *lencopied and return %eax. */
1302: movl 20(%esp),%ecx
1303: subl %edx,%ecx
1304: movl 24(%esp),%edx
1305: testl %edx,%edx
1306: jz 7f
1307: movl %ecx,(%edx)
1308:
1309: 7: popl %edi
1310: popl %esi
1311: ret
1312:
1313: /*
1314: * long fuword(const void *uaddr);
1315: * Fetch an int from the user's address space.
1316: * see fuword(9)
1317: */
1318: /* LINTSTUB: Func: long fuword(const void *base) */
1319: ENTRY(fuword)
1.24 yamt 1320: DO_DEFERRED_SWITCH(%eax)
1.1 fvdl 1321: movl 4(%esp),%edx
1322: cmpl $VM_MAXUSER_ADDRESS-4,%edx
1323: ja _C_LABEL(fusuaddrfault)
1324: GET_CURPCB(%ecx)
1325: movl $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
1326: movl (%edx),%eax
1327: movl $0,PCB_ONFAULT(%ecx)
1328: ret
1.30 junyoung 1329:
1.1 fvdl 1330: /*
1331: * int fusword(const void *uaddr);
1332: * Fetch a short from the user's address space.
1333: * see fusword(9)
1334: */
1335: /* LINTSTUB: Func: int fusword(const void *base) */
1336: ENTRY(fusword)
1.24 yamt 1337: DO_DEFERRED_SWITCH(%eax)
1.1 fvdl 1338: movl 4(%esp),%edx
1339: cmpl $VM_MAXUSER_ADDRESS-2,%edx
1340: ja _C_LABEL(fusuaddrfault)
1341: GET_CURPCB(%ecx)
1342: movl $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
1343: movzwl (%edx),%eax
1344: movl $0,PCB_ONFAULT(%ecx)
1345: ret
1.30 junyoung 1346:
1.1 fvdl 1347: /*
1348: * int fuswintr(const void *uaddr);
1349: * Fetch a short from the user's address space. Can be called during an
1350: * interrupt.
1351: * see fuswintr(9)
1352: */
1353: /* LINTSTUB: Func: int fuswintr(const void *base) */
1354: ENTRY(fuswintr)
1.24 yamt 1355: cmpl $TLBSTATE_VALID, CPUVAR(TLBSTATE)
1356: jnz _C_LABEL(fusuaddrfault)
1.1 fvdl 1357: movl 4(%esp),%edx
1358: cmpl $VM_MAXUSER_ADDRESS-2,%edx
1359: ja _C_LABEL(fusuaddrfault)
1.5 thorpej 1360: movl CPUVAR(CURLWP),%ecx
1361: movl L_ADDR(%ecx),%ecx
1.1 fvdl 1362: movl $_C_LABEL(fusubail),PCB_ONFAULT(%ecx)
1363: movzwl (%edx),%eax
1364: movl $0,PCB_ONFAULT(%ecx)
1365: ret
1.30 junyoung 1366:
1.1 fvdl 1367: /*
1368: * int fubyte(const void *uaddr);
1369: * Fetch a byte from the user's address space.
1370: * see fubyte(9)
1371: */
1372: /* LINTSTUB: Func: int fubyte(const void *base) */
1373: ENTRY(fubyte)
1.24 yamt 1374: DO_DEFERRED_SWITCH(%eax)
1.1 fvdl 1375: movl 4(%esp),%edx
1376: cmpl $VM_MAXUSER_ADDRESS-1,%edx
1377: ja _C_LABEL(fusuaddrfault)
1378: GET_CURPCB(%ecx)
1379: movl $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
1380: movzbl (%edx),%eax
1381: movl $0,PCB_ONFAULT(%ecx)
1382: ret
1383:
1384: /*
1385: * Handle faults from [fs]u*(). Clean up and return -1.
1386: */
1387: /* LINTSTUB: Ignore */
1388: NENTRY(fusufault)
1389: movl $0,PCB_ONFAULT(%ecx)
1390: movl $-1,%eax
1391: ret
1392:
1393: /*
1394: * Handle faults from [fs]u*(). Clean up and return -1. This differs from
1395: * fusufault() in that trap() will recognize it and return immediately rather
1396: * than trying to page fault.
1397: */
1398: /* LINTSTUB: Ignore */
1399: NENTRY(fusubail)
1400: movl $0,PCB_ONFAULT(%ecx)
1401: movl $-1,%eax
1402: ret
1403:
1404: /*
1405: * Handle earlier faults from [fs]u*(), due to our of range addresses.
1406: */
1407: /* LINTSTUB: Ignore */
1408: NENTRY(fusuaddrfault)
1409: movl $-1,%eax
1410: ret
1411:
1412: /*
1413: * int suword(void *uaddr, long x);
1414: * Store an int in the user's address space.
1415: * see suword(9)
1416: */
1417: /* LINTSTUB: Func: int suword(void *base, long c) */
1418: ENTRY(suword)
1.24 yamt 1419: DO_DEFERRED_SWITCH(%eax)
1.1 fvdl 1420: movl 4(%esp),%edx
1421: cmpl $VM_MAXUSER_ADDRESS-4,%edx
1422: ja _C_LABEL(fusuaddrfault)
1423:
1424: #if defined(I386_CPU)
1425: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
1426: cmpl $CPUCLASS_386,_C_LABEL(cpu_class)
1427: jne 2f
1428: #endif /* I486_CPU || I586_CPU || I686_CPU */
1429:
1430: GET_CURPCB(%eax)
1431: movl $3f,PCB_ONFAULT(%eax)
1432:
1433: movl %edx,%eax
1434: shrl $PGSHIFT,%eax # calculate pte address
1.31 junyoung 1435: testb $PG_RW,PTE_BASE(,%eax,4)
1.1 fvdl 1436: jnz 1f
1437:
1438: 3: /* Simulate a trap. */
1439: pushl %edx
1440: pushl %edx
1441: call _C_LABEL(trapwrite) # trapwrite(addr)
1442: addl $4,%esp # clear parameter from the stack
1443: popl %edx
1444: GET_CURPCB(%ecx)
1445: testl %eax,%eax
1446: jnz _C_LABEL(fusufault)
1447:
1448: 1: /* XXX also need to check the following 3 bytes for validity! */
1449: #endif
1450:
1451: 2: GET_CURPCB(%ecx)
1452: movl $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
1453:
1454: movl 8(%esp),%eax
1455: movl %eax,(%edx)
1456: xorl %eax,%eax
1457: movl %eax,PCB_ONFAULT(%ecx)
1458: ret
1.30 junyoung 1459:
1.1 fvdl 1460: /*
1461: * int susword(void *uaddr, short x);
1462: * Store a short in the user's address space.
1463: * see susword(9)
1464: */
1465: /* LINTSTUB: Func: int susword(void *base, short c) */
1466: ENTRY(susword)
1.24 yamt 1467: DO_DEFERRED_SWITCH(%eax)
1.1 fvdl 1468: movl 4(%esp),%edx
1469: cmpl $VM_MAXUSER_ADDRESS-2,%edx
1470: ja _C_LABEL(fusuaddrfault)
1471:
1472: #if defined(I386_CPU)
1473: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
1474: cmpl $CPUCLASS_386,_C_LABEL(cpu_class)
1475: jne 2f
1476: #endif /* I486_CPU || I586_CPU || I686_CPU */
1477:
1478: GET_CURPCB(%eax)
1479: movl $3f,PCB_ONFAULT(%eax)
1480:
1481: movl %edx,%eax
1482: shrl $PGSHIFT,%eax # calculate pte address
1.31 junyoung 1483: testb $PG_RW,PTE_BASE(,%eax,4)
1.1 fvdl 1484: jnz 1f
1485:
1486: 3: /* Simulate a trap. */
1487: pushl %edx
1488: pushl %edx
1489: call _C_LABEL(trapwrite) # trapwrite(addr)
1490: addl $4,%esp # clear parameter from the stack
1491: popl %edx
1492: GET_CURPCB(%ecx)
1493: testl %eax,%eax
1494: jnz _C_LABEL(fusufault)
1495:
1496: 1: /* XXX also need to check the following byte for validity! */
1497: #endif
1498:
1499: 2: GET_CURPCB(%ecx)
1500: movl $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
1501:
1502: movl 8(%esp),%eax
1503: movw %ax,(%edx)
1504: xorl %eax,%eax
1505: movl %eax,PCB_ONFAULT(%ecx)
1506: ret
1507:
1508: /*
1509: * int suswintr(void *uaddr, short x);
1510: * Store a short in the user's address space. Can be called during an
1511: * interrupt.
1512: * see suswintr(9)
1513: */
1514: /* LINTSTUB: Func: int suswintr(void *base, short c) */
1515: ENTRY(suswintr)
1.24 yamt 1516: cmpl $TLBSTATE_VALID, CPUVAR(TLBSTATE)
1517: jnz _C_LABEL(fusuaddrfault)
1.1 fvdl 1518: movl 4(%esp),%edx
1519: cmpl $VM_MAXUSER_ADDRESS-2,%edx
1520: ja _C_LABEL(fusuaddrfault)
1.5 thorpej 1521: movl CPUVAR(CURLWP),%ecx
1522: movl L_ADDR(%ecx),%ecx
1.1 fvdl 1523: movl $_C_LABEL(fusubail),PCB_ONFAULT(%ecx)
1524:
1525: #if defined(I386_CPU)
1526: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
1527: cmpl $CPUCLASS_386,_C_LABEL(cpu_class)
1528: jne 2f
1529: #endif /* I486_CPU || I586_CPU || I686_CPU */
1530:
1531: movl %edx,%eax
1532: shrl $PGSHIFT,%eax # calculate pte address
1.31 junyoung 1533: testb $PG_RW,PTE_BASE(,%eax,4)
1.1 fvdl 1534: jnz 1f
1535:
1536: /* Simulate a trap. */
1537: jmp _C_LABEL(fusubail)
1538:
1539: 1: /* XXX also need to check the following byte for validity! */
1540: #endif
1541:
1542: 2: movl 8(%esp),%eax
1543: movw %ax,(%edx)
1544: xorl %eax,%eax
1545: movl %eax,PCB_ONFAULT(%ecx)
1546: ret
1547:
1548: /*
1549: * int subyte(void *uaddr, char x);
1550: * Store a byte in the user's address space.
1551: * see subyte(9)
1552: */
1553: /* LINTSTUB: Func: int subyte(void *base, int c) */
1554: ENTRY(subyte)
1.24 yamt 1555: DO_DEFERRED_SWITCH(%eax)
1.1 fvdl 1556: movl 4(%esp),%edx
1557: cmpl $VM_MAXUSER_ADDRESS-1,%edx
1558: ja _C_LABEL(fusuaddrfault)
1559:
1560: #if defined(I386_CPU)
1561: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
1562: cmpl $CPUCLASS_386,_C_LABEL(cpu_class)
1563: jne 2f
1564: #endif /* I486_CPU || I586_CPU || I686_CPU */
1565:
1.30 junyoung 1566: GET_CURPCB(%eax)
1.1 fvdl 1567: movl $3f,PCB_ONFAULT(%eax)
1568:
1569: movl %edx,%eax
1570: shrl $PGSHIFT,%eax # calculate pte address
1.31 junyoung 1571: testb $PG_RW,PTE_BASE(,%eax,4)
1.1 fvdl 1572: jnz 1f
1573:
1574: 3: /* Simulate a trap. */
1575: pushl %edx
1576: pushl %edx
1577: call _C_LABEL(trapwrite) # trapwrite(addr)
1578: addl $4,%esp # clear parameter from the stack
1579: popl %edx
1580: GET_CURPCB(%ecx)
1581: testl %eax,%eax
1582: jnz _C_LABEL(fusufault)
1583:
1584: 1:
1585: #endif
1586:
1587: 2: GET_CURPCB(%ecx)
1588: movl $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
1589:
1590: movb 8(%esp),%al
1591: movb %al,(%edx)
1592: xorl %eax,%eax
1593: movl %eax,PCB_ONFAULT(%ecx)
1594: ret
1595:
1596: /*****************************************************************************/
1597:
1598: /*
1599: * The following is i386-specific nonsense.
1600: */
1601:
1602: /*
1603: * void lgdt(struct region_descriptor *rdp);
1604: * Load a new GDT pointer (and do any necessary cleanup).
1605: * XXX It's somewhat questionable whether reloading all the segment registers
1606: * is necessary, since the actual descriptor data is not changed except by
1607: * process creation and exit, both of which clean up via task switches. OTOH,
1608: * this only happens at run time when the GDT is resized.
1609: */
1610: /* LINTSTUB: Func: void lgdt(struct region_descriptor *rdp) */
1611: NENTRY(lgdt)
1612: /* Reload the descriptor table. */
1613: movl 4(%esp),%eax
1614: lgdt (%eax)
1615: /* Flush the prefetch queue. */
1616: jmp 1f
1617: nop
1618: 1: /* Reload "stale" selectors. */
1619: movl $GSEL(GDATA_SEL, SEL_KPL),%eax
1620: movw %ax,%ds
1621: movw %ax,%es
1622: movw %ax,%gs
1623: movw %ax,%ss
1624: movl $GSEL(GCPU_SEL, SEL_KPL),%eax
1625: movw %ax,%fs
1626: /* Reload code selector by doing intersegment return. */
1627: popl %eax
1628: pushl $GSEL(GCODE_SEL, SEL_KPL)
1629: pushl %eax
1630: lret
1631:
1632: /*****************************************************************************/
1633:
1634: /*
1635: * These functions are primarily used by DDB.
1636: */
1637:
1638: /* LINTSTUB: Func: int setjmp (label_t *l) */
1639: ENTRY(setjmp)
1640: movl 4(%esp),%eax
1641: movl %ebx,(%eax) # save ebx
1642: movl %esp,4(%eax) # save esp
1643: movl %ebp,8(%eax) # save ebp
1644: movl %esi,12(%eax) # save esi
1645: movl %edi,16(%eax) # save edi
1646: movl (%esp),%edx # get rta
1647: movl %edx,20(%eax) # save eip
1648: xorl %eax,%eax # return (0);
1649: ret
1650:
1651: /* LINTSTUB: Func: void longjmp (label_t *l) */
1652: ENTRY(longjmp)
1653: movl 4(%esp),%eax
1654: movl (%eax),%ebx # restore ebx
1655: movl 4(%eax),%esp # restore esp
1656: movl 8(%eax),%ebp # restore ebp
1657: movl 12(%eax),%esi # restore esi
1658: movl 16(%eax),%edi # restore edi
1659: movl 20(%eax),%edx # get rta
1660: movl %edx,(%esp) # put in return frame
1661: xorl %eax,%eax # return (1);
1662: incl %eax
1663: ret
1664:
1665: /*****************************************************************************/
1666:
1667: .globl _C_LABEL(sched_whichqs),_C_LABEL(sched_qs)
1668: .globl _C_LABEL(uvmexp),_C_LABEL(panic)
1669:
1670: #ifdef DIAGNOSTIC
1671: NENTRY(switch_error)
1672: pushl $1f
1673: call _C_LABEL(panic)
1674: /* NOTREACHED */
1675: 1: .asciz "cpu_switch"
1676: #endif /* DIAGNOSTIC */
1677:
1678: /*
1.5 thorpej 1679: * void cpu_switch(struct lwp *)
1.1 fvdl 1680: * Find a runnable process and switch to it. Wait if necessary. If the new
1681: * process is the same as the old one, we short-circuit the context save and
1682: * restore.
1.30 junyoung 1683: *
1.1 fvdl 1684: * Note that the stack frame layout is known to "struct switchframe"
1.30 junyoung 1685: * in <machine/frame.h> and to the code in cpu_fork() which initializes
1.5 thorpej 1686: * it for a new lwp.
1.1 fvdl 1687: */
1688: ENTRY(cpu_switch)
1689: pushl %ebx
1690: pushl %esi
1691: pushl %edi
1692:
1693: #ifdef DEBUG
1694: cmpl $IPL_SCHED,CPUVAR(ILEVEL)
1695: jae 1f
1.5 thorpej 1696: pushl $2f
1.1 fvdl 1697: call _C_LABEL(panic)
1698: /* NOTREACHED */
1.5 thorpej 1699: 2: .asciz "not splsched() in cpu_switch!"
1.30 junyoung 1700: 1:
1.1 fvdl 1701: #endif /* DEBUG */
1.30 junyoung 1702:
1.5 thorpej 1703: movl 16(%esp),%esi # current
1.1 fvdl 1704:
1705: /*
1.5 thorpej 1706: * Clear curlwp so that we don't accumulate system time while idle.
1707: * This also insures that schedcpu() will move the old lwp to
1.1 fvdl 1708: * the correct queue if it happens to get called from the spllower()
1709: * below and changes the priority. (See corresponding comment in
1710: * userret()).
1711: */
1.5 thorpej 1712: movl $0,CPUVAR(CURLWP)
1.1 fvdl 1713: /*
1.5 thorpej 1714: * First phase: find new lwp.
1.1 fvdl 1715: *
1716: * Registers:
1717: * %eax - queue head, scratch, then zero
1718: * %ebx - queue number
1719: * %ecx - cached value of whichqs
1.5 thorpej 1720: * %edx - next lwp in queue
1721: * %esi - old lwp
1722: * %edi - new lwp
1.1 fvdl 1723: */
1724:
1.5 thorpej 1725: /* Look for new lwp. */
1.1 fvdl 1726: cli # splhigh doesn't do a cli
1727: movl _C_LABEL(sched_whichqs),%ecx
1728: bsfl %ecx,%ebx # find a full q
1729: jnz switch_dequeue
1730:
1731: /*
1732: * idling: save old context.
1733: *
1734: * Registers:
1735: * %eax, %ecx - scratch
1.5 thorpej 1736: * %esi - old lwp, then old pcb
1.1 fvdl 1737: * %edi - idle pcb
1738: */
1739:
1740: pushl %esi
1.24 yamt 1741: call _C_LABEL(pmap_deactivate2) # pmap_deactivate(oldproc)
1.1 fvdl 1742: addl $4,%esp
1743:
1.5 thorpej 1744: movl L_ADDR(%esi),%esi
1.1 fvdl 1745:
1746: /* Save stack pointers. */
1747: movl %esp,PCB_ESP(%esi)
1748: movl %ebp,PCB_EBP(%esi)
1749:
1750: /* Find idle PCB for this CPU */
1751: #ifndef MULTIPROCESSOR
1.5 thorpej 1752: movl $_C_LABEL(lwp0),%ebx
1753: movl L_ADDR(%ebx),%edi
1754: movl L_MD_TSS_SEL(%ebx),%edx
1.1 fvdl 1755: #else
1756: movl CPUVAR(IDLE_PCB),%edi
1757: movl CPUVAR(IDLE_TSS_SEL),%edx
1758: #endif
1.5 thorpej 1759: movl $0,CPUVAR(CURLWP) /* In case we fault... */
1.1 fvdl 1760:
1761: /* Restore the idle context (avoid interrupts) */
1762: cli
1763:
1764: /* Restore stack pointers. */
1765: movl PCB_ESP(%edi),%esp
1766: movl PCB_EBP(%edi),%ebp
1767:
1768: /* Switch TSS. Reset "task busy" flag before loading. */
1.26 yamt 1769: movl %cr3,%eax
1770: movl %eax,PCB_CR3(%edi)
1.1 fvdl 1771: #ifdef MULTIPROCESSOR
1772: movl CPUVAR(GDT),%eax
1773: #else
1774: movl _C_LABEL(gdt),%eax
1775: #endif
1776: andl $~0x0200,4-SEL_KPL(%eax,%edx,1)
1777: ltr %dx
1778:
1779: /* We're always in the kernel, so we don't need the LDT. */
1780:
1781: /* Restore cr0 (including FPU state). */
1782: movl PCB_CR0(%edi),%ecx
1783: movl %ecx,%cr0
1784:
1785: /* Record new pcb. */
1786: SET_CURPCB(%edi)
1787:
1788: xorl %esi,%esi
1789: sti
1.30 junyoung 1790: idle_unlock:
1791: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1.1 fvdl 1792: call _C_LABEL(sched_unlock_idle)
1793: #endif
1794: /* Interrupts are okay again. */
1.2 fvdl 1795: pushl $IPL_NONE # spl0()
1.1 fvdl 1796: call _C_LABEL(Xspllower) # process pending interrupts
1.2 fvdl 1797: addl $4,%esp
1.1 fvdl 1798: jmp idle_start
1.30 junyoung 1799: idle_zero:
1.1 fvdl 1800: sti
1801: call _C_LABEL(uvm_pageidlezero)
1802: cli
1803: cmpl $0,_C_LABEL(sched_whichqs)
1804: jnz idle_exit
1805: idle_loop:
1806: /* Try to zero some pages. */
1807: movl _C_LABEL(uvm)+UVM_PAGE_IDLE_ZERO,%ecx
1808: testl %ecx,%ecx
1809: jnz idle_zero
1810: sti
1811: hlt
1812: NENTRY(mpidle)
1.30 junyoung 1813: idle_start:
1.1 fvdl 1814: cli
1815: cmpl $0,_C_LABEL(sched_whichqs)
1816: jz idle_loop
1.30 junyoung 1817: idle_exit:
1.1 fvdl 1818: movl $IPL_HIGH,CPUVAR(ILEVEL) # splhigh
1.14 fvdl 1819: sti
1.30 junyoung 1820: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1.1 fvdl 1821: call _C_LABEL(sched_lock_idle)
1822: #endif
1823: movl _C_LABEL(sched_whichqs),%ecx
1824: bsfl %ecx,%ebx
1825: jz idle_unlock
1826:
1.30 junyoung 1827: switch_dequeue:
1828: /*
1.1 fvdl 1829: * we're running at splhigh(), but it's otherwise okay to take
1.30 junyoung 1830: * interrupts here.
1.1 fvdl 1831: */
1832: sti
1833: leal _C_LABEL(sched_qs)(,%ebx,8),%eax # select q
1834:
1.5 thorpej 1835: movl L_FORW(%eax),%edi # unlink from front of process q
1.1 fvdl 1836: #ifdef DIAGNOSTIC
1837: cmpl %edi,%eax # linked to self (i.e. nothing queued)?
1838: je _C_LABEL(switch_error) # not possible
1839: #endif /* DIAGNOSTIC */
1.5 thorpej 1840: movl L_FORW(%edi),%edx
1841: movl %edx,L_FORW(%eax)
1842: movl %eax,L_BACK(%edx)
1.1 fvdl 1843:
1844: cmpl %edx,%eax # q empty?
1845: jne 3f
1846:
1847: btrl %ebx,%ecx # yes, clear to indicate empty
1848: movl %ecx,_C_LABEL(sched_whichqs) # update q status
1849:
1850: 3: /* We just did it. */
1851: xorl %eax,%eax
1852: CLEAR_RESCHED(%eax)
1853:
1.5 thorpej 1854: switch_resume:
1.1 fvdl 1855: #ifdef DIAGNOSTIC
1.5 thorpej 1856: cmpl %eax,L_WCHAN(%edi) # Waiting for something?
1.1 fvdl 1857: jne _C_LABEL(switch_error) # Yes; shouldn't be queued.
1.5 thorpej 1858: cmpb $LSRUN,L_STAT(%edi) # In run state?
1.1 fvdl 1859: jne _C_LABEL(switch_error) # No; shouldn't be queued.
1860: #endif /* DIAGNOSTIC */
1861:
1.5 thorpej 1862: /* Isolate lwp. XXX Is this necessary? */
1863: movl %eax,L_BACK(%edi)
1.1 fvdl 1864:
1.5 thorpej 1865: /* Record new lwp. */
1866: movb $LSONPROC,L_STAT(%edi) # l->l_stat = LSONPROC
1867: SET_CURLWP(%edi,%ecx)
1.1 fvdl 1868:
1.5 thorpej 1869: /* Skip context switch if same lwp. */
1.10 fvdl 1870: xorl %ebx,%ebx
1.1 fvdl 1871: cmpl %edi,%esi
1872: je switch_return
1873:
1.5 thorpej 1874: /* If old lwp exited, don't bother. */
1.1 fvdl 1875: testl %esi,%esi
1876: jz switch_exited
1877:
1878: /*
1879: * Second phase: save old context.
1880: *
1881: * Registers:
1882: * %eax, %ecx - scratch
1.5 thorpej 1883: * %esi - old lwp, then old pcb
1884: * %edi - new lwp
1.1 fvdl 1885: */
1886:
1887: pushl %esi
1.24 yamt 1888: call _C_LABEL(pmap_deactivate2) # pmap_deactivate(oldproc)
1.1 fvdl 1889: addl $4,%esp
1890:
1.5 thorpej 1891: movl L_ADDR(%esi),%esi
1.1 fvdl 1892:
1893: /* Save stack pointers. */
1894: movl %esp,PCB_ESP(%esi)
1895: movl %ebp,PCB_EBP(%esi)
1896:
1897: switch_exited:
1898: /*
1899: * Third phase: restore saved context.
1900: *
1901: * Registers:
1902: * %eax, %ebx, %ecx, %edx - scratch
1903: * %esi - new pcb
1.5 thorpej 1904: * %edi - new lwp
1.1 fvdl 1905: */
1906:
1907: /* No interrupts while loading new state. */
1908: cli
1.5 thorpej 1909: movl L_ADDR(%edi),%esi
1.1 fvdl 1910:
1911: /* Restore stack pointers. */
1912: movl PCB_ESP(%esi),%esp
1913: movl PCB_EBP(%esi),%ebp
1914:
1915: #if 0
1916: /* Don't bother with the rest if switching to a system process. */
1.5 thorpej 1917: testl $P_SYSTEM,L_FLAG(%edi); XXX NJWLWP lwp's don't have P_SYSTEM!
1.1 fvdl 1918: jnz switch_restored
1919: #endif
1920:
1.26 yamt 1921: /* Switch TSS. Reset "task busy" flag before loading. */
1922: movl %cr3,%eax
1923: movl %eax,PCB_CR3(%esi) /* XXX should be done by pmap_activate? */
1.1 fvdl 1924: #ifdef MULTIPROCESSOR
1925: movl CPUVAR(GDT),%eax
1.30 junyoung 1926: #else
1.1 fvdl 1927: /* Load TSS info. */
1928: movl _C_LABEL(gdt),%eax
1929: #endif
1.5 thorpej 1930: movl L_MD_TSS_SEL(%edi),%edx
1.1 fvdl 1931:
1932: andl $~0x0200,4(%eax,%edx, 1)
1933: ltr %dx
1934:
1935: pushl %edi
1936: call _C_LABEL(pmap_activate) # pmap_activate(p)
1937: addl $4,%esp
1938:
1939: #if 0
1940: switch_restored:
1941: #endif
1942: /* Restore cr0 (including FPU state). */
1943: movl PCB_CR0(%esi),%ecx
1944: #ifdef MULTIPROCESSOR
1.30 junyoung 1945: /*
1.22 wiz 1946: * If our floating point registers are on a different CPU,
1.1 fvdl 1947: * clear CR0_TS so we'll trap rather than reuse bogus state.
1948: */
1949: movl PCB_FPCPU(%esi),%ebx
1950: cmpl CPUVAR(SELF),%ebx
1951: jz 1f
1952: orl $CR0_TS,%ecx
1.30 junyoung 1953: 1:
1954: #endif
1.1 fvdl 1955: movl %ecx,%cr0
1956:
1957: /* Record new pcb. */
1958: SET_CURPCB(%esi)
1959:
1960: /* Interrupts are okay again. */
1961: sti
1962:
1963: /*
1964: * Check for restartable atomic sequences (RAS)
1965: */
1.5 thorpej 1966: movl CPUVAR(CURLWP),%edi
1967: movl L_PROC(%edi),%esi
1.20 dsl 1968: cmpl $0,P_RASLIST(%esi)
1969: jne 2f
1.1 fvdl 1970: 1:
1.10 fvdl 1971: movl $1,%ebx
1.1 fvdl 1972:
1973: switch_return:
1.30 junyoung 1974: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1.1 fvdl 1975: call _C_LABEL(sched_unlock_idle)
1976: #endif
1.28 yamt 1977: cmpl $0,CPUVAR(IPENDING)
1978: jz 3f
1.2 fvdl 1979: pushl $IPL_NONE # spl0()
1.1 fvdl 1980: call _C_LABEL(Xspllower) # process pending interrupts
1.2 fvdl 1981: addl $4,%esp
1.28 yamt 1982: 3:
1.1 fvdl 1983: movl $IPL_HIGH,CPUVAR(ILEVEL) # splhigh()
1.9 fvdl 1984:
1985: movl %ebx,%eax
1.30 junyoung 1986:
1.1 fvdl 1987: popl %edi
1988: popl %esi
1989: popl %ebx
1990: ret
1.20 dsl 1991:
1992: 2: # check RAS list
1993: movl L_MD_REGS(%edi),%ebx
1994: movl TF_EIP(%ebx),%eax
1995: pushl %eax
1996: pushl %esi
1997: call _C_LABEL(ras_lookup)
1998: addl $8,%esp
1999: cmpl $-1,%eax
2000: je 1b
2001: movl %eax,TF_EIP(%ebx)
2002: jmp 1b
1.1 fvdl 2003:
2004: /*
1.5 thorpej 2005: * void cpu_switchto(struct lwp *current, struct lwp *next)
2006: * Switch to the specified next LWP.
2007: */
2008: ENTRY(cpu_switchto)
2009: pushl %ebx
2010: pushl %esi
2011: pushl %edi
2012:
2013: #ifdef DEBUG
2014: cmpl $IPL_SCHED,CPUVAR(ILEVEL)
2015: jae 1f
2016: pushl $2f
2017: call _C_LABEL(panic)
2018: /* NOTREACHED */
2019: 2: .asciz "not splsched() in cpu_switchto!"
2020: 1:
2021: #endif /* DEBUG */
2022:
2023: movl 16(%esp),%esi # current
2024: movl 20(%esp),%edi # next
2025:
2026: /*
2027: * Clear curlwp so that we don't accumulate system time while idle.
2028: * This also insures that schedcpu() will move the old process to
2029: * the correct queue if it happens to get called from the spllower()
2030: * below and changes the priority. (See corresponding comment in
2031: * usrret()).
2032: *
2033: * XXX Is this necessary? We know we won't go idle.
2034: */
2035: movl $0,CPUVAR(CURLWP)
2036:
2037: /*
2038: * We're running at splhigh(), but it's otherwise okay to take
2039: * interrupts here.
2040: */
2041: sti
2042:
2043: /* Jump into the middle of cpu_switch */
2044: xorl %eax,%eax
2045: jmp switch_resume
2046:
2047: /*
1.21 jdolecek 2048: * void cpu_exit(struct lwp *l)
1.30 junyoung 2049: * Switch to the appropriate idle context (lwp0's if uniprocessor; the CPU's
2050: * if multiprocessor) and deallocate the address space and kernel stack for p.
1.1 fvdl 2051: * Then jump into cpu_switch(), as if we were in the idle proc all along.
2052: */
2053: #ifndef MULTIPROCESSOR
1.5 thorpej 2054: .globl _C_LABEL(lwp0)
1.1 fvdl 2055: #endif
2056: .globl _C_LABEL(uvmspace_free),_C_LABEL(kernel_map)
2057: .globl _C_LABEL(uvm_km_free),_C_LABEL(tss_free)
1.21 jdolecek 2058: /* LINTSTUB: Func: void cpu_exit(struct lwp *l) */
2059: ENTRY(cpu_exit)
1.1 fvdl 2060: movl 4(%esp),%edi # old process
2061: #ifndef MULTIPROCESSOR
1.5 thorpej 2062: movl $_C_LABEL(lwp0),%ebx
2063: movl L_ADDR(%ebx),%esi
2064: movl L_MD_TSS_SEL(%ebx),%edx
1.1 fvdl 2065: #else
2066: movl CPUVAR(IDLE_PCB),%esi
2067: movl CPUVAR(IDLE_TSS_SEL),%edx
2068: #endif
2069: /* In case we fault... */
1.5 thorpej 2070: movl $0,CPUVAR(CURLWP)
1.1 fvdl 2071:
2072: /* Restore the idle context. */
2073: cli
2074:
2075: /* Restore stack pointers. */
2076: movl PCB_ESP(%esi),%esp
2077: movl PCB_EBP(%esi),%ebp
2078:
1.26 yamt 2079: /* Switch TSS. Reset "task busy" flag before loading. */
2080: movl %cr3,%eax
2081: movl %eax,PCB_CR3(%esi)
1.1 fvdl 2082: #ifdef MULTIPROCESSOR
2083: movl CPUVAR(GDT),%eax
1.30 junyoung 2084: #else
1.1 fvdl 2085: /* Load TSS info. */
2086: movl _C_LABEL(gdt),%eax
2087: #endif
2088:
2089: andl $~0x0200,4-SEL_KPL(%eax,%edx,1)
2090: ltr %dx
2091:
2092: /* We're always in the kernel, so we don't need the LDT. */
2093:
2094: /* Restore cr0 (including FPU state). */
2095: movl PCB_CR0(%esi),%ecx
2096: movl %ecx,%cr0
2097:
2098: /* Record new pcb. */
2099: SET_CURPCB(%esi)
2100:
2101: /* Interrupts are okay again. */
2102: sti
2103:
2104: /*
1.21 jdolecek 2105: * Schedule the dead LWP's stack to be freed.
1.1 fvdl 2106: */
1.21 jdolecek 2107: pushl %edi
2108: call _C_LABEL(lwp_exit2)
1.1 fvdl 2109: addl $4,%esp
2110:
2111: /* Jump into cpu_switch() with the right state. */
2112: xorl %esi,%esi
1.5 thorpej 2113: movl %esi,CPUVAR(CURLWP)
1.1 fvdl 2114: jmp idle_start
2115:
2116: /*
2117: * void savectx(struct pcb *pcb);
2118: * Update pcb, saving current processor state.
2119: */
2120: /* LINTSTUB: Func: void savectx(struct pcb *pcb) */
2121: ENTRY(savectx)
2122: movl 4(%esp),%edx # edx = p->p_addr
1.30 junyoung 2123:
1.1 fvdl 2124: /* Save stack pointers. */
2125: movl %esp,PCB_ESP(%edx)
2126: movl %ebp,PCB_EBP(%edx)
2127:
2128: ret
2129:
2130: /*
2131: * Old call gate entry for syscall
2132: */
2133: /* LINTSTUB: Var: char Xosyscall[1]; */
2134: IDTVEC(osyscall)
2135: /* Set eflags in trap frame. */
2136: pushfl
2137: popl 8(%esp)
2138: pushl $7 # size of instruction for restart
2139: jmp syscall1
2140:
2141: /*
2142: * Trap gate entry for syscall
2143: */
2144: /* LINTSTUB: Var: char Xsyscall[1]; */
2145: IDTVEC(syscall)
2146: pushl $2 # size of instruction for restart
2147: syscall1:
2148: pushl $T_ASTFLT # trap # for doing ASTs
2149: INTRENTRY
2150:
2151: #ifdef DIAGNOSTIC
1.24 yamt 2152: cmpl $0, CPUVAR(WANT_PMAPLOAD)
2153: jz 1f
2154: pushl $6f
2155: call _C_LABEL(printf)
2156: addl $4, %esp
2157: 1:
1.1 fvdl 2158: movl CPUVAR(ILEVEL),%ebx
2159: testl %ebx,%ebx
2160: jz 1f
2161: pushl $5f
2162: call _C_LABEL(printf)
2163: addl $4,%esp
2164: #ifdef DDB
2165: int $3
2166: #endif
1.30 junyoung 2167: 1:
1.1 fvdl 2168: #endif /* DIAGNOSTIC */
1.5 thorpej 2169: movl CPUVAR(CURLWP),%edx
2170: movl %esp,L_MD_REGS(%edx) # save pointer to frame
2171: movl L_PROC(%edx),%edx
1.15 fvdl 2172: pushl %esp
1.1 fvdl 2173: call *P_MD_SYSCALL(%edx) # get pointer to syscall() function
1.15 fvdl 2174: addl $4,%esp
1.27 yamt 2175: .Lsyscall_checkast:
1.24 yamt 2176: /* Check for ASTs on exit to user mode. */
1.1 fvdl 2177: cli
1.5 thorpej 2178: CHECK_ASTPENDING(%eax)
1.1 fvdl 2179: je 1f
2180: /* Always returning to user mode here. */
1.5 thorpej 2181: CLEAR_ASTPENDING(%eax)
1.1 fvdl 2182: sti
2183: /* Pushed T_ASTFLT into tf_trapno on entry. */
1.15 fvdl 2184: pushl %esp
1.1 fvdl 2185: call _C_LABEL(trap)
1.15 fvdl 2186: addl $4,%esp
1.27 yamt 2187: jmp .Lsyscall_checkast /* re-check ASTs */
1.24 yamt 2188: 1: CHECK_DEFERRED_SWITCH(%eax)
2189: jnz 9f
1.1 fvdl 2190: #ifndef DIAGNOSTIC
1.24 yamt 2191: INTRFASTEXIT
1.1 fvdl 2192: #else /* DIAGNOSTIC */
1.24 yamt 2193: cmpl $IPL_NONE,CPUVAR(ILEVEL)
1.1 fvdl 2194: jne 3f
2195: INTRFASTEXIT
2196: 3: sti
2197: pushl $4f
2198: call _C_LABEL(printf)
2199: addl $4,%esp
2200: #ifdef DDB
2201: int $3
2202: #endif /* DDB */
2203: movl $IPL_NONE,CPUVAR(ILEVEL)
2204: jmp 2b
2205: 4: .asciz "WARNING: SPL NOT LOWERED ON SYSCALL EXIT\n"
1.30 junyoung 2206: 5: .asciz "WARNING: SPL NOT ZERO ON SYSCALL ENTRY\n"
2207: 6: .asciz "WARNING: WANT PMAPLOAD ON SYSCALL ENTRY\n"
1.1 fvdl 2208: #endif /* DIAGNOSTIC */
1.24 yamt 2209: 9: sti
2210: call _C_LABEL(pmap_load)
1.27 yamt 2211: jmp .Lsyscall_checkast /* re-check ASTs */
1.1 fvdl 2212:
2213: #if NNPX > 0
2214: /*
2215: * Special interrupt handlers. Someday intr0-intr15 will be used to count
2216: * interrupts. We'll still need a special exception 16 handler. The busy
2217: * latch stuff in probintr() can be moved to npxprobe().
2218: */
2219:
2220: /* LINTSTUB: Func: void probeintr(void) */
2221: NENTRY(probeintr)
2222: ss
2223: incl _C_LABEL(npx_intrs_while_probing)
2224: pushl %eax
2225: movb $0x20,%al # EOI (asm in strings loses cpp features)
2226: outb %al,$0xa0 # IO_ICU2
2227: outb %al,$0x20 # IO_ICU1
2228: movb $0,%al
2229: outb %al,$0xf0 # clear BUSY# latch
2230: popl %eax
2231: iret
2232:
2233: /* LINTSTUB: Func: void probetrap(void) */
2234: NENTRY(probetrap)
2235: ss
2236: incl _C_LABEL(npx_traps_while_probing)
2237: fnclex
2238: iret
2239:
2240: /* LINTSTUB: Func: int npx586bug1(int a, int b) */
2241: NENTRY(npx586bug1)
2242: fildl 4(%esp) # x
2243: fildl 8(%esp) # y
2244: fld %st(1)
2245: fdiv %st(1),%st # x/y
2246: fmulp %st,%st(1) # (x/y)*y
2247: fsubrp %st,%st(1) # x-(x/y)*y
2248: pushl $0
2249: fistpl (%esp)
2250: popl %eax
2251: ret
2252: #endif /* NNPX > 0 */
CVSweb <webmaster@jp.NetBSD.org>