162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci#include <linux/module.h> 762306a36Sopenharmony_ci#include <linux/memblock.h> 862306a36Sopenharmony_ci#include <linux/mm.h> 962306a36Sopenharmony_ci#include <linux/pfn.h> 1062306a36Sopenharmony_ci#include <asm/page.h> 1162306a36Sopenharmony_ci#include <asm/sections.h> 1262306a36Sopenharmony_ci#include <as-layout.h> 1362306a36Sopenharmony_ci#include <init.h> 1462306a36Sopenharmony_ci#include <kern.h> 1562306a36Sopenharmony_ci#include <mem_user.h> 1662306a36Sopenharmony_ci#include <os.h> 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_cistatic int physmem_fd = -1; 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci/* Changed during early boot */ 2162306a36Sopenharmony_ciunsigned long high_physmem; 2262306a36Sopenharmony_ciEXPORT_SYMBOL(high_physmem); 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ciextern unsigned long long physmem_size; 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_civoid __init mem_total_pages(unsigned long physmem, unsigned long iomem, 2762306a36Sopenharmony_ci unsigned long highmem) 2862306a36Sopenharmony_ci{ 2962306a36Sopenharmony_ci unsigned long phys_pages, highmem_pages; 3062306a36Sopenharmony_ci unsigned long iomem_pages, total_pages; 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci phys_pages = physmem >> PAGE_SHIFT; 3362306a36Sopenharmony_ci iomem_pages = iomem >> PAGE_SHIFT; 3462306a36Sopenharmony_ci highmem_pages = highmem >> PAGE_SHIFT; 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci total_pages = phys_pages + iomem_pages + highmem_pages; 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci max_mapnr = total_pages; 3962306a36Sopenharmony_ci} 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_civoid map_memory(unsigned long virt, unsigned long phys, unsigned long len, 4262306a36Sopenharmony_ci int r, int w, int x) 4362306a36Sopenharmony_ci{ 4462306a36Sopenharmony_ci __u64 offset; 4562306a36Sopenharmony_ci int fd, err; 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci fd = phys_mapping(phys, &offset); 4862306a36Sopenharmony_ci err = os_map_memory((void *) virt, fd, offset, len, r, w, x); 4962306a36Sopenharmony_ci if (err) { 5062306a36Sopenharmony_ci if (err == -ENOMEM) 5162306a36Sopenharmony_ci printk(KERN_ERR "try increasing the host's " 5262306a36Sopenharmony_ci "/proc/sys/vm/max_map_count to <physical " 5362306a36Sopenharmony_ci "memory size>/4096\n"); 5462306a36Sopenharmony_ci panic("map_memory(0x%lx, %d, 0x%llx, %ld, %d, %d, %d) failed, " 5562306a36Sopenharmony_ci "err = %d\n", virt, fd, offset, len, r, w, x, err); 5662306a36Sopenharmony_ci } 5762306a36Sopenharmony_ci} 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci/** 6062306a36Sopenharmony_ci * setup_physmem() - Setup physical memory for UML 6162306a36Sopenharmony_ci * @start: Start address of the physical kernel memory, 6262306a36Sopenharmony_ci * i.e start address of the executable image. 6362306a36Sopenharmony_ci * @reserve_end: end address of the physical kernel memory. 6462306a36Sopenharmony_ci * @len: Length of total physical memory that should be mapped/made 6562306a36Sopenharmony_ci * available, in bytes. 6662306a36Sopenharmony_ci * @highmem: Number of highmem bytes that should be mapped/made available. 6762306a36Sopenharmony_ci * 6862306a36Sopenharmony_ci * Creates an unlinked temporary file of size (len + highmem) and memory maps 6962306a36Sopenharmony_ci * it on the last executable image address (uml_reserved). 7062306a36Sopenharmony_ci * 7162306a36Sopenharmony_ci * The offset is needed as the length of the total physical memory 7262306a36Sopenharmony_ci * (len + highmem) includes the size of the memory used be the executable image, 7362306a36Sopenharmony_ci * but the mapped-to address is the last address of the executable image 7462306a36Sopenharmony_ci * (uml_reserved == end address of executable image). 7562306a36Sopenharmony_ci * 7662306a36Sopenharmony_ci * The memory mapped memory of the temporary file is used as backing memory 7762306a36Sopenharmony_ci * of all user space processes/kernel tasks. 7862306a36Sopenharmony_ci */ 7962306a36Sopenharmony_civoid __init setup_physmem(unsigned long start, unsigned long reserve_end, 8062306a36Sopenharmony_ci unsigned long len, unsigned long long highmem) 8162306a36Sopenharmony_ci{ 8262306a36Sopenharmony_ci unsigned long reserve = reserve_end - start; 8362306a36Sopenharmony_ci long map_size = len - reserve; 8462306a36Sopenharmony_ci int err; 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci if(map_size <= 0) { 8762306a36Sopenharmony_ci os_warn("Too few physical memory! Needed=%lu, given=%lu\n", 8862306a36Sopenharmony_ci reserve, len); 8962306a36Sopenharmony_ci exit(1); 9062306a36Sopenharmony_ci } 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci physmem_fd = create_mem_file(len + highmem); 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci err = os_map_memory((void *) reserve_end, physmem_fd, reserve, 9562306a36Sopenharmony_ci map_size, 1, 1, 1); 9662306a36Sopenharmony_ci if (err < 0) { 9762306a36Sopenharmony_ci os_warn("setup_physmem - mapping %ld bytes of memory at 0x%p " 9862306a36Sopenharmony_ci "failed - errno = %d\n", map_size, 9962306a36Sopenharmony_ci (void *) reserve_end, err); 10062306a36Sopenharmony_ci exit(1); 10162306a36Sopenharmony_ci } 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci /* 10462306a36Sopenharmony_ci * Special kludge - This page will be mapped in to userspace processes 10562306a36Sopenharmony_ci * from physmem_fd, so it needs to be written out there. 10662306a36Sopenharmony_ci */ 10762306a36Sopenharmony_ci os_seek_file(physmem_fd, __pa(__syscall_stub_start)); 10862306a36Sopenharmony_ci os_write_file(physmem_fd, __syscall_stub_start, PAGE_SIZE); 10962306a36Sopenharmony_ci os_fsync_file(physmem_fd); 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci memblock_add(__pa(start), len + highmem); 11262306a36Sopenharmony_ci memblock_reserve(__pa(start), reserve); 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci min_low_pfn = PFN_UP(__pa(reserve_end)); 11562306a36Sopenharmony_ci max_low_pfn = min_low_pfn + (map_size >> PAGE_SHIFT); 11662306a36Sopenharmony_ci} 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ciint phys_mapping(unsigned long phys, unsigned long long *offset_out) 11962306a36Sopenharmony_ci{ 12062306a36Sopenharmony_ci int fd = -1; 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci if (phys < physmem_size) { 12362306a36Sopenharmony_ci fd = physmem_fd; 12462306a36Sopenharmony_ci *offset_out = phys; 12562306a36Sopenharmony_ci } 12662306a36Sopenharmony_ci else if (phys < __pa(end_iomem)) { 12762306a36Sopenharmony_ci struct iomem_region *region = iomem_regions; 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci while (region != NULL) { 13062306a36Sopenharmony_ci if ((phys >= region->phys) && 13162306a36Sopenharmony_ci (phys < region->phys + region->size)) { 13262306a36Sopenharmony_ci fd = region->fd; 13362306a36Sopenharmony_ci *offset_out = phys - region->phys; 13462306a36Sopenharmony_ci break; 13562306a36Sopenharmony_ci } 13662306a36Sopenharmony_ci region = region->next; 13762306a36Sopenharmony_ci } 13862306a36Sopenharmony_ci } 13962306a36Sopenharmony_ci else if (phys < __pa(end_iomem) + highmem) { 14062306a36Sopenharmony_ci fd = physmem_fd; 14162306a36Sopenharmony_ci *offset_out = phys - iomem_size; 14262306a36Sopenharmony_ci } 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci return fd; 14562306a36Sopenharmony_ci} 14662306a36Sopenharmony_ciEXPORT_SYMBOL(phys_mapping); 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_cistatic int __init uml_mem_setup(char *line, int *add) 14962306a36Sopenharmony_ci{ 15062306a36Sopenharmony_ci char *retptr; 15162306a36Sopenharmony_ci physmem_size = memparse(line,&retptr); 15262306a36Sopenharmony_ci return 0; 15362306a36Sopenharmony_ci} 15462306a36Sopenharmony_ci__uml_setup("mem=", uml_mem_setup, 15562306a36Sopenharmony_ci"mem=<Amount of desired ram>\n" 15662306a36Sopenharmony_ci" This controls how much \"physical\" memory the kernel allocates\n" 15762306a36Sopenharmony_ci" for the system. The size is specified as a number followed by\n" 15862306a36Sopenharmony_ci" one of 'k', 'K', 'm', 'M', which have the obvious meanings.\n" 15962306a36Sopenharmony_ci" This is not related to the amount of memory in the host. It can\n" 16062306a36Sopenharmony_ci" be more, and the excess, if it's ever used, will just be swapped out.\n" 16162306a36Sopenharmony_ci" Example: mem=64M\n\n" 16262306a36Sopenharmony_ci); 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ciextern int __init parse_iomem(char *str, int *add); 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci__uml_setup("iomem=", parse_iomem, 16762306a36Sopenharmony_ci"iomem=<name>,<file>\n" 16862306a36Sopenharmony_ci" Configure <file> as an IO memory region named <name>.\n\n" 16962306a36Sopenharmony_ci); 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci/* 17262306a36Sopenharmony_ci * This list is constructed in parse_iomem and addresses filled in 17362306a36Sopenharmony_ci * setup_iomem, both of which run during early boot. Afterwards, it's 17462306a36Sopenharmony_ci * unchanged. 17562306a36Sopenharmony_ci */ 17662306a36Sopenharmony_cistruct iomem_region *iomem_regions; 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci/* Initialized in parse_iomem and unchanged thereafter */ 17962306a36Sopenharmony_ciint iomem_size; 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ciunsigned long find_iomem(char *driver, unsigned long *len_out) 18262306a36Sopenharmony_ci{ 18362306a36Sopenharmony_ci struct iomem_region *region = iomem_regions; 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci while (region != NULL) { 18662306a36Sopenharmony_ci if (!strcmp(region->driver, driver)) { 18762306a36Sopenharmony_ci *len_out = region->size; 18862306a36Sopenharmony_ci return region->virt; 18962306a36Sopenharmony_ci } 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci region = region->next; 19262306a36Sopenharmony_ci } 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci return 0; 19562306a36Sopenharmony_ci} 19662306a36Sopenharmony_ciEXPORT_SYMBOL(find_iomem); 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_cistatic int setup_iomem(void) 19962306a36Sopenharmony_ci{ 20062306a36Sopenharmony_ci struct iomem_region *region = iomem_regions; 20162306a36Sopenharmony_ci unsigned long iomem_start = high_physmem + PAGE_SIZE; 20262306a36Sopenharmony_ci int err; 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ci while (region != NULL) { 20562306a36Sopenharmony_ci err = os_map_memory((void *) iomem_start, region->fd, 0, 20662306a36Sopenharmony_ci region->size, 1, 1, 0); 20762306a36Sopenharmony_ci if (err) 20862306a36Sopenharmony_ci printk(KERN_ERR "Mapping iomem region for driver '%s' " 20962306a36Sopenharmony_ci "failed, errno = %d\n", region->driver, -err); 21062306a36Sopenharmony_ci else { 21162306a36Sopenharmony_ci region->virt = iomem_start; 21262306a36Sopenharmony_ci region->phys = __pa(region->virt); 21362306a36Sopenharmony_ci } 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ci iomem_start += region->size + PAGE_SIZE; 21662306a36Sopenharmony_ci region = region->next; 21762306a36Sopenharmony_ci } 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci return 0; 22062306a36Sopenharmony_ci} 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci__initcall(setup_iomem); 223