xref: /netbsd-src/sys/arch/m68k/m68k/vm_machdep.c (revision 27fd3f6531803adac12382d7643a9a492b576601)
1 /*	$NetBSD: vm_machdep.c,v 1.28 2009/05/30 17:52:05 martin 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.28 2009/05/30 17:52:05 martin Exp $");
81 
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/proc.h>
85 #include <sys/malloc.h>
86 #include <sys/buf.h>
87 #include <sys/vnode.h>
88 #include <sys/user.h>
89 #include <sys/core.h>
90 #include <sys/exec.h>
91 
92 #include <machine/frame.h>
93 #include <machine/cpu.h>
94 #include <machine/pte.h>
95 #include <machine/reg.h>
96 
97 #include <uvm/uvm_extern.h>
98 
99 void
100 cpu_proc_fork(struct proc *p1, struct proc *p2)
101 {
102 
103 	p2->p_md.mdp_flags = p1->p_md.mdp_flags;
104 }
105 
106 /*
107  * Finish a fork operation, with process l2 nearly set up.
108  * Copy and update the pcb and trap frame, making the child ready to run.
109  *
110  * Rig the child's kernel stack so that it will start out in
111  * lwp_trampoline() and call child_return() with l2 as an
112  * argument. This causes the newly-created child process to go
113  * directly to user level with an apparent return value of 0 from
114  * fork(), while the parent process returns normally.
115  *
116  * l1 is the process being forked; if l1 == &lwp0, we are creating
117  * a kernel thread, and the return path and argument are specified with
118  * `func' and `arg'.
119  *
120  * If an alternate user-level stack is requested (with non-zero values
121  * in both the stack and stacksize args), set up the user stack pointer
122  * accordingly.
123  */
124 void
125 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
126     void (*func)(void *), void *arg)
127 {
128 	struct pcb *pcb = &l2->l_addr->u_pcb;
129 	struct trapframe *tf;
130 	struct switchframe *sf;
131 	extern struct pcb *curpcb;
132 	extern void lwp_trampoline(void);
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 	}
141 #ifdef DIAGNOSTIC
142 	else if (l1 != &lwp0)
143 		panic("cpu_lwp_fork: curlwp");
144 #endif
145 	*pcb = l1->l_addr->u_pcb;
146 
147 	/*
148 	 * Copy the trap frame.
149 	 */
150 	tf = (struct trapframe *)((u_int)l2->l_addr + USPACE) - 1;
151 	l2->l_md.md_regs = (int *)tf;
152 	*tf = *(struct trapframe *)l1->l_md.md_regs;
153 
154 	/*
155 	 * If specified, give the child a different stack.
156 	 */
157 	if (stack != NULL)
158 		tf->tf_regs[15] = (u_int)stack + stacksize;
159 
160 	sf = (struct switchframe *)tf - 1;
161 	sf->sf_pc = (u_int)lwp_trampoline;
162 	pcb->pcb_regs[6] = (int)func;		/* A2 */
163 	pcb->pcb_regs[7] = (int)arg;		/* A3 */
164 	pcb->pcb_regs[8] = (int)l2;		/* A4 */
165 	pcb->pcb_regs[11] = (int)sf;		/* SSP */
166 	pcb->pcb_ps = PSL_LOWIPL;		/* start kthreads at IPL 0 */
167 }
168 
169 void
170 cpu_setfunc(struct lwp *l, void (*func)(void *), void *arg)
171 {
172 	struct pcb *pcb = &l->l_addr->u_pcb;
173 	struct trapframe *tf = (struct trapframe *)l->l_md.md_regs;
174 	struct switchframe *sf = (struct switchframe *)tf - 1;
175 	extern void setfunc_trampoline(void);
176 
177 	sf->sf_pc = (u_int)setfunc_trampoline;
178 	pcb->pcb_regs[6] = (int)func;		/* A2 */
179 	pcb->pcb_regs[7] = (int)arg;		/* A3 */
180 	pcb->pcb_regs[11] = (int)sf;		/* SSP */
181 }
182 
183 void
184 cpu_lwp_free(struct lwp *l, int proc)
185 {
186 
187 	/* Nothing to do */
188 }
189 
190 void
191 cpu_lwp_free2(struct lwp *l)
192 {
193 
194 	/* Nothing to do */
195 }
196 
197 /*
198  * Map a user I/O request into kernel virtual address space.
199  * Note: the pages are already locked by uvm_vslock(), so we
200  * do not need to pass an access_type to pmap_enter().
201  */
202 void
203 vmapbuf(struct buf *bp, vsize_t len)
204 {
205 	struct pmap *upmap, *kpmap;
206 	vaddr_t uva;		/* User VA (map from) */
207 	vaddr_t kva;		/* Kernel VA (new to) */
208 	paddr_t pa; 		/* physical address */
209 	vsize_t off;
210 
211 	if ((bp->b_flags & B_PHYS) == 0)
212 		panic("vmapbuf");
213 
214 	uva = m68k_trunc_page(bp->b_saveaddr = bp->b_data);
215 	off = (vaddr_t)bp->b_data - uva;
216 	len = m68k_round_page(off + len);
217 	kva = uvm_km_alloc(phys_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
218 	bp->b_data = (void *)(kva + off);
219 
220 	upmap = vm_map_pmap(&bp->b_proc->p_vmspace->vm_map);
221 	kpmap = vm_map_pmap(phys_map);
222 	do {
223 		if (pmap_extract(upmap, uva, &pa) == false)
224 			panic("vmapbuf: null page frame");
225 #ifdef M68K_VAC
226 		pmap_enter(kpmap, kva, pa, VM_PROT_READ | VM_PROT_WRITE,
227 		    PMAP_WIRED);
228 #else
229 		pmap_kenter_pa(kva, pa, VM_PROT_READ | VM_PROT_WRITE);
230 #endif
231 		uva += PAGE_SIZE;
232 		kva += PAGE_SIZE;
233 		len -= PAGE_SIZE;
234 	} while (len);
235 	pmap_update(kpmap);
236 }
237 
238 /*
239  * Unmap a previously-mapped user I/O request.
240  */
241 void
242 vunmapbuf(struct buf *bp, vsize_t len)
243 {
244 	vaddr_t kva;
245 	vsize_t off;
246 
247 	if ((bp->b_flags & B_PHYS) == 0)
248 		panic("vunmapbuf");
249 
250 	kva = m68k_trunc_page(bp->b_data);
251 	off = (vaddr_t)bp->b_data - kva;
252 	len = m68k_round_page(off + len);
253 
254 #ifdef M68K_VAC
255 	pmap_remove(vm_map_pmap(phys_map), kva, kva + len);
256 #else
257 	pmap_kremove(kva, len);
258 #endif
259 	pmap_update(pmap_kernel());
260 	uvm_km_free(phys_map, kva, len, UVM_KMF_VAONLY);
261 	bp->b_data = bp->b_saveaddr;
262 	bp->b_saveaddr = 0;
263 }
264 
265 
266 #if defined(M68K_MMU_MOTOROLA) || defined(M68K_MMU_HP)
267 
268 #include <m68k/cacheops.h>
269 
270 /*
271  * Map `size' bytes of physical memory starting at `paddr' into
272  * kernel VA space at `vaddr'.  Read/write and cache-inhibit status
273  * are specified by `prot'.
274  */
275 void
276 physaccess(void *vaddr, void *paddr, int size, int prot)
277 {
278 	pt_entry_t *pte;
279 	u_int page;
280 
281 	pte = kvtopte(vaddr);
282 	page = (u_int)paddr & PG_FRAME;
283 	for (size = btoc(size); size; size--) {
284 		*pte++ = PG_V | prot | page;
285 		page += PAGE_SIZE;
286 	}
287 	TBIAS();
288 }
289 
290 void
291 physunaccess(void *vaddr, int size)
292 {
293 	pt_entry_t *pte;
294 
295 	pte = kvtopte(vaddr);
296 	for (size = btoc(size); size; size--)
297 		*pte++ = PG_NV;
298 	TBIAS();
299 }
300 
301 /*
302  * Convert kernel VA to physical address
303  */
304 int
305 kvtop(void *addr)
306 {
307 	paddr_t pa;
308 
309 	if (pmap_extract(pmap_kernel(), (vaddr_t)addr, &pa) == false)
310 		panic("kvtop: zero page frame");
311 	return (int)pa;
312 }
313 
314 #endif
315