xref: /original-bsd/sys/i386/i386/vm_machdep.c (revision ba762ddc)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * the University of Utah, and William Jolitz.
7  *
8  * %sccs.include.386.c%
9  *
10  *	@(#)vm_machdep.c	5.7 (Berkeley) 05/02/91
11  */
12 
13 /*
14  * Copyright (c) 1989, 1990 William F. Jolitz
15  */
16 
17 /*
18  * Copyright (c) 1988 University of Utah.
19  * All rights reserved.  The Utah Software License Agreement
20  * specifies the terms and conditions for redistribution.
21  *
22  *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
23  */
24 /*
25  * Copyright (c) 1982, 1986 Regents of the University of California.
26  * All rights reserved.  The Berkeley software License Agreement
27  * specifies the terms and conditions for redistribution.
28  *
29  *	@(#)vm_machdep.c	7.1 (Berkeley) 6/5/86
30  */
31 
32 #include "sys/param.h"
33 #include "sys/systm.h"
34 #include "sys/user.h"
35 #include "sys/proc.h"
36 #include "sys/cmap.h"
37 #include "sys/malloc.h"
38 #include "sys/buf.h"
39 
40 #include "machine/cpu.h"
41 
42 #include "vm/vm_param.h"
43 #include "vm/pmap.h"
44 #include "vm/vm_map.h"
45 
46 /*
47  * Set a red zone in the kernel stack after the u. area.
48  */
49 setredzone(pte, vaddr)
50 	u_short *pte;
51 	caddr_t vaddr;
52 {
53 /* eventually do this by setting up an expand-down stack segment
54    for ss0: selector, allowing stack access down to top of u.
55    this means though that protection violations need to be handled
56    thru a double fault exception that must do an integral task
57    switch to a known good context, within which a dump can be
58    taken. a sensible scheme might be to save the initial context
59    used by sched (that has physical memory mapped 1:1 at bottom)
60    and take the dump while still in mapped mode */
61 }
62 
63 /*
64  * Move pages from one kernel virtual address to another.
65  * Both addresses are assumed to reside in the Sysmap,
66  * and size must be a multiple of CLSIZE.
67  */
68 pagemove(from, to, size)
69 	register caddr_t from, to;
70 	int size;
71 {
72 	register struct pte *fpte, *tpte;
73 
74 	if (size % CLBYTES)
75 		panic("pagemove");
76 	fpte = kvtopte(from);
77 	tpte = kvtopte(to);
78 	while (size > 0) {
79 		*tpte++ = *fpte;
80 		*(int *)fpte++ = 0;
81 		from += NBPG;
82 		to += NBPG;
83 		size -= NBPG;
84 	}
85 	load_cr3(u.u_pcb.pcb_cr3);
86 }
87 
88 /*
89  * Convert kernel VA to physical address
90  */
91 kvtop(addr)
92 	register caddr_t addr;
93 {
94 	vm_offset_t va;
95 
96 	va = pmap_extract(kernel_pmap, (vm_offset_t)addr);
97 	if (va == 0)
98 		panic("kvtop: zero page frame");
99 	return((int)va);
100 }
101 
102 #ifdef notdef
103 /*
104  * The probe[rw] routines should probably be redone in assembler
105  * for efficiency.
106  */
107 prober(addr)
108 	register u_int addr;
109 {
110 	register int page;
111 	register struct proc *p;
112 
113 	if (addr >= USRSTACK)
114 		return(0);
115 	p = u.u_procp;
116 	page = btop(addr);
117 	if (page < dptov(p, p->p_dsize) || page > sptov(p, p->p_ssize))
118 		return(1);
119 	return(0);
120 }
121 
122 probew(addr)
123 	register u_int addr;
124 {
125 	register int page;
126 	register struct proc *p;
127 
128 	if (addr >= USRSTACK)
129 		return(0);
130 	p = u.u_procp;
131 	page = btop(addr);
132 	if (page < dptov(p, p->p_dsize) || page > sptov(p, p->p_ssize))
133 		return((*(int *)vtopte(p, page) & PG_PROT) == PG_UW);
134 	return(0);
135 }
136 
137 /*
138  * NB: assumes a physically contiguous kernel page table
139  *     (makes life a LOT simpler).
140  */
141 kernacc(addr, count, rw)
142 	register u_int addr;
143 	int count, rw;
144 {
145 	register struct pde *pde;
146 	register struct pte *pte;
147 	register int ix, cnt;
148 	extern long Syssize;
149 
150 	if (count <= 0)
151 		return(0);
152 	pde = (struct pde *)((u_int)u.u_procp->p_p0br + u.u_procp->p_szpt * NBPG);
153 	ix = (addr & PD_MASK) >> PD_SHIFT;
154 	cnt = ((addr + count + (1 << PD_SHIFT) - 1) & PD_MASK) >> PD_SHIFT;
155 	cnt -= ix;
156 	for (pde += ix; cnt; cnt--, pde++)
157 		if (pde->pd_v == 0)
158 			return(0);
159 	ix = btop(addr-0xfe000000);
160 	cnt = btop(addr-0xfe000000+count+NBPG-1);
161 	if (cnt > (int)&Syssize)
162 		return(0);
163 	cnt -= ix;
164 	for (pte = &Sysmap[ix]; cnt; cnt--, pte++)
165 		if (pte->pg_v == 0 /*|| (rw == B_WRITE && pte->pg_prot == 1)*/)
166 			return(0);
167 	return(1);
168 }
169 
170 useracc(addr, count, rw)
171 	register u_int addr;
172 	int count, rw;
173 {
174 	register int (*func)();
175 	register u_int addr2;
176 	extern int prober(), probew();
177 
178 	if (count <= 0)
179 		return(0);
180 	addr2 = addr;
181 	addr += count;
182 	func = (rw == B_READ) ? prober : probew;
183 	do {
184 		if ((*func)(addr2) == 0)
185 			return(0);
186 		addr2 = (addr2 + NBPG) & ~PGOFSET;
187 	} while (addr2 < addr);
188 	return(1);
189 }
190 #endif
191 
192 extern vm_map_t phys_map;
193 
194 /*
195  * Map an IO request into kernel virtual address space.  Requests fall into
196  * one of five catagories:
197  *
198  *	B_PHYS|B_UAREA:	User u-area swap.
199  *			Address is relative to start of u-area (p_addr).
200  *	B_PHYS|B_PAGET:	User page table swap.
201  *			Address is a kernel VA in usrpt (Usrptmap).
202  *	B_PHYS|B_DIRTY:	Dirty page push.
203  *			Address is a VA in proc2's address space.
204  *	B_PHYS|B_PGIN:	Kernel pagein of user pages.
205  *			Address is VA in user's address space.
206  *	B_PHYS:		User "raw" IO request.
207  *			Address is VA in user's address space.
208  *
209  * All requests are (re)mapped into kernel VA space via the useriomap
210  * (a name with only slightly more meaning than "kernelmap")
211  */
212 vmapbuf(bp)
213 	register struct buf *bp;
214 {
215 	register int npf;
216 	register caddr_t addr;
217 	register long flags = bp->b_flags;
218 	struct proc *p;
219 	int off;
220 	vm_offset_t kva;
221 	register vm_offset_t pa;
222 
223 	if ((flags & B_PHYS) == 0)
224 		panic("vmapbuf");
225 	addr = bp->b_saveaddr = bp->b_un.b_addr;
226 	off = (int)addr & PGOFSET;
227 	p = bp->b_proc;
228 	npf = btoc(round_page(bp->b_bcount + off));
229 	kva = kmem_alloc_wait(phys_map, ctob(npf));
230 	bp->b_un.b_addr = (caddr_t) (kva + off);
231 	while (npf--) {
232 		pa = pmap_extract(vm_map_pmap(p->p_map), (vm_offset_t)addr);
233 		if (pa == 0)
234 			panic("vmapbuf: null page frame");
235 		pmap_enter(vm_map_pmap(phys_map), kva, trunc_page(pa),
236 			   VM_PROT_READ|VM_PROT_WRITE, TRUE);
237 		addr += PAGE_SIZE;
238 		kva += PAGE_SIZE;
239 	}
240 }
241 
242 /*
243  * Free the io map PTEs associated with this IO operation.
244  * We also invalidate the TLB entries and restore the original b_addr.
245  */
246 vunmapbuf(bp)
247 	register struct buf *bp;
248 {
249 	register int npf;
250 	register caddr_t addr = bp->b_un.b_addr;
251 	vm_offset_t kva;
252 
253 	if ((bp->b_flags & B_PHYS) == 0)
254 		panic("vunmapbuf");
255 	npf = btoc(round_page(bp->b_bcount + ((int)addr & PGOFSET)));
256 	kva = (vm_offset_t)((int)addr & ~PGOFSET);
257 	kmem_free_wakeup(phys_map, kva, ctob(npf));
258 	bp->b_un.b_addr = bp->b_saveaddr;
259 	bp->b_saveaddr = NULL;
260 }
261