physmem.c 5.9 KB

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