lvaddr_t paging_x86_32_map_special(lpaddr_t base, size_t size, uint64_t bitmap) { // Allocate backwards from a page below end of address space static lvaddr_t vbase = (lvaddr_t)X86_32_VADDR_SPACE_SIZE; lpaddr_t addr; lvaddr_t vaddr; paging_align(&vbase, &base, &size, X86_32_MEM_PAGE_SIZE); // Align physical base address lpaddr_t offset = base & (X86_32_MEM_PAGE_SIZE - 1); base -= offset; if(vbase - size < X86_32_VADDR_SPACE_SIZE - X86_32_DEVICE_SPACE_LIMIT) { return 0; } // Map pages, tables and directories (reverse order) for(vaddr = vbase - X86_32_MEM_PAGE_SIZE, addr = base + size - X86_32_MEM_PAGE_SIZE; vaddr >= vbase - size; vaddr -= X86_32_MEM_PAGE_SIZE, addr -= X86_32_MEM_PAGE_SIZE) { #ifdef CONFIG_PAE union x86_32_pdpte_entry *pdpte_base = &pdpte[X86_32_PDPTE_BASE(vaddr)]; union x86_32_ptable_entry *pdir_base = &mem_pdir[X86_32_PDPTE_BASE(mem_to_local_phys(vaddr))][X86_32_PDIR_BASE(vaddr)]; debug(SUBSYS_PAGING, "Mapping 2M device page: vaddr = 0x%x, addr = 0x%x, " "PDPTE_BASE = %u, PDIR_BASE = %u -- ", vaddr, addr, X86_32_PDPTE_BASE(vaddr), X86_32_PDIR_BASE(vaddr)); mapit(pdpte_base, pdir_base, addr, bitmap); #else # ifdef CONFIG_PSE union x86_32_ptable_entry *pdir_base = &pdir[X86_32_PDIR_BASE(vaddr)]; debug(SUBSYS_PAGING, "Mapping 4M device page: vaddr = 0x%x, addr = 0x%x, " "PDIR_BASE = %u -- ", vaddr, addr, X86_32_PDIR_BASE(vaddr)); mapit(pdir_base, addr, bitmap); # else union x86_32_pdir_entry *pdir_base = &pdir[X86_32_PDIR_BASE(vaddr)]; union x86_32_ptable_entry *ptable_base = &mem_ptable[X86_32_PDIR_BASE(vaddr) - (X86_32_PTABLE_SIZE - MEM_PTABLE_SIZE)][X86_32_PTABLE_BASE(vaddr)]; debug(SUBSYS_PAGING, "Mapping 4K device page: vaddr = 0x%"PRIxLVADDR", " "addr = 0x%"PRIxLPADDR", " "PDIR_BASE = %"PRIxLPADDR", PTABLE_BASE = %"PRIxLPADDR", pdir = %p, ptable = %p -- ", vaddr, addr, X86_32_PDIR_BASE(vaddr), X86_32_PTABLE_BASE(vaddr), pdir, mem_ptable[X86_32_PDIR_BASE(vaddr) - (X86_32_PTABLE_SIZE - MEM_PTABLE_SIZE)]); mapit(pdir_base, ptable_base, addr, bitmap); # endif #endif } vbase -= size; return vbase + offset; }
MEMREGION * visor_memregion_create(HOSTADDRESS physaddr, ulong nbytes) { MEMREGION *rc = NULL; MEMREGION *memregion = kzalloc(sizeof(MEMREGION), GFP_KERNEL | __GFP_NORETRY); if (memregion == NULL) { ERRDRV("visor_memregion_create allocation failed"); return NULL; } memregion->physaddr = physaddr; memregion->nbytes = nbytes; memregion->overlapped = FALSE; if (!mapit(memregion)) { rc = NULL; goto Away; } rc = memregion; Away: if (rc == NULL) { if (memregion != NULL) { visor_memregion_destroy(memregion); memregion = NULL; } } return rc; }
int main(int ac, char **av) { int fd; int size; int random = 0; char *prog = av[0]; if (ac != 3 && ac != 4) { fprintf(stderr, "usage: %s [-r] size file\n", prog); exit(1); } if (strcmp("-r", av[1]) == 0) { random = 1; ac--, av++; } size = bytes(av[1]); if (size < MINSIZE) { return (1); } CHK(fd = open(av[2], O_CREAT|O_RDWR, 0666)); CHK(ftruncate(fd, size)); BENCH(mapit(fd, size, random), 0); micromb(size, get_n()); return(0); }
//***************************************************************************** // void COOP_GiveStoredUVDPickupsToPlayer ( const ULONG ulPlayer ) { const TMap<FName, int>::Pair *pair; TMap<FName, int>::ConstIterator mapit ( UVDpickupMap ); while ( mapit.NextPair (pair) ) { const PClass *pType = PClass::FindClass ( pair->Key.GetChars() ); if ( pType ) DoGiveInv ( players[ulPlayer].mo, pType, 1 ); } }
/** * \brief Map a region of physical memory into physical memory address space. * * Maps the region of physical memory, based at base and sized size bytes * to the same-sized virtual memory region. All pages are flagged according to * bitmap. This function automatically fills the needed page directory entries * in the page hierarchy rooted at pml4. base and size will be made * page-aligned by this function. * * \param base Physical base address of memory region * \param size Size in bytes of memory region * \param bitmap Bitmap of flags for page tables/directories * * \return 0 on success, -1 on error (out of range) */ static int paging_map_mem(lpaddr_t base, size_t size, uint64_t bitmap) { lvaddr_t vaddr, vbase = local_phys_to_mem(base); lpaddr_t addr; // Align given physical base address if (base & X86_64_MEM_PAGE_MASK) { base -= base & X86_64_MEM_PAGE_MASK; } paging_align(&vbase, &base, &size, X86_64_MEM_PAGE_SIZE); // Is mapped region out of range? assert(base + size <= (lpaddr_t)K1OM_PADDR_SPACE_LIMIT); if (base + size > (lpaddr_t) K1OM_PADDR_SPACE_LIMIT) { return -1; } // Map pages, tables and directories for (vaddr = vbase, addr = base; vaddr < vbase + size; vaddr += X86_64_MEM_PAGE_SIZE, addr += X86_64_MEM_PAGE_SIZE) { union x86_64_pdir_entry *pml4_base = &pml4[X86_64_PML4_BASE(vaddr)]; union x86_64_pdir_entry *pdpt_base = &mem_pdpt[X86_64_PML4_BASE(addr)][X86_64_PDPT_BASE(vaddr)]; union x86_64_ptable_entry *pdir_base = &mem_pdir[X86_64_PML4_BASE(addr)][X86_64_PDPT_BASE(addr)][X86_64_PDIR_BASE( vaddr)]; debug(SUBSYS_PAGING, "Mapping 2M page: vaddr = 0x%"PRIxLVADDR"x, addr = 0x%lx, " "PML4_BASE = %lu, PDPT_BASE = %lu, PDIR_BASE = %lu -- ", vaddr, addr, X86_64_PML4_BASE(vaddr), X86_64_PDPT_BASE(vaddr), X86_64_PDIR_BASE(vaddr)); mapit(pml4_base, pdpt_base, pdir_base, addr, bitmap); } // XXX FIXME: get rid of this TBL flush code, or move it elsewhere // uint64_t cr3; // __asm__ __volatile__("mov %%cr3,%0" : "=a" (cr3) : ); // __asm__ __volatile__("mov %0,%%cr3" : : "a" (cr3)); return 0; }
int visor_memregion_resize(MEMREGION *memregion, ulong newsize) { if (newsize == memregion->nbytes) return 0; if (memregion->overlapped) /* no error check here - we no longer know the * parent's range! */ memregion->nbytes = newsize; else { unmapit(memregion); memregion->nbytes = newsize; if (!mapit(memregion)) return -1; } return 0; }
/** * \brief Map a region of physical memory into physical memory address space. * * Maps the region of physical memory, based at base and sized size bytes * to the same-sized virtual memory region. All pages are flagged according to * bitmap. This function automatically fills the needed page directory entries * in the page hierarchy rooted at pml4. base and size will be made * page-aligned by this function. * * \param base Base address of memory region * \param size Size in bytes of memory region * \param bitmap Bitmap of flags for page tables/directories * * \return 0 on success, -1 on error (out of range) */ static int paging_x86_32_map_mem(lpaddr_t base, size_t size, uint64_t bitmap) { lvaddr_t vaddr, vbase = local_phys_to_mem(base); lpaddr_t addr; paging_align(&vbase, &base, &size, X86_32_MEM_PAGE_SIZE); // Is mapped region out of range? assert(local_phys_to_gen_phys(base + size) <= X86_32_PADDR_SPACE_LIMIT); if(local_phys_to_gen_phys(base + size) > X86_32_PADDR_SPACE_LIMIT) { printk(LOG_ERR, "Mapped region [%"PRIxLPADDR",%"PRIxLPADDR"]" "out of physical address range!", base, base + size); return -1; } assert(local_phys_to_gen_phys(vbase + size) <= X86_32_VADDR_SPACE_SIZE); // Map pages, tables and directories for(vaddr = vbase, addr = base;; vaddr += X86_32_MEM_PAGE_SIZE, addr += X86_32_MEM_PAGE_SIZE) { #ifdef CONFIG_PAE union x86_32_pdpte_entry *pdpte_base = &pdpte[X86_32_PDPTE_BASE(vaddr)]; union x86_32_ptable_entry *pdir_base = &mem_pdir[X86_32_PDPTE_BASE(addr)][X86_32_PDIR_BASE(vaddr)]; #else union x86_32_pdir_entry *pdir_base = &pdir[X86_32_PDIR_BASE(vaddr)]; # ifndef CONFIG_PSE union x86_32_ptable_entry *ptable_base = &mem_ptable[X86_32_PDIR_BASE(addr)][X86_32_PTABLE_BASE(vaddr)]; # endif #endif if(vbase + size != 0) { if(vaddr >= vbase + size) { break; } } #ifdef CONFIG_PAE debug(SUBSYS_PAGING, "Mapping 2M page: vaddr = 0x%x, addr = 0x%x, " "PDPTE_BASE = %u, PDIR_BASE = %u -- ", vaddr, addr, X86_32_PDPTE_BASE(vaddr), X86_32_PDIR_BASE(vaddr)); mapit(pdpte_base, pdir_base, addr, bitmap); #else # ifdef CONFIG_PSE debug(SUBSYS_PAGING, "Mapping 4M page: vaddr = 0x%x, addr = 0x%x, " "PDIR_BASE = %u -- ", vaddr, addr, X86_32_PDIR_BASE(vaddr)); mapit(pdir_base, addr, bitmap); # else debug(SUBSYS_PAGING, "Mapping 4K page: vaddr = 0x%"PRIxLVADDR", " "addr = 0x%"PRIxLVADDR", " "PDIR_BASE = %"PRIuLPADDR", PTABLE_BASE = %"PRIuLPADDR" -- ", vaddr, addr, X86_32_PDIR_BASE(vaddr), X86_32_PTABLE_BASE(vaddr)); mapit(pdir_base, ptable_base, addr, bitmap); # endif #endif if(vbase + size == 0) { // Bail out if mapped last page of address space to prevent overflow if(vaddr == 0xffe00000) { break; } } } return 0; }
int main(){ unsigned in=2,out=0; // echo(2); mapit(0,&out,&in); printf("%d %d\n", in, out); }
void nemo_main() { stream instr, outstr; int i, n, naxis1, naxis2, naxis[2], moment; double edges[2]; struct fits_header fh; struct my_table_header r; char *record, *cp, mesg[80]; string *hitem; FITS *map; /* Setup */ instr = stropen(getparam("in"),"r"); /* open input */ moment = getiparam("moment"); if (moment < 1 || moment > 2) error("moment must be 1 or 2"); band = getiparam("band"); if (band < 1 || band > 10) { band = band_id(getdparam("band")); if (band < 1) error("Invalid DIRBE band"); } naxis[0] = nlon = getiparam("nlong"); naxis[1] = nlat = getiparam("nlat"); grid = (entryptr *) allocate(nlat*nlon*sizeof(entryptr)); for (i=0; i<nlat*nlon; i++) grid[i] = NULL; cp = getparam("coord"); switch (*cp) { case 'g': gal_coord = TRUE; break; case 'e': gal_coord = FALSE; break; default: error("Bad coordinate system choosen; try gal or ecl"); } if (nemoinpd(getparam("long"),edges,2) != 2) error("long= needs 2 values"); if (edges[0] <= edges[1]) error("long= needs left edge to be largest"); lonmin = edges[0]; lonmax = edges[1]; dlon = (lonmax-lonmin)/(float)nlon; if (nemoinpd(getparam("lat"),edges,2) != 2) error("lat= needs 2 values"); if (edges[0] >= edges[1]) error("lat= needs right edge to be largest"); latmin = edges[0]; latmax = edges[1]; dlat = (latmax-latmin)/(float)nlat; dprintf(1,"GridSize: %d * %d Pixels: %g * %g\n",nlon,nlat,dlon,dlat); gc_middle = (lonmax < 0.0 && lonmin > 0.0); /* see if to use SYM_ANGLE */ ncell = getiparam("ncell"); sigma2 = 2*sqr(getdparam("sigma")); /* Open output FITS file, and write a small yet descriptive enough header */ map = fitopen(getparam("out"),"new",2,naxis); fitwrhda(map,"CTYPE1", gal_coord ? "GLON" : "ELON"); fitwrhdr(map,"CRPIX1",(float) 1.0); /* should use center */ fitwrhdr(map,"CRVAL1",(float) (lonmin + 0.5 * dlon)); fitwrhdr(map,"CDELT1",(float) dlon); fitwrhda(map,"CTYPE2", gal_coord ? "GLAT" : "ELAT"); fitwrhdr(map,"CRPIX2",(float) 1.0); /* should use center */ fitwrhdr(map,"CRVAL2",(float) (latmin + 0.5 * dlat)); fitwrhdr(map,"CDELT2",(float) dlat); fitwrhda(map,"TELESCOP","COBE"); fitwrhda(map,"INSTRUME","DIRBE"); fitwrhda(map,"ORIGIN","NEMO processing on CDAC data"); fitwrhda(map,"BUNIT","MJy/sr"); sprintf(mesg,"NEMO: %s VERSION=%s",getargv0(),getparam("VERSION")); fitwra(map,"HISTORY", mesg); hitem = ask_history(); fitwra(map,"HISTORY","NEMO: History in reversed order"); for (i=0, cp=hitem[0]; cp != NULL; i++) { fitwral(map,"HISTORY",cp); cp = hitem[i+1]; /* point to next history item */ } /* Open input file, and process all rows */ fts_zero(&fh); /* clean out header */ n = fts_rhead(&fh,instr); /* read primary header */ if (n<0) error("Error reading primary HDU"); fts_sdata(&fh,instr); /* and skip data .. */ fts_zero(&fh); /* clean out header */ n = fts_rhead(&fh,instr); /* read primary header */ if (n<0) error("Error reading second HDU"); naxis1 = fh.naxisn[0]; /* size of one row */ naxis2 = fh.naxisn[1]; /* number of rows */ record = allocate(naxis1); for (i=0; i<naxis2; i++) { /* loop reading rows */ n = fread(record,1,naxis1,instr); if (n != naxis1) error("Early EOF on record %d",i+1); stuffit(&fh,record,&r); } printf("Used %d/%d points in gridding\n",nused,naxis2); /* map the data on a grid */ mapit(map,moment); /* finish off */ fitclose(map); strclose(instr); }