[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/powerpc/booke/vm_machdep.c

Version: -  FREEBSD  -  FREEBSD8  -  FREEBSD7  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  OPENSOLARIS  -  minix-3-1-1  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  3  -  10 

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

Cache object: db75a4d5e1b57b22299c6aa04119e968


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.