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