Home | History | Annotate | Line # | Download | only in booke
booke_pmap.c revision 1.10.6.1
      1  1.10.6.1   mrg /*	$NetBSD: booke_pmap.c,v 1.10.6.1 2012/02/18 07:32:52 mrg Exp $	*/
      2       1.2  matt /*-
      3       1.2  matt  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      4       1.2  matt  * All rights reserved.
      5       1.2  matt  *
      6       1.2  matt  * This code is derived from software contributed to The NetBSD Foundation
      7       1.2  matt  * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
      8       1.2  matt  * Agency and which was developed by Matt Thomas of 3am Software Foundry.
      9       1.2  matt  *
     10       1.2  matt  * This material is based upon work supported by the Defense Advanced Research
     11       1.2  matt  * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
     12       1.2  matt  * Contract No. N66001-09-C-2073.
     13       1.2  matt  * Approved for Public Release, Distribution Unlimited
     14       1.2  matt  *
     15       1.2  matt  * Redistribution and use in source and binary forms, with or without
     16       1.2  matt  * modification, are permitted provided that the following conditions
     17       1.2  matt  * are met:
     18       1.2  matt  * 1. Redistributions of source code must retain the above copyright
     19       1.2  matt  *    notice, this list of conditions and the following disclaimer.
     20       1.2  matt  * 2. Redistributions in binary form must reproduce the above copyright
     21       1.2  matt  *    notice, this list of conditions and the following disclaimer in the
     22       1.2  matt  *    documentation and/or other materials provided with the distribution.
     23       1.2  matt  *
     24       1.2  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25       1.2  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26       1.2  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27       1.2  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28       1.2  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29       1.2  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30       1.2  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31       1.2  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32       1.2  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33       1.2  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34       1.2  matt  * POSSIBILITY OF SUCH DAMAGE.
     35       1.2  matt  */
     36       1.2  matt 
     37       1.4  matt #define __PMAP_PRIVATE
     38       1.3  matt 
     39       1.2  matt #include <sys/cdefs.h>
     40       1.2  matt 
     41  1.10.6.1   mrg __KERNEL_RCSID(0, "$NetBSD: booke_pmap.c,v 1.10.6.1 2012/02/18 07:32:52 mrg Exp $");
     42       1.2  matt 
     43       1.2  matt #include <sys/param.h>
     44       1.2  matt #include <sys/kcore.h>
     45       1.2  matt #include <sys/buf.h>
     46       1.2  matt 
     47       1.6  matt #include <uvm/uvm.h>
     48       1.2  matt 
     49       1.2  matt #include <machine/pmap.h>
     50       1.2  matt 
     51       1.2  matt /*
     52       1.2  matt  * Initialize the kernel pmap.
     53       1.2  matt  */
     54       1.2  matt #ifdef MULTIPROCESSOR
     55       1.2  matt #define	PMAP_SIZE	offsetof(struct pmap, pm_pai[MAXCPUS])
     56       1.2  matt #else
     57       1.2  matt #define	PMAP_SIZE	sizeof(struct pmap)
     58       1.2  matt #endif
     59       1.2  matt 
     60       1.2  matt CTASSERT(sizeof(struct pmap_segtab) == NBPG);
     61       1.2  matt 
     62       1.2  matt void
     63       1.2  matt pmap_procwr(struct proc *p, vaddr_t va, size_t len)
     64       1.2  matt {
     65       1.2  matt 	struct pmap * const pmap = p->p_vmspace->vm_map.pmap;
     66       1.2  matt 	vsize_t off = va & PAGE_SIZE;
     67       1.2  matt 
     68       1.2  matt 	kpreempt_disable();
     69       1.2  matt 	for (const vaddr_t eva = va + len; va < eva; off = 0) {
     70       1.2  matt 		const vaddr_t segeva = min(va + len, va - off + PAGE_SIZE);
     71       1.2  matt 		pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
     72       1.2  matt 		if (ptep == NULL) {
     73       1.2  matt 			va = segeva;
     74       1.2  matt 			continue;
     75       1.2  matt 		}
     76       1.2  matt 		pt_entry_t pt_entry = *ptep;
     77       1.2  matt 		if (!pte_valid_p(pt_entry) || !pte_exec_p(pt_entry)) {
     78       1.2  matt 			va = segeva;
     79       1.2  matt 			continue;
     80       1.2  matt 		}
     81       1.2  matt 		kpreempt_enable();
     82       1.2  matt 		dcache_wb(pte_to_paddr(pt_entry), segeva - va);
     83       1.2  matt 		icache_inv(pte_to_paddr(pt_entry), segeva - va);
     84       1.2  matt 		kpreempt_disable();
     85       1.2  matt 		va = segeva;
     86       1.2  matt 	}
     87       1.2  matt 	kpreempt_enable();
     88       1.2  matt }
     89       1.2  matt 
     90       1.2  matt void
     91       1.4  matt pmap_md_page_syncicache(struct vm_page *pg, __cpuset_t onproc)
     92       1.2  matt {
     93       1.4  matt 	/*
     94       1.4  matt 	 * If onproc is empty, we could do a
     95       1.4  matt 	 * pmap_page_protect(pg, VM_PROT_NONE) and remove all
     96       1.4  matt 	 * mappings of the page and clear its execness.  Then
     97       1.4  matt 	 * the next time page is faulted, it will get icache
     98       1.4  matt 	 * synched.  But this is easier. :)
     99       1.4  matt 	 */
    100       1.2  matt 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
    101       1.2  matt 	dcache_wb_page(pa);
    102       1.2  matt 	icache_inv_page(pa);
    103       1.2  matt }
    104       1.2  matt 
    105       1.2  matt vaddr_t
    106       1.2  matt pmap_md_direct_map_paddr(paddr_t pa)
    107       1.2  matt {
    108       1.2  matt 	return (vaddr_t) pa;
    109       1.2  matt }
    110       1.2  matt 
    111       1.2  matt bool
    112       1.2  matt pmap_md_direct_mapped_vaddr_p(vaddr_t va)
    113       1.2  matt {
    114       1.2  matt 	return va < VM_MIN_KERNEL_ADDRESS || VM_MAX_KERNEL_ADDRESS <= va;
    115       1.2  matt }
    116       1.2  matt 
    117       1.2  matt paddr_t
    118       1.2  matt pmap_md_direct_mapped_vaddr_to_paddr(vaddr_t va)
    119       1.2  matt {
    120       1.2  matt 	return (paddr_t) va;
    121       1.2  matt }
    122       1.2  matt 
    123       1.2  matt /*
    124       1.2  matt  *	Bootstrap the system enough to run with virtual memory.
    125       1.2  matt  *	firstaddr is the first unused kseg0 address (not page aligned).
    126       1.2  matt  */
    127       1.2  matt void
    128       1.2  matt pmap_bootstrap(vaddr_t startkernel, vaddr_t endkernel,
    129       1.2  matt 	const phys_ram_seg_t *avail, size_t cnt)
    130       1.2  matt {
    131       1.2  matt 	for (size_t i = 0; i < cnt; i++) {
    132       1.2  matt 		printf(" uvm_page_physload(%#lx,%#lx,%#lx,%#lx,%d)",
    133       1.2  matt 		    atop(avail[i].start),
    134       1.2  matt 		    atop(avail[i].start + avail[i].size) - 1,
    135       1.2  matt 		    atop(avail[i].start),
    136       1.2  matt 		    atop(avail[i].start + avail[i].size) - 1,
    137       1.2  matt 		    VM_FREELIST_DEFAULT);
    138       1.2  matt 		uvm_page_physload(
    139       1.2  matt 		    atop(avail[i].start),
    140       1.2  matt 		    atop(avail[i].start + avail[i].size) - 1,
    141       1.2  matt 		    atop(avail[i].start),
    142       1.2  matt 		    atop(avail[i].start + avail[i].size) - 1,
    143       1.2  matt 		    VM_FREELIST_DEFAULT);
    144       1.2  matt 	}
    145       1.2  matt 
    146       1.2  matt 	pmap_tlb_info_init(&pmap_tlb0_info);		/* init the lock */
    147       1.2  matt 
    148       1.2  matt 	/*
    149  1.10.6.1   mrg 	 * Compute the number of pages kmem_arena will have.
    150       1.2  matt 	 */
    151       1.2  matt 	kmeminit_nkmempages();
    152       1.2  matt 
    153       1.2  matt 	/*
    154       1.2  matt 	 * Figure out how many PTE's are necessary to map the kernel.
    155       1.2  matt 	 * We also reserve space for kmem_alloc_pageable() for vm_fork().
    156       1.2  matt 	 */
    157       1.2  matt 
    158       1.2  matt 	/* Get size of buffer cache and set an upper limit */
    159       1.2  matt 	buf_setvalimit((VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / 8);
    160       1.2  matt 	vsize_t bufsz = buf_memcalc();
    161       1.2  matt 	buf_setvalimit(bufsz);
    162       1.2  matt 
    163       1.2  matt 	vsize_t nsegtabs = pmap_round_seg(VM_PHYS_SIZE
    164       1.2  matt 	    + (ubc_nwins << ubc_winshift)
    165       1.2  matt 	    + bufsz
    166       1.2  matt 	    + 16 * NCARGS
    167       1.2  matt 	    + pager_map_size
    168       1.2  matt 	    + maxproc * USPACE
    169       1.2  matt #ifdef SYSVSHM
    170       1.2  matt 	    + NBPG * shminfo.shmall
    171       1.2  matt #endif
    172       1.2  matt 	    + NBPG * nkmempages);
    173       1.2  matt 
    174       1.2  matt 	/*
    175       1.2  matt 	 * Initialize `FYI' variables.	Note we're relying on
    176       1.2  matt 	 * the fact that BSEARCH sorts the vm_physmem[] array
    177       1.2  matt 	 * for us.  Must do this before uvm_pageboot_alloc()
    178       1.2  matt 	 * can be called.
    179       1.2  matt 	 */
    180       1.2  matt 	pmap_limits.avail_start = vm_physmem[0].start << PGSHIFT;
    181       1.2  matt 	pmap_limits.avail_end = vm_physmem[vm_nphysseg - 1].end << PGSHIFT;
    182       1.2  matt 	const vsize_t max_nsegtabs =
    183       1.2  matt 	    (pmap_round_seg(VM_MAX_KERNEL_ADDRESS)
    184       1.2  matt 		- pmap_trunc_seg(VM_MIN_KERNEL_ADDRESS)) / NBSEG;
    185       1.2  matt 	if (nsegtabs >= max_nsegtabs) {
    186       1.2  matt 		pmap_limits.virtual_end = VM_MAX_KERNEL_ADDRESS;
    187       1.2  matt 		nsegtabs = max_nsegtabs;
    188       1.2  matt 	} else {
    189       1.2  matt 		pmap_limits.virtual_end = VM_MIN_KERNEL_ADDRESS
    190       1.2  matt 		    + nsegtabs * NBSEG;
    191       1.2  matt 	}
    192       1.2  matt 
    193       1.2  matt 	pmap_pvlist_lock_init(curcpu()->ci_ci.dcache_line_size);
    194       1.2  matt 
    195       1.2  matt 	/*
    196       1.2  matt 	 * Now actually allocate the kernel PTE array (must be done
    197       1.2  matt 	 * after virtual_end is initialized).
    198       1.2  matt 	 */
    199       1.2  matt 	vaddr_t segtabs =
    200       1.2  matt 	    uvm_pageboot_alloc(NBPG * nsegtabs + sizeof(struct pmap_segtab));
    201       1.2  matt 
    202       1.2  matt 	/*
    203       1.2  matt 	 * Initialize the kernel's two-level page level.  This only wastes
    204       1.2  matt 	 * an extra page for the segment table and allows the user/kernel
    205       1.2  matt 	 * access to be common.
    206       1.2  matt 	 */
    207       1.2  matt 	struct pmap_segtab * const stp = (void *)segtabs;
    208       1.2  matt 	segtabs += round_page(sizeof(struct pmap_segtab));
    209       1.2  matt 	pt_entry_t **ptp = &stp->seg_tab[VM_MIN_KERNEL_ADDRESS >> SEGSHIFT];
    210       1.2  matt 	for (u_int i = 0; i < nsegtabs; i++, segtabs += NBPG) {
    211       1.2  matt 		*ptp++ = (void *)segtabs;
    212       1.2  matt 	}
    213       1.2  matt 	pmap_kernel()->pm_segtab = stp;
    214       1.2  matt 	curcpu()->ci_pmap_kern_segtab = stp;
    215       1.2  matt 	printf(" kern_segtab=%p", stp);
    216       1.2  matt 
    217       1.2  matt #if 0
    218       1.2  matt 	nsegtabs = (physmem + NPTEPG - 1) / NPTEPG;
    219       1.2  matt 	segtabs = uvm_pageboot_alloc(NBPG * nsegtabs);
    220       1.2  matt 	ptp = stp->seg_tab;
    221       1.2  matt 	pt_entry_t pt_entry = PTE_M|PTE_xX|PTE_xR;
    222       1.2  matt 	pt_entry_t *ptep = (void *)segtabs;
    223       1.2  matt 	printf("%s: allocated %lu page table pages for mapping %u pages\n",
    224       1.2  matt 	    __func__, nsegtabs, physmem);
    225       1.2  matt 	for (u_int i = 0; i < nsegtabs; i++, segtabs += NBPG, ptp++) {
    226       1.2  matt 		*ptp = ptep;
    227       1.2  matt 		for (u_int j = 0; j < NPTEPG; j++, ptep++) {
    228       1.2  matt 			*ptep = pt_entry;
    229       1.2  matt 			pt_entry += NBPG;
    230       1.2  matt 		}
    231       1.2  matt 		printf(" [%u]=%p (%#x)", i, *ptp, **ptp);
    232       1.2  matt 		pt_entry |= PTE_xW;
    233       1.2  matt 		pt_entry &= ~PTE_xX;
    234       1.2  matt 	}
    235       1.2  matt 
    236       1.2  matt 	/*
    237       1.2  matt 	 * Now make everything before the kernel inaccessible.
    238       1.2  matt 	 */
    239       1.2  matt 	for (u_int i = 0; i < startkernel / NBPG; i += NBPG) {
    240       1.2  matt 		stp->seg_tab[i >> SEGSHIFT][(i & SEGOFSET) >> PAGE_SHIFT] = 0;
    241       1.2  matt 	}
    242       1.2  matt #endif
    243       1.2  matt 
    244       1.2  matt 	/*
    245       1.2  matt 	 * Initialize the pools.
    246       1.2  matt 	 */
    247       1.2  matt 	pool_init(&pmap_pmap_pool, PMAP_SIZE, 0, 0, 0, "pmappl",
    248       1.2  matt 	    &pool_allocator_nointr, IPL_NONE);
    249       1.2  matt 	pool_init(&pmap_pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pvpl",
    250       1.2  matt 	    &pmap_pv_page_allocator, IPL_NONE);
    251       1.2  matt 
    252       1.2  matt 	tlb_set_asid(0);
    253       1.2  matt }
    254       1.2  matt 
    255       1.2  matt struct vm_page *
    256       1.2  matt pmap_md_alloc_poolpage(int flags)
    257       1.2  matt {
    258       1.2  matt 	/*
    259       1.2  matt 	 * Any managed page works for us.
    260       1.2  matt 	 */
    261       1.2  matt 	return uvm_pagealloc(NULL, 0, NULL, flags);
    262       1.2  matt }
    263       1.2  matt 
    264       1.2  matt void
    265       1.2  matt pmap_zero_page(paddr_t pa)
    266       1.2  matt {
    267       1.2  matt 	dcache_zero_page(pa);
    268       1.5  matt 
    269       1.6  matt 	KASSERT(!VM_PAGEMD_EXECPAGE_P(VM_PAGE_TO_MD(PHYS_TO_VM_PAGE(pa))));
    270       1.2  matt }
    271       1.2  matt 
    272       1.2  matt void
    273       1.2  matt pmap_copy_page(paddr_t src, paddr_t dst)
    274       1.2  matt {
    275       1.2  matt 	const size_t line_size = curcpu()->ci_ci.dcache_line_size;
    276       1.2  matt 	const paddr_t end = src + PAGE_SIZE;
    277       1.2  matt 
    278       1.2  matt 	while (src < end) {
    279       1.2  matt 		__asm(
    280       1.2  matt 			"dcbt	%2,%1"	"\n\t"	/* touch next src cachline */
    281       1.2  matt 			"dcba	0,%1"	"\n\t" 	/* don't fetch dst cacheline */
    282       1.2  matt 		    :: "b"(src), "b"(dst), "b"(line_size));
    283       1.2  matt 		for (u_int i = 0;
    284       1.2  matt 		     i < line_size;
    285       1.2  matt 		     src += 32, dst += 32, i += 32) {
    286       1.2  matt 			__asm(
    287       1.2  matt 				"lmw	24,0(%0)" "\n\t"
    288       1.2  matt 				"stmw	24,0(%1)"
    289       1.2  matt 			    :: "b"(src), "b"(dst)
    290       1.2  matt 			    : "r24", "r25", "r26", "r27",
    291       1.2  matt 			      "r28", "r29", "r30", "r31");
    292       1.2  matt 		}
    293       1.2  matt 	}
    294       1.5  matt 
    295       1.6  matt 	KASSERT(!VM_PAGEMD_EXECPAGE_P(VM_PAGE_TO_MD(PHYS_TO_VM_PAGE(dst - PAGE_SIZE))));
    296       1.2  matt }
    297       1.2  matt 
    298       1.2  matt void
    299       1.2  matt pmap_md_init(void)
    300       1.2  matt {
    301       1.2  matt 
    302       1.2  matt 	/* nothing for now */
    303       1.2  matt }
    304       1.2  matt 
    305       1.2  matt bool
    306       1.2  matt pmap_md_io_vaddr_p(vaddr_t va)
    307       1.2  matt {
    308       1.2  matt 	return va >= pmap_limits.avail_end
    309       1.2  matt 	    && !(VM_MIN_KERNEL_ADDRESS <= va && va < VM_MAX_KERNEL_ADDRESS);
    310       1.2  matt }
    311       1.2  matt 
    312       1.7  matt bool
    313       1.7  matt pmap_md_tlb_check_entry(void *ctx, vaddr_t va, tlb_asid_t asid, pt_entry_t pte)
    314       1.7  matt {
    315       1.7  matt 	pmap_t pm = ctx;
    316       1.7  matt         struct pmap_asid_info * const pai = PMAP_PAI(pm, curcpu()->ci_tlb_info);
    317       1.7  matt 
    318       1.7  matt 	if (asid != pai->pai_asid)
    319       1.7  matt 		return true;
    320       1.7  matt 
    321       1.7  matt 	const pt_entry_t * const ptep = pmap_pte_lookup(pm, va);
    322       1.7  matt 	KASSERT(ptep != NULL);
    323       1.7  matt 	pt_entry_t xpte = *ptep;
    324       1.7  matt 	xpte &= ~((xpte & (PTE_UNSYNCED|PTE_UNMODIFIED)) << 1);
    325       1.7  matt 	xpte ^= xpte & (PTE_UNSYNCED|PTE_UNMODIFIED|PTE_WIRED);
    326       1.7  matt 
    327       1.7  matt 	KASSERTMSG(pte == xpte,
    328      1.10   jym 	    "pm=%p va=%#"PRIxVADDR" asid=%u: TLB pte (%#x) != real pte (%#x/%#x)",
    329      1.10   jym 	    pm, va, asid, pte, xpte, *ptep);
    330       1.7  matt 
    331       1.7  matt 	return true;
    332       1.7  matt }
    333       1.8  matt 
    334       1.8  matt #ifdef MULTIPROCESSOR
    335       1.8  matt void
    336       1.8  matt pmap_md_tlb_info_attach(struct pmap_tlb_info *ti, struct cpu_info *ci)
    337       1.8  matt {
    338       1.8  matt 	/* nothing */
    339       1.8  matt }
    340       1.8  matt #endif /* MULTIPROCESSOR */
    341