1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 4 */ 5 6 #include <linux/module.h> 7 #include <linux/memblock.h> 8 #include <linux/mm.h> 9 #include <linux/pfn.h> 10 #include <asm/page.h> 11 #include <asm/sections.h> 12 #include <as-layout.h> 13 #include <init.h> 14 #include <kern.h> 15 #include <kern_util.h> 16 #include <mem_user.h> 17 #include <os.h> 18 19 static int physmem_fd = -1; 20 21 /* Changed during early boot */ 22 unsigned long high_physmem; 23 EXPORT_SYMBOL(high_physmem); 24 25 extern unsigned long long physmem_size; 26 27 void __init mem_total_pages(unsigned long physmem, unsigned long iomem, 28 unsigned long highmem) 29 { 30 unsigned long phys_pages, highmem_pages; 31 unsigned long iomem_pages, total_pages; 32 33 phys_pages = physmem >> PAGE_SHIFT; 34 iomem_pages = iomem >> PAGE_SHIFT; 35 highmem_pages = highmem >> PAGE_SHIFT; 36 37 total_pages = phys_pages + iomem_pages + highmem_pages; 38 39 max_mapnr = total_pages; 40 } 41 42 void map_memory(unsigned long virt, unsigned long phys, unsigned long len, 43 int r, int w, int x) 44 { 45 __u64 offset; 46 int fd, err; 47 48 fd = phys_mapping(phys, &offset); 49 err = os_map_memory((void *) virt, fd, offset, len, r, w, x); 50 if (err) { 51 if (err == -ENOMEM) 52 printk(KERN_ERR "try increasing the host's " 53 "/proc/sys/vm/max_map_count to <physical " 54 "memory size>/4096\n"); 55 panic("map_memory(0x%lx, %d, 0x%llx, %ld, %d, %d, %d) failed, " 56 "err = %d\n", virt, fd, offset, len, r, w, x, err); 57 } 58 } 59 60 /** 61 * setup_physmem() - Setup physical memory for UML 62 * @start: Start address of the physical kernel memory, 63 * i.e start address of the executable image. 64 * @reserve_end: end address of the physical kernel memory. 65 * @len: Length of total physical memory that should be mapped/made 66 * available, in bytes. 67 * @highmem: Number of highmem bytes that should be mapped/made available. 68 * 69 * Creates an unlinked temporary file of size (len + highmem) and memory maps 70 * it on the last executable image address (uml_reserved). 71 * 72 * The offset is needed as the length of the total physical memory 73 * (len + highmem) includes the size of the memory used be the executable image, 74 * but the mapped-to address is the last address of the executable image 75 * (uml_reserved == end address of executable image). 76 * 77 * The memory mapped memory of the temporary file is used as backing memory 78 * of all user space processes/kernel tasks. 79 */ 80 void __init setup_physmem(unsigned long start, unsigned long reserve_end, 81 unsigned long len, unsigned long long highmem) 82 { 83 unsigned long reserve = reserve_end - start; 84 long map_size = len - reserve; 85 int err; 86 87 if(map_size <= 0) { 88 os_warn("Too few physical memory! Needed=%lu, given=%lu\n", 89 reserve, len); 90 exit(1); 91 } 92 93 physmem_fd = create_mem_file(len + highmem); 94 95 err = os_map_memory((void *) reserve_end, physmem_fd, reserve, 96 map_size, 1, 1, 1); 97 if (err < 0) { 98 os_warn("setup_physmem - mapping %ld bytes of memory at 0x%p " 99 "failed - errno = %d\n", map_size, 100 (void *) reserve_end, err); 101 exit(1); 102 } 103 104 /* 105 * Special kludge - This page will be mapped in to userspace processes 106 * from physmem_fd, so it needs to be written out there. 107 */ 108 os_seek_file(physmem_fd, __pa(__syscall_stub_start)); 109 os_write_file(physmem_fd, __syscall_stub_start, PAGE_SIZE); 110 os_fsync_file(physmem_fd); 111 112 memblock_add(__pa(start), len + highmem); 113 memblock_reserve(__pa(start), reserve); 114 115 min_low_pfn = PFN_UP(__pa(reserve_end)); 116 max_low_pfn = min_low_pfn + (map_size >> PAGE_SHIFT); 117 } 118 119 int phys_mapping(unsigned long phys, unsigned long long *offset_out) 120 { 121 int fd = -1; 122 123 if (phys < physmem_size) { 124 fd = physmem_fd; 125 *offset_out = phys; 126 } 127 else if (phys < __pa(end_iomem)) { 128 struct iomem_region *region = iomem_regions; 129 130 while (region != NULL) { 131 if ((phys >= region->phys) && 132 (phys < region->phys + region->size)) { 133 fd = region->fd; 134 *offset_out = phys - region->phys; 135 break; 136 } 137 region = region->next; 138 } 139 } 140 else if (phys < __pa(end_iomem) + highmem) { 141 fd = physmem_fd; 142 *offset_out = phys - iomem_size; 143 } 144 145 return fd; 146 } 147 EXPORT_SYMBOL(phys_mapping); 148 149 static int __init uml_mem_setup(char *line, int *add) 150 { 151 char *retptr; 152 physmem_size = memparse(line,&retptr); 153 return 0; 154 } 155 __uml_setup("mem=", uml_mem_setup, 156 "mem=<Amount of desired ram>\n" 157 " This controls how much \"physical\" memory the kernel allocates\n" 158 " for the system. The size is specified as a number followed by\n" 159 " one of 'k', 'K', 'm', 'M', which have the obvious meanings.\n" 160 " This is not related to the amount of memory in the host. It can\n" 161 " be more, and the excess, if it's ever used, will just be swapped out.\n" 162 " Example: mem=64M\n\n" 163 ); 164 165 __uml_setup("iomem=", parse_iomem, 166 "iomem=<name>,<file>\n" 167 " Configure <file> as an IO memory region named <name>.\n\n" 168 ); 169 170 /* 171 * This list is constructed in parse_iomem and addresses filled in 172 * setup_iomem, both of which run during early boot. Afterwards, it's 173 * unchanged. 174 */ 175 struct iomem_region *iomem_regions; 176 177 /* Initialized in parse_iomem and unchanged thereafter */ 178 int iomem_size; 179 180 unsigned long find_iomem(char *driver, unsigned long *len_out) 181 { 182 struct iomem_region *region = iomem_regions; 183 184 while (region != NULL) { 185 if (!strcmp(region->driver, driver)) { 186 *len_out = region->size; 187 return region->virt; 188 } 189 190 region = region->next; 191 } 192 193 return 0; 194 } 195 EXPORT_SYMBOL(find_iomem); 196 197 static int setup_iomem(void) 198 { 199 struct iomem_region *region = iomem_regions; 200 unsigned long iomem_start = high_physmem + PAGE_SIZE; 201 int err; 202 203 while (region != NULL) { 204 err = os_map_memory((void *) iomem_start, region->fd, 0, 205 region->size, 1, 1, 0); 206 if (err) 207 printk(KERN_ERR "Mapping iomem region for driver '%s' " 208 "failed, errno = %d\n", region->driver, -err); 209 else { 210 region->virt = iomem_start; 211 region->phys = __pa(region->virt); 212 } 213 214 iomem_start += region->size + PAGE_SIZE; 215 region = region->next; 216 } 217 218 return 0; 219 } 220 221 __initcall(setup_iomem); 222
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.