physmem.c 6.0 KB

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