mem.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/stddef.h>
  6. #include <linux/module.h>
  7. #include <linux/bootmem.h>
  8. #include <linux/highmem.h>
  9. #include <linux/mm.h>
  10. #include <linux/swap.h>
  11. #include <linux/slab.h>
  12. #include <asm/fixmap.h>
  13. #include <asm/page.h>
  14. #include "as-layout.h"
  15. #include "init.h"
  16. #include "kern.h"
  17. #include "kern_util.h"
  18. #include "mem_user.h"
  19. #include "os.h"
  20. /* allocated in paging_init, zeroed in mem_init, and unchanged thereafter */
  21. unsigned long *empty_zero_page = NULL;
  22. EXPORT_SYMBOL(empty_zero_page);
  23. /* allocated in paging_init and unchanged thereafter */
  24. static unsigned long *empty_bad_page = NULL;
  25. /*
  26. * Initialized during boot, and readonly for initializing page tables
  27. * afterwards
  28. */
  29. pgd_t swapper_pg_dir[PTRS_PER_PGD];
  30. /* Initialized at boot time, and readonly after that */
  31. unsigned long long highmem;
  32. int kmalloc_ok = 0;
  33. /* Used during early boot */
  34. static unsigned long brk_end;
  35. #ifdef CONFIG_HIGHMEM
  36. static void setup_highmem(unsigned long highmem_start,
  37. unsigned long highmem_len)
  38. {
  39. struct page *page;
  40. unsigned long highmem_pfn;
  41. int i;
  42. highmem_pfn = __pa(highmem_start) >> PAGE_SHIFT;
  43. for (i = 0; i < highmem_len >> PAGE_SHIFT; i++) {
  44. page = &mem_map[highmem_pfn + i];
  45. ClearPageReserved(page);
  46. init_page_count(page);
  47. __free_page(page);
  48. }
  49. }
  50. #endif
  51. void __init mem_init(void)
  52. {
  53. /* clear the zero-page */
  54. memset(empty_zero_page, 0, PAGE_SIZE);
  55. /* Map in the area just after the brk now that kmalloc is about
  56. * to be turned on.
  57. */
  58. brk_end = (unsigned long) UML_ROUND_UP(sbrk(0));
  59. map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0);
  60. free_bootmem(__pa(brk_end), uml_reserved - brk_end);
  61. uml_reserved = brk_end;
  62. /* this will put all low memory onto the freelists */
  63. totalram_pages = free_all_bootmem();
  64. max_low_pfn = totalram_pages;
  65. #ifdef CONFIG_HIGHMEM
  66. totalhigh_pages = highmem >> PAGE_SHIFT;
  67. totalram_pages += totalhigh_pages;
  68. #endif
  69. num_physpages = totalram_pages;
  70. max_pfn = totalram_pages;
  71. printk(KERN_INFO "Memory: %luk available\n",
  72. nr_free_pages() << (PAGE_SHIFT-10));
  73. kmalloc_ok = 1;
  74. #ifdef CONFIG_HIGHMEM
  75. setup_highmem(end_iomem, highmem);
  76. #endif
  77. }
  78. /*
  79. * Create a page table and place a pointer to it in a middle page
  80. * directory entry.
  81. */
  82. static void __init one_page_table_init(pmd_t *pmd)
  83. {
  84. if (pmd_none(*pmd)) {
  85. pte_t *pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
  86. set_pmd(pmd, __pmd(_KERNPG_TABLE +
  87. (unsigned long) __pa(pte)));
  88. if (pte != pte_offset_kernel(pmd, 0))
  89. BUG();
  90. }
  91. }
  92. static void __init one_md_table_init(pud_t *pud)
  93. {
  94. #ifdef CONFIG_3_LEVEL_PGTABLES
  95. pmd_t *pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
  96. set_pud(pud, __pud(_KERNPG_TABLE + (unsigned long) __pa(pmd_table)));
  97. if (pmd_table != pmd_offset(pud, 0))
  98. BUG();
  99. #endif
  100. }
  101. static void __init fixrange_init(unsigned long start, unsigned long end,
  102. pgd_t *pgd_base)
  103. {
  104. pgd_t *pgd;
  105. pud_t *pud;
  106. pmd_t *pmd;
  107. int i, j;
  108. unsigned long vaddr;
  109. vaddr = start;
  110. i = pgd_index(vaddr);
  111. j = pmd_index(vaddr);
  112. pgd = pgd_base + i;
  113. for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) {
  114. pud = pud_offset(pgd, vaddr);
  115. if (pud_none(*pud))
  116. one_md_table_init(pud);
  117. pmd = pmd_offset(pud, vaddr);
  118. for (; (j < PTRS_PER_PMD) && (vaddr < end); pmd++, j++) {
  119. one_page_table_init(pmd);
  120. vaddr += PMD_SIZE;
  121. }
  122. j = 0;
  123. }
  124. }
  125. #ifdef CONFIG_HIGHMEM
  126. pte_t *kmap_pte;
  127. pgprot_t kmap_prot;
  128. #define kmap_get_fixmap_pte(vaddr) \
  129. pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)),\
  130. (vaddr)), (vaddr))
  131. static void __init kmap_init(void)
  132. {
  133. unsigned long kmap_vstart;
  134. /* cache the first kmap pte */
  135. kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
  136. kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
  137. kmap_prot = PAGE_KERNEL;
  138. }
  139. static void __init init_highmem(void)
  140. {
  141. pgd_t *pgd;
  142. pud_t *pud;
  143. pmd_t *pmd;
  144. pte_t *pte;
  145. unsigned long vaddr;
  146. /*
  147. * Permanent kmaps:
  148. */
  149. vaddr = PKMAP_BASE;
  150. fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, swapper_pg_dir);
  151. pgd = swapper_pg_dir + pgd_index(vaddr);
  152. pud = pud_offset(pgd, vaddr);
  153. pmd = pmd_offset(pud, vaddr);
  154. pte = pte_offset_kernel(pmd, vaddr);
  155. pkmap_page_table = pte;
  156. kmap_init();
  157. }
  158. #endif /* CONFIG_HIGHMEM */
  159. static void __init fixaddr_user_init( void)
  160. {
  161. #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
  162. long size = FIXADDR_USER_END - FIXADDR_USER_START;
  163. pgd_t *pgd;
  164. pud_t *pud;
  165. pmd_t *pmd;
  166. pte_t *pte;
  167. phys_t p;
  168. unsigned long v, vaddr = FIXADDR_USER_START;
  169. if (!size)
  170. return;
  171. fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir);
  172. v = (unsigned long) alloc_bootmem_low_pages(size);
  173. memcpy((void *) v , (void *) FIXADDR_USER_START, size);
  174. p = __pa(v);
  175. for ( ; size > 0; size -= PAGE_SIZE, vaddr += PAGE_SIZE,
  176. p += PAGE_SIZE) {
  177. pgd = swapper_pg_dir + pgd_index(vaddr);
  178. pud = pud_offset(pgd, vaddr);
  179. pmd = pmd_offset(pud, vaddr);
  180. pte = pte_offset_kernel(pmd, vaddr);
  181. pte_set_val(*pte, p, PAGE_READONLY);
  182. }
  183. #endif
  184. }
  185. void __init paging_init(void)
  186. {
  187. unsigned long zones_size[MAX_NR_ZONES], vaddr;
  188. int i;
  189. empty_zero_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
  190. empty_bad_page = (unsigned long *) alloc_bootmem_low_pages(PAGE_SIZE);
  191. for (i = 0; i < ARRAY_SIZE(zones_size); i++)
  192. zones_size[i] = 0;
  193. zones_size[ZONE_NORMAL] = (end_iomem >> PAGE_SHIFT) -
  194. (uml_physmem >> PAGE_SHIFT);
  195. #ifdef CONFIG_HIGHMEM
  196. zones_size[ZONE_HIGHMEM] = highmem >> PAGE_SHIFT;
  197. #endif
  198. free_area_init(zones_size);
  199. /*
  200. * Fixed mappings, only the page table structure has to be
  201. * created - mappings will be set by set_fixmap():
  202. */
  203. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  204. fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir);
  205. fixaddr_user_init();
  206. #ifdef CONFIG_HIGHMEM
  207. init_highmem();
  208. #endif
  209. }
  210. /*
  211. * This can't do anything because nothing in the kernel image can be freed
  212. * since it's not in kernel physical memory.
  213. */
  214. void free_initmem(void)
  215. {
  216. }
  217. #ifdef CONFIG_BLK_DEV_INITRD
  218. void free_initrd_mem(unsigned long start, unsigned long end)
  219. {
  220. if (start < end)
  221. printk(KERN_INFO "Freeing initrd memory: %ldk freed\n",
  222. (end - start) >> 10);
  223. for (; start < end; start += PAGE_SIZE) {
  224. ClearPageReserved(virt_to_page(start));
  225. init_page_count(virt_to_page(start));
  226. free_page(start);
  227. totalram_pages++;
  228. }
  229. }
  230. #endif
  231. /* Allocate and free page tables. */
  232. pgd_t *pgd_alloc(struct mm_struct *mm)
  233. {
  234. pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL);
  235. if (pgd) {
  236. memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
  237. memcpy(pgd + USER_PTRS_PER_PGD,
  238. swapper_pg_dir + USER_PTRS_PER_PGD,
  239. (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
  240. }
  241. return pgd;
  242. }
  243. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  244. {
  245. free_page((unsigned long) pgd);
  246. }
  247. pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  248. {
  249. pte_t *pte;
  250. pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
  251. return pte;
  252. }
  253. pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
  254. {
  255. struct page *pte;
  256. pte = alloc_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
  257. if (pte)
  258. pgtable_page_ctor(pte);
  259. return pte;
  260. }
  261. #ifdef CONFIG_3_LEVEL_PGTABLES
  262. pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address)
  263. {
  264. pmd_t *pmd = (pmd_t *) __get_free_page(GFP_KERNEL);
  265. if (pmd)
  266. memset(pmd, 0, PAGE_SIZE);
  267. return pmd;
  268. }
  269. #endif
  270. void *uml_kmalloc(int size, int flags)
  271. {
  272. return kmalloc(size, flags);
  273. }