]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/powerpc/aim/vm_machdep.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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/syscall.h>
85 #include <sys/sysctl.h>
86 #include <sys/sysent.h>
87 #include <sys/unistd.h>
88
89 #include <machine/cpu.h>
90 #include <machine/fpu.h>
91 #include <machine/frame.h>
92 #include <machine/md_var.h>
93 #include <machine/pcb.h>
94
95 #include <dev/ofw/openfirm.h>
96
97 #include <vm/vm.h>
98 #include <vm/vm_param.h>
99 #include <vm/vm_kern.h>
100 #include <vm/vm_page.h>
101 #include <vm/vm_map.h>
102 #include <vm/vm_extern.h>
103
104 /*
105  * On systems without a direct mapped region (e.g. PPC64),
106  * we use the same code as the Book E implementation. Since
107  * we need to have runtime detection of this, define some machinery
108  * for sf_bufs in this case, and ignore it on systems with direct maps.
109  */
110
111 #ifndef NSFBUFS
112 #define NSFBUFS         (512 + maxusers * 16)
113 #endif
114
115 static void sf_buf_init(void *arg);
116 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL);
117  
118 LIST_HEAD(sf_head, sf_buf);
119  
120 /* A hash table of active sendfile(2) buffers */
121 static struct sf_head *sf_buf_active;
122 static u_long sf_buf_hashmask;
123
124 #define SF_BUF_HASH(m)  (((m) - vm_page_array) & sf_buf_hashmask)
125
126 static TAILQ_HEAD(, sf_buf) sf_buf_freelist;
127 static u_int sf_buf_alloc_want;
128
129 /*
130  * A lock used to synchronize access to the hash table and free list
131  */
132 static struct mtx sf_buf_lock;
133
134
135 /*
136  * Finish a fork operation, with process p2 nearly set up.
137  * Copy and update the pcb, set up the stack so that the child
138  * ready to run and return to user mode.
139  */
140 void
141 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
142 {
143         struct  proc *p1;
144         struct  trapframe *tf;
145         struct  callframe *cf;
146         struct  pcb *pcb;
147
148         KASSERT(td1 == curthread || td1 == &thread0,
149             ("cpu_fork: p1 not curproc and not proc0"));
150         CTR3(KTR_PROC, "cpu_fork: called td1=%08x p2=%08x flags=%x", (u_int)td1, (u_int)p2, flags);
151
152         if ((flags & RFPROC) == 0)
153                 return;
154
155         p1 = td1->td_proc;
156
157         pcb = (struct pcb *)((td2->td_kstack +
158             td2->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb)) & ~0x2fU);
159         td2->td_pcb = pcb;
160
161         /* Copy the pcb */
162         bcopy(td1->td_pcb, pcb, sizeof(struct pcb));
163
164         /*
165          * Create a fresh stack for the new process.
166          * Copy the trap frame for the return to user mode as if from a
167          * syscall.  This copies most of the user mode register values.
168          */
169         tf = (struct trapframe *)pcb - 1;
170         bcopy(td1->td_frame, tf, sizeof(*tf));
171
172         /* Set up trap frame. */
173         tf->fixreg[FIRSTARG] = 0;
174         tf->fixreg[FIRSTARG + 1] = 0;
175         tf->cr &= ~0x10000000;
176
177         td2->td_frame = tf;
178
179         cf = (struct callframe *)tf - 1;
180         memset(cf, 0, sizeof(struct callframe));
181         cf->cf_func = (register_t)fork_return;
182         cf->cf_arg0 = (register_t)td2;
183         cf->cf_arg1 = (register_t)tf;
184
185         pcb->pcb_sp = (register_t)cf;
186         pcb->pcb_lr = (register_t)fork_trampoline;
187         pcb->pcb_cpu.aim.usr = kernel_pmap->pm_sr[USER_SR];
188
189         /* Setup to release spin count in fork_exit(). */
190         td2->td_md.md_spinlock_count = 1;
191         td2->td_md.md_saved_msr = PSL_KERNSET;
192
193         /*
194          * Now cpu_switch() can schedule the new process.
195          */
196 }
197
198 /*
199  * Intercept the return address from a freshly forked process that has NOT
200  * been scheduled yet.
201  *
202  * This is needed to make kernel threads stay in kernel mode.
203  */
204 void
205 cpu_set_fork_handler(td, func, arg)
206         struct thread *td;
207         void (*func)(void *);
208         void *arg;
209 {
210         struct  callframe *cf;
211
212         CTR4(KTR_PROC, "%s called with td=%08x func=%08x arg=%08x",
213             __func__, (u_int)td, (u_int)func, (u_int)arg);
214
215         cf = (struct callframe *)td->td_pcb->pcb_sp;
216
217         cf->cf_func = (register_t)func;
218         cf->cf_arg0 = (register_t)arg;
219 }
220
221 void
222 cpu_exit(td)
223         register struct thread *td;
224 {
225 }
226
227 /*
228  * Reset back to firmware.
229  */
230 void
231 cpu_reset()
232 {
233         OF_reboot();
234 }
235
236 /*
237  * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
238  */
239 static void
240 sf_buf_init(void *arg)
241 {
242         struct sf_buf *sf_bufs;
243         vm_offset_t sf_base;
244         int i;
245
246         /* Don't bother on systems with a direct map */
247
248         if (hw_direct_map)
249                 return;
250
251         nsfbufs = NSFBUFS;
252         TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
253
254         sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask);
255         TAILQ_INIT(&sf_buf_freelist);
256         sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE);
257         sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, M_NOWAIT | M_ZERO);
258
259         for (i = 0; i < nsfbufs; i++) {
260                 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
261                 TAILQ_INSERT_TAIL(&sf_buf_freelist, &sf_bufs[i], free_entry);
262         }
263         sf_buf_alloc_want = 0;
264         mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF);
265 }
266
267 /*
268  * Get an sf_buf from the freelist. Will block if none are available.
269  */
270 struct sf_buf *
271 sf_buf_alloc(struct vm_page *m, int flags)
272 {
273         struct sf_head *hash_list;
274         struct sf_buf *sf;
275         int error;
276
277         if (hw_direct_map) {
278                 /* Shortcut the direct mapped case */
279
280                 return ((struct sf_buf *)m);
281         }
282
283         hash_list = &sf_buf_active[SF_BUF_HASH(m)];
284         mtx_lock(&sf_buf_lock);
285         LIST_FOREACH(sf, hash_list, list_entry) {
286                 if (sf->m == m) {
287                         sf->ref_count++;
288                         if (sf->ref_count == 1) {
289                                 TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
290                                 nsfbufsused++;
291                                 nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
292                         }
293                         goto done;
294                 }
295         }
296
297         while ((sf = TAILQ_FIRST(&sf_buf_freelist)) == NULL) {
298                 if (flags & SFB_NOWAIT)
299                         goto done;
300
301                 sf_buf_alloc_want++;
302                 mbstat.sf_allocwait++;
303                 error = msleep(&sf_buf_freelist, &sf_buf_lock,
304                     (flags & SFB_CATCH) ? PCATCH | PVM : PVM, "sfbufa", 0);
305                 sf_buf_alloc_want--;
306
307                 /*
308                  * If we got a signal, don't risk going back to sleep.
309                  */
310                 if (error)
311                         goto done;
312         }
313
314         TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
315         if (sf->m != NULL)
316                 LIST_REMOVE(sf, list_entry);
317
318         LIST_INSERT_HEAD(hash_list, sf, list_entry);
319         sf->ref_count = 1;
320         sf->m = m;
321         nsfbufsused++;
322         nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
323         pmap_qenter(sf->kva, &sf->m, 1);
324 done:
325         mtx_unlock(&sf_buf_lock);
326         return (sf);
327 }
328
329 /*
330  * Detatch mapped page and release resources back to the system.
331  *
332  * Remove a reference from the given sf_buf, adding it to the free
333  * list when its reference count reaches zero. A freed sf_buf still,
334  * however, retains its virtual-to-physical mapping until it is
335  * recycled or reactivated by sf_buf_alloc(9).
336  */
337 void
338 sf_buf_free(struct sf_buf *sf)
339 {
340         if (hw_direct_map)
341                 return;
342
343         mtx_lock(&sf_buf_lock);
344         sf->ref_count--;
345         if (sf->ref_count == 0) {
346                 TAILQ_INSERT_TAIL(&sf_buf_freelist, sf, free_entry);
347                 nsfbufsused--;
348
349                 if (sf_buf_alloc_want > 0)
350                         wakeup_one(&sf_buf_freelist);
351         }
352         mtx_unlock(&sf_buf_lock);
353 }
354
355 /*
356  * Software interrupt handler for queued VM system processing.
357  */
358 void
359 swi_vm(void *dummy)
360 {
361 #if 0 /* XXX: Don't have busdma stuff yet */
362         if (busdma_swi_pending != 0)
363                 busdma_swi();
364 #endif
365 }
366
367 /*
368  * Tell whether this address is in some physical memory region.
369  * Currently used by the kernel coredump code in order to avoid
370  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
371  * or other unpredictable behaviour.
372  */
373
374
375 int
376 is_physical_memory(addr)
377         vm_offset_t addr;
378 {
379         /*
380          * stuff other tests for known memory-mapped devices (PCI?)
381          * here
382          */
383
384         return 1;
385 }
386
387 /*
388  * Threading functions
389  */
390 void
391 cpu_thread_exit(struct thread *td)
392 {
393 }
394
395 void
396 cpu_thread_clean(struct thread *td)
397 {
398 }
399
400 void
401 cpu_thread_alloc(struct thread *td)
402 {
403         struct pcb *pcb;
404
405         pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
406             sizeof(struct pcb)) & ~0x2fU);
407         td->td_pcb = pcb;
408         td->td_frame = (struct trapframe *)pcb - 1;
409 }
410
411 void
412 cpu_thread_free(struct thread *td)
413 {
414 }
415
416 void
417 cpu_thread_swapin(struct thread *td)
418 {
419 }
420
421 void
422 cpu_thread_swapout(struct thread *td)
423 {
424 }
425
426 void
427 cpu_set_syscall_retval(struct thread *td, int error)
428 {
429         struct proc *p;
430         struct trapframe *tf;
431         int fixup;
432
433         if (error == EJUSTRETURN)
434                 return;
435
436         p = td->td_proc;
437         tf = td->td_frame;
438
439         if (tf->fixreg[0] == SYS___syscall) {
440                 int code = tf->fixreg[FIRSTARG + 1];
441                 if (p->p_sysent->sv_mask)
442                         code &= p->p_sysent->sv_mask;
443                 fixup = (code != SYS_freebsd6_lseek && code != SYS_lseek) ?
444                     1 : 0;
445         } else
446                 fixup = 0;
447
448         switch (error) {
449         case 0:
450                 if (fixup) {
451                         /*
452                          * 64-bit return, 32-bit syscall. Fixup byte order
453                          */
454                         tf->fixreg[FIRSTARG] = 0;
455                         tf->fixreg[FIRSTARG + 1] = td->td_retval[0];
456                 } else {
457                         tf->fixreg[FIRSTARG] = td->td_retval[0];
458                         tf->fixreg[FIRSTARG + 1] = td->td_retval[1];
459                 }
460                 tf->cr &= ~0x10000000;          /* XXX: Magic number */
461                 break;
462         case ERESTART:
463                 /*
464                  * Set user's pc back to redo the system call.
465                  */
466                 tf->srr0 -= 4;
467                 break;
468         default:
469                 if (p->p_sysent->sv_errsize) {
470                         error = (error < p->p_sysent->sv_errsize) ?
471                             p->p_sysent->sv_errtbl[error] : -1;
472                 }
473                 tf->fixreg[FIRSTARG] = error;
474                 tf->cr |= 0x10000000;           /* XXX: Magic number */
475                 break;
476         }
477 }
478
479 void
480 cpu_set_upcall(struct thread *td, struct thread *td0)
481 {
482         struct pcb *pcb2;
483         struct trapframe *tf;
484         struct callframe *cf;
485
486         pcb2 = td->td_pcb;
487
488         /* Copy the upcall pcb */
489         bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
490
491         /* Create a stack for the new thread */
492         tf = td->td_frame;
493         bcopy(td0->td_frame, tf, sizeof(struct trapframe));
494         tf->fixreg[FIRSTARG] = 0;
495         tf->fixreg[FIRSTARG + 1] = 0;
496         tf->cr &= ~0x10000000;
497
498         /* Set registers for trampoline to user mode. */
499         cf = (struct callframe *)tf - 1;
500         memset(cf, 0, sizeof(struct callframe));
501         cf->cf_func = (register_t)fork_return;
502         cf->cf_arg0 = (register_t)td;
503         cf->cf_arg1 = (register_t)tf;
504
505         pcb2->pcb_sp = (register_t)cf;
506         pcb2->pcb_lr = (register_t)fork_trampoline;
507         pcb2->pcb_cpu.aim.usr = kernel_pmap->pm_sr[USER_SR];
508
509         /* Setup to release spin count in fork_exit(). */
510         td->td_md.md_spinlock_count = 1;
511         td->td_md.md_saved_msr = PSL_KERNSET;
512 }
513
514 void
515 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
516         stack_t *stack)
517 {
518         struct trapframe *tf;
519         uint32_t sp;
520
521         tf = td->td_frame;
522         /* align stack and alloc space for frame ptr and saved LR */
523         sp = ((uint32_t)stack->ss_sp + stack->ss_size - sizeof(uint64_t)) &
524             ~0x1f;
525         bzero(tf, sizeof(struct trapframe));
526
527         tf->fixreg[1] = (register_t)sp;
528         tf->fixreg[3] = (register_t)arg;
529         tf->srr0 = (register_t)entry;
530         tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
531         td->td_pcb->pcb_flags = 0;
532
533         td->td_retval[0] = (register_t)entry;
534         td->td_retval[1] = 0;
535 }
536
537 int
538 cpu_set_user_tls(struct thread *td, void *tls_base)
539 {
540
541         td->td_frame->fixreg[2] = (register_t)tls_base + 0x7008;
542         return (0);
543 }