FreeBSD/sys/i386/i386/pmap.c
/*
 * Tries to promote the 512 or 1024, contiguous 4KB page mappings that are
 * within a single page table page (PTP) to a single 2- or 4MB page mapping.
 * For promotion to occur, two conditions must be met: (1) the 4KB page
 * mappings must map aligned, contiguous physical memory and (2) the 4KB page
 * mappings must have identical characteristics.
 *
 * Managed (PG_MANAGED) mappings within the kernel address space are not
 * promoted.  The reason is that kernel PDEs are replicated in each pmap but
 * pmap_clear_ptes() and pmap_ts_referenced() only read the PDE from the kernel
 * pmap.
 */
Tries to promote the 512 or 1024, contiguous 4KB page mappings that are
within a single page table page (PTP) to a single 2- or 4MB page mapping.
For promotion to occur, two conditions must be met: (1) the 4KB page
mappings must map aligned, contiguous physical memory and (2) the 4KB page
mappings must have identical characteristics.

Managed (PG_MANAGED) mappings within the kernel address space are not
promoted.  The reason is that kernel PDEs are replicated in each pmap but
pmap_clear_ptes() and pmap_ts_referenced() only read the PDE from the kernel
pmap.
Tries to promote the 512 or 1024 contiguous 4KB page mappings that are within a single page table page ( PTP ) to a single 2 - or 4MB page mapping . For promotion to occur two conditions must be met : ( 1 ) the 4KB page mappings must map aligned contiguous physical memory and ( 2 ) the 4KB page mappings must have identical characteristics . Managed ( PG_MANAGED ) mappings within the kernel address space are not promoted . The reason is that kernel PDEs are replicated in each pmap but pmap_clear_ptes () and pmap_ts_referenced () only read the PDE from the kernel pmap .
static void
pmap_promote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
{
	pd_entry_t newpde;
	pt_entry_t *firstpte, oldpte, pa, *pte;
	vm_offset_t oldpteva;
	vm_page_t mpte;
	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
	firstpte = pmap_pte_quick(pmap, trunc_4mpage(va));
setpde:
	newpde = *firstpte;
	if ((newpde & ((PG_FRAME & PDRMASK) | PG_A | PG_V)) != (PG_A | PG_V)) {
		pmap_pde_p_failures++;
		CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
		    " in pmap %p", va, pmap);
		return;
	}
	if ((*firstpte & PG_MANAGED) != 0 && pmap == kernel_pmap) {
		pmap_pde_p_failures++;
		CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
		    " in pmap %p", va, pmap);
		return;
	}
	if ((newpde & (PG_M | PG_RW)) == PG_RW) {
		if (!atomic_cmpset_int((u_int *)firstpte, newpde, newpde &
		    ~PG_RW))  
			goto setpde;
		newpde &= ~PG_RW;
	}
	pa = (newpde & (PG_PS_FRAME | PG_A | PG_V)) + NBPDR - PAGE_SIZE;
	for (pte = firstpte + NPTEPG - 1; pte > firstpte; pte--) {
setpte:
		oldpte = *pte;
		if ((oldpte & (PG_FRAME | PG_A | PG_V)) != pa) {
			pmap_pde_p_failures++;
			CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
			    " in pmap %p", va, pmap);
			return;
		}
		if ((oldpte & (PG_M | PG_RW)) == PG_RW) {
			if (!atomic_cmpset_int((u_int *)pte, oldpte,
			    oldpte & ~PG_RW))
				goto setpte;
			oldpte &= ~PG_RW;
			oldpteva = (oldpte & PG_FRAME & PDRMASK) |
			    (va & ~PDRMASK);
			CTR2(KTR_PMAP, "pmap_promote_pde: protect for va %#x"
			    " in pmap %p", oldpteva, pmap);
		}
		if ((oldpte & PG_PTE_PROMOTE) != (newpde & PG_PTE_PROMOTE)) {
			pmap_pde_p_failures++;
			CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
			    " in pmap %p", va, pmap);
			return;
		}
		pa -= PAGE_SIZE;
	}
	mpte = PHYS_TO_VM_PAGE(*pde & PG_FRAME);
	KASSERT(mpte >= vm_page_array &&
	    mpte < &vm_page_array[vm_page_array_size],
	    ("pmap_promote_pde: page table page is out of range"));
	KASSERT(mpte->pindex == va >> PDRSHIFT,
	    ("pmap_promote_pde: page table page's pindex is wrong"));
	if (pmap_insert_pt_page(pmap, mpte)) {
		pmap_pde_p_failures++;
		CTR2(KTR_PMAP,
		    "pmap_promote_pde: failure for va %#x in pmap %p", va,
		    pmap);
		return;
	}
	if ((newpde & PG_MANAGED) != 0)
		pmap_pv_promote_pde(pmap, va, newpde & PG_PS_FRAME);
	if ((newpde & PG_PTE_PAT) != 0)
		newpde ^= PG_PDE_PAT | PG_PTE_PAT;
	if (workaround_erratum383)
		pmap_update_pde(pmap, va, pde, PG_PS | newpde);
	else if (pmap == kernel_pmap)
		pmap_kenter_pde(va, PG_PS | newpde);
	else
		pde_store(pde, PG_PS | newpde);
	pmap_pde_promotions++;
	CTR2(KTR_PMAP, "pmap_promote_pde: success for va %#x"
	    " in pmap %p", va, pmap);
}
static void pmap_promote_pde ( pmap_t pmap , pd_entry_t * pde , vm_offset_t va ) { pd_entry_t newpde ; pt_entry_t * firstpte , oldpte , pa , * pte ; vm_offset_t oldpteva ; vm_page_t mpte ; PMAP_LOCK_ASSERT ( pmap , MA_OWNED ) ; firstpte = pmap_pte_quick ( pmap , trunc_4mpage ( va ) ) ; setpde : newpde = * firstpte ; if ( ( newpde & ( ( PG_FRAME & PDRMASK ) | PG_A | PG_V ) ) != ( PG_A | PG_V ) ) { pmap_pde_p_failures ++ ; CTR2 ( KTR_PMAP , " " " " , va , pmap ) ; return ; } if ( ( * firstpte & PG_MANAGED ) != 0 && pmap == kernel_pmap ) { pmap_pde_p_failures ++ ; CTR2 ( KTR_PMAP , " " " " , va , pmap ) ; return ; } if ( ( newpde & ( PG_M | PG_RW ) ) == PG_RW ) { if ( ! atomic_cmpset_int ( ( u_int * ) firstpte , newpde , newpde & ~ PG_RW ) ) goto setpde ; newpde &= ~ PG_RW ; } pa = ( newpde & ( PG_PS_FRAME | PG_A | PG_V ) ) + NBPDR - PAGE_SIZE ; for ( pte = firstpte + NPTEPG - 1 ; pte > firstpte ; pte -- ) { setpte : oldpte = * pte ; if ( ( oldpte & ( PG_FRAME | PG_A | PG_V ) ) != pa ) { pmap_pde_p_failures ++ ; CTR2 ( KTR_PMAP , " " " " , va , pmap ) ; return ; } if ( ( oldpte & ( PG_M | PG_RW ) ) == PG_RW ) { if ( ! atomic_cmpset_int ( ( u_int * ) pte , oldpte , oldpte & ~ PG_RW ) ) goto setpte ; oldpte &= ~ PG_RW ; oldpteva = ( oldpte & PG_FRAME & PDRMASK ) | ( va & ~ PDRMASK ) ; CTR2 ( KTR_PMAP , " " " " , oldpteva , pmap ) ; } if ( ( oldpte & PG_PTE_PROMOTE ) != ( newpde & PG_PTE_PROMOTE ) ) { pmap_pde_p_failures ++ ; CTR2 ( KTR_PMAP , " " " " , va , pmap ) ; return ; } pa -= PAGE_SIZE ; } mpte = PHYS_TO_VM_PAGE ( * pde & PG_FRAME ) ; KASSERT ( mpte >= vm_page_array && mpte < & vm_page_array [ vm_page_array_size ] , ( " " ) ) ; KASSERT ( mpte -> pindex == va >> PDRSHIFT , ( " " ) ) ; if ( pmap_insert_pt_page ( pmap , mpte ) ) { pmap_pde_p_failures ++ ; CTR2 ( KTR_PMAP , " " , va , pmap ) ; return ; } if ( ( newpde & PG_MANAGED ) != 0 ) pmap_pv_promote_pde ( pmap , va , newpde & PG_PS_FRAME ) ; if ( ( newpde & PG_PTE_PAT ) != 0 ) newpde ^= PG_PDE_PAT | PG_PTE_PAT ; if ( workaround_erratum383 ) pmap_update_pde ( pmap , va , pde , PG_PS | newpde ) ; else if ( pmap == kernel_pmap ) pmap_kenter_pde ( va , PG_PS | newpde ) ; else pde_store ( pde , PG_PS | newpde ) ; pmap_pde_promotions ++ ; CTR2 ( KTR_PMAP , " " " " , va , pmap ) ; }
Tries to promote the 512 or 1024, contiguous 4KB page mappings that are
 within a single page table page (PTP) to a single 2- or 4MB page mapping.
Tries to promote the 512 or 1024 contiguous 4KB page mappings that are within a single page table page ( PTP ) to a single 2 - or 4MB page mapping .
/*
	 * Examine the first PTE in the specified PTP.  Abort if this PTE is
	 * either invalid, unused, or does not map the first 4KB physical page
	 * within a 2- or 4MB page.
	 */ /*
		 * When PG_M is already clear, PG_RW can be cleared without
		 * a TLB invalidation.
		 */ /* 
	 * Examine each of the other PTEs in the specified PTP.  Abort if this
	 * PTE maps an unexpected 4KB physical page or does not have identical
	 * characteristics to the first PTE.
	 */ /*
			 * When PG_M is already clear, PG_RW can be cleared
			 * without a TLB invalidation.
			 */ /*
	 * Save the page table page in its current state until the PDE
	 * mapping the superpage is demoted by pmap_demote_pde() or
	 * destroyed by pmap_remove_pde(). 
	 */ /*
	 * Promote the pv entries.
	 */ /*
	 * Propagate the PAT index to its proper position.
	 */ /*
	 * Map the superpage.
	 */