xref: /netbsd-src/sys/arch/m68k/m68k/vm_machdep.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: vm_machdep.c,v 1.35 2011/01/14 02:06:27 rmind Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1990, 1993
5  *	The Regents of the University of California.  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.
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. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * from: Utah $Hdr: vm_machdep.c 1.21 91/04/06$
36  *
37  *	@(#)vm_machdep.c	8.6 (Berkeley) 1/12/94
38  */
39 /*
40  * Copyright (c) 1988 University of Utah.
41  *
42  * This code is derived from software contributed to Berkeley by
43  * the Systems Programming Group of the University of Utah Computer
44  * Science Department.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *	This product includes software developed by the University of
57  *	California, Berkeley and its contributors.
58  * 4. Neither the name of the University nor the names of its contributors
59  *    may be used to endorse or promote products derived from this software
60  *    without specific prior written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72  * SUCH DAMAGE.
73  *
74  * from: Utah $Hdr: vm_machdep.c 1.21 91/04/06$
75  *
76  *	@(#)vm_machdep.c	8.6 (Berkeley) 1/12/94
77  */
78 
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.35 2011/01/14 02:06:27 rmind Exp $");
81 
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/proc.h>
85 #include <sys/cpu.h>
86 #include <sys/malloc.h>
87 #include <sys/buf.h>
88 #include <sys/vnode.h>
89 #include <sys/core.h>
90 #include <sys/exec.h>
91 
92 #include <machine/frame.h>
93 #include <machine/pte.h>
94 #include <machine/pcb.h>
95 
96 #include <uvm/uvm_extern.h>
97 
98 void
99 cpu_proc_fork(struct proc *p1, struct proc *p2)
100 {
101 
102 	p2->p_md.mdp_flags = p1->p_md.mdp_flags;
103 }
104 
105 /*
106  * Finish a fork operation, with process l2 nearly set up.
107  * Copy and update the pcb and trap frame, making the child ready to run.
108  *
109  * Rig the child's kernel stack so that it will start out in
110  * lwp_trampoline() and call child_return() with l2 as an
111  * argument. This causes the newly-created child process to go
112  * directly to user level with an apparent return value of 0 from
113  * fork(), while the parent process returns normally.
114  *
115  * l1 is the process being forked; if l1 == &lwp0, we are creating
116  * a kernel thread, and the return path and argument are specified with
117  * `func' and `arg'.
118  *
119  * If an alternate user-level stack is requested (with non-zero values
120  * in both the stack and stacksize args), set up the user stack pointer
121  * accordingly.
122  */
123 void
124 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
125     void (*func)(void *), void *arg)
126 {
127 	struct pcb *pcb1, *pcb2;
128 	struct trapframe *tf;
129 	struct switchframe *sf;
130 
131 	pcb1 = lwp_getpcb(l1);
132 	pcb2 = lwp_getpcb(l2);
133 
134 	l2->l_md.md_flags = l1->l_md.md_flags;
135 
136 	/* Copy pcb from lwp l1 to l2. */
137 	if (l1 == curlwp) {
138 		/* Sync the PCB before we copy it. */
139 		savectx(curpcb);
140 	} else {
141 		KASSERT(l1 == &lwp0);
142 	}
143 
144 	*pcb2 = *pcb1;
145 
146 	/*
147 	 * Copy the trap frame.
148 	 */
149 	tf = (struct trapframe *)(uvm_lwp_getuarea(l2) + USPACE) - 1;
150 	l2->l_md.md_regs = (int *)tf;
151 	*tf = *(struct trapframe *)l1->l_md.md_regs;
152 
153 	/*
154 	 * If specified, give the child a different stack.
155 	 */
156 	if (stack != NULL)
157 		tf->tf_regs[15] = (u_int)stack + stacksize;
158 
159 	sf = (struct switchframe *)tf - 1;
160 	sf->sf_pc = (u_int)lwp_trampoline;
161 	pcb2->pcb_regs[6] = (int)func;		/* A2 */
162 	pcb2->pcb_regs[7] = (int)arg;		/* A3 */
163 	pcb2->pcb_regs[8] = (int)l2;		/* A4 */
164 	pcb2->pcb_regs[11] = (int)sf;		/* SSP */
165 	pcb2->pcb_ps = PSL_LOWIPL;		/* start kthreads at IPL 0 */
166 }
167 
168 void
169 cpu_setfunc(struct lwp *l, void (*func)(void *), void *arg)
170 {
171 	struct pcb *pcb = lwp_getpcb(l);
172 	struct trapframe *tf = (struct trapframe *)l->l_md.md_regs;
173 	struct switchframe *sf = (struct switchframe *)tf - 1;
174 
175 	sf->sf_pc = (u_int)setfunc_trampoline;
176 	pcb->pcb_regs[6] = (int)func;		/* A2 */
177 	pcb->pcb_regs[7] = (int)arg;		/* A3 */
178 	pcb->pcb_regs[11] = (int)sf;		/* SSP */
179 }
180 
181 void
182 cpu_lwp_free(struct lwp *l, int proc)
183 {
184 
185 	/* Nothing to do */
186 }
187 
188 void
189 cpu_lwp_free2(struct lwp *l)
190 {
191 
192 	/* Nothing to do */
193 }
194 
195 /*
196  * Map a user I/O request into kernel virtual address space.
197  * Note: the pages are already locked by uvm_vslock(), so we
198  * do not need to pass an access_type to pmap_enter().
199  */
200 void
201 vmapbuf(struct buf *bp, vsize_t len)
202 {
203 	struct pmap *upmap, *kpmap;
204 	vaddr_t uva;		/* User VA (map from) */
205 	vaddr_t kva;		/* Kernel VA (new to) */
206 	paddr_t pa; 		/* physical address */
207 	vsize_t off;
208 
209 	if ((bp->b_flags & B_PHYS) == 0)
210 		panic("vmapbuf");
211 
212 	uva = m68k_trunc_page(bp->b_saveaddr = bp->b_data);
213 	off = (vaddr_t)bp->b_data - uva;
214 	len = m68k_round_page(off + len);
215 	kva = uvm_km_alloc(phys_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
216 	bp->b_data = (void *)(kva + off);
217 
218 	upmap = vm_map_pmap(&bp->b_proc->p_vmspace->vm_map);
219 	kpmap = vm_map_pmap(phys_map);
220 	do {
221 		if (pmap_extract(upmap, uva, &pa) == false)
222 			panic("vmapbuf: null page frame");
223 #ifdef M68K_VAC
224 		pmap_enter(kpmap, kva, pa, VM_PROT_READ | VM_PROT_WRITE,
225 		    PMAP_WIRED);
226 #else
227 		pmap_kenter_pa(kva, pa, VM_PROT_READ | VM_PROT_WRITE, 0);
228 #endif
229 		uva += PAGE_SIZE;
230 		kva += PAGE_SIZE;
231 		len -= PAGE_SIZE;
232 	} while (len);
233 	pmap_update(kpmap);
234 }
235 
236 /*
237  * Unmap a previously-mapped user I/O request.
238  */
239 void
240 vunmapbuf(struct buf *bp, vsize_t len)
241 {
242 	vaddr_t kva;
243 	vsize_t off;
244 
245 	if ((bp->b_flags & B_PHYS) == 0)
246 		panic("vunmapbuf");
247 
248 	kva = m68k_trunc_page(bp->b_data);
249 	off = (vaddr_t)bp->b_data - kva;
250 	len = m68k_round_page(off + len);
251 
252 #ifdef M68K_VAC
253 	pmap_remove(vm_map_pmap(phys_map), kva, kva + len);
254 #else
255 	pmap_kremove(kva, len);
256 #endif
257 	pmap_update(pmap_kernel());
258 	uvm_km_free(phys_map, kva, len, UVM_KMF_VAONLY);
259 	bp->b_data = bp->b_saveaddr;
260 	bp->b_saveaddr = 0;
261 }
262 
263 
264 #if defined(M68K_MMU_MOTOROLA) || defined(M68K_MMU_HP)
265 
266 #include <m68k/cacheops.h>
267 
268 /*
269  * Map `size' bytes of physical memory starting at `paddr' into
270  * kernel VA space at `vaddr'.  Read/write and cache-inhibit status
271  * are specified by `prot'.
272  */
273 void
274 physaccess(void *vaddr, void *paddr, int size, int prot)
275 {
276 	pt_entry_t *pte;
277 	u_int page;
278 
279 	pte = kvtopte(vaddr);
280 	page = (u_int)paddr & PG_FRAME;
281 	for (size = btoc(size); size; size--) {
282 		*pte++ = PG_V | prot | page;
283 		page += PAGE_SIZE;
284 	}
285 	TBIAS();
286 }
287 
288 void
289 physunaccess(void *vaddr, int size)
290 {
291 	pt_entry_t *pte;
292 
293 	pte = kvtopte(vaddr);
294 	for (size = btoc(size); size; size--)
295 		*pte++ = PG_NV;
296 	TBIAS();
297 }
298 
299 /*
300  * Convert kernel VA to physical address
301  */
302 int
303 kvtop(void *addr)
304 {
305 	paddr_t pa;
306 
307 	if (pmap_extract(pmap_kernel(), (vaddr_t)addr, &pa) == false)
308 		panic("kvtop: zero page frame");
309 	return (int)pa;
310 }
311 
312 #endif
313 
314