vm_machdep.c revision 32516
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.93 1997/12/27 02:28:28 peter 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	vmspace_free(p->p_vmspace);
703}
704
705/*
706 * Dump the machine specific header information at the start of a core dump.
707 */
708int
709cpu_coredump(p, vp, cred)
710	struct proc *p;
711	struct vnode *vp;
712	struct ucred *cred;
713{
714
715	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
716	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
717	    p));
718}
719
720#ifdef notyet
721static void
722setredzone(pte, vaddr)
723	u_short *pte;
724	caddr_t vaddr;
725{
726/* eventually do this by setting up an expand-down stack segment
727   for ss0: selector, allowing stack access down to top of u.
728   this means though that protection violations need to be handled
729   thru a double fault exception that must do an integral task
730   switch to a known good context, within which a dump can be
731   taken. a sensible scheme might be to save the initial context
732   used by sched (that has physical memory mapped 1:1 at bottom)
733   and take the dump while still in mapped mode */
734}
735#endif
736
737/*
738 * Convert kernel VA to physical address
739 */
740u_long
741kvtop(void *addr)
742{
743	vm_offset_t va;
744
745	va = pmap_kextract((vm_offset_t)addr);
746	if (va == 0)
747		panic("kvtop: zero page frame");
748	return((int)va);
749}
750
751/*
752 * Map an IO request into kernel virtual address space.
753 *
754 * All requests are (re)mapped into kernel VA space.
755 * Notice that we use b_bufsize for the size of the buffer
756 * to be mapped.  b_bcount might be modified by the driver.
757 */
758void
759vmapbuf(bp)
760	register struct buf *bp;
761{
762	register caddr_t addr, v, kva;
763	vm_offset_t pa;
764
765	if ((bp->b_flags & B_PHYS) == 0)
766		panic("vmapbuf");
767
768	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data);
769	    addr < bp->b_data + bp->b_bufsize;
770	    addr += PAGE_SIZE, v += PAGE_SIZE) {
771		/*
772		 * Do the vm_fault if needed; do the copy-on-write thing
773		 * when reading stuff off device into memory.
774		 */
775		vm_fault_quick(addr,
776			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
777		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
778		if (pa == 0)
779			panic("vmapbuf: page not present");
780		vm_page_hold(PHYS_TO_VM_PAGE(pa));
781		pmap_kenter((vm_offset_t) v, pa);
782	}
783
784	kva = bp->b_saveaddr;
785	bp->b_saveaddr = bp->b_data;
786	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
787}
788
789/*
790 * Free the io map PTEs associated with this IO operation.
791 * We also invalidate the TLB entries and restore the original b_addr.
792 */
793void
794vunmapbuf(bp)
795	register struct buf *bp;
796{
797	register caddr_t addr;
798	vm_offset_t pa;
799
800	if ((bp->b_flags & B_PHYS) == 0)
801		panic("vunmapbuf");
802
803	for (addr = (caddr_t)trunc_page(bp->b_data);
804	    addr < bp->b_data + bp->b_bufsize;
805	    addr += PAGE_SIZE) {
806		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
807		pmap_kremove((vm_offset_t) addr);
808		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
809	}
810
811	bp->b_data = bp->b_saveaddr;
812}
813
814/*
815 * Force reset the processor by invalidating the entire address space!
816 */
817void
818cpu_reset()
819{
820
821#ifdef PC98
822	/*
823	 * Attempt to do a CPU reset via CPU reset port.
824	 */
825	disable_intr();
826	outb(0x37, 0x0f);		/* SHUT0 = 0. */
827	outb(0x37, 0x0b);		/* SHUT1 = 0. */
828	outb(0xf0, 0x00);		/* Reset. */
829#else
830	/*
831	 * Attempt to do a CPU reset via the keyboard controller,
832	 * do not turn of the GateA20, as any machine that fails
833	 * to do the reset here would then end up in no man's land.
834	 */
835
836#if !defined(BROKEN_KEYBOARD_RESET)
837	outb(IO_KBD + 4, 0xFE);
838	DELAY(500000);	/* wait 0.5 sec to see if that did it */
839	printf("Keyboard reset did not work, attempting CPU shutdown\n");
840	DELAY(1000000);	/* wait 1 sec for printf to complete */
841#endif
842#endif /* PC98 */
843	/* force a shutdown by unmapping entire address space ! */
844	bzero((caddr_t) PTD, PAGE_SIZE);
845
846	/* "good night, sweet prince .... <THUNK!>" */
847	invltlb();
848	/* NOTREACHED */
849	while(1);
850}
851
852/*
853 * Grow the user stack to allow for 'sp'. This version grows the stack in
854 *	chunks of SGROWSIZ.
855 */
856int
857grow(p, sp)
858	struct proc *p;
859	u_int sp;
860{
861	unsigned int nss;
862	caddr_t v;
863	struct vmspace *vm = p->p_vmspace;
864
865	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
866	    return (1);
867
868	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
869
870	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
871		return (0);
872
873	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
874	    SGROWSIZ) < nss) {
875		int grow_amount;
876		/*
877		 * If necessary, grow the VM that the stack occupies
878		 * to allow for the rlimit. This allows us to not have
879		 * to allocate all of the VM up-front in execve (which
880		 * is expensive).
881		 * Grow the VM by the amount requested rounded up to
882		 * the nearest SGROWSIZ to provide for some hysteresis.
883		 */
884		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
885		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
886		    SGROWSIZ) - grow_amount;
887		/*
888		 * If there isn't enough room to extend by SGROWSIZ, then
889		 * just extend to the maximum size
890		 */
891		if (v < vm->vm_maxsaddr) {
892			v = vm->vm_maxsaddr;
893			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
894		}
895		if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v,
896		    grow_amount, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != KERN_SUCCESS)) {
897			return (0);
898		}
899		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
900	}
901
902	return (1);
903}
904
905/*
906 * Implement the pre-zeroed page mechanism.
907 * This routine is called from the idle loop.
908 */
909int
910vm_page_zero_idle()
911{
912	static int free_rover;
913	vm_page_t m;
914	int s;
915
916#ifdef WRONG
917	if (cnt.v_free_count <= cnt.v_interrupt_free_min)
918		return (0);
919#endif
920	/*
921	 * XXX
922	 * We stop zeroing pages when there are sufficent prezeroed pages.
923	 * This threshold isn't really needed, except we want to
924	 * bypass unneeded calls to vm_page_list_find, and the
925	 * associated cache flush and latency.  The pre-zero will
926	 * still be called when there are significantly more
927	 * non-prezeroed pages than zeroed pages.  The threshold
928	 * of half the number of reserved pages is arbitrary, but
929	 * approximately the right amount.  Eventually, we should
930	 * perhaps interrupt the zero operation when a process
931	 * is found to be ready to run.
932	 */
933	if (cnt.v_free_count - vm_page_zero_count <= cnt.v_free_reserved / 2)
934		return (0);
935#ifdef SMP
936	get_mplock();
937#endif
938	s = splvm();
939	enable_intr();
940	m = vm_page_list_find(PQ_FREE, free_rover);
941	if (m != NULL) {
942		--(*vm_page_queues[m->queue].lcnt);
943		TAILQ_REMOVE(vm_page_queues[m->queue].pl, m, pageq);
944		splx(s);
945#ifdef SMP
946		rel_mplock();
947#endif
948		pmap_zero_page(VM_PAGE_TO_PHYS(m));
949#ifdef SMP
950		get_mplock();
951#endif
952		(void)splvm();
953		m->queue = PQ_ZERO + m->pc;
954		++(*vm_page_queues[m->queue].lcnt);
955		TAILQ_INSERT_HEAD(vm_page_queues[m->queue].pl, m, pageq);
956		free_rover = (free_rover + PQ_PRIME3) & PQ_L2_MASK;
957		++vm_page_zero_count;
958	}
959	splx(s);
960	disable_intr();
961#ifdef SMP
962	rel_mplock();
963#endif
964	return (1);
965}
966
967/*
968 * Software interrupt handler for queued VM system processing.
969 */
970void
971swi_vm()
972{
973	if (busdma_swi_pending != 0)
974		busdma_swi();
975}
976
977/*
978 * Tell whether this address is in some physical memory region.
979 * Currently used by the kernel coredump code in order to avoid
980 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
981 * or other unpredictable behaviour.
982 */
983
984#include "isa.h"
985
986int
987is_physical_memory(addr)
988	vm_offset_t addr;
989{
990
991#if NISA > 0
992	/* The ISA ``memory hole''. */
993	if (addr >= 0xa0000 && addr < 0x100000)
994		return 0;
995#endif
996
997	/*
998	 * stuff other tests for known memory-mapped devices (PCI?)
999	 * here
1000	 */
1001
1002	return 1;
1003}
1004