]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/powerpc/aim/vm_machdep.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / powerpc / aim / vm_machdep.c
1 /*-
2  * Copyright (c) 1982, 1986 The Regents of the University of California.
3  * Copyright (c) 1989, 1990 William Jolitz
4  * Copyright (c) 1994 John Dyson
5  * 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, and William Jolitz.
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. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      from: @(#)vm_machdep.c  7.3 (Berkeley) 5/13/91
40  *      Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
41  * $FreeBSD$
42  */
43 /*-
44  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
45  * All rights reserved.
46  *
47  * Author: Chris G. Demetriou
48  *
49  * Permission to use, copy, modify and distribute this software and
50  * its documentation is hereby granted, provided that both the copyright
51  * notice and this permission notice appear in all copies of the
52  * software, derivative works or modified versions, and any portions
53  * thereof, and that both notices appear in supporting documentation.
54  *
55  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58  *
59  * Carnegie Mellon requests users of this software to return to
60  *
61  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62  *  School of Computer Science
63  *  Carnegie Mellon University
64  *  Pittsburgh PA 15213-3890
65  *
66  * any improvements or extensions that they make and grant Carnegie the
67  * rights to redistribute these changes.
68  */
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/proc.h>
73 #include <sys/malloc.h>
74 #include <sys/bio.h>
75 #include <sys/buf.h>
76 #include <sys/ktr.h>
77 #include <sys/lock.h>
78 #include <sys/mutex.h>
79 #include <sys/vnode.h>
80 #include <sys/vmmeter.h>
81 #include <sys/kernel.h>
82 #include <sys/mbuf.h>
83 #include <sys/sf_buf.h>
84 #include <sys/sysctl.h>
85 #include <sys/unistd.h>
86
87 #include <machine/cpu.h>
88 #include <machine/fpu.h>
89 #include <machine/frame.h>
90 #include <machine/md_var.h>
91 #include <machine/pcb.h>
92
93 #include <dev/ofw/openfirm.h>
94
95 #include <vm/vm.h>
96 #include <vm/vm_param.h>
97 #include <vm/vm_kern.h>
98 #include <vm/vm_page.h>
99 #include <vm/vm_map.h>
100 #include <vm/vm_extern.h>
101
102 /*
103  * On systems without a direct mapped region (e.g. PPC64),
104  * we use the same code as the Book E implementation. Since
105  * we need to have runtime detection of this, define some machinery
106  * for sf_bufs in this case, and ignore it on systems with direct maps.
107  */
108
109 #ifndef NSFBUFS
110 #define NSFBUFS         (512 + maxusers * 16)
111 #endif
112
113 static void sf_buf_init(void *arg);
114 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL);
115  
116 LIST_HEAD(sf_head, sf_buf);
117  
118 /* A hash table of active sendfile(2) buffers */
119 static struct sf_head *sf_buf_active;
120 static u_long sf_buf_hashmask;
121
122 #define SF_BUF_HASH(m)  (((m) - vm_page_array) & sf_buf_hashmask)
123
124 static TAILQ_HEAD(, sf_buf) sf_buf_freelist;
125 static u_int sf_buf_alloc_want;
126
127 /*
128  * A lock used to synchronize access to the hash table and free list
129  */
130 static struct mtx sf_buf_lock;
131
132
133 /*
134  * Finish a fork operation, with process p2 nearly set up.
135  * Copy and update the pcb, set up the stack so that the child
136  * ready to run and return to user mode.
137  */
138 void
139 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
140 {
141         struct  proc *p1;
142         struct  trapframe *tf;
143         struct  callframe *cf;
144         struct  pcb *pcb;
145
146         KASSERT(td1 == curthread || td1 == &thread0,
147             ("cpu_fork: p1 not curproc and not proc0"));
148         CTR3(KTR_PROC, "cpu_fork: called td1=%08x p2=%08x flags=%x", (u_int)td1, (u_int)p2, flags);
149
150         if ((flags & RFPROC) == 0)
151                 return;
152
153         p1 = td1->td_proc;
154
155         pcb = (struct pcb *)((td2->td_kstack +
156             td2->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb)) & ~0x2fU);
157         td2->td_pcb = pcb;
158
159         /* Copy the pcb */
160         bcopy(td1->td_pcb, pcb, sizeof(struct pcb));
161
162         /*
163          * Create a fresh stack for the new process.
164          * Copy the trap frame for the return to user mode as if from a
165          * syscall.  This copies most of the user mode register values.
166          */
167         tf = (struct trapframe *)pcb - 1;
168         bcopy(td1->td_frame, tf, sizeof(*tf));
169
170         /* Set up trap frame. */
171         tf->fixreg[FIRSTARG] = 0;
172         tf->fixreg[FIRSTARG + 1] = 0;
173         tf->cr &= ~0x10000000;
174
175         td2->td_frame = tf;
176
177         cf = (struct callframe *)tf - 1;
178         memset(cf, 0, sizeof(struct callframe));
179         cf->cf_func = (register_t)fork_return;
180         cf->cf_arg0 = (register_t)td2;
181         cf->cf_arg1 = (register_t)tf;
182
183         pcb->pcb_sp = (register_t)cf;
184         pcb->pcb_lr = (register_t)fork_trampoline;
185         pcb->pcb_cpu.aim.usr = kernel_pmap->pm_sr[USER_SR];
186
187         /* Setup to release spin count in fork_exit(). */
188         td2->td_md.md_spinlock_count = 1;
189         td2->td_md.md_saved_msr = PSL_KERNSET;
190
191         /*
192          * Now cpu_switch() can schedule the new process.
193          */
194 }
195
196 /*
197  * Intercept the return address from a freshly forked process that has NOT
198  * been scheduled yet.
199  *
200  * This is needed to make kernel threads stay in kernel mode.
201  */
202 void
203 cpu_set_fork_handler(td, func, arg)
204         struct thread *td;
205         void (*func)(void *);
206         void *arg;
207 {
208         struct  callframe *cf;
209
210         CTR4(KTR_PROC, "%s called with td=%08x func=%08x arg=%08x",
211             __func__, (u_int)td, (u_int)func, (u_int)arg);
212
213         cf = (struct callframe *)td->td_pcb->pcb_sp;
214
215         cf->cf_func = (register_t)func;
216         cf->cf_arg0 = (register_t)arg;
217 }
218
219 void
220 cpu_exit(td)
221         register struct thread *td;
222 {
223 }
224
225 /*
226  * Reset back to firmware.
227  */
228 void
229 cpu_reset()
230 {
231         OF_reboot();
232 }
233
234 /*
235  * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
236  */
237 static void
238 sf_buf_init(void *arg)
239 {
240         struct sf_buf *sf_bufs;
241         vm_offset_t sf_base;
242         int i;
243
244         /* Don't bother on systems with a direct map */
245
246         if (hw_direct_map)
247                 return;
248
249         nsfbufs = NSFBUFS;
250         TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
251
252         sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask);
253         TAILQ_INIT(&sf_buf_freelist);
254         sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE);
255         sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, M_NOWAIT | M_ZERO);
256
257         for (i = 0; i < nsfbufs; i++) {
258                 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
259                 TAILQ_INSERT_TAIL(&sf_buf_freelist, &sf_bufs[i], free_entry);
260         }
261         sf_buf_alloc_want = 0;
262         mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF);
263 }
264
265 /*
266  * Get an sf_buf from the freelist. Will block if none are available.
267  */
268 struct sf_buf *
269 sf_buf_alloc(struct vm_page *m, int flags)
270 {
271         struct sf_head *hash_list;
272         struct sf_buf *sf;
273         int error;
274
275         if (hw_direct_map) {
276                 /* Shortcut the direct mapped case */
277
278                 return ((struct sf_buf *)m);
279         }
280
281         hash_list = &sf_buf_active[SF_BUF_HASH(m)];
282         mtx_lock(&sf_buf_lock);
283         LIST_FOREACH(sf, hash_list, list_entry) {
284                 if (sf->m == m) {
285                         sf->ref_count++;
286                         if (sf->ref_count == 1) {
287                                 TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
288                                 nsfbufsused++;
289                                 nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
290                         }
291                         goto done;
292                 }
293         }
294
295         while ((sf = TAILQ_FIRST(&sf_buf_freelist)) == NULL) {
296                 if (flags & SFB_NOWAIT)
297                         goto done;
298
299                 sf_buf_alloc_want++;
300                 mbstat.sf_allocwait++;
301                 error = msleep(&sf_buf_freelist, &sf_buf_lock,
302                     (flags & SFB_CATCH) ? PCATCH | PVM : PVM, "sfbufa", 0);
303                 sf_buf_alloc_want--;
304
305                 /*
306                  * If we got a signal, don't risk going back to sleep.
307                  */
308                 if (error)
309                         goto done;
310         }
311
312         TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
313         if (sf->m != NULL)
314                 LIST_REMOVE(sf, list_entry);
315
316         LIST_INSERT_HEAD(hash_list, sf, list_entry);
317         sf->ref_count = 1;
318         sf->m = m;
319         nsfbufsused++;
320         nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
321         pmap_qenter(sf->kva, &sf->m, 1);
322 done:
323         mtx_unlock(&sf_buf_lock);
324         return (sf);
325 }
326
327 /*
328  * Detatch mapped page and release resources back to the system.
329  *
330  * Remove a reference from the given sf_buf, adding it to the free
331  * list when its reference count reaches zero. A freed sf_buf still,
332  * however, retains its virtual-to-physical mapping until it is
333  * recycled or reactivated by sf_buf_alloc(9).
334  */
335 void
336 sf_buf_free(struct sf_buf *sf)
337 {
338         if (hw_direct_map)
339                 return;
340
341         mtx_lock(&sf_buf_lock);
342         sf->ref_count--;
343         if (sf->ref_count == 0) {
344                 TAILQ_INSERT_TAIL(&sf_buf_freelist, sf, free_entry);
345                 nsfbufsused--;
346
347                 if (sf_buf_alloc_want > 0)
348                         wakeup_one(&sf_buf_freelist);
349         }
350         mtx_unlock(&sf_buf_lock);
351 }
352
353 /*
354  * Software interrupt handler for queued VM system processing.
355  */
356 void
357 swi_vm(void *dummy)
358 {
359 #if 0 /* XXX: Don't have busdma stuff yet */
360         if (busdma_swi_pending != 0)
361                 busdma_swi();
362 #endif
363 }
364
365 /*
366  * Tell whether this address is in some physical memory region.
367  * Currently used by the kernel coredump code in order to avoid
368  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
369  * or other unpredictable behaviour.
370  */
371
372
373 int
374 is_physical_memory(addr)
375         vm_offset_t addr;
376 {
377         /*
378          * stuff other tests for known memory-mapped devices (PCI?)
379          * here
380          */
381
382         return 1;
383 }
384
385 /*
386  * Threading functions
387  */
388 void
389 cpu_thread_exit(struct thread *td)
390 {
391 }
392
393 void
394 cpu_thread_clean(struct thread *td)
395 {
396 }
397
398 void
399 cpu_thread_alloc(struct thread *td)
400 {
401         struct pcb *pcb;
402
403         pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
404             sizeof(struct pcb)) & ~0x2fU);
405         td->td_pcb = pcb;
406         td->td_frame = (struct trapframe *)pcb - 1;
407 }
408
409 void
410 cpu_thread_free(struct thread *td)
411 {
412 }
413
414 void
415 cpu_thread_swapin(struct thread *td)
416 {
417 }
418
419 void
420 cpu_thread_swapout(struct thread *td)
421 {
422 }
423
424 void
425 cpu_set_upcall(struct thread *td, struct thread *td0)
426 {
427         struct pcb *pcb2;
428         struct trapframe *tf;
429         struct callframe *cf;
430
431         pcb2 = td->td_pcb;
432
433         /* Copy the upcall pcb */
434         bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
435
436         /* Create a stack for the new thread */
437         tf = td->td_frame;
438         bcopy(td0->td_frame, tf, sizeof(struct trapframe));
439         tf->fixreg[FIRSTARG] = 0;
440         tf->fixreg[FIRSTARG + 1] = 0;
441         tf->cr &= ~0x10000000;
442
443         /* Set registers for trampoline to user mode. */
444         cf = (struct callframe *)tf - 1;
445         memset(cf, 0, sizeof(struct callframe));
446         cf->cf_func = (register_t)fork_return;
447         cf->cf_arg0 = (register_t)td;
448         cf->cf_arg1 = (register_t)tf;
449
450         pcb2->pcb_sp = (register_t)cf;
451         pcb2->pcb_lr = (register_t)fork_trampoline;
452         pcb2->pcb_cpu.aim.usr = kernel_pmap->pm_sr[USER_SR];
453
454         /* Setup to release spin count in fork_exit(). */
455         td->td_md.md_spinlock_count = 1;
456         td->td_md.md_saved_msr = PSL_KERNSET;
457 }
458
459 void
460 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
461         stack_t *stack)
462 {
463         struct trapframe *tf;
464         uint32_t sp;
465
466         tf = td->td_frame;
467         /* align stack and alloc space for frame ptr and saved LR */
468         sp = ((uint32_t)stack->ss_sp + stack->ss_size - sizeof(uint64_t)) &
469             ~0x1f;
470         bzero(tf, sizeof(struct trapframe));
471
472         tf->fixreg[1] = (register_t)sp;
473         tf->fixreg[3] = (register_t)arg;
474         tf->srr0 = (register_t)entry;
475         tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
476         td->td_pcb->pcb_flags = 0;
477
478         td->td_retval[0] = (register_t)entry;
479         td->td_retval[1] = 0;
480 }
481
482 int
483 cpu_set_user_tls(struct thread *td, void *tls_base)
484 {
485
486         td->td_frame->fixreg[2] = (register_t)tls_base + 0x7008;
487         return (0);
488 }