vm_machdep.c revision 33134
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.98 1998/02/04 22:32:13 eivind 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			printf("vm_bounce_init: bounce memory out of range -- bounce disabled\n");
540			free(bounceallocarray, M_TEMP);
541			bounceallocarray = NULL;
542			free(bouncepa, M_TEMP);
543			bouncepa = NULL;
544			bouncepages = 0;
545			break;
546		}
547		if( pa == 0)
548			panic("bounce memory not resident");
549		bouncepa[i] = pa;
550		bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int))));
551	}
552	bouncefree = bouncepages;
553
554}
555#endif /* BOUNCE_BUFFERS */
556
557/*
558 * quick version of vm_fault
559 */
560void
561vm_fault_quick(v, prot)
562	caddr_t v;
563	int prot;
564{
565	if (prot & VM_PROT_WRITE)
566		subyte(v, fubyte(v));
567	else
568		fubyte(v);
569}
570
571/*
572 * Finish a fork operation, with process p2 nearly set up.
573 * Copy and update the pcb, set up the stack so that the child
574 * ready to run and return to user mode.
575 */
576void
577cpu_fork(p1, p2)
578	register struct proc *p1, *p2;
579{
580	struct pcb *pcb2 = &p2->p_addr->u_pcb;
581
582	/* Ensure that p1's pcb is up to date. */
583	if (npxproc == p1)
584		npxsave(&p1->p_addr->u_pcb.pcb_savefpu);
585
586	/* Copy p1's pcb. */
587	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
588
589	/*
590	 * Create a new fresh stack for the new process.
591	 * Copy the trap frame for the return to user mode as if from a
592	 * syscall.  This copies the user mode register values.
593	 */
594	p2->p_md.md_regs = (struct trapframe *)
595			   ((int)p2->p_addr + UPAGES * PAGE_SIZE) - 1;
596	*p2->p_md.md_regs = *p1->p_md.md_regs;
597
598	/*
599	 * Set registers for trampoline to user mode.  Leave space for the
600	 * return address on stack.  These are the kernel mode register values.
601	 */
602	pcb2->pcb_cr3 = vtophys(p2->p_vmspace->vm_pmap.pm_pdir);
603	pcb2->pcb_edi = p2->p_md.md_regs->tf_edi;
604	pcb2->pcb_esi = (int)fork_return;
605	pcb2->pcb_ebp = p2->p_md.md_regs->tf_ebp;
606	pcb2->pcb_esp = (int)p2->p_md.md_regs - sizeof(void *);
607	pcb2->pcb_ebx = (int)p2;
608	pcb2->pcb_eip = (int)fork_trampoline;
609	/*
610	 * pcb2->pcb_ldt:	duplicated below, if necessary.
611	 * pcb2->pcb_ldt_len:	cloned above.
612	 * pcb2->pcb_savefpu:	cloned above.
613	 * pcb2->pcb_flags:	cloned above (always 0 here?).
614	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
615	 */
616
617#ifdef VM86
618	/*
619	 * XXX don't copy the i/o pages.  this should probably be fixed.
620	 */
621	pcb2->pcb_ext = 0;
622#endif
623
624#ifdef USER_LDT
625        /* Copy the LDT, if necessary. */
626        if (pcb2->pcb_ldt != 0) {
627                union descriptor *new_ldt;
628                size_t len = pcb2->pcb_ldt_len * sizeof(union descriptor);
629
630                new_ldt = (union descriptor *)kmem_alloc(kernel_map, len);
631                bcopy(pcb2->pcb_ldt, new_ldt, len);
632                pcb2->pcb_ldt = (caddr_t)new_ldt;
633        }
634#endif
635
636	/*
637	 * Now, cpu_switch() can schedule the new process.
638	 * pcb_esp is loaded pointing to the cpu_switch() stack frame
639	 * containing the return address when exiting cpu_switch.
640	 * This will normally be to proc_trampoline(), which will have
641	 * %ebx loaded with the new proc's pointer.  proc_trampoline()
642	 * will set up a stack to call fork_return(p, frame); to complete
643	 * the return to user-mode.
644	 */
645}
646
647/*
648 * Intercept the return address from a freshly forked process that has NOT
649 * been scheduled yet.
650 *
651 * This is needed to make kernel threads stay in kernel mode.
652 */
653void
654cpu_set_fork_handler(p, func, arg)
655	struct proc *p;
656	void (*func) __P((void *));
657	void *arg;
658{
659	/*
660	 * Note that the trap frame follows the args, so the function
661	 * is really called like this:  func(arg, frame);
662	 */
663	p->p_addr->u_pcb.pcb_esi = (int) func;	/* function */
664	p->p_addr->u_pcb.pcb_ebx = (int) arg;	/* first arg */
665}
666
667void
668cpu_exit(p)
669	register struct proc *p;
670{
671#if defined(USER_LDT) || defined(VM86)
672	struct pcb *pcb = &p->p_addr->u_pcb;
673#endif
674
675#if NNPX > 0
676	npxexit(p);
677#endif	/* NNPX */
678#ifdef VM86
679	if (pcb->pcb_ext != 0) {
680	        /*
681		 * XXX do we need to move the TSS off the allocated pages
682		 * before freeing them?  (not done here)
683		 */
684		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext,
685		    ctob(IOPAGES + 1));
686		pcb->pcb_ext = 0;
687	}
688#endif
689#ifdef USER_LDT
690	if (pcb->pcb_ldt != 0) {
691		if (pcb == curpcb)
692			lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
693		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
694			pcb->pcb_ldt_len * sizeof(union descriptor));
695		pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
696	}
697#endif
698	cnt.v_swtch++;
699	cpu_switch(p);
700	panic("cpu_exit");
701}
702
703void
704cpu_wait(p)
705	struct proc *p;
706{
707	/* drop per-process resources */
708	pmap_dispose_proc(p);
709
710	/* and clean-out the vmspace */
711	vmspace_free(p->p_vmspace);
712}
713
714/*
715 * Dump the machine specific header information at the start of a core dump.
716 */
717int
718cpu_coredump(p, vp, cred)
719	struct proc *p;
720	struct vnode *vp;
721	struct ucred *cred;
722{
723
724	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
725	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
726	    p));
727}
728
729#ifdef notyet
730static void
731setredzone(pte, vaddr)
732	u_short *pte;
733	caddr_t vaddr;
734{
735/* eventually do this by setting up an expand-down stack segment
736   for ss0: selector, allowing stack access down to top of u.
737   this means though that protection violations need to be handled
738   thru a double fault exception that must do an integral task
739   switch to a known good context, within which a dump can be
740   taken. a sensible scheme might be to save the initial context
741   used by sched (that has physical memory mapped 1:1 at bottom)
742   and take the dump while still in mapped mode */
743}
744#endif
745
746/*
747 * Convert kernel VA to physical address
748 */
749u_long
750kvtop(void *addr)
751{
752	vm_offset_t va;
753
754	va = pmap_kextract((vm_offset_t)addr);
755	if (va == 0)
756		panic("kvtop: zero page frame");
757	return((int)va);
758}
759
760/*
761 * Map an IO request into kernel virtual address space.
762 *
763 * All requests are (re)mapped into kernel VA space.
764 * Notice that we use b_bufsize for the size of the buffer
765 * to be mapped.  b_bcount might be modified by the driver.
766 */
767void
768vmapbuf(bp)
769	register struct buf *bp;
770{
771	register caddr_t addr, v, kva;
772	vm_offset_t pa;
773
774	if ((bp->b_flags & B_PHYS) == 0)
775		panic("vmapbuf");
776
777	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data);
778	    addr < bp->b_data + bp->b_bufsize;
779	    addr += PAGE_SIZE, v += PAGE_SIZE) {
780		/*
781		 * Do the vm_fault if needed; do the copy-on-write thing
782		 * when reading stuff off device into memory.
783		 */
784		vm_fault_quick(addr,
785			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
786		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
787		if (pa == 0)
788			panic("vmapbuf: page not present");
789		vm_page_hold(PHYS_TO_VM_PAGE(pa));
790		pmap_kenter((vm_offset_t) v, pa);
791	}
792
793	kva = bp->b_saveaddr;
794	bp->b_saveaddr = bp->b_data;
795	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
796}
797
798/*
799 * Free the io map PTEs associated with this IO operation.
800 * We also invalidate the TLB entries and restore the original b_addr.
801 */
802void
803vunmapbuf(bp)
804	register struct buf *bp;
805{
806	register caddr_t addr;
807	vm_offset_t pa;
808
809	if ((bp->b_flags & B_PHYS) == 0)
810		panic("vunmapbuf");
811
812	for (addr = (caddr_t)trunc_page(bp->b_data);
813	    addr < bp->b_data + bp->b_bufsize;
814	    addr += PAGE_SIZE) {
815		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
816		pmap_kremove((vm_offset_t) addr);
817		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
818	}
819
820	bp->b_data = bp->b_saveaddr;
821}
822
823/*
824 * Force reset the processor by invalidating the entire address space!
825 */
826void
827cpu_reset()
828{
829
830#ifdef PC98
831	/*
832	 * Attempt to do a CPU reset via CPU reset port.
833	 */
834	disable_intr();
835	outb(0x37, 0x0f);		/* SHUT0 = 0. */
836	outb(0x37, 0x0b);		/* SHUT1 = 0. */
837	outb(0xf0, 0x00);		/* Reset. */
838#else
839	/*
840	 * Attempt to do a CPU reset via the keyboard controller,
841	 * do not turn of the GateA20, as any machine that fails
842	 * to do the reset here would then end up in no man's land.
843	 */
844
845#if !defined(BROKEN_KEYBOARD_RESET)
846	outb(IO_KBD + 4, 0xFE);
847	DELAY(500000);	/* wait 0.5 sec to see if that did it */
848	printf("Keyboard reset did not work, attempting CPU shutdown\n");
849	DELAY(1000000);	/* wait 1 sec for printf to complete */
850#endif
851#endif /* PC98 */
852	/* force a shutdown by unmapping entire address space ! */
853	bzero((caddr_t) PTD, PAGE_SIZE);
854
855	/* "good night, sweet prince .... <THUNK!>" */
856	invltlb();
857	/* NOTREACHED */
858	while(1);
859}
860
861/*
862 * Grow the user stack to allow for 'sp'. This version grows the stack in
863 *	chunks of SGROWSIZ.
864 */
865int
866grow(p, sp)
867	struct proc *p;
868	u_int sp;
869{
870	unsigned int nss;
871	caddr_t v;
872	struct vmspace *vm = p->p_vmspace;
873
874	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
875	    return (1);
876
877	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
878
879	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
880		return (0);
881
882	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
883	    SGROWSIZ) < nss) {
884		int grow_amount;
885		/*
886		 * If necessary, grow the VM that the stack occupies
887		 * to allow for the rlimit. This allows us to not have
888		 * to allocate all of the VM up-front in execve (which
889		 * is expensive).
890		 * Grow the VM by the amount requested rounded up to
891		 * the nearest SGROWSIZ to provide for some hysteresis.
892		 */
893		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
894		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
895		    SGROWSIZ) - grow_amount;
896		/*
897		 * If there isn't enough room to extend by SGROWSIZ, then
898		 * just extend to the maximum size
899		 */
900		if (v < vm->vm_maxsaddr) {
901			v = vm->vm_maxsaddr;
902			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
903		}
904		if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v,
905		    grow_amount, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != KERN_SUCCESS)) {
906			return (0);
907		}
908		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
909	}
910
911	return (1);
912}
913
914/*
915 * Implement the pre-zeroed page mechanism.
916 * This routine is called from the idle loop.
917 */
918int
919vm_page_zero_idle()
920{
921	static int free_rover;
922	vm_page_t m;
923	int s;
924
925#ifdef WRONG
926	if (cnt.v_free_count <= cnt.v_interrupt_free_min)
927		return (0);
928#endif
929	/*
930	 * XXX
931	 * We stop zeroing pages when there are sufficent prezeroed pages.
932	 * This threshold isn't really needed, except we want to
933	 * bypass unneeded calls to vm_page_list_find, and the
934	 * associated cache flush and latency.  The pre-zero will
935	 * still be called when there are significantly more
936	 * non-prezeroed pages than zeroed pages.  The threshold
937	 * of half the number of reserved pages is arbitrary, but
938	 * approximately the right amount.  Eventually, we should
939	 * perhaps interrupt the zero operation when a process
940	 * is found to be ready to run.
941	 */
942	if (cnt.v_free_count - vm_page_zero_count <= cnt.v_free_reserved / 2)
943		return (0);
944#ifdef SMP
945	get_mplock();
946#endif
947	s = splvm();
948	enable_intr();
949	m = vm_page_list_find(PQ_FREE, free_rover);
950	if (m != NULL) {
951		--(*vm_page_queues[m->queue].lcnt);
952		TAILQ_REMOVE(vm_page_queues[m->queue].pl, m, pageq);
953		m->queue = PQ_NONE;
954		splx(s);
955#ifdef SMP
956		rel_mplock();
957#endif
958		pmap_zero_page(VM_PAGE_TO_PHYS(m));
959#ifdef SMP
960		get_mplock();
961#endif
962		(void)splvm();
963		m->queue = PQ_ZERO + m->pc;
964		++(*vm_page_queues[m->queue].lcnt);
965		TAILQ_INSERT_HEAD(vm_page_queues[m->queue].pl, m, pageq);
966		free_rover = (free_rover + PQ_PRIME3) & PQ_L2_MASK;
967		++vm_page_zero_count;
968	}
969	splx(s);
970	disable_intr();
971#ifdef SMP
972	rel_mplock();
973#endif
974	return (1);
975}
976
977/*
978 * Software interrupt handler for queued VM system processing.
979 */
980void
981swi_vm()
982{
983	if (busdma_swi_pending != 0)
984		busdma_swi();
985}
986
987/*
988 * Tell whether this address is in some physical memory region.
989 * Currently used by the kernel coredump code in order to avoid
990 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
991 * or other unpredictable behaviour.
992 */
993
994#include "isa.h"
995
996int
997is_physical_memory(addr)
998	vm_offset_t addr;
999{
1000
1001#if NISA > 0
1002	/* The ISA ``memory hole''. */
1003	if (addr >= 0xa0000 && addr < 0x100000)
1004		return 0;
1005#endif
1006
1007	/*
1008	 * stuff other tests for known memory-mapped devices (PCI?)
1009	 * here
1010	 */
1011
1012	return 1;
1013}
1014