vm_machdep.c revision 32702
1/*-
2 * Copyright (c) 1982, 1986 The Regents of the University of California.
3 * Copyright (c) 1989, 1990 William Jolitz
4 * Copyright (c) 1994 John Dyson
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department, and William Jolitz.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the University of
22 *	California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
40 *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
41 *	$Id: vm_machdep.c,v 1.95 1998/01/19 04:16:16 tegge Exp $
42 */
43
44#include "npx.h"
45#include "opt_bounce.h"
46#include "opt_user_ldt.h"
47#include "opt_vm86.h"
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/proc.h>
52#include <sys/malloc.h>
53#include <sys/buf.h>
54#include <sys/vnode.h>
55#include <sys/vmmeter.h>
56
57#include <machine/clock.h>
58#include <machine/cpu.h>
59#include <machine/md_var.h>
60#ifdef SMP
61#include <machine/smp.h>
62#endif
63
64#include <vm/vm.h>
65#include <vm/vm_param.h>
66#include <vm/vm_prot.h>
67#include <sys/lock.h>
68#include <vm/vm_kern.h>
69#include <vm/vm_page.h>
70#include <vm/vm_map.h>
71#include <vm/vm_extern.h>
72
73#include <sys/user.h>
74
75#ifdef PC98
76#include <pc98/pc98/pc98.h>
77#else
78#include <i386/isa/isa.h>
79#endif
80
81#ifdef BOUNCE_BUFFERS
82static vm_offset_t
83		vm_bounce_kva __P((int size, int waitok));
84static void	vm_bounce_kva_free __P((vm_offset_t addr, vm_offset_t size,
85					int now));
86static vm_offset_t
87		vm_bounce_page_find __P((int count));
88static void	vm_bounce_page_free __P((vm_offset_t pa, int count));
89
90static volatile int	kvasfreecnt;
91
92caddr_t		bouncememory;
93static int	bpwait;
94static vm_offset_t	*bouncepa;
95static int		bmwait, bmfreeing;
96
97#define BITS_IN_UNSIGNED (8*sizeof(unsigned))
98static int		bounceallocarraysize;
99static unsigned	*bounceallocarray;
100static int		bouncefree;
101
102#if defined(PC98) && defined (EPSON_BOUNCEDMA)
103#define SIXTEENMEG (3840*4096)			/* 15MB boundary */
104#else
105#define SIXTEENMEG (4096*4096)
106#endif
107#define MAXBKVA 1024
108int		maxbkva = MAXBKVA*PAGE_SIZE;
109
110/* special list that can be used at interrupt time for eventual kva free */
111static struct kvasfree {
112	vm_offset_t addr;
113	vm_offset_t size;
114} kvaf[MAXBKVA];
115
116/*
117 * get bounce buffer pages (count physically contiguous)
118 * (only 1 inplemented now)
119 */
120static vm_offset_t
121vm_bounce_page_find(count)
122	int count;
123{
124	int bit;
125	int s,i;
126
127	if (count != 1)
128		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
129
130	s = splbio();
131retry:
132	for (i = 0; i < bounceallocarraysize; i++) {
133		if (bounceallocarray[i] != 0xffffffff) {
134			bit = ffs(~bounceallocarray[i]);
135			if (bit) {
136				bounceallocarray[i] |= 1 << (bit - 1) ;
137				bouncefree -= count;
138				splx(s);
139				return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))];
140			}
141		}
142	}
143	bpwait = 1;
144	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
145	goto retry;
146}
147
148static void
149vm_bounce_kva_free(addr, size, now)
150	vm_offset_t addr;
151	vm_offset_t size;
152	int now;
153{
154	int s = splbio();
155	kvaf[kvasfreecnt].addr = addr;
156	kvaf[kvasfreecnt].size = size;
157	++kvasfreecnt;
158	if( now) {
159		/*
160		 * this will do wakeups
161		 */
162		vm_bounce_kva(0,0);
163	} else {
164		if (bmwait) {
165		/*
166		 * if anyone is waiting on the bounce-map, then wakeup
167		 */
168			wakeup((caddr_t) io_map);
169			bmwait = 0;
170		}
171	}
172	splx(s);
173}
174
175/*
176 * free count bounce buffer pages
177 */
178static void
179vm_bounce_page_free(pa, count)
180	vm_offset_t pa;
181	int count;
182{
183	int allocindex;
184	int index;
185	int bit;
186
187	if (count != 1)
188		panic("vm_bounce_page_free -- no support for > 1 page yet!!!");
189
190	for(index=0;index<bouncepages;index++) {
191		if( pa == bouncepa[index])
192			break;
193	}
194
195	if( index == bouncepages)
196		panic("vm_bounce_page_free: invalid bounce buffer");
197
198	allocindex = index / BITS_IN_UNSIGNED;
199	bit = index % BITS_IN_UNSIGNED;
200
201	bounceallocarray[allocindex] &= ~(1 << bit);
202
203	bouncefree += count;
204	if (bpwait) {
205		bpwait = 0;
206		wakeup((caddr_t) &bounceallocarray);
207	}
208}
209
210/*
211 * allocate count bounce buffer kva pages
212 */
213static vm_offset_t
214vm_bounce_kva(size, waitok)
215	int size;
216	int waitok;
217{
218	int i;
219	vm_offset_t kva = 0;
220	vm_offset_t off;
221	int s = splbio();
222more:
223	if (!bmfreeing && kvasfreecnt) {
224		bmfreeing = 1;
225		for (i = 0; i < kvasfreecnt; i++) {
226			for(off=0;off<kvaf[i].size;off+=PAGE_SIZE) {
227				pmap_kremove( kvaf[i].addr + off);
228			}
229			kmem_free_wakeup(io_map, kvaf[i].addr,
230				kvaf[i].size);
231		}
232		kvasfreecnt = 0;
233		bmfreeing = 0;
234		if( bmwait) {
235			bmwait = 0;
236			wakeup( (caddr_t) io_map);
237		}
238	}
239
240	if( size == 0) {
241		splx(s);
242		return 0;
243	}
244
245	if ((kva = kmem_alloc_pageable(io_map, size)) == 0) {
246		if( !waitok) {
247			splx(s);
248			return 0;
249		}
250		bmwait = 1;
251		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
252		goto more;
253	}
254	splx(s);
255	return kva;
256}
257
258/*
259 * same as vm_bounce_kva -- but really allocate (but takes pages as arg)
260 */
261vm_offset_t
262vm_bounce_kva_alloc(count)
263int count;
264{
265	int i;
266	vm_offset_t kva;
267	vm_offset_t pa;
268	if( bouncepages == 0) {
269		kva = (vm_offset_t) malloc(count*PAGE_SIZE, M_TEMP, M_WAITOK);
270		return kva;
271	}
272	kva = vm_bounce_kva(count*PAGE_SIZE, 1);
273	for(i=0;i<count;i++) {
274		pa = vm_bounce_page_find(1);
275		pmap_kenter(kva + i * PAGE_SIZE, pa);
276	}
277	return kva;
278}
279
280/*
281 * same as vm_bounce_kva_free -- but really free
282 */
283void
284vm_bounce_kva_alloc_free(kva, count)
285	vm_offset_t kva;
286	int count;
287{
288	int i;
289	vm_offset_t pa;
290	if( bouncepages == 0) {
291		free((caddr_t) kva, M_TEMP);
292		return;
293	}
294	for(i = 0; i < count; i++) {
295		pa = pmap_kextract(kva + i * PAGE_SIZE);
296		vm_bounce_page_free(pa, 1);
297	}
298	vm_bounce_kva_free(kva, count*PAGE_SIZE, 0);
299}
300
301/*
302 * do the things necessary to the struct buf to implement
303 * bounce buffers...  inserted before the disk sort
304 */
305void
306vm_bounce_alloc(bp)
307	struct buf *bp;
308{
309	int countvmpg;
310	vm_offset_t vastart, vaend;
311	vm_offset_t vapstart, vapend;
312	vm_offset_t va, kva;
313	vm_offset_t pa;
314	int dobounceflag = 0;
315	int i;
316
317	if (bouncepages == 0)
318		return;
319
320	if (bp->b_flags & B_BOUNCE) {
321		printf("vm_bounce_alloc: called recursively???\n");
322		return;
323	}
324
325	if (bp->b_bufsize < bp->b_bcount) {
326		printf(
327		    "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n",
328			bp->b_bufsize, bp->b_bcount);
329		panic("vm_bounce_alloc");
330	}
331
332/*
333 *  This is not really necessary
334 *	if( bp->b_bufsize != bp->b_bcount) {
335 *		printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount);
336 *	}
337 */
338
339
340	vastart = (vm_offset_t) bp->b_data;
341	vaend = (vm_offset_t) bp->b_data + bp->b_bufsize;
342
343	vapstart = trunc_page(vastart);
344	vapend = round_page(vaend);
345	countvmpg = (vapend - vapstart) / PAGE_SIZE;
346
347/*
348 * if any page is above 16MB, then go into bounce-buffer mode
349 */
350	va = vapstart;
351	for (i = 0; i < countvmpg; i++) {
352		pa = pmap_kextract(va);
353		if (pa >= SIXTEENMEG)
354			++dobounceflag;
355		if( pa == 0)
356			panic("vm_bounce_alloc: Unmapped page");
357		va += PAGE_SIZE;
358	}
359	if (dobounceflag == 0)
360		return;
361
362	if (bouncepages < dobounceflag)
363		panic("Not enough bounce buffers!!!");
364
365/*
366 * allocate a replacement kva for b_addr
367 */
368	kva = vm_bounce_kva(countvmpg*PAGE_SIZE, 1);
369#if 0
370	printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ",
371		(bp->b_flags & B_READ) ? "read":"write",
372			vapstart, vapend, countvmpg, kva);
373#endif
374	va = vapstart;
375	for (i = 0; i < countvmpg; i++) {
376		pa = pmap_kextract(va);
377		if (pa >= SIXTEENMEG) {
378			/*
379			 * allocate a replacement page
380			 */
381			vm_offset_t bpa = vm_bounce_page_find(1);
382			pmap_kenter(kva + (PAGE_SIZE * i), bpa);
383#if 0
384			printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa);
385#endif
386			/*
387			 * if we are writing, the copy the data into the page
388			 */
389			if ((bp->b_flags & B_READ) == 0) {
390				bcopy((caddr_t) va, (caddr_t) kva + (PAGE_SIZE * i), PAGE_SIZE);
391			}
392		} else {
393			/*
394			 * use original page
395			 */
396			pmap_kenter(kva + (PAGE_SIZE * i), pa);
397		}
398		va += PAGE_SIZE;
399	}
400
401/*
402 * flag the buffer as being bounced
403 */
404	bp->b_flags |= B_BOUNCE;
405/*
406 * save the original buffer kva
407 */
408	bp->b_savekva = bp->b_data;
409/*
410 * put our new kva into the buffer (offset by original offset)
411 */
412	bp->b_data = (caddr_t) (((vm_offset_t) kva) |
413				((vm_offset_t) bp->b_savekva & PAGE_MASK));
414#if 0
415	printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data);
416#endif
417	return;
418}
419
420/*
421 * hook into biodone to free bounce buffer
422 */
423void
424vm_bounce_free(bp)
425	struct buf *bp;
426{
427	int i;
428	vm_offset_t origkva, bouncekva, bouncekvaend;
429
430/*
431 * if this isn't a bounced buffer, then just return
432 */
433	if ((bp->b_flags & B_BOUNCE) == 0)
434		return;
435
436/*
437 *  This check is not necessary
438 *	if (bp->b_bufsize != bp->b_bcount) {
439 *		printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n",
440 *			bp->b_bufsize, bp->b_bcount);
441 *	}
442 */
443
444	origkva = (vm_offset_t) bp->b_savekva;
445	bouncekva = (vm_offset_t) bp->b_data;
446/*
447	printf("free: %d ", bp->b_bufsize);
448*/
449
450/*
451 * check every page in the kva space for b_addr
452 */
453	for (i = 0; i < bp->b_bufsize; ) {
454		vm_offset_t mybouncepa;
455		vm_offset_t copycount;
456
457		copycount = round_page(bouncekva + 1) - bouncekva;
458		mybouncepa = pmap_kextract(trunc_page(bouncekva));
459
460/*
461 * if this is a bounced pa, then process as one
462 */
463		if ( mybouncepa != pmap_kextract( trunc_page( origkva))) {
464			vm_offset_t tocopy = copycount;
465			if (i + tocopy > bp->b_bufsize)
466				tocopy = bp->b_bufsize - i;
467/*
468 * if this is a read, then copy from bounce buffer into original buffer
469 */
470			if (bp->b_flags & B_READ)
471				bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy);
472/*
473 * free the bounce allocation
474 */
475
476/*
477			printf("(kva: %x, pa: %x)", bouncekva, mybouncepa);
478*/
479			vm_bounce_page_free(mybouncepa, 1);
480		}
481
482		origkva += copycount;
483		bouncekva += copycount;
484		i += copycount;
485	}
486
487/*
488	printf("\n");
489*/
490/*
491 * add the old kva into the "to free" list
492 */
493
494	bouncekva= trunc_page((vm_offset_t) bp->b_data);
495	bouncekvaend= round_page((vm_offset_t)bp->b_data + bp->b_bufsize);
496
497/*
498	printf("freeva: %d\n", (bouncekvaend - bouncekva) / PAGE_SIZE);
499*/
500	vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0);
501	bp->b_data = bp->b_savekva;
502	bp->b_savekva = 0;
503	bp->b_flags &= ~B_BOUNCE;
504
505	return;
506}
507
508
509/*
510 * init the bounce buffer system
511 */
512void
513vm_bounce_init()
514{
515	int i;
516
517	kvasfreecnt = 0;
518
519	if (bouncepages == 0)
520		return;
521
522	bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED;
523	bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT);
524
525	if (!bounceallocarray)
526		panic("Cannot allocate bounce resource array");
527
528	bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT);
529	if (!bouncepa)
530		panic("Cannot allocate physical memory array");
531
532	for(i=0;i<bounceallocarraysize;i++) {
533		bounceallocarray[i] = 0xffffffff;
534	}
535
536	for(i=0;i<bouncepages;i++) {
537		vm_offset_t pa;
538		if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * PAGE_SIZE)) >= SIXTEENMEG)
539			panic("bounce memory out of range");
540		if( pa == 0)
541			panic("bounce memory not resident");
542		bouncepa[i] = pa;
543		bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int))));
544	}
545	bouncefree = bouncepages;
546
547}
548#endif /* BOUNCE_BUFFERS */
549
550/*
551 * quick version of vm_fault
552 */
553void
554vm_fault_quick(v, prot)
555	caddr_t v;
556	int prot;
557{
558	if (prot & VM_PROT_WRITE)
559		subyte(v, fubyte(v));
560	else
561		fubyte(v);
562}
563
564/*
565 * Finish a fork operation, with process p2 nearly set up.
566 * Copy and update the pcb, set up the stack so that the child
567 * ready to run and return to user mode.
568 */
569void
570cpu_fork(p1, p2)
571	register struct proc *p1, *p2;
572{
573	struct pcb *pcb2 = &p2->p_addr->u_pcb;
574
575	/* Ensure that p1's pcb is up to date. */
576	if (npxproc == p1)
577		npxsave(&p1->p_addr->u_pcb.pcb_savefpu);
578
579	/* Copy p1's pcb. */
580	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
581
582	/*
583	 * Create a new fresh stack for the new process.
584	 * Copy the trap frame for the return to user mode as if from a
585	 * syscall.  This copies the user mode register values.
586	 */
587	p2->p_md.md_regs = (struct trapframe *)
588			   ((int)p2->p_addr + UPAGES * PAGE_SIZE) - 1;
589	*p2->p_md.md_regs = *p1->p_md.md_regs;
590
591	/*
592	 * Set registers for trampoline to user mode.  Leave space for the
593	 * return address on stack.  These are the kernel mode register values.
594	 */
595	pcb2->pcb_cr3 = vtophys(p2->p_vmspace->vm_pmap.pm_pdir);
596	pcb2->pcb_edi = p2->p_md.md_regs->tf_edi;
597	pcb2->pcb_esi = (int)fork_return;
598	pcb2->pcb_ebp = p2->p_md.md_regs->tf_ebp;
599	pcb2->pcb_esp = (int)p2->p_md.md_regs - sizeof(void *);
600	pcb2->pcb_ebx = (int)p2;
601	pcb2->pcb_eip = (int)fork_trampoline;
602	/*
603	 * pcb2->pcb_ldt:	duplicated below, if necessary.
604	 * pcb2->pcb_ldt_len:	cloned above.
605	 * pcb2->pcb_savefpu:	cloned above.
606	 * pcb2->pcb_flags:	cloned above (always 0 here?).
607	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
608	 */
609
610#ifdef VM86
611	/*
612	 * XXX don't copy the i/o pages.  this should probably be fixed.
613	 */
614	pcb2->pcb_ext = 0;
615#endif
616
617#ifdef USER_LDT
618        /* Copy the LDT, if necessary. */
619        if (pcb2->pcb_ldt != 0) {
620                union descriptor *new_ldt;
621                size_t len = pcb2->pcb_ldt_len * sizeof(union descriptor);
622
623                new_ldt = (union descriptor *)kmem_alloc(kernel_map, len);
624                bcopy(pcb2->pcb_ldt, new_ldt, len);
625                pcb2->pcb_ldt = (caddr_t)new_ldt;
626        }
627#endif
628
629	/*
630	 * Now, cpu_switch() can schedule the new process.
631	 * pcb_esp is loaded pointing to the cpu_switch() stack frame
632	 * containing the return address when exiting cpu_switch.
633	 * This will normally be to proc_trampoline(), which will have
634	 * %ebx loaded with the new proc's pointer.  proc_trampoline()
635	 * will set up a stack to call fork_return(p, frame); to complete
636	 * the return to user-mode.
637	 */
638}
639
640/*
641 * Intercept the return address from a freshly forked process that has NOT
642 * been scheduled yet.
643 *
644 * This is needed to make kernel threads stay in kernel mode.
645 */
646void
647cpu_set_fork_handler(p, func, arg)
648	struct proc *p;
649	void (*func) __P((void *));
650	void *arg;
651{
652	/*
653	 * Note that the trap frame follows the args, so the function
654	 * is really called like this:  func(arg, frame);
655	 */
656	p->p_addr->u_pcb.pcb_esi = (int) func;	/* function */
657	p->p_addr->u_pcb.pcb_ebx = (int) arg;	/* first arg */
658}
659
660void
661cpu_exit(p)
662	register struct proc *p;
663{
664#if defined(USER_LDT) || defined(VM86)
665	struct pcb *pcb = &p->p_addr->u_pcb;
666#endif
667
668#if NNPX > 0
669	npxexit(p);
670#endif	/* NNPX */
671#ifdef VM86
672	if (pcb->pcb_ext != 0) {
673	        /*
674		 * XXX do we need to move the TSS off the allocated pages
675		 * before freeing them?  (not done here)
676		 */
677		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext,
678		    ctob(IOPAGES + 1));
679		pcb->pcb_ext = 0;
680	}
681#endif
682#ifdef USER_LDT
683	if (pcb->pcb_ldt != 0) {
684		if (pcb == curpcb)
685			lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
686		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
687			pcb->pcb_ldt_len * sizeof(union descriptor));
688		pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
689	}
690#endif
691	cnt.v_swtch++;
692	cpu_switch(p);
693	panic("cpu_exit");
694}
695
696void
697cpu_wait(p)
698	struct proc *p;
699{
700	/* drop per-process resources */
701	pmap_dispose_proc(p);
702
703	/* and clean-out the vmspace */
704	vmspace_free(p->p_vmspace);
705}
706
707/*
708 * Dump the machine specific header information at the start of a core dump.
709 */
710int
711cpu_coredump(p, vp, cred)
712	struct proc *p;
713	struct vnode *vp;
714	struct ucred *cred;
715{
716
717	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
718	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
719	    p));
720}
721
722#ifdef notyet
723static void
724setredzone(pte, vaddr)
725	u_short *pte;
726	caddr_t vaddr;
727{
728/* eventually do this by setting up an expand-down stack segment
729   for ss0: selector, allowing stack access down to top of u.
730   this means though that protection violations need to be handled
731   thru a double fault exception that must do an integral task
732   switch to a known good context, within which a dump can be
733   taken. a sensible scheme might be to save the initial context
734   used by sched (that has physical memory mapped 1:1 at bottom)
735   and take the dump while still in mapped mode */
736}
737#endif
738
739/*
740 * Convert kernel VA to physical address
741 */
742u_long
743kvtop(void *addr)
744{
745	vm_offset_t va;
746
747	va = pmap_kextract((vm_offset_t)addr);
748	if (va == 0)
749		panic("kvtop: zero page frame");
750	return((int)va);
751}
752
753/*
754 * Map an IO request into kernel virtual address space.
755 *
756 * All requests are (re)mapped into kernel VA space.
757 * Notice that we use b_bufsize for the size of the buffer
758 * to be mapped.  b_bcount might be modified by the driver.
759 */
760void
761vmapbuf(bp)
762	register struct buf *bp;
763{
764	register caddr_t addr, v, kva;
765	vm_offset_t pa;
766
767	if ((bp->b_flags & B_PHYS) == 0)
768		panic("vmapbuf");
769
770	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data);
771	    addr < bp->b_data + bp->b_bufsize;
772	    addr += PAGE_SIZE, v += PAGE_SIZE) {
773		/*
774		 * Do the vm_fault if needed; do the copy-on-write thing
775		 * when reading stuff off device into memory.
776		 */
777		vm_fault_quick(addr,
778			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
779		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
780		if (pa == 0)
781			panic("vmapbuf: page not present");
782		vm_page_hold(PHYS_TO_VM_PAGE(pa));
783		pmap_kenter((vm_offset_t) v, pa);
784	}
785
786	kva = bp->b_saveaddr;
787	bp->b_saveaddr = bp->b_data;
788	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
789}
790
791/*
792 * Free the io map PTEs associated with this IO operation.
793 * We also invalidate the TLB entries and restore the original b_addr.
794 */
795void
796vunmapbuf(bp)
797	register struct buf *bp;
798{
799	register caddr_t addr;
800	vm_offset_t pa;
801
802	if ((bp->b_flags & B_PHYS) == 0)
803		panic("vunmapbuf");
804
805	for (addr = (caddr_t)trunc_page(bp->b_data);
806	    addr < bp->b_data + bp->b_bufsize;
807	    addr += PAGE_SIZE) {
808		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
809		pmap_kremove((vm_offset_t) addr);
810		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
811	}
812
813	bp->b_data = bp->b_saveaddr;
814}
815
816/*
817 * Force reset the processor by invalidating the entire address space!
818 */
819void
820cpu_reset()
821{
822
823#ifdef PC98
824	/*
825	 * Attempt to do a CPU reset via CPU reset port.
826	 */
827	disable_intr();
828	outb(0x37, 0x0f);		/* SHUT0 = 0. */
829	outb(0x37, 0x0b);		/* SHUT1 = 0. */
830	outb(0xf0, 0x00);		/* Reset. */
831#else
832	/*
833	 * Attempt to do a CPU reset via the keyboard controller,
834	 * do not turn of the GateA20, as any machine that fails
835	 * to do the reset here would then end up in no man's land.
836	 */
837
838#if !defined(BROKEN_KEYBOARD_RESET)
839	outb(IO_KBD + 4, 0xFE);
840	DELAY(500000);	/* wait 0.5 sec to see if that did it */
841	printf("Keyboard reset did not work, attempting CPU shutdown\n");
842	DELAY(1000000);	/* wait 1 sec for printf to complete */
843#endif
844#endif /* PC98 */
845	/* force a shutdown by unmapping entire address space ! */
846	bzero((caddr_t) PTD, PAGE_SIZE);
847
848	/* "good night, sweet prince .... <THUNK!>" */
849	invltlb();
850	/* NOTREACHED */
851	while(1);
852}
853
854/*
855 * Grow the user stack to allow for 'sp'. This version grows the stack in
856 *	chunks of SGROWSIZ.
857 */
858int
859grow(p, sp)
860	struct proc *p;
861	u_int sp;
862{
863	unsigned int nss;
864	caddr_t v;
865	struct vmspace *vm = p->p_vmspace;
866
867	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
868	    return (1);
869
870	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
871
872	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
873		return (0);
874
875	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
876	    SGROWSIZ) < nss) {
877		int grow_amount;
878		/*
879		 * If necessary, grow the VM that the stack occupies
880		 * to allow for the rlimit. This allows us to not have
881		 * to allocate all of the VM up-front in execve (which
882		 * is expensive).
883		 * Grow the VM by the amount requested rounded up to
884		 * the nearest SGROWSIZ to provide for some hysteresis.
885		 */
886		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
887		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
888		    SGROWSIZ) - grow_amount;
889		/*
890		 * If there isn't enough room to extend by SGROWSIZ, then
891		 * just extend to the maximum size
892		 */
893		if (v < vm->vm_maxsaddr) {
894			v = vm->vm_maxsaddr;
895			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
896		}
897		if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v,
898		    grow_amount, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != KERN_SUCCESS)) {
899			return (0);
900		}
901		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
902	}
903
904	return (1);
905}
906
907/*
908 * Implement the pre-zeroed page mechanism.
909 * This routine is called from the idle loop.
910 */
911int
912vm_page_zero_idle()
913{
914	static int free_rover;
915	vm_page_t m;
916	int s;
917
918#ifdef WRONG
919	if (cnt.v_free_count <= cnt.v_interrupt_free_min)
920		return (0);
921#endif
922	/*
923	 * XXX
924	 * We stop zeroing pages when there are sufficent prezeroed pages.
925	 * This threshold isn't really needed, except we want to
926	 * bypass unneeded calls to vm_page_list_find, and the
927	 * associated cache flush and latency.  The pre-zero will
928	 * still be called when there are significantly more
929	 * non-prezeroed pages than zeroed pages.  The threshold
930	 * of half the number of reserved pages is arbitrary, but
931	 * approximately the right amount.  Eventually, we should
932	 * perhaps interrupt the zero operation when a process
933	 * is found to be ready to run.
934	 */
935	if (cnt.v_free_count - vm_page_zero_count <= cnt.v_free_reserved / 2)
936		return (0);
937#ifdef SMP
938	get_mplock();
939#endif
940	s = splvm();
941	enable_intr();
942	m = vm_page_list_find(PQ_FREE, free_rover);
943	if (m != NULL) {
944		--(*vm_page_queues[m->queue].lcnt);
945		TAILQ_REMOVE(vm_page_queues[m->queue].pl, m, pageq);
946		m->queue = PQ_NONE;
947		splx(s);
948#ifdef SMP
949		rel_mplock();
950#endif
951		pmap_zero_page(VM_PAGE_TO_PHYS(m));
952#ifdef SMP
953		get_mplock();
954#endif
955		(void)splvm();
956		m->queue = PQ_ZERO + m->pc;
957		++(*vm_page_queues[m->queue].lcnt);
958		TAILQ_INSERT_HEAD(vm_page_queues[m->queue].pl, m, pageq);
959		free_rover = (free_rover + PQ_PRIME3) & PQ_L2_MASK;
960		++vm_page_zero_count;
961	}
962	splx(s);
963	disable_intr();
964#ifdef SMP
965	rel_mplock();
966#endif
967	return (1);
968}
969
970/*
971 * Software interrupt handler for queued VM system processing.
972 */
973void
974swi_vm()
975{
976	if (busdma_swi_pending != 0)
977		busdma_swi();
978}
979
980/*
981 * Tell whether this address is in some physical memory region.
982 * Currently used by the kernel coredump code in order to avoid
983 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
984 * or other unpredictable behaviour.
985 */
986
987#include "isa.h"
988
989int
990is_physical_memory(addr)
991	vm_offset_t addr;
992{
993
994#if NISA > 0
995	/* The ISA ``memory hole''. */
996	if (addr >= 0xa0000 && addr < 0x100000)
997		return 0;
998#endif
999
1000	/*
1001	 * stuff other tests for known memory-mapped devices (PCI?)
1002	 * here
1003	 */
1004
1005	return 1;
1006}
1007