xref: /original-bsd/sys/hp300/hp300/vm_machdep.c (revision 333da485)
1 /*
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1982, 1986, 1990, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department.
9  *
10  * %sccs.include.redist.c%
11  *
12  * from: Utah $Hdr: vm_machdep.c 1.21 91/04/06$
13  *
14  *	@(#)vm_machdep.c	8.6 (Berkeley) 01/12/94
15  */
16 
17 #include <sys/param.h>
18 #include <sys/systm.h>
19 #include <sys/proc.h>
20 #include <sys/malloc.h>
21 #include <sys/buf.h>
22 #include <sys/vnode.h>
23 #include <sys/user.h>
24 
25 #include <machine/cpu.h>
26 
27 #include <vm/vm.h>
28 #include <vm/vm_kern.h>
29 #include <hp300/hp300/pte.h>
30 
31 /*
32  * Finish a fork operation, with process p2 nearly set up.
33  * Copy and update the kernel stack and pcb, making the child
34  * ready to run, and marking it so that it can return differently
35  * than the parent.  Returns 1 in the child process, 0 in the parent.
36  * We currently double-map the user area so that the stack is at the same
37  * address in each process; in the future we will probably relocate
38  * the frame pointers on the stack after copying.
39  */
40 cpu_fork(p1, p2)
41 	register struct proc *p1, *p2;
42 {
43 	register struct user *up = p2->p_addr;
44 	int offset;
45 	extern caddr_t getsp();
46 	extern char kstack[];
47 
48 	p2->p_md.md_regs = p1->p_md.md_regs;
49 	p2->p_md.md_flags = (p1->p_md.md_flags & ~(MDP_AST|MDP_HPUXTRACE));
50 
51 	/*
52 	 * Copy pcb and stack from proc p1 to p2.
53 	 * We do this as cheaply as possible, copying only the active
54 	 * part of the stack.  The stack and pcb need to agree;
55 	 * this is tricky, as the final pcb is constructed by savectx,
56 	 * but its frame isn't yet on the stack when the stack is copied.
57 	 * switch compensates for this when the child eventually runs.
58 	 * This should be done differently, with a single call
59 	 * that copies and updates the pcb+stack,
60 	 * replacing the bcopy and savectx.
61 	 */
62 	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
63 	offset = getsp() - kstack;
64 	bcopy((caddr_t)kstack + offset, (caddr_t)p2->p_addr + offset,
65 	    (unsigned) ctob(UPAGES) - offset);
66 
67 	PMAP_ACTIVATE(&p2->p_vmspace->vm_pmap, &up->u_pcb, 0);
68 
69 	/*
70 	 * Arrange for a non-local goto when the new process
71 	 * is started, to resume here, returning nonzero from setjmp.
72 	 */
73 	if (savectx(up, 1)) {
74 		/*
75 		 * Return 1 in child.
76 		 */
77 		return (1);
78 	}
79 	return (0);
80 }
81 
82 /*
83  * cpu_exit is called as the last action during exit.
84  * We release the address space and machine-dependent resources,
85  * including the memory for the user structure and kernel stack.
86  * Once finished, we call switch_exit, which switches to a temporary
87  * pcb and stack and never returns.  We block memory allocation
88  * until switch_exit has made things safe again.
89  */
90 cpu_exit(p)
91 	struct proc *p;
92 {
93 
94 	vmspace_free(p->p_vmspace);
95 
96 	(void) splimp();
97 	kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
98 	switch_exit();
99 	/* NOTREACHED */
100 }
101 
102 /*
103  * Dump the machine specific header information at the start of a core dump.
104  */
105 cpu_coredump(p, vp, cred)
106 	struct proc *p;
107 	struct vnode *vp;
108 	struct ucred *cred;
109 {
110 #ifdef HPUXCOMPAT
111 	/*
112 	 * If we loaded from an HP-UX format binary file we dump enough
113 	 * of an HP-UX style user struct so that the HP-UX debuggers can
114 	 * grok it.
115 	 */
116 	if (p->p_md.md_flags & MDP_HPUX)
117 		return (hpuxdumpu(vp, cred));
118 #endif
119 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
120 	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) NULL,
121 	    p));
122 }
123 
124 /*
125  * Move pages from one kernel virtual address to another.
126  * Both addresses are assumed to reside in the Sysmap,
127  * and size must be a multiple of CLSIZE.
128  */
129 pagemove(from, to, size)
130 	register caddr_t from, to;
131 	int size;
132 {
133 	register vm_offset_t pa;
134 
135 #ifdef DEBUG
136 	if (size & CLOFSET)
137 		panic("pagemove");
138 #endif
139 	while (size > 0) {
140 		pa = pmap_extract(kernel_pmap, (vm_offset_t)from);
141 #ifdef DEBUG
142 		if (pa == 0)
143 			panic("pagemove 2");
144 		if (pmap_extract(kernel_pmap, (vm_offset_t)to) != 0)
145 			panic("pagemove 3");
146 #endif
147 		pmap_remove(kernel_pmap,
148 			    (vm_offset_t)from, (vm_offset_t)from + PAGE_SIZE);
149 		pmap_enter(kernel_pmap,
150 			   (vm_offset_t)to, pa, VM_PROT_READ|VM_PROT_WRITE, 1);
151 		from += PAGE_SIZE;
152 		to += PAGE_SIZE;
153 		size -= PAGE_SIZE;
154 	}
155 }
156 
157 /*
158  * Map `size' bytes of physical memory starting at `paddr' into
159  * kernel VA space at `vaddr'.  Read/write and cache-inhibit status
160  * are specified by `prot'.
161  */
162 physaccess(vaddr, paddr, size, prot)
163 	caddr_t vaddr, paddr;
164 	register int size, prot;
165 {
166 	register struct pte *pte;
167 	register u_int page;
168 
169 	pte = kvtopte(vaddr);
170 	page = (u_int)paddr & PG_FRAME;
171 	for (size = btoc(size); size; size--) {
172 		*(int *)pte++ = PG_V | prot | page;
173 		page += NBPG;
174 	}
175 	TBIAS();
176 }
177 
178 physunaccess(vaddr, size)
179 	caddr_t vaddr;
180 	register int size;
181 {
182 	register struct pte *pte;
183 
184 	pte = kvtopte(vaddr);
185 	for (size = btoc(size); size; size--)
186 		*(int *)pte++ = PG_NV;
187 	TBIAS();
188 }
189 
190 /*
191  * Set a red zone in the kernel stack after the u. area.
192  * We don't support a redzone right now.  It really isn't clear
193  * that it is a good idea since, if the kernel stack were to roll
194  * into a write protected page, the processor would lock up (since
195  * it cannot create an exception frame) and we would get no useful
196  * post-mortem info.  Currently, under the DEBUG option, we just
197  * check at every clock interrupt to see if the current k-stack has
198  * gone too far (i.e. into the "redzone" page) and if so, panic.
199  * Look at _lev6intr in locore.s for more details.
200  */
201 /*ARGSUSED*/
202 setredzone(pte, vaddr)
203 	struct pte *pte;
204 	caddr_t vaddr;
205 {
206 }
207 
208 /*
209  * Convert kernel VA to physical address
210  */
211 kvtop(addr)
212 	register caddr_t addr;
213 {
214 	vm_offset_t va;
215 
216 	va = pmap_extract(kernel_pmap, (vm_offset_t)addr);
217 	if (va == 0)
218 		panic("kvtop: zero page frame");
219 	return((int)va);
220 }
221 
222 extern vm_map_t phys_map;
223 
224 /*
225  * Map an IO request into kernel virtual address space.
226  *
227  * XXX we allocate KVA space by using kmem_alloc_wait which we know
228  * allocates space without backing physical memory.  This implementation
229  * is a total crock, the multiple mappings of these physical pages should
230  * be reflected in the higher-level VM structures to avoid problems.
231  */
232 vmapbuf(bp)
233 	register struct buf *bp;
234 {
235 	register int npf;
236 	register caddr_t addr;
237 	register long flags = bp->b_flags;
238 	struct proc *p;
239 	int off;
240 	vm_offset_t kva;
241 	register vm_offset_t pa;
242 
243 	if ((flags & B_PHYS) == 0)
244 		panic("vmapbuf");
245 	addr = bp->b_saveaddr = bp->b_data;
246 	off = (int)addr & PGOFSET;
247 	p = bp->b_proc;
248 	npf = btoc(round_page(bp->b_bcount + off));
249 	kva = kmem_alloc_wait(phys_map, ctob(npf));
250 	bp->b_data = (caddr_t)(kva + off);
251 	while (npf--) {
252 		pa = pmap_extract(vm_map_pmap(&p->p_vmspace->vm_map),
253 		    (vm_offset_t)addr);
254 		if (pa == 0)
255 			panic("vmapbuf: null page frame");
256 		pmap_enter(vm_map_pmap(phys_map), kva, trunc_page(pa),
257 			   VM_PROT_READ|VM_PROT_WRITE, TRUE);
258 		addr += PAGE_SIZE;
259 		kva += PAGE_SIZE;
260 	}
261 }
262 
263 /*
264  * Free the io map PTEs associated with this IO operation.
265  */
266 vunmapbuf(bp)
267 	register struct buf *bp;
268 {
269 	register caddr_t addr;
270 	register int npf;
271 	vm_offset_t kva;
272 
273 	if ((bp->b_flags & B_PHYS) == 0)
274 		panic("vunmapbuf");
275 	addr = bp->b_data;
276 	npf = btoc(round_page(bp->b_bcount + ((int)addr & PGOFSET)));
277 	kva = (vm_offset_t)((int)addr & ~PGOFSET);
278 	kmem_free_wakeup(phys_map, kva, ctob(npf));
279 	bp->b_data = bp->b_saveaddr;
280 	bp->b_saveaddr = NULL;
281 }
282 
283 #ifdef MAPPEDCOPY
284 u_int mappedcopysize = 4096;
285 
286 mappedcopyin(fromp, top, count)
287 	register char *fromp, *top;
288 	register int count;
289 {
290 	register vm_offset_t kva, upa;
291 	register int off, len;
292 	int alignable;
293 	pmap_t upmap;
294 	extern caddr_t CADDR1;
295 
296 	kva = (vm_offset_t) CADDR1;
297 	off = (vm_offset_t)fromp & PAGE_MASK;
298 	alignable = (off == ((vm_offset_t)top & PAGE_MASK));
299 	upmap = vm_map_pmap(&curproc->p_vmspace->vm_map);
300 	while (count > 0) {
301 		/*
302 		 * First access of a page, use fubyte to make sure
303 		 * page is faulted in and read access allowed.
304 		 */
305 		if (fubyte(fromp) == -1)
306 			return (EFAULT);
307 		/*
308 		 * Map in the page and bcopy data in from it
309 		 */
310 		upa = pmap_extract(upmap, trunc_page(fromp));
311 		if (upa == 0)
312 			panic("mappedcopyin");
313 		len = min(count, PAGE_SIZE-off);
314 		pmap_enter(kernel_pmap, kva, upa, VM_PROT_READ, TRUE);
315 		if (len == PAGE_SIZE && alignable && off == 0)
316 			copypage(kva, top);
317 		else
318 			bcopy((caddr_t)(kva+off), top, len);
319 		fromp += len;
320 		top += len;
321 		count -= len;
322 		off = 0;
323 	}
324 	pmap_remove(kernel_pmap, kva, kva+PAGE_SIZE);
325 	return (0);
326 }
327 
328 mappedcopyout(fromp, top, count)
329 	register char *fromp, *top;
330 	register int count;
331 {
332 	register vm_offset_t kva, upa;
333 	register int off, len;
334 	int alignable;
335 	pmap_t upmap;
336 	extern caddr_t CADDR2;
337 
338 	kva = (vm_offset_t) CADDR2;
339 	off = (vm_offset_t)top & PAGE_MASK;
340 	alignable = (off == ((vm_offset_t)fromp & PAGE_MASK));
341 	upmap = vm_map_pmap(&curproc->p_vmspace->vm_map);
342 	while (count > 0) {
343 		/*
344 		 * First access of a page, use subyte to make sure
345 		 * page is faulted in and write access allowed.
346 		 */
347 		if (subyte(top, *fromp) == -1)
348 			return (EFAULT);
349 		/*
350 		 * Map in the page and bcopy data out to it
351 		 */
352 		upa = pmap_extract(upmap, trunc_page(top));
353 		if (upa == 0)
354 			panic("mappedcopyout");
355 		len = min(count, PAGE_SIZE-off);
356 		pmap_enter(kernel_pmap, kva, upa,
357 			   VM_PROT_READ|VM_PROT_WRITE, TRUE);
358 		if (len == PAGE_SIZE && alignable && off == 0)
359 			copypage(fromp, kva);
360 		else
361 			bcopy(fromp, (caddr_t)(kva+off), len);
362 		fromp += len;
363 		top += len;
364 		count -= len;
365 		off = 0;
366 	}
367 	pmap_remove(kernel_pmap, kva, kva+PAGE_SIZE);
368 	return (0);
369 }
370 #endif
371